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