Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add json output mode to enumerate_devices #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 72 additions & 2 deletions src/libhs/examples/enumerate_devices/enumerate_devices.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@

#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>

/* 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);
Expand All @@ -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;
}