]> sigrok.org Git - libsigrok.git/blob - hardware/brymen-dmm/protocol.c
brymen-dmm: Use sr_dev_inst to store connection handle.
[libsigrok.git] / hardware / brymen-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  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "protocol.h"
21
22 /* parser.c */
23 SR_PRIV int sr_brymen_parse(const uint8_t *buf, float *floatval,
24                             struct sr_datafeed_analog *analog, void *info);
25
26 static void handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi)
27 {
28         float floatval;
29         struct dev_context *devc;
30         struct sr_datafeed_packet packet;
31         struct sr_datafeed_analog analog;
32
33         devc = sdi->priv;
34
35         analog.num_samples = 1;
36         analog.mq = -1;
37
38         sr_brymen_parse(buf, &floatval, &analog, NULL);
39         analog.data = &floatval;
40
41         analog.probes = sdi->probes;
42
43         if (analog.mq != -1) {
44                 /* Got a measurement. */
45                 packet.type = SR_DF_ANALOG;
46                 packet.payload = &analog;
47                 sr_session_send(devc->cb_data, &packet);
48                 devc->num_samples++;
49         }
50 }
51
52 static void handle_new_data(struct sr_dev_inst *sdi)
53 {
54         struct dev_context *devc;
55         int len, status, offset = 0;
56         struct sr_serial_dev_inst *serial;
57
58         devc = sdi->priv;
59         serial = sdi->conn;
60
61         /* Try to get as much data as the buffer can hold. */
62         len = DMM_BUFSIZE - devc->buflen;
63         len = serial_read(serial, devc->buf + devc->buflen, len);
64         if (len < 1) {
65                 sr_err("Serial port read error: %d.", len);
66                 return;
67         }
68         devc->buflen += len;
69         status = PACKET_INVALID_HEADER;
70
71         /* Now look for packets in that data. */
72         while (status != PACKET_NEED_MORE_DATA) {
73                 /* We don't have a header, look for one. */
74                 if (devc->next_packet_len == 0) {
75                         len = devc->buflen - offset;
76                         status = brymen_packet_length(devc->buf + offset, &len);
77                         if (status == PACKET_HEADER_OK) {
78                                 /* We know how large the packet will be. */
79                                 devc->next_packet_len = len;
80                         } else if (status == PACKET_NEED_MORE_DATA) {
81                                 /* We didn't yet receive the full header. */
82                                 devc->next_packet_len = 0;
83                                 break;
84                         } else {
85                                 /* Invalid header. Move on. */
86                                 devc->next_packet_len = 0;
87                                 offset++;
88                                 continue;
89                         }
90                 }
91
92                 /* We know how the packet size, but did we receive all of it? */
93                 if (devc->buflen - offset < devc->next_packet_len)
94                         break;
95
96                 /* We should have a full packet here, so we can check it. */
97                 if (brymen_packet_is_valid(devc->buf + offset)) {
98                         handle_packet(devc->buf + offset, sdi);
99                         offset += devc->next_packet_len;
100                 } else {
101                         offset++;
102                 }
103
104                 /* We are done with this packet. Look for a new one. */
105                 devc->next_packet_len = 0;
106         }
107
108         /* If we have any data left, move it to the beginning of our buffer. */
109         memmove(devc->buf, devc->buf + offset, devc->buflen - offset);
110         devc->buflen -= offset;
111 }
112
113 SR_PRIV int brymen_dmm_receive_data(int fd, int revents, void *cb_data)
114 {
115         struct sr_dev_inst *sdi;
116         struct dev_context *devc;
117         struct sr_serial_dev_inst *serial;
118         int ret;
119         int64_t time;
120
121         (void)fd;
122
123         if (!(sdi = cb_data))
124                 return TRUE;
125
126         if (!(devc = sdi->priv))
127                 return TRUE;
128
129         serial = sdi->conn;
130
131         if (revents == G_IO_IN) {
132                 /* Serial data arrived. */
133                 handle_new_data(sdi);
134         } else {
135                 /* Timeout, send another packet request. */
136                 if ((ret = brymen_packet_request(serial)) < 0) {
137                         sr_err("Failed to request packet: %d.", ret);
138                         return FALSE;
139                 }
140         }
141
142         if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
143                 sr_info("Requested number of samples reached, stopping.");
144                 sdi->driver->dev_acquisition_stop(sdi, cb_data);
145                 return TRUE;
146         }
147
148         if (devc->limit_msec) {
149                 time = (g_get_monotonic_time() - devc->starttime) / 1000;
150                 if (time > (int64_t)devc->limit_msec) {
151                         sr_info("Requested time limit reached, stopping.");
152                         sdi->driver->dev_acquisition_stop(sdi, cb_data);
153                         return TRUE;
154                 }
155         }
156
157         return TRUE;
158 }
159
160 /**
161  * Try to find a valid packet in a serial data stream.
162  *
163  * @param serial Previously initialized serial port structure.
164  * @param buf Buffer containing the bytes to write.
165  * @param buflen Size of the buffer.
166  * @param get_packet_size Callback that assesses the size of incoming packets.
167  * @param is_valid Callback that assesses whether the packet is valid or not.
168  * @param timeout_ms The timeout after which, if no packet is detected, to
169  *                   abort scanning.
170  * @param baudrate The baudrate of the serial port. This parameter is not
171  *                 critical, but it helps fine tune the serial port polling
172  *                 delay.
173  *
174  * @return SR_OK if a valid packet is found within the given timeout,
175  *         SR_ERR upon failure.
176  */
177 SR_PRIV int brymen_stream_detect(struct sr_serial_dev_inst *serial,
178                                 uint8_t *buf, size_t *buflen,
179                                 packet_length_t get_packet_size,
180                                 packet_valid_t is_valid,
181                                 uint64_t timeout_ms, int baudrate)
182 {
183         int64_t start, time, byte_delay_us;
184         size_t ibuf, i, maxlen;
185         int status, len, packet_len, stream_len;
186
187         maxlen = *buflen;
188
189         sr_dbg("Detecting packets on FD %d (timeout = %" PRIu64
190                "ms, baudrate = %d).", serial->fd, timeout_ms, baudrate);
191
192         /* Assume 8n1 transmission. That is 10 bits for every byte. */
193         byte_delay_us = 10 * (1000000 / baudrate);
194         start = g_get_monotonic_time();
195
196         packet_len = i = ibuf = len = 0;
197         while (ibuf < maxlen) {
198                 len = serial_read(serial, &buf[ibuf], maxlen - ibuf);
199                 if (len > 0) {
200                         ibuf += len;
201                         sr_spew("Read %d bytes.", len);
202                 }
203
204                 time = g_get_monotonic_time() - start;
205                 time /= 1000;
206
207                 stream_len = ibuf - i;
208                 if (stream_len > 0 && packet_len == 0) {
209                         /* How large of a packet are we expecting? */
210                         packet_len = stream_len;
211                         status = get_packet_size(&buf[i], &packet_len);
212                         switch(status) {
213                         case PACKET_HEADER_OK:
214                                 /* We know how much data we need to wait for. */
215                                 break;
216                         case PACKET_NEED_MORE_DATA:
217                                 /* We did not receive the full header. */
218                                 packet_len = 0;
219                                 break;
220                         case PACKET_INVALID_HEADER:
221                         default:
222                                 /*
223                                  * We had enough data, but here was an error in
224                                  * parsing the header. Restart parsing from the
225                                  * next byte.
226                                  */
227                                 packet_len = 0;
228                                 i++;
229                                 break;
230                         }
231                 }
232
233                 if ((stream_len >= packet_len) && (packet_len != 0)) {
234                         /* We have at least a packet's worth of data. */
235                         if (is_valid(&buf[i])) {
236                                 sr_spew("Found valid %d-byte packet after "
237                                         "%" PRIu64 "ms.", packet_len, time);
238                                 *buflen = ibuf;
239                                 return SR_OK;
240                         } else {
241                                 sr_spew("Got %d bytes, but not a valid "
242                                         "packet.", packet_len);
243
244                         }
245
246                         /* Not a valid packet. Continue searching. */
247                         i++;
248                         packet_len = 0;
249                 }
250
251                 if (time >= (int64_t)timeout_ms) {
252                         /* Timeout */
253                         sr_dbg("Detection timed out after %dms.", time);
254                         break;
255                 }
256                 g_usleep(byte_delay_us);
257         }
258
259         *buflen = ibuf;
260         sr_err("Didn't find a valid packet (read %d bytes).", ibuf);
261
262         return SR_ERR;
263 }