]> sigrok.org Git - libserialport.git/blobdiff - serialport.c
posix: Consistent debug output when blocking operations time out.
[libserialport.git] / serialport.c
index 8d71865bad14ce5dc55d36f44d83570fb08c8d8a..c77682f98566fcf903e8aa76689949cdc4b8523c 100644 (file)
@@ -763,7 +763,8 @@ SP_API enum sp_return sp_blocking_write(struct sp_port *port, const void *buf,
                RETURN_INT(count);
        } else if (GetLastError() == ERROR_IO_PENDING) {
                DEBUG("Waiting for write to complete");
-               GetOverlappedResult(port->hdl, &port->write_ovl, &bytes_written, TRUE);
+               if (GetOverlappedResult(port->hdl, &port->write_ovl, &bytes_written, TRUE) == 0)
+                       RETURN_FAIL("GetOverlappedResult() failed");
                DEBUG_FMT("Write completed, %d/%d bytes written", bytes_written, count);
                RETURN_INT(bytes_written);
        } else {
@@ -786,17 +787,17 @@ SP_API enum sp_return sp_blocking_write(struct sp_port *port, const void *buf,
                timeradd(&start, &delta, &end);
        }
 
+       FD_ZERO(&fds);
+       FD_SET(port->fd, &fds);
+
        /* Loop until we have written the requested number of bytes. */
        while (bytes_written < count) {
                /* Wait until space is available. */
-               FD_ZERO(&fds);
-               FD_SET(port->fd, &fds);
                if (timeout_ms) {
                        gettimeofday(&now, NULL);
-                       if (timercmp(&now, &end, >)) {
-                               DEBUG("Write timed out");
-                               RETURN_INT(bytes_written);
-                       }
+                       if (timercmp(&now, &end, >))
+                               /* Timeout has expired. */
+                               break;
                        timersub(&end, &now, &delta);
                }
                result = select(port->fd + 1, NULL, &fds, NULL, timeout_ms ? &delta : NULL);
@@ -808,8 +809,8 @@ SP_API enum sp_return sp_blocking_write(struct sp_port *port, const void *buf,
                                RETURN_FAIL("select() failed");
                        }
                } else if (result == 0) {
-                       DEBUG("Write timed out");
-                       RETURN_INT(bytes_written);
+                       /* Timeout has expired. */
+                       break;
                }
 
                /* Do write. */
@@ -828,6 +829,9 @@ SP_API enum sp_return sp_blocking_write(struct sp_port *port, const void *buf,
                ptr += result;
        }
 
+       if (bytes_written < count)
+               DEBUG("Write timed out");
+
        RETURN_INT(bytes_written);
 #endif
 }
@@ -973,7 +977,8 @@ SP_API enum sp_return sp_blocking_read(struct sp_port *port, void *buf,
                bytes_read = count;
        } else if (GetLastError() == ERROR_IO_PENDING) {
                DEBUG("Waiting for read to complete");
-               GetOverlappedResult(port->hdl, &port->read_ovl, &bytes_read, TRUE);
+               if (GetOverlappedResult(port->hdl, &port->read_ovl, &bytes_read, TRUE) == 0)
+                       RETURN_FAIL("GetOverlappedResult() failed");
                DEBUG_FMT("Read completed, %d/%d bytes read", bytes_read, count);
        } else {
                RETURN_FAIL("ReadFile() failed");
@@ -1000,16 +1005,17 @@ SP_API enum sp_return sp_blocking_read(struct sp_port *port, void *buf,
                timeradd(&start, &delta, &end);
        }
 
+       FD_ZERO(&fds);
+       FD_SET(port->fd, &fds);
+
        /* Loop until we have the requested number of bytes. */
        while (bytes_read < count) {
                /* Wait until data is available. */
-               FD_ZERO(&fds);
-               FD_SET(port->fd, &fds);
                if (timeout_ms) {
                        gettimeofday(&now, NULL);
                        if (timercmp(&now, &end, >))
                                /* Timeout has expired. */
-                               RETURN_INT(bytes_read);
+                               break;
                        timersub(&end, &now, &delta);
                }
                result = select(port->fd + 1, &fds, NULL, NULL, timeout_ms ? &delta : NULL);
@@ -1021,8 +1027,8 @@ SP_API enum sp_return sp_blocking_read(struct sp_port *port, void *buf,
                                RETURN_FAIL("select() failed");
                        }
                } else if (result == 0) {
-                       DEBUG("Read timed out");
-                       RETURN_INT(bytes_read);
+                       /* Timeout has expired. */
+                       break;
                }
 
                /* Do read. */
@@ -1041,6 +1047,9 @@ SP_API enum sp_return sp_blocking_read(struct sp_port *port, void *buf,
                ptr += result;
        }
 
+       if (bytes_read < count)
+               DEBUG("Read timed out");
+
        RETURN_INT(bytes_read);
 #endif
 }