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 "bt_utils.h"
22 #include "bta_api.h"
23 #include "bta_dm_ci.h"
24 #include "bta_dm_co.h"
25 #include "bta_sys.h"
26 #include "bte_appl.h"
27 #include "btif_dm.h"
28 #include "osi/include/osi.h"
29 
30 tBTE_APPL_CFG bte_appl_cfg = {
31     BTA_LE_AUTH_REQ_SC_MITM_BOND,  // Authentication requirements
32     BTM_LOCAL_IO_CAPS_BLE, BTM_BLE_INITIATOR_KEY_SIZE,
33     BTM_BLE_RESPONDER_KEY_SIZE, BTM_BLE_MAX_KEY_SIZE};
34 
35 /*******************************************************************************
36  *
37  * Function         bta_dm_co_get_compress_memory
38  *
39  * Description      This callout function is executed by DM to get memory for
40  compression
41 
42  * Parameters       id  -  BTA SYS ID
43  *                  memory_p - memory return by callout
44  *                  memory_size - memory size
45  *
46  * Returns          true for success, false for fail.
47  *
48  ******************************************************************************/
bta_dm_co_get_compress_memory(UNUSED_ATTR tBTA_SYS_ID id,UNUSED_ATTR uint8_t ** memory_p,UNUSED_ATTR uint32_t * memory_size)49 bool bta_dm_co_get_compress_memory(UNUSED_ATTR tBTA_SYS_ID id,
50                                    UNUSED_ATTR uint8_t** memory_p,
51                                    UNUSED_ATTR uint32_t* memory_size) {
52   return true;
53 }
54 
55 /*******************************************************************************
56  *
57  * Function         bta_dm_co_io_req
58  *
59  * Description      This callout function is executed by DM to get IO
60  *                  capabilities of the local device for the Simple Pairing
61  *                  process.
62  *
63  * Parameters       bd_addr  - The peer device
64  *                  *p_io_cap - The local Input/Output capabilities
65  *                  *p_oob_data - true, if OOB data is available for the peer
66  *                                device.
67  *                  *p_auth_req - true, if MITM protection is required.
68  *
69  * Returns          void.
70  *
71  ******************************************************************************/
bta_dm_co_io_req(UNUSED_ATTR BD_ADDR bd_addr,tBTA_IO_CAP * p_io_cap,tBTA_OOB_DATA * p_oob_data,tBTA_AUTH_REQ * p_auth_req,bool is_orig)72 void bta_dm_co_io_req(UNUSED_ATTR BD_ADDR bd_addr, tBTA_IO_CAP* p_io_cap,
73                       tBTA_OOB_DATA* p_oob_data, tBTA_AUTH_REQ* p_auth_req,
74                       bool is_orig) {
75   btif_dm_set_oob_for_io_req(p_oob_data);
76   btif_dm_proc_io_req(bd_addr, p_io_cap, p_oob_data, p_auth_req, is_orig);
77   BTIF_TRACE_DEBUG("bta_dm_co_io_req *p_oob_data = %d", *p_oob_data);
78   BTIF_TRACE_DEBUG("bta_dm_co_io_req *p_io_cap = %d", *p_io_cap);
79   BTIF_TRACE_DEBUG("bta_dm_co_io_req *p_auth_req = %d", *p_auth_req);
80   BTIF_TRACE_DEBUG("bta_dm_co_io_req is_orig = %d", is_orig);
81 }
82 
83 /*******************************************************************************
84  *
85  * Function         bta_dm_co_io_rsp
86  *
87  * Description      This callout function is executed by DM to report IO
88  *                  capabilities of the peer device for the Simple Pairing
89  *                  process.
90  *
91  * Parameters       bd_addr  - The peer device
92  *                  io_cap - The remote Input/Output capabilities
93  *                  oob_data - true, if OOB data is available for the peer
94  *                             device.
95  *                  auth_req - true, if MITM protection is required.
96  *
97  * Returns          void.
98  *
99  ******************************************************************************/
bta_dm_co_io_rsp(BD_ADDR bd_addr,tBTA_IO_CAP io_cap,tBTA_OOB_DATA oob_data,tBTA_AUTH_REQ auth_req)100 void bta_dm_co_io_rsp(BD_ADDR bd_addr, tBTA_IO_CAP io_cap,
101                       tBTA_OOB_DATA oob_data, tBTA_AUTH_REQ auth_req) {
102   btif_dm_proc_io_rsp(bd_addr, io_cap, oob_data, auth_req);
103 }
104 
105 /*******************************************************************************
106  *
107  * Function         bta_dm_co_lk_upgrade
108  *
109  * Description      This callout function is executed by DM to check if the
110  *                  platform wants allow link key upgrade
111  *
112  * Parameters       bd_addr  - The peer device
113  *                  *p_upgrade - true, if link key upgrade is desired.
114  *
115  * Returns          void.
116  *
117  ******************************************************************************/
bta_dm_co_lk_upgrade(UNUSED_ATTR BD_ADDR bd_addr,UNUSED_ATTR bool * p_upgrade)118 void bta_dm_co_lk_upgrade(UNUSED_ATTR BD_ADDR bd_addr,
119                           UNUSED_ATTR bool* p_upgrade) {}
120 
121 /*******************************************************************************
122  *
123  * Function         bta_dm_co_loc_oob
124  *
125  * Description      This callout function is executed by DM to report the OOB
126  *                  data of the local device for the Simple Pairing process
127  *
128  * Parameters       valid - true, if the local OOB data is retrieved from LM
129  *                  c     - Simple Pairing Hash C
130  *                  r     - Simple Pairing Randomnizer R
131  *
132  * Returns          void.
133  *
134  ******************************************************************************/
bta_dm_co_loc_oob(bool valid,BT_OCTET16 c,BT_OCTET16 r)135 void bta_dm_co_loc_oob(bool valid, BT_OCTET16 c, BT_OCTET16 r) {
136   BTIF_TRACE_DEBUG("bta_dm_co_loc_oob, valid = %d", valid);
137 #ifdef BTIF_DM_OOB_TEST
138   btif_dm_proc_loc_oob(valid, c, r);
139 #endif
140 }
141 
142 /*******************************************************************************
143  *
144  * Function         bta_dm_co_rmt_oob
145  *
146  * Description      This callout function is executed by DM to request the OOB
147  *                  data for the remote device for the Simple Pairing process
148  *                  Need to call bta_dm_ci_rmt_oob() in response
149  *
150  * Parameters       bd_addr  - The peer device
151  *
152  * Returns          void.
153  *
154  ******************************************************************************/
bta_dm_co_rmt_oob(BD_ADDR bd_addr)155 void bta_dm_co_rmt_oob(BD_ADDR bd_addr) {
156   BT_OCTET16 p_c;
157   BT_OCTET16 p_r;
158   bool result = false;
159 
160 #ifdef BTIF_DM_OOB_TEST
161   result = btif_dm_proc_rmt_oob(bd_addr, p_c, p_r);
162 #endif
163 
164   BTIF_TRACE_DEBUG("bta_dm_co_rmt_oob: result=%d", result);
165   bta_dm_ci_rmt_oob(result, bd_addr, p_c, p_r);
166 }
167 
168 // REMOVE FOR BLUEDROID ?
169 
170 #if (BTM_SCO_HCI_INCLUDED == TRUE) && (BTM_SCO_INCLUDED == TRUE)
171 
172 /*******************************************************************************
173  *
174  * Function         btui_sco_codec_callback
175  *
176  * Description      Callback for btui codec.
177  *
178  *
179  * Returns          void
180  *
181  ******************************************************************************/
btui_sco_codec_callback(uint16_t event,uint16_t sco_handle)182 static void btui_sco_codec_callback(uint16_t event, uint16_t sco_handle) {
183   bta_dm_sco_ci_data_ready(event, sco_handle);
184 }
185 
186 /*******************************************************************************
187  *
188  * Function         bta_dm_sco_co_open
189  *
190  * Description      This function is executed when a SCO connection is open.
191  *
192  *
193  * Returns          void
194  *
195  ******************************************************************************/
bta_dm_sco_co_open(uint16_t handle,uint8_t pkt_size,uint16_t event)196 void bta_dm_sco_co_open(uint16_t handle, uint8_t pkt_size, uint16_t event) {
197   tBTUI_SCO_CODEC_CFG cfg;
198 
199   if (btui_cb.sco_hci) {
200     BTIF_TRACE_DEBUG("bta_dm_sco_co_open handle:%d pkt_size:%d", handle,
201                      pkt_size);
202     cfg.p_cback = btui_sco_codec_callback;
203     cfg.pkt_size = pkt_size;
204     cfg.cb_event = event;
205     /* open and start the codec */
206     btui_sco_codec_open(&cfg);
207     btui_sco_codec_start(handle);
208   }
209 }
210 
211 /*******************************************************************************
212  *
213  * Function         bta_dm_sco_co_close
214  *
215  * Description      This function is called when a SCO connection is closed
216  *
217  *
218  * Returns          void
219  *
220  ******************************************************************************/
bta_dm_sco_co_close(void)221 void bta_dm_sco_co_close(void) {
222   if (btui_cb.sco_hci) {
223     BTIF_TRACE_DEBUG("bta_dm_sco_co_close close codec");
224     /* close sco codec */
225     btui_sco_codec_close();
226 
227     btui_cb.sco_hci = false;
228   }
229 }
230 
231 /*******************************************************************************
232  *
233  * Function         bta_dm_sco_co_in_data
234  *
235  * Description      This function is called to send incoming SCO data to
236  *                  application.
237  *
238  * Returns          void
239  *
240  ******************************************************************************/
bta_dm_sco_co_in_data(BT_HDR * p_buf)241 void bta_dm_sco_co_in_data(BT_HDR* p_buf) {
242   if (btui_cfg.sco_use_mic)
243     btui_sco_codec_inqdata(p_buf);
244   else
245     osi_free(p_buf);
246 }
247 
248 /*******************************************************************************
249  *
250  * Function         bta_dm_sco_co_out_data
251  *
252  * Description      This function is called to send SCO data over HCI.
253  *
254  * Returns          void
255  *
256  ******************************************************************************/
bta_dm_sco_co_out_data(BT_HDR ** p_buf)257 void bta_dm_sco_co_out_data(BT_HDR** p_buf) { btui_sco_codec_readbuf(p_buf); }
258 
259 #endif /* (BTM_SCO_HCI_INCLUDED == TRUE) && (BTM_SCO_INCLUDED == TRUE)*/
260 
261 /*******************************************************************************
262  *
263  * Function         bta_dm_co_le_io_key_req
264  *
265  * Description      This callout function is executed by DM to get BLE key
266  *                  information
267  *                  before SMP pairing gets going.
268  *
269  * Parameters       bd_addr  - The peer device
270  *                  *p_max_key_size - max key size local device supported.
271  *                  *p_init_key - initiator keys.
272  *                  *p_resp_key - responder keys.
273  *
274  * Returns          void.
275  *
276  ******************************************************************************/
bta_dm_co_le_io_key_req(UNUSED_ATTR BD_ADDR bd_addr,uint8_t * p_max_key_size,tBTA_LE_KEY_TYPE * p_init_key,tBTA_LE_KEY_TYPE * p_resp_key)277 void bta_dm_co_le_io_key_req(UNUSED_ATTR BD_ADDR bd_addr,
278                              uint8_t* p_max_key_size,
279                              tBTA_LE_KEY_TYPE* p_init_key,
280                              tBTA_LE_KEY_TYPE* p_resp_key) {
281   BTIF_TRACE_ERROR("##################################");
282   BTIF_TRACE_ERROR("bta_dm_co_le_io_key_req: only setting max size to 16");
283   BTIF_TRACE_ERROR("##################################");
284   *p_max_key_size = 16;
285   *p_init_key = *p_resp_key =
286       (BTA_LE_KEY_PENC | BTA_LE_KEY_PID | BTA_LE_KEY_PCSRK | BTA_LE_KEY_LENC |
287        BTA_LE_KEY_LID | BTA_LE_KEY_LCSRK);
288 }
289 
290 /*******************************************************************************
291  *
292  * Function         bta_dm_co_ble_local_key_reload
293  *
294  * Description      This callout function is to load the local BLE keys if
295  *                  available on the device.
296  *
297  * Parameters       none
298  *
299  * Returns          void.
300  *
301  ******************************************************************************/
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)302 void bta_dm_co_ble_load_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK* p_key_mask,
303                                    BT_OCTET16 er,
304                                    tBTA_BLE_LOCAL_ID_KEYS* p_id_keys) {
305   BTIF_TRACE_DEBUG("##################################");
306   BTIF_TRACE_DEBUG(
307       "bta_dm_co_ble_load_local_keys:  Load local keys if any are persisted");
308   BTIF_TRACE_DEBUG("##################################");
309   btif_dm_get_ble_local_keys(p_key_mask, er, p_id_keys);
310 }
311 
312 /*******************************************************************************
313  *
314  * Function         bta_dm_co_ble_io_req
315  *
316  * Description      This callout function is executed by DM to get BLE IO
317  *                  capabilities before SMP pairing gets going.
318  *
319  * Parameters       bd_addr  - The peer device
320  *                  *p_io_cap - The local Input/Output capabilities
321  *                  *p_oob_data - true, if OOB data is available for the peer
322  *                                device.
323  *                  *p_auth_req -  Auth request setting (Bonding and MITM
324  *                                 required or not)
325  *                  *p_max_key_size - max key size local device supported.
326  *                  *p_init_key - initiator keys.
327  *                  *p_resp_key - responder keys.
328  *
329  * Returns          void.
330  *
331  ******************************************************************************/
bta_dm_co_ble_io_req(UNUSED_ATTR BD_ADDR bd_addr,tBTA_IO_CAP * p_io_cap,tBTA_OOB_DATA * p_oob_data,tBTA_LE_AUTH_REQ * p_auth_req,uint8_t * p_max_key_size,tBTA_LE_KEY_TYPE * p_init_key,tBTA_LE_KEY_TYPE * p_resp_key)332 void bta_dm_co_ble_io_req(UNUSED_ATTR BD_ADDR bd_addr, tBTA_IO_CAP* p_io_cap,
333                           tBTA_OOB_DATA* p_oob_data,
334                           tBTA_LE_AUTH_REQ* p_auth_req, uint8_t* p_max_key_size,
335                           tBTA_LE_KEY_TYPE* p_init_key,
336                           tBTA_LE_KEY_TYPE* p_resp_key) {
337   /* Retrieve the properties from file system if possible */
338   tBTE_APPL_CFG nv_config;
339   if (btif_dm_get_smp_config(&nv_config)) bte_appl_cfg = nv_config;
340 
341   /* *p_auth_req by default is false for devices with NoInputNoOutput; true for
342    * other devices. */
343 
344   if (bte_appl_cfg.ble_auth_req)
345     *p_auth_req = bte_appl_cfg.ble_auth_req |
346                   (bte_appl_cfg.ble_auth_req & 0x04) | ((*p_auth_req) & 0x04);
347 
348   /* if OOB is not supported, this call-out function does not need to do
349    * anything
350    * otherwise, look for the OOB data associated with the address and set
351    * *p_oob_data accordingly.
352    * If the answer can not be obtained right away,
353    * set *p_oob_data to BTA_OOB_UNKNOWN and call bta_dm_ci_io_req() when the
354    * answer is available.
355    */
356 
357   btif_dm_set_oob_for_le_io_req(bd_addr, p_oob_data, p_auth_req);
358 
359   if (bte_appl_cfg.ble_io_cap <= 4) *p_io_cap = bte_appl_cfg.ble_io_cap;
360 
361   if (bte_appl_cfg.ble_init_key <= BTM_BLE_INITIATOR_KEY_SIZE)
362     *p_init_key = bte_appl_cfg.ble_init_key;
363 
364   if (bte_appl_cfg.ble_resp_key <= BTM_BLE_RESPONDER_KEY_SIZE)
365     *p_resp_key = bte_appl_cfg.ble_resp_key;
366 
367   if (bte_appl_cfg.ble_max_key_size > 7 && bte_appl_cfg.ble_max_key_size <= 16)
368     *p_max_key_size = bte_appl_cfg.ble_max_key_size;
369 }
370