Attachments you submit will be routed for moderation. If you have an account, please log in first.

Ticket #43: getdevserial.c

File getdevserial.c, 1.5 KB (added by hannibal, 20 months ago)

Program to fetch 0 stringID

Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include <libusb-1.0/libusb.h>
4
5libusb_device **list = NULL;
6libusb_device_handle *handle = NULL;
7ssize_t count;
8
9void onexit(void) {
10        if (handle) {
11                libusb_close(handle);
12                handle = NULL;
13        }
14        if (list) {
15                libusb_free_device_list(list, 1);
16                list = NULL;
17        }
18        libusb_exit(NULL);
19}
20
21int main(int argc, char *argv[]) {
22        int err, len = 0;
23        libusb_device **list = NULL;
24        struct libusb_device_descriptor dev;
25        ssize_t i;
26        unsigned char buffer[256];
27        if (argc < 3) {
28                printf("Usage: %s VENDOR_ID PRODUCT_ID\n", argv[0]);
29                return -1;
30        }
31        if ((err = libusb_init(NULL)) != 0) {
32                printf("Init error\n");
33                return err;
34        }
35        atexit(onexit);
36        if ((count = libusb_get_device_list(NULL, &list)) < 0) {
37                printf("No dev list\n");
38                return err;
39        }
40        for(i = 0; i < count; i++) {
41                libusb_get_device_descriptor(list[i], &dev);
42                if ((strtol(argv[1], NULL, 0) == dev.idVendor) && (strtol(argv[2], NULL, 0) == dev.idProduct)) {
43                        if ((err = libusb_open(list[i], &handle)) < 0) {
44                                printf("Dev open error\n");
45                                return err;
46                        }
47                        if ((err = libusb_set_configuration(handle, 1)) < 0) {
48                                printf("Set config error\n");
49//                              return err;
50                        }
51                        len = libusb_get_string_descriptor_ascii(handle, 0, buffer, 255);
52                        buffer[255] = '\0';
53                        printf("ASCII Length: %3i; Text: %s\n", len, buffer);
54                        len = libusb_get_descriptor(handle, LIBUSB_DT_STRING, 0, buffer, 255);
55                        buffer[255] = '\0';
56                        printf("RAW   Length: %3i; Text: %s\n", len, buffer);
57                        return 0;
58                }
59        }
60        printf("Device not found\n");
61        return -1;
62}