libusb_bulk_transfer 说明

libusb_bulk_transfer函数说明

  1.  
    API_EXPORTED int libusb_bulk_transfer(struct libusb_device_handle *dev_handle,
  2.  
    unsigned char endpoint, unsigned char *data, int length, int *transferred,
  3.  
    unsigned int timeout)
  4.  
    {
  5.  
    // 调用do_sync_bulk_transfer
  6.  
    return do_sync_bulk_transfer(dev_handle, endpoint, data, length,
  7.  
    transferred, timeout, LIBUSB_TRANSFER_TYPE_BULK);
  8.  
    }


代码:

 

    1.  
      static int do_sync_bulk_transfer(struct libusb_device_handle *dev_handle,
    2.  
      unsigned char endpoint, unsigned char *buffer, int length,
    3.  
      int *transferred, unsigned int timeout, unsigned char type)
    4.  
      {
    5.  
      struct libusb_transfer *transfer = libusb_alloc_transfer(0);
    6.  
      int completed = 0;
    7.  
      int r;
    8.  
       
    9.  
      if (!transfer)
    10.  
      return LIBUSB_ERROR_NO_MEM;
    11.  
       
    12.  
      //传输结构 想必其核心就是传输的数据以及一些状态 其实就是货车
    13.  
      libusb_fill_bulk_transfer(transfer, dev_handle, endpoint, buffer, length,
    14.  
      bulk_transfer_cb, &completed, timeout);
    15.  
      transfer->type = type;
    16.  
       
    17.  
      //在货车装完货物就可以出发了, 申请传输
    18.  
      r = libusb_submit_transfer(transfer);
    19.  
      if (r < 0) {
    20.  
      libusb_free_transfer(transfer);
    21.  
      return r;
    22.  
      }
    23.  
       
    24.  
       
    25.  
       
    26.  
      //complete就是一个装载完毕的标准
    27.  
       
    28.  
      while (!completed) {
    29.  
      r = libusb_handle_events(HANDLE_CTX(dev_handle));
    30.  
      if (r < 0) {
    31.  
      if (r == LIBUSB_ERROR_INTERRUPTED)
    32.  
      continue;
    33.  
      libusb_cancel_transfer(transfer);
    34.  
      while (!completed)
    35.  
      if (libusb_handle_events(HANDLE_CTX(dev_handle)) < 0)
    36.  
      break;
    37.  
      libusb_free_transfer(transfer);
    38.  
      return r;
    39.  
      }
    40.  
      }
    41.  
       
    42.  
      *transferred = transfer->actual_length;
    43.  
      switch (transfer->status) {
    44.  
      case LIBUSB_TRANSFER_COMPLETED:
    45.  
      r = 0;
    46.  
      break;
    47.  
      case LIBUSB_TRANSFER_TIMED_OUT:
    48.  
      r = LIBUSB_ERROR_TIMEOUT;
    49.  
      break;
    50.  
      case LIBUSB_TRANSFER_STALL:
    51.  
      r = LIBUSB_ERROR_PIPE;
    52.  
      break;
    53.  
      case LIBUSB_TRANSFER_OVERFLOW:
    54.  
      r = LIBUSB_ERROR_OVERFLOW;
    55.  
      break;
    56.  
      case LIBUSB_TRANSFER_NO_DEVICE:
    57.  
      r = LIBUSB_ERROR_NO_DEVICE;
    58.  
      break;
    59.  
      default:
    60.  
      usbi_warn(HANDLE_CTX(dev_handle),
    61.  
      "unrecognised status code %d", transfer->status);
    62.  
      r = LIBUSB_ERROR_OTHER;
    63.  
      }
    64.  
      libusb_free_transfer(transfer);
    65.  
      return r;
    66.  
      }
       
      https://blog.csdn.net/zdyueguanyun/article/details/51192885

你可能感兴趣的:(libusb_bulk_transfer 说明)