esp_hidh.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. // Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD
  2. //
  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. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #pragma once
  14. #include "sdkconfig.h"
  15. #include "esp_err.h"
  16. #include "esp_log.h"
  17. #include "esp_event.h"
  18. #include "esp_hid_common.h"
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. /**
  23. * @brief HIDH structure forward declaration
  24. */
  25. struct esp_hidh_dev_s;
  26. typedef struct esp_hidh_dev_s esp_hidh_dev_t;
  27. ESP_EVENT_DECLARE_BASE(ESP_HIDH_EVENTS);
  28. /**
  29. * @brief HIDH callback events enum
  30. */
  31. typedef enum {
  32. ESP_HIDH_ANY_EVENT = ESP_EVENT_ANY_ID, /*!< HID device any event */
  33. ESP_HIDH_OPEN_EVENT = 0, /*!< HID device opened */
  34. ESP_HIDH_BATTERY_EVENT, /*!< HID device battery level changed */
  35. ESP_HIDH_INPUT_EVENT, /*!< Received HID device INPUT report */
  36. ESP_HIDH_FEATURE_EVENT, /*!< Received HID device FEATURE report */
  37. ESP_HIDH_CLOSE_EVENT, /*!< HID device closed */
  38. ESP_HIDH_START_EVENT, /*!< HID host stack started, used only for Classic Bluetooth */
  39. ESP_HIDH_STOP_EVENT, /*!< HID host stack stopped, used only for Classic Bluetooth */
  40. ESP_HIDH_MAX_EVENT, /*!< HID events end marker */
  41. } esp_hidh_event_t;
  42. /**
  43. * @brief HIDH callback parameters union
  44. */
  45. typedef union {
  46. /**
  47. * @brief ESP_HIDH_START_EVENT
  48. * @note Used only for Classic Bluetooth.
  49. */
  50. struct {
  51. esp_err_t status; /*!< HID host operation status */
  52. } start; /*!< HID callback param of ESP_HIDH_START_EVENT */
  53. /**
  54. * @brief ESP_HIDH_STOP_EVENT
  55. * @note Used only for Classic Bluetooth.
  56. */
  57. struct {
  58. esp_err_t status; /*!< HID host operation status */
  59. } stop; /*!< HID callback param of ESP_HIDH_STOP_EVENT */
  60. /**
  61. * @brief ESP_HIDH_OPEN_EVENT
  62. */
  63. struct {
  64. esp_hidh_dev_t *dev; /*!< HID Remote bluetooth device */
  65. esp_err_t status; /*!< HID host operation status, used only for Classic Bluetooth */
  66. } open; /*!< HID callback param of ESP_HIDH_OPEN_EVENT */
  67. /**
  68. * @brief ESP_HIDH_CLOSE_EVENT
  69. */
  70. struct {
  71. esp_hidh_dev_t *dev; /*!< HID Remote bluetooth device. */
  72. int reason; /*!< Reason why the connection was closed. BLE Only */
  73. esp_err_t status; /*!< HID host operation status, used only for Classic Bluetooth */
  74. } close; /*!< HID callback param of ESP_HIDH_CLOSE_EVENT */
  75. /**
  76. * @brief ESP_HIDH_BATTERY_EVENT
  77. */
  78. struct {
  79. esp_hidh_dev_t *dev; /*!< HID Remote bluetooth device */
  80. uint8_t level; /*!< Battery Level (0-100%) */
  81. esp_err_t status; /*!< HID host operation status */
  82. } battery; /*!< HID callback param of ESP_HIDH_BATTERY_EVENT */
  83. /**
  84. * @brief ESP_HIDH_INPUT_EVENT
  85. */
  86. struct {
  87. esp_hidh_dev_t *dev; /*!< HID Remote bluetooth device */
  88. esp_hid_usage_t usage; /*!< HID report usage */
  89. uint16_t report_id; /*!< HID report index */
  90. uint16_t length; /*!< HID data length */
  91. uint8_t *data; /*!< The pointer to the HID data */
  92. uint8_t map_index; /*!< HID report map index */
  93. } input; /*!< HID callback param of ESP_HIDH_INPUT_EVENT */
  94. /**
  95. * @brief ESP_HIDH_FEATURE_EVENT
  96. */
  97. struct {
  98. esp_hidh_dev_t *dev; /*!< HID Remote bluetooth device */
  99. esp_hid_usage_t usage; /*!< HID report usage */
  100. uint16_t report_id; /*!< HID report index */
  101. uint16_t length; /*!< HID data length */
  102. uint8_t *data; /*!< The pointer to the HID data */
  103. uint8_t map_index; /*!< HID report map index */
  104. esp_err_t status; /*!< HID host operation status, used only for Classic Bluetooth */
  105. esp_hid_trans_type_t trans_type; /*!< HID host feature transaction type, used only for Classic Bluetooth */
  106. } feature; /*!< HID callback param of ESP_HIDH_FEATURE_EVENT */
  107. } esp_hidh_event_data_t;
  108. typedef struct {
  109. esp_event_handler_t callback;
  110. uint16_t event_stack_size;
  111. void *callback_arg;
  112. } esp_hidh_config_t;
  113. /**
  114. * @brief Initialize the HID Host component
  115. * @param config : pointer to esp_hidh_config_t structure
  116. *
  117. * @return: ESP_OK on success
  118. */
  119. esp_err_t esp_hidh_init(const esp_hidh_config_t *config);
  120. /**
  121. * @brief De-initialize the HID Host component
  122. *
  123. * @return: ESP_OK on success
  124. */
  125. esp_err_t esp_hidh_deinit(void);
  126. /**
  127. * @brief Close HID Device
  128. * @param dev : pointer to the device
  129. *
  130. * @return: ESP_OK on success
  131. */
  132. esp_err_t esp_hidh_dev_close(esp_hidh_dev_t *dev);
  133. /**
  134. * @brief Free HID Device Memory
  135. * This function MUST be called when handling ESP_HIDH_CLOSE_EVENT
  136. * Only then all memory used for the device will be freed.
  137. * @param dev : pointer to the device
  138. *
  139. * @return: ESP_OK on success
  140. */
  141. esp_err_t esp_hidh_dev_free(esp_hidh_dev_t *dev);
  142. /**
  143. * @brief Check if the device still exists.
  144. * @param dev : pointer to the device
  145. *
  146. * @return: true if exists
  147. */
  148. bool esp_hidh_dev_exists(esp_hidh_dev_t *dev);
  149. /**
  150. * @brief Send an OUTPUT report to the device
  151. * @param dev : pointer to the device
  152. * @param map_index : index of the device report map
  153. * @param report_id : id of the HID OUTPUT report
  154. * @param data : pointer to the data to send
  155. * @param length : length of the data to send
  156. *
  157. * @return: ESP_OK on success
  158. */
  159. esp_err_t esp_hidh_dev_output_set(esp_hidh_dev_t *dev, size_t map_index, size_t report_id, uint8_t *data, size_t length);
  160. /**
  161. * @brief Send a FEATURE report to the device
  162. * @param dev : pointer to the device
  163. * @param map_index : index of the device report map
  164. * @param report_id : id of the HID FEATURE report
  165. * @param data : pointer to the data to send
  166. * @param length : length of the data to send
  167. *
  168. * @return: ESP_OK on success
  169. */
  170. esp_err_t esp_hidh_dev_feature_set(esp_hidh_dev_t *dev, size_t map_index, size_t report_id, uint8_t *data, size_t length);
  171. /**
  172. * @brief Get the value a FEATURE report from the device
  173. * @param dev : pointer to the device
  174. * @param map_index : index of the device report map
  175. * @param report_id : id of the HID FEATURE report
  176. * @param max_len : size of the buffer that will hold the data
  177. * @param data : pointer to the data buffer
  178. * @param length : pointer to the value that will be set to the number of bytes received
  179. *
  180. * @return: ESP_OK on success
  181. */
  182. esp_err_t esp_hidh_dev_feature_get(esp_hidh_dev_t *dev, size_t map_index, size_t report_id, size_t max_len, uint8_t *data, size_t *length);
  183. /**
  184. * @brief Set_Report command.
  185. * @note For now, this function used only for Classic Bluetooth.
  186. *
  187. * @param dev : pointer to the device
  188. * @param map_index : index of the device report map
  189. * @param report_id : id of the HID FEATURE report
  190. * @param report_type : report type, defines in `esp_hid_common.h`
  191. * @param data : pointer to the data to send
  192. * @param length : length of the data to send
  193. *
  194. * @return: ESP_OK on success
  195. */
  196. esp_err_t esp_hidh_dev_set_report(esp_hidh_dev_t *dev, size_t map_index, size_t report_id, int report_type,
  197. uint8_t *data, size_t length);
  198. /**
  199. * @brief Get_Report command.
  200. * @note For now, this function used only for Classic Bluetooth.
  201. *
  202. * @param dev : pointer to the device
  203. * @param map_index : index of the device report map
  204. * @param report_id : id of the HID FEATURE report
  205. * @param report_type : report type, defines in `esp_hid_common.h`
  206. * @param max_len : size of the buffer that will hold the data
  207. *
  208. * @return: ESP_OK on success
  209. */
  210. esp_err_t esp_hidh_dev_get_report(esp_hidh_dev_t *dev, size_t map_index, size_t report_id, int report_type,
  211. size_t max_len);
  212. /**
  213. * @brief Get_Idle Command.
  214. * @note For now, this function used only for Classic Bluetooth.
  215. *
  216. * @param dev : pointer to the device
  217. *
  218. * @return: ESP_OK on success
  219. */
  220. esp_err_t esp_hidh_dev_get_idle(esp_hidh_dev_t *dev);
  221. /**
  222. * @brief Set_Idle Command.
  223. * @note For now, this function used only for Classic Bluetooth.
  224. *
  225. * @param dev : pointer to the device
  226. * @param idle_time : idle_time
  227. *
  228. * @return: ESP_OK on success
  229. */
  230. esp_err_t esp_hidh_dev_set_idle(esp_hidh_dev_t *dev, uint8_t idle_time);
  231. /**
  232. * @brief Get_Protocol Command.
  233. * @note For now, this function used only for Classic Bluetooth.
  234. *
  235. * @param dev : pointer to the device
  236. *
  237. * @return: ESP_OK on success
  238. */
  239. esp_err_t esp_hidh_dev_get_protocol(esp_hidh_dev_t *dev);
  240. /**
  241. * @brief Set_Protocol Command.
  242. * @note For now, this function used only for Classic Bluetooth.
  243. *
  244. * @param dev : pointer to the device
  245. * @param protocol_mode : protocol_mode
  246. *
  247. * @return: ESP_OK on success
  248. */
  249. esp_err_t esp_hidh_dev_set_protocol(esp_hidh_dev_t *dev, uint8_t protocol_mode);
  250. /**
  251. * @brief Dump the properties of HID Device to UART
  252. * @param dev : pointer to the HID Device
  253. * @param fp : pointer to the output file
  254. */
  255. void esp_hidh_dev_dump(esp_hidh_dev_t *dev, FILE *fp);
  256. /**
  257. * @brief Get the BT Device Address of a HID Device
  258. * @param dev : pointer to the HID Device
  259. *
  260. * @return: pointer to the BDA byte array or NULL
  261. */
  262. const uint8_t *esp_hidh_dev_bda_get(esp_hidh_dev_t *dev);
  263. /**
  264. * @brief Get the HID Device Transport
  265. * @param dev : pointer to the HID Device
  266. *
  267. * @return: the transport of the connected device or ESP_HID_TRANSPORT_MAX
  268. */
  269. esp_hid_transport_t esp_hidh_dev_transport_get(esp_hidh_dev_t *dev);
  270. /**
  271. * @brief Get the HID Device Cofiguration
  272. * @param dev : pointer to the HID Device
  273. *
  274. * @return: pointer to the config structure or NULL
  275. */
  276. const esp_hid_device_config_t *esp_hidh_dev_config_get(esp_hidh_dev_t *dev);
  277. /**
  278. * @brief Get the name of a HID Device
  279. * @param dev : pointer to the HID Device
  280. *
  281. * @return: pointer to the character array or NULL
  282. */
  283. const char *esp_hidh_dev_name_get(esp_hidh_dev_t *dev);
  284. /**
  285. * @brief Get the manufacturer of a HID Device
  286. * @param dev : pointer to the HID Device
  287. *
  288. * @return: pointer to the character array
  289. */
  290. const char *esp_hidh_dev_manufacturer_get(esp_hidh_dev_t *dev);
  291. /**
  292. * @brief Get the serial number of a HID Device
  293. * @param dev : pointer to the HID Device
  294. *
  295. * @return: pointer to the character array or NULL
  296. */
  297. const char *esp_hidh_dev_serial_get(esp_hidh_dev_t *dev);
  298. /**
  299. * @brief Get the VID of a HID Device
  300. * @param dev : pointer to the HID Device
  301. *
  302. * @return: the VID value
  303. */
  304. uint16_t esp_hidh_dev_vendor_id_get(esp_hidh_dev_t *dev);
  305. /**
  306. * @brief Get the PID of a HID Device
  307. * @param dev : pointer to the HID Device
  308. *
  309. * @return: the PID value
  310. */
  311. uint16_t esp_hidh_dev_product_id_get(esp_hidh_dev_t *dev);
  312. /**
  313. * @brief Get the version HID Device
  314. * @param dev : pointer to the HID Device
  315. *
  316. * @return: the version value
  317. */
  318. uint16_t esp_hidh_dev_version_get(esp_hidh_dev_t *dev);
  319. /**
  320. * @brief Get the appearance of BLE HID Device
  321. * @param dev : pointer to the BLE HID Device
  322. *
  323. * @return: the appearance value
  324. */
  325. uint16_t esp_hidh_dev_appearance_get(esp_hidh_dev_t *dev); //BLE Only
  326. /**
  327. * @brief Get the calculated HID Device usage type
  328. * @param dev : pointer to the HID Device
  329. *
  330. * @return: the hid usage type
  331. */
  332. esp_hid_usage_t esp_hidh_dev_usage_get(esp_hidh_dev_t *dev);
  333. /**
  334. * @brief Get an array of all reports found on a device
  335. * @param dev : pointer to the device
  336. * @param num_reports : pointer to the value that will be set to the number of reports
  337. * @param reports : location to set to the pointer of the reports array
  338. *
  339. * @return: ESP_OK on success
  340. */
  341. esp_err_t esp_hidh_dev_reports_get(esp_hidh_dev_t *dev, size_t *num_reports, esp_hid_report_item_t **reports);
  342. /**
  343. * @brief Get an array of the report maps found on a device
  344. * @param dev : pointer to the device
  345. * @param num_maps : pointer to the value that will be set to the number of report maps found
  346. * @param maps : location to set to the pointer of the report maps array
  347. *
  348. * @return: ESP_OK on success
  349. */
  350. esp_err_t esp_hidh_dev_report_maps_get(esp_hidh_dev_t *dev, size_t *num_maps, esp_hid_raw_report_map_t **maps);
  351. #include "esp_hidh_transport.h"
  352. #ifdef __cplusplus
  353. }
  354. #endif