+ if (config->dtr >= 0 || config->dsr >= 0) {
+ if (data->termiox_supported) {
+ data->flow &= ~(DTR_FLOW | DSR_FLOW);
+ 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->flow |= DTR_FLOW;
+ break;
+ default:
+ break;
+ }
+ if (config->dsr == SP_DSR_FLOW_CONTROL)
+ data->flow |= DSR_FLOW;
+ } 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, TCSADRAIN, &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__)
+ if (baud_nonstd)
+ TRY(set_baudrate(port->fd, config->baudrate));
+#ifdef USE_TERMIOX
+ if (data->termiox_supported)
+ TRY(set_flow(port->fd, data->flow));
+#endif
+#endif
+
+#endif /* !_WIN32 */
+
+ RETURN_OK();
+}
+
+enum sp_return sp_new_config(struct sp_port_config **config_ptr)
+{
+ TRACE("%p", config_ptr);
+ struct sp_port_config *config;
+
+ 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", config); \
+ 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();