esp_hid_common.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. #include <stdint.h>
  18. #include <stddef.h>
  19. #include <stdbool.h>
  20. #include <stdio.h>
  21. /* HID Report Map Values */
  22. #define HID_RM_INPUT 0x80
  23. #define HID_RM_OUTPUT 0x90
  24. #define HID_RM_FEATURE 0xb0
  25. #define HID_RM_COLLECTION 0xa0
  26. #define HID_RM_END_COLLECTION 0xc0
  27. #define HID_RM_USAGE_PAGE 0x04
  28. #define HID_RM_LOGICAL_MINIMUM 0x14
  29. #define HID_RM_LOGICAL_MAXIMUM 0x24
  30. #define HID_RM_PHYSICAL_MINIMUM 0x34
  31. #define HID_RM_PHYSICAL_MAXIMUM 0x44
  32. #define HID_RM_UNIT_EXPONENT 0x54
  33. #define HID_RM_UNIT 0x64
  34. #define HID_RM_REPORT_SIZE 0x74
  35. #define HID_RM_REPORT_ID 0x84
  36. #define HID_RM_REPORT_COUNT 0x94
  37. #define HID_RM_PUSH 0xa4
  38. #define HID_RM_POP 0xb4
  39. #define HID_RM_USAGE 0x08
  40. #define HID_RM_USAGE_MINIMUM 0x18
  41. #define HID_RM_USAGE_MAXIMUM 0x28
  42. #define HID_RM_DESIGNATOR_INDEX 0x38
  43. #define HID_RM_DESIGNATOR_MINIMUM 0x48
  44. #define HID_RM_DESIGNATOR_MAXIMUM 0x58
  45. #define HID_RM_STRING_INDEX 0x78
  46. #define HID_RM_STRING_MINIMUM 0x88
  47. #define HID_RM_STRING_MAXIMUM 0x98
  48. #define HID_RM_DELIMITER 0xa8
  49. /* HID Usage Pages and Usages */
  50. #define HID_USAGE_PAGE_GENERIC_DESKTOP 0x01
  51. #define HID_USAGE_KEYBOARD 0x06
  52. #define HID_USAGE_MOUSE 0x02
  53. #define HID_USAGE_JOYSTICK 0x04
  54. #define HID_USAGE_GAMEPAD 0x05
  55. #define HID_USAGE_PAGE_CONSUMER_DEVICE 0x0C
  56. #define HID_USAGE_CONSUMER_CONTROL 0x01
  57. /* HID BT COD Peripheral Min Values Main Role */
  58. #define ESP_HID_COD_MIN_KEYBOARD 0x10
  59. #define ESP_HID_COD_MIN_MOUSE 0x20
  60. /* HID BLE Appearances */
  61. #define ESP_HID_APPEARANCE_GENERIC 0x03C0
  62. #define ESP_HID_APPEARANCE_KEYBOARD 0x03C1
  63. #define ESP_HID_APPEARANCE_MOUSE 0x03C2
  64. #define ESP_HID_APPEARANCE_JOYSTICK 0x03C3
  65. #define ESP_HID_APPEARANCE_GAMEPAD 0x03C4
  66. /* HID Report Types */
  67. #define ESP_HID_REPORT_TYPE_INPUT 1
  68. #define ESP_HID_REPORT_TYPE_OUTPUT 2
  69. #define ESP_HID_REPORT_TYPE_FEATURE 3
  70. /* HID Protocol Modes */
  71. #define ESP_HID_PROTOCOL_MODE_BOOT 0x00 // Boot Protocol Mode
  72. #define ESP_HID_PROTOCOL_MODE_REPORT 0x01 // Report Protocol Mode
  73. /* HID information flags */
  74. #define ESP_HID_FLAGS_REMOTE_WAKE 0x01 // RemoteWake
  75. #define ESP_HID_FLAGS_NORMALLY_CONNECTABLE 0x02 // NormallyConnectable
  76. /* Control point commands */
  77. #define ESP_HID_CONTROL_SUSPEND 0x00 // Suspend
  78. #define ESP_HID_CONTROL_EXIT_SUSPEND 0x01 // Exit Suspend
  79. /* Client Characteristic Configuration values */
  80. #define ESP_HID_CCC_NOTIFICATIONS_ENABLED 0x01 // Notifications enabled
  81. #define ESP_HID_CCC_INDICATIONS_ENABLED 0x02 // Indications enabled
  82. /* HID Transports */
  83. typedef enum {
  84. ESP_HID_TRANSPORT_BT,
  85. ESP_HID_TRANSPORT_BLE,
  86. ESP_HID_TRANSPORT_USB,
  87. ESP_HID_TRANSPORT_MAX
  88. } esp_hid_transport_t;
  89. /* HID Usage Types */
  90. typedef enum {
  91. ESP_HID_USAGE_GENERIC = 0,
  92. ESP_HID_USAGE_KEYBOARD = 1,
  93. ESP_HID_USAGE_MOUSE = 2,
  94. ESP_HID_USAGE_JOYSTICK = 4,
  95. ESP_HID_USAGE_GAMEPAD = 8,
  96. ESP_HID_USAGE_TABLET = 16,
  97. ESP_HID_USAGE_CCONTROL = 32,
  98. ESP_HID_USAGE_VENDOR = 64
  99. } esp_hid_usage_t;
  100. /* HID BT COD Peripheral Min Values. Mask of (keyboard|mouse|ESP_HIDH_COD_*) */
  101. typedef enum {
  102. ESP_HID_COD_MIN_GENERIC,
  103. ESP_HID_COD_MIN_JOYSTICK,
  104. ESP_HID_COD_MIN_GAMEPAD,
  105. ESP_HID_COD_MIN_REMOTE,
  106. ESP_HID_COD_MIN_SENSOR,
  107. ESP_HID_COD_MIN_TABLET,
  108. ESP_HID_COD_MIN_CARD_READER,
  109. ESP_HID_COD_MIN_MAX
  110. } esp_hid_cod_min_t;
  111. /* HID transaction Types */
  112. typedef enum {
  113. ESP_HID_TRANS_HANDSHAKE = 0,
  114. ESP_HID_TRANS_CONTROL = 1,
  115. ESP_HID_TRANS_GET_REPORT = 4,
  116. ESP_HID_TRANS_SET_REPORT = 5,
  117. ESP_HID_TRANS_GET_PROTOCOL = 6,
  118. ESP_HID_TRANS_SET_PROTOCOL = 7,
  119. ESP_HID_TRANS_GET_IDLE = 8,
  120. ESP_HID_TRANS_SET_IDLE = 9,
  121. ESP_HID_TRANS_DATA = 10,
  122. ESP_HID_TRANS_DATAC = 11,
  123. ESP_HID_TRANS_MAX
  124. } esp_hid_trans_type_t;
  125. /**
  126. * @brief HID report item structure
  127. */
  128. typedef struct {
  129. uint8_t map_index; /*!< HID report map index */
  130. uint8_t report_id; /*!< HID report id */
  131. uint8_t report_type; /*!< HID report type */
  132. uint8_t protocol_mode; /*!< HID protocol mode */
  133. esp_hid_usage_t usage; /*!< HID usage type */
  134. uint16_t value_len; /*!< HID report length in bytes */
  135. } esp_hid_report_item_t;
  136. /**
  137. * @brief HID parsed report map structure
  138. */
  139. typedef struct {
  140. esp_hid_usage_t usage; /*!< Dominant HID usage. (keyboard > mouse > joystick > gamepad > generic) */
  141. uint16_t appearance; /*!< Calculated HID Appearance based on the dominant usage */
  142. uint8_t reports_len; /*!< Number of reports discovered in the report map */
  143. esp_hid_report_item_t *reports; /*!< Reports discovered in the report map */
  144. } esp_hid_report_map_t;
  145. /**
  146. * @brief HID raw report map structure
  147. */
  148. typedef struct {
  149. const uint8_t *data; /*!< Pointer to the HID report map data */
  150. uint16_t len; /*!< HID report map data length */
  151. } esp_hid_raw_report_map_t;
  152. /**
  153. * @brief HID device config structure
  154. */
  155. typedef struct {
  156. uint16_t vendor_id; /*!< HID Vendor ID */
  157. uint16_t product_id; /*!< HID Product ID */
  158. uint16_t version; /*!< HID Product Version */
  159. const char *device_name; /*!< HID Device Name */
  160. const char *manufacturer_name; /*!< HID Manufacturer */
  161. const char *serial_number; /*!< HID Serial Number */
  162. esp_hid_raw_report_map_t *report_maps; /*!< Array of the raw HID report maps */
  163. uint8_t report_maps_len; /*!< number of raw report maps in the array */
  164. } esp_hid_device_config_t;
  165. /*
  166. * @brief Parse RAW HID report map
  167. * It is a responsibility of the user to free the parsed report map,
  168. * when it's no longer needed. Use esp_hid_free_report_map
  169. * @param hid_rm : pointer to the hid report map data
  170. * @param hid_rm_len : length to the hid report map data
  171. *
  172. * @return: pointer to the parsed report map
  173. */
  174. esp_hid_report_map_t *esp_hid_parse_report_map(const uint8_t *hid_rm, size_t hid_rm_len);
  175. /*
  176. * @brief Free parsed HID report map
  177. * @param map : pointer to the parsed hid report map
  178. */
  179. void esp_hid_free_report_map(esp_hid_report_map_t *map);
  180. /**
  181. * @brief Calculate the HID Device usage type from the BLE Apperance
  182. * @param appearance : BLE Apperance value
  183. *
  184. * @return: the hid usage type
  185. */
  186. esp_hid_usage_t esp_hid_usage_from_appearance(uint16_t appearance);
  187. /**
  188. * @brief Calculate the HID Device usage type from the BT CoD
  189. * @param cod : BT CoD value
  190. *
  191. * @return: the hid usage type
  192. */
  193. esp_hid_usage_t esp_hid_usage_from_cod(uint32_t cod);
  194. /**
  195. * @brief Convert device usage type to string
  196. * @param usage : The HID usage type to convert
  197. *
  198. * @return: a pointer to the string or NULL
  199. */
  200. const char *esp_hid_usage_str(esp_hid_usage_t usage);
  201. /**
  202. * @brief Convert HID protocol mode to string
  203. * @param protocol_mode : The HID protocol mode to convert
  204. * BOOT/REPORT
  205. *
  206. * @return: a pointer to the string or NULL
  207. */
  208. const char *esp_hid_protocol_mode_str(uint8_t protocol_mode);
  209. /**
  210. * @brief Convert HID report type to string
  211. * @param report_type : The HID report type to convert
  212. * INPUT/OUTPUT/FEATURE
  213. *
  214. * @return: a pointer to the string or NULL
  215. */
  216. const char *esp_hid_report_type_str(uint8_t report_type);
  217. /**
  218. * @brief Convert BT CoD major to string
  219. * @param cod_major : The CoD major value to convert
  220. *
  221. * @return: a pointer to the string or NULL
  222. */
  223. const char *esp_hid_cod_major_str(uint8_t cod_major);
  224. /**
  225. * @brief Print BT CoD minor value
  226. * @param cod_min : The CoD minor value to print
  227. * @param fp : pointer to the output file
  228. */
  229. void esp_hid_cod_minor_print(uint8_t cod_min, FILE *fp);
  230. /**
  231. * @brief Convert BLE disconnect reason to string
  232. * @param reason : The value of the reason
  233. *
  234. * @return: a pointer to the string or NULL
  235. */
  236. const char *esp_hid_disconnect_reason_str(esp_hid_transport_t transport, int reason);
  237. #ifdef __cplusplus
  238. }
  239. #endif