X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;f=serialport.c;h=1b5a95e21d87fdfe48c7c10d686a47e2acac0e24;hb=HEAD;hp=eee453a276155d667a1a32894c62788891bc42aa;hpb=bf40b1cea9eb2e13d13f05118fe11b43be621485;p=libserialport.git diff --git a/serialport.c b/serialport.c index eee453a..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); @@ -1307,6 +1347,11 @@ SP_API enum sp_return sp_output_waiting(struct sp_port *port) { TRACE("%p", port); +#ifdef __CYGWIN__ + /* TIOCOUTQ is not defined in Cygwin headers */ + RETURN_ERROR(SP_ERR_SUPP, + "Getting output bytes waiting is not supported on Cygwin"); +#else CHECK_OPEN_PORT(); DEBUG_FMT("Checking output bytes waiting on port %s", port->name); @@ -1324,6 +1369,7 @@ SP_API enum sp_return sp_output_waiting(struct sp_port *port) RETURN_FAIL("TIOCOUTQ ioctl failed"); RETURN_INT(bytes_waiting); #endif +#endif } SP_API enum sp_return sp_new_event_set(struct sp_event_set **result_ptr) @@ -1442,6 +1488,7 @@ SP_API enum sp_return sp_wait(struct sp_event_set *event_set, RETURN_OK(); #else struct timeout timeout; + int poll_timeout; int result; struct pollfd *pollfds; unsigned int i; @@ -1472,7 +1519,11 @@ SP_API enum sp_return sp_wait(struct sp_event_set *event_set, break; } - result = poll(pollfds, event_set->count, timeout_remaining_ms(&timeout) || -1); + poll_timeout = (int) timeout_remaining_ms(&timeout); + if (poll_timeout == 0) + poll_timeout = -1; + + result = poll(pollfds, event_set->count, poll_timeout); timeout_update(&timeout); @@ -1637,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: @@ -1838,6 +1886,7 @@ static enum sp_return set_config(struct sp_port *port, struct port_data *data, DEBUG_FMT("Setting configuration for port %s", port->name); #ifdef _WIN32 + BYTE* new_buf; TRY(await_write_completion(port)); @@ -1854,11 +1903,10 @@ static enum sp_return set_config(struct sp_port *port, struct port_data *data, /* Allocate write buffer for 50ms of data at baud rate. */ port->write_buf_size = max(config->baudrate / (8 * 20), 1); - port->write_buf = realloc(port->write_buf, - port->write_buf_size); - - if (!port->write_buf) + new_buf = realloc(port->write_buf, port->write_buf_size); + if (!new_buf) RETURN_ERROR(SP_ERR_MEM, "Allocating write buffer failed"); + port->write_buf = new_buf; } if (config->bits >= 0)