visomat-data-downloader: add some debug facilities
authorAntonio Ospite <ao2@ao2.it>
Sat, 9 Jan 2016 18:20:11 +0000 (19:20 +0100)
committerAntonio Ospite <ao2@ao2.it>
Sat, 9 Jan 2016 18:20:11 +0000 (19:20 +0100)
src/Makefile
src/visomat-data-downloader.c

index c39f955..4ac4019 100644 (file)
@@ -32,6 +32,10 @@ ifneq ($(CC),clang)
   CFLAGS += -Wunsafe-loop-optimizations -Wunused-but-set-variable
 endif
 
+ifeq ($(DEBUG),1)
+  CFLAGS += -DDEBUG
+endif
+
 CFLAGS += $(shell pkg-config --cflags libusb-1.0)
 LDLIBS := $(shell pkg-config --libs libusb-1.0)
 
index a53f72c..69f62d6 100644 (file)
 #include <unistd.h>
 #include <libusb.h>
 
+#ifdef DEBUG
+#define debug(...) fprintf(stderr, __VA_ARGS__)
+static void debug_dump_buffer(const char *filename, uint8_t *buffer, unsigned int len)
+{
+       FILE *dump_file;
+
+       dump_file = fopen(filename, "wb");
+       if (dump_file == NULL) {
+               fprintf(stderr, "Failed to open %s: %s\n", filename, strerror(errno));
+               return;
+       }
+
+       fwrite(buffer, 1, len, dump_file);
+       fclose(dump_file);
+}
+#else
+#define debug(...) do {} while(0)
+static void debug_dump_buffer(const char *filename, uint8_t *buffer, unsigned int len)
+{
+       (void)filename;
+       (void)buffer;
+       (void)len;
+}
+#endif
+
 #define VISOMAT_DEVICE_VID    0x1247
 #define VISOMAT_DEVICE_PID    0x00f8
 #define VISOMAT_CONFIGURATION 1
@@ -300,6 +325,9 @@ static int visomat_dump_eeprom(visomat_device *dev, unsigned int user_mask)
        if (ret < 0)
                return ret;
 
+       debug("buffer len: %d\n", ret);
+       debug_dump_buffer("eeprom.bin", buffer, sizeof(buffer));
+
        ret = decode_eeprom(buffer, ret, user_mask);
        if (ret < 0)
                return ret;