usbhid.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*******************************************************************************
  2. Copyright 2019 Yepkit Lda (www.yepkit.com)
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. *******************************************************************************/
  13. #ifndef _USBHID_H_
  14. #define _USBHID_H_
  15. #include <libusb.h>
  16. /** hidapi info structure */
  17. struct hid_device_info {
  18. /** Platform-specific device path */
  19. char *path;
  20. /** Device Vendor ID */
  21. unsigned short vendor_id;
  22. /** Device Product ID */
  23. unsigned short product_id;
  24. /** Serial Number */
  25. wchar_t *serial_number;
  26. char *serial_number_ascii;
  27. /** Device Release Number in binary-coded decimal,
  28. also known as Device Version Number */
  29. unsigned short release_number;
  30. /** Manufacturer String */
  31. wchar_t *manufacturer_string;
  32. char *manufacturer_string_ascii;
  33. /** Product string */
  34. wchar_t *product_string;
  35. /** Usage Page for this Device/Interface
  36. (Windows/Mac only). */
  37. char *product_string_ascii;
  38. unsigned short usage_page;
  39. /** Usage for this Device/Interface
  40. (Windows/Mac only).*/
  41. unsigned short usage;
  42. /** The USB interface which this logical device
  43. represents.
  44. * Valid on both Linux implementations in all cases.
  45. * Valid on the Windows implementation only if the device
  46. contains more than one interface.
  47. * Valid on the Mac implementation if and only if the device
  48. is a USB HID device. */
  49. int interface_number;
  50. /** Pointer to the next device */
  51. struct hid_device_info *next;
  52. };
  53. struct input_report {
  54. uint8_t *data;
  55. size_t len;
  56. struct input_report *next;
  57. };
  58. typedef struct hid_device {
  59. /* Handle to the actual device. */
  60. libusb_device_handle *handle;
  61. /* Endpoint information */
  62. int input_endpoint;
  63. int output_endpoint;
  64. int input_ep_max_packet_size;
  65. /* The interface number of the HID */
  66. int interface;
  67. /* Indexes of Strings */
  68. int manufacturer_index;
  69. int product_index;
  70. int serial_index;
  71. /* Whether blocking reads are used */
  72. int blocking; /* boolean */
  73. int cancelled;
  74. struct libusb_transfer *transfer;
  75. /* List of received input reports. */
  76. struct input_report *input_reports;
  77. } HidDevice;
  78. class UsbHid {
  79. public:
  80. /**
  81. * \brief UsbHid class constructor.
  82. */
  83. UsbHid();
  84. /**
  85. * \brief Enumerates the USB devices with the provided VID and PID.
  86. *
  87. * This method will alloc memory for device structure that must be free when not needed.
  88. * Call free_enumeration() method to free the device structure.
  89. *
  90. * \return List of USB devices currently attached to the system with the provided VID and PID.
  91. */
  92. struct hid_device_info *enumerate(unsigned int vendor_id, unsigned int product_id);
  93. void free_enumeration(struct hid_device_info *devs);
  94. /**
  95. * \brief Opens the device and sets the open_device property
  96. *
  97. * \retval 0 Success
  98. * \retval -1 Error
  99. */
  100. int open(unsigned int vendor_id, unsigned int product_id, char *serial);
  101. /**
  102. * \brief Closes de open_device
  103. */
  104. void close(void);
  105. /**
  106. * \brief Writes to the open_device
  107. *
  108. * \retval 0 Success
  109. * \retval -1 Error
  110. */
  111. int write(unsigned char *data, size_t length);
  112. int read(unsigned char *data, int length);
  113. private:
  114. libusb_context *usb_context;
  115. int init();
  116. wchar_t *get_usb_string(libusb_device_handle *dev, uint8_t idx);
  117. char *get_usb_string_ascii(libusb_device_handle *dev, uint8_t idx);
  118. HidDevice open_device;
  119. };
  120. #endif