简体   繁体   English

如何使用libusb和libusb_get_device_descriptor()?

[英]How to use libusb and libusb_get_device_descriptor()?

I'm learning to use libusb v1.0.0 for the first time on Ubuntu 12.10. 我正在学习在Ubuntu 12.10上第一次使用libusb v1.0.0。 Here is some small test code I'm using to try and understand how to use this API: 这是我用来尝试理解如何使用此API的一些小测试代码:

#include <libusb-1.0/libusb.h>
...
libusb_device **list;
libusb_get_device_list(ctx, &list); // Returns 11 USB devices which is correct.
for (size_t idx = 0; list[idx] != NULL; idx ++)
{
    libusb_device *dev = list[idx];
    libusb_device_descriptor desc = {0};
    int rc = libusb_get_device_descriptor(dev, &desc);

At this point, rc == 0, meaning it should have completed successfully. 此时,rc == 0,意味着它应该已经成功完成。 Source: documentation for *libusb_get_device_descriptor()* . 来源: * libusb_get_device_descriptor()*的文档

But the structure desc is always empty. 但结构desc总是空的。 None of the fields ever get set. 没有任何字段被设置。 If I change the last two lines above to this: 如果我将上面的最后两行更改为:

    libusb_device_descriptor desc = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    int rc = libusb_get_device_descriptor(dev, &desc);

...then when libusb_get_device_descriptor() returns, I see desc remains unchanged, confirming for me that I'm not getting what I expect from this API. ...然后当libusb_get_device_descriptor()返回时,我看到desc保持不变,向我确认我没有得到我对此API的期望。

I've also tried to run a.out as root just in case this requires elevated privileges. 我还尝试以root身份运行a.out ,以防万一这需要提升权限。 Doing a Google search on libusb_get_device_descriptor hasn't gotten me anywhere. libusb_get_device_descriptor上进行谷歌搜索并没有让我在任何地方。

Relevant commands I ran to try this code: 我跑来试试这段代码的相关命令:

sudo apt-get install libusb-1.0.0-dev
g++ -ggdb test.cpp -lusb-1.0
./a.out 

Ah! 啊! Crazy user error! 疯狂的用户错误! sharth's code helped me figure it out. sharth的代码帮我搞清楚了。 Here is the code I was actually using -- see if you can spot the error: 这是我实际使用的代码 - 看看你是否能发现错误:

std::cout << "rc == " << libusb_get_device_descriptor(dev, &desc) << std::endl
          << "vendor == " << desc.idVendor << std::endl;

I guess the way the compiler evaluates this, it is free to evaluate desc.idVendor before the call to libusb_get_device_descriptor() has actually been made. 我想编译器评估它的方式,在实际调用libusb_get_device_descriptor()之前可以自由地评估desc.idVendor My bad. 我的错。

You didn't include a full, compilable test case. 您没有包含完整的,可编译的测试用例。 So I built one. 所以我建了一个。 This works for me on CentOS 6 x64. 这适用于CentOS 6 x64。 I'm also running this as a normal user account. 我也将其作为普通用户帐户运行。

Source 资源

#include <cassert>
#include <cstdio>
#include <libusb-1.0/libusb.h>

int main() {
    libusb_context *context = NULL;
    libusb_device **list = NULL;
    int rc = 0;
    ssize_t count = 0;

    rc = libusb_init(&context);
    assert(rc == 0);

    count = libusb_get_device_list(context, &list);
    assert(count > 0);

    for (size_t idx = 0; idx < count; ++idx) {
        libusb_device *device = list[idx];
        libusb_device_descriptor desc = {0};

        rc = libusb_get_device_descriptor(device, &desc);
        assert(rc == 0);

        printf("Vendor:Device = %04x:%04x\n", desc.idVendor, desc.idProduct);
    }

    libusb_free_device_list(list, count);
    libusb_exit(context);
}

Output 产量

Vendor:Device = 1d6b:0002
Vendor:Device = 1d6b:0002
Vendor:Device = 8087:0020
Vendor:Device = 8087:0020
Vendor:Device = 0424:2514
Vendor:Device = 10c4:ea60
Vendor:Device = 051d:0002
Vendor:Device = 0624:0248

This is not an standalone answer, it's more a comment to Bill Lynch's post. 这不是一个独立的答案,而是对比尔林奇的帖子的评论。 I'm not able to add a comment to Bill Lynch's post (missing reputation) so I decided to do it this way ;-). 我无法在Bill Lynch的帖子中添加评论(缺少声望)所以我决定这样做;-)。

There's a little detail missing in the code above: 上面的代码中缺少一些细节:

you have to free the list you got filled from libusb_get_device_list by hand. 你必须手动释放你从libusb_get_device_list填写的清单。

From the libusb docu : 来自libusb文档

You are expected to unreference all the devices when you are done with them, and then free the list with libusb_free_device_list(). 完成后,您需要取消所有设备的引用,然后使用libusb_free_device_list()释放列表。 Note that libusb_free_device_list() can unref all the devices for you. 请注意,libusb_free_device_list()可以为您取消所有设备。 Be careful not to unreference a device you are about to open until after you have opened it. 在打开设备之前,请注意不要推断您要打开的设备。

Just to add to Bill's answer, to avoid the multiple warnings you will likely recieve from this line 只是为了补充比尔的答案,以避免你可能会从这条线上收到的多重警告

libusb_device_descriptor desc = {0};

Simply remove the assignment. 只需删除作业即可。

libusb_device_descriptor desc;

Not a huge deal, but these things bother me. 这不是什么大不了的事,但这些事情让我烦恼。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM