+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 { 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 = err; } 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)