Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // cc getddesc.c -Wall -O2 -lusb
- #include <stdio.h>
- #include <string.h>
- #include <usb.h>
- static void dump(unsigned char *buf, int len)
- {
- int i;
- for (i = 0; i < len; i++)
- printf("%02x ", buf[i]);
- printf("\n");
- }
- static void do_test(usb_dev_handle *dh, int i)
- {
- unsigned char buf[256];
- int err;
- memset(buf, 0, sizeof(buf));
- err = usb_control_msg(dh, 0x80, 0x06, 0x0100, 0x0000, (char *)buf, i, 5000);
- printf("%d: ", i);
- dump(buf, err);
- }
- static void do_action(struct usb_device *dev)
- {
- usb_dev_handle *dh;
- int i;
- /* open device */
- dh = usb_open(dev);
- if (dh == NULL) {
- printf("usb_open NULL\n");
- goto fin0;
- }
- /* test */
- for (i = 0x20; i >= 0; i--)
- do_test(dh, i);
- //fin1:
- usb_close(dh);
- fin0:
- return;
- }
- int main(int argc, char *argv[])
- {
- struct usb_bus *bus;
- struct usb_device *dev;
- int found, bnum, dnum, vid, pid;
- if (argc < 3) {
- printf("%s [idVendor] [idProduct]\n", argv[0]);
- goto fin0;
- }
- vid = strtol(argv[1], NULL, 0);
- pid = strtol(argv[2], NULL, 0);
- /* initialize libusb */
- usb_init();
- usb_find_busses();
- usb_find_devices();
- /* no device as default */
- found = 0;
- /* find device */
- for (bus = usb_get_busses(), bnum = 0; bus != NULL;
- bus = bus->next, bnum++) {
- for (dev = bus->devices, dnum = 0; dev != NULL;
- dev = dev->next, dnum++) {
- if (dev->descriptor.idVendor == vid &&
- dev->descriptor.idProduct == pid) {
- found = 1;
- goto found;
- }
- }
- }
- found:
- /* do action when device is found */
- if (found) {
- printf("device at %d:%d\n", bnum, dnum);
- do_action(dev);
- } else {
- printf("device not found\n");
- }
- fin0:
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement