]> sigrok.org Git - libsigrok.git/blobdiff - hardware/common/serial.c
serial: allow for extra rts and dtr options in conn string
[libsigrok.git] / hardware / common / serial.c
index 76464970bfe123077898d79431cdd5c373e966dc..6591ec89527fe45d0210bae85cfe74523ad35793 100644 (file)
@@ -32,6 +32,7 @@
 #endif
 #include <stdlib.h>
 #include <errno.h>
+#include <sys/ioctl.h>
 #include <glib.h>
 #include "libsigrok.h"
 #include "libsigrok-internal.h"
@@ -283,9 +284,8 @@ SR_PRIV int serial_read(struct sr_serial_dev_inst *serial, void *buf,
  * @return SR_OK upon success, SR_ERR upon failure.
  */
 SR_PRIV int serial_set_params(struct sr_serial_dev_inst *serial, int baudrate,
-               int bits, int parity, int stopbits, int flowcontrol)
+               int bits, int parity, int stopbits, int flowcontrol, int rts, int dtr)
 {
-
        if (!serial) {
                sr_dbg("Invalid serial port.");
                return SR_ERR;
@@ -341,6 +341,7 @@ SR_PRIV int serial_set_params(struct sr_serial_dev_inst *serial, int baudrate,
 #else
        struct termios term;
        speed_t baud;
+       int ret, controlbits;
 
        if (tcgetattr(serial->fd, &term) < 0) {
                sr_err("tcgetattr() error on port %s (fd %d): %s.",
@@ -504,6 +505,27 @@ SR_PRIV int serial_set_params(struct sr_serial_dev_inst *serial, int baudrate,
                sr_err("tcsetattr() error: %s.", strerror(errno));
                return SR_ERR;
        }
+
+       if (rts != -1) {
+               sr_spew("Setting RTS %s.", rts ? "high" : "low");
+               controlbits = TIOCM_RTS;
+               if ((ret = ioctl(serial->fd, rts ? TIOCMBIS : TIOCMBIC,
+                               &controlbits)) < 0) {
+                       sr_err("Error setting RTS: %s.", strerror(errno));
+                       return SR_ERR;
+               }
+       }
+
+       if (dtr != -1) {
+               sr_spew("Setting DTR %s.", dtr ? "high" : "low");
+               controlbits = TIOCM_DTR;
+               if ((ret = ioctl(serial->fd, dtr ? TIOCMBIS : TIOCMBIC,
+                               &controlbits)) < 0) {
+                       sr_err("Error setting DTR: %s.", strerror(errno));
+                       return SR_ERR;
+               }
+       }
+
 #endif
 
        return SR_OK;
@@ -519,16 +541,17 @@ SR_PRIV int serial_set_params(struct sr_serial_dev_inst *serial, int baudrate,
  *
  * @return SR_OK upon success, SR_ERR upon failure.
  */
-#define SERIAL_COMM_SPEC "^(\\d+)/([78])([neo])([12])$"
+#define SERIAL_COMM_SPEC "^(\\d+)/([78])([neo])([12])(.*)$"
 SR_PRIV int serial_set_paramstr(struct sr_serial_dev_inst *serial,
                const char *paramstr)
 {
        GRegex *reg;
        GMatchInfo *match;
-       int speed, databits, parity, stopbits;
-       char *mstr;
+       int speed, databits, parity, stopbits, rts, dtr, i;
+       char *mstr, **opts, **kv;
 
        speed = databits = parity = stopbits = 0;
+       rts = dtr = -1;
        reg = g_regex_new(SERIAL_COMM_SPEC, 0, 0, NULL);
        if (g_regex_match(reg, paramstr, 0, &match)) {
                if ((mstr = g_match_info_fetch(match, 1)))
@@ -554,12 +577,47 @@ SR_PRIV int serial_set_paramstr(struct sr_serial_dev_inst *serial,
                if ((mstr = g_match_info_fetch(match, 4)))
                        stopbits = strtoul(mstr, NULL, 10);
                g_free(mstr);
+               if ((mstr = g_match_info_fetch(match, 5)) && mstr[0] != '\0') {
+                       if (mstr[0] != '/') {
+                               sr_dbg("missing separator before extra options");
+                               speed = 0;
+                       } else {
+                               /* A set of "key=value" options separated by / */
+                               opts = g_strsplit(mstr + 1, "/", 0);
+                               for (i = 0; opts[i]; i++) {
+                                       kv = g_strsplit(opts[i], "=", 2);
+                                       if (!strncmp(kv[0], "rts", 3)) {
+                                               if (kv[1][0] == '1')
+                                                       rts = 1;
+                                               else if (kv[1][0] == '0')
+                                                       rts = 0;
+                                               else {
+                                                       sr_dbg("invalid value for rts: %c", kv[1][0]);
+                                                       speed = 0;
+                                               }
+                                       } else if (!strncmp(kv[0], "dtr", 3)) {
+                                               if (kv[1][0] == '1')
+                                                       dtr = 1;
+                                               else if (kv[1][0] == '0')
+                                                       dtr = 0;
+                                               else {
+                                                       sr_dbg("invalid value for dtr: %c", kv[1][0]);
+                                                       speed = 0;
+                                               }
+                                       }
+                                       g_strfreev(kv);
+                               }
+                               g_strfreev(opts);
+                       }
+               }
+               g_free(mstr);
        }
        g_match_info_unref(match);
        g_regex_unref(reg);
 
        if (speed)
-               return serial_set_params(serial, speed, databits, parity, stopbits, 0);
+               return serial_set_params(serial, speed, databits, parity, stopbits,
+                               0, rts, dtr);
        else
                return SR_ERR_ARG;
 }
@@ -660,8 +718,6 @@ SR_PRIV int serial_stream_detect(struct sr_serial_dev_inst *serial,
                return SR_ERR;
        }
 
-       timeout_ms *= 1000;
-
        /* Assume 8n1 transmission. That is 10 bits for every byte. */
        byte_delay_us = 10 * (1000000 / baudrate);
        start = g_get_monotonic_time();
@@ -676,11 +732,13 @@ SR_PRIV int serial_stream_detect(struct sr_serial_dev_inst *serial,
                } else {
                        /* Error reading byte, but continuing anyway. */
                }
+
+               time = g_get_monotonic_time() - start;
+               time /= 1000;
+
                if ((ibuf - i) >= packet_size) {
                        /* We have at least a packet's worth of data. */
                        if (is_valid(&buf[i])) {
-                               time = g_get_monotonic_time() - start;
-                               time /= 1000;
                                sr_spew("Found valid %d-byte packet after "
                                        "%" PRIu64 "ms.", (ibuf - i), time);
                                *buflen = ibuf;
@@ -692,9 +750,9 @@ SR_PRIV int serial_stream_detect(struct sr_serial_dev_inst *serial,
                        /* Not a valid packet. Continue searching. */
                        i++;
                }
-               if (g_get_monotonic_time() - start > timeout_ms) {
+               if (time >= timeout_ms) {
                        /* Timeout */
-                       sr_dbg("Detection timed out after %dms.", timeout_ms);
+                       sr_dbg("Detection timed out after %dms.", time);
                        break;
                }
                g_usleep(byte_delay_us);