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_api.h" 26 #include "bta_sys.h" 27 #include "bta_dm_int.h" 28 29 30 /***************************************************************************** 31 ** Constants and types 32 *****************************************************************************/ 33 34 #if BTA_DYNAMIC_MEMORY == FALSE 35 tBTA_DM_CB bta_dm_cb; 36 tBTA_DM_SEARCH_CB bta_dm_search_cb; 37 tBTA_DM_DI_CB bta_dm_di_cb; 38 #endif 39 40 41 #define BTA_DM_NUM_ACTIONS (BTA_DM_MAX_EVT & 0x00ff) 42 43 /* type for action functions */ 44 typedef void (*tBTA_DM_ACTION)(tBTA_DM_MSG *p_data); 45 46 /* action function list */ 47 const tBTA_DM_ACTION bta_dm_action[] = 48 { 49 50 /* device manager local device API events */ 51 bta_dm_enable, /* 0 BTA_DM_API_ENABLE_EVT */ 52 bta_dm_disable, /* 1 BTA_DM_API_DISABLE_EVT */ 53 bta_dm_set_dev_name, /* 2 BTA_DM_API_SET_NAME_EVT */ 54 bta_dm_set_visibility, /* 3 BTA_DM_API_SET_VISIBILITY_EVT */ 55 bta_dm_set_afhchannels, /* 4 BTA_DM_API_SET_AFH_CHANNELS_EVT */ 56 bta_dm_signal_strength, /* 5 BTA_API_DM_SIG_STRENGTH_EVT */ 57 bta_dm_vendor_spec_command,/* 6 BTA_DM_API_VENDOR_SPECIFIC_COMMAND_EVT */ 58 bta_dm_tx_inqpower, /* 7 BTA_DM_API_SIG_STRENGTH_EVT */ 59 bta_dm_acl_change, /* 8 BTA_DM_ACL_CHANGE_EVT */ 60 bta_dm_add_device, /* 9 BTA_DM_API_ADD_DEVICE_EVT */ 61 bta_dm_close_acl, /* 10 BTA_DM_API_ADD_DEVICE_EVT */ 62 63 /* security API events */ 64 bta_dm_bond, /* 11 BTA_DM_API_BOND_EVT */ 65 bta_dm_bond_cancel, /* 12 BTA_DM_API_BOND_CANCEL_EVT */ 66 bta_dm_pin_reply, /* 13 BTA_DM_API_PIN_REPLY_EVT */ 67 bta_dm_link_policy, /* 14 BTA_DM_API_LINK_POLICY_EVT */ 68 bta_dm_auth_reply, /* 15 BTA_DM_API_AUTH_REPLY_EVT */ 69 70 /* power manger events */ 71 bta_dm_pm_btm_status, /* 16 BTA_DM_PM_BTM_STATUS_EVT */ 72 bta_dm_pm_timer, /* 17 BTA_DM_PM_TIMER_EVT*/ 73 74 /* simple pairing events */ 75 bta_dm_confirm, /* 18 BTA_DM_API_CONFIRM_EVT */ 76 77 bta_dm_set_encryption, /* BTA_DM_API_SET_ENCRYPTION_EVT */ 78 79 #if (BTM_LOCAL_IO_CAPS != BTM_IO_CAP_NONE) 80 bta_dm_passkey_cancel, /* 19 BTA_DM_API_PASKY_CANCEL_EVT */ 81 #endif 82 #if (BTM_OOB_INCLUDED == TRUE) 83 bta_dm_loc_oob, /* 20 BTA_DM_API_LOC_OOB_EVT */ 84 bta_dm_ci_io_req_act, /* 21 BTA_DM_CI_IO_REQ_EVT */ 85 bta_dm_ci_rmt_oob_act, /* 22 BTA_DM_CI_RMT_OOB_EVT */ 86 #endif /* BTM_OOB_INCLUDED */ 87 88 bta_dm_remove_device, /* BTA_DM_API_REMOVE_DEVICE_EVT */ 89 90 #if BLE_INCLUDED == TRUE 91 bta_dm_add_blekey, /* BTA_DM_API_ADD_BLEKEY_EVT */ 92 bta_dm_add_ble_device, /* BTA_DM_API_ADD_BLEDEVICE_EVT */ 93 bta_dm_ble_passkey_reply, /* BTA_DM_API_BLE_PASSKEY_REPLY_EVT */ 94 bta_dm_security_grant, 95 bta_dm_ble_set_bg_conn_type, 96 bta_dm_ble_set_conn_params, /* BTA_DM_API_BLE_CONN_PARAM_EVT */ 97 bta_dm_ble_set_scan_params, /* BTA_DM_API_BLE_SCAN_PARAM_EVT */ 98 bta_dm_ble_observe, 99 bta_dm_ble_set_adv_params, /* BTA_DM_API_BLE_SCAN_PARAM_EVT */ 100 bta_dm_ble_set_adv_config, /* BTA_DM_API_BLE_SET_ADV_CONFIG_EVT */ 101 #endif 102 103 #if ( BTM_EIR_SERVER_INCLUDED == TRUE )&&( BTA_EIR_CANNED_UUID_LIST != TRUE )&&(BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0) 104 bta_dm_update_eir_uuid, /* BTA_DM_API_UPDATE_EIR_UUID_EVT */ 105 #endif 106 #if (BTM_EIR_SERVER_INCLUDED == TRUE) 107 bta_dm_set_eir_config, /* BTA_DM_API_SET_EIR_CONFIG_EVT */ 108 #endif 109 110 bta_dm_enable_test_mode, /* BTA_DM_API_ENABLE_TEST_MODE_EVT */ 111 bta_dm_disable_test_mode, /* BTA_DM_API_DISABLE_TEST_MODE_EVT */ 112 bta_dm_execute_callback, /* BTA_DM_API_EXECUTE_CBACK_EVT */ 113 bta_dm_set_afh_channel_assesment /* BTA_DM_API_SET_AFH_CHANNEL_ASSESMENT_EVT */ 114 }; 115 116 117 118 /* state machine action enumeration list */ 119 enum 120 { 121 BTA_DM_API_SEARCH, /* 0 bta_dm_search_start */ 122 BTA_DM_API_SEARCH_CANCEL, /* 1 bta_dm_search_cancel */ 123 BTA_DM_API_DISCOVER, /* 2 bta_dm_discover */ 124 BTA_DM_INQUIRY_CMPL, /* 3 bta_dm_inq_cmpl */ 125 BTA_DM_REMT_NAME, /* 4 bta_dm_rmt_name */ 126 BTA_DM_SDP_RESULT, /* 5 bta_dm_sdp_result */ 127 BTA_DM_SEARCH_CMPL, /* 6 bta_dm_search_cmpl*/ 128 BTA_DM_FREE_SDP_DB, /* 7 bta_dm_free_sdp_db */ 129 BTA_DM_DISC_RESULT, /* 8 bta_dm_disc_result */ 130 BTA_DM_SEARCH_RESULT, /* 9 bta_dm_search_result */ 131 BTA_DM_QUEUE_SEARCH, /* 10 bta_dm_queue_search */ 132 BTA_DM_QUEUE_DISC, /* 11 bta_dm_queue_disc */ 133 BTA_DM_SEARCH_CLEAR_QUEUE, /* 12 bta_dm_search_clear_queue */ 134 BTA_DM_SEARCH_CANCEL_CMPL, /* 13 bta_dm_search_cancel_cmpl */ 135 BTA_DM_SEARCH_CANCEL_NOTIFY, /* 14 bta_dm_search_cancel_notify */ 136 BTA_DM_SEARCH_CANCEL_TRANSAC_CMPL, /* 15 bta_dm_search_cancel_transac_cmpl */ 137 BTA_DM_DISC_RMT_NAME, /* 16 bta_dm_disc_rmt_name */ 138 BTA_DM_API_DI_DISCOVER, /* 17 bta_dm_di_disc */ 139 #if BLE_INCLUDED == TRUE 140 BTA_DM_CLOSE_GATT_CONN, /* 18 bta_dm_close_gatt_conn */ 141 #endif 142 BTA_DM_SEARCH_NUM_ACTIONS /* 19 */ 143 }; 144 145 146 /* action function list */ 147 const tBTA_DM_ACTION bta_dm_search_action[] = 148 { 149 150 bta_dm_search_start, /* 0 BTA_DM_API_SEARCH */ 151 bta_dm_search_cancel, /* 1 BTA_DM_API_SEARCH_CANCEL */ 152 bta_dm_discover, /* 2 BTA_DM_API_DISCOVER */ 153 bta_dm_inq_cmpl, /* 3 BTA_DM_INQUIRY_CMPL */ 154 bta_dm_rmt_name, /* 4 BTA_DM_REMT_NAME */ 155 bta_dm_sdp_result, /* 5 BTA_DM_SDP_RESULT */ 156 bta_dm_search_cmpl, /* 6 BTA_DM_SEARCH_CMPL */ 157 bta_dm_free_sdp_db, /* 7 BTA_DM_FREE_SDP_DB */ 158 bta_dm_disc_result, /* 8 BTA_DM_DISC_RESULT */ 159 bta_dm_search_result, /* 9 BTA_DM_SEARCH_RESULT */ 160 bta_dm_queue_search, /* 10 BTA_DM_QUEUE_SEARCH */ 161 bta_dm_queue_disc, /* 11 BTA_DM_QUEUE_DISC */ 162 bta_dm_search_clear_queue, /* 12 BTA_DM_SEARCH_CLEAR_QUEUE */ 163 bta_dm_search_cancel_cmpl, /* 13 BTA_DM_SEARCH_CANCEL_CMPL */ 164 bta_dm_search_cancel_notify, /* 14 BTA_DM_SEARCH_CANCEL_NOTIFY */ 165 bta_dm_search_cancel_transac_cmpl, /* 15 BTA_DM_SEARCH_CANCEL_TRANSAC_CMPL */ 166 bta_dm_disc_rmt_name, /* 16 BTA_DM_DISC_RMT_NAME */ 167 bta_dm_di_disc /* 17 BTA_DM_API_DI_DISCOVER */ 168 #if BLE_INCLUDED == TRUE 169 ,bta_dm_close_gatt_conn 170 #endif 171 }; 172 173 #define BTA_DM_SEARCH_IGNORE BTA_DM_SEARCH_NUM_ACTIONS 174 /* state table information */ 175 #define BTA_DM_SEARCH_ACTIONS 2 /* number of actions */ 176 #define BTA_DM_SEARCH_NEXT_STATE 2 /* position of next state */ 177 #define BTA_DM_SEARCH_NUM_COLS 3 /* number of columns in state tables */ 178 179 180 181 /* state table for listen state */ 182 const UINT8 bta_dm_search_idle_st_table[][BTA_DM_SEARCH_NUM_COLS] = 183 { 184 185 /* Event Action 1 Action 2 Next State */ 186 /* API_SEARCH */ {BTA_DM_API_SEARCH, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_ACTIVE}, 187 /* API_SEARCH_CANCEL */ {BTA_DM_SEARCH_CANCEL_NOTIFY, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IDLE}, 188 /* API_SEARCH_DISC */ {BTA_DM_API_DISCOVER, BTA_DM_SEARCH_IGNORE, BTA_DM_DISCOVER_ACTIVE}, 189 /* INQUIRY_CMPL */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IDLE}, 190 /* REMT_NAME_EVT */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IDLE}, 191 /* SDP_RESULT_EVT */ {BTA_DM_FREE_SDP_DB, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IDLE}, 192 /* SEARCH_CMPL_EVT */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IDLE}, 193 /* DISCV_RES_EVT */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IDLE}, 194 /* API_DI_DISCOVER_EVT */ {BTA_DM_API_DI_DISCOVER, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_ACTIVE} 195 #if BLE_INCLUDED == TRUE 196 /* DISC_CLOSE_TOUT_EVT */ ,{BTA_DM_CLOSE_GATT_CONN, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IDLE} 197 #endif 198 199 }; 200 const UINT8 bta_dm_search_search_active_st_table[][BTA_DM_SEARCH_NUM_COLS] = 201 { 202 203 /* Event Action 1 Action 2 Next State */ 204 /* API_SEARCH */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_ACTIVE}, 205 /* API_SEARCH_CANCEL */ {BTA_DM_API_SEARCH_CANCEL, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_CANCELLING}, 206 /* API_SEARCH_DISC */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_ACTIVE}, 207 /* INQUIRY_CMPL */ {BTA_DM_INQUIRY_CMPL, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_ACTIVE}, 208 /* REMT_NAME_EVT */ {BTA_DM_REMT_NAME, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_ACTIVE}, 209 /* SDP_RESULT_EVT */ {BTA_DM_SDP_RESULT, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_ACTIVE}, 210 /* SEARCH_CMPL_EVT */ {BTA_DM_SEARCH_CMPL, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IDLE}, 211 /* DISCV_RES_EVT */ {BTA_DM_SEARCH_RESULT, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_ACTIVE}, 212 /* API_DI_DISCOVER_EVT */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_ACTIVE} 213 214 #if BLE_INCLUDED == TRUE 215 /* DISC_CLOSE_TOUT_EVT */ ,{BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_ACTIVE} 216 #endif 217 218 }; 219 220 const UINT8 bta_dm_search_search_cancelling_st_table[][BTA_DM_SEARCH_NUM_COLS] = 221 { 222 223 /* Event Action 1 Action 2 Next State */ 224 /* API_SEARCH */ {BTA_DM_QUEUE_SEARCH, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_CANCELLING}, 225 /* API_SEARCH_CANCEL */ {BTA_DM_SEARCH_CLEAR_QUEUE, BTA_DM_SEARCH_CANCEL_NOTIFY, BTA_DM_SEARCH_CANCELLING}, 226 /* API_SEARCH_DISC */ {BTA_DM_QUEUE_DISC, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_CANCELLING}, 227 /* INQUIRY_CMPL */ {BTA_DM_SEARCH_CANCEL_CMPL, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IDLE}, 228 /* REMT_NAME_EVT */ {BTA_DM_SEARCH_CANCEL_TRANSAC_CMPL, BTA_DM_SEARCH_CANCEL_CMPL, BTA_DM_SEARCH_IDLE}, 229 /* SDP_RESULT_EVT */ {BTA_DM_SEARCH_CANCEL_TRANSAC_CMPL, BTA_DM_SEARCH_CANCEL_CMPL, BTA_DM_SEARCH_IDLE}, 230 /* SEARCH_CMPL_EVT */ {BTA_DM_SEARCH_CANCEL_TRANSAC_CMPL, BTA_DM_SEARCH_CANCEL_CMPL, BTA_DM_SEARCH_IDLE}, 231 /* DISCV_RES_EVT */ {BTA_DM_SEARCH_CANCEL_TRANSAC_CMPL, BTA_DM_SEARCH_CANCEL_CMPL, BTA_DM_SEARCH_IDLE}, 232 /* API_DI_DISCOVER_EVT */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_CANCELLING} 233 234 #if BLE_INCLUDED == TRUE 235 /* DISC_CLOSE_TOUT_EVT */ ,{BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_CANCELLING} 236 #endif 237 238 }; 239 240 const UINT8 bta_dm_search_disc_active_st_table[][BTA_DM_SEARCH_NUM_COLS] = 241 { 242 243 /* Event Action 1 Action 2 Next State */ 244 /* API_SEARCH */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_DISCOVER_ACTIVE}, 245 /* API_SEARCH_CANCEL */ {BTA_DM_SEARCH_CANCEL_NOTIFY, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_CANCELLING}, 246 /* API_SEARCH_DISC */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_DISCOVER_ACTIVE}, 247 /* INQUIRY_CMPL */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_DISCOVER_ACTIVE}, 248 /* REMT_NAME_EVT */ {BTA_DM_DISC_RMT_NAME, BTA_DM_SEARCH_IGNORE, BTA_DM_DISCOVER_ACTIVE}, 249 /* SDP_RESULT_EVT */ {BTA_DM_SDP_RESULT, BTA_DM_SEARCH_IGNORE, BTA_DM_DISCOVER_ACTIVE}, 250 /* SEARCH_CMPL_EVT */ {BTA_DM_SEARCH_CMPL, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IDLE}, 251 /* DISCV_RES_EVT */ {BTA_DM_DISC_RESULT, BTA_DM_SEARCH_IGNORE, BTA_DM_DISCOVER_ACTIVE}, 252 /* API_DI_DISCOVER_EVT */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_DISCOVER_ACTIVE} 253 254 #if BLE_INCLUDED == TRUE 255 /* DISC_CLOSE_TOUT_EVT */ ,{BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_DISCOVER_ACTIVE} 256 #endif 257 258 }; 259 260 typedef const UINT8 (*tBTA_DM_ST_TBL)[BTA_DM_SEARCH_NUM_COLS]; 261 262 /* state table */ 263 const tBTA_DM_ST_TBL bta_dm_search_st_tbl[] = { 264 bta_dm_search_idle_st_table, 265 bta_dm_search_search_active_st_table, 266 bta_dm_search_search_cancelling_st_table, 267 bta_dm_search_disc_active_st_table 268 }; 269 270 271 /******************************************************************************* 272 ** 273 ** Function bta_dm_sm_disable 274 ** 275 ** Description unregister BTA DM 276 ** 277 ** 278 ** Returns void 279 ** 280 *******************************************************************************/ 281 void bta_dm_sm_disable( ) 282 { 283 bta_sys_deregister( BTA_ID_DM ); 284 } 285 286 287 /******************************************************************************* 288 ** 289 ** Function bta_dm_sm_execute 290 ** 291 ** Description State machine event handling function for DM 292 ** 293 ** 294 ** Returns void 295 ** 296 *******************************************************************************/ 297 BOOLEAN bta_dm_sm_execute(BT_HDR *p_msg) 298 { 299 UINT16 event = p_msg->event & 0x00ff; 300 301 APPL_TRACE_EVENT1("bta_dm_sm_execute event:0x%x", event); 302 303 /* execute action functions */ 304 if(event < BTA_DM_NUM_ACTIONS) 305 { 306 (*bta_dm_action[event])( (tBTA_DM_MSG*) p_msg); 307 } 308 309 return TRUE; 310 } 311 312 /******************************************************************************* 313 ** 314 ** Function bta_dm_sm_search_disable 315 ** 316 ** Description unregister BTA SEARCH DM 317 ** 318 ** 319 ** Returns void 320 ** 321 *******************************************************************************/ 322 void bta_dm_search_sm_disable( ) 323 { 324 bta_sys_deregister( BTA_ID_DM_SEARCH ); 325 326 } 327 328 329 /******************************************************************************* 330 ** 331 ** Function bta_dm_search_sm_execute 332 ** 333 ** Description State machine event handling function for DM 334 ** 335 ** 336 ** Returns void 337 ** 338 *******************************************************************************/ 339 BOOLEAN bta_dm_search_sm_execute(BT_HDR *p_msg) 340 { 341 tBTA_DM_ST_TBL state_table; 342 UINT8 action; 343 int i; 344 345 APPL_TRACE_EVENT2("bta_dm_search_sm_execute state:%d, event:0x%x", 346 bta_dm_search_cb.state, p_msg->event); 347 348 /* look up the state table for the current state */ 349 state_table = bta_dm_search_st_tbl[bta_dm_search_cb.state]; 350 351 bta_dm_search_cb.state = state_table[p_msg->event & 0x00ff][BTA_DM_SEARCH_NEXT_STATE]; 352 353 354 /* execute action functions */ 355 for (i = 0; i < BTA_DM_SEARCH_ACTIONS; i++) 356 { 357 if ((action = state_table[p_msg->event & 0x00ff][i]) != BTA_DM_SEARCH_IGNORE) 358 { 359 (*bta_dm_search_action[action])( (tBTA_DM_MSG*) p_msg); 360 } 361 else 362 { 363 break; 364 } 365 } 366 return TRUE; 367 } 368 369