]> sigrok.org Git - libsigrok.git/blame_incremental - src/serial.c
ols: use serial layer's "has receive data" query
[libsigrok.git] / src / serial.c
... / ...
CommitLineData
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
5 * Copyright (C) 2010-2012 Uwe Hermann <uwe@hermann-uwe.de>
6 * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
7 * Copyright (C) 2014 Uffe Jakobsen <uffe@uffe.org>
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23#include <config.h>
24#include <string.h>
25#include <stdlib.h>
26#include <glib.h>
27#include <glib/gstdio.h>
28#include <libserialport.h>
29#include <libsigrok/libsigrok.h>
30#include "libsigrok-internal.h"
31#ifdef _WIN32
32#include <windows.h> /* for HANDLE */
33#endif
34
35/** @cond PRIVATE */
36#define LOG_PREFIX "serial"
37/** @endcond */
38
39/**
40 * @file
41 *
42 * Serial port handling.
43 */
44
45/**
46 * @defgroup grp_serial Serial port handling
47 *
48 * Serial port handling functions.
49 *
50 * @{
51 */
52
53/**
54 * Open the specified serial port.
55 *
56 * @param serial Previously initialized serial port structure.
57 * @param[in] flags Flags to use when opening the serial port. Possible flags
58 * include SERIAL_RDWR, SERIAL_RDONLY.
59 *
60 * If the serial structure contains a serialcomm string, it will be
61 * passed to serial_set_paramstr() after the port is opened.
62 *
63 * @retval SR_OK Success.
64 * @retval SR_ERR Failure.
65 *
66 * @private
67 */
68SR_PRIV int serial_open(struct sr_serial_dev_inst *serial, int flags)
69{
70 int ret;
71
72 if (!serial) {
73 sr_dbg("Invalid serial port.");
74 return SR_ERR;
75 }
76
77 sr_spew("Opening serial port '%s' (flags %d).", serial->port, flags);
78
79 ret = sr_ser_libsp_open(serial, flags);
80 if (ret != SR_OK)
81 return ret;
82
83 if (serial->serialcomm)
84 return serial_set_paramstr(serial, serial->serialcomm);
85 else
86 return SR_OK;
87}
88
89/**
90 * Close the specified serial port.
91 *
92 * @param serial Previously initialized serial port structure.
93 *
94 * @retval SR_OK Success.
95 * @retval SR_ERR Failure.
96 *
97 * @private
98 */
99SR_PRIV int serial_close(struct sr_serial_dev_inst *serial)
100{
101 if (!serial) {
102 sr_dbg("Invalid serial port.");
103 return SR_ERR;
104 }
105
106 sr_spew("Closing serial port %s.", serial->port);
107
108 return sr_ser_libsp_close(serial);
109}
110
111/**
112 * Flush serial port buffers. Empty buffers, discard pending RX and TX data.
113 *
114 * @param serial Previously initialized serial port structure.
115 *
116 * @retval SR_OK Success.
117 * @retval SR_ERR Failure.
118 *
119 * @private
120 */
121SR_PRIV int serial_flush(struct sr_serial_dev_inst *serial)
122{
123 if (!serial) {
124 sr_dbg("Invalid serial port.");
125 return SR_ERR;
126 }
127
128 sr_spew("Flushing serial port %s.", serial->port);
129
130 return sr_ser_libsp_flush(serial);
131}
132
133/**
134 * Drain serial port buffers. Wait for pending TX data to be sent.
135 *
136 * @param serial Previously initialized serial port structure.
137 *
138 * @retval SR_OK Success.
139 * @retval SR_ERR Failure.
140 *
141 * @private
142 */
143SR_PRIV int serial_drain(struct sr_serial_dev_inst *serial)
144{
145 if (!serial) {
146 sr_dbg("Invalid serial port.");
147 return SR_ERR;
148 }
149
150 sr_spew("Draining serial port %s.", serial->port);
151
152 return sr_ser_libsp_drain(serial);
153}
154
155/**
156 * Check for available receive data.
157 *
158 * @param[in] serial Previously opened serial port instance.
159 *
160 * @returns The number of (known) available RX data bytes.
161 *
162 * Returns 0 if no receive data is available, or if the amount of
163 * available receive data cannot get determined.
164 */
165SR_PRIV size_t serial_has_receive_data(struct sr_serial_dev_inst *serial)
166{
167 return sr_ser_libsp_get_rx_avail(serial);
168}
169
170static int _serial_write(struct sr_serial_dev_inst *serial,
171 const void *buf, size_t count,
172 int nonblocking, unsigned int timeout_ms)
173{
174 ssize_t ret;
175
176 if (!serial) {
177 sr_dbg("Invalid serial port.");
178 return SR_ERR;
179 }
180
181 ret = sr_ser_libsp_write(serial, buf, count, nonblocking, timeout_ms);
182 sr_spew("Wrote %zd/%zu bytes.", ret, count);
183
184 return ret;
185}
186
187/**
188 * Write a number of bytes to the specified serial port, blocking until finished.
189 *
190 * @param serial Previously initialized serial port structure.
191 * @param[in] buf Buffer containing the bytes to write.
192 * @param[in] count Number of bytes to write.
193 * @param[in] timeout_ms Timeout in ms, or 0 for no timeout.
194 *
195 * @retval SR_ERR_ARG Invalid argument.
196 * @retval SR_ERR Other error.
197 * @retval other The number of bytes written. If this is less than the number
198 * specified in the call, the timeout was reached.
199 *
200 * @private
201 */
202SR_PRIV int serial_write_blocking(struct sr_serial_dev_inst *serial,
203 const void *buf, size_t count, unsigned int timeout_ms)
204{
205 return _serial_write(serial, buf, count, 0, timeout_ms);
206}
207
208/**
209 * Write a number of bytes to the specified serial port, return immediately.
210 *
211 * @param serial Previously initialized serial port structure.
212 * @param[in] buf Buffer containing the bytes to write.
213 * @param[in] count Number of bytes to write.
214 *
215 * @retval SR_ERR_ARG Invalid argument.
216 * @retval SR_ERR Other error.
217 * @retval other The number of bytes written.
218 *
219 * @private
220 */
221SR_PRIV int serial_write_nonblocking(struct sr_serial_dev_inst *serial,
222 const void *buf, size_t count)
223{
224 return _serial_write(serial, buf, count, 1, 0);
225}
226
227static int _serial_read(struct sr_serial_dev_inst *serial,
228 void *buf, size_t count, int nonblocking, unsigned int timeout_ms)
229{
230 ssize_t ret;
231
232 if (!serial) {
233 sr_dbg("Invalid serial port.");
234 return SR_ERR;
235 }
236
237 ret = sr_ser_libsp_read(serial, buf, count, nonblocking, timeout_ms);
238 if (ret > 0)
239 sr_spew("Read %zd/%zu bytes.", ret, count);
240
241 return ret;
242}
243
244/**
245 * Read a number of bytes from the specified serial port, block until finished.
246 *
247 * @param serial Previously initialized serial port structure.
248 * @param buf Buffer where to store the bytes that are read.
249 * @param[in] count The number of bytes to read.
250 * @param[in] timeout_ms Timeout in ms, or 0 for no timeout.
251 *
252 * @retval SR_ERR_ARG Invalid argument.
253 * @retval SR_ERR Other error.
254 * @retval other The number of bytes read. If this is less than the number
255 * requested, the timeout was reached.
256 *
257 * @private
258 */
259SR_PRIV int serial_read_blocking(struct sr_serial_dev_inst *serial,
260 void *buf, size_t count, unsigned int timeout_ms)
261{
262 return _serial_read(serial, buf, count, 0, timeout_ms);
263}
264
265/**
266 * Try to read up to @a count bytes from the specified serial port, return
267 * immediately with what's available.
268 *
269 * @param serial Previously initialized serial port structure.
270 * @param buf Buffer where to store the bytes that are read.
271 * @param[in] count The number of bytes to read.
272 *
273 * @retval SR_ERR_ARG Invalid argument.
274 * @retval SR_ERR Other error.
275 * @retval other The number of bytes read.
276 *
277 * @private
278 */
279SR_PRIV int serial_read_nonblocking(struct sr_serial_dev_inst *serial,
280 void *buf, size_t count)
281{
282 return _serial_read(serial, buf, count, 1, 0);
283}
284
285/**
286 * Set serial parameters for the specified serial port.
287 *
288 * @param serial Previously initialized serial port structure.
289 * @param[in] baudrate The baudrate to set.
290 * @param[in] bits The number of data bits to use (5, 6, 7 or 8).
291 * @param[in] parity The parity setting to use (0 = none, 1 = even, 2 = odd).
292 * @param[in] stopbits The number of stop bits to use (1 or 2).
293 * @param[in] flowcontrol The flow control settings to use (0 = none,
294 * 1 = RTS/CTS, 2 = XON/XOFF).
295 * @param[in] rts Status of RTS line (0 or 1; required by some interfaces).
296 * @param[in] dtr Status of DTR line (0 or 1; required by some interfaces).
297 *
298 * @retval SR_OK Success.
299 * @retval SR_ERR Failure.
300 *
301 * @private
302 */
303SR_PRIV int serial_set_params(struct sr_serial_dev_inst *serial,
304 int baudrate, int bits, int parity, int stopbits,
305 int flowcontrol, int rts, int dtr)
306{
307 int ret;
308
309 if (!serial) {
310 sr_dbg("Invalid serial port.");
311 return SR_ERR;
312 }
313
314 sr_spew("Setting serial parameters on port %s.", serial->port);
315
316 ret = sr_ser_libsp_set_params(serial,
317 baudrate, bits, parity, stopbits, flowcontrol, rts, dtr);
318 if (ret == SR_OK) {
319 serial->comm_params.bit_rate = baudrate;
320 serial->comm_params.data_bits = bits;
321 serial->comm_params.parity_bits = parity ? 1 : 0;
322 serial->comm_params.stop_bits = stopbits;
323 sr_dbg("DBG: %s() rate %d, %d%s%d", __func__,
324 baudrate, bits,
325 (parity == 0) ? "n" : "x",
326 stopbits);
327 }
328
329 return ret;
330}
331
332/**
333 * Set serial parameters for the specified serial port from parameter string.
334 *
335 * @param serial Previously initialized serial port structure.
336 * @param[in] paramstr A serial communication parameters string of the form
337 * "<baudrate>/<bits><parity><stopbits>{/<option>}".\n
338 * Examples: "9600/8n1", "600/7o2/dtr=1/rts=0" or "460800/8n1/flow=2".\n
339 * \<baudrate\>=integer Baud rate.\n
340 * \<bits\>=5|6|7|8 Number of data bits.\n
341 * \<parity\>=n|e|o None, even, odd.\n
342 * \<stopbits\>=1|2 One or two stop bits.\n
343 * Options:\n
344 * dtr=0|1 Set DTR off resp. on.\n
345 * flow=0|1|2 Flow control. 0 for none, 1 for RTS/CTS, 2 for XON/XOFF.\n
346 * rts=0|1 Set RTS off resp. on.\n
347 * Please note that values and combinations of these parameters must be
348 * supported by the concrete serial interface hardware and the drivers for it.
349 *
350 * @retval SR_OK Success.
351 * @retval SR_ERR Failure.
352 *
353 * @private
354 */
355SR_PRIV int serial_set_paramstr(struct sr_serial_dev_inst *serial,
356 const char *paramstr)
357{
358/** @cond PRIVATE */
359#define SERIAL_COMM_SPEC "^(\\d+)/([5678])([neo])([12])(.*)$"
360/** @endcond */
361
362 GRegex *reg;
363 GMatchInfo *match;
364 int speed, databits, parity, stopbits, flow, rts, dtr, i;
365 char *mstr, **opts, **kv;
366
367 speed = databits = parity = stopbits = flow = 0;
368 rts = dtr = -1;
369 sr_spew("Parsing parameters from \"%s\".", paramstr);
370 reg = g_regex_new(SERIAL_COMM_SPEC, 0, 0, NULL);
371 if (g_regex_match(reg, paramstr, 0, &match)) {
372 if ((mstr = g_match_info_fetch(match, 1)))
373 speed = strtoul(mstr, NULL, 10);
374 g_free(mstr);
375 if ((mstr = g_match_info_fetch(match, 2)))
376 databits = strtoul(mstr, NULL, 10);
377 g_free(mstr);
378 if ((mstr = g_match_info_fetch(match, 3))) {
379 switch (mstr[0]) {
380 case 'n':
381 parity = SP_PARITY_NONE;
382 break;
383 case 'e':
384 parity = SP_PARITY_EVEN;
385 break;
386 case 'o':
387 parity = SP_PARITY_ODD;
388 break;
389 }
390 }
391 g_free(mstr);
392 if ((mstr = g_match_info_fetch(match, 4)))
393 stopbits = strtoul(mstr, NULL, 10);
394 g_free(mstr);
395 if ((mstr = g_match_info_fetch(match, 5)) && mstr[0] != '\0') {
396 if (mstr[0] != '/') {
397 sr_dbg("missing separator before extra options");
398 speed = 0;
399 } else {
400 /* A set of "key=value" options separated by / */
401 opts = g_strsplit(mstr + 1, "/", 0);
402 for (i = 0; opts[i]; i++) {
403 kv = g_strsplit(opts[i], "=", 2);
404 if (!strncmp(kv[0], "rts", 3)) {
405 if (kv[1][0] == '1')
406 rts = 1;
407 else if (kv[1][0] == '0')
408 rts = 0;
409 else {
410 sr_dbg("invalid value for rts: %c", kv[1][0]);
411 speed = 0;
412 }
413 } else if (!strncmp(kv[0], "dtr", 3)) {
414 if (kv[1][0] == '1')
415 dtr = 1;
416 else if (kv[1][0] == '0')
417 dtr = 0;
418 else {
419 sr_dbg("invalid value for dtr: %c", kv[1][0]);
420 speed = 0;
421 }
422 } else if (!strncmp(kv[0], "flow", 4)) {
423 if (kv[1][0] == '0')
424 flow = 0;
425 else if (kv[1][0] == '1')
426 flow = 1;
427 else if (kv[1][0] == '2')
428 flow = 2;
429 else {
430 sr_dbg("invalid value for flow: %c", kv[1][0]);
431 speed = 0;
432 }
433 }
434 g_strfreev(kv);
435 }
436 g_strfreev(opts);
437 }
438 }
439 g_free(mstr);
440 }
441 g_match_info_unref(match);
442 g_regex_unref(reg);
443
444 if (speed) {
445 return serial_set_params(serial, speed, databits, parity,
446 stopbits, flow, rts, dtr);
447 } else {
448 sr_dbg("Could not infer speed from parameter string.");
449 return SR_ERR_ARG;
450 }
451}
452
453/**
454 * Read a line from the specified serial port.
455 *
456 * @param[in] serial Previously initialized serial port structure.
457 * @param[out] buf Buffer where to store the bytes that are read.
458 * @param[in] buflen Size of the buffer.
459 * @param[in] timeout_ms How long to wait for a line to come in.
460 *
461 * Reading stops when CR or LF is found, which is stripped from the buffer.
462 *
463 * @retval SR_OK Success.
464 * @retval SR_ERR Failure.
465 *
466 * @private
467 */
468SR_PRIV int serial_readline(struct sr_serial_dev_inst *serial,
469 char **buf, int *buflen, gint64 timeout_ms)
470{
471 gint64 start, remaining;
472 int maxlen, len;
473
474 if (!serial) {
475 sr_dbg("Invalid serial port.");
476 return SR_ERR;
477 }
478
479 if (!serial->sp_data) {
480 sr_dbg("Cannot use unopened serial port %s.", serial->port);
481 return -1;
482 }
483
484 start = g_get_monotonic_time();
485 remaining = timeout_ms;
486
487 maxlen = *buflen;
488 *buflen = len = 0;
489 while (1) {
490 len = maxlen - *buflen - 1;
491 if (len < 1)
492 break;
493 len = serial_read_blocking(serial, *buf + *buflen, 1, remaining);
494 if (len > 0) {
495 *buflen += len;
496 *(*buf + *buflen) = '\0';
497 if (*buflen > 0 && (*(*buf + *buflen - 1) == '\r'
498 || *(*buf + *buflen - 1) == '\n')) {
499 /* Strip CR/LF and terminate. */
500 *(*buf + --*buflen) = '\0';
501 break;
502 }
503 }
504 /* Reduce timeout by time elapsed. */
505 remaining = timeout_ms - ((g_get_monotonic_time() - start) / 1000);
506 if (remaining <= 0)
507 /* Timeout */
508 break;
509 if (len < 1)
510 g_usleep(2000);
511 }
512 if (*buflen)
513 sr_dbg("Received %d: '%s'.", *buflen, *buf);
514
515 return SR_OK;
516}
517
518/**
519 * Try to find a valid packet in a serial data stream.
520 *
521 * @param serial Previously initialized serial port structure.
522 * @param buf Buffer containing the bytes to write.
523 * @param buflen Size of the buffer.
524 * @param[in] packet_size Size, in bytes, of a valid packet.
525 * @param is_valid Callback that assesses whether the packet is valid or not.
526 * @param[in] timeout_ms The timeout after which, if no packet is detected, to
527 * abort scanning.
528 * @param[in] baudrate The baudrate of the serial port. This parameter is not
529 * critical, but it helps fine tune the serial port polling
530 * delay.
531 *
532 * @retval SR_OK Valid packet was found within the given timeout.
533 * @retval SR_ERR Failure.
534 *
535 * @private
536 */
537SR_PRIV int serial_stream_detect(struct sr_serial_dev_inst *serial,
538 uint8_t *buf, size_t *buflen,
539 size_t packet_size,
540 packet_valid_callback is_valid,
541 uint64_t timeout_ms, int baudrate)
542{
543 uint64_t start, time, byte_delay_us;
544 size_t ibuf, i, maxlen;
545 ssize_t len;
546
547 maxlen = *buflen;
548
549 sr_dbg("Detecting packets on %s (timeout = %" PRIu64
550 "ms, baudrate = %d).", serial->port, timeout_ms, baudrate);
551
552 if (maxlen < (packet_size / 2) ) {
553 sr_err("Buffer size must be at least twice the packet size.");
554 return SR_ERR;
555 }
556
557 /* Assume 8n1 transmission. That is 10 bits for every byte. */
558 byte_delay_us = 10 * ((1000 * 1000) / baudrate);
559 start = g_get_monotonic_time();
560
561 i = ibuf = len = 0;
562 while (ibuf < maxlen) {
563 len = serial_read_nonblocking(serial, &buf[ibuf], 1);
564 if (len > 0) {
565 ibuf += len;
566 } else if (len == 0) {
567 /* No logging, already done in serial_read(). */
568 } else {
569 /* Error reading byte, but continuing anyway. */
570 }
571
572 time = g_get_monotonic_time() - start;
573 time /= 1000;
574
575 if ((ibuf - i) >= packet_size) {
576 GString *text;
577 /* We have at least a packet's worth of data. */
578 text = sr_hexdump_new(&buf[i], packet_size);
579 sr_spew("Trying packet: %s", text->str);
580 sr_hexdump_free(text);
581 if (is_valid(&buf[i])) {
582 sr_spew("Found valid %zu-byte packet after "
583 "%" PRIu64 "ms.", (ibuf - i), time);
584 *buflen = ibuf;
585 return SR_OK;
586 } else {
587 sr_spew("Got %zu bytes, but not a valid "
588 "packet.", (ibuf - i));
589 }
590 /* Not a valid packet. Continue searching. */
591 i++;
592 }
593 if (time >= timeout_ms) {
594 /* Timeout */
595 sr_dbg("Detection timed out after %" PRIu64 "ms.", time);
596 break;
597 }
598 if (len < 1)
599 g_usleep(byte_delay_us);
600 }
601
602 *buflen = ibuf;
603
604 sr_err("Didn't find a valid packet (read %zu bytes).", *buflen);
605
606 return SR_ERR;
607}
608
609/**
610 * Extract the serial device and options from the options linked list.
611 *
612 * @param options List of options passed from the command line.
613 * @param serial_device Pointer where to store the extracted serial device.
614 * @param serial_options Pointer where to store the optional extracted serial
615 * options.
616 *
617 * @return SR_OK if a serial_device is found, SR_ERR if no device is found. The
618 * returned string should not be freed by the caller.
619 *
620 * @private
621 */
622SR_PRIV int sr_serial_extract_options(GSList *options,
623 const char **serial_device, const char **serial_options)
624{
625 GSList *l;
626 struct sr_config *src;
627
628 *serial_device = NULL;
629
630 for (l = options; l; l = l->next) {
631 src = l->data;
632 switch (src->key) {
633 case SR_CONF_CONN:
634 *serial_device = g_variant_get_string(src->data, NULL);
635 sr_dbg("Parsed serial device: %s.", *serial_device);
636 break;
637 case SR_CONF_SERIALCOMM:
638 *serial_options = g_variant_get_string(src->data, NULL);
639 sr_dbg("Parsed serial options: %s.", *serial_options);
640 break;
641 }
642 }
643
644 if (!*serial_device) {
645 sr_dbg("No serial device specified.");
646 return SR_ERR;
647 }
648
649 return SR_OK;
650}
651
652/** @private */
653SR_PRIV int serial_source_add(struct sr_session *session,
654 struct sr_serial_dev_inst *serial, int events, int timeout,
655 sr_receive_data_callback cb, void *cb_data)
656{
657 if ((events & (G_IO_IN | G_IO_ERR)) && (events & G_IO_OUT)) {
658 sr_err("Cannot poll input/error and output simultaneously.");
659 return SR_ERR_ARG;
660 }
661
662 if (!serial->sp_data) {
663 sr_err("Invalid serial port.");
664 return SR_ERR_ARG;
665 }
666
667 return sr_ser_libsp_source_add(session, serial,
668 events, timeout, cb, cb_data);
669}
670
671/** @private */
672SR_PRIV int serial_source_remove(struct sr_session *session,
673 struct sr_serial_dev_inst *serial)
674{
675 if (!serial->sp_data) {
676 sr_err("Invalid serial port.");
677 return SR_ERR_ARG;
678 }
679
680 return sr_ser_libsp_source_remove(session, serial);
681}
682
683/**
684 * Create/allocate a new sr_serial_port structure.
685 *
686 * @param name The OS dependent name of the serial port. Must not be NULL.
687 * @param description An end user friendly description for the serial port.
688 * Can be NULL (in that case the empty string is used
689 * as description).
690 *
691 * @return The newly allocated sr_serial_port struct.
692 */
693static struct sr_serial_port *sr_serial_new(const char *name,
694 const char *description)
695{
696 struct sr_serial_port *serial;
697
698 if (!name)
699 return NULL;
700
701 serial = g_malloc0(sizeof(*serial));
702 serial->name = g_strdup(name);
703 serial->description = g_strdup(description ? description : "");
704
705 return serial;
706}
707
708/**
709 * Free a previously allocated sr_serial_port structure.
710 *
711 * @param serial The sr_serial_port struct to free. Must not be NULL.
712 */
713SR_API void sr_serial_free(struct sr_serial_port *serial)
714{
715 if (!serial)
716 return;
717 g_free(serial->name);
718 g_free(serial->description);
719 g_free(serial);
720}
721
722static GSList *append_port_list(GSList *devs, const char *name, const char *desc)
723{
724 return g_slist_append(devs, sr_serial_new(name, desc));
725}
726
727/**
728 * List available serial devices.
729 *
730 * @return A GSList of strings containing the path of the serial devices or
731 * NULL if no serial device is found. The returned list must be freed
732 * by the caller.
733 */
734SR_API GSList *sr_serial_list(const struct sr_dev_driver *driver)
735{
736 GSList *tty_devs;
737
738 /* Currently unused, but will be used by some drivers later on. */
739 (void)driver;
740
741 tty_devs = NULL;
742 tty_devs = sr_ser_libsp_list(tty_devs, append_port_list);
743
744 return tty_devs;
745}
746
747static GSList *append_port_find(GSList *devs, const char *name)
748{
749 if (!name || !*name)
750 return devs;
751
752 return g_slist_append(devs, g_strdup(name));
753}
754
755/**
756 * Find USB serial devices via the USB vendor ID and product ID.
757 *
758 * @param[in] vendor_id Vendor ID of the USB device.
759 * @param[in] product_id Product ID of the USB device.
760 *
761 * @return A GSList of strings containing the path of the serial device or
762 * NULL if no serial device is found. The returned list must be freed
763 * by the caller.
764 *
765 * @private
766 */
767SR_PRIV GSList *sr_serial_find_usb(uint16_t vendor_id, uint16_t product_id)
768{
769 GSList *tty_devs;
770
771 tty_devs = NULL;
772 tty_devs = sr_ser_libsp_find_usb(tty_devs, append_port_find,
773 vendor_id, product_id);
774
775 return tty_devs;
776}
777
778/** @private */
779SR_PRIV int serial_timeout(struct sr_serial_dev_inst *port, int num_bytes)
780{
781 int bits, baud;
782 int ret;
783 int timeout_ms;
784
785 /* Get the bitrate and frame length. */
786 bits = baud = 0;
787 ret = sr_ser_libsp_get_frame_format(port, &baud, &bits);
788 if (ret != SR_OK)
789 bits = baud = 0;
790 if (!bits || !baud) {
791 baud = port->comm_params.bit_rate;
792 bits = 1 + port->comm_params.data_bits +
793 port->comm_params.parity_bits +
794 port->comm_params.stop_bits;
795 }
796
797 /* Derive the timeout. Default to 1s. */
798 timeout_ms = 1000;
799 if (bits && baud) {
800 /* Throw in 10ms for misc OS overhead. */
801 timeout_ms = 10;
802 timeout_ms += ((1000.0 / baud) * bits) * num_bytes;
803 }
804
805 return timeout_ms;
806}
807
808/** @} */