hidapi.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*******************************************************
  2. HIDAPI - Multi-Platform library for
  3. communication with HID devices.
  4. Alan Ott
  5. Signal 11 Software
  6. 8/22/2009
  7. Copyright 2009, All Rights Reserved.
  8. At the discretion of the user of this library,
  9. this software may be licensed under the terms of the
  10. GNU General Public License v3, a BSD-Style license, or the
  11. original HIDAPI license as outlined in the LICENSE.txt,
  12. LICENSE-gpl3.txt, LICENSE-bsd.txt, and LICENSE-orig.txt
  13. files located at the root of the source distribution.
  14. These files may also be found in the public source
  15. code repository located at:
  16. http://github.com/signal11/hidapi .
  17. ********************************************************/
  18. /** @file
  19. * @defgroup API hidapi API
  20. */
  21. #ifndef HIDAPI_H__
  22. #define HIDAPI_H__
  23. #include <wchar.h>
  24. #ifdef _WIN32
  25. #define HID_API_EXPORT __declspec(dllexport)
  26. #define HID_API_CALL
  27. #else
  28. #define HID_API_EXPORT /**< API export macro */
  29. #define HID_API_CALL /**< API call macro */
  30. #endif
  31. #define HID_API_EXPORT_CALL HID_API_EXPORT HID_API_CALL /**< API export and call macro*/
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. struct hid_device_;
  36. typedef struct hid_device_ hid_device; /**< opaque hidapi structure */
  37. /** hidapi info structure */
  38. struct hid_device_info {
  39. /** Platform-specific device path */
  40. char *path;
  41. /** Device Vendor ID */
  42. unsigned short vendor_id;
  43. /** Device Product ID */
  44. unsigned short product_id;
  45. /** Serial Number */
  46. wchar_t *serial_number;
  47. /** Device Release Number in binary-coded decimal,
  48. also known as Device Version Number */
  49. unsigned short release_number;
  50. /** Manufacturer String */
  51. wchar_t *manufacturer_string;
  52. /** Product string */
  53. wchar_t *product_string;
  54. /** Usage Page for this Device/Interface
  55. (Windows/Mac only). */
  56. unsigned short usage_page;
  57. /** Usage for this Device/Interface
  58. (Windows/Mac only).*/
  59. unsigned short usage;
  60. /** The USB interface which this logical device
  61. represents. Valid on both Linux implementations
  62. in all cases, and valid on the Windows implementation
  63. only if the device contains more than one interface. */
  64. int interface_number;
  65. /** Pointer to the next device */
  66. struct hid_device_info *next;
  67. };
  68. /** @brief Initialize the HIDAPI library.
  69. This function initializes the HIDAPI library. Calling it is not
  70. strictly necessary, as it will be called automatically by
  71. hid_enumerate() and any of the hid_open_*() functions if it is
  72. needed. This function should be called at the beginning of
  73. execution however, if there is a chance of HIDAPI handles
  74. being opened by different threads simultaneously.
  75. @ingroup API
  76. @returns
  77. This function returns 0 on success and -1 on error.
  78. */
  79. int HID_API_EXPORT HID_API_CALL hid_init(void);
  80. /** @brief Finalize the HIDAPI library.
  81. This function frees all of the static data associated with
  82. HIDAPI. It should be called at the end of execution to avoid
  83. memory leaks.
  84. @ingroup API
  85. @returns
  86. This function returns 0 on success and -1 on error.
  87. */
  88. int HID_API_EXPORT HID_API_CALL hid_exit(void);
  89. /** @brief Enumerate the HID Devices.
  90. This function returns a linked list of all the HID devices
  91. attached to the system which match vendor_id and product_id.
  92. If @p vendor_id is set to 0 then any vendor matches.
  93. If @p product_id is set to 0 then any product matches.
  94. If @p vendor_id and @p product_id are both set to 0, then
  95. all HID devices will be returned.
  96. @ingroup API
  97. @param vendor_id The Vendor ID (VID) of the types of device
  98. to open.
  99. @param product_id The Product ID (PID) of the types of
  100. device to open.
  101. @returns
  102. This function returns a pointer to a linked list of type
  103. struct #hid_device, containing information about the HID devices
  104. attached to the system, or NULL in the case of failure. Free
  105. this linked list by calling hid_free_enumeration().
  106. */
  107. struct hid_device_info HID_API_EXPORT * HID_API_CALL hid_enumerate(unsigned short vendor_id, unsigned short product_id);
  108. /** @brief Free an enumeration Linked List
  109. This function frees a linked list created by hid_enumerate().
  110. @ingroup API
  111. @param devs Pointer to a list of struct_device returned from
  112. hid_enumerate().
  113. */
  114. void HID_API_EXPORT HID_API_CALL hid_free_enumeration(struct hid_device_info *devs);
  115. /** @brief Open a HID device using a Vendor ID (VID), Product ID
  116. (PID) and optionally a serial number.
  117. If @p serial_number is NULL, the first device with the
  118. specified VID and PID is opened.
  119. @ingroup API
  120. @param vendor_id The Vendor ID (VID) of the device to open.
  121. @param product_id The Product ID (PID) of the device to open.
  122. @param serial_number The Serial Number of the device to open
  123. (Optionally NULL).
  124. @returns
  125. This function returns a pointer to a #hid_device object on
  126. success or NULL on failure.
  127. */
  128. HID_API_EXPORT hid_device * HID_API_CALL hid_open(unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number);
  129. /** @brief Open a HID device by its path name.
  130. The path name be determined by calling hid_enumerate(), or a
  131. platform-specific path name can be used (eg: /dev/hidraw0 on
  132. Linux).
  133. @ingroup API
  134. @param path The path name of the device to open
  135. @returns
  136. This function returns a pointer to a #hid_device object on
  137. success or NULL on failure.
  138. */
  139. HID_API_EXPORT hid_device * HID_API_CALL hid_open_path(const char *path);
  140. /** @brief Write an Output report to a HID device.
  141. The first byte of @p data[] must contain the Report ID. For
  142. devices which only support a single report, this must be set
  143. to 0x0. The remaining bytes contain the report data. Since
  144. the Report ID is mandatory, calls to hid_write() will always
  145. contain one more byte than the report contains. For example,
  146. if a hid report is 16 bytes long, 17 bytes must be passed to
  147. hid_write(), the Report ID (or 0x0, for devices with a
  148. single report), followed by the report data (16 bytes). In
  149. this example, the length passed in would be 17.
  150. hid_write() will send the data on the first OUT endpoint, if
  151. one exists. If it does not, it will send the data through
  152. the Control Endpoint (Endpoint 0).
  153. @ingroup API
  154. @param device A device handle returned from hid_open().
  155. @param data The data to send, including the report number as
  156. the first byte.
  157. @param length The length in bytes of the data to send.
  158. @returns
  159. This function returns the actual number of bytes written and
  160. -1 on error.
  161. */
  162. int HID_API_EXPORT HID_API_CALL hid_write(hid_device *device, const unsigned char *data, size_t length);
  163. /** @brief Read an Input report from a HID device with timeout.
  164. Input reports are returned
  165. to the host through the INTERRUPT IN endpoint. The first byte will
  166. contain the Report number if the device uses numbered reports.
  167. @ingroup API
  168. @param device A device handle returned from hid_open().
  169. @param data A buffer to put the read data into.
  170. @param length The number of bytes to read. For devices with
  171. multiple reports, make sure to read an extra byte for
  172. the report number.
  173. @param milliseconds timeout in milliseconds or -1 for blocking wait.
  174. @returns
  175. This function returns the actual number of bytes read and
  176. -1 on error. If no packet was available to be read within
  177. the timeout period, this function returns 0.
  178. */
  179. int HID_API_EXPORT HID_API_CALL hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds);
  180. /** @brief Read an Input report from a HID device.
  181. Input reports are returned
  182. to the host through the INTERRUPT IN endpoint. The first byte will
  183. contain the Report number if the device uses numbered reports.
  184. @ingroup API
  185. @param device A device handle returned from hid_open().
  186. @param data A buffer to put the read data into.
  187. @param length The number of bytes to read. For devices with
  188. multiple reports, make sure to read an extra byte for
  189. the report number.
  190. @returns
  191. This function returns the actual number of bytes read and
  192. -1 on error. If no packet was available to be read and
  193. the handle is in non-blocking mode, this function returns 0.
  194. */
  195. int HID_API_EXPORT HID_API_CALL hid_read(hid_device *device, unsigned char *data, size_t length);
  196. /** @brief Set the device handle to be non-blocking.
  197. In non-blocking mode calls to hid_read() will return
  198. immediately with a value of 0 if there is no data to be
  199. read. In blocking mode, hid_read() will wait (block) until
  200. there is data to read before returning.
  201. Nonblocking can be turned on and off at any time.
  202. @ingroup API
  203. @param device A device handle returned from hid_open().
  204. @param nonblock enable or not the nonblocking reads
  205. - 1 to enable nonblocking
  206. - 0 to disable nonblocking.
  207. @returns
  208. This function returns 0 on success and -1 on error.
  209. */
  210. int HID_API_EXPORT HID_API_CALL hid_set_nonblocking(hid_device *device, int nonblock);
  211. /** @brief Send a Feature report to the device.
  212. Feature reports are sent over the Control endpoint as a
  213. Set_Report transfer. The first byte of @p data[] must
  214. contain the Report ID. For devices which only support a
  215. single report, this must be set to 0x0. The remaining bytes
  216. contain the report data. Since the Report ID is mandatory,
  217. calls to hid_send_feature_report() will always contain one
  218. more byte than the report contains. For example, if a hid
  219. report is 16 bytes long, 17 bytes must be passed to
  220. hid_send_feature_report(): the Report ID (or 0x0, for
  221. devices which do not use numbered reports), followed by the
  222. report data (16 bytes). In this example, the length passed
  223. in would be 17.
  224. @ingroup API
  225. @param device A device handle returned from hid_open().
  226. @param data The data to send, including the report number as
  227. the first byte.
  228. @param length The length in bytes of the data to send, including
  229. the report number.
  230. @returns
  231. This function returns the actual number of bytes written and
  232. -1 on error.
  233. */
  234. int HID_API_EXPORT HID_API_CALL hid_send_feature_report(hid_device *device, const unsigned char *data, size_t length);
  235. /** @brief Get a feature report from a HID device.
  236. Set the first byte of @p data[] to the Report ID of the
  237. report to be read. Make sure to allow space for this
  238. extra byte in @p data[]. Upon return, the first byte will
  239. still contain the Report ID, and the report data will
  240. start in data[1].
  241. @ingroup API
  242. @param device A device handle returned from hid_open().
  243. @param data A buffer to put the read data into, including
  244. the Report ID. Set the first byte of @p data[] to the
  245. Report ID of the report to be read, or set it to zero
  246. if your device does not use numbered reports.
  247. @param length The number of bytes to read, including an
  248. extra byte for the report ID. The buffer can be longer
  249. than the actual report.
  250. @returns
  251. This function returns the number of bytes read plus
  252. one for the report ID (which is still in the first
  253. byte), or -1 on error.
  254. */
  255. int HID_API_EXPORT HID_API_CALL hid_get_feature_report(hid_device *device, unsigned char *data, size_t length);
  256. /** @brief Close a HID device.
  257. @ingroup API
  258. @param device A device handle returned from hid_open().
  259. */
  260. void HID_API_EXPORT HID_API_CALL hid_close(hid_device *device);
  261. /** @brief Get The Manufacturer String from a HID device.
  262. @ingroup API
  263. @param device A device handle returned from hid_open().
  264. @param string A wide string buffer to put the data into.
  265. @param maxlen The length of the buffer in multiples of wchar_t.
  266. @returns
  267. This function returns 0 on success and -1 on error.
  268. */
  269. int HID_API_EXPORT_CALL hid_get_manufacturer_string(hid_device *device, wchar_t *string, size_t maxlen);
  270. /** @brief Get The Product String from a HID device.
  271. @ingroup API
  272. @param device A device handle returned from hid_open().
  273. @param string A wide string buffer to put the data into.
  274. @param maxlen The length of the buffer in multiples of wchar_t.
  275. @returns
  276. This function returns 0 on success and -1 on error.
  277. */
  278. int HID_API_EXPORT_CALL hid_get_product_string(hid_device *device, wchar_t *string, size_t maxlen);
  279. /** @brief Get The Serial Number String from a HID device.
  280. @ingroup API
  281. @param device A device handle returned from hid_open().
  282. @param string A wide string buffer to put the data into.
  283. @param maxlen The length of the buffer in multiples of wchar_t.
  284. @returns
  285. This function returns 0 on success and -1 on error.
  286. */
  287. int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *device, wchar_t *string, size_t maxlen);
  288. /** @brief Get a string from a HID device, based on its string index.
  289. @ingroup API
  290. @param device A device handle returned from hid_open().
  291. @param string_index The index of the string to get.
  292. @param string A wide string buffer to put the data into.
  293. @param maxlen The length of the buffer in multiples of wchar_t.
  294. @returns
  295. This function returns 0 on success and -1 on error.
  296. */
  297. int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *device, int string_index, wchar_t *string, size_t maxlen);
  298. /** @brief Get a string describing the last error which occurred.
  299. @ingroup API
  300. @param device A device handle returned from hid_open().
  301. @returns
  302. This function returns a string containing the last error
  303. which occurred or NULL if none has occurred.
  304. */
  305. HID_API_EXPORT const wchar_t* HID_API_CALL hid_error(hid_device *device);
  306. #ifdef __cplusplus
  307. }
  308. #endif
  309. #endif