+ return SR_ERR_ARG;
+ }
+ sp_set_config_stopbits(config, stopbits);
+ sp_set_config_rts(config, flowcontrol == 1 ? SP_RTS_FLOW_CONTROL : rts);
+ sp_set_config_cts(config, flowcontrol == 1 ? SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE);
+ sp_set_config_dtr(config, dtr);
+ sp_set_config_dsr(config, SP_DSR_IGNORE);
+ sp_set_config_xon_xoff(config, flowcontrol == 2 ? SP_XONXOFF_INOUT : SP_XONXOFF_DISABLED);
+
+ ret = sp_set_config(serial->data, config);
+ sp_free_config(config);
+
+ switch (ret) {
+ case SP_ERR_ARG:
+ sr_err("Invalid arguments for setting serial port parameters.");
+ return SR_ERR_ARG;
+ case SP_ERR_FAIL:
+ error = sp_last_error_message();
+ sr_err("Error setting serial port parameters: %s.", error);
+ sp_free_error_message(error);
+ return SR_ERR;
+ }
+
+ return SR_OK;
+}
+
+/**
+ * Set serial parameters for the specified serial port.
+ *
+ * @param serial Previously initialized serial port structure.
+ * @param paramstr A serial communication parameters string, in the form
+ * of <speed>/<data bits><parity><stopbits><flow>, for example "9600/8n1" or
+ * "600/7o2" or "460800/8n1/flow=2" where flow is 0 for none, 1 for rts/cts and 2 for xon/xoff.
+ *
+ * @return SR_OK upon success, SR_ERR upon failure.
+ */
+#define SERIAL_COMM_SPEC "^(\\d+)/([5678])([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, flow, rts, dtr, i;
+ char *mstr, **opts, **kv;
+
+ speed = databits = parity = stopbits = flow = 0;
+ rts = dtr = -1;
+ sr_spew("Parsing parameters from \"%s\".", paramstr);
+ 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)))
+ speed = strtoul(mstr, NULL, 10);
+ g_free(mstr);
+ if ((mstr = g_match_info_fetch(match, 2)))
+ databits = strtoul(mstr, NULL, 10);
+ g_free(mstr);
+ if ((mstr = g_match_info_fetch(match, 3))) {
+ switch (mstr[0]) {
+ case 'n':
+ parity = SERIAL_PARITY_NONE;
+ break;
+ case 'e':
+ parity = SERIAL_PARITY_EVEN;
+ break;
+ case 'o':
+ parity = SERIAL_PARITY_ODD;
+ break;
+ }
+ }
+ g_free(mstr);
+ 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;
+ }
+ } else if (!strncmp(kv[0], "flow", 4)) {
+ if (kv[1][0] == '0')
+ flow = 0;
+ else if (kv[1][0] == '1')
+ flow = 1;
+ else if (kv[1][0] == '2')
+ flow = 2;
+ else {
+ sr_dbg("invalid value for flow: %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, flow, rts, dtr);
+ } else {
+ sr_dbg("Could not infer speed from parameter string.");
+ return SR_ERR_ARG;
+ }
+}
+
+/**
+ * Read a line from the specified serial port.
+ *
+ * @param serial Previously initialized serial port structure.
+ * @param buf Buffer where to store the bytes that are read.
+ * @param buflen Size of the buffer.
+ * @param timeout_ms How long to wait for a line to come in.
+ *
+ * Reading stops when CR of LR is found, which is stripped from the buffer.
+ *
+ * @return SR_OK on success, SR_ERR on failure.
+ */
+SR_PRIV int serial_readline(struct sr_serial_dev_inst *serial, char **buf,
+ int *buflen, gint64 timeout_ms)
+{
+ gint64 start;
+ int maxlen, len;
+
+ if (!serial || serial->fd == -1) {
+ sr_dbg("Invalid serial port.");