esp_hidd.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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_event.h"
  17. #include "esp_hid_common.h"
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. #include "esp_hidd_transport.h"
  22. ESP_EVENT_DECLARE_BASE(ESP_HIDD_EVENTS); // Declare the event base for HID device
  23. /**
  24. * @brief HIDD callback events enum
  25. */
  26. typedef enum {
  27. ESP_HIDD_ANY_EVENT = ESP_EVENT_ANY_ID, /*!< HID device any event */
  28. ESP_HIDD_START_EVENT = 0, /*!< HID device stack started */
  29. ESP_HIDD_CONNECT_EVENT, /*!< HID device connected */
  30. ESP_HIDD_PROTOCOL_MODE_EVENT, /*!< HID device protocol mode change */
  31. ESP_HIDD_CONTROL_EVENT, /*!< HID device control request */
  32. ESP_HIDD_OUTPUT_EVENT, /*!< HID device output report event */
  33. ESP_HIDD_FEATURE_EVENT, /*!< HID device feature report event */
  34. ESP_HIDD_DISCONNECT_EVENT, /*!< HID device disconnected */
  35. ESP_HIDD_STOP_EVENT, /*!< HID device stack stopped */
  36. ESP_HIDD_MAX_EVENT, /*!< HID events end marker */
  37. } esp_hidd_event_t;
  38. /**
  39. * @brief HIDD structure forward declaration
  40. */
  41. struct esp_hidd_dev_s;
  42. typedef struct esp_hidd_dev_s esp_hidd_dev_t;
  43. /**
  44. * @brief HIDD callback parameters union
  45. */
  46. typedef union {
  47. /**
  48. * @brief ESP_HIDD_START_EVENT
  49. * @note Used only for Classic Bluetooth.
  50. */
  51. struct {
  52. esp_err_t status; /*!< HID device operation status */
  53. } start; /*!< HID callback param of ESP_HIDD_START_EVENT */
  54. /**
  55. * @brief ESP_HIDD_STOP_EVENT
  56. * @note Used only for Classic Bluetooth.
  57. */
  58. struct {
  59. esp_err_t status; /*!< HID device operation status */
  60. } stop; /*!< HID callback param of ESP_HIDD_STOP_EVENT */
  61. /**
  62. * @brief ESP_HIDD_CONNECT_EVENT
  63. */
  64. struct {
  65. esp_hidd_dev_t *dev; /*!< HID device structure */
  66. esp_err_t status; /*!< HID device operation status, used only for Classic Bluetooth */
  67. } connect; /*!< HID callback param of ESP_HIDD_CONNECT_EVENT */
  68. /**
  69. * @brief ESP_HIDD_DISCONNECT_EVENT
  70. */
  71. struct {
  72. esp_hidd_dev_t *dev; /*!< HID device structure */
  73. int reason; /*!< Indicate the reason of disconnection */
  74. esp_err_t status; /*!< HID device operation status, used only for Classic Bluetooth */
  75. } disconnect; /*!< HID callback param of ESP_HIDD_DISCONNECT_EVENT */
  76. /**
  77. * @brief ESP_HIDD_OUTPUT_EVENT
  78. */
  79. struct {
  80. esp_hidd_dev_t *dev; /*!< HID device structure */
  81. esp_hid_usage_t usage; /*!< HID report usage */
  82. uint16_t report_id; /*!< HID report index */
  83. uint16_t length; /*!< data length */
  84. uint8_t *data; /*!< The pointer to the data */
  85. uint8_t map_index; /*!< HID config report map index */
  86. } output; /*!< HID callback param of ESP_HIDD_OUTPUT_EVENT */
  87. /**
  88. * @brief ESP_HIDD_FEATURE_EVENT
  89. */
  90. struct {
  91. esp_hidd_dev_t *dev; /*!< HID device structure */
  92. esp_hid_usage_t usage; /*!< HID report usage */
  93. uint16_t report_id; /*!< HID report index */
  94. uint16_t length; /*!< data length */
  95. uint8_t *data; /*!< The pointer to the data */
  96. uint8_t map_index; /*!< HID config report map index */
  97. uint8_t trans_type; /*!< HID device feature transaction type, used only for Classic Bluetooth */
  98. uint8_t report_type; /*!< HID device feature report type, used only for Classic Bluetooth */
  99. } feature; /*!< HID callback param of ESP_HIDD_FEATURE_EVENT */
  100. /**
  101. * @brief ESP_HIDD_PROTOCOL_MODE_EVENT
  102. */
  103. struct {
  104. esp_hidd_dev_t *dev; /*!< HID device structure */
  105. uint8_t protocol_mode; /*!< HID Protocol Mode */
  106. uint8_t map_index; /*!< HID config report map index */
  107. } protocol_mode; /*!< HID callback param of ESP_HIDD_PROTOCOL_MODE_EVENT */
  108. /**
  109. * @brief ESP_HIDD_CONTROL_EVENT
  110. */
  111. struct {
  112. esp_hidd_dev_t *dev; /*!< HID device structure */
  113. uint8_t control; /*!< HID Control Point */
  114. uint8_t map_index; /*!< HID config report map index */
  115. } control; /*!< HID callback param of ESP_HIDD_CONTROL_EVENT */
  116. } esp_hidd_event_data_t;
  117. /**
  118. * @brief Init HID Device
  119. * @param config : configuration for the device
  120. * @param transport: protocol that the device will use (ESP_HID_TRANSPORT_BLE/BT/USB)
  121. * @param callback : function to call when events for this device are generated.
  122. * Can be NULL, but will miss the START event.
  123. * @param[out] dev : location to return the pointer to the device structure
  124. *
  125. * @return: ESP_OK on success
  126. */
  127. esp_err_t esp_hidd_dev_init(const esp_hid_device_config_t *config, esp_hid_transport_t transport, esp_event_handler_t callback, esp_hidd_dev_t **dev);
  128. /**
  129. * @brief Deinit HID Device
  130. * @param dev : pointer to the device to deinit
  131. *
  132. * @return: ESP_OK on success
  133. */
  134. esp_err_t esp_hidd_dev_deinit(esp_hidd_dev_t *dev);
  135. /**
  136. * @brief Get the HID Device Transport
  137. * @param dev : pointer to the HID Device
  138. *
  139. * @return: the transport of the connected device or ESP_HID_TRANSPORT_MAX
  140. */
  141. esp_hid_transport_t esp_hidd_dev_transport_get(esp_hidd_dev_t *dev);
  142. /**
  143. * @brief Check if HID Device is connected
  144. * @param dev : pointer to the device
  145. *
  146. * @return: true if the device is connected
  147. */
  148. bool esp_hidd_dev_connected(esp_hidd_dev_t *dev);
  149. /**
  150. * @brief Set the battery level reported by the HID Device
  151. * @param dev : pointer to the device
  152. * @param level : battery level (0-100)
  153. *
  154. * @return: ESP_OK on success
  155. */
  156. esp_err_t esp_hidd_dev_battery_set(esp_hidd_dev_t *dev, uint8_t level);
  157. /**
  158. * @brief Send an INPUT report to the host
  159. * @param dev : pointer to the device
  160. * @param map_index : index of the device report map in the init config
  161. * @param report_id : id of the HID INPUT report
  162. * @param data : pointer to the data to send
  163. * @param length : length of the data to send
  164. *
  165. * @return: ESP_OK on success
  166. */
  167. esp_err_t esp_hidd_dev_input_set(esp_hidd_dev_t *dev, size_t map_index, size_t report_id, uint8_t *data, size_t length);
  168. /**
  169. * @brief Send a FEATURE report to the host
  170. * @param dev : pointer to the device
  171. * @param map_index : index of the device report map in the init config
  172. * @param report_id : id of the HID FEATURE report
  173. * @param data : pointer to the data to send
  174. * @param length : length of the data to send
  175. *
  176. * @return: ESP_OK on success
  177. */
  178. esp_err_t esp_hidd_dev_feature_set(esp_hidd_dev_t *dev, size_t map_index, size_t report_id, uint8_t *data, size_t length);
  179. /**
  180. * @brief Register function to listen for device events
  181. * @param dev : pointer to the device
  182. * @param callback : event handler function
  183. * @param event : event to listen for (ESP_HIDD_ANY_EVENT for all)
  184. *
  185. * @return: ESP_OK on success
  186. */
  187. esp_err_t esp_hidd_dev_event_handler_register(esp_hidd_dev_t *dev, esp_event_handler_t callback, esp_hidd_event_t event);
  188. /**
  189. * @brief Unregister function that is listening for device events
  190. * @param dev : pointer to the device
  191. * @param callback : event handler function
  192. * @param event : event that is listening for (ESP_HIDD_ANY_EVENT for all)
  193. *
  194. * @return: ESP_OK on success
  195. */
  196. esp_err_t esp_hidd_dev_event_handler_unregister(esp_hidd_dev_t *dev, esp_event_handler_t callback, esp_hidd_event_t event);
  197. #ifdef __cplusplus
  198. }
  199. #endif