core: Fix return value of usbi_clock_gettime()
authorChris Dickens <christopher.a.dickens@gmail.com>
Mon, 16 Mar 2020 08:06:27 +0000 (01:06 -0700)
committerChris Dickens <christopher.a.dickens@gmail.com>
Mon, 16 Mar 2020 08:06:27 +0000 (01:06 -0700)
In most cases, usbi_clock_gettime() will map to the standard library's
clock_gettime() function. The semantics of this function are that it
returns -1 upon failure with the error code available in the errno
variable. The backends that need to implement this function should
follow the same semantics, and the return value of usbi_clock_gettime()
should not be directly propagated upwards.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
libusb/io.c
libusb/os/darwin_usb.c
libusb/os/windows_common.c
libusb/version_nano.h

index 09ccc83..b2208e8 100644 (file)
@@ -1214,7 +1214,7 @@ static int calculate_timeout(struct usbi_transfer *itransfer)
        if (r < 0) {
                usbi_err(ITRANSFER_CTX(itransfer),
                        "failed to read monotonic clock, errno=%d", errno);
-               return r;
+               return LIBUSB_ERROR_OTHER;
        }
 
        itransfer->timeout.tv_sec += timeout / 1000U;
@@ -2027,8 +2027,10 @@ static int handle_timeouts_locked(struct libusb_context *ctx)
 
        /* get current time */
        r = usbi_clock_gettime(USBI_CLOCK_MONOTONIC, &systime);
-       if (r < 0)
-               return r;
+       if (r < 0) {
+               usbi_err(ctx, "failed to read monotonic clock, errno=%d", errno);
+               return LIBUSB_ERROR_OTHER;
+       }
 
        /* iterate through flying transfers list, finding all transfers that
         * have expired timeouts */
index e0a364e..f8da747 100644 (file)
@@ -2182,7 +2182,8 @@ int usbi_clock_gettime(int clk_id, struct timespec *tp) {
     clock_ref = clock_monotonic;
     break;
   default:
-    return LIBUSB_ERROR_INVALID_PARAM;
+    errno = EINVAL;
+    return -1;
   }
 
   clock_get_time (clock_ref, &sys_time);
@@ -2190,7 +2191,7 @@ int usbi_clock_gettime(int clk_id, struct timespec *tp) {
   tp->tv_sec  = sys_time.tv_sec;
   tp->tv_nsec = sys_time.tv_nsec;
 
-  return LIBUSB_SUCCESS;
+  return 0;
 }
 #endif
 
index e37a514..debff74 100644 (file)
@@ -24,6 +24,7 @@
 
 #include <config.h>
 
+#include <errno.h>
 #include <inttypes.h>
 #include <process.h>
 #include <stdio.h>
@@ -829,13 +830,15 @@ int usbi_clock_gettime(int clk_id, struct timespec *tp)
                        QueryPerformanceCounter(&hires_counter);
                        tp->tv_sec = (long)(hires_counter.QuadPart / hires_frequency);
                        tp->tv_nsec = (long)(((hires_counter.QuadPart % hires_frequency) * hires_ticks_to_ps) / UINT64_C(1000));
-                       return LIBUSB_SUCCESS;
+                       return 0;
                }
                // Fall through and return real-time if monotonic was not detected @ timer init
        case USBI_CLOCK_REALTIME:
 #if defined(_MSC_VER) && (_MSC_VER >= 1900)
-               if (!timespec_get(tp, TIME_UTC))
-                       return LIBUSB_ERROR_OTHER;
+               if (!timespec_get(tp, TIME_UTC)) {
+                       errno = EIO;
+                       return -1;
+               }
 #else
                // We follow http://msdn.microsoft.com/en-us/library/ms724928%28VS.85%29.aspx
                // with a predef epoch time to have an epoch that starts at 1970.01.01 00:00
@@ -848,9 +851,10 @@ int usbi_clock_gettime(int clk_id, struct timespec *tp)
                tp->tv_sec = (long)(rtime.QuadPart / 10000000);
                tp->tv_nsec = (long)((rtime.QuadPart % 10000000) * 100);
 #endif
-               return LIBUSB_SUCCESS;
+               return 0;
        default:
-               return LIBUSB_ERROR_INVALID_PARAM;
+               errno = EINVAL;
+               return -1;
        }
 }
 #endif
index a744f2f..626497e 100644 (file)
@@ -1 +1 @@
-#define LIBUSB_NANO 11465
+#define LIBUSB_NANO 11466