libusb检测U盘插入

摘自https://www.cnblogs.com/zzxap/archive/2011/05/18/2175687.html

Oops, my fault, hic. I should run as root and I'll get what I need. :P

Thanks,
    Have a nice day!

http://libusb.sourceforge.net/doc/examples-code.html

http://fedoraforum.org/forum/showthread.php?t=178455

 

  1 //libusb检测U盘插入
  2 
  3 /* testlibusb.c from LQ*/
  4 
  5 #include <stdio.h>
  6 #include <string.h>
  7 #include <usb.h>
  8 
  9 
 10 void print_endpoint(struct usb_endpoint_descriptor *endpoint)
 11 {
 12     printf(" bEndpointAddress: %02xh\n", endpoint->bEndpointAddress);
 13     printf(" bmAttributes: %02xh\n", endpoint->bmAttributes);
 14     printf(" wMaxPacketSize: %d\n", endpoint->wMaxPacketSize);
 15     printf(" bInterval: %d\n", endpoint->bInterval);
 16     printf(" bRefresh: %d\n", endpoint->bRefresh);
 17     printf(" bSynchAddress: %d\n", endpoint->bSynchAddress);
 18 }
 19 
 20 
 21 void print_altsetting(struct usb_interface_descriptor *interface)
 22 {
 23     int i;
 24 
 25     printf(" bInterfaceNumber: %d\n", interface->bInterfaceNumber);
 26     printf(" bAlternateSetting: %d\n", interface->bAlternateSetting);
 27     printf(" bNumEndpoints: %d\n", interface->bNumEndpoints);
 28     printf(" bInterfaceClass: %d\n", interface->bInterfaceClass);
 29     printf(" bInterfaceSubClass: %d\n", interface->bInterfaceSubClass);
 30     printf(" bInterfaceProtocol: %d\n", interface->bInterfaceProtocol);
 31     printf(" iInterface: %d\n", interface->iInterface);
 32 
 33     for (i = 0; i < interface->bNumEndpoints; i++)
 34         print_endpoint(&interface->endpoint[i]);
 35 }
 36 
 37 
 38 void print_interface(struct usb_interface *interface)
 39 {
 40     int i;
 41 
 42     for (i = 0; i < interface->num_altsetting; i++)
 43         print_altsetting(&interface->altsetting[i]);
 44 }
 45 
 46 
 47 void print_configuration(struct usb_config_descriptor *config)
 48 {
 49     int i;
 50 
 51     printf(" wTotalLength: %d\n", config->wTotalLength);
 52     printf(" bNumInterfaces: %d\n", config->bNumInterfaces);
 53     printf(" bConfigurationValue: %d\n", config->bConfigurationValue);
 54     printf(" iConfiguration: %d\n", config->iConfiguration);
 55     printf(" bmAttributes: %02xh\n", config->bmAttributes);
 56     printf(" MaxPower: %d\n", config->MaxPower);
 57 
 58     for (i = 0; i < config->bNumInterfaces; i++)
 59         print_interface(&config->interface[i]);
 60 }
 61 
 62 
 63 int main(void)
 64 {
 65     struct usb_bus *bus;
 66     struct usb_device *dev;
 67 
 68     usb_init();
 69     usb_find_busses();
 70     usb_find_devices();
 71 
 72     printf("bus/device idVendor/idProduct\n");
 73 
 74     for (bus = usb_busses; bus; bus = bus->next) {
 75         for (dev = bus->devices; dev; dev = dev->next) {
 76             int ret, i;
 77             char string[256];
 78             usb_dev_handle *udev;
 79 
 80             printf("%s/%s %04X/%04X\n", bus->dirname, dev->filename,
 81                     dev->descriptor.idVendor, dev->descriptor.idProduct);
 82 
 83             udev = usb_open(dev);
 84             if (udev) {
 85                 if (dev->descriptor.iManufacturer) {
 86                     ret = usb_get_string_simple(udev, dev->descriptor.iManufacturer, string, sizeof(string));
 87                     if (ret > 0)
 88                         printf("- Manufacturer : %s\n", string);
 89                     else
 90                         printf("- Unable to fetch manufacturer string\n");
 91                 }
 92 
 93                 if (dev->descriptor.iProduct) {
 94                     ret = usb_get_string_simple(udev, dev->descriptor.iProduct, string, sizeof(string));
 95                     if (ret > 0)
 96                         printf("- Product : %s\n", string);
 97                     else
 98                         printf("- Unable to fetch product string\n");
 99                 }
100 
101                 if (dev->descriptor.iSerialNumber) {
102                     ret = usb_get_string_simple(udev, dev->descriptor.iSerialNumber, string, sizeof(string));
103                     if (ret > 0)
104                         printf("- Serial Number: %s\n", string);
105                     else
106                         printf("- Unable to fetch serial number string\n");
107                 }
108 
109                 usb_close (udev);
110             }
111 
112             if (!dev->config) {
113                 printf(" Couldn't retrieve descriptors\n");
114                 continue;
115             }
116 
117             for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
118                 print_configuration(&dev->config[i]);
119         }
120     }
121 
122     return 0;
123 }

 

posted @ 2020-02-21 11:47  LiuYanYGZ  阅读(907)  评论(0编辑  收藏  举报