使用libusb输出不正确

时间:2015-03-16 17:25:41

标签: c linux libusb

我使用libusb创建了一个程序。我怀疑输出是否正确,因为所有条目都显示相同的供应商和产品ID。以下是代码:

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

这是我的输出:

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

这就是lsusb所说的:

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

需要帮助来找到错误(如果有的话)。

1 个答案:

答案 0 :(得分:3)

必须在某处意外跳过一行...我建议您添加以下行,否则ret会被酉化:

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

要将输出与lsusb的输出进行比较,请使用十六进制格式更改%x以打印idVendoridProduct

printf("Vendor: %x\n",desc.idVendor);
printf("Product ID: %x\n",desc.idProduct);

以下问题libusb semi-working, but libusb_device_descriptor undeclared?有助于找到缺失的行。这一个帮助我将libusb Libusb undefined reference to建议与gcc main.c -o main -lusb-1.0编译联系起来。