+ 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();
+}
+
+SP_API 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();
+}
+
+SP_API void sp_free_config(struct sp_port_config *config)
+{
+ TRACE("%p", config);
+
+ if (!config)
+ DEBUG("Null config");