]>
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 <config.h> | |
21 | #include "protocol.h" | |
22 | ||
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; | |
28 | struct sr_datafeed_analog_old analog; | |
29 | ||
30 | devc = sdi->priv; | |
31 | ||
32 | analog.num_samples = 1; | |
33 | analog.mq = -1; | |
34 | ||
35 | if (brymen_parse(buf, &floatval, &analog, NULL) != SR_OK) | |
36 | return; | |
37 | analog.data = &floatval; | |
38 | ||
39 | analog.channels = sdi->channels; | |
40 | ||
41 | if (analog.mq != -1) { | |
42 | /* Got a measurement. */ | |
43 | packet.type = SR_DF_ANALOG_OLD; | |
44 | packet.payload = &analog; | |
45 | sr_session_send(sdi, &packet); | |
46 | sr_sw_limits_update_samples_read(&devc->sw_limits, 1); | |
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; | |
54 | struct sr_serial_dev_inst *serial; | |
55 | ||
56 | devc = sdi->priv; | |
57 | serial = sdi->conn; | |
58 | ||
59 | /* Try to get as much data as the buffer can hold. */ | |
60 | len = DMM_BUFSIZE - devc->buflen; | |
61 | len = serial_read_nonblocking(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) { | |
71 | /* We don't have a header, look for one. */ | |
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) { | |
76 | /* We know how large the packet will be. */ | |
77 | devc->next_packet_len = len; | |
78 | } else if (status == PACKET_NEED_MORE_DATA) { | |
79 | /* We didn't yet receive the full header. */ | |
80 | devc->next_packet_len = 0; | |
81 | break; | |
82 | } else { | |
83 | /* Invalid header. Move on. */ | |
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 | ||
94 | /* We should have a full packet here, so we can check it. */ | |
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 | } | |
101 | ||
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 | ||
111 | SR_PRIV int brymen_dmm_receive_data(int fd, int revents, void *cb_data) | |
112 | { | |
113 | struct sr_dev_inst *sdi; | |
114 | struct dev_context *devc; | |
115 | struct sr_serial_dev_inst *serial; | |
116 | int ret; | |
117 | ||
118 | (void)fd; | |
119 | ||
120 | if (!(sdi = cb_data)) | |
121 | return TRUE; | |
122 | ||
123 | if (!(devc = sdi->priv)) | |
124 | return TRUE; | |
125 | ||
126 | serial = sdi->conn; | |
127 | ||
128 | if (revents == G_IO_IN) { | |
129 | /* Serial data arrived. */ | |
130 | handle_new_data(sdi); | |
131 | } else { | |
132 | /* Timeout, send another packet request. */ | |
133 | if ((ret = brymen_packet_request(serial)) < 0) { | |
134 | sr_err("Failed to request packet: %d.", ret); | |
135 | return FALSE; | |
136 | } | |
137 | } | |
138 | ||
139 | if (sr_sw_limits_check(&devc->sw_limits)) | |
140 | sdi->driver->dev_acquisition_stop(sdi); | |
141 | ||
142 | return TRUE; | |
143 | } | |
144 | ||
145 | /** | |
146 | * Try to find a valid packet in a serial data stream. | |
147 | * | |
148 | * @param serial Previously initialized serial port structure. | |
149 | * @param buf Buffer containing the bytes to write. | |
150 | * @param buflen Size of the buffer. | |
151 | * @param get_packet_size Callback that assesses the size of incoming packets. | |
152 | * @param is_valid Callback that assesses whether the packet is valid or not. | |
153 | * @param timeout_ms The timeout after which, if no packet is detected, to | |
154 | * abort scanning. | |
155 | * @param baudrate The baudrate of the serial port. This parameter is not | |
156 | * critical, but it helps fine tune the serial port polling | |
157 | * delay. | |
158 | * | |
159 | * @return SR_OK if a valid packet is found within the given timeout, | |
160 | * SR_ERR upon failure. | |
161 | */ | |
162 | SR_PRIV int brymen_stream_detect(struct sr_serial_dev_inst *serial, | |
163 | uint8_t *buf, size_t *buflen, | |
164 | packet_length_t get_packet_size, | |
165 | packet_valid_callback is_valid, | |
166 | uint64_t timeout_ms, int baudrate) | |
167 | { | |
168 | int64_t start, time, byte_delay_us; | |
169 | size_t ibuf, i, maxlen; | |
170 | ssize_t len, stream_len; | |
171 | int packet_len; | |
172 | int status; | |
173 | ||
174 | maxlen = *buflen; | |
175 | ||
176 | sr_dbg("Detecting packets on %s (timeout = %" PRIu64 | |
177 | "ms, baudrate = %d).", serial->port, timeout_ms, baudrate); | |
178 | ||
179 | /* Assume 8n1 transmission. That is 10 bits for every byte. */ | |
180 | byte_delay_us = 10 * ((1000 * 1000) / baudrate); | |
181 | start = g_get_monotonic_time(); | |
182 | ||
183 | packet_len = i = ibuf = len = 0; | |
184 | while (ibuf < maxlen) { | |
185 | len = serial_read_nonblocking(serial, &buf[ibuf], maxlen - ibuf); | |
186 | if (len > 0) { | |
187 | ibuf += len; | |
188 | sr_spew("Read %zd bytes.", len); | |
189 | } | |
190 | ||
191 | time = g_get_monotonic_time() - start; | |
192 | time /= 1000; | |
193 | ||
194 | stream_len = ibuf - i; | |
195 | if (stream_len > 0 && packet_len == 0) { | |
196 | /* How large of a packet are we expecting? */ | |
197 | packet_len = stream_len; | |
198 | status = get_packet_size(&buf[i], &packet_len); | |
199 | switch (status) { | |
200 | case PACKET_HEADER_OK: | |
201 | /* We know how much data we need to wait for. */ | |
202 | break; | |
203 | case PACKET_NEED_MORE_DATA: | |
204 | /* We did not receive the full header. */ | |
205 | packet_len = 0; | |
206 | break; | |
207 | case PACKET_INVALID_HEADER: | |
208 | default: | |
209 | /* | |
210 | * We had enough data, but here was an error in | |
211 | * parsing the header. Restart parsing from the | |
212 | * next byte. | |
213 | */ | |
214 | packet_len = 0; | |
215 | i++; | |
216 | break; | |
217 | } | |
218 | } | |
219 | ||
220 | if ((stream_len >= packet_len) && (packet_len != 0)) { | |
221 | /* We have at least a packet's worth of data. */ | |
222 | if (is_valid(&buf[i])) { | |
223 | sr_spew("Found valid %d-byte packet after " | |
224 | "%" PRIu64 "ms.", packet_len, time); | |
225 | *buflen = ibuf; | |
226 | return SR_OK; | |
227 | } else { | |
228 | sr_spew("Got %d bytes, but not a valid " | |
229 | "packet.", packet_len); | |
230 | ||
231 | } | |
232 | ||
233 | /* Not a valid packet. Continue searching. */ | |
234 | i++; | |
235 | packet_len = 0; | |
236 | } | |
237 | ||
238 | if (time >= (int64_t)timeout_ms) { | |
239 | /* Timeout */ | |
240 | sr_dbg("Detection timed out after %" PRIi64 "ms.", time); | |
241 | break; | |
242 | } | |
243 | g_usleep(byte_delay_us); | |
244 | } | |
245 | ||
246 | *buflen = ibuf; | |
247 | sr_err("Didn't find a valid packet (read %zu bytes).", ibuf); | |
248 | ||
249 | return SR_ERR; | |
250 | } |