]> sigrok.org Git - libserialport.git/blobdiff - serialport.c
Add API for and default handler for debug messages.
[libserialport.git] / serialport.c
index 5e776072b2d580b206af740fe6df969d094bc413..2bb53b4f56edb9c14301bec2304892500447689b 100644 (file)
@@ -27,6 +27,8 @@
 #include <unistd.h>
 #include <stdlib.h>
 #include <errno.h>
+#include <stdio.h>
+#include <stdarg.h>
 #ifdef _WIN32
 #include <windows.h>
 #include <tchar.h>
 #endif
 #endif
 
+#ifndef _WIN32
+#include "linux_termios.h"
+#endif
+
 #include "libserialport.h"
 
 struct port_data {
@@ -58,10 +64,9 @@ struct port_data {
 #else
        struct termios term;
        int controlbits;
-#ifdef USE_TERMIOX
+       int termiox_supported;
        int flow;
 #endif
-#endif
 };
 
 /* Standard baud rates. */
@@ -98,6 +103,8 @@ const struct std_baudrate std_baudrates[] = {
 #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)
 
@@ -609,11 +616,15 @@ static enum sp_return get_baudrate(int fd, int *baudrate)
        if (!(data = malloc(get_termios_size())))
                return SP_ERR_MEM;
 
-       if (ioctl(fd, get_termios_get_ioctl(), data) < 0)
+       if (ioctl(fd, get_termios_get_ioctl(), data) < 0) {
+               free(data);
                return SP_ERR_FAIL;
+       }
 
        *baudrate = get_termios_speed(data);
 
+       free(data);
+
        return SP_OK;
 }
 
@@ -624,13 +635,19 @@ static enum sp_return set_baudrate(int fd, int baudrate)
        if (!(data = malloc(get_termios_size())))
                return SP_ERR_MEM;
 
-       if (ioctl(fd, get_termios_get_ioctl(), data) < 0)
+       if (ioctl(fd, get_termios_get_ioctl(), data) < 0) {
+               free(data);
                return SP_ERR_FAIL;
+       }
 
        set_termios_speed(data, baudrate);
 
-       if (ioctl(fd, get_termios_set_ioctl(), data) < 0)
+       if (ioctl(fd, get_termios_set_ioctl(), data) < 0) {
+               free(data);
                return SP_ERR_FAIL;
+       }
+
+       free(data);
 
        return SP_OK;
 }
@@ -643,11 +660,15 @@ static enum sp_return get_flow(int fd, int *flow)
        if (!(data = malloc(get_termiox_size())))
                return SP_ERR_MEM;
 
-       if (ioctl(fd, TCGETX, data) < 0)
+       if (ioctl(fd, TCGETX, data) < 0) {
+               free(data);
                return SP_ERR_FAIL;
+       }
 
        *flow = get_termiox_flow(data);
 
+       free(data);
+
        return SP_OK;
 }
 
@@ -658,13 +679,19 @@ static enum sp_return set_flow(int fd, int flow)
        if (!(data = malloc(get_termiox_size())))
                return SP_ERR_MEM;
 
-       if (ioctl(fd, TCGETX, data) < 0)
+       if (ioctl(fd, TCGETX, data) < 0) {
+               free(data);
                return SP_ERR_FAIL;
+       }
 
        set_termiox_flow(data, flow);
 
-       if (ioctl(fd, TCSETX, data) < 0)
+       if (ioctl(fd, TCSETX, data) < 0) {
+               free(data);
                return SP_ERR_FAIL;
+       }
+
+       free(data);
 
        return SP_OK;
 }
