]> sigrok.org Git - libsigrok.git/blob - src/hardware/serial-dmm/protocol.c
dev_acquisition_{start,stop}(): Drop duplicate 'cb_data' parameter.
[libsigrok.git] / src / hardware / serial-dmm / protocol.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
5  * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
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
21 #include <config.h>
22 #include <stdlib.h>
23 #include <math.h>
24 #include <string.h>
25 #include <glib.h>
26 #include <libsigrok/libsigrok.h>
27 #include "libsigrok-internal.h"
28 #include "protocol.h"
29
30 static void log_dmm_packet(const uint8_t *buf)
31 {
32         sr_dbg("DMM packet: %02x %02x %02x %02x %02x %02x %02x "
33                "%02x %02x %02x %02x %02x %02x %02x %02x %02x "
34                "%02x %02x %02x %02x %02x %02x %02x",
35                buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6],
36                buf[7], buf[8], buf[9], buf[10], buf[11], buf[12], buf[13],
37                buf[14], buf[15], buf[16], buf[17], buf[18], buf[19], buf[20],
38                buf[21], buf[22]);
39 }
40
41 static void handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi,
42                           void *info)
43 {
44         struct dmm_info *dmm;
45         float floatval;
46         struct sr_datafeed_packet packet;
47         struct sr_datafeed_analog_old analog;
48         struct dev_context *devc;
49
50         dmm = (struct dmm_info *)sdi->driver;
51
52         log_dmm_packet(buf);
53         devc = sdi->priv;
54
55         memset(&analog, 0, sizeof(struct sr_datafeed_analog_old));
56
57         analog.channels = sdi->channels;
58         analog.num_samples = 1;
59         analog.mq = -1;
60
61         dmm->packet_parse(buf, &floatval, &analog, info);
62         analog.data = &floatval;
63
64         /* If this DMM needs additional handling, call the resp. function. */
65         if (dmm->dmm_details)
66                 dmm->dmm_details(&analog, info);
67
68         if (analog.mq != -1) {
69                 /* Got a measurement. */
70                 packet.type = SR_DF_ANALOG_OLD;
71                 packet.payload = &analog;
72                 sr_session_send(sdi, &packet);
73                 devc->num_samples++;
74         }
75 }
76
77 /** Request packet, if required. */
78 SR_PRIV int req_packet(struct sr_dev_inst *sdi)
79 {
80         struct dmm_info *dmm;
81         struct dev_context *devc;
82         struct sr_serial_dev_inst *serial;
83         int ret;
84
85         dmm = (struct dmm_info *)sdi->driver;
86
87         if (!dmm->packet_request)
88                 return SR_OK;
89
90         devc = sdi->priv;
91         serial = sdi->conn;
92
93         if (devc->req_next_at && (devc->req_next_at > g_get_monotonic_time())) {
94                 sr_spew("Not requesting new packet yet, %" PRIi64 " ms left.",
95                         ((devc->req_next_at - g_get_monotonic_time()) / 1000));
96                 return SR_OK;
97         }
98
99         ret = dmm->packet_request(serial);
100         if (ret < 0) {
101                 sr_err("Failed to request packet: %d.", ret);
102                 return ret;
103         }
104
105         if (dmm->req_timeout_ms)
106                 devc->req_next_at = g_get_monotonic_time() + (dmm->req_timeout_ms * 1000);
107
108         return SR_OK;
109 }
110
111 static void handle_new_data(struct sr_dev_inst *sdi, void *info)
112 {
113         struct dmm_info *dmm;
114         struct dev_context *devc;
115         int len, i, offset = 0;
116         struct sr_serial_dev_inst *serial;
117
118         dmm = (struct dmm_info *)sdi->driver;
119
120         devc = sdi->priv;
121         serial = sdi->conn;
122
123         /* Try to get as much data as the buffer can hold. */
124         len = DMM_BUFSIZE - devc->buflen;
125         len = serial_read_nonblocking(serial, devc->buf + devc->buflen, len);
126         if (len == 0)
127                 return; /* No new bytes, nothing to do. */
128         if (len < 0) {
129                 sr_err("Serial port read error: %d.", len);
130                 return;
131         }
132         devc->buflen += len;
133
134         /* Now look for packets in that data. */
135         while ((devc->buflen - offset) >= dmm->packet_size) {
136                 if (dmm->packet_valid(devc->buf + offset)) {
137                         handle_packet(devc->buf + offset, sdi, info);
138                         offset += dmm->packet_size;
139
140                         /* Request next packet, if required. */
141                         if (!dmm->packet_request)
142                                 break;
143                         if (dmm->req_timeout_ms || dmm->req_delay_ms)
144                                 devc->req_next_at = g_get_monotonic_time() +
145                                         dmm->req_delay_ms * 1000;
146                         req_packet(sdi);
147                 } else {
148                         offset++;
149                 }
150         }
151
152         /* If we have any data left, move it to the beginning of our buffer. */
153         for (i = 0; i < devc->buflen - offset; i++)
154                 devc->buf[i] = devc->buf[offset + i];
155         devc->buflen -= offset;
156 }
157
158 int receive_data(int fd, int revents, void *cb_data)
159 {
160         struct sr_dev_inst *sdi;
161         struct dev_context *devc;
162         struct dmm_info *dmm;
163         int64_t time;
164         void *info;
165
166         (void)fd;
167
168         if (!(sdi = cb_data))
169                 return TRUE;
170
171         if (!(devc = sdi->priv))
172                 return TRUE;
173
174         dmm = (struct dmm_info *)sdi->driver;
175
176         if (revents == G_IO_IN) {
177                 /* Serial data arrived. */
178                 info = g_malloc(dmm->info_size);
179                 handle_new_data(sdi, info);
180                 g_free(info);
181         } else {
182                 /* Timeout; send another packet request if DMM needs it. */
183                 if (dmm->packet_request && (req_packet(sdi) < 0))
184                         return FALSE;
185         }
186
187         if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
188                 sr_info("Requested number of samples reached.");
189                 sdi->driver->dev_acquisition_stop(sdi);
190                 return TRUE;
191         }
192
193         if (devc->limit_msec) {
194                 time = (g_get_monotonic_time() - devc->starttime) / 1000;
195                 if (time > (int64_t)devc->limit_msec) {
196                         sr_info("Requested time limit reached.");
197                         sdi->driver->dev_acquisition_stop(sdi);
198                         return TRUE;
199                 }
200         }
201
202         return TRUE;
203 }