]> sigrok.org Git - libserialport.git/blobdiff - serialport.c
change type of result variables to ssize_t
[libserialport.git] / serialport.c
index eda2d19250fdcc7316b9f1f60ffa21ff4d37655f..1b5a95e21d87fdfe48c7c10d686a47e2acac0e24 100644 (file)
@@ -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);
 
@@ -1448,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;
@@ -1478,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);
 
@@ -1643,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: