zoukankan      html  css  js  c++  java
  • 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
    ", endpoint->bEndpointAddress);
     13     printf(" bmAttributes: %02xh
    ", endpoint->bmAttributes);
     14     printf(" wMaxPacketSize: %d
    ", endpoint->wMaxPacketSize);
     15     printf(" bInterval: %d
    ", endpoint->bInterval);
     16     printf(" bRefresh: %d
    ", endpoint->bRefresh);
     17     printf(" bSynchAddress: %d
    ", endpoint->bSynchAddress);
     18 }
     19 
     20 
     21 void print_altsetting(struct usb_interface_descriptor *interface)
     22 {
     23     int i;
     24 
     25     printf(" bInterfaceNumber: %d
    ", interface->bInterfaceNumber);
     26     printf(" bAlternateSetting: %d
    ", interface->bAlternateSetting);
     27     printf(" bNumEndpoints: %d
    ", interface->bNumEndpoints);
     28     printf(" bInterfaceClass: %d
    ", interface->bInterfaceClass);
     29     printf(" bInterfaceSubClass: %d
    ", interface->bInterfaceSubClass);
     30     printf(" bInterfaceProtocol: %d
    ", interface->bInterfaceProtocol);
     31     printf(" iInterface: %d
    ", 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
    ", config->wTotalLength);
     52     printf(" bNumInterfaces: %d
    ", config->bNumInterfaces);
     53     printf(" bConfigurationValue: %d
    ", config->bConfigurationValue);
     54     printf(" iConfiguration: %d
    ", config->iConfiguration);
     55     printf(" bmAttributes: %02xh
    ", config->bmAttributes);
     56     printf(" MaxPower: %d
    ", 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
    ");
     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
    ", 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
    ", string);
     89                     else
     90                         printf("- Unable to fetch manufacturer string
    ");
     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
    ", string);
     97                     else
     98                         printf("- Unable to fetch product string
    ");
     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
    ", string);
    105                     else
    106                         printf("- Unable to fetch serial number string
    ");
    107                 }
    108 
    109                 usb_close (udev);
    110             }
    111 
    112             if (!dev->config) {
    113                 printf(" Couldn't retrieve descriptors
    ");
    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 }
  • 相关阅读:
    从虚拟地址,到物理地址(开PAE)
    无LoadLibrary获取指定模块基址
    练习
    Centos安装Python3及设置对应版本pip
    Varnish安装使用(初学)
    luogu P2463 [SDOI2008]Sandy的卡片 |二分+hash
    luogu P2852 [USACO06DEC]牛奶模式Milk Patterns |二分+hash
    luogu P4051 [JSOI2007]字符加密 |后缀数组(SA)
    弦图 学习笔记&
    luogu P1600 天天爱跑步 |树上差分+LCA
  • 原文地址:https://www.cnblogs.com/LiuYanYGZ/p/12340770.html
Copyright © 2011-2022 走看看