]> sigrok.org Git - libserialport.git/blobdiff - serialport.c
Minor cosmetics.
[libserialport.git] / serialport.c
index 8408ec7efb7d892d06399e329c9d00041fd7926f..5e776072b2d580b206af740fe6df969d094bc413 100644 (file)
 #ifdef __linux__
 #include "libudev.h"
 #include "linux/serial.h"
+#include "linux_termios.h"
+#if defined(TCGETX) && defined(TCSETX) && defined(HAVE_TERMIOX)
+#define USE_TERMIOX
+#endif
 #endif
 
 #include "libserialport.h"
@@ -54,6 +58,9 @@ struct port_data {
 #else
        struct termios term;
        int controlbits;
+#ifdef USE_TERMIOX
+       int flow;
+#endif
 #endif
 };
 
@@ -94,6 +101,8 @@ const struct std_baudrate std_baudrates[] = {
 #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)
+
 /* Helper functions. */
 static enum sp_return validate_port(struct sp_port *port);
 static struct sp_port **list_append(struct sp_port **list, const char *portname);
@@ -413,9 +422,10 @@ enum sp_return sp_open(struct sp_port *port, enum sp_mode flags)
        sprintf(escaped_port_name, "\\\\.\\%s", port->name);
 
        /* Map 'flags' to the OS-specific settings. */
-       desired_access |= GENERIC_READ;
        flags_and_attributes = FILE_ATTRIBUTE_NORMAL;
-       if (flags & SP_MODE_RDWR)
+       if (flags & SP_MODE_READ)
+               desired_access |= GENERIC_READ;
+       if (flags & SP_MODE_WRITE)
                desired_access |= GENERIC_WRITE;
        if (flags & SP_MODE_NONBLOCK)
                flags_and_attributes |= FILE_FLAG_OVERLAPPED;
@@ -434,10 +444,12 @@ enum sp_return sp_open(struct sp_port *port, enum sp_mode flags)
        int ret;
 
        /* Map 'flags' to the OS-specific settings. */
-       if (flags & SP_MODE_RDWR)
+       if (flags & (SP_MODE_READ | SP_MODE_WRITE))
                flags_local |= O_RDWR;
-       if (flags & SP_MODE_RDONLY)
+       else if (flags & SP_MODE_READ)
                flags_local |= O_RDONLY;
+       else if (flags & SP_MODE_WRITE)
+               flags_local |= O_WRONLY;
        if (flags & SP_MODE_NONBLOCK)
                flags_local |= O_NONBLOCK;
 
@@ -493,22 +505,53 @@ enum sp_return sp_close(struct sp_port *port)
        return SP_OK;
 }
 
-enum sp_return sp_flush(struct sp_port *port)
+enum sp_return sp_flush(struct sp_port *port, enum sp_buffer buffers)
 {
        CHECK_PORT();
 
 #ifdef _WIN32
+       DWORD flags = 0;
+       if (buffers & SP_BUF_INPUT)
+               flags |= PURGE_RXCLEAR;
+       if (buffers & SP_BUF_OUTPUT)
+               flags |= PURGE_TXCLEAR;
+
        /* Returns non-zero upon success, 0 upon failure. */
-       if (PurgeComm(port->hdl, PURGE_RXCLEAR | PURGE_TXCLEAR) == 0)
+       if (PurgeComm(port->hdl, flags) == 0)
                return SP_ERR_FAIL;
 #else
+       int flags = 0;
+       if (buffers & SP_BUF_BOTH)
+               flags = TCIOFLUSH;
+       else if (buffers & SP_BUF_INPUT)
+               flags = TCIFLUSH;
+       else if (buffers & SP_BUF_OUTPUT)
+               flags = TCOFLUSH;
+
        /* Returns 0 upon success, -1 upon failure. */
-       if (tcflush(port->fd, TCIOFLUSH) < 0)
+       if (tcflush(port->fd, flags) < 0)
                return SP_ERR_FAIL;
 #endif
        return SP_OK;
 }
 
+enum sp_return sp_drain(struct sp_port *port)
+{
+       CHECK_PORT();
+
+#ifdef _WIN32
+       /* Returns non-zero upon success, 0 upon failure. */
+       if (FlushFileBuffers(port->hdl) == 0)
+               return SP_ERR_FAIL;
+#else
+       /* Returns 0 upon success, -1 upon failure. */
+       if (tcdrain(port->fd) < 0)
+               return SP_ERR_FAIL;
+#endif
+
+       return SP_OK;
+}
+
 enum sp_return sp_write(struct sp_port *port, const void *buf, size_t count)
 {
        CHECK_PORT();
@@ -558,6 +601,76 @@ enum sp_return sp_read(struct sp_port *port, void *buf, size_t count)
 #endif
 }
 
