btm_devctl.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 1999-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 file contains functions that handle BTM interface functions for the
  21. * Bluetooth device including Rest, HCI buffer size and others
  22. *
  23. ******************************************************************************/
  24. #include <stdlib.h>
  25. #include <string.h>
  26. //#include <stdio.h>
  27. #include <stddef.h>
  28. #include "common/bt_trace.h"
  29. #include "stack/bt_types.h"
  30. //#include "bt_utils.h"
  31. #include "btm_int.h"
  32. #include "stack/btu.h"
  33. #include "device/controller.h"
  34. #include "hci/hci_layer.h"
  35. #include "stack/hcimsgs.h"
  36. #include "l2c_int.h"
  37. //#include "btcore/include/module.h"
  38. //#include "osi/include/osi/thread.h"
  39. #if BLE_INCLUDED == TRUE
  40. #include "gatt_int.h"
  41. #endif /* BLE_INCLUDED */
  42. //extern thread_t *bt_workqueue_thread;
  43. /********************************************************************************/
  44. /* L O C A L D A T A D E F I N I T I O N S */
  45. /********************************************************************************/
  46. #ifndef BTM_DEV_RESET_TIMEOUT
  47. #define BTM_DEV_RESET_TIMEOUT 4
  48. #endif
  49. #define BTM_DEV_REPLY_TIMEOUT 2 /* 1 second expiration time is not good. Timer may start between 0 and 1 second. */
  50. /* if it starts at the very end of the 0 second, timer will expire really easily. */
  51. #define BTM_INFO_TIMEOUT 5 /* 5 seconds for info response */
  52. /********************************************************************************/
  53. /* L O C A L F U N C T I O N P R O T O T Y P E S */
  54. /********************************************************************************/
  55. static void btm_decode_ext_features_page (UINT8 page_number, const BD_FEATURES p_features);
  56. /*******************************************************************************
  57. **
  58. ** Function btm_dev_init
  59. **
  60. ** Description This function is on the BTM startup
  61. **
  62. ** Returns void
  63. **
  64. *******************************************************************************/
  65. void btm_dev_init (void)
  66. {
  67. #if 0 /* cleared in btm_init; put back in if called from anywhere else! */
  68. memset (&btm_cb.devcb, 0, sizeof (tBTM_DEVCB));
  69. #endif
  70. /* Initialize nonzero defaults */
  71. #if (BTM_MAX_LOC_BD_NAME_LEN > 0)
  72. memset(btm_cb.cfg.bd_name, 0, sizeof(tBTM_LOC_BD_NAME));
  73. #endif
  74. btm_cb.devcb.reset_timer.param = (TIMER_PARAM_TYPE)TT_DEV_RESET;
  75. btm_cb.devcb.rln_timer.param = (TIMER_PARAM_TYPE)TT_DEV_RLN;
  76. btm_cb.btm_acl_pkt_types_supported = BTM_ACL_PKT_TYPES_MASK_DH1 + BTM_ACL_PKT_TYPES_MASK_DM1 +
  77. BTM_ACL_PKT_TYPES_MASK_DH3 + BTM_ACL_PKT_TYPES_MASK_DM3 +
  78. BTM_ACL_PKT_TYPES_MASK_DH5 + BTM_ACL_PKT_TYPES_MASK_DM5;
  79. btm_cb.btm_sco_pkt_types_supported = BTM_SCO_PKT_TYPES_MASK_HV1 +
  80. BTM_SCO_PKT_TYPES_MASK_HV2 +
  81. BTM_SCO_PKT_TYPES_MASK_HV3 +
  82. BTM_SCO_PKT_TYPES_MASK_EV3 +
  83. BTM_SCO_PKT_TYPES_MASK_EV4 +
  84. BTM_SCO_PKT_TYPES_MASK_EV5;
  85. }
  86. /*******************************************************************************
  87. **
  88. ** Function btm_db_reset
  89. **
  90. ** Description This function is called by BTM_DeviceReset and clears out any
  91. ** pending callbacks for inquiries, discoveries, other pending
  92. ** functions that may be in progress.
  93. **
  94. ** Returns void
  95. **
  96. *******************************************************************************/
  97. static void btm_db_reset (void)
  98. {
  99. tBTM_CMPL_CB *p_cb;
  100. tBTM_STATUS status = BTM_DEV_RESET;
  101. btm_inq_db_reset();
  102. if (btm_cb.devcb.p_rln_cmpl_cb) {
  103. p_cb = btm_cb.devcb.p_rln_cmpl_cb;
  104. btm_cb.devcb.p_rln_cmpl_cb = NULL;
  105. if (p_cb) {
  106. (*p_cb)((void *) NULL);
  107. }
  108. }
  109. if (btm_cb.devcb.p_rssi_cmpl_cb) {
  110. p_cb = btm_cb.devcb.p_rssi_cmpl_cb;
  111. btm_cb.devcb.p_rssi_cmpl_cb = NULL;
  112. if (p_cb) {
  113. (*p_cb)((tBTM_RSSI_RESULTS *) &status);
  114. }
  115. }
  116. }
  117. static void reset_complete(void)
  118. {
  119. const controller_t *controller = controller_get_interface();
  120. /* Tell L2CAP that all connections are gone */
  121. l2cu_device_reset ();
  122. #if (SMP_INCLUDED == TRUE)
  123. /* Clear current security state */
  124. {
  125. list_node_t *p_node = NULL;
  126. for (p_node = list_begin(btm_cb.p_sec_dev_rec_list); p_node; p_node = list_next(p_node)) {
  127. tBTM_SEC_DEV_REC *p_dev_rec = (tBTM_SEC_DEV_REC *) list_node(p_node);
  128. p_dev_rec->sec_state = BTM_SEC_STATE_IDLE;
  129. }
  130. }
  131. #endif ///SMP_INCLUDED == TRUE
  132. /* After the reset controller should restore all parameters to defaults. */
  133. btm_cb.btm_inq_vars.inq_counter = 1;
  134. btm_cb.btm_inq_vars.inq_scan_window = HCI_DEF_INQUIRYSCAN_WINDOW;
  135. btm_cb.btm_inq_vars.inq_scan_period = HCI_DEF_INQUIRYSCAN_INTERVAL;
  136. btm_cb.btm_inq_vars.inq_scan_type = HCI_DEF_SCAN_TYPE;
  137. btm_cb.btm_inq_vars.page_scan_window = HCI_DEF_PAGESCAN_WINDOW;
  138. btm_cb.btm_inq_vars.page_scan_period = HCI_DEF_PAGESCAN_INTERVAL;
  139. btm_cb.btm_inq_vars.page_scan_type = HCI_DEF_SCAN_TYPE;
  140. btm_cb.btm_inq_vars.page_timeout = HCI_DEFAULT_PAGE_TOUT;
  141. #if (BLE_INCLUDED == TRUE)
  142. btm_cb.ble_ctr_cb.conn_state = BLE_CONN_IDLE;
  143. btm_cb.ble_ctr_cb.bg_conn_type = BTM_BLE_CONN_NONE;
  144. btm_cb.ble_ctr_cb.p_select_cback = NULL;
  145. gatt_reset_bgdev_list();
  146. btm_ble_multi_adv_init();
  147. #endif
  148. btm_pm_reset();
  149. l2c_link_processs_num_bufs(controller->get_acl_buffer_count_classic());
  150. #if BTM_SCO_HCI_INCLUDED == TRUE
  151. btm_sco_process_num_bufs(controller->get_sco_buffer_count());
  152. #endif
  153. #if (BLE_INCLUDED == TRUE)
  154. #if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == TRUE)
  155. /* Set up the BLE privacy settings */
  156. if (controller->supports_ble() && controller->supports_ble_privacy() &&
  157. controller->get_ble_resolving_list_max_size() > 0) {
  158. btm_ble_resolving_list_init(controller->get_ble_resolving_list_max_size());
  159. /* set the default random private address timeout */
  160. btsnd_hcic_ble_set_rand_priv_addr_timeout(BTM_BLE_PRIVATE_ADDR_INT);
  161. }
  162. #endif
  163. if (controller->supports_ble()) {
  164. btm_ble_white_list_init(controller->get_ble_white_list_size());
  165. l2c_link_processs_ble_num_bufs(controller->get_acl_buffer_count_ble());
  166. }
  167. #endif
  168. #if (SMP_INCLUDED == TRUE && CLASSIC_BT_INCLUDED == TRUE)
  169. BTM_SetPinType (btm_cb.cfg.pin_type, btm_cb.cfg.pin_code, btm_cb.cfg.pin_code_len);
  170. #endif ///SMP_INCLUDED == TRUE && CLASSIC_BT_INCLUDED == TRUE
  171. #if (CLASSIC_BT_INCLUDED == TRUE)
  172. BTM_WritePageTimeout(btm_cb.btm_inq_vars.page_timeout, NULL);
  173. #endif ///CLASSIC_BT_INCLUDED == TRUE
  174. for (int i = 0; i <= controller->get_last_features_classic_index(); i++) {
  175. btm_decode_ext_features_page(i, controller->get_features_classic(i)->as_array);
  176. }
  177. btm_report_device_status(BTM_DEV_STATUS_UP);
  178. }
  179. // TODO(zachoverflow): remove this function
  180. void BTM_DeviceReset (UNUSED_ATTR tBTM_CMPL_CB *p_cb)
  181. {
  182. /* Flush all ACL connections */
  183. btm_acl_device_down();
  184. /* Clear the callback, so application would not hang on reset */
  185. btm_db_reset();
  186. /* todo: review the below logic; start_up executes under another task context
  187. * reset_complete runs in btu task */
  188. controller_get_interface()->start_up();
  189. reset_complete();
  190. }
  191. /*******************************************************************************
  192. **
  193. ** Function BTM_IsDeviceUp
  194. **
  195. ** Description This function is called to check if the device is up.
  196. **
  197. ** Returns TRUE if device is up, else FALSE
  198. **
  199. *******************************************************************************/
  200. BOOLEAN BTM_IsDeviceUp (void)
  201. {
  202. return controller_get_interface()->get_is_ready();
  203. }
  204. /*******************************************************************************
  205. **
  206. ** Function btm_dev_timeout
  207. **
  208. ** Description This function is called when a timer list entry expires.
  209. **
  210. ** Returns void
  211. **
  212. *******************************************************************************/
  213. void btm_dev_timeout (TIMER_LIST_ENT *p_tle)
  214. {
  215. TIMER_PARAM_TYPE timer_type = (TIMER_PARAM_TYPE)p_tle->param;
  216. if (timer_type == (TIMER_PARAM_TYPE)TT_DEV_RLN) {
  217. tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_rln_cmpl_cb;
  218. btm_cb.devcb.p_rln_cmpl_cb = NULL;
  219. if (p_cb) {
  220. (*p_cb)((void *) NULL);
  221. }
  222. }
  223. }
  224. /*******************************************************************************
  225. **
  226. ** Function btm_decode_ext_features_page
  227. **
  228. ** Description This function is decodes a features page.
  229. **
  230. ** Returns void
  231. **
  232. *******************************************************************************/
  233. static void btm_decode_ext_features_page (UINT8 page_number, const BD_FEATURES p_features)
  234. {
  235. BTM_TRACE_DEBUG ("btm_decode_ext_features_page page: %d", page_number);
  236. switch (page_number) {
  237. /* Extended (Legacy) Page 0 */
  238. case HCI_EXT_FEATURES_PAGE_0:
  239. /* Create ACL supported packet types mask */
  240. btm_cb.btm_acl_pkt_types_supported = (BTM_ACL_PKT_TYPES_MASK_DH1 +
  241. BTM_ACL_PKT_TYPES_MASK_DM1);
  242. if (HCI_3_SLOT_PACKETS_SUPPORTED(p_features)) {
  243. btm_cb.btm_acl_pkt_types_supported |= (BTM_ACL_PKT_TYPES_MASK_DH3 +
  244. BTM_ACL_PKT_TYPES_MASK_DM3);
  245. }
  246. if (HCI_5_SLOT_PACKETS_SUPPORTED(p_features)) {
  247. btm_cb.btm_acl_pkt_types_supported |= (BTM_ACL_PKT_TYPES_MASK_DH5 +
  248. BTM_ACL_PKT_TYPES_MASK_DM5);
  249. }
  250. /* Add in EDR related ACL types */
  251. if (!HCI_EDR_ACL_2MPS_SUPPORTED(p_features)) {
  252. btm_cb.btm_acl_pkt_types_supported |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH1 +
  253. BTM_ACL_PKT_TYPES_MASK_NO_2_DH3 +
  254. BTM_ACL_PKT_TYPES_MASK_NO_2_DH5);
  255. }
  256. if (!HCI_EDR_ACL_3MPS_SUPPORTED(p_features)) {
  257. btm_cb.btm_acl_pkt_types_supported |= (BTM_ACL_PKT_TYPES_MASK_NO_3_DH1 +
  258. BTM_ACL_PKT_TYPES_MASK_NO_3_DH3 +
  259. BTM_ACL_PKT_TYPES_MASK_NO_3_DH5);
  260. }
  261. /* Check to see if 3 and 5 slot packets are available */
  262. if (HCI_EDR_ACL_2MPS_SUPPORTED(p_features) ||
  263. HCI_EDR_ACL_3MPS_SUPPORTED(p_features)) {
  264. if (!HCI_3_SLOT_EDR_ACL_SUPPORTED(p_features)) {
  265. btm_cb.btm_acl_pkt_types_supported |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH3 +
  266. BTM_ACL_PKT_TYPES_MASK_NO_3_DH3);
  267. }
  268. if (!HCI_5_SLOT_EDR_ACL_SUPPORTED(p_features)) {
  269. btm_cb.btm_acl_pkt_types_supported |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH5 +
  270. BTM_ACL_PKT_TYPES_MASK_NO_3_DH5);
  271. }
  272. }
  273. BTM_TRACE_DEBUG("Local supported ACL packet types: 0x%04x",
  274. btm_cb.btm_acl_pkt_types_supported);
  275. /* Create (e)SCO supported packet types mask */
  276. btm_cb.btm_sco_pkt_types_supported = 0;
  277. #if BTM_SCO_INCLUDED == TRUE
  278. btm_cb.sco_cb.esco_supported = FALSE;
  279. #endif
  280. if (HCI_SCO_LINK_SUPPORTED(p_features)) {
  281. btm_cb.btm_sco_pkt_types_supported = BTM_SCO_PKT_TYPES_MASK_HV1;
  282. if (HCI_HV2_PACKETS_SUPPORTED(p_features)) {
  283. btm_cb.btm_sco_pkt_types_supported |= BTM_SCO_PKT_TYPES_MASK_HV2;
  284. }
  285. if (HCI_HV3_PACKETS_SUPPORTED(p_features)) {
  286. btm_cb.btm_sco_pkt_types_supported |= BTM_SCO_PKT_TYPES_MASK_HV3;
  287. }
  288. }
  289. if (HCI_ESCO_EV3_SUPPORTED(p_features)) {
  290. btm_cb.btm_sco_pkt_types_supported |= BTM_SCO_PKT_TYPES_MASK_EV3;
  291. }
  292. if (HCI_ESCO_EV4_SUPPORTED(p_features)) {
  293. btm_cb.btm_sco_pkt_types_supported |= BTM_SCO_PKT_TYPES_MASK_EV4;
  294. }
  295. if (HCI_ESCO_EV5_SUPPORTED(p_features)) {
  296. btm_cb.btm_sco_pkt_types_supported |= BTM_SCO_PKT_TYPES_MASK_EV5;
  297. }
  298. #if BTM_SCO_INCLUDED == TRUE
  299. if (btm_cb.btm_sco_pkt_types_supported & BTM_ESCO_LINK_ONLY_MASK) {
  300. btm_cb.sco_cb.esco_supported = TRUE;
  301. /* Add in EDR related eSCO types */
  302. if (HCI_EDR_ESCO_2MPS_SUPPORTED(p_features)) {
  303. if (!HCI_3_SLOT_EDR_ESCO_SUPPORTED(p_features)) {
  304. btm_cb.btm_sco_pkt_types_supported |= BTM_SCO_PKT_TYPES_MASK_NO_2_EV5;
  305. }
  306. } else {
  307. btm_cb.btm_sco_pkt_types_supported |= (BTM_SCO_PKT_TYPES_MASK_NO_2_EV3 +
  308. BTM_SCO_PKT_TYPES_MASK_NO_2_EV5);
  309. }
  310. if (HCI_EDR_ESCO_3MPS_SUPPORTED(p_features)) {
  311. if (!HCI_3_SLOT_EDR_ESCO_SUPPORTED(p_features)) {
  312. btm_cb.btm_sco_pkt_types_supported |= BTM_SCO_PKT_TYPES_MASK_NO_3_EV5;
  313. }
  314. } else {
  315. btm_cb.btm_sco_pkt_types_supported |= (BTM_SCO_PKT_TYPES_MASK_NO_3_EV3 +
  316. BTM_SCO_PKT_TYPES_MASK_NO_3_EV5);
  317. }
  318. }
  319. #endif
  320. BTM_TRACE_DEBUG("Local supported SCO packet types: 0x%04x",
  321. btm_cb.btm_sco_pkt_types_supported);
  322. /* Create Default Policy Settings */
  323. if (HCI_SWITCH_SUPPORTED(p_features)) {
  324. btm_cb.btm_def_link_policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
  325. } else {
  326. btm_cb.btm_def_link_policy &= ~HCI_ENABLE_MASTER_SLAVE_SWITCH;
  327. }
  328. if (HCI_HOLD_MODE_SUPPORTED(p_features)) {
  329. btm_cb.btm_def_link_policy |= HCI_ENABLE_HOLD_MODE;
  330. } else {
  331. btm_cb.btm_def_link_policy &= ~HCI_ENABLE_HOLD_MODE;
  332. }
  333. if (HCI_SNIFF_MODE_SUPPORTED(p_features)) {
  334. btm_cb.btm_def_link_policy |= HCI_ENABLE_SNIFF_MODE;
  335. } else {
  336. btm_cb.btm_def_link_policy &= ~HCI_ENABLE_SNIFF_MODE;
  337. }
  338. if (HCI_PARK_MODE_SUPPORTED(p_features)) {
  339. btm_cb.btm_def_link_policy |= HCI_ENABLE_PARK_MODE;
  340. } else {
  341. btm_cb.btm_def_link_policy &= ~HCI_ENABLE_PARK_MODE;
  342. }
  343. btm_sec_dev_reset ();
  344. if (HCI_LMP_INQ_RSSI_SUPPORTED(p_features)) {
  345. if (HCI_EXT_INQ_RSP_SUPPORTED(p_features)) {
  346. BTM_SetInquiryMode (BTM_INQ_RESULT_EXTENDED);
  347. } else {
  348. BTM_SetInquiryMode (BTM_INQ_RESULT_WITH_RSSI);
  349. }
  350. }
  351. #if L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE
  352. if ( HCI_NON_FLUSHABLE_PB_SUPPORTED(p_features)) {
  353. l2cu_set_non_flushable_pbf(TRUE);
  354. } else {
  355. l2cu_set_non_flushable_pbf(FALSE);
  356. }
  357. #endif
  358. BTM_SetPageScanType (BTM_DEFAULT_SCAN_TYPE);
  359. BTM_SetInquiryScanType (BTM_DEFAULT_SCAN_TYPE);
  360. break;
  361. /* Extended Page 1 */
  362. case HCI_EXT_FEATURES_PAGE_1:
  363. /* Nothing to do for page 1 */
  364. break;
  365. /* Extended Page 2 */
  366. case HCI_EXT_FEATURES_PAGE_2:
  367. /* Nothing to do for page 2 */
  368. break;
  369. default:
  370. BTM_TRACE_ERROR("btm_decode_ext_features_page page=%d unknown", page_number);
  371. break;
  372. }
  373. }
  374. /*******************************************************************************
  375. **
  376. ** Function BTM_SetLocalDeviceName
  377. **
  378. ** Description This function is called to set the local device name.
  379. **
  380. ** Returns status of the operation
  381. **
  382. *******************************************************************************/
  383. tBTM_STATUS BTM_SetLocalDeviceName (char *p_name)
  384. {
  385. UINT8 *p;
  386. if (!p_name || !p_name[0] || (strlen ((char *)p_name) > BD_NAME_LEN)) {
  387. return (BTM_ILLEGAL_VALUE);
  388. }
  389. if (!controller_get_interface()->get_is_ready()) {
  390. return (BTM_DEV_RESET);
  391. }
  392. #if BTM_MAX_LOC_BD_NAME_LEN > 0
  393. /* Save the device name if local storage is enabled */
  394. p = (UINT8 *)btm_cb.cfg.bd_name;
  395. if (p != (UINT8 *)p_name) {
  396. BCM_STRNCPY_S(btm_cb.cfg.bd_name, p_name, BTM_MAX_LOC_BD_NAME_LEN);
  397. btm_cb.cfg.bd_name[BTM_MAX_LOC_BD_NAME_LEN] = '\0';
  398. }
  399. #else
  400. p = (UINT8 *)p_name;
  401. #endif
  402. #if CLASSIC_BT_INCLUDED
  403. if (btsnd_hcic_change_name(p)) {
  404. return (BTM_CMD_STARTED);
  405. } else
  406. #endif
  407. {
  408. return (BTM_NO_RESOURCES);
  409. }
  410. }
  411. /*******************************************************************************
  412. **
  413. ** Function BTM_ReadLocalDeviceName
  414. **
  415. ** Description This function is called to read the local device name.
  416. **
  417. ** Returns status of the operation
  418. ** If success, BTM_SUCCESS is returned and p_name points stored
  419. ** local device name
  420. ** If BTM doesn't store local device name, BTM_NO_RESOURCES is
  421. ** is returned and p_name is set to NULL
  422. **
  423. *******************************************************************************/
  424. tBTM_STATUS BTM_ReadLocalDeviceName (char **p_name)
  425. {
  426. #if BTM_MAX_LOC_BD_NAME_LEN > 0
  427. *p_name = btm_cb.cfg.bd_name;
  428. return (BTM_SUCCESS);
  429. #else
  430. *p_name = NULL;
  431. return (BTM_NO_RESOURCES);
  432. #endif
  433. }
  434. /*******************************************************************************
  435. **
  436. ** Function BTM_ReadLocalDeviceNameFromController
  437. **
  438. ** Description Get local device name from controller. Do not use cached
  439. ** name (used to get chip-id prior to btm reset complete).
  440. **
  441. ** Returns BTM_CMD_STARTED if successful, otherwise an error
  442. **
  443. *******************************************************************************/
  444. tBTM_STATUS BTM_ReadLocalDeviceNameFromController (tBTM_CMPL_CB *p_rln_cmpl_cback)
  445. {
  446. /* Check if rln already in progress */
  447. if (btm_cb.devcb.p_rln_cmpl_cb) {
  448. return (BTM_NO_RESOURCES);
  449. }
  450. /* Save callback */
  451. btm_cb.devcb.p_rln_cmpl_cb = p_rln_cmpl_cback;
  452. btsnd_hcic_read_name();
  453. btu_start_timer (&btm_cb.devcb.rln_timer, BTU_TTYPE_BTM_DEV_CTL, BTM_DEV_REPLY_TIMEOUT);
  454. return BTM_CMD_STARTED;
  455. }
  456. /*******************************************************************************
  457. **
  458. ** Function btm_read_local_name_complete
  459. **
  460. ** Description This function is called when local name read complete.
  461. ** message is received from the HCI.
  462. **
  463. ** Returns void
  464. **
  465. *******************************************************************************/
  466. void btm_read_local_name_complete (UINT8 *p, UINT16 evt_len)
  467. {
  468. tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_rln_cmpl_cb;
  469. UINT8 status;
  470. UNUSED(evt_len);
  471. btu_free_timer (&btm_cb.devcb.rln_timer);
  472. /* If there was a callback address for read local name, call it */
  473. btm_cb.devcb.p_rln_cmpl_cb = NULL;
  474. if (p_cb) {
  475. STREAM_TO_UINT8 (status, p);
  476. if (status == HCI_SUCCESS) {
  477. (*p_cb)(p);
  478. } else {
  479. (*p_cb)(NULL);
  480. }
  481. }
  482. }
  483. /*******************************************************************************
  484. **
  485. ** Function BTM_SetDeviceClass
  486. **
  487. ** Description This function is called to set the local device class
  488. **
  489. ** Returns status of the operation
  490. **
  491. *******************************************************************************/
  492. tBTM_STATUS BTM_SetDeviceClass (DEV_CLASS dev_class)
  493. {
  494. if (!memcmp (btm_cb.devcb.dev_class, dev_class, DEV_CLASS_LEN)) {
  495. return (BTM_SUCCESS);
  496. }
  497. memcpy (btm_cb.devcb.dev_class, dev_class, DEV_CLASS_LEN);
  498. if (!controller_get_interface()->get_is_ready()) {
  499. return (BTM_DEV_RESET);
  500. }
  501. if (!btsnd_hcic_write_dev_class (dev_class)) {
  502. return (BTM_NO_RESOURCES);
  503. }
  504. return (BTM_SUCCESS);
  505. }
  506. /*******************************************************************************
  507. **
  508. ** Function BTM_ReadDeviceClass
  509. **
  510. ** Description This function is called to read the local device class
  511. **
  512. ** Returns pointer to the device class
  513. **
  514. *******************************************************************************/
  515. UINT8 *BTM_ReadDeviceClass (void)
  516. {
  517. return ((UINT8 *)btm_cb.devcb.dev_class);
  518. }
  519. /*******************************************************************************
  520. **
  521. ** Function BTM_ReadLocalFeatures
  522. **
  523. ** Description This function is called to read the local features
  524. **
  525. ** Returns pointer to the local features string
  526. **
  527. *******************************************************************************/
  528. // TODO(zachoverflow): get rid of this function
  529. UINT8 *BTM_ReadLocalFeatures (void)
  530. {
  531. // Discarding const modifier for now, until this function dies
  532. return (UINT8 *)controller_get_interface()->get_features_classic(0)->as_array;
  533. }
  534. /*******************************************************************************
  535. **
  536. ** Function BTM_RegisterForDeviceStatusNotif
  537. **
  538. ** Description This function is called to register for device status
  539. ** change notifications.
  540. **
  541. ** If one registration is already there calling function should
  542. ** save the pointer to the function that is return and
  543. ** call it when processing of the event is complete
  544. **
  545. ** Returns status of the operation
  546. **
  547. *******************************************************************************/
  548. tBTM_DEV_STATUS_CB *BTM_RegisterForDeviceStatusNotif (tBTM_DEV_STATUS_CB *p_cb)
  549. {
  550. tBTM_DEV_STATUS_CB *p_prev = btm_cb.devcb.p_dev_status_cb;
  551. btm_cb.devcb.p_dev_status_cb = p_cb;
  552. return (p_prev);
  553. }
  554. /*******************************************************************************
  555. **
  556. ** Function BTM_VendorSpecificCommand
  557. **
  558. ** Description Send a vendor specific HCI command to the controller.
  559. **
  560. ** Returns
  561. ** BTM_SUCCESS Command sent. Does not expect command complete
  562. ** event. (command cmpl callback param is NULL)
  563. ** BTM_CMD_STARTED Command sent. Waiting for command cmpl event.
  564. **
  565. ** Notes
  566. ** Opcode will be OR'd with HCI_GRP_VENDOR_SPECIFIC.
  567. **
  568. *******************************************************************************/
  569. tBTM_STATUS BTM_VendorSpecificCommand(UINT16 opcode, UINT8 param_len,
  570. UINT8 *p_param_buf, tBTM_VSC_CMPL_CB *p_cb)
  571. {
  572. BT_HDR *p_buf;
  573. BTM_TRACE_EVENT ("BTM: BTM_VendorSpecificCommand: Opcode: 0x%04X, ParamLen: %i.",
  574. opcode, param_len);
  575. /* Allocate a buffer to hold HCI command plus the callback function */
  576. if ((p_buf = HCI_GET_CMD_BUF(param_len)) != NULL) {
  577. /* Send the HCI command (opcode will be OR'd with HCI_GRP_VENDOR_SPECIFIC) */
  578. btsnd_hcic_vendor_spec_cmd (p_buf, opcode, param_len, p_param_buf, (void *)p_cb);
  579. /* Return value */
  580. if (p_cb != NULL) {
  581. return (BTM_CMD_STARTED);
  582. } else {
  583. return (BTM_SUCCESS);
  584. }
  585. } else {
  586. return (BTM_NO_RESOURCES);
  587. }
  588. }
  589. #if (ESP_COEX_VSC_INCLUDED == TRUE)
  590. tBTM_STATUS BTM_ConfigCoexStatus(tBTM_COEX_OPERATION op, tBTM_COEX_TYPE type, UINT8 status)
  591. {
  592. UINT8 param[3];
  593. UINT8 *p = (UINT8 *)param;
  594. UINT8_TO_STREAM(p, type);
  595. UINT8_TO_STREAM(p, op);
  596. UINT8_TO_STREAM(p, status);
  597. return BTM_VendorSpecificCommand(HCI_VENDOR_COMMON_COEX_STATUS_CMD_OPCODE, 3, param, NULL);
  598. }
  599. #endif
  600. /*******************************************************************************
  601. **
  602. ** Function btm_vsc_complete
  603. **
  604. ** Description This function is called when local HCI Vendor Specific
  605. ** Command complete message is received from the HCI.
  606. **
  607. ** Returns void
  608. **
  609. *******************************************************************************/
  610. void btm_vsc_complete (UINT8 *p, UINT16 opcode, UINT16 evt_len,
  611. tBTM_CMPL_CB *p_vsc_cplt_cback)
  612. {
  613. #if (BLE_INCLUDED == TRUE)
  614. tBTM_BLE_CB *ble_cb = &btm_cb.ble_ctr_cb;
  615. switch(opcode) {
  616. case HCI_VENDOR_BLE_LONG_ADV_DATA:
  617. BTM_TRACE_EVENT("Set long adv data complete\n");
  618. break;
  619. case HCI_VENDOR_BLE_UPDATE_DUPLICATE_EXCEPTIONAL_LIST: {
  620. uint8_t subcode, status; uint32_t length;
  621. STREAM_TO_UINT8(status, p);
  622. STREAM_TO_UINT8(subcode, p);
  623. STREAM_TO_UINT32(length, p);
  624. if(ble_cb && ble_cb->update_exceptional_list_cmp_cb) {
  625. (*ble_cb->update_exceptional_list_cmp_cb)(status, subcode, length, p);
  626. }
  627. break;
  628. }
  629. case HCI_VENDOR_BLE_CLEAR_ADV: {
  630. uint8_t status;
  631. STREAM_TO_UINT8(status, p);
  632. if (ble_cb && ble_cb->inq_var.p_clear_adv_cb) {
  633. ble_cb->inq_var.p_clear_adv_cb(status);
  634. }
  635. break;
  636. }
  637. default:
  638. break;
  639. }
  640. tBTM_VSC_CMPL vcs_cplt_params;
  641. /* If there was a callback address for vcs complete, call it */
  642. if (p_vsc_cplt_cback) {
  643. /* Pass paramters to the callback function */
  644. vcs_cplt_params.opcode = opcode; /* Number of bytes in return info */
  645. vcs_cplt_params.param_len = evt_len; /* Number of bytes in return info */
  646. vcs_cplt_params.p_param_buf = p;
  647. (*p_vsc_cplt_cback)(&vcs_cplt_params); /* Call the VSC complete callback function */
  648. }
  649. #endif
  650. }
  651. /*******************************************************************************
  652. **
  653. ** Function BTM_RegisterForVSEvents
  654. **
  655. ** Description This function is called to register/deregister for vendor
  656. ** specific HCI events.
  657. **
  658. ** If is_register=TRUE, then the function will be registered;
  659. ** if is_register=FALSE, then the function will be deregistered.
  660. **
  661. ** Returns BTM_SUCCESS if successful,
  662. ** BTM_BUSY if maximum number of callbacks have already been
  663. ** registered.
  664. **
  665. *******************************************************************************/
  666. tBTM_STATUS BTM_RegisterForVSEvents (tBTM_VS_EVT_CB *p_cb, BOOLEAN is_register)
  667. {
  668. tBTM_STATUS retval = BTM_SUCCESS;
  669. UINT8 i, free_idx = BTM_MAX_VSE_CALLBACKS;
  670. /* See if callback is already registered */
  671. for (i = 0; i < BTM_MAX_VSE_CALLBACKS; i++) {
  672. if (btm_cb.devcb.p_vend_spec_cb[i] == NULL) {
  673. /* Found a free slot. Store index */
  674. free_idx = i;
  675. } else if (btm_cb.devcb.p_vend_spec_cb[i] == p_cb) {
  676. /* Found callback in lookup table. If deregistering, clear the entry. */
  677. if (is_register == FALSE) {
  678. btm_cb.devcb.p_vend_spec_cb[i] = NULL;
  679. BTM_TRACE_EVENT("BTM Deregister For VSEvents is successfully");
  680. }
  681. return (BTM_SUCCESS);
  682. }
  683. }
  684. /* Didn't find callback. Add callback to free slot if registering */
  685. if (is_register) {
  686. if (free_idx < BTM_MAX_VSE_CALLBACKS) {
  687. btm_cb.devcb.p_vend_spec_cb[free_idx] = p_cb;
  688. BTM_TRACE_EVENT("BTM Register For VSEvents is successfully");
  689. } else {
  690. /* No free entries available */
  691. BTM_TRACE_ERROR ("BTM_RegisterForVSEvents: too many callbacks registered");
  692. retval = BTM_NO_RESOURCES;
  693. }
  694. }
  695. return (retval);
  696. }
  697. /*******************************************************************************
  698. **
  699. ** Function btm_vendor_specific_evt
  700. **
  701. ** Description Process event HCI_VENDOR_SPECIFIC_EVT
  702. **
  703. ** Note: Some controllers do not send command complete, so
  704. ** the callback and busy flag are cleared here also.
  705. **
  706. ** Returns void
  707. **
  708. *******************************************************************************/
  709. void btm_vendor_specific_evt (UINT8 *p, UINT8 evt_len)
  710. {
  711. UINT8 i;
  712. #if (CLASSIC_BT_INCLUDED == TRUE)
  713. UINT8 sub_event;
  714. UINT8 *p_evt = p;
  715. STREAM_TO_UINT8(sub_event, p_evt);
  716. /* Check in subevent if authentication is through Legacy Authentication. */
  717. if (sub_event == ESP_VS_REM_LEGACY_AUTH_CMP) {
  718. UINT16 hci_handle;
  719. STREAM_TO_UINT16(hci_handle, p_evt);
  720. btm_sec_handle_remote_legacy_auth_cmp(hci_handle);
  721. }
  722. #endif /// (CLASSIC_BT_INCLUDED == TRUE)
  723. for (i = 0; i < BTM_MAX_VSE_CALLBACKS; i++) {
  724. if (btm_cb.devcb.p_vend_spec_cb[i]) {
  725. (*btm_cb.devcb.p_vend_spec_cb[i])(evt_len, p);
  726. }
  727. }
  728. BTM_TRACE_DEBUG ("BTM Event: Vendor Specific event from controller");
  729. }
  730. #if (CLASSIC_BT_INCLUDED == TRUE)
  731. /*******************************************************************************
  732. **
  733. ** Function BTM_WritePageTimeout
  734. **
  735. ** Description Send HCI Write Page Timeout.
  736. **
  737. ** Returns
  738. ** BTM_SUCCESS Command sent.
  739. ** BTM_NO_RESOURCES If out of resources to send the command.
  740. **
  741. **
  742. *******************************************************************************/
  743. tBTM_STATUS BTM_WritePageTimeout(UINT16 timeout, tBTM_CMPL_CB *p_cb)
  744. {
  745. BTM_TRACE_EVENT ("BTM: BTM_WritePageTimeout: Timeout: %d.", timeout);
  746. if (timeout >= HCI_MIN_PAGE_TOUT) {
  747. btm_cb.btm_inq_vars.page_timeout = timeout;
  748. }
  749. btm_cb.devcb.p_page_to_set_cmpl_cb = p_cb;
  750. /* Send the HCI command */
  751. if (!btsnd_hcic_write_page_tout (timeout)) {
  752. return (BTM_NO_RESOURCES);
  753. }
  754. if (p_cb) {
  755. btu_start_timer(&btm_cb.devcb.page_timeout_set_timer, BTU_TTYPE_BTM_SET_PAGE_TO, BTM_DEV_REPLY_TIMEOUT);
  756. }
  757. return (BTM_CMD_STARTED);
  758. }
  759. /*******************************************************************************
  760. **
  761. ** Function btm_set_page_timeout_complete
  762. **
  763. ** Description This function is called when setting page timeout complete.
  764. ** message is received from the HCI.
  765. **
  766. ** Returns void
  767. **
  768. *******************************************************************************/
  769. void btm_set_page_timeout_complete (const UINT8 *p)
  770. {
  771. tBTM_SET_PAGE_TIMEOUT_RESULTS results;
  772. tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_page_to_set_cmpl_cb;
  773. btm_cb.devcb.p_page_to_set_cmpl_cb = NULL;
  774. btu_free_timer (&btm_cb.devcb.page_timeout_set_timer);
  775. /* If there is a callback address for setting page timeout, call it */
  776. if (p_cb) {
  777. if (p) {
  778. STREAM_TO_UINT8 (results.hci_status, p);
  779. switch (results.hci_status) {
  780. case HCI_SUCCESS:
  781. results.status = BTM_SUCCESS;
  782. break;
  783. case HCI_ERR_UNSUPPORTED_VALUE:
  784. case HCI_ERR_ILLEGAL_PARAMETER_FMT:
  785. results.status = BTM_ILLEGAL_VALUE;
  786. break;
  787. default:
  788. results.status = BTM_NO_RESOURCES;
  789. break;
  790. }
  791. } else {
  792. results.hci_status = HCI_ERR_HOST_TIMEOUT;
  793. results.status = BTM_DEVICE_TIMEOUT;
  794. }
  795. (*p_cb)(&results);
  796. }
  797. }
  798. #endif /// CLASSIC_BT_INCLUDED == TRUE
  799. /*******************************************************************************
  800. **
  801. ** Function btm_page_to_setup_timeout
  802. **
  803. ** Description This function processes a timeout.
  804. ** Currently, we just report an error log
  805. **
  806. ** Returns void
  807. **
  808. *******************************************************************************/
  809. void btm_page_to_setup_timeout (void *p_tle)
  810. {
  811. UNUSED(p_tle);
  812. BTM_TRACE_DEBUG ("%s\n", __func__);
  813. #if (CLASSIC_BT_INCLUDED == TRUE)
  814. btm_set_page_timeout_complete (NULL);
  815. #endif /// CLASSIC_BT_INCLUDED == TRUE
  816. }
  817. /*******************************************************************************
  818. **
  819. ** Function BTM_ReadPageTimeout
  820. **
  821. ** Description Send HCI Read Page Timeout.
  822. **
  823. ** Returns
  824. ** BTM_SUCCESS Command sent.
  825. ** BTM_NO_RESOURCES If out of resources to send the command.
  826. **
  827. *******************************************************************************/
  828. //extern
  829. tBTM_STATUS BTM_ReadPageTimeout(tBTM_CMPL_CB *p_cb)
  830. {
  831. BTM_TRACE_EVENT ("BTM: BTM_ReadPageTimeout");
  832. /* Get the page timeout */
  833. tBTM_GET_PAGE_TIMEOUT_RESULTS results;
  834. if (p_cb) {
  835. results.hci_status = HCI_SUCCESS;
  836. results.status = BTM_SUCCESS;
  837. results.page_to = btm_cb.btm_inq_vars.page_timeout;
  838. (*p_cb)(&results);
  839. return (BTM_SUCCESS);
  840. } else {
  841. return (BTM_NO_RESOURCES);
  842. }
  843. }
  844. /*******************************************************************************
  845. **
  846. ** Function BTM_WriteVoiceSettings
  847. **
  848. ** Description Send HCI Write Voice Settings command.
  849. ** See stack/hcidefs.h for settings bitmask values.
  850. **
  851. ** Returns
  852. ** BTM_SUCCESS Command sent.
  853. ** BTM_NO_RESOURCES If out of resources to send the command.
  854. **
  855. **
  856. *******************************************************************************/
  857. tBTM_STATUS BTM_WriteVoiceSettings(UINT16 settings)
  858. {
  859. BTM_TRACE_EVENT ("BTM: BTM_WriteVoiceSettings: Settings: 0x%04x.", settings);
  860. /* Send the HCI command */
  861. if (btsnd_hcic_write_voice_settings ((UINT16)(settings & 0x03ff))) {
  862. return (BTM_SUCCESS);
  863. }
  864. return (BTM_NO_RESOURCES);
  865. }
  866. /*******************************************************************************
  867. **
  868. ** Function BTM_EnableTestMode
  869. **
  870. ** Description Send HCI the enable device under test command.
  871. **
  872. ** Note: Controller can only be taken out of this mode by
  873. ** resetting the controller.
  874. **
  875. ** Returns
  876. ** BTM_SUCCESS Command sent.
  877. ** BTM_NO_RESOURCES If out of resources to send the command.
  878. **
  879. **
  880. *******************************************************************************/
  881. tBTM_STATUS BTM_EnableTestMode(void)
  882. {
  883. UINT8 cond;
  884. BTM_TRACE_EVENT ("BTM: BTM_EnableTestMode");
  885. /* set auto accept connection as this is needed during test mode */
  886. /* Allocate a buffer to hold HCI command */
  887. cond = HCI_DO_AUTO_ACCEPT_CONNECT;
  888. if (!btsnd_hcic_set_event_filter(HCI_FILTER_CONNECTION_SETUP,
  889. HCI_FILTER_COND_NEW_DEVICE,
  890. &cond, sizeof(cond))) {
  891. return (BTM_NO_RESOURCES);
  892. }
  893. /* put device to connectable mode */
  894. if (BTM_SetConnectability(BTM_CONNECTABLE, BTM_DEFAULT_CONN_WINDOW,
  895. BTM_DEFAULT_CONN_INTERVAL) != BTM_SUCCESS) {
  896. return BTM_NO_RESOURCES;
  897. }
  898. /* put device to discoverable mode */
  899. if (BTM_SetDiscoverability(BTM_GENERAL_DISCOVERABLE, BTM_DEFAULT_DISC_WINDOW,
  900. BTM_DEFAULT_DISC_INTERVAL) != BTM_SUCCESS) {
  901. return BTM_NO_RESOURCES;
  902. }
  903. /* mask off all of event from controller */
  904. hci_layer_get_interface()->transmit_command(
  905. hci_packet_factory_get_interface()->make_set_event_mask((const bt_event_mask_t *)("\x00\x00\x00\x00\x00\x00\x00\x00")),
  906. NULL,
  907. NULL,
  908. NULL);
  909. /* Send the HCI command */
  910. if (btsnd_hcic_enable_test_mode ()) {
  911. return (BTM_SUCCESS);
  912. } else {
  913. return (BTM_NO_RESOURCES);
  914. }
  915. }
  916. /*******************************************************************************
  917. **
  918. ** Function BTM_DeleteStoredLinkKey
  919. **
  920. ** Description This function is called to delete link key for the specified
  921. ** device addresses from the NVRAM storage attached to the Bluetooth
  922. ** controller.
  923. **
  924. ** Parameters: bd_addr - Addresses of the devices
  925. ** p_cb - Call back function to be called to return
  926. ** the results
  927. **
  928. *******************************************************************************/
  929. tBTM_STATUS BTM_DeleteStoredLinkKey(BD_ADDR bd_addr, tBTM_CMPL_CB *p_cb)
  930. {
  931. BD_ADDR local_bd_addr = {0};
  932. BOOLEAN delete_all_flag = FALSE;
  933. /* Check if the previous command is completed */
  934. if (btm_cb.devcb.p_stored_link_key_cmpl_cb) {
  935. return (BTM_BUSY);
  936. }
  937. if (!bd_addr) {
  938. /* This is to delete all link keys */
  939. delete_all_flag = TRUE;
  940. /* We don't care the BD address. Just pass a non zero pointer */
  941. bd_addr = local_bd_addr;
  942. }
  943. BTM_TRACE_EVENT ("BTM: BTM_DeleteStoredLinkKey: delete_all_flag: %s",
  944. delete_all_flag ? "TRUE" : "FALSE");
  945. /* Send the HCI command */
  946. btm_cb.devcb.p_stored_link_key_cmpl_cb = p_cb;
  947. if (!btsnd_hcic_delete_stored_key (bd_addr, delete_all_flag)) {
  948. return (BTM_NO_RESOURCES);
  949. } else {
  950. return (BTM_SUCCESS);
  951. }
  952. }
  953. /*******************************************************************************
  954. **
  955. ** Function btm_delete_stored_link_key_complete
  956. **
  957. ** Description This function is called when the command complete message
  958. ** is received from the HCI for the delete stored link key command.
  959. **
  960. ** Returns void
  961. **
  962. *******************************************************************************/
  963. void btm_delete_stored_link_key_complete (UINT8 *p)
  964. {
  965. tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_stored_link_key_cmpl_cb;
  966. tBTM_DELETE_STORED_LINK_KEY_COMPLETE result;
  967. /* If there was a callback registered for read stored link key, call it */
  968. btm_cb.devcb.p_stored_link_key_cmpl_cb = NULL;
  969. if (p_cb) {
  970. /* Set the call back event to indicate command complete */
  971. result.event = BTM_CB_EVT_DELETE_STORED_LINK_KEYS;
  972. /* Extract the result fields from the HCI event */
  973. STREAM_TO_UINT8 (result.status, p);
  974. STREAM_TO_UINT16 (result.num_keys, p);
  975. /* Call the call back and pass the result */
  976. (*p_cb)(&result);
  977. }
  978. }
  979. /*******************************************************************************
  980. **
  981. ** Function btm_report_device_status
  982. **
  983. ** Description This function is called when there is a change in the device
  984. ** status. This function will report the new device status to
  985. ** the application
  986. **
  987. ** Returns void
  988. **
  989. *******************************************************************************/
  990. void btm_report_device_status (tBTM_DEV_STATUS status)
  991. {
  992. tBTM_DEV_STATUS_CB *p_cb = btm_cb.devcb.p_dev_status_cb;
  993. /* Call the call back to pass the device status to application */
  994. if (p_cb) {
  995. (*p_cb)(status);
  996. }
  997. }
  998. #if (CLASSIC_BT_INCLUDED == TRUE)
  999. /*******************************************************************************
  1000. **
  1001. ** Function BTM_SetAfhChannels
  1002. **
  1003. ** Description This function is called to set AFH channels
  1004. **
  1005. ** Returns status of the operation
  1006. **
  1007. *******************************************************************************/
  1008. tBTM_STATUS BTM_SetAfhChannels (AFH_CHANNELS channels, tBTM_CMPL_CB *p_afh_channels_cmpl_cback)
  1009. {
  1010. if (!controller_get_interface()->get_is_ready()) {
  1011. return (BTM_DEV_RESET);
  1012. }
  1013. /* Check if set afh already in progress */
  1014. if (btm_cb.devcb.p_afh_channels_cmpl_cb) {
  1015. return (BTM_NO_RESOURCES);
  1016. }
  1017. /* Save callback */
  1018. btm_cb.devcb.p_afh_channels_cmpl_cb = p_afh_channels_cmpl_cback;
  1019. if (!btsnd_hcic_set_afh_channels (channels)) {
  1020. return (BTM_NO_RESOURCES);
  1021. }
  1022. btu_start_timer (&btm_cb.devcb.afh_channels_timer, BTU_TTYPE_BTM_ACL, BTM_DEV_REPLY_TIMEOUT);
  1023. return BTM_CMD_STARTED;
  1024. }
  1025. /*******************************************************************************
  1026. **
  1027. ** Function btm_set_afh_channels_complete
  1028. **
  1029. ** Description This function is called when setting AFH channels complete.
  1030. ** message is received from the HCI.
  1031. **
  1032. ** Returns void
  1033. **
  1034. *******************************************************************************/
  1035. void btm_set_afh_channels_complete (UINT8 *p)
  1036. {
  1037. tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_afh_channels_cmpl_cb;
  1038. tBTM_SET_AFH_CHANNELS_RESULTS results;
  1039. btu_free_timer (&btm_cb.devcb.afh_channels_timer);
  1040. /* If there is a callback address for setting AFH channels, call it */
  1041. btm_cb.devcb.p_afh_channels_cmpl_cb = NULL;
  1042. if (p_cb) {
  1043. STREAM_TO_UINT8 (results.hci_status, p);
  1044. switch (results.hci_status){
  1045. case HCI_SUCCESS:
  1046. results.status = BTM_SUCCESS;
  1047. break;
  1048. case HCI_ERR_UNSUPPORTED_VALUE:
  1049. case HCI_ERR_ILLEGAL_PARAMETER_FMT:
  1050. results.status = BTM_ILLEGAL_VALUE;
  1051. break;
  1052. default:
  1053. results.status = BTM_ERR_PROCESSING;
  1054. break;
  1055. }
  1056. (*p_cb)(&results);
  1057. }
  1058. }
  1059. #endif /// CLASSIC_BT_INCLUDED == TRUE
  1060. #if (BLE_INCLUDED == TRUE)
  1061. /*******************************************************************************
  1062. **
  1063. ** Function BTM_BleSetChannels
  1064. **
  1065. ** Description This function is called to set BLE channels
  1066. **
  1067. ** Returns status of the operation
  1068. **
  1069. *******************************************************************************/
  1070. tBTM_STATUS BTM_BleSetChannels (BLE_CHANNELS channels, tBTM_CMPL_CB *p_ble_channels_cmpl_cback)
  1071. {
  1072. if (!controller_get_interface()->get_is_ready()) {
  1073. return (BTM_DEV_RESET);
  1074. }
  1075. /* Check if set afh already in progress */
  1076. if (btm_cb.devcb.p_ble_channels_cmpl_cb) {
  1077. return (BTM_NO_RESOURCES);
  1078. }
  1079. /* Save callback */
  1080. btm_cb.devcb.p_ble_channels_cmpl_cb = p_ble_channels_cmpl_cback;
  1081. if (!btsnd_hcic_ble_set_channels (channels)) {
  1082. return (BTM_NO_RESOURCES);
  1083. }
  1084. btu_start_timer (&btm_cb.devcb.ble_channels_timer, BTU_TTYPE_BTM_ACL, BTM_DEV_REPLY_TIMEOUT);
  1085. return BTM_CMD_STARTED;
  1086. }
  1087. /*******************************************************************************
  1088. **
  1089. ** Function btm_ble_set_channels_complete
  1090. **
  1091. ** Description This function is called when setting AFH channels complete.
  1092. ** message is received from the HCI.
  1093. **
  1094. ** Returns void
  1095. **
  1096. *******************************************************************************/
  1097. void btm_ble_set_channels_complete (UINT8 *p)
  1098. {
  1099. tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_ble_channels_cmpl_cb;
  1100. tBTM_BLE_SET_CHANNELS_RESULTS results;
  1101. btu_free_timer (&btm_cb.devcb.ble_channels_timer);
  1102. /* If there is a callback address for setting AFH channels, call it */
  1103. btm_cb.devcb.p_ble_channels_cmpl_cb = NULL;
  1104. if (p_cb) {
  1105. STREAM_TO_UINT8 (results.hci_status, p);
  1106. switch (results.hci_status){
  1107. case HCI_SUCCESS:
  1108. results.status = BTM_SUCCESS;
  1109. break;
  1110. case HCI_ERR_UNSUPPORTED_VALUE:
  1111. case HCI_ERR_ILLEGAL_PARAMETER_FMT:
  1112. results.status = BTM_ILLEGAL_VALUE;
  1113. break;
  1114. default:
  1115. results.status = BTM_ERR_PROCESSING;
  1116. break;
  1117. }
  1118. (*p_cb)(&results);
  1119. }
  1120. }
  1121. #endif /// BLE_INCLUDED == TRUE