]> sigrok.org Git - libsigrok.git/blob - src/hardware/serial-dmm/protocol.c
Build: Set local include directories in Makefile.am
[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 <stdlib.h>
22 #include <math.h>
23 #include <string.h>
24 #include <glib.h>
25 #include <libsigrok/libsigrok.h>
26 #include "libsigrok-internal.h"
27 #include "protocol.h"
28
29 static void log_dmm_packet(const uint8_t *buf)
30 {
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",
34                buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6],
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]);
38 }
39
40 static void handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi,
41                           void *info)
42 {
43         struct dmm_info *dmm;
44         float floatval;
45         struct sr_datafeed_packet packet;
46         struct sr_datafeed_analog analog;
47         struct dev_context *devc;
48
49         dmm = (struct dmm_info *)sdi->driver;
50
51         log_dmm_packet(buf);
52         devc = sdi->priv;
53
54         memset(&analog, 0, sizeof(struct sr_datafeed_analog));
55
56         analog.channels = sdi->channels;
57         analog.num_samples = 1;
58         analog.mq = -1;
59
60         dmm->packet_parse(buf, &floatval, &analog, info);
61         analog.data = &floatval;
62
63         /* If this DMM needs additional handling, call the resp. function. */
64         if (dmm->dmm_details)
65                 dmm->dmm_details(&analog, info);
66
67         if (analog.mq != -1) {
68                 /* Got a measurement. */
69                 packet.type = SR_DF_ANALOG;
70                 packet.payload = &analog;
71                 sr_session_send(devc->cb_data, &packet);
72                 devc->num_samples++;
73         }
74 }
75
76 /** Request packet, if required. */
77 SR_PRIV int req_packet(struct sr_dev_inst *sdi)
78 {
79         struct dmm_info *dmm;
80         struct dev_context *devc;
81         struct sr_serial_dev_inst *serial;
82         int ret;
83
84         dmm = (struct dmm_info *)sdi->driver;
85
86         if (!dmm->packet_request)
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
98         ret = dmm->packet_request(serial);
99         if (ret < 0) {
100                 sr_err("Failed to request packet: %d.", ret);
101                 return ret;
102         }
103
104         if (dmm->req_timeout_ms)
105                 devc->req_next_at = g_get_monotonic_time() + (dmm->req_timeout_ms * 1000);
106
107         return SR_OK;
108 }
109
110 static void handle_new_data(struct sr_dev_inst *sdi, void *info)
111 {
112         struct dmm_info *dmm;
113         struct dev_context *devc;
114         int len, i, offset = 0;
115         struct sr_serial_dev_inst *serial;
116
117         dmm = (struct dmm_info *)sdi->driver;
118
119         devc = sdi->priv;
120         serial = sdi->conn;
121
122         /* Try to get as much data as the buffer can hold. */
123         len = DMM_BUFSIZE - devc->buflen;
124         len = serial_read_nonblocking(serial, devc->buf + devc->buflen, len);
125         if (len == 0)
126                 return; /* No new bytes, nothing to do. */
127         if (len < 0) {
128                 sr_err("Serial port read error: %d.", len);
129                 return;
130         }
131         devc->buflen += len;
132
133         /* Now look for packets in that data. */
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;
138
139                         /* Request next packet, if required. */
140                         if (!dmm->packet_request)
141                                 break;
142                         if (dmm->req_timeout_ms || dmm->req_delay_ms)
143                                 devc->req_next_at = g_get_monotonic_time() +
144                                         dmm->req_delay_ms * 1000;
145                         req_packet(sdi);
146                 } else {
147                         offset++;
148                 }
149         }
150
151         /* If we have any data left, move it to the beginning of our buffer. */
152         for (i = 0; i < devc->buflen - offset; i++)
153                 devc->buf[i] = devc->buf[offset + i];
154         devc->buflen -= offset;
155 }
156
157 int receive_data(int fd, int revents, void *cb_data)
158 {
159         struct sr_dev_inst *sdi;
160         struct dev_context *devc;
161         struct dmm_info *dmm;
162         int64_t time;
163         void *info;
164
165         (void)fd;
166
167         if (!(sdi = cb_data))
168                 return TRUE;
169
170         if (!(devc = sdi->priv))
171                 return TRUE;
172
173         dmm = (struct dmm_info *)sdi->driver;
174
175         if (revents == G_IO_IN) {
176                 /* Serial data arrived. */
177                 info = g_malloc(dmm->info_size);
178                 handle_new_data(sdi, info);
179                 g_free(info);
180         } else {
181                 /* Timeout; send another packet request if DMM needs it. */
182                 if (dmm->packet_request && (req_packet(sdi) < 0))
183                         return FALSE;
184         }
185
186         if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
187                 sr_info("Requested number of samples reached.");
188                 sdi->driver->dev_acquisition_stop(sdi, cb_data);
189                 return TRUE;
190         }
191
192         if (devc->limit_msec) {
193                 time = (g_get_monotonic_time() - devc->starttime) / 1000;
194                 if (time > (int64_t)devc->limit_msec) {
195                         sr_info("Requested time limit reached.");
196                         sdi->driver->dev_acquisition_stop(sdi, cb_data);
197                         return TRUE;
198                 }
199         }
200
201         return TRUE;
202 }