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