将Windows API移植到linux,USB HID设备读取

将Windows API移植到linux,USB HID设备读取,linux,usb,api-design,porting,Linux,Usb,Api Design,Porting,我使用一个USB HID设备,一个带LED的按钮。有两个API用于访问设备。一个用于windows,一个用于linux。不幸的是,linux API没有读取按钮状态(按下/释放)的功能,但windows API有此功能。我想将现有功能移植到linux API。我已经试过好几次了,但还不能从USB设备获取数据 从设备读取现有和功能性windows实现的步骤: 首先是一个具有id和四个字符的写入操作(在要写入的第一个字符中配置了0x02) 然后对与之前写操作相同的结构执行读操作。该状态的读

我使用一个USB HID设备,一个带LED的按钮。有两个API用于访问设备。一个用于windows,一个用于linux。不幸的是,linux API没有读取按钮状态(按下/释放)的功能,但windows API有此功能。我想将现有功能移植到linux API。我已经试过好几次了,但还不能从USB设备获取数据

从设备读取现有和功能性windows实现的步骤:

  • 首先是一个具有id和四个字符的写入操作(在要写入的第一个字符中配置了0x02)

  • 然后对与之前写操作相同的结构执行读操作。该状态的读取位置与之前写入时放置的0x02位置相同

    PACDRIVE_API BOOL_stdcall USBButtonGetState(内部id,PBOOL状态) { if(m_hidDeviceData[id].Type!=DEVICETYPE_USBBUTTON) 虚假的

    }

在linux API上,他们使用libusb-1.0/libusb.h库, 用于设置按钮颜色的现有写入功能正在使用 libusb_控制_传输函数:

bool writeUSBButton(unsigned char* barray, int autoconnect, bool transfer, unsigned int size)
{
  libusb_context *ctx = NULL;
  struct libusb_device_handle *handle = NULL;
  unsigned char mesg[USBBTN_MESG_LENGTH] = {0,0,0,0};

  bool result = true;

  int pos = 0;
  int ret = 0;

  if (transfer)
  {
    handle = openUSB(ctx, USBBTN_VENDOR, USBBTN_PRODUCT, USBBTN_INTERFACE, autoconnect);

    if (!handle)
    {
      result = false;
      goto error;
    }
  }

  while (pos < size)
  {
    memcpy(&mesg[0], &barray[pos], 4);

    debug ("Writing (%i): %x, %x, %x, %x", pos, mesg[0], mesg[1], mesg[2], mesg[3]);
    if (transfer)
    {
      ret = libusb_control_transfer(handle,
                                    UM_REQUEST_TYPE,
                                    UM_REQUEST,
                                    USBBTN_VALUE,
                                    USBBTN_INTERFACE,
                                    mesg,
                                    USBBTN_MESG_LENGTH,
                                    UM_TIMEOUT);
      debug ("Write result: %i", ret);
    }
    pos+=USBBTN_MESG_LENGTH;
  }

exit:
  if (transfer)
  {
    closeUSB(ctx, handle, USBBTN_INTERFACE);
  }
  else
  {
    log_info ("board array was not written out!!!");
  }
  return result;

error:
  return result;
}
我使用这些参数来阅读:

> Setup Data (read)
>     bmRequestType: 
>         1... .... = Direction: Device to Host
>         .01. .... = Type: Class (0x1)
>         ...0 0000 = Recipient: Device (0x00)
>               1010 0000  -> 0xa0(hex)
非工作代码:

bool readUSBButton(unsigned char* barray, int autoconnect, bool transfer, unsigned int size)
{


 // barray[0] = 0x02;
 // barray[1] = 0x00;
 // barray[2] = 0x00;
 // barray[3] = 0x00;

 // result = writeUSBButton(barray, 1, true, USBBTN_MESG_LENGTH);


  libusb_context *ctx = NULL;
  struct libusb_device_handle *handle = NULL;
  // USBBTN_MESG_LENGTH = 4 
  unsigned char mesg[USBBTN_MESG_LENGTH] = {0,0,0,0};

  bool result = true;

  int pos = 0;
  int ret = 0;

  if (transfer)
  {
    handle = openUSB(ctx, USBBTN_VENDOR, USBBTN_PRODUCT, USBBTN_INTERFACE, autoconnect);

    if (!handle)
    {
      result = false;
      goto error;
    }
  }

  while (pos < size)
  {

    debug ("reading (%i): %x, %x, %x, %x", pos, mesg[0], mesg[1], mesg[2], mesg[3]);
    if (transfer)
    {   
     uint8_t buttonData;

      ret = libusb_control_transfer (handle, 0xA0, 2, 0, 0, &buttonData, 1, 0); 
      printf(" #### Read: %x  ######  ret= %i", buttonData , ret); 
      debug ("read result: %i", ret);
    }
    pos+=USBBTN_MESG_LENGTH;
  }

exit:
  if (transfer)
  {
    closeUSB(ctx, handle, USBBTN_INTERFACE);
  }
  else
  {
    log_info ("board array was not read !!!");
  }
  return result;

error:
  return result;

}
  
