X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=serialport.c;h=1b5a95e21d87fdfe48c7c10d686a47e2acac0e24;hb=HEAD;hp=72010913ae4e7a2cc4772112afc857350e7a37f0;hpb=d81a4dfdc6f9779c13c6dd10b342e9d4c5a7ff97;p=libserialport.git diff --git a/serialport.c b/serialport.c index 7201091..1b5a95e 100644 --- a/serialport.c +++ b/serialport.c @@ -155,10 +155,7 @@ SP_API enum sp_transport sp_get_port_transport(const struct sp_port *port) { TRACE("%p", port); - if (!port) - RETURN_ERROR(SP_ERR_ARG, "Null port"); - - RETURN_INT(port->transport); + RETURN_INT(port ? port->transport : SP_TRANSPORT_NATIVE); } SP_API enum sp_return sp_get_port_usb_bus_address(const struct sp_port *port, @@ -551,6 +548,39 @@ SP_API enum sp_return sp_open(struct sp_port *port, enum sp_mode flags) if ((port->fd = open(port->name, flags_local)) < 0) RETURN_FAIL("open() failed"); + + /* + * On POSIX in the default case the file descriptor of a serial port + * is not opened exclusively. Therefore the settings of a port are + * overwritten if the serial port is opened a second time. Windows + * opens all serial ports exclusively. + * So the idea is to open the serial ports alike in the exclusive mode. + * + * ioctl(*, TIOCEXCL) defines the file descriptor as exclusive. So all + * further open calls on the serial port will fail. + * + * There is a race condition if two processes open the same serial + * port. None of the processes will notice the exclusive ownership of + * the other process because ioctl() doesn't return an error code if + * the file descriptor is already marked as exclusive. + * This can be solved with flock(). It returns an error if the file + * descriptor is already locked by another process. + */ +#ifdef HAVE_FLOCK + if (flock(port->fd, LOCK_EX | LOCK_NB) < 0) + RETURN_FAIL("flock() failed"); +#endif + +#ifdef TIOCEXCL + /* + * Before Linux 3.8 ioctl(*, TIOCEXCL) was not implemented and could + * lead to EINVAL or ENOTTY. + * These errors aren't fatal and can be ignored. + */ + if (ioctl(port->fd, TIOCEXCL) < 0 && errno != EINVAL && errno != ENOTTY) + RETURN_FAIL("ioctl() failed"); +#endif + #endif ret = get_config(port, &data, &config); @@ -560,6 +590,15 @@ SP_API enum sp_return sp_open(struct sp_port *port, enum sp_mode flags) RETURN_CODEVAL(ret); } + /* + * Assume a default baudrate if the OS does not provide one. + * Cannot assign -1 here since Windows holds the baudrate in + * the DCB and does not configure the rate individually. + */ + if (config.baudrate == 0) { + config.baudrate = 9600; + } + /* Set sane port settings. */ #ifdef _WIN32 data.dcb.fBinary = TRUE; @@ -603,7 +642,8 @@ SP_API enum sp_return sp_open(struct sp_port *port, enum sp_mode flags) data.term.c_cc[VTIME] = 0; /* Ignore modem status lines; enable receiver; leave control lines alone on close. */ - data.term.c_cflag |= (CLOCAL | CREAD | HUPCL); + data.term.c_cflag |= (CLOCAL | CREAD); + data.term.c_cflag &= ~(HUPCL); #endif #ifdef _WIN32 @@ -846,7 +886,7 @@ SP_API enum sp_return sp_blocking_write(struct sp_port *port, const void *buf, unsigned char *ptr = (unsigned char *) buf; struct timeout timeout; fd_set fds; - int result; + ssize_t result; timeout_start(&timeout, timeout_ms); @@ -1051,7 +1091,7 @@ SP_API enum sp_return sp_blocking_read(struct sp_port *port, void *buf, unsigned char *ptr = (unsigned char *) buf; struct timeout timeout; fd_set fds; - int result; + ssize_t result; timeout_start(&timeout, timeout_ms); @@ -1174,7 +1214,7 @@ SP_API enum sp_return sp_blocking_read_next(struct sp_port *port, void *buf, size_t bytes_read = 0; struct timeout timeout; fd_set fds; - int result; + ssize_t result; timeout_start(&timeout, timeout_ms); @@ -1648,28 +1688,25 @@ static enum sp_return get_config(struct sp_port *port, struct port_data *data, config->bits = data->dcb.ByteSize; - if (data->dcb.fParity) - switch (data->dcb.Parity) { - case NOPARITY: - config->parity = SP_PARITY_NONE; - break; - case ODDPARITY: - config->parity = SP_PARITY_ODD; - break; - case EVENPARITY: - config->parity = SP_PARITY_EVEN; - break; - case MARKPARITY: - config->parity = SP_PARITY_MARK; - break; - case SPACEPARITY: - config->parity = SP_PARITY_SPACE; - break; - default: - config->parity = -1; - } - else + switch (data->dcb.Parity) { + case NOPARITY: config->parity = SP_PARITY_NONE; + break; + case ODDPARITY: + config->parity = SP_PARITY_ODD; + break; + case EVENPARITY: + config->parity = SP_PARITY_EVEN; + break; + case MARKPARITY: + config->parity = SP_PARITY_MARK; + break; + case SPACEPARITY: + config->parity = SP_PARITY_SPACE; + break; + default: + config->parity = -1; + } switch (data->dcb.StopBits) { case ONESTOPBIT: