esp_hidd.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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_CONNECT_EVENT
  49. */
  50. struct {
  51. esp_hidd_dev_t *dev; /*!< HID device structure */
  52. } connect; /*!< HID callback param of ESP_HIDD_CONNECT_EVENT */
  53. /**
  54. * @brief ESP_HIDD_DISCONNECT_EVENT
  55. */
  56. struct {
  57. esp_hidd_dev_t *dev; /*!< HID device structure */
  58. int reason; /*!< Indicate the reason of disconnection */
  59. } disconnect; /*!< HID callback param of ESP_HIDD_DISCONNECT_EVENT */
  60. /**
  61. * @brief ESP_HIDD_OUTPUT_EVENT
  62. */
  63. struct {
  64. esp_hidd_dev_t *dev; /*!< HID device structure */
  65. esp_hid_usage_t usage; /*!< HID report usage */
  66. uint16_t report_id; /*!< HID report index */
  67. uint16_t length; /*!< data length */
  68. uint8_t *data; /*!< The pointer to the data */
  69. uint8_t map_index; /*!< HID config report map index */
  70. } output; /*!< HID callback param of ESP_HIDD_OUTPUT_EVENT */
  71. /**
  72. * @brief ESP_HIDD_FEATURE_EVENT
  73. */
  74. struct {
  75. esp_hidd_dev_t *dev; /*!< HID device structure */
  76. esp_hid_usage_t usage; /*!< HID report usage */
  77. uint16_t report_id; /*!< HID report index */
  78. uint16_t length; /*!< data length */
  79. uint8_t *data; /*!< The pointer to the data */
  80. uint8_t map_index; /*!< HID config report map index */
  81. } feature; /*!< HID callback param of ESP_HIDD_FEATURE_EVENT */
  82. /**
  83. * @brief ESP_HIDD_PROTOCOL_MODE_EVENT
  84. */
  85. struct {
  86. esp_hidd_dev_t *dev; /*!< HID device structure */
  87. uint8_t protocol_mode; /*!< HID Protocol Mode */
  88. uint8_t map_index; /*!< HID config report map index */
  89. } protocol_mode; /*!< HID callback param of ESP_HIDD_PROTOCOL_MODE_EVENT */
  90. /**
  91. * @brief ESP_HIDD_CONTROL_EVENT
  92. */
  93. struct {
  94. esp_hidd_dev_t *dev; /*!< HID device structure */
  95. uint8_t control; /*!< HID Control Point */
  96. uint8_t map_index; /*!< HID config report map index */
  97. } control; /*!< HID callback param of ESP_HIDD_CONTROL_EVENT */
  98. } esp_hidd_event_data_t;
  99. /**
  100. * @brief Init HID Device
  101. * @param config : configuration for the device
  102. * @param transport: protocol that the device will use (ESP_HID_TRANSPORT_BLE/BT/USB)
  103. * @param callback : function to call when events for this device are generated.
  104. * Can be NULL, but will miss the START event.
  105. * @param[out] dev : location to return the pointer to the device structure
  106. *
  107. * @return: ESP_OK on success
  108. */
  109. 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);
  110. /**
  111. * @brief Deinit HID Device
  112. * @param dev : pointer to the device to deinit
  113. *
  114. * @return: ESP_OK on success
  115. */
  116. esp_err_t esp_hidd_dev_deinit(esp_hidd_dev_t *dev);
  117. /**
  118. * @brief Get the HID Device Transport
  119. * @param dev : pointer to the HID Device
  120. *
  121. * @return: the transport of the connected device or ESP_HID_TRANSPORT_MAX
  122. */
  123. esp_hid_transport_t esp_hidd_dev_transport_get(esp_hidd_dev_t *dev);
  124. /**
  125. * @brief Check if HID Device is connected
  126. * @param dev : pointer to the device
  127. *
  128. * @return: true if the device is connected
  129. */
  130. bool esp_hidd_dev_connected(esp_hidd_dev_t *dev);
  131. /**
  132. * @brief Set the battery level reported by the HID Device
  133. * @param dev : pointer to the device
  134. * @param level : battery level (0-100)
  135. *
  136. * @return: ESP_OK on success
  137. */
  138. esp_err_t esp_hidd_dev_battery_set(esp_hidd_dev_t *dev, uint8_t level);
  139. /**
  140. * @brief Send an INPUT report to the host
  141. * @param dev : pointer to the device
  142. * @param map_index : index of the device report map in the init config
  143. * @param report_id : id of the HID INPUT report
  144. * @param data : pointer to the data to send
  145. * @param length : length of the data to send
  146. *
  147. * @return: ESP_OK on success
  148. */
  149. 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);
  150. /**
  151. * @brief Send a FEATURE report to the host
  152. * @param dev : pointer to the device
  153. * @param map_index : index of the device report map in the init config
  154. * @param report_id : id of the HID FEATURE report
  155. * @param data : pointer to the data to send
  156. * @param length : length of the data to send
  157. *
  158. * @return: ESP_OK on success
  159. */
  160. 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);
  161. /**
  162. * @brief Register function to listen for device events
  163. * @param dev : pointer to the device
  164. * @param callback : event handler function
  165. * @param event : event to listen for (ESP_HIDD_ANY_EVENT for all)
  166. *
  167. * @return: ESP_OK on success
  168. */
  169. esp_err_t esp_hidd_dev_event_handler_register(esp_hidd_dev_t *dev, esp_event_handler_t callback, esp_hidd_event_t event);
  170. /**
  171. * @brief Unregister function that is listening for device events
  172. * @param dev : pointer to the device
  173. * @param callback : event handler function
  174. * @param event : event that is listening for (ESP_HIDD_ANY_EVENT for all)
  175. *
  176. * @return: ESP_OK on success
  177. */
  178. esp_err_t esp_hidd_dev_event_handler_unregister(esp_hidd_dev_t *dev, esp_event_handler_t callback, esp_hidd_event_t event);
  179. #ifdef __cplusplus
  180. }
  181. #endif