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 16 17 #ifndef __HID_DEVICE_LE_PRF__ 18 #define __HID_DEVICE_LE_PRF__ 19 #include <stdbool.h> 20 #include "esp_gatts_api.h" 21 #include "esp_gatt_defs.h" 22 #include "esp_hidd_prf_api.h" 23 #include "esp_gap_ble_api.h" 24 #include "hid_dev.h" 25 26 #define SUPPORT_REPORT_VENDOR false 27 //HID BLE profile log tag 28 #define HID_LE_PRF_TAG "HID_LE_PRF" 29 30 /// Maximal number of HIDS that can be added in the DB 31 #ifndef USE_ONE_HIDS_INSTANCE 32 #define HIDD_LE_NB_HIDS_INST_MAX (2) 33 #else 34 #define HIDD_LE_NB_HIDS_INST_MAX (1) 35 #endif 36 37 #define HIDD_GREAT_VER 0x01 //Version + Subversion 38 #define HIDD_SUB_VER 0x00 //Version + Subversion 39 #define HIDD_VERSION ((HIDD_GREAT_VER<<8)|HIDD_SUB_VER) //Version + Subversion 40 41 #define HID_MAX_APPS 1 42 43 // Number of HID reports defined in the service 44 #define HID_NUM_REPORTS 9 45 46 // HID Report IDs for the service 47 #define HID_RPT_ID_MOUSE_IN 1 // Mouse input report ID 48 #define HID_RPT_ID_KEY_IN 2 // Keyboard input report ID 49 #define HID_RPT_ID_CC_IN 3 //Consumer Control input report ID 50 #define HID_RPT_ID_VENDOR_OUT 4 // Vendor output report ID 51 #define HID_RPT_ID_LED_OUT 0 // LED output report ID 52 #define HID_RPT_ID_FEATURE 0 // Feature report ID 53 54 #define HIDD_APP_ID 0x1812//ATT_SVC_HID 55 56 #define BATTRAY_APP_ID 0x180f 57 58 59 #define ATT_SVC_HID 0x1812 60 61 /// Maximal number of Report Char. that can be added in the DB for one HIDS - Up to 11 62 #define HIDD_LE_NB_REPORT_INST_MAX (5) 63 64 /// Maximal length of Report Char. Value 65 #define HIDD_LE_REPORT_MAX_LEN (255) 66 /// Maximal length of Report Map Char. Value 67 #define HIDD_LE_REPORT_MAP_MAX_LEN (512) 68 69 /// Length of Boot Report Char. Value Maximal Length 70 #define HIDD_LE_BOOT_REPORT_MAX_LEN (8) 71 72 /// Boot KB Input Report Notification Configuration Bit Mask 73 #define HIDD_LE_BOOT_KB_IN_NTF_CFG_MASK (0x40) 74 /// Boot KB Input Report Notification Configuration Bit Mask 75 #define HIDD_LE_BOOT_MOUSE_IN_NTF_CFG_MASK (0x80) 76 /// Boot Report Notification Configuration Bit Mask 77 #define HIDD_LE_REPORT_NTF_CFG_MASK (0x20) 78 79 80 /* HID information flags */ 81 #define HID_FLAGS_REMOTE_WAKE 0x01 // RemoteWake 82 #define HID_FLAGS_NORMALLY_CONNECTABLE 0x02 // NormallyConnectable 83 84 /* Control point commands */ 85 #define HID_CMD_SUSPEND 0x00 // Suspend 86 #define HID_CMD_EXIT_SUSPEND 0x01 // Exit Suspend 87 88 /* HID protocol mode values */ 89 #define HID_PROTOCOL_MODE_BOOT 0x00 // Boot Protocol Mode 90 #define HID_PROTOCOL_MODE_REPORT 0x01 // Report Protocol Mode 91 92 /* Attribute value lengths */ 93 #define HID_PROTOCOL_MODE_LEN 1 // HID Protocol Mode 94 #define HID_INFORMATION_LEN 4 // HID Information 95 #define HID_REPORT_REF_LEN 2 // HID Report Reference Descriptor 96 #define HID_EXT_REPORT_REF_LEN 2 // External Report Reference Descriptor 97 98 // HID feature flags 99 #define HID_KBD_FLAGS HID_FLAGS_REMOTE_WAKE 100 101 /* HID Report type */ 102 #define HID_REPORT_TYPE_INPUT 1 103 #define HID_REPORT_TYPE_OUTPUT 2 104 #define HID_REPORT_TYPE_FEATURE 3 105 106 107 /// HID Service Attributes Indexes 108 enum { 109 HIDD_LE_IDX_SVC, 110 111 // Included Service 112 HIDD_LE_IDX_INCL_SVC, 113 114 // HID Information 115 HIDD_LE_IDX_HID_INFO_CHAR, 116 HIDD_LE_IDX_HID_INFO_VAL, 117 118 // HID Control Point 119 HIDD_LE_IDX_HID_CTNL_PT_CHAR, 120 HIDD_LE_IDX_HID_CTNL_PT_VAL, 121 122 // Report Map 123 HIDD_LE_IDX_REPORT_MAP_CHAR, 124 HIDD_LE_IDX_REPORT_MAP_VAL, 125 HIDD_LE_IDX_REPORT_MAP_EXT_REP_REF, 126 127 // Protocol Mode 128 HIDD_LE_IDX_PROTO_MODE_CHAR, 129 HIDD_LE_IDX_PROTO_MODE_VAL, 130 131 // Report mouse input 132 HIDD_LE_IDX_REPORT_MOUSE_IN_CHAR, 133 HIDD_LE_IDX_REPORT_MOUSE_IN_VAL, 134 HIDD_LE_IDX_REPORT_MOUSE_IN_CCC, 135 HIDD_LE_IDX_REPORT_MOUSE_REP_REF, 136 //Report Key input 137 HIDD_LE_IDX_REPORT_KEY_IN_CHAR, 138 HIDD_LE_IDX_REPORT_KEY_IN_VAL, 139 HIDD_LE_IDX_REPORT_KEY_IN_CCC, 140 HIDD_LE_IDX_REPORT_KEY_IN_REP_REF, 141 ///Report Led output 142 HIDD_LE_IDX_REPORT_LED_OUT_CHAR, 143 HIDD_LE_IDX_REPORT_LED_OUT_VAL, 144 HIDD_LE_IDX_REPORT_LED_OUT_REP_REF, 145 146 #if (SUPPORT_REPORT_VENDOR == true) 147 /// Report Vendor 148 HIDD_LE_IDX_REPORT_VENDOR_OUT_CHAR, 149 HIDD_LE_IDX_REPORT_VENDOR_OUT_VAL, 150 HIDD_LE_IDX_REPORT_VENDOR_OUT_REP_REF, 151 #endif 152 HIDD_LE_IDX_REPORT_CC_IN_CHAR, 153 HIDD_LE_IDX_REPORT_CC_IN_VAL, 154 HIDD_LE_IDX_REPORT_CC_IN_CCC, 155 HIDD_LE_IDX_REPORT_CC_IN_REP_REF, 156 157 // Boot Keyboard Input Report 158 HIDD_LE_IDX_BOOT_KB_IN_REPORT_CHAR, 159 HIDD_LE_IDX_BOOT_KB_IN_REPORT_VAL, 160 HIDD_LE_IDX_BOOT_KB_IN_REPORT_NTF_CFG, 161 162 // Boot Keyboard Output Report 163 HIDD_LE_IDX_BOOT_KB_OUT_REPORT_CHAR, 164 HIDD_LE_IDX_BOOT_KB_OUT_REPORT_VAL, 165 166 // Boot Mouse Input Report 167 HIDD_LE_IDX_BOOT_MOUSE_IN_REPORT_CHAR, 168 HIDD_LE_IDX_BOOT_MOUSE_IN_REPORT_VAL, 169 HIDD_LE_IDX_BOOT_MOUSE_IN_REPORT_NTF_CFG, 170 171 // Report 172 HIDD_LE_IDX_REPORT_CHAR, 173 HIDD_LE_IDX_REPORT_VAL, 174 HIDD_LE_IDX_REPORT_REP_REF, 175 //HIDD_LE_IDX_REPORT_NTF_CFG, 176 177 HIDD_LE_IDX_NB, 178 }; 179 180 181 /// Attribute Table Indexes 182 enum { 183 HIDD_LE_INFO_CHAR, 184 HIDD_LE_CTNL_PT_CHAR, 185 HIDD_LE_REPORT_MAP_CHAR, 186 HIDD_LE_REPORT_CHAR, 187 HIDD_LE_PROTO_MODE_CHAR, 188 HIDD_LE_BOOT_KB_IN_REPORT_CHAR, 189 HIDD_LE_BOOT_KB_OUT_REPORT_CHAR, 190 HIDD_LE_BOOT_MOUSE_IN_REPORT_CHAR, 191 HIDD_LE_CHAR_MAX //= HIDD_LE_REPORT_CHAR + HIDD_LE_NB_REPORT_INST_MAX, 192 }; 193 194 ///att read event table Indexs 195 enum { 196 HIDD_LE_READ_INFO_EVT, 197 HIDD_LE_READ_CTNL_PT_EVT, 198 HIDD_LE_READ_REPORT_MAP_EVT, 199 HIDD_LE_READ_REPORT_EVT, 200 HIDD_LE_READ_PROTO_MODE_EVT, 201 HIDD_LE_BOOT_KB_IN_REPORT_EVT, 202 HIDD_LE_BOOT_KB_OUT_REPORT_EVT, 203 HIDD_LE_BOOT_MOUSE_IN_REPORT_EVT, 204 205 HID_LE_EVT_MAX 206 }; 207 208 /// Client Characteristic Configuration Codes 209 enum { 210 HIDD_LE_DESC_MASK = 0x10, 211 212 HIDD_LE_BOOT_KB_IN_REPORT_CFG = HIDD_LE_BOOT_KB_IN_REPORT_CHAR | HIDD_LE_DESC_MASK, 213 HIDD_LE_BOOT_MOUSE_IN_REPORT_CFG = HIDD_LE_BOOT_MOUSE_IN_REPORT_CHAR | HIDD_LE_DESC_MASK, 214 HIDD_LE_REPORT_CFG = HIDD_LE_REPORT_CHAR | HIDD_LE_DESC_MASK, 215 }; 216 217 /// Features Flag Values 218 enum { 219 HIDD_LE_CFG_KEYBOARD = 0x01, 220 HIDD_LE_CFG_MOUSE = 0x02, 221 HIDD_LE_CFG_PROTO_MODE = 0x04, 222 HIDD_LE_CFG_MAP_EXT_REF = 0x08, 223 HIDD_LE_CFG_BOOT_KB_WR = 0x10, 224 HIDD_LE_CFG_BOOT_MOUSE_WR = 0x20, 225 }; 226 227 /// Report Char. Configuration Flag Values 228 enum { 229 HIDD_LE_CFG_REPORT_IN = 0x01, 230 HIDD_LE_CFG_REPORT_OUT = 0x02, 231 //HOGPD_CFG_REPORT_FEAT can be used as a mask to check Report type 232 HIDD_LE_CFG_REPORT_FEAT = 0x03, 233 HIDD_LE_CFG_REPORT_WR = 0x10, 234 }; 235 236 /// Pointer to the connection clean-up function 237 #define HIDD_LE_CLEANUP_FNCT (NULL) 238 239 /* 240 * TYPE DEFINITIONS 241 **************************************************************************************** 242 */ 243 244 /// HIDD Features structure 245 typedef struct { 246 /// Service Features 247 uint8_t svc_features; 248 /// Number of Report Char. instances to add in the database 249 uint8_t report_nb; 250 /// Report Char. Configuration 251 uint8_t report_char_cfg[HIDD_LE_NB_REPORT_INST_MAX]; 252 } hidd_feature_t; 253 254 255 typedef struct { 256 bool in_use; 257 bool congest; 258 uint16_t conn_id; 259 bool connected; 260 esp_bd_addr_t remote_bda; 261 uint32_t trans_id; 262 uint8_t cur_srvc_id; 263 264 } hidd_clcb_t; 265 266 // HID report mapping table 267 typedef struct { 268 uint16_t handle; // Handle of report characteristic 269 uint16_t cccdHandle; // Handle of CCCD for report characteristic 270 uint8_t id; // Report ID 271 uint8_t type; // Report type 272 uint8_t mode; // Protocol mode (report or boot) 273 } hidRptMap_t; 274 275 276 typedef struct { 277 /// hidd profile id 278 uint8_t app_id; 279 /// Notified handle 280 uint16_t ntf_handle; 281 ///Attribute handle Table 282 uint16_t att_tbl[HIDD_LE_IDX_NB]; 283 /// Supported Features 284 hidd_feature_t hidd_feature[HIDD_LE_NB_HIDS_INST_MAX]; 285 /// Current Protocol Mode 286 uint8_t proto_mode[HIDD_LE_NB_HIDS_INST_MAX]; 287 /// Number of HIDS added in the database 288 uint8_t hids_nb; 289 uint8_t pending_evt; 290 uint16_t pending_hal; 291 } hidd_inst_t; 292 293 /// Report Reference structure 294 typedef struct 295 { 296 ///Report ID 297 uint8_t report_id; 298 ///Report Type 299 uint8_t report_type; 300 }hids_report_ref_t; 301 302 /// HID Information structure 303 typedef struct 304 { 305 /// bcdHID 306 uint16_t bcdHID; 307 /// bCountryCode 308 uint8_t bCountryCode; 309 /// Flags 310 uint8_t flags; 311 }hids_hid_info_t; 312 313 314 /* service engine control block */ 315 typedef struct { 316 hidd_clcb_t hidd_clcb[HID_MAX_APPS]; /* connection link*/ 317 esp_gatt_if_t gatt_if; 318 bool enabled; 319 bool is_take; 320 bool is_primery; 321 hidd_inst_t hidd_inst; 322 esp_hidd_event_cb_t hidd_cb; 323 uint8_t inst_id; 324 } hidd_le_env_t; 325 326 extern hidd_le_env_t hidd_le_env; 327 extern uint8_t hidProtocolMode; 328 329 330 void hidd_clcb_alloc (uint16_t conn_id, esp_bd_addr_t bda); 331 332 bool hidd_clcb_dealloc (uint16_t conn_id); 333 334 void hidd_le_create_service(esp_gatt_if_t gatts_if); 335 336 void hidd_set_attr_value(uint16_t handle, uint16_t val_len, const uint8_t *value); 337 338 void hidd_get_attr_value(uint16_t handle, uint16_t *length, uint8_t **value); 339 340 esp_err_t hidd_register_cb(void); 341 342 343 #endif ///__HID_DEVICE_LE_PRF__ 344