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