+ config->bits = -1;
+ }
+
+ if (!(data->term.c_cflag & PARENB) && (data->term.c_iflag & IGNPAR))
+ config->parity = SP_PARITY_NONE;
+ else if (!(data->term.c_cflag & PARENB) || (data->term.c_iflag & IGNPAR))
+ config->parity = -1;
+#ifdef CMSPAR
+ else if (data->term.c_cflag & CMSPAR)
+ config->parity = (data->term.c_cflag & PARODD) ? SP_PARITY_MARK : SP_PARITY_SPACE;
+#endif
+ else
+ config->parity = (data->term.c_cflag & PARODD) ? SP_PARITY_ODD : SP_PARITY_EVEN;
+
+ config->stopbits = (data->term.c_cflag & CSTOPB) ? 2 : 1;
+
+ if (data->term.c_cflag & CRTSCTS) {
+ config->rts = SP_RTS_FLOW_CONTROL;
+ config->cts = SP_CTS_FLOW_CONTROL;
+ } else {
+ if (data->termiox_supported && data->rts_flow)
+ config->rts = SP_RTS_FLOW_CONTROL;
+ else
+ config->rts = (data->controlbits & TIOCM_RTS) ? SP_RTS_ON : SP_RTS_OFF;
+
+ config->cts = (data->termiox_supported && data->cts_flow) ?
+ SP_CTS_FLOW_CONTROL : SP_CTS_IGNORE;
+ }
+
+ if (data->termiox_supported && data->dtr_flow)
+ config->dtr = SP_DTR_FLOW_CONTROL;
+ else
+ config->dtr = (data->controlbits & TIOCM_DTR) ? SP_DTR_ON : SP_DTR_OFF;
+
+ config->dsr = (data->termiox_supported && data->dsr_flow) ?
+ SP_DSR_FLOW_CONTROL : SP_DSR_IGNORE;
+
+ if (data->term.c_iflag & IXOFF) {
+ if (data->term.c_iflag & IXON)
+ config->xon_xoff = SP_XONXOFF_INOUT;
+ else
+ config->xon_xoff = SP_XONXOFF_IN;
+ } else {
+ if (data->term.c_iflag & IXON)
+ config->xon_xoff = SP_XONXOFF_OUT;
+ else
+ config->xon_xoff = SP_XONXOFF_DISABLED;
+ }
+#endif
+
+ 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_FMT("Setting configuration for port %s", port->name);
+
+#ifdef _WIN32
+ BYTE* new_buf;
+
+ TRY(await_write_completion(port));
+
+ 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;
+
+ /* Allocate write buffer for 50ms of data at baud rate. */
+ port->write_buf_size = max(config->baudrate / (8 * 20), 1);
+ new_buf = realloc(port->write_buf, port->write_buf_size);
+ if (!new_buf)
+ RETURN_ERROR(SP_ERR_MEM, "Allocating write buffer failed");
+ port->write_buf = new_buf;
+ }
+
+ 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");
+ }