]>
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 | ||
6ec6c43b | 20 | #include <config.h> |
20cbc785 AG |
21 | #include "protocol.h" |
22 | ||
601fb67c AG |
23 | static void handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi) |
24 | { | |
25 | float floatval; | |
26 | struct dev_context *devc; | |
27 | struct sr_datafeed_packet packet; | |
5faebab2 | 28 | struct sr_datafeed_analog_old analog; |
601fb67c AG |
29 | |
30 | devc = sdi->priv; | |
c5d6f5cc | 31 | |
601fb67c AG |
32 | analog.num_samples = 1; |
33 | analog.mq = -1; | |
34 | ||
933e63a1 BV |
35 | if (brymen_parse(buf, &floatval, &analog, NULL) != SR_OK) |
36 | return; | |
601fb67c AG |
37 | analog.data = &floatval; |
38 | ||
ba7dd8bb | 39 | analog.channels = sdi->channels; |
601fb67c AG |
40 | |
41 | if (analog.mq != -1) { | |
42 | /* Got a measurement. */ | |
5faebab2 | 43 | packet.type = SR_DF_ANALOG_OLD; |
601fb67c AG |
44 | packet.payload = &analog; |
45 | sr_session_send(devc->cb_data, &packet); | |
46 | devc->num_samples++; | |
47 | } | |
48 | } | |
49 | ||
50 | static void handle_new_data(struct sr_dev_inst *sdi) | |
51 | { | |
52 | struct dev_context *devc; | |
53 | int len, status, offset = 0; | |
d9a7c349 | 54 | struct sr_serial_dev_inst *serial; |
601fb67c AG |
55 | |
56 | devc = sdi->priv; | |
d9a7c349 | 57 | serial = sdi->conn; |
601fb67c AG |
58 | |
59 | /* Try to get as much data as the buffer can hold. */ | |
60 | len = DMM_BUFSIZE - devc->buflen; | |
44be13b1 | 61 | len = serial_read_nonblocking(serial, devc->buf + devc->buflen, len); |
601fb67c AG |
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; |
d9a7c349 | 115 | struct sr_serial_dev_inst *serial; |
601fb67c AG |
116 | int ret; |
117 | int64_t time; | |
118 | ||
119 | (void)fd; | |
20cbc785 AG |
120 | |
121 | if (!(sdi = cb_data)) | |
122 | return TRUE; | |
123 | ||
124 | if (!(devc = sdi->priv)) | |
125 | return TRUE; | |
126 | ||
d9a7c349 UH |
127 | serial = sdi->conn; |
128 | ||
20cbc785 | 129 | if (revents == G_IO_IN) { |
601fb67c AG |
130 | /* Serial data arrived. */ |
131 | handle_new_data(sdi); | |
132 | } else { | |
133 | /* Timeout, send another packet request. */ | |
d9a7c349 | 134 | if ((ret = brymen_packet_request(serial)) < 0) { |
601fb67c AG |
135 | sr_err("Failed to request packet: %d.", ret); |
136 | return FALSE; | |
137 | } | |
138 | } | |
139 | ||
140 | if (devc->limit_samples && devc->num_samples >= devc->limit_samples) { | |
141 | sr_info("Requested number of samples reached, stopping."); | |
142 | sdi->driver->dev_acquisition_stop(sdi, cb_data); | |
143 | return TRUE; | |
144 | } | |
145 | ||
146 | if (devc->limit_msec) { | |
147 | time = (g_get_monotonic_time() - devc->starttime) / 1000; | |
148 | if (time > (int64_t)devc->limit_msec) { | |
149 | sr_info("Requested time limit reached, stopping."); | |
150 | sdi->driver->dev_acquisition_stop(sdi, cb_data); | |
151 | return TRUE; | |
152 | } | |
20cbc785 AG |
153 | } |
154 | ||
155 | return TRUE; | |
156 | } | |
601fb67c AG |
157 | |
158 | /** | |
159 | * Try to find a valid packet in a serial data stream. | |
160 | * | |
161 | * @param serial Previously initialized serial port structure. | |
162 | * @param buf Buffer containing the bytes to write. | |
163 | * @param buflen Size of the buffer. | |
c5d6f5cc | 164 | * @param get_packet_size Callback that assesses the size of incoming packets. |
601fb67c AG |
165 | * @param is_valid Callback that assesses whether the packet is valid or not. |
166 | * @param timeout_ms The timeout after which, if no packet is detected, to | |
167 | * abort scanning. | |
168 | * @param baudrate The baudrate of the serial port. This parameter is not | |
169 | * critical, but it helps fine tune the serial port polling | |
170 | * delay. | |
171 | * | |
172 | * @return SR_OK if a valid packet is found within the given timeout, | |
173 | * SR_ERR upon failure. | |
174 | */ | |
175 | SR_PRIV int brymen_stream_detect(struct sr_serial_dev_inst *serial, | |
176 | uint8_t *buf, size_t *buflen, | |
177 | packet_length_t get_packet_size, | |
144f6660 | 178 | packet_valid_callback is_valid, |
601fb67c AG |
179 | uint64_t timeout_ms, int baudrate) |
180 | { | |
181 | int64_t start, time, byte_delay_us; | |
182 | size_t ibuf, i, maxlen; | |
6433156c DE |
183 | ssize_t len, stream_len; |
184 | int packet_len; | |
185 | int status; | |
601fb67c AG |
186 | |
187 | maxlen = *buflen; | |
188 | ||
af473e0e ML |
189 | sr_dbg("Detecting packets on %s (timeout = %" PRIu64 |
190 | "ms, baudrate = %d).", serial->port, timeout_ms, baudrate); | |
601fb67c AG |
191 | |
192 | /* Assume 8n1 transmission. That is 10 bits for every byte. */ | |
1a46cc62 | 193 | byte_delay_us = 10 * ((1000 * 1000) / baudrate); |
601fb67c AG |
194 | start = g_get_monotonic_time(); |
195 | ||
196 | packet_len = i = ibuf = len = 0; | |
197 | while (ibuf < maxlen) { | |
44be13b1 | 198 | len = serial_read_nonblocking(serial, &buf[ibuf], maxlen - ibuf); |
601fb67c AG |
199 | if (len > 0) { |
200 | ibuf += len; | |
6433156c | 201 | sr_spew("Read %zd bytes.", len); |
601fb67c AG |
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); | |
0c5f2abc | 212 | switch (status) { |
601fb67c | 213 | case PACKET_HEADER_OK: |
c5d6f5cc | 214 | /* We know how much data we need to wait for. */ |
601fb67c AG |
215 | break; |
216 | case PACKET_NEED_MORE_DATA: | |
c5d6f5cc | 217 | /* We did not receive the full header. */ |
601fb67c AG |
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 | |
c5d6f5cc | 225 | * next byte. |
601fb67c AG |
226 | */ |
227 | packet_len = 0; | |
228 | i++; | |
229 | break; | |
230 | } | |
231 | } | |
232 | ||
c5d6f5cc | 233 | if ((stream_len >= packet_len) && (packet_len != 0)) { |
601fb67c AG |
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 " | |
c5d6f5cc | 237 | "%" PRIu64 "ms.", packet_len, time); |
601fb67c AG |
238 | *buflen = ibuf; |
239 | return SR_OK; | |
240 | } else { | |
241 | sr_spew("Got %d bytes, but not a valid " | |
c5d6f5cc | 242 | "packet.", packet_len); |
601fb67c AG |
243 | |
244 | } | |
c5d6f5cc | 245 | |
601fb67c AG |
246 | /* Not a valid packet. Continue searching. */ |
247 | i++; | |
248 | packet_len = 0; | |
249 | } | |
c5d6f5cc | 250 | |
601fb67c AG |
251 | if (time >= (int64_t)timeout_ms) { |
252 | /* Timeout */ | |
6433156c | 253 | sr_dbg("Detection timed out after %" PRIi64 "ms.", time); |
601fb67c AG |
254 | break; |
255 | } | |
256 | g_usleep(byte_delay_us); | |
257 | } | |
258 | ||
259 | *buflen = ibuf; | |
6433156c | 260 | sr_err("Didn't find a valid packet (read %zu bytes).", ibuf); |
601fb67c AG |
261 | |
262 | return SR_ERR; | |
263 | } |