cpollinglibusb

What is the correct way of polling a list of libusb_pollfd structures?


The structure of pollfd is:

struct pollfd {
        int fd;             /* file descriptor */
        short events;       /* requested events */
        short revents;      /* returned events */
}

The structure of libusb_pollfd is:

struct libusb_pollfd {
         int fd;         /* Numeric file descriptor. */
         short events;
}

The structure of the poll function is:

int poll(struct pollfd *fds, nfds_t nfds, int timeout);

What is the correct way of polling a list of libusb_pollfd structures? Do you use the poll function with some kind of modification? Or is there a libusb function which is supposed to be used?


Solution

  • It seems the only way to achieve this, is using the following code:

        struct libusb_pollfd **pollfds; 
        struct pollfd pollfd_array[n];
    
        pollfds = libusb_get_pollfds(context);
        for (i=0; pollfds[i] != NULL; i++)
        {
            pollfd_array[i].fd = pollfds[i]->fd;
            pollfd_array[i].events = pollfds[i]->events;
        }
    
        poll_ret = poll(pollfd_array, i, 0);
    

    Which is what I was trying to avoid. Let me know if there's a better way.