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