From 786c620a9d67d1fd4312524b43c49a76becaf36d Mon Sep 17 00:00:00 2001 From: Timo Kokkonen Date: Sat, 30 Apr 2011 17:28:57 +0300 Subject: [PATCH] If no suitable devices are found, wait until one is found This makes it more convenient to use this tool as it doesn't immediately bail out if it can't find a device to talk to. Signed-off-by: Timo Kokkonen --- hiddev.c | 2 +- main.c | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/hiddev.c b/hiddev.c index 6d3d5d3..81e2954 100644 --- a/hiddev.c +++ b/hiddev.c @@ -170,7 +170,7 @@ int hiddev_open_by_id(int vendor_id, int product_id, int *usage_code) dir = opendir(HIDDEV_PATH); if (dir == NULL) { error = errno; - printf("Failed to open directory %s: %s\n", HIDDEV_PATH, + trace(4, "Failed to open directory %s: %s\n", HIDDEV_PATH, strerror(error)); return -error; } diff --git a/main.c b/main.c index a97f6fa..bfa1545 100644 --- a/main.c +++ b/main.c @@ -282,6 +282,27 @@ int communicate(int fd, int uc) return 0; } +int wait_for_device(int vendor, int product, int *usage_code) +{ + int fd; + + fd = hiddev_open_by_id(vendor, product, usage_code); + + if (fd > 0) + return fd; + + trace(0, + "No suitable device found. Please plug in your glucose meter\n"); + do { + usleep(500 * 1000); + fd = hiddev_open_by_id(vendor, product, usage_code); + } while(fd < 0); + + usleep(2000 * 1000); + + return fd; +} + int main(int argc, char *argv[]) { int fd, usage_code; @@ -291,7 +312,7 @@ int main(int argc, char *argv[]) trace_level = opts.trace_level; if (opts.usbdev == NULL) - fd = hiddev_open_by_id(CONTOUR_USB_VENDOR_ID, + fd = wait_for_device(CONTOUR_USB_VENDOR_ID, CONTOUR_USB_PRODUCT_ID, &usage_code); else fd = hiddev_open(opts.usbdev, &usage_code); -- 2.44.0