+#include "linux_termios.h"
+
+/* TCGETX/TCSETX is not available everywhere. */
+#if defined(TCGETX) && defined(TCSETX) && defined(HAVE_TERMIOX)
+#define USE_TERMIOX
+#endif
+#endif
+
+/* TIOCINQ/TIOCOUTQ is not available everywhere. */
+#if !defined(TIOCINQ) && defined(FIONREAD)
+#define TIOCINQ FIONREAD
+#endif
+#if !defined(TIOCOUTQ) && defined(FIONWRITE)
+#define TIOCOUTQ FIONWRITE
+#endif
+
+/* Non-standard baudrates are not available everywhere. */
+#if defined(HAVE_TERMIOS_SPEED) || defined(HAVE_TERMIOS2_SPEED)
+#define USE_TERMIOS_SPEED
+#endif
+
+#include "libserialport.h"
+
+struct sp_port {
+ char *name;
+#ifdef _WIN32
+ HANDLE hdl;
+ COMMTIMEOUTS timeouts;
+ OVERLAPPED write_ovl;
+ OVERLAPPED read_ovl;
+ OVERLAPPED wait_ovl;
+ DWORD events;
+ BYTE pending_byte;
+ BOOL writing;
+#else
+ int fd;
+#endif
+};
+
+struct sp_port_config {
+ int baudrate;
+ int bits;
+ enum sp_parity parity;
+ int stopbits;
+ enum sp_rts rts;
+ enum sp_cts cts;
+ enum sp_dtr dtr;
+ enum sp_dsr dsr;
+ enum sp_xonxoff xon_xoff;
+};
+
+struct port_data {
+#ifdef _WIN32
+ DCB dcb;
+#else
+ struct termios term;
+ int controlbits;
+ int termiox_supported;
+ int rts_flow;
+ int cts_flow;
+ int dtr_flow;
+ int dsr_flow;
+#endif
+};
+
+#ifdef _WIN32
+typedef HANDLE event_handle;
+#else
+typedef int event_handle;
+#endif
+
+/* Standard baud rates. */
+#ifdef _WIN32
+#define BAUD_TYPE DWORD
+#define BAUD(n) {CBR_##n, n}
+#else
+#define BAUD_TYPE speed_t
+#define BAUD(n) {B##n, n}
+#endif
+
+struct std_baudrate {
+ BAUD_TYPE index;
+ int value;
+};
+
+const struct std_baudrate std_baudrates[] = {
+#ifdef _WIN32
+ /*
+ * The baudrates 50/75/134/150/200/1800/230400/460800 do not seem to
+ * have documented CBR_* macros.
+ */
+ BAUD(110), BAUD(300), BAUD(600), BAUD(1200), BAUD(2400), BAUD(4800),
+ BAUD(9600), BAUD(14400), BAUD(19200), BAUD(38400), BAUD(57600),
+ BAUD(115200), BAUD(128000), BAUD(256000),
+#else
+ BAUD(50), BAUD(75), BAUD(110), BAUD(134), BAUD(150), BAUD(200),
+ BAUD(300), BAUD(600), BAUD(1200), BAUD(1800), BAUD(2400), BAUD(4800),
+ BAUD(9600), BAUD(19200), BAUD(38400), BAUD(57600), BAUD(115200),
+ BAUD(230400),
+#if !defined(__APPLE__) && !defined(__OpenBSD__)
+ BAUD(460800),
+#endif
+#endif
+};
+
+void (*sp_debug_handler)(const char *format, ...) = sp_default_debug_handler;
+
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
+#define NUM_STD_BAUDRATES ARRAY_SIZE(std_baudrates)
+
+/* Debug output macros. */
+#define DEBUG(fmt, ...) do { if (sp_debug_handler) sp_debug_handler(fmt ".\n", ##__VA_ARGS__); } while (0)
+#define DEBUG_ERROR(err, msg) DEBUG("%s returning " #err ": " msg, __func__)
+#define DEBUG_FAIL(msg) do { \
+ char *errmsg = sp_last_error_message(); \
+ DEBUG("%s returning SP_ERR_FAIL: " msg ": %s", __func__, errmsg); \
+ sp_free_error_message(errmsg); \
+} while (0);
+#define RETURN() do { DEBUG("%s returning", __func__); return; } while(0)
+#define RETURN_CODE(x) do { DEBUG("%s returning " #x, __func__); return x; } while (0)
+#define RETURN_CODEVAL(x) do { \
+ switch (x) { \
+ case SP_OK: RETURN_CODE(SP_OK); \
+ case SP_ERR_ARG: RETURN_CODE(SP_ERR_ARG); \
+ case SP_ERR_FAIL: RETURN_CODE(SP_ERR_FAIL); \
+ case SP_ERR_MEM: RETURN_CODE(SP_ERR_MEM); \
+ case SP_ERR_SUPP: RETURN_CODE(SP_ERR_SUPP); \
+ } \
+} while (0)
+#define RETURN_OK() RETURN_CODE(SP_OK);
+#define RETURN_ERROR(err, msg) do { DEBUG_ERROR(err, msg); return err; } while (0)
+#define RETURN_FAIL(msg) do { DEBUG_FAIL(msg); return SP_ERR_FAIL; } while (0)
+#define RETURN_VALUE(fmt, x) do { \
+ typeof(x) _x = x; \
+ DEBUG("%s returning " fmt, __func__, _x); \
+ return _x; \
+} while (0)
+#define SET_ERROR(val, err, msg) do { DEBUG_ERROR(err, msg); val = err; } while (0)
+#define SET_FAIL(val, msg) do { DEBUG_FAIL(msg); val = SP_ERR_FAIL; } while (0)
+#define TRACE(fmt, ...) DEBUG("%s(" fmt ") called", __func__, ##__VA_ARGS__)
+
+#define TRY(x) do { int ret = x; if (ret != SP_OK) RETURN_CODEVAL(ret); } while (0)
+
+/* Helper functions. */
+static struct sp_port **list_append(struct sp_port **list, const char *portname);
+static enum sp_return get_config(struct sp_port *port, struct port_data *data,
+ struct sp_port_config *config);
+static enum sp_return set_config(struct sp_port *port, struct port_data *data,
+ const struct sp_port_config *config);
+
+enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr)
+{
+ struct sp_port *port;
+ int len;
+
+ TRACE("%s, %p", portname, port_ptr);
+
+ if (!port_ptr)
+ RETURN_ERROR(SP_ERR_ARG, "Null result pointer");
+
+ *port_ptr = NULL;
+
+ if (!portname)
+ RETURN_ERROR(SP_ERR_ARG, "Null port name");
+
+ DEBUG("Building structure for port %s", portname);
+
+ if (!(port = malloc(sizeof(struct sp_port))))
+ RETURN_ERROR(SP_ERR_MEM, "Port structure malloc failed");
+
+ len = strlen(portname) + 1;
+
+ if (!(port->name = malloc(len))) {
+ free(port);
+ RETURN_ERROR(SP_ERR_MEM, "Port name malloc failed");
+ }
+
+ memcpy(port->name, portname, len);
+
+#ifdef _WIN32
+ port->hdl = INVALID_HANDLE_VALUE;
+#else
+ port->fd = -1;
+#endif
+
+ *port_ptr = port;
+
+ RETURN_OK();
+}
+
+char *sp_get_port_name(const struct sp_port *port)
+{
+ TRACE("%p", port);
+
+ if (!port)
+ return NULL;
+
+ RETURN_VALUE("%s", port->name);
+}
+
+enum sp_return sp_get_port_handle(const struct sp_port *port, void *result_ptr)
+{
+ TRACE("%p, %p", port, result_ptr);
+
+ if (!port)
+ RETURN_ERROR(SP_ERR_ARG, "Null port");
+
+#ifdef _WIN32
+ HANDLE *handle_ptr = result_ptr;
+ *handle_ptr = port->hdl;
+#else
+ int *fd_ptr = result_ptr;
+ *fd_ptr = port->fd;
+#endif
+
+ RETURN_OK();
+}
+
+enum sp_return sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr)
+{
+ TRACE("%p, %p", port, copy_ptr);
+
+ if (!copy_ptr)
+ RETURN_ERROR(SP_ERR_ARG, "Null result pointer");
+
+ *copy_ptr = NULL;
+
+ if (!port)
+ RETURN_ERROR(SP_ERR_ARG, "Null port");
+
+ if (!port->name)
+ RETURN_ERROR(SP_ERR_ARG, "Null port name");
+
+ DEBUG("Copying port structure");
+
+ RETURN_VALUE("%p", sp_get_port_by_name(port->name, copy_ptr));
+}
+
+void sp_free_port(struct sp_port *port)
+{
+ TRACE("%p", port);
+
+ if (!port) {
+ DEBUG("Null port");
+ RETURN();
+ }
+
+ DEBUG("Freeing port structure");
+
+ if (port->name)
+ free(port->name);
+
+ free(port);