From b1fc2852e06c6e179dd57f830ed871f1cb27d991 Mon Sep 17 00:00:00 2001 From: Omer Kilic Date: Tue, 19 Jun 2018 16:44:52 +0100 Subject: [PATCH] add json output mode: "--json". --- .../enumerate_devices/enumerate_devices.c | 74 ++++++++++++++++++- 1 file changed, 72 insertions(+), 2 deletions(-) diff --git a/src/libhs/examples/enumerate_devices/enumerate_devices.c b/src/libhs/examples/enumerate_devices/enumerate_devices.c index 408296f0..c025eaba 100644 --- a/src/libhs/examples/enumerate_devices/enumerate_devices.c +++ b/src/libhs/examples/enumerate_devices/enumerate_devices.c @@ -10,11 +10,15 @@ #include #include +#include +#include /* For single-file use you need a tiny bit more than that, see libhs.h for more information. */ #include "../../libhs.h" +bool json_first_object = true; + static int device_callback(hs_device *dev, void *udata) { (void)(udata); @@ -23,27 +27,93 @@ static int device_callback(hs_device *dev, void *udata) dev->location, dev->iface_number, dev->vid, dev->pid, hs_device_type_strings[dev->type]); printf(" - device node: %s\n", dev->path); + if (dev->manufacturer_string) + { printf(" - manufacturer: %s\n", dev->manufacturer_string); + } + if (dev->product_string) + { printf(" - product: %s\n", dev->product_string); + } + if (dev->serial_number_string) + { printf(" - serial number: %s\n", dev->serial_number_string); + } /* If you return a non-zero value, the enumeration is aborted and this value is returned from the calling function. */ return 0; } -int main(void) +static int device_callback_json(hs_device *dev, void *udata) +{ + (void)(udata); + + if (json_first_object) + { + json_first_object = false; /* not the first object any more */ + printf("{\n"); + } + else + { + printf(",\n{\n"); + } + + printf(" \"location\": \"%s@%" PRIu8 + "\",\n \"vid\": \"%04" PRIx16 + "\",\n \"pid\": \"%04" PRIx16 + "\",\n \"type\": \"%s\"", + dev->location, dev->iface_number, dev->vid, dev->pid, + hs_device_type_strings[dev->type]); + + printf(",\n \"node\": \"%s\"", dev->path); + + if (dev->manufacturer_string) + { + printf(",\n \"manufacturer\": \"%s\"", dev->manufacturer_string); + } + + if (dev->product_string) + { + printf(",\n \"product\": \"%s\"", dev->product_string); + } + + if (dev->serial_number_string) + { + printf(",\n \"serial\": \"%s\"", dev->serial_number_string); + } + + printf("\n}"); + + /* If you return a non-zero value, the enumeration is aborted and this value is returned + from the calling function. */ + return 0; +} + +int main(int argc, char *argv[]) { int r; /* Go through the device tree and call our callback for each device. The callback can abort the enumeration by returning a non-zero value. */ - r = hs_enumerate(NULL, 0, device_callback, NULL); + if (argc > 1 && (strcmp(argv[1], "--json") == 0)) + { + printf("[\n"); + r = hs_enumerate(NULL, 0, device_callback_json, NULL); + printf("\n]\n"); + } + else + { + r = hs_enumerate(NULL, 0, device_callback, NULL); + } + if (r < 0) + { return -r; + } return 0; }