Add sp_set_flowcontrol helper function.

This commit is contained in:
Martin Ling 2013-11-14 22:43:03 +00:00
parent 824dcb4561
commit 18fc2dd12b
2 changed files with 55 additions and 11 deletions

View File

@ -132,6 +132,14 @@ enum {
SP_XONXOFF_INOUT = 3
};
/* Standard flow control combinations. */
enum {
SP_FLOWCONTROL_NONE = 0,
SP_FLOWCONTROL_XONXOFF = 1,
SP_FLOWCONTROL_RTSCTS = 2,
SP_FLOWCONTROL_DTRDSR = 3
};
/* Enumeration */
int sp_get_port_by_name(const char *portname, struct sp_port **port_ptr);
void sp_free_port(struct sp_port *port);
@ -148,12 +156,15 @@ int sp_read(struct sp_port *port, void *buf, size_t count);
int sp_write(struct sp_port *port, const void *buf, size_t count);
int sp_flush(struct sp_port *port);
/* Port configuration */
/* Basic port configuration */
int sp_set_config(struct sp_port *port, struct sp_port_config *config);
int sp_set_baudrate(struct sp_port *port, int baudrate);
int sp_set_bits(struct sp_port *port, int bits);
int sp_set_parity(struct sp_port *port, int parity);
int sp_set_stopbits(struct sp_port *port, int stopbits);
int sp_set_flowcontrol(struct sp_port *port, int flowcontrol);
/* Advanced port configuration */
int sp_set_rts(struct sp_port *port, int rts);
int sp_set_cts(struct sp_port *port, int cts);
int sp_set_dtr(struct sp_port *port, int dtr);

View File

@ -939,22 +939,55 @@ static int apply_config(struct sp_port *port, struct sp_port_data *data)
}
#define TRY(x) do { int ret = x; if (ret != SP_OK) return ret; } while (0)
#define TRY_SET(x) do { if (config->x >= 0) TRY(set_##x(&data, config->x)); } while (0)
#define TRY_SET(x, y) do { if (y >= 0) TRY(set_##x(&data, y)); } while (0)
#define TRY_SET_CONFIG(x) TRY_SET(x, config->x)
int sp_set_config(struct sp_port *port, struct sp_port_config *config)
{
struct sp_port_data data;
TRY(start_config(port, &data));
TRY_SET(baudrate);
TRY_SET(bits);
TRY_SET(parity);
TRY_SET(stopbits);
TRY_SET(rts);
TRY_SET(cts);
TRY_SET(dtr);
TRY_SET(dsr);
TRY_SET(xon_xoff);
TRY_SET_CONFIG(baudrate);
TRY_SET_CONFIG(bits);
TRY_SET_CONFIG(parity);
TRY_SET_CONFIG(stopbits);
TRY_SET_CONFIG(rts);
TRY_SET_CONFIG(cts);
TRY_SET_CONFIG(dtr);
TRY_SET_CONFIG(dsr);
TRY_SET_CONFIG(xon_xoff);
TRY(apply_config(port, &data));
return SP_OK;
}
int sp_set_flowcontrol(struct sp_port *port, int flowcontrol)
{
struct sp_port_data data;
TRY(start_config(port, &data));
if (flowcontrol == SP_FLOWCONTROL_XONXOFF)
TRY_SET(xon_xoff, SP_XONXOFF_INOUT);
else
TRY_SET(xon_xoff, SP_XONXOFF_DISABLED);
if (flowcontrol == SP_FLOWCONTROL_RTSCTS) {
TRY_SET(rts, SP_RTS_FLOW_CONTROL);
TRY_SET(cts, SP_CTS_FLOW_CONTROL);
} else {
TRY_SET(rts, SP_RTS_ON);
TRY_SET(cts, SP_CTS_IGNORE);
}
if (flowcontrol == SP_FLOWCONTROL_DTRDSR) {
TRY_SET(dtr, SP_DTR_FLOW_CONTROL);
TRY_SET(dsr, SP_DSR_FLOW_CONTROL);
} else {
TRY_SET(dtr, SP_DTR_ON);
TRY_SET(dsr, SP_DSR_IGNORE);
}
TRY(apply_config(port, &data));
return SP_OK;