1 /*
2  * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "common/bt_target.h"
8 #include <stdlib.h>
9 #include <string.h>
10 #include "btc/btc_common.h"
11 #include "btc/btc_dm.h"
12 #include "btc/btc_main.h"
13 #include "common/bt_trace.h"
14 #include "common/bt_target.h"
15 #include "btc/btc_storage.h"
16 #include "btc/btc_ble_storage.h"
17 #include "btc_gap_ble.h"
18 #include "btm_int.h"
19 #include "bta/bta_api.h"
20 #include "bta/bta_gatt_api.h"
21 #include "osi/allocator.h"
22 #include "btc/btc_manage.h"
23 
24 
25 #if (BTC_GAP_BT_INCLUDED == TRUE)
26 #include "btc_gap_bt.h"
27 #endif /* BTC_GAP_BT_INCLUDED == TRUE */
28 /******************************************************************************
29 **  Constants & Macros
30 ******************************************************************************/
31 #define BTA_SERVICE_ID_TO_SERVICE_MASK(id)       (1 << (id))
32 
33 /******************************************************************************
34 **  Static variables
35 ******************************************************************************/
36 #if BTC_DYNAMIC_MEMORY == FALSE
37 btc_dm_cb_t btc_dm_cb = {0};
38 #else
39 btc_dm_cb_t *btc_dm_cb_ptr;
40 #endif
41 
42 /******************************************************************************
43 **  Static functions
44 ******************************************************************************/
45 /******************************************************************************
46 **  Externs
47 ******************************************************************************/
48 #if BTC_AV_INCLUDED
49 extern bt_status_t btc_av_source_execute_service(BOOLEAN b_enable);
50 extern bt_status_t btc_av_sink_execute_service(BOOLEAN b_enable);
51 #endif
52 #if BTC_HF_INCLUDED
53 extern bt_status_t btc_hf_execute_service(BOOLEAN b_enable);
54 #endif
55 #if BTC_HF_CLIENT_INCLUDED
56 extern bt_status_t btc_hf_client_execute_service(BOOLEAN b_enable);
57 #endif
58 /******************************************************************************
59 **  Functions
60 ******************************************************************************/
btc_dm_sec_arg_deep_free(btc_msg_t * msg)61 static void btc_dm_sec_arg_deep_free(btc_msg_t *msg)
62 {
63     btc_dm_sec_args_t *arg = (btc_dm_sec_args_t *)(msg->arg);
64     if (msg->act == BTA_DM_BLE_KEY_EVT) {
65         osi_free(arg->sec.ble_key.p_key_value);
66     }
67 }
68 
btc_dm_sec_arg_deep_copy(btc_msg_t * msg,void * dst,void * src)69 void btc_dm_sec_arg_deep_copy(btc_msg_t *msg, void *dst, void *src)
70 {
71     tBTA_DM_SEC *dst_dm_sec = (tBTA_DM_SEC *)dst;
72     tBTA_DM_SEC *src_dm_sec = (tBTA_DM_SEC *)src;
73 
74     if (!src_dm_sec) {
75         return;
76     }
77 
78     assert(dst_dm_sec);
79     memcpy(dst_dm_sec, src_dm_sec, sizeof(tBTA_DM_SEC));
80 
81     if (msg->act == BTA_DM_BLE_KEY_EVT) {
82         dst_dm_sec->ble_key.p_key_value = osi_malloc(sizeof(tBTM_LE_KEY_VALUE));
83         assert(src_dm_sec->ble_key.p_key_value);
84         assert(dst_dm_sec->ble_key.p_key_value);
85         memcpy(dst_dm_sec->ble_key.p_key_value, src_dm_sec->ble_key.p_key_value, sizeof(tBTM_LE_KEY_VALUE));
86     }
87 }
88 
89 /*******************************************************************************
90 **
91 ** Function         btc_dm_evt
92 **
93 ** Description      Switches context from BTE to BTC for all DM events
94 **
95 ** Returns          void
96 **
97 *******************************************************************************/
98 
btc_dm_sec_evt(tBTA_DM_SEC_EVT event,tBTA_DM_SEC * data)99 void btc_dm_sec_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC *data)
100 {
101     btc_msg_t msg = {0};
102 
103     msg.sig = BTC_SIG_API_CB;
104     msg.pid = BTC_PID_DM_SEC;
105     msg.act = event;
106 
107     btc_transfer_context(&msg, (btc_dm_sec_args_t *)data,
108                             data == NULL ? 0 : sizeof(btc_dm_sec_args_t),
109                             btc_dm_sec_arg_deep_copy, btc_dm_sec_arg_deep_free);
110 }
111 
btc_enable_bluetooth_evt(tBTA_STATUS status)112 static void btc_enable_bluetooth_evt(tBTA_STATUS status)
113 {
114     if (status == BTA_SUCCESS) {
115         future_ready(*btc_main_get_future_p(BTC_MAIN_ENABLE_FUTURE), FUTURE_SUCCESS);
116     } else {
117         future_ready(*btc_main_get_future_p(BTC_MAIN_ENABLE_FUTURE), FUTURE_FAIL);
118     }
119 }
120 
btc_disable_bluetooth_evt(void)121 static void btc_disable_bluetooth_evt(void)
122 {
123     BTC_TRACE_DEBUG("%s", __FUNCTION__);
124 
125     future_ready(*btc_main_get_future_p(BTC_MAIN_DISABLE_FUTURE), FUTURE_SUCCESS);
126 }
127 
128 #if (SMP_INCLUDED == TRUE)
129 #if (BLE_INCLUDED == TRUE)
btc_dm_load_ble_local_keys(void)130 void btc_dm_load_ble_local_keys(void)
131 {
132     memset(&btc_dm_cb.ble_local_key_cb, 0, sizeof(btc_dm_local_key_cb_t));
133 
134     if (btc_storage_get_ble_local_key(BTC_LE_LOCAL_KEY_ER,(char*)&btc_dm_cb.ble_local_key_cb.er[0],
135                                       BT_OCTET16_LEN)== BT_STATUS_SUCCESS) {
136         btc_dm_cb.ble_local_key_cb.is_er_rcvd = TRUE;
137         BTC_TRACE_DEBUG("%s BLE ER key loaded",__func__ );
138     }
139 
140     if ((btc_storage_get_ble_local_key(BTC_LE_LOCAL_KEY_IR,(char*)&btc_dm_cb.ble_local_key_cb.id_keys.ir[0],
141                                        BT_OCTET16_LEN)== BT_STATUS_SUCCESS )&&
142             (btc_storage_get_ble_local_key(BTC_LE_LOCAL_KEY_IRK, (char*)&btc_dm_cb.ble_local_key_cb.id_keys.irk[0],
143                                            BT_OCTET16_LEN)== BT_STATUS_SUCCESS)&&
144             (btc_storage_get_ble_local_key(BTC_LE_LOCAL_KEY_DHK,(char*)&btc_dm_cb.ble_local_key_cb.id_keys.dhk[0],
145                                            BT_OCTET16_LEN)== BT_STATUS_SUCCESS)) {
146         btc_dm_cb.ble_local_key_cb.is_id_keys_rcvd = TRUE;
147         BTC_TRACE_DEBUG("%s BLE ID keys loaded", __func__);
148     }
149 
150 }
btc_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK * p_key_mask,BT_OCTET16 er,tBTA_BLE_LOCAL_ID_KEYS * p_id_keys)151 void btc_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er,
152                                tBTA_BLE_LOCAL_ID_KEYS *p_id_keys)
153 {
154     if (btc_dm_cb.ble_local_key_cb.is_er_rcvd ) {
155         memcpy(&er[0], &btc_dm_cb.ble_local_key_cb.er[0], sizeof(BT_OCTET16));
156         *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ER;
157     }
158 
159     if (btc_dm_cb.ble_local_key_cb.is_id_keys_rcvd) {
160         memcpy(&p_id_keys->ir[0], &btc_dm_cb.ble_local_key_cb.id_keys.ir[0], sizeof(BT_OCTET16));
161         memcpy(&p_id_keys->irk[0],  &btc_dm_cb.ble_local_key_cb.id_keys.irk[0], sizeof(BT_OCTET16));
162         memcpy(&p_id_keys->dhk[0],  &btc_dm_cb.ble_local_key_cb.id_keys.dhk[0], sizeof(BT_OCTET16));
163         *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ID;
164     }
165     BTC_TRACE_DEBUG("%s  *p_key_mask=0x%02x",__func__,   *p_key_mask);
166 }
167 
168 
btc_dm_remove_ble_bonding_keys(void)169 static void btc_dm_remove_ble_bonding_keys(void)
170 {
171     bt_bdaddr_t bd_addr;
172     BTC_TRACE_DEBUG("%s\n",__func__);
173 
174     bdcpy(bd_addr.address, btc_dm_cb.pairing_cb.bd_addr);
175 
176     btc_storage_remove_gatt_cl_supp_feat(&bd_addr);
177     btc_storage_remove_gatt_db_hash(&bd_addr);
178     btc_storage_remove_remote_addr_type(&bd_addr, false);
179     btc_storage_remove_ble_dev_auth_mode(&bd_addr, false);
180     btc_storage_remove_ble_dev_type(&bd_addr, false);
181     btc_storage_remove_ble_bonding_keys(&bd_addr);
182 }
183 
btc_dm_save_ble_bonding_keys(void)184 static void btc_dm_save_ble_bonding_keys(void)
185 {
186     if (!(btc_dm_cb.pairing_cb.ble.is_penc_key_rcvd || btc_dm_cb.pairing_cb.ble.is_pid_key_rcvd || btc_dm_cb.pairing_cb.ble.is_pcsrk_key_rcvd ||
187          btc_dm_cb.pairing_cb.ble.is_lenc_key_rcvd || btc_dm_cb.pairing_cb.ble.is_lcsrk_key_rcvd || btc_dm_cb.pairing_cb.ble.is_lidk_key_rcvd)) {
188         return ;
189     }
190     bt_bdaddr_t bd_addr;
191 
192     bdcpy(bd_addr.address, btc_dm_cb.pairing_cb.bd_addr);
193 
194     btc_storage_set_ble_dev_type(&bd_addr, false);
195     BTC_TRACE_DEBUG("%s, penc = %d, pid = %d", __func__, btc_dm_cb.pairing_cb.ble.is_penc_key_rcvd, btc_dm_cb.pairing_cb.ble.is_pid_key_rcvd);
196     if (btc_dm_cb.pairing_cb.ble.is_penc_key_rcvd) {
197         btc_storage_add_ble_bonding_key(&bd_addr,
198                                         (char *) &btc_dm_cb.pairing_cb.ble.penc_key,
199                                         BTM_LE_KEY_PENC,
200                                         sizeof(tBTM_LE_PENC_KEYS));
201         btc_dm_cb.pairing_cb.ble.is_penc_key_rcvd = false;
202     }
203 
204     if (btc_dm_cb.pairing_cb.ble.is_pid_key_rcvd) {
205         btc_storage_add_ble_bonding_key(&bd_addr,
206                                         (char *) &btc_dm_cb.pairing_cb.ble.pid_key,
207                                         BTM_LE_KEY_PID,
208                                         sizeof(tBTM_LE_PID_KEYS));
209         btc_dm_cb.pairing_cb.ble.is_pid_key_rcvd = false;
210     }
211 
212 
213     if (btc_dm_cb.pairing_cb.ble.is_pcsrk_key_rcvd) {
214         btc_storage_add_ble_bonding_key(&bd_addr,
215                                         (char *) &btc_dm_cb.pairing_cb.ble.pcsrk_key,
216                                         BTM_LE_KEY_PCSRK,
217                                         sizeof(tBTM_LE_PCSRK_KEYS));
218         btc_dm_cb.pairing_cb.ble.is_pcsrk_key_rcvd = false;
219     }
220 
221 
222     if (btc_dm_cb.pairing_cb.ble.is_lenc_key_rcvd) {
223         btc_storage_add_ble_bonding_key(&bd_addr,
224                                         (char *) &btc_dm_cb.pairing_cb.ble.lenc_key,
225                                         BTM_LE_KEY_LENC,
226                                         sizeof(tBTM_LE_LENC_KEYS));
227         btc_dm_cb.pairing_cb.ble.is_lenc_key_rcvd = false;
228     }
229 
230     if (btc_dm_cb.pairing_cb.ble.is_lcsrk_key_rcvd) {
231         btc_storage_add_ble_bonding_key(&bd_addr,
232                                         (char *) &btc_dm_cb.pairing_cb.ble.lcsrk_key,
233                                         BTM_LE_KEY_LCSRK,
234                                         sizeof(tBTM_LE_LCSRK_KEYS));
235         btc_dm_cb.pairing_cb.ble.is_lcsrk_key_rcvd = false;
236     }
237 
238     if (btc_dm_cb.pairing_cb.ble.is_lidk_key_rcvd) {
239         btc_storage_add_ble_bonding_key(&bd_addr,
240                                         NULL,
241                                         BTM_LE_KEY_LID,
242                                         0);
243         btc_dm_cb.pairing_cb.ble.is_lidk_key_rcvd = false;
244     }
245 }
246 
btc_dm_ble_auth_cmpl_evt(tBTA_DM_AUTH_CMPL * p_auth_cmpl)247 static void btc_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
248 {
249     /* Save link key, if not temporary */
250     BTC_TRACE_DEBUG("%s, status = %d", __func__, p_auth_cmpl->success);
251     bt_status_t status = BT_STATUS_FAIL;
252     int addr_type;
253     bt_bdaddr_t bdaddr;
254     bdcpy(bdaddr.address, p_auth_cmpl->bd_addr);
255     bdcpy(btc_dm_cb.pairing_cb.bd_addr, p_auth_cmpl->bd_addr);
256 
257     if (p_auth_cmpl->success) {
258         status = BT_STATUS_SUCCESS;
259         BTC_TRACE_DEBUG ("%s, -  p_auth_cmpl->bd_addr: %08x%04x", __func__,
260                              (p_auth_cmpl->bd_addr[0] << 24) + (p_auth_cmpl->bd_addr[1] << 16) + (p_auth_cmpl->bd_addr[2] << 8) + p_auth_cmpl->bd_addr[3],
261                              (p_auth_cmpl->bd_addr[4] << 8) + p_auth_cmpl->bd_addr[5]);
262 
263         // Check if need to save BLE keys
264         if((p_auth_cmpl->auth_mode & SMP_AUTH_GEN_BOND) == 0) {
265             return;
266         }
267 
268          if (btc_storage_get_remote_addr_type(&bdaddr, &addr_type) != BT_STATUS_SUCCESS) {
269             btc_storage_set_remote_addr_type(&bdaddr, p_auth_cmpl->addr_type, true);
270         }
271         btc_storage_set_ble_dev_auth_mode(&bdaddr, p_auth_cmpl->auth_mode, true);
272         btc_dm_save_ble_bonding_keys();
273     } else {
274         /*Map the HCI fail reason  to  bt status  */
275         switch (p_auth_cmpl->fail_reason) {
276         case BTA_DM_AUTH_SMP_PAIR_AUTH_FAIL:
277         case BTA_DM_AUTH_SMP_CONFIRM_VALUE_FAIL:
278             btc_dm_remove_ble_bonding_keys();
279             status = BT_STATUS_AUTH_FAILURE;
280             break;
281         case BTA_DM_AUTH_SMP_PAIR_NOT_SUPPORT:
282             status = BT_STATUS_AUTH_REJECTED;
283             break;
284         default:
285             btc_dm_remove_ble_bonding_keys();
286             status =  BT_STATUS_FAIL;
287             break;
288         }
289 
290     }
291 
292 #if (CONFIG_BT_STACK_NO_LOG)
293     (void) status;
294 #endif
295     BTC_TRACE_DEBUG("%s, authentication status = %x", __func__, status);
296     return;
297 
298 }
299 #endif  ///BLE_INCLUDED == TRUE
300 #endif ///SMP_INCLUDED == TRUE
301 
btc_dm_link_up_evt(tBTA_DM_LINK_UP * p_link_up)302 static void btc_dm_link_up_evt(tBTA_DM_LINK_UP *p_link_up)
303 {
304     BD_ADDR bd_addr;
305     bt_bdaddr_t bt_bdaddr;
306 
307 
308     if (p_link_up->sc_downgrade == 1) {
309         memcpy(bt_bdaddr.address, p_link_up->bd_addr, sizeof(BD_ADDR));
310         if (btc_storage_remove_bonded_device(&bt_bdaddr) == BT_STATUS_SUCCESS) {
311             memcpy(bd_addr, p_link_up->bd_addr, sizeof(BD_ADDR));
312             if (BTA_DmRemoveDevice(bd_addr, BT_TRANSPORT_BR_EDR) == BTA_SUCCESS) {
313                 BTC_TRACE_EVENT(" %s() Bonding information removed.", __FUNCTION__);
314             } else {
315                 BTC_TRACE_ERROR(" %s() BTA_DmRemoveDevice error", __FUNCTION__);
316             }
317         } else {
318             BTC_TRACE_ERROR(" %s() btc_storage_remove_bonded_device error", __FUNCTION__);
319         }
320     }
321 }
322 
btc_dm_auth_cmpl_evt(tBTA_DM_AUTH_CMPL * p_auth_cmpl)323 static void btc_dm_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
324 {
325     /* Save link key, if not temporary */
326     bt_bdaddr_t bd_addr;
327     bt_status_t status;
328     BTC_TRACE_DEBUG("%s: bond state success %d, present %d, type%d\n", __func__, p_auth_cmpl->success,
329               p_auth_cmpl->key_present, p_auth_cmpl->key_type);
330 
331     bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
332     if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) ) {
333 #if 0
334         if ((p_auth_cmpl->key_type < HCI_LKEY_TYPE_DEBUG_COMB) ||
335                 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_AUTH_COMB) ||
336                 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_CHANGED_COMB) ||
337                 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_AUTH_COMB_P_256)
338            )
339 #endif
340             if (1) {
341                 bt_status_t ret __attribute__((unused));
342                 BTC_TRACE_DEBUG("%s: Storing link key. key_type=0x%x",
343                           __FUNCTION__, p_auth_cmpl->key_type);
344                 ret = btc_storage_add_bonded_device(&bd_addr,
345                                                     p_auth_cmpl->key, p_auth_cmpl->key_type,
346                                                     16, p_auth_cmpl->sc_support);
347                 BTC_ASSERTC(ret == BT_STATUS_SUCCESS, "storing link key failed", ret);
348             } else {
349                 BTC_TRACE_DEBUG("%s: Temporary key. Not storing. key_type=0x%x",
350                           __FUNCTION__, p_auth_cmpl->key_type);
351             }
352     }
353 
354     // Skip SDP for certain  HID Devices
355     if (p_auth_cmpl->success) {
356         status = BT_STATUS_SUCCESS;
357     } else {
358         // Map the HCI fail reason  to  bt status
359         switch (p_auth_cmpl->fail_reason) {
360         case HCI_ERR_PAGE_TIMEOUT:
361             BTC_TRACE_WARNING("%s() - Pairing timeout; retrying () ...", __FUNCTION__);
362             return;
363         /* Fall-through */
364         case HCI_ERR_CONNECTION_TOUT:
365             status =  BT_STATUS_RMT_DEV_DOWN;
366             break;
367 
368         case HCI_ERR_PAIRING_NOT_ALLOWED:
369             status = BT_STATUS_AUTH_REJECTED;
370             break;
371 
372         case HCI_ERR_LMP_RESPONSE_TIMEOUT:
373             status =  BT_STATUS_AUTH_FAILURE;
374             break;
375 
376         /* map the auth failure codes, so we can retry pairing if necessary */
377         case HCI_ERR_AUTH_FAILURE:
378         case HCI_ERR_KEY_MISSING:
379             btc_storage_remove_bonded_device(&bd_addr);
380         case HCI_ERR_HOST_REJECT_SECURITY:
381         case HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE:
382         case HCI_ERR_UNIT_KEY_USED:
383         case HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED:
384         case HCI_ERR_INSUFFCIENT_SECURITY:
385         case HCI_ERR_PEER_USER:
386         case HCI_ERR_UNSPECIFIED:
387             BTC_TRACE_ERROR(" %s() Authentication fail reason %d",
388                       __FUNCTION__, p_auth_cmpl->fail_reason);
389             /* if autopair attempts are more than 1, or not attempted */
390             status =  BT_STATUS_AUTH_FAILURE;
391             break;
392         default:
393             status =  BT_STATUS_FAIL;
394             break;
395         }
396     }
397 #if (BTC_GAP_BT_INCLUDED == TRUE)
398     esp_bt_gap_cb_param_t param;
399     bt_status_t ret;
400     btc_msg_t *msg;
401 
402     msg = (btc_msg_t *)osi_malloc(sizeof(btc_msg_t) + sizeof(esp_bt_gap_cb_param_t));
403     if (msg == NULL) {
404         BTC_TRACE_ERROR("%s malloc fail", __func__);
405         return;
406     }
407     msg->sig = BTC_SIG_API_CB;
408     msg->pid = BTC_PID_GAP_BT;
409     msg->act = BTC_GAP_BT_AUTH_CMPL_EVT;
410     param.auth_cmpl.stat = status;
411     memcpy(param.auth_cmpl.bda, p_auth_cmpl->bd_addr, ESP_BD_ADDR_LEN);
412     memcpy(param.auth_cmpl.device_name, p_auth_cmpl->bd_name, ESP_BT_GAP_MAX_BDNAME_LEN + 1);
413     memcpy(msg->arg, &param, sizeof(esp_bt_gap_cb_param_t));
414 
415     ret = btc_inter_profile_call(msg);
416     osi_free(msg);
417 
418     if (ret != BT_STATUS_SUCCESS) {
419         BTC_TRACE_ERROR("%s btc_inter_profile_call failed\n", __func__);
420     }
421 
422 #endif /// BTC_GAP_BT_INCLUDED == TRUE
423     (void) status;
424 }
425 
btc_dm_pin_req_evt(tBTA_DM_PIN_REQ * p_pin_req)426 static void btc_dm_pin_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
427 {
428 #if (BTC_GAP_BT_INCLUDED == TRUE)
429     esp_bt_gap_cb_param_t param;
430     bt_status_t ret;
431     btc_msg_t *msg;
432 
433     msg = (btc_msg_t *)osi_malloc(sizeof(btc_msg_t) + sizeof(esp_bt_gap_cb_param_t));
434     if (msg == NULL) {
435         BTC_TRACE_ERROR("%s malloc fail", __func__);
436         return;
437     }
438     msg->sig = BTC_SIG_API_CB;
439     msg->pid = BTC_PID_GAP_BT;
440     msg->act = BTC_GAP_BT_PIN_REQ_EVT;
441     param.pin_req.min_16_digit = p_pin_req->min_16_digit;
442     memcpy(param.pin_req.bda, p_pin_req->bd_addr, ESP_BD_ADDR_LEN);
443     memcpy(msg->arg, &param, sizeof(esp_bt_gap_cb_param_t));
444 
445     ret = btc_inter_profile_call(msg);
446     osi_free(msg);
447 
448     if (ret != BT_STATUS_SUCCESS) {
449         BTC_TRACE_ERROR("%s btc_inter_profile_call failed\n", __func__);
450     }
451 #endif /// BTC_GAP_BT_INCLUDED == TRUE
452 }
453 
454 #if (BT_SSP_INCLUDED == TRUE)
btc_dm_sp_cfm_req_evt(tBTA_DM_SP_CFM_REQ * p_cfm_req)455 static void btc_dm_sp_cfm_req_evt(tBTA_DM_SP_CFM_REQ *p_cfm_req)
456 {
457 #if (BTC_GAP_BT_INCLUDED == TRUE)
458     if (p_cfm_req->just_works) {
459         // just work, not show to users.
460         BTA_DmConfirm(p_cfm_req->bd_addr, true);
461         return;
462     }
463 
464     esp_bt_gap_cb_param_t param;
465     bt_status_t ret;
466     btc_msg_t *msg;
467 
468     msg = (btc_msg_t *)osi_malloc(sizeof(btc_msg_t) + sizeof(esp_bt_gap_cb_param_t));
469     if (msg == NULL) {
470         BTC_TRACE_ERROR("%s malloc fail", __func__);
471         return;
472     }
473     msg->sig = BTC_SIG_API_CB;
474     msg->pid = BTC_PID_GAP_BT;
475     msg->act = BTC_GAP_BT_CFM_REQ_EVT;
476     param.cfm_req.num_val = p_cfm_req->num_val;
477     memcpy(param.cfm_req.bda, p_cfm_req->bd_addr, ESP_BD_ADDR_LEN);
478     memcpy(msg->arg, &param, sizeof(esp_bt_gap_cb_param_t));
479 
480     ret = btc_inter_profile_call(msg);
481     osi_free(msg);
482 
483     if (ret != BT_STATUS_SUCCESS) {
484         BTC_TRACE_ERROR("%s btc_inter_profile_call failed\n", __func__);
485     }
486 #endif /// BTC_GAP_BT_INCLUDED == TRUE
487 }
488 
btc_dm_sp_key_notif_evt(tBTA_DM_SP_KEY_NOTIF * p_key_notif)489 static void btc_dm_sp_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_key_notif)
490 {
491 #if (BTC_GAP_BT_INCLUDED == TRUE)
492     esp_bt_gap_cb_param_t param;
493     bt_status_t ret;
494     btc_msg_t *msg;
495 
496     msg = (btc_msg_t *)osi_malloc(sizeof(btc_msg_t) + sizeof(esp_bt_gap_cb_param_t));
497     if (msg == NULL) {
498         BTC_TRACE_ERROR("%s malloc fail", __func__);
499         return;
500     }
501     msg->sig = BTC_SIG_API_CB;
502     msg->pid = BTC_PID_GAP_BT;
503     msg->act = BTC_GAP_BT_KEY_NOTIF_EVT;
504     param.key_notif.passkey = p_key_notif->passkey;
505     memcpy(param.key_notif.bda, p_key_notif->bd_addr, ESP_BD_ADDR_LEN);
506     memcpy(msg->arg, &param, sizeof(esp_bt_gap_cb_param_t));
507 
508     ret = btc_inter_profile_call(msg);
509     osi_free(msg);
510 
511     if (ret != BT_STATUS_SUCCESS) {
512         BTC_TRACE_ERROR("%s btc_inter_profile_call failed\n", __func__);
513     }
514 #endif /// BTC_GAP_BT_INCLUDED == TRUE
515 }
516 
btc_dm_sp_key_req_evt(tBTA_DM_SP_KEY_REQ * p_key_req)517 static void btc_dm_sp_key_req_evt(tBTA_DM_SP_KEY_REQ *p_key_req)
518 {
519 #if (BTC_GAP_BT_INCLUDED == TRUE)
520     esp_bt_gap_cb_param_t param;
521     bt_status_t ret;
522     btc_msg_t *msg;
523 
524     msg = (btc_msg_t *)osi_malloc(sizeof(btc_msg_t) + sizeof(esp_bt_gap_cb_param_t));
525     if (msg == NULL) {
526         BTC_TRACE_ERROR("%s malloc fail", __func__);
527         return;
528     }
529     msg->sig = BTC_SIG_API_CB;
530     msg->pid = BTC_PID_GAP_BT;
531     msg->act = BTC_GAP_BT_KEY_REQ_EVT;
532     memcpy(param.key_req.bda, p_key_req->bd_addr, ESP_BD_ADDR_LEN);
533     memcpy(msg->arg, &param, sizeof(esp_bt_gap_cb_param_t));
534 
535     ret = btc_inter_profile_call(msg);
536     osi_free(msg);
537 
538     if (ret != BT_STATUS_SUCCESS) {
539         BTC_TRACE_ERROR("%s btc_inter_profile_call failed\n", __func__);
540     }
541 #endif /// BTC_GAP_BT_INCLUDED == TRUE
542 }
543 #endif /// BT_SSP_INCLUDED == TRUE
544 
btc_dm_dev_unpaired_evt(tBTA_DM_LINK_DOWN * p_link_down)545 static void btc_dm_dev_unpaired_evt(tBTA_DM_LINK_DOWN *p_link_down)
546 {
547     esp_bt_gap_cb_param_t param;
548     BTC_TRACE_DEBUG("%s",__func__);
549     memcpy(param.remove_bond_dev_cmpl.bda, p_link_down->bd_addr, ESP_BD_ADDR_LEN);
550     btm_set_bond_type_dev(p_link_down->bd_addr, BOND_TYPE_UNKNOWN);
551     if (p_link_down->status == HCI_SUCCESS) {
552         //remove the bonded key in the config and nvs flash.
553         param.remove_bond_dev_cmpl.status = btc_storage_remove_bonded_device((bt_bdaddr_t *)param.remove_bond_dev_cmpl.bda);
554     } else {
555         param.remove_bond_dev_cmpl.status = ESP_BT_STATUS_FAIL;
556     }
557 
558 #if (BTC_GAP_BT_INCLUDED == TRUE)
559     bt_status_t ret;
560     btc_msg_t *msg;
561 
562     msg = (btc_msg_t *)osi_malloc(sizeof(btc_msg_t) + sizeof(esp_bt_gap_cb_param_t));
563     if (msg == NULL) {
564         BTC_TRACE_ERROR("%s malloc fail", __func__);
565         return;
566     }
567     msg->sig = BTC_SIG_API_CB;
568     msg->pid = BTC_PID_GAP_BT;
569     msg->act = BTC_GAP_BT_REMOVE_BOND_DEV_COMPLETE_EVT;
570     memcpy(msg->arg, &param, sizeof(esp_bt_gap_cb_param_t));
571 
572     ret = btc_inter_profile_call(msg);
573     osi_free(msg);
574 
575     if (ret != BT_STATUS_SUCCESS) {
576         BTC_TRACE_ERROR("%s btc_inter_profile_call failed\n", __func__);
577     }
578 #endif /// BTC_GAP_BT_INCLUDED == TRUE
579 }
580 
581 
582 #if (BTC_DM_PM_INCLUDED == TRUE)
btc_dm_pm_mode_chg_evt(tBTA_DM_MODE_CHG * p_mode_chg)583 static void btc_dm_pm_mode_chg_evt(tBTA_DM_MODE_CHG *p_mode_chg)
584 {
585     esp_bt_gap_cb_param_t param;
586     bt_status_t ret;
587     btc_msg_t *msg;
588 
589     msg = (btc_msg_t *)osi_malloc(sizeof(btc_msg_t) + sizeof(esp_bt_gap_cb_param_t));
590     if (msg == NULL) {
591         BTC_TRACE_ERROR("%s malloc fail", __func__);
592         return;
593     }
594     msg->sig = BTC_SIG_API_CB;
595     msg->pid = BTC_PID_GAP_BT;
596     msg->act = BTC_GAP_BT_MODE_CHG_EVT;
597     memcpy(param.mode_chg.bda, p_mode_chg->bd_addr, ESP_BD_ADDR_LEN);
598     param.mode_chg.mode = p_mode_chg->mode;
599     memcpy(msg->arg, &param, sizeof(esp_bt_gap_cb_param_t));
600 
601     ret = btc_inter_profile_call(msg);
602     osi_free(msg);
603 
604     if (ret != BT_STATUS_SUCCESS) {
605         BTC_TRACE_ERROR("%s btc_inter_profile_call failed\n", __func__);
606     }
607 }
608 #endif /// BTC_DM_PM_INCLUDED == TRUE
609 
btc_get_enabled_services_mask(void)610 tBTA_SERVICE_MASK btc_get_enabled_services_mask(void)
611 {
612     return btc_dm_cb.btc_enabled_services;
613 }
614 
btc_clear_services_mask(void)615 void btc_clear_services_mask(void)
616 {
617     btc_dm_cb.btc_enabled_services = 0;
618 }
619 
btc_in_execute_service_request(tBTA_SERVICE_ID service_id,BOOLEAN b_enable)620 static bt_status_t btc_in_execute_service_request(tBTA_SERVICE_ID service_id,
621         BOOLEAN b_enable)
622 {
623     BTC_TRACE_DEBUG("%s service_id: %d\n", __FUNCTION__, service_id);
624     /* Check the service_ID and invoke the profile's BT state changed API */
625     switch (service_id) {
626 #if BTC_AV_INCLUDED
627     case BTA_A2DP_SOURCE_SERVICE_ID:
628         btc_av_source_execute_service(b_enable);
629         break;
630     case BTA_A2DP_SINK_SERVICE_ID:
631         btc_av_sink_execute_service(b_enable);
632         break;
633 #endif
634 #if BTC_HF_INCLUDED
635     case BTA_HFP_SERVICE_ID:
636         btc_hf_execute_service(b_enable);
637         break;
638 #endif /* #if BTC_HF_INCLUDED */
639 #if BTC_HF_CLIENT_INCLUDED
640     case BTA_HFP_HS_SERVICE_ID:
641         btc_hf_client_execute_service(b_enable);
642         break;
643 #endif /* #if BTC_HF_CLIENT_INCLUDED */
644     default:
645         BTC_TRACE_ERROR("%s: Unknown service being enabled\n", __FUNCTION__);
646         return BT_STATUS_FAIL;
647     }
648     return BT_STATUS_SUCCESS;
649 }
650 
btc_dm_execute_service_request(BOOLEAN enable,char * p_param)651 void btc_dm_execute_service_request(BOOLEAN enable, char *p_param)
652 {
653     btc_in_execute_service_request(*((tBTA_SERVICE_ID *)p_param), enable);
654 }
655 
btc_dm_enable_service(tBTA_SERVICE_ID service_id)656 bt_status_t btc_dm_enable_service(tBTA_SERVICE_ID service_id)
657 {
658     tBTA_SERVICE_ID *p_id = &service_id;
659 
660     btc_dm_cb.btc_enabled_services |= (1 << service_id);
661 
662     BTC_TRACE_DEBUG("%s: current services:0x%x", __FUNCTION__, btc_dm_cb.btc_enabled_services);
663 
664     btc_dm_execute_service_request(TRUE, (char *)p_id);
665 
666     return BT_STATUS_SUCCESS;
667 }
668 
btc_dm_disable_service(tBTA_SERVICE_ID service_id)669 bt_status_t btc_dm_disable_service(tBTA_SERVICE_ID service_id)
670 {
671     tBTA_SERVICE_ID *p_id = &service_id;
672 
673     btc_dm_cb.btc_enabled_services &= (tBTA_SERVICE_MASK)(~(1 << service_id));
674 
675     BTC_TRACE_DEBUG("%s: Current Services:0x%x", __FUNCTION__, btc_dm_cb.btc_enabled_services);
676 
677     btc_dm_execute_service_request(FALSE, (char *)p_id);
678 
679     return BT_STATUS_SUCCESS;
680 }
681 
btc_dm_acl_link_stat(tBTA_DM_ACL_LINK_STAT * p_acl_link_stat)682 static void btc_dm_acl_link_stat(tBTA_DM_ACL_LINK_STAT *p_acl_link_stat)
683 {
684 #if (BTC_GAP_BT_INCLUDED == TRUE)
685     esp_bt_gap_cb_param_t param;
686     esp_bt_gap_cb_event_t event = ESP_BT_GAP_EVT_MAX;
687 
688     switch (p_acl_link_stat->event) {
689     case BTA_ACL_LINK_STAT_CONN_CMPL: {
690         event = ESP_BT_GAP_ACL_CONN_CMPL_STAT_EVT;
691         param.acl_conn_cmpl_stat.stat = p_acl_link_stat->link_act.conn_cmpl.status | ESP_BT_STATUS_BASE_FOR_HCI_ERR;
692         param.acl_conn_cmpl_stat.handle = p_acl_link_stat->link_act.conn_cmpl.handle;
693         memcpy(param.acl_conn_cmpl_stat.bda, p_acl_link_stat->link_act.conn_cmpl.bd_addr, ESP_BD_ADDR_LEN);
694         break;
695     }
696     case BTA_ACL_LINK_STAT_DISCONN_CMPL: {
697         event = ESP_BT_GAP_ACL_DISCONN_CMPL_STAT_EVT;
698         param.acl_disconn_cmpl_stat.reason = p_acl_link_stat->link_act.disconn_cmpl.reason | ESP_BT_STATUS_BASE_FOR_HCI_ERR;
699         param.acl_disconn_cmpl_stat.handle = p_acl_link_stat->link_act.disconn_cmpl.handle;
700         memcpy(param.acl_disconn_cmpl_stat.bda, p_acl_link_stat->link_act.disconn_cmpl.bd_addr, ESP_BD_ADDR_LEN);
701         break;
702     }
703     default: {
704         BTC_TRACE_WARNING("%s: invalid event %x", __FUNCTION__, event);
705         return;
706     }
707     }
708 
709     esp_bt_gap_cb_t cb = (esp_bt_gap_cb_t)btc_profile_cb_get(BTC_PID_GAP_BT);
710     if (cb) {
711         cb(event, &param);
712     }
713 #endif
714 }
715 
btc_dm_sec_cb_handler(btc_msg_t * msg)716 void btc_dm_sec_cb_handler(btc_msg_t *msg)
717 {
718     btc_dm_sec_args_t *arg = (btc_dm_sec_args_t *)(msg->arg);
719     tBTA_DM_SEC *p_data = &(arg->sec);
720     esp_ble_gap_cb_param_t param = {0};
721     btc_msg_t *ble_msg;
722     bool rsp_app = false;
723     bt_status_t ret = BT_STATUS_SUCCESS;
724 
725     ble_msg = (btc_msg_t *)osi_malloc(sizeof(btc_msg_t) + sizeof(esp_ble_gap_cb_param_t));
726     if (ble_msg == NULL) {
727         BTC_TRACE_ERROR("%s malloc fail", __func__);
728         btc_dm_sec_arg_deep_free(msg);
729         return;
730     }
731     ble_msg->sig = BTC_SIG_API_CB;
732     ble_msg->pid = BTC_PID_GAP_BLE;
733     // tBTA_SERVICE_MASK service_mask;
734     BTC_TRACE_DEBUG("btc_dm_upstreams_cback  ev: %d\n", msg->act);
735 
736     switch (msg->act) {
737     case BTA_DM_ENABLE_EVT: {
738         btc_clear_services_mask();
739 #if (SMP_INCLUDED == TRUE)
740         btc_storage_load_bonded_devices();
741 #if (BLE_INCLUDED == TRUE)
742         //load the bonding device to the btm layer
743         btc_storage_load_bonded_ble_devices();
744 #endif  ///BLE_INCLUDED == TRUE
745 #endif  ///SMP_INCLUDED == TRUE
746 
747         /* Set initial device name, it can be overwritten later */
748         if (p_data->enable.status == BTA_SUCCESS) {
749             const char *initial_device_name = "ESP32";
750             BTA_DmSetDeviceName(initial_device_name);
751         }
752         btc_enable_bluetooth_evt(p_data->enable.status);
753         break;
754     }
755     case BTA_DM_DISABLE_EVT: {
756         tBTA_SERVICE_MASK service_mask = btc_get_enabled_services_mask();
757         for (int i = 0; i <= BTA_MAX_SERVICE_ID; i++) {
758             if (service_mask &
759                     (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i))) {
760                 btc_in_execute_service_request(i, FALSE);
761             }
762         }
763         btc_disable_bluetooth_evt();
764         break;
765     }
766     case BTA_DM_PIN_REQ_EVT:
767         BTC_TRACE_DEBUG("BTA_DM_PIN_REQ_EVT");
768         btc_dm_pin_req_evt(&p_data->pin_req);
769         break;
770     case BTA_DM_AUTH_CMPL_EVT:
771         btc_dm_auth_cmpl_evt(&p_data->auth_cmpl);
772         break;
773     case BTA_DM_BOND_CANCEL_CMPL_EVT:
774         BTC_TRACE_DEBUG("BTA_DM_BOND_CANCEL_CMPL_EVT");
775         break;
776 #if (BT_SSP_INCLUDED == TRUE)
777     case BTA_DM_SP_CFM_REQ_EVT:
778         btc_dm_sp_cfm_req_evt(&p_data->cfm_req);
779         break;
780     case BTA_DM_SP_KEY_NOTIF_EVT:
781         btc_dm_sp_key_notif_evt(&p_data->key_notif);
782         break;
783     case BTA_DM_SP_KEY_REQ_EVT:
784         btc_dm_sp_key_req_evt(&p_data->key_req);
785         break;
786     case BTA_DM_SP_RMT_OOB_EVT:
787         BTC_TRACE_DEBUG("BTA_DM_SP_RMT_OOB_EVT");
788         break;
789     case BTA_DM_SP_KEYPRESS_EVT:
790         BTC_TRACE_DEBUG("BTA_DM_SP_KEYPRESS_EVT");
791         break;
792 #endif ///BT_SSP_INCLUDED == TRUE
793 
794     case BTA_DM_ACL_LINK_STAT_EVT: {
795         btc_dm_acl_link_stat(&p_data->acl_link_stat);
796         break;
797     }
798     case BTA_DM_DEV_UNPAIRED_EVT: {
799         btc_dm_dev_unpaired_evt(&p_data->link_down);
800         break;
801     }
802 #if (BLE_INCLUDED == TRUE)
803     case BTA_DM_BLE_DEV_UNPAIRED_EVT: {
804 #if (SMP_INCLUDED == TRUE)
805         bt_bdaddr_t bd_addr;
806         rsp_app = true;
807         BTC_TRACE_DEBUG("BTA_DM_BLE_DEV_UNPAIRED_EVT");
808         memcpy(bd_addr.address, p_data->link_down.bd_addr, sizeof(BD_ADDR));
809         btm_set_bond_type_dev(p_data->link_down.bd_addr, BOND_TYPE_UNKNOWN);
810         param.remove_bond_dev_cmpl.status = ESP_BT_STATUS_FAIL;
811 
812         if (p_data->link_down.status == HCI_SUCCESS) {
813             //remove the bonded key in the config and nvs flash.
814             btc_storage_remove_gatt_cl_supp_feat(&bd_addr);
815             btc_storage_remove_gatt_db_hash(&bd_addr);
816             btc_storage_remove_ble_dev_type(&bd_addr, false);
817             btc_storage_remove_remote_addr_type(&bd_addr, false);
818             btc_storage_remove_ble_dev_auth_mode(&bd_addr, false);
819             param.remove_bond_dev_cmpl.status = btc_storage_remove_ble_bonding_keys(&bd_addr);
820         }
821         ble_msg->act = ESP_GAP_BLE_REMOVE_BOND_DEV_COMPLETE_EVT;
822         memcpy(param.remove_bond_dev_cmpl.bd_addr, p_data->link_down.bd_addr, sizeof(BD_ADDR));
823 #endif /* #if (SMP_INCLUDED == TRUE) */
824         break;
825     }
826 #endif  ///BLE_INCLUDED == TRUE
827     case BTA_DM_BUSY_LEVEL_EVT:
828 #if (BTC_GAP_BT_INCLUDED == TRUE)
829         {
830         if (p_data->busy_level.level_flags & BTM_BL_INQUIRY_PAGING_MASK) {
831             btc_gap_bt_busy_level_updated(p_data->busy_level.level_flags);
832         }
833         break;
834         }
835 #endif /* BTC_GAP_BT_INCLUDED  == TRUE */
836     case BTA_DM_LINK_DOWN_EVT:
837     case BTA_DM_HW_ERROR_EVT:
838         BTC_TRACE_DEBUG( "btc_dm_sec_cback : unhandled event (%d)\n", msg->act );
839         break;
840     case BTA_DM_LINK_UP_EVT:
841         btc_dm_link_up_evt(&p_data->link_up);
842         break;
843 #if ((BLE_INCLUDED == TRUE) && (SMP_INCLUDED == TRUE))
844     case BTA_DM_BLE_AUTH_CMPL_EVT: {
845         rsp_app = true;
846         ble_msg->act = ESP_GAP_BLE_AUTH_CMPL_EVT;
847         param.ble_security.auth_cmpl.addr_type = p_data->auth_cmpl.addr_type;
848         param.ble_security.auth_cmpl.dev_type = p_data->auth_cmpl.dev_type;
849         param.ble_security.auth_cmpl.key_type = p_data->auth_cmpl.key_type;
850         param.ble_security.auth_cmpl.fail_reason = p_data->auth_cmpl.fail_reason;
851         param.ble_security.auth_cmpl.success = p_data->auth_cmpl.success ? true : false;
852         param.ble_security.auth_cmpl.key_present = p_data->auth_cmpl.key_present;
853         memcpy(param.ble_security.auth_cmpl.bd_addr, p_data->auth_cmpl.bd_addr, sizeof(BD_ADDR));
854         memcpy(param.ble_security.auth_cmpl.key, p_data->auth_cmpl.key, sizeof(LINK_KEY));
855         param.ble_security.auth_cmpl.auth_mode = p_data->auth_cmpl.auth_mode;
856         btc_dm_ble_auth_cmpl_evt(&p_data->auth_cmpl);
857         break;
858     }
859     case BTA_DM_BLE_KEY_EVT: {
860         rsp_app = true;
861         ble_msg->act = ESP_GAP_BLE_KEY_EVT;
862         param.ble_security.ble_key.key_type = p_data->ble_key.key_type;
863         memcpy(param.ble_security.ble_key.bd_addr, p_data->ble_key.bd_addr, BD_ADDR_LEN);
864         switch (p_data->ble_key.key_type) {
865             case BTM_LE_KEY_PENC: {
866                 BTC_TRACE_DEBUG("Rcv BTA_LE_KEY_PENC");
867                 btc_dm_cb.pairing_cb.ble.is_penc_key_rcvd = TRUE;
868                 btc_dm_cb.pairing_cb.ble.penc_key = p_data->ble_key.p_key_value->penc_key;
869                 memcpy(&btc_dm_cb.pairing_cb.ble.penc_key, &p_data->ble_key.p_key_value->penc_key,
870                              sizeof(tBTM_LE_PENC_KEYS));
871                 memcpy(&param.ble_security.ble_key.p_key_value.penc_key,
872                              &p_data->ble_key.p_key_value->penc_key, sizeof(tBTM_LE_PENC_KEYS));
873                 break;
874             }
875             case BTM_LE_KEY_PID: {
876                 BTC_TRACE_DEBUG("Rcv BTA_LE_KEY_PID");
877                 btc_dm_cb.pairing_cb.ble.is_pid_key_rcvd = TRUE;
878                 memcpy(&btc_dm_cb.pairing_cb.ble.pid_key, &p_data->ble_key.p_key_value->pid_key,
879                             sizeof(tBTM_LE_PID_KEYS));
880                 //Note: The memory size of the addr_type in ble_security.ble_key.p_key_value.pid_key is different from that of p_data->ble_key.p_key_value->pid_key.
881                 memcpy(&param.ble_security.ble_key.p_key_value.pid_key.irk,
882                              &p_data->ble_key.p_key_value->pid_key.irk, ESP_BT_OCTET16_LEN);
883                 param.ble_security.ble_key.p_key_value.pid_key.addr_type = p_data->ble_key.p_key_value->pid_key.addr_type;
884                 memcpy(&param.ble_security.ble_key.p_key_value.pid_key.static_addr,
885                              &p_data->ble_key.p_key_value->pid_key.static_addr, ESP_BD_ADDR_LEN);
886                 break;
887             }
888             case BTM_LE_KEY_PCSRK: {
889                 BTC_TRACE_DEBUG("Rcv BTA_LE_KEY_PCSRK");
890                 btc_dm_cb.pairing_cb.ble.is_pcsrk_key_rcvd = TRUE;
891                 memcpy(&btc_dm_cb.pairing_cb.ble.pcsrk_key, &p_data->ble_key.p_key_value->pcsrk_key,
892                              sizeof(tBTM_LE_PCSRK_KEYS));
893                 memcpy(&param.ble_security.ble_key.p_key_value.pcsrk_key,
894                              &p_data->ble_key.p_key_value->pcsrk_key, sizeof(tBTM_LE_PCSRK_KEYS));
895                 break;
896             }
897             case BTM_LE_KEY_LENC: {
898                 BTC_TRACE_DEBUG("Rcv BTA_LE_KEY_LENC");
899                 btc_dm_cb.pairing_cb.ble.is_lenc_key_rcvd = TRUE;
900                 memcpy(&btc_dm_cb.pairing_cb.ble.lenc_key, &p_data->ble_key.p_key_value->lenc_key,
901                             sizeof(tBTM_LE_LENC_KEYS));
902                 memcpy(&param.ble_security.ble_key.p_key_value.lenc_key,
903                              &p_data->ble_key.p_key_value->lenc_key, sizeof(tBTM_LE_LENC_KEYS));
904                 break;
905             }
906             case BTM_LE_KEY_LCSRK: {
907                 BTC_TRACE_DEBUG("Rcv BTA_LE_KEY_LCSRK");
908                 btc_dm_cb.pairing_cb.ble.is_lcsrk_key_rcvd = TRUE;
909                 memcpy(&btc_dm_cb.pairing_cb.ble.lcsrk_key, &p_data->ble_key.p_key_value->lcsrk_key,
910                             sizeof(tBTM_LE_LCSRK_KEYS));
911                 memcpy(&param.ble_security.ble_key.p_key_value.lcsrk_key,
912                              &p_data->ble_key.p_key_value->lcsrk_key, sizeof(tBTM_LE_LCSRK_KEYS));
913                 break;
914             }
915             case BTM_LE_KEY_LID: {
916                 BTC_TRACE_DEBUG("Rcv BTA_LE_KEY_LID");
917                 btc_dm_cb.pairing_cb.ble.is_lidk_key_rcvd =  TRUE;
918                 break;
919             }
920             default:
921                 break;
922         }
923 
924         break;
925     }
926     case BTA_DM_BLE_SEC_REQ_EVT: {
927         rsp_app = true;
928         ble_msg->act = ESP_GAP_BLE_SEC_REQ_EVT;
929         memcpy(param.ble_security.ble_req.bd_addr, p_data->ble_req.bd_addr, BD_ADDR_LEN);
930         break;
931     }
932     case BTA_DM_BLE_PASSKEY_NOTIF_EVT: {
933         rsp_app = true;
934         ble_msg->act = ESP_GAP_BLE_PASSKEY_NOTIF_EVT;
935         param.ble_security.key_notif.passkey = p_data->key_notif.passkey;
936         memcpy(param.ble_security.key_notif.bd_addr, p_data->ble_req.bd_addr, BD_ADDR_LEN);
937         break;
938     }
939     case BTA_DM_BLE_PASSKEY_REQ_EVT: {
940         rsp_app = true;
941         ble_msg->act = ESP_GAP_BLE_PASSKEY_REQ_EVT;
942         memcpy(param.ble_security.ble_req.bd_addr, p_data->ble_req.bd_addr, BD_ADDR_LEN);
943         break;
944     }
945     case BTA_DM_BLE_OOB_REQ_EVT: {
946         rsp_app = true;
947         ble_msg->act = ESP_GAP_BLE_OOB_REQ_EVT;
948         memcpy(param.ble_security.ble_req.bd_addr, p_data->ble_req.bd_addr, BD_ADDR_LEN);
949         break;
950     }
951     case BTA_DM_BLE_SC_OOB_REQ_EVT: {
952         rsp_app = true;
953         ble_msg->act = ESP_GAP_BLE_SC_OOB_REQ_EVT;
954         memcpy(param.ble_security.ble_req.bd_addr, p_data->ble_req.bd_addr, BD_ADDR_LEN);
955         break;
956     }
957     case BTA_DM_BLE_SC_CR_LOC_OOB_EVT: {
958         rsp_app = true;
959         ble_msg->act = ESP_GAP_BLE_SC_CR_LOC_OOB_EVT;
960         memcpy(param.ble_security.oob_data.oob_c, p_data->local_oob_data.local_oob_c, BT_OCTET16_LEN);
961         memcpy(param.ble_security.oob_data.oob_r, p_data->local_oob_data.local_oob_r, BT_OCTET16_LEN);
962         break;
963     }
964     case BTA_DM_BLE_LOCAL_IR_EVT: {
965         rsp_app = true;
966         ble_msg->act = ESP_GAP_BLE_LOCAL_IR_EVT;
967         memcpy(&param.ble_security.ble_id_keys, &p_data->ble_id_keys, sizeof(tBTA_BLE_LOCAL_ID_KEYS));
968         BTC_TRACE_DEBUG("BTA_DM_BLE_LOCAL_IR_EVT. ");
969         btc_dm_cb.ble_local_key_cb.is_id_keys_rcvd = TRUE;
970         memcpy(&btc_dm_cb.ble_local_key_cb.id_keys.irk[0],
971                &p_data->ble_id_keys.irk[0], sizeof(BT_OCTET16));
972         memcpy(&btc_dm_cb.ble_local_key_cb.id_keys.ir[0],
973                &p_data->ble_id_keys.ir[0], sizeof(BT_OCTET16));
974         memcpy(&btc_dm_cb.ble_local_key_cb.id_keys.dhk[0],
975                &p_data->ble_id_keys.dhk[0], sizeof(BT_OCTET16));
976         btc_storage_add_ble_local_key( (char *)&btc_dm_cb.ble_local_key_cb.id_keys.irk[0],
977                                        BTC_LE_LOCAL_KEY_IRK,
978                                        BT_OCTET16_LEN);
979         btc_storage_add_ble_local_key( (char *)&btc_dm_cb.ble_local_key_cb.id_keys.ir[0],
980                                        BTC_LE_LOCAL_KEY_IR,
981                                        BT_OCTET16_LEN);
982         btc_storage_add_ble_local_key( (char *)&btc_dm_cb.ble_local_key_cb.id_keys.dhk[0],
983                                        BTC_LE_LOCAL_KEY_DHK,
984                                        BT_OCTET16_LEN);
985         break;
986     }
987     case BTA_DM_BLE_LOCAL_ER_EVT: {
988         rsp_app = true;
989         ble_msg->act = ESP_GAP_BLE_LOCAL_ER_EVT;
990         memcpy(&param.ble_security.ble_id_keys, &p_data->ble_id_keys, sizeof(tBTA_BLE_LOCAL_ID_KEYS));
991         BTC_TRACE_DEBUG("BTA_DM_BLE_LOCAL_ER_EVT. ");
992         btc_dm_cb.ble_local_key_cb.is_er_rcvd = TRUE;
993         memcpy(&btc_dm_cb.ble_local_key_cb.er[0], &p_data->ble_er[0], sizeof(BT_OCTET16));
994         btc_storage_add_ble_local_key( (char *)&btc_dm_cb.ble_local_key_cb.er[0],
995                                        BTC_LE_LOCAL_KEY_ER,
996                                        BT_OCTET16_LEN);
997         break;
998     }
999     case BTA_DM_BLE_NC_REQ_EVT: {
1000         rsp_app = true;
1001         ble_msg->act = ESP_GAP_BLE_NC_REQ_EVT;
1002         memcpy(param.ble_security.key_notif.bd_addr, p_data->key_notif.bd_addr, BD_ADDR_LEN);
1003         param.ble_security.key_notif.passkey = p_data->key_notif.passkey;
1004         break;
1005     }
1006 #endif
1007 
1008 #if (BTC_DM_PM_INCLUDED == TRUE)
1009     case BTA_DM_PM_MODE_CHG_EVT:
1010         BTC_TRACE_DEBUG("BTA_DM_PM_MODE_CHG_EVT mode:%d", p_data->mode_chg.mode);
1011         btc_dm_pm_mode_chg_evt(&p_data->mode_chg);
1012         break;
1013 #endif /// BTA_DM_PM_INCLUDED == TRUE
1014 
1015     case BTA_DM_AUTHORIZE_EVT:
1016     case BTA_DM_SIG_STRENGTH_EVT:
1017     case BTA_DM_ROLE_CHG_EVT:
1018         BTC_TRACE_DEBUG( "btc_dm_sec_cback : unhandled event (%d)\n", msg->act );
1019         break;
1020     default:
1021         BTC_TRACE_DEBUG( "btc_dm_sec_cback : unhandled event (%d)\n", msg->act );
1022         break;
1023     }
1024 
1025     if (rsp_app) {
1026         memcpy(ble_msg->arg, &param, sizeof(esp_ble_gap_cb_param_t));
1027         ret = btc_inter_profile_call(ble_msg);
1028 
1029         if (ret != BT_STATUS_SUCCESS) {
1030             BTC_TRACE_ERROR("%s btc_inter_profile_call failed\n", __func__);
1031         }
1032     }
1033     osi_free(ble_msg);
1034     btc_dm_sec_arg_deep_free(msg);
1035 }
1036