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