]> sigrok.org Git - libserialport.git/blobdiff - serialport.c
Windows nonblocking write: Dont't return if async I/O finishes immediately.
[libserialport.git] / serialport.c
index e2cf48abd33df6785a6930c39b5f4742f7cce2fa..712b951daad2b765e3b661a6b506bbedd0aec54a 100644 (file)
 #include <sys/syslimits.h>
 #endif
 #ifdef __linux__
+#ifdef HAVE_LIBUDEV
 #include "libudev.h"
+#endif
+#ifndef __ANDROID__
 #include "linux/serial.h"
+#endif
 #include "linux_termios.h"
+
+/* TCGETX/TCSETX is not available everywhere. */
 #if defined(TCGETX) && defined(TCSETX) && defined(HAVE_TERMIOX)
 #define USE_TERMIOX
 #endif
 #endif
 
+/* TIOCINQ/TIOCOUTQ is not available everywhere. */
+#if !defined(TIOCINQ) && defined(FIONREAD)
+#define TIOCINQ FIONREAD
+#endif
+#if !defined(TIOCOUTQ) && defined(FIONWRITE)
+#define TIOCOUTQ FIONWRITE
+#endif
+
 #ifndef _WIN32
 #include "linux_termios.h"
 #endif
@@ -159,7 +173,11 @@ void (*sp_debug_handler)(const char *format, ...) = sp_default_debug_handler;
 #define RETURN_OK() RETURN_CODE(SP_OK);
 #define RETURN_ERROR(err, msg) do { DEBUG_ERROR(err, msg); return err; } while (0)
 #define RETURN_FAIL(msg) do { DEBUG_FAIL(msg); return SP_ERR_FAIL; } while (0)
-#define RETURN_VALUE(fmt, x) do { DEBUG("%s returning " fmt, __func__, x); return x; } while (0)
+#define RETURN_VALUE(fmt, x) do { \
+       typeof(x) _x = x; \
+       DEBUG("%s returning " fmt, __func__, _x); \
+       return _x; \
+} while (0)
 #define SET_ERROR(val, err, msg) do { DEBUG_ERROR(err, msg); val = err; } while (0)
 #define SET_FAIL(val, msg) do { DEBUG_FAIL(msg); val = SP_ERR_FAIL; } while (0)
 #define TRACE(fmt, ...) DEBUG("%s(" fmt ") called", __func__, ##__VA_ARGS__)
@@ -450,7 +468,7 @@ out_release:
        IOObjectRelease(iter);
 out_done:
 #endif
-#ifdef __linux__
+#if defined(__linux__) && defined(HAVE_LIBUDEV)
        struct udev *ud;
        struct udev_enumerate *ud_enumerate;
        struct udev_list_entry *ud_list;
@@ -799,13 +817,28 @@ enum sp_return sp_drain(struct sp_port *port)
        /* Returns non-zero upon success, 0 upon failure. */
        if (FlushFileBuffers(port->hdl) == 0)
                RETURN_FAIL("FlushFileBuffers() failed");
+       RETURN_OK();
 #else
-       /* Returns 0 upon success, -1 upon failure. */
-       if (tcdrain(port->fd) < 0)
-               RETURN_FAIL("tcdrain() failed");
+       int result;
+       while (1) {
+#ifdef __ANDROID__
+               int arg = 1;
+               result = ioctl(port->fd, TCSBRK, &arg);
+#else
+               result = tcdrain(port->fd);
+#endif
+               if (result < 0) {
+                       if (errno == EINTR) {
+                               DEBUG("tcdrain() was interrupted");
+                               continue;
+                       } else {
+                               RETURN_FAIL("tcdrain() failed");
+                       }
+               } else {
+                       RETURN_OK();
+               }
+       }
 #endif
-
-       RETURN_OK();
 }
 
 enum sp_return sp_blocking_write(struct sp_port *port, const void *buf, size_t count, unsigned int timeout)
@@ -870,7 +903,7 @@ enum sp_return sp_blocking_write(struct sp_port *port, const void *buf, size_t c
                gettimeofday(&start, NULL);
                /* Define duration of timeout. */
                delta.tv_sec = timeout / 1000;
-               delta.tv_usec = timeout % 1000;
+               delta.tv_usec = (timeout % 1000) * 1000;
                /* Calculate time at which we should give up. */
                timeradd(&start, &delta, &end);
        }
@@ -890,9 +923,14 @@ enum sp_return sp_blocking_write(struct sp_port *port, const void *buf, size_t c
                        timersub(&end, &now, &delta);
                }
                result = select(port->fd + 1, NULL, &fds, NULL, timeout ? &delta : NULL);
