btm_devctl.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  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 "bt_trace.h"
  29. #include "bt_types.h"
  30. //#include "bt_utils.h"
  31. #include "btm_int.h"
  32. #include "btu.h"
  33. #include "controller.h"
  34. #include "hci_layer.h"
  35. #include "hcimsgs.h"
  36. #include "l2c_int.h"
  37. //#include "btcore/include/module.h"
  38. //#include "osi/include/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. /* Clear current security state */
  123. for (int devinx = 0; devinx < BTM_SEC_MAX_DEVICE_RECORDS; devinx++) {
  124. btm_cb.sec_dev_rec[devinx].sec_state = BTM_SEC_STATE_IDLE;
  125. }
  126. /* After the reset controller should restore all parameters to defaults. */
  127. btm_cb.btm_inq_vars.inq_counter = 1;
  128. btm_cb.btm_inq_vars.inq_scan_window = HCI_DEF_INQUIRYSCAN_WINDOW;
  129. btm_cb.btm_inq_vars.inq_scan_period = HCI_DEF_INQUIRYSCAN_INTERVAL;
  130. btm_cb.btm_inq_vars.inq_scan_type = HCI_DEF_SCAN_TYPE;
  131. btm_cb.btm_inq_vars.page_scan_window = HCI_DEF_PAGESCAN_WINDOW;
  132. btm_cb.btm_inq_vars.page_scan_period = HCI_DEF_PAGESCAN_INTERVAL;
  133. btm_cb.btm_inq_vars.page_scan_type = HCI_DEF_SCAN_TYPE;
  134. #if (BLE_INCLUDED == TRUE)
  135. btm_cb.ble_ctr_cb.conn_state = BLE_CONN_IDLE;
  136. btm_cb.ble_ctr_cb.bg_conn_type = BTM_BLE_CONN_NONE;
  137. btm_cb.ble_ctr_cb.p_select_cback = NULL;
  138. gatt_reset_bgdev_list();
  139. btm_ble_multi_adv_init();
  140. #endif
  141. btm_pm_reset();
  142. l2c_link_processs_num_bufs(controller->get_acl_buffer_count_classic());
  143. #if (BLE_INCLUDED == TRUE)
  144. #if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == TRUE)
  145. /* Set up the BLE privacy settings */
  146. if (controller->supports_ble() && controller->supports_ble_privacy() &&
  147. controller->get_ble_resolving_list_max_size() > 0) {
  148. btm_ble_resolving_list_init(controller->get_ble_resolving_list_max_size());
  149. /* set the default random private address timeout */
  150. btsnd_hcic_ble_set_rand_priv_addr_timeout(BTM_BLE_PRIVATE_ADDR_INT);
  151. }
  152. #endif
  153. if (controller->supports_ble()) {
  154. btm_ble_white_list_init(controller->get_ble_white_list_size());
  155. l2c_link_processs_ble_num_bufs(controller->get_acl_buffer_count_ble());
  156. }
  157. #endif
  158. BTM_SetPinType (btm_cb.cfg.pin_type, btm_cb.cfg.pin_code, btm_cb.cfg.pin_code_len);
  159. for (int i = 0; i <= controller->get_last_features_classic_index(); i++) {
  160. btm_decode_ext_features_page(i, controller->get_features_classic(i)->as_array);
  161. }
  162. btm_report_device_status(BTM_DEV_STATUS_UP);
  163. }
  164. // TODO(zachoverflow): remove this function
  165. void BTM_DeviceReset (UNUSED_ATTR tBTM_CMPL_CB *p_cb)
  166. {
  167. /* Flush all ACL connections */
  168. btm_acl_device_down();
  169. /* Clear the callback, so application would not hang on reset */
  170. btm_db_reset();
  171. /* todo: review the below logic; start_up executes under another task context
  172. * reset_complete runs in btu task */
  173. controller_get_interface()->start_up();
  174. reset_complete();
  175. }
  176. /*******************************************************************************
  177. **
  178. ** Function BTM_IsDeviceUp
  179. **
  180. ** Description This function is called to check if the device is up.
  181. **
  182. ** Returns TRUE if device is up, else FALSE
  183. **
  184. *******************************************************************************/
  185. BOOLEAN BTM_IsDeviceUp (void)
  186. {
  187. return controller_get_interface()->get_is_ready();
  188. }
  189. /*******************************************************************************
  190. **
  191. ** Function btm_dev_timeout
  192. **
  193. ** Description This function is called when a timer list entry expires.
  194. **
  195. ** Returns void
  196. **
  197. *******************************************************************************/
  198. void btm_dev_timeout (TIMER_LIST_ENT *p_tle)
  199. {
  200. TIMER_PARAM_TYPE timer_type = (TIMER_PARAM_TYPE)p_tle->param;
  201. if (timer_type == (TIMER_PARAM_TYPE)TT_DEV_RLN) {
  202. tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_rln_cmpl_cb;
  203. btm_cb.devcb.p_rln_cmpl_cb = NULL;
  204. if (p_cb) {
  205. (*p_cb)((void *) NULL);
  206. }
  207. }
  208. }
  209. /*******************************************************************************
  210. **
  211. ** Function btm_decode_ext_features_page
  212. **
  213. ** Description This function is decodes a features page.
  214. **
  215. ** Returns void
  216. **
  217. *******************************************************************************/
  218. static void btm_decode_ext_features_page (UINT8 page_number, const UINT8 *p_features)
  219. {
  220. BTM_TRACE_DEBUG ("btm_decode_ext_features_page page: %d", page_number);
  221. switch (page_number) {
  222. /* Extended (Legacy) Page 0 */
  223. case HCI_EXT_FEATURES_PAGE_0:
  224. /* Create ACL supported packet types mask */
  225. btm_cb.btm_acl_pkt_types_supported = (BTM_ACL_PKT_TYPES_MASK_DH1 +
  226. BTM_ACL_PKT_TYPES_MASK_DM1);
  227. if (HCI_3_SLOT_PACKETS_SUPPORTED(p_features))
  228. btm_cb.btm_acl_pkt_types_supported |= (BTM_ACL_PKT_TYPES_MASK_DH3 +
  229. BTM_ACL_PKT_TYPES_MASK_DM3);
  230. if (HCI_5_SLOT_PACKETS_SUPPORTED(p_features))
  231. btm_cb.btm_acl_pkt_types_supported |= (BTM_ACL_PKT_TYPES_MASK_DH5 +
  232. BTM_ACL_PKT_TYPES_MASK_DM5);
  233. /* Add in EDR related ACL types */
  234. if (!HCI_EDR_ACL_2MPS_SUPPORTED(p_features)) {
  235. btm_cb.btm_acl_pkt_types_supported |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH1 +
  236. BTM_ACL_PKT_TYPES_MASK_NO_2_DH3 +
  237. BTM_ACL_PKT_TYPES_MASK_NO_2_DH5);
  238. }
  239. if (!HCI_EDR_ACL_3MPS_SUPPORTED(p_features)) {
  240. btm_cb.btm_acl_pkt_types_supported |= (BTM_ACL_PKT_TYPES_MASK_NO_3_DH1 +
  241. BTM_ACL_PKT_TYPES_MASK_NO_3_DH3 +
  242. BTM_ACL_PKT_TYPES_MASK_NO_3_DH5);
  243. }
  244. /* Check to see if 3 and 5 slot packets are available */
  245. if (HCI_EDR_ACL_2MPS_SUPPORTED(p_features) ||
  246. HCI_EDR_ACL_3MPS_SUPPORTED(p_features)) {
  247. if (!HCI_3_SLOT_EDR_ACL_SUPPORTED(p_features))
  248. btm_cb.btm_acl_pkt_types_supported |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH3 +
  249. BTM_ACL_PKT_TYPES_MASK_NO_3_DH3);
  250. if (!HCI_5_SLOT_EDR_ACL_SUPPORTED(p_features))
  251. btm_cb.btm_acl_pkt_types_supported |= (BTM_ACL_PKT_TYPES_MASK_NO_2_DH5 +
  252. BTM_ACL_PKT_TYPES_MASK_NO_3_DH5);
  253. }
  254. BTM_TRACE_DEBUG("Local supported ACL packet types: 0x%04x",
  255. btm_cb.btm_acl_pkt_types_supported);
  256. /* Create (e)SCO supported packet types mask */
  257. btm_cb.btm_sco_pkt_types_supported = 0;
  258. #if BTM_SCO_INCLUDED == TRUE
  259. btm_cb.sco_cb.esco_supported = FALSE;
  260. #endif
  261. if (HCI_SCO_LINK_SUPPORTED(p_features)) {
  262. btm_cb.btm_sco_pkt_types_supported = BTM_SCO_PKT_TYPES_MASK_HV1;
  263. if (HCI_HV2_PACKETS_SUPPORTED(p_features)) {
  264. btm_cb.btm_sco_pkt_types_supported |= BTM_SCO_PKT_TYPES_MASK_HV2;
  265. }
  266. if (HCI_HV3_PACKETS_SUPPORTED(p_features)) {
  267. btm_cb.btm_sco_pkt_types_supported |= BTM_SCO_PKT_TYPES_MASK_HV3;
  268. }
  269. }
  270. if (HCI_ESCO_EV3_SUPPORTED(p_features)) {
  271. btm_cb.btm_sco_pkt_types_supported |= BTM_SCO_PKT_TYPES_MASK_EV3;
  272. }
  273. if (HCI_ESCO_EV4_SUPPORTED(p_features)) {
  274. btm_cb.btm_sco_pkt_types_supported |= BTM_SCO_PKT_TYPES_MASK_EV4;
  275. }
  276. if (HCI_ESCO_EV5_SUPPORTED(p_features)) {
  277. btm_cb.btm_sco_pkt_types_supported |= BTM_SCO_PKT_TYPES_MASK_EV5;
  278. }
  279. #if BTM_SCO_INCLUDED == TRUE
  280. if (btm_cb.btm_sco_pkt_types_supported & BTM_ESCO_LINK_ONLY_MASK) {
  281. btm_cb.sco_cb.esco_supported = TRUE;
  282. /* Add in EDR related eSCO types */
  283. if (HCI_EDR_ESCO_2MPS_SUPPORTED(p_features)) {
  284. if (!HCI_3_SLOT_EDR_ESCO_SUPPORTED(p_features)) {
  285. btm_cb.btm_sco_pkt_types_supported |= BTM_SCO_PKT_TYPES_MASK_NO_2_EV5;
  286. }
  287. } else {
  288. btm_cb.btm_sco_pkt_types_supported |= (BTM_SCO_PKT_TYPES_MASK_NO_2_EV3 +
  289. BTM_SCO_PKT_TYPES_MASK_NO_2_EV5);
  290. }
  291. if (HCI_EDR_ESCO_3MPS_SUPPORTED(p_features)) {
  292. if (!HCI_3_SLOT_EDR_ESCO_SUPPORTED(p_features)) {
  293. btm_cb.btm_sco_pkt_types_supported |= BTM_SCO_PKT_TYPES_MASK_NO_3_EV5;
  294. }
  295. } else {
  296. btm_cb.btm_sco_pkt_types_supported |= (BTM_SCO_PKT_TYPES_MASK_NO_3_EV3 +
  297. BTM_SCO_PKT_TYPES_MASK_NO_3_EV5);
  298. }
  299. }
  300. #endif
  301. BTM_TRACE_DEBUG("Local supported SCO packet types: 0x%04x",
  302. btm_cb.btm_sco_pkt_types_supported);
  303. /* Create Default Policy Settings */
  304. if (HCI_SWITCH_SUPPORTED(p_features)) {
  305. btm_cb.btm_def_link_policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
  306. } else {
  307. btm_cb.btm_def_link_policy &= ~HCI_ENABLE_MASTER_SLAVE_SWITCH;
  308. }
  309. if (HCI_HOLD_MODE_SUPPORTED(p_features)) {
  310. btm_cb.btm_def_link_policy |= HCI_ENABLE_HOLD_MODE;
  311. } else {
  312. btm_cb.btm_def_link_policy &= ~HCI_ENABLE_HOLD_MODE;
  313. }
  314. if (HCI_SNIFF_MODE_SUPPORTED(p_features)) {
  315. btm_cb.btm_def_link_policy |= HCI_ENABLE_SNIFF_MODE;
  316. } else {
  317. btm_cb.btm_def_link_policy &= ~HCI_ENABLE_SNIFF_MODE;
  318. }
  319. if (HCI_PARK_MODE_SUPPORTED(p_features)) {
  320. btm_cb.btm_def_link_policy |= HCI_ENABLE_PARK_MODE;
  321. } else {
  322. btm_cb.btm_def_link_policy &= ~HCI_ENABLE_PARK_MODE;
  323. }
  324. btm_sec_dev_reset ();
  325. if (HCI_LMP_INQ_RSSI_SUPPORTED(p_features)) {
  326. if (HCI_EXT_INQ_RSP_SUPPORTED(p_features)) {
  327. BTM_SetInquiryMode (BTM_INQ_RESULT_EXTENDED);
  328. } else {
  329. BTM_SetInquiryMode (BTM_INQ_RESULT_WITH_RSSI);
  330. }
  331. }
  332. #if L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE
  333. if ( HCI_NON_FLUSHABLE_PB_SUPPORTED(p_features)) {
  334. l2cu_set_non_flushable_pbf(TRUE);
  335. } else {
  336. l2cu_set_non_flushable_pbf(FALSE);
  337. }
  338. #endif
  339. BTM_SetPageScanType (BTM_DEFAULT_SCAN_TYPE);
  340. BTM_SetInquiryScanType (BTM_DEFAULT_SCAN_TYPE);
  341. break;
  342. /* Extended Page 1 */
  343. case HCI_EXT_FEATURES_PAGE_1:
  344. /* Nothing to do for page 1 */
  345. break;
  346. /* Extended Page 2 */
  347. case HCI_EXT_FEATURES_PAGE_2:
  348. /* Nothing to do for page 2 */
  349. break;
  350. default:
  351. BTM_TRACE_ERROR("btm_decode_ext_features_page page=%d unknown", page_number);
  352. break;
  353. }
  354. }
  355. /*******************************************************************************
  356. **
  357. ** Function BTM_SetLocalDeviceName
  358. **
  359. ** Description This function is called to set the local device name.
  360. **
  361. ** Returns status of the operation
  362. **
  363. *******************************************************************************/
  364. tBTM_STATUS BTM_SetLocalDeviceName (char *p_name)
  365. {
  366. UINT8 *p;
  367. if (!p_name || !p_name[0] || (strlen ((char *)p_name) > BD_NAME_LEN)) {
  368. return (BTM_ILLEGAL_VALUE);
  369. }
  370. if (!controller_get_interface()->get_is_ready()) {
  371. return (BTM_DEV_RESET);
  372. }
  373. #if BTM_MAX_LOC_BD_NAME_LEN > 0
  374. /* Save the device name if local storage is enabled */
  375. p = (UINT8 *)btm_cb.cfg.bd_name;
  376. if (p != (UINT8 *)p_name) {
  377. BCM_STRNCPY_S(btm_cb.cfg.bd_name, sizeof(btm_cb.cfg.bd_name), p_name, BTM_MAX_LOC_BD_NAME_LEN);
  378. btm_cb.cfg.bd_name[BTM_MAX_LOC_BD_NAME_LEN] = '\0';
  379. }
  380. #else
  381. p = (UINT8 *)p_name;
  382. #endif
  383. if (btsnd_hcic_change_name(p)) {
  384. return (BTM_CMD_STARTED);
  385. } else {
  386. return (BTM_NO_RESOURCES);
  387. }
  388. }
  389. /*******************************************************************************
  390. **
  391. ** Function BTM_ReadLocalDeviceName
  392. **
  393. ** Description This function is called to read the local device name.
  394. **
  395. ** Returns status of the operation
  396. ** If success, BTM_SUCCESS is returned and p_name points stored
  397. ** local device name
  398. ** If BTM doesn't store local device name, BTM_NO_RESOURCES is
  399. ** is returned and p_name is set to NULL
  400. **
  401. *******************************************************************************/
  402. tBTM_STATUS BTM_ReadLocalDeviceName (char **p_name)
  403. {
  404. #if BTM_MAX_LOC_BD_NAME_LEN > 0
  405. *p_name = btm_cb.cfg.bd_name;
  406. return (BTM_SUCCESS);
  407. #else
  408. *p_name = NULL;
  409. return (BTM_NO_RESOURCES);
  410. #endif
  411. }
  412. /*******************************************************************************
  413. **
  414. ** Function BTM_ReadLocalDeviceNameFromController
  415. **
  416. ** Description Get local device name from controller. Do not use cached
  417. ** name (used to get chip-id prior to btm reset complete).
  418. **
  419. ** Returns BTM_CMD_STARTED if successful, otherwise an error
  420. **
  421. *******************************************************************************/
  422. tBTM_STATUS BTM_ReadLocalDeviceNameFromController (tBTM_CMPL_CB *p_rln_cmpl_cback)
  423. {
  424. /* Check if rln already in progress */
  425. if (btm_cb.devcb.p_rln_cmpl_cb) {
  426. return (BTM_NO_RESOURCES);
  427. }
  428. /* Save callback */
  429. btm_cb.devcb.p_rln_cmpl_cb = p_rln_cmpl_cback;
  430. btsnd_hcic_read_name();
  431. btu_start_timer (&btm_cb.devcb.rln_timer, BTU_TTYPE_BTM_DEV_CTL, BTM_DEV_REPLY_TIMEOUT);
  432. return BTM_CMD_STARTED;
  433. }
  434. /*******************************************************************************
  435. **
  436. ** Function btm_read_local_name_complete
  437. **
  438. ** Description This function is called when local name read complete.
  439. ** message is received from the HCI.
  440. **
  441. ** Returns void
  442. **
  443. *******************************************************************************/
  444. void btm_read_local_name_complete (UINT8 *p, UINT16 evt_len)
  445. {
  446. tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_rln_cmpl_cb;
  447. UINT8 status;
  448. UNUSED(evt_len);
  449. btu_stop_timer (&btm_cb.devcb.rln_timer);
  450. /* If there was a callback address for read local name, call it */
  451. btm_cb.devcb.p_rln_cmpl_cb = NULL;
  452. if (p_cb) {
  453. STREAM_TO_UINT8 (status, p);
  454. if (status == HCI_SUCCESS) {
  455. (*p_cb)(p);
  456. } else {
  457. (*p_cb)(NULL);
  458. }
  459. }
  460. }
  461. /*******************************************************************************
  462. **
  463. ** Function BTM_SetDeviceClass
  464. **
  465. ** Description This function is called to set the local device class
  466. **
  467. ** Returns status of the operation
  468. **
  469. *******************************************************************************/
  470. tBTM_STATUS BTM_SetDeviceClass (DEV_CLASS dev_class)
  471. {
  472. if (!memcmp (btm_cb.devcb.dev_class, dev_class, DEV_CLASS_LEN)) {
  473. return (BTM_SUCCESS);
  474. }
  475. memcpy (btm_cb.devcb.dev_class, dev_class, DEV_CLASS_LEN);
  476. if (!controller_get_interface()->get_is_ready()) {
  477. return (BTM_DEV_RESET);
  478. }
  479. if (!btsnd_hcic_write_dev_class (dev_class)) {
  480. return (BTM_NO_RESOURCES);
  481. }
  482. return (BTM_SUCCESS);
  483. }
  484. /*******************************************************************************
  485. **
  486. ** Function BTM_ReadDeviceClass
  487. **
  488. ** Description This function is called to read the local device class
  489. **
  490. ** Returns pointer to the device class
  491. **
  492. *******************************************************************************/
  493. UINT8 *BTM_ReadDeviceClass (void)
  494. {
  495. return ((UINT8 *)btm_cb.devcb.dev_class);
  496. }
  497. /*******************************************************************************
  498. **
  499. ** Function BTM_ReadLocalFeatures
  500. **
  501. ** Description This function is called to read the local features
  502. **
  503. ** Returns pointer to the local features string
  504. **
  505. *******************************************************************************/
  506. // TODO(zachoverflow): get rid of this function
  507. UINT8 *BTM_ReadLocalFeatures (void)
  508. {
  509. // Discarding const modifier for now, until this function dies
  510. return (UINT8 *)controller_get_interface()->get_features_classic(0)->as_array;
  511. }
  512. /*******************************************************************************
  513. **
  514. ** Function BTM_RegisterForDeviceStatusNotif
  515. **
  516. ** Description This function is called to register for device status
  517. ** change notifications.
  518. **
  519. ** If one registration is already there calling function should
  520. ** save the pointer to the function that is return and
  521. ** call it when processing of the event is complete
  522. **
  523. ** Returns status of the operation
  524. **
  525. *******************************************************************************/
  526. tBTM_DEV_STATUS_CB *BTM_RegisterForDeviceStatusNotif (tBTM_DEV_STATUS_CB *p_cb)
  527. {
  528. tBTM_DEV_STATUS_CB *p_prev = btm_cb.devcb.p_dev_status_cb;
  529. btm_cb.devcb.p_dev_status_cb = p_cb;
  530. return (p_prev);
  531. }
  532. /*******************************************************************************
  533. **
  534. ** Function BTM_VendorSpecificCommand
  535. **
  536. ** Description Send a vendor specific HCI command to the controller.
  537. **
  538. ** Returns
  539. ** BTM_SUCCESS Command sent. Does not expect command complete
  540. ** event. (command cmpl callback param is NULL)
  541. ** BTM_CMD_STARTED Command sent. Waiting for command cmpl event.
  542. **
  543. ** Notes
  544. ** Opcode will be OR'd with HCI_GRP_VENDOR_SPECIFIC.
  545. **
  546. *******************************************************************************/
  547. tBTM_STATUS BTM_VendorSpecificCommand(UINT16 opcode, UINT8 param_len,
  548. UINT8 *p_param_buf, tBTM_VSC_CMPL_CB *p_cb)
  549. {
  550. void *p_buf;
  551. BTM_TRACE_EVENT ("BTM: BTM_VendorSpecificCommand: Opcode: 0x%04X, ParamLen: %i.",
  552. opcode, param_len);
  553. /* Allocate a buffer to hold HCI command plus the callback function */
  554. if ((p_buf = GKI_getbuf((UINT16)(sizeof(BT_HDR) + sizeof (tBTM_CMPL_CB *) +
  555. param_len + HCIC_PREAMBLE_SIZE))) != NULL) {
  556. /* Send the HCI command (opcode will be OR'd with HCI_GRP_VENDOR_SPECIFIC) */
  557. btsnd_hcic_vendor_spec_cmd (p_buf, opcode, param_len, p_param_buf, (void *)p_cb);
  558. /* Return value */
  559. if (p_cb != NULL) {
  560. return (BTM_CMD_STARTED);
  561. } else {
  562. return (BTM_SUCCESS);
  563. }
  564. } else {
  565. return (BTM_NO_RESOURCES);
  566. }
  567. }
  568. /*******************************************************************************
  569. **
  570. ** Function btm_vsc_complete
  571. **
  572. ** Description This function is called when local HCI Vendor Specific
  573. ** Command complete message is received from the HCI.
  574. **
  575. ** Returns void
  576. **
  577. *******************************************************************************/
  578. void btm_vsc_complete (UINT8 *p, UINT16 opcode, UINT16 evt_len,
  579. tBTM_CMPL_CB *p_vsc_cplt_cback)
  580. {
  581. tBTM_VSC_CMPL vcs_cplt_params;
  582. /* If there was a callback address for vcs complete, call it */
  583. if (p_vsc_cplt_cback) {
  584. /* Pass paramters to the callback function */
  585. vcs_cplt_params.opcode = opcode; /* Number of bytes in return info */
  586. vcs_cplt_params.param_len = evt_len; /* Number of bytes in return info */
  587. vcs_cplt_params.p_param_buf = p;
  588. (*p_vsc_cplt_cback)(&vcs_cplt_params); /* Call the VSC complete callback function */
  589. }
  590. }
  591. /*******************************************************************************
  592. **
  593. ** Function BTM_RegisterForVSEvents
  594. **
  595. ** Description This function is called to register/deregister for vendor
  596. ** specific HCI events.
  597. **
  598. ** If is_register=TRUE, then the function will be registered;
  599. ** if is_register=FALSE, then the function will be deregistered.
  600. **
  601. ** Returns BTM_SUCCESS if successful,
  602. ** BTM_BUSY if maximum number of callbacks have already been
  603. ** registered.
  604. **
  605. *******************************************************************************/
  606. tBTM_STATUS BTM_RegisterForVSEvents (tBTM_VS_EVT_CB *p_cb, BOOLEAN is_register)
  607. {
  608. tBTM_STATUS retval = BTM_SUCCESS;
  609. UINT8 i, free_idx = BTM_MAX_VSE_CALLBACKS;
  610. /* See if callback is already registered */
  611. for (i = 0; i < BTM_MAX_VSE_CALLBACKS; i++) {
  612. if (btm_cb.devcb.p_vend_spec_cb[i] == NULL) {
  613. /* Found a free slot. Store index */
  614. free_idx = i;
  615. } else if (btm_cb.devcb.p_vend_spec_cb[i] == p_cb) {
  616. /* Found callback in lookup table. If deregistering, clear the entry. */
  617. if (is_register == FALSE) {
  618. btm_cb.devcb.p_vend_spec_cb[i] = NULL;
  619. BTM_TRACE_EVENT("BTM Deregister For VSEvents is successfully");
  620. }
  621. return (BTM_SUCCESS);
  622. }
  623. }
  624. /* Didn't find callback. Add callback to free slot if registering */
  625. if (is_register) {
  626. if (free_idx < BTM_MAX_VSE_CALLBACKS) {
  627. btm_cb.devcb.p_vend_spec_cb[free_idx] = p_cb;
  628. BTM_TRACE_EVENT("BTM Register For VSEvents is successfully");
  629. } else {
  630. /* No free entries available */
  631. BTM_TRACE_ERROR ("BTM_RegisterForVSEvents: too many callbacks registered");
  632. retval = BTM_NO_RESOURCES;
  633. }
  634. }
  635. return (retval);
  636. }
  637. /*******************************************************************************
  638. **
  639. ** Function btm_vendor_specific_evt
  640. **
  641. ** Description Process event HCI_VENDOR_SPECIFIC_EVT
  642. **
  643. ** Note: Some controllers do not send command complete, so
  644. ** the callback and busy flag are cleared here also.
  645. **
  646. ** Returns void
  647. **
  648. *******************************************************************************/
  649. void btm_vendor_specific_evt (UINT8 *p, UINT8 evt_len)
  650. {
  651. UINT8 i;
  652. BTM_TRACE_DEBUG ("BTM Event: Vendor Specific event from controller");
  653. for (i = 0; i < BTM_MAX_VSE_CALLBACKS; i++) {
  654. if (btm_cb.devcb.p_vend_spec_cb[i]) {
  655. (*btm_cb.devcb.p_vend_spec_cb[i])(evt_len, p);
  656. }
  657. }
  658. }
  659. /*******************************************************************************
  660. **
  661. ** Function BTM_WritePageTimeout
  662. **
  663. ** Description Send HCI Write Page Timeout.
  664. **
  665. ** Returns
  666. ** BTM_SUCCESS Command sent.
  667. ** BTM_NO_RESOURCES If out of resources to send the command.
  668. **
  669. **
  670. *******************************************************************************/
  671. tBTM_STATUS BTM_WritePageTimeout(UINT16 timeout)
  672. {
  673. BTM_TRACE_EVENT ("BTM: BTM_WritePageTimeout: Timeout: %d.", timeout);
  674. /* Send the HCI command */
  675. if (btsnd_hcic_write_page_tout (timeout)) {
  676. return (BTM_SUCCESS);
  677. } else {
  678. return (BTM_NO_RESOURCES);
  679. }
  680. }
  681. /*******************************************************************************
  682. **
  683. ** Function BTM_WriteVoiceSettings
  684. **
  685. ** Description Send HCI Write Voice Settings command.
  686. ** See hcidefs.h for settings bitmask values.
  687. **
  688. ** Returns
  689. ** BTM_SUCCESS Command sent.
  690. ** BTM_NO_RESOURCES If out of resources to send the command.
  691. **
  692. **
  693. *******************************************************************************/
  694. tBTM_STATUS BTM_WriteVoiceSettings(UINT16 settings)
  695. {
  696. BTM_TRACE_EVENT ("BTM: BTM_WriteVoiceSettings: Settings: 0x%04x.", settings);
  697. /* Send the HCI command */
  698. if (btsnd_hcic_write_voice_settings ((UINT16)(settings & 0x03ff))) {
  699. return (BTM_SUCCESS);
  700. }
  701. return (BTM_NO_RESOURCES);
  702. }
  703. /*******************************************************************************
  704. **
  705. ** Function BTM_EnableTestMode
  706. **
  707. ** Description Send HCI the enable device under test command.
  708. **
  709. ** Note: Controller can only be taken out of this mode by
  710. ** resetting the controller.
  711. **
  712. ** Returns
  713. ** BTM_SUCCESS Command sent.
  714. ** BTM_NO_RESOURCES If out of resources to send the command.
  715. **
  716. **
  717. *******************************************************************************/
  718. tBTM_STATUS BTM_EnableTestMode(void)
  719. {
  720. UINT8 cond;
  721. BTM_TRACE_EVENT ("BTM: BTM_EnableTestMode");
  722. /* set auto accept connection as this is needed during test mode */
  723. /* Allocate a buffer to hold HCI command */
  724. cond = HCI_DO_AUTO_ACCEPT_CONNECT;
  725. if (!btsnd_hcic_set_event_filter(HCI_FILTER_CONNECTION_SETUP,
  726. HCI_FILTER_COND_NEW_DEVICE,
  727. &cond, sizeof(cond))) {
  728. return (BTM_NO_RESOURCES);
  729. }
  730. /* put device to connectable mode */
  731. if (!BTM_SetConnectability(BTM_CONNECTABLE, BTM_DEFAULT_CONN_WINDOW,
  732. BTM_DEFAULT_CONN_INTERVAL) == BTM_SUCCESS) {
  733. return BTM_NO_RESOURCES;
  734. }
  735. /* put device to discoverable mode */
  736. if (!BTM_SetDiscoverability(BTM_GENERAL_DISCOVERABLE, BTM_DEFAULT_DISC_WINDOW,
  737. BTM_DEFAULT_DISC_INTERVAL) == BTM_SUCCESS) {
  738. return BTM_NO_RESOURCES;
  739. }
  740. /* mask off all of event from controller */
  741. hci_layer_get_interface()->transmit_command(
  742. hci_packet_factory_get_interface()->make_set_event_mask((const bt_event_mask_t *)("\x00\x00\x00\x00\x00\x00\x00\x00")),
  743. NULL,
  744. NULL,
  745. NULL);
  746. /* Send the HCI command */
  747. if (btsnd_hcic_enable_test_mode ()) {
  748. return (BTM_SUCCESS);
  749. } else {
  750. return (BTM_NO_RESOURCES);
  751. }
  752. }
  753. /*******************************************************************************
  754. **
  755. ** Function BTM_DeleteStoredLinkKey
  756. **
  757. ** Description This function is called to delete link key for the specified
  758. ** device addresses from the NVRAM storage attached to the Bluetooth
  759. ** controller.
  760. **
  761. ** Parameters: bd_addr - Addresses of the devices
  762. ** p_cb - Call back function to be called to return
  763. ** the results
  764. **
  765. *******************************************************************************/
  766. tBTM_STATUS BTM_DeleteStoredLinkKey(BD_ADDR bd_addr, tBTM_CMPL_CB *p_cb)
  767. {
  768. BD_ADDR local_bd_addr;
  769. BOOLEAN delete_all_flag = FALSE;
  770. /* Check if the previous command is completed */
  771. if (btm_cb.devcb.p_stored_link_key_cmpl_cb) {
  772. return (BTM_BUSY);
  773. }
  774. if (!bd_addr) {
  775. /* This is to delete all link keys */
  776. delete_all_flag = TRUE;
  777. /* We don't care the BD address. Just pass a non zero pointer */
  778. bd_addr = local_bd_addr;
  779. }
  780. BTM_TRACE_EVENT ("BTM: BTM_DeleteStoredLinkKey: delete_all_flag: %s",
  781. delete_all_flag ? "TRUE" : "FALSE");
  782. /* Send the HCI command */
  783. btm_cb.devcb.p_stored_link_key_cmpl_cb = p_cb;
  784. if (!btsnd_hcic_delete_stored_key (bd_addr, delete_all_flag)) {
  785. return (BTM_NO_RESOURCES);
  786. } else {
  787. return (BTM_SUCCESS);
  788. }
  789. }
  790. /*******************************************************************************
  791. **
  792. ** Function btm_delete_stored_link_key_complete
  793. **
  794. ** Description This function is called when the command complete message
  795. ** is received from the HCI for the delete stored link key command.
  796. **
  797. ** Returns void
  798. **
  799. *******************************************************************************/
  800. void btm_delete_stored_link_key_complete (UINT8 *p)
  801. {
  802. tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_stored_link_key_cmpl_cb;
  803. tBTM_DELETE_STORED_LINK_KEY_COMPLETE result;
  804. /* If there was a callback registered for read stored link key, call it */
  805. btm_cb.devcb.p_stored_link_key_cmpl_cb = NULL;
  806. if (p_cb) {
  807. /* Set the call back event to indicate command complete */
  808. result.event = BTM_CB_EVT_DELETE_STORED_LINK_KEYS;
  809. /* Extract the result fields from the HCI event */
  810. STREAM_TO_UINT8 (result.status, p);
  811. STREAM_TO_UINT16 (result.num_keys, p);
  812. /* Call the call back and pass the result */
  813. (*p_cb)(&result);
  814. }
  815. }
  816. /*******************************************************************************
  817. **
  818. ** Function btm_report_device_status
  819. **
  820. ** Description This function is called when there is a change in the device
  821. ** status. This function will report the new device status to
  822. ** the application
  823. **
  824. ** Returns void
  825. **
  826. *******************************************************************************/
  827. void btm_report_device_status (tBTM_DEV_STATUS status)
  828. {
  829. tBTM_DEV_STATUS_CB *p_cb = btm_cb.devcb.p_dev_status_cb;
  830. /* Call the call back to pass the device status to application */
  831. if (p_cb) {
  832. (*p_cb)(status);
  833. }
  834. }