问题
I have made a program using libusb. I doubt if the output is correct as all the entries show the same vendor and product id. Following is the code:
#include <stdio.h>
#include <stdlib.h>
#include <libusb-1.0/libusb.h>
void print_devices(libusb_device *dev)
{
struct libusb_device_descriptor desc;
struct libusb_config_descriptor *config;
const struct libusb_interface *inter;
const struct libusb_interface_descriptor *interdesc;
const struct libusb_endpoint_descriptor *endpointdesc;
int ret;
int i,j,k;
if(ret<0)
{
fprintf(stderr,"error in getting device descriptor\n");
return;
}
printf("Number of possible configs is %d\n",desc.bNumConfigurations);
printf("Vendor: %d\n",desc.idVendor);
printf("Product ID: %d\n",desc.idProduct);
libusb_get_config_descriptor(dev, 0, &config);
printf("Interface: %d\n", config->bNumInterfaces);
printf("\n\n");
}
int main(int argc,char *argv[])
{
libusb_device **devs;
libusb_context *context = NULL;
size_t list;
size_t i;
int ret,temp;
ret = libusb_init(&context);
if(ret < 0)
{
perror("libusb_init");
exit(1);
}
list = libusb_get_device_list(context, &devs);
if(list < 0)
{
fprintf(stderr, "Error in getting device list\n");
libusb_free_device_list(devs, 1);
libusb_exit(context);
exit(1);
}
temp=(int)list;
printf("\n%d devices found\n\n",temp);
for(i=0;i<temp;i++)
{
//print devices
print_devices(devs[i]);
}
libusb_free_device_list(devs, 1);
libusb_exit(context);
return 0;
}
and this is my output:
anubhav@anubhav-Inspiron-3421:~/Desktop/usb$ ./usbtest
9 devices found
Number of possible configs is 103
Vendor: 0
Product ID: 0
Interface: 1
Number of possible configs is 103
Vendor: 0
Product ID: 0
Interface: 1
Number of possible configs is 103
Vendor: 0
Product ID: 0
Interface: 2
Number of possible configs is 103
Vendor: 0
Product ID: 0
Interface: 4
Number of possible configs is 103
Vendor: 0
Product ID: 0
Interface: 1
Number of possible configs is 103
Vendor: 0
Product ID: 0
Interface: 1
Number of possible configs is 103
Vendor: 0
Product ID: 0
Interface: 1
Number of possible configs is 103
Vendor: 0
Product ID: 0
Interface: 1
Number of possible configs is 103
Vendor: 0
Product ID: 0
Interface: 1
this is what lsusb shows:
anubhav@anubhav-Inspiron-3421:~/Desktop/usb$ lsusb
Bus 002 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 004: ID 064e:812c Suyin Corp.
Bus 001 Device 007: ID 0a5c:21d7 Broadcom Corp. BCM43142 Bluetooth 4.0
Bus 001 Device 003: ID 0bda:0129 Realtek Semiconductor Corp. RTS5129 Card Reader Controller
Bus 001 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Need help to locate the error(if any).
回答1:
A line must have been accidentally skipped somewhere... I suggest you add the following line, otherwise ret
is unitialized :
int ret;
int i,j,k;
ret = libusb_get_device_descriptor(dev, &desc);//this line !
if(ret<0)
{
fprintf(stderr,"error in getting device descriptor\n");
return;
}
To compare the output to the one of lsusb
, change for %x
to print idVendor
and idProduct
using hexadecimal format.
printf("Vendor: %x\n",desc.idVendor);
printf("Product ID: %x\n",desc.idProduct);
The following question libusb semi-working, but libusb_device_descriptor undeclared? was helpful to find the missing line. And this one helped me linking libusb
Libusb undefined reference to it advices to compile by gcc main.c -o main -lusb-1.0
.
来源:https://stackoverflow.com/questions/29083184/improper-output-using-libusb