> Setup Data (read)
>     bmRequestType: 
>         1... .... = Direction: Device to Host
>         .01. .... = Type: Class (0x1)
>         ...0 0000 = Recipient: Device (0x00)
>               1010 0000  -> 0xa0(hex)
bool readUSBButton(unsigned char* barray, int autoconnect, bool transfer, unsigned int size)
{


 // barray[0] = 0x02;
 // barray[1] = 0x00;
 // barray[2] = 0x00;
 // barray[3] = 0x00;

 // result = writeUSBButton(barray, 1, true, USBBTN_MESG_LENGTH);


  libusb_context *ctx = NULL;
  struct libusb_device_handle *handle = NULL;
  // USBBTN_MESG_LENGTH = 4 
  unsigned char mesg[USBBTN_MESG_LENGTH] = {0,0,0,0};

  bool result = true;

  int pos = 0;
  int ret = 0;

  if (transfer)
  {
    handle = openUSB(ctx, USBBTN_VENDOR, USBBTN_PRODUCT, USBBTN_INTERFACE, autoconnect);

    if (!handle)
    {
      result = false;
      goto error;
    }
  }

  while (pos < size)
  {

    debug ("reading (%i): %x, %x, %x, %x", pos, mesg[0], mesg[1], mesg[2], mesg[3]);
    if (transfer)
    {   
     uint8_t buttonData;

      ret = libusb_control_transfer (handle, 0xA0, 2, 0, 0, &buttonData, 1, 0); 
      printf(" #### Read: %x  ######  ret= %i", buttonData , ret); 
      debug ("read result: %i", ret);
    }
    pos+=USBBTN_MESG_LENGTH;
  }

exit:
  if (transfer)
  {
    closeUSB(ctx, handle, USBBTN_INTERFACE);
  }
  else
  {
    log_info ("board array was not read !!!");
  }
  return result;

error:
  return result;

}
  
Bus 001 Device 006: ID d209:1200 Ultimarc
Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               2.00
  bDeviceClass            0
  bDeviceSubClass         0
  bDeviceProtocol         0
  bMaxPacketSize0         8
  idVendor           0xd209 Ultimarc
  idProduct          0x1200
  bcdDevice            0.01
  iManufacturer           2 USBButton
  iProduct                2 USBButton
  iSerial                 1 1
  bNumConfigurations      1
  Configuration Descriptor:
    bLength                 9
    bDescriptorType         2
    wTotalLength       0x003b
    bNumInterfaces          2
    bConfigurationValue     1
    iConfiguration          2 USBButton
    bmAttributes         0xa0
      (Bus Powered)
      Remote Wakeup
    MaxPower              100mA
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        0
      bAlternateSetting       0
      bNumEndpoints           1
      bInterfaceClass         3 Human Interface Device
      bInterfaceSubClass      0
      bInterfaceProtocol      0
      iInterface              2 USBButton
        HID Device Descriptor:
          bLength                 9
          bDescriptorType        33
          bcdHID               1.11
          bCountryCode            0 Not supported
          bNumDescriptors         1
          bDescriptorType        34 Report
          wDescriptorLength      33
         Report Descriptors:
           ** UNAVAILABLE **
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x81  EP 1 IN
        bmAttributes            3
          Transfer Type            Interrupt
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0008  1x 8 bytes
        bInterval              10
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        1
      bAlternateSetting       0
      bNumEndpoints           1
      bInterfaceClass         3 Human Interface Device
      bInterfaceSubClass      1 Boot Interface Subclass
      bInterfaceProtocol      1 Keyboard
      iInterface              2 USBButton
        HID Device Descriptor:
          bLength                 9
          bDescriptorType        33
          bcdHID               1.11
          bCountryCode            0 Not supported
          bNumDescriptors         1
          bDescriptorType        34 Report
          wDescriptorLength     168
         Report Descriptors:
           ** UNAVAILABLE **
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x82  EP 2 IN
        bmAttributes            3
          Transfer Type            Interrupt
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0008  1x 8 bytes
        bInterval              10
can't get device qualifier: Resource temporarily unavailable
can't get debug descriptor: Resource temporarily unavailable
Device Status:     0x0000
  (Bus Powered)