+static void receive_transfer(struct libusb_transfer *transfer)
+{
+ struct dev_context *devc;
+ struct sr_dev_inst *sdi;
+ int ret;
+
+ sdi = transfer->user_data;
+ devc = sdi->priv;
+ if (transfer->status == LIBUSB_TRANSFER_NO_DEVICE) {
+ /* USB device was unplugged. */
+ hw_dev_acquisition_stop(sdi, sdi);
+ } else if (transfer->status == LIBUSB_TRANSFER_COMPLETED) {
+ sr_dbg("got %d-byte packet", transfer->actual_length);
+ if (transfer->actual_length == 14) {
+ devc->num_samples++;
+ /* TODO */
+
+ if (devc->limit_samples) {
+ if (devc->num_samples >= devc->limit_samples)
+ hw_dev_acquisition_stop(sdi, sdi);
+ }
+ }
+ }
+ /* Anything else is either an error or a timeout, which is fine:
+ * we were just going to send another transfer request anyway. */
+
+ if (sdi->status == SR_ST_ACTIVE) {
+ /* Send the same request again. */
+ if ((ret = libusb_submit_transfer(transfer) != 0)) {
+ sr_err("unable to resubmit transfer: %s", libusb_error_name(ret));
+ libusb_free_transfer(transfer);
+ g_free(transfer->buffer);
+ hw_dev_acquisition_stop(sdi, sdi);
+ }
+ } else {
+ /* This was the last transfer we're going to receive, so
+ * clean up now. */
+ libusb_free_transfer(transfer);
+ g_free(transfer->buffer);
+ }
+
+}
+
+static int handle_events(int fd, int revents, void *cb_data)
+{
+ struct dev_context *devc;
+ struct sr_datafeed_packet packet;
+ struct sr_dev_inst *sdi;
+ struct timeval tv;
+ gint64 now;
+ int i;
+
+ (void)fd;
+ (void)revents;
+
+ sdi = cb_data;
+ devc = sdi->priv;
+
+ if (devc->limit_msec) {
+ now = g_get_monotonic_time() / 1000;
+ if (now > devc->end_time)
+ hw_dev_acquisition_stop(sdi, sdi);
+ }
+
+ if (sdi->status == SR_ST_STOPPING) {
+ for (i = 0; devc->usbfd[i] != -1; i++)
+ sr_source_remove(devc->usbfd[i]);
+
+ hw_dev_close(sdi);
+
+ packet.type = SR_DF_END;
+ sr_session_send(cb_data, &packet);
+ }
+
+ memset(&tv, 0, sizeof(struct timeval));
+ libusb_handle_events_timeout_completed(NULL, &tv, NULL);
+
+ return TRUE;
+}
+