+/**
+ * Open the specified serial port.
+ *
+ * @param serial Previously initialized serial port structure.
+ * @param flags Flags to use when opening the serial port. Possible flags
+ * include SERIAL_RDWR, SERIAL_RDONLY, SERIAL_NONBLOCK.
+ *
+ * If the serial structure contains a serialcomm string, it will be
+ * passed to serial_set_paramstr() after the port is opened.
+ *
+ * @return SR_OK on success, SR_ERR on failure.
+ */
+SR_PRIV int serial_open(struct sr_serial_dev_inst *serial, int flags)
+{
+ int ret;
+ char *error;
+
+ if (!serial) {
+ sr_dbg("Invalid serial port.");
+ return SR_ERR;
+ }
+
+ sr_spew("Opening serial port '%s' (flags %d).", serial->port, flags);
+
+ sp_get_port_by_name(serial->port, &serial->data);
+
+ ret = sp_open(serial->data, flags);
+
+ switch (ret) {
+ case SP_ERR_ARG:
+ sr_err("Attempt to open serial port with invalid parameters.");
+ return SR_ERR_ARG;
+ case SP_ERR_FAIL:
+ error = sp_last_error_message();
+ sr_err("Error opening port: %s.", error);
+ sp_free_error_message(error);
+ return SR_ERR;
+ }
+
+#ifndef _WIN32
+ serial->fd = serial->data->fd;
+#endif
+
+ if (serial->serialcomm)
+ return serial_set_paramstr(serial, serial->serialcomm);
+ else
+ return SR_OK;
+}
+
+/**
+ * Close the specified serial port.
+ *
+ * @param serial Previously initialized serial port structure.
+ *
+ * @return SR_OK on success, SR_ERR on failure.
+ */
+SR_PRIV int serial_close(struct sr_serial_dev_inst *serial)
+{
+ int ret;
+ char *error;
+
+ if (!serial) {
+ sr_dbg("Invalid serial port.");
+ return SR_ERR;
+ }
+
+ if (serial->fd == -1) {
+ sr_dbg("Cannot close unopened serial port %s (fd %d).",
+ serial->port, serial->fd);
+ return SR_ERR;
+ }
+
+ sr_spew("Closing serial port %s (fd %d).", serial->port, serial->fd);
+
+ ret = sp_close(serial->data);
+
+ switch (ret) {
+ case SP_ERR_ARG:
+ sr_err("Attempt to close an invalid serial port.");
+ return SR_ERR_ARG;
+ case SP_ERR_FAIL:
+ error = sp_last_error_message();
+ sr_err("Error closing port: %s.", error);
+ sp_free_error_message(error);
+ return SR_ERR;
+ }
+
+ serial->fd = -1;
+
+ return SR_OK;
+}
+
+/**
+ * Flush serial port buffers.
+ *
+ * @param serial Previously initialized serial port structure.
+ *
+ * @return SR_OK on success, SR_ERR on failure.
+ */
+SR_PRIV int serial_flush(struct sr_serial_dev_inst *serial)
+{
+ int ret;
+ char *error;
+
+ if (!serial) {
+ sr_dbg("Invalid serial port.");
+ return SR_ERR;
+ }
+
+ if (serial->fd == -1) {
+ sr_dbg("Cannot flush unopened serial port %s (fd %d).",
+ serial->port, serial->fd);
+ return SR_ERR;
+ }
+
+ sr_spew("Flushing serial port %s (fd %d).", serial->port, serial->fd);
+
+ ret = sp_flush(serial->data);
+
+ switch (ret) {
+ case SP_ERR_ARG:
+ sr_err("Attempt to flush an invalid serial port.");
+ return SR_ERR_ARG;
+ case SP_ERR_FAIL:
+ error = sp_last_error_message();
+ sr_err("Error flushing port: %s.", error);
+ sp_free_error_message(error);
+ return SR_ERR;
+ }
+
+ return SR_OK;
+}
+
+/**
+ * Write a number of bytes to the specified serial port.
+ *
+ * @param serial Previously initialized serial port structure.
+ * @param buf Buffer containing the bytes to write.
+ * @param count Number of bytes to write.
+ *
+ * @return The number of bytes written, or a negative error code upon failure.
+ */
+SR_PRIV int serial_write(struct sr_serial_dev_inst *serial,
+ const void *buf, size_t count)
+{
+ ssize_t ret;
+ char *error;
+
+ if (!serial) {
+ sr_dbg("Invalid serial port.");
+ return SR_ERR;
+ }
+
+ if (serial->fd == -1) {
+ sr_dbg("Cannot use unopened serial port %s (fd %d).",
+ serial->port, serial->fd);
+ return SR_ERR;
+ }
+
+ ret = sp_write(serial->data, buf, count);
+
+ switch (ret) {
+ case SP_ERR_ARG:
+ sr_err("Attempted serial port write with invalid arguments.");
+ return SR_ERR_ARG;
+ case SP_ERR_FAIL:
+ error = sp_last_error_message();
+ sr_err("Write error: %s.", error);
+ sp_free_error_message(error);
+ return SR_ERR;
+ }
+
+ sr_spew("Wrote %d/%d bytes (fd %d).", ret, count, serial->fd);
+
+ return ret;
+}
+
+/**
+ * Read a number of bytes from the specified serial port.
+ *
+ * @param serial Previously initialized serial port structure.
+ * @param buf Buffer where to store the bytes that are read.
+ * @param count The number of bytes to read.
+ *
+ * @return The number of bytes read, or a negative error code upon failure.
+ */
+SR_PRIV int serial_read(struct sr_serial_dev_inst *serial, void *buf,
+ size_t count)
+{
+ ssize_t ret;
+ char *error;
+
+ if (!serial) {
+ sr_dbg("Invalid serial port.");
+ return SR_ERR;
+ }
+
+ if (serial->fd == -1) {
+ sr_dbg("Cannot use unopened serial port %s (fd %d).",
+ serial->port, serial->fd);
+ return SR_ERR;
+ }
+
+ ret = sp_read(serial->data, buf, count);
+
+ switch (ret) {
+ case SP_ERR_ARG:
+ sr_err("Attempted serial port read with invalid arguments.");
+ return SR_ERR_ARG;
+ case SP_ERR_FAIL:
+ error = sp_last_error_message();
+ sr_err("Read error: %s.", error);
+ sp_free_error_message(error);
+ return SR_ERR;
+ }
+
+ sr_spew("Read %d/%d bytes (fd %d).", ret, count, serial->fd);
+
+ return ret;
+}
+
+/**
+ * Set serial parameters for the specified serial port.
+ *
+ * @param serial Previously initialized serial port structure.
+ * @param baudrate The baudrate to set.
+ * @param bits The number of data bits to use.
+ * @param parity The parity setting to use (0 = none, 1 = even, 2 = odd).
+ * @param stopbits The number of stop bits to use (1 or 2).
+ * @param flowcontrol The flow control settings to use (0 = none, 1 = RTS/CTS,
+ * 2 = XON/XOFF).
+ *
+ * @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 rts, int dtr)
+{
+ int ret;
+ char *error;
+
+ if (!serial) {
+ sr_dbg("Invalid serial port.");
+ return SR_ERR;
+ }
+
+ if (serial->fd == -1) {
+ sr_dbg("Cannot configure unopened serial port %s (fd %d).",
+ serial->port, serial->fd);
+ return SR_ERR;
+ }
+
+ sr_spew("Setting serial parameters on port %s (fd %d).", serial->port,
+ serial->fd);
+
+ ret = sp_set_params(serial->data, baudrate, bits, parity, stopbits,
+ flowcontrol, rts, dtr);
+
+ 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+)/([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, 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;
+ }