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
32tBTE_APPL_CFG bte_appl_cfg =
33{
34    BTM_AUTH_SPGB_YES,  // Authentication requirements
35    BTM_LOCAL_IO_CAPS_BLE, // I/O capabilities
36    7,                  // Initiaor key size
37    7,                  // Responder key size
38    16                  // Maximum key size
39};
40#endif
41
42/*******************************************************************************
43**
44** Function         bta_dm_co_get_compress_memory
45**
46** Description      This callout function is executed by DM to get memory for compression
47
48** Parameters       id  -  BTA SYS ID
49**                  memory_p - memory return by callout
50**                  memory_size - memory size
51**
52** Returns          TRUE for success, FALSE for fail.
53**
54*******************************************************************************/
55BOOLEAN bta_dm_co_get_compress_memory(tBTA_SYS_ID id, UINT8 **memory_p, UINT32 *memory_size)
56{
57    UNUSED(id);
58    UNUSED(memory_p);
59    UNUSED(memory_size);
60    return TRUE;
61}
62
63/*******************************************************************************
64**
65** Function         bta_dm_co_io_req
66**
67** Description      This callout function is executed by DM to get IO capabilities
68**                  of the local device for the Simple Pairing process
69**
70** Parameters       bd_addr  - The peer device
71**                  *p_io_cap - The local Input/Output capabilities
72**                  *p_oob_data - TRUE, if OOB data is available for the peer device.
73**                  *p_auth_req - TRUE, if MITM protection is required.
74**
75** Returns          void.
76**
77*******************************************************************************/
78void bta_dm_co_io_req(BD_ADDR bd_addr, tBTA_IO_CAP *p_io_cap, tBTA_OOB_DATA *p_oob_data,
79                      tBTA_AUTH_REQ *p_auth_req, BOOLEAN is_orig)
80{
81    UNUSED(bd_addr);
82#if (BTM_OOB_INCLUDED == TRUE)
83    btif_dm_set_oob_for_io_req(p_oob_data);
84#endif
85    btif_dm_proc_io_req(bd_addr, p_io_cap, p_oob_data, p_auth_req, is_orig);
86    BTIF_TRACE_DEBUG("bta_dm_co_io_req *p_oob_data = %d", *p_oob_data);
87    BTIF_TRACE_DEBUG("bta_dm_co_io_req *p_io_cap = %d", *p_io_cap);
88    BTIF_TRACE_DEBUG("bta_dm_co_io_req *p_auth_req = %d", *p_auth_req);
89    BTIF_TRACE_DEBUG("bta_dm_co_io_req is_orig = %d", is_orig);
90}
91
92/*******************************************************************************
93**
94** Function         bta_dm_co_io_rsp
95**
96** Description      This callout function is executed by DM to report IO capabilities
97**                  of the peer device for the Simple Pairing process
98**
99** Parameters       bd_addr  - The peer device
100**                  io_cap - The remote Input/Output capabilities
101**                  oob_data - TRUE, if OOB data is available for the peer device.
102**                  auth_req - TRUE, if MITM protection is required.
103**
104** Returns          void.
105**
106*******************************************************************************/
107void bta_dm_co_io_rsp(BD_ADDR bd_addr, tBTA_IO_CAP io_cap,
108                      tBTA_OOB_DATA oob_data, tBTA_AUTH_REQ auth_req)
109{
110    btif_dm_proc_io_rsp(bd_addr, io_cap, oob_data, auth_req);
111}
112
113/*******************************************************************************
114**
115** Function         bta_dm_co_lk_upgrade
116**
117** Description      This callout function is executed by DM to check if the
118**                  platform wants allow link key upgrade
119**
120** Parameters       bd_addr  - The peer device
121**                  *p_upgrade - TRUE, if link key upgrade is desired.
122**
123** Returns          void.
124**
125*******************************************************************************/
126void  bta_dm_co_lk_upgrade(BD_ADDR bd_addr, BOOLEAN *p_upgrade )
127{
128    UNUSED(bd_addr);
129    UNUSED(p_upgrade);
130}
131
132#if (BTM_OOB_INCLUDED == TRUE)
133/*******************************************************************************
134**
135** Function         bta_dm_co_loc_oob
136**
137** Description      This callout function is executed by DM to report the OOB
138**                  data of the local device for the Simple Pairing process
139**
140** Parameters       valid - TRUE, if the local OOB data is retrieved from LM
141**                  c     - Simple Pairing Hash C
142**                  r     - Simple Pairing Randomnizer R
143**
144** Returns          void.
145**
146*******************************************************************************/
147void bta_dm_co_loc_oob(BOOLEAN valid, BT_OCTET16 c, BT_OCTET16 r)
148{
149    BTIF_TRACE_DEBUG("bta_dm_co_loc_oob, valid = %d", valid);
150#ifdef BTIF_DM_OOB_TEST
151    btif_dm_proc_loc_oob(valid, c, r);
152#endif
153}
154
155/*******************************************************************************
156**
157** Function         bta_dm_co_rmt_oob
158**
159** Description      This callout function is executed by DM to request the OOB
160**                  data for the remote device for the Simple Pairing process
161**                  Need to call bta_dm_ci_rmt_oob() in response
162**
163** Parameters       bd_addr  - The peer device
164**
165** Returns          void.
166**
167*******************************************************************************/
168void bta_dm_co_rmt_oob(BD_ADDR bd_addr)
169{
170    BT_OCTET16 p_c;
171    BT_OCTET16 p_r;
172    BOOLEAN result = FALSE;
173
174#ifdef BTIF_DM_OOB_TEST
175    result = btif_dm_proc_rmt_oob(bd_addr, p_c, p_r);
176#endif
177
178    BTIF_TRACE_DEBUG("bta_dm_co_rmt_oob: result=%d",result);
179    bta_dm_ci_rmt_oob(result, bd_addr, p_c, p_r);
180}
181
182#endif /* BTM_OOB_INCLUDED */
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*******************************************************************************/
199static 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*******************************************************************************/
215tBTA_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*******************************************************************************/
259void 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        /* use dedicated SCO buffer pool for SCO TX data */
267        cfg.pool_id = HCI_SCO_POOL_ID;
268        cfg.p_cback = btui_sco_codec_callback;
269        cfg.pkt_size = pkt_size;
270        cfg.cb_event = event;
271        /* open and start the codec */
272        btui_sco_codec_open(&cfg);
273        btui_sco_codec_start(handle);
274    }
275}
276
277/*******************************************************************************
278**
279** Function         bta_dm_sco_co_close
280**
281** Description      This function is called when a SCO connection is closed
282**
283**
284** Returns          void
285**
286*******************************************************************************/
287void bta_dm_sco_co_close(void)
288{
289    if (btui_cb.sco_hci)
290    {
291        BTIF_TRACE_DEBUG("bta_dm_sco_co_close close codec");
292        /* close sco codec */
293        btui_sco_codec_close();
294
295        btui_cb.sco_hci = FALSE;
296    }
297}
298
299/*******************************************************************************
300**
301** Function         bta_dm_sco_co_in_data
302**
303** Description      This function is called to send incoming SCO data to application.
304**
305** Returns          void
306**
307*******************************************************************************/
308void bta_dm_sco_co_in_data(BT_HDR  *p_buf)
309{
310    if (btui_cfg.sco_use_mic)
311        btui_sco_codec_inqdata (p_buf);
312    else
313        GKI_freebuf(p_buf);
314}
315
316/*******************************************************************************
317**
318** Function         bta_dm_sco_co_out_data
319**
320** Description      This function is called to send SCO data over HCI.
321**
322** Returns          void
323**
324*******************************************************************************/
325void bta_dm_sco_co_out_data(BT_HDR  **p_buf)
326{
327    btui_sco_codec_readbuf(p_buf);
328}
329
330#endif /* #if (BTM_SCO_HCI_INCLUDED == TRUE ) && (BTM_SCO_INCLUDED == TRUE)*/
331
332
333#if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE)
334/*******************************************************************************
335**
336** Function         bta_dm_co_le_io_key_req
337**
338** Description      This callout function is executed by DM to get BLE key information
339**                  before SMP pairing gets going.
340**
341** Parameters       bd_addr  - The peer device
342**                  *p_max_key_size - max key size local device supported.
343**                  *p_init_key - initiator keys.
344**                  *p_resp_key - responder keys.
345**
346** Returns          void.
347**
348*******************************************************************************/
349void bta_dm_co_le_io_key_req(BD_ADDR bd_addr, UINT8 *p_max_key_size,
350                             tBTA_LE_KEY_TYPE *p_init_key,
351                             tBTA_LE_KEY_TYPE  *p_resp_key )
352{
353    UNUSED(bd_addr);
354    BTIF_TRACE_ERROR("##################################");
355    BTIF_TRACE_ERROR("bta_dm_co_le_io_key_req: only setting max size to 16");
356    BTIF_TRACE_ERROR("##################################");
357    *p_max_key_size = 16;
358    *p_init_key = *p_resp_key =
359                  (BTA_LE_KEY_PENC|BTA_LE_KEY_PID|BTA_LE_KEY_PCSRK|BTA_LE_KEY_LENC|BTA_LE_KEY_LID|BTA_LE_KEY_LCSRK);
360}
361
362
363/*******************************************************************************
364**
365** Function         bta_dm_co_ble_local_key_reload
366**
367** Description      This callout function is to load the local BLE keys if available
368**                  on the device.
369**
370** Parameters       none
371**
372** Returns          void.
373**
374*******************************************************************************/
375void bta_dm_co_ble_load_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er,
376                                   tBTA_BLE_LOCAL_ID_KEYS *p_id_keys)
377{
378    BTIF_TRACE_DEBUG("##################################");
379    BTIF_TRACE_DEBUG("bta_dm_co_ble_load_local_keys:  Load local keys if any are persisted");
380    BTIF_TRACE_DEBUG("##################################");
381    btif_dm_get_ble_local_keys( p_key_mask, er, p_id_keys);
382}
383
384/*******************************************************************************
385**
386** Function         bta_dm_co_ble_io_req
387**
388** Description      This callout function is executed by DM to get BLE IO capabilities
389**                  before SMP pairing gets going.
390**
391** Parameters       bd_addr  - The peer device
392**                  *p_io_cap - The local Input/Output capabilities
393**                  *p_oob_data - TRUE, if OOB data is available for the peer device.
394**                  *p_auth_req -  Auth request setting (Bonding and MITM required or not)
395**                  *p_max_key_size - max key size local device supported.
396**                  *p_init_key - initiator keys.
397**                  *p_resp_key - responder keys.
398**
399** Returns          void.
400**
401*******************************************************************************/
402void bta_dm_co_ble_io_req(BD_ADDR bd_addr,  tBTA_IO_CAP *p_io_cap,
403                          tBTA_OOB_DATA *p_oob_data,
404                          tBTA_LE_AUTH_REQ *p_auth_req,
405                          UINT8 *p_max_key_size,
406                          tBTA_LE_KEY_TYPE *p_init_key,
407                          tBTA_LE_KEY_TYPE  *p_resp_key )
408{
409    UNUSED(bd_addr);
410    /* if OOB is not supported, this call-out function does not need to do anything
411     * otherwise, look for the OOB data associated with the address and set *p_oob_data accordingly
412     * If the answer can not be obtained right away,
413     * set *p_oob_data to BTA_OOB_UNKNOWN and call bta_dm_ci_io_req() when the answer is available */
414
415    *p_oob_data = FALSE;
416
417    /* *p_auth_req by default is FALSE for devices with NoInputNoOutput; TRUE for other devices. */
418
419    if (bte_appl_cfg.ble_auth_req)
420        *p_auth_req = bte_appl_cfg.ble_auth_req | (bte_appl_cfg.ble_auth_req & 0x04) | ((*p_auth_req) & 0x04);
421
422    if (bte_appl_cfg.ble_io_cap <=4)
423        *p_io_cap = bte_appl_cfg.ble_io_cap;
424
425    if (bte_appl_cfg.ble_init_key<=7)
426        *p_init_key = bte_appl_cfg.ble_init_key;
427
428    if (bte_appl_cfg.ble_resp_key<=7)
429        *p_resp_key = bte_appl_cfg.ble_resp_key;
430
431    if (bte_appl_cfg.ble_max_key_size > 7 && bte_appl_cfg.ble_max_key_size <= 16)
432        *p_max_key_size = bte_appl_cfg.ble_max_key_size;
433}
434
435
436#endif
437
438