]>
Commit | Line | Data |
---|---|---|
20cbc785 AG |
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 | ||
20cbc785 AG |
20 | #include "protocol.h" |
21 | ||
c5d6f5cc | 22 | /* parser.c */ |
601fb67c AG |
23 | SR_PRIV int sr_brymen_parse(const uint8_t *buf, float *floatval, |
24 | struct sr_datafeed_analog *analog, void *info); | |
25 | ||
601fb67c AG |
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; | |
c5d6f5cc | 34 | |
601fb67c AG |
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 | ||
57 | devc = sdi->priv; | |
58 | ||
59 | /* Try to get as much data as the buffer can hold. */ | |
60 | len = DMM_BUFSIZE - devc->buflen; | |
61 | len = serial_read(devc->serial, devc->buf + devc->buflen, len); | |
62 | if (len < 1) { | |
63 | sr_err("Serial port read error: %d.", len); | |
64 | return; | |
65 | } | |
66 | devc->buflen += len; | |
67 | status = PACKET_INVALID_HEADER; | |
68 | ||
69 | /* Now look for packets in that data. */ | |
70 | while (status != PACKET_NEED_MORE_DATA) { | |
c5d6f5cc | 71 | /* We don't have a header, look for one. */ |
601fb67c AG |
72 | if (devc->next_packet_len == 0) { |
73 | len = devc->buflen - offset; | |
74 | status = brymen_packet_length(devc->buf + offset, &len); | |
75 | if (status == PACKET_HEADER_OK) { | |
c5d6f5cc | 76 | /* We know how large the packet will be. */ |
601fb67c AG |
77 | devc->next_packet_len = len; |
78 | } else if (status == PACKET_NEED_MORE_DATA) { | |
c5d6f5cc | 79 | /* We didn't yet receive the full header. */ |
601fb67c AG |
80 | devc->next_packet_len = 0; |
81 | break; | |
82 | } else { | |
c5d6f5cc | 83 | /* Invalid header. Move on. */ |
601fb67c AG |
84 | devc->next_packet_len = 0; |
85 | offset++; | |
86 | continue; | |
87 | } | |
88 | } | |
89 | ||
90 | /* We know how the packet size, but did we receive all of it? */ | |
91 | if (devc->buflen - offset < devc->next_packet_len) | |
92 | break; | |
93 | ||
c5d6f5cc | 94 | /* We should have a full packet here, so we can check it. */ |
601fb67c AG |
95 | if (brymen_packet_is_valid(devc->buf + offset)) { |
96 | handle_packet(devc->buf + offset, sdi); | |
97 | offset += devc->next_packet_len; | |
98 | } else { | |
99 | offset++; | |
100 | } | |
c5d6f5cc | 101 | |
601fb67c AG |
102 | /* We are done with this packet. Look for a new one. */ |
103 | devc->next_packet_len = 0; | |
104 | } | |
105 | ||
106 | /* If we have any data left, move it to the beginning of our buffer. */ | |
107 | memmove(devc->buf, devc->buf + offset, devc->buflen - offset); | |
108 | devc->buflen -= offset; | |
109 | } | |
110 | ||
20cbc785 AG |
111 | SR_PRIV int brymen_dmm_receive_data(int fd, int revents, void *cb_data) |
112 | { | |
601fb67c | 113 | struct sr_dev_inst *sdi; |
20cbc785 | 114 | struct dev_context *devc; |
601fb67c AG |
115 | int ret; |
116 | int64_t time; | |
117 | ||
118 | (void)fd; | |
20cbc785 AG |
119 | |
120 | if (!(sdi = cb_data)) | |
121 | return TRUE; | |
122 | ||
123 | if (!(devc = sdi->priv)) | |
124 | return TRUE; | |
125 | ||
126 | if (revents == G_IO_IN) { | |
601fb67c AG |
127 | /* Serial data arrived. */ |
128 | handle_new_data(sdi); | |
129 | } else { | |
130 | /* Timeout, send another packet request. */ | |
c5d6f5cc | 131 | if ((ret = brymen_packet_request(devc->serial)) < 0) { |
601fb67c AG |
132 | sr_err("Failed to request packet: %d.", ret); |
133 | return FALSE; | |
134 | } | |
135 | } | |
136 | ||
137 | if (devc->limit_samples && devc->num_samples >= devc->limit_samples) { | |
138 | sr_info("Requested number of samples reached, stopping."); | |
139 | sdi->driver->dev_acquisition_stop(sdi, cb_data); | |
140 | return TRUE; | |
141 | } | |
142 | ||
143 | if (devc->limit_msec) { | |
144 | time = (g_get_monotonic_time() - devc->starttime) / 1000; | |
145 | if (time > (int64_t)devc->limit_msec) { | |
146 | sr_info("Requested time limit reached, stopping."); | |
147 | sdi->driver->dev_acquisition_stop(sdi, cb_data); | |
148 | return TRUE; | |
149 | } | |
20cbc785 AG |
150 | } |
151 | ||
152 | return TRUE; | |
153 | } | |
601fb67c AG |
154 | |
155 | /** | |
156 | * Try to find a valid packet in a serial data stream. | |
157 | * | |
158 | * @param serial Previously initialized serial port structure. | |
159 | * @param buf Buffer containing the bytes to write. | |
160 | * @param buflen Size of the buffer. | |
c5d6f5cc | 161 | * @param get_packet_size Callback that assesses the size of incoming packets. |
601fb67c AG |
162 | * @param is_valid Callback that assesses whether the packet is valid or not. |
163 | * @param timeout_ms The timeout after which, if no packet is detected, to | |
164 | * abort scanning. | |
165 | * @param baudrate The baudrate of the serial port. This parameter is not | |
166 | * critical, but it helps fine tune the serial port polling | |
167 | * delay. | |
168 | * | |
169 | * @return SR_OK if a valid packet is found within the given timeout, | |
170 | * SR_ERR upon failure. | |
171 | */ | |
172 | SR_PRIV int brymen_stream_detect(struct sr_serial_dev_inst *serial, | |
173 | uint8_t *buf, size_t *buflen, | |
174 | packet_length_t get_packet_size, | |
175 | packet_valid_t is_valid, | |
176 | uint64_t timeout_ms, int baudrate) | |
177 | { | |
178 | int64_t start, time, byte_delay_us; | |
179 | size_t ibuf, i, maxlen; | |
180 | int status, len, packet_len, stream_len; | |
181 | ||
182 | maxlen = *buflen; | |
183 | ||
184 | sr_dbg("Detecting packets on FD %d (timeout = %" PRIu64 | |
c5d6f5cc | 185 | "ms, baudrate = %d).", serial->fd, timeout_ms, baudrate); |
601fb67c AG |
186 | |
187 | /* Assume 8n1 transmission. That is 10 bits for every byte. */ | |
188 | byte_delay_us = 10 * (1000000 / baudrate); | |
189 | start = g_get_monotonic_time(); | |
190 | ||
191 | packet_len = i = ibuf = len = 0; | |
192 | while (ibuf < maxlen) { | |
193 | len = serial_read(serial, &buf[ibuf], maxlen - ibuf); | |
194 | if (len > 0) { | |
195 | ibuf += len; | |
c5d6f5cc | 196 | sr_spew("Read %d bytes.", len); |
601fb67c AG |
197 | } |
198 | ||
199 | time = g_get_monotonic_time() - start; | |
200 | time /= 1000; | |
201 | ||
202 | stream_len = ibuf - i; | |
203 | if (stream_len > 0 && packet_len == 0) { | |
204 | /* How large of a packet are we expecting? */ | |
205 | packet_len = stream_len; | |
206 | status = get_packet_size(&buf[i], &packet_len); | |
207 | switch(status) { | |
208 | case PACKET_HEADER_OK: | |
c5d6f5cc | 209 | /* We know how much data we need to wait for. */ |
601fb67c AG |
210 | break; |
211 | case PACKET_NEED_MORE_DATA: | |
c5d6f5cc | 212 | /* We did not receive the full header. */ |
601fb67c AG |
213 | packet_len = 0; |
214 | break; | |
215 | case PACKET_INVALID_HEADER: | |
216 | default: | |
217 | /* | |
218 | * We had enough data, but here was an error in | |
219 | * parsing the header. Restart parsing from the | |
c5d6f5cc | 220 | * next byte. |
601fb67c AG |
221 | */ |
222 | packet_len = 0; | |
223 | i++; | |
224 | break; | |
225 | } | |
226 | } | |
227 | ||
c5d6f5cc | 228 | if ((stream_len >= packet_len) && (packet_len != 0)) { |
601fb67c AG |
229 | /* We have at least a packet's worth of data. */ |
230 | if (is_valid(&buf[i])) { | |
231 | sr_spew("Found valid %d-byte packet after " | |
c5d6f5cc | 232 | "%" PRIu64 "ms.", packet_len, time); |
601fb67c AG |
233 | *buflen = ibuf; |
234 | return SR_OK; | |
235 | } else { | |
236 | sr_spew("Got %d bytes, but not a valid " | |
c5d6f5cc | 237 | "packet.", packet_len); |
601fb67c AG |
238 | |
239 | } | |
c5d6f5cc | 240 | |
601fb67c AG |
241 | /* Not a valid packet. Continue searching. */ |
242 | i++; | |
243 | packet_len = 0; | |
244 | } | |
c5d6f5cc | 245 | |
601fb67c AG |
246 | if (time >= (int64_t)timeout_ms) { |
247 | /* Timeout */ | |
248 | sr_dbg("Detection timed out after %dms.", time); | |
249 | break; | |
250 | } | |
251 | g_usleep(byte_delay_us); | |
252 | } | |
253 | ||
254 | *buflen = ibuf; | |
255 | sr_err("Didn't find a valid packet (read %d bytes).", ibuf); | |
256 | ||
257 | return SR_ERR; | |
258 | } |