+ 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();
+}
+
+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");
+ else
+ free(config);
+
+ RETURN();
+}
+
+SP_API 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();
+}
+
+SP_API 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) \
+SP_API 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(); \
+} \
+SP_API 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(); \
+} \
+SP_API 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)
+
+SP_API 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");