Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I need to list existing USB hub and the devices connected in the hub using my C++ program.

I can able to print the USB Hub and devices connected in hub from terminal using the commands

lsusb
lsusb -v 

I want to use that feature in my C++ program.

How I can do this programmatically. Is there any C++ classes available to use in my Qt application.

share|improve this question

closed as off topic by Gilles, Michael Mrozek Jan 17 '13 at 0:23

Questions on Unix & Linux Stack Exchange are expected to relate to Unix or Linux within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

    
use libusb, search here: there was a similar question recently. Look here. –  Eddy_Em Jan 16 '13 at 11:45
    
In archlinux this is in libusb-compat package. –  Eddy_Em Jan 17 '13 at 7:41

2 Answers 2

up vote 2 down vote accepted

Here the simplest analogue of lsusb:

#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);
        }
}

Compile with -lusb

share|improve this answer
    
I tried it but did not worked to me. I am using Ubuntu OS and qt creator and I try after installing libusb-1.0-0-dev, libusb++-0.1-4c2 etc. where this usb.h header file come from –  Arun Kumar K S Jan 17 '13 at 7:13
    
Finally i installed its libary file.Now I can include #include <usb.h> But where its library file is located? –  Arun Kumar K S Jan 17 '13 at 11:39
    
Thanks Compile with -lusb its working –  Arun Kumar K S Jan 17 '13 at 12:02

This is Fedora, for Debian based distributions you should be able to do something similar:

  • Get the package containing that program: "rpm -qif /usr/bin/lsusb" tells you it comes from the usbutils package, homepage is http://www.linux-usb.org
  • Look at the sources posted there (presumably in C, it should be easy to extract what you need from there)

Alternatively:

  • Find out where/how the kernel publishes the information on the USB setup, i.e. /sys/bus/usb/devices shows the whole setup
  • Read up on how that information is organized (this is set up for easy parsing, so it shouldn't be too hard to handle)

A desperate solution:

  • Just run the lsusb(8) program itself and get the required data from its output

BTW, consider using Python instead of C++ to write your GUI application. GUIs rarely are performance critical enough, and the savings in programmer time more than offset any efficiency losses (and the user probably won't notice them anyway).

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.