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 BOOLEAN 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         if (bta_dm_remove_sec_dev_entry(bd_addr)) {
3073             return BTM_SEC_DEV_REC_REMOVED;
3074         }
3075     }
3076 
3077     return BTM_SUCCESS;
3078 }
3079 
3080 #if (BT_SSP_INCLUDED == TRUE)
3081 /*******************************************************************************
3082 **
3083 ** Function         bta_dm_sp_cback
3084 **
3085 ** Description      simple pairing callback from BTM
3086 **
3087 ** Returns          void
3088 **
3089 *******************************************************************************/
bta_dm_sp_cback(tBTM_SP_EVT event,tBTM_SP_EVT_DATA * p_data)3090 static UINT8 bta_dm_sp_cback (tBTM_SP_EVT event, tBTM_SP_EVT_DATA *p_data)
3091 {
3092     tBTM_STATUS status = BTM_CMD_STARTED;
3093     tBTA_DM_SEC sec_event;
3094     tBTA_DM_SEC_EVT pin_evt = BTA_DM_SP_KEY_NOTIF_EVT;
3095 
3096     APPL_TRACE_EVENT("bta_dm_sp_cback: %d", event);
3097     if (!bta_dm_cb.p_sec_cback) {
3098         return BTM_NOT_AUTHORIZED;
3099     }
3100 
3101     /* TODO_SP */
3102     switch (event) {
3103     case BTM_SP_IO_REQ_EVT:
3104 #if (BT_SSP_INCLUDED == TRUE)
3105         /* translate auth_req */
3106         bta_dm_co_io_req(p_data->io_req.bd_addr, &p_data->io_req.io_cap,
3107                          &p_data->io_req.oob_data, &p_data->io_req.auth_req, p_data->io_req.is_orig);
3108 #endif
3109 #if BTM_OOB_INCLUDED == FALSE
3110         status = BTM_SUCCESS;
3111 #endif
3112 
3113         APPL_TRACE_EVENT("io mitm: %d oob_data:%d", p_data->io_req.auth_req, p_data->io_req.oob_data);
3114         break;
3115     case BTM_SP_IO_RSP_EVT:
3116 #if (BT_SSP_INCLUDED == TRUE)
3117         bta_dm_co_io_rsp(p_data->io_rsp.bd_addr, p_data->io_rsp.io_cap,
3118                          p_data->io_rsp.oob_data, p_data->io_rsp.auth_req );
3119 #endif
3120         break;
3121 
3122     case BTM_SP_CFM_REQ_EVT:
3123         pin_evt = BTA_DM_SP_CFM_REQ_EVT;
3124         bta_dm_cb.just_works = sec_event.cfm_req.just_works = p_data->cfm_req.just_works;
3125         sec_event.cfm_req.loc_auth_req = p_data->cfm_req.loc_auth_req;
3126         sec_event.cfm_req.rmt_auth_req = p_data->cfm_req.rmt_auth_req;
3127         sec_event.cfm_req.loc_io_caps = p_data->cfm_req.loc_io_caps;
3128         sec_event.cfm_req.rmt_io_caps = p_data->cfm_req.rmt_io_caps;
3129 
3130         /* continue to next case */
3131 #if (BT_SSP_INCLUDED == TRUE)
3132     /* Passkey entry mode, mobile device with output capability is very
3133         unlikely to receive key request, so skip this event */
3134     case BTM_SP_KEY_REQ_EVT:
3135     case BTM_SP_KEY_NOTIF_EVT:
3136 #endif
3137         if (BTM_SP_CFM_REQ_EVT == event) {
3138             /* Due to the switch case falling through below to BTM_SP_KEY_NOTIF_EVT,
3139                call remote name request using values from cfm_req */
3140             if (p_data->cfm_req.bd_name[0] == 0) {
3141                 bta_dm_cb.pin_evt = pin_evt;
3142                 bdcpy(bta_dm_cb.pin_bd_addr, p_data->cfm_req.bd_addr);
3143                 BTA_COPY_DEVICE_CLASS(bta_dm_cb.pin_dev_class, p_data->cfm_req.dev_class);
3144                 if ((BTM_ReadRemoteDeviceName(p_data->cfm_req.bd_addr, bta_dm_pinname_cback,
3145                                               BT_TRANSPORT_BR_EDR)) == BTM_CMD_STARTED) {
3146                     return BTM_CMD_STARTED;
3147                 }
3148                 APPL_TRACE_WARNING(" bta_dm_sp_cback() -> Failed to start Remote Name Request  ");
3149             } else {
3150                 /* Due to the switch case falling through below to BTM_SP_KEY_NOTIF_EVT,
3151                    copy these values into key_notif from cfm_req */
3152                 bdcpy(sec_event.key_notif.bd_addr, p_data->cfm_req.bd_addr);
3153                 BTA_COPY_DEVICE_CLASS(sec_event.key_notif.dev_class, p_data->cfm_req.dev_class);
3154                 BCM_STRNCPY_S((char *)sec_event.key_notif.bd_name, (char *)p_data->cfm_req.bd_name, (BD_NAME_LEN - 1));
3155                 sec_event.key_notif.bd_name[BD_NAME_LEN - 1] = 0;
3156             }
3157         }
3158 
3159         bta_dm_cb.num_val = sec_event.key_notif.passkey = p_data->key_notif.passkey;
3160         if (BTM_SP_KEY_NOTIF_EVT == event) {
3161             /* If the device name is not known, save bdaddr and devclass
3162                and initiate a name request with values from key_notif */
3163             if (p_data->key_notif.bd_name[0] == 0) {
3164                 bta_dm_cb.pin_evt = pin_evt;
3165                 bdcpy(bta_dm_cb.pin_bd_addr, p_data->key_notif.bd_addr);
3166                 BTA_COPY_DEVICE_CLASS(bta_dm_cb.pin_dev_class, p_data->key_notif.dev_class);
3167                 if ((BTM_ReadRemoteDeviceName(p_data->key_notif.bd_addr, bta_dm_pinname_cback,
3168                                               BT_TRANSPORT_BR_EDR)) == BTM_CMD_STARTED) {
3169                     return BTM_CMD_STARTED;
3170                 }
3171                 APPL_TRACE_WARNING(" bta_dm_sp_cback() -> Failed to start Remote Name Request  ");
3172             } else {
3173                 bdcpy(sec_event.key_notif.bd_addr, p_data->key_notif.bd_addr);
3174                 BTA_COPY_DEVICE_CLASS(sec_event.key_notif.dev_class, p_data->key_notif.dev_class);
3175                 BCM_STRNCPY_S((char *)sec_event.key_notif.bd_name, (char *)p_data->key_notif.bd_name, (BD_NAME_LEN - 1));
3176                 sec_event.key_notif.bd_name[BD_NAME_LEN - 1] = 0;
3177             }
3178         }
3179 
3180         if (BTM_SP_KEY_REQ_EVT == event) {
3181             pin_evt = BTA_DM_SP_KEY_REQ_EVT;
3182             /* If the device name is not known, save bdaddr and devclass
3183                and initiate a name request with values from key_notif */
3184             if (p_data->key_notif.bd_name[0] == 0) {
3185                 bta_dm_cb.pin_evt = pin_evt;
3186                 bdcpy(bta_dm_cb.pin_bd_addr, p_data->key_notif.bd_addr);
3187                 BTA_COPY_DEVICE_CLASS(bta_dm_cb.pin_dev_class, p_data->key_notif.dev_class);
3188                 if ((BTM_ReadRemoteDeviceName(p_data->key_notif.bd_addr, bta_dm_pinname_cback,
3189                                               BT_TRANSPORT_BR_EDR)) == BTM_CMD_STARTED) {
3190                     return BTM_CMD_STARTED;
3191                 }
3192                 APPL_TRACE_WARNING(" bta_dm_sp_cback() -> Failed to start Remote Name Request  ");
3193             } else {
3194                 bdcpy(sec_event.key_notif.bd_addr, p_data->key_notif.bd_addr);
3195                 BTA_COPY_DEVICE_CLASS(sec_event.key_notif.dev_class, p_data->key_notif.dev_class);
3196                 BCM_STRNCPY_S((char *)sec_event.key_notif.bd_name,(char *)p_data->key_notif.bd_name, (BD_NAME_LEN - 1));
3197                 sec_event.key_notif.bd_name[BD_NAME_LEN - 1] = 0;
3198             }
3199         }
3200         bta_dm_cb.p_sec_cback(pin_evt, &sec_event);
3201 
3202         break;
3203 
3204 #if BTM_OOB_INCLUDED == TRUE
3205     case BTM_SP_LOC_OOB_EVT:
3206         bta_dm_co_loc_oob((BOOLEAN)(p_data->loc_oob.status == BTM_SUCCESS),
3207                           p_data->loc_oob.c, p_data->loc_oob.r);
3208         break;
3209 
3210     case BTM_SP_RMT_OOB_EVT:
3211         /* If the device name is not known, save bdaddr and devclass and initiate a name request */
3212         if (p_data->rmt_oob.bd_name[0] == 0) {
3213             bta_dm_cb.pin_evt = BTA_DM_SP_RMT_OOB_EVT;
3214             bdcpy(bta_dm_cb.pin_bd_addr, p_data->rmt_oob.bd_addr);
3215             BTA_COPY_DEVICE_CLASS(bta_dm_cb.pin_dev_class, p_data->rmt_oob.dev_class);
3216             if ((BTM_ReadRemoteDeviceName(p_data->rmt_oob.bd_addr, bta_dm_pinname_cback,
3217                                           BT_TRANSPORT_BR_EDR)) == BTM_CMD_STARTED) {
3218                 return BTM_CMD_STARTED;
3219             }
3220             APPL_TRACE_WARNING(" bta_dm_sp_cback() -> Failed to start Remote Name Request  ");
3221         }
3222 
3223         bdcpy(sec_event.rmt_oob.bd_addr, p_data->rmt_oob.bd_addr);
3224         BTA_COPY_DEVICE_CLASS(sec_event.rmt_oob.dev_class, p_data->rmt_oob.dev_class);
3225         BCM_STRNCPY_S((char *)sec_event.rmt_oob.bd_name, (char *)p_data->rmt_oob.bd_name, (BD_NAME_LEN - 1));
3226         sec_event.rmt_oob.bd_name[BD_NAME_LEN - 1] = 0;
3227 
3228         bta_dm_cb.p_sec_cback(BTA_DM_SP_RMT_OOB_EVT, &sec_event);
3229 
3230         bta_dm_co_rmt_oob(p_data->rmt_oob.bd_addr);
3231         break;
3232 #endif
3233     case BTM_SP_COMPLT_EVT:
3234         /* do not report this event - handled by link_key_callback or auth_complete_callback */
3235         break;
3236 
3237     case BTM_SP_KEYPRESS_EVT:
3238         memcpy(&sec_event.key_press, &p_data->key_press, sizeof(tBTM_SP_KEYPRESS));
3239         bta_dm_cb.p_sec_cback(BTA_DM_SP_KEYPRESS_EVT, &sec_event);
3240         break;
3241 
3242     case BTM_SP_UPGRADE_EVT:
3243         bta_dm_co_lk_upgrade(p_data->upgrade.bd_addr, &p_data->upgrade.upgrade );
3244         break;
3245 
3246     default:
3247         status = BTM_NOT_AUTHORIZED;
3248         break;
3249     }
3250     APPL_TRACE_EVENT("dm status: %d", status);
3251     return status;
3252 }
3253 #endif /* (BT_SSP_INCLUDED == TRUE) */
3254 
3255 #endif  ///SMP_INCLUDED == TRUE
3256 
3257 
3258 /*******************************************************************************
3259 **
3260 ** Function         bta_dm_local_name_cback
3261 **
3262 ** Description      Callback from btm after local name is read
3263 **
3264 **
3265 ** Returns          void
3266 **
3267 *******************************************************************************/
bta_dm_local_name_cback(UINT8 * p_name)3268 static void bta_dm_local_name_cback(UINT8 *p_name)
3269 {
3270     tBTA_DM_SEC sec_event;
3271     UNUSED(p_name);
3272 
3273     sec_event.enable.status = BTA_SUCCESS;
3274 
3275     if (bta_dm_cb.p_sec_cback) {
3276         bta_dm_cb.p_sec_cback(BTA_DM_ENABLE_EVT, &sec_event);
3277     }
3278 
3279 }
3280 
3281 /*******************************************************************************
3282 **
3283 ** Function         bta_dm_bl_change_cback
3284 **
3285 ** Description      Callback from btm when acl connection goes up or down
3286 **
3287 **
3288 ** Returns          void
3289 **
3290 *******************************************************************************/
bta_dm_bl_change_cback(tBTM_BL_EVENT_DATA * p_data)3291 static void bta_dm_bl_change_cback (tBTM_BL_EVENT_DATA *p_data)
3292 {
3293     tBTA_DM_ACL_CHANGE *p_msg;
3294 
3295     if ((p_msg = (tBTA_DM_ACL_CHANGE *) osi_malloc(sizeof(tBTA_DM_ACL_CHANGE))) != NULL) {
3296         p_msg->event = p_data->event;
3297         p_msg->is_new = FALSE;
3298 
3299         switch (p_msg->event) {
3300         case BTM_BL_CONN_EVT:
3301             p_msg->sc_downgrade = p_data->conn.sc_downgrade;
3302             p_msg->is_new = TRUE;
3303             bdcpy(p_msg->bd_addr, p_data->conn.p_bda);
3304 #if BLE_INCLUDED == TRUE
3305             p_msg->transport = p_data->conn.transport;
3306             p_msg->handle = p_data->conn.handle;
3307 #endif
3308             break;
3309         case BTM_BL_DISCN_EVT:
3310             bdcpy(p_msg->bd_addr, p_data->discn.p_bda);
3311 #if BLE_INCLUDED == TRUE
3312             p_msg->transport = p_data->discn.transport;
3313             p_msg->handle = p_data->discn.handle;
3314 #endif
3315             break;
3316         case BTM_BL_UPDATE_EVT:
3317             p_msg->busy_level = p_data->update.busy_level;
3318             p_msg->busy_level_flags = p_data->update.busy_level_flags;
3319             break;
3320         case BTM_BL_ROLE_CHG_EVT:
3321             p_msg->new_role = p_data->role_chg.new_role;
3322             p_msg->hci_status = p_data->role_chg.hci_status;
3323             bdcpy(p_msg->bd_addr, p_data->role_chg.p_bda);
3324             break;
3325         case BTM_BL_COLLISION_EVT:
3326             bdcpy(p_msg->bd_addr, p_data->conn.p_bda);
3327             break;
3328         }
3329 
3330         p_msg->hdr.event = BTA_DM_ACL_CHANGE_EVT;
3331         bta_sys_sendmsg(p_msg);
3332 
3333     }
3334 
3335 }
3336 
3337 /*******************************************************************************
3338 **
3339 ** Function         bta_dm_rs_cback
3340 **
3341 ** Description      Receives the role switch complete event
3342 **
3343 ** Returns
3344 **
3345 *******************************************************************************/
bta_dm_rs_cback(tBTM_ROLE_SWITCH_CMPL * p1)3346 static void bta_dm_rs_cback (tBTM_ROLE_SWITCH_CMPL *p1)
3347 {
3348     UNUSED(p1);
3349     APPL_TRACE_WARNING("bta_dm_rs_cback:%d", bta_dm_cb.rs_event);
3350     if (bta_dm_cb.rs_event == BTA_DM_API_SEARCH_EVT) {
3351         bta_dm_cb.search_msg.rs_res = BTA_DM_RS_OK; /* do not care about the result for now */
3352         bta_dm_cb.rs_event = 0;
3353         bta_dm_search_start((tBTA_DM_MSG *)&bta_dm_cb.search_msg);
3354     }
3355 }
3356 
3357 /*******************************************************************************
3358 **
3359 ** Function         bta_dm_check_av
3360 **
3361 ** Description      This function checks if AV is active
3362 **                  if yes, make sure the AV link is master
3363 **
3364 ** Returns          BOOLEAN - TRUE, if switch is in progress
3365 **
3366 *******************************************************************************/
bta_dm_check_av(UINT16 event)3367 static BOOLEAN bta_dm_check_av(UINT16 event)
3368 {
3369     BOOLEAN avoid_roleswitch = FALSE;
3370     BOOLEAN switching = FALSE;
3371     UINT8 i;
3372     tBTA_DM_PEER_DEVICE *p_dev;
3373 
3374 #if defined(BTA_DM_AVOID_A2DP_ROLESWITCH_ON_INQUIRY) && (BTA_DM_AVOID_A2DP_ROLESWITCH_ON_INQUIRY == TRUE)
3375 
3376     /* avoid role switch upon inquiry if a2dp is actively streaming as it
3377        introduces an audioglitch due to FW scheduling delays (unavoidable) */
3378     if (event == BTA_DM_API_SEARCH_EVT) {
3379         avoid_roleswitch = TRUE;
3380     }
3381 #endif
3382 
3383     APPL_TRACE_EVENT("bta_dm_check_av:%d", bta_dm_cb.cur_av_count);
3384     if (bta_dm_cb.cur_av_count) {
3385         for (i = 0; i < bta_dm_cb.device_list.count; i++) {
3386             p_dev = &bta_dm_cb.device_list.peer_device[i];
3387             APPL_TRACE_WARNING("[%d]: state:%d, info:x%x, avoid_rs %d",
3388                                i, p_dev->conn_state, p_dev->info, avoid_roleswitch);
3389             if ((p_dev->conn_state == BTA_DM_CONNECTED) && (p_dev->info & BTA_DM_DI_AV_ACTIVE) &&
3390                     (avoid_roleswitch == FALSE)) {
3391                 /* make master and take away the role switch policy */
3392                 if (BTM_CMD_STARTED == BTM_SwitchRole (p_dev->peer_bdaddr, HCI_ROLE_MASTER, (tBTM_CMPL_CB *)bta_dm_rs_cback)) {
3393                     /* the role switch command is actually sent */
3394                     bta_dm_cb.rs_event = event;
3395                     switching = TRUE;
3396                 }
3397                 /* else either already master or can not switch for some reasons */
3398                 bta_dm_policy_cback(BTA_SYS_PLCY_CLR, 0, HCI_ENABLE_MASTER_SLAVE_SWITCH, p_dev->peer_bdaddr);
3399                 break;
3400             }
3401         }
3402     }
3403     return switching;
3404 }
3405 
3406 /*******************************************************************************
3407 **
3408 ** Function         bta_dm_acl_change
3409 **
3410 ** Description      Process BTA_DM_ACL_CHANGE_EVT
3411 **
3412 **
3413 ** Returns          void
3414 **
3415 *******************************************************************************/
bta_dm_acl_change(tBTA_DM_MSG * p_data)3416 void bta_dm_acl_change(tBTA_DM_MSG *p_data)
3417 {
3418 
3419     UINT8 i;
3420     UINT8 *p;
3421     tBTA_DM_SEC conn;
3422     BOOLEAN is_new = p_data->acl_change.is_new;
3423     BD_ADDR_PTR     p_bda = p_data->acl_change.bd_addr;
3424     BOOLEAN         need_policy_change = FALSE;
3425     BOOLEAN         issue_unpair_cb = FALSE;
3426 
3427     tBTA_DM_PEER_DEVICE *p_dev;
3428     memset(&conn, 0, sizeof(tBTA_DM_SEC));
3429 
3430     switch (p_data->acl_change.event) {
3431     case BTM_BL_UPDATE_EVT:     /* busy level update */
3432         if ( bta_dm_cb.p_sec_cback ) {
3433             conn.busy_level.level = p_data->acl_change.busy_level;
3434             conn.busy_level.level_flags = p_data->acl_change.busy_level_flags;
3435             bta_dm_cb.p_sec_cback(BTA_DM_BUSY_LEVEL_EVT, &conn);
3436         }
3437         return;
3438 
3439     case BTM_BL_ROLE_CHG_EVT:   /* role change event */
3440         p_dev = bta_dm_find_peer_device(p_bda);
3441         if (p_dev) {
3442             APPL_TRACE_DEBUG("bta_dm_acl_change role chg info:x%x new_role:%d dev count:%d",
3443                              p_dev->info, p_data->acl_change.new_role, bta_dm_cb.device_list.count);
3444             if (p_dev->info & BTA_DM_DI_AV_ACTIVE) {
3445                 /* there's AV activity on this link */
3446                 if (p_data->acl_change.new_role == HCI_ROLE_SLAVE && bta_dm_cb.device_list.count > 1
3447                         && p_data->acl_change.hci_status == HCI_SUCCESS) {
3448                     /* more than one connections and the AV connection is role switched to slave
3449                      * switch it back to master and remove the switch policy */
3450                     BTM_SwitchRole(p_bda, BTM_ROLE_MASTER, NULL);
3451                     need_policy_change = TRUE;
3452                 } else if (p_bta_dm_cfg->avoid_scatter && (p_data->acl_change.new_role == HCI_ROLE_MASTER)) {
3453                     /* if the link updated to be master include AV activities, remove the switch policy */
3454                     need_policy_change = TRUE;
3455                 }
3456 
3457                 if (need_policy_change) {
3458                     bta_dm_policy_cback(BTA_SYS_PLCY_CLR, 0, HCI_ENABLE_MASTER_SLAVE_SWITCH, p_dev->peer_bdaddr);
3459                 }
3460             } else {
3461                 /* there's AV no activity on this link and role switch happened
3462                  * check if AV is active
3463                  * if so, make sure the AV link is master */
3464                 bta_dm_check_av(0);
3465             }
3466             bta_sys_notify_role_chg(p_data->acl_change.bd_addr, p_data->acl_change.new_role, p_data->acl_change.hci_status);
3467             bdcpy(conn.role_chg.bd_addr, p_bda);
3468             conn.role_chg.new_role = (UINT8) p_data->acl_change.new_role;
3469             if ( bta_dm_cb.p_sec_cback ) {
3470                 bta_dm_cb.p_sec_cback(BTA_DM_ROLE_CHG_EVT, (tBTA_DM_SEC *)&conn);
3471             }
3472         }
3473         return;
3474     }
3475 
3476     /* Collision report from Stack: Notify profiles */
3477     if (p_data->acl_change.event == BTM_BL_COLLISION_EVT) {
3478         bta_sys_notify_collision (p_bda);
3479         return;
3480     }
3481 
3482     if (is_new) {
3483         for (i = 0; i < bta_dm_cb.device_list.count; i++) {
3484             if (!bdcmp( bta_dm_cb.device_list.peer_device[i].peer_bdaddr, p_bda)
3485 #if BLE_INCLUDED == TRUE
3486                     && bta_dm_cb.device_list.peer_device[i].conn_handle == p_data->acl_change.handle
3487 #endif
3488                ) {
3489                 break;
3490             }
3491 
3492         }
3493 
3494         if (i == bta_dm_cb.device_list.count) {
3495             if (bta_dm_cb.device_list.count < BTA_DM_NUM_PEER_DEVICE) {
3496                 bdcpy(bta_dm_cb.device_list.peer_device[bta_dm_cb.device_list.count].peer_bdaddr, p_bda);
3497                 bta_dm_cb.device_list.peer_device[bta_dm_cb.device_list.count].link_policy = bta_dm_cb.cur_policy;
3498                 bta_dm_cb.device_list.count++;
3499 #if BLE_INCLUDED == TRUE
3500                 bta_dm_cb.device_list.peer_device[i].conn_handle = p_data->acl_change.handle;
3501                 if (p_data->acl_change.transport == BT_TRANSPORT_LE) {
3502                     bta_dm_cb.device_list.le_count++;
3503                 }
3504 #endif
3505             } else {
3506                 APPL_TRACE_ERROR("%s max active connection reached, no resources", __func__);
3507                 return;
3508             }
3509         }
3510 
3511         bta_dm_cb.device_list.peer_device[i].conn_state = BTA_DM_CONNECTED;
3512         bta_dm_cb.device_list.peer_device[i].pref_role = BTA_ANY_ROLE;
3513         bdcpy(conn.link_up.bd_addr, p_bda);
3514         bta_dm_cb.device_list.peer_device[i].info = BTA_DM_DI_NONE;
3515 #if BLE_INCLUDED == TRUE
3516         conn.link_up.link_type = p_data->acl_change.transport;
3517         bta_dm_cb.device_list.peer_device[i].transport = p_data->acl_change.transport;
3518 #endif
3519 
3520         if (((NULL != (p = BTM_ReadLocalFeatures ())) && HCI_SNIFF_SUB_RATE_SUPPORTED(p)) &&
3521                 ((NULL != (p = BTM_ReadRemoteFeatures (p_bda))) && HCI_SNIFF_SUB_RATE_SUPPORTED(p))) {
3522             /* both local and remote devices support SSR */
3523             bta_dm_cb.device_list.peer_device[i].info = BTA_DM_DI_USE_SSR;
3524         }
3525         APPL_TRACE_DEBUG("%s info: 0x%x", __func__, bta_dm_cb.device_list.peer_device[i].info);
3526 
3527         if (bta_dm_cb.p_sec_cback) {
3528             conn.link_up.sc_downgrade = p_data->acl_change.sc_downgrade;
3529             bta_dm_cb.p_sec_cback(BTA_DM_LINK_UP_EVT, (tBTA_DM_SEC *)&conn);
3530         }
3531     } else {
3532         for (i = 0; i < bta_dm_cb.device_list.count; i++) {
3533             if (bdcmp( bta_dm_cb.device_list.peer_device[i].peer_bdaddr, p_bda)
3534 #if BLE_INCLUDED == TRUE
3535                     || bta_dm_cb.device_list.peer_device[i].transport != p_data->acl_change.transport
3536 #endif
3537                ) {
3538                 continue;
3539             }
3540 
3541             if ( bta_dm_cb.device_list.peer_device[i].conn_state == BTA_DM_UNPAIRING ) {
3542                 if (BTM_SecDeleteDevice(bta_dm_cb.device_list.peer_device[i].peer_bdaddr, bta_dm_cb.device_list.peer_device[i].transport)) {
3543                     issue_unpair_cb = TRUE;
3544                 }
3545 
3546                 APPL_TRACE_DEBUG("%s: Unpairing: issue unpair CB = %d ", __FUNCTION__, issue_unpair_cb);
3547             }
3548 
3549             conn.link_down.is_removed = bta_dm_cb.device_list.peer_device[i].remove_dev_pending;
3550 
3551             for (; i < bta_dm_cb.device_list.count ; i++) {
3552                 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]));
3553             }
3554             break;
3555         }
3556         if (bta_dm_cb.device_list.count) {
3557             bta_dm_cb.device_list.count--;
3558         }
3559 #if BLE_INCLUDED == TRUE
3560         if ((p_data->acl_change.transport == BT_TRANSPORT_LE) &&
3561                 (bta_dm_cb.device_list.le_count)) {
3562             bta_dm_cb.device_list.le_count--;
3563         }
3564         conn.link_down.link_type = p_data->acl_change.transport;
3565 #endif
3566 
3567         if (bta_dm_search_cb.wait_disc && !bdcmp(bta_dm_search_cb.peer_bdaddr, p_bda)) {
3568             bta_dm_search_cb.wait_disc = FALSE;
3569 
3570             if (bta_dm_search_cb.sdp_results) {
3571                 APPL_TRACE_EVENT(" timer stopped  ");
3572                 bta_sys_stop_timer(&bta_dm_search_cb.search_timer);
3573                 bta_dm_discover_next_device();
3574             }
3575 
3576         }
3577 
3578         if (bta_dm_cb.disabling) {
3579             if (!BTM_GetNumAclLinks()) {
3580                 bta_sys_stop_timer(&bta_dm_cb.disable_timer);
3581                 bta_dm_cb.disable_timer.p_cback = (TIMER_CBACK *)&bta_dm_disable_conn_down_timer_cback;
3582                 /*
3583                  * Start a timer to make sure that the profiles
3584                  * get the disconnect event.
3585                  */
3586                 bta_sys_start_timer(&bta_dm_cb.disable_timer, 0, 1000);
3587             }
3588         }
3589         if (conn.link_down.is_removed) {
3590             BTM_SecDeleteDevice(p_bda, p_data->acl_change.transport);
3591 #if (BLE_INCLUDED == TRUE && GATTC_INCLUDED == TRUE)
3592             /* need to remove all pending background connection */
3593             BTA_GATTC_CancelOpen(0, p_bda, FALSE);
3594             /* remove all cached GATT information */
3595             BTA_GATTC_Refresh(p_bda, false);
3596 #endif
3597         }
3598 
3599         bdcpy(conn.link_down.bd_addr, p_bda);
3600         conn.link_down.reason = (UINT8) btm_get_acl_disc_reason_code();
3601         if ( bta_dm_cb.p_sec_cback ) {
3602             bta_dm_cb.p_sec_cback(BTA_DM_LINK_DOWN_EVT, &conn);
3603             if ( issue_unpair_cb ) {
3604                 if (p_data->acl_change.transport == BT_TRANSPORT_LE) {
3605                     bta_dm_cb.p_sec_cback(BTA_DM_BLE_DEV_UNPAIRED_EVT, &conn);
3606                 } else {
3607                     bta_dm_cb.p_sec_cback(BTA_DM_DEV_UNPAIRED_EVT, &conn);
3608                 }
3609             }
3610         }
3611     }
3612 
3613     bta_dm_adjust_roles(TRUE);
3614 }
3615 
3616 /*******************************************************************************
3617 **
3618 ** Function         bta_dm_disable_conn_down_timer_cback
3619 **
3620 ** Description      Sends disable event to application
3621 **
3622 **
3623 ** Returns          void
3624 **
3625 *******************************************************************************/
bta_dm_disable_conn_down_timer_cback(TIMER_LIST_ENT * p_tle)3626 static void bta_dm_disable_conn_down_timer_cback (TIMER_LIST_ENT *p_tle)
3627 {
3628     UNUSED(p_tle);
3629     tBTA_SYS_HW_MSG *sys_enable_event;
3630 
3631 #if (BTA_DM_PM_INCLUDED == TRUE)
3632     /* disable the power managment module */
3633     bta_dm_disable_pm();
3634 #endif /* #if (BTA_DM_PM_INCLUDED == TRUE) */
3635 
3636     /* register our callback to SYS HW manager */
3637     bta_sys_hw_register( BTA_SYS_HW_BLUETOOTH, bta_dm_sys_hw_cback );
3638 
3639     /* send a message to BTA SYS */
3640     if ((sys_enable_event = (tBTA_SYS_HW_MSG *) osi_malloc(sizeof(tBTA_SYS_HW_MSG))) != NULL) {
3641         sys_enable_event->hdr.event = BTA_SYS_API_DISABLE_EVT;
3642         sys_enable_event->hw_module = BTA_SYS_HW_BLUETOOTH;
3643         bta_sys_sendmsg(sys_enable_event);
3644     }
3645 
3646     bta_dm_cb.disabling = FALSE;
3647 
3648 }
3649 
3650 /*******************************************************************************
3651 **
3652 ** Function         bta_dm_rm_cback
3653 **
3654 ** Description      Role management callback from sys
3655 **
3656 **
3657 ** Returns          void
3658 **
3659 *******************************************************************************/
bta_dm_rm_cback(tBTA_SYS_CONN_STATUS status,UINT8 id,UINT8 app_id,BD_ADDR peer_addr)3660 static void bta_dm_rm_cback(tBTA_SYS_CONN_STATUS status, UINT8 id, UINT8 app_id, BD_ADDR peer_addr)
3661 {
3662     UINT8 j;
3663     tBTA_PREF_ROLES role;
3664     tBTA_DM_PEER_DEVICE *p_dev;
3665 
3666     p_dev = bta_dm_find_peer_device(peer_addr);
3667     if ( status == BTA_SYS_CONN_OPEN) {
3668         if (p_dev) {
3669             /* Do not set to connected if we are in the middle of unpairing. When AV stream is
3670              * started it fakes out a SYS_CONN_OPEN to potentially trigger a role switch command.
3671              * But this should not be done if we are in the middle of unpairing.
3672              */
3673             if (p_dev->conn_state != BTA_DM_UNPAIRING) {
3674                 p_dev->conn_state = BTA_DM_CONNECTED;
3675             }
3676 
3677             for (j = 1; j <= p_bta_dm_rm_cfg[0].app_id; j++) {
3678                 if (((p_bta_dm_rm_cfg[j].app_id == app_id) || (p_bta_dm_rm_cfg[j].app_id == BTA_ALL_APP_ID))
3679                         && (p_bta_dm_rm_cfg[j].id == id)) {
3680                     role = p_bta_dm_rm_cfg[j].cfg;
3681 
3682                     if (role > p_dev->pref_role ) {
3683                         p_dev->pref_role = role;
3684                     }
3685                     break;
3686                 }
3687             }
3688         }
3689     }
3690 
3691     if ((BTA_ID_AV == id) || (BTA_ID_AVK == id)) {
3692         if ( status == BTA_SYS_CONN_BUSY) {
3693             if (p_dev) {
3694                 p_dev->info |= BTA_DM_DI_AV_ACTIVE;
3695             }
3696             /* AV calls bta_sys_conn_open with the A2DP stream count as app_id */
3697             if (BTA_ID_AV == id) {
3698                 bta_dm_cb.cur_av_count = bta_dm_get_av_count();
3699             }
3700         } else if ( status == BTA_SYS_CONN_IDLE) {
3701             if (p_dev) {
3702                 p_dev->info &= ~BTA_DM_DI_AV_ACTIVE;
3703             }
3704 
3705             /* get cur_av_count from connected services */
3706             if (BTA_ID_AV == id) {
3707                 bta_dm_cb.cur_av_count = bta_dm_get_av_count();
3708             }
3709         }
3710         APPL_TRACE_EVENT("bta_dm_rm_cback:%d, status:%d", bta_dm_cb.cur_av_count, status);
3711     }
3712 
3713     /* Don't adjust roles for each busy/idle state transition to avoid
3714        excessive switch requests when individual profile busy/idle status
3715        changes */
3716     if ((status != BTA_SYS_CONN_BUSY) && (status != BTA_SYS_CONN_IDLE)) {
3717         bta_dm_adjust_roles(FALSE);
3718     }
3719 }
3720 
3721 /*******************************************************************************
3722 **
3723 ** Function         bta_dm_delay_role_switch_cback
3724 **
3725 ** Description      Callback from btm to delay a role switch
3726 **
3727 ** Returns          void
3728 **
3729 *******************************************************************************/
bta_dm_delay_role_switch_cback(TIMER_LIST_ENT * p_tle)3730 static void bta_dm_delay_role_switch_cback(TIMER_LIST_ENT *p_tle)
3731 {
3732     UNUSED(p_tle);
3733     APPL_TRACE_EVENT("bta_dm_delay_role_switch_cback: initiating Delayed RS");
3734     bta_dm_adjust_roles (FALSE);
3735 }
3736 
3737 /*******************************************************************************
3738 **
3739 ** Function         bta_dm_remove_sec_dev_entry
3740 **
3741 ** Description      Removes device entry from Security device DB if ACL connection with
3742 **                  remtoe device does not exist, else schedule for dev entry removal upon
3743                      ACL close
3744 **
3745 ** Returns          TRUE if device entry is removed from Security device DB, FALSE otherwise
3746 **
3747 *******************************************************************************/
3748 #if (SMP_INCLUDED == TRUE)
bta_dm_remove_sec_dev_entry(BD_ADDR remote_bd_addr)3749 static BOOLEAN bta_dm_remove_sec_dev_entry(BD_ADDR remote_bd_addr)
3750 {
3751     BOOLEAN is_device_deleted = FALSE;
3752     UINT16 index = 0;
3753     if ( BTM_IsAclConnectionUp(remote_bd_addr, BT_TRANSPORT_LE) ||
3754             BTM_IsAclConnectionUp(remote_bd_addr, BT_TRANSPORT_BR_EDR)) {
3755         APPL_TRACE_DEBUG("%s ACL is not down. Schedule for  Dev Removal when ACL closes",
3756                          __FUNCTION__);
3757         BTM_SecClearSecurityFlags (remote_bd_addr);
3758         for (index = 0; index < bta_dm_cb.device_list.count; index ++) {
3759             if (!bdcmp( bta_dm_cb.device_list.peer_device[index].peer_bdaddr, remote_bd_addr)) {
3760                 break;
3761             }
3762         }
3763         if (index != bta_dm_cb.device_list.count) {
3764             bta_dm_cb.device_list.peer_device[index].remove_dev_pending = TRUE;
3765         } else {
3766             APPL_TRACE_ERROR(" %s Device does not exist in DB", __FUNCTION__);
3767         }
3768     } else {
3769         is_device_deleted = BTM_SecDeleteDevice (remote_bd_addr, bta_dm_cb.device_list.peer_device[index].transport);
3770 #if (BLE_INCLUDED == TRUE && GATTC_INCLUDED == TRUE)
3771         /* need to remove all pending background connection */
3772         BTA_GATTC_CancelOpen(0, remote_bd_addr, FALSE);
3773         /* remove all cached GATT information */
3774         BTA_GATTC_Refresh(remote_bd_addr, false);
3775 #endif
3776     }
3777     return is_device_deleted;
3778 }
3779 #endif  ///SMP_INCLUDED == TRUE
3780 
3781 
3782 /*******************************************************************************
3783 **
3784 ** Function         bta_dm_adjust_roles
3785 **
3786 ** Description      Adjust roles
3787 **
3788 **
3789 ** Returns          void
3790 **
3791 *******************************************************************************/
bta_dm_adjust_roles(BOOLEAN delay_role_switch)3792 static void bta_dm_adjust_roles(BOOLEAN delay_role_switch)
3793 {
3794 
3795     UINT8 i;
3796     BOOLEAN set_master_role = FALSE;
3797 #if BLE_INCLUDED == TRUE
3798     UINT8 br_count = bta_dm_cb.device_list.count - bta_dm_cb.device_list.le_count;
3799 #else
3800     UINT8 br_count = bta_dm_cb.device_list.count;
3801 #endif
3802     if (br_count) {
3803 
3804         /* the configuration is no scatternet
3805          * or AV connection exists and there are more than one ACL link */
3806         if ( (p_bta_dm_rm_cfg[0].cfg == BTA_DM_NO_SCATTERNET) ||
3807                 (bta_dm_cb.cur_av_count && br_count > 1) ) {
3808 
3809             L2CA_SetDesireRole (HCI_ROLE_MASTER);
3810             set_master_role = TRUE;
3811 
3812         }
3813 
3814         for (i = 0; i < bta_dm_cb.device_list.count; i++) {
3815             if (bta_dm_cb.device_list.peer_device[i].conn_state == BTA_DM_CONNECTED
3816 #if BLE_INCLUDED == TRUE
3817                     && bta_dm_cb.device_list.peer_device[i].transport == BT_TRANSPORT_BR_EDR
3818 #endif
3819                ) {
3820                 if (!set_master_role && (bta_dm_cb.device_list.peer_device[i].pref_role != BTA_ANY_ROLE)
3821                         && (p_bta_dm_rm_cfg[0].cfg == BTA_DM_PARTIAL_SCATTERNET)) {
3822                     L2CA_SetDesireRole (HCI_ROLE_MASTER);
3823                     set_master_role = TRUE;
3824                 }
3825 
3826                 if ((bta_dm_cb.device_list.peer_device[i].pref_role == BTA_MASTER_ROLE_ONLY)
3827                         || (br_count > 1)) {
3828 
3829                     /* Initiating immediate role switch with certain remote devices
3830                       has caused issues due to role  switch colliding with link encryption setup and
3831                       causing encryption (and in turn the link) to fail .  These device . Firmware
3832                       versions are stored in a blacklist and role switch with these devices are
3833                       delayed to avoid the collision with link encryption setup */
3834 
3835                     if (bta_dm_cb.device_list.peer_device[i].pref_role != BTA_SLAVE_ROLE_ONLY &&
3836                             delay_role_switch == FALSE) {
3837                         BTM_SwitchRole (bta_dm_cb.device_list.peer_device[i].peer_bdaddr,
3838                                         HCI_ROLE_MASTER, NULL);
3839                     } else {
3840                         bta_dm_cb.switch_delay_timer[i].p_cback =
3841                             (TIMER_CBACK *)&bta_dm_delay_role_switch_cback;
3842                         /* Start the timer if not active */
3843                         if (!bta_sys_timer_is_active(&bta_dm_cb.switch_delay_timer[i])) {
3844                             bta_sys_start_timer(&bta_dm_cb.switch_delay_timer[i], 0, 500);
3845                         }
3846                     }
3847                 }
3848 
3849             }
3850         }
3851 
3852 
3853         if (!set_master_role) {
3854 
3855             L2CA_SetDesireRole (L2CAP_DESIRED_LINK_ROLE);
3856 
3857         }
3858 
3859     } else {
3860         L2CA_SetDesireRole (L2CAP_DESIRED_LINK_ROLE);
3861     }
3862 
3863 
3864 }
3865 
3866 /*******************************************************************************
3867 **
3868 ** Function         bta_dm_get_remname
3869 **
3870 ** Description      Returns a pointer to the remote name stored in the DM control
3871 **                  block if it exists, or from the BTM memory.
3872 **
3873 ** Returns          char * - Pointer to the remote device name
3874 *******************************************************************************/
3875 #if (SDP_INCLUDED == TRUE || SMP_INCLUDED == TRUE)
bta_dm_get_remname(void)3876 static char *bta_dm_get_remname(void)
3877 {
3878     char *p_name = (char *)bta_dm_search_cb.peer_name;
3879     char *p_temp;
3880 
3881     /* If the name isn't already stored, try retrieving from BTM */
3882     if (*p_name == '\0') {
3883         if ((p_temp = BTM_SecReadDevName(bta_dm_search_cb.peer_bdaddr)) != NULL) {
3884             p_name = p_temp;
3885         }
3886     }
3887     return p_name;
3888 }
3889 #endif  ///SDP_INCLUDED == TRUE || SMP_INCLUDED == TRUE
3890 
3891 /*******************************************************************************
3892 **
3893 ** Function         bta_dm_bond_cancel_complete_cback
3894 **
3895 ** Description      Authentication complete callback from BTM
3896 **
3897 ** Returns          void
3898 **
3899 *******************************************************************************/
3900 #if (SMP_INCLUDED == TRUE)
bta_dm_bond_cancel_complete_cback(tBTM_STATUS result)3901 static void bta_dm_bond_cancel_complete_cback(tBTM_STATUS result)
3902 {
3903 
3904     tBTA_DM_SEC sec_event;
3905 
3906     if (result == BTM_SUCCESS) {
3907         sec_event.bond_cancel_cmpl.result = BTA_SUCCESS;
3908     } else {
3909         sec_event.bond_cancel_cmpl.result = BTA_FAILURE;
3910     }
3911 
3912     if (bta_dm_cb.p_sec_cback) {
3913         bta_dm_cb.p_sec_cback(BTA_DM_BOND_CANCEL_CMPL_EVT, &sec_event);
3914     }
3915 }
3916 #endif  ///SMP_INCLUDED == TRUE
3917 
3918 /*******************************************************************************
3919 **
3920 ** Function         bta_dm_set_eir
3921 **
3922 ** Description      This function creates EIR tagged data and writes it to controller.
3923 **
3924 ** Returns          None
3925 **
3926 *******************************************************************************/
3927 #if (CLASSIC_BT_INCLUDED == TRUE)
bta_dm_set_eir(char * local_name)3928 static void bta_dm_set_eir (char *local_name)
3929 {
3930     BT_HDR   *p_buf;
3931     UINT8    *p;
3932     UINT8    *p_length;
3933 #if (BTA_EIR_CANNED_UUID_LIST != TRUE)
3934     UINT8    *p_type;
3935     UINT8    max_num_uuid;
3936 #if (BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0)
3937     UINT8    custom_uuid_idx;
3938 #endif  // BTA_EIR_SERVER_NUM_CUSTOM_UUID
3939 #endif  // BTA_EIR_CANNED_UUID_LIST
3940 
3941     UINT8    free_eir_length;
3942     if (p_bta_dm_eir_cfg->bta_dm_eir_fec_required) {
3943         free_eir_length = HCI_DM5_PACKET_SIZE;
3944     } else {
3945         free_eir_length = HCI_EXT_INQ_RESPONSE_LEN;
3946     }
3947 
3948     UINT8    num_uuid;
3949     UINT8    data_type;
3950     UINT8    local_name_len;
3951 
3952     UINT8    eir_type[BTM_EIR_TYPE_MAX_NUM];
3953     UINT8    eir_type_num = 0;
3954 
3955     tBTA_STATUS status = BTA_SUCCESS;
3956 
3957     /* wait until complete to disable */
3958     if (bta_dm_cb.disable_timer.in_use) {
3959         if (p_bta_dm_eir_cfg->config_eir_callback) {
3960             p_bta_dm_eir_cfg->config_eir_callback(BTA_WRONG_MODE, eir_type_num , eir_type);
3961         }
3962         return;
3963     }
3964 
3965 #if ( BTA_EIR_CANNED_UUID_LIST != TRUE )
3966     /* wait until App is ready */
3967     if (bta_dm_cb.app_ready_timer.in_use) {
3968         if (p_bta_dm_eir_cfg->config_eir_callback) {
3969             p_bta_dm_eir_cfg->config_eir_callback(BTA_WRONG_MODE, eir_type_num , eir_type);
3970         }
3971         return;
3972     }
3973 
3974     /* if local name is not provided, get it from controller */
3975     if ( local_name == NULL ) {
3976         if ( BTM_ReadLocalDeviceName( &local_name ) != BTM_SUCCESS ) {
3977             APPL_TRACE_ERROR("Fail to read local device name for EIR");
3978         }
3979     }
3980 #endif  // BTA_EIR_CANNED_UUID_LIST
3981 
3982     /* Allocate a buffer to hold HCI command */
3983     if ((p_buf = (BT_HDR *)osi_malloc(BTM_CMD_BUF_SIZE)) == NULL) {
3984         APPL_TRACE_ERROR("bta_dm_set_eir couldn't allocate buffer");
3985         if (p_bta_dm_eir_cfg->config_eir_callback) {
3986             p_bta_dm_eir_cfg->config_eir_callback(BTA_NO_RESOURCES, eir_type_num , eir_type);
3987         }
3988         return;
3989     }
3990     p = (UINT8 *)p_buf + BTM_HCI_EIR_OFFSET;
3991 
3992     memset(p, 0x00, HCI_EXT_INQ_RESPONSE_LEN );
3993 
3994     APPL_TRACE_DEBUG("BTA is generating EIR");
3995 
3996     if ( local_name ) {
3997         local_name_len = strlen( local_name );
3998     } else {
3999         local_name_len = 0;
4000     }
4001 
4002     data_type = BTM_EIR_COMPLETE_LOCAL_NAME_TYPE;
4003     /* if local name is longer than minimum length of shortened name */
4004     /* check whether it needs to be shortened or not */
4005     if ( local_name_len > p_bta_dm_eir_cfg->bta_dm_eir_min_name_len ) {
4006         /* get number of UUID 16-bit list */
4007 #if (BTA_EIR_CANNED_UUID_LIST == TRUE)
4008         num_uuid = p_bta_dm_eir_cfg->bta_dm_eir_uuid16_len / LEN_UUID_16;
4009 #else  // BTA_EIR_CANNED_UUID_LIST
4010         max_num_uuid = (free_eir_length - 2) / LEN_UUID_16;
4011         data_type = BTM_GetEirSupportedServices( bta_dm_cb.eir_uuid, &p,
4012                     max_num_uuid, &num_uuid );
4013         p = (UINT8 *)p_buf + BTM_HCI_EIR_OFFSET; /* reset p */
4014 #endif  // BTA_EIR_CANNED_UUID_LIST
4015 
4016         /* if UUID doesn't fit remaing space, shorten local name */
4017         if ( local_name_len > (free_eir_length - 4 - num_uuid * LEN_UUID_16)) {
4018             APPL_TRACE_WARNING("BTA EIR: local name is shortened");
4019             local_name_len = p_bta_dm_eir_cfg->bta_dm_eir_min_name_len;
4020             data_type = BTM_EIR_SHORTENED_LOCAL_NAME_TYPE;
4021         } else {
4022             data_type = BTM_EIR_COMPLETE_LOCAL_NAME_TYPE;
4023         }
4024     }
4025 
4026     UINT8_TO_STREAM(p, local_name_len + 1);
4027     UINT8_TO_STREAM(p, data_type);
4028     eir_type[eir_type_num++] = data_type;
4029 
4030     if (local_name != NULL) {
4031         memcpy(p, local_name, local_name_len);
4032         p += local_name_len;
4033     }
4034     free_eir_length -= local_name_len + 2;
4035 
4036     /* if UUIDs are provided in configuration */
4037     if (p_bta_dm_eir_cfg->bta_dm_eir_included_uuid) {
4038 #if (BTA_EIR_CANNED_UUID_LIST == TRUE)
4039         /* if UUID list is provided as static data in configuration */
4040         if (( p_bta_dm_eir_cfg->bta_dm_eir_uuid16_len > 0 )
4041                 && (p_bta_dm_eir_cfg->bta_dm_eir_uuid16)) {
4042             if ( free_eir_length > LEN_UUID_16 + 2) {
4043                 free_eir_length -= 2;
4044 
4045                 if ( free_eir_length >= p_bta_dm_eir_cfg->bta_dm_eir_uuid16_len) {
4046                     num_uuid = p_bta_dm_eir_cfg->bta_dm_eir_uuid16_len / LEN_UUID_16;
4047                     data_type = BTM_EIR_COMPLETE_16BITS_UUID_TYPE;
4048                 } else { /* not enough room for all UUIDs */
4049                     APPL_TRACE_WARNING("BTA EIR: UUID 16-bit list is truncated");
4050                     num_uuid = free_eir_length / LEN_UUID_16;
4051                     data_type = BTM_EIR_MORE_16BITS_UUID_TYPE;
4052                 }
4053                 UINT8_TO_STREAM(p, num_uuid * LEN_UUID_16 + 1);
4054                 UINT8_TO_STREAM(p, data_type);
4055                 eir_type[eir_type_num++] = data_type;
4056                 memcpy(p, p_bta_dm_eir_cfg->bta_dm_eir_uuid16, num_uuid * LEN_UUID_16 );
4057                 p += num_uuid * LEN_UUID_16;
4058                 free_eir_length -= num_uuid * LEN_UUID_16;
4059             } else {
4060                 status = BTA_EIR_TOO_LARGE;
4061             }
4062         }
4063 #else /* (BTA_EIR_CANNED_UUID_LIST == TRUE) */
4064         /* if UUID list is dynamic */
4065         if ( free_eir_length >= 2) {
4066             p_length = p++;
4067             p_type   = p++;
4068             num_uuid = 0;
4069 
4070             max_num_uuid = (free_eir_length - 2) / LEN_UUID_16;
4071             data_type = BTM_GetEirSupportedServices( bta_dm_cb.eir_uuid, &p, max_num_uuid, &num_uuid );
4072 
4073             if ( data_type == BTM_EIR_MORE_16BITS_UUID_TYPE ) {
4074                 APPL_TRACE_WARNING("BTA EIR: UUID 16-bit list is truncated");
4075             }
4076 #if (BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0)
4077             else {
4078                 for (custom_uuid_idx = 0; custom_uuid_idx < BTA_EIR_SERVER_NUM_CUSTOM_UUID; custom_uuid_idx++) {
4079                     if (bta_dm_cb.custom_uuid[custom_uuid_idx].len == LEN_UUID_16) {
4080                         if ( num_uuid < max_num_uuid ) {
4081                             UINT16_TO_STREAM(p, bta_dm_cb.custom_uuid[custom_uuid_idx].uu.uuid16);
4082                             num_uuid++;
4083                         } else {
4084                             data_type = BTM_EIR_MORE_16BITS_UUID_TYPE;
4085                             APPL_TRACE_WARNING("BTA EIR: UUID 16-bit list is truncated");
4086                             break;
4087                         }
4088                     }
4089                 }
4090             }
4091 #endif /* (BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0) */
4092 
4093             UINT8_TO_STREAM(p_length, num_uuid * LEN_UUID_16 + 1);
4094             UINT8_TO_STREAM(p_type, data_type);
4095             eir_type[eir_type_num++] = data_type;
4096             free_eir_length -= num_uuid * LEN_UUID_16 + 2;
4097         } else {
4098             status = BTA_EIR_TOO_LARGE;
4099         }
4100 #endif /* (BTA_EIR_CANNED_UUID_LIST == TRUE) */
4101 
4102 #if ( BTA_EIR_CANNED_UUID_LIST != TRUE )&&(BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0)
4103         /* Adding 32-bit UUID list */
4104         if ( free_eir_length >= 2) {
4105             p_length = p++;
4106             p_type   = p++;
4107             num_uuid = 0;
4108             data_type = BTM_EIR_COMPLETE_32BITS_UUID_TYPE;
4109 
4110             max_num_uuid = (free_eir_length - 2) / LEN_UUID_32;
4111 
4112             for (custom_uuid_idx = 0; custom_uuid_idx < BTA_EIR_SERVER_NUM_CUSTOM_UUID; custom_uuid_idx++) {
4113                 if (bta_dm_cb.custom_uuid[custom_uuid_idx].len == LEN_UUID_32) {
4114                     if ( num_uuid < max_num_uuid ) {
4115                         UINT32_TO_STREAM(p, bta_dm_cb.custom_uuid[custom_uuid_idx].uu.uuid32);
4116                         num_uuid++;
4117                     } else {
4118                         data_type = BTM_EIR_MORE_32BITS_UUID_TYPE;
4119                         APPL_TRACE_WARNING("BTA EIR: UUID 32-bit list is truncated");
4120                         break;
4121                     }
4122                 }
4123             }
4124 
4125             UINT8_TO_STREAM(p_length, num_uuid * LEN_UUID_32 + 1);
4126             UINT8_TO_STREAM(p_type, data_type);
4127             eir_type[eir_type_num++] = data_type;
4128             free_eir_length -= num_uuid * LEN_UUID_32 + 2;
4129         } else {
4130             status = BTA_EIR_TOO_LARGE;
4131         }
4132 
4133         /* Adding 128-bit UUID list */
4134         if ( free_eir_length >= 2) {
4135             p_length = p++;
4136             p_type   = p++;
4137             num_uuid = 0;
4138             data_type = BTM_EIR_COMPLETE_128BITS_UUID_TYPE;
4139 
4140             max_num_uuid = (free_eir_length - 2) / LEN_UUID_128;
4141 
4142             for (custom_uuid_idx = 0; custom_uuid_idx < BTA_EIR_SERVER_NUM_CUSTOM_UUID; custom_uuid_idx++) {
4143                 if (bta_dm_cb.custom_uuid[custom_uuid_idx].len == LEN_UUID_128) {
4144                     if ( num_uuid < max_num_uuid ) {
4145                         ARRAY16_TO_STREAM(p, bta_dm_cb.custom_uuid[custom_uuid_idx].uu.uuid128);
4146                         num_uuid++;
4147                     } else {
4148                         data_type = BTM_EIR_MORE_128BITS_UUID_TYPE;
4149                         APPL_TRACE_WARNING("BTA EIR: UUID 128-bit list is truncated");
4150                         break;
4151                     }
4152                 }
4153             }
4154 
4155             UINT8_TO_STREAM(p_length, num_uuid * LEN_UUID_128 + 1);
4156             UINT8_TO_STREAM(p_type, data_type);
4157             eir_type[eir_type_num++] = data_type;
4158             free_eir_length -= num_uuid * LEN_UUID_128 + 2;
4159         }
4160          else {
4161             status = BTA_EIR_TOO_LARGE;
4162         }
4163 #endif /* ( BTA_EIR_CANNED_UUID_LIST != TRUE )&&(BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0) */
4164     }
4165 
4166     /* if Flags are provided in configuration */
4167     if ( p_bta_dm_eir_cfg->bta_dm_eir_flags != 0 ) {
4168         if ( free_eir_length >= 3 ) {
4169             UINT8_TO_STREAM(p, 2);
4170             UINT8_TO_STREAM(p, BTM_EIR_FLAGS_TYPE);
4171             eir_type[eir_type_num++] = BTM_EIR_FLAGS_TYPE;
4172             UINT8_TO_STREAM(p, p_bta_dm_eir_cfg->bta_dm_eir_flags);
4173             free_eir_length -= 3;
4174         } else {
4175             status = BTA_EIR_TOO_LARGE;
4176         }
4177     }
4178 
4179     /* if Manufacturer Specific are provided in configuration */
4180     if (( p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len > 0 )
4181             && ( p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec )) {
4182         if ( free_eir_length >= p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len + 2) {
4183             p_length = p;
4184 
4185             UINT8_TO_STREAM(p, p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len + 1);
4186             UINT8_TO_STREAM(p, BTM_EIR_MANUFACTURER_SPECIFIC_TYPE);
4187             eir_type[eir_type_num++] = BTM_EIR_MANUFACTURER_SPECIFIC_TYPE;
4188             memcpy(p, p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec,
4189                p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len);
4190             p += p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len;
4191             free_eir_length -= p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len + 2;
4192         } else {
4193             status = BTA_EIR_TOO_LARGE;
4194         }
4195     } else {
4196         p_length = NULL;
4197     }
4198 
4199     /* if Inquiry Tx Resp Power compiled */
4200     if (p_bta_dm_eir_cfg->bta_dm_eir_included_tx_power) {
4201         if (free_eir_length >= 3) {
4202             int min_power_level, max_power_level;
4203             if (bredr_txpwr_get(&min_power_level, &max_power_level) == 0) {
4204                 INT8 btm_tx_power[BTM_TX_POWER_LEVEL_MAX + 1] = BTM_TX_POWER;
4205                 p_bta_dm_eir_cfg->bta_dm_eir_inq_tx_power = btm_tx_power[max_power_level];
4206                 UINT8_TO_STREAM(p, 2);      /* Length field */
4207                 UINT8_TO_STREAM(p, BTM_EIR_TX_POWER_LEVEL_TYPE);
4208                 eir_type[eir_type_num++] = BTM_EIR_TX_POWER_LEVEL_TYPE;
4209                 UINT8_TO_STREAM(p, p_bta_dm_eir_cfg->bta_dm_eir_inq_tx_power);
4210             free_eir_length -= 3;
4211             }
4212         } else {
4213             status = BTA_EIR_TOO_LARGE;
4214         }
4215     }
4216 
4217     /* if URL are provided in configuration */
4218     if (( p_bta_dm_eir_cfg->bta_dm_eir_url_len > 0 )
4219             && ( p_bta_dm_eir_cfg->bta_dm_eir_url )) {
4220         if ( free_eir_length >= p_bta_dm_eir_cfg->bta_dm_eir_url_len + 2 ) {
4221             UINT8_TO_STREAM(p, p_bta_dm_eir_cfg->bta_dm_eir_url_len + 1);
4222             UINT8_TO_STREAM(p, BTM_EIR_URL_TYPE);
4223             eir_type[eir_type_num++] = BTM_EIR_URL_TYPE;
4224             memcpy(p, p_bta_dm_eir_cfg->bta_dm_eir_url,
4225                p_bta_dm_eir_cfg->bta_dm_eir_url_len);
4226             p += p_bta_dm_eir_cfg->bta_dm_eir_url_len;
4227             free_eir_length -= p_bta_dm_eir_cfg->bta_dm_eir_url_len + 2;
4228         } else {
4229             status = BTA_EIR_TOO_LARGE;
4230         }
4231     }
4232 
4233     if ( free_eir_length ) {
4234         UINT8_TO_STREAM(p, 0);    /* terminator of significant part */
4235     }
4236 
4237     tBTM_STATUS btm_status = BTM_WriteEIR( p_buf, p_bta_dm_eir_cfg->bta_dm_eir_fec_required );
4238 
4239     if ( btm_status == BTM_MODE_UNSUPPORTED) {
4240         status = BTA_WRONG_MODE;
4241     } else if (btm_status != BTM_SUCCESS) {
4242         status = BTA_FAILURE;
4243     }
4244 
4245     if (p_bta_dm_eir_cfg->config_eir_callback) {
4246         p_bta_dm_eir_cfg->config_eir_callback(status, eir_type_num, eir_type);
4247     }
4248 }
4249 #endif
4250 
4251 /*******************************************************************************
4252 **
4253 ** Function         bta_dm_eir_search_services
4254 **
4255 ** Description      This function searches services in received EIR
4256 **
4257 ** Returns          None
4258 **
4259 *******************************************************************************/
4260 #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)4261 static void bta_dm_eir_search_services( tBTM_INQ_RESULTS  *p_result,
4262                                         tBTA_SERVICE_MASK *p_services_to_search,
4263                                         tBTA_SERVICE_MASK *p_services_found)
4264 {
4265     tBTA_SERVICE_MASK       service_index = 0;
4266     tBTM_EIR_SEARCH_RESULT  result;
4267 
4268     APPL_TRACE_DEBUG("BTA searching services in EIR of BDA:0x%02X%02X%02X%02X%02X%02X",
4269                      p_result->remote_bd_addr[0], p_result->remote_bd_addr[1],
4270                      p_result->remote_bd_addr[2], p_result->remote_bd_addr[3],
4271                      p_result->remote_bd_addr[4], p_result->remote_bd_addr[5]);
4272 
4273     APPL_TRACE_DEBUG("    with services_to_search=0x%08X", *p_services_to_search);
4274 
4275 #if BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE
4276     /* always do GATT based service discovery by SDP instead of from EIR    */
4277     /* if GATT based service is also to be put in EIR, need to modify this  */
4278     while (service_index < (BTA_MAX_SERVICE_ID - 1))
4279 #else
4280     while (service_index < BTA_MAX_SERVICE_ID)
4281 #endif
4282     {
4283         if ( *p_services_to_search
4284                 & (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(service_index))) {
4285             result = BTM_HasInquiryEirService( p_result,
4286                                                bta_service_id_to_uuid_lkup_tbl[service_index] );
4287 
4288             /* Searching for HSP v1.2 only device */
4289             if ((result != BTM_EIR_FOUND) &&
4290                     (bta_service_id_to_uuid_lkup_tbl[service_index] == UUID_SERVCLASS_HEADSET)) {
4291                 result = BTM_HasInquiryEirService (p_result, UUID_SERVCLASS_HEADSET_HS);
4292             }
4293 
4294             if ( result == BTM_EIR_FOUND ) {
4295                 /* If Plug and Play service record, need to check to see if Broadcom stack */
4296                 /* However, EIR data doesn't have EXT_BRCM_VERSION so just skip it */
4297                 if ( bta_service_id_to_uuid_lkup_tbl[service_index]
4298                         != UUID_SERVCLASS_PNP_INFORMATION ) {
4299 
4300                     *p_services_found |=
4301                         (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(service_index));
4302                     /* remove the service from services to be searched  */
4303                     *p_services_to_search &=
4304                         (tBTA_SERVICE_MASK)(~(BTA_SERVICE_ID_TO_SERVICE_MASK(service_index)));
4305                 }
4306             } else if ( result == BTM_EIR_NOT_FOUND ) {
4307                 /* remove the service from services to be searched  */
4308                 *p_services_to_search &=
4309                     (tBTA_SERVICE_MASK)(~(BTA_SERVICE_ID_TO_SERVICE_MASK(service_index)));
4310             }
4311         }
4312 
4313         service_index++;
4314     }
4315 
4316     APPL_TRACE_ERROR("BTA EIR search result, services_to_search=0x%08X, services_found=0x%08X",
4317                      *p_services_to_search, *p_services_found);
4318 }
4319 #endif  ///SDP_INCLUDED == TRUE
4320 
4321 #if (BTA_EIR_CANNED_UUID_LIST != TRUE)
4322 /*******************************************************************************
4323 **
4324 ** Function         bta_dm_eir_update_uuid
4325 **
4326 ** Description      This function adds or removes service UUID in EIR database.
4327 **
4328 ** Returns          None
4329 **
4330 *******************************************************************************/
bta_dm_eir_update_uuid(UINT16 uuid16,BOOLEAN adding)4331 void bta_dm_eir_update_uuid(UINT16 uuid16, BOOLEAN adding)
4332 {
4333     /* if this UUID is not advertised in EIR */
4334     if ( !BTM_HasEirService( p_bta_dm_eir_cfg->uuid_mask, uuid16 )) {
4335         return;
4336     }
4337 
4338     if ( adding ) {
4339         APPL_TRACE_EVENT("Adding UUID=0x%04X into EIR", uuid16);
4340 
4341         BTM_AddEirService( bta_dm_cb.eir_uuid, uuid16 );
4342     } else {
4343         APPL_TRACE_EVENT("Removing UUID=0x%04X from EIR", uuid16);
4344 
4345         BTM_RemoveEirService( bta_dm_cb.eir_uuid, uuid16 );
4346     }
4347 #if CLASSIC_BT_INCLUDED
4348     bta_dm_set_eir (NULL);
4349 #endif
4350 
4351     APPL_TRACE_EVENT("bta_dm_eir_update_uuid UUID bit mask=0x%08X %08X",
4352                      bta_dm_cb.eir_uuid[1], bta_dm_cb.eir_uuid[0] );
4353 }
4354 #endif
4355 
4356 /*******************************************************************************
4357 **
4358 ** Function         bta_dm_enable_test_mode
4359 **
4360 ** Description      enable test mode
4361 **
4362 **
4363 ** Returns          void
4364 **
4365 *******************************************************************************/
bta_dm_enable_test_mode(tBTA_DM_MSG * p_data)4366 void bta_dm_enable_test_mode(tBTA_DM_MSG *p_data)
4367 {
4368     UNUSED(p_data);
4369     BTM_EnableTestMode();
4370 }
4371 
4372 /*******************************************************************************
4373 **
4374 ** Function         bta_dm_disable_test_mode
4375 **
4376 ** Description      disable test mode
4377 **
4378 **
4379 ** Returns          void
4380 **
4381 *******************************************************************************/
bta_dm_disable_test_mode(tBTA_DM_MSG * p_data)4382 void bta_dm_disable_test_mode(tBTA_DM_MSG *p_data)
4383 {
4384     UNUSED(p_data);
4385     BTM_DeviceReset(NULL);
4386 }
4387 
4388 /*******************************************************************************
4389 **
4390 ** Function         bta_dm_execute_callback
4391 **
4392 ** Description      Just execute a generic call back in the context of the BTU/BTA tack
4393 **
4394 **
4395 ** Returns          void
4396 **
4397 *******************************************************************************/
bta_dm_execute_callback(tBTA_DM_MSG * p_data)4398 void bta_dm_execute_callback(tBTA_DM_MSG *p_data)
4399 {
4400     /* sanity check */
4401     if (p_data->exec_cback.p_exec_cback == NULL) {
4402         return;
4403     }
4404 
4405     p_data->exec_cback.p_exec_cback(p_data->exec_cback.p_param);
4406 }
4407 
4408 /*******************************************************************************
4409 **
4410 ** Function         bta_dm_encrypt_cback
4411 **
4412 ** Description      link encryption complete callback.
4413 **
4414 ** Returns         None
4415 **
4416 *******************************************************************************/
bta_dm_encrypt_cback(BD_ADDR bd_addr,tBT_TRANSPORT transport,void * p_ref_data,tBTM_STATUS result)4417 void bta_dm_encrypt_cback(BD_ADDR bd_addr, tBT_TRANSPORT transport, void *p_ref_data, tBTM_STATUS result)
4418 {
4419     tBTA_STATUS   bta_status = BTA_SUCCESS;
4420     tBTA_DM_ENCRYPT_CBACK *p_callback = NULL;
4421     UINT8   i ;
4422     UNUSED(p_ref_data);
4423 
4424     for (i = 0; i < bta_dm_cb.device_list.count; i++) {
4425         if (bdcmp( bta_dm_cb.device_list.peer_device[i].peer_bdaddr, bd_addr) == 0 &&
4426                 bta_dm_cb.device_list.peer_device[i].conn_state == BTA_DM_CONNECTED) {
4427             break;
4428         }
4429     }
4430 
4431     if (i < bta_dm_cb.device_list.count) {
4432         p_callback = bta_dm_cb.device_list.peer_device[i].p_encrypt_cback;
4433         bta_dm_cb.device_list.peer_device[i].p_encrypt_cback = NULL;
4434     }
4435 
4436     switch (result) {
4437     case BTM_SUCCESS:
4438         break;
4439     case BTM_WRONG_MODE:
4440         bta_status = BTA_WRONG_MODE;
4441         break;
4442     case BTM_NO_RESOURCES:
4443         bta_status = BTA_NO_RESOURCES;
4444         break;
4445     case BTM_BUSY:
4446         bta_status = BTA_BUSY;
4447         break;
4448     default:
4449         bta_status = BTA_FAILURE;
4450         break;
4451     }
4452 
4453     APPL_TRACE_DEBUG("bta_dm_encrypt_cback status =%d p_callback=%p", bta_status, p_callback);
4454 
4455     if (p_callback) {
4456         (*p_callback)(bd_addr, transport, bta_status);
4457     }
4458 }
4459 
4460 /*******************************************************************************
4461 **
4462 ** Function         bta_dm_set_encryption
4463 **
4464 ** Description      This function to encrypt the link
4465 **
4466 ** Returns          None
4467 **
4468 *******************************************************************************/
4469 #if (SMP_INCLUDED == TRUE)
bta_dm_set_encryption(tBTA_DM_MSG * p_data)4470 void bta_dm_set_encryption (tBTA_DM_MSG *p_data)
4471 {
4472     UINT8 i ;
4473 
4474     APPL_TRACE_DEBUG("bta_dm_set_encryption\n"); //todo
4475     if (!p_data->set_encryption.p_callback) {
4476         APPL_TRACE_ERROR("bta_dm_set_encryption callback is not provided\n");
4477         return;
4478     }
4479     for (i = 0; i < bta_dm_cb.device_list.count; i++) {
4480         if (bdcmp( bta_dm_cb.device_list.peer_device[i].peer_bdaddr, p_data->set_encryption.bd_addr) == 0 &&
4481                 bta_dm_cb.device_list.peer_device[i].conn_state == BTA_DM_CONNECTED) {
4482             break;
4483         }
4484     }
4485     if (i < bta_dm_cb.device_list.count) {
4486         if (bta_dm_cb.device_list.peer_device[i].p_encrypt_cback) {
4487             APPL_TRACE_ERROR("earlier enc was not done for same device\n");
4488             (*p_data->set_encryption.p_callback)(p_data->set_encryption.bd_addr,
4489                                                  p_data->set_encryption.transport,
4490                                                  BTA_BUSY);
4491             return;
4492         }
4493 
4494         if (BTM_SetEncryption(p_data->set_encryption.bd_addr, p_data->set_encryption.transport,
4495                               bta_dm_encrypt_cback, &p_data->set_encryption.sec_act)
4496                 == BTM_CMD_STARTED) {
4497             bta_dm_cb.device_list.peer_device[i].p_encrypt_cback = p_data->set_encryption.p_callback;
4498         }
4499     }else{
4500         APPL_TRACE_ERROR("%s, not find peer_bdaddr or peer_bdaddr connection state error", __func__);
4501     }
4502 }
4503 #endif  ///SMP_INCLUDED == TRUE
4504 
4505 #if (BTA_HD_INCLUDED == TRUE)
bta_dm_check_if_only_hd_connected(BD_ADDR peer_addr)4506 BOOLEAN bta_dm_check_if_only_hd_connected(BD_ADDR peer_addr)
4507 {
4508     APPL_TRACE_DEBUG("%s: count(%d)", __func__, bta_dm_conn_srvcs.count);
4509     for (uint8_t j = 0; j < bta_dm_conn_srvcs.count; j++) {
4510         // Check if profiles other than hid are connected
4511         if ((bta_dm_conn_srvcs.conn_srvc[j].id != BTA_ID_HD) &&
4512             !bdcmp(bta_dm_conn_srvcs.conn_srvc[j].peer_bdaddr, peer_addr)) {
4513             APPL_TRACE_DEBUG("%s: Another profile (id=%d) is connected", __func__, bta_dm_conn_srvcs.conn_srvc[j].id);
4514             return FALSE;
4515         }
4516     }
4517     return TRUE;
4518 }
4519 #endif /* BTA_HD_INCLUDED == TRUE */
4520 
4521 #if (BLE_INCLUDED == TRUE)
4522 /*******************************************************************************
4523 **
4524 ** Function         bta_dm_observe_results_cb
4525 **
4526 ** Description      Callback for BLE Observe result
4527 **
4528 **
4529 ** Returns          void
4530 **
4531 *******************************************************************************/
bta_dm_observe_results_cb(tBTM_INQ_RESULTS * p_inq,UINT8 * p_eir)4532 static void bta_dm_observe_results_cb (tBTM_INQ_RESULTS *p_inq, UINT8 *p_eir)
4533 {
4534     tBTA_DM_SEARCH     result;
4535     tBTM_INQ_INFO      *p_inq_info;
4536     APPL_TRACE_DEBUG("bta_dm_observe_results_cb")
4537 
4538     bdcpy(result.inq_res.bd_addr, p_inq->remote_bd_addr);
4539     result.inq_res.rssi = p_inq->rssi;
4540     result.inq_res.ble_addr_type    = p_inq->ble_addr_type;
4541     result.inq_res.inq_result_type  = p_inq->inq_result_type;
4542     result.inq_res.device_type      = p_inq->device_type;
4543     result.inq_res.flag             = p_inq->flag;
4544     result.inq_res.adv_data_len     = p_inq->adv_data_len;
4545     result.inq_res.scan_rsp_len     = p_inq->scan_rsp_len;
4546     memcpy(result.inq_res.dev_class, p_inq->dev_class, sizeof(DEV_CLASS));
4547     result.inq_res.ble_evt_type     = p_inq->ble_evt_type;
4548 
4549     /* application will parse EIR to find out remote device name */
4550     result.inq_res.p_eir = p_eir;
4551 
4552     if ((p_inq_info = BTM_InqDbRead(p_inq->remote_bd_addr)) != NULL) {
4553         /* initialize remt_name_not_required to FALSE so that we get the name by default */
4554         result.inq_res.remt_name_not_required = FALSE;
4555     }
4556 
4557     if (bta_dm_search_cb.p_scan_cback) {
4558         bta_dm_search_cb.p_scan_cback(BTA_DM_INQ_RES_EVT, &result);
4559     }
4560 
4561     if (p_inq_info) {
4562         /* application indicates if it knows the remote name, inside the callback
4563          copy that to the inquiry data base*/
4564         if (result.inq_res.remt_name_not_required) {
4565             p_inq_info->appl_knows_rem_name = TRUE;
4566         }
4567     }
4568 }
4569 
4570 /*******************************************************************************
4571 **
4572 ** Function         bta_dm_observe_cmpl_cb
4573 **
4574 ** Description      Callback for BLE Observe complete
4575 **
4576 **
4577 ** Returns          void
4578 **
4579 *******************************************************************************/
bta_dm_observe_cmpl_cb(void * p_result)4580 static void bta_dm_observe_cmpl_cb (void *p_result)
4581 {
4582     tBTA_DM_SEARCH  data;
4583 
4584     APPL_TRACE_DEBUG("bta_dm_observe_cmpl_cb");
4585 
4586     data.inq_cmpl.num_resps = ((tBTM_INQUIRY_CMPL *)p_result)->num_resp;
4587     if (bta_dm_search_cb.p_scan_cback) {
4588         bta_dm_search_cb.p_scan_cback(BTA_DM_INQ_CMPL_EVT, &data);
4589     }
4590 }
4591 
4592 /*******************************************************************************
4593 **
4594 ** Function         bta_dm_observe_discard_cb
4595 **
4596 ** Description      Callback for BLE Observe lost
4597 **
4598 **
4599 ** Returns          void
4600 **
4601 *******************************************************************************/
bta_dm_observe_discard_cb(uint32_t num_dis)4602 static void bta_dm_observe_discard_cb (uint32_t num_dis)
4603 {
4604     tBTA_DM_SEARCH  data;
4605 
4606     APPL_TRACE_DEBUG("bta_dm_observe_discard_cb");
4607 
4608     data.inq_dis.num_dis = num_dis;
4609     if (bta_dm_search_cb.p_scan_cback) {
4610         bta_dm_search_cb.p_scan_cback(BTA_DM_INQ_DISCARD_NUM_EVT, &data);
4611     }
4612 }
4613 
4614 #if (SMP_INCLUDED == TRUE)
4615 /*******************************************************************************
4616 **
4617 ** Function         bta_dm_ble_smp_cback
4618 **
4619 ** Description      Callback for BLE SMP
4620 **
4621 **
4622 ** Returns          void
4623 **
4624 *******************************************************************************/
bta_dm_ble_smp_cback(tBTM_LE_EVT event,BD_ADDR bda,tBTM_LE_EVT_DATA * p_data)4625 static UINT8 bta_dm_ble_smp_cback (tBTM_LE_EVT event, BD_ADDR bda, tBTM_LE_EVT_DATA *p_data)
4626 {
4627     tBTM_STATUS status = BTM_SUCCESS;
4628     tBTA_DM_SEC sec_event;
4629     char *p_name = NULL;
4630 
4631     if (!bta_dm_cb.p_sec_cback) {
4632         return BTM_NOT_AUTHORIZED;
4633     }
4634 
4635     memset(&sec_event, 0, sizeof(tBTA_DM_SEC));
4636     switch (event) {
4637     case BTM_LE_IO_REQ_EVT: {
4638         // #if (BT_SSP_INCLUDED == TRUE)
4639         bta_dm_co_ble_io_req(bda,
4640                              &p_data->io_req.io_cap,
4641                              &p_data->io_req.oob_data,
4642                              &p_data->io_req.auth_req,
4643                              &p_data->io_req.max_key_size,
4644                              &p_data->io_req.init_keys,
4645                              &p_data->io_req.resp_keys);
4646         // #endif
4647 #if BTM_OOB_INCLUDED == FALSE
4648         status = BTM_SUCCESS;
4649 #endif
4650         APPL_TRACE_EVENT("io mitm: %d oob_data:%d\n", p_data->io_req.auth_req, p_data->io_req.oob_data);
4651 
4652         break;
4653     }
4654 
4655     case BTM_LE_SEC_REQUEST_EVT:
4656         bdcpy(sec_event.ble_req.bd_addr, bda);
4657         p_name = BTM_SecReadDevName(bda);
4658         if (p_name != NULL) {
4659             BCM_STRNCPY_S((char *)sec_event.ble_req.bd_name,p_name, (BD_NAME_LEN));
4660         } else {
4661             sec_event.ble_req.bd_name[0] = 0;
4662         }
4663         sec_event.ble_req.bd_name[BD_NAME_LEN] = 0;
4664         bta_dm_cb.p_sec_cback(BTA_DM_BLE_SEC_REQ_EVT, &sec_event);
4665         break;
4666 
4667     case BTM_LE_KEY_NOTIF_EVT:
4668         bdcpy(sec_event.key_notif.bd_addr, bda);
4669         p_name = BTM_SecReadDevName(bda);
4670         if (p_name != NULL) {
4671             BCM_STRNCPY_S((char *)sec_event.key_notif.bd_name, p_name, (BD_NAME_LEN));
4672         } else {
4673             sec_event.key_notif.bd_name[0] = 0;
4674         }
4675         sec_event.ble_req.bd_name[BD_NAME_LEN] = 0;
4676         sec_event.key_notif.passkey = p_data->key_notif;
4677         bta_dm_cb.p_sec_cback(BTA_DM_BLE_PASSKEY_NOTIF_EVT, &sec_event);
4678         break;
4679 
4680     case BTM_LE_KEY_REQ_EVT:
4681         bdcpy(sec_event.ble_req.bd_addr, bda);
4682         bta_dm_cb.p_sec_cback(BTA_DM_BLE_PASSKEY_REQ_EVT, &sec_event);
4683         break;
4684 
4685     case BTM_LE_OOB_REQ_EVT:
4686         bdcpy(sec_event.ble_req.bd_addr, bda);
4687         bta_dm_cb.p_sec_cback(BTA_DM_BLE_OOB_REQ_EVT, &sec_event);
4688         break;
4689 
4690     case BTM_LE_NC_REQ_EVT:
4691         bdcpy(sec_event.key_notif.bd_addr, bda);
4692         BCM_STRNCPY_S((char *)sec_event.key_notif.bd_name,bta_dm_get_remname(), (BD_NAME_LEN));
4693         sec_event.ble_req.bd_name[BD_NAME_LEN] = 0;
4694         sec_event.key_notif.passkey = p_data->key_notif;
4695         bta_dm_cb.p_sec_cback(BTA_DM_BLE_NC_REQ_EVT, &sec_event);
4696         break;
4697 
4698     case BTM_LE_KEY_EVT:
4699         bdcpy(sec_event.ble_key.bd_addr, bda);
4700         sec_event.ble_key.key_type = p_data->key.key_type;
4701         sec_event.ble_key.p_key_value = p_data->key.p_key_value;
4702         bta_dm_cb.p_sec_cback(BTA_DM_BLE_KEY_EVT, &sec_event);
4703         break;
4704 
4705     case BTM_LE_COMPLT_EVT:
4706         bdcpy(sec_event.auth_cmpl.bd_addr, bda);
4707 #if BLE_INCLUDED == TRUE
4708         BTM_ReadDevInfo(bda, &sec_event.auth_cmpl.dev_type, &sec_event.auth_cmpl.addr_type);
4709 #endif
4710         p_name = BTM_SecReadDevName(bda);
4711         if (p_name != NULL) {
4712             BCM_STRNCPY_S((char *)sec_event.auth_cmpl.bd_name, p_name, (BD_NAME_LEN));
4713         } else {
4714             sec_event.auth_cmpl.bd_name[0] = 0;
4715         }
4716         if (p_data->complt.reason != 0) {
4717             sec_event.auth_cmpl.fail_reason = BTA_DM_AUTH_CONVERT_SMP_CODE(((UINT8)p_data->complt.reason));
4718             /* delete this device entry from Sec Dev DB */
4719             bta_dm_remove_sec_dev_entry (bda);
4720         } else {
4721             sec_event.auth_cmpl.success = TRUE;
4722             if (!p_data->complt.smp_over_br) {
4723 
4724             }
4725         }
4726         sec_event.auth_cmpl.auth_mode = p_data->complt.auth_mode;
4727         if (bta_dm_cb.p_sec_cback) {
4728             //bta_dm_cb.p_sec_cback(BTA_DM_AUTH_CMPL_EVT, &sec_event);
4729             bta_dm_cb.p_sec_cback(BTA_DM_BLE_AUTH_CMPL_EVT, &sec_event);
4730         }
4731 
4732         break;
4733 
4734     default:
4735         status = BTM_NOT_AUTHORIZED;
4736         break;
4737     }
4738     return status;
4739 }
4740 
4741 /*******************************************************************************
4742 **
4743 ** Function         bta_dm_ble_id_key_cback
4744 **
4745 ** Description      Callback for BLE local ID keys
4746 **
4747 **
4748 ** Returns          void
4749 **
4750 *******************************************************************************/
bta_dm_ble_id_key_cback(UINT8 key_type,tBTM_BLE_LOCAL_KEYS * p_key)4751 static void bta_dm_ble_id_key_cback (UINT8 key_type, tBTM_BLE_LOCAL_KEYS *p_key)
4752 {
4753     UINT8   evt;
4754     tBTA_DM_SEC dm_key;
4755 
4756     switch (key_type) {
4757     case BTM_BLE_KEY_TYPE_ID:
4758     case BTM_BLE_KEY_TYPE_ER:
4759         if (bta_dm_cb.p_sec_cback) {
4760             memcpy(&dm_key.ble_id_keys, p_key, sizeof(tBTM_BLE_LOCAL_KEYS));
4761 
4762             evt = (key_type == BTM_BLE_KEY_TYPE_ID) ? BTA_DM_BLE_LOCAL_IR_EVT : \
4763                   BTA_DM_BLE_LOCAL_ER_EVT;
4764             bta_dm_cb.p_sec_cback(evt, &dm_key);
4765         }
4766         break;
4767 
4768     default:
4769         APPL_TRACE_DEBUG("Unknown key type %d", key_type);
4770         break;
4771     }
4772     return;
4773 
4774 }
4775 
4776 /*******************************************************************************
4777 **
4778 ** Function         bta_dm_add_blekey
4779 **
4780 ** Description      This function adds an BLE Key to an security database entry.
4781 **                  This function shall only be called AFTER BTA_DmAddBleDevice has been called.
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_blekey(tBTA_DM_MSG * p_data)4788 void bta_dm_add_blekey (tBTA_DM_MSG *p_data)
4789 {
4790     if (!BTM_SecAddBleKey (p_data->add_ble_key.bd_addr,
4791                            (tBTM_LE_KEY_VALUE *)&p_data->add_ble_key.blekey,
4792                            p_data->add_ble_key.key_type)) {
4793         APPL_TRACE_ERROR ("BTA_DM: Error adding BLE Key for device %08x%04x",
4794                           (p_data->add_ble_key.bd_addr[0] << 24) + (p_data->add_ble_key.bd_addr[1] << 16) + \
4795                           (p_data->add_ble_key.bd_addr[2] << 8) + p_data->add_ble_key.bd_addr[3],
4796                           (p_data->add_ble_key.bd_addr[4] << 8) + p_data->add_ble_key.bd_addr[5]);
4797     }
4798 }
4799 
4800 /*******************************************************************************
4801 **
4802 ** Function         bta_dm_add_ble_device
4803 **
4804 ** Description      This function adds an BLE device to an security database entry.
4805 **                  It is normally called during host startup to restore all required information
4806 **                  stored in the NVRAM.
4807 **
4808 ** Parameters:
4809 **
4810 *******************************************************************************/
bta_dm_add_ble_device(tBTA_DM_MSG * p_data)4811 void bta_dm_add_ble_device (tBTA_DM_MSG *p_data)
4812 {
4813     if (!BTM_SecAddBleDevice (p_data->add_ble_device.bd_addr, NULL,
4814                               p_data->add_ble_device.dev_type  ,
4815                               p_data->add_ble_device.addr_type,
4816                               p_data->add_ble_device.auth_mode)) {
4817         APPL_TRACE_ERROR ("BTA_DM: Error adding BLE Device for device %08x%04x",
4818                           (p_data->add_ble_device.bd_addr[0] << 24) + (p_data->add_ble_device.bd_addr[1] << 16) + \
4819                           (p_data->add_ble_device.bd_addr[2] << 8) + p_data->add_ble_device.bd_addr[3],
4820                           (p_data->add_ble_device.bd_addr[4] << 8) + p_data->add_ble_device.bd_addr[5]);
4821     }
4822 }
4823 
4824 /*******************************************************************************
4825 **
4826 ** Function         bta_dm_add_ble_device
4827 **
4828 ** Description      This function adds an BLE device to an security database entry.
4829 **                  It is normally called during host startup to restore all required information
4830 **                  stored in the NVRAM.
4831 **
4832 ** Parameters:
4833 **
4834 *******************************************************************************/
bta_dm_ble_passkey_reply(tBTA_DM_MSG * p_data)4835 void bta_dm_ble_passkey_reply (tBTA_DM_MSG *p_data)
4836 {
4837     if (p_data->pin_reply.accept) {
4838         BTM_BlePasskeyReply(p_data->ble_passkey_reply.bd_addr, BTM_SUCCESS, p_data->ble_passkey_reply.passkey);
4839     } else {
4840         BTM_BlePasskeyReply(p_data->ble_passkey_reply.bd_addr, BTM_NOT_AUTHORIZED, p_data->ble_passkey_reply.passkey);
4841     }
4842 
4843 }
4844 
bta_dm_ble_set_static_passkey(tBTA_DM_MSG * p_data)4845 void bta_dm_ble_set_static_passkey(tBTA_DM_MSG *p_data)
4846 {
4847     BTM_BleSetStaticPasskey(p_data->ble_set_static_passkey.add, p_data->ble_set_static_passkey.static_passkey);
4848 }
4849 /*******************************************************************************
4850 **
4851 ** Function         bta_dm_ble_confirm_reply
4852 **
4853 ** Description      This is response to SM numeric comparison request submitted
4854 **                  to application.
4855 **
4856 ** Parameters:
4857 **
4858 *******************************************************************************/
bta_dm_ble_confirm_reply(tBTA_DM_MSG * p_data)4859 void bta_dm_ble_confirm_reply (tBTA_DM_MSG *p_data)
4860 {
4861     if (p_data->confirm.accept) {
4862         BTM_BleConfirmReply(p_data->confirm.bd_addr, BTM_SUCCESS);
4863     } else {
4864         BTM_BleConfirmReply(p_data->ble_passkey_reply.bd_addr, BTM_NOT_AUTHORIZED);
4865     }
4866 }
4867 
4868 /*******************************************************************************
4869 **
4870 ** Function         bta_dm_security_grant
4871 **
4872 ** Description      This function grant SMP security request access.
4873 **
4874 ** Parameters:
4875 **
4876 *******************************************************************************/
bta_dm_security_grant(tBTA_DM_MSG * p_data)4877 void bta_dm_security_grant (tBTA_DM_MSG *p_data)
4878 {
4879     BTM_SecurityGrant(p_data->ble_sec_grant.bd_addr, p_data->ble_sec_grant.res);
4880 }
4881 #endif  ///SMP_INCLUDED == TRUE
4882 
4883 /*******************************************************************************
4884 **
4885 ** Function         bta_dm_ble_set_bg_conn_type
4886 **
4887 ** Description      This function set the BLE background connection type
4888 **
4889 ** Parameters:
4890 **
4891 *******************************************************************************/
bta_dm_ble_set_bg_conn_type(tBTA_DM_MSG * p_data)4892 void bta_dm_ble_set_bg_conn_type (tBTA_DM_MSG *p_data)
4893 {
4894     BTM_BleSetBgConnType(p_data->ble_set_bd_conn_type.bg_conn_type,
4895                          p_data->ble_set_bd_conn_type.p_select_cback);
4896 }
4897 
4898 /*******************************************************************************
4899 **
4900 ** Function         bta_dm_ble_set_conn_params
4901 **
4902 ** Description      This function set the preferred connection parameters.
4903 **
4904 ** Parameters:
4905 **
4906 *******************************************************************************/
bta_dm_ble_set_conn_params(tBTA_DM_MSG * p_data)4907 void bta_dm_ble_set_conn_params (tBTA_DM_MSG *p_data)
4908 {
4909     BTM_BleSetPrefConnParams(p_data->ble_set_conn_params.peer_bda,
4910                              p_data->ble_set_conn_params.conn_int_min,
4911                              p_data->ble_set_conn_params.conn_int_max,
4912                              p_data->ble_set_conn_params.slave_latency,
4913                              p_data->ble_set_conn_params.supervision_tout);
4914 
4915     BTM_BleConfigConnParams(p_data->ble_set_conn_params.conn_int_min, p_data->ble_set_conn_params.conn_int_max,
4916             p_data->ble_set_conn_params.slave_latency, p_data->ble_set_conn_params.supervision_tout);
4917 }
4918 
4919 /*******************************************************************************
4920 **
4921 ** Function         bta_dm_ble_set_conn_scan_params
4922 **
4923 ** Description      This function sets BLE scan parameters.
4924 **
4925 ** Parameters:
4926 **
4927 *******************************************************************************/
bta_dm_ble_set_scan_params(tBTA_DM_MSG * p_data)4928 void bta_dm_ble_set_scan_params(tBTA_DM_MSG *p_data)
4929 {
4930     BTM_BleSetScanParams(p_data->ble_set_scan_params.client_if,
4931                          p_data->ble_set_scan_params.scan_int,
4932                          p_data->ble_set_scan_params.scan_window,
4933                          p_data->ble_set_scan_params.scan_mode,
4934                          p_data->ble_set_scan_params.scan_param_setup_cback);
4935 }
4936 
4937 /*******************************************************************************
4938 **
4939 ** Function         bta_dm_ble_set_scan_fil_params
4940 **
4941 ** Description      This function sets BLE scan filter & parameters.
4942 **
4943 ** Parameters:
4944 **
4945 *******************************************************************************/
bta_dm_ble_set_scan_fil_params(tBTA_DM_MSG * p_data)4946 void bta_dm_ble_set_scan_fil_params(tBTA_DM_MSG *p_data)
4947 {
4948     tBTA_STATUS status = BTA_FAILURE;
4949 
4950     if (BTM_BleSetScanFilterParams (p_data->ble_set_scan_fil_params.client_if,
4951                                 p_data->ble_set_scan_fil_params.scan_int,
4952                                 p_data->ble_set_scan_fil_params.scan_window,
4953                                 p_data->ble_set_scan_fil_params.scan_mode,
4954                                 p_data->ble_set_scan_fil_params.addr_type_own,
4955                                 p_data->ble_set_scan_fil_params.scan_duplicate_filter,
4956                                 p_data->ble_set_scan_fil_params.scan_filter_policy,
4957                                 p_data->ble_set_scan_fil_params.scan_param_setup_cback) == BTM_SUCCESS) {
4958         status = BTA_SUCCESS;
4959 
4960     } else {
4961         APPL_TRACE_ERROR("%s(), fail to set scan params.", __func__);
4962     }
4963     if (p_data->ble_set_scan_fil_params.scan_param_setup_cback != NULL) {
4964         p_data->ble_set_scan_fil_params.scan_param_setup_cback(p_data->ble_set_scan_fil_params.client_if, status);
4965     }
4966 
4967 }
4968 
4969 
4970 /*******************************************************************************
4971 **
4972 ** Function         bta_dm_ble_set_conn_scan_params
4973 **
4974 ** Description      This function set the preferred connection scan parameters.
4975 **
4976 ** Parameters:
4977 **
4978 *******************************************************************************/
bta_dm_ble_set_conn_scan_params(tBTA_DM_MSG * p_data)4979 void bta_dm_ble_set_conn_scan_params (tBTA_DM_MSG *p_data)
4980 {
4981     BTM_BleSetConnScanParams(p_data->ble_set_conn_scan_params.scan_int,
4982                              p_data->ble_set_conn_scan_params.scan_window);
4983 }
4984 /*******************************************************************************
4985 **
4986 ** Function         bta_dm_ble_update_conn_params
4987 **
4988 ** Description      This function update LE connection parameters.
4989 **
4990 ** Parameters:
4991 **
4992 *******************************************************************************/
bta_dm_ble_update_conn_params(tBTA_DM_MSG * p_data)4993 void bta_dm_ble_update_conn_params (tBTA_DM_MSG *p_data)
4994 {
4995     if (!L2CA_UpdateBleConnParams(p_data->ble_update_conn_params.bd_addr,
4996                                   p_data->ble_update_conn_params.min_int,
4997                                   p_data->ble_update_conn_params.max_int,
4998                                   p_data->ble_update_conn_params.latency,
4999                                   p_data->ble_update_conn_params.timeout)) {
5000         APPL_TRACE_ERROR("Update connection parameters failed!");
5001     }
5002 }
5003 /*******************************************************************************
5004 **
5005 ** Function         bta_dm_ble_disconnect
5006 **
5007 ** Description      This function disconnect the ble connection.
5008 **
5009 ** Parameters:
5010 **
5011 *******************************************************************************/
bta_dm_ble_disconnect(tBTA_DM_MSG * p_data)5012 void bta_dm_ble_disconnect (tBTA_DM_MSG *p_data)
5013 {
5014     L2CA_RemoveFixedChnl(L2CAP_ATT_CID, p_data->ble_disconnect.remote_bda);
5015 }
5016 
5017 /*******************************************************************************
5018 **
5019 ** Function         bta_dm_ble_set_rand_address
5020 **
5021 ** Description      This function set the LE random address for the device.
5022 **
5023 ** Parameters:      rand_addr:the random address whitch should be setting
5024 ** Explanation:     This function added by Yulong at 2016/9/9
5025 *******************************************************************************/
bta_dm_ble_set_rand_address(tBTA_DM_MSG * p_data)5026 void bta_dm_ble_set_rand_address(tBTA_DM_MSG *p_data)
5027 {
5028     tBTM_STATUS status = BTM_SET_STATIC_RAND_ADDR_FAIL;
5029     if (p_data->set_addr.addr_type != BLE_ADDR_RANDOM) {
5030         APPL_TRACE_ERROR("Invalid random adress type = %d\n", p_data->set_addr.addr_type);
5031         if(p_data->set_addr.p_set_rand_addr_cback) {
5032             (*p_data->set_addr.p_set_rand_addr_cback)(status);
5033         }
5034         return;
5035     }
5036     //send the setting random address to BTM layer
5037     status = BTM_BleSetRandAddress(p_data->set_addr.address);
5038     if(p_data->set_addr.p_set_rand_addr_cback) {
5039         (*p_data->set_addr.p_set_rand_addr_cback)(status);
5040     }
5041 
5042 }
5043 
bta_dm_ble_clear_rand_address(tBTA_DM_MSG * p_data)5044 void bta_dm_ble_clear_rand_address(tBTA_DM_MSG *p_data)
5045 {
5046     UNUSED(p_data);
5047     BTM_BleClearRandAddress();
5048 }
5049 
5050 /*******************************************************************************
5051 **
5052 ** Function         bta_dm_ble_stop_advertising
5053 **
5054 ** Description      This function stop the BLE avdertising for the device.
5055 **
5056 ** Parameters:      void
5057 ** Explanation:     This function added by Yulong at 2016/10/19
5058 *******************************************************************************/
bta_dm_ble_stop_advertising(tBTA_DM_MSG * p_data)5059 void bta_dm_ble_stop_advertising(tBTA_DM_MSG *p_data)
5060 {
5061     if (p_data->hdr.event != BTA_DM_API_BLE_STOP_ADV_EVT) {
5062         APPL_TRACE_ERROR("Invalid BTA event,can't stop the BLE adverting\n");
5063     }
5064 
5065     btm_ble_stop_adv();
5066 }
5067 
5068 
5069 
5070 #if BLE_PRIVACY_SPT == TRUE
5071 /*******************************************************************************
5072 **
5073 ** Function         bta_dm_ble_config_local_privacy
5074 **
5075 ** Description      This function set the local device LE privacy settings.
5076 **
5077 ** Parameters:
5078 **
5079 *******************************************************************************/
bta_dm_ble_config_local_privacy(tBTA_DM_MSG * p_data)5080 void bta_dm_ble_config_local_privacy (tBTA_DM_MSG *p_data)
5081 {
5082     BTM_BleConfigPrivacy (p_data->ble_local_privacy.privacy_enable, p_data->ble_local_privacy.set_local_privacy_cback);
5083 }
5084 #endif
5085 
5086 /*******************************************************************************
5087 **
5088 ** Function         bta_dm_ble_config_local_icon
5089 **
5090 ** Description      This function sets the local icon value.
5091 **
5092 **
5093 *******************************************************************************/
bta_dm_ble_config_local_icon(tBTA_DM_MSG * p_data)5094 void bta_dm_ble_config_local_icon (tBTA_DM_MSG *p_data)
5095 {
5096     BTM_BleConfigLocalIcon (p_data->ble_local_icon.icon);
5097 }
5098 
5099 /*******************************************************************************
5100 **
5101 ** Function         bta_dm_ble_observe
5102 **
5103 ** Description      This function set the preferred connection scan parameters.
5104 **
5105 ** Parameters:
5106 **
5107 *******************************************************************************/
bta_dm_ble_observe(tBTA_DM_MSG * p_data)5108 void bta_dm_ble_observe (tBTA_DM_MSG *p_data)
5109 {
5110     tBTM_STATUS status;
5111     if (p_data->ble_observe.start) {
5112         /*Save the  callback to be called when a scan results are available */
5113         bta_dm_search_cb.p_scan_cback = p_data->ble_observe.p_cback;
5114 
5115         if ((status = BTM_BleObserve(TRUE, p_data->ble_observe.duration,
5116                                      bta_dm_observe_results_cb, bta_dm_observe_cmpl_cb)) != BTM_CMD_STARTED) {
5117             APPL_TRACE_WARNING(" %s start observe failed. status=0x%x\n", __FUNCTION__, status);
5118         }
5119 
5120         if (p_data->ble_observe.p_start_scan_cback) {
5121             status = (status == BTM_CMD_STARTED ? BTA_SUCCESS : BTA_FAILURE);
5122             p_data->ble_observe.p_start_scan_cback(status);
5123         }
5124     } else {
5125         bta_dm_search_cb.p_scan_cback = NULL;
5126         status = BTM_BleObserve(FALSE, 0, NULL, NULL);
5127 
5128         if (status != BTM_CMD_STARTED){
5129             APPL_TRACE_WARNING(" %s stop observe failed, status=0x%x\n", __FUNCTION__, status);
5130         }
5131 
5132         if (p_data->ble_observe.p_stop_scan_cback) {
5133             status = (status == BTM_CMD_STARTED ? BTA_SUCCESS : BTA_FAILURE);
5134             p_data->ble_observe.p_stop_scan_cback(status);
5135         }
5136     }
5137 }
5138 
5139 /*******************************************************************************
5140 **
5141 ** Function         bta_dm_ble_scan
5142 **
5143 ** Description      This function set the preferred connection scan parameters.
5144 **
5145 ** Parameters:
5146 **
5147 *******************************************************************************/
bta_dm_ble_scan(tBTA_DM_MSG * p_data)5148 void bta_dm_ble_scan (tBTA_DM_MSG *p_data)
5149 {
5150     tBTM_STATUS status;
5151     if (p_data->ble_scan.start) {
5152         /*Save the  callback to be called when a scan results are available */
5153         bta_dm_search_cb.p_scan_cback = p_data->ble_scan.p_cback;
5154 
5155         if ((status = BTM_BleScan(TRUE, p_data->ble_scan.duration,
5156                                      bta_dm_observe_results_cb, bta_dm_observe_cmpl_cb, bta_dm_observe_discard_cb)) != BTM_CMD_STARTED) {
5157             APPL_TRACE_WARNING(" %s start scan failed. status=0x%x\n", __FUNCTION__, status);
5158         }
5159 
5160         if (p_data->ble_scan.p_start_scan_cback) {
5161             status = (status == BTM_CMD_STARTED ? BTA_SUCCESS : BTA_FAILURE);
5162             p_data->ble_scan.p_start_scan_cback(status);
5163         }
5164     } else {
5165         bta_dm_search_cb.p_scan_cback = NULL;
5166         status = BTM_BleScan(FALSE, 0, NULL, NULL, NULL);
5167 
5168         if (status != BTM_CMD_STARTED){
5169             APPL_TRACE_WARNING(" %s stop scan failed, status=0x%x\n", __FUNCTION__, status);
5170         }
5171 
5172         if (p_data->ble_scan.p_stop_scan_cback) {
5173             status = (status == BTM_CMD_STARTED ? BTA_SUCCESS : BTA_FAILURE);
5174             p_data->ble_scan.p_stop_scan_cback(status);
5175         }
5176     }
5177 }
5178 
5179 /*******************************************************************************
5180 **
5181 ** Function         bta_dm_ble_set_adv_params
5182 **
5183 ** Description      This function set the adv parameters.
5184 **
5185 ** Parameters:
5186 **
5187 *******************************************************************************/
bta_dm_ble_set_adv_params(tBTA_DM_MSG * p_data)5188 void bta_dm_ble_set_adv_params (tBTA_DM_MSG *p_data)
5189 {
5190     BTM_BleSetAdvParams(p_data->ble_set_adv_params.adv_int_min,
5191                         p_data->ble_set_adv_params.adv_int_max,
5192                         p_data->ble_set_adv_params.p_dir_bda,
5193                         BTA_DM_BLE_ADV_CHNL_MAP);
5194 }
5195 
5196 /*******************************************************************************
5197 **
5198 ** Function         bta_dm_ble_set_adv_params_all
5199 **
5200 ** Description      This function is called to set all of the advertising parameters.
5201 **
5202 ** Parameters:       None.
5203 **
5204 ** Returns          void
5205 **
5206 *******************************************************************************/
bta_dm_ble_set_adv_params_all(tBTA_DM_MSG * p_data)5207 void bta_dm_ble_set_adv_params_all  (tBTA_DM_MSG *p_data)
5208 {
5209     tBTA_STATUS status = BTA_FAILURE;
5210     if (BTM_BleSetAdvParamsAll(p_data->ble_set_adv_params_all.adv_int_min,
5211                                 p_data->ble_set_adv_params_all.adv_int_max,
5212                                 p_data->ble_set_adv_params_all.adv_type,
5213                                 p_data->ble_set_adv_params_all.addr_type_own,
5214                                 p_data->ble_set_adv_params_all.p_dir_bda,
5215                                 p_data->ble_set_adv_params_all.channel_map,
5216                                 p_data->ble_set_adv_params_all.adv_filter_policy,
5217                                 p_data->ble_set_adv_params_all.p_start_adv_cback) == BTM_SUCCESS) {
5218         APPL_TRACE_DEBUG("%s(), success to set ble adv params.", __func__);
5219     } else {
5220         APPL_TRACE_ERROR("%s(), fail to set ble adv params.", __func__);
5221         if(p_data->ble_set_adv_params_all.p_start_adv_cback) {
5222             (*p_data->ble_set_adv_params_all.p_start_adv_cback)(status);
5223         }
5224         return;
5225     }
5226     if(BTM_BleStartAdv() == BTM_SUCCESS) {
5227         status = BTA_SUCCESS;
5228     }
5229     if(p_data->ble_set_adv_params_all.p_start_adv_cback) {
5230         (*p_data->ble_set_adv_params_all.p_start_adv_cback)(status);
5231     }
5232 }
5233 
5234 /*******************************************************************************
5235 **
5236 ** Function         bta_dm_ble_update_duplicate_exceptional_list
5237 **
5238 ** Description      This function is to update duplicate scan exceptional list
5239 **
5240 **
5241 *******************************************************************************/
bta_dm_ble_update_duplicate_exceptional_list(tBTA_DM_MSG * p_data)5242 void bta_dm_ble_update_duplicate_exceptional_list(tBTA_DM_MSG *p_data)
5243 {
5244     BTM_UpdateBleDuplicateExceptionalList(p_data->ble_duplicate_exceptional_list.subcode,
5245                                           p_data->ble_duplicate_exceptional_list.type,
5246                                           p_data->ble_duplicate_exceptional_list.device_info,
5247                                           p_data->ble_duplicate_exceptional_list.exceptional_list_cb);
5248 }
5249 
5250 /*******************************************************************************
5251 **
5252 ** Function         bta_dm_ble_set_adv_config
5253 **
5254 ** Description      This function set the customized ADV data configuration
5255 **
5256 ** Parameters:
5257 **
5258 *******************************************************************************/
bta_dm_ble_set_adv_config(tBTA_DM_MSG * p_data)5259 void bta_dm_ble_set_adv_config (tBTA_DM_MSG *p_data)
5260 {
5261     tBTA_STATUS status = BTA_FAILURE;
5262 
5263     if (BTM_BleWriteAdvData(p_data->ble_set_adv_data.data_mask,
5264                             (tBTM_BLE_ADV_DATA *)p_data->ble_set_adv_data.p_adv_cfg) == BTM_SUCCESS) {
5265         status = BTA_SUCCESS;
5266     }
5267 
5268     if (p_data->ble_set_adv_data.p_adv_data_cback) {
5269         (*p_data->ble_set_adv_data.p_adv_data_cback)(status);
5270     }
5271 }
5272 
5273 /*******************************************************************************
5274 **
5275 ** Function         bta_dm_ble_set_long_adv
5276 **
5277 ** Description      This function set the long ADV data
5278 **
5279 ** Parameters:
5280 **
5281 *******************************************************************************/
bta_dm_ble_set_long_adv(tBTA_DM_MSG * p_data)5282 void bta_dm_ble_set_long_adv (tBTA_DM_MSG *p_data)
5283 {
5284     tBTA_STATUS status = BTA_FAILURE;
5285 
5286     if (BTM_BleWriteLongAdvData(p_data->ble_set_long_adv_data.adv_data,
5287                                 p_data->ble_set_long_adv_data.adv_data_len) == BTM_SUCCESS) {
5288         status = BTA_SUCCESS;
5289     }
5290 
5291     if (p_data->ble_set_adv_data.p_adv_data_cback) {
5292         (*p_data->ble_set_adv_data.p_adv_data_cback)(status);
5293     }
5294 }
5295 
5296 /*******************************************************************************
5297 **
5298 ** Function         bta_dm_ble_set_adv_config_raw
5299 **
5300 ** Description      This function set the customized ADV data configuration
5301 **
5302 ** Parameters:
5303 **
5304 *******************************************************************************/
bta_dm_ble_set_adv_config_raw(tBTA_DM_MSG * p_data)5305 void bta_dm_ble_set_adv_config_raw (tBTA_DM_MSG *p_data)
5306 {
5307     tBTA_STATUS status = BTA_FAILURE;
5308 
5309     if (BTM_BleWriteAdvDataRaw(p_data->ble_set_adv_data_raw.p_raw_adv,
5310                                p_data->ble_set_adv_data_raw.raw_adv_len) == BTM_SUCCESS) {
5311         status = BTA_SUCCESS;
5312     }
5313 
5314     if (p_data->ble_set_adv_data_raw.p_adv_data_cback) {
5315         (*p_data->ble_set_adv_data_raw.p_adv_data_cback)(status);
5316     }
5317 }
5318 
5319 
5320 /*******************************************************************************
5321 **
5322 ** Function         bta_dm_ble_set_scan_rsp
5323 **
5324 ** Description      This function set the customized ADV scan resp. configuration
5325 **
5326 ** Parameters:
5327 **
5328 *******************************************************************************/
bta_dm_ble_set_scan_rsp(tBTA_DM_MSG * p_data)5329 void bta_dm_ble_set_scan_rsp (tBTA_DM_MSG *p_data)
5330 {
5331     tBTA_STATUS status = BTA_FAILURE;
5332 
5333     if (BTM_BleWriteScanRsp(p_data->ble_set_adv_data.data_mask,
5334                             (tBTM_BLE_ADV_DATA *)p_data->ble_set_adv_data.p_adv_cfg) == BTM_SUCCESS) {
5335         status = BTA_SUCCESS;
5336     }
5337 
5338     if (p_data->ble_set_adv_data.p_adv_data_cback) {
5339         (*p_data->ble_set_adv_data.p_adv_data_cback)(status);
5340     }
5341 }
5342 
5343 /*******************************************************************************
5344 **
5345 ** Function         bta_dm_ble_set_scan_rsp_raw
5346 **
5347 ** Description      This function set the raw scan response data
5348 **
5349 ** Parameters:
5350 **
5351 *******************************************************************************/
bta_dm_ble_set_scan_rsp_raw(tBTA_DM_MSG * p_data)5352 void bta_dm_ble_set_scan_rsp_raw (tBTA_DM_MSG *p_data)
5353 {
5354     tBTA_STATUS status = BTA_FAILURE;
5355 
5356     if (BTM_BleWriteScanRspRaw(p_data->ble_set_adv_data_raw.p_raw_adv,
5357                                p_data->ble_set_adv_data_raw.raw_adv_len) == BTM_SUCCESS) {
5358         status = BTA_SUCCESS;
5359     }
5360 
5361     if (p_data->ble_set_adv_data_raw.p_adv_data_cback) {
5362         (*p_data->ble_set_adv_data_raw.p_adv_data_cback)(status);
5363     }
5364 }
5365 
5366 /*******************************************************************************
5367 **
5368 ** Function         bta_dm_ble_set_data_length
5369 **
5370 ** Description      This function set the maximum transmission packet size
5371 **
5372 ** Parameters
5373 **
5374 *******************************************************************************/
bta_dm_ble_set_data_length(tBTA_DM_MSG * p_data)5375 void bta_dm_ble_set_data_length(tBTA_DM_MSG *p_data)
5376 {
5377     UINT8 status = BTM_SUCCESS;
5378     tACL_CONN *p_acl_cb = btm_bda_to_acl(p_data->ble_set_data_length.remote_bda, BT_TRANSPORT_LE);
5379     if (p_acl_cb == NULL) {
5380         APPL_TRACE_ERROR("%s error: Invalid connection remote_bda.", __func__);
5381         return;
5382     }
5383 
5384     p_acl_cb->p_set_pkt_data_cback = p_data->ble_set_data_length.p_set_pkt_data_cback;
5385     // if the value of the data length is same, triger callback directly
5386     if(p_data->ble_set_data_length.tx_data_length == p_acl_cb->data_length_params.tx_len) {
5387         if(p_data->ble_set_data_length.p_set_pkt_data_cback) {
5388             (*p_data->ble_set_data_length.p_set_pkt_data_cback)(status, &p_acl_cb->data_length_params);
5389         }
5390         return;
5391     }
5392 
5393     if(p_acl_cb->data_len_updating) {
5394         // aleady have one cmd
5395         if(p_acl_cb->data_len_waiting) {
5396             status = BTM_ILLEGAL_ACTION;
5397         } else {
5398             // save the command
5399             p_acl_cb->p_set_data_len_cback_waiting = p_data->ble_set_data_length.p_set_pkt_data_cback;
5400             p_acl_cb->tx_len_waiting = p_data->ble_set_data_length.tx_data_length;
5401             p_acl_cb->data_len_waiting = true;
5402             return;
5403         }
5404     } else {
5405         status = BTM_SetBleDataLength(p_data->ble_set_data_length.remote_bda,
5406                                         p_data->ble_set_data_length.tx_data_length);
5407     }
5408     if (status != BTM_SUCCESS) {
5409         APPL_TRACE_ERROR("%s failed\n", __FUNCTION__);
5410     }
5411     if (p_data->ble_set_data_length.p_set_pkt_data_cback && status != BTM_SUCCESS) {
5412         if (p_acl_cb->data_length_params.tx_len == 0){
5413             uint16_t length = controller_get_interface()->get_acl_data_size_ble();
5414             p_acl_cb->data_length_params.rx_len = length;
5415             p_acl_cb->data_length_params.tx_len = length;
5416         }
5417         (*p_data->ble_set_data_length.p_set_pkt_data_cback)(status, &p_acl_cb->data_length_params);
5418     }
5419 
5420 }
5421 
5422 /*******************************************************************************
5423 **
5424 ** Function         bta_dm_ble_broadcast
5425 **
5426 ** Description      Starts or stops LE broadcasts
5427 **
5428 ** Parameters:
5429 **
5430 *******************************************************************************/
bta_dm_ble_broadcast(tBTA_DM_MSG * p_data)5431 void bta_dm_ble_broadcast (tBTA_DM_MSG *p_data)
5432 {
5433     tBTA_STATUS status = BTA_FAILURE;
5434     BOOLEAN start = p_data->ble_observe.start;
5435 
5436     if (BTM_BleBroadcast(start, p_data->ble_observe.p_stop_adv_cback) == BTM_SUCCESS) {
5437         status = BTA_SUCCESS;
5438     } else {
5439         APPL_TRACE_ERROR("%s failed\n", __FUNCTION__);
5440     }
5441 
5442     if (p_data->ble_observe.p_stop_adv_cback){
5443         (*p_data->ble_observe.p_stop_adv_cback)(status);
5444     }
5445 
5446 }
5447 
5448 /*******************************************************************************
5449 **
5450 ** Function         bta_dm_ble_multi_adv_enb
5451 **
5452 ** Description      This function enables a single advertising instance
5453 **
5454 ** Parameters:
5455 **
5456 *******************************************************************************/
bta_dm_ble_multi_adv_enb(tBTA_DM_MSG * p_data)5457 void bta_dm_ble_multi_adv_enb(tBTA_DM_MSG *p_data)
5458 {
5459     tBTM_STATUS btm_status = 0;
5460 
5461     bta_dm_cb.p_multi_adv_cback = p_data->ble_multi_adv_enb.p_cback;
5462     if (BTM_BleMaxMultiAdvInstanceCount() > 0 && NULL != p_data->ble_multi_adv_enb.p_ref) {
5463         btm_status = BTM_BleEnableAdvInstance((tBTM_BLE_ADV_PARAMS *)
5464                                               p_data->ble_multi_adv_enb.p_params,
5465                                               p_data->ble_multi_adv_enb.p_cback,
5466                                               p_data->ble_multi_adv_enb.p_ref);
5467     }
5468 
5469     if (BTM_CMD_STARTED != btm_status) {
5470         bta_dm_cb.p_multi_adv_cback(BTA_BLE_MULTI_ADV_ENB_EVT, 0xFF,
5471                                     p_data->ble_multi_adv_enb.p_ref, BTA_FAILURE);
5472     }
5473 }
5474 /*******************************************************************************
5475 **
5476 ** Function         bta_dm_ble_multi_adv_param_upd
5477 **
5478 ** Description      This function updates multiple advertising instance parameters
5479 **
5480 ** Parameters:
5481 **
5482 *******************************************************************************/
bta_dm_ble_multi_adv_upd_param(tBTA_DM_MSG * p_data)5483 void bta_dm_ble_multi_adv_upd_param(tBTA_DM_MSG *p_data)
5484 {
5485     tBTM_STATUS btm_status = 0;
5486     void *p_ref = NULL;
5487 
5488     if (BTM_BleMaxMultiAdvInstanceCount() > 0 && p_data->ble_multi_adv_param.inst_id > 0
5489             && p_data->ble_multi_adv_param.inst_id < BTM_BleMaxMultiAdvInstanceCount()) {
5490         btm_status = BTM_BleUpdateAdvInstParam(p_data->ble_multi_adv_param.inst_id,
5491                                                (tBTM_BLE_ADV_PARAMS *)p_data->ble_multi_adv_param.p_params);
5492     }
5493 
5494     if (BTM_CMD_STARTED != btm_status) {
5495         p_ref = btm_ble_multi_adv_get_ref(p_data->ble_multi_adv_param.inst_id);
5496         bta_dm_cb.p_multi_adv_cback(BTA_BLE_MULTI_ADV_PARAM_EVT,
5497                                     p_data->ble_multi_adv_param.inst_id, p_ref, BTA_FAILURE);
5498     }
5499 }
5500 /*******************************************************************************
5501 **
5502 ** Function         bta_dm_ble_multi_adv_data
5503 **
5504 ** Description      This function write multiple advertising instance adv data
5505 **                  or scan response data
5506 **
5507 ** Parameters:
5508 **
5509 *******************************************************************************/
bta_dm_ble_multi_adv_data(tBTA_DM_MSG * p_data)5510 void bta_dm_ble_multi_adv_data(tBTA_DM_MSG *p_data)
5511 {
5512     tBTM_STATUS btm_status = 0;
5513     void *p_ref = NULL;
5514 
5515     if (BTM_BleMaxMultiAdvInstanceCount() > 0 && p_data->ble_multi_adv_data.inst_id > 0
5516             && p_data->ble_multi_adv_data.inst_id < BTM_BleMaxMultiAdvInstanceCount()) {
5517         btm_status = BTM_BleCfgAdvInstData(p_data->ble_multi_adv_data.inst_id,
5518                                            p_data->ble_multi_adv_data.is_scan_rsp,
5519                                            p_data->ble_multi_adv_data.data_mask,
5520                                            (tBTM_BLE_ADV_DATA *)p_data->ble_multi_adv_data.p_data);
5521     }
5522 
5523     if (BTM_CMD_STARTED != btm_status) {
5524         p_ref = btm_ble_multi_adv_get_ref(p_data->ble_multi_adv_data.inst_id);
5525         bta_dm_cb.p_multi_adv_cback(BTA_BLE_MULTI_ADV_DATA_EVT,
5526                                     p_data->ble_multi_adv_data.inst_id, p_ref, BTA_FAILURE);
5527     }
5528 
5529 }
5530 /*******************************************************************************
5531 **
5532 ** Function         btm_dm_ble_multi_adv_disable
5533 **
5534 ** Description      This function disable a single adv instance
5535 **
5536 ** Parameters:
5537 **
5538 *******************************************************************************/
btm_dm_ble_multi_adv_disable(tBTA_DM_MSG * p_data)5539 void btm_dm_ble_multi_adv_disable(tBTA_DM_MSG *p_data)
5540 {
5541     tBTM_STATUS btm_status = 0;
5542     void *p_ref = NULL;
5543 
5544     if (BTM_BleMaxMultiAdvInstanceCount() > 0 && p_data->ble_multi_adv_disable.inst_id > 0
5545             && p_data->ble_multi_adv_disable.inst_id < BTM_BleMaxMultiAdvInstanceCount()) {
5546         btm_status = BTM_BleDisableAdvInstance(p_data->ble_multi_adv_disable.inst_id);
5547     }
5548 
5549     if (BTM_CMD_STARTED != btm_status) {
5550         p_ref = btm_ble_multi_adv_get_ref(p_data->ble_multi_adv_disable.inst_id);
5551         bta_dm_cb.p_multi_adv_cback(BTA_BLE_MULTI_ADV_DISABLE_EVT,
5552                                     p_data->ble_multi_adv_disable.inst_id, p_ref, BTA_FAILURE);
5553     }
5554 }
5555 #if (BLE_50_FEATURE_SUPPORT == TRUE)
bta_dm_ble_gap_read_phy(tBTA_DM_MSG * p_data)5556 void bta_dm_ble_gap_read_phy(tBTA_DM_MSG *p_data)
5557 {
5558     //tBTM_STATUS btm_status = 0;
5559     UINT8 tx_phy = 0, rx_phy = 0;
5560 
5561     BTM_BleReadPhy(p_data->ble_read_phy.bd_addr, &tx_phy, &rx_phy);
5562 
5563 }
5564 
bta_dm_ble_gap_set_prefer_default_phy(tBTA_DM_MSG * p_data)5565 void bta_dm_ble_gap_set_prefer_default_phy(tBTA_DM_MSG *p_data)
5566 {
5567     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);
5568     BTM_BleSetPreferDefaultPhy(p_data->ble_set_per_def_phy.tx_phy_mask, p_data->ble_set_per_def_phy.rx_phy_mask);
5569 }
5570 
bta_dm_ble_gap_set_prefer_phy(tBTA_DM_MSG * p_data)5571 void bta_dm_ble_gap_set_prefer_phy(tBTA_DM_MSG *p_data)
5572 {
5573     APPL_TRACE_API("%s, tx_phy_mask = %d, rx_phy_mask = %d, phy_options = %d",
5574                      __func__, p_data->ble_set_per_phy.tx_phy_mask,
5575                      p_data->ble_set_per_phy.rx_phy_mask, p_data->ble_set_per_phy.phy_options);
5576     BTM_BleSetPreferPhy(p_data->ble_set_per_phy.bd_addr, p_data->ble_set_per_phy.all_phys,
5577                         p_data->ble_set_per_phy.tx_phy_mask, p_data->ble_set_per_phy.rx_phy_mask,
5578                         p_data->ble_set_per_phy.phy_options);
5579 }
5580 
bta_dm_ble_gap_ext_adv_set_rand_addr(tBTA_DM_MSG * p_data)5581 void bta_dm_ble_gap_ext_adv_set_rand_addr(tBTA_DM_MSG *p_data)
5582 {
5583     BTM_BleSetExtendedAdvRandaddr(p_data->ble_set_ext_adv_rand_addr.instance, p_data->ble_set_ext_adv_rand_addr.rand_addr);
5584 }
bta_dm_ble_gap_ext_adv_set_params(tBTA_DM_MSG * p_data)5585 void bta_dm_ble_gap_ext_adv_set_params(tBTA_DM_MSG *p_data)
5586 {
5587     APPL_TRACE_API("%s, instance = %d", __func__, p_data->ble_set_ext_adv_params.instance);
5588 
5589     BTM_BleSetExtendedAdvParams(p_data->ble_set_ext_adv_params.instance,
5590                                 (tBTM_BLE_GAP_EXT_ADV_PARAMS *)&p_data->ble_set_ext_adv_params.params);
5591 }
5592 
bta_dm_ble_gap_config_ext_adv_data_raw(tBTA_DM_MSG * p_data)5593 void bta_dm_ble_gap_config_ext_adv_data_raw(tBTA_DM_MSG *p_data)
5594 {
5595     APPL_TRACE_API("%s, instance = %d, len = %d", __func__, p_data->ble_cfg_ext_adv_data.instance,
5596          p_data->ble_cfg_ext_adv_data.length);
5597 
5598     BTM_BleConfigExtendedAdvDataRaw(p_data->ble_cfg_ext_adv_data.is_scan_rsp,
5599                                    p_data->ble_cfg_ext_adv_data.instance,
5600                                    p_data->ble_cfg_ext_adv_data.length,
5601                                    p_data->ble_cfg_ext_adv_data.data);
5602 }
5603 
bta_dm_ble_gap_start_ext_adv(tBTA_DM_MSG * p_data)5604 void bta_dm_ble_gap_start_ext_adv(tBTA_DM_MSG *p_data)
5605 {
5606     APPL_TRACE_API("%s, enable = %d, num = %d", __func__, p_data->ble_start_ext_adv.enable,
5607              p_data->ble_start_ext_adv.num);
5608     BTM_BleStartExtAdv(p_data->ble_start_ext_adv.enable, p_data->ble_start_ext_adv.num,
5609                        (tBTM_BLE_EXT_ADV *)p_data->ble_start_ext_adv.ext_adv);
5610 }
5611 
bta_dm_ble_gap_ext_adv_set_remove(tBTA_DM_MSG * p_data)5612 void bta_dm_ble_gap_ext_adv_set_remove(tBTA_DM_MSG *p_data)
5613 {
5614     BTM_BleExtAdvSetRemove(p_data->ble_ext_adv_set_remove.instance);
5615 }
5616 
bta_dm_ble_gap_ext_adv_set_clear(tBTA_DM_MSG * p_data)5617 void bta_dm_ble_gap_ext_adv_set_clear(tBTA_DM_MSG *p_data)
5618 {
5619     BTM_BleExtAdvSetClear();
5620 }
5621 
bta_dm_ble_gap_periodic_adv_set_params(tBTA_DM_MSG * p_data)5622 void bta_dm_ble_gap_periodic_adv_set_params(tBTA_DM_MSG *p_data)
5623 {
5624     APPL_TRACE_API("%s, instance = %d", __func__, p_data->ble_set_periodic_adv_params.instance);
5625     BTM_BlePeriodicAdvSetParams(p_data->ble_set_periodic_adv_params.instance,
5626                                (tBTM_BLE_Periodic_Adv_Params *)&p_data->ble_set_periodic_adv_params.params);
5627 }
5628 
bta_dm_ble_gap_periodic_adv_cfg_data_raw(tBTA_DM_MSG * p_data)5629 void bta_dm_ble_gap_periodic_adv_cfg_data_raw(tBTA_DM_MSG *p_data)
5630 {
5631     APPL_TRACE_API("%s, instance = %d, length = %d", __func__, p_data->ble_cfg_periodic_adv_data.instance,
5632                      p_data->ble_cfg_periodic_adv_data.length);
5633 
5634     BTM_BlePeriodicAdvCfgDataRaw(p_data->ble_cfg_periodic_adv_data.instance,
5635                                  p_data->ble_cfg_periodic_adv_data.length,
5636                                  p_data->ble_cfg_periodic_adv_data.data);
5637 }
5638 
bta_dm_ble_gap_periodic_adv_enable(tBTA_DM_MSG * p_data)5639 void bta_dm_ble_gap_periodic_adv_enable(tBTA_DM_MSG *p_data)
5640 {
5641     APPL_TRACE_API("%s, instance = %d, enable = %d", __func__, p_data->ble_enable_periodic_adv.instance,
5642                          p_data->ble_enable_periodic_adv.enable);
5643 
5644     BTM_BlePeriodicAdvEnable(p_data->ble_enable_periodic_adv.instance,
5645                              p_data->ble_enable_periodic_adv.enable);
5646 }
5647 
bta_dm_ble_gap_periodic_adv_create_sync(tBTA_DM_MSG * p_data)5648 void bta_dm_ble_gap_periodic_adv_create_sync(tBTA_DM_MSG *p_data)
5649 {
5650     APPL_TRACE_API("%s", __func__);
5651     BTM_BlePeriodicAdvCreateSync((tBTM_BLE_Periodic_Sync_Params *)&p_data->ble_periodic_adv_sync.params);
5652 }
5653 
bta_dm_ble_gap_periodic_adv_sync_cancel(tBTA_DM_MSG * p_data)5654 void bta_dm_ble_gap_periodic_adv_sync_cancel(tBTA_DM_MSG *p_data)
5655 {
5656     APPL_TRACE_API("%s", __func__);
5657     BTM_BlePeriodicAdvSyncCancel();
5658 }
5659 
bta_dm_ble_gap_periodic_adv_sync_terminate(tBTA_DM_MSG * p_data)5660 void bta_dm_ble_gap_periodic_adv_sync_terminate(tBTA_DM_MSG *p_data)
5661 {
5662     APPL_TRACE_API("%s", __func__);
5663     BTM_BlePeriodicAdvSyncTerm(p_data->ble_periodic_adv_sync_term.sync_handle);
5664 }
5665 
bta_dm_ble_gap_periodic_adv_add_dev_to_list(tBTA_DM_MSG * p_data)5666 void bta_dm_ble_gap_periodic_adv_add_dev_to_list(tBTA_DM_MSG *p_data)
5667 {
5668     APPL_TRACE_API("%s, addr_type = %d, sid = %d", __func__, p_data->ble_periodic_adv_add_dev_to_list.addr_type,
5669                     p_data->ble_periodic_adv_add_dev_to_list.sid);
5670 
5671     BTM_BlePeriodicAdvAddDevToList(p_data->ble_periodic_adv_add_dev_to_list.addr_type,
5672                                    p_data->ble_periodic_adv_add_dev_to_list.addr,
5673                                    p_data->ble_periodic_adv_add_dev_to_list.sid);
5674 }
5675 
bta_dm_ble_gap_periodic_adv_remove_dev_from_list(tBTA_DM_MSG * p_data)5676 void bta_dm_ble_gap_periodic_adv_remove_dev_from_list(tBTA_DM_MSG *p_data)
5677 {
5678     APPL_TRACE_API("%s, addr_type = %d, sid = %d", __func__, p_data->ble_periodic_adv_remove_dev_from_list.addr_type,
5679                         p_data->ble_periodic_adv_remove_dev_from_list.sid);
5680 
5681     BTM_BlePeriodicAdvRemoveDevFromList(p_data->ble_periodic_adv_remove_dev_from_list.addr_type,
5682                                         p_data->ble_periodic_adv_remove_dev_from_list.addr,
5683                                         p_data->ble_periodic_adv_remove_dev_from_list.sid);
5684 
5685 }
5686 
bta_dm_ble_gap_periodic_adv_clear_dev(tBTA_DM_MSG * p_data)5687 void bta_dm_ble_gap_periodic_adv_clear_dev(tBTA_DM_MSG *p_data)
5688 {
5689     APPL_TRACE_API("%s", __func__);
5690     BTM_BlePeriodicAdvClearDev();
5691 }
5692 
5693 
bta_dm_ble_gap_set_ext_scan_params(tBTA_DM_MSG * p_data)5694 void bta_dm_ble_gap_set_ext_scan_params(tBTA_DM_MSG *p_data)
5695 {
5696     APPL_TRACE_API("%s", __func__);
5697     BTM_BleSetExtendedScanParams((tBTM_BLE_EXT_SCAN_PARAMS *)&p_data->ble_set_ext_scan_params.params);
5698 }
5699 
bta_dm_ble_gap_ext_scan(tBTA_DM_MSG * p_data)5700 void bta_dm_ble_gap_ext_scan(tBTA_DM_MSG *p_data)
5701 {
5702     APPL_TRACE_API("%s, start = %d, duration = %d, period = %d", __func__, p_data->ble_ext_scan.start, p_data->ble_ext_scan.duration,
5703                     p_data->ble_ext_scan.period);
5704     BTM_BleExtendedScan(p_data->ble_ext_scan.start, p_data->ble_ext_scan.duration,
5705                         p_data->ble_ext_scan.period);
5706 }
5707 
bta_dm_ble_gap_set_prefer_ext_conn_params(tBTA_DM_MSG * p_data)5708 void bta_dm_ble_gap_set_prefer_ext_conn_params(tBTA_DM_MSG *p_data)
5709 {
5710     tBTM_EXT_CONN_PARAMS conn_params;
5711     conn_params.phy_mask = p_data->ble_set_per_ext_conn_params.phy_mask;
5712 
5713     APPL_TRACE_API("%s, start = %d, duration = %d, period = %d", __func__, p_data->ble_ext_scan.start, p_data->ble_ext_scan.duration,
5714                     p_data->ble_ext_scan.period);
5715 
5716     if (conn_params.phy_mask & BTA_PHY_1M_MASK) {
5717         memcpy(&conn_params.phy_1m_conn_params, &p_data->ble_set_per_ext_conn_params.phy_1m_conn_params,
5718                sizeof(tBTA_DM_BLE_CONN_PARAMS));
5719     }
5720 
5721     if (conn_params.phy_mask & BTA_PHY_2M_MASK) {
5722         memcpy(&conn_params.phy_2m_conn_params, &p_data->ble_set_per_ext_conn_params.phy_2m_conn_params,
5723                sizeof(tBTA_DM_BLE_CONN_PARAMS));
5724     }
5725 
5726     if (conn_params.phy_mask & BTAS_PHY_CODED_MASK) {
5727         memcpy(&conn_params.phy_coded_conn_params, &p_data->ble_set_per_ext_conn_params.phy_coded_conn_params,
5728                sizeof(tBTA_DM_BLE_CONN_PARAMS));
5729     }
5730 
5731     BTM_BleSetPreferExtenedConnParams(p_data->ble_set_per_ext_conn_params.bd_addr, &conn_params);
5732 }
5733 
5734 #endif // #if (BLE_50_FEATURE_SUPPORT == TRUE)
5735 /*******************************************************************************
5736 **
5737 ** Function         bta_dm_ble_setup_storage
5738 **
5739 ** Description      This function configures up the storage parameters for ADV batch scanning
5740 **
5741 ** Parameters:
5742 **
5743 *******************************************************************************/
bta_dm_ble_setup_storage(tBTA_DM_MSG * p_data)5744 void bta_dm_ble_setup_storage (tBTA_DM_MSG *p_data)
5745 {
5746     tBTM_STATUS btm_status = 0;
5747     tBTM_BLE_VSC_CB cmn_ble_vsc_cb;
5748 
5749     BTM_BleGetVendorCapabilities(&cmn_ble_vsc_cb);
5750 
5751     if (0 != cmn_ble_vsc_cb.tot_scan_results_strg) {
5752         btm_status = BTM_BleSetStorageConfig(p_data->ble_set_storage.batch_scan_full_max,
5753                                              p_data->ble_set_storage.batch_scan_trunc_max,
5754                                              p_data->ble_set_storage.batch_scan_notify_threshold,
5755                                              p_data->ble_set_storage.p_setup_cback,
5756                                              p_data->ble_set_storage.p_thres_cback,
5757                                              p_data->ble_set_storage.p_read_rep_cback,
5758                                              p_data->ble_set_storage.ref_value);
5759     }
5760 
5761     if (BTM_CMD_STARTED != btm_status) {
5762         bta_ble_scan_setup_cb(BTM_BLE_BATCH_SCAN_CFG_STRG_EVT, p_data->ble_set_storage.ref_value,
5763                               btm_status);
5764     }
5765 }
5766 
5767 /*******************************************************************************
5768 **
5769 ** Function         bta_dm_ble_enable_batch_scan
5770 **
5771 ** Description      This function sets up the parameters and enables batch scan
5772 **
5773 ** Parameters:
5774 **
5775 *******************************************************************************/
bta_dm_ble_enable_batch_scan(tBTA_DM_MSG * p_data)5776 void bta_dm_ble_enable_batch_scan (tBTA_DM_MSG *p_data)
5777 {
5778     tBTM_STATUS btm_status = 0;
5779     tBTM_BLE_VSC_CB cmn_ble_vsc_cb;
5780 
5781     BTM_BleGetVendorCapabilities(&cmn_ble_vsc_cb);
5782 
5783     if (0 != cmn_ble_vsc_cb.tot_scan_results_strg) {
5784         btm_status = BTM_BleEnableBatchScan(p_data->ble_enable_scan.scan_mode,
5785                                             p_data->ble_enable_scan.scan_int,
5786                                             p_data->ble_enable_scan.scan_window,
5787                                             p_data->ble_enable_scan.discard_rule,
5788                                             p_data->ble_enable_scan.addr_type,
5789                                             p_data->ble_enable_scan.ref_value);
5790     }
5791 
5792     if (BTM_CMD_STARTED != btm_status) {
5793         bta_ble_scan_setup_cb(BTM_BLE_BATCH_SCAN_ENABLE_EVT, p_data->ble_enable_scan.ref_value,
5794                               btm_status);
5795     }
5796 }
5797 
5798 /*******************************************************************************
5799 **
5800 ** Function         bta_dm_ble_disable_batch_scan
5801 **
5802 ** Description      This function disables the batch scan
5803 **
5804 ** Parameters:
5805 **
5806 *******************************************************************************/
bta_dm_ble_disable_batch_scan(tBTA_DM_MSG * p_data)5807 void bta_dm_ble_disable_batch_scan (tBTA_DM_MSG *p_data)
5808 {
5809     UNUSED(p_data);
5810     tBTM_STATUS btm_status = 0;
5811     tBTM_BLE_VSC_CB cmn_ble_vsc_cb;
5812 
5813     BTM_BleGetVendorCapabilities(&cmn_ble_vsc_cb);
5814 
5815     if (0 != cmn_ble_vsc_cb.tot_scan_results_strg) {
5816         btm_status = BTM_BleDisableBatchScan(p_data->ble_disable_scan.ref_value);
5817     }
5818 
5819     if (BTM_CMD_STARTED != btm_status) {
5820         bta_ble_scan_setup_cb(BTM_BLE_BATCH_SCAN_DISABLE_EVT, p_data->ble_enable_scan.ref_value,
5821                               btm_status);
5822     }
5823 }
5824 
5825 /*******************************************************************************
5826 **
5827 ** Function         bta_dm_ble_read_scan_reports
5828 **
5829 ** Description      This function reads the batch scan reports
5830 **
5831 ** Parameters:
5832 **
5833 *******************************************************************************/
bta_dm_ble_read_scan_reports(tBTA_DM_MSG * p_data)5834 void bta_dm_ble_read_scan_reports(tBTA_DM_MSG *p_data)
5835 {
5836     tBTM_STATUS btm_status = 0;
5837     tBTM_BLE_VSC_CB cmn_ble_vsc_cb;
5838 
5839     BTM_BleGetVendorCapabilities(&cmn_ble_vsc_cb);
5840 
5841     if (0 != cmn_ble_vsc_cb.tot_scan_results_strg) {
5842         btm_status = BTM_BleReadScanReports(p_data->ble_read_reports.scan_type,
5843                                             p_data->ble_read_reports.ref_value);
5844     }
5845 
5846     if (BTM_CMD_STARTED != btm_status) {
5847         bta_ble_scan_setup_cb(BTM_BLE_BATCH_SCAN_READ_REPTS_EVT, p_data->ble_enable_scan.ref_value,
5848                               btm_status);
5849     }
5850 }
5851 
5852 /*******************************************************************************
5853 **
5854 ** Function         bta_dm_ble_track_advertiser
5855 **
5856 ** Description      This function tracks the specific advertiser
5857 **
5858 ** Parameters:
5859 **
5860 *******************************************************************************/
bta_dm_ble_track_advertiser(tBTA_DM_MSG * p_data)5861 void bta_dm_ble_track_advertiser(tBTA_DM_MSG *p_data)
5862 {
5863     tBTM_STATUS btm_status = 0;
5864     BD_ADDR bda;
5865     memset(&bda, 0 , sizeof(BD_ADDR));
5866     tBTM_BLE_VSC_CB cmn_ble_vsc_cb;
5867     tBTA_DM_BLE_TRACK_ADV_DATA track_adv_data;
5868 
5869     BTM_BleGetVendorCapabilities(&cmn_ble_vsc_cb);
5870 
5871     if (0 != cmn_ble_vsc_cb.tot_scan_results_strg) {
5872         btm_status = BTM_BleTrackAdvertiser((tBTM_BLE_TRACK_ADV_CBACK *)
5873                                             p_data->ble_track_advert.p_track_adv_cback,
5874                                             p_data->ble_track_advert.ref_value);
5875     }
5876 
5877     if (BTM_CMD_STARTED != btm_status) {
5878         memset(&track_adv_data, 0, sizeof(tBTA_DM_BLE_TRACK_ADV_DATA));
5879         track_adv_data.advertiser_info_present = NO_ADV_INFO_PRESENT; /* Indicates failure */
5880         track_adv_data.client_if = (UINT8)p_data->ble_track_advert.ref_value;
5881         p_data->ble_track_advert.p_track_adv_cback(&track_adv_data);
5882     }
5883 }
5884 
5885 /*******************************************************************************
5886 **
5887 ** Function         bta_ble_scan_setup_cb
5888 **
5889 ** Description      Handle the setup callback from BTM layer and forward it to app layer
5890 **
5891 ** Parameters:
5892 **
5893 *******************************************************************************/
bta_ble_scan_setup_cb(tBTM_BLE_BATCH_SCAN_EVT evt,tBTM_BLE_REF_VALUE ref_value,tBTM_STATUS status)5894 void bta_ble_scan_setup_cb(tBTM_BLE_BATCH_SCAN_EVT evt, tBTM_BLE_REF_VALUE ref_value,
5895                            tBTM_STATUS status)
5896 {
5897     tBTA_BLE_BATCH_SCAN_EVT bta_evt = 0;
5898 
5899     APPL_TRACE_DEBUG("bta_ble_scan_setup_cb : evt: %d, ref_value: %d, status:%d", evt,
5900                      ref_value, status);
5901 
5902     switch (evt) {
5903     case BTM_BLE_BATCH_SCAN_ENABLE_EVT:
5904         bta_evt = BTA_BLE_BATCH_SCAN_ENB_EVT;
5905         break;
5906     case BTM_BLE_BATCH_SCAN_CFG_STRG_EVT:
5907         bta_evt = BTA_BLE_BATCH_SCAN_CFG_STRG_EVT;
5908         break;
5909     case BTM_BLE_BATCH_SCAN_DISABLE_EVT:
5910         bta_evt = BTA_BLE_BATCH_SCAN_DIS_EVT;
5911         break;
5912     case BTM_BLE_BATCH_SCAN_PARAM_EVT:
5913         bta_evt = BTA_BLE_BATCH_SCAN_PARAM_EVT;
5914         break;
5915     default:
5916         break;
5917     }
5918 
5919     if (NULL != bta_dm_cb.p_setup_cback) {
5920         bta_dm_cb.p_setup_cback(bta_evt, ref_value, status);
5921     }
5922 }
5923 
5924 
5925 #if BLE_ANDROID_CONTROLLER_SCAN_FILTER == TRUE
5926 /*******************************************************************************
5927 **
5928 ** Function         bta_ble_scan_pf_cmpl
5929 **
5930 ** Description      ADV payload filtering operation complete callback
5931 **
5932 **
5933 ** Returns         TRUE if handled, otherwise FALSE.
5934 **
5935 *******************************************************************************/
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)5936 static void bta_ble_scan_cfg_cmpl(tBTM_BLE_PF_ACTION action, tBTM_BLE_SCAN_COND_OP cfg_op,
5937                                   tBTM_BLE_PF_AVBL_SPACE avbl_space, tBTM_STATUS status,
5938                                   tBTM_BLE_REF_VALUE ref_value)
5939 {
5940     tBTA_STATUS st = (status == BTM_SUCCESS) ? BTA_SUCCESS : BTA_FAILURE;
5941 
5942     APPL_TRACE_DEBUG("bta_ble_scan_cfg_cmpl: %d, %d, %d, %d", action, cfg_op, avbl_space, status);
5943 
5944     if (bta_dm_cb.p_scan_filt_cfg_cback) {
5945         bta_dm_cb.p_scan_filt_cfg_cback(action, cfg_op, avbl_space, st, ref_value);
5946     }
5947 }
5948 
5949 /*******************************************************************************
5950 **
5951 ** Function         bta_dm_cfg_filter_cond
5952 **
5953 ** Description      This function configure adv payload filtering condition
5954 **
5955 ** Parameters:
5956 **
5957 *******************************************************************************/
bta_dm_cfg_filter_cond(tBTA_DM_MSG * p_data)5958 void bta_dm_cfg_filter_cond (tBTA_DM_MSG *p_data)
5959 {
5960     tBTM_STATUS st = BTM_MODE_UNSUPPORTED;
5961     tBTA_STATUS status = BTA_FAILURE;
5962 
5963     tBTM_BLE_VSC_CB cmn_vsc_cb;
5964 
5965     APPL_TRACE_DEBUG("bta_dm_cfg_filter_cond");
5966     BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
5967     if (0 != cmn_vsc_cb.filter_support) {
5968         if ((st = BTM_BleCfgFilterCondition(p_data->ble_cfg_filter_cond.action,
5969                                             p_data->ble_cfg_filter_cond.cond_type,
5970                                             (tBTM_BLE_PF_FILT_INDEX)p_data->ble_cfg_filter_cond.filt_index,
5971                                             (tBTM_BLE_PF_COND_PARAM *)p_data->ble_cfg_filter_cond.p_cond_param,
5972                                             bta_ble_scan_cfg_cmpl, p_data->ble_cfg_filter_cond.ref_value))
5973                 == BTM_CMD_STARTED) {
5974             bta_dm_cb.p_scan_filt_cfg_cback = p_data->ble_cfg_filter_cond.p_filt_cfg_cback;
5975             return;
5976         }
5977     }
5978 
5979     if (p_data->ble_cfg_filter_cond.p_filt_cfg_cback) {
5980         p_data->ble_cfg_filter_cond.p_filt_cfg_cback(BTA_DM_BLE_PF_CONFIG_EVT,
5981                 p_data->ble_cfg_filter_cond.cond_type, 0, status,
5982                 p_data->ble_cfg_filter_cond.ref_value);
5983     }
5984     return;
5985 }
5986 
5987 /*******************************************************************************
5988 **
5989 ** Function         bta_dm_enable_scan_filter
5990 **
5991 ** Description      This function enable/disable adv payload filtering condition
5992 **
5993 ** Parameters:
5994 **
5995 *******************************************************************************/
bta_dm_enable_scan_filter(tBTA_DM_MSG * p_data)5996 void bta_dm_enable_scan_filter(tBTA_DM_MSG *p_data)
5997 {
5998     tBTM_STATUS st = BTM_MODE_UNSUPPORTED;
5999     tBTA_STATUS status = BTA_FAILURE;
6000 
6001     tBTM_BLE_VSC_CB cmn_vsc_cb;
6002     APPL_TRACE_DEBUG("bta_dm_enable_scan_filter");
6003     BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
6004 
6005     if (0 != cmn_vsc_cb.filter_support) {
6006         if ((st = BTM_BleEnableDisableFilterFeature(p_data->ble_enable_scan_filt.action,
6007                   p_data->ble_enable_scan_filt.p_filt_status_cback,
6008                   (tBTM_BLE_REF_VALUE)p_data->ble_enable_scan_filt.ref_value)) == BTM_CMD_STARTED) {
6009             bta_dm_cb.p_scan_filt_status_cback = p_data->ble_enable_scan_filt.p_filt_status_cback;
6010         }
6011         return;
6012     }
6013 
6014     if (p_data->ble_enable_scan_filt.p_filt_status_cback) {
6015         p_data->ble_enable_scan_filt.p_filt_status_cback (BTA_DM_BLE_PF_ENABLE_EVT,
6016                 p_data->ble_enable_scan_filt.ref_value, status);
6017     }
6018 
6019 }
6020 
6021 /*******************************************************************************
6022 **
6023 ** Function         bta_dm_scan_filter_param_setup
6024 **
6025 ** Description      This function sets up scan filter params
6026 **
6027 ** Parameters:
6028 **
6029 *******************************************************************************/
bta_dm_scan_filter_param_setup(tBTA_DM_MSG * p_data)6030 void bta_dm_scan_filter_param_setup (tBTA_DM_MSG *p_data)
6031 {
6032     tBTM_STATUS st = BTM_MODE_UNSUPPORTED;
6033     tBTA_STATUS status = BTA_FAILURE;
6034 
6035     tBTM_BLE_VSC_CB cmn_vsc_cb;
6036 
6037     APPL_TRACE_DEBUG("bta_dm_scan_filter_param_setup");
6038     BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
6039     if (0 != cmn_vsc_cb.filter_support) {
6040         if ((st = BTM_BleAdvFilterParamSetup(p_data->ble_scan_filt_param_setup.action,
6041                                              p_data->ble_scan_filt_param_setup.filt_index,
6042                                              (tBTM_BLE_PF_FILT_PARAMS *)&p_data->ble_scan_filt_param_setup.filt_params,
6043                                              p_data->ble_scan_filt_param_setup.p_target,
6044                                              p_data->ble_scan_filt_param_setup.p_filt_param_cback,
6045                                              p_data->ble_scan_filt_param_setup.ref_value)) == BTM_CMD_STARTED) {
6046             bta_dm_cb.p_scan_filt_param_cback = p_data->ble_scan_filt_param_setup.p_filt_param_cback;
6047             return;
6048         }
6049     }
6050 
6051     if (p_data->ble_scan_filt_param_setup.p_filt_param_cback) {
6052         p_data->ble_scan_filt_param_setup.p_filt_param_cback (BTA_DM_BLE_PF_ENABLE_EVT, 0,
6053                 p_data->ble_scan_filt_param_setup.ref_value, status);
6054     }
6055 
6056     return;
6057 }
6058 #endif
6059 
6060 /*******************************************************************************
6061 **
6062 ** Function         bta_ble_enable_scan_cmpl
6063 **
6064 ** Description      ADV payload filtering enable / disable complete callback
6065 **
6066 **
6067 ** Returns          None
6068 **
6069 *******************************************************************************/
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)6070 static void bta_ble_energy_info_cmpl(tBTM_BLE_TX_TIME_MS tx_time,
6071                                      tBTM_BLE_RX_TIME_MS rx_time,
6072                                      tBTM_BLE_IDLE_TIME_MS idle_time,
6073                                      tBTM_BLE_ENERGY_USED  energy_used,
6074                                      tBTM_STATUS status)
6075 {
6076     tBTA_STATUS st = (status == BTM_SUCCESS) ? BTA_SUCCESS : BTA_FAILURE;
6077     tBTA_DM_CONTRL_STATE ctrl_state = 0;
6078 #if ((defined BTA_GATT_INCLUDED) &&  (BTA_GATT_INCLUDED == TRUE) && SDP_INCLUDED == TRUE)
6079     if (BTA_SUCCESS == st) {
6080         ctrl_state = bta_dm_pm_obtain_controller_state();
6081     }
6082 #endif
6083     if (bta_dm_cb.p_energy_info_cback) {
6084         bta_dm_cb.p_energy_info_cback(tx_time, rx_time, idle_time, energy_used, ctrl_state, st);
6085     }
6086 }
6087 
6088 /*******************************************************************************
6089 **
6090 ** Function         bta_dm_ble_get_energy_info
6091 **
6092 ** Description      This function obtains the energy info
6093 **
6094 ** Parameters:
6095 **
6096 *******************************************************************************/
bta_dm_ble_get_energy_info(tBTA_DM_MSG * p_data)6097 void bta_dm_ble_get_energy_info(tBTA_DM_MSG *p_data)
6098 {
6099     tBTM_STATUS btm_status = 0;
6100 
6101     bta_dm_cb.p_energy_info_cback = p_data->ble_energy_info.p_energy_info_cback;
6102     btm_status = BTM_BleGetEnergyInfo(bta_ble_energy_info_cmpl);
6103     if (BTM_CMD_STARTED != btm_status) {
6104         bta_ble_energy_info_cmpl(0, 0, 0, 0, btm_status);
6105     }
6106 }
6107 
6108 #if ((defined BTA_GATT_INCLUDED) &&  (BTA_GATT_INCLUDED == TRUE) && SDP_INCLUDED == TRUE)
6109 #ifndef BTA_DM_GATT_CLOSE_DELAY_TOUT
6110 #define BTA_DM_GATT_CLOSE_DELAY_TOUT    1000
6111 #endif
6112 
6113 /*******************************************************************************
6114 **
6115 ** Function         bta_dm_gattc_register
6116 **
6117 ** Description      Register with GATTC in DM if BLE is needed.
6118 **
6119 **
6120 ** Returns          void
6121 **
6122 *******************************************************************************/
6123 #if (GATTC_INCLUDED == TRUE)
bta_dm_gattc_register(void)6124 static void bta_dm_gattc_register(void)
6125 {
6126     tBT_UUID                app_uuid = {LEN_UUID_128, {0}};
6127 
6128     if (bta_dm_search_cb.client_if == BTA_GATTS_INVALID_IF) {
6129         memset (&app_uuid.uu.uuid128, 0x87, LEN_UUID_128);
6130         BTA_GATTC_AppRegister(&app_uuid, bta_dm_gattc_callback);
6131     }
6132 }
6133 #endif /* GATTC_INCLUDED == TRUE */
6134 
6135 /*******************************************************************************
6136 **
6137 ** Function         btm_dm_start_disc_gatt_services
6138 **
6139 ** Description      This function starts a GATT service search request.
6140 **
6141 ** Parameters:
6142 **
6143 *******************************************************************************/
6144 #if (GATTC_INCLUDED == TRUE)
btm_dm_start_disc_gatt_services(UINT16 conn_id)6145 static void btm_dm_start_disc_gatt_services (UINT16 conn_id)
6146 {
6147     tBT_UUID    *p_uuid = bta_dm_search_cb.p_srvc_uuid +
6148                           bta_dm_search_cb.num_uuid - bta_dm_search_cb.uuid_to_search;
6149 
6150     p_uuid = bta_dm_search_cb.p_srvc_uuid +
6151              bta_dm_search_cb.num_uuid - bta_dm_search_cb.uuid_to_search;
6152 
6153     /* always search for all services */
6154     BTA_GATTC_ServiceSearchRequest(conn_id, p_uuid);
6155 }
6156 #endif /* GATTC_INCLUDED == TRUE */
6157 
6158 /*******************************************************************************
6159 **
6160 ** Function         bta_dm_gatt_disc_result
6161 **
6162 ** Description      This function process the GATT service search result.
6163 **
6164 ** Parameters:
6165 **
6166 *******************************************************************************/
6167 #if (GATTC_INCLUDED == TRUE)
bta_dm_gatt_disc_result(tBTA_GATT_ID service_id)6168 static void bta_dm_gatt_disc_result(tBTA_GATT_ID service_id)
6169 {
6170     tBTA_DM_SEARCH   result;
6171 
6172     /*
6173         * This logic will not work for gatt case.  We are checking against the bluetooth profiles here
6174         * just copy the GATTID in raw data field and send it across.
6175         */
6176 
6177 
6178     if ( bta_dm_search_cb.ble_raw_used + sizeof(tBTA_GATT_ID) < bta_dm_search_cb.ble_raw_size ) {
6179         APPL_TRACE_DEBUG("ADDING BLE SERVICE uuid=0x%x, ble_ptr = %p, ble_raw_used = 0x%x",
6180                          service_id.uuid.uu.uuid16, bta_dm_search_cb.p_ble_rawdata, bta_dm_search_cb.ble_raw_used);
6181 
6182         if (bta_dm_search_cb.p_ble_rawdata) {
6183             memcpy((bta_dm_search_cb.p_ble_rawdata + bta_dm_search_cb.ble_raw_used), &service_id,
6184                    sizeof(service_id) );
6185 
6186             bta_dm_search_cb.ble_raw_used += sizeof(service_id);
6187         } else {
6188             APPL_TRACE_ERROR("p_ble_rawdata is NULL");
6189         }
6190 
6191     } else {
6192         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 );
6193     }
6194 
6195     APPL_TRACE_API("%s service_id_uuid_len=%d ", __func__, service_id.uuid.len);
6196     if ( bta_dm_search_cb.state != BTA_DM_SEARCH_IDLE) {
6197 
6198         /* send result back to app now, one by one */
6199         bdcpy (result.disc_ble_res.bd_addr, bta_dm_search_cb.peer_bdaddr);
6200         BCM_STRNCPY_S((char *)result.disc_ble_res.bd_name, bta_dm_get_remname(), (BD_NAME_LEN - 1));
6201         result.disc_ble_res.bd_name[BD_NAME_LEN] = 0;
6202         memcpy(&result.disc_ble_res.service, &service_id.uuid, sizeof(tBT_UUID));
6203 
6204         bta_dm_search_cb.p_search_cback(BTA_DM_DISC_BLE_RES_EVT, &result);
6205     }
6206 }
6207 #endif /* #if (GATTC_INCLUDED == TRUE) */
6208 /*******************************************************************************
6209 **
6210 ** Function         bta_dm_gatt_disc_complete
6211 **
6212 ** Description      This function process the GATT service search complete.
6213 **
6214 ** Parameters:
6215 **
6216 *******************************************************************************/
6217 #if (GATTC_INCLUDED == TRUE)
bta_dm_gatt_disc_complete(UINT16 conn_id,tBTA_GATT_STATUS status)6218 static void bta_dm_gatt_disc_complete(UINT16 conn_id, tBTA_GATT_STATUS status)
6219 {
6220     tBTA_DM_MSG *p_msg;
6221 
6222     APPL_TRACE_DEBUG("bta_dm_gatt_disc_complete conn_id = %d", conn_id);
6223 
6224     if (bta_dm_search_cb.uuid_to_search > 0) {
6225         bta_dm_search_cb.uuid_to_search --;
6226     }
6227 
6228     if (status == BTA_GATT_OK && bta_dm_search_cb.uuid_to_search > 0) {
6229         btm_dm_start_disc_gatt_services(conn_id);
6230     } else {
6231         bta_dm_search_cb.uuid_to_search = 0;
6232 
6233         /* no more services to be discovered */
6234         if ((p_msg = (tBTA_DM_MSG *) osi_malloc(sizeof(tBTA_DM_MSG))) != NULL) {
6235             p_msg->hdr.event = BTA_DM_DISCOVERY_RESULT_EVT;
6236             p_msg->disc_result.result.disc_res.result = (status == BTA_GATT_OK) ? BTA_SUCCESS : BTA_FAILURE;
6237             APPL_TRACE_DEBUG("%s service found: 0x%08x", __FUNCTION__,
6238                              bta_dm_search_cb.services_found);
6239             p_msg->disc_result.result.disc_res.services = bta_dm_search_cb.services_found;
6240             p_msg->disc_result.result.disc_res.num_uuids = 0;
6241             p_msg->disc_result.result.disc_res.p_uuid_list = NULL;
6242             bdcpy (p_msg->disc_result.result.disc_res.bd_addr, bta_dm_search_cb.peer_bdaddr);
6243             BCM_STRNCPY_S((char *)p_msg->disc_result.result.disc_res.bd_name,bta_dm_get_remname(), (BD_NAME_LEN - 1));
6244 
6245             /* make sure the string is terminated */
6246             p_msg->disc_result.result.disc_res.bd_name[BD_NAME_LEN - 1] = 0;
6247 
6248             p_msg->disc_result.result.disc_res.device_type |= BT_DEVICE_TYPE_BLE;
6249             if ( bta_dm_search_cb.ble_raw_used > 0 ) {
6250                 p_msg->disc_result.result.disc_res.p_raw_data = osi_malloc(bta_dm_search_cb.ble_raw_used);
6251 
6252                 memcpy( p_msg->disc_result.result.disc_res.p_raw_data,
6253                         bta_dm_search_cb.p_ble_rawdata,
6254                         bta_dm_search_cb.ble_raw_used );
6255 
6256                 p_msg->disc_result.result.disc_res.raw_data_size = bta_dm_search_cb.ble_raw_used;
6257             } else {
6258                 p_msg->disc_result.result.disc_res.p_raw_data = NULL;
6259                 bta_dm_search_cb.p_ble_rawdata = 0;
6260             }
6261 
6262             bta_sys_sendmsg(p_msg);
6263         }
6264 
6265         if (conn_id != BTA_GATT_INVALID_CONN_ID) {
6266             /* start a GATT channel close delay timer */
6267             bta_sys_start_timer(&bta_dm_search_cb.gatt_close_timer, BTA_DM_DISC_CLOSE_TOUT_EVT,
6268                                 BTA_DM_GATT_CLOSE_DELAY_TOUT);
6269             bdcpy(bta_dm_search_cb.pending_close_bda, bta_dm_search_cb.peer_bdaddr);
6270         }
6271         bta_dm_search_cb.gatt_disc_active = FALSE;
6272     }
6273 }
6274 #endif /* #if (GATTC_INCLUDED == TRUE) */
6275 
6276 /*******************************************************************************
6277 **
6278 ** Function         bta_dm_close_gatt_conn
6279 **
6280 ** Description      This function close the GATT connection after delay timeout.
6281 **
6282 ** Parameters:
6283 **
6284 *******************************************************************************/
6285 #if (GATTC_INCLUDED == TRUE)
bta_dm_close_gatt_conn(tBTA_DM_MSG * p_data)6286 void bta_dm_close_gatt_conn(tBTA_DM_MSG *p_data)
6287 {
6288     UNUSED(p_data);
6289 
6290     if (bta_dm_search_cb.conn_id != BTA_GATT_INVALID_CONN_ID) {
6291         BTA_GATTC_Close(bta_dm_search_cb.conn_id);
6292     }
6293 
6294     memset(bta_dm_search_cb.pending_close_bda, 0, BD_ADDR_LEN);
6295     bta_dm_search_cb.conn_id = BTA_GATT_INVALID_CONN_ID;
6296 }
6297 #endif /* #if (GATTC_INCLUDED == TRUE) */
6298 /*******************************************************************************
6299 **
6300 ** Function         btm_dm_start_gatt_discovery
6301 **
6302 ** Description      This is GATT initiate the service search by open a GATT connection
6303 **                  first.
6304 **
6305 ** Parameters:
6306 **
6307 *******************************************************************************/
6308 #if (GATTC_INCLUDED == TRUE)
btm_dm_start_gatt_discovery(BD_ADDR bd_addr)6309 void btm_dm_start_gatt_discovery (BD_ADDR bd_addr)
6310 {
6311     bta_dm_search_cb.gatt_disc_active = TRUE;
6312 
6313     /* connection is already open */
6314     if (bdcmp(bta_dm_search_cb.pending_close_bda, bd_addr) == 0 &&
6315             bta_dm_search_cb.conn_id != BTA_GATT_INVALID_CONN_ID) {
6316         memset(bta_dm_search_cb.pending_close_bda, 0, BD_ADDR_LEN);
6317         bta_sys_stop_timer(&bta_dm_search_cb.gatt_close_timer);
6318         btm_dm_start_disc_gatt_services(bta_dm_search_cb.conn_id);
6319     } else {
6320         //TODO need to add addr_type in future
6321         BTA_GATTC_Open(bta_dm_search_cb.client_if, bd_addr, BLE_ADDR_UNKNOWN_TYPE, TRUE, BTA_GATT_TRANSPORT_LE, FALSE);
6322     }
6323 }
6324 #endif /* #if (GATTC_INCLUDED == TRUE) */
6325 /*******************************************************************************
6326 **
6327 ** Function         bta_dm_cancel_gatt_discovery
6328 **
6329 ** Description      This is GATT cancel the GATT service search.
6330 **
6331 ** Parameters:
6332 **
6333 *******************************************************************************/
6334 #if (GATTC_INCLUDED == TRUE)
bta_dm_cancel_gatt_discovery(BD_ADDR bd_addr)6335 static void bta_dm_cancel_gatt_discovery(BD_ADDR bd_addr)
6336 {
6337     if (bta_dm_search_cb.conn_id == BTA_GATT_INVALID_CONN_ID) {
6338         BTA_GATTC_CancelOpen(bta_dm_search_cb.client_if, bd_addr, TRUE);
6339     }
6340 
6341     bta_dm_gatt_disc_complete(bta_dm_search_cb.conn_id, (tBTA_GATT_STATUS) BTA_GATT_ERROR);
6342 }
6343 #endif /* #if (GATTC_INCLUDED == TRUE) */
6344 
6345 /*******************************************************************************
6346 **
6347 ** Function         bta_dm_proc_open_evt
6348 **
6349 ** Description      process BTA_GATTC_OPEN_EVT in DM.
6350 **
6351 ** Parameters:
6352 **
6353 *******************************************************************************/
6354 #if (GATTC_INCLUDED == TRUE)
bta_dm_proc_open_evt(tBTA_GATTC_OPEN * p_data)6355 void bta_dm_proc_open_evt(tBTA_GATTC_OPEN *p_data)
6356 {
6357     UINT8           *p1;
6358     UINT8           *p2;
6359 
6360     p1 = bta_dm_search_cb.peer_bdaddr;
6361     p2 = p_data->remote_bda;
6362 
6363     APPL_TRACE_DEBUG("DM Search state= %d search_cb.peer_dbaddr: [%08x%04x] connected_bda= [%08x%04x] ",
6364                      bta_dm_search_cb.state,
6365                      ((p1[0]) << 24) + ((p1[1]) << 16) + ((p1[2]) << 8) + (p1[3]),
6366                      ((p1[4]) << 8) + p1[5],
6367                      ((p2[0]) << 24) + ((p2[1]) << 16) + ((p2[2]) << 8) + (p2[3]),
6368                      ((p2[4]) << 8) + p2[5]);
6369 
6370     UNUSED(p1);
6371     UNUSED(p2);
6372     APPL_TRACE_DEBUG("BTA_GATTC_OPEN_EVT conn_id = %d client_if=%d status = %d" ,
6373                      p_data->conn_id,
6374                      p_data->client_if,
6375                      p_data->status);
6376 
6377     bta_dm_search_cb.conn_id = p_data->conn_id;
6378 
6379     if (p_data->status == BTA_GATT_OK) {
6380         btm_dm_start_disc_gatt_services(p_data->conn_id);
6381     } else {
6382         bta_dm_gatt_disc_complete(BTA_GATT_INVALID_CONN_ID, p_data->status);
6383     }
6384 }
6385 #endif /* #if (GATTC_INCLUDED == TRUE) */
6386 
6387 /*******************************************************************************
6388 **
6389 ** Function         bta_dm_gattc_callback
6390 **
6391 ** Description      This is GATT client callback function used in DM.
6392 **
6393 ** Parameters:
6394 **
6395 *******************************************************************************/
6396 #if (GATTC_INCLUDED == TRUE)
bta_dm_gattc_callback(tBTA_GATTC_EVT event,tBTA_GATTC * p_data)6397 static void bta_dm_gattc_callback(tBTA_GATTC_EVT event, tBTA_GATTC *p_data)
6398 {
6399     APPL_TRACE_DEBUG("bta_dm_gattc_callback event = %d", event);
6400 
6401     switch (event) {
6402     case BTA_GATTC_REG_EVT:
6403         APPL_TRACE_DEBUG("BTA_GATTC_REG_EVT client_if = %d",  p_data->reg_oper.client_if);
6404         if (p_data->reg_oper.status == BTA_GATT_OK) {
6405             bta_dm_search_cb.client_if = p_data->reg_oper.client_if;
6406         } else {
6407             bta_dm_search_cb.client_if = BTA_GATTS_INVALID_IF;
6408         }
6409         break;
6410 
6411     case BTA_GATTC_OPEN_EVT:
6412         bta_dm_proc_open_evt(&p_data->open);
6413         break;
6414 
6415     case BTA_GATTC_SEARCH_RES_EVT:
6416         bta_dm_gatt_disc_result(p_data->srvc_res.service_uuid);
6417         break;
6418 
6419     case BTA_GATTC_SEARCH_CMPL_EVT:
6420         if ( bta_dm_search_cb.state != BTA_DM_SEARCH_IDLE) {
6421             bta_dm_gatt_disc_complete(p_data->search_cmpl.conn_id, p_data->search_cmpl.status);
6422         }
6423         break;
6424 
6425     case BTA_GATTC_CLOSE_EVT:
6426         APPL_TRACE_DEBUG("BTA_GATTC_CLOSE_EVT reason = %d", p_data->close.reason);
6427         /* in case of disconnect before search is completed */
6428         if ( (bta_dm_search_cb.state != BTA_DM_SEARCH_IDLE) &&
6429                 (bta_dm_search_cb.state != BTA_DM_SEARCH_ACTIVE) &&
6430                 !memcmp(p_data->close.remote_bda, bta_dm_search_cb.peer_bdaddr, BD_ADDR_LEN)) {
6431             bta_dm_gatt_disc_complete((UINT16)BTA_GATT_INVALID_CONN_ID,  (tBTA_GATT_STATUS) BTA_GATT_ERROR);
6432         }
6433         break;
6434 
6435     default:
6436         break;
6437     }
6438 }
6439 #endif /* #if (GATTC_INCLUDED == TRUE) */
6440 #endif /* BTA_GATT_INCLUDED */
6441 
6442 #if BLE_VND_INCLUDED == TRUE
6443 /*******************************************************************************
6444 **
6445 ** Function         bta_dm_ctrl_features_rd_cmpl_cback
6446 **
6447 ** Description      callback to handle controller feature read complete
6448 **
6449 ** Parameters:
6450 **
6451 *******************************************************************************/
bta_dm_ctrl_features_rd_cmpl_cback(tBTM_STATUS result)6452 static void bta_dm_ctrl_features_rd_cmpl_cback(tBTM_STATUS result)
6453 {
6454     APPL_TRACE_DEBUG("%s  status = %d ", __FUNCTION__, result);
6455     if (result == BTM_SUCCESS) {
6456         if (bta_dm_cb.p_sec_cback) {
6457             bta_dm_cb.p_sec_cback(BTA_DM_LE_FEATURES_READ, NULL);
6458         }
6459     } else {
6460         APPL_TRACE_ERROR("%s Ctrl BLE feature read failed: status :%d", __FUNCTION__, result);
6461     }
6462 
6463 }
6464 #endif /* BLE_VND_INCLUDED */
6465 
6466 #endif  /* BLE_INCLUDED */
6467