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