2 * This file is part of the libsigrok project.
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 * Copyright (C) 2017-2019 Gerhard Sittig <gerhard.sittig@gmx.net>
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #include <glib/gstdio.h>
29 #ifdef HAVE_LIBSERIALPORT
30 #include <libserialport.h>
32 #include <libsigrok/libsigrok.h>
33 #include "libsigrok-internal.h"
35 #include <windows.h> /* for HANDLE */
39 #define LOG_PREFIX "serial"
45 * Serial port handling.
49 * @defgroup grp_serial Serial port handling
51 * Serial port handling functions.
56 #ifdef HAVE_SERIAL_COMM
58 /* See if an (assumed opened) serial port is of any supported type. */
59 static int dev_is_supported(struct sr_serial_dev_inst *serial)
61 if (!serial || !serial->lib_funcs)
68 * Open the specified serial port.
70 * @param serial Previously initialized serial port structure.
71 * @param[in] flags Flags to use when opening the serial port. Possible flags
72 * include SERIAL_RDWR, SERIAL_RDONLY.
74 * If the serial structure contains a serialcomm string, it will be
75 * passed to serial_set_paramstr() after the port is opened.
77 * @retval SR_OK Success.
78 * @retval SR_ERR Failure.
82 SR_PRIV int serial_open(struct sr_serial_dev_inst *serial, int flags)
87 sr_dbg("Invalid serial port.");
91 sr_spew("Opening serial port '%s' (flags %d).", serial->port, flags);
94 * Determine which serial transport library to use. Derive the
95 * variant from the serial port's name. Default to libserialport
96 * for backwards compatibility.
98 if (ser_name_is_hid(serial))
99 serial->lib_funcs = ser_lib_funcs_hid;
100 else if (ser_name_is_bt(serial))
101 serial->lib_funcs = ser_lib_funcs_bt;
103 serial->lib_funcs = ser_lib_funcs_libsp;
104 if (!serial->lib_funcs)
108 * Note that use of the 'rcv_buffer' is optional, and the buffer's
109 * size heavily depends on the specific transport. That's why the
110 * buffer's content gets accessed and the buffer is released here in
111 * common code, but the buffer gets allocated in libraries' open()
116 * Run the transport's open routine. Setup the bitrate and the
119 if (!serial->lib_funcs->open)
121 ret = serial->lib_funcs->open(serial, flags);
125 if (serial->serialcomm)
126 return serial_set_paramstr(serial, serial->serialcomm);
132 * Close the specified serial port.
134 * @param serial Previously initialized serial port structure.
136 * @retval SR_OK Success.
137 * @retval SR_ERR Failure.
141 SR_PRIV int serial_close(struct sr_serial_dev_inst *serial)
146 sr_dbg("Invalid serial port.");
150 sr_spew("Closing serial port %s.", serial->port);
152 if (!serial->lib_funcs || !serial->lib_funcs->close)
155 rc = serial->lib_funcs->close(serial);
156 if (rc == SR_OK && serial->rcv_buffer) {
157 g_string_free(serial->rcv_buffer, TRUE);
158 serial->rcv_buffer = NULL;
165 * Flush serial port buffers. Empty buffers, discard pending RX and TX data.
167 * @param serial Previously initialized serial port structure.
169 * @retval SR_OK Success.
170 * @retval SR_ERR Failure.
174 SR_PRIV int serial_flush(struct sr_serial_dev_inst *serial)
177 sr_dbg("Invalid serial port.");
181 sr_spew("Flushing serial port %s.", serial->port);
183 sr_ser_discard_queued_data(serial);
185 if (!serial->lib_funcs || !serial->lib_funcs->flush)
188 return serial->lib_funcs->flush(serial);
192 * Drain serial port buffers. Wait for pending TX data to be sent.
194 * @param serial Previously initialized serial port structure.
196 * @retval SR_OK Success.
197 * @retval SR_ERR Failure.
201 SR_PRIV int serial_drain(struct sr_serial_dev_inst *serial)
204 sr_dbg("Invalid serial port.");
208 sr_spew("Draining serial port %s.", serial->port);
210 if (!serial->lib_funcs || !serial->lib_funcs->drain)
213 return serial->lib_funcs->drain(serial);
217 * Provide an internal RX data buffer for the serial port. This is not
218 * supposed to be used directly by applications. Instead optional and
219 * alternative transports for serial communication can use this buffer
220 * if their progress is driven from background activity, and is not
221 * (directly) driven by external API calls.
223 * BEWARE! This implementation assumes that data which gets communicated
224 * via UART can get stored in a GString (which is a char array). Since
225 * the API hides this detail, we can address this issue later when needed.
226 * Callers use the API which communicates bytes.
228 * Applications optionally can register a "per RX chunk" callback, when
229 * they depend on the frame boundaries of the respective physical layer.
230 * Most callers just want the stream of RX data, and can use the buffer.
232 * The availability of RX chunks to callbacks, as well as the capability
233 * to pass on exact frames as chunks or potential re-assembly of chunks
234 * to a single data block, depend on each transport's implementation.
238 * Register application callback for RX data chunks.
240 * @param[in] serial Previously initialized serial port instance.
241 * @param[in] cb Routine to call as RX data becomes available.
242 * @param[in] cb_data User data to pass to the callback in addition to RX data.
244 * @retval SR_ERR_ARG Invalid parameters.
245 * @retval SR_OK Successful registration.
247 * Callbacks get unregistered by specifying NULL for the 'cb' parameter.
251 SR_PRIV int serial_set_read_chunk_cb(struct sr_serial_dev_inst *serial,
252 serial_rx_chunk_callback cb, void *cb_data)
257 serial->rx_chunk_cb_func = cb;
258 serial->rx_chunk_cb_data = cb_data;
264 * Discard previously queued RX data. Internal to the serial subsystem,
265 * coordination between common and transport specific support code.
267 * @param[in] serial Previously opened serial port instance.
271 SR_PRIV void sr_ser_discard_queued_data(struct sr_serial_dev_inst *serial)
273 if (!serial || !serial->rcv_buffer)
276 g_string_truncate(serial->rcv_buffer, 0);
280 * Get amount of queued RX data. Internal to the serial subsystem,
281 * coordination between common and transport specific support code.
283 * @param[in] serial Previously opened serial port instance.
287 SR_PRIV size_t sr_ser_has_queued_data(struct sr_serial_dev_inst *serial)
289 if (!serial || !serial->rcv_buffer)
292 return serial->rcv_buffer->len;
296 * Queue received data. Internal to the serial subsystem, coordination
297 * between common and transport specific support code.
299 * @param[in] serial Previously opened serial port instance.
300 * @param[in] data Pointer to data bytes to queue.
301 * @param[in] len Number of data bytes to queue.
305 SR_PRIV void sr_ser_queue_rx_data(struct sr_serial_dev_inst *serial,
306 const uint8_t *data, size_t len)
308 if (!serial || !data || !len)
311 if (serial->rx_chunk_cb_func)
312 serial->rx_chunk_cb_func(serial, serial->rx_chunk_cb_data, data, len);
313 else if (serial->rcv_buffer)
314 g_string_append_len(serial->rcv_buffer, (const gchar *)data, len);
318 * Retrieve previously queued RX data. Internal to the serial subsystem,
319 * coordination between common and transport specific support code.
321 * @param[in] serial Previously opened serial port instance.
322 * @param[out] data Pointer to store retrieved data bytes into.
323 * @param[in] len Number of data bytes to retrieve.
327 SR_PRIV size_t sr_ser_unqueue_rx_data(struct sr_serial_dev_inst *serial,
328 uint8_t *data, size_t len)
333 if (!serial || !data || !len)
336 qlen = sr_ser_has_queued_data(serial);
340 buf = serial->rcv_buffer;
344 memcpy(data, buf->str, len);
345 g_string_erase(buf, 0, len);
352 * Check for available receive data.
354 * @param[in] serial Previously opened serial port instance.
356 * @returns The number of (known) available RX data bytes.
358 * Returns 0 if no receive data is available, or if the amount of
359 * available receive data cannot get determined.
363 SR_PRIV size_t serial_has_receive_data(struct sr_serial_dev_inst *serial)
365 size_t lib_count, buf_count;
371 if (serial->lib_funcs && serial->lib_funcs->get_rx_avail)
372 lib_count = serial->lib_funcs->get_rx_avail(serial);
374 buf_count = sr_ser_has_queued_data(serial);
376 return lib_count + buf_count;
379 static int _serial_write(struct sr_serial_dev_inst *serial,
380 const void *buf, size_t count,
381 int nonblocking, unsigned int timeout_ms)
386 sr_dbg("Invalid serial port.");
390 if (!serial->lib_funcs || !serial->lib_funcs->write)
392 ret = serial->lib_funcs->write(serial, buf, count,
393 nonblocking, timeout_ms);
394 sr_spew("Wrote %zd/%zu bytes.", ret, count);
400 * Write a number of bytes to the specified serial port, blocking until finished.
402 * @param serial Previously initialized serial port structure.
403 * @param[in] buf Buffer containing the bytes to write.
404 * @param[in] count Number of bytes to write.
405 * @param[in] timeout_ms Timeout in ms, or 0 for no timeout.
407 * @retval SR_ERR_ARG Invalid argument.
408 * @retval SR_ERR Other error.
409 * @retval other The number of bytes written. If this is less than the number
410 * specified in the call, the timeout was reached.
414 SR_PRIV int serial_write_blocking(struct sr_serial_dev_inst *serial,
415 const void *buf, size_t count, unsigned int timeout_ms)
417 return _serial_write(serial, buf, count, 0, timeout_ms);
421 * Write a number of bytes to the specified serial port, return immediately.
423 * @param serial Previously initialized serial port structure.
424 * @param[in] buf Buffer containing the bytes to write.
425 * @param[in] count Number of bytes to write.
427 * @retval SR_ERR_ARG Invalid argument.
428 * @retval SR_ERR Other error.
429 * @retval other The number of bytes written.
433 SR_PRIV int serial_write_nonblocking(struct sr_serial_dev_inst *serial,
434 const void *buf, size_t count)
436 return _serial_write(serial, buf, count, 1, 0);
439 static int _serial_read(struct sr_serial_dev_inst *serial,
440 void *buf, size_t count, int nonblocking, unsigned int timeout_ms)
445 sr_dbg("Invalid serial port.");
449 if (!serial->lib_funcs || !serial->lib_funcs->read)
451 ret = serial->lib_funcs->read(serial, buf, count,
452 nonblocking, timeout_ms);
454 sr_spew("Read %zd/%zu bytes.", ret, count);
460 * Read a number of bytes from the specified serial port, block until finished.
462 * @param serial Previously initialized serial port structure.
463 * @param buf Buffer where to store the bytes that are read.
464 * @param[in] count The number of bytes to read.
465 * @param[in] timeout_ms Timeout in ms, or 0 for no timeout.
467 * @retval SR_ERR_ARG Invalid argument.
468 * @retval SR_ERR Other error.
469 * @retval other The number of bytes read. If this is less than the number
470 * requested, the timeout was reached.
474 SR_PRIV int serial_read_blocking(struct sr_serial_dev_inst *serial,
475 void *buf, size_t count, unsigned int timeout_ms)
477 return _serial_read(serial, buf, count, 0, timeout_ms);
481 * Try to read up to @a count bytes from the specified serial port, return
482 * immediately with what's available.
484 * @param serial Previously initialized serial port structure.
485 * @param buf Buffer where to store the bytes that are read.
486 * @param[in] count The number of bytes to read.
488 * @retval SR_ERR_ARG Invalid argument.
489 * @retval SR_ERR Other error.
490 * @retval other The number of bytes read.
494 SR_PRIV int serial_read_nonblocking(struct sr_serial_dev_inst *serial,
495 void *buf, size_t count)
497 return _serial_read(serial, buf, count, 1, 0);
501 * Set serial parameters for the specified serial port.
503 * @param serial Previously initialized serial port structure.
504 * @param[in] baudrate The baudrate to set.
505 * @param[in] bits The number of data bits to use (5, 6, 7 or 8).
506 * @param[in] parity The parity setting to use (0 = none, 1 = even, 2 = odd).
507 * @param[in] stopbits The number of stop bits to use (1 or 2).
508 * @param[in] flowcontrol The flow control settings to use (0 = none,
509 * 1 = RTS/CTS, 2 = XON/XOFF).
510 * @param[in] rts Status of RTS line (0 or 1; required by some interfaces).
511 * @param[in] dtr Status of DTR line (0 or 1; required by some interfaces).
513 * @retval SR_OK Success.
514 * @retval SR_ERR Failure.
518 SR_PRIV int serial_set_params(struct sr_serial_dev_inst *serial,
519 int baudrate, int bits, int parity, int stopbits,
520 int flowcontrol, int rts, int dtr)
525 sr_dbg("Invalid serial port.");
529 sr_spew("Setting serial parameters on port %s.", serial->port);
531 if (!serial->lib_funcs || !serial->lib_funcs->set_params)
533 ret = serial->lib_funcs->set_params(serial,
534 baudrate, bits, parity, stopbits,
535 flowcontrol, rts, dtr);
537 serial->comm_params.bit_rate = baudrate;
538 serial->comm_params.data_bits = bits;
539 serial->comm_params.parity_bits = parity ? 1 : 0;
540 serial->comm_params.stop_bits = stopbits;
541 sr_dbg("DBG: %s() rate %d, %d%s%d", __func__,
543 (parity == 0) ? "n" : "x",
551 * Set serial parameters for the specified serial port from parameter string.
553 * @param serial Previously initialized serial port structure.
554 * @param[in] paramstr A serial communication parameters string of the form
555 * "<baudrate>/<bits><parity><stopbits>{/<option>}".\n
556 * Examples: "9600/8n1", "600/7o2/dtr=1/rts=0" or "460800/8n1/flow=2".\n
557 * \<baudrate\>=integer Baud rate.\n
558 * \<bits\>=5|6|7|8 Number of data bits.\n
559 * \<parity\>=n|e|o None, even, odd.\n
560 * \<stopbits\>=1|2 One or two stop bits.\n
562 * dtr=0|1 Set DTR off resp. on.\n
563 * flow=0|1|2 Flow control. 0 for none, 1 for RTS/CTS, 2 for XON/XOFF.\n
564 * rts=0|1 Set RTS off resp. on.\n
565 * Please note that values and combinations of these parameters must be
566 * supported by the concrete serial interface hardware and the drivers for it.
568 * @retval SR_OK Success.
569 * @retval SR_ERR Failure.
573 SR_PRIV int serial_set_paramstr(struct sr_serial_dev_inst *serial,
574 const char *paramstr)
577 #define SERIAL_COMM_SPEC "^(\\d+)(/([5678])([neo])([12]))?(.*)$"
582 int speed, databits, parity, stopbits, flow, rts, dtr, i;
583 char *mstr, **opts, **kv;
587 parity = SP_PARITY_NONE;
590 sr_spew("Parsing parameters from \"%s\".", paramstr);
591 reg = g_regex_new(SERIAL_COMM_SPEC, 0, 0, NULL);
592 if (g_regex_match(reg, paramstr, 0, &match)) {
593 if ((mstr = g_match_info_fetch(match, 1)))
594 speed = strtoul(mstr, NULL, 10);
596 if ((mstr = g_match_info_fetch(match, 3)) && mstr[0])
597 databits = strtoul(mstr, NULL, 10);
599 if ((mstr = g_match_info_fetch(match, 4)) && mstr[0]) {
602 parity = SP_PARITY_NONE;
605 parity = SP_PARITY_EVEN;
608 parity = SP_PARITY_ODD;
613 if ((mstr = g_match_info_fetch(match, 5)) && mstr[0])
614 stopbits = strtoul(mstr, NULL, 10);
616 if ((mstr = g_match_info_fetch(match, 6)) && mstr[0] != '\0') {
617 if (mstr[0] != '/') {
618 sr_dbg("missing separator before extra options");
621 /* A set of "key=value" options separated by / */
622 opts = g_strsplit(mstr + 1, "/", 0);
623 for (i = 0; opts[i]; i++) {
624 kv = g_strsplit(opts[i], "=", 2);
625 if (!strncmp(kv[0], "rts", 3)) {
628 else if (kv[1][0] == '0')
631 sr_dbg("invalid value for rts: %c", kv[1][0]);
634 } else if (!strncmp(kv[0], "dtr", 3)) {
637 else if (kv[1][0] == '0')
640 sr_dbg("invalid value for dtr: %c", kv[1][0]);
643 } else if (!strncmp(kv[0], "flow", 4)) {
646 else if (kv[1][0] == '1')
648 else if (kv[1][0] == '2')
651 sr_dbg("invalid value for flow: %c", kv[1][0]);
662 g_match_info_unref(match);
664 sr_spew("Got params: rate %d, frame %d/%d/%d, flow %d, rts %d, dtr %d.",
665 speed, databits, parity, stopbits, flow, rts, dtr);
668 return serial_set_params(serial, speed, databits, parity,
669 stopbits, flow, rts, dtr);
671 sr_dbg("Could not infer speed from parameter string.");
677 * Read a line from the specified serial port.
679 * @param[in] serial Previously initialized serial port structure.
680 * @param[out] buf Buffer where to store the bytes that are read.
681 * @param[in] buflen Size of the buffer.
682 * @param[in] timeout_ms How long to wait for a line to come in.
684 * Reading stops when CR or LF is found, which is stripped from the buffer.
686 * @retval SR_OK Success.
687 * @retval SR_ERR Failure.
691 SR_PRIV int serial_readline(struct sr_serial_dev_inst *serial,
692 char **buf, int *buflen, gint64 timeout_ms)
694 gint64 start, remaining;
698 sr_dbg("Invalid serial port.");
702 if (!dev_is_supported(serial)) {
703 sr_dbg("Cannot use unopened serial port %s.", serial->port);
707 start = g_get_monotonic_time();
708 remaining = timeout_ms;
713 len = maxlen - *buflen - 1;
716 len = serial_read_blocking(serial, *buf + *buflen, 1, remaining);
719 *(*buf + *buflen) = '\0';
720 if (*buflen > 0 && (*(*buf + *buflen - 1) == '\r'
721 || *(*buf + *buflen - 1) == '\n')) {
722 /* Strip CR/LF and terminate. */
723 *(*buf + --*buflen) = '\0';
727 /* Reduce timeout by time elapsed. */
728 remaining = timeout_ms - ((g_get_monotonic_time() - start) / 1000);
736 sr_dbg("Received %d: '%s'.", *buflen, *buf);
742 * Try to find a valid packet in a serial data stream.
744 * @param serial Previously initialized serial port structure.
745 * @param buf Buffer containing the bytes to write.
746 * @param buflen Size of the buffer.
747 * @param[in] packet_size Size, in bytes, of a valid packet.
748 * @param is_valid Callback that assesses whether the packet is valid or not.
749 * @param[in] timeout_ms The timeout after which, if no packet is detected, to
752 * @retval SR_OK Valid packet was found within the given timeout.
753 * @retval SR_ERR Failure.
757 SR_PRIV int serial_stream_detect(struct sr_serial_dev_inst *serial,
758 uint8_t *buf, size_t *buflen,
760 packet_valid_callback is_valid,
763 uint64_t start, time, byte_delay_us;
764 size_t ibuf, i, maxlen;
769 sr_dbg("Detecting packets on %s (timeout = %" PRIu64 "ms).",
770 serial->port, timeout_ms);
772 if (maxlen < (packet_size * 2) ) {
773 sr_err("Buffer size must be at least twice the packet size.");
777 /* Assume 8n1 transmission. That is 10 bits for every byte. */
778 byte_delay_us = serial_timeout(serial, 1) * 1000;
779 start = g_get_monotonic_time();
782 while (ibuf < maxlen) {
783 len = serial_read_nonblocking(serial, &buf[ibuf], 1);
786 } else if (len == 0) {
787 /* No logging, already done in serial_read(). */
789 /* Error reading byte, but continuing anyway. */
792 time = g_get_monotonic_time() - start;
795 if ((ibuf - i) >= packet_size) {
797 /* We have at least a packet's worth of data. */
798 text = sr_hexdump_new(&buf[i], packet_size);
799 sr_spew("Trying packet: %s", text->str);
800 sr_hexdump_free(text);
801 if (is_valid(&buf[i])) {
802 sr_spew("Found valid %zu-byte packet after "
803 "%" PRIu64 "ms.", (ibuf - i), time);
807 sr_spew("Got %zu bytes, but not a valid "
808 "packet.", (ibuf - i));
810 /* Not a valid packet. Continue searching. */
813 if (time >= timeout_ms) {
815 sr_dbg("Detection timed out after %" PRIu64 "ms.", time);
819 g_usleep(byte_delay_us);
824 sr_err("Didn't find a valid packet (read %zu bytes).", *buflen);
830 * Extract the serial device and options from the options linked list.
832 * @param options List of options passed from the command line.
833 * @param serial_device Pointer where to store the extracted serial device.
834 * @param serial_options Pointer where to store the optional extracted serial
837 * @return SR_OK if a serial_device is found, SR_ERR if no device is found. The
838 * returned string should not be freed by the caller.
842 SR_PRIV int sr_serial_extract_options(GSList *options,
843 const char **serial_device, const char **serial_options)
846 struct sr_config *src;
848 *serial_device = NULL;
850 for (l = options; l; l = l->next) {
854 *serial_device = g_variant_get_string(src->data, NULL);
855 sr_dbg("Parsed serial device: %s.", *serial_device);
857 case SR_CONF_SERIALCOMM:
858 *serial_options = g_variant_get_string(src->data, NULL);
859 sr_dbg("Parsed serial options: %s.", *serial_options);
864 if (!*serial_device) {
865 sr_dbg("No serial device specified.");
873 SR_PRIV int serial_source_add(struct sr_session *session,
874 struct sr_serial_dev_inst *serial, int events, int timeout,
875 sr_receive_data_callback cb, void *cb_data)
877 if ((events & (G_IO_IN | G_IO_ERR)) && (events & G_IO_OUT)) {
878 sr_err("Cannot poll input/error and output simultaneously.");
882 if (!dev_is_supported(serial)) {
883 sr_err("Invalid serial port.");
887 if (!serial->lib_funcs || !serial->lib_funcs->setup_source_add)
890 return serial->lib_funcs->setup_source_add(session, serial,
891 events, timeout, cb, cb_data);
895 SR_PRIV int serial_source_remove(struct sr_session *session,
896 struct sr_serial_dev_inst *serial)
898 if (!dev_is_supported(serial)) {
899 sr_err("Invalid serial port.");
903 if (!serial->lib_funcs || !serial->lib_funcs->setup_source_remove)
906 return serial->lib_funcs->setup_source_remove(session, serial);
910 * Create/allocate a new sr_serial_port structure.
912 * @param name The OS dependent name of the serial port. Must not be NULL.
913 * @param description An end user friendly description for the serial port.
914 * Can be NULL (in that case the empty string is used
917 * @return The newly allocated sr_serial_port struct.
919 static struct sr_serial_port *sr_serial_new(const char *name,
920 const char *description)
922 struct sr_serial_port *serial;
927 serial = g_malloc0(sizeof(*serial));
928 serial->name = g_strdup(name);
929 serial->description = g_strdup(description ? description : "");
935 * Free a previously allocated sr_serial_port structure.
937 * @param serial The sr_serial_port struct to free. Must not be NULL.
939 SR_API void sr_serial_free(struct sr_serial_port *serial)
943 g_free(serial->name);
944 g_free(serial->description);
948 static GSList *append_port_list(GSList *devs, const char *name, const char *desc)
950 return g_slist_append(devs, sr_serial_new(name, desc));
954 * List available serial devices.
956 * @return A GSList of strings containing the path of the serial devices or
957 * NULL if no serial device is found. The returned list must be freed
960 SR_API GSList *sr_serial_list(const struct sr_dev_driver *driver)
963 GSList *(*list_func)(GSList *list, sr_ser_list_append_t append);
965 /* Currently unused, but will be used by some drivers later on. */
969 if (ser_lib_funcs_libsp && ser_lib_funcs_libsp->list) {
970 list_func = ser_lib_funcs_libsp->list;
971 tty_devs = list_func(tty_devs, append_port_list);
973 if (ser_lib_funcs_hid && ser_lib_funcs_hid->list) {
974 list_func = ser_lib_funcs_hid->list;
975 tty_devs = list_func(tty_devs, append_port_list);
977 if (ser_lib_funcs_bt && ser_lib_funcs_bt->list) {
978 list_func = ser_lib_funcs_bt->list;
979 tty_devs = list_func(tty_devs, append_port_list);
985 static GSList *append_port_find(GSList *devs, const char *name)
990 return g_slist_append(devs, g_strdup(name));
994 * Find USB serial devices via the USB vendor ID and product ID.
996 * @param[in] vendor_id Vendor ID of the USB device.
997 * @param[in] product_id Product ID of the USB device.
999 * @return A GSList of strings containing the path of the serial device or
1000 * NULL if no serial device is found. The returned list must be freed
1005 SR_PRIV GSList *sr_serial_find_usb(uint16_t vendor_id, uint16_t product_id)
1008 GSList *(*find_func)(GSList *list, sr_ser_find_append_t append,
1009 uint16_t vid, uint16_t pid);
1012 if (ser_lib_funcs_libsp && ser_lib_funcs_libsp->find_usb) {
1013 find_func = ser_lib_funcs_libsp->find_usb;
1014 tty_devs = find_func(tty_devs, append_port_find,
1015 vendor_id, product_id);
1017 if (ser_lib_funcs_hid && ser_lib_funcs_hid->find_usb) {
1018 find_func = ser_lib_funcs_hid->find_usb;
1019 tty_devs = find_func(tty_devs, append_port_find,
1020 vendor_id, product_id);
1027 SR_PRIV int serial_timeout(struct sr_serial_dev_inst *port, int num_bytes)
1029 int bits, baud, ret, timeout_ms;
1031 /* Get the bitrate and frame length. */
1033 if (port->lib_funcs && port->lib_funcs->get_frame_format) {
1034 ret = port->lib_funcs->get_frame_format(port, &baud, &bits);
1038 baud = port->comm_params.bit_rate;
1039 bits = 1 + port->comm_params.data_bits +
1040 port->comm_params.parity_bits +
1041 port->comm_params.stop_bits;
1044 /* Derive the timeout. Default to 1s. */
1047 /* Throw in 10ms for misc OS overhead. */
1049 timeout_ms += ((1000.0 / baud) * bits) * num_bytes;
1057 /* TODO Put fallback.c content here? */