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>
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.
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.
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/>.
25 #include <glib/gstdio.h>
26 #include <libserialport.h>
27 #include "libsigrok.h"
28 #include "libsigrok-internal.h"
30 #define LOG_PREFIX "serial"
33 * Open the specified serial port.
35 * @param serial Previously initialized serial port structure.
36 * @param[in] flags Flags to use when opening the serial port. Possible flags
37 * include SERIAL_RDWR, SERIAL_RDONLY.
39 * If the serial structure contains a serialcomm string, it will be
40 * passed to serial_set_paramstr() after the port is opened.
42 * @retval SR_OK Success.
43 * @retval SR_ERR Failure.
45 SR_PRIV int serial_open(struct sr_serial_dev_inst *serial, int flags)
52 sr_dbg("Invalid serial port.");
56 sr_spew("Opening serial port '%s' (flags %d).", serial->port, flags);
58 sp_get_port_by_name(serial->port, &serial->data);
60 if (flags & SERIAL_RDWR)
61 sp_flags = (SP_MODE_READ | SP_MODE_WRITE);
62 else if (flags & SERIAL_RDONLY)
63 sp_flags = SP_MODE_READ;
65 ret = sp_open(serial->data, sp_flags);
69 sr_err("Attempt to open serial port with invalid parameters.");
72 error = sp_last_error_message();
73 sr_err("Error opening port (%d): %s.",
74 sp_last_error_code(), error);
75 sp_free_error_message(error);
79 if (serial->serialcomm)
80 return serial_set_paramstr(serial, serial->serialcomm);
86 * Close the specified serial port.
88 * @param serial Previously initialized serial port structure.
90 * @retval SR_OK Success.
91 * @retval SR_ERR Failure.
93 SR_PRIV int serial_close(struct sr_serial_dev_inst *serial)
99 sr_dbg("Invalid serial port.");
104 sr_dbg("Cannot close unopened serial port %s.", serial->port);
108 sr_spew("Closing serial port %s.", serial->port);
110 ret = sp_close(serial->data);
114 sr_err("Attempt to close an invalid serial port.");
117 error = sp_last_error_message();
118 sr_err("Error closing port (%d): %s.",
119 sp_last_error_code(), error);
120 sp_free_error_message(error);
124 sp_free_port(serial->data);
131 * Flush serial port buffers.
133 * @param serial Previously initialized serial port structure.
135 * @retval SR_OK Success.
136 * @retval SR_ERR Failure.
138 SR_PRIV int serial_flush(struct sr_serial_dev_inst *serial)
144 sr_dbg("Invalid serial port.");
149 sr_dbg("Cannot flush unopened serial port %s.", serial->port);
153 sr_spew("Flushing serial port %s.", serial->port);
155 ret = sp_flush(serial->data, SP_BUF_BOTH);
159 sr_err("Attempt to flush an invalid serial port.");
162 error = sp_last_error_message();
163 sr_err("Error flushing port (%d): %s.",
164 sp_last_error_code(), error);
165 sp_free_error_message(error);
172 static int _serial_write(struct sr_serial_dev_inst *serial,
173 const void *buf, size_t count, int nonblocking, unsigned int timeout_ms)
179 sr_dbg("Invalid serial port.");
184 sr_dbg("Cannot use unopened serial port %s.", serial->port);
189 ret = sp_nonblocking_write(serial->data, buf, count);
191 ret = sp_blocking_write(serial->data, buf, count, timeout_ms);
195 sr_err("Attempted serial port write with invalid arguments.");
198 error = sp_last_error_message();
199 sr_err("Write error (%d): %s.", sp_last_error_code(), error);
200 sp_free_error_message(error);
204 sr_spew("Wrote %d/%d bytes.", ret, count);
210 * Write a number of bytes to the specified serial port, blocking until finished.
212 * @param serial Previously initialized serial port structure.
213 * @param[in] buf Buffer containing the bytes to write.
214 * @param[in] count Number of bytes to write.
215 * @param[in] timeout_ms Timeout in ms, or 0 for no timeout.
217 * @retval SR_ERR_ARG Invalid argument.
218 * @retval SR_ERR Other error.
219 * @retval other The number of bytes written. If this is less than the number
220 * specified in the call, the timeout was reached.
222 SR_PRIV int serial_write_blocking(struct sr_serial_dev_inst *serial,
223 const void *buf, size_t count, unsigned int timeout_ms)
225 return _serial_write(serial, buf, count, 0, timeout_ms);
229 * Write a number of bytes to the specified serial port, return immediately.
231 * @param serial Previously initialized serial port structure.
232 * @param[in] buf Buffer containing the bytes to write.
233 * @param[in] count Number of bytes to write.
235 * @retval SR_ERR_ARG Invalid argument.
236 * @retval SR_ERR Other error.
237 * @retval other The number of bytes written.
239 SR_PRIV int serial_write_nonblocking(struct sr_serial_dev_inst *serial,
240 const void *buf, size_t count)
242 return _serial_write(serial, buf, count, 1, 0);
245 static int _serial_read(struct sr_serial_dev_inst *serial, void *buf,
246 size_t count, int nonblocking, unsigned int timeout_ms)
252 sr_dbg("Invalid serial port.");
257 sr_dbg("Cannot use unopened serial port %s.", serial->port);
262 ret = sp_nonblocking_read(serial->data, buf, count);
264 ret = sp_blocking_read(serial->data, buf, count, timeout_ms);
268 sr_err("Attempted serial port read with invalid arguments.");
271 error = sp_last_error_message();
272 sr_err("Read error (%d): %s.", sp_last_error_code(), error);
273 sp_free_error_message(error);
278 sr_spew("Read %d/%d bytes.", ret, count);
284 * Read a number of bytes from the specified serial port, block until finished.
286 * @param serial Previously initialized serial port structure.
287 * @param buf Buffer where to store the bytes that are read.
288 * @param[in] count The number of bytes to read.
289 * @param[in] timeout_ms Timeout in ms, or 0 for no timeout.
291 * @retval SR_ERR_ARG Invalid argument.
292 * @retval SR_ERR Other error.
293 * @retval other The number of bytes read. If this is less than the number
294 * requested, the timeout was reached.
296 SR_PRIV int serial_read_blocking(struct sr_serial_dev_inst *serial, void *buf,
297 size_t count, unsigned int timeout_ms)
299 return _serial_read(serial, buf, count, 0, timeout_ms);
303 * Try to read up to @a count bytes from the specified serial port, return
304 * immediately with what's available.
306 * @param serial Previously initialized serial port structure.
307 * @param buf Buffer where to store the bytes that are read.
308 * @param[in] count The number of bytes to read.
310 * @retval SR_ERR_ARG Invalid argument.
311 * @retval SR_ERR Other error.
312 * @retval other The number of bytes read.
314 SR_PRIV int serial_read_nonblocking(struct sr_serial_dev_inst *serial, void *buf,
317 return _serial_read(serial, buf, count, 1, 0);
321 * Set serial parameters for the specified serial port.
323 * @param serial Previously initialized serial port structure.
324 * @param[in] baudrate The baudrate to set.
325 * @param[in] bits The number of data bits to use (5, 6, 7 or 8).
326 * @param[in] parity The parity setting to use (0 = none, 1 = even, 2 = odd).
327 * @param[in] stopbits The number of stop bits to use (1 or 2).
328 * @param[in] flowcontrol The flow control settings to use (0 = none,
329 * 1 = RTS/CTS, 2 = XON/XOFF).
330 * @param[in] rts Status of RTS line (0 or 1; required by some interfaces).
331 * @param[in] dtr Status of DTR line (0 or 1; required by some interfaces).
333 * @retval SR_OK Success.
334 * @retval SR_ERR Failure.
336 SR_PRIV int serial_set_params(struct sr_serial_dev_inst *serial, int baudrate,
337 int bits, int parity, int stopbits,
338 int flowcontrol, int rts, int dtr)
342 struct sp_port_config *config;
345 sr_dbg("Invalid serial port.");
350 sr_dbg("Cannot configure unopened serial port %s.", serial->port);
354 sr_spew("Setting serial parameters on port %s.", serial->port);
356 sp_new_config(&config);
357 sp_set_config_baudrate(config, baudrate);
358 sp_set_config_bits(config, bits);
361 sp_set_config_parity(config, SP_PARITY_NONE);
364 sp_set_config_parity(config, SP_PARITY_EVEN);
367 sp_set_config_parity(config, SP_PARITY_ODD);
372 sp_set_config_stopbits(config, stopbits);
373 sp_set_config_rts(config, flowcontrol == 1 ? SP_RTS_FLOW_CONTROL : rts);
374 sp_set_config_cts(config, flowcontrol == 1 ? SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE);
375 sp_set_config_dtr(config, dtr);
376 sp_set_config_dsr(config, SP_DSR_IGNORE);
377 sp_set_config_xon_xoff(config, flowcontrol == 2 ? SP_XONXOFF_INOUT : SP_XONXOFF_DISABLED);
379 ret = sp_set_config(serial->data, config);
380 sp_free_config(config);
384 sr_err("Invalid arguments for setting serial port parameters.");
387 error = sp_last_error_message();
388 sr_err("Error setting serial port parameters (%d): %s.",
389 sp_last_error_code(), error);
390 sp_free_error_message(error);
398 * Set serial parameters for the specified serial port from parameter string.
400 * @param serial Previously initialized serial port structure.
401 * @param[in] paramstr A serial communication parameters string of the form
402 * "<baudrate>/<bits><parity><stopbits>{/<option>}".\n
403 * Examples: "9600/8n1", "600/7o2/dtr=1/rts=0" or "460800/8n1/flow=2".\n
404 * \<baudrate\>=integer Baud rate.\n
405 * \<bits\>=5|6|7|8 Number of data bits.\n
406 * \<parity\>=n|e|o None, even, odd.\n
407 * \<stopbits\>=1|2 One or two stop bits.\n
409 * dtr=0|1 Set DTR off resp. on.\n
410 * flow=0|1|2 Flow control. 0 for none, 1 for RTS/CTS, 2 for XON/XOFF.\n
411 * rts=0|1 Set RTS off resp. on.\n
412 * Please note that values and combinations of these parameters must be
413 * supported by the concrete serial interface hardware and the drivers for it.
414 * @retval SR_OK Success.
415 * @retval SR_ERR Failure.
417 SR_PRIV int serial_set_paramstr(struct sr_serial_dev_inst *serial,
418 const char *paramstr)
420 #define SERIAL_COMM_SPEC "^(\\d+)/([5678])([neo])([12])(.*)$"
424 int speed, databits, parity, stopbits, flow, rts, dtr, i;
425 char *mstr, **opts, **kv;
427 speed = databits = parity = stopbits = flow = 0;
429 sr_spew("Parsing parameters from \"%s\".", paramstr);
430 reg = g_regex_new(SERIAL_COMM_SPEC, 0, 0, NULL);
431 if (g_regex_match(reg, paramstr, 0, &match)) {
432 if ((mstr = g_match_info_fetch(match, 1)))
433 speed = strtoul(mstr, NULL, 10);
435 if ((mstr = g_match_info_fetch(match, 2)))
436 databits = strtoul(mstr, NULL, 10);
438 if ((mstr = g_match_info_fetch(match, 3))) {
441 parity = SERIAL_PARITY_NONE;
444 parity = SERIAL_PARITY_EVEN;
447 parity = SERIAL_PARITY_ODD;
452 if ((mstr = g_match_info_fetch(match, 4)))
453 stopbits = strtoul(mstr, NULL, 10);
455 if ((mstr = g_match_info_fetch(match, 5)) && mstr[0] != '\0') {
456 if (mstr[0] != '/') {
457 sr_dbg("missing separator before extra options");
460 /* A set of "key=value" options separated by / */
461 opts = g_strsplit(mstr + 1, "/", 0);
462 for (i = 0; opts[i]; i++) {
463 kv = g_strsplit(opts[i], "=", 2);
464 if (!strncmp(kv[0], "rts", 3)) {
467 else if (kv[1][0] == '0')
470 sr_dbg("invalid value for rts: %c", kv[1][0]);
473 } else if (!strncmp(kv[0], "dtr", 3)) {
476 else if (kv[1][0] == '0')
479 sr_dbg("invalid value for dtr: %c", kv[1][0]);
482 } else if (!strncmp(kv[0], "flow", 4)) {
485 else if (kv[1][0] == '1')
487 else if (kv[1][0] == '2')
490 sr_dbg("invalid value for flow: %c", kv[1][0]);
501 g_match_info_unref(match);
505 return serial_set_params(serial, speed, databits, parity,
506 stopbits, flow, rts, dtr);
508 sr_dbg("Could not infer speed from parameter string.");
514 * Read a line from the specified serial port.
516 * @param serial Previously initialized serial port structure.
517 * @param buf Buffer where to store the bytes that are read.
518 * @param buflen Size of the buffer.
519 * @param[in] timeout_ms How long to wait for a line to come in.
521 * Reading stops when CR of LR is found, which is stripped from the buffer.
523 * @retval SR_OK Success.
524 * @retval SR_ERR Failure.
526 SR_PRIV int serial_readline(struct sr_serial_dev_inst *serial, char **buf,
527 int *buflen, gint64 timeout_ms)
529 gint64 start, remaining;
533 sr_dbg("Invalid serial port.");
538 sr_dbg("Cannot use unopened serial port %s.", serial->port);
542 start = g_get_monotonic_time();
543 remaining = timeout_ms;
548 len = maxlen - *buflen - 1;
551 len = sp_blocking_read(serial->data, *buf + *buflen, 1, remaining);
554 *(*buf + *buflen) = '\0';
555 if (*buflen > 0 && (*(*buf + *buflen - 1) == '\r'
556 || *(*buf + *buflen - 1) == '\n')) {
557 /* Strip CR/LF and terminate. */
558 *(*buf + --*buflen) = '\0';
562 /* Reduce timeout by time elapsed. */
563 remaining = timeout_ms - ((g_get_monotonic_time() - start) / 1000);
571 sr_dbg("Received %d: '%s'.", *buflen, *buf);
577 * Try to find a valid packet in a serial data stream.
579 * @param serial Previously initialized serial port structure.
580 * @param buf Buffer containing the bytes to write.
581 * @param buflen Size of the buffer.
582 * @param[in] packet_size Size, in bytes, of a valid packet.
583 * @param is_valid Callback that assesses whether the packet is valid or not.
584 * @param[in] timeout_ms The timeout after which, if no packet is detected, to
586 * @param[in] baudrate The baudrate of the serial port. This parameter is not
587 * critical, but it helps fine tune the serial port polling
590 * @retval SR_OK Valid packet was found within the given timeout.
591 * @retval SR_ERR Failure.
593 SR_PRIV int serial_stream_detect(struct sr_serial_dev_inst *serial,
594 uint8_t *buf, size_t *buflen,
596 packet_valid_callback is_valid,
597 uint64_t timeout_ms, int baudrate)
599 uint64_t start, time, byte_delay_us;
600 size_t ibuf, i, maxlen;
605 sr_dbg("Detecting packets on %s (timeout = %" PRIu64
606 "ms, baudrate = %d).", serial->port, timeout_ms, baudrate);
608 if (maxlen < (packet_size / 2) ) {
609 sr_err("Buffer size must be at least twice the packet size.");
613 /* Assume 8n1 transmission. That is 10 bits for every byte. */
614 byte_delay_us = 10 * (1000000 / baudrate);
615 start = g_get_monotonic_time();
618 while (ibuf < maxlen) {
619 len = serial_read_nonblocking(serial, &buf[ibuf], 1);
622 } else if (len == 0) {
623 /* No logging, already done in serial_read(). */
625 /* Error reading byte, but continuing anyway. */
628 time = g_get_monotonic_time() - start;
631 if ((ibuf - i) >= packet_size) {
632 /* We have at least a packet's worth of data. */
633 if (is_valid(&buf[i])) {
634 sr_spew("Found valid %d-byte packet after "
635 "%" PRIu64 "ms.", (ibuf - i), time);
639 sr_spew("Got %d bytes, but not a valid "
640 "packet.", (ibuf - i));
642 /* Not a valid packet. Continue searching. */
645 if (time >= timeout_ms) {
647 sr_dbg("Detection timed out after %dms.", time);
651 g_usleep(byte_delay_us);
656 sr_err("Didn't find a valid packet (read %d bytes).", *buflen);
662 * Extract the serial device and options from the options linked list.
664 * @param options List of options passed from the command line.
665 * @param serial_device Pointer where to store the exctracted serial device.
666 * @param serial_options Pointer where to store the optional extracted serial
669 * @return SR_OK if a serial_device is found, SR_ERR if no device is found. The
670 * returned string should not be freed by the caller.
672 SR_PRIV int sr_serial_extract_options(GSList *options, const char **serial_device,
673 const char **serial_options)
676 struct sr_config *src;
678 *serial_device = NULL;
680 for (l = options; l; l = l->next) {
684 *serial_device = g_variant_get_string(src->data, NULL);
685 sr_dbg("Parsed serial device: %s", *serial_device);
688 case SR_CONF_SERIALCOMM:
689 *serial_options = g_variant_get_string(src->data, NULL);
690 sr_dbg("Parsed serial options: %s", *serial_options);
695 if (!*serial_device) {
696 sr_dbg("No serial device specified");
704 typedef HANDLE event_handle;
706 typedef int event_handle;
709 SR_PRIV int serial_source_add(struct sr_session *session,
710 struct sr_serial_dev_inst *serial, int events, int timeout,
711 sr_receive_data_callback cb, void *cb_data)
713 enum sp_event mask = 0;
716 if (sp_new_event_set(&serial->event_set) != SP_OK)
719 if (events & G_IO_IN)
720 mask |= SP_EVENT_RX_READY;
721 if (events & G_IO_OUT)
722 mask |= SP_EVENT_TX_READY;
723 if (events & G_IO_ERR)
724 mask |= SP_EVENT_ERROR;
726 if (sp_add_port_events(serial->event_set, serial->data, mask) != SP_OK) {
727 sp_free_event_set(serial->event_set);
731 serial->pollfds = (GPollFD *) g_malloc0(sizeof(GPollFD) * serial->event_set->count);
733 for (i = 0; i < serial->event_set->count; i++) {
735 serial->pollfds[i].fd = ((event_handle *) serial->event_set->handles)[i];
737 mask = serial->event_set->masks[i];
739 if (mask & SP_EVENT_RX_READY)
740 serial->pollfds[i].events |= G_IO_IN;
741 if (mask & SP_EVENT_TX_READY)
742 serial->pollfds[i].events |= G_IO_OUT;
743 if (mask & SP_EVENT_ERROR)
744 serial->pollfds[i].events |= G_IO_ERR;
746 if (sr_session_source_add_pollfd(session, &serial->pollfds[i],
747 timeout, cb, cb_data) != SR_OK)
754 SR_PRIV int serial_source_remove(struct sr_session *session,
755 struct sr_serial_dev_inst *serial)
759 for (i = 0; i < serial->event_set->count; i++)
760 if (sr_session_source_remove_pollfd(session, &serial->pollfds[i]) != SR_OK)
763 g_free(serial->pollfds);
764 sp_free_event_set(serial->event_set);
766 serial->pollfds = NULL;
767 serial->event_set = NULL;
773 * Find USB serial devices via the USB vendor ID and product ID.
775 * @param[in] vendor_id Vendor ID of the USB device.
776 * @param[in] product_id Product ID of the USB device.
778 * @return A GSList of strings containing the path of the serial device or
779 * NULL if no serial device is found. The returned list must be freed
782 SR_PRIV GSList *sr_serial_find_usb(uint16_t vendor_id, uint16_t product_id)
784 GSList *tty_devs = NULL;
785 struct sp_port **ports;
788 if (sp_list_ports(&ports) != SP_OK)
791 for (i=0; ports[i]; i++)
792 if (sp_get_port_transport(ports[i]) == SP_TRANSPORT_USB &&
793 sp_get_port_usb_vid_pid(ports[i], &vid, &pid) == SP_OK &&
794 vid == vendor_id && pid == product_id)
795 tty_devs = g_slist_prepend(tty_devs,
796 g_strdup(sp_get_port_name(ports[i])));
798 sp_free_port_list(ports);
802 SR_PRIV int serial_timeout(struct sr_serial_dev_inst *port, int num_bytes)
804 struct sp_port_config *config;
805 int timeout_ms, bits, baud, tmp;
810 if (sp_new_config(&config) < 0)
815 if (sp_get_config(port->data, config) < 0)
820 if (sp_get_config_bits(config, &tmp) < 0)
823 if (sp_get_config_stopbits(config, &tmp) < 0)
826 if (sp_get_config_baudrate(config, &tmp) < 0)
832 /* Throw in 10ms for misc OS overhead. */
834 timeout_ms += ((1000.0 / baud) * bits) * num_bytes;
837 sp_free_config(config);