visomat-data-downloader: add an option to enable libusb debug output
authorAntonio Ospite <ao2@ao2.it>
Sun, 10 Jan 2016 09:03:55 +0000 (10:03 +0100)
committerAntonio Ospite <ao2@ao2.it>
Sun, 10 Jan 2016 09:13:04 +0000 (10:13 +0100)
src/Makefile
src/visomat-data-downloader.c

index 4ac4019..79105ea 100644 (file)
@@ -36,6 +36,9 @@ ifeq ($(DEBUG),1)
   CFLAGS += -DDEBUG
 endif
 
   CFLAGS += -DDEBUG
 endif
 
+# for getopt
+CFLAGS += -D_POSIX_C_SOURCE=2
+
 CFLAGS += $(shell pkg-config --cflags libusb-1.0)
 LDLIBS := $(shell pkg-config --libs libusb-1.0)
 
 CFLAGS += $(shell pkg-config --cflags libusb-1.0)
 LDLIBS := $(shell pkg-config --libs libusb-1.0)
 
index 69f62d6..2ea275a 100644 (file)
@@ -20,6 +20,7 @@
  */
 
 #include <stdio.h>
  */
 
 #include <stdio.h>
+#include <stdbool.h>
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
@@ -355,12 +356,38 @@ static int visomat_get_datetime(visomat_device *dev)
        return 0;
 }
 
        return 0;
 }
 
-int main(void)
+static void usage(const char *name)
+{
+       printf("usage: %s [OPTIONS]\n\n", name);
+       printf("OPTIONS:\n");
+       printf("\t-D\t\tenable libusb debug output\n");
+       printf("\t-h\t\tthis help message\n");
+}
+
+int main(int argc, char *argv[])
 {
        int ret;
 {
        int ret;
+       int opt;
+       bool enable_libusb_debug = false;
        libusb_device_handle *dev;
        int current_configuration;
 
        libusb_device_handle *dev;
        int current_configuration;
 
+       while ((opt = getopt(argc, argv, "Dh")) != -1) {
+               switch (opt) {
+               case 'D':
+                       enable_libusb_debug = true;
+                       break;
+               case 'h':
+                       usage(argv[0]);
+                       ret = 0;
+                       goto out;
+               default: /* '?' */
+                       usage(argv[0]);
+                       ret = -EINVAL;
+                       goto out;
+               }
+       }
+
        ret = libusb_init(NULL);
        if (ret < 0) {
                fprintf(stderr, "libusb_init failed: %s\n",
        ret = libusb_init(NULL);
        if (ret < 0) {
                fprintf(stderr, "libusb_init failed: %s\n",
@@ -368,7 +395,8 @@ int main(void)
                goto out;
        }
 
                goto out;
        }
 
-       libusb_set_debug(NULL, LIBUSB_LOG_LEVEL_INFO);
+       libusb_set_debug(NULL, enable_libusb_debug ?
+                        LIBUSB_LOG_LEVEL_DEBUG : LIBUSB_LOG_LEVEL_INFO);
 
        dev = libusb_open_device_with_vid_pid(NULL,
                                              VISOMAT_DEVICE_VID,
 
        dev = libusb_open_device_with_vid_pid(NULL,
                                              VISOMAT_DEVICE_VID,