+ RETURN_OK();
+}
+
+static enum sp_return set_config(struct sp_port *port, struct port_data *data,
+ const struct sp_port_config *config)
+{
+ unsigned int i;
+#ifdef __APPLE__
+ BAUD_TYPE baud_nonstd;
+
+ baud_nonstd = B0;
+#endif
+#ifdef USE_TERMIOS_SPEED
+ int baud_nonstd = 0;
+#endif
+
+ TRACE("%p, %p, %p", port, data, config);
+
+ DEBUG("Setting configuration for port %s", port->name);
+
+#ifdef _WIN32
+ if (config->baudrate >= 0) {
+ for (i = 0; i < NUM_STD_BAUDRATES; i++) {
+ if (config->baudrate == std_baudrates[i].value) {
+ data->dcb.BaudRate = std_baudrates[i].index;
+ break;
+ }
+ }
+
+ if (i == NUM_STD_BAUDRATES)
+ data->dcb.BaudRate = config->baudrate;
+ }
+
+ if (config->bits >= 0)
+ data->dcb.ByteSize = config->bits;
+
+ if (config->parity >= 0) {
+ switch (config->parity) {
+ 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_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->stopbits >= 0) {
+ switch (config->stopbits) {
+ /* Note: There's also ONE5STOPBITS == 1.5 (unneeded so far). */
+ case 1:
+ data->dcb.StopBits = ONESTOPBIT;
+ break;
+ case 2:
+ data->dcb.StopBits = TWOSTOPBITS;
+ break;
+ default:
+ RETURN_ERROR(SP_ERR_ARG, "Invalid stop bit setting");
+ }
+ }
+
+ if (config->rts >= 0) {
+ switch (config->rts) {
+ case SP_RTS_OFF:
+ data->dcb.fRtsControl = RTS_CONTROL_DISABLE;
+ break;
+ case SP_RTS_ON:
+ data->dcb.fRtsControl = RTS_CONTROL_ENABLE;
+ break;
+ case SP_RTS_FLOW_CONTROL:
+ data->dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
+ break;
+ default:
+ RETURN_ERROR(SP_ERR_ARG, "Invalid RTS setting");
+ }
+ }
+
+ if (config->cts >= 0) {
+ switch (config->cts) {
+ case SP_CTS_IGNORE:
+ data->dcb.fOutxCtsFlow = FALSE;
+ break;
+ case SP_CTS_FLOW_CONTROL:
+ data->dcb.fOutxCtsFlow = TRUE;
+ break;
+ default:
+ RETURN_ERROR(SP_ERR_ARG, "Invalid CTS setting");
+ }
+ }
+
+ if (config->dtr >= 0) {
+ switch (config->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_ERROR(SP_ERR_ARG, "Invalid DTR setting");
+ }
+ }
+
+ if (config->dsr >= 0) {
+ switch (config->dsr) {
+ case SP_DSR_IGNORE:
+ data->dcb.fOutxDsrFlow = FALSE;
+ break;
+ case SP_DSR_FLOW_CONTROL:
+ data->dcb.fOutxDsrFlow = TRUE;
+ break;
+ default:
+ RETURN_ERROR(SP_ERR_ARG, "Invalid DSR setting");
+ }
+ }
+
+ if (config->xon_xoff >= 0) {
+ switch (config->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_ERROR(SP_ERR_ARG, "Invalid XON/XOFF setting");
+ }
+ }
+
+ if (!SetCommState(port->hdl, &data->dcb))
+ RETURN_FAIL("SetCommState() failed");
+
+#else /* !_WIN32 */
+
+ int controlbits;
+
+ if (config->baudrate >= 0) {
+ for (i = 0; i < NUM_STD_BAUDRATES; i++) {
+ if (config->baudrate == std_baudrates[i].value) {
+ if (cfsetospeed(&data->term, std_baudrates[i].index) < 0)
+ RETURN_FAIL("cfsetospeed() failed");
+
+ if (cfsetispeed(&data->term, std_baudrates[i].index) < 0)
+ RETURN_FAIL("cfsetispeed() failed");
+ break;
+ }
+ }
+
+ /* Non-standard baud rate */
+ if (i == NUM_STD_BAUDRATES) {
+#ifdef __APPLE__
+ /* Set "dummy" baud rate. */
+ if (cfsetspeed(&data->term, B9600) < 0)
+ RETURN_FAIL("cfsetspeed() failed");
+ baud_nonstd = config->baudrate;
+#elif defined(USE_TERMIOS_SPEED)
+ baud_nonstd = 1;
+#else
+ RETURN_ERROR(SP_ERR_SUPP, "Non-standard baudrate not supported");
+#endif
+ }
+ }
+
+ if (config->bits >= 0) {
+ data->term.c_cflag &= ~CSIZE;
+ switch (config->bits) {
+ case 8:
+ data->term.c_cflag |= CS8;
+ break;
+ case 7:
+ data->term.c_cflag |= CS7;
+ break;
+ case 6:
+ data->term.c_cflag |= CS6;
+ break;
+ case 5:
+ data->term.c_cflag |= CS5;
+ break;
+ default:
+ RETURN_ERROR(SP_ERR_ARG, "Invalid data bits setting");
+ }
+ }
+
+ if (config->parity >= 0) {
+ data->term.c_iflag &= ~IGNPAR;
+ data->term.c_cflag &= ~(PARENB | PARODD);
+#ifdef CMSPAR
+ data->term.c_cflag &= ~CMSPAR;
+#endif
+ switch (config->parity) {
+ case SP_PARITY_NONE:
+ data->term.c_iflag |= IGNPAR;
+ break;
+ case SP_PARITY_EVEN:
+ data->term.c_cflag |= PARENB;
+ break;
+ case SP_PARITY_ODD:
+ data->term.c_cflag |= PARENB | PARODD;
+ break;
+#ifdef CMSPAR
+ case SP_PARITY_MARK:
+ data->term.c_cflag |= PARENB | PARODD;
+ data->term.c_cflag |= CMSPAR;
+ break;
+ case SP_PARITY_SPACE:
+ data->term.c_cflag |= PARENB;
+ data->term.c_cflag |= CMSPAR;
+ break;
+#else
+ case SP_PARITY_MARK:
+ case SP_PARITY_SPACE:
+ RETURN_ERROR(SP_ERR_SUPP, "Mark/space parity not supported");
+#endif
+ default:
+ RETURN_ERROR(SP_ERR_ARG, "Invalid parity setting");
+ }
+ }
+
+ if (config->stopbits >= 0) {
+ data->term.c_cflag &= ~CSTOPB;
+ switch (config->stopbits) {
+ case 1:
+ data->term.c_cflag &= ~CSTOPB;
+ break;
+ case 2:
+ data->term.c_cflag |= CSTOPB;
+ break;
+ default:
+ RETURN_ERROR(SP_ERR_ARG, "Invalid stop bits setting");
+ }
+ }
+
+ if (config->rts >= 0 || config->cts >= 0) {
+ if (data->termiox_supported) {
+ data->rts_flow = data->cts_flow = 0;
+ 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_FAIL("Setting RTS signal level failed");
+ break;
+ case SP_RTS_FLOW_CONTROL:
+ data->rts_flow = 1;
+ break;
+ default:
+ break;
+ }
+ if (config->cts == SP_CTS_FLOW_CONTROL)
+ data->cts_flow = 1;
+
+ if (data->rts_flow && data->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_ERROR(SP_ERR_SUPP, "RTS & CTS flow control must be disabled together");
+ }
+ if (config->cts >= 0 && config->cts != SP_CTS_FLOW_CONTROL) {
+ if (config->rts <= 0 || config->rts == SP_RTS_FLOW_CONTROL)
+ RETURN_ERROR(SP_ERR_SUPP, "RTS & CTS flow control must be disabled together");
+ }
+ } 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_ERROR(SP_ERR_SUPP, "RTS & CTS flow control must be enabled together");
+ }
+
+ 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_FAIL("Setting RTS signal level failed");
+ }
+ }
+ }
+ }
+
+ if (config->dtr >= 0 || config->dsr >= 0) {
+ if (data->termiox_supported) {
+ data->dtr_flow = data->dsr_flow = 0;
+ 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_FAIL("Setting DTR signal level failed");
+ break;
+ case SP_DTR_FLOW_CONTROL:
+ data->dtr_flow = 1;
+ break;
+ default:
+ break;
+ }
+ if (config->dsr == SP_DSR_FLOW_CONTROL)
+ data->dsr_flow = 1;
+ } else {
+ /* DTR/DSR flow control not supported. */
+ if (config->dtr == SP_DTR_FLOW_CONTROL || config->dsr == SP_DSR_FLOW_CONTROL)
+ RETURN_ERROR(SP_ERR_SUPP, "DTR/DSR flow control not supported");
+
+ if (config->dtr >= 0) {
+ controlbits = TIOCM_DTR;
+ if (ioctl(port->fd, config->dtr == SP_DTR_ON ? TIOCMBIS : TIOCMBIC,
+ &controlbits) < 0)
+ RETURN_FAIL("Setting DTR signal level failed");
+ }
+ }
+ }
+
+ if (config->xon_xoff >= 0) {
+ data->term.c_iflag &= ~(IXON | IXOFF | IXANY);
+ switch (config->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_ERROR(SP_ERR_ARG, "Invalid XON/XOFF setting");
+ }
+ }
+
+ if (tcsetattr(port->fd, TCSANOW, &data->term) < 0)
+ RETURN_FAIL("tcsetattr() failed");
+
+#ifdef __APPLE__
+ if (baud_nonstd != B0) {
+ if (ioctl(port->fd, IOSSIOSPEED, &baud_nonstd) == -1)
+ RETURN_FAIL("IOSSIOSPEED ioctl failed");
+ /* Set baud rates in data->term to correct, but incompatible
+ * with tcsetattr() value, same as delivered by tcgetattr(). */
+ if (cfsetspeed(&data->term, baud_nonstd) < 0)
+ RETURN_FAIL("cfsetspeed() failed");
+ }
+#elif defined(__linux__)
+#ifdef USE_TERMIOS_SPEED
+ if (baud_nonstd)
+ TRY(set_baudrate(port->fd, config->baudrate));
+#endif
+#ifdef USE_TERMIOX
+ if (data->termiox_supported)
+ TRY(set_flow(port->fd, data));
+#endif
+#endif
+
+#endif /* !_WIN32 */
+
+ RETURN_OK();
+}
+
+enum sp_return sp_new_config(struct sp_port_config **config_ptr)
+{
+ struct sp_port_config *config;
+
+ TRACE("%p", config_ptr);
+
+ 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;
+ struct sp_port_config prev_config;
+
+ TRACE("%p, %p", port, config);
+
+ CHECK_OPEN_PORT();
+
+ if (!config)
+ RETURN_ERROR(SP_ERR_ARG, "Null config");
+
+ TRY(get_config(port, &data, &prev_config));
+ TRY(set_config(port, &data, config));
+
+ RETURN_OK();
+}
+
+#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); \
+ CHECK_OPEN_PORT(); \
+ TRY(get_config(port, &data, &config)); \
+ 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, %p", config, x); \
+ 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_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)
+{
+ if (!config)
+ RETURN_ERROR(SP_ERR_ARG, "Null configuration");
+
+ if (flowcontrol > SP_FLOWCONTROL_DTRDSR)
+ RETURN_ERROR(SP_ERR_ARG, "Invalid flow control setting");
+
+ if (flowcontrol == SP_FLOWCONTROL_XONXOFF)
+ config->xon_xoff = SP_XONXOFF_INOUT;
+ else
+ config->xon_xoff = SP_XONXOFF_DISABLED;
+
+ if (flowcontrol == SP_FLOWCONTROL_RTSCTS) {
+ 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 (flowcontrol == SP_FLOWCONTROL_DTRDSR) {
+ 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;
+ }
+
+ 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();
+}
+
+enum sp_return sp_get_signals(struct sp_port *port, enum sp_signal *signals)
+{
+ TRACE("%p, %p", port, signals);
+
+ CHECK_OPEN_PORT();
+
+ if (!signals)
+ RETURN_ERROR(SP_ERR_ARG, "Null result pointer");
+
+ DEBUG("Getting control signals for port %s", port->name);
+
+ *signals = 0;
+#ifdef _WIN32
+ DWORD bits;
+ if (GetCommModemStatus(port->hdl, &bits) == 0)
+ RETURN_FAIL("GetCommModemStatus() failed");
+ if (bits & MS_CTS_ON)
+ *signals |= SP_SIG_CTS;
+ if (bits & MS_DSR_ON)
+ *signals |= SP_SIG_DSR;
+ if (bits & MS_RLSD_ON)
+ *signals |= SP_SIG_DCD;
+ if (bits & MS_RING_ON)
+ *signals |= SP_SIG_RI;
+#else
+ int bits;
+ if (ioctl(port->fd, TIOCMGET, &bits) < 0)
+ RETURN_FAIL("TIOCMGET ioctl failed");
+ 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_OK();
+}
+
+enum sp_return sp_start_break(struct sp_port *port)
+{
+ TRACE("%p", port);
+
+ CHECK_OPEN_PORT();
+#ifdef _WIN32
+ if (SetCommBreak(port->hdl) == 0)
+ RETURN_FAIL("SetCommBreak() failed");
+#else
+ if (ioctl(port->fd, TIOCSBRK, 1) < 0)
+ RETURN_FAIL("TIOCSBRK ioctl failed");
+#endif
+
+ RETURN_OK();
+}
+
+enum sp_return sp_end_break(struct sp_port *port)
+{
+ TRACE("%p", port);
+
+ CHECK_OPEN_PORT();
+#ifdef _WIN32
+ if (ClearCommBreak(port->hdl) == 0)
+ RETURN_FAIL("ClearCommBreak() failed");
+#else
+ if (ioctl(port->fd, TIOCCBRK, 1) < 0)
+ RETURN_FAIL("TIOCCBRK ioctl failed");
+#endif
+
+ RETURN_OK();