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_api.h"
22 #include "bta_sys.h"
23 #include "bta_dm_co.h"
24 #include "bta_dm_ci.h"
25 #include "bt_utils.h"
26 #include "btif_dm.h"
27 #if (defined WEAR_LE_IO_CAP_OVERRIDE && WEAR_LE_IO_CAP_OVERRIDE == TRUE)
28 #include "btif_storage.h"
29 #endif
30 #if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE)
31 #include "bte_appl.h"
32
33 tBTE_APPL_CFG bte_appl_cfg =
34 {
35 #if SMP_INCLUDED == TRUE
36 BTA_LE_AUTH_REQ_SC_MITM_BOND, // Authentication requirements
37 #else
38 BTM_AUTH_SPGB_YES, // Authentication requirements
39 #endif
40 BTM_LOCAL_IO_CAPS_BLE,
41 BTM_BLE_INITIATOR_KEY_SIZE,
42 BTM_BLE_RESPONDER_KEY_SIZE,
43 BTM_BLE_MAX_KEY_SIZE
44 };
45 #endif
46
47 /*******************************************************************************
48 **
49 ** Function bta_dm_co_get_compress_memory
50 **
51 ** Description This callout function is executed by DM to get memory for compression
52
53 ** Parameters id - BTA SYS ID
54 ** memory_p - memory return by callout
55 ** memory_size - memory size
56 **
57 ** Returns TRUE for success, FALSE for fail.
58 **
59 *******************************************************************************/
bta_dm_co_get_compress_memory(tBTA_SYS_ID id,UINT8 ** memory_p,UINT32 * memory_size)60 BOOLEAN bta_dm_co_get_compress_memory(tBTA_SYS_ID id, UINT8 **memory_p, UINT32 *memory_size)
61 {
62 UNUSED(id);
63 UNUSED(memory_p);
64 UNUSED(memory_size);
65 return TRUE;
66 }
67
68 /*******************************************************************************
69 **
70 ** Function bta_dm_co_io_req
71 **
72 ** Description This callout function is executed by DM to get IO capabilities
73 ** of the local device for the Simple Pairing process
74 **
75 ** Parameters bd_addr - The peer device
76 ** *p_io_cap - The local Input/Output capabilities
77 ** *p_oob_data - TRUE, if OOB data is available for the peer device.
78 ** *p_auth_req - TRUE, if MITM protection is required.
79 **
80 ** Returns void.
81 **
82 *******************************************************************************/
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)83 void bta_dm_co_io_req(BD_ADDR bd_addr, tBTA_IO_CAP *p_io_cap, tBTA_OOB_DATA *p_oob_data,
84 tBTA_AUTH_REQ *p_auth_req, BOOLEAN is_orig)
85 {
86 UNUSED(bd_addr);
87 btif_dm_set_oob_for_io_req(p_oob_data);
88 btif_dm_proc_io_req(bd_addr, p_io_cap, p_oob_data, p_auth_req, is_orig);
89 BTIF_TRACE_DEBUG("bta_dm_co_io_req *p_oob_data = %d", *p_oob_data);
90 BTIF_TRACE_DEBUG("bta_dm_co_io_req *p_io_cap = %d", *p_io_cap);
91 BTIF_TRACE_DEBUG("bta_dm_co_io_req *p_auth_req = %d", *p_auth_req);
92 BTIF_TRACE_DEBUG("bta_dm_co_io_req is_orig = %d", is_orig);
93 }
94
95 /*******************************************************************************
96 **
97 ** Function bta_dm_co_io_rsp
98 **
99 ** Description This callout function is executed by DM to report IO capabilities
100 ** of the peer device for the Simple Pairing process
101 **
102 ** Parameters bd_addr - The peer device
103 ** io_cap - The remote Input/Output capabilities
104 ** oob_data - TRUE, if OOB data is available for the peer device.
105 ** auth_req - TRUE, if MITM protection is required.
106 **
107 ** Returns void.
108 **
109 *******************************************************************************/
bta_dm_co_io_rsp(BD_ADDR bd_addr,tBTA_IO_CAP io_cap,tBTA_OOB_DATA oob_data,tBTA_AUTH_REQ auth_req)110 void bta_dm_co_io_rsp(BD_ADDR bd_addr, tBTA_IO_CAP io_cap,
111 tBTA_OOB_DATA oob_data, tBTA_AUTH_REQ auth_req)
112 {
113 btif_dm_proc_io_rsp(bd_addr, io_cap, oob_data, auth_req);
114 }
115
116 /*******************************************************************************
117 **
118 ** Function bta_dm_co_lk_upgrade
119 **
120 ** Description This callout function is executed by DM to check if the
121 ** platform wants allow link key upgrade
122 **
123 ** Parameters bd_addr - The peer device
124 ** *p_upgrade - TRUE, if link key upgrade is desired.
125 **
126 ** Returns void.
127 **
128 *******************************************************************************/
bta_dm_co_lk_upgrade(BD_ADDR bd_addr,BOOLEAN * p_upgrade)129 void bta_dm_co_lk_upgrade(BD_ADDR bd_addr, BOOLEAN *p_upgrade )
130 {
131 UNUSED(bd_addr);
132 UNUSED(p_upgrade);
133 }
134
135 /*******************************************************************************
136 **
137 ** Function bta_dm_co_loc_oob
138 **
139 ** Description This callout function is executed by DM to report the OOB
140 ** data of the local device for the Simple Pairing process
141 **
142 ** Parameters valid - TRUE, if the local OOB data is retrieved from LM
143 ** c - Simple Pairing Hash C
144 ** r - Simple Pairing Randomnizer R
145 **
146 ** Returns void.
147 **
148 *******************************************************************************/
bta_dm_co_loc_oob(BOOLEAN valid,BT_OCTET16 c,BT_OCTET16 r)149 void bta_dm_co_loc_oob(BOOLEAN valid, BT_OCTET16 c, BT_OCTET16 r)
150 {
151 BTIF_TRACE_DEBUG("bta_dm_co_loc_oob, valid = %d", valid);
152 #ifdef BTIF_DM_OOB_TEST
153 btif_dm_proc_loc_oob(valid, c, r);
154 #endif
155 }
156
157 /*******************************************************************************
158 **
159 ** Function bta_dm_co_rmt_oob
160 **
161 ** Description This callout function is executed by DM to request the OOB
162 ** data for the remote device for the Simple Pairing process
163 ** Need to call bta_dm_ci_rmt_oob() in response
164 **
165 ** Parameters bd_addr - The peer device
166 **
167 ** Returns void.
168 **
169 *******************************************************************************/
bta_dm_co_rmt_oob(BD_ADDR bd_addr)170 void bta_dm_co_rmt_oob(BD_ADDR bd_addr)
171 {
172 BT_OCTET16 p_c;
173 BT_OCTET16 p_r;
174 BOOLEAN result = FALSE;
175
176 #ifdef BTIF_DM_OOB_TEST
177 result = btif_dm_proc_rmt_oob(bd_addr, p_c, p_r);
178 #endif
179
180 BTIF_TRACE_DEBUG("bta_dm_co_rmt_oob: result=%d",result);
181 bta_dm_ci_rmt_oob(result, bd_addr, p_c, p_r);
182 }
183
184
185 // REMOVE FOR BLUEDROID ?
186
187 #if (BTM_SCO_HCI_INCLUDED == TRUE ) && (BTM_SCO_INCLUDED == TRUE)
188
189 /*******************************************************************************
190 **
191 ** Function btui_sco_codec_callback
192 **
193 ** Description Callback for btui codec.
194 **
195 **
196 ** Returns void
197 **
198 *******************************************************************************/
btui_sco_codec_callback(UINT16 event,UINT16 sco_handle)199 static void btui_sco_codec_callback(UINT16 event, UINT16 sco_handle)
200 {
201 bta_dm_sco_ci_data_ready(event, sco_handle);
202 }
203 /*******************************************************************************
204 **
205 ** Function bta_dm_sco_co_init
206 **
207 ** Description This function can be used by the phone to initialize audio
208 ** codec or for other initialization purposes before SCO connection
209 ** is opened.
210 **
211 **
212 ** Returns tBTA_DM_SCO_ROUTE_TYPE: SCO routing configuration type.
213 **
214 *******************************************************************************/
bta_dm_sco_co_init(UINT32 rx_bw,UINT32 tx_bw,tBTA_CODEC_INFO * p_codec_type,UINT8 app_id)215 tBTA_DM_SCO_ROUTE_TYPE bta_dm_sco_co_init(UINT32 rx_bw, UINT32 tx_bw,
216 tBTA_CODEC_INFO * p_codec_type, UINT8 app_id)
217 {
218 tBTM_SCO_ROUTE_TYPE route = BTA_DM_SCO_ROUTE_PCM;
219
220 BTIF_TRACE_DEBUG("bta_dm_sco_co_init");
221
222 /* set up SCO routing configuration if SCO over HCI app ID is used and run time
223 configuration is set to SCO over HCI */
224 /* HS invoke this call-out */
225 if (
226 #if (BTA_HS_INCLUDED == TRUE ) && (BTA_HS_INCLUDED == TRUE)
227 (app_id == BTUI_DM_SCO_4_HS_APP_ID && btui_cfg.hs_sco_over_hci) ||
228 #endif
229 /* AG invoke this call-out */
230 (app_id != BTUI_DM_SCO_4_HS_APP_ID && btui_cfg.ag_sco_over_hci ))
231 {
232 route = btui_cb.sco_hci = BTA_DM_SCO_ROUTE_HCI;
233 }
234 /* no codec is is used for the SCO data */
235 if (p_codec_type->codec_type == BTA_SCO_CODEC_PCM && route == BTA_DM_SCO_ROUTE_HCI)
236 {
237 /* initialize SCO codec */
238 if (!btui_sco_codec_init(rx_bw, tx_bw))
239 {
240 BTIF_TRACE_ERROR("codec initialization exception!");
241 }
242 }
243
244 return route;
245 }
246
247
248
249 /*******************************************************************************
250 **
251 ** Function bta_dm_sco_co_open
252 **
253 ** Description This function is executed when a SCO connection is open.
254 **
255 **
256 ** Returns void
257 **
258 *******************************************************************************/
bta_dm_sco_co_open(UINT16 handle,UINT8 pkt_size,UINT16 event)259 void bta_dm_sco_co_open(UINT16 handle, UINT8 pkt_size, UINT16 event)
260 {
261 tBTUI_SCO_CODEC_CFG cfg;
262
263 if (btui_cb.sco_hci)
264 {
265 BTIF_TRACE_DEBUG("bta_dm_sco_co_open handle:%d pkt_size:%d", handle, pkt_size);
266 cfg.p_cback = btui_sco_codec_callback;
267 cfg.pkt_size = pkt_size;
268 cfg.cb_event = event;
269 /* open and start the codec */
270 btui_sco_codec_open(&cfg);
271 btui_sco_codec_start(handle);
272 }
273 }
274
275 /*******************************************************************************
276 **
277 ** Function bta_dm_sco_co_close
278 **
279 ** Description This function is called when a SCO connection is closed
280 **
281 **
282 ** Returns void
283 **
284 *******************************************************************************/
bta_dm_sco_co_close(void)285 void bta_dm_sco_co_close(void)
286 {
287 if (btui_cb.sco_hci)
288 {
289 BTIF_TRACE_DEBUG("bta_dm_sco_co_close close codec");
290 /* close sco codec */
291 btui_sco_codec_close();
292
293 btui_cb.sco_hci = FALSE;
294 }
295 }
296
297 /*******************************************************************************
298 **
299 ** Function bta_dm_sco_co_in_data
300 **
301 ** Description This function is called to send incoming SCO data to application.
302 **
303 ** Returns void
304 **
305 *******************************************************************************/
bta_dm_sco_co_in_data(BT_HDR * p_buf)306 void bta_dm_sco_co_in_data(BT_HDR *p_buf)
307 {
308 if (btui_cfg.sco_use_mic)
309 btui_sco_codec_inqdata(p_buf);
310 else
311 osi_free(p_buf);
312 }
313
314 /*******************************************************************************
315 **
316 ** Function bta_dm_sco_co_out_data
317 **
318 ** Description This function is called to send SCO data over HCI.
319 **
320 ** Returns void
321 **
322 *******************************************************************************/
bta_dm_sco_co_out_data(BT_HDR ** p_buf)323 void bta_dm_sco_co_out_data(BT_HDR **p_buf)
324 {
325 btui_sco_codec_readbuf(p_buf);
326 }
327
328 #endif /* #if (BTM_SCO_HCI_INCLUDED == TRUE ) && (BTM_SCO_INCLUDED == TRUE)*/
329
330
331 #if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE)
332 /*******************************************************************************
333 **
334 ** Function bta_dm_co_le_io_key_req
335 **
336 ** Description This callout function is executed by DM to get BLE key information
337 ** before SMP pairing gets going.
338 **
339 ** Parameters bd_addr - The peer device
340 ** *p_max_key_size - max key size local device supported.
341 ** *p_init_key - initiator keys.
342 ** *p_resp_key - responder keys.
343 **
344 ** Returns void.
345 **
346 *******************************************************************************/
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)347 void bta_dm_co_le_io_key_req(BD_ADDR bd_addr, UINT8 *p_max_key_size,
348 tBTA_LE_KEY_TYPE *p_init_key,
349 tBTA_LE_KEY_TYPE *p_resp_key )
350 {
351 UNUSED(bd_addr);
352 BTIF_TRACE_ERROR("##################################");
353 BTIF_TRACE_ERROR("bta_dm_co_le_io_key_req: only setting max size to 16");
354 BTIF_TRACE_ERROR("##################################");
355 *p_max_key_size = 16;
356 *p_init_key = *p_resp_key =
357 (BTA_LE_KEY_PENC|BTA_LE_KEY_PID|BTA_LE_KEY_PCSRK|BTA_LE_KEY_LENC|BTA_LE_KEY_LID|BTA_LE_KEY_LCSRK);
358 }
359
360
361 /*******************************************************************************
362 **
363 ** Function bta_dm_co_ble_local_key_reload
364 **
365 ** Description This callout function is to load the local BLE keys if available
366 ** on the device.
367 **
368 ** Parameters none
369 **
370 ** Returns void.
371 **
372 *******************************************************************************/
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)373 void bta_dm_co_ble_load_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er,
374 tBTA_BLE_LOCAL_ID_KEYS *p_id_keys)
375 {
376 BTIF_TRACE_DEBUG("##################################");
377 BTIF_TRACE_DEBUG("bta_dm_co_ble_load_local_keys: Load local keys if any are persisted");
378 BTIF_TRACE_DEBUG("##################################");
379 btif_dm_get_ble_local_keys( p_key_mask, er, p_id_keys);
380 }
381
382 /*******************************************************************************
383 **
384 ** Function bta_dm_co_ble_io_req
385 **
386 ** Description This callout function is executed by DM to get BLE IO capabilities
387 ** before SMP pairing gets going.
388 **
389 ** Parameters bd_addr - The peer device
390 ** *p_io_cap - The local Input/Output capabilities
391 ** *p_oob_data - TRUE, if OOB data is available for the peer device.
392 ** *p_auth_req - Auth request setting (Bonding and MITM required or not)
393 ** *p_max_key_size - max key size local device supported.
394 ** *p_init_key - initiator keys.
395 ** *p_resp_key - responder keys.
396 **
397 ** Returns void.
398 **
399 *******************************************************************************/
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)400 void bta_dm_co_ble_io_req(BD_ADDR bd_addr, tBTA_IO_CAP *p_io_cap,
401 tBTA_OOB_DATA *p_oob_data,
402 tBTA_LE_AUTH_REQ *p_auth_req,
403 UINT8 *p_max_key_size,
404 tBTA_LE_KEY_TYPE *p_init_key,
405 tBTA_LE_KEY_TYPE *p_resp_key )
406 {
407 UNUSED(bd_addr);
408
409
410 #if (defined WEAR_LE_IO_CAP_OVERRIDE && WEAR_LE_IO_CAP_OVERRIDE == TRUE)
411 /*
412 * Note: This is a Wear-specific feature for iOS pairing.
413 *
414 * Set WearLeIoCap config to force local IO capability to be BTM_IO_CAP_NONE
415 * (No input, no output) for the first bond creation, that indirectly
416 * triggers Just Works pairing.
417 */
418 if (btif_storage_get_num_bonded_devices() == 0)
419 bte_appl_cfg.ble_io_cap = BTM_IO_CAP_NONE;
420 #endif
421
422 /* For certification testing purpose, LE IO capability can also be specified with
423 * "PTS_SmpOptions" in the BT stack configuration file (i.e. bt_stack.conf).
424 * Note that if "PTS_SmpOptions" is set, it could override IO capability set above.
425 */
426 tBTE_APPL_CFG nv_config;
427 if(btif_dm_get_smp_config(&nv_config))
428 bte_appl_cfg = nv_config;
429
430 /* *p_auth_req by default is FALSE for devices with NoInputNoOutput; TRUE for other devices. */
431
432 if (bte_appl_cfg.ble_auth_req)
433 *p_auth_req = bte_appl_cfg.ble_auth_req | (bte_appl_cfg.ble_auth_req & 0x04) | ((*p_auth_req) & 0x04);
434
435 /* if OOB is not supported, this call-out function does not need to do anything
436 * otherwise, look for the OOB data associated with the address and set *p_oob_data accordingly.
437 * If the answer can not be obtained right away,
438 * set *p_oob_data to BTA_OOB_UNKNOWN and call bta_dm_ci_io_req() when the answer is available.
439 */
440
441 btif_dm_set_oob_for_le_io_req(bd_addr, p_oob_data, p_auth_req);
442
443 if (bte_appl_cfg.ble_io_cap <=4)
444 *p_io_cap = bte_appl_cfg.ble_io_cap;
445
446 if (bte_appl_cfg.ble_init_key <= BTM_BLE_INITIATOR_KEY_SIZE)
447 *p_init_key = bte_appl_cfg.ble_init_key;
448
449 if (bte_appl_cfg.ble_resp_key <= BTM_BLE_RESPONDER_KEY_SIZE)
450 *p_resp_key = bte_appl_cfg.ble_resp_key;
451
452 if (bte_appl_cfg.ble_max_key_size > 7 && bte_appl_cfg.ble_max_key_size <= 16)
453 *p_max_key_size = bte_appl_cfg.ble_max_key_size;
454 }
455
456
457 #endif
458
459