Debian Codegraph @

3rdparty/hidapi/hidparser/testparse.c

raw symbols SCIP
1 #include "hidapi.h"
2 #include "hidparse.h"
3
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <string.h>
7
8 const char padding[] = " "; // sixteen spaces
9
10
11
12
13 void print_report_descriptor(FILE* fp, int depth, hid_item* item)
14 {
15 if (item->collection) {
16 hid_item* c = item->collection;
17 for (; c != NULL; c = c->next) {
18 print_report_descriptor(fp, depth + 1, c);
19 }
20 } else {
21 // it's a leaf
22 fprintf(fp, "%sitem type %02x usage %08x bits %2d, report %02x\n", padding + (16 - depth),
23 item->type, item->usage, item->report_size, item->report_id);
24 }
25 }
26
27 int main(int argc, char* argv[])
28 {
29 #define MAX_STR 255
30 wchar_t wstr[MAX_STR];
31 int res;
32
33 if (hid_init())
34 return EXIT_FAILURE;
35
36 if (argc == 1) {
37 struct hid_device_info *devs, *cur_dev;
38 devs = hid_enumerate(0x0, 0x0);
39 cur_dev = devs;
40 while (cur_dev) {
41 printf("Device Found\n type: %04hx %04hx\n path: %s\n serial_number: %ls", cur_dev->vendor_id, cur_dev->product_id, cur_dev->path, cur_dev->serial_number);
42 printf("\n");
43 printf(" Manufacturer: %ls\n", cur_dev->manufacturer_string);
44 printf(" Product: %ls\n", cur_dev->product_string);
45 printf(" Release: %hx\n", cur_dev->release_number);
46 printf(" Interface: %d\n", cur_dev->interface_number);
47 printf("\n");
48 cur_dev = cur_dev->next;
49 }
50 hid_free_enumeration(devs);
51 return EXIT_SUCCESS;
52 }
53
54 // assume argv[1] is path
55 hid_device *handle;
56 handle = hid_open_path(argv[1]);
57 if (!handle) {
58 fprintf(stderr, "unable to open device with path %s\n", argv[1]);
59 return EXIT_FAILURE;
60 }
61
62 wstr[0] = 0x0000;
63 res = hid_get_product_string(handle, wstr, MAX_STR);
64 if (res < 0)
65 fprintf(stderr, "Unable to read product string\n");
66 printf("Product String: %ls\n", wstr);
67
68 unsigned char descriptorBuffer[8192];
69 int descriptorBytes = hid_get_descriptor(handle, descriptorBuffer, sizeof(descriptorBuffer));
70 if (descriptorBytes < 0) {
71 fprintf(stderr, "failed to read report descriptor");
72 return EXIT_FAILURE;
73 }
74
75 hid_item* rootItem;
76 res = hid_parse_reportdesc(descriptorBuffer, descriptorBytes, &rootItem);
77 if (res != 0) {
78 fprintf(stderr, "failure parsing report");
79 return EXIT_FAILURE;
80 }
81
82 print_report_descriptor(stdout, 0, rootItem);
83
84
85 hid_free_reportdesc(rootItem);
86
87 return EXIT_SUCCESS;
88 }