1 /*
2 * Copyright (C) 2021 Huawei Device Co., Ltd.
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 #include "gap_le.h"
17 #include "gap_internal.h"
18 #include "gap_task_internal.h"
19
20 #include <securec.h>
21
22 #include "allocator.h"
23 #include "log.h"
24
25 #include "btm.h"
26 #include "btm/btm_controller.h"
27 #include "btm/btm_thread.h"
28
29 #ifdef GAP_LE_SUPPORT
30
31 typedef struct {
32 GapLeConnCallback callback;
33 void *context;
34 } LeConnUpdateCallback;
35
36 static LeConnUpdateCallback g_leConnUpdateCallback;
37
GapWriteAuthenticatedPayloadTimeoutComplete(const HciWriteAuthenticatedPayloadTimeoutReturnParam * param)38 void GapWriteAuthenticatedPayloadTimeoutComplete(const HciWriteAuthenticatedPayloadTimeoutReturnParam *param)
39 {
40 if (param->status != HCI_SUCCESS) {
41 LOG_WARN("%{public}s:handle:0x%04x status:0x%02x", __FUNCTION__, param->connectionHandle, param->status);
42 }
43 }
44
GapOnAuthenticatedPayloadTimeoutExpiredEvent(const HciAuthenticatedPayloadTimeoutExpiredEventParam * eventParam)45 void GapOnAuthenticatedPayloadTimeoutExpiredEvent(const HciAuthenticatedPayloadTimeoutExpiredEventParam *eventParam)
46 {
47 LOG_WARN("%{public}s:handle:0x%04x", __FUNCTION__, eventParam->connectionHandle);
48 }
49
GAP_RegisterLeConnCallback(const GapLeConnCallback * callback,void * context)50 int GAP_RegisterLeConnCallback(const GapLeConnCallback *callback, void *context)
51 {
52 LOG_INFO("%{public}s:%{public}s", __FUNCTION__, callback ? "register" : "NULL");
53 if (callback == NULL) {
54 (void)memset_s(&g_leConnUpdateCallback.callback,
55 sizeof(g_leConnUpdateCallback.callback),
56 0x00,
57 sizeof(g_leConnUpdateCallback.callback));
58 } else {
59 g_leConnUpdateCallback.callback = *callback;
60 }
61 g_leConnUpdateCallback.context = context;
62 return GAP_SUCCESS;
63 }
64
GAP_DeregisterLeConnCallback(void)65 int GAP_DeregisterLeConnCallback(void)
66 {
67 (void)memset_s(&g_leConnUpdateCallback.callback,
68 sizeof(g_leConnUpdateCallback.callback),
69 0x00,
70 sizeof(g_leConnUpdateCallback.callback));
71 g_leConnUpdateCallback.context = NULL;
72 return GAP_SUCCESS;
73 }
74
GapLeConnectionUpdate(uint16_t handle,const GapLeConnectionParameter * connParam)75 static int GapLeConnectionUpdate(uint16_t handle, const GapLeConnectionParameter *connParam)
76 {
77 HciLeConnectionUpdateParam hciCmdParam = {
78 .connectionHandle = handle,
79 .connIntervalMin = connParam->connIntervalMin,
80 .connIntervalMax = connParam->connIntervalMax,
81 .connLatency = connParam->connLatency,
82 .supervisionTimeout = connParam->timeout,
83 .minimumCELength = connParam->minCeLen,
84 .maximumCELength = connParam->maxCeLen,
85 };
86
87 return HCI_LeConnectionUpdate(&hciCmdParam);
88 }
89
GapOnLeConnectionUpdateCompleteEvent(const HciLeConnectionUpdateCompleteEventParam * eventParam)90 void GapOnLeConnectionUpdateCompleteEvent(const HciLeConnectionUpdateCompleteEventParam *eventParam)
91 {
92 BtAddr addr = {0};
93
94 LeConnectionInfoBlock *connectionInfoBlock = GapGetLeConnectionInfoBlock();
95 LeDeviceInfo *deviceInfo = NULL;
96 deviceInfo = ListForEachData(
97 connectionInfoBlock->deviceList, GapFindLeConnectionDeviceByHandle, (void *)&eventParam->connectionHandle);
98 if (deviceInfo != NULL) {
99 (void)memcpy_s(&addr, sizeof(BtAddr), &deviceInfo->addr, sizeof(BtAddr));
100 if (deviceInfo->paramUpdateReq != NULL) {
101 MEM_MALLOC.free(deviceInfo->paramUpdateReq);
102 deviceInfo->paramUpdateReq = NULL;
103 }
104 }
105
106 if (deviceInfo != NULL) {
107 if (g_leConnUpdateCallback.callback.leConnectionUpdateComplete) {
108 g_leConnUpdateCallback.callback.leConnectionUpdateComplete(eventParam->status,
109 &addr,
110 eventParam->connInterval,
111 eventParam->connLatency,
112 eventParam->supervisionTimeout,
113 g_leConnUpdateCallback.context);
114 }
115 }
116 }
117
GAP_LeConnParamUpdate(const BtAddr * addr,const GapLeConnectionParameter * connParam)118 int GAP_LeConnParamUpdate(const BtAddr *addr, const GapLeConnectionParameter *connParam)
119 {
120 LOG_INFO("%{public}s:", __FUNCTION__);
121 int ret = GAP_SUCCESS;
122
123 if (GapIsLeEnable() == false) {
124 return GAP_ERR_NOT_ENABLE;
125 }
126
127 if (GapLeRolesCheck(GAP_LE_ROLE_CENTRAL | GAP_LE_ROLE_PREIPHERAL) == false) {
128 ret = GAP_ERR_INVAL_STATE;
129 } else {
130 LeConnectionInfoBlock *connectionInfoBlock = GapGetLeConnectionInfoBlock();
131 LeDeviceInfo *deviceInfo = NULL;
132 deviceInfo = ListForEachData(connectionInfoBlock->deviceList, GapFindLeConnectionDeviceByAddr, (void *)addr);
133 if (deviceInfo != NULL) {
134 if (deviceInfo->role == LE_CONNECTION_ROLE_MASTER) {
135 ret = GapLeConnectionUpdate(deviceInfo->handle, connParam);
136 } else if (BTM_IsControllerSupportConnectionParametersRequestProcedure() && false) {
137 ret = GapLeConnectionUpdate(deviceInfo->handle, connParam);
138 } else {
139 L2capLeConnectionParameter parameter = {
140 .connIntervalMax = connParam->connIntervalMax,
141 .connIntervalMin = connParam->connIntervalMin,
142 .connLatency = connParam->connLatency,
143 .supervisionTimeout = connParam->timeout,
144 };
145 GapLeConnectionParameterUpdateReq(deviceInfo->handle, ¶meter);
146 }
147 } else {
148 ret = GAP_ERR_INVAL_STATE;
149 }
150 }
151
152 return ret;
153 }
154
GapLeRemoteConnectionParameterRequestNegativeReply(uint16_t handle,uint8_t reason)155 static int GapLeRemoteConnectionParameterRequestNegativeReply(uint16_t handle, uint8_t reason)
156 {
157 HciLeRemoteConnectionParameterRequestNegativeReplyParam hciCmdParam = {
158 .connectionHandle = handle,
159 .reason = reason,
160 };
161
162 return HCI_LeRemoteConnectionParameterRequestNegativeReply(&hciCmdParam);
163 }
164
GapLeRemoteConnectionParameterRequestNegativeReplyComplete(const HciLeRemoteConnectionParameterRequestNegativeReplyReturnParam * param)165 void GapLeRemoteConnectionParameterRequestNegativeReplyComplete(
166 const HciLeRemoteConnectionParameterRequestNegativeReplyReturnParam *param)
167 {
168 if (param->status) {
169 LOG_ERROR("%{public}s:status:%02x, handle:%04x", __FUNCTION__, param->status, param->connectionHandle);
170 }
171 }
172
GapLeRemoteConnectionParameterRequestReply(uint16_t handle,const GapLeConnectionParameter * connParam)173 static int GapLeRemoteConnectionParameterRequestReply(uint16_t handle, const GapLeConnectionParameter *connParam)
174 {
175 HciLeRemoteConnectionParameterRequestReplyParam hciCmdParam = {
176 .connectionHandle = handle,
177 .intervalMin = connParam->connIntervalMin,
178 .intervalMax = connParam->connIntervalMax,
179 .latency = connParam->connLatency,
180 .timeout = connParam->timeout,
181 .minimumCELength = connParam->minCeLen,
182 .maximumCELength = connParam->maxCeLen,
183 };
184
185 return HCI_LeRemoteConnectionParameterRequestReply(&hciCmdParam);
186 }
187
GapLeRemoteConnectionParameterRequestReplyComplete(const HciLeRemoteConnectionParameterRequestReplyReturnParam * param)188 void GapLeRemoteConnectionParameterRequestReplyComplete(
189 const HciLeRemoteConnectionParameterRequestReplyReturnParam *param)
190 {
191 if (param->status) {
192 LOG_WARN("%{public}s:status:%02x, handle:%04x", __FUNCTION__, param->status, param->connectionHandle);
193 GapLeRemoteConnectionParameterRequestNegativeReply(param->connectionHandle, HCI_UNSPECIFIED_ERROR);
194 }
195 }
196
GapOnLeRemoteConnectionParameterRequestEvent(const HciLeRemoteConnectionParameterRequestEventParam * eventParam)197 void GapOnLeRemoteConnectionParameterRequestEvent(const HciLeRemoteConnectionParameterRequestEventParam *eventParam)
198 {
199 BtAddr addr = {0};
200 bool doReject = false;
201
202 LeConnectionInfoBlock *connectionInfoBlock = GapGetLeConnectionInfoBlock();
203 LeDeviceInfo *deviceInfo = NULL;
204 deviceInfo = ListForEachData(
205 connectionInfoBlock->deviceList, GapFindLeConnectionDeviceByHandle, (void *)&eventParam->connectionHandle);
206 if (deviceInfo != NULL) {
207 (void)memcpy_s(&addr, sizeof(BtAddr), &deviceInfo->addr, sizeof(BtAddr));
208 if (deviceInfo->paramUpdateReq != NULL) {
209 doReject = true;
210 } else {
211 deviceInfo->paramUpdateReq = MEM_MALLOC.alloc(sizeof(LeConnParamUpdateReq));
212 if (deviceInfo->paramUpdateReq != NULL) {
213 deviceInfo->paramUpdateReq->status = GAP_LE_CONN_PARAM_UPDATE_RECV_HCI;
214 deviceInfo->paramUpdateReq->id = 0x00;
215 } else {
216 doReject = true;
217 }
218 }
219 }
220
221 if (doReject) {
222 GapLeRemoteConnectionParameterRequestNegativeReply(eventParam->connectionHandle, HCI_UNSPECIFIED_ERROR);
223 } else {
224 if (g_leConnUpdateCallback.callback.leConnectionParameterReq) {
225 g_leConnUpdateCallback.callback.leConnectionParameterReq(&addr,
226 eventParam->intervalMin,
227 eventParam->IntervalMax,
228 eventParam->latency,
229 eventParam->timeout,
230 g_leConnUpdateCallback.context);
231 } else {
232 GapLeRemoteConnectionParameterRequestNegativeReply(eventParam->connectionHandle, HCI_UNSPECIFIED_ERROR);
233 }
234 }
235 }
236
GapAcceptConnectionParameterUpdate(const LeDeviceInfo * deviceInfo,const GapLeConnectionParameter * connParam)237 static int GapAcceptConnectionParameterUpdate(const LeDeviceInfo *deviceInfo, const GapLeConnectionParameter *connParam)
238 {
239 int ret;
240
241 if (deviceInfo->paramUpdateReq->status == GAP_LE_CONN_PARAM_UPDATE_RECV_HCI) {
242 ret = GapLeRemoteConnectionParameterRequestReply(deviceInfo->handle, connParam);
243 } else {
244 GapLeConnectionParameterUpdateRsp(
245 deviceInfo->handle, deviceInfo->paramUpdateReq->id, L2CAP_LE_CONNECTION_PARAMETERS_ACCEPTED);
246 ret = GapLeConnectionUpdate(deviceInfo->handle, connParam);
247 }
248
249 return ret;
250 }
251
GapRejectConnectionParameterUpdate(const LeDeviceInfo * deviceInfo)252 static int GapRejectConnectionParameterUpdate(const LeDeviceInfo *deviceInfo)
253 {
254 int ret = GAP_SUCCESS;
255
256 if (deviceInfo->paramUpdateReq->status == GAP_LE_CONN_PARAM_UPDATE_RECV_HCI) {
257 ret = GapLeRemoteConnectionParameterRequestNegativeReply(
258 deviceInfo->handle, HCI_UNSUPPORTED_LMP_PARAMETER_VALUE_OR_UNSUPPORTED_LL_PARAMETER_VALUE);
259 } else {
260 GapLeConnectionParameterUpdateRsp(
261 deviceInfo->handle, deviceInfo->paramUpdateReq->id, L2CAP_LE_CONNECTION_PARAMETERS_REJECTED);
262 }
263
264 return ret;
265 }
266
GAP_LeConnectionParameterRsp(const BtAddr * addr,uint8_t accept,const GapLeConnectionParameter * connParam)267 int GAP_LeConnectionParameterRsp(const BtAddr *addr, uint8_t accept, const GapLeConnectionParameter *connParam)
268 {
269 LOG_INFO("%{public}s:", __FUNCTION__);
270 int ret;
271
272 if (GapIsLeEnable() == false) {
273 return GAP_ERR_NOT_ENABLE;
274 }
275
276 if (GapLeRolesCheck(GAP_LE_ROLE_CENTRAL | GAP_LE_ROLE_PREIPHERAL) == false) {
277 ret = GAP_ERR_INVAL_STATE;
278 } else {
279 LeConnectionInfoBlock *connectionInfoBlock = GapGetLeConnectionInfoBlock();
280 LeDeviceInfo *deviceInfo = NULL;
281 deviceInfo = ListForEachData(connectionInfoBlock->deviceList, GapFindLeConnectionDeviceByAddr, (void *)addr);
282 if (deviceInfo != NULL && deviceInfo->paramUpdateReq != NULL) {
283 if (accept == GAP_ACCEPT) {
284 ret = GapAcceptConnectionParameterUpdate(deviceInfo, connParam);
285 } else if (accept == GAP_NOT_ACCEPT) {
286 ret = GapRejectConnectionParameterUpdate(deviceInfo);
287 } else {
288 ret = GAP_ERR_INVAL_PARAM;
289 }
290 } else {
291 ret = GAP_ERR_INVAL_STATE;
292 }
293 }
294
295 return ret;
296 }
297
GapLeSetHostChannelClassification(uint64_t channelMap)298 static int GapLeSetHostChannelClassification(uint64_t channelMap)
299 {
300 HciLeSetHostChannelClassificationParam hciCmdParam;
301 (void)memcpy_s(hciCmdParam.channelMap, sizeof(hciCmdParam.channelMap), &channelMap, sizeof(hciCmdParam.channelMap));
302
303 return HCI_LeSetHostChannelClassification(&hciCmdParam);
304 }
305
GapLeSetHostChannelClassificationComplete(const HciLeSetHostChannelClassificationReturnParam * param)306 void GapLeSetHostChannelClassificationComplete(const HciLeSetHostChannelClassificationReturnParam *param)
307 {
308 if (g_leConnUpdateCallback.callback.GapleSetHostChannelClassificationResult) {
309 g_leConnUpdateCallback.callback.GapleSetHostChannelClassificationResult(
310 param->status, g_leConnUpdateCallback.context);
311 }
312 }
313
GAP_LeSetHostChannelClassification(uint64_t channelMap)314 int GAP_LeSetHostChannelClassification(uint64_t channelMap)
315 {
316 LOG_INFO("%{public}s:", __FUNCTION__);
317 int ret;
318
319 if (GapIsLeEnable() == false) {
320 return GAP_ERR_NOT_ENABLE;
321 }
322
323 if (GapLeRolesCheck(GAP_LE_ROLE_CENTRAL | GAP_LE_ROLE_PREIPHERAL) == false) {
324 ret = GAP_ERR_INVAL_STATE;
325 } else {
326 ret = GapLeSetHostChannelClassification(channelMap);
327 }
328
329 return ret;
330 }
331
GapLeReadChannelMapComplete(const HciLeReadChannelMapReturnParam * param)332 void GapLeReadChannelMapComplete(const HciLeReadChannelMapReturnParam *param)
333 {
334 BtAddr addr = {0};
335 uint64_t channelMap = 0;
336
337 LeConnectionInfoBlock *connectionInfoBlock = GapGetLeConnectionInfoBlock();
338 LeDeviceInfo *deviceInfo = NULL;
339 deviceInfo = ListForEachData(
340 connectionInfoBlock->deviceList, GapFindLeConnectionDeviceByHandle, (void *)¶m->connectionHandle);
341 if (deviceInfo != NULL) {
342 (void)memcpy_s(&addr, sizeof(BtAddr), &deviceInfo->addr, sizeof(BtAddr));
343 (void)memcpy_s(&channelMap, sizeof(channelMap), param->channelMap, sizeof(param->channelMap));
344 }
345
346 if (deviceInfo != NULL) {
347 if (g_leConnUpdateCallback.callback.GapleReadChannelMapResult) {
348 g_leConnUpdateCallback.callback.GapleReadChannelMapResult(
349 param->status, &addr, channelMap, g_leConnUpdateCallback.context);
350 }
351 }
352 }
353
GAP_LeSetBondMode(uint8_t mode)354 int GAP_LeSetBondMode(uint8_t mode)
355 {
356 LOG_INFO("%{public}s: mode[%hhu]", __FUNCTION__, mode);
357 if (GapIsLeEnable() == false) {
358 return GAP_ERR_NOT_ENABLE;
359 }
360
361 LeLocalInfo *localInfo = GapGetLeLocalInfo();
362 localInfo->bondableMode = mode;
363
364 return GAP_SUCCESS;
365 }
366
GAP_LeSetSecurityMode(GAP_LeSecMode1Level mode1Level,GAP_LeSecMode2Level mode2Level)367 int GAP_LeSetSecurityMode(GAP_LeSecMode1Level mode1Level, GAP_LeSecMode2Level mode2Level)
368 {
369 LOG_INFO("%{public}s: mode1Level[%{public}d], mode2Level[%{public}d]", __FUNCTION__, mode1Level, mode2Level);
370 int ret;
371
372 if (GapIsLeEnable() == false) {
373 return GAP_ERR_NOT_ENABLE;
374 }
375 LeLocalInfo *localInfo = GapGetLeLocalInfo();
376 localInfo->mode1Level = mode1Level;
377 localInfo->mode2Level = mode2Level;
378 if (mode1Level == LE_MODE_1_LEVEL_4) {
379 BtmLocalVersionInformation version;
380 ret = BTM_GetLocalVersionInformation(&version);
381 if ((ret == BT_SUCCESS) && (version.hciVersion >= BLUETOOTH_CORE_SPECIFICATION_4_2)) {
382 ret = SMP_SetSecureConnOnlyMode(true);
383 } else {
384 ret = GAP_ERR_NOT_SUPPORT;
385 }
386 } else {
387 ret = SMP_SetSecureConnOnlyMode(false);
388 }
389 return ret;
390 }
391
GAP_LeGetSecurityStatus(const BtAddr * addr,GAP_LeSecurityStatus * status,uint8_t * encKeySize)392 int GAP_LeGetSecurityStatus(const BtAddr *addr, GAP_LeSecurityStatus *status, uint8_t *encKeySize)
393 {
394 int ret = GAP_SUCCESS;
395 if (GapIsLeEnable() == false) {
396 return GAP_ERR_NOT_ENABLE;
397 }
398 LeConnectionInfoBlock *connectionInfoBlock = GapGetLeConnectionInfoBlock();
399 LeDeviceInfo *deviceInfo = NULL;
400 deviceInfo = ListForEachData(connectionInfoBlock->deviceList, GapFindLeConnectionDeviceByAddr, (void *)addr);
401 if (deviceInfo != NULL) {
402 *status = deviceInfo->encryptionStatus;
403 *encKeySize = deviceInfo->keySize;
404 LOG_INFO("%{public}s:" BT_ADDR_FMT " status:%{public}d, keySize:%hhu",
405 __FUNCTION__,
406 BT_ADDR_FMT_OUTPUT(addr->addr),
407 *status,
408 *encKeySize);
409 } else {
410 ret = GAP_ERR_INVAL_STATE;
411 }
412
413 return ret;
414 }
415
GapLeDeviceNeedBond(const LeDeviceInfo * deviceInfo)416 bool GapLeDeviceNeedBond(const LeDeviceInfo *deviceInfo)
417 {
418 LeBondBlock *leBondBlock = GapGetLeBondBlock();
419 return (leBondBlock->isPairing == true) && (GapAddrCompare(&deviceInfo->addr, &leBondBlock->addr));
420 }
421
GapWaitExAdvTerminatedTimeoutTask(void * ctx)422 static void GapWaitExAdvTerminatedTimeoutTask(void *ctx)
423 {
424 LeDeviceInfo *deviceInfo = ListForEachData(GapGetLeConnectionInfoBlock()->deviceList, GapFindCmpListData, ctx);
425 if (deviceInfo != NULL) {
426 deviceInfo->ownAddrUpdated = true;
427 GapLeRequestSecurityProcess(deviceInfo);
428 if (GapLeDeviceNeedBond(deviceInfo)) {
429 GapLeDoPair(&deviceInfo->addr);
430 }
431 }
432 }
433
GapWaitExAdvTerminatedTimeout(void * dev)434 void GapWaitExAdvTerminatedTimeout(void *dev)
435 {
436 LOG_INFO("%{public}s: ", __FUNCTION__);
437 GapGeneralPointerInfo *ctx = MEM_MALLOC.alloc(sizeof(GapGeneralPointerInfo));
438 if (ctx == NULL) {
439 LOG_ERROR("%{public}s: Alloc error.", __FUNCTION__);
440 return;
441 }
442
443 ctx->pointer = dev;
444
445 int ret = GapRunTaskUnBlockProcess(GapWaitExAdvTerminatedTimeoutTask, ctx, NULL);
446 if (ret != BT_SUCCESS) {
447 LOG_ERROR("%{public}s: Task error:%{public}d.", __FUNCTION__, ret);
448 }
449 }
450
GapLeConnectionComplete(uint8_t status,uint16_t connectionHandle,const BtAddr * addr,uint8_t role,void * context)451 void GapLeConnectionComplete(uint8_t status, uint16_t connectionHandle, const BtAddr *addr, uint8_t role, void *context)
452 {
453 LeConnectionInfoBlock *leConnectionInfoBlock = NULL;
454 LeDeviceInfo *deviceInfo = NULL;
455
456 leConnectionInfoBlock = GapGetLeConnectionInfoBlock();
457 if (status == HCI_SUCCESS) {
458 deviceInfo = MEM_MALLOC.alloc(sizeof(LeDeviceInfo));
459 if (deviceInfo != NULL) {
460 (void)memset_s(deviceInfo, sizeof(LeDeviceInfo), 0x00, sizeof(LeDeviceInfo));
461 (void)memcpy_s(&deviceInfo->addr, sizeof(BtAddr), addr, sizeof(BtAddr));
462 deviceInfo->handle = connectionHandle;
463 BTM_GetLeConnectionAddress(connectionHandle, &deviceInfo->ownAddr, &deviceInfo->peerAddr);
464 LOG_DEBUG("%{public}s: own:" BT_ADDR_FMT " %hhu",
465 __FUNCTION__,
466 BT_ADDR_FMT_OUTPUT(deviceInfo->ownAddr.addr),
467 deviceInfo->ownAddr.type);
468 LOG_DEBUG("%{public}s: peer:" BT_ADDR_FMT " %hhu",
469 __FUNCTION__,
470 BT_ADDR_FMT_OUTPUT(deviceInfo->peerAddr.addr),
471 deviceInfo->peerAddr.type);
472 deviceInfo->role = role;
473 deviceInfo->alarm = AlarmCreate("LEWaitExAdvTerminated", false);
474 ListAddLast(leConnectionInfoBlock->deviceList, deviceInfo);
475 if (deviceInfo->role == HCI_ROLE_SLAVE) {
476 AlarmSet(deviceInfo->alarm, GAP_WAIT_EX_ADV_TERMINATED, GapWaitExAdvTerminatedTimeout, deviceInfo);
477 } else if (deviceInfo->role == HCI_ROLE_MASTER && GapLeDeviceNeedBond(deviceInfo)) {
478 GapLeDoPair(&deviceInfo->addr);
479 } else {
480 deviceInfo->ownAddrUpdated = true;
481 }
482 } else {
483 LOG_ERROR("%{public}s:alloc failed.", __FUNCTION__);
484 }
485 } else {
486 LeBondBlock *leBondBlock = GapGetLeBondBlock();
487 if (leBondBlock->isPairing == true && GapAddrCompare(addr, &leBondBlock->addr)) {
488 GapDoPairResultCallback(addr, status);
489 }
490 GapClearPairingStatus(addr);
491 }
492
493 if (status == HCI_SUCCESS) {
494 GapRequestSigningAlgorithmInfo(addr);
495 }
496 }
497
GapLeDisconnectionComplete(uint8_t status,uint16_t connectionHandle,uint8_t reason,void * context)498 void GapLeDisconnectionComplete(uint8_t status, uint16_t connectionHandle, uint8_t reason, void *context)
499 {
500 LeConnectionInfoBlock *leConnectionInfoBlock = NULL;
501 LeDeviceInfo *deviceInfo = NULL;
502
503 leConnectionInfoBlock = GapGetLeConnectionInfoBlock();
504 if (status == HCI_SUCCESS) {
505 deviceInfo =
506 ListForEachData(leConnectionInfoBlock->deviceList, GapFindLeConnectionDeviceByHandle, &connectionHandle);
507 if (deviceInfo != NULL) {
508 ListRemoveNode(leConnectionInfoBlock->deviceList, deviceInfo);
509 }
510 }
511 }
512
GapReceiveL2capParameterUpdateReq(uint16_t aclHandle,uint8_t id,const L2capLeConnectionParameter * param,void * ctx)513 void GapReceiveL2capParameterUpdateReq(
514 uint16_t aclHandle, uint8_t id, const L2capLeConnectionParameter *param, void *ctx)
515 {
516 BtAddr addr = {0};
517 bool doReject = false;
518
519 LeDeviceInfo *deviceInfo =
520 ListForEachData(GapGetLeConnectionInfoBlock()->deviceList, GapFindLeConnectionDeviceByHandle, &aclHandle);
521 if (deviceInfo != NULL) {
522 (void)memcpy_s(&addr, sizeof(BtAddr), &deviceInfo->addr, sizeof(BtAddr));
523 if (deviceInfo->paramUpdateReq != NULL) {
524 doReject = true;
525 } else {
526 deviceInfo->paramUpdateReq = MEM_MALLOC.alloc(sizeof(LeConnParamUpdateReq));
527 if (deviceInfo->paramUpdateReq != NULL) {
528 deviceInfo->paramUpdateReq->status = GAP_LE_CONN_PARAM_UPDATE_RECV_L2CAP;
529 deviceInfo->paramUpdateReq->id = id;
530 } else {
531 doReject = true;
532 }
533 }
534 }
535
536 if (doReject) {
537 GapLeConnectionParameterUpdateRsp(aclHandle, id, L2CAP_LE_CONNECTION_PARAMETERS_REJECTED);
538 } else {
539 if (g_leConnUpdateCallback.callback.leConnectionParameterReq) {
540 g_leConnUpdateCallback.callback.leConnectionParameterReq(&addr,
541 param->connIntervalMin,
542 param->connIntervalMax,
543 param->connLatency,
544 param->supervisionTimeout,
545 g_leConnUpdateCallback.context);
546 } else {
547 GapLeConnectionParameterUpdateRsp(aclHandle, id, L2CAP_LE_CONNECTION_PARAMETERS_REJECTED);
548 }
549 }
550 }
551
GapReceiveL2capParameterUpdateRsp(uint16_t aclHandle,uint16_t result,void * ctx)552 void GapReceiveL2capParameterUpdateRsp(uint16_t aclHandle, uint16_t result, void *ctx)
553 {
554 BtAddr addr = {0};
555
556 LeDeviceInfo *deviceInfo =
557 ListForEachData(GapGetLeConnectionInfoBlock()->deviceList, GapFindLeConnectionDeviceByHandle, &aclHandle);
558 if (deviceInfo != NULL) {
559 (void)memcpy_s(&addr, sizeof(BtAddr), &deviceInfo->addr, sizeof(BtAddr));
560 }
561
562 if (result == L2CAP_LE_CONNECTION_PARAMETERS_REJECTED) {
563 if (g_leConnUpdateCallback.callback.leConnectionUpdateComplete) {
564 g_leConnUpdateCallback.callback.leConnectionUpdateComplete(
565 GAP_STATUS_FAILED, &addr, 0, 0, 0, g_leConnUpdateCallback.context);
566 }
567 }
568 }
569
570 #endif