[update] submit_wait timeout
authorFabien Le Mentec <texane@gmail.com>
Sun, 11 Sep 2011 05:58:24 +0000 (00:58 -0500)
committerFabien Le Mentec <texane@gmail.com>
Sun, 11 Sep 2011 05:58:24 +0000 (00:58 -0500)
stm32l/src/main.c

index 7deb3284c64e50e90fd282f8238e34b443b8d4fc..1e9ded1182a47ac60c71fadc9c34ee6e2b83755a 100644 (file)
@@ -2,6 +2,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <stdint.h>
+#include <time.h>
 #include <sys/types.h>
 #include <libusb-1.0/libusb.h>
 #include "stlink-hw.h"
@@ -106,6 +107,9 @@ static void on_trans_done(struct libusb_transfer* trans)
 
 static int submit_wait(struct libusb_transfer* trans)
 {
+  struct timeval start;
+  struct timeval now;
+  struct timeval diff;
   struct trans_ctx trans_ctx;
   enum libusb_error error;
 
@@ -121,6 +125,8 @@ static int submit_wait(struct libusb_transfer* trans)
     return -1;
   }
 
+  gettimeofday(&start, NULL);
+
   while (!(trans_ctx.flags & TRANS_FLAGS_IS_DONE))
   {
     if (libusb_handle_events(NULL))
@@ -128,6 +134,14 @@ static int submit_wait(struct libusb_transfer* trans)
       printf("libusb_handle_events()\n");
       return -1;
     }
+
+    gettimeofday(&now, NULL);
+    timersub(&now, &start, &diff);
+    if (diff.tv_sec >= 3)
+    {
+      printf("libusb_handle_events() timeout\n");
+      return -1;
+    }
   }
 
   return 0;