c++ - 如何将 libusb 与 libevent 一起使用?

标签 c++ c libusb libevent libusb-1.0

我正在使用 libevent 编写一个事件驱动的应用程序,我需要使用 libusb-1.0 进行 USB 传输。

我想使用 libusb_get_pollfds获取文件描述符列表(在 fds 中)并将它们添加到 libevent,如下所示:

const struct libusb_pollfd **fds = libusb_get_pollfds(device->context);

const struct libusb_pollfd *it = *fds;
for(;it != NULL; ++it) {
    cout << "Adding fd: " << it->fd << ", " << it->events << endl;
    struct event * ev = event_new(base_, 
        it->fd, it->events | EV_PERSIST, 
        callbacks::libusb_cb, this);
    event_add(ev, 0);
    libusb_fds_events.insert(std::make_pair(it->fd, ev));
}

free(fds);

// (...)

// And the callback function:
void callbacks::libusb_cb(evutil_socket_t fd, short what, void *arg) {
    Server *s = reinterpret_cast<Server*>(arg);
    libusb_handle_events_timeout(s->device_->context, 0);
}

此外,我使用 libusb_set_pollfd_notifierslibusb_fds_events 添加/删除 fds。

问题是我在 libusb 返回的列表中得到了很多奇怪的 fds(例如,我多次得到 stdin(!),事件等于 0)。

我是否以正确的方式使用它?

最佳答案

我发现代码中有错误。应该是:

const struct libusb_pollfd **it = fds;
for(;*it != NULL; ++it) {
    cout << "Adding fd: " << (*it)->fd << ", " << (*it)->events << endl;
    struct event * ev = event_new(base_, 
        (*it)->fd, (*it)->events | EV_PERSIST, 
        callbacks::libusb_cb, this);
    event_add(ev, 0);
    libusb_fds_events.insert(std::make_pair((*it)->fd, ev));
}

关于c++ - 如何将 libusb 与 libevent 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5852742/

相关文章:

c++ - 如何将具有已知总线地址的 USB 设备映射到挂载路径?

c++ - Boost.Intrusive-恒定时间迭代器

c++ - 使用函数c++查找数组中的元素

c - 发送命令行参数时出现错误 : Command not found,

c++ - 测试整数类型是有符号还是无符号的宏

c++ - 错误 : invalid use of non-static member function ‘int test::hotplug_callback(libusb_context*, libusb_device*, libusb_hotplug_event, void*)’

c++ - SFML C++ 绘制形状 vector

c++ - 格式 '%c' 需要类型为 'char' 的参数

c - 多次fscanf

avr - 使用USBASP编程器进行SPI通信