qsadfasdgfgads

Untitled

Nov 2nd, 2022
636
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.40 KB | None | 0 0
  1. #include <iostream>
  2. #include <libusb-1.0/libusb.h>
  3. using namespace std;
  4.  
  5. string get_type_of_device(unsigned char device_class){
  6.     switch(device_class){
  7.     case 0x1:
  8.         return "Audio";
  9.     case 0x2:
  10.         return "Communications and CDC Control";
  11.     case 0x3:
  12.         return "HID (Human Interface Device)";
  13.     case 0x5:
  14.         return "Physical";
  15.     case 0x6:
  16.         return "Image";
  17.     case 0x7:
  18.         return "Printer";
  19.     case 0x8:
  20.         return "Mass storage";
  21.     case 0x9:
  22.         return "Hub";
  23.     case 0xA:
  24.         return "CDC-Data";
  25.     case 0xB:
  26.         return "Smart Card";
  27.     case 0xD:
  28.         return "Content security";
  29.     case 0xE:
  30.         return "Video";
  31.     case 0xF:
  32.         return "Personal healthcare";
  33.     case 0x10:
  34.         return "Audio/Video device";
  35.     case 0x11:
  36.         return "Billboard device";
  37.     case 0x12:
  38.         return "USB Type-c bridge";
  39.     case 0x3C:
  40.         return "I3C devie";
  41.     case 0xDC:
  42.         return "Diagnostic device";
  43.     case 0xE0:
  44.         return "Wireless controller";
  45.     case 0xEF:
  46.         return "Miscellaneous";
  47.     case 0xFE:
  48.         return "Application specific";
  49.     case 0xFF:
  50.         return "Vendor specific";
  51.     default:
  52.         return "Unknown";
  53.     }
  54. }
  55.  
  56. void printdev(libusb_device *device){
  57.     struct libusb_device_descriptor device_descriptor{};
  58.     struct libusb_config_descriptor* config_descriptor{};
  59.  
  60.     // 0 - success, LIBUSB_ERROR - failure
  61.     int code1 = libusb_get_device_descriptor(device, &device_descriptor);
  62.     if (code1){
  63.         printf("Error, couldn't get descriptor of a device\n\n");
  64.         return;
  65.     }
  66.  
  67.     // 0 - Success, LIBUSB_ERROR_NOT_FOUND or LIBUSB_ERROR - failure
  68.     int code2 = libusb_get_config_descriptor(device, 0, &config_descriptor); // 0 - the index of the configuration to retrieve
  69.     if (code2){
  70.         printf("Error, couldn't get config descriptor\n\n");
  71.         return;
  72.     }
  73.  
  74.     struct libusb_device_handle* device_handle;
  75.  
  76.     libusb_open(device, &device_handle);
  77.  
  78.     printf("%-25s%s\n", "Type", get_type_of_device(device_descriptor.bDeviceClass).c_str());
  79.     printf("%-25s%02Xh\n", "Class", device_descriptor.bDeviceClass);
  80.     printf("%-25s%02Xh\n", "Subclass", device_descriptor.bDeviceSubClass);
  81.     printf("%-25s%02Xh\n", "Protocol", device_descriptor.bDeviceProtocol);
  82.     printf("%-25s%u\n", "Vendor ID", device_descriptor.idVendor);
  83.     printf("%-25s%u\n", "Product ID", device_descriptor.idProduct);
  84.  
  85.     unsigned char buffer1[BUFSIZ] = {'\0'};
  86.  
  87.     printf("%s", "Manufacturer   ");
  88.     if (libusb_get_string_descriptor_ascii(device_handle, device_descriptor.iManufacturer, buffer1, sizeof(buffer1))){
  89.         printf("%s", buffer1);
  90.     }
  91.     printf("\n");
  92.  
  93.     unsigned char buffer2[BUFSIZ] = {'\0'};
  94.     printf("%s", "iProduct   ");
  95.     if (libusb_get_string_descriptor_ascii(device_handle, device_descriptor.iProduct, buffer2, sizeof(buffer2))){
  96.         printf("%s", buffer2);
  97.     }
  98.     printf("\n");
  99.  
  100.  
  101.     unsigned char buffer3[BUFSIZ] = {'\0'};
  102.     printf("%s", "Serial   ");
  103.     if (libusb_get_string_descriptor_ascii(device_handle, device_descriptor.iSerialNumber, buffer3, sizeof(buffer3))){
  104.         printf("%s", buffer3);
  105.     }
  106.     printf("\n");
  107.  
  108.  
  109.     if (device_descriptor.bDeviceClass == 0){
  110.         for(unsigned int i = 0; i < (unsigned int)config_descriptor->bNumInterfaces; ++i){
  111.             const struct libusb_interface* interface = &config_descriptor->interface[i];
  112.             for(unsigned int j = 0; j < interface->num_altsetting; ++j){
  113.                 const struct libusb_interface_descriptor* interface_descriptor = &interface->altsetting[j];
  114.  
  115.                 printf("%s  %s\n", "Interface class!!", get_type_of_device(interface_descriptor->bInterfaceClass).c_str());
  116.             }
  117.         }
  118.     }
  119.  
  120.     libusb_close(device_handle);
  121.  
  122.     printf("\n\n");
  123.  
  124.     libusb_free_config_descriptor(config_descriptor);
  125. }
  126.  
  127. int main(){
  128.     // https://www.usb.org/defined-class-codes
  129.  
  130.     struct libusb_context* ctx = nullptr; // Context of session
  131.  
  132.     // Init libusb and start session
  133.     // 0 - success, LIBUSB_ERROR - failure
  134.     int init_code = libusb_init(&ctx);
  135.  
  136.     if (init_code){
  137.         printf("Error, initialization didn't succeed\n");
  138.         return 1;
  139.     }
  140.  
  141.     struct libusb_device** devices; // all found devices
  142.  
  143.     libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_WARNING); // Sets level of log messages
  144.  
  145.     unsigned int devices_counter = libusb_get_device_list(ctx, &devices);
  146.  
  147.     if(devices_counter <= 0){
  148.         printf("Error, couldn't get devices list\n");
  149.         return 1;
  150.     }
  151.  
  152.     printf("Found devices: %u\n\n", devices_counter);
  153.  
  154.     for(unsigned int i = 0; i < devices_counter; ++i){
  155.         printdev(devices[i]);
  156.     }
  157.  
  158.     libusb_free_device_list(devices, 1); // 1 - whether to unref the devices in the list
  159.  
  160.     libusb_exit(ctx);
  161.  
  162.     return 0;
  163. }
  164.  
Advertisement
Add Comment
Please, Sign In to add comment