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