-               if (result < 0)
-                       RETURN_FAIL("select() failed");
-               if (result == 0) {
+               if (result < 0) {
+                       if (errno == EINTR) {
+                               DEBUG("select() call was interrupted, repeating");
+                               continue;
+                       } else {
+                               RETURN_FAIL("select() failed");
+                       }
+               } else if (result == 0) {
                        DEBUG("write timed out");
                        RETURN_VALUE("%d", bytes_written);
                }
@@ -962,9 +1000,16 @@ enum sp_return sp_nonblocking_write(struct sp_port *port, const void *buf, size_
                /* Start asynchronous write. */
                if (WriteFile(port->hdl, &port->pending_byte, 1, NULL, &port->write_ovl) == 0) {
                        if (GetLastError() == ERROR_IO_PENDING) {
-                               DEBUG("Asynchronous write started");
-                               port->writing = 1;
-                               RETURN_VALUE("%d", ++written);
+                               if (HasOverlappedIoCompleted(&port->write_ovl)) {
+                                       DEBUG("Asynchronous write completed immediately");
+                                       port->writing = 0;
+                                       written++;
+                                       continue;
+                               } else {
+                                       DEBUG("Asynchronous write running");
+                                       port->writing = 1;
+                                       RETURN_VALUE("%d", ++written);
+                               }
                        } else {
                                /* Actual failure of some kind. */
                                RETURN_FAIL("WriteFile() failed");
@@ -1041,7 +1086,7 @@ enum sp_return sp_blocking_read(struct sp_port *port, void *buf, size_t count, u
                gettimeofday(&start, NULL);
                /* Define duration of timeout. */
                delta.tv_sec = timeout / 1000;
-               delta.tv_usec = timeout % 1000;
+               delta.tv_usec = (timeout % 1000) * 1000;
                /* Calculate time at which we should give up. */
                timeradd(&start, &delta, &end);
        }
@@ -1060,9 +1105,14 @@ enum sp_return sp_blocking_read(struct sp_port *port, void *buf, size_t count, u
                        timersub(&end, &now, &delta);
                }
                result = select(port->fd + 1, &fds, NULL, NULL, timeout ? &delta : NULL);
-               if (result < 0)
-                       RETURN_FAIL("select() failed");
-               if (result == 0) {
+               if (result < 0) {
+                       if (errno == EINTR) {
+                               DEBUG("select() call was interrupted, repeating");
+                               continue;
+                       } else {
+                               RETURN_FAIL("select() failed");
+                       }
+               } else if (result == 0) {
                        DEBUG("read timed out");
                        RETURN_VALUE("%d", bytes_read);
                }
@@ -1112,7 +1162,8 @@ enum sp_return sp_nonblocking_read(struct sp_port *port, void *buf, size_t count
                RETURN_FAIL("ReadFile() failed");
 
        /* Get number of bytes read. */
-       GetOverlappedResult(port->hdl, &port->read_ovl, &bytes_read, TRUE);
+       if (GetOverlappedResult(port->hdl, &port->read_ovl, &bytes_read, TRUE) == 0)
+               RETURN_FAIL("GetOverlappedResult() failed");
 
        RETURN_VALUE("%d", bytes_read);
 #else
@@ -1131,6 +1182,52 @@ enum sp_return sp_nonblocking_read(struct sp_port *port, void *buf, size_t count
 #endif
 }
 
+enum sp_return sp_input_waiting(struct sp_port *port)
+{
+       TRACE("%p", port);
+
+       CHECK_OPEN_PORT();
+
+       DEBUG("Checking input bytes waiting on port %s", port->name);
+
+#ifdef _WIN32
+       DWORD errors;
+       COMSTAT comstat;
+
+       if (ClearCommError(port->hdl, &errors, &comstat) == 0)
+               RETURN_FAIL("ClearComError() failed");
+       RETURN_VALUE("%d", comstat.cbInQue);
+#else
+       int bytes_waiting;
+       if (ioctl(port->fd, TIOCINQ, &bytes_waiting) < 0)
+               RETURN_FAIL("TIOCINQ ioctl failed");
+       RETURN_VALUE("%d", bytes_waiting);
+#endif
+}
+
+enum sp_return sp_output_waiting(struct sp_port *port)
+{
+       TRACE("%p", port);
+
+       CHECK_OPEN_PORT();
+
+       DEBUG("Checking output bytes waiting on port %s", port->name);
+
+#ifdef _WIN32
+       DWORD errors;
+       COMSTAT comstat;
+
+       if (ClearCommError(port->hdl, &errors, &comstat) == 0)
+               RETURN_FAIL("ClearComError() failed");
+       RETURN_VALUE("%d", comstat.cbOutQue);
+#else
+       int bytes_waiting;
+       if (ioctl(port->fd, TIOCOUTQ, &bytes_waiting) < 0)
+               RETURN_FAIL("TIOCOUTQ ioctl failed");
+       RETURN_VALUE("%d", bytes_waiting);
+#endif
+}
+
 #ifdef __linux__
 static enum sp_return get_baudrate(int fd, int *baudrate)
 {