Reputation: 2713
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?
Upvotes: 3
Views: 3052
Reputation: 2713
I found an error in code. It should have been:
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));
}
Upvotes: 5