Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to get information about the connected device?



  • I'm writing to C ++, I'm using Qt under Windows. I need to get: the name of the model, the manufacturer and the serial number of the device (video camera, photo, quadrocopter, flash drive or SD Card). Can I do this with libusb and if so how? I can not find an example of this information output.


  • Moderators



  • @raven-worx
    I got:
    8086:1e31 (bus 2, device 0)
    04a9:1795 (bus 1, device 6) path: 1.5
    09da:0080 (bus 1, device 5) path: 1.2
    8086:1e26 (bus 3, device 0)
    8087:0024 (bus 1, device 1) path: 1
    090c:1000 (bus 3, device 3) path: 1.5
    04a9:3207 (bus 1, device 4) path: 1.4
    8086:1e2d (bus 1, device 0)
    0bda:0151 (bus 1, device 2) path: 1.3
    046d:0a38 (bus 1, device 3) path: 1.1
    8087:0024 (bus 3, device 1) path: 1
    046d:c31c (bus 3, device 2) path: 1.4

    How do I get the data that is shown on the image?0_1527619956057_24a37b84-2c4a-4b51-a665-c9edf23660c4-image.png


  • Moderators

    @haifisch

    1. the example i've posted retrieves a device descriptor @ line 32
    2. this device descriptor contains descriptor indexes for retrieval
    3. call libusb_get_string_descriptor_ascii() with the desired index
    4. convert the returned data to a QString using QString::fromLatin1(data, size)


  • On Windows just use a win32 API, called as 'Setup API'. Where you can enumerate all USB devices (even any device by desired interface class UUID) and to get its info. You don't need in libusb and other external stuff.



  • @raven-worx
    I got :
    Dev (bus 2, device 0): 8086 - 1E31
    Dev (bus 1, device 4): 04A9 - 1795
    Dev (bus 1, device 3): A4Tech - USB Mouse
    Dev (bus 3, device 0): 8086 - 1E26
    Dev (bus 1, device 1): 8087 - 0024
    Dev (bus 1, device 0): 8086 - 1E2D
    Dev (bus 1, device 2): 0BDA - 0151
    Dev (bus 1, device 6): Logitech Inc. - Logitech USB Headset H340
    Dev (bus 3, device 2): 8087 - 0024
    Dev (bus 3, device 3): Logitech - USB Keyboard
    Dev (bus 3, device 1): 04A9 - 3206
    I do not see a camera or a USB flash drive here. Although they are connected. Some USB ports appear or disappear from the list when I turn them off. But their names are not displayed. Why is this happening?
    Dev (bus 2, device 0): 8086 - 1E31
    Dev (bus 1, device 5): A4Tech - USB Mouse
    Dev (bus 3, device 0): 8086 - 1E26
    Dev (bus 1, device 1): 8087 - 0024
    Dev (bus 1, device 3): 04A9 - 3207
    Dev (bus 1, device 0): 8086 - 1E2D
    Dev (bus 1, device 2): 0BDA - 0151
    Dev (bus 1, device 6): Logitech Inc. - Logitech USB Headset H340
    Dev (bus 3, device 2): 8087 - 0024
    Dev (bus 3, device 3): Logitech - USB Keyboard



  • @kuzulis A sample list, connected devices can be seen? To show the name of the model, the manufacturer and the serial number of the device.



  • @raven-worx
    My code:

    #include <stdio.h>
    #include <string.h>
    #include "libusb.h"
    
    #if defined(_MSC_VER) && (_MSC_VER < 1900)
    #define snprintf _snprintf
    #endif
    
    int verbose = 0;
    
    static void print_endpoint_comp(const struct libusb_ss_endpoint_companion_descriptor *ep_comp)
    {
        printf("      USB 3.0 Endpoint Companion:\n");
        printf("        bMaxBurst:        %d\n", ep_comp->bMaxBurst);
        printf("        bmAttributes:     0x%02x\n", ep_comp->bmAttributes);
        printf("        wBytesPerInterval: %d\n", ep_comp->wBytesPerInterval);
    }
    
    static void print_endpoint(const struct libusb_endpoint_descriptor *endpoint)
    {
        int i, ret;
    
        printf("      Endpoint:\n");
        printf("        bEndpointAddress: %02xh\n", endpoint->bEndpointAddress);
        printf("        bmAttributes:     %02xh\n", endpoint->bmAttributes);
        printf("        wMaxPacketSize:   %d\n", endpoint->wMaxPacketSize);
        printf("        bInterval:        %d\n", endpoint->bInterval);
        printf("        bRefresh:         %d\n", endpoint->bRefresh);
        printf("        bSynchAddress:    %d\n", endpoint->bSynchAddress);
    
        for (i = 0; i < endpoint->extra_length;) {
            if (LIBUSB_DT_SS_ENDPOINT_COMPANION == endpoint->extra[i + 1]) {
                struct libusb_ss_endpoint_companion_descriptor *ep_comp;
    
                ret = libusb_get_ss_endpoint_companion_descriptor(NULL, endpoint, &ep_comp);
                if (LIBUSB_SUCCESS != ret) {
                    continue;
                }
    
                print_endpoint_comp(ep_comp);
    
                libusb_free_ss_endpoint_companion_descriptor(ep_comp);
            }
    
            i += endpoint->extra[i];
        }
    }
    
    static void print_altsetting(const struct libusb_interface_descriptor *interface)
    {
        uint8_t i;
    
        printf("    Interface:\n");
        printf("      bInterfaceNumber:   %d\n", interface->bInterfaceNumber);
        printf("      bAlternateSetting:  %d\n", interface->bAlternateSetting);
        printf("      bNumEndpoints:      %d\n", interface->bNumEndpoints);
        printf("      bInterfaceClass:    %d\n", interface->bInterfaceClass);
        printf("      bInterfaceSubClass: %d\n", interface->bInterfaceSubClass);
        printf("      bInterfaceProtocol: %d\n", interface->bInterfaceProtocol);
        printf("      iInterface:         %d\n", interface->iInterface);
    
        for (i = 0; i < interface->bNumEndpoints; i++)
            print_endpoint(&interface->endpoint[i]);
    }
    
    static void print_2_0_ext_cap(struct libusb_usb_2_0_extension_descriptor *usb_2_0_ext_cap)
    {
        printf("    USB 2.0 Extension Capabilities:\n");
        printf("      bDevCapabilityType: %d\n", usb_2_0_ext_cap->bDevCapabilityType);
        printf("      bmAttributes:       0x%x\n", usb_2_0_ext_cap->bmAttributes);
    }
    
    static void print_ss_usb_cap(struct libusb_ss_usb_device_capability_descriptor *ss_usb_cap)
    {
        printf("    USB 3.0 Capabilities:\n");
        printf("      bDevCapabilityType: %d\n", ss_usb_cap->bDevCapabilityType);
        printf("      bmAttributes:       0x%x\n", ss_usb_cap->bmAttributes);
        printf("      wSpeedSupported:    0x%x\n", ss_usb_cap->wSpeedSupported);
        printf("      bFunctionalitySupport: %d\n", ss_usb_cap->bFunctionalitySupport);
        printf("      bU1devExitLat:      %d\n", ss_usb_cap->bU1DevExitLat);
        printf("      bU2devExitLat:      %d\n", ss_usb_cap->bU2DevExitLat);
    }
    
    static void print_bos(libusb_device_handle *handle)
    {
        struct libusb_bos_descriptor *bos;
        int ret;
    
        ret = libusb_get_bos_descriptor(handle, &bos);
        if (0 > ret) {
            return;
        }
    
        printf("  Binary Object Store (BOS):\n");
        printf("    wTotalLength:       %d\n", bos->wTotalLength);
        printf("    bNumDeviceCaps:     %d\n", bos->bNumDeviceCaps);
    
        if(bos->dev_capability[0]->bDevCapabilityType == LIBUSB_BT_USB_2_0_EXTENSION) {
    
            struct libusb_usb_2_0_extension_descriptor *usb_2_0_extension;
                ret =  libusb_get_usb_2_0_extension_descriptor(NULL, bos->dev_capability[0],&usb_2_0_extension);
                if (0 > ret) {
                    return;
                }
    
                    print_2_0_ext_cap(usb_2_0_extension);
                    libusb_free_usb_2_0_extension_descriptor(usb_2_0_extension);
            }
    
        if(bos->dev_capability[0]->bDevCapabilityType == LIBUSB_BT_SS_USB_DEVICE_CAPABILITY) {
    
                struct libusb_ss_usb_device_capability_descriptor *dev_cap;
            ret = libusb_get_ss_usb_device_capability_descriptor(NULL, bos->dev_capability[0],&dev_cap);
                if (0 > ret) {
                    return;
                }
    
                print_ss_usb_cap(dev_cap);
                libusb_free_ss_usb_device_capability_descriptor(dev_cap);
            }
    
        libusb_free_bos_descriptor(bos);
    }
    
    static void print_interface(const struct libusb_interface *interface)
    {
        int i;
    
        for (i = 0; i < interface->num_altsetting; i++)
            print_altsetting(&interface->altsetting[i]);
    }
    
    static void print_configuration(struct libusb_config_descriptor *config)
    {
        uint8_t i;
    
        printf("  Configuration:\n");
        printf("    wTotalLength:         %d\n", config->wTotalLength);
        printf("    bNumInterfaces:       %d\n", config->bNumInterfaces);
        printf("    bConfigurationValue:  %d\n", config->bConfigurationValue);
        printf("    iConfiguration:       %d\n", config->iConfiguration);
        printf("    bmAttributes:         %02xh\n", config->bmAttributes);
        printf("    MaxPower:             %d\n", config->MaxPower);
    
        for (i = 0; i < config->bNumInterfaces; i++)
            print_interface(&config->interface[i]);
    }
    
    static int print_device(libusb_device *dev, int level)
    {
        struct libusb_device_descriptor desc;
        libusb_device_handle *handle = NULL;
        char description[256];
        unsigned char string[256];
        int ret;
        uint8_t i;
    
        ret = libusb_get_device_descriptor(dev, &desc);
        if (ret < 0) {
            fprintf(stderr, "failed to get device descriptor");
            return -1;
        }
    
        ret = libusb_open(dev, &handle);
        if (LIBUSB_SUCCESS == ret) {
            if (desc.iManufacturer) {
                ret = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer, string, sizeof(string));
                if (ret > 0)
                    snprintf(description, sizeof(description), "%s - ", string);
                else
                    snprintf(description, sizeof(description), "%04X - ",
                    desc.idVendor);
            }
            else
                snprintf(description, sizeof(description), "%04X - ",
                desc.idVendor);
    
            if (desc.iProduct) {
                ret = libusb_get_string_descriptor_ascii(handle, desc.iProduct, string, sizeof(string));
                if (ret > 0)
                    snprintf(description + strlen(description), sizeof(description) -
                    strlen(description), "%s", string);
                else
                    snprintf(description + strlen(description), sizeof(description) -
                    strlen(description), "%04X", desc.idProduct);
            }
            else
                snprintf(description + strlen(description), sizeof(description) -
                strlen(description), "%04X", desc.idProduct);
        }
        else {
            snprintf(description, sizeof(description), "%04X - %04X",
                desc.idVendor, desc.idProduct);
        }
    
        printf("%.*sDev (bus %d, device %d): %s\n", level * 2, "                    ",
            libusb_get_bus_number(dev), libusb_get_device_address(dev), description);
    
        if (handle && verbose) {
            if (desc.iSerialNumber) {
                ret = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, string, sizeof(string));
                if (ret > 0)
                    printf("%.*s  - Serial Number: %s\n", level * 2,
                    "                    ", string);
            }
        }
    
        if (verbose) {
            for (i = 0; i < desc.bNumConfigurations; i++) {
                struct libusb_config_descriptor *config;
                ret = libusb_get_config_descriptor(dev, i, &config);
                if (LIBUSB_SUCCESS != ret) {
                    printf("  Couldn't retrieve descriptors\n");
                    continue;
                }
    
                print_configuration(config);
    
                libusb_free_config_descriptor(config);
            }
    
            if (handle && desc.bcdUSB >= 0x0201) {
                print_bos(handle);
            }
        }
    
        if (handle)
            libusb_close(handle);
    
        return 0;
    }
    
    
    
    int main(int argc, char *argv[])
    {
        libusb_device **devs;
        ssize_t cnt;
        int r, i;
    
        if (argc > 1 && !strcmp(argv[1], "-v"))
            verbose = 1;
    
        r = libusb_init(NULL);
        if (r < 0)
            return r;
    
        cnt = libusb_get_device_list(NULL, &devs);
        if (cnt < 0)
            return (int)cnt;
    
        for (i = 0; devs[i]; ++i) {
            print_device(devs[i], 0);
        }
    
        libusb_free_device_list(devs, 1);
    
        libusb_exit(NULL);
        return 0;
    
        /*
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        return a.exec();
        */
    
    }
    


  • A sample list, connected devices can be seen? To show the name of the model, the manufacturer and the serial number of the device.

    You can got same info as from "device properties" of device manager and so on.

    PS: It belongs not only to USB devices...



  • This post is deleted!


  • @kuzulis Can you provide a sample code?




Log in to reply