#include "libserialport.h"
+struct sp_port {
+ char *name;
+ int nonblocking;
+#ifdef _WIN32
+ HANDLE hdl;
+ OVERLAPPED write_ovl;
+ BYTE pending_byte;
+ BOOL writing;
+#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;
#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 SET_FAIL(val, msg) do { DEBUG_FAIL(msg); val = SP_ERR_FAIL; } 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)
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);
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);
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) {
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");
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);
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;
DEBUG("Opening port %s", port->name);
+ port->nonblocking = (flags & SP_MODE_NONBLOCK) ? 1 : 0;
+
#ifdef _WIN32
DWORD desired_access = 0, flags_and_attributes = 0;
+ COMMTIMEOUTS timeouts;
char *escaped_port_name;
/* Prefix port name with '\\.\' to work with ports above COM9. */
if (port->hdl == INVALID_HANDLE_VALUE)
RETURN_FAIL("CreateFile() failed");
+
+ /* All timeouts disabled. */
+ timeouts.ReadIntervalTimeout = 0;
+ timeouts.ReadTotalTimeoutMultiplier = 0;
+ timeouts.ReadTotalTimeoutConstant = 0;
+ timeouts.WriteTotalTimeoutMultiplier = 0;
+ timeouts.WriteTotalTimeoutConstant = 0;
+
+ if (port->nonblocking) {
+ /* Set read timeout such that all reads return immediately. */
+ timeouts.ReadIntervalTimeout = MAXDWORD;
+ /* Prepare OVERLAPPED structure for non-blocking writes. */
+ memset(&port->write_ovl, 0, sizeof(port->write_ovl));
+ if (!(port->write_ovl.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL))) {
+ sp_close(port);
+ RETURN_FAIL("CreateEvent() failed");
+ }
+ port->writing = FALSE;
+ }
+
+ if (SetCommTimeouts(port->hdl, &timeouts) == 0) {
+ sp_close(port);
+ RETURN_FAIL("SetCommTimeouts() failed");
+ }
#else
int flags_local = 0;
struct port_data data;
RETURN_CODEVAL(ret);
}
- /* Turn off all serial port cooking. */
- data.term.c_iflag &= ~(ISTRIP | INLCR | ICRNL);
- data.term.c_oflag &= ~(ONLCR | OCRNL | ONOCR);
-#if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__)
+ /* Turn off all fancy termios tricks, give us a raw channel. */
+ data.term.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IUCLC|IMAXBEL);
+ data.term.c_oflag &= ~(OPOST|OLCUC|ONLCR|OCRNL|ONOCR|ONLRET|NLDLY|CRDLY|TABDLY|BSDLY|VTDLY|FFDLY);
+#ifdef OFILL
data.term.c_oflag &= ~OFILL;
#endif
- /* Disable canonical mode, and don't echo input characters. */
- data.term.c_lflag &= ~(ICANON | ECHO);
+ data.term.c_lflag &= ~(ISIG|ICANON|ECHO|IEXTEN);
+ data.term.c_cc[VMIN] = 0;
+ data.term.c_cc[VTIME] = 0;
- /* Ignore modem status lines; enable receiver */
- data.term.c_cflag |= (CLOCAL | CREAD);
+ /* Ignore modem status lines; enable receiver; leave control lines alone on close. */
+ data.term.c_cflag |= (CLOCAL | CREAD | HUPCL);
ret = set_config(port, &data, &config);
if (CloseHandle(port->hdl) == 0)
RETURN_FAIL("CloseHandle() failed");
port->hdl = INVALID_HANDLE_VALUE;
+ if (port->nonblocking) {
+ /* Close event handle created for overlapped writes. */
+ if (CloseHandle(port->write_ovl.hEvent) == 0)
+ RETURN_FAIL("CloseHandle() failed");
+ }
#else
/* Returns 0 upon success, -1 upon failure. */
if (close(port->fd) == -1)
if (buffers > SP_BUF_BOTH)
RETURN_ERROR(SP_ERR_ARG, "Invalid buffer selection");
- const char *buffer_names[] = {"input", "output", "both"};
+ const char *buffer_names[] = {"no", "input", "output", "both"};
DEBUG("Flushing %s buffers on port %s", buffer_names[buffers], port->name);
DEBUG("Writing up to %d bytes to port %s", count, port->name);
+ if (count == 0)
+ RETURN_VALUE("0", 0);
+
#ifdef _WIN32
DWORD written = 0;
+ BYTE *ptr = (BYTE *) buf;
+
+ if (port->nonblocking) {
+ /* Non-blocking write. */
+
+ /* Check whether previous write is complete. */
+ if (port->writing) {
+ if (HasOverlappedIoCompleted(&port->write_ovl)) {
+ DEBUG("Previous write completed");
+ port->writing = 0;
+ } else {
+ DEBUG("Previous write not complete");
+ /* Can't take a new write until the previous one finishes. */
+ RETURN_VALUE("0", 0);
+ }
+ }
+
+ /* Keep writing data until the OS has to actually start an async IO for it.
+ * At that point we know the buffer is full. */
+ while (written < count)
+ {
+ /* Copy first byte of user buffer. */
+ port->pending_byte = *ptr++;
+
+ /* Start asynchronous write. */
+ if (WriteFile(port->hdl, &port->pending_byte, 1, NULL, &port->write_ovl) == 0) {
+ if (GetLastError() == ERROR_IO_PENDING) {
+ DEBUG("Asynchronous write started");
+ port->writing = 1;
+ RETURN_VALUE("%d", ++written);
+ } else {
+ /* Actual failure of some kind. */
+ RETURN_FAIL("WriteFile() failed");
+ }
+ } else {
+ DEBUG("Single byte written immediately.");
+ written++;
+ }
+ }
+
+ DEBUG("All bytes written immediately.");
+
+ } else {
+ /* Blocking write. */
+ if (WriteFile(port->hdl, buf, count, &written, NULL) == 0) {
+ RETURN_FAIL("WriteFile() failed");
+ }
+ }
- /* Returns non-zero upon success, 0 upon failure. */
- if (WriteFile(port->hdl, buf, count, &written, NULL) == 0)
- RETURN_FAIL("WriteFile() failed");
RETURN_VALUE("%d", written);
#else
/* Returns the number of bytes written, or -1 upon failure. */
ssize_t bytes_read;
/* Returns the number of bytes read, or -1 upon failure. */
- if ((bytes_read = read(port->fd, buf, count)) < 0)
- RETURN_FAIL("read() failed");
+ if ((bytes_read = read(port->fd, buf, count)) < 0) {
+ if (port->nonblocking && errno == EAGAIN)
+ /* Port is opened in nonblocking mode and there are no bytes available. */
+ bytes_read = 0;
+ else
+ /* This is an actual failure. */
+ RETURN_FAIL("read() failed");
+ }
RETURN_VALUE("%d", bytes_read);
#endif
}
case NOPARITY:
config->parity = SP_PARITY_NONE;
break;
+ case ODDPARITY:
+ config->parity = SP_PARITY_ODD;
+ break;
case EVENPARITY:
config->parity = SP_PARITY_EVEN;
break;
- case ODDPARITY:
- config->parity = SP_PARITY_ODD;
+ case MARKPARITY:
+ config->parity = SP_PARITY_MARK;
+ break;
+ case SPACEPARITY:
+ config->parity = SP_PARITY_SPACE;
break;
default:
config->parity = -1;
config->parity = SP_PARITY_NONE;
else if (!(data->term.c_cflag & PARENB) || (data->term.c_iflag & IGNPAR))
config->parity = -1;
+ else if (data->term.c_cflag & CMSPAR)
+ config->parity = (data->term.c_cflag & PARODD) ? SP_PARITY_MARK : SP_PARITY_SPACE;
else
config->parity = (data->term.c_cflag & PARODD) ? SP_PARITY_ODD : SP_PARITY_EVEN;
case SP_PARITY_NONE:
data->dcb.Parity = NOPARITY;
break;
+ case SP_PARITY_ODD:
+ data->dcb.Parity = ODDPARITY;
+ break;
case SP_PARITY_EVEN:
data->dcb.Parity = EVENPARITY;
break;
- case SP_PARITY_ODD:
- data->dcb.Parity = ODDPARITY;
+ case SP_PARITY_MARK:
+ data->dcb.Parity = MARKPARITY;
+ break;
+ case SP_PARITY_SPACE:
+ data->dcb.Parity = SPACEPARITY;
break;
default:
RETURN_ERROR(SP_ERR_ARG, "Invalid parity setting");
if (config->parity >= 0) {
data->term.c_iflag &= ~IGNPAR;
- data->term.c_cflag &= ~(PARENB | PARODD);
+ data->term.c_cflag &= ~(PARENB | PARODD | CMSPAR);
switch (config->parity) {
case SP_PARITY_NONE:
data->term.c_iflag |= IGNPAR;
case SP_PARITY_ODD:
data->term.c_cflag |= PARENB | PARODD;
break;
+ case SP_PARITY_MARK:
+ data->term.c_cflag |= PARENB | PARODD | CMSPAR;
+ break;
+ case SP_PARITY_SPACE:
+ data->term.c_cflag |= PARENB | CMSPAR;
+ break;
default:
RETURN_ERROR(SP_ERR_ARG, "Invalid parity setting");
}
RETURN_OK();
}
+enum sp_return sp_new_config(struct sp_port_config **config_ptr)
+{
+ TRACE("%p", config_ptr);
+ struct sp_port_config *config;
+
+ if (!config_ptr)
+ RETURN_ERROR(SP_ERR_ARG, "Null result pointer");
+
+ *config_ptr = NULL;
+
+ if (!(config = malloc(sizeof(struct sp_port_config))))
+ RETURN_ERROR(SP_ERR_MEM, "config malloc failed");
+
+ config->baudrate = -1;
+ config->bits = -1;
+ config->parity = -1;
+ config->stopbits = -1;
+ config->rts = -1;
+ config->cts = -1;
+ config->dtr = -1;
+ config->dsr = -1;
+
+ *config_ptr = config;
+
+ 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;
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); \
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_OPEN_PORT();
+ if (!config)
+ RETURN_ERROR(SP_ERR_ARG, "Null configuration");
if (flowcontrol > SP_FLOWCONTROL_DTRDSR)
RETURN_ERROR(SP_ERR_ARG, "Invalid flow control setting");
- TRY(get_config(port, &data, &config));
-
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();
*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;