esp_hidh.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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_MAX_EVENT, /*!< HID events end marker */
  39. } esp_hidh_event_t;
  40. /**
  41. * @brief HIDH callback parameters union
  42. */
  43. typedef union {
  44. /**
  45. * @brief ESP_HIDH_OPEN_EVENT
  46. */
  47. struct {
  48. esp_hidh_dev_t *dev; /*!< HID Remote bluetooth device */
  49. } open; /*!< HID callback param of ESP_HIDH_OPEN_EVENT */
  50. /**
  51. * @brief ESP_HIDH_CLOSE_EVENT
  52. */
  53. struct {
  54. esp_hidh_dev_t *dev; /*!< HID Remote bluetooth device. */
  55. int reason; /*!< Reason why the connection was closed. BLE Only */
  56. } close; /*!< HID callback param of ESP_HIDH_CLOSE_EVENT */
  57. /**
  58. * @brief ESP_HIDH_BATTERY_EVENT
  59. */
  60. struct {
  61. esp_hidh_dev_t *dev; /*!< HID Remote bluetooth device */
  62. uint8_t level; /*!< Battery Level (0-100%) */
  63. } battery; /*!< HID callback param of ESP_HIDH_BATTERY_EVENT */
  64. /**
  65. * @brief ESP_HIDH_INPUT_EVENT
  66. */
  67. struct {
  68. esp_hidh_dev_t *dev; /*!< HID Remote bluetooth device */
  69. esp_hid_usage_t usage; /*!< HID report usage */
  70. uint16_t report_id; /*!< HID report index */
  71. uint16_t length; /*!< HID data length */
  72. uint8_t *data; /*!< The pointer to the HID data */
  73. uint8_t map_index; /*!< HID report map index */
  74. } input; /*!< HID callback param of ESP_HIDH_INPUT_EVENT */
  75. /**
  76. * @brief ESP_HIDH_FEATURE_EVENT
  77. */
  78. struct {
  79. esp_hidh_dev_t *dev; /*!< HID Remote bluetooth device */
  80. esp_hid_usage_t usage; /*!< HID report usage */
  81. uint16_t report_id; /*!< HID report index */
  82. uint16_t length; /*!< HID data length */
  83. uint8_t *data; /*!< The pointer to the HID data */
  84. uint8_t map_index; /*!< HID report map index */
  85. } feature; /*!< HID callback param of ESP_HIDH_FEATURE_EVENT */
  86. } esp_hidh_event_data_t;
  87. typedef struct {
  88. esp_event_handler_t callback;
  89. } esp_hidh_config_t;
  90. /**
  91. * @brief Initialize the HID Host component
  92. * @param config : pointer to esp_hidh_config_t structure
  93. *
  94. * @return: ESP_OK on success
  95. */
  96. esp_err_t esp_hidh_init(const esp_hidh_config_t *config);
  97. /**
  98. * @brief De-initialize the HID Host component
  99. *
  100. * @return: ESP_OK on success
  101. */
  102. esp_err_t esp_hidh_deinit(void);
  103. /**
  104. * @brief Close HID Device
  105. * @param dev : pointer to the device
  106. *
  107. * @return: ESP_OK on success
  108. */
  109. esp_err_t esp_hidh_dev_close(esp_hidh_dev_t *dev);
  110. /**
  111. * @brief Free HID Device Memory
  112. * This function MUST be called when handling ESP_HIDH_CLOSE_EVENT
  113. * Only then all memory used for the device will be freed.
  114. * @param dev : pointer to the device
  115. *
  116. * @return: ESP_OK on success
  117. */
  118. esp_err_t esp_hidh_dev_free(esp_hidh_dev_t *dev);
  119. /**
  120. * @brief Send an OUTPUT report to the device
  121. * @param dev : pointer to the device
  122. * @param map_index : index of the device report map
  123. * @param report_id : id of the HID OUTPUT report
  124. * @param data : pointer to the data to send
  125. * @param length : length of the data to send
  126. *
  127. * @return: ESP_OK on success
  128. */
  129. 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);
  130. /**
  131. * @brief Send a FEATURE report to the device
  132. * @param dev : pointer to the device
  133. * @param map_index : index of the device report map
  134. * @param report_id : id of the HID FEATURE report
  135. * @param data : pointer to the data to send
  136. * @param length : length of the data to send
  137. *
  138. * @return: ESP_OK on success
  139. */
  140. 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);
  141. /**
  142. * @brief Get the value a FEATURE report from the device
  143. * @param dev : pointer to the device
  144. * @param map_index : index of the device report map
  145. * @param report_id : id of the HID FEATURE report
  146. * @param max_len : size of the buffer that will hold the data
  147. * @param data : pointer to the data buffer
  148. * @param length : pointer to the value that will be set to the number of bytes received
  149. *
  150. * @return: ESP_OK on success
  151. */
  152. 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);
  153. /**
  154. * @brief Dump the properties of HID Device to UART
  155. * @param dev : pointer to the HID Device
  156. * @param fp : pointer to the output file
  157. */
  158. void esp_hidh_dev_dump(esp_hidh_dev_t *dev, FILE *fp);
  159. /**
  160. * @brief Get the BT Device Address of a HID Device
  161. * @param dev : pointer to the HID Device
  162. *
  163. * @return: pointer to the BDA byte array or NULL
  164. */
  165. const uint8_t *esp_hidh_dev_bda_get(esp_hidh_dev_t *dev);
  166. /**
  167. * @brief Get the HID Device Transport
  168. * @param dev : pointer to the HID Device
  169. *
  170. * @return: the transport of the connected device or ESP_HID_TRANSPORT_MAX
  171. */
  172. esp_hid_transport_t esp_hidh_dev_transport_get(esp_hidh_dev_t *dev);
  173. /**
  174. * @brief Get the HID Device Cofiguration
  175. * @param dev : pointer to the HID Device
  176. *
  177. * @return: pointer to the config structure or NULL
  178. */
  179. const esp_hid_device_config_t *esp_hidh_dev_config_get(esp_hidh_dev_t *dev);
  180. /**
  181. * @brief Get the name of a HID Device
  182. * @param dev : pointer to the HID Device
  183. *
  184. * @return: pointer to the character array or NULL
  185. */
  186. const char *esp_hidh_dev_name_get(esp_hidh_dev_t *dev);
  187. /**
  188. * @brief Get the manufacturer of a HID Device
  189. * @param dev : pointer to the HID Device
  190. *
  191. * @return: pointer to the character array
  192. */
  193. const char *esp_hidh_dev_manufacturer_get(esp_hidh_dev_t *dev);
  194. /**
  195. * @brief Get the serial number of a HID Device
  196. * @param dev : pointer to the HID Device
  197. *
  198. * @return: pointer to the character array or NULL
  199. */
  200. const char *esp_hidh_dev_serial_get(esp_hidh_dev_t *dev);
  201. /**
  202. * @brief Get the VID of a HID Device
  203. * @param dev : pointer to the HID Device
  204. *
  205. * @return: the VID value
  206. */
  207. uint16_t esp_hidh_dev_vendor_id_get(esp_hidh_dev_t *dev);
  208. /**
  209. * @brief Get the PID of a HID Device
  210. * @param dev : pointer to the HID Device
  211. *
  212. * @return: the PID value
  213. */
  214. uint16_t esp_hidh_dev_product_id_get(esp_hidh_dev_t *dev);
  215. /**
  216. * @brief Get the version HID Device
  217. * @param dev : pointer to the HID Device
  218. *
  219. * @return: the version value
  220. */
  221. uint16_t esp_hidh_dev_version_get(esp_hidh_dev_t *dev);
  222. /**
  223. * @brief Get the appearance of BLE HID Device
  224. * @param dev : pointer to the BLE HID Device
  225. *
  226. * @return: the appearance value
  227. */
  228. uint16_t esp_hidh_dev_appearance_get(esp_hidh_dev_t *dev); //BLE Only
  229. /**
  230. * @brief Get the calculated HID Device usage type
  231. * @param dev : pointer to the HID Device
  232. *
  233. * @return: the hid usage type
  234. */
  235. esp_hid_usage_t esp_hidh_dev_usage_get(esp_hidh_dev_t *dev);
  236. /**
  237. * @brief Get an array of all reports found on a device
  238. * @param dev : pointer to the device
  239. * @param num_reports : pointer to the value that will be set to the number of reports
  240. * @param reports : location to set to the pointer of the reports array
  241. *
  242. * @return: ESP_OK on success
  243. */
  244. esp_err_t esp_hidh_dev_reports_get(esp_hidh_dev_t *dev, size_t *num_reports, esp_hid_report_item_t **reports);
  245. /**
  246. * @brief Get an array of the report maps found on a device
  247. * @param dev : pointer to the device
  248. * @param num_maps : pointer to the value that will be set to the number of report maps found
  249. * @param maps : location to set to the pointer of the report maps array
  250. *
  251. * @return: ESP_OK on success
  252. */
  253. 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);
  254. #include "esp_hidh_transport.h"
  255. #ifdef __cplusplus
  256. }
  257. #endif