bta_dm_co.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2009-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. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include "bta/bta_api.h"
  21. #include "bta/bta_sys.h"
  22. #include "bta/bta_dm_co.h"
  23. #include "bta/bta_dm_ci.h"
  24. #include "btc/btc_dm.h"
  25. #if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE)
  26. #include "common/bt_defs.h"
  27. #if (BTM_OOB_INCLUDED == TRUE)
  28. #include "btif_dm.h"
  29. #endif
  30. #endif /* #if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE) */
  31. #if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE)
  32. #include "common/bte_appl.h"
  33. #define BTM_BLE_ONLY_ACCEPT_SPECIFIED_SEC_AUTH_DISABLE 0
  34. #define BTM_BLE_ONLY_ACCEPT_SPECIFIED_SEC_AUTH_ENABLE 1
  35. #define BTM_BLE_OOB_DISABLE 0
  36. #define BTM_BLE_OOB_ENABLE 1
  37. tBTE_APPL_CFG bte_appl_cfg = {
  38. #if SMP_INCLUDED == TRUE
  39. BTA_LE_AUTH_REQ_SC_MITM_BOND, // Authentication requirements
  40. #else
  41. BTM_AUTH_SPGB_YES, // Authentication requirements
  42. #endif
  43. BTM_LOCAL_IO_CAPS_BLE,
  44. BTM_BLE_INITIATOR_KEY_SIZE,
  45. BTM_BLE_RESPONDER_KEY_SIZE,
  46. BTM_BLE_MAX_KEY_SIZE,
  47. BTM_BLE_MIN_KEY_SIZE,
  48. BTM_BLE_ONLY_ACCEPT_SPECIFIED_SEC_AUTH_DISABLE,
  49. BTM_BLE_OOB_DISABLE,
  50. BTM_BLE_APPL_ENC_KEY_SIZE,
  51. };
  52. #endif
  53. #if (defined CLASSIC_BT_INCLUDED && CLASSIC_BT_INCLUDED == TRUE && BT_SSP_INCLUDED == TRUE)
  54. #include "common/bte_appl.h"
  55. #include "btm_int.h"
  56. tBTE_BT_APPL_CFG bte_bt_appl_cfg = {
  57. 0, //Todo, Authentication requirements
  58. BTM_LOCAL_IO_CAPS,
  59. NULL, //Todo, OOB data
  60. };
  61. #endif
  62. /*******************************************************************************
  63. **
  64. ** Function bta_dm_co_get_compress_memory
  65. **
  66. ** Description This callout function is executed by DM to get memory for compression
  67. ** Parameters id - BTA SYS ID
  68. ** memory_p - memory return by callout
  69. ** memory_size - memory size
  70. **
  71. ** Returns TRUE for success, FALSE for fail.
  72. **
  73. *******************************************************************************/
  74. BOOLEAN bta_dm_co_get_compress_memory(tBTA_SYS_ID id, UINT8 **memory_p, UINT32 *memory_size)
  75. {
  76. UNUSED(id);
  77. UNUSED(memory_p);
  78. UNUSED(memory_size);
  79. return TRUE;
  80. }
  81. /*******************************************************************************
  82. **
  83. ** Function bta_dm_co_bt_set_io_cap
  84. **
  85. ** Description This function is used to set IO capabilities
  86. **
  87. ** Parameters bt_io_cap - IO capabilities
  88. **
  89. ** @return - ESP_BT_STATUS_SUCCESS : success
  90. ** - other : failed
  91. **
  92. *******************************************************************************/
  93. esp_err_t bta_dm_co_bt_set_io_cap(UINT8 bt_io_cap)
  94. {
  95. esp_err_t ret = ESP_BT_STATUS_SUCCESS;
  96. #if (BT_SSP_INCLUDED == TRUE)
  97. if(bt_io_cap < BTM_IO_CAP_MAX ) {
  98. bte_bt_appl_cfg.bt_io_cap = bt_io_cap;
  99. btm_cb.devcb.loc_io_caps = bt_io_cap;
  100. ret = ESP_BT_STATUS_SUCCESS;
  101. } else {
  102. ret = ESP_BT_STATUS_FAIL;
  103. APPL_TRACE_ERROR("%s error:Invalid io cap value.",__func__);
  104. }
  105. #endif ///BT_SSP_INCLUDED == TRUE
  106. return ret;
  107. }
  108. /*******************************************************************************
  109. **
  110. ** Function bta_dm_co_io_req
  111. **
  112. ** Description This callout function is executed by DM to get IO capabilities
  113. ** of the local device for the Simple Pairing process
  114. **
  115. ** Parameters bd_addr - The peer device
  116. ** *p_io_cap - The local Input/Output capabilities
  117. ** *p_oob_data - TRUE, if OOB data is available for the peer device.
  118. ** *p_auth_req - TRUE, if MITM protection is required.
  119. **
  120. ** Returns void.
  121. **
  122. *******************************************************************************/
  123. void bta_dm_co_io_req(BD_ADDR bd_addr, tBTA_IO_CAP *p_io_cap, tBTA_OOB_DATA *p_oob_data,
  124. tBTA_AUTH_REQ *p_auth_req, BOOLEAN is_orig)
  125. {
  126. UNUSED(bd_addr);
  127. #if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE)
  128. #if (BTM_OOB_INCLUDED == TRUE)
  129. btif_dm_set_oob_for_io_req(p_oob_data);
  130. #endif
  131. btif_dm_proc_io_req(bd_addr, p_io_cap, p_oob_data, p_auth_req, is_orig);
  132. #else
  133. BTIF_TRACE_DEBUG("bta_dm_co_io_req: func not ported\n");
  134. #endif /* #if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE) */
  135. BTIF_TRACE_DEBUG("bta_dm_co_io_req *p_oob_data = %d", *p_oob_data);
  136. BTIF_TRACE_DEBUG("bta_dm_co_io_req *p_io_cap = %d", *p_io_cap);
  137. BTIF_TRACE_DEBUG("bta_dm_co_io_req *p_auth_req = %d", *p_auth_req);
  138. BTIF_TRACE_DEBUG("bta_dm_co_io_req is_orig = %d", is_orig);
  139. }
  140. /*******************************************************************************
  141. **
  142. ** Function bta_dm_co_io_rsp
  143. **
  144. ** Description This callout function is executed by DM to report IO capabilities
  145. ** of the peer device for the Simple Pairing process
  146. **
  147. ** Parameters bd_addr - The peer device
  148. ** io_cap - The remote Input/Output capabilities
  149. ** oob_data - TRUE, if OOB data is available for the peer device.
  150. ** auth_req - TRUE, if MITM protection is required.
  151. **
  152. ** Returns void.
  153. **
  154. *******************************************************************************/
  155. void bta_dm_co_io_rsp(BD_ADDR bd_addr, tBTA_IO_CAP io_cap,
  156. tBTA_OOB_DATA oob_data, tBTA_AUTH_REQ auth_req)
  157. {
  158. #if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE)
  159. btif_dm_proc_io_rsp(bd_addr, io_cap, oob_data, auth_req);
  160. #else
  161. BTIF_TRACE_DEBUG("bta_dm_co_io_rsp: func not ported\n");
  162. #endif /* #if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE) */
  163. }
  164. /*******************************************************************************
  165. **
  166. ** Function bta_dm_co_lk_upgrade
  167. **
  168. ** Description This callout function is executed by DM to check if the
  169. ** platform wants allow link key upgrade
  170. **
  171. ** Parameters bd_addr - The peer device
  172. ** *p_upgrade - TRUE, if link key upgrade is desired.
  173. **
  174. ** Returns void.
  175. **
  176. *******************************************************************************/
  177. void bta_dm_co_lk_upgrade(BD_ADDR bd_addr, BOOLEAN *p_upgrade )
  178. {
  179. UNUSED(bd_addr);
  180. UNUSED(p_upgrade);
  181. }
  182. #if (BTM_OOB_INCLUDED == TRUE)
  183. /*******************************************************************************
  184. **
  185. ** Function bta_dm_co_loc_oob
  186. **
  187. ** Description This callout function is executed by DM to report the OOB
  188. ** data of the local device for the Simple Pairing process
  189. **
  190. ** Parameters valid - TRUE, if the local OOB data is retrieved from LM
  191. ** c - Simple Pairing Hash C
  192. ** r - Simple Pairing Randomnizer R
  193. **
  194. ** Returns void.
  195. **
  196. *******************************************************************************/
  197. void bta_dm_co_loc_oob(BOOLEAN valid, BT_OCTET16 c, BT_OCTET16 r)
  198. {
  199. #if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE)
  200. BTIF_TRACE_DEBUG("bta_dm_co_loc_oob, valid = %d", valid);
  201. #ifdef BTIF_DM_OOB_TEST
  202. btif_dm_proc_loc_oob(valid, c, r);
  203. #endif
  204. #else
  205. BTIF_TRACE_DEBUG("bta_dm_co_loc_oob: func not ported\n");
  206. #endif /* #if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE) */
  207. }
  208. /*******************************************************************************
  209. **
  210. ** Function bta_dm_co_rmt_oob
  211. **
  212. ** Description This callout function is executed by DM to request the OOB
  213. ** data for the remote device for the Simple Pairing process
  214. ** Need to call bta_dm_ci_rmt_oob() in response
  215. **
  216. ** Parameters bd_addr - The peer device
  217. **
  218. ** Returns void.
  219. **
  220. *******************************************************************************/
  221. void bta_dm_co_rmt_oob(BD_ADDR bd_addr)
  222. {
  223. BT_OCTET16 p_c;
  224. BT_OCTET16 p_r;
  225. BOOLEAN result = FALSE;
  226. #ifdef BTIF_DM_OOB_TEST
  227. #if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE)
  228. result = btif_dm_proc_rmt_oob(bd_addr, p_c, p_r);
  229. #else
  230. BTIF_TRACE_DEBUG("bta_dm_rmt_oob: func not ported\n");
  231. #endif /* #if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE) */
  232. #endif
  233. BTIF_TRACE_DEBUG("bta_dm_co_rmt_oob: result=%d", result);
  234. bta_dm_ci_rmt_oob(result, bd_addr, p_c, p_r);
  235. }
  236. #endif /* BTM_OOB_INCLUDED */
  237. // REMOVE FOR BLUEDROID ?
  238. #if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE)
  239. /*******************************************************************************
  240. **
  241. ** Function bta_dm_co_le_io_key_req
  242. **
  243. ** Description This callout function is executed by DM to get BLE key information
  244. ** before SMP pairing gets going.
  245. **
  246. ** Parameters bd_addr - The peer device
  247. ** *p_max_key_size - max key size local device supported.
  248. ** *p_init_key - initiator keys.
  249. ** *p_resp_key - responder keys.
  250. **
  251. ** Returns void.
  252. **
  253. *******************************************************************************/
  254. void bta_dm_co_le_io_key_req(BD_ADDR bd_addr, UINT8 *p_max_key_size,
  255. tBTA_LE_KEY_TYPE *p_init_key,
  256. tBTA_LE_KEY_TYPE *p_resp_key )
  257. {
  258. UNUSED(bd_addr);
  259. #if (SMP_INCLUDED == TRUE)
  260. BTIF_TRACE_DEBUG("##################################");
  261. BTIF_TRACE_DEBUG("bta_dm_co_le_io_key_req: only setting max size to 16");
  262. BTIF_TRACE_DEBUG("##################################");
  263. *p_max_key_size = 16;
  264. *p_init_key = *p_resp_key =
  265. (BTA_LE_KEY_PENC | BTA_LE_KEY_PID | BTA_LE_KEY_PCSRK | BTA_LE_KEY_LENC | BTA_LE_KEY_LID | BTA_LE_KEY_LCSRK);
  266. #endif ///SMP_INCLUDED == TRUE
  267. }
  268. /*******************************************************************************
  269. **
  270. ** Function bta_dm_co_ble_local_key_reload
  271. **
  272. ** Description This callout function is to load the local BLE keys if available
  273. ** on the device.
  274. **
  275. ** Parameters none
  276. **
  277. ** Returns void.
  278. **
  279. *******************************************************************************/
  280. void bta_dm_co_ble_load_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er,
  281. tBTA_BLE_LOCAL_ID_KEYS *p_id_keys)
  282. {
  283. #if (defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE)
  284. BTIF_TRACE_DEBUG("##################################");
  285. BTIF_TRACE_DEBUG("bta_dm_co_ble_load_local_keys: Load local keys if any are persisted");
  286. BTIF_TRACE_DEBUG("##################################");
  287. btif_dm_get_ble_local_keys( p_key_mask, er, p_id_keys);
  288. #endif ///defined(BTIF_INCLUDED) && BTIF_INCLUDED == TRUE
  289. #if (SMP_INCLUDED == TRUE)
  290. btc_dm_get_ble_local_keys( p_key_mask, er, p_id_keys);
  291. #endif ///SMP_INCLUDED == TRUE
  292. }
  293. /*******************************************************************************
  294. **
  295. ** Function bta_dm_co_ble_io_req
  296. **
  297. ** Description This callout function is executed by DM to get BLE IO capabilities
  298. ** before SMP pairing gets going.
  299. **
  300. ** Parameters bd_addr - The peer device
  301. ** *p_io_cap - The local Input/Output capabilities
  302. ** *p_oob_data - TRUE, if OOB data is available for the peer device.
  303. ** *p_auth_req - Auth request setting (Bonding and MITM required or not)
  304. ** *p_max_key_size - max key size local device supported.
  305. ** *p_init_key - initiator keys.
  306. ** *p_resp_key - responder keys.
  307. **
  308. ** Returns void.
  309. **
  310. *******************************************************************************/
  311. void bta_dm_co_ble_io_req(BD_ADDR bd_addr, tBTA_IO_CAP *p_io_cap,
  312. tBTA_OOB_DATA *p_oob_data,
  313. tBTA_LE_AUTH_REQ *p_auth_req,
  314. UINT8 *p_max_key_size,
  315. tBTA_LE_KEY_TYPE *p_init_key,
  316. tBTA_LE_KEY_TYPE *p_resp_key )
  317. {
  318. #if (SMP_INCLUDED == TRUE)
  319. UNUSED(bd_addr);
  320. /* if OOB is not supported, this call-out function does not need to do anything
  321. * otherwise, look for the OOB data associated with the address and set *p_oob_data accordingly
  322. * If the answer can not be obtained right away,
  323. * set *p_oob_data to BTA_OOB_UNKNOWN and call bta_dm_ci_io_req() when the answer is available */
  324. *p_oob_data = bte_appl_cfg.oob_support;
  325. /* *p_auth_req by default is FALSE for devices with NoInputNoOutput; TRUE for other devices. */
  326. *p_auth_req = bte_appl_cfg.ble_auth_req | (bte_appl_cfg.ble_auth_req & BTA_LE_AUTH_REQ_MITM) | ((*p_auth_req) & BTA_LE_AUTH_REQ_MITM);
  327. if (*p_oob_data == BTM_BLE_OOB_ENABLE) {
  328. *p_auth_req = (*p_auth_req)&(~BTA_LE_AUTH_REQ_SC_ONLY);
  329. }
  330. if (bte_appl_cfg.ble_io_cap <= 4) {
  331. *p_io_cap = bte_appl_cfg.ble_io_cap;
  332. }
  333. if (bte_appl_cfg.ble_init_key <= BTM_BLE_INITIATOR_KEY_SIZE) {
  334. *p_init_key = bte_appl_cfg.ble_init_key;
  335. }
  336. if (bte_appl_cfg.ble_resp_key <= BTM_BLE_RESPONDER_KEY_SIZE) {
  337. *p_resp_key = bte_appl_cfg.ble_resp_key;
  338. }
  339. if (bte_appl_cfg.ble_max_key_size > 7 && bte_appl_cfg.ble_max_key_size <= 16) {
  340. *p_max_key_size = bte_appl_cfg.ble_max_key_size;
  341. }
  342. #endif ///SMP_INCLUDED == TRUE
  343. }
  344. void bta_dm_co_ble_set_io_cap(UINT8 ble_io_cap)
  345. {
  346. #if (SMP_INCLUDED == TRUE)
  347. if(ble_io_cap < BTM_IO_CAP_MAX ) {
  348. bte_appl_cfg.ble_io_cap = ble_io_cap;
  349. } else {
  350. APPL_TRACE_ERROR("%s error:Invalid io cap value.",__func__);
  351. }
  352. #endif ///SMP_INCLUDED == TRUE
  353. }
  354. void bta_dm_co_ble_set_auth_req(UINT8 ble_auth_req)
  355. {
  356. #if (SMP_INCLUDED == TRUE)
  357. bte_appl_cfg.ble_auth_req = ble_auth_req;
  358. #endif ///SMP_INCLUDED == TRUE
  359. }
  360. void bta_dm_co_ble_set_init_key_req(UINT8 init_key)
  361. {
  362. #if (SMP_INCLUDED == TRUE)
  363. init_key &= 0x0f; // 4~7bit reservd, only used the 0~3bit
  364. bte_appl_cfg.ble_init_key &= init_key;
  365. #endif ///SMP_INCLUDED == TRUE
  366. }
  367. void bta_dm_co_ble_set_rsp_key_req(UINT8 rsp_key)
  368. {
  369. #if (SMP_INCLUDED == TRUE)
  370. rsp_key &= 0x0f; // 4~7bit reservd, only used the 0~3bit
  371. bte_appl_cfg.ble_resp_key &= rsp_key;
  372. #endif ///SMP_INCLUDED == TRUE
  373. }
  374. void bta_dm_co_ble_set_max_key_size(UINT8 ble_key_size)
  375. {
  376. #if (SMP_INCLUDED == TRUE)
  377. if(ble_key_size >= bte_appl_cfg.ble_min_key_size && ble_key_size <= BTM_BLE_MAX_KEY_SIZE) {
  378. bte_appl_cfg.ble_max_key_size = ble_key_size;
  379. } else {
  380. APPL_TRACE_ERROR("%s error:Invalid key size value, key_size =%d",__func__, ble_key_size);
  381. }
  382. #endif ///SMP_INCLUDED == TRUE
  383. }
  384. void bta_dm_co_ble_set_min_key_size(UINT8 ble_key_size)
  385. {
  386. #if (SMP_INCLUDED == TRUE)
  387. if(ble_key_size >= BTM_BLE_MIN_KEY_SIZE && ble_key_size <= bte_appl_cfg.ble_max_key_size) {
  388. bte_appl_cfg.ble_min_key_size = ble_key_size;
  389. } else {
  390. APPL_TRACE_ERROR("%s error:Invalid key size value, key_size =%d",__func__, ble_key_size);
  391. }
  392. #endif ///SMP_INCLUDED == TRUE
  393. }
  394. void bta_dm_co_ble_set_appl_enc_key_size(UINT8 ble_key_size)
  395. {
  396. #if (SMP_INCLUDED == TRUE)
  397. if(ble_key_size >= bte_appl_cfg.ble_min_key_size && ble_key_size <= bte_appl_cfg.ble_max_key_size) {
  398. bte_appl_cfg.ble_appl_enc_key_size = ble_key_size;
  399. } else {
  400. APPL_TRACE_ERROR("%s error:Invalid key size value, key_size =%d",__func__, ble_key_size);
  401. }
  402. #endif ///SMP_INCLUDED == TRUE
  403. }
  404. void bta_dm_co_ble_set_accept_auth_enable(UINT8 enable)
  405. {
  406. #if (SMP_INCLUDED == TRUE)
  407. if (enable) {
  408. enable = BTM_BLE_ONLY_ACCEPT_SPECIFIED_SEC_AUTH_ENABLE;
  409. }
  410. bte_appl_cfg.ble_accept_auth_enable = enable;
  411. #endif ///SMP_INCLUDED == TRUE
  412. }
  413. UINT8 bta_dm_co_ble_get_accept_auth_enable(void)
  414. {
  415. #if (SMP_INCLUDED == TRUE)
  416. return bte_appl_cfg.ble_accept_auth_enable;
  417. #endif ///SMP_INCLUDED == TRUE
  418. return 0;
  419. }
  420. UINT8 bta_dm_co_ble_get_auth_req(void)
  421. {
  422. #if (SMP_INCLUDED == TRUE)
  423. return bte_appl_cfg.ble_auth_req;
  424. #endif ///SMP_INCLUDED == TRUE
  425. return 0;
  426. }
  427. void bta_dm_co_ble_oob_support(UINT8 enable)
  428. {
  429. #if (SMP_INCLUDED == TRUE)
  430. if (enable) {
  431. bte_appl_cfg.oob_support = BTM_BLE_OOB_ENABLE;
  432. } else {
  433. bte_appl_cfg.oob_support = BTM_BLE_OOB_DISABLE;
  434. }
  435. #endif ///SMP_INCLUDED == TRUE
  436. }
  437. #endif