1 /******************************************************************************
2  *
3  *  Copyright (C) 2003-2012 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 is the main implementation file for the BTA device manager.
22  *
23  ******************************************************************************/
24 
25 #include "bta/bta_api.h"
26 #include "bta/bta_sys.h"
27 #include "bta_dm_int.h"
28 #include "osi/allocator.h"
29 #include <string.h>
30 
31 #include "esp_coexist.h"
32 
33 /*****************************************************************************
34 ** Constants and types
35 *****************************************************************************/
36 
37 #if BTA_DYNAMIC_MEMORY == FALSE
38 tBTA_DM_CB  bta_dm_cb;
39 tBTA_DM_SEARCH_CB bta_dm_search_cb;
40 tBTA_DM_DI_CB       bta_dm_di_cb;
41 #else
42 tBTA_DM_CB  *bta_dm_cb_ptr;
43 tBTA_DM_SEARCH_CB *bta_dm_search_cb_ptr;
44 tBTA_DM_DI_CB       *bta_dm_di_cb_ptr;
45 #endif
46 
47 
48 #define BTA_DM_NUM_ACTIONS  (BTA_DM_MAX_EVT & 0x00ff)
49 
50 /* type for action functions */
51 typedef void (*tBTA_DM_ACTION)(tBTA_DM_MSG *p_data);
52 
53 /* action function list */
54 const tBTA_DM_ACTION bta_dm_action[BTA_DM_MAX_EVT] = {
55 
56     /* device manager local device API events */
57     bta_dm_enable,                          /* BTA_DM_API_ENABLE_EVT */
58     bta_dm_disable,                         /* BTA_DM_API_DISABLE_EVT */
59     bta_dm_set_dev_name,                    /* BTA_DM_API_SET_NAME_EVT */
60 #if (CLASSIC_BT_INCLUDED == TRUE)
61     bta_dm_config_eir,                      /* BTA_DM_API_CONFIG_EIR_EVT */
62 #endif
63     bta_dm_set_afh_channels,                /* BTA_DM_API_SET_AFH_CHANNELS_EVT */
64 #if (SDP_INCLUDED == TRUE)
65     bta_dm_read_rmt_name,                    /* BTA_DM_API_GET_REMOTE_NAME_EVT*/
66 #endif
67     bta_dm_set_visibility,                  /* BTA_DM_API_SET_VISIBILITY_EVT */
68     bta_dm_acl_change,                      /* BTA_DM_ACL_CHANGE_EVT */
69     bta_dm_add_device,                      /* BTA_DM_API_ADD_DEVICE_EVT */
70     bta_dm_close_acl,                       /* BTA_DM_API_REMOVE_ACL_EVT */
71 #if (SMP_INCLUDED == TRUE)
72     /* security API events */
73     bta_dm_bond,                            /* BTA_DM_API_BOND_EVT */
74     bta_dm_bond_cancel,                     /* BTA_DM_API_BOND_CANCEL_EVT */
75     bta_dm_set_pin_type,                    /* BTA_DM_API_SET_PIN_TYPE_EVT */
76     bta_dm_pin_reply,                       /* BTA_DM_API_PIN_REPLY_EVT */
77 #endif  ///SMP_INCLUDED == TRUE
78 #if (BTA_DM_PM_INCLUDED == TRUE)
79     /* power manger events */
80     bta_dm_pm_btm_status,                   /* BTA_DM_PM_BTM_STATUS_EVT */
81     bta_dm_pm_timer,                        /* BTA_DM_PM_TIMER_EVT */
82 #endif /* #if (BTA_DM_PM_INCLUDED == TRUE) */
83 #if (BTA_DM_QOS_INCLUDED == TRUE)
84     /* Quality of Service set events */
85     bta_dm_set_qos,                         /* BTA_DM_API_QOS_SET_EVT */
86 #endif /* #if (BTA_DM_QOS_INCLUDED == TRUE) */
87     /* simple pairing events */
88 #if (SMP_INCLUDED == TRUE)
89     bta_dm_confirm,                         /* BTA_DM_API_CONFIRM_EVT */
90 #if (BT_SSP_INCLUDED == TRUE)
91     bta_dm_key_req,                         /* BTA_DM_API_KEY_REQ_EVT */
92 #endif ///BT_SSP_INCLUDED == TRUE
93     bta_dm_set_encryption,                  /* BTA_DM_API_SET_ENCRYPTION_EVT */
94 #endif  ///SMP_INCLUDED == TRUE
95 #if (BTM_OOB_INCLUDED == TRUE && SMP_INCLUDED == TRUE)
96     bta_dm_loc_oob,                         /* BTA_DM_API_LOC_OOB_EVT */
97     bta_dm_oob_reply,                       /* BTA_DM_API_OOB_REPLY_EVT */
98     bta_dm_ci_io_req_act,                   /* BTA_DM_CI_IO_REQ_EVT */
99     bta_dm_ci_rmt_oob_act,                  /* BTA_DM_CI_RMT_OOB_EVT */
100 #endif /* BTM_OOB_INCLUDED */
101 
102 
103 #if BLE_INCLUDED == TRUE
104 #if SMP_INCLUDED == TRUE
105     bta_dm_add_blekey,                      /*  BTA_DM_API_ADD_BLEKEY_EVT           */
106     bta_dm_add_ble_device,                  /*  BTA_DM_API_ADD_BLEDEVICE_EVT        */
107     bta_dm_ble_passkey_reply,               /*  BTA_DM_API_BLE_PASSKEY_REPLY_EVT    */
108     bta_dm_ble_set_static_passkey,          /* BTA_DM_API_BLE_SET_STATIC_PASSKEY_EVT */
109     bta_dm_ble_confirm_reply,               /*  BTA_DM_API_BLE_CONFIRM_REPLY_EVT    */
110     bta_dm_security_grant,
111 #endif  ///SMP_INCLUDED == TRUE
112     bta_dm_ble_set_bg_conn_type,
113     bta_dm_ble_set_conn_params,             /* BTA_DM_API_BLE_CONN_PARAM_EVT */
114     bta_dm_ble_set_conn_scan_params,        /* BTA_DM_API_BLE_CONN_SCAN_PARAM_EVT */
115     bta_dm_ble_set_scan_params,             /* BTA_DM_API_BLE_SCAN_PARAM_EVT */
116     bta_dm_ble_set_scan_fil_params,         /* BTA_DM_API_BLE_SCAN_FIL_PARAM_EVT */
117     bta_dm_ble_observe,                     /* BTA_DM_API_BLE_OBSERVE_EVT */
118     bta_dm_ble_scan,                        /* BTA_DM_API_BLE_SCAN_EVT */
119     bta_dm_ble_update_conn_params,          /* BTA_DM_API_UPDATE_CONN_PARAM_EVT */
120     /* This handler function added by
121        Yulong at 2016/9/9 to support the
122        random address setting for the APP */
123     bta_dm_ble_set_rand_address,            /* BTA_DM_API_SET_RAND_ADDR_EVT*/
124     bta_dm_ble_clear_rand_address,          /* BTA_DM_API_CLEAR_RAND_ADDR_EVT */
125     /* This handler function added by
126        Yulong at 2016/10/19 to support
127        stop the ble advertising setting
128        by the APP */
129     bta_dm_ble_stop_advertising,            /* BTA_DM_API_BLE_STOP_ADV_EVT */
130 #if BLE_PRIVACY_SPT == TRUE
131     bta_dm_ble_config_local_privacy,        /* BTA_DM_API_LOCAL_PRIVACY_EVT */
132 #endif
133     bta_dm_ble_config_local_icon,           /* BTA_DM_API_LOCAL_ICON_EVT */
134     bta_dm_ble_set_adv_params,              /* BTA_DM_API_BLE_ADV_PARAM_EVT */
135     bta_dm_ble_set_adv_params_all,          /* BTA_DM_API_BLE_ADV_PARAM_All_EVT */
136     bta_dm_ble_set_adv_config,              /* BTA_DM_API_BLE_SET_ADV_CONFIG_EVT */
137     /* New function to allow set raw adv
138        data to HCI */
139     bta_dm_ble_set_adv_config_raw,          /* BTA_DM_API_BLE_SET_ADV_CONFIG_RAW_EVT */
140     bta_dm_ble_set_scan_rsp,                /* BTA_DM_API_BLE_SET_SCAN_RSP_EVT */
141     /* New function to allow set raw scan
142        response data to HCI */
143     bta_dm_ble_set_scan_rsp_raw,            /* BTA_DM_API_BLE_SET_SCAN_RSP_RAW_EVT */
144     bta_dm_ble_broadcast,                   /* BTA_DM_API_BLE_BROADCAST_EVT */
145     bta_dm_ble_set_data_length,             /* BTA_DM_API_SET_DATA_LENGTH_EVT */
146     bta_dm_ble_set_long_adv,                /* BTA_DM_API_BLE_SET_LONG_ADV_EVT */
147 #if BLE_ANDROID_CONTROLLER_SCAN_FILTER == TRUE
148     bta_dm_cfg_filter_cond,                 /* BTA_DM_API_CFG_FILTER_COND_EVT */
149     bta_dm_scan_filter_param_setup,         /* BTA_DM_API_SCAN_FILTER_SETUP_EVT */
150     bta_dm_enable_scan_filter,              /* BTA_DM_API_SCAN_FILTER_ENABLE_EVT */
151 #endif
152     bta_dm_ble_multi_adv_enb,               /* BTA_DM_API_BLE_MULTI_ADV_ENB_EVT */
153     bta_dm_ble_multi_adv_upd_param,         /* BTA_DM_API_BLE_MULTI_ADV_PARAM_UPD_EVT */
154     bta_dm_ble_multi_adv_data,              /* BTA_DM_API_BLE_MULTI_ADV_DATA_EVT */
155     btm_dm_ble_multi_adv_disable,           /* BTA_DM_API_BLE_MULTI_ADV_DISABLE_EVT */
156     bta_dm_ble_setup_storage,               /* BTA_DM_API_BLE_SETUP_STORAGE_EVT */
157     bta_dm_ble_enable_batch_scan,           /* BTA_DM_API_BLE_ENABLE_BATCH_SCAN_EVT */
158     bta_dm_ble_disable_batch_scan,          /* BTA_DM_API_BLE_DISABLE_BATCH_SCAN_EVT */
159     bta_dm_ble_read_scan_reports,           /* BTA_DM_API_BLE_READ_SCAN_REPORTS_EVT */
160     bta_dm_ble_track_advertiser,            /* BTA_DM_API_BLE_TRACK_ADVERTISER_EVT */
161     bta_dm_ble_get_energy_info,             /* BTA_DM_API_BLE_ENERGY_INFO_EVT */
162     bta_dm_ble_disconnect,                  /* BTA_DM_API_BLE_DISCONNECT_EVT */
163 #endif
164 
165     bta_dm_enable_test_mode,                /* BTA_DM_API_ENABLE_TEST_MODE_EVT */
166     bta_dm_disable_test_mode,               /* BTA_DM_API_DISABLE_TEST_MODE_EVT */
167     bta_dm_execute_callback,                /* BTA_DM_API_EXECUTE_CBACK_EVT */
168 
169     bta_dm_remove_all_acl,                  /* BTA_DM_API_REMOVE_ALL_ACL_EVT */
170     bta_dm_remove_device,                   /* BTA_DM_API_REMOVE_DEVICE_EVT */
171     bta_dm_ble_set_channels,                /* BTA_DM_API_BLE_SET_CHANNELS_EVT */
172     bta_dm_update_white_list,               /* BTA_DM_API_UPDATE_WHITE_LIST_EVT */
173     bta_dm_clear_white_list,                /* BTA_DM_API_CLEAR_WHITE_LIST_EVT */
174     bta_dm_ble_read_adv_tx_power,           /* BTA_DM_API_BLE_READ_ADV_TX_POWER_EVT */
175     bta_dm_read_rssi,                       /* BTA_DM_API_READ_RSSI_EVT */
176 #if BLE_INCLUDED == TRUE
177     bta_dm_ble_update_duplicate_exceptional_list,/* BTA_DM_API_UPDATE_DUPLICATE_EXCEPTIONAL_LIST_EVT */
178 #endif
179 #if (BLE_50_FEATURE_SUPPORT == TRUE)
180     bta_dm_ble_gap_read_phy,                /* BTA_DM_API_READ_PHY_EVT */
181     bta_dm_ble_gap_set_prefer_default_phy,  /* BTA_DM_API_SET_PER_DEF_PHY_EVT */
182     bta_dm_ble_gap_set_prefer_phy,          /* BTA_DM_API_SET_PER_PHY_EVT */
183     bta_dm_ble_gap_ext_adv_set_rand_addr,   /* BTA_DM_API_SET_EXT_ADV_RAND_ADDR_EVT */
184     bta_dm_ble_gap_ext_adv_set_params,      /* BTA_DM_API_SET_EXT_ADV_PARAMS_EVT */
185     bta_dm_ble_gap_config_ext_adv_data_raw, /* BTA_DM_API_CFG_ADV_DATA_RAW_EVT */
186     bta_dm_ble_gap_start_ext_adv,           /* BTA_DM_API_EXT_ADV_ENABLE_EVT */
187     bta_dm_ble_gap_ext_adv_set_remove,      /* BTA_DM_API_EXT_ADV_SET_REMOVE_EVT */
188     bta_dm_ble_gap_ext_adv_set_clear,       /* BTA_DM_API_EXT_ADV_SET_CLEAR_EVT */
189     bta_dm_ble_gap_periodic_adv_set_params, /* BTA_DM_API_PERIODIC_ADV_SET_PARAMS_EVT */
190     bta_dm_ble_gap_periodic_adv_cfg_data_raw, /* BTA_DM_API_PERIODIC_ADV_CFG_DATA_EVT */
191     bta_dm_ble_gap_periodic_adv_enable,     /* BTA_DM_API_PERIODIC_ADV_ENABLE_EVT */
192     bta_dm_ble_gap_periodic_adv_create_sync, /* BTA_DM_API_PERIODIC_ADV_SYNC_EVT */
193     bta_dm_ble_gap_periodic_adv_sync_cancel, /* BTA_DM_API_PERIODIC_ADV_SYNC_CANCEL_EVT */
194     bta_dm_ble_gap_periodic_adv_sync_terminate, /* BTA_DM_API_PERIODIC_ADV_SYNC_TERMINATE_EVT */
195     bta_dm_ble_gap_periodic_adv_add_dev_to_list, /* BTA_DM_API_PERIODIC_ADV_ADD_DEV_TO_LSIT_EVT */
196     bta_dm_ble_gap_periodic_adv_remove_dev_from_list, /* BTA_DM_API_PERIODIC_ADV_REMOVE_DEV_FROM_LSIT_EVT */
197     bta_dm_ble_gap_periodic_adv_clear_dev,  /* BTA_DM_API_PERIODIC_ADV_CLEAR_DEV_EVT */
198     bta_dm_ble_gap_set_ext_scan_params,     /* BTA_DM_API_SET_EXT_SCAN_PARAMS_EVT */
199     bta_dm_ble_gap_ext_scan,                /* BTA_DM_API_START_EXT_SCAN_EVT */
200     bta_dm_ble_gap_set_prefer_ext_conn_params, /* BTA_DM_API_SET_PERF_EXT_CONN_PARAMS_EVT */
201 #endif // #if (BLE_50_FEATURE_SUPPORT == TRUE)
202 };
203 
204 
205 
206 /* state machine action enumeration list */
207 enum {
208     BTA_DM_API_SEARCH,                  /* 0 bta_dm_search_start */
209     BTA_DM_API_SEARCH_CANCEL,           /* 1 bta_dm_search_cancel */
210 #if (SDP_INCLUDED == TRUE)
211     BTA_DM_API_DISCOVER,                /* 2 bta_dm_discover */
212 #endif  ///SDP_INCLUDED == TRUE
213     BTA_DM_INQUIRY_CMPL,                /* 3 bta_dm_inq_cmpl */
214     BTA_DM_REMT_NAME,                   /* 4 bta_dm_rmt_name */
215 #if (SDP_INCLUDED == TRUE)
216     BTA_DM_SDP_RESULT,                  /* 5 bta_dm_sdp_result */
217 #endif  ///SDP_INCLUDED == TRUE
218     BTA_DM_SEARCH_CMPL,                 /* 6 bta_dm_search_cmpl*/
219 #if (SDP_INCLUDED == TRUE)
220     BTA_DM_FREE_SDP_DB,                 /* 7 bta_dm_free_sdp_db */
221 #endif  ///SDP_INCLUDED == TRUE
222     BTA_DM_DISC_RESULT,                 /* 8 bta_dm_disc_result */
223     BTA_DM_SEARCH_RESULT,               /* 9 bta_dm_search_result */
224     BTA_DM_QUEUE_SEARCH,                /* 10 bta_dm_queue_search */
225 #if (SDP_INCLUDED == TRUE)
226     BTA_DM_QUEUE_DISC,                  /* 11 bta_dm_queue_disc */
227 #endif  ///SDP_INCLUDED == TRUE
228     BTA_DM_SEARCH_CLEAR_QUEUE,          /* 12 bta_dm_search_clear_queue */
229     BTA_DM_SEARCH_CANCEL_CMPL,          /* 13 bta_dm_search_cancel_cmpl */
230     BTA_DM_SEARCH_CANCEL_NOTIFY,        /* 14 bta_dm_search_cancel_notify */
231 #if (SDP_INCLUDED == TRUE)
232     BTA_DM_SEARCH_CANCEL_TRANSAC_CMPL,  /* 15 bta_dm_search_cancel_transac_cmpl */
233 #endif  ///SDP_INCLUDED == TRUE
234     BTA_DM_DISC_RMT_NAME,               /* 16 bta_dm_disc_rmt_name */
235 #if (SDP_INCLUDED == TRUE)
236     BTA_DM_API_DI_DISCOVER,             /* 17 bta_dm_di_disc */
237 #endif  ///SDP_INCLUDED == TRUE
238 #if BLE_INCLUDED == TRUE && SDP_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE && GATTC_INCLUDED == TRUE
239     BTA_DM_CLOSE_GATT_CONN,             /* 18 bta_dm_close_gatt_conn */
240 #endif /* BLE_INCLUDED == TRUE && SDP_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE && GATTC_INCLUDED == TRUE */
241     BTA_DM_SEARCH_NUM_ACTIONS           /* 19 */
242 };
243 
244 
245 /* action function list */
246 const tBTA_DM_ACTION bta_dm_search_action[] = {
247 
248     bta_dm_search_start,                /* 0 BTA_DM_API_SEARCH */
249     bta_dm_search_cancel,               /* 1 BTA_DM_API_SEARCH_CANCEL */
250 #if (SDP_INCLUDED == TRUE)
251     bta_dm_discover,                    /* 2 BTA_DM_API_DISCOVER */
252 #endif  ///SDP_INCLUDED == TRUE
253     bta_dm_inq_cmpl,                    /* 3 BTA_DM_INQUIRY_CMPL */
254     bta_dm_rmt_name,                    /* 4 BTA_DM_REMT_NAME */
255 #if (SDP_INCLUDED == TRUE)
256     bta_dm_sdp_result,                  /* 5 BTA_DM_SDP_RESULT */
257 #endif  ///SDP_INCLUDED == TRUE
258     bta_dm_search_cmpl,                 /* 6 BTA_DM_SEARCH_CMPL */
259 #if (SDP_INCLUDED == TRUE)
260     bta_dm_free_sdp_db,                 /* 7 BTA_DM_FREE_SDP_DB */
261 #endif  ///SDP_INCLUDED == TRUE
262     bta_dm_disc_result,                 /* 8 BTA_DM_DISC_RESULT */
263     bta_dm_search_result,               /* 9 BTA_DM_SEARCH_RESULT */
264     bta_dm_queue_search,                /* 10 BTA_DM_QUEUE_SEARCH */
265 #if (SDP_INCLUDED == TRUE)
266     bta_dm_queue_disc,                  /* 11 BTA_DM_QUEUE_DISC */
267 #endif  ///SDP_INCLUDED == TRUE
268     bta_dm_search_clear_queue,          /* 12 BTA_DM_SEARCH_CLEAR_QUEUE */
269     bta_dm_search_cancel_cmpl,          /* 13 BTA_DM_SEARCH_CANCEL_CMPL */
270     bta_dm_search_cancel_notify,        /* 14 BTA_DM_SEARCH_CANCEL_NOTIFY */
271 #if (SDP_INCLUDED == TRUE)
272     bta_dm_search_cancel_transac_cmpl,  /* 15 BTA_DM_SEARCH_CANCEL_TRANSAC_CMPL */
273 #endif  ///SDP_INCLUDED == TRUE
274     bta_dm_disc_rmt_name,               /* 16 BTA_DM_DISC_RMT_NAME */
275 #if (SDP_INCLUDED == TRUE)
276     bta_dm_di_disc                      /* 17 BTA_DM_API_DI_DISCOVER */
277 #endif  ///SDP_INCLUDED == TRUE
278 #if BLE_INCLUDED == TRUE && SDP_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE && GATTC_INCLUDED == TRUE
279     , bta_dm_close_gatt_conn
280 #endif
281 };
282 
283 #define BTA_DM_SEARCH_IGNORE       BTA_DM_SEARCH_NUM_ACTIONS
284 /* state table information */
285 #define BTA_DM_SEARCH_ACTIONS              2       /* number of actions */
286 #define BTA_DM_SEARCH_NEXT_STATE           2       /* position of next state */
287 #define BTA_DM_SEARCH_NUM_COLS             3       /* number of columns in state tables */
288 
289 
290 
291 /* state table for listen state */
292 const UINT8 bta_dm_search_idle_st_table[][BTA_DM_SEARCH_NUM_COLS] = {
293 
294     /* Event                        Action 1                            Action 2                    Next State */
295     /* API_SEARCH */            {BTA_DM_API_SEARCH,                BTA_DM_SEARCH_IGNORE,          BTA_DM_SEARCH_ACTIVE},
296     /* API_SEARCH_CANCEL */     {BTA_DM_SEARCH_CANCEL_NOTIFY,      BTA_DM_SEARCH_IGNORE,          BTA_DM_SEARCH_IDLE},
297 #if (SDP_INCLUDED == TRUE)
298     /* API_SEARCH_DISC */       {BTA_DM_API_DISCOVER,              BTA_DM_SEARCH_IGNORE,          BTA_DM_DISCOVER_ACTIVE},
299 #endif  ///SDP_INCLUDED == TRUE
300     /* INQUIRY_CMPL */          {BTA_DM_SEARCH_IGNORE,             BTA_DM_SEARCH_IGNORE,          BTA_DM_SEARCH_IDLE},
301     /* REMT_NAME_EVT */         {BTA_DM_SEARCH_IGNORE,             BTA_DM_SEARCH_IGNORE,          BTA_DM_SEARCH_IDLE},
302 #if (SDP_INCLUDED == TRUE)
303     /* SDP_RESULT_EVT */        {BTA_DM_FREE_SDP_DB,               BTA_DM_SEARCH_IGNORE,          BTA_DM_SEARCH_IDLE},
304 #endif  ///SDP_INCLUDED == TRUE
305     /* SEARCH_CMPL_EVT */       {BTA_DM_SEARCH_IGNORE,             BTA_DM_SEARCH_IGNORE,          BTA_DM_SEARCH_IDLE},
306     /* DISCV_RES_EVT */         {BTA_DM_SEARCH_IGNORE,             BTA_DM_SEARCH_IGNORE,          BTA_DM_SEARCH_IDLE},
307 #if (SDP_INCLUDED == TRUE)
308     /* API_DI_DISCOVER_EVT */   {BTA_DM_API_DI_DISCOVER,           BTA_DM_SEARCH_IGNORE,          BTA_DM_SEARCH_ACTIVE},
309 #endif  ///SDP_INCLUDED == TRUE
310 #if BLE_INCLUDED == TRUE && SDP_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE && GATTC_INCLUDED == TRUE
311     // #if BLE_INCLUDED == TRUE
312     /* DISC_CLOSE_TOUT_EVT */   {BTA_DM_CLOSE_GATT_CONN,           BTA_DM_SEARCH_IGNORE,          BTA_DM_SEARCH_IDLE},
313 #endif
314 };
315 const UINT8 bta_dm_search_search_active_st_table[][BTA_DM_SEARCH_NUM_COLS] = {
316 
317     /* Event                        Action 1                            Action 2                    Next State */
318     /* API_SEARCH */            {BTA_DM_SEARCH_IGNORE,             BTA_DM_SEARCH_IGNORE,          BTA_DM_SEARCH_ACTIVE},
319     /* API_SEARCH_CANCEL */     {BTA_DM_API_SEARCH_CANCEL,         BTA_DM_SEARCH_IGNORE,          BTA_DM_SEARCH_CANCELLING},
320     /* API_SEARCH_DISC */       {BTA_DM_SEARCH_IGNORE,             BTA_DM_SEARCH_IGNORE,          BTA_DM_SEARCH_ACTIVE},
321     /* INQUIRY_CMPL */          {BTA_DM_INQUIRY_CMPL,              BTA_DM_SEARCH_IGNORE,          BTA_DM_SEARCH_ACTIVE},
322     /* REMT_NAME_EVT */         {BTA_DM_REMT_NAME,                 BTA_DM_SEARCH_IGNORE,          BTA_DM_SEARCH_ACTIVE},
323 #if (SDP_INCLUDED == TRUE)
324     /* SDP_RESULT_EVT */        {BTA_DM_SDP_RESULT,                BTA_DM_SEARCH_IGNORE,          BTA_DM_SEARCH_ACTIVE},
325 #endif  ///SDP_INCLUDED == TRUE
326     /* SEARCH_CMPL_EVT */       {BTA_DM_SEARCH_CMPL,               BTA_DM_SEARCH_IGNORE,          BTA_DM_SEARCH_IDLE},
327     /* DISCV_RES_EVT */         {BTA_DM_SEARCH_RESULT,             BTA_DM_SEARCH_IGNORE,          BTA_DM_SEARCH_ACTIVE},
328     /* API_DI_DISCOVER_EVT */   {BTA_DM_SEARCH_IGNORE,             BTA_DM_SEARCH_IGNORE,          BTA_DM_SEARCH_ACTIVE}
329 #if BLE_INCLUDED == TRUE && SDP_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE && GATTC_INCLUDED == TRUE
330     // #if BLE_INCLUDED == TRUE
331     /* DISC_CLOSE_TOUT_EVT */   , {BTA_DM_CLOSE_GATT_CONN,          BTA_DM_SEARCH_IGNORE,          BTA_DM_SEARCH_ACTIVE}
332 #endif
333 
334 };
335 
336 const UINT8 bta_dm_search_search_cancelling_st_table[][BTA_DM_SEARCH_NUM_COLS] = {
337 
338     /* Event                        Action 1                            Action 2                    Next State */
339     /* API_SEARCH */            {BTA_DM_QUEUE_SEARCH,               BTA_DM_SEARCH_IGNORE,          BTA_DM_SEARCH_CANCELLING},
340     /* API_SEARCH_CANCEL */     {BTA_DM_SEARCH_CLEAR_QUEUE,         BTA_DM_SEARCH_CANCEL_NOTIFY,   BTA_DM_SEARCH_CANCELLING},
341 #if (SDP_INCLUDED == TRUE)
342     /* API_SEARCH_DISC */       {BTA_DM_QUEUE_DISC,                 BTA_DM_SEARCH_IGNORE,          BTA_DM_SEARCH_CANCELLING},
343 #endif  ///SDP_INCLUDED == TRUE
344     /* INQUIRY_CMPL */          {BTA_DM_SEARCH_CANCEL_CMPL,         BTA_DM_SEARCH_IGNORE,          BTA_DM_SEARCH_IDLE},
345 #if (SDP_INCLUDED == TRUE)
346     /* REMT_NAME_EVT */         {BTA_DM_SEARCH_CANCEL_TRANSAC_CMPL, BTA_DM_SEARCH_CANCEL_CMPL,     BTA_DM_SEARCH_IDLE},
347     /* SDP_RESULT_EVT */        {BTA_DM_SEARCH_CANCEL_TRANSAC_CMPL, BTA_DM_SEARCH_CANCEL_CMPL,     BTA_DM_SEARCH_IDLE},
348     /* SEARCH_CMPL_EVT */       {BTA_DM_SEARCH_CANCEL_TRANSAC_CMPL, BTA_DM_SEARCH_CANCEL_CMPL,     BTA_DM_SEARCH_IDLE},
349     /* DISCV_RES_EVT */         {BTA_DM_SEARCH_CANCEL_TRANSAC_CMPL, BTA_DM_SEARCH_CANCEL_CMPL,     BTA_DM_SEARCH_IDLE},
350 #endif  ///SDP_INCLUDED == TRUE
351     /* API_DI_DISCOVER_EVT */   {BTA_DM_SEARCH_IGNORE,              BTA_DM_SEARCH_IGNORE,          BTA_DM_SEARCH_CANCELLING}
352 #if BLE_INCLUDED == TRUE
353     /* DISC_CLOSE_TOUT_EVT */   , {BTA_DM_SEARCH_IGNORE,              BTA_DM_SEARCH_IGNORE,          BTA_DM_SEARCH_CANCELLING}
354 #endif
355 
356 };
357 
358 const UINT8 bta_dm_search_disc_active_st_table[][BTA_DM_SEARCH_NUM_COLS] = {
359 
360     /* Event                        Action 1                            Action 2                    Next State */
361     /* API_SEARCH */            {BTA_DM_SEARCH_IGNORE,             BTA_DM_SEARCH_IGNORE,          BTA_DM_DISCOVER_ACTIVE},
362     /* API_SEARCH_CANCEL */     {BTA_DM_SEARCH_CANCEL_NOTIFY,      BTA_DM_SEARCH_IGNORE,          BTA_DM_SEARCH_CANCELLING},
363     /* API_SEARCH_DISC */       {BTA_DM_SEARCH_IGNORE,             BTA_DM_SEARCH_IGNORE,          BTA_DM_DISCOVER_ACTIVE},
364     /* INQUIRY_CMPL */          {BTA_DM_SEARCH_IGNORE,             BTA_DM_SEARCH_IGNORE,          BTA_DM_DISCOVER_ACTIVE},
365     /* REMT_NAME_EVT */         {BTA_DM_DISC_RMT_NAME,             BTA_DM_SEARCH_IGNORE,          BTA_DM_DISCOVER_ACTIVE},
366 #if (SDP_INCLUDED == TRUE)
367     /* SDP_RESULT_EVT */        {BTA_DM_SDP_RESULT,                BTA_DM_SEARCH_IGNORE,          BTA_DM_DISCOVER_ACTIVE},
368 #endif  ///SDP_INCLUDED == TRUE
369     /* SEARCH_CMPL_EVT */       {BTA_DM_SEARCH_CMPL,               BTA_DM_SEARCH_IGNORE,          BTA_DM_SEARCH_IDLE},
370     /* DISCV_RES_EVT */         {BTA_DM_DISC_RESULT,               BTA_DM_SEARCH_IGNORE,          BTA_DM_DISCOVER_ACTIVE},
371     /* API_DI_DISCOVER_EVT */   {BTA_DM_SEARCH_IGNORE,             BTA_DM_SEARCH_IGNORE,          BTA_DM_DISCOVER_ACTIVE}
372 
373 #if BLE_INCLUDED == TRUE
374     /* DISC_CLOSE_TOUT_EVT */   , {BTA_DM_SEARCH_IGNORE,             BTA_DM_SEARCH_IGNORE,          BTA_DM_DISCOVER_ACTIVE}
375 #endif
376 };
377 
378 typedef const UINT8 (*tBTA_DM_ST_TBL)[BTA_DM_SEARCH_NUM_COLS];
379 
380 /* state table */
381 const tBTA_DM_ST_TBL bta_dm_search_st_tbl[] = {
382     bta_dm_search_idle_st_table,
383     bta_dm_search_search_active_st_table,
384     bta_dm_search_search_cancelling_st_table,
385     bta_dm_search_disc_active_st_table
386 };
387 
388 
389 /*******************************************************************************
390 **
391 ** Function         bta_dm_sm_disable
392 **
393 ** Description     unregister BTA DM
394 **
395 **
396 ** Returns          void
397 **
398 *******************************************************************************/
bta_dm_sm_disable(void)399 void bta_dm_sm_disable(void)
400 {
401     bta_sys_deregister( BTA_ID_DM );
402 }
403 
bta_dm_sm_deinit(void)404 void bta_dm_sm_deinit(void)
405 {
406     memset(&bta_dm_cb, 0, sizeof(tBTA_DM_CB));
407     memset(&bta_dm_search_cb, 0, sizeof(tBTA_DM_SEARCH_CB));
408     memset(&bta_dm_di_cb, 0, sizeof(tBTA_DM_DI_CB));
409 #if BTA_DYNAMIC_MEMORY
410     FREE_AND_RESET(bta_dm_cb_ptr);
411     FREE_AND_RESET(bta_dm_search_cb_ptr);
412     FREE_AND_RESET(bta_dm_di_cb_ptr);
413 #endif /* #if BTA_DYNAMIC_MEMORY */
414 }
415 
416 
417 /*******************************************************************************
418 **
419 ** Function         bta_dm_sm_execute
420 **
421 ** Description      State machine event handling function for DM
422 **
423 **
424 ** Returns          void
425 **
426 *******************************************************************************/
bta_dm_sm_execute(BT_HDR * p_msg)427 BOOLEAN bta_dm_sm_execute(BT_HDR *p_msg)
428 {
429     UINT16  event = p_msg->event & 0x00ff;
430 
431     APPL_TRACE_EVENT("bta_dm_sm_execute event:0x%x", event);
432 
433     /* execute action functions */
434     if (event < BTA_DM_NUM_ACTIONS) {
435         (*bta_dm_action[event])( (tBTA_DM_MSG *) p_msg);
436     }
437 
438     return TRUE;
439 }
440 
BTA_DmCoexEventTrigger(uint32_t event)441 void BTA_DmCoexEventTrigger(uint32_t event)
442 {
443     switch(event) {
444     case BTA_COEX_EVT_SCAN_STARTED:
445     case BTA_COEX_EVT_SCAN_STOPPED:
446     case BTA_COEX_EVT_SNIFF_ENTER:
447     case BTA_COEX_EVT_SNIFF_EXIT:
448     case BTA_COEX_EVT_A2DP_PAUSED_ENTER:
449     case BTA_COEX_EVT_A2DP_PAUSED_EXIT:
450     case BTA_COEX_EVT_ACL_CONNECTED:
451     case BTA_COEX_EVT_ACL_DISCONNECTED:
452         break;
453     case BTA_COEX_EVT_STREAMING_STARTED:
454         esp_coex_status_bit_set(ESP_COEX_ST_TYPE_BT, ESP_COEX_BT_ST_A2DP_STREAMING);
455         esp_coex_status_bit_clear(ESP_COEX_ST_TYPE_BT, ESP_COEX_BT_ST_A2DP_PAUSED);
456         break;
457     case BTA_COEX_EVT_STREAMING_STOPPED:
458         esp_coex_status_bit_clear(ESP_COEX_ST_TYPE_BT, ESP_COEX_BT_ST_A2DP_STREAMING);
459         esp_coex_status_bit_clear(ESP_COEX_ST_TYPE_BT, ESP_COEX_BT_ST_A2DP_PAUSED);
460         break;
461     default:
462         break;
463     }
464 }
465 
466 /*******************************************************************************
467 **
468 ** Function         bta_dm_sm_search_disable
469 **
470 ** Description     unregister BTA SEARCH DM
471 **
472 **
473 ** Returns          void
474 **
475 *******************************************************************************/
bta_dm_search_sm_disable(void)476 void bta_dm_search_sm_disable(void)
477 {
478     bta_sys_deregister( BTA_ID_DM_SEARCH );
479 
480 }
481 
482 
483 /*******************************************************************************
484 **
485 ** Function         bta_dm_search_sm_execute
486 **
487 ** Description      State machine event handling function for DM
488 **
489 **
490 ** Returns          void
491 **
492 *******************************************************************************/
bta_dm_search_sm_execute(BT_HDR * p_msg)493 BOOLEAN bta_dm_search_sm_execute(BT_HDR *p_msg)
494 {
495     tBTA_DM_ST_TBL      state_table;
496     UINT8               action;
497     int                 i;
498 
499     APPL_TRACE_EVENT("bta_dm_search_sm_execute state:%d, event:0x%x",
500                      bta_dm_search_cb.state, p_msg->event);
501 
502     /* look up the state table for the current state */
503     state_table = bta_dm_search_st_tbl[bta_dm_search_cb.state];
504 
505     bta_dm_search_cb.state = state_table[p_msg->event & 0x00ff][BTA_DM_SEARCH_NEXT_STATE];
506 
507 
508     /* execute action functions */
509     for (i = 0; i < BTA_DM_SEARCH_ACTIONS; i++) {
510         if ((action = state_table[p_msg->event & 0x00ff][i]) != BTA_DM_SEARCH_IGNORE) {
511             (*bta_dm_search_action[action])( (tBTA_DM_MSG *) p_msg);
512         } else {
513             break;
514         }
515     }
516     return TRUE;
517 }
518