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