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