+ 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));