+#ifdef __linux__
+static enum sp_return get_baudrate(int fd, int *baudrate)
+{
+       void *data;
+
+       if (!(data = malloc(get_termios_size())))
+               return SP_ERR_MEM;
+
+       if (ioctl(fd, get_termios_get_ioctl(), data) < 0)
+               return SP_ERR_FAIL;
+
+       *baudrate = get_termios_speed(data);
+
+       return SP_OK;
+}
+
+static enum sp_return set_baudrate(int fd, int baudrate)
+{
+       void *data;
+
+       if (!(data = malloc(get_termios_size())))
+               return SP_ERR_MEM;
+
+       if (ioctl(fd, get_termios_get_ioctl(), data) < 0)
+               return SP_ERR_FAIL;
+
+       set_termios_speed(data, baudrate);
+
+       if (ioctl(fd, get_termios_set_ioctl(), data) < 0)
+               return SP_ERR_FAIL;
+
+       return SP_OK;
+}
+
+#ifdef USE_TERMIOX
+static enum sp_return get_flow(int fd, int *flow)
+{
+       void *data;
+
+       if (!(data = malloc(get_termiox_size())))
+               return SP_ERR_MEM;
+
+       if (ioctl(fd, TCGETX, data) < 0)
+               return SP_ERR_FAIL;
+
+       *flow = get_termiox_flow(data);
+
+       return SP_OK;
+}
+
+static enum sp_return set_flow(int fd, int flow)
+{
+       void *data;
+
+       if (!(data = malloc(get_termiox_size())))
+               return SP_ERR_MEM;
+
+       if (ioctl(fd, TCGETX, data) < 0)
+               return SP_ERR_FAIL;
+
+       set_termiox_flow(data, flow);
+
+       if (ioctl(fd, TCSETX, data) < 0)
+               return SP_ERR_FAIL;
+
+       return SP_OK;
+}
+#endif /* USE_TERMIOX */
+#endif /* __linux__ */
+
 static enum sp_return get_config(struct sp_port *port, struct port_data *data,
        struct sp_port_config *config)
 {
@@ -659,6 +772,11 @@ static enum sp_return get_config(struct sp_port *port, struct port_data *data,
 
        if (ioctl(port->fd, TIOCMGET, &data->controlbits) < 0)
                return SP_ERR_FAIL;
+
+#ifdef USE_TERMIOX
+       TRY(get_flow(port->fd, &data->flow));
+#endif
+
        for (i = 0; i < NUM_STD_BAUDRATES; i++) {
                if (cfgetispeed(&data->term) == std_baudrates[i].index) {
                        config->baudrate = std_baudrates[i].value;
@@ -669,6 +787,8 @@ static enum sp_return get_config(struct sp_port *port, struct port_data *data,
        if (i == NUM_STD_BAUDRATES) {
 #ifdef __APPLE__
                config->baudrate = (int)data->term.c_ispeed;
+#elif defined(__linux__)
+               TRY(get_baudrate(port->fd, &config->baudrate));
 #else
                config->baudrate = -1;
 #endif
@@ -704,12 +824,30 @@ static enum sp_return get_config(struct sp_port *port, struct port_data *data,
                config->rts = SP_RTS_FLOW_CONTROL;
                config->cts = SP_CTS_FLOW_CONTROL;
        } else {
+#ifdef USE_TERMIOX
+               if (data->flow & RTS_FLOW)
+                       config->rts = SP_RTS_FLOW_CONTROL;
+               else
+                       config->rts = (data->controlbits & TIOCM_RTS) ? SP_RTS_ON : SP_RTS_OFF;
+
+               config->cts = (data->flow & CTS_FLOW) ? SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE;
+#else
                config->rts = (data->controlbits & TIOCM_RTS) ? SP_RTS_ON : SP_RTS_OFF;
                config->cts = SP_CTS_IGNORE;
+#endif
        }
 
+#ifdef USE_TERMIOX
+       if (data->flow & DTR_FLOW)
+               config->dtr = SP_DTR_FLOW_CONTROL;
+       else
+               config->dtr = (data->controlbits & TIOCM_DTR) ? SP_DTR_ON : SP_DTR_OFF;
+
+       config->dsr = (data->flow & DSR_FLOW) ? SP_DSR_FLOW_CONTROL : SP_DSR_IGNORE;
+#else
        config->dtr = (data->controlbits & TIOCM_DTR) ? SP_DTR_ON : SP_DTR_OFF;
        config->dsr = SP_DSR_IGNORE;
+#endif
 
        if (data->term.c_iflag & IXOFF) {
                if (data->term.c_iflag & IXON)
@@ -727,7 +865,7 @@ static enum sp_return get_config(struct sp_port *port, struct port_data *data,
        return SP_OK;
 }
 
-static enum sp_return set_config(struct sp_port *port, struct port_data *data, 
+static enum sp_return set_config(struct sp_port *port, struct port_data *data,
        const struct sp_port_config *config)
 {
        unsigned int i;
@@ -736,6 +874,9 @@ static enum sp_return set_config(struct sp_port *port, struct port_data *data,
 
        baud_nonstd = B0;
 #endif
+#ifdef __linux__
+       int baud_nonstd = 0;
+#endif
 
 #ifdef _WIN32
        if (config->baudrate >= 0) {
@@ -870,6 +1011,8 @@ static enum sp_return set_config(struct sp_port *port, struct port_data *data,
 
 #else /* !_WIN32 */
 
+       int controlbits;
+
        if (config->baudrate >= 0) {
                for (i = 0; i < NUM_STD_BAUDRATES; i++) {
                        if (config->baudrate == std_baudrates[i].value) {
@@ -885,10 +1028,12 @@ static enum sp_return set_config(struct sp_port *port, struct port_data *data,
                /* Non-standard baud rate */
                if (i == NUM_STD_BAUDRATES) {
 #ifdef __APPLE__
-                       /* Set "dummy" baud rate */
+                       /* Set "dummy" baud rate. */
                        if (cfsetspeed(&data->term, B9600) < 0)
                                return SP_ERR_FAIL;
                        baud_nonstd = config->baudrate;
+#elif defined(__linux__)
+                       baud_nonstd = 1;
 #else
                        return SP_ERR_ARG;
 #endif
@@ -948,8 +1093,30 @@ static enum sp_return set_config(struct sp_port *port, struct port_data *data,
        }
 
        if (config->rts >= 0 || config->cts >= 0) {
-               /* Asymmetric use of RTS/CTS not supported yet. */
+#ifdef USE_TERMIOX
+               data->flow &= ~(RTS_FLOW | CTS_FLOW);
+               switch (config->rts) {
+               case SP_RTS_OFF:
+               case SP_RTS_ON:
+                       controlbits = TIOCM_RTS;
+                       if (ioctl(port->fd, config->rts == SP_RTS_ON ? TIOCMBIS : TIOCMBIC, &controlbits) < 0)
+                               return SP_ERR_FAIL;
+                       break;
+               case SP_RTS_FLOW_CONTROL:
+                       data->flow |= RTS_FLOW;
+                       break;
+               default:
+                       break;
+               }
+               if (config->cts == SP_CTS_FLOW_CONTROL)
+                       data->flow |= CTS_FLOW;
 
+               if (data->flow & (RTS_FLOW | CTS_FLOW))
+                       data->term.c_iflag |= CRTSCTS;
+               else
+                       data->term.c_iflag &= ~CRTSCTS;
+#else
+               /* Asymmetric use of RTS/CTS not supported. */
                if (data->term.c_iflag & CRTSCTS) {
                        /* Flow control can only be disabled for both RTS & CTS together. */
                        if (config->rts >= 0 && config->rts != SP_RTS_FLOW_CONTROL) {
@@ -971,25 +1138,45 @@ static enum sp_return set_config(struct sp_port *port, struct port_data *data,
                        if (config->rts == SP_RTS_FLOW_CONTROL) {
                                data->term.c_iflag |= CRTSCTS;
                        } else {
-                               int controlbits = TIOCM_RTS;
+                               controlbits = TIOCM_RTS;
                                if (ioctl(port->fd, config->rts == SP_RTS_ON ? TIOCMBIS : TIOCMBIC,
                                                &controlbits) < 0)
                                        return SP_ERR_FAIL;
                        }
                }
+#endif
        }
 
        if (config->dtr >= 0 || config->dsr >= 0) {
-               /* DTR/DSR flow control not supported yet. */
+#ifdef USE_TERMIOX
+               data->flow &= ~(DTR_FLOW | DSR_FLOW);
+               switch (config->dtr) {
+               case SP_DTR_OFF:
+               case SP_DTR_ON:
+                       controlbits = TIOCM_DTR;
+                       if (ioctl(port->fd, config->dtr == SP_DTR_ON ? TIOCMBIS : TIOCMBIC, &controlbits) < 0)
+                               return SP_ERR_FAIL;
+                       break;
+               case SP_DTR_FLOW_CONTROL:
+                       data->flow |= DTR_FLOW;
+                       break;
+               default:
+                       break;
+               }
+               if (config->dsr == SP_DSR_FLOW_CONTROL)
+                       data->flow |= DSR_FLOW;
+#else
+               /* DTR/DSR flow control not supported. */
                if (config->dtr == SP_DTR_FLOW_CONTROL || config->dsr == SP_DSR_FLOW_CONTROL)
                        return SP_ERR_ARG;
 
                if (config->dtr >= 0) {
-                       int controlbits = TIOCM_DTR;
+                       controlbits = TIOCM_DTR;
                        if (ioctl(port->fd, config->dtr == SP_DTR_ON ? TIOCMBIS : TIOCMBIC,
                                        &controlbits) < 0)
                                return SP_ERR_FAIL;
                }
+#endif
        }
 
        if (config->xon_xoff >= 0) {
@@ -1023,15 +1210,19 @@ static enum sp_return set_config(struct sp_port *port, struct port_data *data,
                if (cfsetspeed(&data->term, baud_nonstd) < 0)
                        return SP_ERR_FAIL;
        }
-#endif /* __APPLE__ */
+#elif defined(__linux__)
+       if (baud_nonstd)
+               TRY(set_baudrate(port->fd, config->baudrate));
+#ifdef USE_TERMIOX
+       TRY(set_flow(port->fd, data->flow));
+#endif
+#endif
 
 #endif /* !_WIN32 */
 
        return SP_OK;
 }
 
-#define TRY(x) do { int ret = x; if (ret != SP_OK) return ret; } while (0)
-
 enum sp_return sp_set_config(struct sp_port *port, const struct sp_port_config *config)
 {
        struct port_data data;
@@ -1105,6 +1296,70 @@ enum sp_return sp_set_flowcontrol(struct sp_port *port, enum sp_flowcontrol flow
        return SP_OK;
 }
 
+enum sp_return sp_get_signals(struct sp_port *port, enum sp_signal *signals)
+{
+       CHECK_PORT();
+
+       if (!signals)
+               return SP_ERR_ARG;
+
+       *signals = 0;
+#ifdef _WIN32
+       DWORD bits;
+       if (GetCommModemStatus(port->hdl, &bits) == 0)
+               return SP_ERR_FAIL;
+       if (bits & MS_CTS_ON)
+               *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_RI;
+#else
+       int bits;
+       if (ioctl(port->fd, TIOCMGET, &bits) < 0)
+               return SP_ERR_FAIL;
+       if (bits & TIOCM_CTS)
+               *signals |= SP_SIG_CTS;
+       if (bits & TIOCM_DSR)
+               *signals |= SP_SIG_DSR;
+       if (bits & TIOCM_CAR)
+               *signals |= SP_SIG_DCD;
+       if (bits & TIOCM_RNG)
+               *signals |= SP_SIG_RI;
+#endif
+       return SP_OK;
+}
+
+enum sp_return sp_start_break(struct sp_port *port)
+{
+       CHECK_PORT();
+#ifdef _WIN32
+       if (SetCommBreak(port->hdl) == 0)
+               return SP_ERR_FAIL;
+#else
+       if (ioctl(port->fd, TIOCSBRK, 1) < 0)
+               return SP_ERR_FAIL;
+#endif
+
+       return SP_OK;
+}
+
+enum sp_return sp_end_break(struct sp_port *port)
+{
+       CHECK_PORT();
+#ifdef _WIN32
+       if (ClearCommBreak(port->hdl) == 0)
+               return SP_ERR_FAIL;
+#else
+       if (ioctl(port->fd, TIOCCBRK, 1) < 0)
+               return SP_ERR_FAIL;
+#endif
+
+       return SP_OK;
+}
+
 int sp_last_error_code(void)
 {
 #ifdef _WIN32