hid_dev.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // Copyright 2017-2018 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. #ifndef HID_DEV_H__
  14. #define HID_DEV_H__
  15. #include "hidd_le_prf_int.h"
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. /* HID Report type */
  20. #define HID_TYPE_INPUT 1
  21. #define HID_TYPE_OUTPUT 2
  22. #define HID_TYPE_FEATURE 3
  23. // HID Keyboard/Keypad Usage IDs (subset of the codes available in the USB HID Usage Tables spec)
  24. #define HID_KEY_RESERVED 0 // No event inidicated
  25. #define HID_KEY_A 4 // Keyboard a and A
  26. #define HID_KEY_B 5 // Keyboard b and B
  27. #define HID_KEY_C 6 // Keyboard c and C
  28. #define HID_KEY_D 7 // Keyboard d and D
  29. #define HID_KEY_E 8 // Keyboard e and E
  30. #define HID_KEY_F 9 // Keyboard f and F
  31. #define HID_KEY_G 10 // Keyboard g and G
  32. #define HID_KEY_H 11 // Keyboard h and H
  33. #define HID_KEY_I 12 // Keyboard i and I
  34. #define HID_KEY_J 13 // Keyboard j and J
  35. #define HID_KEY_K 14 // Keyboard k and K
  36. #define HID_KEY_L 15 // Keyboard l and L
  37. #define HID_KEY_M 16 // Keyboard m and M
  38. #define HID_KEY_N 17 // Keyboard n and N
  39. #define HID_KEY_O 18 // Keyboard o and O
  40. #define HID_KEY_P 19 // Keyboard p and p
  41. #define HID_KEY_Q 20 // Keyboard q and Q
  42. #define HID_KEY_R 21 // Keyboard r and R
  43. #define HID_KEY_S 22 // Keyboard s and S
  44. #define HID_KEY_T 23 // Keyboard t and T
  45. #define HID_KEY_U 24 // Keyboard u and U
  46. #define HID_KEY_V 25 // Keyboard v and V
  47. #define HID_KEY_W 26 // Keyboard w and W
  48. #define HID_KEY_X 27 // Keyboard x and X
  49. #define HID_KEY_Y 28 // Keyboard y and Y
  50. #define HID_KEY_Z 29 // Keyboard z and Z
  51. #define HID_KEY_1 30 // Keyboard 1 and !
  52. #define HID_KEY_2 31 // Keyboard 2 and @
  53. #define HID_KEY_3 32 // Keyboard 3 and #
  54. #define HID_KEY_4 33 // Keyboard 4 and %
  55. #define HID_KEY_5 34 // Keyboard 5 and %
  56. #define HID_KEY_6 35 // Keyboard 6 and ^
  57. #define HID_KEY_7 36 // Keyboard 7 and &
  58. #define HID_KEY_8 37 // Keyboard 8 and *
  59. #define HID_KEY_9 38 // Keyboard 9 and (
  60. #define HID_KEY_0 39 // Keyboard 0 and )
  61. #define HID_KEY_RETURN 40 // Keyboard Return (ENTER)
  62. #define HID_KEY_ESCAPE 41 // Keyboard ESCAPE
  63. #define HID_KEY_DELETE 42 // Keyboard DELETE (Backspace)
  64. #define HID_KEY_TAB 43 // Keyboard Tab
  65. #define HID_KEY_SPACEBAR 44 // Keyboard Spacebar
  66. #define HID_KEY_MINUS 45 // Keyboard - and (underscore)
  67. #define HID_KEY_EQUAL 46 // Keyboard = and +
  68. #define HID_KEY_LEFT_BRKT 47 // Keyboard [ and {
  69. #define HID_KEY_RIGHT_BRKT 48 // Keyboard ] and }
  70. #define HID_KEY_BACK_SLASH 49 // Keyboard \ and |
  71. #define HID_KEY_SEMI_COLON 51 // Keyboard ; and :
  72. #define HID_KEY_SGL_QUOTE 52 // Keyboard ' and "
  73. #define HID_KEY_GRV_ACCENT 53 // Keyboard Grave Accent and Tilde
  74. #define HID_KEY_COMMA 54 // Keyboard , and <
  75. #define HID_KEY_DOT 55 // Keyboard . and >
  76. #define HID_KEY_FWD_SLASH 56 // Keyboard / and ?
  77. #define HID_KEY_CAPS_LOCK 57 // Keyboard Caps Lock
  78. #define HID_KEY_F1 58 // Keyboard F1
  79. #define HID_KEY_F2 59 // Keyboard F2
  80. #define HID_KEY_F3 60 // Keyboard F3
  81. #define HID_KEY_F4 61 // Keyboard F4
  82. #define HID_KEY_F5 62 // Keyboard F5
  83. #define HID_KEY_F6 63 // Keyboard F6
  84. #define HID_KEY_F7 64 // Keyboard F7
  85. #define HID_KEY_F8 65 // Keyboard F8
  86. #define HID_KEY_F9 66 // Keyboard F9
  87. #define HID_KEY_F10 67 // Keyboard F10
  88. #define HID_KEY_F11 68 // Keyboard F11
  89. #define HID_KEY_F12 69 // Keyboard F12
  90. #define HID_KEY_PRNT_SCREEN 70 // Keyboard Print Screen
  91. #define HID_KEY_SCROLL_LOCK 71 // Keyboard Scroll Lock
  92. #define HID_KEY_PAUSE 72 // Keyboard Pause
  93. #define HID_KEY_INSERT 73 // Keyboard Insert
  94. #define HID_KEY_HOME 74 // Keyboard Home
  95. #define HID_KEY_PAGE_UP 75 // Keyboard PageUp
  96. #define HID_KEY_DELETE_FWD 76 // Keyboard Delete Forward
  97. #define HID_KEY_END 77 // Keyboard End
  98. #define HID_KEY_PAGE_DOWN 78 // Keyboard PageDown
  99. #define HID_KEY_RIGHT_ARROW 79 // Keyboard RightArrow
  100. #define HID_KEY_LEFT_ARROW 80 // Keyboard LeftArrow
  101. #define HID_KEY_DOWN_ARROW 81 // Keyboard DownArrow
  102. #define HID_KEY_UP_ARROW 82 // Keyboard UpArrow
  103. #define HID_KEY_NUM_LOCK 83 // Keypad Num Lock and Clear
  104. #define HID_KEY_DIVIDE 84 // Keypad /
  105. #define HID_KEY_MULTIPLY 85 // Keypad *
  106. #define HID_KEY_SUBTRACT 86 // Keypad -
  107. #define HID_KEY_ADD 87 // Keypad +
  108. #define HID_KEY_ENTER 88 // Keypad ENTER
  109. #define HID_KEYPAD_1 89 // Keypad 1 and End
  110. #define HID_KEYPAD_2 90 // Keypad 2 and Down Arrow
  111. #define HID_KEYPAD_3 91 // Keypad 3 and PageDn
  112. #define HID_KEYPAD_4 92 // Keypad 4 and Lfet Arrow
  113. #define HID_KEYPAD_5 93 // Keypad 5
  114. #define HID_KEYPAD_6 94 // Keypad 6 and Right Arrow
  115. #define HID_KEYPAD_7 95 // Keypad 7 and Home
  116. #define HID_KEYPAD_8 96 // Keypad 8 and Up Arrow
  117. #define HID_KEYPAD_9 97 // Keypad 9 and PageUp
  118. #define HID_KEYPAD_0 98 // Keypad 0 and Insert
  119. #define HID_KEYPAD_DOT 99 // Keypad . and Delete
  120. #define HID_KEY_MUTE 127 // Keyboard Mute
  121. #define HID_KEY_VOLUME_UP 128 // Keyboard Volume up
  122. #define HID_KEY_VOLUME_DOWN 129 // Keyboard Volume down
  123. #define HID_KEY_LEFT_CTRL 224 // Keyboard LeftContorl
  124. #define HID_KEY_LEFT_SHIFT 225 // Keyboard LeftShift
  125. #define HID_KEY_LEFT_ALT 226 // Keyboard LeftAlt
  126. #define HID_KEY_LEFT_GUI 227 // Keyboard LeftGUI
  127. #define HID_KEY_RIGHT_CTRL 228 // Keyboard LeftContorl
  128. #define HID_KEY_RIGHT_SHIFT 229 // Keyboard LeftShift
  129. #define HID_KEY_RIGHT_ALT 230 // Keyboard LeftAlt
  130. #define HID_KEY_RIGHT_GUI 231 // Keyboard RightGUI
  131. typedef uint8_t keyboard_cmd_t;
  132. #define HID_MOUSE_LEFT 253
  133. #define HID_MOUSE_MIDDLE 254
  134. #define HID_MOUSE_RIGHT 255
  135. typedef uint8_t mouse_cmd_t;
  136. // HID Consumer Usage IDs (subset of the codes available in the USB HID Usage Tables spec)
  137. #define HID_CONSUMER_POWER 48 // Power
  138. #define HID_CONSUMER_RESET 49 // Reset
  139. #define HID_CONSUMER_SLEEP 50 // Sleep
  140. #define HID_CONSUMER_MENU 64 // Menu
  141. #define HID_CONSUMER_SELECTION 128 // Selection
  142. #define HID_CONSUMER_ASSIGN_SEL 129 // Assign Selection
  143. #define HID_CONSUMER_MODE_STEP 130 // Mode Step
  144. #define HID_CONSUMER_RECALL_LAST 131 // Recall Last
  145. #define HID_CONSUMER_QUIT 148 // Quit
  146. #define HID_CONSUMER_HELP 149 // Help
  147. #define HID_CONSUMER_CHANNEL_UP 156 // Channel Increment
  148. #define HID_CONSUMER_CHANNEL_DOWN 157 // Channel Decrement
  149. #define HID_CONSUMER_PLAY 176 // Play
  150. #define HID_CONSUMER_PAUSE 177 // Pause
  151. #define HID_CONSUMER_RECORD 178 // Record
  152. #define HID_CONSUMER_FAST_FORWARD 179 // Fast Forward
  153. #define HID_CONSUMER_REWIND 180 // Rewind
  154. #define HID_CONSUMER_SCAN_NEXT_TRK 181 // Scan Next Track
  155. #define HID_CONSUMER_SCAN_PREV_TRK 182 // Scan Previous Track
  156. #define HID_CONSUMER_STOP 183 // Stop
  157. #define HID_CONSUMER_EJECT 184 // Eject
  158. #define HID_CONSUMER_RANDOM_PLAY 185 // Random Play
  159. #define HID_CONSUMER_SELECT_DISC 186 // Select Disk
  160. #define HID_CONSUMER_ENTER_DISC 187 // Enter Disc
  161. #define HID_CONSUMER_REPEAT 188 // Repeat
  162. #define HID_CONSUMER_STOP_EJECT 204 // Stop/Eject
  163. #define HID_CONSUMER_PLAY_PAUSE 205 // Play/Pause
  164. #define HID_CONSUMER_PLAY_SKIP 206 // Play/Skip
  165. #define HID_CONSUMER_VOLUME 224 // Volume
  166. #define HID_CONSUMER_BALANCE 225 // Balance
  167. #define HID_CONSUMER_MUTE 226 // Mute
  168. #define HID_CONSUMER_BASS 227 // Bass
  169. #define HID_CONSUMER_VOLUME_UP 233 // Volume Increment
  170. #define HID_CONSUMER_VOLUME_DOWN 234 // Volume Decrement
  171. typedef uint8_t consumer_cmd_t;
  172. #define HID_CC_RPT_MUTE 1
  173. #define HID_CC_RPT_POWER 2
  174. #define HID_CC_RPT_LAST 3
  175. #define HID_CC_RPT_ASSIGN_SEL 4
  176. #define HID_CC_RPT_PLAY 5
  177. #define HID_CC_RPT_PAUSE 6
  178. #define HID_CC_RPT_RECORD 7
  179. #define HID_CC_RPT_FAST_FWD 8
  180. #define HID_CC_RPT_REWIND 9
  181. #define HID_CC_RPT_SCAN_NEXT_TRK 10
  182. #define HID_CC_RPT_SCAN_PREV_TRK 11
  183. #define HID_CC_RPT_STOP 12
  184. #define HID_CC_RPT_CHANNEL_UP 0x01
  185. #define HID_CC_RPT_CHANNEL_DOWN 0x03
  186. #define HID_CC_RPT_VOLUME_UP 0x40
  187. #define HID_CC_RPT_VOLUME_DOWN 0x80
  188. // HID Consumer Control report bitmasks
  189. #define HID_CC_RPT_NUMERIC_BITS 0xF0
  190. #define HID_CC_RPT_CHANNEL_BITS 0xCF
  191. #define HID_CC_RPT_VOLUME_BITS 0x3F
  192. #define HID_CC_RPT_BUTTON_BITS 0xF0
  193. #define HID_CC_RPT_SELECTION_BITS 0xCF
  194. // Macros for the HID Consumer Control 2-byte report
  195. #define HID_CC_RPT_SET_NUMERIC(s, x) (s)[0] &= HID_CC_RPT_NUMERIC_BITS; \
  196. (s)[0] = (x)
  197. #define HID_CC_RPT_SET_CHANNEL(s, x) (s)[0] &= HID_CC_RPT_CHANNEL_BITS; \
  198. (s)[0] |= ((x) & 0x03) << 4
  199. #define HID_CC_RPT_SET_VOLUME_UP(s) (s)[0] &= HID_CC_RPT_VOLUME_BITS; \
  200. (s)[0] |= 0x40
  201. #define HID_CC_RPT_SET_VOLUME_DOWN(s) (s)[0] &= HID_CC_RPT_VOLUME_BITS; \
  202. (s)[0] |= 0x80
  203. #define HID_CC_RPT_SET_BUTTON(s, x) (s)[1] &= HID_CC_RPT_BUTTON_BITS; \
  204. (s)[1] |= (x)
  205. #define HID_CC_RPT_SET_SELECTION(s, x) (s)[1] &= HID_CC_RPT_SELECTION_BITS; \
  206. (s)[1] |= ((x) & 0x03) << 4
  207. // HID report mapping table
  208. typedef struct
  209. {
  210. uint16_t handle; // Handle of report characteristic
  211. uint16_t cccdHandle; // Handle of CCCD for report characteristic
  212. uint8_t id; // Report ID
  213. uint8_t type; // Report type
  214. uint8_t mode; // Protocol mode (report or boot)
  215. } hid_report_map_t;
  216. // HID dev configuration structure
  217. typedef struct
  218. {
  219. uint32_t idleTimeout; // Idle timeout in milliseconds
  220. uint8_t hidFlags; // HID feature flags
  221. } hid_dev_cfg_t;
  222. void hid_dev_register_reports(uint8_t num_reports, hid_report_map_t *p_report);
  223. void hid_dev_send_report(esp_gatt_if_t gatts_if, uint16_t conn_id,
  224. uint8_t id, uint8_t type, uint8_t length, uint8_t *data);
  225. void hid_consumer_build_report(uint8_t *buffer, consumer_cmd_t cmd);
  226. void hid_keyboard_build_report(uint8_t *buffer, keyboard_cmd_t cmd);
  227. void hid_mouse_build_report(uint8_t *buffer, mouse_cmd_t cmd);
  228. #ifdef __cplusplus
  229. } // extern "C"
  230. #endif
  231. #endif /* HID_DEV_H__ */