如何将 libusb 与 libevent 结合使用?
我正在使用 libevent 编写一个事件驱动的应用程序,并且需要使用 libusb-1.0 进行 USB 传输。
我想使用 libusb_get_pollfds 获取文件描述符列表(在 < code>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_notifiers 从 libusb_fds_events
添加/删除 fd。
问题是我在 libusb 返回的列表上得到了许多奇怪的 fd(例如,我多次得到 stdin
(!) 且事件等于 0)。
我是否以正确的方式使用它?
I'm writing an event-driven application using libevent, and I need to do a USB transfer using libusb-1.0.
I wanted to use libusb_get_pollfds to get a list of files descriptors (in fds
) and add them to libevent like this:
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);
}
Also, I use libusb_set_pollfd_notifiers to add/remove fds from libusb_fds_events
.
The problem is I get many strange fds on the list returned by libusb (for example, I get stdin
(!) many times with event equals to 0).
Am I using it in right way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现代码中有错误。本来应该是:
I found an error in code. It should have been: