#include <iostream>#include <libusb.h>usingnamespace std;void printdev(libusb_device *dev);//prototype of the functionint main(){
libusb_device **devs;//pointer to pointer of device, used to retrieve a list of devices
libusb_context *ctx =NULL;//a libusb sessionint r;//for return values
ssize_t cnt;//holding number of devices in list
r = libusb_init(&ctx);//initialize a library sessionif(r <0){cout<<"Init Error "<<r<<endl;//there was an errorreturn1;}
libusb_set_debug(ctx, 3);//set verbosity level to 3, as suggested in the documentation
cnt = libusb_get_device_list(ctx, &devs);//get the list of devicesif(cnt <0){cout<<"Get Device Error"<<endl;//there was an error}cout<<cnt<<" Devices in list."<<endl;//print total number of usb device
ssize_t i;//for iterating through the listfor(i =0; i < cnt; i++){
printdev(devs[i]);//print specs of this device}
libusb_free_device_list(devs, 1);//free the list, unref the devices in it
libusb_exit(ctx);//close the sessionreturn0;}void printdev(libusb_device *dev){
libusb_device_descriptor desc;int r = libusb_get_device_descriptor(dev, &desc);if(r <0){cout<<"failed to get device descriptor"<<endl;return;}cout<<"Number of possible configurations: "<<(int)desc.bNumConfigurations<<" ";cout<<"Device Class: "<<(int)desc.bDeviceClass<<" ";cout<<"VendorID: "<<desc.idVendor<<" ";cout<<"ProductID: "<<desc.idProduct<<endl;
libusb_config_descriptor *config;
libusb_get_config_descriptor(dev, 0, &config);cout<<"Interfaces: "<<(int)config->bNumInterfaces<<" ||| ";const libusb_interface *inter;const libusb_interface_descriptor *interdesc;const libusb_endpoint_descriptor *epdesc;for(int i=0; i<(int)config->bNumInterfaces; i++){
inter =&config->interface[i];cout<<"Number of alternate settings: "<<inter->num_altsetting<<" | ";for(int j=0; j<inter->num_altsetting; j++){
interdesc =&inter->altsetting[j];cout<<"Interface Number: "<<(int)interdesc->bInterfaceNumber<<" | ";cout<<"Number of endpoints: "<<(int)interdesc->bNumEndpoints<<" | ";for(int k=0; k<(int)interdesc->bNumEndpoints; k++){
epdesc =&interdesc->endpoint[k];cout<<"Descriptor Type: "<<(int)epdesc->bDescriptorType<<" | ";cout<<"EP Address: "<<(int)epdesc->bEndpointAddress<<" | ";}}}cout<<endl<<endl<<endl;
libusb_free_config_descriptor(config);}
#include <iostream>#include <libusb.h>usingnamespace std;int main(){
libusb_device **devs;//pointer to pointer of device, used to retrieve a list of devices
libusb_device_handle *dev_handle;//a device handle
libusb_context *ctx =NULL;//a libusb sessionint r;//for return values
ssize_t cnt;//holding number of devices in list
r = libusb_init(&ctx);//initialize the library for the session we just declaredif(r <0){cout<<"Init Error "<<r<<endl;//there was an errorreturn1;}
libusb_set_debug(ctx, 3);//set verbosity level to 3, as suggested in the documentation
cnt = libusb_get_device_list(ctx, &devs);//get the list of devicesif(cnt <0){cout<<"Get Device Error"<<endl;//there was an errorreturn1;}cout<<cnt<<" Devices in list."<<endl;
dev_handle = libusb_open_device_with_vid_pid(ctx, 5118, 7424);//these are vendorID and productID I found for my usb deviceif(dev_handle ==NULL)cout<<"Cannot open device"<<endl;elsecout<<"Device Opened"<<endl;
libusb_free_device_list(devs, 1);//free the list, unref the devices in itunsignedchar*data =newunsignedchar[4];//data to write
data[0]='a';data[1]='b';data[2]='c';data[3]='d';//some dummy valuesint actual;//used to find out how many bytes were writtenif(libusb_kernel_driver_active(dev_handle, 0)==1){//find out if kernel driver is attachedcout<<"Kernel Driver Active"<<endl;if(libusb_detach_kernel_driver(dev_handle, 0)==0)//detach itcout<<"Kernel Driver Detached!"<<endl;}
r = libusb_claim_interface(dev_handle, 0);//claim interface 0 (the first) of device (mine had jsut 1)if(r <0){cout<<"Cannot Claim Interface"<<endl;return1;}cout<<"Claimed Interface"<<endl;cout<<"Data->"<<data<<"<-"<<endl;//just to see the data we want to write : abcdcout<<"Writing Data..."<<endl;
r = libusb_bulk_transfer(dev_handle, (2| LIBUSB_ENDPOINT_OUT), data, 4, &actual, 0);//my device's out endpoint was 2, found with trial- the device had 2 endpoints: 2 and 129if(r ==0&& actual ==4)//we wrote the 4 bytes successfullycout<<"Writing Successful!"<<endl;elsecout<<"Write Error"<<endl;
r = libusb_release_interface(dev_handle, 0);//release the claimed interfaceif(r!=0){cout<<"Cannot Release Interface"<<endl;return1;}cout<<"Released Interface"<<endl;
libusb_close(dev_handle);//close the device we opened
libusb_exit(ctx);//needs to be called to end thedelete[] data;//delete the allocated memory for datareturn0;}