Advertisement
uaa

getddesc.c : test code of GET_DESCRIPTOR(DEVICE)

uaa
Jun 13th, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. // cc getddesc.c -Wall -O2 -lusb
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <usb.h>
  5.  
  6. static void dump(unsigned char *buf, int len)
  7. {
  8. int i;
  9.  
  10. for (i = 0; i < len; i++)
  11. printf("%02x ", buf[i]);
  12.  
  13. printf("\n");
  14. }
  15.  
  16. static void do_test(usb_dev_handle *dh, int i)
  17. {
  18. unsigned char buf[256];
  19. int err;
  20.  
  21. memset(buf, 0, sizeof(buf));
  22. err = usb_control_msg(dh, 0x80, 0x06, 0x0100, 0x0000, (char *)buf, i, 5000);
  23. printf("%d: ", i);
  24. dump(buf, err);
  25. }
  26.  
  27. static void do_action(struct usb_device *dev)
  28. {
  29. usb_dev_handle *dh;
  30. int i;
  31.  
  32. /* open device */
  33. dh = usb_open(dev);
  34. if (dh == NULL) {
  35. printf("usb_open NULL\n");
  36. goto fin0;
  37. }
  38.  
  39. /* test */
  40. for (i = 0x20; i >= 0; i--)
  41. do_test(dh, i);
  42.  
  43. //fin1:
  44. usb_close(dh);
  45. fin0:
  46. return;
  47. }
  48.  
  49. int main(int argc, char *argv[])
  50. {
  51. struct usb_bus *bus;
  52. struct usb_device *dev;
  53. int found, bnum, dnum, vid, pid;
  54.  
  55. if (argc < 3) {
  56. printf("%s [idVendor] [idProduct]\n", argv[0]);
  57. goto fin0;
  58. }
  59.  
  60. vid = strtol(argv[1], NULL, 0);
  61. pid = strtol(argv[2], NULL, 0);
  62.  
  63. /* initialize libusb */
  64. usb_init();
  65. usb_find_busses();
  66. usb_find_devices();
  67.  
  68. /* no device as default */
  69. found = 0;
  70.  
  71. /* find device */
  72. for (bus = usb_get_busses(), bnum = 0; bus != NULL;
  73. bus = bus->next, bnum++) {
  74. for (dev = bus->devices, dnum = 0; dev != NULL;
  75. dev = dev->next, dnum++) {
  76. if (dev->descriptor.idVendor == vid &&
  77. dev->descriptor.idProduct == pid) {
  78. found = 1;
  79. goto found;
  80. }
  81. }
  82. }
  83. found:
  84. /* do action when device is found */
  85. if (found) {
  86. printf("device at %d:%d\n", bnum, dnum);
  87. do_action(dev);
  88. } else {
  89. printf("device not found\n");
  90. }
  91. fin0:
  92. return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement