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