1 /******************************************************************************
2  *
3  *  Copyright (C) 2003-2014 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 /******************************************************************************
20  *
21  *  This file contains the action functions for device manager state
22  *  machine.
23  *
24  ******************************************************************************/
25 
26 #include "common/bt_target.h"
27 #include "stack/bt_types.h"
28 #include "bta/bta_sys.h"
29 #include "bta/bta_api.h"
30 #include "bta_dm_int.h"
31 #include "bta/bta_dm_co.h"
32 #include "bta/bta_gattc_co.h"
33 #include "stack/btm_api.h"
34 #include "btm_int.h"
35 #include "stack/btu.h"
36 #include "stack/sdp_api.h"
37 #include "stack/l2c_api.h"
38 #include "bta/utl.h"
39 #include "stack/gap_api.h"    /* For GAP_BleReadPeerPrefConnParams */
40 #include <string.h>
41 #include "device/controller.h"
42 
43 #define LOG_TAG "bt_bta_dm"
44 // #include "osi/include/log.h"
45 
46 #if (GAP_INCLUDED == TRUE)
47 #include "stack/gap_api.h"
48 #endif
49 
50 static void bta_dm_inq_results_cb (tBTM_INQ_RESULTS *p_inq, UINT8 *p_eir);
51 static void bta_dm_inq_cmpl_cb (void *p_result);
52 static void bta_dm_service_search_remname_cback (BD_ADDR bd_addr, DEV_CLASS dc, BD_NAME bd_name);
53 static void bta_dm_remname_cback (tBTM_REMOTE_DEV_NAME *p_remote_name);
54 #if (SDP_INCLUDED == TRUE)
55 static void bta_dm_find_services ( BD_ADDR bd_addr);
56 #endif  ///SDP_INCLUDED == TRUE
57 static void bta_dm_discover_next_device(void);
58 #if (SDP_INCLUDED == TRUE)
59 static void bta_dm_sdp_callback (UINT16 sdp_status);
60 #endif  ///SDP_INCLUDED == TRUE
61 #if (SMP_INCLUDED == TRUE)
62 static UINT8 bta_dm_authorize_cback (BD_ADDR bd_addr, DEV_CLASS dev_class, BD_NAME bd_name, UINT8 *service_name, UINT8 service_id, BOOLEAN is_originator);
63 #if (CLASSIC_BT_INCLUDED == TRUE)
64 static UINT8 bta_dm_pin_cback (BD_ADDR bd_addr, DEV_CLASS dev_class, BD_NAME bd_name, BOOLEAN min_16_digit);
65 #endif /// CLASSIC_BT_INCLUDED == TRUE
66 static UINT8 bta_dm_new_link_key_cback(BD_ADDR bd_addr, DEV_CLASS dev_class, BD_NAME bd_name, LINK_KEY key, UINT8 key_type, BOOLEAN sc_support);
67 static UINT8 bta_dm_authentication_complete_cback(BD_ADDR bd_addr, DEV_CLASS dev_class, BD_NAME bd_name, int result);
68 #endif  ///SMP_INCLUDED == TRUE
69 static void bta_dm_local_name_cback(BD_ADDR bd_addr);
70 static BOOLEAN bta_dm_check_av(UINT16 event);
71 static void bta_dm_bl_change_cback (tBTM_BL_EVENT_DATA *p_data);
72 
73 
74 static void bta_dm_policy_cback(tBTA_SYS_CONN_STATUS status, UINT8 id, UINT8 app_id, BD_ADDR peer_addr);
75 
76 /* Extended Inquiry Response */
77 #if (BT_SSP_INCLUDED == TRUE && SMP_INCLUDED == TRUE)
78 static UINT8 bta_dm_sp_cback (tBTM_SP_EVT event, tBTM_SP_EVT_DATA *p_data);
79 #endif /* (BT_SSP_INCLUDED == TRUE) */
80 #if (CLASSIC_BT_INCLUDED == TRUE)
81 static void bta_dm_set_eir (char *local_name);
82 #endif
83 #if (SDP_INCLUDED == TRUE)
84 static void bta_dm_eir_search_services( tBTM_INQ_RESULTS  *p_result,
85                                         tBTA_SERVICE_MASK *p_services_to_search,
86                                         tBTA_SERVICE_MASK *p_services_found);
87 #endif  ///SDP_INCLUDED == TRUE
88 static void bta_dm_search_timer_cback (TIMER_LIST_ENT *p_tle);
89 static void bta_dm_disable_conn_down_timer_cback (TIMER_LIST_ENT *p_tle);
90 static void bta_dm_rm_cback(tBTA_SYS_CONN_STATUS status, UINT8 id, UINT8 app_id, BD_ADDR peer_addr);
91 static void bta_dm_adjust_roles(BOOLEAN delay_role_switch);
92 #if (SDP_INCLUDED == TRUE || SMP_INCLUDED == TRUE)
93 static char *bta_dm_get_remname(void);
94 #endif  ///SDP_INCLUDED == TRUE || SMP_INCLUDED == TRUE
95 #if (SMP_INCLUDED == TRUE)
96 static void bta_dm_bond_cancel_complete_cback(tBTM_STATUS result);
97 #endif  ///SMP_INCLUDED == TRUE
98 #if (SDP_INCLUDED == TRUE)
99 static BOOLEAN bta_dm_read_remote_device_name (BD_ADDR bd_addr, tBT_TRANSPORT transport);
100 static void bta_dm_discover_device(BD_ADDR remote_bd_addr);
101 #endif  ///SDP_INCLUDED == TRUE
102 static void bta_dm_sys_hw_cback( tBTA_SYS_HW_EVT status );
103 static void bta_dm_disable_search_and_disc(void);
104 
105 #if ((defined BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
106 #if ((defined SMP_INCLUDED) && (SMP_INCLUDED == TRUE))
107 static UINT8 bta_dm_ble_smp_cback (tBTM_LE_EVT event, BD_ADDR bda, tBTM_LE_EVT_DATA *p_data);
108 static void bta_dm_ble_id_key_cback (UINT8 key_type, tBTM_BLE_LOCAL_KEYS *p_key);
109 #endif  ///SMP_INCLUDED == TRUE
110 #if ((defined BTA_GATT_INCLUDED) &&  (BTA_GATT_INCLUDED == TRUE) && SDP_INCLUDED == TRUE)
111 #if (GATTC_INCLUDED == TRUE)
112 static void bta_dm_gattc_register(void);
113 static void btm_dm_start_gatt_discovery(BD_ADDR bd_addr);
114 static void bta_dm_cancel_gatt_discovery(BD_ADDR bd_addr);
115 static void bta_dm_gattc_callback(tBTA_GATTC_EVT event, tBTA_GATTC *p_data);
116 #endif // (GATTC_INCLUDED == TRUE)
117 extern tBTA_DM_CONTRL_STATE bta_dm_pm_obtain_controller_state(void);
118 #endif
119 
120 #if BLE_VND_INCLUDED == TRUE
121 static void bta_dm_ctrl_features_rd_cmpl_cback(tBTM_STATUS result);
122 #endif
123 
124 #ifndef BTA_DM_BLE_ADV_CHNL_MAP
125 #define BTA_DM_BLE_ADV_CHNL_MAP (BTM_BLE_ADV_CHNL_37|BTM_BLE_ADV_CHNL_38|BTM_BLE_ADV_CHNL_39)
126 #endif
127 #endif
128 #if (SMP_INCLUDED == TRUE)
129 static void bta_dm_remove_sec_dev_entry(BD_ADDR remote_bd_addr);
130 #endif  ///SMP_INCLUDED == TRUE
131 #if (BLE_INCLUDED == TRUE)
132 static void bta_dm_observe_results_cb(tBTM_INQ_RESULTS *p_inq, UINT8 *p_eir);
133 static void bta_dm_observe_cmpl_cb(void *p_result);
134 static void bta_dm_observe_discard_cb (uint32_t num_dis);
135 #endif  ///BLE_INCLUDED == TRUE
136 
137 static void bta_dm_delay_role_switch_cback(TIMER_LIST_ENT *p_tle);
138 extern void sdpu_uuid16_to_uuid128(UINT16 uuid16, UINT8 *p_uuid128);
139 static void bta_dm_disable_timer_cback(TIMER_LIST_ENT *p_tle);
140 extern int bredr_txpwr_get(int *min_power_level, int *max_power_level);
141 
142 const UINT16 bta_service_id_to_uuid_lkup_tbl [BTA_MAX_SERVICE_ID] = {
143     UUID_SERVCLASS_PNP_INFORMATION,         /* Reserved */
144     UUID_SERVCLASS_SERIAL_PORT,             /* BTA_SPP_SERVICE_ID */
145     UUID_SERVCLASS_DIALUP_NETWORKING,       /* BTA_DUN_SERVICE_ID */
146     UUID_SERVCLASS_AUDIO_SOURCE,            /* BTA_A2DP_SOURCE_SERVICE_ID */
147     UUID_SERVCLASS_LAN_ACCESS_USING_PPP,    /* BTA_LAP_SERVICE_ID */
148     UUID_SERVCLASS_HEADSET,                 /* BTA_HSP_HS_SERVICE_ID */
149     UUID_SERVCLASS_HF_HANDSFREE,            /* BTA_HFP_HS_SERVICE_ID */
150     UUID_SERVCLASS_OBEX_OBJECT_PUSH,        /* BTA_OPP_SERVICE_ID */
151     UUID_SERVCLASS_OBEX_FILE_TRANSFER,      /* BTA_FTP_SERVICE_ID */
152     UUID_SERVCLASS_CORDLESS_TELEPHONY,      /* BTA_CTP_SERVICE_ID */
153     UUID_SERVCLASS_INTERCOM,                /* BTA_ICP_SERVICE_ID */
154     UUID_SERVCLASS_IRMC_SYNC,               /* BTA_SYNC_SERVICE_ID */
155     UUID_SERVCLASS_DIRECT_PRINTING,         /* BTA_BPP_SERVICE_ID */
156     UUID_SERVCLASS_IMAGING_RESPONDER,       /* BTA_BIP_SERVICE_ID */
157     UUID_SERVCLASS_PANU,                    /* BTA_PANU_SERVICE_ID */
158     UUID_SERVCLASS_NAP,                     /* BTA_NAP_SERVICE_ID */
159     UUID_SERVCLASS_GN,                      /* BTA_GN_SERVICE_ID */
160     UUID_SERVCLASS_SAP,                     /* BTA_SAP_SERVICE_ID */
161     UUID_SERVCLASS_AUDIO_SINK,              /* BTA_A2DP_SERVICE_ID */
162     UUID_SERVCLASS_AV_REMOTE_CONTROL,       /* BTA_AVRCP_SERVICE_ID */
163     UUID_SERVCLASS_HUMAN_INTERFACE,         /* BTA_HID_SERVICE_ID */
164     UUID_SERVCLASS_VIDEO_SINK,              /* BTA_VDP_SERVICE_ID */
165     UUID_SERVCLASS_PBAP_PSE,                /* BTA_PBAP_SERVICE_ID */
166     UUID_SERVCLASS_HEADSET_AUDIO_GATEWAY,   /* BTA_HSP_SERVICE_ID */
167     UUID_SERVCLASS_AG_HANDSFREE,            /* BTA_HFP_SERVICE_ID */
168     UUID_SERVCLASS_MESSAGE_ACCESS,          /* BTA_MAP_SERVICE_ID */
169     UUID_SERVCLASS_MESSAGE_NOTIFICATION,    /* BTA_MN_SERVICE_ID */
170     UUID_SERVCLASS_HDP_PROFILE,             /* BTA_HDP_SERVICE_ID */
171     UUID_SERVCLASS_PBAP_PCE                 /* BTA_PCE_SERVICE_ID */
172 #if BLE_INCLUDED && BTA_GATT_INCLUDED
173     , UUID_PROTOCOL_ATT                      /* BTA_GATT_SERVICE_ID */
174 #endif
175 };
176 
177 /*
178  * NOTE : The number of element in bta_service_id_to_btm_srv_id_lkup_tbl should be matching with
179  *        the value BTA_MAX_SERVICE_ID in bta/bta_api.h
180  *
181  *        i.e., If you add new Service ID for BTA, the correct security ID of the new service
182  *              from Security service definitions (stack/btm_api.h) should be added to this lookup table.
183  */
184 const UINT32 bta_service_id_to_btm_srv_id_lkup_tbl [BTA_MAX_SERVICE_ID] = {
185     0,                                      /* Reserved */
186     BTM_SEC_SERVICE_SERIAL_PORT,            /* BTA_SPP_SERVICE_ID */
187     BTM_SEC_SERVICE_DUN,                    /* BTA_DUN_SERVICE_ID */
188     BTM_SEC_SERVICE_AVDTP,                  /* BTA_AUDIO_SOURCE_SERVICE_ID */
189     BTM_SEC_SERVICE_LAN_ACCESS,             /* BTA_LAP_SERVICE_ID */
190     BTM_SEC_SERVICE_HEADSET_AG,             /* BTA_HSP_SERVICE_ID */
191     BTM_SEC_SERVICE_AG_HANDSFREE,           /* BTA_HFP_SERVICE_ID */
192     BTM_SEC_SERVICE_OBEX,                   /* BTA_OPP_SERVICE_ID */
193     BTM_SEC_SERVICE_OBEX_FTP,               /* BTA_FTP_SERVICE_ID */
194     BTM_SEC_SERVICE_CORDLESS,               /* BTA_CTP_SERVICE_ID */
195     BTM_SEC_SERVICE_INTERCOM,               /* BTA_ICP_SERVICE_ID */
196     BTM_SEC_SERVICE_IRMC_SYNC,              /* BTA_SYNC_SERVICE_ID */
197     BTM_SEC_SERVICE_BPP_JOB,                /* BTA_BPP_SERVICE_ID */
198     BTM_SEC_SERVICE_BIP,                    /* BTA_BIP_SERVICE_ID */
199     BTM_SEC_SERVICE_BNEP_PANU,              /* BTA_PANU_SERVICE_ID */
200     BTM_SEC_SERVICE_BNEP_NAP,               /* BTA_NAP_SERVICE_ID */
201     BTM_SEC_SERVICE_BNEP_GN,                /* BTA_GN_SERVICE_ID */
202     BTM_SEC_SERVICE_SAP,                    /* BTA_SAP_SERVICE_ID */
203     BTM_SEC_SERVICE_AVDTP,                  /* BTA_A2DP_SERVICE_ID */
204     BTM_SEC_SERVICE_AVCTP,                  /* BTA_AVRCP_SERVICE_ID */
205     BTM_SEC_SERVICE_HIDH_SEC_CTRL,          /* BTA_HID_SERVICE_ID */
206     BTM_SEC_SERVICE_AVDTP,                  /* BTA_VDP_SERVICE_ID */
207     BTM_SEC_SERVICE_PBAP,                   /* BTA_PBAP_SERVICE_ID */
208     BTM_SEC_SERVICE_HEADSET,                /* BTA_HSP_HS_SERVICE_ID */
209     BTM_SEC_SERVICE_HF_HANDSFREE,           /* BTA_HFP_HS_SERVICE_ID */
210     BTM_SEC_SERVICE_MAP,                    /* BTA_MAP_SERVICE_ID */
211     BTM_SEC_SERVICE_MAP,                    /* BTA_MN_SERVICE_ID */
212     BTM_SEC_SERVICE_HDP_SNK,                /* BTA_HDP_SERVICE_ID */
213     BTM_SEC_SERVICE_PBAP                    /* BTA_PCE_SERVICE_ID */
214 #if BLE_INCLUDED && BTA_GATT_INCLUDED
215     , BTM_SEC_SERVICE_ATT                   /* BTA_GATT_SERVICE_ID */
216 #endif
217 
218 };
219 
220 /* bta security callback */
221 #if (SMP_INCLUDED == TRUE)
222 const tBTM_APPL_INFO bta_security = {
223     &bta_dm_authorize_cback,
224 #if (CLASSIC_BT_INCLUDED == TRUE)
225     &bta_dm_pin_cback,
226 #else
227     NULL,
228 #endif
229     &bta_dm_new_link_key_cback,
230     &bta_dm_authentication_complete_cback,
231     &bta_dm_bond_cancel_complete_cback,
232 #if (BT_SSP_INCLUDED == TRUE)
233     &bta_dm_sp_cback,
234 #else
235     NULL,
236 #endif
237 #if BLE_INCLUDED == TRUE
238     &bta_dm_ble_smp_cback,
239     &bta_dm_ble_id_key_cback,
240 #endif  ///BLE_INCLUDED == TRUE
241 
242 };
243 #endif  ///SMP_INCLUDED == TRUE
244 
245 #if (SDP_INCLUDED == TRUE)
246 #if BTA_DYNAMIC_MEMORY == FALSE
247 UINT8 g_disc_raw_data_buf[MAX_DISC_RAW_DATA_BUF];
248 #else
249 UINT8 *g_disc_raw_data_buf;
250 #endif
251 #endif  ///SDP_INCLUDED == TRUE
252 
253 /*******************************************************************************
254 **
255 ** Function         bta_dm_enable
256 **
257 ** Description      Initialises the BT device manager
258 **
259 **
260 ** Returns          void
261 **
262 *******************************************************************************/
bta_dm_enable(tBTA_DM_MSG * p_data)263 void bta_dm_enable(tBTA_DM_MSG *p_data)
264 {
265     tBTA_SYS_HW_MSG *sys_enable_event;
266     tBTA_DM_ENABLE enable_event;
267 
268     /* if already in use, return an error */
269     if ( bta_dm_cb.is_bta_dm_active == TRUE  ) {
270         APPL_TRACE_WARNING("%s Device already started by another application", __func__);
271         memset(&enable_event, 0, sizeof(tBTA_DM_ENABLE));
272         enable_event.status = BTA_FAILURE;
273         if (p_data->enable.p_sec_cback != NULL) {
274             p_data->enable.p_sec_cback(BTA_DM_ENABLE_EVT, (tBTA_DM_SEC *)&enable_event);
275         }
276         return;
277     }
278 
279     /* first, register our callback to SYS HW manager */
280     bta_sys_hw_register( BTA_SYS_HW_BLUETOOTH, bta_dm_sys_hw_cback );
281 
282     /* make sure security callback is saved - if no callback, do not erase the previous one,
283     it could be an error recovery mechanism */
284     if ( p_data->enable.p_sec_cback != NULL  ) {
285         bta_dm_cb.p_sec_cback = p_data->enable.p_sec_cback;
286     }
287     /* notify BTA DM is now active */
288     bta_dm_cb.is_bta_dm_active = TRUE;
289 
290     /* send a message to BTA SYS */
291     if ((sys_enable_event = (tBTA_SYS_HW_MSG *) osi_malloc(sizeof(tBTA_SYS_HW_MSG))) != NULL) {
292         sys_enable_event->hdr.event = BTA_SYS_API_ENABLE_EVT;
293         sys_enable_event->hw_module = BTA_SYS_HW_BLUETOOTH;
294 
295         bta_sys_sendmsg(sys_enable_event);
296     }
297 }
298 
299 /*******************************************************************************
300  *
301  * Function         bta_dm_init_cb
302  *
303  * Description      Initializes the bta_dm_cb control block
304  *
305  *
306  * Returns          void
307  *
308  ******************************************************************************/
bta_dm_init_cb(void)309 void bta_dm_init_cb(void)
310 {
311     memset(&bta_dm_cb, 0, sizeof(bta_dm_cb));
312 }
313 
314 /*******************************************************************************
315  *
316  * Function         bta_dm_deinit_cb
317  *
318  * Description      De-initializes the bta_dm_cb control block
319  *
320  *
321  * Returns          void
322  *
323  ******************************************************************************/
bta_dm_deinit_cb(void)324 void bta_dm_deinit_cb(void)
325 {
326     bta_sys_free_timer(&bta_dm_cb.disable_timer);
327 #if ( BTA_EIR_CANNED_UUID_LIST != TRUE )
328     bta_sys_free_timer(&bta_dm_cb.app_ready_timer);
329 #endif
330 #if BTM_SSR_INCLUDED == TRUE
331     for (size_t i = 0; i < BTA_DM_NUM_PM_TIMER; i++) {
332         for (size_t j = 0; j < BTA_DM_PM_MODE_TIMER_MAX; j++) {
333             bta_sys_free_timer(&bta_dm_cb.pm_timer[i].timer[j]);
334         }
335     }
336 #endif
337     memset(&bta_dm_cb, 0, sizeof(bta_dm_cb));
338 #if BTA_DYNAMIC_MEMORY
339     xSemaphoreGive(deinit_semaphore);
340 #endif /* #if BTA_DYNAMIC_MEMORY */
341 }
342 
343 /*******************************************************************************
344  *
345  * Function         bta_dm_eir_cfg_init
346  *
347  * Description      Initializes the p_bta_dm_eir_cfg
348  *
349  *
350  * Returns          void
351  *
352  ******************************************************************************/
bta_dm_eir_cfg_init(void)353 static void bta_dm_eir_cfg_init(void)
354 {
355     p_bta_dm_eir_cfg->bta_dm_eir_fec_required = BTM_EIR_DEFAULT_FEC_REQUIRED;
356     p_bta_dm_eir_cfg->bta_dm_eir_min_name_len = 50;
357 
358     p_bta_dm_eir_cfg->bta_dm_eir_included_uuid = TRUE;
359     p_bta_dm_eir_cfg->bta_dm_eir_included_tx_power = FALSE;
360     p_bta_dm_eir_cfg->bta_dm_eir_inq_tx_power = 3;
361     p_bta_dm_eir_cfg->bta_dm_eir_flags = 0;
362 
363     p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len = 0;
364     p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec = NULL;
365 
366     p_bta_dm_eir_cfg->bta_dm_eir_url_len = 0;
367     p_bta_dm_eir_cfg->bta_dm_eir_url = NULL;
368 }
369 
370 /*******************************************************************************
371  *
372  * Function         bta_dm_eir_cfg_deinit
373  *
374  * Description      De-initializes the p_bta_dm_eir_cfg
375  *
376  *
377  * Returns          void
378  *
379  ******************************************************************************/
bta_dm_eir_cfg_deinit(void)380 static void bta_dm_eir_cfg_deinit(void)
381 {
382     p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len = 0;
383     if (p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec) {
384         osi_free(p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec);
385         p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec = NULL;
386     }
387 
388     p_bta_dm_eir_cfg->bta_dm_eir_url_len = 0;
389     if (p_bta_dm_eir_cfg->bta_dm_eir_url) {
390         osi_free(p_bta_dm_eir_cfg->bta_dm_eir_url);
391         p_bta_dm_eir_cfg->bta_dm_eir_url = NULL;
392     }
393 }
394 
395 /*******************************************************************************
396 **
397 ** Function         bta_dm_sys_hw_cback
398 **
399 ** Description     callback register to SYS to get HW status updates
400 **
401 **
402 ** Returns          void
403 **
404 *******************************************************************************/
bta_dm_sys_hw_cback(tBTA_SYS_HW_EVT status)405 static void bta_dm_sys_hw_cback( tBTA_SYS_HW_EVT status )
406 {
407     DEV_CLASS   dev_class;
408     tBTA_DM_SEC_CBACK           *temp_cback;
409 #if BLE_INCLUDED == TRUE
410     UINT8                   key_mask = 0;
411     BT_OCTET16              er;
412     tBTA_BLE_LOCAL_ID_KEYS  id_key;
413 #endif
414 
415     APPL_TRACE_DEBUG("%s with event: %i", __func__, status);
416 
417     /* On H/W error evt, report to the registered DM application callback */
418     if (status == BTA_SYS_HW_ERROR_EVT) {
419         if ( bta_dm_cb.p_sec_cback != NULL ) {
420             bta_dm_cb.p_sec_cback(BTA_DM_HW_ERROR_EVT, NULL);
421         }
422         return;
423     }
424 
425     if ( status == BTA_SYS_HW_OFF_EVT ) {
426         if ( bta_dm_cb.p_sec_cback != NULL ) {
427             bta_dm_cb.p_sec_cback(BTA_DM_DISABLE_EVT, NULL);
428         }
429 
430         /* reinitialize the control block */
431         bta_dm_deinit_cb();
432 
433         /* reinitialize the Extended Inquiry Response */
434         bta_dm_eir_cfg_deinit();
435 
436         bta_sys_free_timer(&bta_dm_search_cb.search_timer);
437 #if ((defined BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
438 #if ((defined BTA_GATT_INCLUDED) && (BTA_GATT_INCLUDED == TRUE) && SDP_INCLUDED == TRUE)
439         bta_sys_free_timer(&bta_dm_search_cb.gatt_close_timer);
440 #endif
441 #endif
442         memset(&bta_dm_search_cb, 0x00, sizeof(bta_dm_search_cb));
443 
444         /* unregister from SYS */
445         bta_sys_hw_unregister( BTA_SYS_HW_BLUETOOTH );
446         /* notify BTA DM is now unactive */
447         bta_dm_cb.is_bta_dm_active = FALSE;
448 #if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE)
449 #if (GATTC_INCLUDED == TRUE && GATTC_CACHE_NVS == TRUE)
450         /* clear the gattc cache address list */
451         bta_gattc_co_cache_addr_deinit();
452 #endif
453 #endif
454     } else if ( status == BTA_SYS_HW_ON_EVT ) {
455         /* FIXME: We should not unregister as the SYS shall invoke this callback on a H/W error.
456         * We need to revisit when this platform has more than one BLuetooth H/W chip */
457         //bta_sys_hw_unregister( BTA_SYS_HW_BLUETOOTH);
458 
459         /* save security callback */
460         temp_cback = bta_dm_cb.p_sec_cback;
461         /* make sure the control block is properly initialized */
462         bta_dm_init_cb();
463 
464         /* make sure the Extended Inquiry Response is properly initialized */
465         bta_dm_eir_cfg_init();
466 
467         /* and retrieve the callback */
468         bta_dm_cb.p_sec_cback = temp_cback;
469         bta_dm_cb.is_bta_dm_active = TRUE;
470 
471         bta_sys_free_timer(&bta_dm_search_cb.search_timer);
472 #if ((defined BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
473 #if ((defined BTA_GATT_INCLUDED) && (BTA_GATT_INCLUDED == TRUE) && SDP_INCLUDED == TRUE)
474         bta_sys_free_timer(&bta_dm_search_cb.gatt_close_timer);
475 #endif
476 #endif
477         /* hw is ready, go on with BTA DM initialization */
478         memset(&bta_dm_search_cb, 0x00, sizeof(bta_dm_search_cb));
479         memset(&bta_dm_conn_srvcs, 0x00, sizeof(bta_dm_conn_srvcs));
480         memset(&bta_dm_di_cb, 0, sizeof(tBTA_DM_DI_CB));
481         memcpy(dev_class, p_bta_dm_cfg->dev_class, sizeof(dev_class));
482 #if CLASSIC_BT_INCLUDED
483         BTM_SetDeviceClass (dev_class);
484 #endif
485 
486 #if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE)
487 #if (GATTC_INCLUDED == TRUE && GATTC_CACHE_NVS == TRUE)
488         // load the gattc cache address list
489         bta_gattc_co_cache_addr_init();
490 #endif /* #if (GATTC_INCLUDED = TRUE) */
491         /* load BLE local information: ID keys, ER if available */
492         bta_dm_co_ble_load_local_keys(&key_mask, er, &id_key);
493 
494         if (key_mask & BTA_BLE_LOCAL_KEY_TYPE_ER) {
495             BTM_BleLoadLocalKeys(BTA_BLE_LOCAL_KEY_TYPE_ER, (tBTM_BLE_LOCAL_KEYS *)&er);
496         }
497         if (key_mask & BTA_BLE_LOCAL_KEY_TYPE_ID) {
498             BTM_BleLoadLocalKeys(BTA_BLE_LOCAL_KEY_TYPE_ID, (tBTM_BLE_LOCAL_KEYS *)&id_key);
499         }
500 #if ((defined BTA_GATT_INCLUDED) && (BTA_GATT_INCLUDED == TRUE) && SDP_INCLUDED == TRUE)
501         bta_dm_search_cb.conn_id = BTA_GATT_INVALID_CONN_ID;
502 #endif
503 #endif
504 #if (SMP_INCLUDED == TRUE)
505         BTM_SecRegister((tBTM_APPL_INFO *)&bta_security);
506 #endif  ///SMP_INCLUDED == TRUE
507         BTM_SetDefaultLinkSuperTout(p_bta_dm_cfg->link_timeout);
508 #if CLASSIC_BT_INCLUDED
509         BTM_WritePageTimeout(p_bta_dm_cfg->page_timeout);
510         bta_dm_cb.cur_policy = p_bta_dm_cfg->policy_settings;
511         BTM_SetDefaultLinkPolicy(bta_dm_cb.cur_policy);
512 #endif
513         BTM_RegBusyLevelNotif (bta_dm_bl_change_cback, NULL, BTM_BL_UPDATE_MASK | BTM_BL_ROLE_CHG_MASK);
514 
515 #if BLE_VND_INCLUDED == TRUE
516         BTM_BleReadControllerFeatures (bta_dm_ctrl_features_rd_cmpl_cback);
517 #endif
518 
519         /* Earlier, we used to invoke BTM_ReadLocalAddr which was just copying the bd_addr
520            from the control block and invoking the callback which was sending the DM_ENABLE_EVT.
521            But then we have a few HCI commands being invoked above which were still in progress
522            when the ENABLE_EVT was sent. So modified this to fetch the local name which forces
523            the DM_ENABLE_EVT to be sent only after all the init steps are complete */
524         BTM_ReadLocalDeviceNameFromController((tBTM_CMPL_CB *)bta_dm_local_name_cback);
525 
526         bta_sys_rm_register((tBTA_SYS_CONN_CBACK *)bta_dm_rm_cback);
527 
528 #if (BTA_DM_PM_INCLUDED == TRUE)
529         /* initialize bluetooth low power manager */
530         bta_dm_init_pm();
531 #endif /* #if (BTA_DM_PM_INCLUDED == TRUE) */
532 
533         bta_sys_policy_register((tBTA_SYS_CONN_CBACK *)bta_dm_policy_cback);
534 
535 #if (BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE && SDP_INCLUDED == TRUE) && (GATTC_INCLUDED == TRUE)
536         bta_dm_gattc_register();
537 #endif
538 
539     } else {
540         APPL_TRACE_DEBUG(" --- ignored event");
541     }
542 
543 }
544 
545 
546 /*******************************************************************************
547 **
548 ** Function         bta_dm_disable
549 **
550 ** Description      Disables the BT device manager
551 **
552 **
553 ** Returns          void
554 **
555 *******************************************************************************/
bta_dm_disable(tBTA_DM_MSG * p_data)556 void bta_dm_disable (tBTA_DM_MSG *p_data)
557 {
558     UNUSED(p_data);
559 
560     /* Set l2cap idle timeout to 0 (so BTE immediately disconnects ACL link after last channel is closed) */
561     L2CA_SetIdleTimeoutByBdAddr((UINT8 *)BT_BD_ANY, 0, BT_TRANSPORT_BR_EDR);
562     L2CA_SetIdleTimeoutByBdAddr((UINT8 *)BT_BD_ANY, 0, BT_TRANSPORT_LE);
563 
564     /* disable all active subsystems */
565     bta_sys_disable(BTA_SYS_HW_BLUETOOTH);
566 #if CLASSIC_BT_INCLUDED
567     BTM_SetDiscoverability(BTM_NON_DISCOVERABLE, 0, 0);
568     BTM_SetConnectability(BTM_NON_CONNECTABLE, 0, 0);
569 #endif // #if CLASSIC_BT_INCLUDED
570 
571 #if (BTA_DM_PM_INCLUDED == TRUE)
572     bta_dm_disable_pm();
573 #endif /* #if (BTA_DM_PM_INCLUDED == TRUE) */
574 
575     bta_dm_disable_search_and_disc();
576     bta_dm_cb.disabling = TRUE;
577 
578 #if BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE
579     BTM_BleClearBgConnDev();
580 #endif
581 
582     if (BTM_GetNumAclLinks() == 0) {
583 #if (defined(BTA_DISABLE_DELAY) && BTA_DISABLE_DELAY > 0)
584         /* If BTA_DISABLE_DELAY is defined and greater than zero, then delay the shutdown by
585          * BTA_DISABLE_DELAY milliseconds
586          */
587         APPL_TRACE_WARNING("%s BTA_DISABLE_DELAY set to %d ms",
588                            __FUNCTION__, BTA_DISABLE_DELAY);
589         bta_sys_stop_timer(&bta_dm_cb.disable_timer);
590         bta_dm_cb.disable_timer.p_cback = (TIMER_CBACK *)&bta_dm_disable_conn_down_timer_cback;
591         bta_sys_start_timer(&bta_dm_cb.disable_timer, 0, BTA_DISABLE_DELAY);
592 #else
593         bta_dm_disable_conn_down_timer_cback(NULL);
594 #endif
595     } else {
596         bta_dm_cb.disable_timer.p_cback = (TIMER_CBACK *)&bta_dm_disable_timer_cback;
597         bta_dm_cb.disable_timer.param = 0;
598         bta_sys_start_timer(&bta_dm_cb.disable_timer, 0, 5000);
599     }
600 
601 #if BLE_INCLUDED == TRUE && BLE_PRIVACY_SPT == TRUE
602     btm_ble_resolving_list_cleanup ();  //by TH, because cmn_ble_vsc_cb.max_filter has something mistake as btm_ble_adv_filter_cleanup
603 #endif
604 
605 }
606 
607 /*******************************************************************************
608 **
609 ** Function         bta_dm_disable_timer_cback
610 **
611 ** Description      Called if the disable timer expires
612 **                  Used to close ACL connections which are still active
613 **
614 **
615 **
616 ** Returns          void
617 **
618 *******************************************************************************/
bta_dm_disable_timer_cback(TIMER_LIST_ENT * p_tle)619 static void bta_dm_disable_timer_cback (TIMER_LIST_ENT *p_tle)
620 {
621     UNUSED(p_tle);
622     UINT8 i;
623     tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
624     BOOLEAN trigger_disc = FALSE;
625 
626 
627     APPL_TRACE_EVENT(" bta_dm_disable_timer_cback trial %d ", p_tle->param);
628 
629     if (BTM_GetNumAclLinks() && p_tle->param == 0) {
630         for (i = 0; i < bta_dm_cb.device_list.count; i++) {
631 #if (BLE_INCLUDED == TRUE)
632             transport = bta_dm_cb.device_list.peer_device[i].transport;
633 #endif
634             btm_remove_acl(bta_dm_cb.device_list.peer_device[i].peer_bdaddr, transport);
635             trigger_disc = TRUE;
636         }
637 
638         /* Retrigger disable timer in case ACL disconnect failed, DISABLE_EVT still need
639             to be sent out to avoid jave layer disable timeout */
640         if (trigger_disc) {
641             bta_dm_cb.disable_timer.p_cback = (TIMER_CBACK *)&bta_dm_disable_timer_cback;
642             bta_dm_cb.disable_timer.param = 1;
643             bta_sys_start_timer(&bta_dm_cb.disable_timer, 0, 1500);
644         }
645     } else {
646         bta_dm_cb.disabling = FALSE;
647 
648         bta_sys_remove_uuid(UUID_SERVCLASS_PNP_INFORMATION);
649         bta_dm_cb.p_sec_cback(BTA_DM_DISABLE_EVT, NULL);
650     }
651 }
652 
653 
654 
655 
656 /*******************************************************************************
657 **
658 ** Function         bta_dm_set_dev_name
659 **
660 ** Description      Sets local device name
661 **
662 **
663 ** Returns          void
664 **
665 *******************************************************************************/
bta_dm_set_dev_name(tBTA_DM_MSG * p_data)666 void bta_dm_set_dev_name (tBTA_DM_MSG *p_data)
667 {
668     BTM_SetLocalDeviceName((char *)p_data->set_name.name);
669 #if CLASSIC_BT_INCLUDED
670     bta_dm_set_eir ((char *)p_data->set_name.name);
671 #endif /// CLASSIC_BT_INCLUDED
672 }
673 
674 /*******************************************************************************
675 **
676 ** Function         bta_dm_set_afh_channels
677 **
678 ** Description      Sets AFH channels
679 **
680 **
681 ** Returns          void
682 **
683 *******************************************************************************/
bta_dm_set_afh_channels(tBTA_DM_MSG * p_data)684 void bta_dm_set_afh_channels (tBTA_DM_MSG *p_data)
685 {
686 #if CLASSIC_BT_INCLUDED
687     BTM_SetAfhChannels (p_data->set_afh_channels.channels, p_data->set_afh_channels.set_afh_cb);
688 #endif /// CLASSIC_BT_INCLUDED
689 }
690 
691 #if (SDP_INCLUDED == TRUE)
692 /*******************************************************************************
693 **
694 ** Function         bta_dm_read_remote_device_name
695 **
696 ** Description      Initiate to get remote device name
697 **
698 ** Returns          TRUE if started to get remote name
699 **
700 *******************************************************************************/
bta_dm_read_remote_device_name(BD_ADDR bd_addr,tBT_TRANSPORT transport)701 static BOOLEAN bta_dm_read_remote_device_name (BD_ADDR bd_addr, tBT_TRANSPORT transport)
702 {
703     tBTM_STATUS  btm_status;
704 
705     APPL_TRACE_DEBUG("bta_dm_read_remote_device_name");
706 
707     bdcpy(bta_dm_search_cb.peer_bdaddr, bd_addr);
708     bta_dm_search_cb.peer_name[0] = 0;
709 
710     btm_status = BTM_ReadRemoteDeviceName (bta_dm_search_cb.peer_bdaddr,
711                                            (tBTM_CMPL_CB *) bta_dm_remname_cback,
712                                            transport);
713 
714     if ( btm_status == BTM_CMD_STARTED ) {
715         APPL_TRACE_DEBUG("bta_dm_read_remote_device_name: BTM_ReadRemoteDeviceName is started");
716 
717         return (TRUE);
718     } else if ( btm_status == BTM_BUSY ) {
719         APPL_TRACE_DEBUG("bta_dm_read_remote_device_name: BTM_ReadRemoteDeviceName is busy");
720 
721         /* Remote name discovery is on going now so BTM cannot notify through "bta_dm_remname_cback" */
722         /* adding callback to get notified that current reading remore name done */
723         BTM_SecAddRmtNameNotifyCallback(&bta_dm_service_search_remname_cback);
724 
725         return (TRUE);
726     } else {
727         APPL_TRACE_WARNING("bta_dm_read_remote_device_name: BTM_ReadRemoteDeviceName returns 0x%02X", btm_status);
728 
729         return (FALSE);
730     }
731 }
732 
733 /*******************************************************************************
734 **
735 ** Function         bta_dm_read_rmt_name
736 **
737 ** Description      Initiate to get remote device name
738 **
739 ** Returns          TRUE if started to get remote name
740 **
741 *******************************************************************************/
bta_dm_read_rmt_name(tBTA_DM_MSG * p_data)742 void bta_dm_read_rmt_name(tBTA_DM_MSG *p_data)
743 {
744     APPL_TRACE_DEBUG("%s",__func__);
745     bdcpy(bta_dm_search_cb.peer_bdaddr, p_data->get_rmt_name.rmt_addr);
746     bta_dm_search_cb.peer_name[0] = 0;
747 
748     tBTM_STATUS  btm_status = BTM_ReadRemoteDeviceName(bta_dm_search_cb.peer_bdaddr,
749                                                        (tBTM_CMPL_CB *) p_data->get_rmt_name.rmt_name_cb,
750                                                        bta_dm_search_cb.transport);
751 
752     if (btm_status == BTM_CMD_STARTED) {
753         BTM_TRACE_DEBUG("%s: BTM_ReadRemoteDeviceName is started",__func__);
754     } else if (btm_status == BTM_BUSY) {
755         BTM_TRACE_DEBUG("%s: BTM_ReadRemoteDeviceName is busy",__func__);
756     } else {
757         BTM_TRACE_WARNING("%s: BTM_ReadRemoteDeviceName returns 0x%02X",__func__, btm_status);
758     }
759 }
760 #endif  ///SDP_INCLUDED == TRUE
761 
762 #if (CLASSIC_BT_INCLUDED == TRUE)
bta_dm_config_eir(tBTA_DM_MSG * p_data)763 void bta_dm_config_eir (tBTA_DM_MSG *p_data)
764 {
765     tBTA_DM_API_CONFIG_EIR *config_eir = &p_data->config_eir;
766 
767     p_bta_dm_eir_cfg->bta_dm_eir_fec_required = config_eir->eir_fec_required;
768     p_bta_dm_eir_cfg->bta_dm_eir_included_uuid = config_eir->eir_included_uuid;
769     p_bta_dm_eir_cfg->bta_dm_eir_included_tx_power = config_eir->eir_included_tx_power;
770     p_bta_dm_eir_cfg->bta_dm_eir_flags = config_eir->eir_flags;
771     p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len = config_eir->eir_manufac_spec_len;
772     p_bta_dm_eir_cfg->bta_dm_eir_url_len = config_eir->eir_url_len;
773 
774     if (p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec) {
775         osi_free(p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec);
776         p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec = NULL;
777     }
778     if (config_eir->eir_manufac_spec_len > 0) {
779         p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec = osi_malloc(config_eir->eir_manufac_spec_len);
780         if (p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec) {
781             memcpy(p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec, config_eir->eir_manufac_spec, config_eir->eir_manufac_spec_len);
782         } else {
783             APPL_TRACE_ERROR("%s, malloc failed.", __func__);
784             p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len = 0;
785         }
786     }
787 
788     if (p_bta_dm_eir_cfg->bta_dm_eir_url) {
789         osi_free(p_bta_dm_eir_cfg->bta_dm_eir_url);
790         p_bta_dm_eir_cfg->bta_dm_eir_url = NULL;
791     }
792     if (config_eir->eir_url_len > 0) {
793         p_bta_dm_eir_cfg->bta_dm_eir_url = osi_malloc(config_eir->eir_url_len);
794         if (p_bta_dm_eir_cfg->bta_dm_eir_url) {
795             memcpy(p_bta_dm_eir_cfg->bta_dm_eir_url, config_eir->eir_url, config_eir->eir_url_len);
796         } else {
797             APPL_TRACE_ERROR("%s, malloc failed.", __func__);
798             p_bta_dm_eir_cfg->bta_dm_eir_url_len = 0;
799         }
800     }
801 
802     bta_dm_set_eir(NULL);
803 }
804 #endif
805 /*******************************************************************************
806 **
807 ** Function         bta_dm_ble_set_channels
808 **
809 ** Description      Sets AFH channels
810 **
811 **
812 ** Returns          void
813 **
814 *******************************************************************************/
bta_dm_ble_set_channels(tBTA_DM_MSG * p_data)815 void bta_dm_ble_set_channels (tBTA_DM_MSG *p_data)
816 {
817 #if (BLE_INCLUDED == TRUE)
818     BTM_BleSetChannels (p_data->ble_set_channels.channels, p_data->ble_set_channels.set_channels_cb);
819 #endif /// BLE_INCLUDED == TRUE
820 }
821 
bta_dm_update_white_list(tBTA_DM_MSG * p_data)822 void bta_dm_update_white_list(tBTA_DM_MSG *p_data)
823 {
824 #if (BLE_INCLUDED == TRUE)
825     BTM_BleUpdateAdvWhitelist(p_data->white_list.add_remove, p_data->white_list.remote_addr, p_data->white_list.addr_type, p_data->white_list.add_wl_cb);
826 #endif  ///BLE_INCLUDED == TRUE
827 }
828 
bta_dm_clear_white_list(tBTA_DM_MSG * p_data)829 void bta_dm_clear_white_list(tBTA_DM_MSG *p_data)
830 {
831 #if (BLE_INCLUDED == TRUE)
832     BTM_BleClearWhitelist();
833 #endif
834 }
835 
bta_dm_ble_read_adv_tx_power(tBTA_DM_MSG * p_data)836 void bta_dm_ble_read_adv_tx_power(tBTA_DM_MSG *p_data)
837 {
838 #if (BLE_INCLUDED == TRUE)
839     if (p_data->read_tx_power.read_tx_power_cb != NULL) {
840         BTM_BleReadAdvTxPower(p_data->read_tx_power.read_tx_power_cb);
841     } else {
842         APPL_TRACE_ERROR("%s(), the callback function can't be NULL.", __func__);
843     }
844 #endif  ///BLE_INCLUDED == TRUE
845 }
846 
bta_dm_read_rssi(tBTA_DM_MSG * p_data)847 void bta_dm_read_rssi(tBTA_DM_MSG *p_data)
848 {
849     if (p_data->rssi.read_rssi_cb != NULL) {
850         BTM_ReadRSSI(p_data->rssi.remote_addr, p_data->rssi.transport, p_data->rssi.read_rssi_cb);
851     } else {
852         APPL_TRACE_ERROR("%s(), the callback function can't be NULL.", __func__);
853     }
854 }
855 
856 /*******************************************************************************
857 **
858 ** Function         bta_dm_set_visibility
859 **
860 ** Description      Sets discoverability, connectability and pairability
861 **
862 **
863 ** Returns          void
864 **
865 *******************************************************************************/
bta_dm_set_visibility(tBTA_DM_MSG * p_data)866 void bta_dm_set_visibility(tBTA_DM_MSG *p_data)
867 {
868     UINT16 window, interval;
869     UINT16 disc_mode = BTM_ReadDiscoverability(&window, &interval);
870     UINT16 conn_mode = BTM_ReadConnectability(&window, &interval);
871 #if (BLE_INCLUDED == TRUE)
872     UINT16 le_disc_mode = BTM_BleReadDiscoverability();
873     UINT16 le_conn_mode = BTM_BleReadConnectability();
874 #endif  ///BLE_INCLUDED == TRUE
875 
876     /* set modes for Discoverability and connectability if not ignore */
877     if (p_data->set_visibility.disc_mode != (BTA_DM_IGNORE | BTA_DM_LE_IGNORE)) {
878 #if (BLE_INCLUDED == TRUE)
879         if ((p_data->set_visibility.disc_mode & BTA_DM_LE_IGNORE) == BTA_DM_LE_IGNORE) {
880             p_data->set_visibility.disc_mode =
881                 ((p_data->set_visibility.disc_mode & ~BTA_DM_LE_IGNORE) | le_disc_mode);
882         }
883 #endif  ///BLE_INCLUDED == TRUE
884 
885         if ((p_data->set_visibility.disc_mode & BTA_DM_IGNORE) == BTA_DM_IGNORE) {
886             p_data->set_visibility.disc_mode =
887                 ((p_data->set_visibility.disc_mode & ~BTA_DM_IGNORE) | disc_mode);
888         }
889 
890 #if (CLASSIC_BT_INCLUDED == TRUE)
891         BTM_SetDiscoverability(p_data->set_visibility.disc_mode,
892                                bta_dm_cb.inquiry_scan_window,
893                                bta_dm_cb.inquiry_scan_interval);
894 #endif
895     }
896 
897     if (p_data->set_visibility.conn_mode != (BTA_DM_IGNORE | BTA_DM_LE_IGNORE)) {
898 #if (BLE_INCLUDED == TRUE)
899         if ((p_data->set_visibility.conn_mode & BTA_DM_LE_IGNORE) == BTA_DM_LE_IGNORE) {
900             p_data->set_visibility.conn_mode =
901                 ((p_data->set_visibility.conn_mode & ~BTA_DM_LE_IGNORE) | le_conn_mode);
902         }
903 #endif  ///BLE_INCLUDED == TRUE
904 
905         if ((p_data->set_visibility.conn_mode & BTA_DM_IGNORE) == BTA_DM_IGNORE) {
906             p_data->set_visibility.conn_mode =
907                 ((p_data->set_visibility.conn_mode & ~BTA_DM_IGNORE) | conn_mode);
908         }
909 
910 #if (CLASSIC_BT_INCLUDED == TRUE)
911         BTM_SetConnectability(p_data->set_visibility.conn_mode,
912                               bta_dm_cb.page_scan_window,
913                               bta_dm_cb.page_scan_interval);
914 #endif
915     }
916 
917     /* Send False or True if not ignore */
918     if (p_data->set_visibility.pair_mode != BTA_DM_IGNORE ) {
919 
920         if (p_data->set_visibility.pair_mode == BTA_DM_NON_PAIRABLE) {
921             bta_dm_cb.disable_pair_mode = TRUE;
922         } else {
923             bta_dm_cb.disable_pair_mode = FALSE;
924         }
925 
926     }
927 
928     /* Send False or True if not ignore */
929     if (p_data->set_visibility.conn_paired_only != BTA_DM_IGNORE) {
930 
931         if (p_data->set_visibility.conn_paired_only == BTA_DM_CONN_ALL) {
932             bta_dm_cb.conn_paired_only = FALSE;
933         } else {
934             bta_dm_cb.conn_paired_only = TRUE;
935         }
936 
937     }
938 
939     /* Change mode if either mode is not ignore */
940     if (p_data->set_visibility.pair_mode != BTA_DM_IGNORE || p_data->set_visibility.conn_paired_only != BTA_DM_IGNORE) {
941         BTM_SetPairableMode((BOOLEAN)(!(bta_dm_cb.disable_pair_mode)), bta_dm_cb.conn_paired_only);
942     }
943 
944 }
945 
946 /*******************************************************************************
947 **
948 ** Function         bta_dm_process_remove_device
949 **
950 ** Description      Removes device, Disconnects ACL link if required.
951 ****
952 *******************************************************************************/
bta_dm_process_remove_device(BD_ADDR bd_addr,tBT_TRANSPORT transport)953 static void bta_dm_process_remove_device(BD_ADDR bd_addr, tBT_TRANSPORT transport)
954 {
955 #if (BLE_INCLUDED == TRUE && GATTC_INCLUDED == TRUE)
956     /* need to remove all pending background connection before unpair */
957     BTA_GATTC_CancelOpen(0, bd_addr, FALSE);
958 #endif
959 
960     BTM_SecDeleteDevice(bd_addr, transport);
961 
962 #if (BLE_INCLUDED == TRUE && GATTC_INCLUDED == TRUE)
963     /* remove all cached GATT information */
964     BTA_GATTC_Refresh(bd_addr, false);
965 #endif
966     if (bta_dm_cb.p_sec_cback) {
967         tBTA_DM_SEC sec_event;
968         bdcpy(sec_event.link_down.bd_addr, bd_addr);
969         sec_event.link_down.status = HCI_SUCCESS;
970         if (transport == BT_TRANSPORT_LE){
971             bta_dm_cb.p_sec_cback(BTA_DM_BLE_DEV_UNPAIRED_EVT, &sec_event);
972         } else {
973             bta_dm_cb.p_sec_cback(BTA_DM_DEV_UNPAIRED_EVT, &sec_event);
974         }
975 
976     }
977 }
978 
979 /*******************************************************************************
980 **
981 ** Function         bta_dm_remove_device
982 **
983 ** Description      Removes device, disconnects ACL link if required.
984 ****
985 *******************************************************************************/
bta_dm_remove_device(tBTA_DM_MSG * p_data)986 void bta_dm_remove_device(tBTA_DM_MSG *p_data)
987 {
988     tBTA_DM_API_REMOVE_DEVICE *p_dev = &p_data->remove_dev;
989     if (p_dev == NULL) {
990         return;
991     }
992 
993     /* If ACL exists for the device in the remove_bond message*/
994     BOOLEAN continue_delete_dev = FALSE;
995     UINT8 transport = p_dev->transport;
996 
997     if (BTM_IsAclConnectionUp(p_dev->bd_addr, transport)) {
998         APPL_TRACE_DEBUG("%s: ACL Up count  %d", __func__, bta_dm_cb.device_list.count);
999         continue_delete_dev = FALSE;
1000 
1001         /* Take the link down first, and mark the device for removal when disconnected */
1002         for (int i = 0; i < bta_dm_cb.device_list.count; i++) {
1003             if (!bdcmp(bta_dm_cb.device_list.peer_device[i].peer_bdaddr, p_dev->bd_addr)
1004 #if BLE_INCLUDED == TRUE
1005                 && bta_dm_cb.device_list.peer_device[i].transport == transport
1006 #endif
1007             ) {
1008 
1009                 bta_dm_cb.device_list.peer_device[i].conn_state = BTA_DM_UNPAIRING;
1010                 btm_remove_acl( p_dev->bd_addr, transport);
1011                 APPL_TRACE_DEBUG("%s:transport = %d", __func__, transport);
1012                 break;
1013             }
1014         }
1015     } else {
1016         continue_delete_dev = TRUE;
1017     }
1018 
1019     /* Delete the device mentioned in the msg */
1020     if (continue_delete_dev) {
1021         bta_dm_process_remove_device(p_dev->bd_addr, transport);
1022     }
1023 
1024     BTM_ClearInqDb (p_dev->bd_addr);
1025 }
1026 
1027 /*******************************************************************************
1028 **
1029 ** Function         bta_dm_add_device
1030 **
1031 ** Description      This function adds a Link Key to an security database entry.
1032 **                  It is normally called during host startup to restore all required information
1033 **                  stored in the NVRAM.
1034 ****
1035 *******************************************************************************/
bta_dm_add_device(tBTA_DM_MSG * p_data)1036 void bta_dm_add_device (tBTA_DM_MSG *p_data)
1037 {
1038     tBTA_DM_API_ADD_DEVICE *p_dev = &p_data->add_dev;
1039     UINT8   *p_dc = NULL;
1040     UINT8   *p_lc = NULL;
1041     UINT32  trusted_services_mask[BTM_SEC_SERVICE_ARRAY_SIZE];
1042     UINT8   index = 0;
1043     UINT8   btm_mask_index = 0;
1044 
1045     memset (trusted_services_mask, 0, sizeof(trusted_services_mask));
1046 
1047     /* If not all zeros, the device class has been specified */
1048     if (p_dev->dc_known) {
1049         p_dc = (UINT8 *)p_dev->dc;
1050     }
1051 
1052     if (p_dev->link_key_known) {
1053         p_lc = (UINT8 *)p_dev->link_key;
1054     }
1055 
1056     if (p_dev->is_trusted) {
1057         /* covert BTA service mask to BTM mask */
1058         while (p_dev->tm && (index < BTA_MAX_SERVICE_ID)) {
1059             if (p_dev->tm & (UINT32)(1 << index)) {
1060 
1061                 btm_mask_index =  bta_service_id_to_btm_srv_id_lkup_tbl[index] / BTM_SEC_ARRAY_BITS;
1062                 trusted_services_mask[btm_mask_index] |= (UINT32)(1 << (bta_service_id_to_btm_srv_id_lkup_tbl[index] - (UINT32)(btm_mask_index * 32)));
1063 
1064                 p_dev->tm &= (UINT32)(~(1 << index));
1065 
1066             }
1067             index++;
1068         }
1069     }
1070 
1071     if (!BTM_SecAddDevice (p_dev->bd_addr, p_dc, p_dev->bd_name, p_dev->features,
1072                            trusted_services_mask, p_lc, p_dev->key_type, p_dev->io_cap,
1073                            p_dev->pin_length, p_dev->sc_support)) {
1074         APPL_TRACE_ERROR ("BTA_DM: Error adding device %08x%04x",
1075                           (p_dev->bd_addr[0] << 24) + (p_dev->bd_addr[1] << 16) + (p_dev->bd_addr[2] << 8) + p_dev->bd_addr[3],
1076                           (p_dev->bd_addr[4] << 8) + p_dev->bd_addr[5]);
1077     }
1078 }
1079 
1080 /*******************************************************************************
1081 **
1082 ** Function         bta_dm_close_acl
1083 **
1084 ** Description      This function forces to close the connection to a remote device
1085 **                  and optionaly remove the device from security database if
1086 **                  required.
1087 ****
1088 *******************************************************************************/
bta_dm_close_acl(tBTA_DM_MSG * p_data)1089 void bta_dm_close_acl(tBTA_DM_MSG *p_data)
1090 {
1091     tBTA_DM_API_REMOVE_ACL *p_remove_acl = &p_data->remove_acl;
1092     UINT8   index;
1093 
1094     APPL_TRACE_DEBUG("bta_dm_close_acl");
1095 
1096     if (BTM_IsAclConnectionUp(p_remove_acl->bd_addr, p_remove_acl->transport)) {
1097         for (index = 0; index < bta_dm_cb.device_list.count; index ++) {
1098             if (!bdcmp( bta_dm_cb.device_list.peer_device[index].peer_bdaddr, p_remove_acl->bd_addr)) {
1099                 break;
1100             }
1101         }
1102         if (index != bta_dm_cb.device_list.count) {
1103             if (p_remove_acl->remove_dev) {
1104                 bta_dm_cb.device_list.peer_device[index].remove_dev_pending = TRUE;
1105             }
1106         } else {
1107             APPL_TRACE_ERROR("unknown device, remove ACL failed");
1108         }
1109         /* Disconnect the ACL link */
1110         btm_remove_acl(p_remove_acl->bd_addr, p_remove_acl->transport);
1111     }
1112     /* if to remove the device from security database ? do it now */
1113     else if (p_remove_acl->remove_dev) {
1114         if (!BTM_SecDeleteDevice(p_remove_acl->bd_addr, p_remove_acl->transport)) {
1115             APPL_TRACE_ERROR("delete device from security database failed.");
1116         }
1117 #if (BLE_INCLUDED == TRUE && GATTC_INCLUDED == TRUE)
1118         /* need to remove all pending background connection if any */
1119         BTA_GATTC_CancelOpen(0, p_remove_acl->bd_addr, FALSE);
1120         /* remove all cached GATT information */
1121         BTA_GATTC_Refresh(p_remove_acl->bd_addr, false);
1122 #endif
1123     }
1124     /* otherwise, no action needed */
1125 
1126 }
1127 
1128 /*******************************************************************************
1129 **
1130 ** Function         bta_dm_remove_all_acl
1131 **
1132 ** Description      This function forces to close all the ACL links specified by link type
1133 ****
1134 *******************************************************************************/
bta_dm_remove_all_acl(tBTA_DM_MSG * p_data)1135 void bta_dm_remove_all_acl(tBTA_DM_MSG *p_data)
1136 {
1137     const tBTA_DM_LINK_TYPE link_type = p_data->remove_all_acl.link_type;
1138     tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
1139 
1140     APPL_TRACE_DEBUG("%s link type = %d", __func__, link_type);
1141 
1142     for (UINT8 i = 0; i < bta_dm_cb.device_list.count; i++) {
1143         BD_ADDR addr = {0};
1144         bdcpy(addr, bta_dm_cb.device_list.peer_device[i].peer_bdaddr);
1145 #if defined (BLE_INCLUDED) && (BLE_INCLUDED == TRUE)
1146         transport = bta_dm_cb.device_list.peer_device[i].transport;
1147 #endif
1148         if ((link_type == BTA_DM_LINK_TYPE_ALL) ||
1149                 ((link_type == BTA_DM_LINK_TYPE_LE) && (transport == BT_TRANSPORT_LE)) ||
1150                 ((link_type == BTA_DM_LINK_TYPE_BR_EDR) && (transport == BT_TRANSPORT_BR_EDR))) {
1151             /* Disconnect the ACL link */
1152             btm_remove_acl(addr, transport);
1153         }
1154     }
1155 }
1156 
1157 
1158 /*******************************************************************************
1159 **
1160 ** Function         bta_dm_bond
1161 **
1162 ** Description      Bonds with peer device
1163 **
1164 **
1165 ** Returns          void
1166 **
1167 *******************************************************************************/
1168 #if (SMP_INCLUDED == TRUE)
bta_dm_bond(tBTA_DM_MSG * p_data)1169 void bta_dm_bond (tBTA_DM_MSG *p_data)
1170 {
1171     tBTM_STATUS status;
1172     tBTA_DM_SEC sec_event;
1173     char        *p_name;
1174 
1175     if (p_data->bond.transport == BTA_TRANSPORT_UNKNOWN) {
1176         status = BTM_SecBond ( p_data->bond.bd_addr, 0, NULL, 0 );
1177     } else {
1178         status = BTM_SecBondByTransport ( p_data->bond.bd_addr, p_data->bond.transport, 0, NULL, 0 );
1179     }
1180 
1181 
1182     if (bta_dm_cb.p_sec_cback && (status != BTM_CMD_STARTED)) {
1183 
1184         memset(&sec_event, 0, sizeof(tBTA_DM_SEC));
1185         bdcpy(sec_event.auth_cmpl.bd_addr, p_data->bond.bd_addr);
1186         p_name = BTM_SecReadDevName(p_data->bond.bd_addr);
1187         if (p_name != NULL) {
1188             memcpy(sec_event.auth_cmpl.bd_name, p_name, (BD_NAME_LEN - 1));
1189             sec_event.auth_cmpl.bd_name[BD_NAME_LEN - 1] = 0;
1190         }
1191 
1192         /*      taken care of by memset [above]
1193                 sec_event.auth_cmpl.key_present = FALSE;
1194                 sec_event.auth_cmpl.success = FALSE;
1195         */
1196         sec_event.auth_cmpl.fail_reason = HCI_ERR_ILLEGAL_COMMAND;
1197         if (status == BTM_SUCCESS) {
1198             sec_event.auth_cmpl.success = TRUE;
1199         } else {
1200             /* delete this device entry from Sec Dev DB */
1201             bta_dm_remove_sec_dev_entry(p_data->bond.bd_addr);
1202         }
1203         bta_dm_cb.p_sec_cback(BTA_DM_AUTH_CMPL_EVT, &sec_event);
1204     }
1205 
1206 }
1207 
1208 /*******************************************************************************
1209 **
1210 ** Function         bta_dm_bond_cancel
1211 **
1212 ** Description      Cancels bonding with a peer device
1213 **
1214 **
1215 ** Returns          void
1216 **
1217 *******************************************************************************/
bta_dm_bond_cancel(tBTA_DM_MSG * p_data)1218 void bta_dm_bond_cancel (tBTA_DM_MSG *p_data)
1219 {
1220     tBTM_STATUS status;
1221     tBTA_DM_SEC sec_event;
1222 
1223     APPL_TRACE_EVENT(" bta_dm_bond_cancel ");
1224     status = BTM_SecBondCancel ( p_data->bond_cancel.bd_addr );
1225 
1226     if (bta_dm_cb.p_sec_cback && (status != BTM_CMD_STARTED && status != BTM_SUCCESS)) {
1227         sec_event.bond_cancel_cmpl.result = BTA_FAILURE;
1228 
1229         bta_dm_cb.p_sec_cback(BTA_DM_BOND_CANCEL_CMPL_EVT, &sec_event);
1230     }
1231 
1232 }
1233 
1234 /*******************************************************************************
1235 **
1236 ** Function         bta_dm_set_pin_type
1237 **
1238 ** Description      Set the pin type and fixed pin
1239 **
1240 **
1241 ** Returns          void
1242 **
1243 *******************************************************************************/
bta_dm_set_pin_type(tBTA_DM_MSG * p_data)1244 void bta_dm_set_pin_type (tBTA_DM_MSG *p_data)
1245 {
1246 #if (CLASSIC_BT_INCLUDED == TRUE)
1247     BTM_SetPinType (p_data->set_pin_type.pin_type, p_data->set_pin_type.p_pin, p_data->set_pin_type.pin_len);
1248 #endif  ///CLASSIC_BT_INCLUDED == TRUE
1249 }
1250 
1251 /*******************************************************************************
1252 **
1253 ** Function         bta_dm_pin_reply
1254 **
1255 ** Description      Send the pin_reply to a request from BTM
1256 **
1257 **
1258 ** Returns          void
1259 **
1260 *******************************************************************************/
bta_dm_pin_reply(tBTA_DM_MSG * p_data)1261 void bta_dm_pin_reply (tBTA_DM_MSG *p_data)
1262 {
1263 #if (CLASSIC_BT_INCLUDED == TRUE)
1264     UINT32  trusted_mask[BTM_SEC_SERVICE_ARRAY_SIZE];
1265     UINT32   *current_trusted_mask;
1266 
1267     current_trusted_mask = BTM_ReadTrustedMask(p_data->pin_reply.bd_addr);
1268 
1269     if (current_trusted_mask) {
1270         memcpy(trusted_mask, current_trusted_mask, sizeof(trusted_mask));
1271     } else {
1272         memset(trusted_mask, 0, sizeof(trusted_mask));
1273     }
1274 
1275     if (p_data->pin_reply.accept) {
1276 
1277         BTM_PINCodeReply(p_data->pin_reply.bd_addr, BTM_SUCCESS, p_data->pin_reply.pin_len, p_data->pin_reply.p_pin, trusted_mask );
1278     } else {
1279         BTM_PINCodeReply(p_data->pin_reply.bd_addr, BTM_NOT_AUTHORIZED, 0, NULL, trusted_mask );
1280     }
1281 #endif  ///CLASSIC_BT_INCLUDED == TRUE
1282 }
1283 #endif  ///SMP_INCLUDED == TRUE
1284 
1285 /*******************************************************************************
1286 **
1287 ** Function         bta_dm_policy_cback
1288 **
1289 ** Description      process the link policy changes
1290 **
1291 ** Returns          void
1292 **
1293 *******************************************************************************/
bta_dm_policy_cback(tBTA_SYS_CONN_STATUS status,UINT8 id,UINT8 app_id,BD_ADDR peer_addr)1294 static void bta_dm_policy_cback(tBTA_SYS_CONN_STATUS status, UINT8 id, UINT8 app_id, BD_ADDR peer_addr)
1295 {
1296     tBTA_DM_PEER_DEVICE *p_dev = NULL;
1297     UINT16  policy = app_id;
1298     UINT32  mask = (UINT32)(1 << id);
1299 
1300     if (peer_addr) {
1301         p_dev = bta_dm_find_peer_device(peer_addr);
1302     }
1303 
1304     APPL_TRACE_DEBUG(" bta_dm_policy_cback cmd:%d, policy:0x%x",
1305                      status, policy);
1306     switch (status) {
1307     case BTA_SYS_PLCY_SET:
1308         if (!p_dev) {
1309             return;
1310         }
1311         /* restore the default link policy */
1312         p_dev->link_policy |= policy;
1313         BTM_SetLinkPolicy(p_dev->peer_bdaddr, &(p_dev->link_policy));
1314         break;
1315 
1316     case BTA_SYS_PLCY_CLR:
1317         if (!p_dev) {
1318             return;
1319         }
1320         /* clear the policy from the default link policy */
1321         p_dev->link_policy &= (~policy);
1322         BTM_SetLinkPolicy(p_dev->peer_bdaddr, &(p_dev->link_policy));
1323 
1324 #if (BTA_DM_PM_INCLUDED == TRUE)
1325         if (policy & (HCI_ENABLE_SNIFF_MODE | HCI_ENABLE_PARK_MODE)) {
1326             /* if clearing sniff/park, wake the link */
1327             bta_dm_pm_active(p_dev->peer_bdaddr);
1328         }
1329 #endif /* #if (BTA_DM_PM_INCLUDED == TRUE) */
1330         break;
1331 
1332     case BTA_SYS_PLCY_DEF_SET:
1333         /* want to restore/set the role switch policy */
1334         bta_dm_cb.role_policy_mask &= ~mask;
1335         if (0 == bta_dm_cb.role_policy_mask) {
1336             /* if nobody wants to insist on the role */
1337             bta_dm_cb.cur_policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
1338             BTM_SetDefaultLinkPolicy(bta_dm_cb.cur_policy);
1339         }
1340         break;
1341 
1342     case BTA_SYS_PLCY_DEF_CLR:
1343         /* want to remove the role switch policy */
1344         bta_dm_cb.role_policy_mask |= mask;
1345         bta_dm_cb.cur_policy &= ~HCI_ENABLE_MASTER_SLAVE_SWITCH;
1346         BTM_SetDefaultLinkPolicy(bta_dm_cb.cur_policy);
1347         break;
1348     }
1349 }
1350 
1351 /*******************************************************************************
1352 **
1353 ** Function         bta_dm_confirm
1354 **
1355 ** Description      Send the user confirm request reply in response to a
1356 **                  request from BTM
1357 **
1358 ** Returns          void
1359 **
1360 *******************************************************************************/
1361 #if (SMP_INCLUDED == TRUE)
bta_dm_confirm(tBTA_DM_MSG * p_data)1362 void bta_dm_confirm(tBTA_DM_MSG *p_data)
1363 {
1364     tBTM_STATUS res = BTM_NOT_AUTHORIZED;
1365 
1366     if (p_data->confirm.accept == TRUE) {
1367         res = BTM_SUCCESS;
1368     }
1369     BTM_ConfirmReqReply(res, p_data->confirm.bd_addr);
1370 }
1371 #endif  ///SMP_INCLUDED == TRUE
1372 
1373 /*******************************************************************************
1374 **
1375 ** Function         bta_dm_key_req
1376 **
1377 ** Description      Send the user passkey request reply in response to a
1378 **                  request from BTM
1379 **
1380 ** Returns          void
1381 **
1382 *******************************************************************************/
1383 #if (SMP_INCLUDED == TRUE && BT_SSP_INCLUDED)
bta_dm_key_req(tBTA_DM_MSG * p_data)1384 void bta_dm_key_req(tBTA_DM_MSG *p_data)
1385 {
1386     tBTM_STATUS res = BTM_NOT_AUTHORIZED;
1387 
1388     if (p_data->key_req.accept == TRUE) {
1389         res = BTM_SUCCESS;
1390     }
1391     BTM_PasskeyReqReply(res, p_data->key_req.bd_addr, p_data->key_req.passkey);
1392 }
1393 #endif  ///SMP_INCLUDED == TRUE && BT_SSP_INCLUDED
1394 
1395 /*******************************************************************************
1396 **
1397 ** Function         bta_dm_loc_oob
1398 **
1399 ** Description      Retrieve the OOB data from the local LM
1400 **
1401 ** Returns          void
1402 **
1403 *******************************************************************************/
1404 #if (BTM_OOB_INCLUDED == TRUE && SMP_INCLUDED == TRUE)
bta_dm_loc_oob(tBTA_DM_MSG * p_data)1405 void bta_dm_loc_oob(tBTA_DM_MSG *p_data)
1406 {
1407     UNUSED(p_data);
1408     BTM_ReadLocalOobData();
1409 }
1410 
1411 /*******************************************************************************
1412 **
1413 ** Function         bta_dm_oob_reply
1414 **
1415 ** Description      This function is called to provide the OOB data for
1416 **                  SMP in response to BLE OOB request.
1417 **
1418 ** Returns          void
1419 **
1420 *******************************************************************************/
bta_dm_oob_reply(tBTA_DM_MSG * p_data)1421 void bta_dm_oob_reply(tBTA_DM_MSG *p_data)
1422 {
1423 #if (BLE_INCLUDED)
1424     BTM_BleOobDataReply(p_data->oob_reply.bd_addr, BTM_SUCCESS, p_data->oob_reply.len, p_data->oob_reply.value);
1425 #endif
1426 }
1427 
1428 /*******************************************************************************
1429 **
1430 ** Function         bta_dm_ci_io_req_act
1431 **
1432 ** Description      respond to the IO capabilities request from BTM
1433 **
1434 ** Returns          void
1435 **
1436 *******************************************************************************/
bta_dm_ci_io_req_act(tBTA_DM_MSG * p_data)1437 void bta_dm_ci_io_req_act(tBTA_DM_MSG *p_data)
1438 {
1439     tBTM_AUTH_REQ   auth_req = BTM_AUTH_AP_NO;
1440     if (p_data->ci_io_req.auth_req) {
1441         auth_req = BTM_AUTH_AP_YES;
1442     }
1443     BTM_IoCapRsp(p_data->ci_io_req.bd_addr, p_data->ci_io_req.io_cap,
1444                  p_data->ci_io_req.oob_data, auth_req);
1445 }
1446 
1447 /*******************************************************************************
1448 **
1449 ** Function         bta_dm_ci_rmt_oob_act
1450 **
1451 ** Description      respond to the OOB data request for the remote device from BTM
1452 **
1453 **
1454 ** Returns          void
1455 **
1456 *******************************************************************************/
bta_dm_ci_rmt_oob_act(tBTA_DM_MSG * p_data)1457 void bta_dm_ci_rmt_oob_act(tBTA_DM_MSG *p_data)
1458 {
1459     tBTM_STATUS res = BTM_NOT_AUTHORIZED;
1460 
1461     if (p_data->ci_rmt_oob.accept == TRUE) {
1462         res = BTM_SUCCESS;
1463     }
1464     BTM_RemoteOobDataReply(res, p_data->ci_rmt_oob.bd_addr,
1465                            p_data->ci_rmt_oob.c, p_data->ci_rmt_oob.r );
1466 }
1467 #endif /* BTM_OOB_INCLUDED == TRUE && SMP_INCLUDED == TRUE */
1468 
1469 /*******************************************************************************
1470 **
1471 ** Function         bta_dm_search_start
1472 **
1473 ** Description      Starts an inquiry
1474 **
1475 **
1476 ** Returns          void
1477 **
1478 *******************************************************************************/
bta_dm_search_start(tBTA_DM_MSG * p_data)1479 void bta_dm_search_start (tBTA_DM_MSG *p_data)
1480 {
1481     tBTM_INQUIRY_CMPL result;
1482 
1483 #if (BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE && SDP_INCLUDED == TRUE) && (GATTC_INCLUDED == TRUE)
1484     UINT16 len = (UINT16)(sizeof(tBT_UUID) * p_data->search.num_uuid);
1485     bta_dm_gattc_register();
1486 #endif
1487 
1488     APPL_TRACE_DEBUG("%s avoid_scatter=%d", __func__, p_bta_dm_cfg->avoid_scatter);
1489 
1490     if (p_bta_dm_cfg->avoid_scatter &&
1491             (p_data->search.rs_res == BTA_DM_RS_NONE) && bta_dm_check_av(BTA_DM_API_SEARCH_EVT)) {
1492         memcpy(&bta_dm_cb.search_msg, &p_data->search, sizeof(tBTA_DM_API_SEARCH));
1493         return;
1494     }
1495 
1496     BTM_ClearInqDb(NULL);
1497     /* save search params */
1498     bta_dm_search_cb.p_search_cback = p_data->search.p_cback;
1499     bta_dm_search_cb.services = p_data->search.services;
1500 
1501 #if (BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE && SDP_INCLUDED == TRUE) && (GATTC_INCLUDED == TRUE)
1502     utl_freebuf((void **)&bta_dm_search_cb.p_srvc_uuid);
1503 
1504     if ((bta_dm_search_cb.num_uuid = p_data->search.num_uuid) != 0 &&
1505             p_data->search.p_uuid != NULL) {
1506         if ((bta_dm_search_cb.p_srvc_uuid = (tBT_UUID *)osi_malloc(len)) == NULL) {
1507             APPL_TRACE_ERROR("%s no resources", __func__);
1508 
1509             result.status = BTA_FAILURE;
1510             result.num_resp = 0;
1511             bta_dm_inq_cmpl_cb ((void *)&result);
1512             return;
1513         }
1514 
1515         memcpy(bta_dm_search_cb.p_srvc_uuid, p_data->search.p_uuid, len);
1516     }
1517 #endif
1518     result.status = BTM_StartInquiry(   (tBTM_INQ_PARMS *)&p_data->search.inq_params,
1519                                         bta_dm_inq_results_cb,
1520                                         (tBTM_CMPL_CB *) bta_dm_inq_cmpl_cb);
1521 
1522     APPL_TRACE_EVENT("%s status=%d", __func__, result.status);
1523     if (result.status != BTM_CMD_STARTED) {
1524         result.num_resp = 0;
1525         bta_dm_inq_cmpl_cb ((void *)&result);
1526     }
1527 }
1528 
1529 /*******************************************************************************
1530 **
1531 ** Function         bta_dm_search_cancel
1532 **
1533 ** Description      Cancels an ongoing search for devices
1534 **
1535 **
1536 ** Returns          void
1537 **
1538 *******************************************************************************/
bta_dm_search_cancel(tBTA_DM_MSG * p_data)1539 void bta_dm_search_cancel (tBTA_DM_MSG *p_data)
1540 {
1541     UNUSED(p_data);
1542     tBTA_DM_MSG *p_msg;
1543 
1544     if (BTM_IsInquiryActive()) {
1545         if (BTM_CancelInquiry() != BTM_CMD_STARTED) {
1546             bta_dm_search_cancel_notify(NULL);
1547             p_msg = (tBTA_DM_MSG *) osi_malloc(sizeof(tBTA_DM_MSG));
1548             if (p_msg != NULL) {
1549                 p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
1550                 p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
1551                 bta_sys_sendmsg(p_msg);
1552             }
1553         } else {
1554             /* flag a search cancel is pending */
1555             bta_dm_search_cb.cancel_pending = TRUE;
1556         }
1557     }
1558     /* If no Service Search going on then issue cancel remote name in case it is active */
1559     else if (!bta_dm_search_cb.name_discover_done) {
1560         BTM_CancelRemoteDeviceName();
1561 
1562         if ((p_msg = (tBTA_DM_MSG *) osi_malloc(sizeof(tBTA_DM_MSG))) != NULL) {
1563             p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
1564             p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
1565             bta_sys_sendmsg(p_msg);
1566         }
1567 
1568     } else {
1569         if ((p_msg = (tBTA_DM_MSG *) osi_malloc(sizeof(tBTA_DM_MSG))) != NULL) {
1570             p_msg->hdr.event = BTA_DM_INQUIRY_CMPL_EVT;
1571             p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
1572             bta_sys_sendmsg(p_msg);
1573         }
1574     }
1575 
1576 #if BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE && SDP_INCLUDED == TRUE && GATTC_INCLUDED == TRUE
1577     if (bta_dm_search_cb.gatt_disc_active) {
1578         bta_dm_cancel_gatt_discovery(bta_dm_search_cb.peer_bdaddr);
1579     }
1580 #endif
1581 }
1582 
1583 /*******************************************************************************
1584 **
1585 ** Function         bta_dm_discover
1586 **
1587 ** Description      Discovers services on a remote device
1588 **
1589 **
1590 ** Returns          void
1591 **
1592 *******************************************************************************/
1593 #if (SDP_INCLUDED == TRUE)
bta_dm_discover(tBTA_DM_MSG * p_data)1594 void bta_dm_discover (tBTA_DM_MSG *p_data)
1595 {
1596 #if BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE  && GATTC_INCLUDED == TRUE
1597     UINT16 len = (UINT16)(sizeof(tBT_UUID) * p_data->discover.num_uuid);
1598 #endif
1599     APPL_TRACE_EVENT("%s services_to_search=0x%04X, sdp_search=%d", __func__,
1600                      p_data->discover.services, p_data->discover.sdp_search);
1601 
1602     /* save the search condition */
1603     bta_dm_search_cb.services = p_data->discover.services;
1604 
1605 #if BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE && GATTC_INCLUDED == TRUE
1606     bta_dm_gattc_register();
1607     utl_freebuf((void **)&bta_dm_search_cb.p_srvc_uuid);
1608     if ((bta_dm_search_cb.num_uuid = p_data->discover.num_uuid) != 0 &&
1609             p_data->discover.p_uuid != NULL) {
1610         if ((bta_dm_search_cb.p_srvc_uuid = (tBT_UUID *)osi_malloc(len)) == NULL) {
1611             p_data->discover.p_cback(BTA_DM_DISC_CMPL_EVT, NULL);
1612             return;
1613         }
1614         memcpy(bta_dm_search_cb.p_srvc_uuid, p_data->discover.p_uuid, len);
1615     }
1616     bta_dm_search_cb.uuid_to_search = bta_dm_search_cb.num_uuid;
1617 #endif
1618 
1619     bta_dm_search_cb.p_search_cback = p_data->discover.p_cback;
1620     bta_dm_search_cb.sdp_search = p_data->discover.sdp_search;
1621     bta_dm_search_cb.services_to_search = bta_dm_search_cb.services;
1622     bta_dm_search_cb.service_index = 0;
1623     bta_dm_search_cb.services_found = 0;
1624     bta_dm_search_cb.peer_name[0] = 0;
1625     bta_dm_search_cb.sdp_search = p_data->discover.sdp_search;
1626     bta_dm_search_cb.p_btm_inq_info = BTM_InqDbRead (p_data->discover.bd_addr);
1627     bta_dm_search_cb.transport = p_data->discover.transport;
1628 
1629     bta_dm_search_cb.name_discover_done = FALSE;
1630     memcpy(&bta_dm_search_cb.uuid, &p_data->discover.uuid, sizeof(tSDP_UUID));
1631     bta_dm_discover_device(p_data->discover.bd_addr);
1632 }
1633 
1634 /*******************************************************************************
1635 **
1636 ** Function         bta_dm_di_disc_cmpl
1637 **
1638 ** Description      Sends event to application when DI discovery complete
1639 **
1640 ** Returns          void
1641 **
1642 *******************************************************************************/
bta_dm_di_disc_cmpl(tBTA_DM_MSG * p_data)1643 void bta_dm_di_disc_cmpl(tBTA_DM_MSG *p_data)
1644 {
1645     tBTA_DM_DI_DISC_CMPL    di_disc;
1646 
1647     memset(&di_disc, 0, sizeof(tBTA_DM_DI_DISC_CMPL));
1648     bdcpy(di_disc.bd_addr, bta_dm_search_cb.peer_bdaddr);
1649 
1650     if ((p_data->hdr.offset == SDP_SUCCESS)
1651             || (p_data->hdr.offset == SDP_DB_FULL)) {
1652         di_disc.num_record  = SDP_GetNumDiRecords(bta_dm_di_cb.p_di_db);
1653     } else {
1654         di_disc.result      = BTA_FAILURE;
1655     }
1656 
1657     bta_dm_di_cb.p_di_db = NULL;
1658     bta_dm_search_cb.p_search_cback(BTA_DM_DI_DISC_CMPL_EVT, (tBTA_DM_SEARCH *) &di_disc);
1659 }
1660 
1661 /*******************************************************************************
1662 **
1663 ** Function         bta_dm_di_disc_callback
1664 **
1665 ** Description      This function queries a remote device for DI information.
1666 **
1667 **
1668 ** Returns          void
1669 **
1670 *******************************************************************************/
bta_dm_di_disc_callback(UINT16 result)1671 static void bta_dm_di_disc_callback(UINT16 result)
1672 {
1673     tBTA_DM_MSG *p_msg;
1674 
1675     if ((p_msg = (tBTA_DM_MSG *) osi_malloc(sizeof(tBTA_DM_MSG))) != NULL) {
1676         p_msg->hdr.event            = BTA_DM_SEARCH_CMPL_EVT;
1677         p_msg->hdr.layer_specific   = BTA_DM_API_DI_DISCOVER_EVT;
1678         p_msg->hdr.offset           = result;
1679         bta_sys_sendmsg(p_msg);
1680     }
1681 }
1682 #endif  ///SDP_INCLUDED == TRUE
1683 /*******************************************************************************
1684 **
1685 ** Function         bta_dm_disable_search_and_disc
1686 **
1687 ** Description      Cancels an ongoing search or discovery for devices in case of
1688 **                  a Bluetooth disable
1689 **
1690 **
1691 ** Returns          void
1692 **
1693 *******************************************************************************/
bta_dm_disable_search_and_disc(void)1694 static void bta_dm_disable_search_and_disc (void)
1695 {
1696 #if (SDP_INCLUDED == TRUE)
1697     tBTA_DM_DI_DISC_CMPL    di_disc;
1698 #endif ///SDP_INCLUDED == TRUE
1699     if (bta_dm_search_cb.state != BTA_DM_SEARCH_IDLE) {
1700         bta_dm_search_cancel(NULL);
1701     }
1702 #if (SDP_INCLUDED == TRUE)
1703     if (bta_dm_di_cb.p_di_db != NULL) {
1704         memset(&di_disc, 0, sizeof(tBTA_DM_DI_DISC_CMPL));
1705         bdcpy(di_disc.bd_addr, bta_dm_search_cb.peer_bdaddr);
1706         di_disc.result      = BTA_FAILURE;
1707 
1708         bta_dm_di_cb.p_di_db = NULL;
1709         bta_dm_search_cb.p_search_cback(BTA_DM_DI_DISC_CMPL_EVT, NULL);
1710     }
1711 #endif  ///SDP_INCLUDED == TRUE
1712 }
1713 
1714 /*******************************************************************************
1715 **
1716 ** Function         bta_dm_di_disc
1717 **
1718 ** Description      This function queries a remote device for DI information.
1719 **
1720 **
1721 ** Returns          void
1722 **
1723 *******************************************************************************/
1724 #if (SDP_INCLUDED == TRUE)
bta_dm_di_disc(tBTA_DM_MSG * p_data)1725 void bta_dm_di_disc (tBTA_DM_MSG *p_data)
1726 {
1727     UINT16  result = BTA_FAILURE;
1728     tBTA_DM_MSG *p_msg;
1729 
1730     bta_dm_search_cb.p_search_cback = p_data->di_disc.p_cback;
1731     bdcpy(bta_dm_search_cb.peer_bdaddr, p_data->di_disc.bd_addr);
1732     bta_dm_di_cb.p_di_db = p_data->di_disc.p_sdp_db;
1733 
1734     if ((bta_dm_search_cb.p_sdp_db = (tSDP_DISCOVERY_DB *)osi_malloc(BTA_DM_SDP_DB_SIZE)) != NULL) {
1735         if ( SDP_DiDiscover(bta_dm_search_cb.peer_bdaddr, p_data->di_disc.p_sdp_db,
1736                             p_data->di_disc.len, bta_dm_di_disc_callback) == SDP_SUCCESS) {
1737             result = BTA_SUCCESS;
1738         }
1739     } else {
1740         APPL_TRACE_ERROR("No buffer to start DI discovery");
1741     }
1742 
1743     if ( result == BTA_FAILURE &&
1744             (p_msg = (tBTA_DM_MSG *) osi_malloc(sizeof(tBTA_DM_MSG))) != NULL) {
1745         p_msg->hdr.event            = BTA_DM_SEARCH_CMPL_EVT;
1746         p_msg->hdr.layer_specific   = BTA_DM_API_DI_DISCOVER_EVT;
1747         p_data->hdr.offset          = result;
1748         bta_sys_sendmsg(p_msg);
1749     }
1750 }
1751 #endif  ///SDP_INCLUDED == TRUE
1752 
1753 /*******************************************************************************
1754 **
1755 ** Function         bta_dm_inq_cmpl
1756 **
1757 ** Description      Process the inquiry complete event from BTM
1758 **
1759 ** Returns          void
1760 **
1761 *******************************************************************************/
bta_dm_inq_cmpl(tBTA_DM_MSG * p_data)1762 void bta_dm_inq_cmpl (tBTA_DM_MSG *p_data)
1763 {
1764     tBTA_DM_MSG *p_msg;
1765     tBTA_DM_SEARCH  data;
1766 
1767     APPL_TRACE_DEBUG("bta_dm_inq_cmpl");
1768 
1769     data.inq_cmpl.num_resps = p_data->inq_cmpl.num;
1770     bta_dm_search_cb.p_search_cback(BTA_DM_INQ_CMPL_EVT, &data);
1771 
1772     if ((bta_dm_search_cb.p_btm_inq_info = BTM_InqDbFirst()) != NULL) {
1773         /* start name and service discovery from the first device on inquiry result */
1774         bta_dm_search_cb.name_discover_done = FALSE;
1775         bta_dm_search_cb.peer_name[0]       = 0;
1776 #if (SDP_INCLUDED == TRUE)
1777         bta_dm_discover_device(bta_dm_search_cb.p_btm_inq_info->results.remote_bd_addr);
1778 #endif  ///SDP_INCLUDED == TRUE
1779     } else {
1780         /* no devices, search complete */
1781         bta_dm_search_cb.services = 0;
1782 
1783         if ((p_msg = (tBTA_DM_MSG *) osi_malloc(sizeof(tBTA_DM_MSG))) != NULL) {
1784             p_msg->hdr.event          = BTA_DM_SEARCH_CMPL_EVT;
1785             p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
1786             bta_sys_sendmsg(p_msg);
1787         }
1788     }
1789 }
1790 
1791 /*******************************************************************************
1792 **
1793 ** Function         bta_dm_rmt_name
1794 **
1795 ** Description      Process the remote name result from BTM
1796 **
1797 ** Returns          void
1798 **
1799 *******************************************************************************/
bta_dm_rmt_name(tBTA_DM_MSG * p_data)1800 void bta_dm_rmt_name (tBTA_DM_MSG *p_data)
1801 {
1802     APPL_TRACE_DEBUG("bta_dm_rmt_name");
1803 
1804     if ( p_data->rem_name.result.disc_res.bd_name[0] && bta_dm_search_cb.p_btm_inq_info) {
1805         bta_dm_search_cb.p_btm_inq_info->appl_knows_rem_name = TRUE;
1806     }
1807 #if (SDP_INCLUDED == TRUE)
1808     bta_dm_discover_device(bta_dm_search_cb.peer_bdaddr);
1809 #endif  ///SDP_INCLUDED == TRUE
1810 }
1811 
1812 /*******************************************************************************
1813 **
1814 ** Function         bta_dm_disc_rmt_name
1815 **
1816 ** Description      Process the remote name result from BTM when application
1817 **                  wants to find the name for a bdaddr
1818 **
1819 ** Returns          void
1820 **
1821 *******************************************************************************/
bta_dm_disc_rmt_name(tBTA_DM_MSG * p_data)1822 void bta_dm_disc_rmt_name (tBTA_DM_MSG *p_data)
1823 {
1824     tBTM_INQ_INFO *p_btm_inq_info;
1825 
1826     APPL_TRACE_DEBUG("bta_dm_disc_rmt_name");
1827 
1828     p_btm_inq_info = BTM_InqDbRead (p_data->rem_name.result.disc_res.bd_addr);
1829     if ( p_btm_inq_info ) {
1830         if ( p_data->rem_name.result.disc_res.bd_name[0] ) {
1831             p_btm_inq_info->appl_knows_rem_name = TRUE;
1832         }
1833     }
1834 #if (SDP_INCLUDED == TRUE)
1835     bta_dm_discover_device(p_data->rem_name.result.disc_res.bd_addr);
1836 #endif  ///SDP_INCLUDED == TRUE
1837 }
1838 
1839 /*******************************************************************************
1840 **
1841 ** Function         bta_dm_sdp_result
1842 **
1843 ** Description      Process the discovery result from sdp
1844 **
1845 ** Returns          void
1846 **
1847 *******************************************************************************/
1848 #if (SDP_INCLUDED == TRUE)
bta_dm_sdp_result(tBTA_DM_MSG * p_data)1849 void bta_dm_sdp_result (tBTA_DM_MSG *p_data)
1850 {
1851     tSDP_DISC_REC   *p_sdp_rec = NULL;
1852     tBTA_DM_MSG     *p_msg;
1853     BOOLEAN          scn_found = FALSE;
1854     UINT16           service = 0xFFFF;
1855     tSDP_PROTOCOL_ELEM  pe;
1856 
1857 #if BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE
1858     tBT_UUID           *p_uuid = bta_dm_search_cb.p_srvc_uuid;
1859     tBTA_DM_SEARCH      result;
1860     tBT_UUID            service_uuid;
1861 #endif
1862 
1863     UINT32 num_uuids = 0;
1864     UINT8  uuid_list[32][MAX_UUID_SIZE]; // assuming a max of 32 services
1865 
1866     if ((p_data->sdp_event.sdp_result == SDP_SUCCESS)
1867             || (p_data->sdp_event.sdp_result == SDP_NO_RECS_MATCH)
1868             || (p_data->sdp_event.sdp_result == SDP_DB_FULL)) {
1869         APPL_TRACE_DEBUG("sdp_result::0x%x", p_data->sdp_event.sdp_result);
1870         do {
1871             p_sdp_rec = NULL;
1872             if ( bta_dm_search_cb.service_index == (BTA_USER_SERVICE_ID + 1) ) {
1873                 p_sdp_rec = SDP_FindServiceUUIDInDb(bta_dm_search_cb.p_sdp_db, &bta_dm_search_cb.uuid, p_sdp_rec);
1874                 if (p_sdp_rec && SDP_FindProtocolListElemInRec(p_sdp_rec, UUID_PROTOCOL_RFCOMM, &pe)) {
1875                     bta_dm_search_cb.peer_scn = (UINT8) pe.params[0];
1876                     scn_found = TRUE;
1877                 }
1878             } else {
1879                 service = bta_service_id_to_uuid_lkup_tbl[bta_dm_search_cb.service_index - 1];
1880                 p_sdp_rec = SDP_FindServiceInDb(bta_dm_search_cb.p_sdp_db, service, p_sdp_rec);
1881             }
1882 #if BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE
1883             /* finished with BR/EDR services, now we check the result for GATT based service UUID */
1884             if (bta_dm_search_cb.service_index == BTA_MAX_SERVICE_ID) {
1885                 if (bta_dm_search_cb.uuid_to_search != 0 && p_uuid != NULL) {
1886                     p_uuid +=  (bta_dm_search_cb.num_uuid - bta_dm_search_cb.uuid_to_search);
1887                     /* only support 16 bits UUID for now */
1888                     service = p_uuid->uu.uuid16;
1889                 }
1890                 /* all GATT based services */
1891                 do {
1892                     /* find a service record, report it */
1893                     p_sdp_rec = SDP_FindServiceInDb(bta_dm_search_cb.p_sdp_db,
1894                                                     0, p_sdp_rec);
1895                     if (p_sdp_rec) {
1896                         if (SDP_FindServiceUUIDInRec(p_sdp_rec, &service_uuid)) {
1897                             /* send result back to app now, one by one */
1898                             bdcpy (result.disc_ble_res.bd_addr, bta_dm_search_cb.peer_bdaddr);
1899                             BCM_STRNCPY_S((char *)result.disc_ble_res.bd_name, bta_dm_get_remname(), (BD_NAME_LEN));
1900                             result.disc_ble_res.bd_name[BD_NAME_LEN] = 0;
1901                             result.disc_ble_res.service.len = service_uuid.len;
1902                             result.disc_ble_res.service.uu.uuid16 = service_uuid.uu.uuid16;
1903 
1904                             bta_dm_search_cb.p_search_cback(BTA_DM_DISC_BLE_RES_EVT, &result);
1905                         }
1906                     }
1907 
1908                     if (bta_dm_search_cb.uuid_to_search > 0) {
1909                         break;
1910                     }
1911 
1912                 } while (p_sdp_rec);
1913             } else
1914 #endif
1915             {
1916                 /* SDP_DB_FULL means some records with the
1917                    required attributes were received */
1918                 if (((p_data->sdp_event.sdp_result == SDP_DB_FULL) &&
1919                         bta_dm_search_cb.services != BTA_ALL_SERVICE_MASK) ||
1920                         (p_sdp_rec != NULL)) {
1921                     if (service != UUID_SERVCLASS_PNP_INFORMATION && service != 0) {
1922                         UINT16 tmp_svc = 0xFFFF;
1923                         bta_dm_search_cb.services_found |=
1924                             (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(bta_dm_search_cb.service_index - 1));
1925                         tmp_svc = bta_service_id_to_uuid_lkup_tbl[bta_dm_search_cb.service_index - 1];
1926                         /* Add to the list of UUIDs */
1927                         sdpu_uuid16_to_uuid128(tmp_svc, uuid_list[num_uuids]);
1928                         num_uuids++;
1929                     }
1930                 }
1931             }
1932 
1933             if (bta_dm_search_cb.services == BTA_ALL_SERVICE_MASK &&
1934                     bta_dm_search_cb.services_to_search == 0) {
1935 #if BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE
1936                 if ( bta_dm_search_cb.service_index == BTA_BLE_SERVICE_ID &&
1937                         bta_dm_search_cb.uuid_to_search > 0) {
1938                     bta_dm_search_cb.uuid_to_search --;
1939                 }
1940 
1941                 if (bta_dm_search_cb.uuid_to_search == 0 ||
1942                         bta_dm_search_cb.service_index != BTA_BLE_SERVICE_ID)
1943 #endif
1944                     bta_dm_search_cb.service_index++;
1945             } else { /* regular one service per search or PNP search */
1946                 break;
1947             }
1948         } while (bta_dm_search_cb.service_index <= BTA_MAX_SERVICE_ID);
1949 
1950         APPL_TRACE_DEBUG("%s services_found = %04x", __FUNCTION__,
1951                          bta_dm_search_cb.services_found);
1952 
1953         /* Collect the 128-bit services here and put them into the list */
1954         if (bta_dm_search_cb.services == BTA_ALL_SERVICE_MASK) {
1955             p_sdp_rec = NULL;
1956             do {
1957                 tBT_UUID temp_uuid;
1958                 /* find a service record, report it */
1959                 p_sdp_rec = SDP_FindServiceInDb_128bit(bta_dm_search_cb.p_sdp_db, p_sdp_rec);
1960                 if (p_sdp_rec) {
1961                     if (SDP_FindServiceUUIDInRec_128bit(p_sdp_rec, &temp_uuid)) {
1962                         memcpy(uuid_list[num_uuids], temp_uuid.uu.uuid128, MAX_UUID_SIZE);
1963                         num_uuids++;
1964                     }
1965                 }
1966             } while (p_sdp_rec);
1967         }
1968         /* if there are more services to search for */
1969         if (bta_dm_search_cb.services_to_search) {
1970             /* Free up the p_sdp_db before checking the next one */
1971             bta_dm_free_sdp_db(NULL);
1972             bta_dm_find_services(bta_dm_search_cb.peer_bdaddr);
1973         } else {
1974             /* callbacks */
1975             /* start next bd_addr if necessary */
1976 
1977             BTM_SecDeleteRmtNameNotifyCallback(&bta_dm_service_search_remname_cback);
1978 
1979 
1980             if ((p_msg = (tBTA_DM_MSG *) osi_malloc(sizeof(tBTA_DM_MSG))) != NULL) {
1981                 p_msg->hdr.event = BTA_DM_DISCOVERY_RESULT_EVT;
1982                 p_msg->disc_result.result.disc_res.result = BTA_SUCCESS;
1983                 p_msg->disc_result.result.disc_res.p_raw_data = NULL;
1984                 p_msg->disc_result.result.disc_res.raw_data_size = 0;
1985                 p_msg->disc_result.result.disc_res.num_uuids = num_uuids;
1986                 p_msg->disc_result.result.disc_res.p_uuid_list = NULL;
1987                 if (num_uuids > 0) {
1988                     p_msg->disc_result.result.disc_res.p_uuid_list = (UINT8 *)osi_malloc(num_uuids * MAX_UUID_SIZE);
1989                     if (p_msg->disc_result.result.disc_res.p_uuid_list) {
1990                         memcpy(p_msg->disc_result.result.disc_res.p_uuid_list, uuid_list,
1991                                num_uuids * MAX_UUID_SIZE);
1992                     } else {
1993                         p_msg->disc_result.result.disc_res.num_uuids = 0;
1994                         APPL_TRACE_ERROR("%s: Unable to allocate memory for uuid_list", __func__);
1995                     }
1996                 }
1997                 //copy the raw_data to the discovery result  structure
1998                 //
1999 
2000                 if (  bta_dm_search_cb.p_sdp_db != NULL && bta_dm_search_cb.p_sdp_db->raw_used != 0   &&
2001                         bta_dm_search_cb.p_sdp_db->raw_data != NULL) {
2002                     APPL_TRACE_DEBUG(
2003                         "%s raw_data used = 0x%x raw_data_ptr = %p", __func__,
2004                         bta_dm_search_cb.p_sdp_db->raw_used,
2005                         bta_dm_search_cb.p_sdp_db->raw_data);
2006 
2007                     p_msg->disc_result.result.disc_res.p_raw_data = osi_malloc(bta_dm_search_cb.p_sdp_db->raw_used);
2008                     if ( NULL != p_msg->disc_result.result.disc_res.p_raw_data  ) {
2009                         memcpy(     p_msg->disc_result.result.disc_res.p_raw_data,
2010                                     bta_dm_search_cb.p_sdp_db->raw_data,
2011                                     bta_dm_search_cb.p_sdp_db->raw_used );
2012 
2013                         p_msg->disc_result.result.disc_res.raw_data_size =
2014                             bta_dm_search_cb.p_sdp_db->raw_used;
2015 
2016                     } else {
2017                         APPL_TRACE_DEBUG("%s Alloc failed to allocate %d bytes !!", __func__,
2018                                          bta_dm_search_cb.p_sdp_db->raw_used);
2019                     }
2020 
2021                     bta_dm_search_cb.p_sdp_db->raw_data = NULL;     //no need to free this - it is a global assigned.
2022                     bta_dm_search_cb.p_sdp_db->raw_used = 0;
2023                     bta_dm_search_cb.p_sdp_db->raw_size = 0;
2024                 } else {
2025                     APPL_TRACE_DEBUG("%s raw data size is 0 or raw_data is null!!", __func__);
2026                 }
2027                 /* Done with p_sdp_db. Free it */
2028                 bta_dm_free_sdp_db(NULL);
2029                 p_msg->disc_result.result.disc_res.services = bta_dm_search_cb.services_found;
2030 
2031                 //Piggy back the SCN over result field
2032                 if ( scn_found ) {
2033                     p_msg->disc_result.result.disc_res.result = (3 + bta_dm_search_cb.peer_scn);
2034                     p_msg->disc_result.result.disc_res.services |= BTA_USER_SERVICE_MASK;
2035 
2036                     APPL_TRACE_EVENT(" Piggy back the SCN over result field  SCN=%d", bta_dm_search_cb.peer_scn);
2037 
2038                 }
2039                 bdcpy (p_msg->disc_result.result.disc_res.bd_addr, bta_dm_search_cb.peer_bdaddr);
2040                 BCM_STRNCPY_S((char *)p_msg->disc_result.result.disc_res.bd_name, bta_dm_get_remname(), (BD_NAME_LEN - 1));
2041 
2042                 /* make sure the string is null terminated */
2043                 p_msg->disc_result.result.disc_res.bd_name[BD_NAME_LEN - 1] = 0;
2044 
2045                 bta_sys_sendmsg(p_msg);
2046             }
2047         }
2048     } else {
2049         /* conn failed. No need for timer */
2050         if (p_data->sdp_event.sdp_result == SDP_CONN_FAILED || p_data->sdp_event.sdp_result == SDP_CONN_REJECTED
2051                 || p_data->sdp_event.sdp_result == SDP_SECURITY_ERR) {
2052             bta_dm_search_cb.wait_disc = FALSE;
2053         }
2054 
2055         /* not able to connect go to next device */
2056         osi_free(bta_dm_search_cb.p_sdp_db);
2057         bta_dm_search_cb.p_sdp_db = NULL;
2058 
2059         BTM_SecDeleteRmtNameNotifyCallback(&bta_dm_service_search_remname_cback);
2060 
2061         if ((p_msg = (tBTA_DM_MSG *) osi_malloc(sizeof(tBTA_DM_MSG))) != NULL) {
2062             p_msg->hdr.event = BTA_DM_DISCOVERY_RESULT_EVT;
2063             p_msg->disc_result.result.disc_res.result = BTA_FAILURE;
2064             p_msg->disc_result.result.disc_res.services = bta_dm_search_cb.services_found;
2065             bdcpy (p_msg->disc_result.result.disc_res.bd_addr, bta_dm_search_cb.peer_bdaddr);
2066             BCM_STRNCPY_S((char *)p_msg->disc_result.result.disc_res.bd_name,bta_dm_get_remname(), (BD_NAME_LEN - 1));
2067 
2068             /* make sure the string is null terminated */
2069             p_msg->disc_result.result.disc_res.bd_name[BD_NAME_LEN - 1] = 0;
2070 
2071             bta_sys_sendmsg(p_msg);
2072         }
2073     }
2074 }
2075 #endif  ///SDP_INCLUDE == TRUE
2076 
2077 /*******************************************************************************
2078 **
2079 ** Function         bta_dm_search_cmpl
2080 **
2081 ** Description      Sends event to application
2082 **
2083 ** Returns          void
2084 **
2085 *******************************************************************************/
bta_dm_search_cmpl(tBTA_DM_MSG * p_data)2086 void bta_dm_search_cmpl (tBTA_DM_MSG *p_data)
2087 {
2088     APPL_TRACE_EVENT("%s", __func__);
2089 
2090 #if (BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE && SDP_INCLUDED == TRUE)
2091     utl_freebuf((void **)&bta_dm_search_cb.p_srvc_uuid);
2092 #endif
2093 
2094     if (p_data->hdr.layer_specific == BTA_DM_API_DI_DISCOVER_EVT) {
2095  #if (SDP_INCLUDED == TRUE)
2096         bta_dm_di_disc_cmpl(p_data);
2097  #endif ///SDP_INCLUDED == TRUE
2098     } else {
2099         bta_dm_search_cb.p_search_cback(BTA_DM_DISC_CMPL_EVT, NULL);
2100     }
2101 }
2102 
2103 /*******************************************************************************
2104 **
2105 ** Function         bta_dm_disc_result
2106 **
2107 ** Description      Service discovery result when discovering services on a device
2108 **
2109 ** Returns          void
2110 **
2111 *******************************************************************************/
bta_dm_disc_result(tBTA_DM_MSG * p_data)2112 void bta_dm_disc_result (tBTA_DM_MSG *p_data)
2113 {
2114     APPL_TRACE_EVENT("%s", __func__);
2115 
2116 #if BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE
2117     /* if any BR/EDR service discovery has been done, report the event */
2118     if ((bta_dm_search_cb.services & ((BTA_ALL_SERVICE_MASK | BTA_USER_SERVICE_MASK ) & ~BTA_BLE_SERVICE_MASK))) {
2119         bta_dm_search_cb.p_search_cback(BTA_DM_DISC_RES_EVT, &p_data->disc_result.result);
2120     }
2121 #else
2122     bta_dm_search_cb.p_search_cback(BTA_DM_DISC_RES_EVT, &p_data->disc_result.result);
2123 #endif
2124     tBTA_DM_MSG *p_msg = (tBTA_DM_MSG *) osi_malloc(sizeof(tBTA_DM_MSG));
2125 
2126     /* send a message to change state */
2127     if (p_msg != NULL) {
2128         p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
2129         p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
2130         bta_sys_sendmsg(p_msg);
2131     }
2132 }
2133 
2134 /*******************************************************************************
2135 **
2136 ** Function         bta_dm_search_result
2137 **
2138 ** Description      Service discovery result while searching for devices
2139 **
2140 ** Returns          void
2141 **
2142 *******************************************************************************/
bta_dm_search_result(tBTA_DM_MSG * p_data)2143 void bta_dm_search_result (tBTA_DM_MSG *p_data)
2144 {
2145     APPL_TRACE_DEBUG("%s searching:0x%04x, result:0x%04x", __func__,
2146                      bta_dm_search_cb.services,
2147                      p_data->disc_result.result.disc_res.services);
2148 
2149     /* call back if application wants name discovery or found services that application is searching */
2150     if (( !bta_dm_search_cb.services )
2151             || (( bta_dm_search_cb.services ) && ( p_data->disc_result.result.disc_res.services ))) {
2152         bta_dm_search_cb.p_search_cback(BTA_DM_DISC_RES_EVT, &p_data->disc_result.result);
2153     }
2154 
2155     /* if searching did not initiate to create link */
2156     if (!bta_dm_search_cb.wait_disc ) {
2157         /* if service searching is done with EIR, don't search next device */
2158         if ( bta_dm_search_cb.p_btm_inq_info ) {
2159             bta_dm_discover_next_device();
2160         }
2161     } else {
2162         /* wait until link is disconnected or timeout */
2163         bta_dm_search_cb.sdp_results = TRUE;
2164         bta_dm_search_cb.search_timer.p_cback = (TIMER_CBACK *)&bta_dm_search_timer_cback;
2165         bta_sys_start_timer(&bta_dm_search_cb.search_timer, 0, 1000 * (L2CAP_LINK_INACTIVITY_TOUT + 1) );
2166     }
2167 
2168 }
2169 
2170 /*******************************************************************************
2171 **
2172 ** Function         bta_dm_search_timer_cback
2173 **
2174 ** Description      Called when ACL disconnect time is over
2175 **
2176 **
2177 ** Returns          void
2178 **
2179 *******************************************************************************/
bta_dm_search_timer_cback(TIMER_LIST_ENT * p_tle)2180 static void bta_dm_search_timer_cback (TIMER_LIST_ENT *p_tle)
2181 {
2182     UNUSED(p_tle);
2183 
2184     APPL_TRACE_EVENT("%s", __func__);
2185     bta_dm_search_cb.wait_disc = FALSE;
2186 
2187     /* proceed with next device */
2188     bta_dm_discover_next_device();
2189 
2190 }
2191 
2192 
2193 /*******************************************************************************
2194 **
2195 ** Function         bta_dm_free_sdp_db
2196 **
2197 ** Description      Frees SDP data base
2198 **
2199 ** Returns          void
2200 **
2201 *******************************************************************************/
2202 #if (SDP_INCLUDED == TRUE)
bta_dm_free_sdp_db(tBTA_DM_MSG * p_data)2203 void bta_dm_free_sdp_db (tBTA_DM_MSG *p_data)
2204 {
2205     UNUSED(p_data);
2206     if (bta_dm_search_cb.p_sdp_db) {
2207         osi_free(bta_dm_search_cb.p_sdp_db);
2208         bta_dm_search_cb.p_sdp_db = NULL;
2209     }
2210 }
2211 #endif  ///SDP_INCLUDED == TRUE
2212 
2213 /*******************************************************************************
2214 **
2215 ** Function         bta_dm_queue_search
2216 **
2217 ** Description      Queues search command while search is being cancelled
2218 **
2219 ** Returns          void
2220 **
2221 *******************************************************************************/
bta_dm_queue_search(tBTA_DM_MSG * p_data)2222 void bta_dm_queue_search (tBTA_DM_MSG *p_data)
2223 {
2224     if (bta_dm_search_cb.p_search_queue) {
2225         osi_free(bta_dm_search_cb.p_search_queue);
2226     }
2227 
2228     bta_dm_search_cb.p_search_queue = (tBTA_DM_MSG *)osi_malloc(sizeof(tBTA_DM_API_SEARCH));
2229     memcpy(bta_dm_search_cb.p_search_queue, p_data, sizeof(tBTA_DM_API_SEARCH));
2230 
2231 }
2232 
2233 /*******************************************************************************
2234 **
2235 ** Function         bta_dm_queue_disc
2236 **
2237 ** Description      Queues discovery command while search is being cancelled
2238 **
2239 ** Returns          void
2240 **
2241 *******************************************************************************/
2242 #if (SDP_INCLUDED == TRUE)
bta_dm_queue_disc(tBTA_DM_MSG * p_data)2243 void bta_dm_queue_disc (tBTA_DM_MSG *p_data)
2244 {
2245     if (bta_dm_search_cb.p_search_queue) {
2246         osi_free(bta_dm_search_cb.p_search_queue);
2247     }
2248 
2249     bta_dm_search_cb.p_search_queue = (tBTA_DM_MSG *)osi_malloc(sizeof(tBTA_DM_API_DISCOVER));
2250     memcpy(bta_dm_search_cb.p_search_queue, p_data, sizeof(tBTA_DM_API_DISCOVER));
2251 }
2252 #endif  ///SDP_INCLUDED == TRUE
2253 
2254 /*******************************************************************************
2255 **
2256 ** Function         bta_dm_search_clear_queue
2257 **
2258 ** Description      Clears the queue if API search cancel is called
2259 **
2260 ** Returns          void
2261 **
2262 *******************************************************************************/
bta_dm_search_clear_queue(tBTA_DM_MSG * p_data)2263 void bta_dm_search_clear_queue (tBTA_DM_MSG *p_data)
2264 {
2265     UNUSED(p_data);
2266     if (bta_dm_search_cb.p_search_queue) {
2267         osi_free(bta_dm_search_cb.p_search_queue);
2268         bta_dm_search_cb.p_search_queue = NULL;
2269     }
2270 }
2271 
2272 /*******************************************************************************
2273 **
2274 ** Function         bta_dm_search_cancel_cmpl
2275 **
2276 ** Description      Search cancel is complete
2277 **
2278 ** Returns          void
2279 **
2280 *******************************************************************************/
bta_dm_search_cancel_cmpl(tBTA_DM_MSG * p_data)2281 void bta_dm_search_cancel_cmpl (tBTA_DM_MSG *p_data)
2282 {
2283     UNUSED(p_data);
2284     if (bta_dm_search_cb.p_search_queue) {
2285         bta_sys_sendmsg(bta_dm_search_cb.p_search_queue);
2286         bta_dm_search_cb.p_search_queue = NULL;
2287     }
2288 
2289 }
2290 
2291 /*******************************************************************************
2292 **
2293 ** Function         bta_dm_search_cancel_transac_cmpl
2294 **
2295 ** Description      Current Service Discovery or remote name procedure is
2296 **                  completed after search cancellation
2297 **
2298 ** Returns          void
2299 **
2300 *******************************************************************************/
2301 #if (SDP_INCLUDED == TRUE)
bta_dm_search_cancel_transac_cmpl(tBTA_DM_MSG * p_data)2302 void bta_dm_search_cancel_transac_cmpl(tBTA_DM_MSG *p_data)
2303 {
2304     UNUSED(p_data);
2305     if (bta_dm_search_cb.p_sdp_db) {
2306         osi_free(bta_dm_search_cb.p_sdp_db);
2307         bta_dm_search_cb.p_sdp_db = NULL;
2308     }
2309 
2310     bta_dm_search_cancel_notify(NULL);
2311 }
2312 #endif  ///SDP_INCLUDED == TRUE
2313 
2314 
2315 /*******************************************************************************
2316 **
2317 ** Function         bta_dm_search_cancel_notify
2318 **
2319 ** Description      Notify application that search has been cancelled
2320 **
2321 ** Returns          void
2322 **
2323 *******************************************************************************/
bta_dm_search_cancel_notify(tBTA_DM_MSG * p_data)2324 void bta_dm_search_cancel_notify (tBTA_DM_MSG *p_data)
2325 {
2326     UNUSED(p_data);
2327     if (bta_dm_search_cb.p_search_cback) {
2328         bta_dm_search_cb.p_search_cback(BTA_DM_SEARCH_CANCEL_CMPL_EVT, NULL);
2329     }
2330     if (!bta_dm_search_cb.name_discover_done) {
2331         BTM_CancelRemoteDeviceName();
2332     }
2333 #if (BLE_INCLUDED == TRUE) && (BTA_GATT_INCLUDED == TRUE) && (SDP_INCLUDED == TRUE) && (GATTC_INCLUDED) == TRUE
2334     if (bta_dm_search_cb.gatt_disc_active) {
2335         bta_dm_cancel_gatt_discovery(bta_dm_search_cb.peer_bdaddr);
2336     }
2337 #endif
2338 
2339 }
2340 
2341 /*******************************************************************************
2342 **
2343 ** Function         bta_dm_find_services
2344 **
2345 ** Description      Starts discovery on a device
2346 **
2347 ** Returns          void
2348 **
2349 *******************************************************************************/
2350 #if (SDP_INCLUDED == TRUE)
bta_dm_find_services(BD_ADDR bd_addr)2351 static void bta_dm_find_services ( BD_ADDR bd_addr)
2352 {
2353     tSDP_UUID    uuid;
2354     tBTA_DM_MSG *p_msg;
2355 
2356     memset (&uuid, 0, sizeof(tSDP_UUID));
2357 
2358     while (bta_dm_search_cb.service_index < BTA_MAX_SERVICE_ID) {
2359         tBTA_SERVICE_MASK this_service_mask = (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(bta_dm_search_cb.service_index));
2360         if ( bta_dm_search_cb.services_to_search & this_service_mask) {
2361             if ((bta_dm_search_cb.p_sdp_db = (tSDP_DISCOVERY_DB *)osi_malloc(BTA_DM_SDP_DB_SIZE)) != NULL) {
2362                 APPL_TRACE_DEBUG("bta_dm_search_cb.services = %04x***********", bta_dm_search_cb.services);
2363                 /* try to search all services by search based on L2CAP UUID */
2364                 if (bta_dm_search_cb.services == BTA_ALL_SERVICE_MASK ) {
2365                     APPL_TRACE_DEBUG("%s services_to_search=%08x", __func__, bta_dm_search_cb.services_to_search);
2366                     if (bta_dm_search_cb.services_to_search & BTA_RES_SERVICE_MASK) {
2367                         uuid.uu.uuid16 = bta_service_id_to_uuid_lkup_tbl[0];
2368                         bta_dm_search_cb.services_to_search &= ~BTA_RES_SERVICE_MASK;
2369                     } else {
2370                         uuid.uu.uuid16 = UUID_PROTOCOL_L2CAP;
2371                         bta_dm_search_cb.services_to_search = 0;
2372                     }
2373                 } else {
2374 #if BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE
2375                     /* for LE only profile */
2376                     if (this_service_mask == BTA_BLE_SERVICE_MASK) {
2377                         if (bta_dm_search_cb.uuid_to_search > 0 && bta_dm_search_cb.p_srvc_uuid) {
2378                             memcpy(&uuid,
2379                                    (const void *)(bta_dm_search_cb.p_srvc_uuid + \
2380                                                   bta_dm_search_cb.num_uuid - bta_dm_search_cb.uuid_to_search),
2381                                    sizeof(tBT_UUID));
2382 
2383                             bta_dm_search_cb.uuid_to_search -- ;
2384                         } else {
2385                             uuid.uu.uuid16 = bta_service_id_to_uuid_lkup_tbl[bta_dm_search_cb.service_index];
2386                         }
2387 
2388                         /* last one? clear the BLE service bit if all discovery has been done */
2389                         if (bta_dm_search_cb.uuid_to_search == 0) {
2390                             bta_dm_search_cb.services_to_search &=
2391                                 (tBTA_SERVICE_MASK)(~(BTA_SERVICE_ID_TO_SERVICE_MASK(bta_dm_search_cb.service_index)));
2392                         }
2393 
2394                     } else
2395 #endif
2396                     {
2397                         /* remove the service from services to be searched  */
2398                         bta_dm_search_cb.services_to_search &=
2399                             (tBTA_SERVICE_MASK)(~(BTA_SERVICE_ID_TO_SERVICE_MASK(bta_dm_search_cb.service_index)));
2400                         uuid.uu.uuid16 = bta_service_id_to_uuid_lkup_tbl[bta_dm_search_cb.service_index];
2401                     }
2402                 }
2403 
2404                 if (uuid.len == 0) {
2405                     uuid.len = LEN_UUID_16;
2406                 }
2407 
2408                 if (this_service_mask == BTA_USER_SERVICE_MASK) {
2409                     memcpy(&uuid, &bta_dm_search_cb.uuid, sizeof(tSDP_UUID));
2410                 }
2411 
2412                 APPL_TRACE_DEBUG("%s search UUID = %04x", __func__, uuid.uu.uuid16);
2413                 SDP_InitDiscoveryDb (bta_dm_search_cb.p_sdp_db, BTA_DM_SDP_DB_SIZE, 1, &uuid, 0, NULL);
2414 
2415                 memset(g_disc_raw_data_buf, 0, MAX_DISC_RAW_DATA_BUF);
2416                 bta_dm_search_cb.p_sdp_db->raw_data = g_disc_raw_data_buf;
2417 
2418                 bta_dm_search_cb.p_sdp_db->raw_size = MAX_DISC_RAW_DATA_BUF;
2419 
2420                 if (!SDP_ServiceSearchAttributeRequest (bd_addr, bta_dm_search_cb.p_sdp_db, &bta_dm_sdp_callback)) {
2421                     /* if discovery not successful with this device
2422                     proceed to next one */
2423                     osi_free(bta_dm_search_cb.p_sdp_db);
2424                     bta_dm_search_cb.p_sdp_db = NULL;
2425                     bta_dm_search_cb.service_index = BTA_MAX_SERVICE_ID;
2426 
2427                 } else {
2428 #if BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE
2429                     if ((this_service_mask == BTA_BLE_SERVICE_MASK &&
2430                             bta_dm_search_cb.uuid_to_search == 0) ||
2431                             this_service_mask != BTA_BLE_SERVICE_MASK)
2432 #endif
2433                         bta_dm_search_cb.service_index++;
2434                     return;
2435                 }
2436             } else {
2437                 APPL_TRACE_ERROR("#### Failed to allocate SDP DB buffer! ####");
2438             }
2439         }
2440 
2441         bta_dm_search_cb.service_index++;
2442     }
2443 
2444     /* no more services to be discovered */
2445     if (bta_dm_search_cb.service_index >= BTA_MAX_SERVICE_ID) {
2446         if ((p_msg = (tBTA_DM_MSG *) osi_malloc(sizeof(tBTA_DM_MSG))) != NULL) {
2447             p_msg->hdr.event = BTA_DM_DISCOVERY_RESULT_EVT;
2448             p_msg->disc_result.result.disc_res.services = bta_dm_search_cb.services_found;
2449             bdcpy (p_msg->disc_result.result.disc_res.bd_addr, bta_dm_search_cb.peer_bdaddr);
2450             BCM_STRNCPY_S((char *)p_msg->disc_result.result.disc_res.bd_name,bta_dm_get_remname(), (BD_NAME_LEN - 1));
2451 
2452             /* make sure the string is terminated */
2453             p_msg->disc_result.result.disc_res.bd_name[BD_NAME_LEN - 1] = 0;
2454 
2455             bta_sys_sendmsg(p_msg);
2456         }
2457     }
2458 }
2459 #endif  ///SDP_INCLUDED == TRUE
2460 
2461 /*******************************************************************************
2462 **
2463 ** Function         bta_dm_discover_next_device
2464 **
2465 ** Description      Starts discovery on the next device in Inquiry data base
2466 **
2467 ** Returns          void
2468 **
2469 *******************************************************************************/
bta_dm_discover_next_device(void)2470 static void bta_dm_discover_next_device(void)
2471 {
2472 
2473     tBTA_DM_MSG *p_msg;
2474 
2475     APPL_TRACE_DEBUG("bta_dm_discover_next_device");
2476 
2477     /* searching next device on inquiry result */
2478     if ((bta_dm_search_cb.p_btm_inq_info = BTM_InqDbNext(bta_dm_search_cb.p_btm_inq_info)) != NULL) {
2479         bta_dm_search_cb.name_discover_done = FALSE;
2480         bta_dm_search_cb.peer_name[0]       = 0;
2481 #if (SDP_INCLUDED == TRUE)
2482         bta_dm_discover_device(bta_dm_search_cb.p_btm_inq_info->results.remote_bd_addr);
2483 #endif  ///SDP_INCLUDED == TRUE
2484     } else {
2485         /* no devices, search complete */
2486         bta_dm_search_cb.services = 0;
2487 
2488         if ((p_msg = (tBTA_DM_MSG *) osi_malloc(sizeof(tBTA_DM_MSG))) != NULL) {
2489             p_msg->hdr.event          = BTA_DM_SEARCH_CMPL_EVT;
2490             p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
2491             bta_sys_sendmsg(p_msg);
2492         }
2493     }
2494 }
2495 
2496 /*******************************************************************************
2497 **
2498 ** Function         bta_dm_discover_device
2499 **
2500 ** Description      Starts name and service discovery on the device
2501 **
2502 ** Returns          void
2503 **
2504 *******************************************************************************/
2505 #if (SDP_INCLUDED == TRUE)
bta_dm_discover_device(BD_ADDR remote_bd_addr)2506 static void bta_dm_discover_device(BD_ADDR remote_bd_addr)
2507 {
2508     tBTA_DM_MSG *p_msg;
2509     tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
2510 
2511 #if BLE_INCLUDED == TRUE
2512     if (bta_dm_search_cb.transport == BTA_TRANSPORT_UNKNOWN) {
2513         tBT_DEVICE_TYPE dev_type;
2514         tBLE_ADDR_TYPE  addr_type;
2515 
2516         BTM_ReadDevInfo(remote_bd_addr, &dev_type, &addr_type);
2517         if (dev_type == BT_DEVICE_TYPE_BLE || addr_type == BLE_ADDR_RANDOM) {
2518             transport = BT_TRANSPORT_LE;
2519         }
2520     } else {
2521         transport = bta_dm_search_cb.transport;
2522     }
2523 #endif
2524 
2525     /* Reset transport state for next discovery */
2526     bta_dm_search_cb.transport = BTA_TRANSPORT_UNKNOWN;
2527 
2528     APPL_TRACE_DEBUG("%s BDA:0x%02X%02X%02X%02X%02X%02X", __func__,
2529                      remote_bd_addr[0], remote_bd_addr[1],
2530                      remote_bd_addr[2], remote_bd_addr[3],
2531                      remote_bd_addr[4], remote_bd_addr[5]);
2532 
2533     bdcpy(bta_dm_search_cb.peer_bdaddr, remote_bd_addr);
2534 
2535     APPL_TRACE_DEBUG("%s name_discover_done = %d p_btm_inq_info %p state = %d, transport=%d",
2536                      __func__,
2537                      bta_dm_search_cb.name_discover_done,
2538                      bta_dm_search_cb.p_btm_inq_info,
2539                      bta_dm_search_cb.state,
2540                      transport);
2541 
2542     if (bta_dm_search_cb.p_btm_inq_info) {
2543         APPL_TRACE_DEBUG("%s appl_knows_rem_name %d", __func__,
2544                          bta_dm_search_cb.p_btm_inq_info->appl_knows_rem_name);
2545     }
2546 
2547     if ((bta_dm_search_cb.p_btm_inq_info)
2548             && (bta_dm_search_cb.p_btm_inq_info->results.device_type == BT_DEVICE_TYPE_BLE)
2549             && (bta_dm_search_cb.state == BTA_DM_SEARCH_ACTIVE)) {
2550         /* Do not perform RNR for LE devices at inquiry complete*/
2551         bta_dm_search_cb.name_discover_done = TRUE;
2552     }
2553 
2554     /* if name discovery is not done and application needs remote name */
2555     if ((!bta_dm_search_cb.name_discover_done)
2556             && (( bta_dm_search_cb.p_btm_inq_info == NULL )
2557                 || (bta_dm_search_cb.p_btm_inq_info && (!bta_dm_search_cb.p_btm_inq_info->appl_knows_rem_name)))) {
2558         if (bta_dm_read_remote_device_name(bta_dm_search_cb.peer_bdaddr, transport) == TRUE) {
2559             return;
2560         }
2561 
2562         /* starting name discovery failed */
2563         bta_dm_search_cb.name_discover_done = TRUE;
2564     }
2565 
2566     /* if application wants to discover service */
2567     if ( bta_dm_search_cb.services ) {
2568         /* initialize variables */
2569         bta_dm_search_cb.service_index      = 0;
2570         bta_dm_search_cb.services_found     = 0;
2571         bta_dm_search_cb.services_to_search = bta_dm_search_cb.services;
2572 #if BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE && SDP_INCLUDED == TRUE
2573         bta_dm_search_cb.uuid_to_search     = bta_dm_search_cb.num_uuid;
2574 #endif
2575         if ((bta_dm_search_cb.p_btm_inq_info != NULL) &&
2576                 bta_dm_search_cb.services != BTA_USER_SERVICE_MASK
2577                 && (bta_dm_search_cb.sdp_search == FALSE)) {
2578             /* check if EIR provides the information of supported services */
2579             bta_dm_eir_search_services( &bta_dm_search_cb.p_btm_inq_info->results,
2580                                         &bta_dm_search_cb.services_to_search,
2581                                         &bta_dm_search_cb.services_found );
2582         }
2583 
2584         /* if seaching with EIR is not completed */
2585         if (bta_dm_search_cb.services_to_search) {
2586             /* check whether connection already exists to the device
2587                if connection exists, we don't have to wait for ACL
2588                link to go down to start search on next device */
2589             if (BTM_IsAclConnectionUp(bta_dm_search_cb.peer_bdaddr, BT_TRANSPORT_BR_EDR)) {
2590                 bta_dm_search_cb.wait_disc = FALSE;
2591             } else {
2592                 bta_dm_search_cb.wait_disc = TRUE;
2593             }
2594 
2595 #if (BLE_INCLUDED == TRUE && (defined BTA_GATT_INCLUDED) && (BTA_GATT_INCLUDED == TRUE) && SDP_INCLUDED == TRUE) && (GATTC_INCLUDED == TRUE)
2596             if ( bta_dm_search_cb.p_btm_inq_info ) {
2597                 APPL_TRACE_DEBUG("%s p_btm_inq_info %p results.device_type 0x%x services_to_search 0x%x",
2598                                  __func__,
2599                                  bta_dm_search_cb.p_btm_inq_info,
2600                                  bta_dm_search_cb.p_btm_inq_info->results.device_type,
2601                                  bta_dm_search_cb.services_to_search);
2602             }
2603 
2604             if (transport == BT_TRANSPORT_LE) {
2605                 if (bta_dm_search_cb.services_to_search & BTA_BLE_SERVICE_MASK) {
2606                     //set the raw data buffer here
2607                     memset(g_disc_raw_data_buf, 0, MAX_DISC_RAW_DATA_BUF);
2608                     bta_dm_search_cb.p_ble_rawdata = g_disc_raw_data_buf;
2609 
2610                     bta_dm_search_cb.ble_raw_size = MAX_DISC_RAW_DATA_BUF;
2611                     bta_dm_search_cb.ble_raw_used = 0;
2612 
2613                     /* start GATT for service discovery */
2614                     btm_dm_start_gatt_discovery(bta_dm_search_cb.peer_bdaddr);
2615                     return;
2616                 }
2617             } else
2618 #endif
2619             {
2620                 bta_dm_search_cb.sdp_results = FALSE;
2621                 bta_dm_find_services(bta_dm_search_cb.peer_bdaddr);
2622                 return;
2623             }
2624         }
2625     }
2626 
2627     /* name discovery and service discovery are done for this device */
2628     if ((p_msg = (tBTA_DM_MSG *) osi_malloc(sizeof(tBTA_DM_MSG))) != NULL) {
2629         p_msg->hdr.event = BTA_DM_DISCOVERY_RESULT_EVT;
2630         /* initialize the data structure - includes p_raw_data and raw_data_size */
2631         memset(&(p_msg->disc_result.result), 0, sizeof(tBTA_DM_DISC_RES));
2632         p_msg->disc_result.result.disc_res.result = BTA_SUCCESS;
2633         p_msg->disc_result.result.disc_res.services = bta_dm_search_cb.services_found;
2634         bdcpy (p_msg->disc_result.result.disc_res.bd_addr, bta_dm_search_cb.peer_bdaddr);
2635         BCM_STRNCPY_S((char *)p_msg->disc_result.result.disc_res.bd_name,(char *)bta_dm_search_cb.peer_name, (BD_NAME_LEN - 1));
2636 
2637         /* make sure the string is terminated */
2638         p_msg->disc_result.result.disc_res.bd_name[BD_NAME_LEN - 1] = 0;
2639 
2640         bta_sys_sendmsg(p_msg);
2641     }
2642 }
2643 
2644 /*******************************************************************************
2645 **
2646 ** Function         bta_dm_sdp_callback
2647 **
2648 ** Description      Callback from sdp with discovery status
2649 **
2650 ** Returns          void
2651 **
2652 *******************************************************************************/
bta_dm_sdp_callback(UINT16 sdp_status)2653 static void bta_dm_sdp_callback (UINT16 sdp_status)
2654 {
2655 
2656     tBTA_DM_SDP_RESULT *p_msg;
2657 
2658     if ((p_msg = (tBTA_DM_SDP_RESULT *) osi_malloc(sizeof(tBTA_DM_SDP_RESULT))) != NULL) {
2659         p_msg->hdr.event = BTA_DM_SDP_RESULT_EVT;
2660         p_msg->sdp_result = sdp_status;
2661         bta_sys_sendmsg(p_msg);
2662 
2663     }
2664 }
2665 #endif  ///SDP_INCLUDED == TRUE
2666 /*******************************************************************************
2667 **
2668 ** Function         bta_dm_inq_results_cb
2669 **
2670 ** Description      Inquiry results callback from BTM
2671 **
2672 ** Returns          void
2673 **
2674 *******************************************************************************/
bta_dm_inq_results_cb(tBTM_INQ_RESULTS * p_inq,UINT8 * p_eir)2675 static void bta_dm_inq_results_cb (tBTM_INQ_RESULTS *p_inq, UINT8 *p_eir)
2676 {
2677 
2678     tBTA_DM_SEARCH     result;
2679     tBTM_INQ_INFO      *p_inq_info;
2680     UINT16             service_class;
2681 
2682     bdcpy(result.inq_res.bd_addr, p_inq->remote_bd_addr);
2683     memcpy(result.inq_res.dev_class, p_inq->dev_class, DEV_CLASS_LEN);
2684     BTM_COD_SERVICE_CLASS(service_class, p_inq->dev_class);
2685     result.inq_res.is_limited = (service_class & BTM_COD_SERVICE_LMTD_DISCOVER) ? TRUE : FALSE;
2686     result.inq_res.rssi = p_inq->rssi;
2687 
2688 #if (BLE_INCLUDED == TRUE)
2689     result.inq_res.ble_addr_type    = p_inq->ble_addr_type;
2690     result.inq_res.inq_result_type  = p_inq->inq_result_type;
2691     result.inq_res.device_type      = p_inq->device_type;
2692     result.inq_res.flag             = p_inq->flag;
2693 #endif
2694 
2695     /* application will parse EIR to find out remote device name */
2696     result.inq_res.p_eir = p_eir;
2697 
2698     if ((p_inq_info = BTM_InqDbRead(p_inq->remote_bd_addr)) != NULL) {
2699         /* initialize remt_name_not_required to FALSE so that we get the name by default */
2700         result.inq_res.remt_name_not_required = FALSE;
2701 
2702     }
2703 
2704     if (bta_dm_search_cb.p_search_cback) {
2705         bta_dm_search_cb.p_search_cback(BTA_DM_INQ_RES_EVT, &result);
2706     }
2707 
2708     if (p_inq_info) {
2709         /* application indicates if it knows the remote name, inside the callback
2710          copy that to the inquiry data base*/
2711         if (result.inq_res.remt_name_not_required) {
2712             p_inq_info->appl_knows_rem_name = TRUE;
2713         }
2714 
2715     }
2716 
2717 
2718 }
2719 
2720 
2721 /*******************************************************************************
2722 **
2723 ** Function         bta_dm_inq_cmpl_cb
2724 **
2725 ** Description      Inquiry complete callback from BTM
2726 **
2727 ** Returns          void
2728 **
2729 *******************************************************************************/
bta_dm_inq_cmpl_cb(void * p_result)2730 static void bta_dm_inq_cmpl_cb (void *p_result)
2731 {
2732     tBTA_DM_MSG *p_msg;
2733 
2734     if (bta_dm_search_cb.cancel_pending == FALSE) {
2735         APPL_TRACE_DEBUG("%s", __FUNCTION__);
2736         p_msg = (tBTA_DM_MSG *) osi_malloc(sizeof(tBTA_DM_MSG));
2737         if (p_msg != NULL) {
2738             p_msg->inq_cmpl.hdr.event = BTA_DM_INQUIRY_CMPL_EVT;
2739             p_msg->inq_cmpl.num = ((tBTM_INQUIRY_CMPL *)p_result)->num_resp;
2740             bta_sys_sendmsg(p_msg);
2741         }
2742     } else {
2743         bta_dm_search_cb.cancel_pending = FALSE;
2744         bta_dm_search_cancel_notify(NULL);
2745 
2746         p_msg = (tBTA_DM_MSG *) osi_malloc(sizeof(tBTA_DM_MSG));
2747         if (p_msg != NULL) {
2748             p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT;
2749             p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT;
2750             bta_sys_sendmsg(p_msg);
2751         }
2752     }
2753 }
2754 
2755 /*******************************************************************************
2756 **
2757 ** Function         bta_dm_service_search_remname_cback
2758 **
2759 ** Description      Remote name call back from BTM during service discovery
2760 **
2761 ** Returns          void
2762 **
2763 *******************************************************************************/
bta_dm_service_search_remname_cback(BD_ADDR bd_addr,DEV_CLASS dc,BD_NAME bd_name)2764 static void bta_dm_service_search_remname_cback (BD_ADDR bd_addr, DEV_CLASS dc, BD_NAME bd_name)
2765 {
2766     tBTM_REMOTE_DEV_NAME    rem_name;
2767     tBTM_STATUS             btm_status;
2768     UNUSED(dc);
2769 
2770     APPL_TRACE_DEBUG("bta_dm_service_search_remname_cback name=<%s>", bd_name);
2771 
2772     /* if this is what we are looking for */
2773     if (!bdcmp( bta_dm_search_cb.peer_bdaddr, bd_addr)) {
2774         rem_name.length = strlen((char *)bd_name);
2775         if (rem_name.length > (BD_NAME_LEN - 1)) {
2776             rem_name.length = (BD_NAME_LEN - 1);
2777             rem_name.remote_bd_name[(BD_NAME_LEN - 1)] = 0;
2778         }
2779         BCM_STRNCPY_S((char *)rem_name.remote_bd_name, (char *)bd_name, (BD_NAME_LEN - 1));
2780         rem_name.status = BTM_SUCCESS;
2781 
2782         bta_dm_remname_cback(&rem_name);
2783     } else {
2784         /* get name of device */
2785         btm_status = BTM_ReadRemoteDeviceName (bta_dm_search_cb.peer_bdaddr,
2786                                                (tBTM_CMPL_CB *) bta_dm_remname_cback,
2787                                                BT_TRANSPORT_BR_EDR);
2788         if ( btm_status == BTM_BUSY ) {
2789             /* wait for next chance(notification of remote name discovery done) */
2790             APPL_TRACE_DEBUG("bta_dm_service_search_remname_cback: BTM_ReadRemoteDeviceName is busy");
2791         } else if ( btm_status != BTM_CMD_STARTED ) {
2792             /* if failed to start getting remote name then continue */
2793             APPL_TRACE_WARNING("bta_dm_service_search_remname_cback: BTM_ReadRemoteDeviceName returns 0x%02X", btm_status);
2794 
2795             rem_name.length = 0;
2796             rem_name.remote_bd_name[0] = 0;
2797             rem_name.status = btm_status;
2798             bta_dm_remname_cback(&rem_name);
2799         }
2800     }
2801 }
2802 
2803 
2804 /*******************************************************************************
2805 **
2806 ** Function         bta_dm_remname_cback
2807 **
2808 ** Description      Remote name complete call back from BTM
2809 **
2810 ** Returns          void
2811 **
2812 *******************************************************************************/
bta_dm_remname_cback(tBTM_REMOTE_DEV_NAME * p_remote_name)2813 static void bta_dm_remname_cback (tBTM_REMOTE_DEV_NAME *p_remote_name)
2814 {
2815     tBTA_DM_REM_NAME *p_msg;
2816 
2817     APPL_TRACE_DEBUG("bta_dm_remname_cback len = %d name=<%s>", p_remote_name->length,
2818                      p_remote_name->remote_bd_name);
2819     /* remote name discovery is done but it could be failed */
2820     bta_dm_search_cb.name_discover_done = TRUE;
2821     BCM_STRNCPY_S((char *)bta_dm_search_cb.peer_name, (char *)p_remote_name->remote_bd_name, (BD_NAME_LEN));
2822     bta_dm_search_cb.peer_name[BD_NAME_LEN] = 0;
2823 
2824     BTM_SecDeleteRmtNameNotifyCallback(&bta_dm_service_search_remname_cback);
2825 
2826 #if BLE_INCLUDED == TRUE && GATTS_INCLUDED == TRUE
2827     if (bta_dm_search_cb.transport == BT_TRANSPORT_LE ) {
2828         GAP_BleReadPeerPrefConnParams (bta_dm_search_cb.peer_bdaddr);
2829     }
2830 #endif
2831 
2832     if ((p_msg = (tBTA_DM_REM_NAME *) osi_malloc(sizeof(tBTA_DM_REM_NAME))) != NULL) {
2833         bdcpy (p_msg->result.disc_res.bd_addr, bta_dm_search_cb.peer_bdaddr);
2834         BCM_STRNCPY_S((char *)p_msg->result.disc_res.bd_name, (char *)p_remote_name->remote_bd_name, (BD_NAME_LEN));
2835 
2836         /* make sure the string is null terminated */
2837         p_msg->result.disc_res.bd_name[BD_NAME_LEN] = 0;
2838 
2839         p_msg->hdr.event = BTA_DM_REMT_NAME_EVT;
2840         bta_sys_sendmsg(p_msg);
2841     }
2842 }
2843 
2844 /*******************************************************************************
2845 **
2846 ** Function         bta_dm_authorize_cback
2847 **
2848 ** Description      cback requesting authorization
2849 **
2850 ** Returns          void
2851 **
2852 *******************************************************************************/
2853 #if (SMP_INCLUDED == TRUE)
bta_dm_authorize_cback(BD_ADDR bd_addr,DEV_CLASS dev_class,BD_NAME bd_name,UINT8 * service_name,UINT8 service_id,BOOLEAN is_originator)2854 static UINT8 bta_dm_authorize_cback (BD_ADDR bd_addr, DEV_CLASS dev_class, BD_NAME bd_name,
2855                                      UINT8 *service_name, UINT8 service_id, BOOLEAN is_originator)
2856 {
2857     tBTA_DM_SEC sec_event;
2858     UINT8       index = 1;
2859     UNUSED(service_name);
2860     UNUSED(is_originator);
2861 
2862     bdcpy(sec_event.authorize.bd_addr, bd_addr);
2863     memcpy(sec_event.authorize.dev_class, dev_class, DEV_CLASS_LEN);
2864 
2865     BCM_STRNCPY_S((char *)sec_event.authorize.bd_name, (char *)bd_name, (BD_NAME_LEN - 1));
2866 
2867     /* make sure the string is null terminated */
2868     sec_event.authorize.bd_name[BD_NAME_LEN - 1] = 0;
2869 
2870 #if ( defined(BTA_JV_INCLUDED) && BTA_JV_INCLUDED == TRUE )
2871     sec_event.authorize.service = service_id;
2872 #endif
2873 
2874     while (index < BTA_MAX_SERVICE_ID) {
2875         /* get the BTA service id corresponding to BTM id */
2876         if (bta_service_id_to_btm_srv_id_lkup_tbl[index] == service_id) {
2877             sec_event.authorize.service = index;
2878             break;
2879         }
2880         index++;
2881     }
2882 
2883 
2884     /* if supported service callback otherwise not authorized */
2885     if (bta_dm_cb.p_sec_cback && (index < BTA_MAX_SERVICE_ID
2886 #if ( defined(BTA_JV_INCLUDED) && BTA_JV_INCLUDED == TRUE )
2887                                   /* pass through JV service ID */
2888                                   || (service_id >= BTA_FIRST_JV_SERVICE_ID && service_id <= BTA_LAST_JV_SERVICE_ID)
2889 #endif
2890                                  )) {
2891         bta_dm_cb.p_sec_cback(BTA_DM_AUTHORIZE_EVT, &sec_event);
2892         return BTM_CMD_STARTED;
2893     } else {
2894         return BTM_NOT_AUTHORIZED;
2895     }
2896 }
2897 
2898 
2899 #if (CLASSIC_BT_INCLUDED == TRUE)
2900 #if (BT_SSP_INCLUDED == TRUE)
2901 /*******************************************************************************
2902 **
2903 ** Function         bta_dm_pinname_cback
2904 **
2905 ** Description      Callback requesting pin_key
2906 **
2907 ** Returns          void
2908 **
2909 *******************************************************************************/
bta_dm_pinname_cback(void * p_data)2910  static void bta_dm_pinname_cback (void *p_data)
2911 {
2912     tBTM_REMOTE_DEV_NAME *p_result = (tBTM_REMOTE_DEV_NAME *)p_data;
2913     tBTA_DM_SEC           sec_event;
2914     UINT32                bytes_to_copy;
2915     tBTA_DM_SEC_EVT       event = bta_dm_cb.pin_evt;
2916 
2917     if (BTA_DM_SP_CFM_REQ_EVT == event) {
2918         /* Retrieved saved device class and bd_addr */
2919         bdcpy(sec_event.cfm_req.bd_addr, bta_dm_cb.pin_bd_addr);
2920         BTA_COPY_DEVICE_CLASS(sec_event.cfm_req.dev_class, bta_dm_cb.pin_dev_class);
2921 
2922         if (p_result && p_result->status == BTM_SUCCESS) {
2923             bytes_to_copy = (p_result->length < (BD_NAME_LEN - 1))
2924                             ? p_result->length : (BD_NAME_LEN - 1);
2925             memcpy(sec_event.cfm_req.bd_name, p_result->remote_bd_name, bytes_to_copy);
2926             sec_event.pin_req.bd_name[BD_NAME_LEN - 1] = 0;
2927         } else { /* No name found */
2928             sec_event.cfm_req.bd_name[0] = 0;
2929         }
2930 
2931         sec_event.key_notif.passkey    = bta_dm_cb.num_val; /* get PIN code numeric number */
2932 
2933         /* 1 additional event data fields for this event */
2934         sec_event.cfm_req.just_works = bta_dm_cb.just_works;
2935     } else {
2936         /* Retrieved saved device class and bd_addr */
2937         bdcpy(sec_event.pin_req.bd_addr, bta_dm_cb.pin_bd_addr);
2938         BTA_COPY_DEVICE_CLASS(sec_event.pin_req.dev_class, bta_dm_cb.pin_dev_class);
2939 
2940         if (p_result && p_result->status == BTM_SUCCESS) {
2941             bytes_to_copy = (p_result->length < (BD_NAME_LEN - 1))
2942                             ? p_result->length : (BD_NAME_LEN - 1);
2943             memcpy(sec_event.pin_req.bd_name, p_result->remote_bd_name, bytes_to_copy);
2944             sec_event.pin_req.bd_name[BD_NAME_LEN - 1] = 0;
2945         } else { /* No name found */
2946             sec_event.pin_req.bd_name[0] = 0;
2947         }
2948 
2949         event = bta_dm_cb.pin_evt;
2950         sec_event.key_notif.passkey    = bta_dm_cb.num_val; /* get PIN code numeric number */
2951     }
2952 
2953     if ( bta_dm_cb.p_sec_cback ) {
2954         bta_dm_cb.p_sec_cback(event, &sec_event);
2955     }
2956 }
2957 #endif /// BT_SSP_INCLUDED == TRUE
2958 
2959 /*******************************************************************************
2960 **
2961 ** Function         bta_dm_pin_cback
2962 **
2963 ** Description      Callback requesting pin_key
2964 **
2965 ** Returns          void
2966 **
2967 *******************************************************************************/
bta_dm_pin_cback(BD_ADDR bd_addr,DEV_CLASS dev_class,BD_NAME bd_name,BOOLEAN min_16_digit)2968 static UINT8 bta_dm_pin_cback (BD_ADDR bd_addr, DEV_CLASS dev_class, BD_NAME bd_name,
2969                                BOOLEAN min_16_digit)
2970 {
2971     tBTA_DM_SEC sec_event;
2972 
2973     if (!bta_dm_cb.p_sec_cback) {
2974         return BTM_NOT_AUTHORIZED;
2975     }
2976 
2977     bdcpy(sec_event.pin_req.bd_addr, bd_addr);
2978     BTA_COPY_DEVICE_CLASS(sec_event.pin_req.dev_class, dev_class);
2979     BCM_STRNCPY_S((char *)sec_event.pin_req.bd_name, (char *)bd_name, (BD_NAME_LEN - 1));
2980     sec_event.pin_req.bd_name[BD_NAME_LEN - 1] = 0;
2981     sec_event.pin_req.min_16_digit = min_16_digit;
2982 
2983     bta_dm_cb.p_sec_cback(BTA_DM_PIN_REQ_EVT, &sec_event);
2984     return BTM_CMD_STARTED;
2985 }
2986 #endif ///CLASSIC_BT_INCLUDED == TRUE
2987 
2988 /*******************************************************************************
2989 **
2990 ** Function         bta_dm_new_link_key_cback
2991 **
2992 ** Description      Callback from BTM to notify new link key
2993 **
2994 ** Returns          void
2995 **
2996 *******************************************************************************/
bta_dm_new_link_key_cback(BD_ADDR bd_addr,DEV_CLASS dev_class,BD_NAME bd_name,LINK_KEY key,UINT8 key_type,BOOLEAN sc_support)2997 static UINT8  bta_dm_new_link_key_cback(BD_ADDR bd_addr, DEV_CLASS dev_class,
2998                                         BD_NAME bd_name, LINK_KEY key, UINT8 key_type,
2999                                         BOOLEAN sc_support)
3000 {
3001     tBTA_DM_SEC sec_event;
3002     tBTA_DM_AUTH_CMPL *p_auth_cmpl;
3003     UINT8             event;
3004     UNUSED(dev_class);
3005 
3006     memset (&sec_event, 0, sizeof(tBTA_DM_SEC));
3007 
3008     /* Not AMP Key type */
3009     if (key_type != HCI_LKEY_TYPE_AMP_WIFI && key_type != HCI_LKEY_TYPE_AMP_UWB) {
3010         event = BTA_DM_AUTH_CMPL_EVT;
3011         p_auth_cmpl = &sec_event.auth_cmpl;
3012 
3013         bdcpy(p_auth_cmpl->bd_addr, bd_addr);
3014 
3015         memcpy(p_auth_cmpl->bd_name, bd_name, (BD_NAME_LEN - 1));
3016         p_auth_cmpl->bd_name[BD_NAME_LEN - 1] = 0;
3017 
3018         p_auth_cmpl->key_present = TRUE;
3019         p_auth_cmpl->key_type = key_type;
3020         p_auth_cmpl->success = TRUE;
3021         p_auth_cmpl->sc_support = sc_support;
3022 
3023         memcpy(p_auth_cmpl->key, key, LINK_KEY_LEN);
3024         sec_event.auth_cmpl.fail_reason = HCI_SUCCESS;
3025 
3026 #if BLE_INCLUDED == TRUE
3027         // Report the BR link key based on the BR/EDR address and type
3028         BTM_ReadDevInfo(bd_addr, &sec_event.auth_cmpl.dev_type, &sec_event.auth_cmpl.addr_type);
3029 #endif
3030         if (bta_dm_cb.p_sec_cback) {
3031             bta_dm_cb.p_sec_cback(event, &sec_event);
3032         }
3033     } else {
3034         APPL_TRACE_WARNING("%s() Received AMP Key", __func__);
3035     }
3036 
3037     return BTM_CMD_STARTED;
3038 }
3039 
3040 
3041 /*******************************************************************************
3042 **
3043 ** Function         bta_dm_authentication_complete_cback
3044 **
3045 ** Description      Authentication complete callback from BTM
3046 **
3047 ** Returns          void
3048 **
3049 *******************************************************************************/
bta_dm_authentication_complete_cback(BD_ADDR bd_addr,DEV_CLASS dev_class,BD_NAME bd_name,int result)3050 static UINT8 bta_dm_authentication_complete_cback(BD_ADDR bd_addr, DEV_CLASS dev_class, BD_NAME bd_name, int result)
3051 {
3052     tBTA_DM_SEC sec_event;
3053     UNUSED(dev_class);
3054 
3055     if (result != BTM_SUCCESS) {
3056         memset(&sec_event, 0, sizeof(tBTA_DM_SEC));
3057         bdcpy(sec_event.auth_cmpl.bd_addr, bd_addr);
3058 
3059         memcpy(sec_event.auth_cmpl.bd_name, bd_name, (BD_NAME_LEN - 1));
3060         sec_event.auth_cmpl.bd_name[BD_NAME_LEN - 1] = 0;
3061 
3062 #if BLE_INCLUDED == TRUE
3063         // Report the BR link key based on the BR/EDR address and type
3064         BTM_ReadDevInfo(bd_addr, &sec_event.auth_cmpl.dev_type, &sec_event.auth_cmpl.addr_type);
3065 #endif
3066         sec_event.auth_cmpl.fail_reason = (UINT8)result;
3067 
3068         if (bta_dm_cb.p_sec_cback) {
3069             bta_dm_cb.p_sec_cback(BTA_DM_AUTH_CMPL_EVT, &sec_event);
3070         }
3071 
3072         bta_dm_remove_sec_dev_entry(bd_addr);
3073     }
3074 
3075     return BTM_SUCCESS;
3076 }
3077 
3078 #if (BT_SSP_INCLUDED == TRUE)
3079 /*******************************************************************************
3080 **
3081 ** Function         bta_dm_sp_cback
3082 **
3083 ** Description      simple pairing callback from BTM
3084 **
3085 ** Returns          void
3086 **
3087 *******************************************************************************/
bta_dm_sp_cback(tBTM_SP_EVT event,tBTM_SP_EVT_DATA * p_data)3088 static UINT8 bta_dm_sp_cback (tBTM_SP_EVT event, tBTM_SP_EVT_DATA *p_data)
3089 {
3090     tBTM_STATUS status = BTM_CMD_STARTED;
3091     tBTA_DM_SEC sec_event;
3092     tBTA_DM_SEC_EVT pin_evt = BTA_DM_SP_KEY_NOTIF_EVT;
3093 
3094     APPL_TRACE_EVENT("bta_dm_sp_cback: %d", event);
3095     if (!bta_dm_cb.p_sec_cback) {
3096         return BTM_NOT_AUTHORIZED;
3097     }
3098 
3099     /* TODO_SP */
3100     switch (event) {
3101     case BTM_SP_IO_REQ_EVT:
3102 #if (BT_SSP_INCLUDED == TRUE)
3103         /* translate auth_req */
3104         bta_dm_co_io_req(p_data->io_req.bd_addr, &p_data->io_req.io_cap,
3105                          &p_data->io_req.oob_data, &p_data->io_req.auth_req, p_data->io_req.is_orig);
3106 #endif
3107 #if BTM_OOB_INCLUDED == FALSE
3108         status = BTM_SUCCESS;
3109 #endif
3110 
3111         APPL_TRACE_EVENT("io mitm: %d oob_data:%d", p_data->io_req.auth_req, p_data->io_req.oob_data);
3112         break;
3113     case BTM_SP_IO_RSP_EVT:
3114 #if (BT_SSP_INCLUDED == TRUE)
3115         bta_dm_co_io_rsp(p_data->io_rsp.bd_addr, p_data->io_rsp.io_cap,
3116                          p_data->io_rsp.oob_data, p_data->io_rsp.auth_req );
3117 #endif
3118         break;
3119 
3120     case BTM_SP_CFM_REQ_EVT:
3121         pin_evt = BTA_DM_SP_CFM_REQ_EVT;
3122         bta_dm_cb.just_works = sec_event.cfm_req.just_works = p_data->cfm_req.just_works;
3123         sec_event.cfm_req.loc_auth_req = p_data->cfm_req.loc_auth_req;
3124         sec_event.cfm_req.rmt_auth_req = p_data->cfm_req.rmt_auth_req;
3125         sec_event.cfm_req.loc_io_caps = p_data->cfm_req.loc_io_caps;
3126         sec_event.cfm_req.rmt_io_caps = p_data->cfm_req.rmt_io_caps;
3127 
3128         /* continue to next case */
3129 #if (BT_SSP_INCLUDED == TRUE)
3130     /* Passkey entry mode, mobile device with output capability is very
3131         unlikely to receive key request, so skip this event */
3132     case BTM_SP_KEY_REQ_EVT:
3133     case BTM_SP_KEY_NOTIF_EVT:
3134 #endif
3135         if (BTM_SP_CFM_REQ_EVT == event) {
3136             /* Due to the switch case falling through below to BTM_SP_KEY_NOTIF_EVT,
3137                call remote name request using values from cfm_req */
3138             if (p_data->cfm_req.bd_name[0] == 0) {
3139                 bta_dm_cb.pin_evt = pin_evt;
3140                 bdcpy(bta_dm_cb.pin_bd_addr, p_data->cfm_req.bd_addr);
3141                 BTA_COPY_DEVICE_CLASS(bta_dm_cb.pin_dev_class, p_data->cfm_req.dev_class);
3142                 if ((BTM_ReadRemoteDeviceName(p_data->cfm_req.bd_addr, bta_dm_pinname_cback,
3143                                               BT_TRANSPORT_BR_EDR)) == BTM_CMD_STARTED) {
3144                     return BTM_CMD_STARTED;
3145                 }
3146                 APPL_TRACE_WARNING(" bta_dm_sp_cback() -> Failed to start Remote Name Request  ");
3147             } else {
3148                 /* Due to the switch case falling through below to BTM_SP_KEY_NOTIF_EVT,
3149                    copy these values into key_notif from cfm_req */
3150                 bdcpy(sec_event.key_notif.bd_addr, p_data->cfm_req.bd_addr);
3151                 BTA_COPY_DEVICE_CLASS(sec_event.key_notif.dev_class, p_data->cfm_req.dev_class);
3152                 BCM_STRNCPY_S((char *)sec_event.key_notif.bd_name, (char *)p_data->cfm_req.bd_name, (BD_NAME_LEN - 1));
3153                 sec_event.key_notif.bd_name[BD_NAME_LEN - 1] = 0;
3154             }
3155         }
3156 
3157         bta_dm_cb.num_val = sec_event.key_notif.passkey = p_data->key_notif.passkey;
3158         if (BTM_SP_KEY_NOTIF_EVT == event) {
3159             /* If the device name is not known, save bdaddr and devclass
3160                and initiate a name request with values from key_notif */
3161             if (p_data->key_notif.bd_name[0] == 0) {
3162                 bta_dm_cb.pin_evt = pin_evt;
3163                 bdcpy(bta_dm_cb.pin_bd_addr, p_data->key_notif.bd_addr);
3164                 BTA_COPY_DEVICE_CLASS(bta_dm_cb.pin_dev_class, p_data->key_notif.dev_class);
3165                 if ((BTM_ReadRemoteDeviceName(p_data->key_notif.bd_addr, bta_dm_pinname_cback,
3166                                               BT_TRANSPORT_BR_EDR)) == BTM_CMD_STARTED) {
3167                     return BTM_CMD_STARTED;
3168                 }
3169                 APPL_TRACE_WARNING(" bta_dm_sp_cback() -> Failed to start Remote Name Request  ");
3170             } else {
3171                 bdcpy(sec_event.key_notif.bd_addr, p_data->key_notif.bd_addr);
3172                 BTA_COPY_DEVICE_CLASS(sec_event.key_notif.dev_class, p_data->key_notif.dev_class);
3173                 BCM_STRNCPY_S((char *)sec_event.key_notif.bd_name, (char *)p_data->key_notif.bd_name, (BD_NAME_LEN - 1));
3174                 sec_event.key_notif.bd_name[BD_NAME_LEN - 1] = 0;
3175             }
3176         }
3177 
3178         if (BTM_SP_KEY_REQ_EVT == event) {
3179             pin_evt = BTA_DM_SP_KEY_REQ_EVT;
3180             /* If the device name is not known, save bdaddr and devclass
3181                and initiate a name request with values from key_notif */
3182             if (p_data->key_notif.bd_name[0] == 0) {
3183                 bta_dm_cb.pin_evt = pin_evt;
3184                 bdcpy(bta_dm_cb.pin_bd_addr, p_data->key_notif.bd_addr);
3185                 BTA_COPY_DEVICE_CLASS(bta_dm_cb.pin_dev_class, p_data->key_notif.dev_class);
3186                 if ((BTM_ReadRemoteDeviceName(p_data->key_notif.bd_addr, bta_dm_pinname_cback,
3187                                               BT_TRANSPORT_BR_EDR)) == BTM_CMD_STARTED) {
3188                     return BTM_CMD_STARTED;
3189                 }
3190                 APPL_TRACE_WARNING(" bta_dm_sp_cback() -> Failed to start Remote Name Request  ");
3191             } else {
3192                 bdcpy(sec_event.key_notif.bd_addr, p_data->key_notif.bd_addr);
3193                 BTA_COPY_DEVICE_CLASS(sec_event.key_notif.dev_class, p_data->key_notif.dev_class);
3194                 BCM_STRNCPY_S((char *)sec_event.key_notif.bd_name,(char *)p_data->key_notif.bd_name, (BD_NAME_LEN - 1));
3195                 sec_event.key_notif.bd_name[BD_NAME_LEN - 1] = 0;
3196             }
3197         }
3198         bta_dm_cb.p_sec_cback(pin_evt, &sec_event);
3199 
3200         break;
3201 
3202 #if BTM_OOB_INCLUDED == TRUE
3203     case BTM_SP_LOC_OOB_EVT:
3204         bta_dm_co_loc_oob((BOOLEAN)(p_data->loc_oob.status == BTM_SUCCESS),
3205                           p_data->loc_oob.c, p_data->loc_oob.r);
3206         break;
3207 
3208     case BTM_SP_RMT_OOB_EVT:
3209         /* If the device name is not known, save bdaddr and devclass and initiate a name request */
3210         if (p_data->rmt_oob.bd_name[0] == 0) {
3211             bta_dm_cb.pin_evt = BTA_DM_SP_RMT_OOB_EVT;
3212             bdcpy(bta_dm_cb.pin_bd_addr, p_data->rmt_oob.bd_addr);
3213             BTA_COPY_DEVICE_CLASS(bta_dm_cb.pin_dev_class, p_data->rmt_oob.dev_class);
3214             if ((BTM_ReadRemoteDeviceName(p_data->rmt_oob.bd_addr, bta_dm_pinname_cback,
3215                                           BT_TRANSPORT_BR_EDR)) == BTM_CMD_STARTED) {
3216                 return BTM_CMD_STARTED;
3217             }
3218             APPL_TRACE_WARNING(" bta_dm_sp_cback() -> Failed to start Remote Name Request  ");
3219         }
3220 
3221         bdcpy(sec_event.rmt_oob.bd_addr, p_data->rmt_oob.bd_addr);
3222         BTA_COPY_DEVICE_CLASS(sec_event.rmt_oob.dev_class, p_data->rmt_oob.dev_class);
3223         BCM_STRNCPY_S((char *)sec_event.rmt_oob.bd_name, (char *)p_data->rmt_oob.bd_name, (BD_NAME_LEN - 1));
3224         sec_event.rmt_oob.bd_name[BD_NAME_LEN - 1] = 0;
3225 
3226         bta_dm_cb.p_sec_cback(BTA_DM_SP_RMT_OOB_EVT, &sec_event);
3227 
3228         bta_dm_co_rmt_oob(p_data->rmt_oob.bd_addr);
3229         break;
3230 #endif
3231     case BTM_SP_COMPLT_EVT:
3232         /* do not report this event - handled by link_key_callback or auth_complete_callback */
3233         break;
3234 
3235     case BTM_SP_KEYPRESS_EVT:
3236         memcpy(&sec_event.key_press, &p_data->key_press, sizeof(tBTM_SP_KEYPRESS));
3237         bta_dm_cb.p_sec_cback(BTA_DM_SP_KEYPRESS_EVT, &sec_event);
3238         break;
3239 
3240     case BTM_SP_UPGRADE_EVT:
3241         bta_dm_co_lk_upgrade(p_data->upgrade.bd_addr, &p_data->upgrade.upgrade );
3242         break;
3243 
3244     default:
3245         status = BTM_NOT_AUTHORIZED;
3246         break;
3247     }
3248     APPL_TRACE_EVENT("dm status: %d", status);
3249     return status;
3250 }
3251 #endif /* (BT_SSP_INCLUDED == TRUE) */
3252 
3253 #endif  ///SMP_INCLUDED == TRUE
3254 
3255 
3256 /*******************************************************************************
3257 **
3258 ** Function         bta_dm_local_name_cback
3259 **
3260 ** Description      Callback from btm after local name is read
3261 **
3262 **
3263 ** Returns          void
3264 **
3265 *******************************************************************************/
bta_dm_local_name_cback(UINT8 * p_name)3266 static void bta_dm_local_name_cback(UINT8 *p_name)
3267 {
3268     tBTA_DM_SEC sec_event;
3269     UNUSED(p_name);
3270 
3271     sec_event.enable.status = BTA_SUCCESS;
3272 
3273     if (bta_dm_cb.p_sec_cback) {
3274         bta_dm_cb.p_sec_cback(BTA_DM_ENABLE_EVT, &sec_event);
3275     }
3276 
3277 }
3278 
3279 /*******************************************************************************
3280 **
3281 ** Function         bta_dm_bl_change_cback
3282 **
3283 ** Description      Callback from btm when acl connection goes up or down
3284 **
3285 **
3286 ** Returns          void
3287 **
3288 *******************************************************************************/
bta_dm_bl_change_cback(tBTM_BL_EVENT_DATA * p_data)3289 static void bta_dm_bl_change_cback (tBTM_BL_EVENT_DATA *p_data)
3290 {
3291     tBTA_DM_ACL_CHANGE *p_msg;
3292 
3293     if ((p_msg = (tBTA_DM_ACL_CHANGE *) osi_malloc(sizeof(tBTA_DM_ACL_CHANGE))) != NULL) {
3294         p_msg->event = p_data->event;
3295         p_msg->is_new = FALSE;
3296 
3297         switch (p_msg->event) {
3298         case BTM_BL_CONN_EVT:
3299             p_msg->sc_downgrade = p_data->conn.sc_downgrade;
3300             p_msg->is_new = TRUE;
3301             bdcpy(p_msg->bd_addr, p_data->conn.p_bda);
3302 #if BLE_INCLUDED == TRUE
3303             p_msg->transport = p_data->conn.transport;
3304             p_msg->handle = p_data->conn.handle;
3305 #endif
3306             break;
3307         case BTM_BL_DISCN_EVT:
3308             bdcpy(p_msg->bd_addr, p_data->discn.p_bda);
3309 #if BLE_INCLUDED == TRUE
3310             p_msg->transport = p_data->discn.transport;
3311             p_msg->handle = p_data->discn.handle;
3312 #endif
3313             break;
3314         case BTM_BL_UPDATE_EVT:
3315             p_msg->busy_level = p_data->update.busy_level;
3316             p_msg->busy_level_flags = p_data->update.busy_level_flags;
3317             break;
3318         case BTM_BL_ROLE_CHG_EVT:
3319             p_msg->new_role = p_data->role_chg.new_role;
3320             p_msg->hci_status = p_data->role_chg.hci_status;
3321             bdcpy(p_msg->bd_addr, p_data->role_chg.p_bda);
3322             break;
3323         case BTM_BL_COLLISION_EVT:
3324             bdcpy(p_msg->bd_addr, p_data->conn.p_bda);
3325             break;
3326         }
3327 
3328         p_msg->hdr.event = BTA_DM_ACL_CHANGE_EVT;
3329         bta_sys_sendmsg(p_msg);
3330 
3331     }
3332 
3333 }
3334 
3335 /*******************************************************************************
3336 **
3337 ** Function         bta_dm_rs_cback
3338 **
3339 ** Description      Receives the role switch complete event
3340 **
3341 ** Returns
3342 **
3343 *******************************************************************************/
bta_dm_rs_cback(tBTM_ROLE_SWITCH_CMPL * p1)3344 static void bta_dm_rs_cback (tBTM_ROLE_SWITCH_CMPL *p1)
3345 {
3346     UNUSED(p1);
3347     APPL_TRACE_WARNING("bta_dm_rs_cback:%d", bta_dm_cb.rs_event);
3348     if (bta_dm_cb.rs_event == BTA_DM_API_SEARCH_EVT) {
3349         bta_dm_cb.search_msg.rs_res = BTA_DM_RS_OK; /* do not care about the result for now */
3350         bta_dm_cb.rs_event = 0;
3351         bta_dm_search_start((tBTA_DM_MSG *)&bta_dm_cb.search_msg);
3352     }
3353 }
3354 
3355 /*******************************************************************************
3356 **
3357 ** Function         bta_dm_check_av
3358 **
3359 ** Description      This function checks if AV is active
3360 **                  if yes, make sure the AV link is master
3361 **
3362 ** Returns          BOOLEAN - TRUE, if switch is in progress
3363 **
3364 *******************************************************************************/
bta_dm_check_av(UINT16 event)3365 static BOOLEAN bta_dm_check_av(UINT16 event)
3366 {
3367     BOOLEAN avoid_roleswitch = FALSE;
3368     BOOLEAN switching = FALSE;
3369     UINT8 i;
3370     tBTA_DM_PEER_DEVICE *p_dev;
3371 
3372 #if defined(BTA_DM_AVOID_A2DP_ROLESWITCH_ON_INQUIRY) && (BTA_DM_AVOID_A2DP_ROLESWITCH_ON_INQUIRY == TRUE)
3373 
3374     /* avoid role switch upon inquiry if a2dp is actively streaming as it
3375        introduces an audioglitch due to FW scheduling delays (unavoidable) */
3376     if (event == BTA_DM_API_SEARCH_EVT) {
3377         avoid_roleswitch = TRUE;
3378     }
3379 #endif
3380 
3381     APPL_TRACE_EVENT("bta_dm_check_av:%d", bta_dm_cb.cur_av_count);
3382     if (bta_dm_cb.cur_av_count) {
3383         for (i = 0; i < bta_dm_cb.device_list.count; i++) {
3384             p_dev = &bta_dm_cb.device_list.peer_device[i];
3385             APPL_TRACE_WARNING("[%d]: state:%d, info:x%x, avoid_rs %d",
3386                                i, p_dev->conn_state, p_dev->info, avoid_roleswitch);
3387             if ((p_dev->conn_state == BTA_DM_CONNECTED) && (p_dev->info & BTA_DM_DI_AV_ACTIVE) &&
3388                     (avoid_roleswitch == FALSE)) {
3389                 /* make master and take away the role switch policy */
3390                 if (BTM_CMD_STARTED == BTM_SwitchRole (p_dev->peer_bdaddr, HCI_ROLE_MASTER, (tBTM_CMPL_CB *)bta_dm_rs_cback)) {
3391                     /* the role switch command is actually sent */
3392                     bta_dm_cb.rs_event = event;
3393                     switching = TRUE;
3394                 }
3395                 /* else either already master or can not switch for some reasons */
3396                 bta_dm_policy_cback(BTA_SYS_PLCY_CLR, 0, HCI_ENABLE_MASTER_SLAVE_SWITCH, p_dev->peer_bdaddr);
3397                 break;
3398             }
3399         }
3400     }
3401     return switching;
3402 }
3403 
3404 /*******************************************************************************
3405 **
3406 ** Function         bta_dm_acl_change
3407 **
3408 ** Description      Process BTA_DM_ACL_CHANGE_EVT
3409 **
3410 **
3411 ** Returns          void
3412 **
3413 *******************************************************************************/
bta_dm_acl_change(tBTA_DM_MSG * p_data)3414 void bta_dm_acl_change(tBTA_DM_MSG *p_data)
3415 {
3416 
3417     UINT8 i;
3418     UINT8 *p;
3419     tBTA_DM_SEC conn;
3420     BOOLEAN is_new = p_data->acl_change.is_new;
3421     BD_ADDR_PTR     p_bda = p_data->acl_change.bd_addr;
3422     BOOLEAN         need_policy_change = FALSE;
3423     BOOLEAN         issue_unpair_cb = FALSE;
3424 
3425     tBTA_DM_PEER_DEVICE *p_dev;
3426     memset(&conn, 0, sizeof(tBTA_DM_SEC));
3427 
3428     switch (p_data->acl_change.event) {
3429     case BTM_BL_UPDATE_EVT:     /* busy level update */
3430         if ( bta_dm_cb.p_sec_cback ) {
3431             conn.busy_level.level = p_data->acl_change.busy_level;
3432             conn.busy_level.level_flags = p_data->acl_change.busy_level_flags;
3433             bta_dm_cb.p_sec_cback(BTA_DM_BUSY_LEVEL_EVT, &conn);
3434         }
3435         return;
3436 
3437     case BTM_BL_ROLE_CHG_EVT:   /* role change event */
3438         p_dev = bta_dm_find_peer_device(p_bda);
3439         if (p_dev) {
3440             APPL_TRACE_DEBUG("bta_dm_acl_change role chg info:x%x new_role:%d dev count:%d",
3441                              p_dev->info, p_data->acl_change.new_role, bta_dm_cb.device_list.count);
3442             if (p_dev->info & BTA_DM_DI_AV_ACTIVE) {
3443                 /* there's AV activity on this link */
3444                 if (p_data->acl_change.new_role == HCI_ROLE_SLAVE && bta_dm_cb.device_list.count > 1
3445                         && p_data->acl_change.hci_status == HCI_SUCCESS) {
3446                     /* more than one connections and the AV connection is role switched to slave
3447                      * switch it back to master and remove the switch policy */
3448                     BTM_SwitchRole(p_bda, BTM_ROLE_MASTER, NULL);
3449                     need_policy_change = TRUE;
3450                 } else if (p_bta_dm_cfg->avoid_scatter && (p_data->acl_change.new_role == HCI_ROLE_MASTER)) {
3451                     /* if the link updated to be master include AV activities, remove the switch policy */
3452                     need_policy_change = TRUE;
3453                 }
3454 
3455                 if (need_policy_change) {
3456                     bta_dm_policy_cback(BTA_SYS_PLCY_CLR, 0, HCI_ENABLE_MASTER_SLAVE_SWITCH, p_dev->peer_bdaddr);
3457                 }
3458             } else {
3459                 /* there's AV no activity on this link and role switch happened
3460                  * check if AV is active
3461                  * if so, make sure the AV link is master */
3462                 bta_dm_check_av(0);
3463             }
3464             bta_sys_notify_role_chg(p_data->acl_change.bd_addr, p_data->acl_change.new_role, p_data->acl_change.hci_status);
3465             bdcpy(conn.role_chg.bd_addr, p_bda);
3466             conn.role_chg.new_role = (UINT8) p_data->acl_change.new_role;
3467             if ( bta_dm_cb.p_sec_cback ) {
3468                 bta_dm_cb.p_sec_cback(BTA_DM_ROLE_CHG_EVT, (tBTA_DM_SEC *)&conn);
3469             }
3470         }
3471         return;
3472     }
3473 
3474     /* Collision report from Stack: Notify profiles */
3475     if (p_data->acl_change.event == BTM_BL_COLLISION_EVT) {
3476         bta_sys_notify_collision (p_bda);
3477         return;
3478     }
3479 
3480     if (is_new) {
3481         for (i = 0; i < bta_dm_cb.device_list.count; i++) {
3482             if (!bdcmp( bta_dm_cb.device_list.peer_device[i].peer_bdaddr, p_bda)
3483 #if BLE_INCLUDED == TRUE
3484                     && bta_dm_cb.device_list.peer_device[i].conn_handle == p_data->acl_change.handle
3485 #endif
3486                ) {
3487                 break;
3488             }
3489 
3490         }
3491 
3492         if (i == bta_dm_cb.device_list.count) {
3493             if (bta_dm_cb.device_list.count < BTA_DM_NUM_PEER_DEVICE) {
3494                 bdcpy(bta_dm_cb.device_list.peer_device[bta_dm_cb.device_list.count].peer_bdaddr, p_bda);
3495                 bta_dm_cb.device_list.peer_device[bta_dm_cb.device_list.count].link_policy = bta_dm_cb.cur_policy;
3496                 bta_dm_cb.device_list.count++;
3497 #if BLE_INCLUDED == TRUE
3498                 bta_dm_cb.device_list.peer_device[i].conn_handle = p_data->acl_change.handle;
3499                 if (p_data->acl_change.transport == BT_TRANSPORT_LE) {
3500                     bta_dm_cb.device_list.le_count++;
3501                 }
3502 #endif
3503             } else {
3504                 APPL_TRACE_ERROR("%s max active connection reached, no resources", __func__);
3505                 return;
3506             }
3507         }
3508 
3509         bta_dm_cb.device_list.peer_device[i].conn_state = BTA_DM_CONNECTED;
3510         bta_dm_cb.device_list.peer_device[i].pref_role = BTA_ANY_ROLE;
3511         bdcpy(conn.link_up.bd_addr, p_bda);
3512         bta_dm_cb.device_list.peer_device[i].info = BTA_DM_DI_NONE;
3513 #if BLE_INCLUDED == TRUE
3514         conn.link_up.link_type = p_data->acl_change.transport;
3515         bta_dm_cb.device_list.peer_device[i].transport = p_data->acl_change.transport;
3516 #endif
3517 
3518         if (((NULL != (p = BTM_ReadLocalFeatures ())) && HCI_SNIFF_SUB_RATE_SUPPORTED(p)) &&
3519                 ((NULL != (p = BTM_ReadRemoteFeatures (p_bda))) && HCI_SNIFF_SUB_RATE_SUPPORTED(p))) {
3520             /* both local and remote devices support SSR */
3521             bta_dm_cb.device_list.peer_device[i].info = BTA_DM_DI_USE_SSR;
3522         }
3523         APPL_TRACE_DEBUG("%s info: 0x%x", __func__, bta_dm_cb.device_list.peer_device[i].info);
3524 
3525         if (bta_dm_cb.p_sec_cback) {
3526             conn.link_up.sc_downgrade = p_data->acl_change.sc_downgrade;
3527             bta_dm_cb.p_sec_cback(BTA_DM_LINK_UP_EVT, (tBTA_DM_SEC *)&conn);
3528         }
3529     } else {
3530         for (i = 0; i < bta_dm_cb.device_list.count; i++) {
3531             if (bdcmp( bta_dm_cb.device_list.peer_device[i].peer_bdaddr, p_bda)
3532 #if BLE_INCLUDED == TRUE
3533                     || bta_dm_cb.device_list.peer_device[i].transport != p_data->acl_change.transport
3534 #endif
3535                ) {
3536                 continue;
3537             }
3538 
3539             if ( bta_dm_cb.device_list.peer_device[i].conn_state == BTA_DM_UNPAIRING ) {
3540                 if (BTM_SecDeleteDevice(bta_dm_cb.device_list.peer_device[i].peer_bdaddr, bta_dm_cb.device_list.peer_device[i].transport)) {
3541                     issue_unpair_cb = TRUE;
3542                 }
3543 
3544                 APPL_TRACE_DEBUG("%s: Unpairing: issue unpair CB = %d ", __FUNCTION__, issue_unpair_cb);
3545             }
3546 
3547             conn.link_down.is_removed = bta_dm_cb.device_list.peer_device[i].remove_dev_pending;
3548 
3549             for (; i < bta_dm_cb.device_list.count ; i++) {
3550                 memcpy(&bta_dm_cb.device_list.peer_device[i], &bta_dm_cb.device_list.peer_device[i + 1], sizeof(bta_dm_cb.device_list.peer_device[i]));
3551             }
3552             break;
3553         }
3554         if (bta_dm_cb.device_list.count) {
3555             bta_dm_cb.device_list.count--;
3556         }
3557 #if BLE_INCLUDED == TRUE
3558         if ((p_data->acl_change.transport == BT_TRANSPORT_LE) &&
3559                 (bta_dm_cb.device_list.le_count)) {
3560             bta_dm_cb.device_list.le_count--;
3561         }
3562         conn.link_down.link_type = p_data->acl_change.transport;
3563 #endif
3564 
3565         if (bta_dm_search_cb.wait_disc && !bdcmp(bta_dm_search_cb.peer_bdaddr, p_bda)) {
3566             bta_dm_search_cb.wait_disc = FALSE;
3567 
3568             if (bta_dm_search_cb.sdp_results) {
3569                 APPL_TRACE_EVENT(" timer stopped  ");
3570                 bta_sys_stop_timer(&bta_dm_search_cb.search_timer);
3571                 bta_dm_discover_next_device();
3572             }
3573 
3574         }
3575 
3576         if (bta_dm_cb.disabling) {
3577             if (!BTM_GetNumAclLinks()) {
3578                 bta_sys_stop_timer(&bta_dm_cb.disable_timer);
3579                 bta_dm_cb.disable_timer.p_cback = (TIMER_CBACK *)&bta_dm_disable_conn_down_timer_cback;
3580                 /*
3581                  * Start a timer to make sure that the profiles
3582                  * get the disconnect event.
3583                  */
3584                 bta_sys_start_timer(&bta_dm_cb.disable_timer, 0, 1000);
3585             }
3586         }
3587         if (conn.link_down.is_removed) {
3588             BTM_SecDeleteDevice(p_bda, p_data->acl_change.transport);
3589 #if (BLE_INCLUDED == TRUE && GATTC_INCLUDED == TRUE)
3590             /* need to remove all pending background connection */
3591             BTA_GATTC_CancelOpen(0, p_bda, FALSE);
3592             /* remove all cached GATT information */
3593             BTA_GATTC_Refresh(p_bda, false);
3594 #endif
3595         }
3596 
3597         bdcpy(conn.link_down.bd_addr, p_bda);
3598         conn.link_down.reason = (UINT8) btm_get_acl_disc_reason_code();
3599         if ( bta_dm_cb.p_sec_cback ) {
3600             bta_dm_cb.p_sec_cback(BTA_DM_LINK_DOWN_EVT, &conn);
3601             if ( issue_unpair_cb ) {
3602                 if (p_data->acl_change.transport == BT_TRANSPORT_LE) {
3603                     bta_dm_cb.p_sec_cback(BTA_DM_BLE_DEV_UNPAIRED_EVT, &conn);
3604                 } else {
3605                     bta_dm_cb.p_sec_cback(BTA_DM_DEV_UNPAIRED_EVT, &conn);
3606                 }
3607             }
3608         }
3609     }
3610 
3611     bta_dm_adjust_roles(TRUE);
3612 }
3613 
3614 /*******************************************************************************
3615 **
3616 ** Function         bta_dm_disable_conn_down_timer_cback
3617 **
3618 ** Description      Sends disable event to application
3619 **
3620 **
3621 ** Returns          void
3622 **
3623 *******************************************************************************/
bta_dm_disable_conn_down_timer_cback(TIMER_LIST_ENT * p_tle)3624 static void bta_dm_disable_conn_down_timer_cback (TIMER_LIST_ENT *p_tle)
3625 {
3626     UNUSED(p_tle);
3627     tBTA_SYS_HW_MSG *sys_enable_event;
3628 
3629 #if (BTA_DM_PM_INCLUDED == TRUE)
3630     /* disable the power managment module */
3631     bta_dm_disable_pm();
3632 #endif /* #if (BTA_DM_PM_INCLUDED == TRUE) */
3633 
3634     /* register our callback to SYS HW manager */
3635     bta_sys_hw_register( BTA_SYS_HW_BLUETOOTH, bta_dm_sys_hw_cback );
3636 
3637     /* send a message to BTA SYS */
3638     if ((sys_enable_event = (tBTA_SYS_HW_MSG *) osi_malloc(sizeof(tBTA_SYS_HW_MSG))) != NULL) {
3639         sys_enable_event->hdr.event = BTA_SYS_API_DISABLE_EVT;
3640         sys_enable_event->hw_module = BTA_SYS_HW_BLUETOOTH;
3641         bta_sys_sendmsg(sys_enable_event);
3642     }
3643 
3644     bta_dm_cb.disabling = FALSE;
3645 
3646 }
3647 
3648 /*******************************************************************************
3649 **
3650 ** Function         bta_dm_rm_cback
3651 **
3652 ** Description      Role management callback from sys
3653 **
3654 **
3655 ** Returns          void
3656 **
3657 *******************************************************************************/
bta_dm_rm_cback(tBTA_SYS_CONN_STATUS status,UINT8 id,UINT8 app_id,BD_ADDR peer_addr)3658 static void bta_dm_rm_cback(tBTA_SYS_CONN_STATUS status, UINT8 id, UINT8 app_id, BD_ADDR peer_addr)
3659 {
3660     UINT8 j;
3661     tBTA_PREF_ROLES role;
3662     tBTA_DM_PEER_DEVICE *p_dev;
3663 
3664     p_dev = bta_dm_find_peer_device(peer_addr);
3665     if ( status == BTA_SYS_CONN_OPEN) {
3666         if (p_dev) {
3667             /* Do not set to connected if we are in the middle of unpairing. When AV stream is
3668              * started it fakes out a SYS_CONN_OPEN to potentially trigger a role switch command.
3669              * But this should not be done if we are in the middle of unpairing.
3670              */
3671             if (p_dev->conn_state != BTA_DM_UNPAIRING) {
3672                 p_dev->conn_state = BTA_DM_CONNECTED;
3673             }
3674 
3675             for (j = 1; j <= p_bta_dm_rm_cfg[0].app_id; j++) {
3676                 if (((p_bta_dm_rm_cfg[j].app_id == app_id) || (p_bta_dm_rm_cfg[j].app_id == BTA_ALL_APP_ID))
3677                         && (p_bta_dm_rm_cfg[j].id == id)) {
3678                     role = p_bta_dm_rm_cfg[j].cfg;
3679 
3680                     if (role > p_dev->pref_role ) {
3681                         p_dev->pref_role = role;
3682                     }
3683                     break;
3684                 }
3685             }
3686         }
3687     }
3688 
3689     if ((BTA_ID_AV == id) || (BTA_ID_AVK == id)) {
3690         if ( status == BTA_SYS_CONN_BUSY) {
3691             if (p_dev) {
3692                 p_dev->info |= BTA_DM_DI_AV_ACTIVE;
3693             }
3694             /* AV calls bta_sys_conn_open with the A2DP stream count as app_id */
3695             if (BTA_ID_AV == id) {
3696                 bta_dm_cb.cur_av_count = bta_dm_get_av_count();
3697             }
3698         } else if ( status == BTA_SYS_CONN_IDLE) {
3699             if (p_dev) {
3700                 p_dev->info &= ~BTA_DM_DI_AV_ACTIVE;
3701             }
3702 
3703             /* get cur_av_count from connected services */
3704             if (BTA_ID_AV == id) {
3705                 bta_dm_cb.cur_av_count = bta_dm_get_av_count();
3706             }
3707         }
3708         APPL_TRACE_EVENT("bta_dm_rm_cback:%d, status:%d", bta_dm_cb.cur_av_count, status);
3709     }
3710 
3711     /* Don't adjust roles for each busy/idle state transition to avoid
3712        excessive switch requests when individual profile busy/idle status
3713        changes */
3714     if ((status != BTA_SYS_CONN_BUSY) && (status != BTA_SYS_CONN_IDLE)) {
3715         bta_dm_adjust_roles(FALSE);
3716     }
3717 }
3718 
3719 /*******************************************************************************
3720 **
3721 ** Function         bta_dm_delay_role_switch_cback
3722 **
3723 ** Description      Callback from btm to delay a role switch
3724 **
3725 ** Returns          void
3726 **
3727 *******************************************************************************/
bta_dm_delay_role_switch_cback(TIMER_LIST_ENT * p_tle)3728 static void bta_dm_delay_role_switch_cback(TIMER_LIST_ENT *p_tle)
3729 {
3730     UNUSED(p_tle);
3731     APPL_TRACE_EVENT("bta_dm_delay_role_switch_cback: initiating Delayed RS");
3732     bta_dm_adjust_roles (FALSE);
3733 }
3734 
3735 /*******************************************************************************
3736 **
3737 ** Function         bta_dm_remove_sec_dev_entry
3738 **
3739 ** Description      Removes device entry from Security device DB if ACL connection with
3740 **                  remtoe device does not exist, else schedule for dev entry removal upon
3741                      ACL close
3742 **
3743 ** Returns          void
3744 **
3745 *******************************************************************************/
3746 #if (SMP_INCLUDED == TRUE)
bta_dm_remove_sec_dev_entry(BD_ADDR remote_bd_addr)3747 static void bta_dm_remove_sec_dev_entry(BD_ADDR remote_bd_addr)
3748 {
3749     UINT16 index = 0;
3750     if ( BTM_IsAclConnectionUp(remote_bd_addr, BT_TRANSPORT_LE) ||
3751             BTM_IsAclConnectionUp(remote_bd_addr, BT_TRANSPORT_BR_EDR)) {
3752         APPL_TRACE_DEBUG("%s ACL is not down. Schedule for  Dev Removal when ACL closes",
3753                          __FUNCTION__);
3754         BTM_SecClearSecurityFlags (remote_bd_addr);
3755         for (index = 0; index < bta_dm_cb.device_list.count; index ++) {
3756             if (!bdcmp( bta_dm_cb.device_list.peer_device[index].peer_bdaddr, remote_bd_addr)) {
3757                 break;
3758             }
3759         }
3760         if (index != bta_dm_cb.device_list.count) {
3761             bta_dm_cb.device_list.peer_device[index].remove_dev_pending = TRUE;
3762         } else {
3763             APPL_TRACE_ERROR(" %s Device does not exist in DB", __FUNCTION__);
3764         }
3765     } else {
3766         BTM_SecDeleteDevice (remote_bd_addr, bta_dm_cb.device_list.peer_device[index].transport);
3767 #if (BLE_INCLUDED == TRUE && GATTC_INCLUDED == TRUE)
3768         /* need to remove all pending background connection */
3769         BTA_GATTC_CancelOpen(0, remote_bd_addr, FALSE);
3770         /* remove all cached GATT information */
3771         BTA_GATTC_Refresh(remote_bd_addr, false);
3772 #endif
3773     }
3774 }
3775 #endif  ///SMP_INCLUDED == TRUE
3776 
3777 
3778 /*******************************************************************************
3779 **
3780 ** Function         bta_dm_adjust_roles
3781 **
3782 ** Description      Adjust roles
3783 **
3784 **
3785 ** Returns          void
3786 **
3787 *******************************************************************************/
bta_dm_adjust_roles(BOOLEAN delay_role_switch)3788 static void bta_dm_adjust_roles(BOOLEAN delay_role_switch)
3789 {
3790 
3791     UINT8 i;
3792     BOOLEAN set_master_role = FALSE;
3793 #if BLE_INCLUDED == TRUE
3794     UINT8 br_count = bta_dm_cb.device_list.count - bta_dm_cb.device_list.le_count;
3795 #else
3796     UINT8 br_count = bta_dm_cb.device_list.count;
3797 #endif
3798     if (br_count) {
3799 
3800         /* the configuration is no scatternet
3801          * or AV connection exists and there are more than one ACL link */
3802         if ( (p_bta_dm_rm_cfg[0].cfg == BTA_DM_NO_SCATTERNET) ||
3803                 (bta_dm_cb.cur_av_count && br_count > 1) ) {
3804 
3805             L2CA_SetDesireRole (HCI_ROLE_MASTER);
3806             set_master_role = TRUE;
3807 
3808         }
3809 
3810         for (i = 0; i < bta_dm_cb.device_list.count; i++) {
3811             if (bta_dm_cb.device_list.peer_device[i].conn_state == BTA_DM_CONNECTED
3812 #if BLE_INCLUDED == TRUE
3813                     && bta_dm_cb.device_list.peer_device[i].transport == BT_TRANSPORT_BR_EDR
3814 #endif
3815                ) {
3816                 if (!set_master_role && (bta_dm_cb.device_list.peer_device[i].pref_role != BTA_ANY_ROLE)
3817                         && (p_bta_dm_rm_cfg[0].cfg == BTA_DM_PARTIAL_SCATTERNET)) {
3818                     L2CA_SetDesireRole (HCI_ROLE_MASTER);
3819                     set_master_role = TRUE;
3820                 }
3821 
3822                 if ((bta_dm_cb.device_list.peer_device[i].pref_role == BTA_MASTER_ROLE_ONLY)
3823                         || (br_count > 1)) {
3824 
3825                     /* Initiating immediate role switch with certain remote devices
3826                       has caused issues due to role  switch colliding with link encryption setup and
3827                       causing encryption (and in turn the link) to fail .  These device . Firmware
3828                       versions are stored in a blacklist and role switch with these devices are
3829                       delayed to avoid the collision with link encryption setup */
3830 
3831                     if (bta_dm_cb.device_list.peer_device[i].pref_role != BTA_SLAVE_ROLE_ONLY &&
3832                             delay_role_switch == FALSE) {
3833                         BTM_SwitchRole (bta_dm_cb.device_list.peer_device[i].peer_bdaddr,
3834                                         HCI_ROLE_MASTER, NULL);
3835                     } else {
3836                         bta_dm_cb.switch_delay_timer[i].p_cback =
3837                             (TIMER_CBACK *)&bta_dm_delay_role_switch_cback;
3838                         bta_sys_start_timer(&bta_dm_cb.switch_delay_timer[i], 0, 500);
3839                     }
3840                 }
3841 
3842             }
3843         }
3844 
3845 
3846         if (!set_master_role) {
3847 
3848             L2CA_SetDesireRole (L2CAP_DESIRED_LINK_ROLE);
3849 
3850         }
3851 
3852     } else {
3853         L2CA_SetDesireRole (L2CAP_DESIRED_LINK_ROLE);
3854     }
3855 
3856 
3857 }
3858 
3859 /*******************************************************************************
3860 **
3861 ** Function         bta_dm_get_remname
3862 **
3863 ** Description      Returns a pointer to the remote name stored in the DM control
3864 **                  block if it exists, or from the BTM memory.
3865 **
3866 ** Returns          char * - Pointer to the remote device name
3867 *******************************************************************************/
3868 #if (SDP_INCLUDED == TRUE || SMP_INCLUDED == TRUE)
bta_dm_get_remname(void)3869 static char *bta_dm_get_remname(void)
3870 {
3871     char *p_name = (char *)bta_dm_search_cb.peer_name;
3872     char *p_temp;
3873 
3874     /* If the name isn't already stored, try retrieving from BTM */
3875     if (*p_name == '\0') {
3876         if ((p_temp = BTM_SecReadDevName(bta_dm_search_cb.peer_bdaddr)) != NULL) {
3877             p_name = p_temp;
3878         }
3879     }
3880     return p_name;
3881 }
3882 #endif  ///SDP_INCLUDED == TRUE || SMP_INCLUDED == TRUE
3883 
3884 /*******************************************************************************
3885 **
3886 ** Function         bta_dm_bond_cancel_complete_cback
3887 **
3888 ** Description      Authentication complete callback from BTM
3889 **
3890 ** Returns          void
3891 **
3892 *******************************************************************************/
3893 #if (SMP_INCLUDED == TRUE)
bta_dm_bond_cancel_complete_cback(tBTM_STATUS result)3894 static void bta_dm_bond_cancel_complete_cback(tBTM_STATUS result)
3895 {
3896 
3897     tBTA_DM_SEC sec_event;
3898 
3899     if (result == BTM_SUCCESS) {
3900         sec_event.bond_cancel_cmpl.result = BTA_SUCCESS;
3901     } else {
3902         sec_event.bond_cancel_cmpl.result = BTA_FAILURE;
3903     }
3904 
3905     if (bta_dm_cb.p_sec_cback) {
3906         bta_dm_cb.p_sec_cback(BTA_DM_BOND_CANCEL_CMPL_EVT, &sec_event);
3907     }
3908 }
3909 #endif  ///SMP_INCLUDED == TRUE
3910 
3911 /*******************************************************************************
3912 **
3913 ** Function         bta_dm_set_eir
3914 **
3915 ** Description      This function creates EIR tagged data and writes it to controller.
3916 **
3917 ** Returns          None
3918 **
3919 *******************************************************************************/
3920 #if (CLASSIC_BT_INCLUDED == TRUE)
bta_dm_set_eir(char * local_name)3921 static void bta_dm_set_eir (char *local_name)
3922 {
3923     BT_HDR   *p_buf;
3924     UINT8    *p;
3925     UINT8    *p_length;
3926 #if (BTA_EIR_CANNED_UUID_LIST != TRUE)
3927     UINT8    *p_type;
3928     UINT8    max_num_uuid;
3929 #if (BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0)
3930     UINT8    custom_uuid_idx;
3931 #endif  // BTA_EIR_SERVER_NUM_CUSTOM_UUID
3932 #endif  // BTA_EIR_CANNED_UUID_LIST
3933 
3934     UINT8    free_eir_length;
3935     if (p_bta_dm_eir_cfg->bta_dm_eir_fec_required) {
3936         free_eir_length = HCI_DM5_PACKET_SIZE;
3937     } else {
3938         free_eir_length = HCI_EXT_INQ_RESPONSE_LEN;
3939     }
3940 
3941     UINT8    num_uuid;
3942     UINT8    data_type;
3943     UINT8    local_name_len;
3944 
3945     UINT8    eir_type[BTM_EIR_TYPE_MAX_NUM];
3946     UINT8    eir_type_num = 0;
3947 
3948     tBTA_STATUS status = BTA_SUCCESS;
3949 
3950     /* wait until complete to disable */
3951     if (bta_dm_cb.disable_timer.in_use) {
3952         if (p_bta_dm_eir_cfg->config_eir_callback) {
3953             p_bta_dm_eir_cfg->config_eir_callback(BTA_WRONG_MODE, eir_type_num , eir_type);
3954         }
3955         return;
3956     }
3957 
3958 #if ( BTA_EIR_CANNED_UUID_LIST != TRUE )
3959     /* wait until App is ready */
3960     if (bta_dm_cb.app_ready_timer.in_use) {
3961         if (p_bta_dm_eir_cfg->config_eir_callback) {
3962             p_bta_dm_eir_cfg->config_eir_callback(BTA_WRONG_MODE, eir_type_num , eir_type);
3963         }
3964         return;
3965     }
3966 
3967     /* if local name is not provided, get it from controller */
3968     if ( local_name == NULL ) {
3969         if ( BTM_ReadLocalDeviceName( &local_name ) != BTM_SUCCESS ) {
3970             APPL_TRACE_ERROR("Fail to read local device name for EIR");
3971         }
3972     }
3973 #endif  // BTA_EIR_CANNED_UUID_LIST
3974 
3975     /* Allocate a buffer to hold HCI command */
3976     if ((p_buf = (BT_HDR *)osi_malloc(BTM_CMD_BUF_SIZE)) == NULL) {
3977         APPL_TRACE_ERROR("bta_dm_set_eir couldn't allocate buffer");
3978         if (p_bta_dm_eir_cfg->config_eir_callback) {
3979             p_bta_dm_eir_cfg->config_eir_callback(BTA_NO_RESOURCES, eir_type_num , eir_type);
3980         }
3981         return;
3982     }
3983     p = (UINT8 *)p_buf + BTM_HCI_EIR_OFFSET;
3984 
3985     memset(p, 0x00, HCI_EXT_INQ_RESPONSE_LEN );
3986 
3987     APPL_TRACE_DEBUG("BTA is generating EIR");
3988 
3989     if ( local_name ) {
3990         local_name_len = strlen( local_name );
3991     } else {
3992         local_name_len = 0;
3993     }
3994 
3995     data_type = BTM_EIR_COMPLETE_LOCAL_NAME_TYPE;
3996     /* if local name is longer than minimum length of shortened name */
3997     /* check whether it needs to be shortened or not */
3998     if ( local_name_len > p_bta_dm_eir_cfg->bta_dm_eir_min_name_len ) {
3999         /* get number of UUID 16-bit list */
4000 #if (BTA_EIR_CANNED_UUID_LIST == TRUE)
4001         num_uuid = p_bta_dm_eir_cfg->bta_dm_eir_uuid16_len / LEN_UUID_16;
4002 #else  // BTA_EIR_CANNED_UUID_LIST
4003         max_num_uuid = (free_eir_length - 2) / LEN_UUID_16;
4004         data_type = BTM_GetEirSupportedServices( bta_dm_cb.eir_uuid, &p,
4005                     max_num_uuid, &num_uuid );
4006         p = (UINT8 *)p_buf + BTM_HCI_EIR_OFFSET; /* reset p */
4007 #endif  // BTA_EIR_CANNED_UUID_LIST
4008 
4009         /* if UUID doesn't fit remaing space, shorten local name */
4010         if ( local_name_len > (free_eir_length - 4 - num_uuid * LEN_UUID_16)) {
4011             APPL_TRACE_WARNING("BTA EIR: local name is shortened");
4012             local_name_len = p_bta_dm_eir_cfg->bta_dm_eir_min_name_len;
4013             data_type = BTM_EIR_SHORTENED_LOCAL_NAME_TYPE;
4014         } else {
4015             data_type = BTM_EIR_COMPLETE_LOCAL_NAME_TYPE;
4016         }
4017     }
4018 
4019     UINT8_TO_STREAM(p, local_name_len + 1);
4020     UINT8_TO_STREAM(p, data_type);
4021     eir_type[eir_type_num++] = data_type;
4022 
4023     if (local_name != NULL) {
4024         memcpy(p, local_name, local_name_len);
4025         p += local_name_len;
4026     }
4027     free_eir_length -= local_name_len + 2;
4028 
4029     /* if UUIDs are provided in configuration */
4030     if (p_bta_dm_eir_cfg->bta_dm_eir_included_uuid) {
4031 #if (BTA_EIR_CANNED_UUID_LIST == TRUE)
4032         /* if UUID list is provided as static data in configuration */
4033         if (( p_bta_dm_eir_cfg->bta_dm_eir_uuid16_len > 0 )
4034                 && (p_bta_dm_eir_cfg->bta_dm_eir_uuid16)) {
4035             if ( free_eir_length > LEN_UUID_16 + 2) {
4036                 free_eir_length -= 2;
4037 
4038                 if ( free_eir_length >= p_bta_dm_eir_cfg->bta_dm_eir_uuid16_len) {
4039                     num_uuid = p_bta_dm_eir_cfg->bta_dm_eir_uuid16_len / LEN_UUID_16;
4040                     data_type = BTM_EIR_COMPLETE_16BITS_UUID_TYPE;
4041                 } else { /* not enough room for all UUIDs */
4042                     APPL_TRACE_WARNING("BTA EIR: UUID 16-bit list is truncated");
4043                     num_uuid = free_eir_length / LEN_UUID_16;
4044                     data_type = BTM_EIR_MORE_16BITS_UUID_TYPE;
4045                 }
4046                 UINT8_TO_STREAM(p, num_uuid * LEN_UUID_16 + 1);
4047                 UINT8_TO_STREAM(p, data_type);
4048                 eir_type[eir_type_num++] = data_type;
4049                 memcpy(p, p_bta_dm_eir_cfg->bta_dm_eir_uuid16, num_uuid * LEN_UUID_16 );
4050                 p += num_uuid * LEN_UUID_16;
4051                 free_eir_length -= num_uuid * LEN_UUID_16;
4052             } else {
4053                 status = BTA_EIR_TOO_LARGE;
4054             }
4055         }
4056 #else /* (BTA_EIR_CANNED_UUID_LIST == TRUE) */
4057         /* if UUID list is dynamic */
4058         if ( free_eir_length >= 2) {
4059             p_length = p++;
4060             p_type   = p++;
4061             num_uuid = 0;
4062 
4063             max_num_uuid = (free_eir_length - 2) / LEN_UUID_16;
4064             data_type = BTM_GetEirSupportedServices( bta_dm_cb.eir_uuid, &p, max_num_uuid, &num_uuid );
4065 
4066             if ( data_type == BTM_EIR_MORE_16BITS_UUID_TYPE ) {
4067                 APPL_TRACE_WARNING("BTA EIR: UUID 16-bit list is truncated");
4068             }
4069 #if (BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0)
4070             else {
4071                 for (custom_uuid_idx = 0; custom_uuid_idx < BTA_EIR_SERVER_NUM_CUSTOM_UUID; custom_uuid_idx++) {
4072                     if (bta_dm_cb.custom_uuid[custom_uuid_idx].len == LEN_UUID_16) {
4073                         if ( num_uuid < max_num_uuid ) {
4074                             UINT16_TO_STREAM(p, bta_dm_cb.custom_uuid[custom_uuid_idx].uu.uuid16);
4075                             num_uuid++;
4076                         } else {
4077                             data_type = BTM_EIR_MORE_16BITS_UUID_TYPE;
4078                             APPL_TRACE_WARNING("BTA EIR: UUID 16-bit list is truncated");
4079                             break;
4080                         }
4081                     }
4082                 }
4083             }
4084 #endif /* (BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0) */
4085 
4086             UINT8_TO_STREAM(p_length, num_uuid * LEN_UUID_16 + 1);
4087             UINT8_TO_STREAM(p_type, data_type);
4088             eir_type[eir_type_num++] = data_type;
4089             free_eir_length -= num_uuid * LEN_UUID_16 + 2;
4090         } else {
4091             status = BTA_EIR_TOO_LARGE;
4092         }
4093 #endif /* (BTA_EIR_CANNED_UUID_LIST == TRUE) */
4094 
4095 #if ( BTA_EIR_CANNED_UUID_LIST != TRUE )&&(BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0)
4096         /* Adding 32-bit UUID list */
4097         if ( free_eir_length >= 2) {
4098             p_length = p++;
4099             p_type   = p++;
4100             num_uuid = 0;
4101             data_type = BTM_EIR_COMPLETE_32BITS_UUID_TYPE;
4102 
4103             max_num_uuid = (free_eir_length - 2) / LEN_UUID_32;
4104 
4105             for (custom_uuid_idx = 0; custom_uuid_idx < BTA_EIR_SERVER_NUM_CUSTOM_UUID; custom_uuid_idx++) {
4106                 if (bta_dm_cb.custom_uuid[custom_uuid_idx].len == LEN_UUID_32) {
4107                     if ( num_uuid < max_num_uuid ) {
4108                         UINT32_TO_STREAM(p, bta_dm_cb.custom_uuid[custom_uuid_idx].uu.uuid32);
4109                         num_uuid++;
4110                     } else {
4111                         data_type = BTM_EIR_MORE_32BITS_UUID_TYPE;
4112                         APPL_TRACE_WARNING("BTA EIR: UUID 32-bit list is truncated");
4113                         break;
4114                     }
4115                 }
4116             }
4117 
4118             UINT8_TO_STREAM(p_length, num_uuid * LEN_UUID_32 + 1);
4119             UINT8_TO_STREAM(p_type, data_type);
4120             eir_type[eir_type_num++] = data_type;
4121             free_eir_length -= num_uuid * LEN_UUID_32 + 2;
4122         } else {
4123             status = BTA_EIR_TOO_LARGE;
4124         }
4125 
4126         /* Adding 128-bit UUID list */
4127         if ( free_eir_length >= 2) {
4128             p_length = p++;
4129             p_type   = p++;
4130             num_uuid = 0;
4131             data_type = BTM_EIR_COMPLETE_128BITS_UUID_TYPE;
4132 
4133             max_num_uuid = (free_eir_length - 2) / LEN_UUID_128;
4134 
4135             for (custom_uuid_idx = 0; custom_uuid_idx < BTA_EIR_SERVER_NUM_CUSTOM_UUID; custom_uuid_idx++) {
4136                 if (bta_dm_cb.custom_uuid[custom_uuid_idx].len == LEN_UUID_128) {
4137                     if ( num_uuid < max_num_uuid ) {
4138                         ARRAY16_TO_STREAM(p, bta_dm_cb.custom_uuid[custom_uuid_idx].uu.uuid128);
4139                         num_uuid++;
4140                     } else {
4141                         data_type = BTM_EIR_MORE_128BITS_UUID_TYPE;
4142                         APPL_TRACE_WARNING("BTA EIR: UUID 128-bit list is truncated");
4143                         break;
4144                     }
4145                 }
4146             }
4147 
4148             UINT8_TO_STREAM(p_length, num_uuid * LEN_UUID_128 + 1);
4149             UINT8_TO_STREAM(p_type, data_type);
4150             eir_type[eir_type_num++] = data_type;
4151             free_eir_length -= num_uuid * LEN_UUID_128 + 2;
4152         }
4153          else {
4154             status = BTA_EIR_TOO_LARGE;
4155         }
4156 #endif /* ( BTA_EIR_CANNED_UUID_LIST != TRUE )&&(BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0) */
4157     }
4158 
4159     /* if Flags are provided in configuration */
4160     if ( p_bta_dm_eir_cfg->bta_dm_eir_flags != 0 ) {
4161         if ( free_eir_length >= 3 ) {
4162             UINT8_TO_STREAM(p, 2);
4163             UINT8_TO_STREAM(p, BTM_EIR_FLAGS_TYPE);
4164             eir_type[eir_type_num++] = BTM_EIR_FLAGS_TYPE;
4165             UINT8_TO_STREAM(p, p_bta_dm_eir_cfg->bta_dm_eir_flags);
4166             free_eir_length -= 3;
4167         } else {
4168             status = BTA_EIR_TOO_LARGE;
4169         }
4170     }
4171 
4172     /* if Manufacturer Specific are provided in configuration */
4173     if (( p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len > 0 )
4174             && ( p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec )) {
4175         if ( free_eir_length >= p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len + 2) {
4176             p_length = p;
4177 
4178             UINT8_TO_STREAM(p, p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len + 1);
4179             UINT8_TO_STREAM(p, BTM_EIR_MANUFACTURER_SPECIFIC_TYPE);
4180             eir_type[eir_type_num++] = BTM_EIR_MANUFACTURER_SPECIFIC_TYPE;
4181             memcpy(p, p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec,
4182                p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len);
4183             p += p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len;
4184             free_eir_length -= p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len + 2;
4185         } else {
4186             status = BTA_EIR_TOO_LARGE;
4187         }
4188     } else {
4189         p_length = NULL;
4190     }
4191 
4192     /* if Inquiry Tx Resp Power compiled */
4193     if (p_bta_dm_eir_cfg->bta_dm_eir_included_tx_power) {
4194         if (free_eir_length >= 3) {
4195             int min_power_level, max_power_level;
4196             if (bredr_txpwr_get(&min_power_level, &max_power_level) == 0) {
4197                 INT8 btm_tx_power[BTM_TX_POWER_LEVEL_MAX + 1] = BTM_TX_POWER;
4198                 p_bta_dm_eir_cfg->bta_dm_eir_inq_tx_power = btm_tx_power[max_power_level];
4199                 UINT8_TO_STREAM(p, 2);      /* Length field */
4200                 UINT8_TO_STREAM(p, BTM_EIR_TX_POWER_LEVEL_TYPE);
4201                 eir_type[eir_type_num++] = BTM_EIR_TX_POWER_LEVEL_TYPE;
4202                 UINT8_TO_STREAM(p, p_bta_dm_eir_cfg->bta_dm_eir_inq_tx_power);
4203             free_eir_length -= 3;
4204             }
4205         } else {
4206             status = BTA_EIR_TOO_LARGE;
4207         }
4208     }
4209 
4210     /* if URL are provided in configuration */
4211     if (( p_bta_dm_eir_cfg->bta_dm_eir_url_len > 0 )
4212             && ( p_bta_dm_eir_cfg->bta_dm_eir_url )) {
4213         if ( free_eir_length >= p_bta_dm_eir_cfg->bta_dm_eir_url_len + 2 ) {
4214             UINT8_TO_STREAM(p, p_bta_dm_eir_cfg->bta_dm_eir_url_len + 1);
4215             UINT8_TO_STREAM(p, BTM_EIR_URL_TYPE);
4216             eir_type[eir_type_num++] = BTM_EIR_URL_TYPE;
4217             memcpy(p, p_bta_dm_eir_cfg->bta_dm_eir_url,
4218                p_bta_dm_eir_cfg->bta_dm_eir_url_len);
4219             p += p_bta_dm_eir_cfg->bta_dm_eir_url_len;
4220             free_eir_length -= p_bta_dm_eir_cfg->bta_dm_eir_url_len + 2;
4221         } else {
4222             status = BTA_EIR_TOO_LARGE;
4223         }
4224     }
4225 
4226     if ( free_eir_length ) {
4227         UINT8_TO_STREAM(p, 0);    /* terminator of significant part */
4228     }
4229 
4230     tBTM_STATUS btm_status = BTM_WriteEIR( p_buf, p_bta_dm_eir_cfg->bta_dm_eir_fec_required );
4231 
4232     if ( btm_status == BTM_MODE_UNSUPPORTED) {
4233         status = BTA_WRONG_MODE;
4234     } else if (btm_status != BTM_SUCCESS) {
4235         status = BTA_FAILURE;
4236     }
4237 
4238     if (p_bta_dm_eir_cfg->config_eir_callback) {
4239         p_bta_dm_eir_cfg->config_eir_callback(status, eir_type_num, eir_type);
4240     }
4241 }
4242 #endif
4243 
4244 /*******************************************************************************
4245 **
4246 ** Function         bta_dm_eir_search_services
4247 **
4248 ** Description      This function searches services in received EIR
4249 **
4250 ** Returns          None
4251 **
4252 *******************************************************************************/
4253 #if (SDP_INCLUDED == TRUE)
bta_dm_eir_search_services(tBTM_INQ_RESULTS * p_result,tBTA_SERVICE_MASK * p_services_to_search,tBTA_SERVICE_MASK * p_services_found)4254 static void bta_dm_eir_search_services( tBTM_INQ_RESULTS  *p_result,
4255                                         tBTA_SERVICE_MASK *p_services_to_search,
4256                                         tBTA_SERVICE_MASK *p_services_found)
4257 {
4258     tBTA_SERVICE_MASK       service_index = 0;
4259     tBTM_EIR_SEARCH_RESULT  result;
4260 
4261     APPL_TRACE_DEBUG("BTA searching services in EIR of BDA:0x%02X%02X%02X%02X%02X%02X",
4262                      p_result->remote_bd_addr[0], p_result->remote_bd_addr[1],
4263                      p_result->remote_bd_addr[2], p_result->remote_bd_addr[3],
4264                      p_result->remote_bd_addr[4], p_result->remote_bd_addr[5]);
4265 
4266     APPL_TRACE_DEBUG("    with services_to_search=0x%08X", *p_services_to_search);
4267 
4268 #if BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE
4269     /* always do GATT based service discovery by SDP instead of from EIR    */
4270     /* if GATT based service is also to be put in EIR, need to modify this  */
4271     while (service_index < (BTA_MAX_SERVICE_ID - 1))
4272 #else
4273     while (service_index < BTA_MAX_SERVICE_ID)
4274 #endif
4275     {
4276         if ( *p_services_to_search
4277                 & (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(service_index))) {
4278             result = BTM_HasInquiryEirService( p_result,
4279                                                bta_service_id_to_uuid_lkup_tbl[service_index] );
4280 
4281             /* Searching for HSP v1.2 only device */
4282             if ((result != BTM_EIR_FOUND) &&
4283                     (bta_service_id_to_uuid_lkup_tbl[service_index] == UUID_SERVCLASS_HEADSET)) {
4284                 result = BTM_HasInquiryEirService (p_result, UUID_SERVCLASS_HEADSET_HS);
4285             }
4286 
4287             if ( result == BTM_EIR_FOUND ) {
4288                 /* If Plug and Play service record, need to check to see if Broadcom stack */
4289                 /* However, EIR data doesn't have EXT_BRCM_VERSION so just skip it */
4290                 if ( bta_service_id_to_uuid_lkup_tbl[service_index]
4291                         != UUID_SERVCLASS_PNP_INFORMATION ) {
4292 
4293                     *p_services_found |=
4294                         (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(service_index));
4295                     /* remove the service from services to be searched  */
4296                     *p_services_to_search &=
4297                         (tBTA_SERVICE_MASK)(~(BTA_SERVICE_ID_TO_SERVICE_MASK(service_index)));
4298                 }
4299             } else if ( result == BTM_EIR_NOT_FOUND ) {
4300                 /* remove the service from services to be searched  */
4301                 *p_services_to_search &=
4302                     (tBTA_SERVICE_MASK)(~(BTA_SERVICE_ID_TO_SERVICE_MASK(service_index)));
4303             }
4304         }
4305 
4306         service_index++;
4307     }
4308 
4309     APPL_TRACE_ERROR("BTA EIR search result, services_to_search=0x%08X, services_found=0x%08X",
4310                      *p_services_to_search, *p_services_found);
4311 }
4312 #endif  ///SDP_INCLUDED == TRUE
4313 
4314 #if (BTA_EIR_CANNED_UUID_LIST != TRUE)
4315 /*******************************************************************************
4316 **
4317 ** Function         bta_dm_eir_update_uuid
4318 **
4319 ** Description      This function adds or removes service UUID in EIR database.
4320 **
4321 ** Returns          None
4322 **
4323 *******************************************************************************/
bta_dm_eir_update_uuid(UINT16 uuid16,BOOLEAN adding)4324 void bta_dm_eir_update_uuid(UINT16 uuid16, BOOLEAN adding)
4325 {
4326     /* if this UUID is not advertised in EIR */
4327     if ( !BTM_HasEirService( p_bta_dm_eir_cfg->uuid_mask, uuid16 )) {
4328         return;
4329     }
4330 
4331     if ( adding ) {
4332         APPL_TRACE_EVENT("Adding UUID=0x%04X into EIR", uuid16);
4333 
4334         BTM_AddEirService( bta_dm_cb.eir_uuid, uuid16 );
4335     } else {
4336         APPL_TRACE_EVENT("Removing UUID=0x%04X from EIR", uuid16);
4337 
4338         BTM_RemoveEirService( bta_dm_cb.eir_uuid, uuid16 );
4339     }
4340 #if CLASSIC_BT_INCLUDED
4341     bta_dm_set_eir (NULL);
4342 #endif
4343 
4344     APPL_TRACE_EVENT("bta_dm_eir_update_uuid UUID bit mask=0x%08X %08X",
4345                      bta_dm_cb.eir_uuid[1], bta_dm_cb.eir_uuid[0] );
4346 }
4347 #endif
4348 
4349 /*******************************************************************************
4350 **
4351 ** Function         bta_dm_enable_test_mode
4352 **
4353 ** Description      enable test mode
4354 **
4355 **
4356 ** Returns          void
4357 **
4358 *******************************************************************************/
bta_dm_enable_test_mode(tBTA_DM_MSG * p_data)4359 void bta_dm_enable_test_mode(tBTA_DM_MSG *p_data)
4360 {
4361     UNUSED(p_data);
4362     BTM_EnableTestMode();
4363 }
4364 
4365 /*******************************************************************************
4366 **
4367 ** Function         bta_dm_disable_test_mode
4368 **
4369 ** Description      disable test mode
4370 **
4371 **
4372 ** Returns          void
4373 **
4374 *******************************************************************************/
bta_dm_disable_test_mode(tBTA_DM_MSG * p_data)4375 void bta_dm_disable_test_mode(tBTA_DM_MSG *p_data)
4376 {
4377     UNUSED(p_data);
4378     BTM_DeviceReset(NULL);
4379 }
4380 
4381 /*******************************************************************************
4382 **
4383 ** Function         bta_dm_execute_callback
4384 **
4385 ** Description      Just execute a generic call back in the context of the BTU/BTA tack
4386 **
4387 **
4388 ** Returns          void
4389 **
4390 *******************************************************************************/
bta_dm_execute_callback(tBTA_DM_MSG * p_data)4391 void bta_dm_execute_callback(tBTA_DM_MSG *p_data)
4392 {
4393     /* sanity check */
4394     if (p_data->exec_cback.p_exec_cback == NULL) {
4395         return;
4396     }
4397 
4398     p_data->exec_cback.p_exec_cback(p_data->exec_cback.p_param);
4399 }
4400 
4401 /*******************************************************************************
4402 **
4403 ** Function         bta_dm_encrypt_cback
4404 **
4405 ** Description      link encryption complete callback.
4406 **
4407 ** Returns         None
4408 **
4409 *******************************************************************************/
bta_dm_encrypt_cback(BD_ADDR bd_addr,tBT_TRANSPORT transport,void * p_ref_data,tBTM_STATUS result)4410 void bta_dm_encrypt_cback(BD_ADDR bd_addr, tBT_TRANSPORT transport, void *p_ref_data, tBTM_STATUS result)
4411 {
4412     tBTA_STATUS   bta_status = BTA_SUCCESS;
4413     tBTA_DM_ENCRYPT_CBACK *p_callback = NULL;
4414     UINT8   i ;
4415     UNUSED(p_ref_data);
4416 
4417     for (i = 0; i < bta_dm_cb.device_list.count; i++) {
4418         if (bdcmp( bta_dm_cb.device_list.peer_device[i].peer_bdaddr, bd_addr) == 0 &&
4419                 bta_dm_cb.device_list.peer_device[i].conn_state == BTA_DM_CONNECTED) {
4420             break;
4421         }
4422     }
4423 
4424     if (i < bta_dm_cb.device_list.count) {
4425         p_callback = bta_dm_cb.device_list.peer_device[i].p_encrypt_cback;
4426         bta_dm_cb.device_list.peer_device[i].p_encrypt_cback = NULL;
4427     }
4428 
4429     switch (result) {
4430     case BTM_SUCCESS:
4431         break;
4432     case BTM_WRONG_MODE:
4433         bta_status = BTA_WRONG_MODE;
4434         break;
4435     case BTM_NO_RESOURCES:
4436         bta_status = BTA_NO_RESOURCES;
4437         break;
4438     case BTM_BUSY:
4439         bta_status = BTA_BUSY;
4440         break;
4441     default:
4442         bta_status = BTA_FAILURE;
4443         break;
4444     }
4445 
4446     APPL_TRACE_DEBUG("bta_dm_encrypt_cback status =%d p_callback=%p", bta_status, p_callback);
4447 
4448     if (p_callback) {
4449         (*p_callback)(bd_addr, transport, bta_status);
4450     }
4451 }
4452 
4453 /*******************************************************************************
4454 **
4455 ** Function         bta_dm_set_encryption
4456 **
4457 ** Description      This function to encrypt the link
4458 **
4459 ** Returns          None
4460 **
4461 *******************************************************************************/
4462 #if (SMP_INCLUDED == TRUE)
bta_dm_set_encryption(tBTA_DM_MSG * p_data)4463 void bta_dm_set_encryption (tBTA_DM_MSG *p_data)
4464 {
4465     UINT8 i ;
4466 
4467     APPL_TRACE_DEBUG("bta_dm_set_encryption\n"); //todo
4468     if (!p_data->set_encryption.p_callback) {
4469         APPL_TRACE_ERROR("bta_dm_set_encryption callback is not provided\n");
4470         return;
4471     }
4472     for (i = 0; i < bta_dm_cb.device_list.count; i++) {
4473         if (bdcmp( bta_dm_cb.device_list.peer_device[i].peer_bdaddr, p_data->set_encryption.bd_addr) == 0 &&
4474                 bta_dm_cb.device_list.peer_device[i].conn_state == BTA_DM_CONNECTED) {
4475             break;
4476         }
4477     }
4478     if (i < bta_dm_cb.device_list.count) {
4479         if (bta_dm_cb.device_list.peer_device[i].p_encrypt_cback) {
4480             APPL_TRACE_ERROR("earlier enc was not done for same device\n");
4481             (*p_data->set_encryption.p_callback)(p_data->set_encryption.bd_addr,
4482                                                  p_data->set_encryption.transport,
4483                                                  BTA_BUSY);
4484             return;
4485         }
4486 
4487         if (BTM_SetEncryption(p_data->set_encryption.bd_addr, p_data->set_encryption.transport,
4488                               bta_dm_encrypt_cback, &p_data->set_encryption.sec_act)
4489                 == BTM_CMD_STARTED) {
4490             bta_dm_cb.device_list.peer_device[i].p_encrypt_cback = p_data->set_encryption.p_callback;
4491         }
4492     }else{
4493         APPL_TRACE_ERROR("%s, not find peer_bdaddr or peer_bdaddr connection state error", __func__);
4494     }
4495 }
4496 #endif  ///SMP_INCLUDED == TRUE
4497 
4498 #if (BLE_INCLUDED == TRUE)
4499 /*******************************************************************************
4500 **
4501 ** Function         bta_dm_observe_results_cb
4502 **
4503 ** Description      Callback for BLE Observe result
4504 **
4505 **
4506 ** Returns          void
4507 **
4508 *******************************************************************************/
bta_dm_observe_results_cb(tBTM_INQ_RESULTS * p_inq,UINT8 * p_eir)4509 static void bta_dm_observe_results_cb (tBTM_INQ_RESULTS *p_inq, UINT8 *p_eir)
4510 {
4511     tBTA_DM_SEARCH     result;
4512     tBTM_INQ_INFO      *p_inq_info;
4513     APPL_TRACE_DEBUG("bta_dm_observe_results_cb")
4514 
4515     bdcpy(result.inq_res.bd_addr, p_inq->remote_bd_addr);
4516     result.inq_res.rssi = p_inq->rssi;
4517     result.inq_res.ble_addr_type    = p_inq->ble_addr_type;
4518     result.inq_res.inq_result_type  = p_inq->inq_result_type;
4519     result.inq_res.device_type      = p_inq->device_type;
4520     result.inq_res.flag             = p_inq->flag;
4521     result.inq_res.adv_data_len     = p_inq->adv_data_len;
4522     result.inq_res.scan_rsp_len     = p_inq->scan_rsp_len;
4523     memcpy(result.inq_res.dev_class, p_inq->dev_class, sizeof(DEV_CLASS));
4524     result.inq_res.ble_evt_type     = p_inq->ble_evt_type;
4525 
4526     /* application will parse EIR to find out remote device name */
4527     result.inq_res.p_eir = p_eir;
4528 
4529     if ((p_inq_info = BTM_InqDbRead(p_inq->remote_bd_addr)) != NULL) {
4530         /* initialize remt_name_not_required to FALSE so that we get the name by default */
4531         result.inq_res.remt_name_not_required = FALSE;
4532     }
4533 
4534     if (bta_dm_search_cb.p_scan_cback) {
4535         bta_dm_search_cb.p_scan_cback(BTA_DM_INQ_RES_EVT, &result);
4536     }
4537 
4538     if (p_inq_info) {
4539         /* application indicates if it knows the remote name, inside the callback
4540          copy that to the inquiry data base*/
4541         if (result.inq_res.remt_name_not_required) {
4542             p_inq_info->appl_knows_rem_name = TRUE;
4543         }
4544     }
4545 }
4546 
4547 /*******************************************************************************
4548 **
4549 ** Function         bta_dm_observe_cmpl_cb
4550 **
4551 ** Description      Callback for BLE Observe complete
4552 **
4553 **
4554 ** Returns          void
4555 **
4556 *******************************************************************************/
bta_dm_observe_cmpl_cb(void * p_result)4557 static void bta_dm_observe_cmpl_cb (void *p_result)
4558 {
4559     tBTA_DM_SEARCH  data;
4560 
4561     APPL_TRACE_DEBUG("bta_dm_observe_cmpl_cb");
4562 
4563     data.inq_cmpl.num_resps = ((tBTM_INQUIRY_CMPL *)p_result)->num_resp;
4564     if (bta_dm_search_cb.p_scan_cback) {
4565         bta_dm_search_cb.p_scan_cback(BTA_DM_INQ_CMPL_EVT, &data);
4566     }
4567 }
4568 
4569 /*******************************************************************************
4570 **
4571 ** Function         bta_dm_observe_discard_cb
4572 **
4573 ** Description      Callback for BLE Observe lost
4574 **
4575 **
4576 ** Returns          void
4577 **
4578 *******************************************************************************/
bta_dm_observe_discard_cb(uint32_t num_dis)4579 static void bta_dm_observe_discard_cb (uint32_t num_dis)
4580 {
4581     tBTA_DM_SEARCH  data;
4582 
4583     APPL_TRACE_DEBUG("bta_dm_observe_discard_cb");
4584 
4585     data.inq_dis.num_dis = num_dis;
4586     if (bta_dm_search_cb.p_scan_cback) {
4587         bta_dm_search_cb.p_scan_cback(BTA_DM_INQ_DISCARD_NUM_EVT, &data);
4588     }
4589 }
4590 
4591 #if (SMP_INCLUDED == TRUE)
4592 /*******************************************************************************
4593 **
4594 ** Function         bta_dm_ble_smp_cback
4595 **
4596 ** Description      Callback for BLE SMP
4597 **
4598 **
4599 ** Returns          void
4600 **
4601 *******************************************************************************/
bta_dm_ble_smp_cback(tBTM_LE_EVT event,BD_ADDR bda,tBTM_LE_EVT_DATA * p_data)4602 static UINT8 bta_dm_ble_smp_cback (tBTM_LE_EVT event, BD_ADDR bda, tBTM_LE_EVT_DATA *p_data)
4603 {
4604     tBTM_STATUS status = BTM_SUCCESS;
4605     tBTA_DM_SEC sec_event;
4606     char *p_name = NULL;
4607 
4608     if (!bta_dm_cb.p_sec_cback) {
4609         return BTM_NOT_AUTHORIZED;
4610     }
4611 
4612     memset(&sec_event, 0, sizeof(tBTA_DM_SEC));
4613     switch (event) {
4614     case BTM_LE_IO_REQ_EVT: {
4615         // #if (BT_SSP_INCLUDED == TRUE)
4616         bta_dm_co_ble_io_req(bda,
4617                              &p_data->io_req.io_cap,
4618                              &p_data->io_req.oob_data,
4619                              &p_data->io_req.auth_req,
4620                              &p_data->io_req.max_key_size,
4621                              &p_data->io_req.init_keys,
4622                              &p_data->io_req.resp_keys);
4623         // #endif
4624 #if BTM_OOB_INCLUDED == FALSE
4625         status = BTM_SUCCESS;
4626 #endif
4627         APPL_TRACE_EVENT("io mitm: %d oob_data:%d\n", p_data->io_req.auth_req, p_data->io_req.oob_data);
4628 
4629         break;
4630     }
4631 
4632     case BTM_LE_SEC_REQUEST_EVT:
4633         bdcpy(sec_event.ble_req.bd_addr, bda);
4634         p_name = BTM_SecReadDevName(bda);
4635         if (p_name != NULL) {
4636             BCM_STRNCPY_S((char *)sec_event.ble_req.bd_name,p_name, (BD_NAME_LEN));
4637         } else {
4638             sec_event.ble_req.bd_name[0] = 0;
4639         }
4640         sec_event.ble_req.bd_name[BD_NAME_LEN] = 0;
4641         bta_dm_cb.p_sec_cback(BTA_DM_BLE_SEC_REQ_EVT, &sec_event);
4642         break;
4643 
4644     case BTM_LE_KEY_NOTIF_EVT:
4645         bdcpy(sec_event.key_notif.bd_addr, bda);
4646         p_name = BTM_SecReadDevName(bda);
4647         if (p_name != NULL) {
4648             BCM_STRNCPY_S((char *)sec_event.key_notif.bd_name, p_name, (BD_NAME_LEN));
4649         } else {
4650             sec_event.key_notif.bd_name[0] = 0;
4651         }
4652         sec_event.ble_req.bd_name[BD_NAME_LEN] = 0;
4653         sec_event.key_notif.passkey = p_data->key_notif;
4654         bta_dm_cb.p_sec_cback(BTA_DM_BLE_PASSKEY_NOTIF_EVT, &sec_event);
4655         break;
4656 
4657     case BTM_LE_KEY_REQ_EVT:
4658         bdcpy(sec_event.ble_req.bd_addr, bda);
4659         bta_dm_cb.p_sec_cback(BTA_DM_BLE_PASSKEY_REQ_EVT, &sec_event);
4660         break;
4661 
4662     case BTM_LE_OOB_REQ_EVT:
4663         bdcpy(sec_event.ble_req.bd_addr, bda);
4664         bta_dm_cb.p_sec_cback(BTA_DM_BLE_OOB_REQ_EVT, &sec_event);
4665         break;
4666 
4667     case BTM_LE_NC_REQ_EVT:
4668         bdcpy(sec_event.key_notif.bd_addr, bda);
4669         BCM_STRNCPY_S((char *)sec_event.key_notif.bd_name,bta_dm_get_remname(), (BD_NAME_LEN));
4670         sec_event.ble_req.bd_name[BD_NAME_LEN] = 0;
4671         sec_event.key_notif.passkey = p_data->key_notif;
4672         bta_dm_cb.p_sec_cback(BTA_DM_BLE_NC_REQ_EVT, &sec_event);
4673         break;
4674 
4675     case BTM_LE_KEY_EVT:
4676         bdcpy(sec_event.ble_key.bd_addr, bda);
4677         sec_event.ble_key.key_type = p_data->key.key_type;
4678         sec_event.ble_key.p_key_value = p_data->key.p_key_value;
4679         bta_dm_cb.p_sec_cback(BTA_DM_BLE_KEY_EVT, &sec_event);
4680         break;
4681 
4682     case BTM_LE_COMPLT_EVT:
4683         bdcpy(sec_event.auth_cmpl.bd_addr, bda);
4684 #if BLE_INCLUDED == TRUE
4685         BTM_ReadDevInfo(bda, &sec_event.auth_cmpl.dev_type, &sec_event.auth_cmpl.addr_type);
4686 #endif
4687         p_name = BTM_SecReadDevName(bda);
4688         if (p_name != NULL) {
4689             BCM_STRNCPY_S((char *)sec_event.auth_cmpl.bd_name, p_name, (BD_NAME_LEN));
4690         } else {
4691             sec_event.auth_cmpl.bd_name[0] = 0;
4692         }
4693         if (p_data->complt.reason != 0) {
4694             sec_event.auth_cmpl.fail_reason = BTA_DM_AUTH_CONVERT_SMP_CODE(((UINT8)p_data->complt.reason));
4695             /* delete this device entry from Sec Dev DB */
4696             bta_dm_remove_sec_dev_entry (bda);
4697         } else {
4698             sec_event.auth_cmpl.success = TRUE;
4699             if (!p_data->complt.smp_over_br) {
4700 
4701             }
4702         }
4703         sec_event.auth_cmpl.auth_mode = p_data->complt.auth_mode;
4704         if (bta_dm_cb.p_sec_cback) {
4705             //bta_dm_cb.p_sec_cback(BTA_DM_AUTH_CMPL_EVT, &sec_event);
4706             bta_dm_cb.p_sec_cback(BTA_DM_BLE_AUTH_CMPL_EVT, &sec_event);
4707         }
4708 
4709         break;
4710 
4711     default:
4712         status = BTM_NOT_AUTHORIZED;
4713         break;
4714     }
4715     return status;
4716 }
4717 
4718 /*******************************************************************************
4719 **
4720 ** Function         bta_dm_ble_id_key_cback
4721 **
4722 ** Description      Callback for BLE local ID keys
4723 **
4724 **
4725 ** Returns          void
4726 **
4727 *******************************************************************************/
bta_dm_ble_id_key_cback(UINT8 key_type,tBTM_BLE_LOCAL_KEYS * p_key)4728 static void bta_dm_ble_id_key_cback (UINT8 key_type, tBTM_BLE_LOCAL_KEYS *p_key)
4729 {
4730     UINT8   evt;
4731     tBTA_DM_SEC dm_key;
4732 
4733     switch (key_type) {
4734     case BTM_BLE_KEY_TYPE_ID:
4735     case BTM_BLE_KEY_TYPE_ER:
4736         if (bta_dm_cb.p_sec_cback) {
4737             memcpy(&dm_key.ble_id_keys, p_key, sizeof(tBTM_BLE_LOCAL_KEYS));
4738 
4739             evt = (key_type == BTM_BLE_KEY_TYPE_ID) ? BTA_DM_BLE_LOCAL_IR_EVT : \
4740                   BTA_DM_BLE_LOCAL_ER_EVT;
4741             bta_dm_cb.p_sec_cback(evt, &dm_key);
4742         }
4743         break;
4744 
4745     default:
4746         APPL_TRACE_DEBUG("Unknown key type %d", key_type);
4747         break;
4748     }
4749     return;
4750 
4751 }
4752 
4753 /*******************************************************************************
4754 **
4755 ** Function         bta_dm_add_blekey
4756 **
4757 ** Description      This function adds an BLE Key to an security database entry.
4758 **                  This function shall only be called AFTER BTA_DmAddBleDevice has been called.
4759 **                  It is normally called during host startup to restore all required information
4760 **                  stored in the NVRAM.
4761 **
4762 ** Parameters:
4763 **
4764 *******************************************************************************/
bta_dm_add_blekey(tBTA_DM_MSG * p_data)4765 void bta_dm_add_blekey (tBTA_DM_MSG *p_data)
4766 {
4767     if (!BTM_SecAddBleKey (p_data->add_ble_key.bd_addr,
4768                            (tBTM_LE_KEY_VALUE *)&p_data->add_ble_key.blekey,
4769                            p_data->add_ble_key.key_type)) {
4770         APPL_TRACE_ERROR ("BTA_DM: Error adding BLE Key for device %08x%04x",
4771                           (p_data->add_ble_key.bd_addr[0] << 24) + (p_data->add_ble_key.bd_addr[1] << 16) + \
4772                           (p_data->add_ble_key.bd_addr[2] << 8) + p_data->add_ble_key.bd_addr[3],
4773                           (p_data->add_ble_key.bd_addr[4] << 8) + p_data->add_ble_key.bd_addr[5]);
4774     }
4775 }
4776 
4777 /*******************************************************************************
4778 **
4779 ** Function         bta_dm_add_ble_device
4780 **
4781 ** Description      This function adds an BLE device to an security database entry.
4782 **                  It is normally called during host startup to restore all required information
4783 **                  stored in the NVRAM.
4784 **
4785 ** Parameters:
4786 **
4787 *******************************************************************************/
bta_dm_add_ble_device(tBTA_DM_MSG * p_data)4788 void bta_dm_add_ble_device (tBTA_DM_MSG *p_data)
4789 {
4790     if (!BTM_SecAddBleDevice (p_data->add_ble_device.bd_addr, NULL,
4791                               p_data->add_ble_device.dev_type  ,
4792                               p_data->add_ble_device.addr_type,
4793                               p_data->add_ble_device.auth_mode)) {
4794         APPL_TRACE_ERROR ("BTA_DM: Error adding BLE Device for device %08x%04x",
4795                           (p_data->add_ble_device.bd_addr[0] << 24) + (p_data->add_ble_device.bd_addr[1] << 16) + \
4796                           (p_data->add_ble_device.bd_addr[2] << 8) + p_data->add_ble_device.bd_addr[3],
4797                           (p_data->add_ble_device.bd_addr[4] << 8) + p_data->add_ble_device.bd_addr[5]);
4798     }
4799 }
4800 
4801 /*******************************************************************************
4802 **
4803 ** Function         bta_dm_add_ble_device
4804 **
4805 ** Description      This function adds an BLE device to an security database entry.
4806 **                  It is normally called during host startup to restore all required information
4807 **                  stored in the NVRAM.
4808 **
4809 ** Parameters:
4810 **
4811 *******************************************************************************/
bta_dm_ble_passkey_reply(tBTA_DM_MSG * p_data)4812 void bta_dm_ble_passkey_reply (tBTA_DM_MSG *p_data)
4813 {
4814     if (p_data->pin_reply.accept) {
4815         BTM_BlePasskeyReply(p_data->ble_passkey_reply.bd_addr, BTM_SUCCESS, p_data->ble_passkey_reply.passkey);
4816     } else {
4817         BTM_BlePasskeyReply(p_data->ble_passkey_reply.bd_addr, BTM_NOT_AUTHORIZED, p_data->ble_passkey_reply.passkey);
4818     }
4819 
4820 }
4821 
bta_dm_ble_set_static_passkey(tBTA_DM_MSG * p_data)4822 void bta_dm_ble_set_static_passkey(tBTA_DM_MSG *p_data)
4823 {
4824     BTM_BleSetStaticPasskey(p_data->ble_set_static_passkey.add, p_data->ble_set_static_passkey.static_passkey);
4825 }
4826 /*******************************************************************************
4827 **
4828 ** Function         bta_dm_ble_confirm_reply
4829 **
4830 ** Description      This is response to SM numeric comparison request submitted
4831 **                  to application.
4832 **
4833 ** Parameters:
4834 **
4835 *******************************************************************************/
bta_dm_ble_confirm_reply(tBTA_DM_MSG * p_data)4836 void bta_dm_ble_confirm_reply (tBTA_DM_MSG *p_data)
4837 {
4838     if (p_data->confirm.accept) {
4839         BTM_BleConfirmReply(p_data->confirm.bd_addr, BTM_SUCCESS);
4840     } else {
4841         BTM_BleConfirmReply(p_data->ble_passkey_reply.bd_addr, BTM_NOT_AUTHORIZED);
4842     }
4843 }
4844 
4845 /*******************************************************************************
4846 **
4847 ** Function         bta_dm_security_grant
4848 **
4849 ** Description      This function grant SMP security request access.
4850 **
4851 ** Parameters:
4852 **
4853 *******************************************************************************/
bta_dm_security_grant(tBTA_DM_MSG * p_data)4854 void bta_dm_security_grant (tBTA_DM_MSG *p_data)
4855 {
4856     BTM_SecurityGrant(p_data->ble_sec_grant.bd_addr, p_data->ble_sec_grant.res);
4857 }
4858 #endif  ///SMP_INCLUDED == TRUE
4859 
4860 /*******************************************************************************
4861 **
4862 ** Function         bta_dm_ble_set_bg_conn_type
4863 **
4864 ** Description      This function set the BLE background connection type
4865 **
4866 ** Parameters:
4867 **
4868 *******************************************************************************/
bta_dm_ble_set_bg_conn_type(tBTA_DM_MSG * p_data)4869 void bta_dm_ble_set_bg_conn_type (tBTA_DM_MSG *p_data)
4870 {
4871     BTM_BleSetBgConnType(p_data->ble_set_bd_conn_type.bg_conn_type,
4872                          p_data->ble_set_bd_conn_type.p_select_cback);
4873 }
4874 
4875 /*******************************************************************************
4876 **
4877 ** Function         bta_dm_ble_set_conn_params
4878 **
4879 ** Description      This function set the preferred connection parameters.
4880 **
4881 ** Parameters:
4882 **
4883 *******************************************************************************/
bta_dm_ble_set_conn_params(tBTA_DM_MSG * p_data)4884 void bta_dm_ble_set_conn_params (tBTA_DM_MSG *p_data)
4885 {
4886     BTM_BleSetPrefConnParams(p_data->ble_set_conn_params.peer_bda,
4887                              p_data->ble_set_conn_params.conn_int_min,
4888                              p_data->ble_set_conn_params.conn_int_max,
4889                              p_data->ble_set_conn_params.slave_latency,
4890                              p_data->ble_set_conn_params.supervision_tout);
4891 
4892     BTM_BleConfigConnParams(p_data->ble_set_conn_params.conn_int_min, p_data->ble_set_conn_params.conn_int_max,
4893             p_data->ble_set_conn_params.slave_latency, p_data->ble_set_conn_params.supervision_tout);
4894 }
4895 
4896 /*******************************************************************************
4897 **
4898 ** Function         bta_dm_ble_set_conn_scan_params
4899 **
4900 ** Description      This function sets BLE scan parameters.
4901 **
4902 ** Parameters:
4903 **
4904 *******************************************************************************/
bta_dm_ble_set_scan_params(tBTA_DM_MSG * p_data)4905 void bta_dm_ble_set_scan_params(tBTA_DM_MSG *p_data)
4906 {
4907     BTM_BleSetScanParams(p_data->ble_set_scan_params.client_if,
4908                          p_data->ble_set_scan_params.scan_int,
4909                          p_data->ble_set_scan_params.scan_window,
4910                          p_data->ble_set_scan_params.scan_mode,
4911                          p_data->ble_set_scan_params.scan_param_setup_cback);
4912 }
4913 
4914 /*******************************************************************************
4915 **
4916 ** Function         bta_dm_ble_set_scan_fil_params
4917 **
4918 ** Description      This function sets BLE scan filter & parameters.
4919 **
4920 ** Parameters:
4921 **
4922 *******************************************************************************/
bta_dm_ble_set_scan_fil_params(tBTA_DM_MSG * p_data)4923 void bta_dm_ble_set_scan_fil_params(tBTA_DM_MSG *p_data)
4924 {
4925     tBTA_STATUS status = BTA_FAILURE;
4926 
4927     if (BTM_BleSetScanFilterParams (p_data->ble_set_scan_fil_params.client_if,
4928                                 p_data->ble_set_scan_fil_params.scan_int,
4929                                 p_data->ble_set_scan_fil_params.scan_window,
4930                                 p_data->ble_set_scan_fil_params.scan_mode,
4931                                 p_data->ble_set_scan_fil_params.addr_type_own,
4932                                 p_data->ble_set_scan_fil_params.scan_duplicate_filter,
4933                                 p_data->ble_set_scan_fil_params.scan_filter_policy,
4934                                 p_data->ble_set_scan_fil_params.scan_param_setup_cback) == BTM_SUCCESS) {
4935         status = BTA_SUCCESS;
4936 
4937     } else {
4938         APPL_TRACE_ERROR("%s(), fail to set scan params.", __func__);
4939     }
4940     if (p_data->ble_set_scan_fil_params.scan_param_setup_cback != NULL) {
4941         p_data->ble_set_scan_fil_params.scan_param_setup_cback(p_data->ble_set_scan_fil_params.client_if, status);
4942     }
4943 
4944 }
4945 
4946 
4947 /*******************************************************************************
4948 **
4949 ** Function         bta_dm_ble_set_conn_scan_params
4950 **
4951 ** Description      This function set the preferred connection scan parameters.
4952 **
4953 ** Parameters:
4954 **
4955 *******************************************************************************/
bta_dm_ble_set_conn_scan_params(tBTA_DM_MSG * p_data)4956 void bta_dm_ble_set_conn_scan_params (tBTA_DM_MSG *p_data)
4957 {
4958     BTM_BleSetConnScanParams(p_data->ble_set_conn_scan_params.scan_int,
4959                              p_data->ble_set_conn_scan_params.scan_window);
4960 }
4961 /*******************************************************************************
4962 **
4963 ** Function         bta_dm_ble_update_conn_params
4964 **
4965 ** Description      This function update LE connection parameters.
4966 **
4967 ** Parameters:
4968 **
4969 *******************************************************************************/
bta_dm_ble_update_conn_params(tBTA_DM_MSG * p_data)4970 void bta_dm_ble_update_conn_params (tBTA_DM_MSG *p_data)
4971 {
4972     if (!L2CA_UpdateBleConnParams(p_data->ble_update_conn_params.bd_addr,
4973                                   p_data->ble_update_conn_params.min_int,
4974                                   p_data->ble_update_conn_params.max_int,
4975                                   p_data->ble_update_conn_params.latency,
4976                                   p_data->ble_update_conn_params.timeout)) {
4977         APPL_TRACE_ERROR("Update connection parameters failed!");
4978     } else {
4979         BTM_BleConfigConnParams(p_data->ble_update_conn_params.min_int, p_data->ble_update_conn_params.max_int,
4980             p_data->ble_update_conn_params.latency, p_data->ble_update_conn_params.timeout);
4981     }
4982 }
4983 /*******************************************************************************
4984 **
4985 ** Function         bta_dm_ble_disconnect
4986 **
4987 ** Description      This function disconnect the ble connection.
4988 **
4989 ** Parameters:
4990 **
4991 *******************************************************************************/
bta_dm_ble_disconnect(tBTA_DM_MSG * p_data)4992 void bta_dm_ble_disconnect (tBTA_DM_MSG *p_data)
4993 {
4994     L2CA_RemoveFixedChnl(L2CAP_ATT_CID, p_data->ble_disconnect.remote_bda);
4995 }
4996 
4997 /*******************************************************************************
4998 **
4999 ** Function         bta_dm_ble_set_rand_address
5000 **
5001 ** Description      This function set the LE random address for the device.
5002 **
5003 ** Parameters:      rand_addr:the random address whitch should be setting
5004 ** Explanation:     This function added by Yulong at 2016/9/9
5005 *******************************************************************************/
bta_dm_ble_set_rand_address(tBTA_DM_MSG * p_data)5006 void bta_dm_ble_set_rand_address(tBTA_DM_MSG *p_data)
5007 {
5008     tBTM_STATUS status = BTM_SET_STATIC_RAND_ADDR_FAIL;
5009     if (p_data->set_addr.addr_type != BLE_ADDR_RANDOM) {
5010         APPL_TRACE_ERROR("Invalid random adress type = %d\n", p_data->set_addr.addr_type);
5011         if(p_data->set_addr.p_set_rand_addr_cback) {
5012             (*p_data->set_addr.p_set_rand_addr_cback)(status);
5013         }
5014         return;
5015     }
5016     //send the setting random address to BTM layer
5017     status = BTM_BleSetRandAddress(p_data->set_addr.address);
5018     if(p_data->set_addr.p_set_rand_addr_cback) {
5019         (*p_data->set_addr.p_set_rand_addr_cback)(status);
5020     }
5021 
5022 }
5023 
bta_dm_ble_clear_rand_address(tBTA_DM_MSG * p_data)5024 void bta_dm_ble_clear_rand_address(tBTA_DM_MSG *p_data)
5025 {
5026     UNUSED(p_data);
5027     BTM_BleClearRandAddress();
5028 }
5029 
5030 /*******************************************************************************
5031 **
5032 ** Function         bta_dm_ble_stop_advertising
5033 **
5034 ** Description      This function stop the BLE avdertising for the device.
5035 **
5036 ** Parameters:      void
5037 ** Explanation:     This function added by Yulong at 2016/10/19
5038 *******************************************************************************/
bta_dm_ble_stop_advertising(tBTA_DM_MSG * p_data)5039 void bta_dm_ble_stop_advertising(tBTA_DM_MSG *p_data)
5040 {
5041     if (p_data->hdr.event != BTA_DM_API_BLE_STOP_ADV_EVT) {
5042         APPL_TRACE_ERROR("Invalid BTA event,can't stop the BLE adverting\n");
5043     }
5044 
5045     btm_ble_stop_adv();
5046 }
5047 
5048 
5049 
5050 #if BLE_PRIVACY_SPT == TRUE
5051 /*******************************************************************************
5052 **
5053 ** Function         bta_dm_ble_config_local_privacy
5054 **
5055 ** Description      This function set the local device LE privacy settings.
5056 **
5057 ** Parameters:
5058 **
5059 *******************************************************************************/
bta_dm_ble_config_local_privacy(tBTA_DM_MSG * p_data)5060 void bta_dm_ble_config_local_privacy (tBTA_DM_MSG *p_data)
5061 {
5062     BTM_BleConfigPrivacy (p_data->ble_local_privacy.privacy_enable, p_data->ble_local_privacy.set_local_privacy_cback);
5063 }
5064 #endif
5065 
5066 /*******************************************************************************
5067 **
5068 ** Function         bta_dm_ble_config_local_icon
5069 **
5070 ** Description      This function sets the local icon value.
5071 **
5072 **
5073 *******************************************************************************/
bta_dm_ble_config_local_icon(tBTA_DM_MSG * p_data)5074 void bta_dm_ble_config_local_icon (tBTA_DM_MSG *p_data)
5075 {
5076     BTM_BleConfigLocalIcon (p_data->ble_local_icon.icon);
5077 }
5078 
5079 /*******************************************************************************
5080 **
5081 ** Function         bta_dm_ble_observe
5082 **
5083 ** Description      This function set the preferred connection scan parameters.
5084 **
5085 ** Parameters:
5086 **
5087 *******************************************************************************/
bta_dm_ble_observe(tBTA_DM_MSG * p_data)5088 void bta_dm_ble_observe (tBTA_DM_MSG *p_data)
5089 {
5090     tBTM_STATUS status;
5091     if (p_data->ble_observe.start) {
5092         /*Save the  callback to be called when a scan results are available */
5093         bta_dm_search_cb.p_scan_cback = p_data->ble_observe.p_cback;
5094 
5095         if ((status = BTM_BleObserve(TRUE, p_data->ble_observe.duration,
5096                                      bta_dm_observe_results_cb, bta_dm_observe_cmpl_cb)) != BTM_CMD_STARTED) {
5097             APPL_TRACE_WARNING(" %s start observe failed. status=0x%x\n", __FUNCTION__, status);
5098         }
5099 
5100         if (p_data->ble_observe.p_start_scan_cback) {
5101             status = (status == BTM_CMD_STARTED ? BTA_SUCCESS : BTA_FAILURE);
5102             p_data->ble_observe.p_start_scan_cback(status);
5103         }
5104     } else {
5105         bta_dm_search_cb.p_scan_cback = NULL;
5106         status = BTM_BleObserve(FALSE, 0, NULL, NULL);
5107 
5108         if (status != BTM_CMD_STARTED){
5109             APPL_TRACE_WARNING(" %s stop observe failed, status=0x%x\n", __FUNCTION__, status);
5110         }
5111 
5112         if (p_data->ble_observe.p_stop_scan_cback) {
5113             status = (status == BTM_CMD_STARTED ? BTA_SUCCESS : BTA_FAILURE);
5114             p_data->ble_observe.p_stop_scan_cback(status);
5115         }
5116     }
5117 }
5118 
5119 /*******************************************************************************
5120 **
5121 ** Function         bta_dm_ble_scan
5122 **
5123 ** Description      This function set the preferred connection scan parameters.
5124 **
5125 ** Parameters:
5126 **
5127 *******************************************************************************/
bta_dm_ble_scan(tBTA_DM_MSG * p_data)5128 void bta_dm_ble_scan (tBTA_DM_MSG *p_data)
5129 {
5130     tBTM_STATUS status;
5131     if (p_data->ble_scan.start) {
5132         /*Save the  callback to be called when a scan results are available */
5133         bta_dm_search_cb.p_scan_cback = p_data->ble_scan.p_cback;
5134 
5135         if ((status = BTM_BleScan(TRUE, p_data->ble_scan.duration,
5136                                      bta_dm_observe_results_cb, bta_dm_observe_cmpl_cb, bta_dm_observe_discard_cb)) != BTM_CMD_STARTED) {
5137             APPL_TRACE_WARNING(" %s start scan failed. status=0x%x\n", __FUNCTION__, status);
5138         }
5139 
5140         if (p_data->ble_scan.p_start_scan_cback) {
5141             status = (status == BTM_CMD_STARTED ? BTA_SUCCESS : BTA_FAILURE);
5142             p_data->ble_scan.p_start_scan_cback(status);
5143         }
5144     } else {
5145         bta_dm_search_cb.p_scan_cback = NULL;
5146         status = BTM_BleScan(FALSE, 0, NULL, NULL, NULL);
5147 
5148         if (status != BTM_CMD_STARTED){
5149             APPL_TRACE_WARNING(" %s stop scan failed, status=0x%x\n", __FUNCTION__, status);
5150         }
5151 
5152         if (p_data->ble_scan.p_stop_scan_cback) {
5153             status = (status == BTM_CMD_STARTED ? BTA_SUCCESS : BTA_FAILURE);
5154             p_data->ble_scan.p_stop_scan_cback(status);
5155         }
5156     }
5157 }
5158 
5159 /*******************************************************************************
5160 **
5161 ** Function         bta_dm_ble_set_adv_params
5162 **
5163 ** Description      This function set the adv parameters.
5164 **
5165 ** Parameters:
5166 **
5167 *******************************************************************************/
bta_dm_ble_set_adv_params(tBTA_DM_MSG * p_data)5168 void bta_dm_ble_set_adv_params (tBTA_DM_MSG *p_data)
5169 {
5170     BTM_BleSetAdvParams(p_data->ble_set_adv_params.adv_int_min,
5171                         p_data->ble_set_adv_params.adv_int_max,
5172                         p_data->ble_set_adv_params.p_dir_bda,
5173                         BTA_DM_BLE_ADV_CHNL_MAP);
5174 }
5175 
5176 /*******************************************************************************
5177 **
5178 ** Function         bta_dm_ble_set_adv_params_all
5179 **
5180 ** Description      This function is called to set all of the advertising parameters.
5181 **
5182 ** Parameters:       None.
5183 **
5184 ** Returns          void
5185 **
5186 *******************************************************************************/
bta_dm_ble_set_adv_params_all(tBTA_DM_MSG * p_data)5187 void bta_dm_ble_set_adv_params_all  (tBTA_DM_MSG *p_data)
5188 {
5189     tBTA_STATUS status = BTA_FAILURE;
5190     if (BTM_BleSetAdvParamsAll(p_data->ble_set_adv_params_all.adv_int_min,
5191                                 p_data->ble_set_adv_params_all.adv_int_max,
5192                                 p_data->ble_set_adv_params_all.adv_type,
5193                                 p_data->ble_set_adv_params_all.addr_type_own,
5194                                 p_data->ble_set_adv_params_all.p_dir_bda,
5195                                 p_data->ble_set_adv_params_all.channel_map,
5196                                 p_data->ble_set_adv_params_all.adv_filter_policy,
5197                                 p_data->ble_set_adv_params_all.p_start_adv_cback) == BTM_SUCCESS) {
5198         APPL_TRACE_DEBUG("%s(), success to set ble adv params.", __func__);
5199     } else {
5200         APPL_TRACE_ERROR("%s(), fail to set ble adv params.", __func__);
5201         if(p_data->ble_set_adv_params_all.p_start_adv_cback) {
5202             (*p_data->ble_set_adv_params_all.p_start_adv_cback)(status);
5203         }
5204         return;
5205     }
5206     if(BTM_BleStartAdv() == BTM_SUCCESS) {
5207         status = BTA_SUCCESS;
5208     }
5209     if(p_data->ble_set_adv_params_all.p_start_adv_cback) {
5210         (*p_data->ble_set_adv_params_all.p_start_adv_cback)(status);
5211     }
5212 }
5213 
5214 /*******************************************************************************
5215 **
5216 ** Function         bta_dm_ble_update_duplicate_exceptional_list
5217 **
5218 ** Description      This function is to update duplicate scan exceptional list
5219 **
5220 **
5221 *******************************************************************************/
bta_dm_ble_update_duplicate_exceptional_list(tBTA_DM_MSG * p_data)5222 void bta_dm_ble_update_duplicate_exceptional_list(tBTA_DM_MSG *p_data)
5223 {
5224     BTM_UpdateBleDuplicateExceptionalList(p_data->ble_duplicate_exceptional_list.subcode,
5225                                           p_data->ble_duplicate_exceptional_list.type,
5226                                           p_data->ble_duplicate_exceptional_list.device_info,
5227                                           p_data->ble_duplicate_exceptional_list.exceptional_list_cb);
5228 }
5229 
5230 /*******************************************************************************
5231 **
5232 ** Function         bta_dm_ble_set_adv_config
5233 **
5234 ** Description      This function set the customized ADV data configuration
5235 **
5236 ** Parameters:
5237 **
5238 *******************************************************************************/
bta_dm_ble_set_adv_config(tBTA_DM_MSG * p_data)5239 void bta_dm_ble_set_adv_config (tBTA_DM_MSG *p_data)
5240 {
5241     tBTA_STATUS status = BTA_FAILURE;
5242 
5243     if (BTM_BleWriteAdvData(p_data->ble_set_adv_data.data_mask,
5244                             (tBTM_BLE_ADV_DATA *)p_data->ble_set_adv_data.p_adv_cfg) == BTM_SUCCESS) {
5245         status = BTA_SUCCESS;
5246     }
5247 
5248     if (p_data->ble_set_adv_data.p_adv_data_cback) {
5249         (*p_data->ble_set_adv_data.p_adv_data_cback)(status);
5250     }
5251 }
5252 
5253 /*******************************************************************************
5254 **
5255 ** Function         bta_dm_ble_set_long_adv
5256 **
5257 ** Description      This function set the long ADV data
5258 **
5259 ** Parameters:
5260 **
5261 *******************************************************************************/
bta_dm_ble_set_long_adv(tBTA_DM_MSG * p_data)5262 void bta_dm_ble_set_long_adv (tBTA_DM_MSG *p_data)
5263 {
5264     tBTA_STATUS status = BTA_FAILURE;
5265 
5266     if (BTM_BleWriteLongAdvData(p_data->ble_set_long_adv_data.adv_data,
5267                                 p_data->ble_set_long_adv_data.adv_data_len) == BTM_SUCCESS) {
5268         status = BTA_SUCCESS;
5269     }
5270 
5271     if (p_data->ble_set_adv_data.p_adv_data_cback) {
5272         (*p_data->ble_set_adv_data.p_adv_data_cback)(status);
5273     }
5274 }
5275 
5276 /*******************************************************************************
5277 **
5278 ** Function         bta_dm_ble_set_adv_config_raw
5279 **
5280 ** Description      This function set the customized ADV data configuration
5281 **
5282 ** Parameters:
5283 **
5284 *******************************************************************************/
bta_dm_ble_set_adv_config_raw(tBTA_DM_MSG * p_data)5285 void bta_dm_ble_set_adv_config_raw (tBTA_DM_MSG *p_data)
5286 {
5287     tBTA_STATUS status = BTA_FAILURE;
5288 
5289     if (BTM_BleWriteAdvDataRaw(p_data->ble_set_adv_data_raw.p_raw_adv,
5290                                p_data->ble_set_adv_data_raw.raw_adv_len) == BTM_SUCCESS) {
5291         status = BTA_SUCCESS;
5292     }
5293 
5294     if (p_data->ble_set_adv_data_raw.p_adv_data_cback) {
5295         (*p_data->ble_set_adv_data_raw.p_adv_data_cback)(status);
5296     }
5297 }
5298 
5299 
5300 /*******************************************************************************
5301 **
5302 ** Function         bta_dm_ble_set_scan_rsp
5303 **
5304 ** Description      This function set the customized ADV scan resp. configuration
5305 **
5306 ** Parameters:
5307 **
5308 *******************************************************************************/
bta_dm_ble_set_scan_rsp(tBTA_DM_MSG * p_data)5309 void bta_dm_ble_set_scan_rsp (tBTA_DM_MSG *p_data)
5310 {
5311     tBTA_STATUS status = BTA_FAILURE;
5312 
5313     if (BTM_BleWriteScanRsp(p_data->ble_set_adv_data.data_mask,
5314                             (tBTM_BLE_ADV_DATA *)p_data->ble_set_adv_data.p_adv_cfg) == BTM_SUCCESS) {
5315         status = BTA_SUCCESS;
5316     }
5317 
5318     if (p_data->ble_set_adv_data.p_adv_data_cback) {
5319         (*p_data->ble_set_adv_data.p_adv_data_cback)(status);
5320     }
5321 }
5322 
5323 /*******************************************************************************
5324 **
5325 ** Function         bta_dm_ble_set_scan_rsp_raw
5326 **
5327 ** Description      This function set the raw scan response data
5328 **
5329 ** Parameters:
5330 **
5331 *******************************************************************************/
bta_dm_ble_set_scan_rsp_raw(tBTA_DM_MSG * p_data)5332 void bta_dm_ble_set_scan_rsp_raw (tBTA_DM_MSG *p_data)
5333 {
5334     tBTA_STATUS status = BTA_FAILURE;
5335 
5336     if (BTM_BleWriteScanRspRaw(p_data->ble_set_adv_data_raw.p_raw_adv,
5337                                p_data->ble_set_adv_data_raw.raw_adv_len) == BTM_SUCCESS) {
5338         status = BTA_SUCCESS;
5339     }
5340 
5341     if (p_data->ble_set_adv_data_raw.p_adv_data_cback) {
5342         (*p_data->ble_set_adv_data_raw.p_adv_data_cback)(status);
5343     }
5344 }
5345 
5346 /*******************************************************************************
5347 **
5348 ** Function         bta_dm_ble_set_data_length
5349 **
5350 ** Description      This function set the maximum transmission packet size
5351 **
5352 ** Parameters
5353 **
5354 *******************************************************************************/
bta_dm_ble_set_data_length(tBTA_DM_MSG * p_data)5355 void bta_dm_ble_set_data_length(tBTA_DM_MSG *p_data)
5356 {
5357     tACL_CONN *p_acl_cb = btm_bda_to_acl(p_data->ble_set_data_length.remote_bda, BT_TRANSPORT_LE);
5358     if (p_acl_cb == NULL) {
5359         APPL_TRACE_ERROR("%s error: Invalid connection remote_bda.", __func__);
5360         return;
5361     } else {
5362         p_acl_cb->p_set_pkt_data_cback = p_data->ble_set_data_length.p_set_pkt_data_cback;
5363     }
5364     UINT8 status = BTM_SetBleDataLength(p_data->ble_set_data_length.remote_bda,
5365                                         p_data->ble_set_data_length.tx_data_length);
5366     if (status != BTM_SUCCESS) {
5367         APPL_TRACE_ERROR("%s failed\n", __FUNCTION__);
5368     }
5369     if (p_data->ble_set_data_length.p_set_pkt_data_cback) {
5370         if (p_acl_cb->data_length_params.tx_len == 0){
5371             uint16_t length = controller_get_interface()->get_acl_data_size_ble();
5372             p_acl_cb->data_length_params.rx_len = length;
5373             p_acl_cb->data_length_params.tx_len = length;
5374         }
5375         (*p_data->ble_set_data_length.p_set_pkt_data_cback)(status, &p_acl_cb->data_length_params);
5376     }
5377 
5378 }
5379 
5380 /*******************************************************************************
5381 **
5382 ** Function         bta_dm_ble_broadcast
5383 **
5384 ** Description      Starts or stops LE broadcasts
5385 **
5386 ** Parameters:
5387 **
5388 *******************************************************************************/
bta_dm_ble_broadcast(tBTA_DM_MSG * p_data)5389 void bta_dm_ble_broadcast (tBTA_DM_MSG *p_data)
5390 {
5391     tBTA_STATUS status = BTA_FAILURE;
5392     BOOLEAN start = p_data->ble_observe.start;
5393 
5394     if (BTM_BleBroadcast(start, p_data->ble_observe.p_stop_adv_cback) == BTM_SUCCESS) {
5395         status = BTA_SUCCESS;
5396     } else {
5397         APPL_TRACE_ERROR("%s failed\n", __FUNCTION__);
5398     }
5399 
5400     if (p_data->ble_observe.p_stop_adv_cback){
5401         (*p_data->ble_observe.p_stop_adv_cback)(status);
5402     }
5403 
5404 }
5405 
5406 /*******************************************************************************
5407 **
5408 ** Function         bta_dm_ble_multi_adv_enb
5409 **
5410 ** Description      This function enables a single advertising instance
5411 **
5412 ** Parameters:
5413 **
5414 *******************************************************************************/
bta_dm_ble_multi_adv_enb(tBTA_DM_MSG * p_data)5415 void bta_dm_ble_multi_adv_enb(tBTA_DM_MSG *p_data)
5416 {
5417     tBTM_STATUS btm_status = 0;
5418 
5419     bta_dm_cb.p_multi_adv_cback = p_data->ble_multi_adv_enb.p_cback;
5420     if (BTM_BleMaxMultiAdvInstanceCount() > 0 && NULL != p_data->ble_multi_adv_enb.p_ref) {
5421         btm_status = BTM_BleEnableAdvInstance((tBTM_BLE_ADV_PARAMS *)
5422                                               p_data->ble_multi_adv_enb.p_params,
5423                                               p_data->ble_multi_adv_enb.p_cback,
5424                                               p_data->ble_multi_adv_enb.p_ref);
5425     }
5426 
5427     if (BTM_CMD_STARTED != btm_status) {
5428         bta_dm_cb.p_multi_adv_cback(BTA_BLE_MULTI_ADV_ENB_EVT, 0xFF,
5429                                     p_data->ble_multi_adv_enb.p_ref, BTA_FAILURE);
5430     }
5431 }
5432 /*******************************************************************************
5433 **
5434 ** Function         bta_dm_ble_multi_adv_param_upd
5435 **
5436 ** Description      This function updates multiple advertising instance parameters
5437 **
5438 ** Parameters:
5439 **
5440 *******************************************************************************/
bta_dm_ble_multi_adv_upd_param(tBTA_DM_MSG * p_data)5441 void bta_dm_ble_multi_adv_upd_param(tBTA_DM_MSG *p_data)
5442 {
5443     tBTM_STATUS btm_status = 0;
5444     void *p_ref = NULL;
5445 
5446     if (BTM_BleMaxMultiAdvInstanceCount() > 0 && p_data->ble_multi_adv_param.inst_id > 0
5447             && p_data->ble_multi_adv_param.inst_id < BTM_BleMaxMultiAdvInstanceCount()) {
5448         btm_status = BTM_BleUpdateAdvInstParam(p_data->ble_multi_adv_param.inst_id,
5449                                                (tBTM_BLE_ADV_PARAMS *)p_data->ble_multi_adv_param.p_params);
5450     }
5451 
5452     if (BTM_CMD_STARTED != btm_status) {
5453         p_ref = btm_ble_multi_adv_get_ref(p_data->ble_multi_adv_param.inst_id);
5454         bta_dm_cb.p_multi_adv_cback(BTA_BLE_MULTI_ADV_PARAM_EVT,
5455                                     p_data->ble_multi_adv_param.inst_id, p_ref, BTA_FAILURE);
5456     }
5457 }
5458 /*******************************************************************************
5459 **
5460 ** Function         bta_dm_ble_multi_adv_data
5461 **
5462 ** Description      This function write multiple advertising instance adv data
5463 **                  or scan response data
5464 **
5465 ** Parameters:
5466 **
5467 *******************************************************************************/
bta_dm_ble_multi_adv_data(tBTA_DM_MSG * p_data)5468 void bta_dm_ble_multi_adv_data(tBTA_DM_MSG *p_data)
5469 {
5470     tBTM_STATUS btm_status = 0;
5471     void *p_ref = NULL;
5472 
5473     if (BTM_BleMaxMultiAdvInstanceCount() > 0 && p_data->ble_multi_adv_data.inst_id > 0
5474             && p_data->ble_multi_adv_data.inst_id < BTM_BleMaxMultiAdvInstanceCount()) {
5475         btm_status = BTM_BleCfgAdvInstData(p_data->ble_multi_adv_data.inst_id,
5476                                            p_data->ble_multi_adv_data.is_scan_rsp,
5477                                            p_data->ble_multi_adv_data.data_mask,
5478                                            (tBTM_BLE_ADV_DATA *)p_data->ble_multi_adv_data.p_data);
5479     }
5480 
5481     if (BTM_CMD_STARTED != btm_status) {
5482         p_ref = btm_ble_multi_adv_get_ref(p_data->ble_multi_adv_data.inst_id);
5483         bta_dm_cb.p_multi_adv_cback(BTA_BLE_MULTI_ADV_DATA_EVT,
5484                                     p_data->ble_multi_adv_data.inst_id, p_ref, BTA_FAILURE);
5485     }
5486 
5487 }
5488 /*******************************************************************************
5489 **
5490 ** Function         btm_dm_ble_multi_adv_disable
5491 **
5492 ** Description      This function disable a single adv instance
5493 **
5494 ** Parameters:
5495 **
5496 *******************************************************************************/
btm_dm_ble_multi_adv_disable(tBTA_DM_MSG * p_data)5497 void btm_dm_ble_multi_adv_disable(tBTA_DM_MSG *p_data)
5498 {
5499     tBTM_STATUS btm_status = 0;
5500     void *p_ref = NULL;
5501 
5502     if (BTM_BleMaxMultiAdvInstanceCount() > 0 && p_data->ble_multi_adv_disable.inst_id > 0
5503             && p_data->ble_multi_adv_disable.inst_id < BTM_BleMaxMultiAdvInstanceCount()) {
5504         btm_status = BTM_BleDisableAdvInstance(p_data->ble_multi_adv_disable.inst_id);
5505     }
5506 
5507     if (BTM_CMD_STARTED != btm_status) {
5508         p_ref = btm_ble_multi_adv_get_ref(p_data->ble_multi_adv_disable.inst_id);
5509         bta_dm_cb.p_multi_adv_cback(BTA_BLE_MULTI_ADV_DISABLE_EVT,
5510                                     p_data->ble_multi_adv_disable.inst_id, p_ref, BTA_FAILURE);
5511     }
5512 }
5513 #if (BLE_50_FEATURE_SUPPORT == TRUE)
bta_dm_ble_gap_read_phy(tBTA_DM_MSG * p_data)5514 void bta_dm_ble_gap_read_phy(tBTA_DM_MSG *p_data)
5515 {
5516     //tBTM_STATUS btm_status = 0;
5517     UINT8 tx_phy = 0, rx_phy = 0;
5518 
5519     BTM_BleReadPhy(p_data->ble_read_phy.bd_addr, &tx_phy, &rx_phy);
5520 
5521 }
5522 
bta_dm_ble_gap_set_prefer_default_phy(tBTA_DM_MSG * p_data)5523 void bta_dm_ble_gap_set_prefer_default_phy(tBTA_DM_MSG *p_data)
5524 {
5525     APPL_TRACE_API("%s, tx_phy_mask = %d, rx_phy_mask = %d", __func__, p_data->ble_set_per_def_phy.tx_phy_mask, p_data->ble_set_per_def_phy.rx_phy_mask);
5526     BTM_BleSetPreferDefaultPhy(p_data->ble_set_per_def_phy.tx_phy_mask, p_data->ble_set_per_def_phy.rx_phy_mask);
5527 }
5528 
bta_dm_ble_gap_set_prefer_phy(tBTA_DM_MSG * p_data)5529 void bta_dm_ble_gap_set_prefer_phy(tBTA_DM_MSG *p_data)
5530 {
5531     APPL_TRACE_API("%s, tx_phy_mask = %d, rx_phy_mask = %d, phy_options = %d",
5532                      __func__, p_data->ble_set_per_phy.tx_phy_mask,
5533                      p_data->ble_set_per_phy.rx_phy_mask, p_data->ble_set_per_phy.phy_options);
5534     BTM_BleSetPreferPhy(p_data->ble_set_per_phy.bd_addr, p_data->ble_set_per_phy.all_phys,
5535                         p_data->ble_set_per_phy.tx_phy_mask, p_data->ble_set_per_phy.rx_phy_mask,
5536                         p_data->ble_set_per_phy.phy_options);
5537 }
5538 
bta_dm_ble_gap_ext_adv_set_rand_addr(tBTA_DM_MSG * p_data)5539 void bta_dm_ble_gap_ext_adv_set_rand_addr(tBTA_DM_MSG *p_data)
5540 {
5541     BTM_BleSetExtendedAdvRandaddr(p_data->ble_set_ext_adv_rand_addr.instance, p_data->ble_set_ext_adv_rand_addr.rand_addr);
5542 }
bta_dm_ble_gap_ext_adv_set_params(tBTA_DM_MSG * p_data)5543 void bta_dm_ble_gap_ext_adv_set_params(tBTA_DM_MSG *p_data)
5544 {
5545     APPL_TRACE_API("%s, instance = %d", __func__, p_data->ble_set_ext_adv_params.instance);
5546 
5547     BTM_BleSetExtendedAdvParams(p_data->ble_set_ext_adv_params.instance,
5548                                 (tBTM_BLE_GAP_EXT_ADV_PARAMS *)&p_data->ble_set_ext_adv_params.params);
5549 }
5550 
bta_dm_ble_gap_config_ext_adv_data_raw(tBTA_DM_MSG * p_data)5551 void bta_dm_ble_gap_config_ext_adv_data_raw(tBTA_DM_MSG *p_data)
5552 {
5553     APPL_TRACE_API("%s, instance = %d, len = %d", __func__, p_data->ble_cfg_ext_adv_data.instance,
5554          p_data->ble_cfg_ext_adv_data.length);
5555 
5556     BTM_BleConfigExtendedAdvDataRaw(p_data->ble_cfg_ext_adv_data.is_scan_rsp,
5557                                    p_data->ble_cfg_ext_adv_data.instance,
5558                                    p_data->ble_cfg_ext_adv_data.length,
5559                                    p_data->ble_cfg_ext_adv_data.data);
5560 }
5561 
bta_dm_ble_gap_start_ext_adv(tBTA_DM_MSG * p_data)5562 void bta_dm_ble_gap_start_ext_adv(tBTA_DM_MSG *p_data)
5563 {
5564     APPL_TRACE_API("%s, enable = %d, num = %d", __func__, p_data->ble_start_ext_adv.enable,
5565              p_data->ble_start_ext_adv.num);
5566     BTM_BleStartExtAdv(p_data->ble_start_ext_adv.enable, p_data->ble_start_ext_adv.num,
5567                        (tBTM_BLE_EXT_ADV *)p_data->ble_start_ext_adv.ext_adv);
5568 }
5569 
bta_dm_ble_gap_ext_adv_set_remove(tBTA_DM_MSG * p_data)5570 void bta_dm_ble_gap_ext_adv_set_remove(tBTA_DM_MSG *p_data)
5571 {
5572     BTM_BleExtAdvSetRemove(p_data->ble_ext_adv_set_remove.instance);
5573 }
5574 
bta_dm_ble_gap_ext_adv_set_clear(tBTA_DM_MSG * p_data)5575 void bta_dm_ble_gap_ext_adv_set_clear(tBTA_DM_MSG *p_data)
5576 {
5577     BTM_BleExtAdvSetClear();
5578 }
5579 
bta_dm_ble_gap_periodic_adv_set_params(tBTA_DM_MSG * p_data)5580 void bta_dm_ble_gap_periodic_adv_set_params(tBTA_DM_MSG *p_data)
5581 {
5582     APPL_TRACE_API("%s, instance = %d", __func__, p_data->ble_set_periodic_adv_params.instance);
5583     BTM_BlePeriodicAdvSetParams(p_data->ble_set_periodic_adv_params.instance,
5584                                (tBTM_BLE_Periodic_Adv_Params *)&p_data->ble_set_periodic_adv_params.params);
5585 }
5586 
bta_dm_ble_gap_periodic_adv_cfg_data_raw(tBTA_DM_MSG * p_data)5587 void bta_dm_ble_gap_periodic_adv_cfg_data_raw(tBTA_DM_MSG *p_data)
5588 {
5589     APPL_TRACE_API("%s, instance = %d, length = %d", __func__, p_data->ble_cfg_periodic_adv_data.instance,
5590                      p_data->ble_cfg_periodic_adv_data.length);
5591 
5592     BTM_BlePeriodicAdvCfgDataRaw(p_data->ble_cfg_periodic_adv_data.instance,
5593                                  p_data->ble_cfg_periodic_adv_data.length,
5594                                  p_data->ble_cfg_periodic_adv_data.data);
5595 }
5596 
bta_dm_ble_gap_periodic_adv_enable(tBTA_DM_MSG * p_data)5597 void bta_dm_ble_gap_periodic_adv_enable(tBTA_DM_MSG *p_data)
5598 {
5599     APPL_TRACE_API("%s, instance = %d, enable = %d", __func__, p_data->ble_enable_periodic_adv.instance,
5600                          p_data->ble_enable_periodic_adv.enable);
5601 
5602     BTM_BlePeriodicAdvEnable(p_data->ble_enable_periodic_adv.instance,
5603                              p_data->ble_enable_periodic_adv.enable);
5604 }
5605 
bta_dm_ble_gap_periodic_adv_create_sync(tBTA_DM_MSG * p_data)5606 void bta_dm_ble_gap_periodic_adv_create_sync(tBTA_DM_MSG *p_data)
5607 {
5608     APPL_TRACE_API("%s", __func__);
5609     BTM_BlePeriodicAdvCreateSync((tBTM_BLE_Periodic_Sync_Params *)&p_data->ble_periodic_adv_sync.params);
5610 }
5611 
bta_dm_ble_gap_periodic_adv_sync_cancel(tBTA_DM_MSG * p_data)5612 void bta_dm_ble_gap_periodic_adv_sync_cancel(tBTA_DM_MSG *p_data)
5613 {
5614     APPL_TRACE_API("%s", __func__);
5615     BTM_BlePeriodicAdvSyncCancel();
5616 }
5617 
bta_dm_ble_gap_periodic_adv_sync_terminate(tBTA_DM_MSG * p_data)5618 void bta_dm_ble_gap_periodic_adv_sync_terminate(tBTA_DM_MSG *p_data)
5619 {
5620     APPL_TRACE_API("%s", __func__);
5621     BTM_BlePeriodicAdvSyncTerm(p_data->ble_periodic_adv_sync_term.sync_handle);
5622 }
5623 
bta_dm_ble_gap_periodic_adv_add_dev_to_list(tBTA_DM_MSG * p_data)5624 void bta_dm_ble_gap_periodic_adv_add_dev_to_list(tBTA_DM_MSG *p_data)
5625 {
5626     APPL_TRACE_API("%s, addr_type = %d, sid = %d", __func__, p_data->ble_periodic_adv_add_dev_to_list.addr_type,
5627                     p_data->ble_periodic_adv_add_dev_to_list.sid);
5628 
5629     BTM_BlePeriodicAdvAddDevToList(p_data->ble_periodic_adv_add_dev_to_list.addr_type,
5630                                    p_data->ble_periodic_adv_add_dev_to_list.addr,
5631                                    p_data->ble_periodic_adv_add_dev_to_list.sid);
5632 }
5633 
bta_dm_ble_gap_periodic_adv_remove_dev_from_list(tBTA_DM_MSG * p_data)5634 void bta_dm_ble_gap_periodic_adv_remove_dev_from_list(tBTA_DM_MSG *p_data)
5635 {
5636     APPL_TRACE_API("%s, addr_type = %d, sid = %d", __func__, p_data->ble_periodic_adv_remove_dev_from_list.addr_type,
5637                         p_data->ble_periodic_adv_remove_dev_from_list.sid);
5638 
5639     BTM_BlePeriodicAdvRemoveDevFromList(p_data->ble_periodic_adv_remove_dev_from_list.addr_type,
5640                                         p_data->ble_periodic_adv_remove_dev_from_list.addr,
5641                                         p_data->ble_periodic_adv_remove_dev_from_list.sid);
5642 
5643 }
5644 
bta_dm_ble_gap_periodic_adv_clear_dev(tBTA_DM_MSG * p_data)5645 void bta_dm_ble_gap_periodic_adv_clear_dev(tBTA_DM_MSG *p_data)
5646 {
5647     APPL_TRACE_API("%s", __func__);
5648     BTM_BlePeriodicAdvClearDev();
5649 }
5650 
5651 
bta_dm_ble_gap_set_ext_scan_params(tBTA_DM_MSG * p_data)5652 void bta_dm_ble_gap_set_ext_scan_params(tBTA_DM_MSG *p_data)
5653 {
5654     APPL_TRACE_API("%s", __func__);
5655     BTM_BleSetExtendedScanParams((tBTM_BLE_EXT_SCAN_PARAMS *)&p_data->ble_set_ext_scan_params.params);
5656 }
5657 
bta_dm_ble_gap_ext_scan(tBTA_DM_MSG * p_data)5658 void bta_dm_ble_gap_ext_scan(tBTA_DM_MSG *p_data)
5659 {
5660     APPL_TRACE_API("%s, start = %d, duration = %d, period = %d", __func__, p_data->ble_ext_scan.start, p_data->ble_ext_scan.duration,
5661                     p_data->ble_ext_scan.period);
5662     BTM_BleExtendedScan(p_data->ble_ext_scan.start, p_data->ble_ext_scan.duration,
5663                         p_data->ble_ext_scan.period);
5664 }
5665 
bta_dm_ble_gap_set_prefer_ext_conn_params(tBTA_DM_MSG * p_data)5666 void bta_dm_ble_gap_set_prefer_ext_conn_params(tBTA_DM_MSG *p_data)
5667 {
5668     tBTM_EXT_CONN_PARAMS conn_params;
5669     conn_params.phy_mask = p_data->ble_set_per_ext_conn_params.phy_mask;
5670 
5671     APPL_TRACE_API("%s, start = %d, duration = %d, period = %d", __func__, p_data->ble_ext_scan.start, p_data->ble_ext_scan.duration,
5672                     p_data->ble_ext_scan.period);
5673 
5674     if (conn_params.phy_mask & BTA_PHY_1M_MASK) {
5675         memcpy(&conn_params.phy_1m_conn_params, &p_data->ble_set_per_ext_conn_params.phy_1m_conn_params,
5676                sizeof(tBTA_DM_BLE_CONN_PARAMS));
5677     }
5678 
5679     if (conn_params.phy_mask & BTA_PHY_2M_MASK) {
5680         memcpy(&conn_params.phy_2m_conn_params, &p_data->ble_set_per_ext_conn_params.phy_2m_conn_params,
5681                sizeof(tBTA_DM_BLE_CONN_PARAMS));
5682     }
5683 
5684     if (conn_params.phy_mask & BTAS_PHY_CODED_MASK) {
5685         memcpy(&conn_params.phy_coded_conn_params, &p_data->ble_set_per_ext_conn_params.phy_coded_conn_params,
5686                sizeof(tBTA_DM_BLE_CONN_PARAMS));
5687     }
5688 
5689     BTM_BleSetPreferExtenedConnParams(p_data->ble_set_per_ext_conn_params.bd_addr, &conn_params);
5690 }
5691 
5692 #endif // #if (BLE_50_FEATURE_SUPPORT == TRUE)
5693 /*******************************************************************************
5694 **
5695 ** Function         bta_dm_ble_setup_storage
5696 **
5697 ** Description      This function configures up the storage parameters for ADV batch scanning
5698 **
5699 ** Parameters:
5700 **
5701 *******************************************************************************/
bta_dm_ble_setup_storage(tBTA_DM_MSG * p_data)5702 void bta_dm_ble_setup_storage (tBTA_DM_MSG *p_data)
5703 {
5704     tBTM_STATUS btm_status = 0;
5705     tBTM_BLE_VSC_CB cmn_ble_vsc_cb;
5706 
5707     BTM_BleGetVendorCapabilities(&cmn_ble_vsc_cb);
5708 
5709     if (0 != cmn_ble_vsc_cb.tot_scan_results_strg) {
5710         btm_status = BTM_BleSetStorageConfig(p_data->ble_set_storage.batch_scan_full_max,
5711                                              p_data->ble_set_storage.batch_scan_trunc_max,
5712                                              p_data->ble_set_storage.batch_scan_notify_threshold,
5713                                              p_data->ble_set_storage.p_setup_cback,
5714                                              p_data->ble_set_storage.p_thres_cback,
5715                                              p_data->ble_set_storage.p_read_rep_cback,
5716                                              p_data->ble_set_storage.ref_value);
5717     }
5718 
5719     if (BTM_CMD_STARTED != btm_status) {
5720         bta_ble_scan_setup_cb(BTM_BLE_BATCH_SCAN_CFG_STRG_EVT, p_data->ble_set_storage.ref_value,
5721                               btm_status);
5722     }
5723 }
5724 
5725 /*******************************************************************************
5726 **
5727 ** Function         bta_dm_ble_enable_batch_scan
5728 **
5729 ** Description      This function sets up the parameters and enables batch scan
5730 **
5731 ** Parameters:
5732 **
5733 *******************************************************************************/
bta_dm_ble_enable_batch_scan(tBTA_DM_MSG * p_data)5734 void bta_dm_ble_enable_batch_scan (tBTA_DM_MSG *p_data)
5735 {
5736     tBTM_STATUS btm_status = 0;
5737     tBTM_BLE_VSC_CB cmn_ble_vsc_cb;
5738 
5739     BTM_BleGetVendorCapabilities(&cmn_ble_vsc_cb);
5740 
5741     if (0 != cmn_ble_vsc_cb.tot_scan_results_strg) {
5742         btm_status = BTM_BleEnableBatchScan(p_data->ble_enable_scan.scan_mode,
5743                                             p_data->ble_enable_scan.scan_int,
5744                                             p_data->ble_enable_scan.scan_window,
5745                                             p_data->ble_enable_scan.discard_rule,
5746                                             p_data->ble_enable_scan.addr_type,
5747                                             p_data->ble_enable_scan.ref_value);
5748     }
5749 
5750     if (BTM_CMD_STARTED != btm_status) {
5751         bta_ble_scan_setup_cb(BTM_BLE_BATCH_SCAN_ENABLE_EVT, p_data->ble_enable_scan.ref_value,
5752                               btm_status);
5753     }
5754 }
5755 
5756 /*******************************************************************************
5757 **
5758 ** Function         bta_dm_ble_disable_batch_scan
5759 **
5760 ** Description      This function disables the batch scan
5761 **
5762 ** Parameters:
5763 **
5764 *******************************************************************************/
bta_dm_ble_disable_batch_scan(tBTA_DM_MSG * p_data)5765 void bta_dm_ble_disable_batch_scan (tBTA_DM_MSG *p_data)
5766 {
5767     UNUSED(p_data);
5768     tBTM_STATUS btm_status = 0;
5769     tBTM_BLE_VSC_CB cmn_ble_vsc_cb;
5770 
5771     BTM_BleGetVendorCapabilities(&cmn_ble_vsc_cb);
5772 
5773     if (0 != cmn_ble_vsc_cb.tot_scan_results_strg) {
5774         btm_status = BTM_BleDisableBatchScan(p_data->ble_disable_scan.ref_value);
5775     }
5776 
5777     if (BTM_CMD_STARTED != btm_status) {
5778         bta_ble_scan_setup_cb(BTM_BLE_BATCH_SCAN_DISABLE_EVT, p_data->ble_enable_scan.ref_value,
5779                               btm_status);
5780     }
5781 }
5782 
5783 /*******************************************************************************
5784 **
5785 ** Function         bta_dm_ble_read_scan_reports
5786 **
5787 ** Description      This function reads the batch scan reports
5788 **
5789 ** Parameters:
5790 **
5791 *******************************************************************************/
bta_dm_ble_read_scan_reports(tBTA_DM_MSG * p_data)5792 void bta_dm_ble_read_scan_reports(tBTA_DM_MSG *p_data)
5793 {
5794     tBTM_STATUS btm_status = 0;
5795     tBTM_BLE_VSC_CB cmn_ble_vsc_cb;
5796 
5797     BTM_BleGetVendorCapabilities(&cmn_ble_vsc_cb);
5798 
5799     if (0 != cmn_ble_vsc_cb.tot_scan_results_strg) {
5800         btm_status = BTM_BleReadScanReports(p_data->ble_read_reports.scan_type,
5801                                             p_data->ble_read_reports.ref_value);
5802     }
5803 
5804     if (BTM_CMD_STARTED != btm_status) {
5805         bta_ble_scan_setup_cb(BTM_BLE_BATCH_SCAN_READ_REPTS_EVT, p_data->ble_enable_scan.ref_value,
5806                               btm_status);
5807     }
5808 }
5809 
5810 /*******************************************************************************
5811 **
5812 ** Function         bta_dm_ble_track_advertiser
5813 **
5814 ** Description      This function tracks the specific advertiser
5815 **
5816 ** Parameters:
5817 **
5818 *******************************************************************************/
bta_dm_ble_track_advertiser(tBTA_DM_MSG * p_data)5819 void bta_dm_ble_track_advertiser(tBTA_DM_MSG *p_data)
5820 {
5821     tBTM_STATUS btm_status = 0;
5822     BD_ADDR bda;
5823     memset(&bda, 0 , sizeof(BD_ADDR));
5824     tBTM_BLE_VSC_CB cmn_ble_vsc_cb;
5825     tBTA_DM_BLE_TRACK_ADV_DATA track_adv_data;
5826 
5827     BTM_BleGetVendorCapabilities(&cmn_ble_vsc_cb);
5828 
5829     if (0 != cmn_ble_vsc_cb.tot_scan_results_strg) {
5830         btm_status = BTM_BleTrackAdvertiser((tBTM_BLE_TRACK_ADV_CBACK *)
5831                                             p_data->ble_track_advert.p_track_adv_cback,
5832                                             p_data->ble_track_advert.ref_value);
5833     }
5834 
5835     if (BTM_CMD_STARTED != btm_status) {
5836         memset(&track_adv_data, 0, sizeof(tBTA_DM_BLE_TRACK_ADV_DATA));
5837         track_adv_data.advertiser_info_present = NO_ADV_INFO_PRESENT; /* Indicates failure */
5838         track_adv_data.client_if = (UINT8)p_data->ble_track_advert.ref_value;
5839         p_data->ble_track_advert.p_track_adv_cback(&track_adv_data);
5840     }
5841 }
5842 
5843 /*******************************************************************************
5844 **
5845 ** Function         bta_ble_scan_setup_cb
5846 **
5847 ** Description      Handle the setup callback from BTM layer and forward it to app layer
5848 **
5849 ** Parameters:
5850 **
5851 *******************************************************************************/
bta_ble_scan_setup_cb(tBTM_BLE_BATCH_SCAN_EVT evt,tBTM_BLE_REF_VALUE ref_value,tBTM_STATUS status)5852 void bta_ble_scan_setup_cb(tBTM_BLE_BATCH_SCAN_EVT evt, tBTM_BLE_REF_VALUE ref_value,
5853                            tBTM_STATUS status)
5854 {
5855     tBTA_BLE_BATCH_SCAN_EVT bta_evt = 0;
5856 
5857     APPL_TRACE_DEBUG("bta_ble_scan_setup_cb : evt: %d, ref_value: %d, status:%d", evt,
5858                      ref_value, status);
5859 
5860     switch (evt) {
5861     case BTM_BLE_BATCH_SCAN_ENABLE_EVT:
5862         bta_evt = BTA_BLE_BATCH_SCAN_ENB_EVT;
5863         break;
5864     case BTM_BLE_BATCH_SCAN_CFG_STRG_EVT:
5865         bta_evt = BTA_BLE_BATCH_SCAN_CFG_STRG_EVT;
5866         break;
5867     case BTM_BLE_BATCH_SCAN_DISABLE_EVT:
5868         bta_evt = BTA_BLE_BATCH_SCAN_DIS_EVT;
5869         break;
5870     case BTM_BLE_BATCH_SCAN_PARAM_EVT:
5871         bta_evt = BTA_BLE_BATCH_SCAN_PARAM_EVT;
5872         break;
5873     default:
5874         break;
5875     }
5876 
5877     if (NULL != bta_dm_cb.p_setup_cback) {
5878         bta_dm_cb.p_setup_cback(bta_evt, ref_value, status);
5879     }
5880 }
5881 
5882 
5883 #if BLE_ANDROID_CONTROLLER_SCAN_FILTER == TRUE
5884 /*******************************************************************************
5885 **
5886 ** Function         bta_ble_scan_pf_cmpl
5887 **
5888 ** Description      ADV payload filtering operation complete callback
5889 **
5890 **
5891 ** Returns         TRUE if handled, otherwise FALSE.
5892 **
5893 *******************************************************************************/
bta_ble_scan_cfg_cmpl(tBTM_BLE_PF_ACTION action,tBTM_BLE_SCAN_COND_OP cfg_op,tBTM_BLE_PF_AVBL_SPACE avbl_space,tBTM_STATUS status,tBTM_BLE_REF_VALUE ref_value)5894 static void bta_ble_scan_cfg_cmpl(tBTM_BLE_PF_ACTION action, tBTM_BLE_SCAN_COND_OP cfg_op,
5895                                   tBTM_BLE_PF_AVBL_SPACE avbl_space, tBTM_STATUS status,
5896                                   tBTM_BLE_REF_VALUE ref_value)
5897 {
5898     tBTA_STATUS st = (status == BTM_SUCCESS) ? BTA_SUCCESS : BTA_FAILURE;
5899 
5900     APPL_TRACE_DEBUG("bta_ble_scan_cfg_cmpl: %d, %d, %d, %d", action, cfg_op, avbl_space, status);
5901 
5902     if (bta_dm_cb.p_scan_filt_cfg_cback) {
5903         bta_dm_cb.p_scan_filt_cfg_cback(action, cfg_op, avbl_space, st, ref_value);
5904     }
5905 }
5906 
5907 /*******************************************************************************
5908 **
5909 ** Function         bta_dm_cfg_filter_cond
5910 **
5911 ** Description      This function configure adv payload filtering condition
5912 **
5913 ** Parameters:
5914 **
5915 *******************************************************************************/
bta_dm_cfg_filter_cond(tBTA_DM_MSG * p_data)5916 void bta_dm_cfg_filter_cond (tBTA_DM_MSG *p_data)
5917 {
5918     tBTM_STATUS st = BTM_MODE_UNSUPPORTED;
5919     tBTA_STATUS status = BTA_FAILURE;
5920 
5921     tBTM_BLE_VSC_CB cmn_vsc_cb;
5922 
5923     APPL_TRACE_DEBUG("bta_dm_cfg_filter_cond");
5924     BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
5925     if (0 != cmn_vsc_cb.filter_support) {
5926         if ((st = BTM_BleCfgFilterCondition(p_data->ble_cfg_filter_cond.action,
5927                                             p_data->ble_cfg_filter_cond.cond_type,
5928                                             (tBTM_BLE_PF_FILT_INDEX)p_data->ble_cfg_filter_cond.filt_index,
5929                                             (tBTM_BLE_PF_COND_PARAM *)p_data->ble_cfg_filter_cond.p_cond_param,
5930                                             bta_ble_scan_cfg_cmpl, p_data->ble_cfg_filter_cond.ref_value))
5931                 == BTM_CMD_STARTED) {
5932             bta_dm_cb.p_scan_filt_cfg_cback = p_data->ble_cfg_filter_cond.p_filt_cfg_cback;
5933             return;
5934         }
5935     }
5936 
5937     if (p_data->ble_cfg_filter_cond.p_filt_cfg_cback) {
5938         p_data->ble_cfg_filter_cond.p_filt_cfg_cback(BTA_DM_BLE_PF_CONFIG_EVT,
5939                 p_data->ble_cfg_filter_cond.cond_type, 0, status,
5940                 p_data->ble_cfg_filter_cond.ref_value);
5941     }
5942     return;
5943 }
5944 
5945 /*******************************************************************************
5946 **
5947 ** Function         bta_dm_enable_scan_filter
5948 **
5949 ** Description      This function enable/disable adv payload filtering condition
5950 **
5951 ** Parameters:
5952 **
5953 *******************************************************************************/
bta_dm_enable_scan_filter(tBTA_DM_MSG * p_data)5954 void bta_dm_enable_scan_filter(tBTA_DM_MSG *p_data)
5955 {
5956     tBTM_STATUS st = BTM_MODE_UNSUPPORTED;
5957     tBTA_STATUS status = BTA_FAILURE;
5958 
5959     tBTM_BLE_VSC_CB cmn_vsc_cb;
5960     APPL_TRACE_DEBUG("bta_dm_enable_scan_filter");
5961     BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
5962 
5963     if (0 != cmn_vsc_cb.filter_support) {
5964         if ((st = BTM_BleEnableDisableFilterFeature(p_data->ble_enable_scan_filt.action,
5965                   p_data->ble_enable_scan_filt.p_filt_status_cback,
5966                   (tBTM_BLE_REF_VALUE)p_data->ble_enable_scan_filt.ref_value)) == BTM_CMD_STARTED) {
5967             bta_dm_cb.p_scan_filt_status_cback = p_data->ble_enable_scan_filt.p_filt_status_cback;
5968         }
5969         return;
5970     }
5971 
5972     if (p_data->ble_enable_scan_filt.p_filt_status_cback) {
5973         p_data->ble_enable_scan_filt.p_filt_status_cback (BTA_DM_BLE_PF_ENABLE_EVT,
5974                 p_data->ble_enable_scan_filt.ref_value, status);
5975     }
5976 
5977 }
5978 
5979 /*******************************************************************************
5980 **
5981 ** Function         bta_dm_scan_filter_param_setup
5982 **
5983 ** Description      This function sets up scan filter params
5984 **
5985 ** Parameters:
5986 **
5987 *******************************************************************************/
bta_dm_scan_filter_param_setup(tBTA_DM_MSG * p_data)5988 void bta_dm_scan_filter_param_setup (tBTA_DM_MSG *p_data)
5989 {
5990     tBTM_STATUS st = BTM_MODE_UNSUPPORTED;
5991     tBTA_STATUS status = BTA_FAILURE;
5992 
5993     tBTM_BLE_VSC_CB cmn_vsc_cb;
5994 
5995     APPL_TRACE_DEBUG("bta_dm_scan_filter_param_setup");
5996     BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
5997     if (0 != cmn_vsc_cb.filter_support) {
5998         if ((st = BTM_BleAdvFilterParamSetup(p_data->ble_scan_filt_param_setup.action,
5999                                              p_data->ble_scan_filt_param_setup.filt_index,
6000                                              (tBTM_BLE_PF_FILT_PARAMS *)&p_data->ble_scan_filt_param_setup.filt_params,
6001                                              p_data->ble_scan_filt_param_setup.p_target,
6002                                              p_data->ble_scan_filt_param_setup.p_filt_param_cback,
6003                                              p_data->ble_scan_filt_param_setup.ref_value)) == BTM_CMD_STARTED) {
6004             bta_dm_cb.p_scan_filt_param_cback = p_data->ble_scan_filt_param_setup.p_filt_param_cback;
6005             return;
6006         }
6007     }
6008 
6009     if (p_data->ble_scan_filt_param_setup.p_filt_param_cback) {
6010         p_data->ble_scan_filt_param_setup.p_filt_param_cback (BTA_DM_BLE_PF_ENABLE_EVT, 0,
6011                 p_data->ble_scan_filt_param_setup.ref_value, status);
6012     }
6013 
6014     return;
6015 }
6016 #endif
6017 
6018 /*******************************************************************************
6019 **
6020 ** Function         bta_ble_enable_scan_cmpl
6021 **
6022 ** Description      ADV payload filtering enable / disable complete callback
6023 **
6024 **
6025 ** Returns          None
6026 **
6027 *******************************************************************************/
bta_ble_energy_info_cmpl(tBTM_BLE_TX_TIME_MS tx_time,tBTM_BLE_RX_TIME_MS rx_time,tBTM_BLE_IDLE_TIME_MS idle_time,tBTM_BLE_ENERGY_USED energy_used,tBTM_STATUS status)6028 static void bta_ble_energy_info_cmpl(tBTM_BLE_TX_TIME_MS tx_time,
6029                                      tBTM_BLE_RX_TIME_MS rx_time,
6030                                      tBTM_BLE_IDLE_TIME_MS idle_time,
6031                                      tBTM_BLE_ENERGY_USED  energy_used,
6032                                      tBTM_STATUS status)
6033 {
6034     tBTA_STATUS st = (status == BTM_SUCCESS) ? BTA_SUCCESS : BTA_FAILURE;
6035     tBTA_DM_CONTRL_STATE ctrl_state = 0;
6036 #if ((defined BTA_GATT_INCLUDED) &&  (BTA_GATT_INCLUDED == TRUE) && SDP_INCLUDED == TRUE)
6037     if (BTA_SUCCESS == st) {
6038         ctrl_state = bta_dm_pm_obtain_controller_state();
6039     }
6040 #endif
6041     if (bta_dm_cb.p_energy_info_cback) {
6042         bta_dm_cb.p_energy_info_cback(tx_time, rx_time, idle_time, energy_used, ctrl_state, st);
6043     }
6044 }
6045 
6046 /*******************************************************************************
6047 **
6048 ** Function         bta_dm_ble_get_energy_info
6049 **
6050 ** Description      This function obtains the energy info
6051 **
6052 ** Parameters:
6053 **
6054 *******************************************************************************/
bta_dm_ble_get_energy_info(tBTA_DM_MSG * p_data)6055 void bta_dm_ble_get_energy_info(tBTA_DM_MSG *p_data)
6056 {
6057     tBTM_STATUS btm_status = 0;
6058 
6059     bta_dm_cb.p_energy_info_cback = p_data->ble_energy_info.p_energy_info_cback;
6060     btm_status = BTM_BleGetEnergyInfo(bta_ble_energy_info_cmpl);
6061     if (BTM_CMD_STARTED != btm_status) {
6062         bta_ble_energy_info_cmpl(0, 0, 0, 0, btm_status);
6063     }
6064 }
6065 
6066 #if ((defined BTA_GATT_INCLUDED) &&  (BTA_GATT_INCLUDED == TRUE) && SDP_INCLUDED == TRUE)
6067 #ifndef BTA_DM_GATT_CLOSE_DELAY_TOUT
6068 #define BTA_DM_GATT_CLOSE_DELAY_TOUT    1000
6069 #endif
6070 
6071 /*******************************************************************************
6072 **
6073 ** Function         bta_dm_gattc_register
6074 **
6075 ** Description      Register with GATTC in DM if BLE is needed.
6076 **
6077 **
6078 ** Returns          void
6079 **
6080 *******************************************************************************/
6081 #if (GATTC_INCLUDED == TRUE)
bta_dm_gattc_register(void)6082 static void bta_dm_gattc_register(void)
6083 {
6084     tBT_UUID                app_uuid = {LEN_UUID_128, {0}};
6085 
6086     if (bta_dm_search_cb.client_if == BTA_GATTS_INVALID_IF) {
6087         memset (&app_uuid.uu.uuid128, 0x87, LEN_UUID_128);
6088         BTA_GATTC_AppRegister(&app_uuid, bta_dm_gattc_callback);
6089     }
6090 }
6091 #endif /* GATTC_INCLUDED == TRUE */
6092 
6093 /*******************************************************************************
6094 **
6095 ** Function         btm_dm_start_disc_gatt_services
6096 **
6097 ** Description      This function starts a GATT service search request.
6098 **
6099 ** Parameters:
6100 **
6101 *******************************************************************************/
6102 #if (GATTC_INCLUDED == TRUE)
btm_dm_start_disc_gatt_services(UINT16 conn_id)6103 static void btm_dm_start_disc_gatt_services (UINT16 conn_id)
6104 {
6105     tBT_UUID    *p_uuid = bta_dm_search_cb.p_srvc_uuid +
6106                           bta_dm_search_cb.num_uuid - bta_dm_search_cb.uuid_to_search;
6107 
6108     p_uuid = bta_dm_search_cb.p_srvc_uuid +
6109              bta_dm_search_cb.num_uuid - bta_dm_search_cb.uuid_to_search;
6110 
6111     /* always search for all services */
6112     BTA_GATTC_ServiceSearchRequest(conn_id, p_uuid);
6113 }
6114 #endif /* GATTC_INCLUDED == TRUE */
6115 
6116 /*******************************************************************************
6117 **
6118 ** Function         bta_dm_gatt_disc_result
6119 **
6120 ** Description      This function process the GATT service search result.
6121 **
6122 ** Parameters:
6123 **
6124 *******************************************************************************/
6125 #if (GATTC_INCLUDED == TRUE)
bta_dm_gatt_disc_result(tBTA_GATT_ID service_id)6126 static void bta_dm_gatt_disc_result(tBTA_GATT_ID service_id)
6127 {
6128     tBTA_DM_SEARCH   result;
6129 
6130     /*
6131         * This logic will not work for gatt case.  We are checking against the bluetooth profiles here
6132         * just copy the GATTID in raw data field and send it across.
6133         */
6134 
6135 
6136     if ( bta_dm_search_cb.ble_raw_used + sizeof(tBTA_GATT_ID) < bta_dm_search_cb.ble_raw_size ) {
6137         APPL_TRACE_DEBUG("ADDING BLE SERVICE uuid=0x%x, ble_ptr = %p, ble_raw_used = 0x%x",
6138                          service_id.uuid.uu.uuid16, bta_dm_search_cb.p_ble_rawdata, bta_dm_search_cb.ble_raw_used);
6139 
6140         if (bta_dm_search_cb.p_ble_rawdata) {
6141             memcpy((bta_dm_search_cb.p_ble_rawdata + bta_dm_search_cb.ble_raw_used), &service_id,
6142                    sizeof(service_id) );
6143 
6144             bta_dm_search_cb.ble_raw_used += sizeof(service_id);
6145         } else {
6146             APPL_TRACE_ERROR("p_ble_rawdata is NULL");
6147         }
6148 
6149     } else {
6150         APPL_TRACE_ERROR("%s out of room to accomodate more service ids ble_raw_size = %d ble_raw_used = %d", __FUNCTION__, bta_dm_search_cb.ble_raw_size, bta_dm_search_cb.ble_raw_used );
6151     }
6152 
6153     APPL_TRACE_API("%s service_id_uuid_len=%d ", __func__, service_id.uuid.len);
6154     if ( bta_dm_search_cb.state != BTA_DM_SEARCH_IDLE) {
6155 
6156         /* send result back to app now, one by one */
6157         bdcpy (result.disc_ble_res.bd_addr, bta_dm_search_cb.peer_bdaddr);
6158         BCM_STRNCPY_S((char *)result.disc_ble_res.bd_name, bta_dm_get_remname(), (BD_NAME_LEN - 1));
6159         result.disc_ble_res.bd_name[BD_NAME_LEN] = 0;
6160         memcpy(&result.disc_ble_res.service, &service_id.uuid, sizeof(tBT_UUID));
6161 
6162         bta_dm_search_cb.p_search_cback(BTA_DM_DISC_BLE_RES_EVT, &result);
6163     }
6164 }
6165 #endif /* #if (GATTC_INCLUDED == TRUE) */
6166 /*******************************************************************************
6167 **
6168 ** Function         bta_dm_gatt_disc_complete
6169 **
6170 ** Description      This function process the GATT service search complete.
6171 **
6172 ** Parameters:
6173 **
6174 *******************************************************************************/
6175 #if (GATTC_INCLUDED == TRUE)
bta_dm_gatt_disc_complete(UINT16 conn_id,tBTA_GATT_STATUS status)6176 static void bta_dm_gatt_disc_complete(UINT16 conn_id, tBTA_GATT_STATUS status)
6177 {
6178     tBTA_DM_MSG *p_msg;
6179 
6180     APPL_TRACE_DEBUG("bta_dm_gatt_disc_complete conn_id = %d", conn_id);
6181 
6182     if (bta_dm_search_cb.uuid_to_search > 0) {
6183         bta_dm_search_cb.uuid_to_search --;
6184     }
6185 
6186     if (status == BTA_GATT_OK && bta_dm_search_cb.uuid_to_search > 0) {
6187         btm_dm_start_disc_gatt_services(conn_id);
6188     } else {
6189         bta_dm_search_cb.uuid_to_search = 0;
6190 
6191         /* no more services to be discovered */
6192         if ((p_msg = (tBTA_DM_MSG *) osi_malloc(sizeof(tBTA_DM_MSG))) != NULL) {
6193             p_msg->hdr.event = BTA_DM_DISCOVERY_RESULT_EVT;
6194             p_msg->disc_result.result.disc_res.result = (status == BTA_GATT_OK) ? BTA_SUCCESS : BTA_FAILURE;
6195             APPL_TRACE_DEBUG("%s service found: 0x%08x", __FUNCTION__,
6196                              bta_dm_search_cb.services_found);
6197             p_msg->disc_result.result.disc_res.services = bta_dm_search_cb.services_found;
6198             p_msg->disc_result.result.disc_res.num_uuids = 0;
6199             p_msg->disc_result.result.disc_res.p_uuid_list = NULL;
6200             bdcpy (p_msg->disc_result.result.disc_res.bd_addr, bta_dm_search_cb.peer_bdaddr);
6201             BCM_STRNCPY_S((char *)p_msg->disc_result.result.disc_res.bd_name,bta_dm_get_remname(), (BD_NAME_LEN - 1));
6202 
6203             /* make sure the string is terminated */
6204             p_msg->disc_result.result.disc_res.bd_name[BD_NAME_LEN - 1] = 0;
6205 
6206             p_msg->disc_result.result.disc_res.device_type |= BT_DEVICE_TYPE_BLE;
6207             if ( bta_dm_search_cb.ble_raw_used > 0 ) {
6208                 p_msg->disc_result.result.disc_res.p_raw_data = osi_malloc(bta_dm_search_cb.ble_raw_used);
6209 
6210                 memcpy( p_msg->disc_result.result.disc_res.p_raw_data,
6211                         bta_dm_search_cb.p_ble_rawdata,
6212                         bta_dm_search_cb.ble_raw_used );
6213 
6214                 p_msg->disc_result.result.disc_res.raw_data_size = bta_dm_search_cb.ble_raw_used;
6215             } else {
6216                 p_msg->disc_result.result.disc_res.p_raw_data = NULL;
6217                 bta_dm_search_cb.p_ble_rawdata = 0;
6218             }
6219 
6220             bta_sys_sendmsg(p_msg);
6221         }
6222 
6223         if (conn_id != BTA_GATT_INVALID_CONN_ID) {
6224             /* start a GATT channel close delay timer */
6225             bta_sys_start_timer(&bta_dm_search_cb.gatt_close_timer, BTA_DM_DISC_CLOSE_TOUT_EVT,
6226                                 BTA_DM_GATT_CLOSE_DELAY_TOUT);
6227             bdcpy(bta_dm_search_cb.pending_close_bda, bta_dm_search_cb.peer_bdaddr);
6228         }
6229         bta_dm_search_cb.gatt_disc_active = FALSE;
6230     }
6231 }
6232 #endif /* #if (GATTC_INCLUDED == TRUE) */
6233 
6234 /*******************************************************************************
6235 **
6236 ** Function         bta_dm_close_gatt_conn
6237 **
6238 ** Description      This function close the GATT connection after delay timeout.
6239 **
6240 ** Parameters:
6241 **
6242 *******************************************************************************/
6243 #if (GATTC_INCLUDED == TRUE)
bta_dm_close_gatt_conn(tBTA_DM_MSG * p_data)6244 void bta_dm_close_gatt_conn(tBTA_DM_MSG *p_data)
6245 {
6246     UNUSED(p_data);
6247 
6248     if (bta_dm_search_cb.conn_id != BTA_GATT_INVALID_CONN_ID) {
6249         BTA_GATTC_Close(bta_dm_search_cb.conn_id);
6250     }
6251 
6252     memset(bta_dm_search_cb.pending_close_bda, 0, BD_ADDR_LEN);
6253     bta_dm_search_cb.conn_id = BTA_GATT_INVALID_CONN_ID;
6254 }
6255 #endif /* #if (GATTC_INCLUDED == TRUE) */
6256 /*******************************************************************************
6257 **
6258 ** Function         btm_dm_start_gatt_discovery
6259 **
6260 ** Description      This is GATT initiate the service search by open a GATT connection
6261 **                  first.
6262 **
6263 ** Parameters:
6264 **
6265 *******************************************************************************/
6266 #if (GATTC_INCLUDED == TRUE)
btm_dm_start_gatt_discovery(BD_ADDR bd_addr)6267 void btm_dm_start_gatt_discovery (BD_ADDR bd_addr)
6268 {
6269     bta_dm_search_cb.gatt_disc_active = TRUE;
6270 
6271     /* connection is already open */
6272     if (bdcmp(bta_dm_search_cb.pending_close_bda, bd_addr) == 0 &&
6273             bta_dm_search_cb.conn_id != BTA_GATT_INVALID_CONN_ID) {
6274         memset(bta_dm_search_cb.pending_close_bda, 0, BD_ADDR_LEN);
6275         bta_sys_stop_timer(&bta_dm_search_cb.gatt_close_timer);
6276         btm_dm_start_disc_gatt_services(bta_dm_search_cb.conn_id);
6277     } else {
6278         //TODO need to add addr_type in future
6279         BTA_GATTC_Open(bta_dm_search_cb.client_if, bd_addr, BLE_ADDR_UNKNOWN_TYPE, TRUE, BTA_GATT_TRANSPORT_LE, FALSE);
6280     }
6281 }
6282 #endif /* #if (GATTC_INCLUDED == TRUE) */
6283 /*******************************************************************************
6284 **
6285 ** Function         bta_dm_cancel_gatt_discovery
6286 **
6287 ** Description      This is GATT cancel the GATT service search.
6288 **
6289 ** Parameters:
6290 **
6291 *******************************************************************************/
6292 #if (GATTC_INCLUDED == TRUE)
bta_dm_cancel_gatt_discovery(BD_ADDR bd_addr)6293 static void bta_dm_cancel_gatt_discovery(BD_ADDR bd_addr)
6294 {
6295     if (bta_dm_search_cb.conn_id == BTA_GATT_INVALID_CONN_ID) {
6296         BTA_GATTC_CancelOpen(bta_dm_search_cb.client_if, bd_addr, TRUE);
6297     }
6298 
6299     bta_dm_gatt_disc_complete(bta_dm_search_cb.conn_id, (tBTA_GATT_STATUS) BTA_GATT_ERROR);
6300 }
6301 #endif /* #if (GATTC_INCLUDED == TRUE) */
6302 
6303 /*******************************************************************************
6304 **
6305 ** Function         bta_dm_proc_open_evt
6306 **
6307 ** Description      process BTA_GATTC_OPEN_EVT in DM.
6308 **
6309 ** Parameters:
6310 **
6311 *******************************************************************************/
6312 #if (GATTC_INCLUDED == TRUE)
bta_dm_proc_open_evt(tBTA_GATTC_OPEN * p_data)6313 void bta_dm_proc_open_evt(tBTA_GATTC_OPEN *p_data)
6314 {
6315     UINT8           *p1;
6316     UINT8           *p2;
6317 
6318     p1 = bta_dm_search_cb.peer_bdaddr;
6319     p2 = p_data->remote_bda;
6320 
6321     APPL_TRACE_DEBUG("DM Search state= %d search_cb.peer_dbaddr: [%08x%04x] connected_bda= [%08x%04x] ",
6322                      bta_dm_search_cb.state,
6323                      ((p1[0]) << 24) + ((p1[1]) << 16) + ((p1[2]) << 8) + (p1[3]),
6324                      ((p1[4]) << 8) + p1[5],
6325                      ((p2[0]) << 24) + ((p2[1]) << 16) + ((p2[2]) << 8) + (p2[3]),
6326                      ((p2[4]) << 8) + p2[5]);
6327 
6328     UNUSED(p1);
6329     UNUSED(p2);
6330     APPL_TRACE_DEBUG("BTA_GATTC_OPEN_EVT conn_id = %d client_if=%d status = %d" ,
6331                      p_data->conn_id,
6332                      p_data->client_if,
6333                      p_data->status);
6334 
6335     bta_dm_search_cb.conn_id = p_data->conn_id;
6336 
6337     if (p_data->status == BTA_GATT_OK) {
6338         btm_dm_start_disc_gatt_services(p_data->conn_id);
6339     } else {
6340         bta_dm_gatt_disc_complete(BTA_GATT_INVALID_CONN_ID, p_data->status);
6341     }
6342 }
6343 #endif /* #if (GATTC_INCLUDED == TRUE) */
6344 
6345 /*******************************************************************************
6346 **
6347 ** Function         bta_dm_gattc_callback
6348 **
6349 ** Description      This is GATT client callback function used in DM.
6350 **
6351 ** Parameters:
6352 **
6353 *******************************************************************************/
6354 #if (GATTC_INCLUDED == TRUE)
bta_dm_gattc_callback(tBTA_GATTC_EVT event,tBTA_GATTC * p_data)6355 static void bta_dm_gattc_callback(tBTA_GATTC_EVT event, tBTA_GATTC *p_data)
6356 {
6357     APPL_TRACE_DEBUG("bta_dm_gattc_callback event = %d", event);
6358 
6359     switch (event) {
6360     case BTA_GATTC_REG_EVT:
6361         APPL_TRACE_DEBUG("BTA_GATTC_REG_EVT client_if = %d",  p_data->reg_oper.client_if);
6362         if (p_data->reg_oper.status == BTA_GATT_OK) {
6363             bta_dm_search_cb.client_if = p_data->reg_oper.client_if;
6364         } else {
6365             bta_dm_search_cb.client_if = BTA_GATTS_INVALID_IF;
6366         }
6367         break;
6368 
6369     case BTA_GATTC_OPEN_EVT:
6370         bta_dm_proc_open_evt(&p_data->open);
6371         break;
6372 
6373     case BTA_GATTC_SEARCH_RES_EVT:
6374         bta_dm_gatt_disc_result(p_data->srvc_res.service_uuid);
6375         break;
6376 
6377     case BTA_GATTC_SEARCH_CMPL_EVT:
6378         if ( bta_dm_search_cb.state != BTA_DM_SEARCH_IDLE) {
6379             bta_dm_gatt_disc_complete(p_data->search_cmpl.conn_id, p_data->search_cmpl.status);
6380         }
6381         break;
6382 
6383     case BTA_GATTC_CLOSE_EVT:
6384         APPL_TRACE_DEBUG("BTA_GATTC_CLOSE_EVT reason = %d", p_data->close.reason);
6385         /* in case of disconnect before search is completed */
6386         if ( (bta_dm_search_cb.state != BTA_DM_SEARCH_IDLE) &&
6387                 (bta_dm_search_cb.state != BTA_DM_SEARCH_ACTIVE) &&
6388                 !memcmp(p_data->close.remote_bda, bta_dm_search_cb.peer_bdaddr, BD_ADDR_LEN)) {
6389             bta_dm_gatt_disc_complete((UINT16)BTA_GATT_INVALID_CONN_ID,  (tBTA_GATT_STATUS) BTA_GATT_ERROR);
6390         }
6391         break;
6392 
6393     default:
6394         break;
6395     }
6396 }
6397 #endif /* #if (GATTC_INCLUDED == TRUE) */
6398 #endif /* BTA_GATT_INCLUDED */
6399 
6400 #if BLE_VND_INCLUDED == TRUE
6401 /*******************************************************************************
6402 **
6403 ** Function         bta_dm_ctrl_features_rd_cmpl_cback
6404 **
6405 ** Description      callback to handle controller feature read complete
6406 **
6407 ** Parameters:
6408 **
6409 *******************************************************************************/
bta_dm_ctrl_features_rd_cmpl_cback(tBTM_STATUS result)6410 static void bta_dm_ctrl_features_rd_cmpl_cback(tBTM_STATUS result)
6411 {
6412     APPL_TRACE_DEBUG("%s  status = %d ", __FUNCTION__, result);
6413     if (result == BTM_SUCCESS) {
6414         if (bta_dm_cb.p_sec_cback) {
6415             bta_dm_cb.p_sec_cback(BTA_DM_LE_FEATURES_READ, NULL);
6416         }
6417     } else {
6418         APPL_TRACE_ERROR("%s Ctrl BLE feature read failed: status :%d", __FUNCTION__, result);
6419     }
6420 
6421 }
6422 #endif /* BLE_VND_INCLUDED */
6423 
6424 #endif  /* BLE_INCLUDED */
6425