@@ -774,7 +801,16 @@ static enum sp_return get_config(struct sp_port *port, struct port_data *data,
                return SP_ERR_FAIL;
 
 #ifdef USE_TERMIOX
-       TRY(get_flow(port->fd, &data->flow));
+       int ret = get_flow(port->fd, &data->flow);
+
+       if (ret == SP_ERR_FAIL && errno == EINVAL)
+               data->termiox_supported = 0;
+       else if (ret < 0)
+               return ret;
+       else
+               data->termiox_supported = 1;
+#else
+       data->termiox_supported = 0;
 #endif
 
        for (i = 0; i < NUM_STD_BAUDRATES; i++) {
@@ -824,30 +860,22 @@ 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)
+               if (data->termiox_supported && 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
+               config->cts = (data->termiox_supported && data->flow & CTS_FLOW) ?
+                       SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE;
        }
 
-#ifdef USE_TERMIOX
-       if (data->flow & DTR_FLOW)
+       if (data->termiox_supported && 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
+       config->dsr = (data->termiox_supported && data->flow & DSR_FLOW) ?
+               SP_DSR_FLOW_CONTROL : SP_DSR_IGNORE;
 
        if (data->term.c_iflag & IXOFF) {
                if (data->term.c_iflag & IXON)
@@ -1035,7 +1063,7 @@ static enum sp_return set_config(struct sp_port *port, struct port_data *data,
 #elif defined(__linux__)
                        baud_nonstd = 1;
 #else
-                       return SP_ERR_ARG;
+                       return SP_ERR_SUPP;
 #endif
                }
        }
@@ -1093,90 +1121,90 @@ static enum sp_return set_config(struct sp_port *port, struct port_data *data,
        }
 
        if (config->rts >= 0 || config->cts >= 0) {
-#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) {
-                               if (config->cts != SP_CTS_IGNORE)
-                                       return SP_ERR_ARG;
-                       }
-                       if (config->cts >= 0 && config->cts != SP_CTS_FLOW_CONTROL) {
-                               if (config->rts <= 0 || config->rts == SP_RTS_FLOW_CONTROL)
-                                       return SP_ERR_ARG;
+               if (data->termiox_supported) {
+                       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;
                        }
-               } else {
-                       /* Flow control can only be enabled for both RTS & CTS together. */
-                       if (((config->rts == SP_RTS_FLOW_CONTROL) && (config->cts != SP_CTS_FLOW_CONTROL)) ||
-                               ((config->cts == SP_CTS_FLOW_CONTROL) && (config->rts != SP_RTS_FLOW_CONTROL)))
-                               return SP_ERR_ARG;
-               }
+                       if (config->cts == SP_CTS_FLOW_CONTROL)
+                               data->flow |= CTS_FLOW;
 
-               if (config->rts >= 0) {
-                       if (config->rts == SP_RTS_FLOW_CONTROL) {
+                       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) {
+                                       if (config->cts != SP_CTS_IGNORE)
+                                               return SP_ERR_SUPP;
+                               }
+                               if (config->cts >= 0 && config->cts != SP_CTS_FLOW_CONTROL) {
+                                       if (config->rts <= 0 || config->rts == SP_RTS_FLOW_CONTROL)
+                                               return SP_ERR_SUPP;
+                               }
                        } else {
-                               controlbits = TIOCM_RTS;
-                               if (ioctl(port->fd, config->rts == SP_RTS_ON ? TIOCMBIS : TIOCMBIC,
-                                               &controlbits) < 0)
-                                       return SP_ERR_FAIL;
+                               /* Flow control can only be enabled for both RTS & CTS together. */
+                               if (((config->rts == SP_RTS_FLOW_CONTROL) && (config->cts != SP_CTS_FLOW_CONTROL)) ||
+                                       ((config->cts == SP_CTS_FLOW_CONTROL) && (config->rts != SP_RTS_FLOW_CONTROL)))
+                                       return SP_ERR_SUPP;
+                       }
+
+                       if (config->rts >= 0) {
+                               if (config->rts == SP_RTS_FLOW_CONTROL) {
+                                       data->term.c_iflag |= CRTSCTS;
+                               } else {
+                                       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) {
-#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 (data->termiox_supported) {
+                       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_SUPP;
 
-               if (config->dtr >= 0) {
-                       controlbits = TIOCM_DTR;
-                       if (ioctl(port->fd, config->dtr == SP_DTR_ON ? TIOCMBIS : TIOCMBIC,
-                                       &controlbits) < 0)
-                               return SP_ERR_FAIL;
+                       if (config->dtr >= 0) {
+                               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) {
@@ -1214,7 +1242,8 @@ static enum sp_return set_config(struct sp_port *port, struct port_data *data,
        if (baud_nonstd)
                TRY(set_baudrate(port->fd, config->baudrate));
 #ifdef USE_TERMIOX
-       TRY(set_flow(port->fd, data->flow));
+       if (data->termiox_supported)
+               TRY(set_flow(port->fd, data->flow));
 #endif
 #endif
 
@@ -1399,3 +1428,19 @@ void sp_free_error_message(char *message)
        (void)message;
 #endif
 }
+
+void sp_set_debug_handler(void (*handler)(const char *format, ...))
+{
+       sp_debug_handler = handler;
+}
+
+void sp_default_debug_handler(const char *format, ...)
+{
+       va_list args;
+       va_start(args, format);
+       if (getenv("LIBSERIALPORT_DEBUG")) {
+               fputs("libserialport: ", stderr);
+               vfprintf(stderr, format, args);
+       }
+       va_end(args);
+}