]>
Commit | Line | Data |
---|---|---|
a1bb33af | 1 | /* |
50985c20 | 2 | * This file is part of the libsigrok project. |
a1bb33af | 3 | * |
c73d2ea4 | 4 | * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com> |
b19f4622 | 5 | * Copyright (C) 2010-2012 Uwe Hermann <uwe@hermann-uwe.de> |
766456be | 6 | * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com> |
a1bb33af UH |
7 | * |
8 | * This program is free software: you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation, either version 3 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 | */ | |
21 | ||
54b38f64 | 22 | #include <string.h> |
d02a535e | 23 | #include <stdlib.h> |
a1bb33af | 24 | #include <glib.h> |
0d4405ce | 25 | #include <libserialport.h> |
45c59c8b BV |
26 | #include "libsigrok.h" |
27 | #include "libsigrok-internal.h" | |
a1bb33af | 28 | |
29a27196 UH |
29 | /* Message logging helpers with subsystem-specific prefix string. */ |
30 | #define LOG_PREFIX "serial: " | |
31 | #define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args) | |
32 | #define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args) | |
33 | #define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args) | |
34 | #define sr_info(s, args...) sr_info(LOG_PREFIX s, ## args) | |
35 | #define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args) | |
36 | #define sr_err(s, args...) sr_err(LOG_PREFIX s, ## args) | |
b19f4622 | 37 | |
b19f4622 UH |
38 | /** |
39 | * Open the specified serial port. | |
40 | * | |
299bdb24 | 41 | * @param serial Previously initialized serial port structure. |
a54dd31e UH |
42 | * @param flags Flags to use when opening the serial port. Possible flags |
43 | * include SERIAL_RDWR, SERIAL_RDONLY, SERIAL_NONBLOCK. | |
b19f4622 | 44 | * |
299bdb24 BV |
45 | * If the serial structure contains a serialcomm string, it will be |
46 | * passed to serial_set_paramstr() after the port is opened. | |
47 | * | |
48 | * @return SR_OK on success, SR_ERR on failure. | |
b19f4622 | 49 | */ |
299bdb24 | 50 | SR_PRIV int serial_open(struct sr_serial_dev_inst *serial, int flags) |
d02a535e | 51 | { |
a9bce5a5 ML |
52 | int ret; |
53 | char *error; | |
13cd8197 | 54 | int sp_flags = 0; |
b19f4622 | 55 | |
299bdb24 BV |
56 | if (!serial) { |
57 | sr_dbg("Invalid serial port."); | |
58 | return SR_ERR; | |
59 | } | |
60 | ||
61 | sr_spew("Opening serial port '%s' (flags %d).", serial->port, flags); | |
b19f4622 | 62 | |
c9bc57b6 ML |
63 | sp_get_port_by_name(serial->port, &serial->data); |
64 | ||
13cd8197 ML |
65 | if (flags & SERIAL_RDWR) |
66 | sp_flags = (SP_MODE_READ | SP_MODE_WRITE); | |
67 | else if (flags & SERIAL_RDONLY) | |
68 | sp_flags = SP_MODE_READ; | |
9647ce69 ML |
69 | |
70 | serial->nonblocking = (flags & SERIAL_NONBLOCK) ? 1 : 0; | |
13cd8197 ML |
71 | |
72 | ret = sp_open(serial->data, sp_flags); | |
a9bce5a5 | 73 | |
a0dfaa6c UH |
74 | switch (ret) { |
75 | case SP_ERR_ARG: | |
76 | sr_err("Attempt to open serial port with invalid parameters."); | |
77 | return SR_ERR_ARG; | |
78 | case SP_ERR_FAIL: | |
79 | error = sp_last_error_message(); | |
80 | sr_err("Error opening port: %s.", error); | |
81 | sp_free_error_message(error); | |
82 | return SR_ERR; | |
a54dd31e UH |
83 | } |
84 | ||
a9bce5a5 | 85 | #ifndef _WIN32 |
3182932d | 86 | sp_get_port_handle(serial->data, &serial->fd); |
926b866c | 87 | #endif |
299bdb24 BV |
88 | |
89 | if (serial->serialcomm) | |
90 | return serial_set_paramstr(serial, serial->serialcomm); | |
91 | else | |
92 | return SR_OK; | |
d02a535e BV |
93 | } |
94 | ||
b19f4622 UH |
95 | /** |
96 | * Close the specified serial port. | |
97 | * | |
299bdb24 | 98 | * @param serial Previously initialized serial port structure. |
b19f4622 | 99 | * |
299bdb24 | 100 | * @return SR_OK on success, SR_ERR on failure. |
2119ab03 | 101 | */ |
299bdb24 | 102 | SR_PRIV int serial_close(struct sr_serial_dev_inst *serial) |
d02a535e | 103 | { |
299bdb24 | 104 | int ret; |
a9bce5a5 | 105 | char *error; |
299bdb24 BV |
106 | |
107 | if (!serial) { | |
108 | sr_dbg("Invalid serial port."); | |
109 | return SR_ERR; | |
110 | } | |
111 | ||
112 | if (serial->fd == -1) { | |
113 | sr_dbg("Cannot close unopened serial port %s (fd %d).", | |
114 | serial->port, serial->fd); | |
115 | return SR_ERR; | |
116 | } | |
117 | ||
118 | sr_spew("Closing serial port %s (fd %d).", serial->port, serial->fd); | |
b19f4622 | 119 | |
c9bc57b6 | 120 | ret = sp_close(serial->data); |
066d42b1 | 121 | sp_free_port(serial->data); |
a9bce5a5 | 122 | |
a0dfaa6c UH |
123 | switch (ret) { |
124 | case SP_ERR_ARG: | |
125 | sr_err("Attempt to close an invalid serial port."); | |
126 | return SR_ERR_ARG; | |
127 | case SP_ERR_FAIL: | |
128 | error = sp_last_error_message(); | |
129 | sr_err("Error closing port: %s.", error); | |
130 | sp_free_error_message(error); | |
131 | return SR_ERR; | |
b19f4622 | 132 | } |
299bdb24 BV |
133 | |
134 | serial->fd = -1; | |
b19f4622 | 135 | |
6a76efeb | 136 | return SR_OK; |
d02a535e BV |
137 | } |
138 | ||
b19f4622 | 139 | /** |
299bdb24 | 140 | * Flush serial port buffers. |
b19f4622 | 141 | * |
299bdb24 | 142 | * @param serial Previously initialized serial port structure. |
b19f4622 | 143 | * |
299bdb24 | 144 | * @return SR_OK on success, SR_ERR on failure. |
1fdb75e1 | 145 | */ |
299bdb24 | 146 | SR_PRIV int serial_flush(struct sr_serial_dev_inst *serial) |
06d64eb8 | 147 | { |
299bdb24 | 148 | int ret; |
a9bce5a5 | 149 | char *error; |
299bdb24 BV |
150 | |
151 | if (!serial) { | |
152 | sr_dbg("Invalid serial port."); | |
153 | return SR_ERR; | |
154 | } | |
155 | ||
156 | if (serial->fd == -1) { | |
157 | sr_dbg("Cannot flush unopened serial port %s (fd %d).", | |
158 | serial->port, serial->fd); | |
159 | return SR_ERR; | |
160 | } | |
161 | ||
162 | sr_spew("Flushing serial port %s (fd %d).", serial->port, serial->fd); | |
b19f4622 | 163 | |
13cd8197 | 164 | ret = sp_flush(serial->data, SP_BUF_BOTH); |
a9bce5a5 | 165 | |
a0dfaa6c UH |
166 | switch (ret) { |
167 | case SP_ERR_ARG: | |
168 | sr_err("Attempt to flush an invalid serial port."); | |
169 | return SR_ERR_ARG; | |
170 | case SP_ERR_FAIL: | |
171 | error = sp_last_error_message(); | |
172 | sr_err("Error flushing port: %s.", error); | |
173 | sp_free_error_message(error); | |
174 | return SR_ERR; | |
299bdb24 | 175 | } |
b19f4622 | 176 | |
6a76efeb | 177 | return SR_OK; |
06d64eb8 BV |
178 | } |
179 | ||
b19f4622 | 180 | /** |
2119ab03 | 181 | * Write a number of bytes to the specified serial port. |
b19f4622 | 182 | * |
299bdb24 | 183 | * @param serial Previously initialized serial port structure. |
b19f4622 UH |
184 | * @param buf Buffer containing the bytes to write. |
185 | * @param count Number of bytes to write. | |
186 | * | |
6a76efeb | 187 | * @return The number of bytes written, or a negative error code upon failure. |
2119ab03 | 188 | */ |
299bdb24 BV |
189 | SR_PRIV int serial_write(struct sr_serial_dev_inst *serial, |
190 | const void *buf, size_t count) | |
2119ab03 | 191 | { |
299bdb24 | 192 | ssize_t ret; |
a9bce5a5 | 193 | char *error; |
299bdb24 BV |
194 | |
195 | if (!serial) { | |
196 | sr_dbg("Invalid serial port."); | |
6a76efeb | 197 | return SR_ERR; |
299bdb24 BV |
198 | } |
199 | ||
200 | if (serial->fd == -1) { | |
201 | sr_dbg("Cannot use unopened serial port %s (fd %d).", | |
202 | serial->port, serial->fd); | |
6a76efeb | 203 | return SR_ERR; |
299bdb24 BV |
204 | } |
205 | ||
9647ce69 ML |
206 | if (serial->nonblocking) |
207 | ret = sp_nonblocking_write(serial->data, buf, count); | |
208 | else | |
209 | ret = sp_blocking_write(serial->data, buf, count, 0); | |
a9bce5a5 | 210 | |
a0dfaa6c UH |
211 | switch (ret) { |
212 | case SP_ERR_ARG: | |
213 | sr_err("Attempted serial port write with invalid arguments."); | |
214 | return SR_ERR_ARG; | |
215 | case SP_ERR_FAIL: | |
216 | error = sp_last_error_message(); | |
217 | sr_err("Write error: %s.", error); | |
218 | sp_free_error_message(error); | |
219 | return SR_ERR; | |
a9bce5a5 ML |
220 | } |
221 | ||
222 | sr_spew("Wrote %d/%d bytes (fd %d).", ret, count, serial->fd); | |
b19f4622 UH |
223 | |
224 | return ret; | |
2119ab03 UH |
225 | } |
226 | ||
b19f4622 | 227 | /** |
2119ab03 | 228 | * Read a number of bytes from the specified serial port. |
b19f4622 | 229 | * |
299bdb24 | 230 | * @param serial Previously initialized serial port structure. |
b19f4622 UH |
231 | * @param buf Buffer where to store the bytes that are read. |
232 | * @param count The number of bytes to read. | |
233 | * | |
6a76efeb | 234 | * @return The number of bytes read, or a negative error code upon failure. |
2119ab03 | 235 | */ |
299bdb24 BV |
236 | SR_PRIV int serial_read(struct sr_serial_dev_inst *serial, void *buf, |
237 | size_t count) | |
2119ab03 | 238 | { |
299bdb24 | 239 | ssize_t ret; |
c4d85a40 | 240 | char *error; |
299bdb24 BV |
241 | |
242 | if (!serial) { | |
243 | sr_dbg("Invalid serial port."); | |
6a76efeb | 244 | return SR_ERR; |
299bdb24 BV |
245 | } |
246 | ||
247 | if (serial->fd == -1) { | |
248 | sr_dbg("Cannot use unopened serial port %s (fd %d).", | |
249 | serial->port, serial->fd); | |
6a76efeb | 250 | return SR_ERR; |
299bdb24 BV |
251 | } |
252 | ||
9647ce69 ML |
253 | if (serial->nonblocking) |
254 | ret = sp_nonblocking_read(serial->data, buf, count); | |
255 | else | |
256 | ret = sp_blocking_read(serial->data, buf, count, 0); | |
2119ab03 | 257 | |
a0dfaa6c UH |
258 | switch (ret) { |
259 | case SP_ERR_ARG: | |
260 | sr_err("Attempted serial port read with invalid arguments."); | |
261 | return SR_ERR_ARG; | |
c4d85a40 UH |
262 | case SP_ERR_FAIL: |
263 | error = sp_last_error_message(); | |
264 | sr_err("Read error: %s.", error); | |
265 | sp_free_error_message(error); | |
266 | return SR_ERR; | |
a9bce5a5 ML |
267 | } |
268 | ||
c4d85a40 | 269 | if (ret > 0) |
1a540442 | 270 | sr_spew("Read %d/%d bytes (fd %d).", ret, count, serial->fd); |
b19f4622 UH |
271 | |
272 | return ret; | |
2119ab03 UH |
273 | } |
274 | ||
b19f4622 UH |
275 | /** |
276 | * Set serial parameters for the specified serial port. | |
277 | * | |
299bdb24 | 278 | * @param serial Previously initialized serial port structure. |
b19f4622 UH |
279 | * @param baudrate The baudrate to set. |
280 | * @param bits The number of data bits to use. | |
281 | * @param parity The parity setting to use (0 = none, 1 = even, 2 = odd). | |
282 | * @param stopbits The number of stop bits to use (1 or 2). | |
283 | * @param flowcontrol The flow control settings to use (0 = none, 1 = RTS/CTS, | |
284 | * 2 = XON/XOFF). | |
2119ab03 | 285 | * |
b19f4622 | 286 | * @return SR_OK upon success, SR_ERR upon failure. |
1ff7712c | 287 | */ |
299bdb24 | 288 | SR_PRIV int serial_set_params(struct sr_serial_dev_inst *serial, int baudrate, |
39e5d798 UH |
289 | int bits, int parity, int stopbits, |
290 | int flowcontrol, int rts, int dtr) | |
d02a535e | 291 | { |
a9bce5a5 ML |
292 | int ret; |
293 | char *error; | |
25b66c3c | 294 | struct sp_port_config *config; |
a9bce5a5 | 295 | |
299bdb24 BV |
296 | if (!serial) { |
297 | sr_dbg("Invalid serial port."); | |
298 | return SR_ERR; | |
299 | } | |
300 | ||
301 | if (serial->fd == -1) { | |
302 | sr_dbg("Cannot configure unopened serial port %s (fd %d).", | |
39e5d798 | 303 | serial->port, serial->fd); |
299bdb24 BV |
304 | return SR_ERR; |
305 | } | |
306 | ||
307 | sr_spew("Setting serial parameters on port %s (fd %d).", serial->port, | |
39e5d798 | 308 | serial->fd); |
b19f4622 | 309 | |
25b66c3c ML |
310 | sp_new_config(&config); |
311 | sp_set_config_baudrate(config, baudrate); | |
312 | sp_set_config_bits(config, bits); | |
13cd8197 ML |
313 | switch (parity) { |
314 | case 0: | |
25b66c3c | 315 | sp_set_config_parity(config, SP_PARITY_NONE); |
13cd8197 ML |
316 | break; |
317 | case 1: | |
25b66c3c | 318 | sp_set_config_parity(config, SP_PARITY_EVEN); |
13cd8197 ML |
319 | break; |
320 | case 2: | |
25b66c3c | 321 | sp_set_config_parity(config, SP_PARITY_ODD); |
13cd8197 ML |
322 | break; |
323 | default: | |
324 | return SR_ERR_ARG; | |
325 | } | |
25b66c3c ML |
326 | sp_set_config_stopbits(config, stopbits); |
327 | sp_set_config_rts(config, flowcontrol == 1 ? SP_RTS_FLOW_CONTROL : rts); | |
328 | sp_set_config_cts(config, flowcontrol == 1 ? SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE); | |
329 | sp_set_config_dtr(config, dtr); | |
330 | sp_set_config_dsr(config, SP_DSR_IGNORE); | |
331 | sp_set_config_xon_xoff(config, flowcontrol == 2 ? SP_XONXOFF_INOUT : SP_XONXOFF_DISABLED); | |
332 | ||
333 | ret = sp_set_config(serial->data, config); | |
334 | sp_free_config(config); | |
a9bce5a5 | 335 | |
a0dfaa6c UH |
336 | switch (ret) { |
337 | case SP_ERR_ARG: | |
338 | sr_err("Invalid arguments for setting serial port parameters."); | |
339 | return SR_ERR_ARG; | |
340 | case SP_ERR_FAIL: | |
341 | error = sp_last_error_message(); | |
342 | sr_err("Error setting serial port parameters: %s.", error); | |
343 | sp_free_error_message(error); | |
344 | return SR_ERR; | |
700dcd5c UH |
345 | } |
346 | ||
e46b8fb1 | 347 | return SR_OK; |
d02a535e | 348 | } |
792fc686 | 349 | |
299bdb24 BV |
350 | /** |
351 | * Set serial parameters for the specified serial port. | |
352 | * | |
353 | * @param serial Previously initialized serial port structure. | |
354 | * @param paramstr A serial communication parameters string, in the form | |
26ddb5ba | 355 | * of <speed>/<data bits><parity><stopbits><flow>, for example "9600/8n1" or |
356 | * "600/7o2" or "460800/8n1/flow=2" where flow is 0 for none, 1 for rts/cts and 2 for xon/xoff. | |
299bdb24 BV |
357 | * |
358 | * @return SR_OK upon success, SR_ERR upon failure. | |
359 | */ | |
a970522b | 360 | #define SERIAL_COMM_SPEC "^(\\d+)/([5678])([neo])([12])(.*)$" |
299bdb24 BV |
361 | SR_PRIV int serial_set_paramstr(struct sr_serial_dev_inst *serial, |
362 | const char *paramstr) | |
792fc686 BV |
363 | { |
364 | GRegex *reg; | |
365 | GMatchInfo *match; | |
26ddb5ba | 366 | int speed, databits, parity, stopbits, flow, rts, dtr, i; |
71caaad4 | 367 | char *mstr, **opts, **kv; |
792fc686 | 368 | |
26ddb5ba | 369 | speed = databits = parity = stopbits = flow = 0; |
71caaad4 | 370 | rts = dtr = -1; |
ea088bb6 | 371 | sr_spew("Parsing parameters from \"%s\".", paramstr); |
792fc686 BV |
372 | reg = g_regex_new(SERIAL_COMM_SPEC, 0, 0, NULL); |
373 | if (g_regex_match(reg, paramstr, 0, &match)) { | |
374 | if ((mstr = g_match_info_fetch(match, 1))) | |
375 | speed = strtoul(mstr, NULL, 10); | |
376 | g_free(mstr); | |
377 | if ((mstr = g_match_info_fetch(match, 2))) | |
378 | databits = strtoul(mstr, NULL, 10); | |
379 | g_free(mstr); | |
380 | if ((mstr = g_match_info_fetch(match, 3))) { | |
381 | switch (mstr[0]) { | |
382 | case 'n': | |
383 | parity = SERIAL_PARITY_NONE; | |
384 | break; | |
385 | case 'e': | |
386 | parity = SERIAL_PARITY_EVEN; | |
387 | break; | |
388 | case 'o': | |
389 | parity = SERIAL_PARITY_ODD; | |
390 | break; | |
391 | } | |
392 | } | |
393 | g_free(mstr); | |
394 | if ((mstr = g_match_info_fetch(match, 4))) | |
395 | stopbits = strtoul(mstr, NULL, 10); | |
396 | g_free(mstr); | |
71caaad4 BV |
397 | if ((mstr = g_match_info_fetch(match, 5)) && mstr[0] != '\0') { |
398 | if (mstr[0] != '/') { | |
399 | sr_dbg("missing separator before extra options"); | |
400 | speed = 0; | |
401 | } else { | |
402 | /* A set of "key=value" options separated by / */ | |
403 | opts = g_strsplit(mstr + 1, "/", 0); | |
404 | for (i = 0; opts[i]; i++) { | |
405 | kv = g_strsplit(opts[i], "=", 2); | |
406 | if (!strncmp(kv[0], "rts", 3)) { | |
407 | if (kv[1][0] == '1') | |
408 | rts = 1; | |
409 | else if (kv[1][0] == '0') | |
410 | rts = 0; | |
411 | else { | |
412 | sr_dbg("invalid value for rts: %c", kv[1][0]); | |
413 | speed = 0; | |
414 | } | |
415 | } else if (!strncmp(kv[0], "dtr", 3)) { | |
416 | if (kv[1][0] == '1') | |
417 | dtr = 1; | |
418 | else if (kv[1][0] == '0') | |
419 | dtr = 0; | |
420 | else { | |
421 | sr_dbg("invalid value for dtr: %c", kv[1][0]); | |
422 | speed = 0; | |
423 | } | |
26ddb5ba | 424 | } else if (!strncmp(kv[0], "flow", 4)) { |
425 | if (kv[1][0] == '0') | |
426 | flow = 0; | |
427 | else if (kv[1][0] == '1') | |
428 | flow = 1; | |
429 | else if (kv[1][0] == '2') | |
430 | flow = 2; | |
431 | else { | |
432 | sr_dbg("invalid value for flow: %c", kv[1][0]); | |
433 | speed = 0; | |
434 | } | |
71caaad4 BV |
435 | } |
436 | g_strfreev(kv); | |
437 | } | |
438 | g_strfreev(opts); | |
439 | } | |
440 | } | |
441 | g_free(mstr); | |
792fc686 BV |
442 | } |
443 | g_match_info_unref(match); | |
444 | g_regex_unref(reg); | |
445 | ||
ea088bb6 AG |
446 | if (speed) { |
447 | return serial_set_params(serial, speed, databits, parity, | |
26ddb5ba | 448 | stopbits, flow, rts, dtr); |
ea088bb6 AG |
449 | } else { |
450 | sr_dbg("Could not infer speed from parameter string."); | |
792fc686 | 451 | return SR_ERR_ARG; |
ea088bb6 | 452 | } |
792fc686 BV |
453 | } |
454 | ||
299bdb24 BV |
455 | /** |
456 | * Read a line from the specified serial port. | |
457 | * | |
458 | * @param serial Previously initialized serial port structure. | |
459 | * @param buf Buffer where to store the bytes that are read. | |
460 | * @param buflen Size of the buffer. | |
461 | * @param timeout_ms How long to wait for a line to come in. | |
462 | * | |
463 | * Reading stops when CR of LR is found, which is stripped from the buffer. | |
464 | * | |
465 | * @return SR_OK on success, SR_ERR on failure. | |
466 | */ | |
467 | SR_PRIV int serial_readline(struct sr_serial_dev_inst *serial, char **buf, | |
468 | int *buflen, gint64 timeout_ms) | |
6f22a8ef | 469 | { |
b87f8504 | 470 | gint64 start; |
6f22a8ef UH |
471 | int maxlen, len; |
472 | ||
299bdb24 BV |
473 | if (!serial || serial->fd == -1) { |
474 | sr_dbg("Invalid serial port."); | |
475 | return SR_ERR; | |
476 | } | |
477 | ||
478 | if (serial->fd == -1) { | |
479 | sr_dbg("Cannot use unopened serial port %s (fd %d).", | |
480 | serial->port, serial->fd); | |
481 | return -1; | |
482 | } | |
483 | ||
6f22a8ef UH |
484 | timeout_ms *= 1000; |
485 | start = g_get_monotonic_time(); | |
486 | ||
487 | maxlen = *buflen; | |
488 | *buflen = len = 0; | |
489 | while(1) { | |
490 | len = maxlen - *buflen - 1; | |
491 | if (len < 1) | |
492 | break; | |
299bdb24 | 493 | len = serial_read(serial, *buf + *buflen, 1); |
6f22a8ef UH |
494 | if (len > 0) { |
495 | *buflen += len; | |
496 | *(*buf + *buflen) = '\0'; | |
318dd53c BV |
497 | if (*buflen > 0 && (*(*buf + *buflen - 1) == '\r' |
498 | || *(*buf + *buflen - 1) == '\n')) { | |
499 | /* Strip CR/LF and terminate. */ | |
6f22a8ef UH |
500 | *(*buf + --*buflen) = '\0'; |
501 | break; | |
502 | } | |
503 | } | |
504 | if (g_get_monotonic_time() - start > timeout_ms) | |
505 | /* Timeout */ | |
506 | break; | |
5715e84f DT |
507 | if (len < 1) |
508 | g_usleep(2000); | |
6f22a8ef | 509 | } |
318dd53c BV |
510 | if (*buflen) |
511 | sr_dbg("Received %d: '%s'.", *buflen, *buf); | |
6f22a8ef UH |
512 | |
513 | return SR_OK; | |
514 | } | |
766456be UH |
515 | |
516 | /** | |
517 | * Try to find a valid packet in a serial data stream. | |
518 | * | |
519 | * @param serial Previously initialized serial port structure. | |
520 | * @param buf Buffer containing the bytes to write. | |
521 | * @param count Size of the buffer. | |
522 | * @param packet_size Size, in bytes, of a valid packet. | |
523 | * @param is_valid Callback that assesses whether the packet is valid or not. | |
524 | * @param timeout_ms The timeout after which, if no packet is detected, to | |
525 | * abort scanning. | |
526 | * @param baudrate The baudrate of the serial port. This parameter is not | |
527 | * critical, but it helps fine tune the serial port polling | |
528 | * delay. | |
529 | * | |
530 | * @return SR_OK if a valid packet is found within the given timeout, | |
531 | * SR_ERR upon failure. | |
532 | */ | |
533 | SR_PRIV int serial_stream_detect(struct sr_serial_dev_inst *serial, | |
534 | uint8_t *buf, size_t *buflen, | |
535 | size_t packet_size, packet_valid_t is_valid, | |
536 | uint64_t timeout_ms, int baudrate) | |
537 | { | |
538 | uint64_t start, time, byte_delay_us; | |
539 | size_t ibuf, i, maxlen; | |
540 | int len; | |
541 | ||
542 | maxlen = *buflen; | |
543 | ||
544 | sr_dbg("Detecting packets on FD %d (timeout = %" PRIu64 | |
545 | "ms, baudrate = %d).", serial->fd, timeout_ms, baudrate); | |
546 | ||
547 | if (maxlen < (packet_size / 2) ) { | |
548 | sr_err("Buffer size must be at least twice the packet size."); | |
549 | return SR_ERR; | |
550 | } | |
551 | ||
766456be UH |
552 | /* Assume 8n1 transmission. That is 10 bits for every byte. */ |
553 | byte_delay_us = 10 * (1000000 / baudrate); | |
554 | start = g_get_monotonic_time(); | |
555 | ||
556 | i = ibuf = len = 0; | |
557 | while (ibuf < maxlen) { | |
558 | len = serial_read(serial, &buf[ibuf], 1); | |
559 | if (len > 0) { | |
560 | ibuf += len; | |
561 | } else if (len == 0) { | |
006dbe55 | 562 | /* No logging, already done in serial_read(). */ |
766456be UH |
563 | } else { |
564 | /* Error reading byte, but continuing anyway. */ | |
565 | } | |
551c3d8c AG |
566 | |
567 | time = g_get_monotonic_time() - start; | |
568 | time /= 1000; | |
569 | ||
766456be UH |
570 | if ((ibuf - i) >= packet_size) { |
571 | /* We have at least a packet's worth of data. */ | |
572 | if (is_valid(&buf[i])) { | |
766456be UH |
573 | sr_spew("Found valid %d-byte packet after " |
574 | "%" PRIu64 "ms.", (ibuf - i), time); | |
575 | *buflen = ibuf; | |
576 | return SR_OK; | |
577 | } else { | |
578 | sr_spew("Got %d bytes, but not a valid " | |
579 | "packet.", (ibuf - i)); | |
580 | } | |
581 | /* Not a valid packet. Continue searching. */ | |
582 | i++; | |
583 | } | |
551c3d8c | 584 | if (time >= timeout_ms) { |
766456be | 585 | /* Timeout */ |
551c3d8c | 586 | sr_dbg("Detection timed out after %dms.", time); |
766456be UH |
587 | break; |
588 | } | |
5715e84f DT |
589 | if (len < 1) |
590 | g_usleep(byte_delay_us); | |
766456be UH |
591 | } |
592 | ||
593 | *buflen = ibuf; | |
594 | ||
595 | sr_err("Didn't find a valid packet (read %d bytes).", *buflen); | |
596 | ||
597 | return SR_ERR; | |
598 | } | |
1bd9e678 DJ |
599 | |
600 | /** | |
601 | * Extract the serial device and options from the options linked list. | |
602 | * | |
603 | * @param options List of options passed from the command line. | |
604 | * @param serial_device Pointer where to store the exctracted serial device. | |
605 | * @param serial_options Pointer where to store the optional extracted serial | |
606 | * options. | |
607 | * | |
608 | * @return SR_OK if a serial_device is found, SR_ERR if no device is found. The | |
609 | * returned string should not be freed by the caller. | |
610 | */ | |
611 | SR_PRIV int sr_serial_extract_options(GSList *options, const char **serial_device, | |
612 | const char **serial_options) | |
613 | { | |
614 | GSList *l; | |
615 | struct sr_config *src; | |
616 | ||
617 | *serial_device = NULL; | |
618 | ||
619 | for (l = options; l; l = l->next) { | |
620 | src = l->data; | |
621 | switch (src->key) { | |
622 | case SR_CONF_CONN: | |
623 | *serial_device = g_variant_get_string(src->data, NULL); | |
624 | sr_dbg("Parsed serial device: %s", *serial_device); | |
625 | break; | |
626 | ||
627 | case SR_CONF_SERIALCOMM: | |
628 | *serial_options = g_variant_get_string(src->data, NULL); | |
629 | sr_dbg("Parsed serial options: %s", *serial_options); | |
630 | break; | |
631 | } | |
632 | } | |
633 | ||
634 | if (!*serial_device) { | |
635 | sr_dbg("No serial device specified"); | |
636 | return SR_ERR; | |
637 | } | |
638 | ||
639 | return SR_OK; | |
640 | } |