esp_hid_common.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. /**
  112. * @brief HID report item structure
  113. */
  114. typedef struct {
  115. uint8_t map_index; /*!< HID report map index */
  116. uint8_t report_id; /*!< HID report id */
  117. uint8_t report_type; /*!< HID report type */
  118. uint8_t protocol_mode; /*!< HID protocol mode */
  119. esp_hid_usage_t usage; /*!< HID usage type */
  120. uint16_t value_len; /*!< HID report length in bytes */
  121. } esp_hid_report_item_t;
  122. /**
  123. * @brief HID parsed report map structure
  124. */
  125. typedef struct {
  126. esp_hid_usage_t usage; /*!< Dominant HID usage. (keyboard > mouse > joystick > gamepad > generic) */
  127. uint16_t appearance; /*!< Calculated HID Appearance based on the dominant usage */
  128. uint8_t reports_len; /*!< Number of reports discovered in the report map */
  129. esp_hid_report_item_t *reports; /*!< Reports discovered in the report map */
  130. } esp_hid_report_map_t;
  131. /**
  132. * @brief HID raw report map structure
  133. */
  134. typedef struct {
  135. const uint8_t *data; /*!< Pointer to the HID report map data */
  136. uint16_t len; /*!< HID report map data length */
  137. } esp_hid_raw_report_map_t;
  138. /**
  139. * @brief HID device config structure
  140. */
  141. typedef struct {
  142. uint16_t vendor_id; /*!< HID Vendor ID */
  143. uint16_t product_id; /*!< HID Product ID */
  144. uint16_t version; /*!< HID Product Version */
  145. const char *device_name; /*!< HID Device Name */
  146. const char *manufacturer_name; /*!< HID Manufacturer */
  147. const char *serial_number; /*!< HID Serial Number */
  148. esp_hid_raw_report_map_t *report_maps; /*!< Array of the raw HID report maps */
  149. uint8_t report_maps_len; /*!< number of raw report maps in the array */
  150. } esp_hid_device_config_t;
  151. /*
  152. * @brief Parse RAW HID report map
  153. * It is a responsibility of the user to free the parsed report map,
  154. * when it's no longer needed. Use esp_hid_free_report_map
  155. * @param hid_rm : pointer to the hid report map data
  156. * @param hid_rm_len : length to the hid report map data
  157. *
  158. * @return: pointer to the parsed report map
  159. */
  160. esp_hid_report_map_t *esp_hid_parse_report_map(const uint8_t *hid_rm, size_t hid_rm_len);
  161. /*
  162. * @brief Free parsed HID report map
  163. * @param map : pointer to the parsed hid report map
  164. */
  165. void esp_hid_free_report_map(esp_hid_report_map_t *map);
  166. /**
  167. * @brief Calculate the HID Device usage type from the BLE Apperance
  168. * @param appearance : BLE Apperance value
  169. *
  170. * @return: the hid usage type
  171. */
  172. esp_hid_usage_t esp_hid_usage_from_appearance(uint16_t appearance);
  173. /**
  174. * @brief Calculate the HID Device usage type from the BT CoD
  175. * @param cod : BT CoD value
  176. *
  177. * @return: the hid usage type
  178. */
  179. esp_hid_usage_t esp_hid_usage_from_cod(uint32_t cod);
  180. /**
  181. * @brief Convert device usage type to string
  182. * @param usage : The HID usage type to convert
  183. *
  184. * @return: a pointer to the string or NULL
  185. */
  186. const char *esp_hid_usage_str(esp_hid_usage_t usage);
  187. /**
  188. * @brief Convert HID protocol mode to string
  189. * @param protocol_mode : The HID protocol mode to convert
  190. * BOOT/REPORT
  191. *
  192. * @return: a pointer to the string or NULL
  193. */
  194. const char *esp_hid_protocol_mode_str(uint8_t protocol_mode);
  195. /**
  196. * @brief Convert HID report type to string
  197. * @param report_type : The HID report type to convert
  198. * INPUT/OUTPUT/FEATURE
  199. *
  200. * @return: a pointer to the string or NULL
  201. */
  202. const char *esp_hid_report_type_str(uint8_t report_type);
  203. /**
  204. * @brief Convert BT CoD major to string
  205. * @param cod_major : The CoD major value to convert
  206. *
  207. * @return: a pointer to the string or NULL
  208. */
  209. const char *esp_hid_cod_major_str(uint8_t cod_major);
  210. /**
  211. * @brief Print BT CoD minor value
  212. * @param cod_min : The CoD minor value to print
  213. * @param fp : pointer to the output file
  214. */
  215. void esp_hid_cod_minor_print(uint8_t cod_min, FILE *fp);
  216. /**
  217. * @brief Convert BLE disconnect reason to string
  218. * @param reason : The value of the reason
  219. *
  220. * @return: a pointer to the string or NULL
  221. */
  222. const char *esp_hid_disconnect_reason_str(esp_hid_transport_t transport, int reason);
  223. #ifdef __cplusplus
  224. }
  225. #endif