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