Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for (socat) pseudo serial ports #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion libserialport.h
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,9 @@ enum sp_transport {
/** USB serial port adapter. @since 0.1.1 */
SP_TRANSPORT_USB,
/** Bluetooth serial port adapter. @since 0.1.1 */
SP_TRANSPORT_BLUETOOTH
SP_TRANSPORT_BLUETOOTH,
/** Pseudo serial port. @since 0.1.2 */
SP_TRANSPORT_PSEUDO
};

/**
Expand Down
28 changes: 16 additions & 12 deletions linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,22 @@ SP_PRIV enum sp_return get_port_details(struct sp_port *port)
RETURN_ERROR(SP_ERR_ARG, "Device name not recognized");

snprintf(link_name, sizeof(link_name), "/sys/class/tty/%s", dev);
if (lstat(link_name, &statbuf) == -1)
RETURN_ERROR(SP_ERR_ARG, "Device not found");
if (!S_ISLNK(statbuf.st_mode))
snprintf(link_name, sizeof(link_name), "/sys/class/tty/%s/device", dev);
count = readlink(link_name, file_name, sizeof(file_name));
if (count <= 0 || count >= (int)(sizeof(file_name) - 1))
RETURN_ERROR(SP_ERR_ARG, "Device not found");
file_name[count] = 0;
if (strstr(file_name, "bluetooth"))
port->transport = SP_TRANSPORT_BLUETOOTH;
else if (strstr(file_name, "usb"))
port->transport = SP_TRANSPORT_USB;
if (lstat(link_name, &statbuf) == -1) {
if (strncmp(port->name + 5, "pts/", 4))
RETURN_ERROR(SP_ERR_ARG, "Device not found");
port->transport = SP_TRANSPORT_PSEUDO;
} else {
if (!S_ISLNK(statbuf.st_mode))
snprintf(link_name, sizeof(link_name), "/sys/class/tty/%s/device", dev);
count = readlink(link_name, file_name, sizeof(file_name));
if (count <= 0 || count >= (int)(sizeof(file_name) - 1))
RETURN_ERROR(SP_ERR_ARG, "Device not found");
file_name[count] = 0;
if (strstr(file_name, "bluetooth"))
port->transport = SP_TRANSPORT_BLUETOOTH;
else if (strstr(file_name, "usb"))
port->transport = SP_TRANSPORT_USB;
}

if (port->transport == SP_TRANSPORT_USB) {
for (i = 0; i < 5; i++) {
Expand Down
37 changes: 24 additions & 13 deletions serialport.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ static const struct std_baudrate std_baudrates[] = {

void (*sp_debug_handler)(const char *format, ...) = sp_default_debug_handler;

static void init_config(struct sp_port_config *config);

static enum sp_return get_config(struct sp_port *port, struct port_data *data,
struct sp_port_config *config);

Expand Down Expand Up @@ -1652,6 +1654,18 @@ static enum sp_return set_flow(int fd, struct port_data *data)
}
#endif /* USE_TERMIOX */

static void init_config(struct sp_port_config *config)
{
config->baudrate = -1;
config->bits = -1;
config->parity = -1;
config->stopbits = -1;
config->rts = -1;
config->cts = -1;
config->dtr = -1;
config->dsr = -1;
}

static enum sp_return get_config(struct sp_port *port, struct port_data *data,
struct sp_port_config *config)
{
Expand All @@ -1661,6 +1675,8 @@ static enum sp_return get_config(struct sp_port *port, struct port_data *data,

DEBUG_FMT("Getting configuration for port %s", port->name);

init_config(config);

#ifdef _WIN32
if (!GetCommState(port->hdl, &data->dcb))
RETURN_FAIL("GetCommState() failed");
Expand Down Expand Up @@ -1758,7 +1774,8 @@ static enum sp_return get_config(struct sp_port *port, struct port_data *data,
if (tcgetattr(port->fd, &data->term) < 0)
RETURN_FAIL("tcgetattr() failed");

if (ioctl(port->fd, TIOCMGET, &data->controlbits) < 0)
if (port->transport != SP_TRANSPORT_PSEUDO && ioctl(port->fd, TIOCMGET,
&data->controlbits) < 0)
RETURN_FAIL("TIOCMGET ioctl failed");

#ifdef USE_TERMIOX
Expand Down Expand Up @@ -1824,7 +1841,7 @@ static enum sp_return get_config(struct sp_port *port, struct port_data *data,
if (data->term.c_cflag & CRTSCTS) {
config->rts = SP_RTS_FLOW_CONTROL;
config->cts = SP_CTS_FLOW_CONTROL;
} else {
} else if (port->transport != SP_TRANSPORT_PSEUDO) {
if (data->termiox_supported && data->rts_flow)
config->rts = SP_RTS_FLOW_CONTROL;
else
Expand All @@ -1836,11 +1853,12 @@ static enum sp_return get_config(struct sp_port *port, struct port_data *data,

if (data->termiox_supported && data->dtr_flow)
config->dtr = SP_DTR_FLOW_CONTROL;
else
else if (port->transport != SP_TRANSPORT_PSEUDO)
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 (port->transport != SP_TRANSPORT_PSEUDO)
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)
Expand Down Expand Up @@ -2272,14 +2290,7 @@ SP_API enum sp_return sp_new_config(struct sp_port_config **config_ptr)
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;
init_config(config);

*config_ptr = config;

Expand Down