libusb_bulk_transfer

 API_EXPORTED int libusb_bulk_transfer(struct libusb_device_handle *dev_handle,
   unsigned char endpoint, unsigned char *data, int length, int *transferred,
   unsigned int timeout)
  {
   return do_sync_bulk_transfer(dev_handle, endpoint, data, length,
    transferred, timeout, LIBUSB_TRANSFER_TYPE_BULK);
  }

 

可以看到这个是调用do_sync_bulk_transfer

 

static int do_sync_bulk_transfer(struct libusb_device_handle *dev_handle,
   unsigned char endpoint, unsigned char *buffer, int length,
   int *transferred, unsigned int timeout, unsigned char type)
  {
   struct libusb_transfer *transfer = libusb_alloc_transfer(0);
   int completed = 0;
   int r;

   if (!transfer)
    return LIBUSB_ERROR_NO_MEM;

 

 

//传输结构  想必其核心就是传输的数据以及一些状态  其实就是货车

   libusb_fill_bulk_transfer(transfer, dev_handle, endpoint, buffer, length,
    bulk_transfer_cb, &completed, timeout);
   transfer->type = type;

 

//在 货车装完货物就可以出发了, 申请传输

   r = libusb_submit_transfer(transfer);
   if (r < 0) {
    libusb_free_transfer(transfer);
    return r;
   }

 

//complete就是一个装载完毕的标准

   while (!completed) {
    r = libusb_handle_events(HANDLE_CTX(dev_handle));
    if (r < 0) {
     if (r == LIBUSB_ERROR_INTERRUPTED)
      continue;
     libusb_cancel_transfer(transfer);
     while (!completed)
      if (libusb_handle_events(HANDLE_CTX(dev_handle)) < 0)
       break;
     libusb_free_transfer(transfer);
     return r;
    }
   }

   *transferred = transfer->actual_length;
   switch (transfer->status) {
   case LIBUSB_TRANSFER_COMPLETED:
    r = 0;
    break;
   case LIBUSB_TRANSFER_TIMED_OUT:
    r = LIBUSB_ERROR_TIMEOUT;
    break;
   case LIBUSB_TRANSFER_STALL:
    r = LIBUSB_ERROR_PIPE;
    break;
   case LIBUSB_TRANSFER_OVERFLOW:
    r = LIBUSB_ERROR_OVERFLOW;
    break;
   case LIBUSB_TRANSFER_NO_DEVICE:
    r = LIBUSB_ERROR_NO_DEVICE;
    break;
   default:
    usbi_warn(HANDLE_CTX(dev_handle),
     "unrecognised status code %d", transfer->status);
    r = LIBUSB_ERROR_OTHER;
   }

 

 

   libusb_free_transfer(transfer);
   return r;
  }

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页