1 /******************************************************************************
2 *
3 * Copyright 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 "bt_utils.h"
22 #include "bta_api.h"
23 #include "bta_dm_ci.h"
24 #include "bta_dm_co.h"
25 #include "bta_sys.h"
26 #include "bte_appl.h"
27 #include "btif_dm.h"
28 #include "osi/include/osi.h"
29
30 tBTE_APPL_CFG bte_appl_cfg = {
31 BTA_LE_AUTH_REQ_SC_MITM_BOND, // Authentication requirements
32 BTM_LOCAL_IO_CAPS_BLE, BTM_BLE_INITIATOR_KEY_SIZE,
33 BTM_BLE_RESPONDER_KEY_SIZE, BTM_BLE_MAX_KEY_SIZE};
34
35 /*******************************************************************************
36 *
37 * Function bta_dm_co_get_compress_memory
38 *
39 * Description This callout function is executed by DM to get memory for
40 compression
41
42 * Parameters id - BTA SYS ID
43 * memory_p - memory return by callout
44 * memory_size - memory size
45 *
46 * Returns true for success, false for fail.
47 *
48 ******************************************************************************/
bta_dm_co_get_compress_memory(UNUSED_ATTR tBTA_SYS_ID id,UNUSED_ATTR uint8_t ** memory_p,UNUSED_ATTR uint32_t * memory_size)49 bool bta_dm_co_get_compress_memory(UNUSED_ATTR tBTA_SYS_ID id,
50 UNUSED_ATTR uint8_t** memory_p,
51 UNUSED_ATTR uint32_t* memory_size) {
52 return true;
53 }
54
55 /*******************************************************************************
56 *
57 * Function bta_dm_co_io_req
58 *
59 * Description This callout function is executed by DM to get IO
60 * capabilities of the local device for the Simple Pairing
61 * process.
62 *
63 * Parameters bd_addr - The peer device
64 * *p_io_cap - The local Input/Output capabilities
65 * *p_oob_data - true, if OOB data is available for the peer
66 * device.
67 * *p_auth_req - true, if MITM protection is required.
68 *
69 * Returns void.
70 *
71 ******************************************************************************/
bta_dm_co_io_req(UNUSED_ATTR const RawAddress & bd_addr,tBTA_IO_CAP * p_io_cap,tBTA_OOB_DATA * p_oob_data,tBTA_AUTH_REQ * p_auth_req,bool is_orig)72 void bta_dm_co_io_req(UNUSED_ATTR const RawAddress& bd_addr,
73 tBTA_IO_CAP* p_io_cap, tBTA_OOB_DATA* p_oob_data,
74 tBTA_AUTH_REQ* p_auth_req, bool is_orig) {
75 btif_dm_set_oob_for_io_req(p_oob_data);
76 btif_dm_proc_io_req(bd_addr, p_io_cap, p_oob_data, p_auth_req, is_orig);
77 BTIF_TRACE_DEBUG("bta_dm_co_io_req *p_oob_data = %d", *p_oob_data);
78 BTIF_TRACE_DEBUG("bta_dm_co_io_req *p_io_cap = %d", *p_io_cap);
79 BTIF_TRACE_DEBUG("bta_dm_co_io_req *p_auth_req = %d", *p_auth_req);
80 BTIF_TRACE_DEBUG("bta_dm_co_io_req is_orig = %d", is_orig);
81 }
82
83 /*******************************************************************************
84 *
85 * Function bta_dm_co_io_rsp
86 *
87 * Description This callout function is executed by DM to report IO
88 * capabilities of the peer device for the Simple Pairing
89 * process.
90 *
91 * Parameters bd_addr - The peer device
92 * io_cap - The remote Input/Output capabilities
93 * oob_data - true, if OOB data is available for the peer
94 * device.
95 * auth_req - true, if MITM protection is required.
96 *
97 * Returns void.
98 *
99 ******************************************************************************/
bta_dm_co_io_rsp(const RawAddress & bd_addr,tBTA_IO_CAP io_cap,tBTA_OOB_DATA oob_data,tBTA_AUTH_REQ auth_req)100 void bta_dm_co_io_rsp(const RawAddress& bd_addr, tBTA_IO_CAP io_cap,
101 tBTA_OOB_DATA oob_data, tBTA_AUTH_REQ auth_req) {
102 btif_dm_proc_io_rsp(bd_addr, io_cap, oob_data, auth_req);
103 }
104
105 /*******************************************************************************
106 *
107 * Function bta_dm_co_lk_upgrade
108 *
109 * Description This callout function is executed by DM to check if the
110 * platform wants allow link key upgrade
111 *
112 * Parameters bd_addr - The peer device
113 * *p_upgrade - true, if link key upgrade is desired.
114 *
115 * Returns void.
116 *
117 ******************************************************************************/
bta_dm_co_lk_upgrade(UNUSED_ATTR const RawAddress & bd_addr,UNUSED_ATTR bool * p_upgrade)118 void bta_dm_co_lk_upgrade(UNUSED_ATTR const RawAddress& bd_addr,
119 UNUSED_ATTR bool* p_upgrade) {}
120
121 /*******************************************************************************
122 *
123 * Function bta_dm_co_loc_oob
124 *
125 * Description This callout function is executed by DM to report the OOB
126 * data of the local device for the Simple Pairing process
127 *
128 * Parameters valid - true, if the local OOB data is retrieved from LM
129 * c - Simple Pairing Hash C
130 * r - Simple Pairing Randomnizer R
131 *
132 * Returns void.
133 *
134 ******************************************************************************/
bta_dm_co_loc_oob(bool valid,BT_OCTET16 c,BT_OCTET16 r)135 void bta_dm_co_loc_oob(bool valid, BT_OCTET16 c, BT_OCTET16 r) {
136 BTIF_TRACE_DEBUG("bta_dm_co_loc_oob, valid = %d", valid);
137 #ifdef BTIF_DM_OOB_TEST
138 btif_dm_proc_loc_oob(valid, c, r);
139 #endif
140 }
141
142 /*******************************************************************************
143 *
144 * Function bta_dm_co_rmt_oob
145 *
146 * Description This callout function is executed by DM to request the OOB
147 * data for the remote device for the Simple Pairing process
148 * Need to call bta_dm_ci_rmt_oob() in response
149 *
150 * Parameters bd_addr - The peer device
151 *
152 * Returns void.
153 *
154 ******************************************************************************/
bta_dm_co_rmt_oob(const RawAddress & bd_addr)155 void bta_dm_co_rmt_oob(const RawAddress& bd_addr) {
156 BT_OCTET16 p_c;
157 BT_OCTET16 p_r;
158 bool result = false;
159
160 #ifdef BTIF_DM_OOB_TEST
161 result = btif_dm_proc_rmt_oob(bd_addr, p_c, p_r);
162 #endif
163
164 BTIF_TRACE_DEBUG("bta_dm_co_rmt_oob: result=%d", result);
165 bta_dm_ci_rmt_oob(result, bd_addr, p_c, p_r);
166 }
167
168 /*******************************************************************************
169 *
170 * Function bta_dm_co_le_io_key_req
171 *
172 * Description This callout function is executed by DM to get BLE key
173 * information
174 * before SMP pairing gets going.
175 *
176 * Parameters bd_addr - The peer device
177 * *p_max_key_size - max key size local device supported.
178 * *p_init_key - initiator keys.
179 * *p_resp_key - responder keys.
180 *
181 * Returns void.
182 *
183 ******************************************************************************/
bta_dm_co_le_io_key_req(UNUSED_ATTR const RawAddress & bd_addr,uint8_t * p_max_key_size,tBTA_LE_KEY_TYPE * p_init_key,tBTA_LE_KEY_TYPE * p_resp_key)184 void bta_dm_co_le_io_key_req(UNUSED_ATTR const RawAddress& bd_addr,
185 uint8_t* p_max_key_size,
186 tBTA_LE_KEY_TYPE* p_init_key,
187 tBTA_LE_KEY_TYPE* p_resp_key) {
188 BTIF_TRACE_ERROR("##################################");
189 BTIF_TRACE_ERROR("bta_dm_co_le_io_key_req: only setting max size to 16");
190 BTIF_TRACE_ERROR("##################################");
191 *p_max_key_size = 16;
192 *p_init_key = *p_resp_key =
193 (BTA_LE_KEY_PENC | BTA_LE_KEY_PID | BTA_LE_KEY_PCSRK | BTA_LE_KEY_LENC |
194 BTA_LE_KEY_LID | BTA_LE_KEY_LCSRK);
195 }
196
197 /*******************************************************************************
198 *
199 * Function bta_dm_co_ble_local_key_reload
200 *
201 * Description This callout function is to load the local BLE keys if
202 * available on the device.
203 *
204 * Parameters none
205 *
206 * Returns void.
207 *
208 ******************************************************************************/
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)209 void bta_dm_co_ble_load_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK* p_key_mask,
210 BT_OCTET16 er,
211 tBTA_BLE_LOCAL_ID_KEYS* p_id_keys) {
212 BTIF_TRACE_DEBUG("##################################");
213 BTIF_TRACE_DEBUG(
214 "bta_dm_co_ble_load_local_keys: Load local keys if any are persisted");
215 BTIF_TRACE_DEBUG("##################################");
216 btif_dm_get_ble_local_keys(p_key_mask, er, p_id_keys);
217 }
218
219 /*******************************************************************************
220 *
221 * Function bta_dm_co_ble_io_req
222 *
223 * Description This callout function is executed by DM to get BLE IO
224 * capabilities before SMP pairing gets going.
225 *
226 * Parameters bd_addr - The peer device
227 * *p_io_cap - The local Input/Output capabilities
228 * *p_oob_data - true, if OOB data is available for the peer
229 * device.
230 * *p_auth_req - Auth request setting (Bonding and MITM
231 * required or not)
232 * *p_max_key_size - max key size local device supported.
233 * *p_init_key - initiator keys.
234 * *p_resp_key - responder keys.
235 *
236 * Returns void.
237 *
238 ******************************************************************************/
bta_dm_co_ble_io_req(const RawAddress & bd_addr,tBTA_IO_CAP * p_io_cap,tBTA_OOB_DATA * p_oob_data,tBTA_LE_AUTH_REQ * p_auth_req,uint8_t * p_max_key_size,tBTA_LE_KEY_TYPE * p_init_key,tBTA_LE_KEY_TYPE * p_resp_key)239 void bta_dm_co_ble_io_req(const RawAddress& bd_addr, tBTA_IO_CAP* p_io_cap,
240 tBTA_OOB_DATA* p_oob_data,
241 tBTA_LE_AUTH_REQ* p_auth_req, uint8_t* p_max_key_size,
242 tBTA_LE_KEY_TYPE* p_init_key,
243 tBTA_LE_KEY_TYPE* p_resp_key) {
244 /* Retrieve the properties from file system if possible */
245 tBTE_APPL_CFG nv_config;
246 if (btif_dm_get_smp_config(&nv_config)) bte_appl_cfg = nv_config;
247
248 /* *p_auth_req by default is false for devices with NoInputNoOutput; true for
249 * other devices. */
250
251 if (bte_appl_cfg.ble_auth_req)
252 *p_auth_req = bte_appl_cfg.ble_auth_req |
253 (bte_appl_cfg.ble_auth_req & 0x04) | ((*p_auth_req) & 0x04);
254
255 /* if OOB is not supported, this call-out function does not need to do
256 * anything
257 * otherwise, look for the OOB data associated with the address and set
258 * *p_oob_data accordingly.
259 * If the answer can not be obtained right away,
260 * set *p_oob_data to BTA_OOB_UNKNOWN and call bta_dm_ci_io_req() when the
261 * answer is available.
262 */
263
264 btif_dm_set_oob_for_le_io_req(bd_addr, p_oob_data, p_auth_req);
265
266 if (bte_appl_cfg.ble_io_cap <= 4) *p_io_cap = bte_appl_cfg.ble_io_cap;
267
268 if (bte_appl_cfg.ble_init_key <= BTM_BLE_INITIATOR_KEY_SIZE)
269 *p_init_key = bte_appl_cfg.ble_init_key;
270
271 if (bte_appl_cfg.ble_resp_key <= BTM_BLE_RESPONDER_KEY_SIZE)
272 *p_resp_key = bte_appl_cfg.ble_resp_key;
273
274 if (bte_appl_cfg.ble_max_key_size > 7 && bte_appl_cfg.ble_max_key_size <= 16)
275 *p_max_key_size = bte_appl_cfg.ble_max_key_size;
276 }
277