#else
struct termios term;
int rts;
+ int cts;
int dtr;
+ int dsr;
#endif
};
+/* Helper functions for configuring ports. */
+static int start_config(struct sp_port *port, struct sp_port_data *data);
+static int set_baudrate(struct sp_port_data *data, int baudrate);
+static int set_bits(struct sp_port_data *data, int bits);
+static int set_parity(struct sp_port_data *data, int parity);
+static int set_stopbits(struct sp_port_data *data, int stopbits);
+static int set_rts(struct sp_port_data *data, int rts);
+static int set_cts(struct sp_port_data *data, int cts);
+static int set_dtr(struct sp_port_data *data, int dtr);
+static int set_dsr(struct sp_port_data *data, int dsr);
+static int set_xon_xoff(struct sp_port_data *data, int xon_xoff);
+static int apply_config(struct sp_port *port, struct sp_port_data *data);
+
int sp_get_port_by_name(const char *portname, struct sp_port **port_ptr)
{
struct sp_port *port;
return SP_ERR_FAIL;
#else
int flags_local = 0;
+ struct sp_port_data data;
/* Map 'flags' to the OS-specific settings. */
if (flags & SP_MODE_RDWR)
if ((port->fd = open(port->name, flags_local)) < 0)
return SP_ERR_FAIL;
+
+ start_config(port, &data);
+
+ /* 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__)
+ data.term.c_oflag &= ~OFILL;
+#endif
+ /* Disable canonical mode, and don't echo input characters. */
+ data.term.c_lflag &= ~(ICANON | ECHO);
+
+ /* Ignore modem status lines; enable receiver */
+ data.term.c_cflag |= (CLOCAL | CREAD);
+
+ apply_config(port, &data);
#endif
return SP_OK;
if (!GetCommState(port->hdl, &data->dcb))
return SP_ERR_FAIL;
#else
+ int controlbits;
+
if (tcgetattr(port->fd, &data->term) < 0)
return SP_ERR_FAIL;
+
+ if (ioctl(port->fd, TIOCMGET, &controlbits) < 0)
+ return SP_ERR_FAIL;
+
+ if (data->term.c_cflag & CRTSCTS) {
+ data->rts = SP_RTS_FLOW_CONTROL;
+ data->cts = SP_CTS_FLOW_CONTROL;
+ } else {
+ data->rts = (controlbits & TIOCM_RTS) ? SP_RTS_ON : SP_RTS_OFF;
+ data->cts = SP_CTS_IGNORE;
+ }
+
+ data->dtr = (controlbits & TIOCM_DTR) ? SP_DTR_ON : SP_DTR_OFF;
+ data->dsr = SP_DSR_IGNORE;
#endif
return SP_OK;
}
return SP_OK;
}
-static int set_flowcontrol(struct sp_port_data *data, int flowcontrol)
+static int set_rts(struct sp_port_data *data, int rts)
{
-#ifndef _WIN32
- data->term.c_iflag &= ~(IXON | IXOFF | IXANY);
- data->term.c_cflag &= ~CRTSCTS;
- switch (flowcontrol) {
- case 0:
- /* No flow control. */
+#ifdef _WIN32
+ switch (rts) {
+ case SP_RTS_OFF:
+ data->dcb.fRtsControl = RTS_CONTROL_DISABLE;
break;
- case 1:
- data->term.c_cflag |= CRTSCTS;
+ case SP_RTS_ON:
+ data->dcb.fRtsControl = RTS_CONTROL_ENABLE;
break;
- case 2:
- data->term.c_iflag |= IXON | IXOFF | IXANY;
+ case SP_RTS_FLOW_CONTROL:
+ data->dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
break;
default:
return SP_ERR_ARG;
}
+#else
+ data->rts = rts;
#endif
return SP_OK;
}
-static int set_rts(struct sp_port_data *data, int rts)
+static int set_cts(struct sp_port_data *data, int cts)
{
#ifdef _WIN32
- if (rts)
- data->dcb.fRtsControl = RTS_CONTROL_ENABLE;
- else
- data->dcb.fRtsControl = RTS_CONTROL_DISABLE;
+ switch (cts) {
+ case SP_CTS_IGNORE:
+ data->dcb.fOutxCtsFlow = FALSE;
+ break;
+ case SP_CTS_FLOW_CONTROL:
+ data->dcb.fOutxCtsFlow = TRUE;
+ break;
+ default:
+ return SP_ERR_ARG;
+ }
#else
- data->rts = rts;
+ data->cts = cts;
#endif
return SP_OK;
}
static int set_dtr(struct sp_port_data *data, int dtr)
{
#ifdef _WIN32
- if (dtr)
- data->dcb.fDtrControl = DTR_CONTROL_ENABLE;
- else
+ switch (dtr) {
+ case SP_DTR_OFF:
data->dcb.fDtrControl = DTR_CONTROL_DISABLE;
+ break;
+ case SP_DTR_ON:
+ data->dcb.fDtrControl = DTR_CONTROL_ENABLE;
+ break;
+ case SP_DTR_FLOW_CONTROL:
+ data->dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
+ break;
+ default:
+ return SP_ERR_ARG;
+ }
#else
data->dtr = dtr;
#endif
return SP_OK;
}
+static int set_dsr(struct sp_port_data *data, int dsr)
+{
+#ifdef _WIN32
+ switch (dsr) {
+ case SP_DSR_IGNORE:
+ data->dcb.fOutxDsrFlow = FALSE;
+ break;
+ case SP_DSR_FLOW_CONTROL:
+ data->dcb.fOutxDsrFlow = TRUE;
+ break;
+ default:
+ return SP_ERR_ARG;
+ }
+#else
+ data->dsr = dsr;
+#endif
+ return SP_OK;
+}
+
+static int set_xon_xoff(struct sp_port_data *data, int xon_xoff)
+{
+#ifdef _WIN32
+ switch (xon_xoff) {
+ case SP_XONXOFF_DISABLED:
+ data->dcb.fInX = FALSE;
+ data->dcb.fOutX = FALSE;
+ break;
+ case SP_XONXOFF_IN:
+ data->dcb.fInX = TRUE;
+ data->dcb.fOutX = FALSE;
+ break;
+ case SP_XONXOFF_OUT:
+ data->dcb.fInX = FALSE;
+ data->dcb.fOutX = TRUE;
+ break;
+ case SP_XONXOFF_INOUT:
+ data->dcb.fInX = TRUE;
+ data->dcb.fOutX = TRUE;
+ break;
+ default:
+ return SP_ERR_ARG;
+ }
+#else
+ data->term.c_iflag &= ~(IXON | IXOFF | IXANY);
+ switch (xon_xoff) {
+ case SP_XONXOFF_DISABLED:
+ break;
+ case SP_XONXOFF_IN:
+ data->term.c_iflag |= IXOFF;
+ break;
+ case SP_XONXOFF_OUT:
+ data->term.c_iflag |= IXON | IXANY;
+ break;
+ case SP_XONXOFF_INOUT:
+ data->term.c_iflag |= IXON | IXOFF | IXANY;
+ break;
+ default:
+ return SP_ERR_ARG;
+ }
+#endif
+ return SP_OK;
+}
+
static int apply_config(struct sp_port *port, struct sp_port_data *data)
{
#ifdef _WIN32
#else
int controlbits;
- /* 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__)
- data->term.c_oflag &= ~OFILL;
-#endif
- /* Disable canonical mode, and don't echo input characters. */
- data->term.c_lflag &= ~(ICANON | ECHO);
-
- /* Ignore modem status lines; enable receiver */
- data->term.c_cflag |= (CLOCAL | CREAD);
+ /* Asymmetric use of RTS/CTS not supported yet. */
+ if ((data->rts == SP_RTS_FLOW_CONTROL) != (data->cts == SP_CTS_FLOW_CONTROL))
+ return SP_ERR_ARG;
- /* Write the configured settings. */
- if (tcsetattr(port->fd, TCSADRAIN, &data->term) < 0)
- return SP_ERR_FAIL;
+ /* DTR/DSR flow control not supported yet. */
+ if (data->dtr == SP_DTR_FLOW_CONTROL || data->dsr == SP_DSR_FLOW_CONTROL)
+ return SP_ERR_ARG;
- if (data->rts != -1) {
+ if (data->rts == SP_RTS_FLOW_CONTROL)
+ data->term.c_iflag |= CRTSCTS;
+ else
+ {
controlbits = TIOCM_RTS;
- if (ioctl(port->fd, data->rts ? TIOCMBIS : TIOCMBIC,
+ if (ioctl(port->fd, data->rts == SP_RTS_ON ? TIOCMBIS : TIOCMBIC,
&controlbits) < 0)
return SP_ERR_FAIL;
}
- if (data->dtr != -1) {
- controlbits = TIOCM_DTR;
- if (ioctl(port->fd, data->dtr ? TIOCMBIS : TIOCMBIC,
- &controlbits) < 0)
- return SP_ERR_FAIL;
- }
+ controlbits = TIOCM_DTR;
+ if (ioctl(port->fd, data->dtr == SP_DTR_ON ? TIOCMBIS : TIOCMBIC,
+ &controlbits) < 0)
+ return SP_ERR_FAIL;
+
+ /* Write the configured settings. */
+ if (tcsetattr(port->fd, TCSADRAIN, &data->term) < 0)
+ return SP_ERR_FAIL;
#endif
return SP_OK;
}
TRY_SET(bits);
TRY_SET(parity);
TRY_SET(stopbits);
- TRY_SET(flowcontrol);
TRY_SET(rts);
+ TRY_SET(cts);
TRY_SET(dtr);
+ TRY_SET(dsr);
+ TRY_SET(xon_xoff);
TRY(apply_config(port, &data));
return SP_OK;
}
+#define CREATE_SETTER(x) int sp_set_##x(struct sp_port *port, int x) { \
+ struct sp_port_data data; \
+ TRY(start_config(port, &data)); \
+ TRY(set_##x(&data, x)); \
+ TRY(apply_config(port, &data)); \
+ return SP_OK; \
+}
+
+CREATE_SETTER(baudrate)
+CREATE_SETTER(bits)
+CREATE_SETTER(parity)
+CREATE_SETTER(stopbits)
+CREATE_SETTER(rts)
+CREATE_SETTER(cts)
+CREATE_SETTER(dtr)
+CREATE_SETTER(dsr)
+CREATE_SETTER(xon_xoff)
+
int sp_last_error_code(void)
{
#ifdef _WIN32