Fatih Fatih
Fatih Fatih

Reputation: 13

Get device name from usb

lsusb command

i want to get device name like on lsusb. I found this code and i tried its all descriptor parameters.Is there any way to get device name like on picture like Log. Opt. Gam. Mouse

#include <stdio.h>
#include <usb.h>
main(){
    struct usb_bus *bus;
    struct usb_device *dev;
    usb_init();
    usb_find_busses();
    usb_find_devices();
    for (bus = usb_busses; bus; bus = bus->next)
        for (dev = bus->devices; dev; dev = dev->next){
            printf("Trying device %s/%s\n", bus->dirname, dev->filename);
            printf("\tID_VENDOR = 0x%04x\n", dev->descriptor.idVendor);
            printf("\tID_PRODUCT = 0x%04x\n", dev->descriptor.idProduct);
        }
}

Upvotes: 1

Views: 2839

Answers (1)

Devidas
Devidas

Reputation: 2517

the poiter for you is look into libusb library.

starting with libusb_get_device_list which Returns a list of USB devices currently attached to the system. https://libusb.sourceforge.io/api-1.0/group__libusb__dev.html

you can take it from there.

if you want other way reading /sys/bus/usb/devices directory and read valid devices. except root hub or other hubs.

EDIT1:

updated link

here is usage

libusb_device **list;
ssize_t cnt = libusb_get_device_list(NULL, &list);
ssize_t i = 0;
if (cnt < 0)
    handle_error(); //handle error and return
 
for (i = 0; i < cnt; i++) {
    libusb_device *device = list[i];
    // do your work
}
 
libusb_free_device_list(list, 1);

Upvotes: 1

Related Questions