]>
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 analog; | |
29 | struct sr_analog_encoding encoding; | |
30 | struct sr_analog_meaning meaning; | |
31 | struct sr_analog_spec spec; | |
32 | ||
33 | devc = sdi->priv; | |
34 | ||
35 | /* TODO: Use proper 'digits' value for this device (and its modes). */ | |
36 | sr_analog_init(&analog, &encoding, &meaning, &spec, 2); | |
37 | ||
38 | analog.num_samples = 1; | |
39 | analog.meaning->mq = 0; | |
40 | ||
41 | if (brymen_parse(buf, &floatval, &analog, NULL) != SR_OK) | |
42 | return; | |
43 | analog.data = &floatval; | |
44 | ||
45 | analog.meaning->channels = sdi->channels; | |
46 | ||
47 | if (analog.meaning->mq != 0) { | |
48 | /* Got a measurement. */ | |
49 | packet.type = SR_DF_ANALOG; | |
50 | packet.payload = &analog; | |
51 | sr_session_send(sdi, &packet); | |
52 | sr_sw_limits_update_samples_read(&devc->sw_limits, 1); | |
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; | |
60 | struct sr_serial_dev_inst *serial; | |
61 | ||
62 | devc = sdi->priv; | |
63 | serial = sdi->conn; | |
64 | ||
65 | /* Try to get as much data as the buffer can hold. */ | |
66 | len = DMM_BUFSIZE - devc->buflen; | |
67 | len = serial_read_nonblocking(serial, devc->buf + devc->buflen, len); | |
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) { | |
77 | /* We don't have a header, look for one. */ | |
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) { | |
82 | /* We know how large the packet will be. */ | |
83 | devc->next_packet_len = len; | |
84 | } else if (status == PACKET_NEED_MORE_DATA) { | |
85 | /* We didn't yet receive the full header. */ | |
86 | devc->next_packet_len = 0; | |
87 | break; | |
88 | } else { | |
89 | /* Invalid header. Move on. */ | |
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 | ||
100 | /* We should have a full packet here, so we can check it. */ | |
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 | } | |
107 | ||
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 | ||
117 | SR_PRIV int brymen_dmm_receive_data(int fd, int revents, void *cb_data) | |
118 | { | |
119 | struct sr_dev_inst *sdi; | |
120 | struct dev_context *devc; | |
121 | struct sr_serial_dev_inst *serial; | |
122 | int ret; | |
123 | ||
124 | (void)fd; | |
125 | ||
126 | if (!(sdi = cb_data)) | |
127 | return TRUE; | |
128 | ||
129 | if (!(devc = sdi->priv)) | |
130 | return TRUE; | |
131 | ||
132 | serial = sdi->conn; | |
133 | ||
134 | if (revents == G_IO_IN) { | |
135 | /* Serial data arrived. */ | |
136 | handle_new_data(sdi); | |
137 | } else { | |
138 | /* Timeout, send another packet request. */ | |
139 | if ((ret = brymen_packet_request(serial)) < 0) { | |
140 | sr_err("Failed to request packet: %d.", ret); | |
141 | return FALSE; | |
142 | } | |
143 | } | |
144 | ||
145 | if (sr_sw_limits_check(&devc->sw_limits)) | |
146 | sr_dev_acquisition_stop(sdi); | |
147 | ||
148 | return TRUE; | |
149 | } | |
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. | |
157 | * @param get_packet_size Callback that assesses the size of incoming packets. | |
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, | |
171 | packet_valid_callback is_valid, | |
172 | uint64_t timeout_ms, int baudrate) | |
173 | { | |
174 | int64_t start, time, byte_delay_us; | |
175 | size_t ibuf, i, maxlen; | |
176 | ssize_t len, stream_len; | |
177 | int packet_len; | |
178 | int status; | |
179 | ||
180 | maxlen = *buflen; | |
181 | ||
182 | sr_dbg("Detecting packets on %s (timeout = %" PRIu64 | |
183 | "ms, baudrate = %d).", serial->port, timeout_ms, baudrate); | |
184 | ||
185 | /* Assume 8n1 transmission. That is 10 bits for every byte. */ | |
186 | byte_delay_us = 10 * ((1000 * 1000) / baudrate); | |
187 | start = g_get_monotonic_time(); | |
188 | ||
189 | packet_len = i = ibuf = len = 0; | |
190 | while (ibuf < maxlen) { | |
191 | len = serial_read_nonblocking(serial, &buf[ibuf], maxlen - ibuf); | |
192 | if (len > 0) { | |
193 | ibuf += len; | |
194 | sr_spew("Read %zd bytes.", len); | |
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); | |
205 | switch (status) { | |
206 | case PACKET_HEADER_OK: | |
207 | /* We know how much data we need to wait for. */ | |
208 | break; | |
209 | case PACKET_NEED_MORE_DATA: | |
210 | /* We did not receive the full header. */ | |
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 | |
218 | * next byte. | |
219 | */ | |
220 | packet_len = 0; | |
221 | i++; | |
222 | break; | |
223 | } | |
224 | } | |
225 | ||
226 | if ((stream_len >= packet_len) && (packet_len != 0)) { | |
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 " | |
230 | "%" PRIu64 "ms.", packet_len, time); | |
231 | *buflen = ibuf; | |
232 | return SR_OK; | |
233 | } else { | |
234 | sr_spew("Got %d bytes, but not a valid " | |
235 | "packet.", packet_len); | |
236 | ||
237 | } | |
238 | ||
239 | /* Not a valid packet. Continue searching. */ | |
240 | i++; | |
241 | packet_len = 0; | |
242 | } | |
243 | ||
244 | if (time >= (int64_t)timeout_ms) { | |
245 | /* Timeout */ | |
246 | sr_dbg("Detection timed out after %" PRIi64 "ms.", time); | |
247 | break; | |
248 | } | |
249 | g_usleep(byte_delay_us); | |
250 | } | |
251 | ||
252 | *buflen = ibuf; | |
253 | sr_err("Didn't find a valid packet (read %zu bytes).", ibuf); | |
254 | ||
255 | return SR_ERR; | |
256 | } |