如何使用libusb和libusb_get_device_descriptor()? [英] How to use libusb and libusb_get_device_descriptor()?

查看:990
本文介绍了如何使用libusb和libusb_get_device_descriptor()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习在Ubuntu 12.10上第一次使用libusb v1.0.0.这是一些我用来尝试了解如何使用此API的小型测试代码:

I'm learning to use libusb v1.0.0 for the first time on Ubuntu 12.10. Here is some small test code I'm using to try and understand how to use this 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);

这时,rc == 0,这意味着它应该已经成功完成.来源: * libusb_get_device_descriptor()* 的文档.

At this point, rc == 0, meaning it should have completed successfully. Source: documentation for *libusb_get_device_descriptor()*.

但是结构desc始终为空.没有设置任何字段.如果我将上面的最后两行更改为此:

But the structure desc is always empty. 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);

...然后,当libusb_get_device_descriptor()返回时,我看到desc保持不变,这向我确认我没有从该API中得到期望的结果.

...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.

我还试图以root用户身份运行a.out,以防万一这需要提升的特权.在libusb_get_device_descriptor上进行Google搜索并没有帮助我.

I've also tried to run a.out as root just in case this requires elevated privileges. Doing a Google search on libusb_get_device_descriptor hasn't gotten me anywhere.

我运行了相关命令以尝试以下代码:

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 


啊!疯狂的用户错误! sharth的代码帮助我弄清楚了.这是我实际使用的代码-看看是否可以发现错误:


Ah! Crazy user error! sharth's code helped me figure it out. 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;

我猜编译器对此进行评估的方式是,在实际调用libusb_get_device_descriptor()之前可以自由评估desc.idVendor.我不好.

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. My bad.

推荐答案

您没有提供完整的可编译测试用例.所以我建了一个.这对我在CentOS 6 x64上有效.我还以普通用户帐户的身份运行它.

You didn't include a full, compilable test case. So I built one. This works for me on CentOS 6 x64. I'm also running this as a normal user account.

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

输出

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

这篇关于如何使用libusb和libusb_get_device_descriptor()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