bta_dm_main.c 23 KB

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