LibeventでLibusbを使用する方法は?
-
27-10-2019 - |
質問
Libeventを使用してイベント駆動型アプリケーションを書いています。LibUSB-1.0を使用してUSB転送を行う必要があります。
使いたかった libusb_get_pollfds ファイル記述子のリストを取得するには(in 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 FDSを追加/削除するには libusb_fds_events
.
問題は、Libusbによって返されたリストに多くの奇妙なFDを取得することです(たとえば、私は取得します 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));
}
所属していません StackOverflow