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