1 /****************************************************************************** 2 * Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK") 3 * All rights reserved. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 *****************************************************************************/ 18 /* 19 * gap_event.h 20 * 21 * Created on: 2018-12-5 22 * Author: Administrator 23 */ 24 25 #ifndef GAP_EVENT_H_ 26 #define GAP_EVENT_H_ 27 28 /***************************************** SMP message sequence and event chart ************************************* 29 30 GAP_SMP EVENT 31 Situation 1: SMP Standard Pair | 32 Phase 1: Pairing Feature Exchange | 33 Phase 2: Encryption( STK/LTK Generation ) | 34 Phase 3: Key Distribution | 35 | 36 Master Slave | 37 | | | 38 | | | 39 ________|___________________________________________________________|_______ | 40 | | | 41 | Establish LL connection | | 42 |___________________________________________________________________________| | 43 | | | 44 | | | 45 | Phase 1: Pairing Feature Exchange | | 46 _______|___________________________________________________________|_______ | 47 | | | | | 48 | | (Optional)Security_Request | | | 49 | |<----------------------------------------------------------| | | 50 | | Pairing_Req | | 51 | |---------------------------------------------------------->|=======|=>>> GAP_EVT_SMP_PAIRING_BEGIN 52 | | Pairing_Rsp | | 53 | |<----------------------------------------------------------| | | 54 | | ....... | | | 55 |_______|___________________________________________________________|_______| | 56 | | | 57 | | | 58 | Phase 2: Encryption | | 59 _______|___________________________________________________________|_______ | 60 | | | | | 61 | | LL_ENC_REQ | | | 62 | |---------------------------------------------------------->| | | 63 | | LL_ENC_RSP | | | 64 | |<----------------------------------------------------------| | | 65 | | LL_START_ENC_REQ | | | 66 | |<----------------------------------------------------------| | | 67 | | LL_START_ENC_RSP | | | 68 | |---------------------------------------------------------->| | | 69 | | LL_START_ENC_RSP | | 70 | |<----------------------------------------------------------|=======|=>>> GAP_EVT_SMP_CONN_ENCRYPTION_DONE 71 |_______|___________________________________________________________|_______| 72 | | | 73 | | | 74 | Phase 3: Key Distribution | | 75 _______|___________________________________________________________|_______ | 76 | | | | | 77 | | Key Distribution | | | 78 | |<----------------------------------------------------------| | | 79 | | Key Distribution | | | 80 | |<----------------------------------------------------------| | | 81 | | ....... | | | 82 | | | | | 83 | | Key Distribution | | | 84 | |---------------------------------------------------------->| | | 85 | | Key Distribution | | | 86 | |---------------------------------------------------------->| | | 87 | | ....... | | | 88 | | | | 89 | | All Key Distribution Finish |=======|=>>> GAP_EVT_SMP_PAIRING_SUCCESS 90 | | |=======|=>>> GAP_EVT_SMP_SECURITY_PROCESS_DONE 91 |_______|___________________________________________________________|_______| 92 | 93 | 94 | 95 | 96 Situation 2: SMP Fast Connect | 97 Only 1 Phase: Encryption | 98 | 99 Master Slave | 100 | | | 101 | | | 102 ________|___________________________________________________________|_______ | 103 | | | 104 | Establish LL connection | | 105 |___________________________________________________________________________| | 106 | | | 107 | | | 108 | Phase 2: Encryption | | 109 _______|___________________________________________________________|_______ | 110 | | | | | 111 | | LL_ENC_REQ | | | 112 | |---------------------------------------------------------->| | | 113 | | LL_ENC_RSP | | | 114 | |<----------------------------------------------------------| | | 115 | | LL_START_ENC_REQ | | | 116 | |<----------------------------------------------------------| | | 117 | | LL_START_ENC_RSP | | | 118 | |---------------------------------------------------------->| | | 119 | | LL_START_ENC_RSP | | | 120 | |<----------------------------------------------------------|=======|=>>> GAP_EVT_SMP_CONN_ENCRYPTION_DONE 121 | | |=======|=>>> GAP_EVT_SMP_SECURITY_PROCESS_DONE 122 |_______|___________________________________________________________|_______| 123 124 125 *********************************************************************************************************************/ 126 127 /** 128 * @brief GAP event type 129 */ 130 #define GAP_EVT_SMP_PAIRING_BEGIN 0 // Refer to SMP message sequence and event chart above 131 #define GAP_EVT_SMP_PAIRING_SUCCESS 1 // Refer to SMP message sequence and event chart above 132 #define GAP_EVT_SMP_PAIRING_FAIL 2 133 #define GAP_EVT_SMP_CONN_ENCRYPTION_DONE 3 // Refer to SMP message sequence and event chart above 134 #define GAP_EVT_SMP_SECURITY_PROCESS_DONE 4 // Refer to SMP message sequence and event chart above 135 136 #define GAP_EVT_SMP_TK_DISPALY 8 137 #define GAP_EVT_SMP_TK_REQUEST_PASSKEY 9 138 #define GAP_EVT_SMP_TK_REQUEST_OOB 10 139 #define GAP_EVT_SMP_TK_NUMERIC_COMPARE 11 140 #define GAP_EVT_SMP_BONDING_INFO_FULL 12 141 142 #define GAP_EVT_ATT_EXCHANGE_MTU 16 143 #define GAP_EVT_GATT_HANDLE_VLAUE_CONFIRM 17 144 145 #define GAP_EVT_L2CAP_CONN_PARAM_UPDATE_REQ 20 146 #define GAP_EVT_L2CAP_CONN_PARAM_UPDATE_RSP 21 147 148 #if (L2CAP_CREDIT_BASED_FLOW_CONTROL_MODE_EN) 149 #define GAP_EVT_L2CAP_LE_CREDIT_BASED_CONNECT 22 150 #define GAP_EVT_L2CAP_CREDIT_BASED_CONNECT 23 151 #define GAP_EVT_L2CAP_CREDIT_BASED_RECONFIG 24 152 #define GAP_EVT_L2CAP_FLOW_CONTROL_CREDIT 25 153 #define GAP_EVT_L2CAP_DISCONNECT 26 154 #define GAP_EVT_L2CAP_COC_DATA 27 155 #endif 156 157 /** 158 * @brief GAP event mask 159 */ 160 #define GAP_EVT_MASK_NONE 0x00000000 161 #define GAP_EVT_MASK_SMP_PAIRING_BEGIN (1 << GAP_EVT_SMP_PAIRING_BEGIN) 162 #define GAP_EVT_MASK_SMP_PAIRING_SUCCESS (1 << GAP_EVT_SMP_PAIRING_SUCCESS) 163 #define GAP_EVT_MASK_SMP_PAIRING_FAIL (1 << GAP_EVT_SMP_PAIRING_FAIL) 164 #define GAP_EVT_MASK_SMP_CONN_ENCRYPTION_DONE (1 << GAP_EVT_SMP_CONN_ENCRYPTION_DONE) 165 #define GAP_EVT_MASK_SMP_SECURITY_PROCESS_DONE (1 << GAP_EVT_SMP_SECURITY_PROCESS_DONE) 166 167 #define GAP_EVT_MASK_SMP_TK_DISPALY (1 << GAP_EVT_SMP_TK_DISPALY) 168 #define GAP_EVT_MASK_SMP_TK_REQUEST_PASSKEY (1 << GAP_EVT_SMP_TK_REQUEST_PASSKEY) 169 #define GAP_EVT_MASK_SMP_TK_REQUEST_OOB (1 << GAP_EVT_SMP_TK_REQUEST_OOB) 170 #define GAP_EVT_MASK_SMP_TK_NUMERIC_COMPARE (1 << GAP_EVT_SMP_TK_NUMERIC_COMPARE) 171 #define GAP_EVT_MASK_SMP_BONDING_INFO_FULL (1 << GAP_EVT_SMP_BONDING_INFO_FULL) 172 173 #define GAP_EVT_MASK_ATT_EXCHANGE_MTU (1 << GAP_EVT_ATT_EXCHANGE_MTU) 174 #define GAP_EVT_MASK_GATT_HANDLE_VLAUE_CONFIRM (1 << GAP_EVT_GATT_HANDLE_VLAUE_CONFIRM) 175 176 #define GAP_EVT_MASK_L2CAP_CONN_PARAM_UPDATE_REQ (1 << GAP_EVT_L2CAP_CONN_PARAM_UPDATE_REQ) 177 #define GAP_EVT_MASK_L2CAP_CONN_PARAM_UPDATE_RSP (1 << GAP_EVT_L2CAP_CONN_PARAM_UPDATE_RSP) 178 179 #if (L2CAP_CREDIT_BASED_FLOW_CONTROL_MODE_EN) 180 #define GAP_EVT_MASK_L2CAP_LE_CREDIT_BASED_CONNECT (1 << GAP_EVT_L2CAP_LE_CREDIT_BASED_CONNECT) 181 #define GAP_EVT_MASK_L2CAP_CREDIT_BASED_CONNECT (1 << GAP_EVT_L2CAP_CREDIT_BASED_CONNECT) 182 #define GAP_EVT_MASK_L2CAP_CREDIT_BASED_RECONFIG (1 << GAP_EVT_L2CAP_CREDIT_BASED_RECONFIG) 183 #define GAP_EVT_MASK_L2CAP_FLOW_CONTROL_CREDIT (1 << GAP_EVT_L2CAP_FLOW_CONTROL_CREDIT) 184 #define GAP_EVT_MASK_L2CAP_DISCONNECT (1 << GAP_EVT_L2CAP_DISCONNECT) 185 #define GAP_EVT_MASK_L2CAP_COC_DATA (1 << GAP_EVT_L2CAP_COC_DATA) 186 #endif 187 188 #define GAP_EVT_MASK_DEFAULT \ 189 (GAP_EVT_MASK_SMP_TK_DISPALY | GAP_EVT_MASK_SMP_TK_REQUEST_PASSKEY | GAP_EVT_MASK_SMP_TK_REQUEST_OOB | \ 190 GAP_EVT_MASK_ATT_EXCHANGE_MTU) 191 192 /** 193 * @brief Event Parameters for "GAP_EVT_SMP_PAIRING_BEGIN" 194 */ 195 typedef struct { 196 u16 connHandle; 197 u8 secure_conn; 198 u8 tk_method; 199 } gap_smp_pairingBeginEvt_t; 200 201 /** 202 * @brief Event Parameters for "GAP_EVT_SMP_PAIRING_SUCCESS" 203 */ 204 typedef struct { 205 u16 connHandle; 206 u8 bonding; 207 u8 bonding_result; 208 } gap_smp_pairingSuccessEvt_t; 209 210 /** 211 * @brief Event Parameters for "GAP_EVT_SMP_PAIRING_FAIL" 212 */ 213 typedef struct { 214 u16 connHandle; 215 u8 reason; 216 } gap_smp_pairingFailEvt_t; 217 218 /** 219 * @brief Event Parameters for "GAP_EVT_SMP_CONN_ENCRYPTION_DONE" 220 */ 221 typedef struct { 222 u16 connHandle; 223 u8 re_connect; // 1: re_connect, encrypt with previous distributed LTK; 0: pairing , encrypt with STK 224 } gap_smp_connEncDoneEvt_t; 225 226 /** 227 * @brief Event Parameters for "GAP_EVT_SMP_SECURITY_PROCESS_DONE" 228 */ 229 typedef struct { 230 u16 connHandle; 231 u8 re_connect; // 1: re_connect, encrypt with previous distributed LTK; 0: pairing , encrypt with STK 232 } gap_smp_securityProcessDoneEvt_t; 233 234 /** 235 * @brief Event Parameters for "GAP_EVT_SMP_TK_DISPALY" 236 */ 237 typedef struct { 238 u16 connHandle; 239 u32 tk_pincode; 240 } gap_smp_TkDisplayEvt_t; 241 242 /** 243 * @brief Event Parameters for "GAP_EVT_SMP_TK_REQUEST_PASSKEY" 244 */ 245 typedef struct { 246 u16 connHandle; 247 } gap_smp_TkReqPassKeyEvt_t; 248 249 /** 250 * @brief Event Parameters for "GAP_EVT_ATT_EXCHANGE_MTU" 251 */ 252 typedef struct { 253 u16 connHandle; 254 u16 peer_MTU; 255 u16 effective_MTU; 256 } gap_gatt_mtuSizeExchangeEvt_t; 257 258 /** 259 * @brief Event Parameters for "GAP_EVT_L2CAP_CONN_PARAM_UPDATE_REQ" 260 */ 261 typedef struct { 262 u16 connHandle; 263 u8 id; 264 u16 min_interval; 265 u16 max_interval; 266 u16 latency; 267 u16 timeout; 268 } gap_l2cap_connParamUpdateReqEvt_t; 269 270 /** 271 * @brief Event Parameters for "GAP_EVT_L2CAP_CONN_PARAM_UPDATE_RSP" 272 */ 273 typedef struct { 274 u16 connHandle; 275 u8 id; 276 u16 result; 277 } gap_l2cap_connParamUpdateRspEvt_t; 278 279 #if (L2CAP_CREDIT_BASED_FLOW_CONTROL_MODE_EN) 280 typedef struct { 281 u16 connHandle; 282 u16 result; 283 u16 reason; 284 u16 local_mtu; 285 u16 local_mps; 286 u16 local_credit; 287 u16 peer_mtu; 288 u16 peer_mps; 289 u16 peer_credit; 290 u8 srv_num; 291 u16 scid; 292 u16 dcid; 293 } gap_l2cap_leCreditBasedConnectEvt_t; 294 typedef struct { 295 u16 connHandle; 296 u16 result; 297 u16 reason; 298 u16 local_mtu; 299 u16 local_mps; 300 u16 local_credit; 301 u16 peer_mtu; 302 u16 peer_mps; 303 u16 peer_credit; 304 u8 srv_num; 305 u8 cid_count; 306 u16 scid[5]; 307 u16 dcid[5]; 308 } gap_l2cap_creditBasedConnectEvt_t; 309 typedef struct { 310 u16 connHandle; 311 u16 local_credit; 312 u16 peer_credit; 313 u16 scid; 314 u16 dcid; 315 } gap_l2cap_flowControlCreditEvt_t; 316 typedef struct { 317 u16 connHandle; 318 u16 spsm; 319 u16 scid; 320 u16 dcid; 321 } gap_l2cap_disconnectEvt_t; 322 typedef struct { 323 u16 connHandle; 324 u16 result; 325 u16 local_mtu; 326 u16 local_mps; 327 u16 local_credit; 328 u16 peer_mtu; 329 u16 peer_mps; 330 u16 peer_credit; 331 u8 srv_num; 332 u8 cid_count; 333 u16 scid[5]; 334 u16 dcid[5]; 335 } gap_l2cap_creditBasedReconfigEvt_t; 336 typedef struct { 337 u16 connHandle; 338 u16 spsm; 339 u16 scid; 340 u16 dcid; 341 u16 dataLen; 342 u8 *pData; 343 } gap_l2cap_cocData_t; 344 #endif // #if (L2CAP_CREDIT_BASED_FLOW_CONTROL_MODE_EN) 345 346 /** 347 * @brief GAP event callback function declaration 348 */ 349 typedef int (*gap_event_handler_t)(u32 h, u8 *para, int n); 350 351 /** 352 * @brief set event mask for specific gap event 353 * @param[in] evtMask - event mask 354 * @return none 355 */ 356 void blc_gap_setEventMask(u32 evtMask); 357 358 /** 359 * @brief register public enter for all gap event callback 360 * @param[in] handler - public enter function 361 * @return none 362 */ 363 void blc_gap_registerHostEventHandler(gap_event_handler_t handler); 364 365 #endif /* GAP_EVENT_H_ */ 366