#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 {
#else
struct termios term;
int controlbits;
-#ifdef USE_TERMIOX
+ int termiox_supported;
int flow;
#endif
-#endif
};
/* Standard baud rates. */
#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)
flags = TCIOFLUSH;
else if (buffers & SP_BUF_INPUT)
flags = TCIFLUSH;
- if (buffers & SP_BUF_OUTPUT)
+ else if (buffers & SP_BUF_OUTPUT)
flags = TCOFLUSH;
/* Returns 0 upon success, -1 upon failure. */
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;
}
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;
}
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;
}
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;
}
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++) {
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)
/* 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;
+ return SP_ERR_SUPP;
#endif
}
}
}
if (config->rts >= 0 || config->cts >= 0) {
-#ifdef USE_TERMIOX
- data->flow &= ~(RTS_FLOW | CTS_FLOW);
- switch (config->rts) {
+ 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)
+ if (ioctl(port->fd, config->rts == SP_RTS_ON ? TIOCMBIS : TIOCMBIC, &controlbits) < 0)
return SP_ERR_FAIL;
break;
case SP_RTS_FLOW_CONTROL:
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;
- }
- } 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) {
+ 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)
+ if (ioctl(port->fd, config->dtr == SP_DTR_ON ? TIOCMBIS : TIOCMBIC, &controlbits) < 0)
return SP_ERR_FAIL;
break;
case SP_DTR_FLOW_CONTROL:
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->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) {
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
(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);
+}