+ RETURN_OK();
+}
+
+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();
+}
+
+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, %p", config, x); \
+ 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();