]> sigrok.org Git - libsigrok.git/blame - src/hardware/serial-dmm/protocol.c
Build: Set local include directories in Makefile.am
[libsigrok.git] / src / hardware / serial-dmm / protocol.c
CommitLineData
7dc55d93 1/*
50985c20 2 * This file is part of the libsigrok project.
7dc55d93
AG
3 *
4 * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
729b01f9 5 * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
7dc55d93
AG
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
7dc55d93
AG
21#include <stdlib.h>
22#include <math.h>
23#include <string.h>
22f54192 24#include <glib.h>
c1aae900 25#include <libsigrok/libsigrok.h>
bbabddbd
UH
26#include "libsigrok-internal.h"
27#include "protocol.h"
7dc55d93 28
6bef68a7
UH
29static void log_dmm_packet(const uint8_t *buf)
30{
c36f78f7
UH
31 sr_dbg("DMM packet: %02x %02x %02x %02x %02x %02x %02x "
32 "%02x %02x %02x %02x %02x %02x %02x %02x %02x "
33 "%02x %02x %02x %02x %02x %02x %02x",
6bef68a7 34 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6],
c36f78f7
UH
35 buf[7], buf[8], buf[9], buf[10], buf[11], buf[12], buf[13],
36 buf[14], buf[15], buf[16], buf[17], buf[18], buf[19], buf[20],
37 buf[21], buf[22]);
6bef68a7
UH
38}
39
69e19dd7 40static void handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi,
bcbef5ed 41 void *info)
7dc55d93 42{
1a863916 43 struct dmm_info *dmm;
8c1adf37 44 float floatval;
7dc55d93 45 struct sr_datafeed_packet packet;
7c8a9e1e 46 struct sr_datafeed_analog analog;
69e19dd7 47 struct dev_context *devc;
7dc55d93 48
1a863916
UH
49 dmm = (struct dmm_info *)sdi->driver;
50
6bef68a7 51 log_dmm_packet(buf);
69e19dd7 52 devc = sdi->priv;
6bef68a7 53
7c8a9e1e 54 memset(&analog, 0, sizeof(struct sr_datafeed_analog));
bbabddbd 55
ba7dd8bb 56 analog.channels = sdi->channels;
7c8a9e1e
UH
57 analog.num_samples = 1;
58 analog.mq = -1;
7dc55d93 59
bcbef5ed 60 dmm->packet_parse(buf, &floatval, &analog, info);
7c8a9e1e 61 analog.data = &floatval;
be5c1d3b 62
22f54192 63 /* If this DMM needs additional handling, call the resp. function. */
bcbef5ed
ML
64 if (dmm->dmm_details)
65 dmm->dmm_details(&analog, info);
7dc55d93 66
7c8a9e1e 67 if (analog.mq != -1) {
7dc55d93 68 /* Got a measurement. */
7dc55d93 69 packet.type = SR_DF_ANALOG;
7c8a9e1e 70 packet.payload = &analog;
7dc55d93
AG
71 sr_session_send(devc->cb_data, &packet);
72 devc->num_samples++;
73 }
7dc55d93
AG
74}
75
2baac44d 76/** Request packet, if required. */
bcbef5ed 77SR_PRIV int req_packet(struct sr_dev_inst *sdi)
2baac44d 78{
1a863916 79 struct dmm_info *dmm;
2baac44d
MH
80 struct dev_context *devc;
81 struct sr_serial_dev_inst *serial;
82 int ret;
83
1a863916
UH
84 dmm = (struct dmm_info *)sdi->driver;
85
bcbef5ed 86 if (!dmm->packet_request)
2baac44d
MH
87 return SR_OK;
88
89 devc = sdi->priv;
90 serial = sdi->conn;
91
92 if (devc->req_next_at && (devc->req_next_at > g_get_monotonic_time())) {
93 sr_spew("Not requesting new packet yet, %" PRIi64 " ms left.",
94 ((devc->req_next_at - g_get_monotonic_time()) / 1000));
95 return SR_OK;
96 }
97
bcbef5ed 98 ret = dmm->packet_request(serial);
2baac44d
MH
99 if (ret < 0) {
100 sr_err("Failed to request packet: %d.", ret);
101 return ret;
102 }
103
bcbef5ed
ML
104 if (dmm->req_timeout_ms)
105 devc->req_next_at = g_get_monotonic_time() + (dmm->req_timeout_ms * 1000);
2baac44d
MH
106
107 return SR_OK;
108}
109
bcbef5ed 110static void handle_new_data(struct sr_dev_inst *sdi, void *info)
7dc55d93 111{
1a863916 112 struct dmm_info *dmm;
69e19dd7 113 struct dev_context *devc;
bbabddbd 114 int len, i, offset = 0;
1e1bfcd0 115 struct sr_serial_dev_inst *serial;
bbabddbd 116
1a863916
UH
117 dmm = (struct dmm_info *)sdi->driver;
118
69e19dd7 119 devc = sdi->priv;
1e1bfcd0 120 serial = sdi->conn;
69e19dd7 121
bbabddbd 122 /* Try to get as much data as the buffer can hold. */
7dc55d93 123 len = DMM_BUFSIZE - devc->buflen;
4277ac34 124 len = serial_read_nonblocking(serial, devc->buf + devc->buflen, len);
66a43576
UH
125 if (len == 0)
126 return; /* No new bytes, nothing to do. */
127 if (len < 0) {
bbabddbd 128 sr_err("Serial port read error: %d.", len);
7dc55d93
AG
129 return;
130 }
131 devc->buflen += len;
132
bbabddbd 133 /* Now look for packets in that data. */
bcbef5ed
ML
134 while ((devc->buflen - offset) >= dmm->packet_size) {
135 if (dmm->packet_valid(devc->buf + offset)) {
136 handle_packet(devc->buf + offset, sdi, info);
137 offset += dmm->packet_size;
2baac44d
MH
138
139 /* Request next packet, if required. */
bcbef5ed 140 if (!dmm->packet_request)
2baac44d 141 break;
bcbef5ed 142 if (dmm->req_timeout_ms || dmm->req_delay_ms)
2baac44d 143 devc->req_next_at = g_get_monotonic_time() +
bcbef5ed
ML
144 dmm->req_delay_ms * 1000;
145 req_packet(sdi);
7dc55d93
AG
146 } else {
147 offset++;
148 }
149 }
150
bbabddbd
UH
151 /* If we have any data left, move it to the beginning of our buffer. */
152 for (i = 0; i < devc->buflen - offset; i++)
7dc55d93
AG
153 devc->buf[i] = devc->buf[offset + i];
154 devc->buflen -= offset;
155}
156
bcbef5ed 157int receive_data(int fd, int revents, void *cb_data)
7dc55d93 158{
642e9d62 159 struct sr_dev_inst *sdi;
7dc55d93 160 struct dev_context *devc;
bcbef5ed 161 struct dmm_info *dmm;
f9b9bd63 162 int64_t time;
bcbef5ed 163 void *info;
7dc55d93 164
d35afa87
BV
165 (void)fd;
166
7dc55d93
AG
167 if (!(sdi = cb_data))
168 return TRUE;
169
170 if (!(devc = sdi->priv))
171 return TRUE;
172
1a863916 173 dmm = (struct dmm_info *)sdi->driver;
bcbef5ed 174
bbabddbd 175 if (revents == G_IO_IN) {
7dc55d93 176 /* Serial data arrived. */
145d794f 177 info = g_malloc(dmm->info_size);
bcbef5ed 178 handle_new_data(sdi, info);
145d794f 179 g_free(info);
ce3777ad 180 } else {
2baac44d 181 /* Timeout; send another packet request if DMM needs it. */
bcbef5ed 182 if (dmm->packet_request && (req_packet(sdi) < 0))
2baac44d 183 return FALSE;
7dc55d93
AG
184 }
185
d55c89f5 186 if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
35e199da 187 sr_info("Requested number of samples reached.");
7dc55d93
AG
188 sdi->driver->dev_acquisition_stop(sdi, cb_data);
189 return TRUE;
190 }
191
f9b9bd63
AG
192 if (devc->limit_msec) {
193 time = (g_get_monotonic_time() - devc->starttime) / 1000;
194 if (time > (int64_t)devc->limit_msec) {
35e199da 195 sr_info("Requested time limit reached.");
f9b9bd63
AG
196 sdi->driver->dev_acquisition_stop(sdi, cb_data);
197 return TRUE;
198 }
199 }
200
7dc55d93
AG
201 return TRUE;
202}