]> sigrok.org Git - libserialport.git/blobdiff - serialport.c
Make configuration structure opaque.
[libserialport.git] / serialport.c
index 5ad216cc3b43e3ea415b6e5f89c2fb98e0218670..f426fca79efef55d4c2d8b304e22aca869048d37 100644 (file)
 
 #include "libserialport.h"
 
+struct sp_port {
+       char *name;
+#ifdef _WIN32
+       HANDLE hdl;
+#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;
@@ -108,8 +129,6 @@ 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)
 
-#define TRY(x) do { int ret = x; if (ret != SP_OK) return ret; } while (0)
-
 /* 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__)
@@ -137,6 +156,8 @@ void (*sp_debug_handler)(const char *format, ...) = sp_default_debug_handler;
 #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,
@@ -184,6 +205,34 @@ enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_p
        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", port);
+
+       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);
@@ -246,10 +295,13 @@ fail:
 enum sp_return sp_list_ports(struct sp_port ***list_ptr)
 {
        struct sp_port **list;
-       int ret = SP_OK;
+       int ret = SP_ERR_SUPP;
 
        TRACE("%p", list_ptr);
 
+       if (!list_ptr)
+               RETURN_ERROR(SP_ERR_ARG, "Null result pointer");
+
        DEBUG("Enumerating ports");
 
        if (!(list = malloc(sizeof(struct sp_port **))))
@@ -266,6 +318,8 @@ enum sp_return sp_list_ports(struct sp_port ***list_ptr)
        char *name;
        int name_len;
 
+       ret = SP_OK;
+
        DEBUG("Opening registry key");
        if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"),
                        0, KEY_QUERY_VALUE, &key) != ERROR_SUCCESS) {
@@ -336,6 +390,8 @@ out_done:
        CFTypeRef cf_path;
        Boolean result;
 
+       ret = SP_OK;
+
        DEBUG("Getting IOKit master port");
        if (IOMasterPort(MACH_PORT_NULL, &master) != KERN_SUCCESS) {
                SET_FAIL(ret, "IOMasterPort() failed");
@@ -399,6 +455,8 @@ out_done:
        int fd, ioctl_result;
        struct serial_struct serial_info;
 
+       ret = SP_OK;
+
        DEBUG("Enumerating tty devices");
        ud = udev_new();
        ud_enumerate = udev_enumerate_new(ud);
@@ -453,10 +511,13 @@ out:
        udev_unref(ud);
 #endif
 
-       if (ret == SP_OK) {
+       switch (ret) {
+       case SP_OK:
                *list_ptr = list;
                RETURN_OK();
-       } else {
+       case SP_ERR_SUPP:
+               DEBUG_ERROR(SP_ERR_SUPP, "Enumeration not supported on this platform.");
+       default:
                if (list)
                        sp_free_port_list(list);
                *list_ptr = NULL;
@@ -470,6 +531,11 @@ void sp_free_port_list(struct sp_port **list)
 
        TRACE("%p", list);
 
+       if (!list) {
+               DEBUG("Null list");
+               RETURN();
+       }
+
        DEBUG("Freeing port list");
 
        for (i = 0; list[i]; i++)
@@ -479,28 +545,36 @@ void sp_free_port_list(struct sp_port **list)
        RETURN();
 }
 
-#ifdef _WIN32
 #define CHECK_PORT() do { \
        if (port == NULL) \
                RETURN_ERROR(SP_ERR_ARG, "Null port"); \
+       if (port->name == NULL) \
+               RETURN_ERROR(SP_ERR_ARG, "Null port name"); \
+} while (0)
+#ifdef _WIN32
+#define CHECK_PORT_HANDLE() do { \
        if (port->hdl == INVALID_HANDLE_VALUE) \
                RETURN_ERROR(SP_ERR_ARG, "Invalid port handle"); \
-} while(0);
+} while (0)
 #else
-#define CHECK_PORT() do { \
-       if (port == NULL) \
-               RETURN_ERROR(SP_ERR_ARG, "Null port"); \
+#define CHECK_PORT_HANDLE() do { \
        if (port->fd < 0) \
                RETURN_ERROR(SP_ERR_ARG, "Invalid port fd"); \
-} while(0);
+} while (0)
 #endif
+#define CHECK_OPEN_PORT() do { \
+       CHECK_PORT(); \
+       CHECK_PORT_HANDLE(); \
+} while (0)
 
 enum sp_return sp_open(struct sp_port *port, enum sp_mode flags)
 {
        TRACE("%p, %x", port, flags);
 
-       if (!port)
-               RETURN_ERROR(SP_ERR_ARG, "Null port");
+       CHECK_PORT();
+
+       if (flags > (SP_MODE_READ | SP_MODE_WRITE | SP_MODE_NONBLOCK))
+               RETURN_ERROR(SP_ERR_ARG, "Invalid flags");
 
        DEBUG("Opening port %s", port->name);
 
@@ -582,7 +656,7 @@ enum sp_return sp_close(struct sp_port *port)
 {
        TRACE("%p", port);
 
-       CHECK_PORT();
+       CHECK_OPEN_PORT();
 
        DEBUG("Closing port %s", port->name);
 
@@ -605,7 +679,10 @@ enum sp_return sp_flush(struct sp_port *port, enum sp_buffer buffers)
 {
        TRACE("%p, %x", port, buffers);
 
-       CHECK_PORT();
+       CHECK_OPEN_PORT();
+
+       if (buffers > SP_BUF_BOTH)
+               RETURN_ERROR(SP_ERR_ARG, "Invalid buffer selection");
 
        const char *buffer_names[] = {"input", "output", "both"};
 
@@ -641,7 +718,7 @@ enum sp_return sp_drain(struct sp_port *port)
 {
        TRACE("%p", port);
 
-       CHECK_PORT();
+       CHECK_OPEN_PORT();
 
        DEBUG("Draining port %s", port->name);
 
@@ -662,7 +739,7 @@ enum sp_return sp_write(struct sp_port *port, const void *buf, size_t count)
 {
        TRACE("%p, %p, %d", port, buf, count);
 
-       CHECK_PORT();
+       CHECK_OPEN_PORT();
 
        if (!buf)
                RETURN_ERROR(SP_ERR_ARG, "Null buffer");
@@ -691,7 +768,7 @@ enum sp_return sp_read(struct sp_port *port, void *buf, size_t count)
 {
        TRACE("%p, %p, %d", port, buf, count);
 
-       CHECK_PORT();
+       CHECK_OPEN_PORT();
 
        if (!buf)
                RETURN_ERROR(SP_ERR_ARG, "Null buffer");
@@ -1387,6 +1464,47 @@ static enum sp_return set_config(struct sp_port *port, struct port_data *data,
        RETURN_OK();
 }
 
+enum sp_return sp_new_config(struct sp_port_config **config_ptr)
+{
+       TRACE("%p", config_ptr);
+
+       if (!config_ptr)
+               RETURN_ERROR(SP_ERR_ARG, "Null result pointer");
+
+       if (!(*config_ptr = malloc(sizeof(struct sp_port_config))))
+               RETURN_ERROR(SP_ERR_MEM, "config malloc failed");
+
+       RETURN_OK();
+}
+
+void sp_free_config(struct sp_port_config *config)
+{
+       TRACE("%p", config);
+
+       if (!config)
+               DEBUG("Null config");
+       else
+               free(config);
+
+       RETURN();
+}
+
+enum sp_return sp_get_config(struct sp_port *port, struct sp_port_config *config)
+{
+       struct port_data data;
+
+       TRACE("%p, %p", port, config);
+
+       CHECK_OPEN_PORT();
+
+       if (!config)
+               RETURN_ERROR(SP_ERR_ARG, "Null config");
+
+       TRY(get_config(port, &data, config));
+
+       RETURN_OK();
+}
+
 enum sp_return sp_set_config(struct sp_port *port, const struct sp_port_config *config)
 {
        struct port_data data;
@@ -1394,7 +1512,7 @@ enum sp_return sp_set_config(struct sp_port *port, const struct sp_port_config *
 
        TRACE("%p, %p", port, config);
 
-       CHECK_PORT();
+       CHECK_OPEN_PORT();
 
        if (!config)
                RETURN_ERROR(SP_ERR_ARG, "Null config");
@@ -1405,61 +1523,89 @@ enum sp_return sp_set_config(struct sp_port *port, const struct sp_port_config *
        RETURN_OK();
 }
 
-#define CREATE_SETTER(x, type) int sp_set_##x(struct sp_port *port, type x) { \
+#define CREATE_ACCESSORS(x, type) \
+enum sp_return sp_set_##x(struct sp_port *port, type x) { \
        struct port_data data; \
        struct sp_port_config config; \
        TRACE("%p, %d", port, x); \
-       CHECK_PORT(); \
+       CHECK_OPEN_PORT(); \
        TRY(get_config(port, &data, &config)); \
        config.x = x; \
        TRY(set_config(port, &data, &config)); \
        RETURN_OK(); \
+} \
+enum sp_return sp_get_config_##x(const struct sp_port_config *config, type *x) { \
+       TRACE("%p", config); \
+       if (!config) \
+               RETURN_ERROR(SP_ERR_ARG, "Null config"); \
+       *x = config->x; \
+       RETURN_OK(); \
+} \
+enum sp_return sp_set_config_##x(struct sp_port_config *config, type x) { \
+       TRACE("%p, %d", config, x); \
+       if (!config) \
+               RETURN_ERROR(SP_ERR_ARG, "Null config"); \
+       config->x = x; \
+       RETURN_OK(); \
 }
 
-CREATE_SETTER(baudrate, int)
-CREATE_SETTER(bits, int)
-CREATE_SETTER(parity, enum sp_parity)
-CREATE_SETTER(stopbits, int)
-CREATE_SETTER(rts, enum sp_rts)
-CREATE_SETTER(cts, enum sp_cts)
-CREATE_SETTER(dtr, enum sp_dtr)
-CREATE_SETTER(dsr, enum sp_dsr)
-CREATE_SETTER(xon_xoff, enum sp_xonxoff)
-
-enum sp_return sp_set_flowcontrol(struct sp_port *port, enum sp_flowcontrol flowcontrol)
+CREATE_ACCESSORS(baudrate, int)
+CREATE_ACCESSORS(bits, int)
+CREATE_ACCESSORS(parity, enum sp_parity)
+CREATE_ACCESSORS(stopbits, int)
+CREATE_ACCESSORS(rts, enum sp_rts)
+CREATE_ACCESSORS(cts, enum sp_cts)
+CREATE_ACCESSORS(dtr, enum sp_dtr)
+CREATE_ACCESSORS(dsr, enum sp_dsr)
+CREATE_ACCESSORS(xon_xoff, enum sp_xonxoff)
+
+enum sp_return sp_set_config_flowcontrol(struct sp_port_config *config, enum sp_flowcontrol flowcontrol)
 {
-       struct port_data data;
-       struct sp_port_config config;
-
-       TRACE("%p, %d", port, flowcontrol);
-
-       CHECK_PORT();
+       if (!config)
+               RETURN_ERROR(SP_ERR_ARG, "Null configuration");
 
-       TRY(get_config(port, &data, &config));
+       if (flowcontrol > SP_FLOWCONTROL_DTRDSR)
+               RETURN_ERROR(SP_ERR_ARG, "Invalid flow control setting");
 
        if (flowcontrol == SP_FLOWCONTROL_XONXOFF)
-               config.xon_xoff = SP_XONXOFF_INOUT;
+               config->xon_xoff = SP_XONXOFF_INOUT;
        else
-               config.xon_xoff = SP_XONXOFF_DISABLED;
+               config->xon_xoff = SP_XONXOFF_DISABLED;
 
        if (flowcontrol == SP_FLOWCONTROL_RTSCTS) {
-               config.rts = SP_RTS_FLOW_CONTROL;
-               config.cts = SP_CTS_FLOW_CONTROL;
+               config->rts = SP_RTS_FLOW_CONTROL;
+               config->cts = SP_CTS_FLOW_CONTROL;
        } else {
-               if (config.rts == SP_RTS_FLOW_CONTROL)
-                       config.rts = SP_RTS_ON;
-               config.cts = SP_CTS_IGNORE;
+               if (config->rts == SP_RTS_FLOW_CONTROL)
+                       config->rts = SP_RTS_ON;
+               config->cts = SP_CTS_IGNORE;
        }
 
        if (flowcontrol == SP_FLOWCONTROL_DTRDSR) {
-               config.dtr = SP_DTR_FLOW_CONTROL;
-               config.dsr = SP_DSR_FLOW_CONTROL;
+               config->dtr = SP_DTR_FLOW_CONTROL;
+               config->dsr = SP_DSR_FLOW_CONTROL;
        } else {
-               if (config.dtr == SP_DTR_FLOW_CONTROL)
-                       config.dtr = SP_DTR_ON;
-               config.dsr = SP_DSR_IGNORE;
+               if (config->dtr == SP_DTR_FLOW_CONTROL)
+                       config->dtr = SP_DTR_ON;
+               config->dsr = SP_DSR_IGNORE;
        }
 
+       RETURN_OK();
+}
+
+enum sp_return sp_set_flowcontrol(struct sp_port *port, enum sp_flowcontrol flowcontrol)
+{
+       struct port_data data;
+       struct sp_port_config config;
+
+       TRACE("%p, %d", port, flowcontrol);
+
+       CHECK_OPEN_PORT();
+
+       TRY(get_config(port, &data, &config));
+
+       TRY(sp_set_config_flowcontrol(&config, flowcontrol));
+
        TRY(set_config(port, &data, &config));
 
        RETURN_OK();
@@ -1469,7 +1615,7 @@ enum sp_return sp_get_signals(struct sp_port *port, enum sp_signal *signals)
 {
        TRACE("%p, %p", port, signals);
 
-       CHECK_PORT();
+       CHECK_OPEN_PORT();
 
        if (!signals)
                RETURN_ERROR(SP_ERR_ARG, "Null result pointer");
@@ -1485,9 +1631,9 @@ enum sp_return sp_get_signals(struct sp_port *port, enum sp_signal *signals)
                *signals |= SP_SIG_CTS;
        if (bits & MS_DSR_ON)
                *signals |= SP_SIG_DSR;
-       if (bits & MS_RING_ON)
-               *signals |= SP_SIG_DCD;
        if (bits & MS_RLSD_ON)
+               *signals |= SP_SIG_DCD;
+       if (bits & MS_RING_ON)
                *signals |= SP_SIG_RI;
 #else
        int bits;
@@ -1509,7 +1655,7 @@ enum sp_return sp_start_break(struct sp_port *port)
 {
        TRACE("%p", port);
 
-       CHECK_PORT();
+       CHECK_OPEN_PORT();
 #ifdef _WIN32
        if (SetCommBreak(port->hdl) == 0)
                RETURN_FAIL("SetCommBreak() failed");
@@ -1525,7 +1671,7 @@ enum sp_return sp_end_break(struct sp_port *port)
 {
        TRACE("%p", port);
 
-       CHECK_PORT();
+       CHECK_OPEN_PORT();
 #ifdef _WIN32
        if (ClearCommBreak(port->hdl) == 0)
                RETURN_FAIL("ClearCommBreak() failed");