OSDN Git Service

am 65aec3a3: am 6d429d9d: (-s ours) am 3efb9e2e: DO NOT MERGE Change pairing_cb to...
[android-x86/system-bt.git] / btif / src / btif_dm.c
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
19 /************************************************************************************
20  *
21  *  Filename:      btif_dm.c
22  *
23  *  Description:   Contains Device Management (DM) related functionality
24  *
25  *
26  ***********************************************************************************/
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30
31 #include <hardware/bluetooth.h>
32
33 #include <utils/Log.h>
34 #include <cutils/properties.h>
35 #include "gki.h"
36 #include "btu.h"
37 #include "bd.h"
38 #include "bta_api.h"
39 #include "btif_api.h"
40 #include "btif_util.h"
41 #include "btif_dm.h"
42 #include "btif_storage.h"
43 #include "btif_hh.h"
44 #include "btif_config.h"
45
46 #include "bta_gatt_api.h"
47 /******************************************************************************
48 **  Constants & Macros
49 ******************************************************************************/
50
51 #define COD_UNCLASSIFIED ((0x1F) << 8)
52 #define COD_HID_KEYBOARD                    0x0540
53 #define COD_HID_POINTING                    0x0580
54 #define COD_HID_COMBO                       0x05C0
55 #define COD_HID_MAJOR                       0x0500
56 #define COD_AV_HEADSETS                     0x0404
57 #define COD_AV_HANDSFREE                    0x0408
58 #define COD_AV_HEADPHONES                   0x0418
59 #define COD_AV_PORTABLE_AUDIO               0x041C
60 #define COD_AV_HIFI_AUDIO                   0x0428
61
62
63 #define BTIF_DM_DEFAULT_INQ_MAX_RESULTS     0
64 #define BTIF_DM_DEFAULT_INQ_MAX_DURATION    10
65 #define BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING 2
66
67 #define PROPERTY_PRODUCT_MODEL "ro.product.model"
68 #define DEFAULT_LOCAL_NAME_MAX  31
69 #if (DEFAULT_LOCAL_NAME_MAX > BTM_MAX_LOC_BD_NAME_LEN)
70     #error "default btif local name size exceeds stack supported length"
71 #endif
72
73 #if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
74 #define BTIF_DM_INTERLEAVE_DURATION_BR_ONE    2
75 #define BTIF_DM_INTERLEAVE_DURATION_LE_ONE    2
76 #define BTIF_DM_INTERLEAVE_DURATION_BR_TWO    3
77 #define BTIF_DM_INTERLEAVE_DURATION_LE_TWO    4
78 #endif
79
80 #define BOND_TYPE_UNKNOWN     0
81 #define BOND_TYPE_PERSISTENT  1
82 #define BOND_TYPE_TEMPORARY   2
83
84 typedef struct
85 {
86     bt_bond_state_t state;
87     BD_ADDR bd_addr;
88     UINT8   bond_type;
89     UINT8   pin_code_len;
90     UINT8   is_ssp;
91     UINT8   autopair_attempts;
92     UINT8   is_local_initiated;
93     UINT8   sdp_attempts;
94 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
95     BOOLEAN          is_le_only;
96     btif_dm_ble_cb_t ble;
97 #endif
98 } btif_dm_pairing_cb_t;
99
100
101 typedef struct
102 {
103     UINT8       ir[BT_OCTET16_LEN];
104     UINT8       irk[BT_OCTET16_LEN];
105     UINT8       dhk[BT_OCTET16_LEN];
106 }btif_dm_local_key_id_t;
107
108 typedef struct
109 {
110     BOOLEAN                 is_er_rcvd;
111     UINT8                   er[BT_OCTET16_LEN];
112     BOOLEAN                 is_id_keys_rcvd;
113     btif_dm_local_key_id_t  id_keys;  /* ID kyes */
114
115 }btif_dm_local_key_cb_t;
116
117 typedef struct
118 {
119     BD_ADDR bd_addr;
120     BD_NAME bd_name;
121 } btif_dm_remote_name_t;
122
123 typedef struct
124 {
125     BT_OCTET16 sp_c;
126     BT_OCTET16 sp_r;
127     BD_ADDR  oob_bdaddr;  /* peer bdaddr*/
128 } btif_dm_oob_cb_t;
129 #define BTA_SERVICE_ID_TO_SERVICE_MASK(id)       (1 << (id))
130
131 /* This flag will be true if HCI_Inquiry is in progress */
132 static BOOLEAN btif_dm_inquiry_in_progress = FALSE;
133
134 /************************************************************************************
135 **  Static variables
136 ************************************************************************************/
137 static char btif_default_local_name[DEFAULT_LOCAL_NAME_MAX+1] = {'\0'};
138
139 /******************************************************************************
140 **  Static functions
141 ******************************************************************************/
142 static btif_dm_pairing_cb_t pairing_cb;
143 static btif_dm_oob_cb_t     oob_cb;
144 static void btif_dm_generic_evt(UINT16 event, char* p_param);
145 static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr);
146 static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name);
147 static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
148                                           DEV_CLASS dev_class, tBT_DEVICE_TYPE dev_type);
149 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
150 static btif_dm_local_key_cb_t ble_local_key_cb;
151 static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif);
152 static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl);
153 static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req);
154 #endif
155 static char* btif_get_default_local_name();
156 /******************************************************************************
157 **  Externs
158 ******************************************************************************/
159 extern UINT16 bta_service_id_to_uuid_lkup_tbl [BTA_MAX_SERVICE_ID];
160 extern bt_status_t btif_hf_execute_service(BOOLEAN b_enable);
161 extern bt_status_t btif_av_execute_service(BOOLEAN b_enable);
162 extern bt_status_t btif_hh_execute_service(BOOLEAN b_enable);
163 extern int btif_hh_connect(bt_bdaddr_t *bd_addr);
164 extern void bta_gatt_convert_uuid16_to_uuid128(UINT8 uuid_128[LEN_UUID_128], UINT16 uuid_16);
165
166
167 /******************************************************************************
168 **  Functions
169 ******************************************************************************/
170
171 bt_status_t btif_in_execute_service_request(tBTA_SERVICE_ID service_id,
172                                                 BOOLEAN b_enable)
173 {
174     /* Check the service_ID and invoke the profile's BT state changed API */
175     switch (service_id)
176     {
177          case BTA_HFP_SERVICE_ID:
178          case BTA_HSP_SERVICE_ID:
179          {
180               btif_hf_execute_service(b_enable);
181          }break;
182          case BTA_A2DP_SERVICE_ID:
183          {
184               btif_av_execute_service(b_enable);
185          }break;
186          case BTA_HID_SERVICE_ID:
187          {
188               btif_hh_execute_service(b_enable);
189          }break;
190
191          default:
192               BTIF_TRACE_ERROR1("%s: Unknown service being enabled", __FUNCTION__);
193               return BT_STATUS_FAIL;
194     }
195     return BT_STATUS_SUCCESS;
196 }
197
198 /*******************************************************************************
199 **
200 ** Function         check_eir_remote_name
201 **
202 ** Description      Check if remote name is in the EIR data
203 **
204 ** Returns          TRUE if remote name found
205 **                  Populate p_remote_name, if provided and remote name found
206 **
207 *******************************************************************************/
208 static BOOLEAN check_eir_remote_name(tBTA_DM_SEARCH *p_search_data,
209                             UINT8 *p_remote_name, UINT8 *p_remote_name_len)
210 {
211     UINT8 *p_eir_remote_name = NULL;
212     UINT8 remote_name_len = 0;
213
214     /* Check EIR for remote name and services */
215     if (p_search_data->inq_res.p_eir)
216     {
217         p_eir_remote_name = BTA_CheckEirData(p_search_data->inq_res.p_eir,
218                 BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len);
219         if (!p_eir_remote_name)
220         {
221             p_eir_remote_name = BTA_CheckEirData(p_search_data->inq_res.p_eir,
222                     BTM_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len);
223         }
224
225         if (p_eir_remote_name)
226         {
227             if (remote_name_len > BD_NAME_LEN)
228                 remote_name_len = BD_NAME_LEN;
229
230             if (p_remote_name && p_remote_name_len)
231             {
232                 memcpy(p_remote_name, p_eir_remote_name, remote_name_len);
233                 *(p_remote_name + remote_name_len) = 0;
234                 *p_remote_name_len = remote_name_len;
235             }
236
237             return TRUE;
238         }
239     }
240
241     return FALSE;
242
243 }
244
245 /*******************************************************************************
246 **
247 ** Function         check_cached_remote_name
248 **
249 ** Description      Check if remote name is in the NVRAM cache
250 **
251 ** Returns          TRUE if remote name found
252 **                  Populate p_remote_name, if provided and remote name found
253 **
254 *******************************************************************************/
255 static BOOLEAN check_cached_remote_name(tBTA_DM_SEARCH *p_search_data,
256                                 UINT8 *p_remote_name, UINT8 *p_remote_name_len)
257 {
258     bt_bdname_t bdname;
259     bt_bdaddr_t remote_bdaddr;
260     bt_property_t prop_name;
261
262     /* check if we already have it in our btif_storage cache */
263     bdcpy(remote_bdaddr.address, p_search_data->inq_res.bd_addr);
264     BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_BDNAME,
265                                sizeof(bt_bdname_t), &bdname);
266     if (btif_storage_get_remote_device_property(
267         &remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
268     {
269         if (p_remote_name && p_remote_name_len)
270         {
271             strcpy((char *)p_remote_name, (char *)bdname.name);
272             *p_remote_name_len = strlen((char *)p_remote_name);
273         }
274         return TRUE;
275     }
276
277     return FALSE;
278 }
279
280 BOOLEAN check_cod(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
281 {
282     uint32_t    remote_cod;
283     bt_property_t prop_name;
284
285     /* check if we already have it in our btif_storage cache */
286     BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
287                                sizeof(uint32_t), &remote_cod);
288     if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
289     {
290         if ((remote_cod & 0x7ff) == cod)
291             return TRUE;
292     }
293
294     return FALSE;
295 }
296
297 BOOLEAN check_cod_hid(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
298 {
299     uint32_t    remote_cod;
300     bt_property_t prop_name;
301
302     /* check if we already have it in our btif_storage cache */
303     BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
304                                sizeof(uint32_t), &remote_cod);
305     if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
306                                 &prop_name) == BT_STATUS_SUCCESS)
307     {
308         if ((remote_cod & 0x700) == cod)
309             return TRUE;
310     }
311     return FALSE;
312 }
313
314 BOOLEAN check_hid_le(const bt_bdaddr_t *remote_bdaddr)
315 {
316     uint32_t    remote_dev_type;
317     bt_property_t prop_name;
318
319     /* check if we already have it in our btif_storage cache */
320     BTIF_STORAGE_FILL_PROPERTY(&prop_name,BT_PROPERTY_TYPE_OF_DEVICE,
321                                sizeof(uint32_t), &remote_dev_type);
322     if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
323                                 &prop_name) == BT_STATUS_SUCCESS)
324     {
325         if (remote_dev_type == BT_DEVICE_DEVTYPE_BLE)
326         {
327             bdstr_t bdstr;
328             bd2str(remote_bdaddr, &bdstr);
329             if(btif_config_exist("Remote", bdstr, "HidAppId"))
330                 return TRUE;
331         }
332     }
333     return FALSE;
334 }
335
336 static void bond_state_changed(bt_status_t status, bt_bdaddr_t *bd_addr, bt_bond_state_t state)
337 {
338     /* Send bonding state only once - based on outgoing/incoming we may receive duplicates */
339     if ( (pairing_cb.state == state) && (state == BT_BOND_STATE_BONDING) )
340         return;
341
342     if (pairing_cb.bond_type == BOND_TYPE_TEMPORARY)
343     {
344        state = BT_BOND_STATE_NONE;
345     }
346     BTIF_TRACE_DEBUG3("%s: state=%d prev_state=%d", __FUNCTION__, state, pairing_cb.state);
347
348     HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state);
349
350     if (state == BT_BOND_STATE_BONDING)
351     {
352         pairing_cb.state = state;
353         bdcpy(pairing_cb.bd_addr, bd_addr->address);
354     }
355     else
356     {
357         memset(&pairing_cb, 0, sizeof(pairing_cb));
358     }
359
360 }
361
362 /* store remote version in bt config to always have access
363    to it post pairing*/
364 static void btif_update_remote_version_property(bt_bdaddr_t *p_bd)
365 {
366     bt_property_t property;
367     UINT8 lmp_ver = 0;
368     UINT16 lmp_subver = 0;
369     UINT16 mfct_set = 0;
370     tBTM_STATUS btm_status;
371     bt_remote_version_t info;
372     bt_status_t status;
373     bdstr_t bdstr;
374
375     btm_status = BTM_ReadRemoteVersion(*(BD_ADDR*)p_bd, &lmp_ver,
376                           &mfct_set, &lmp_subver);
377
378     ALOGD("remote version info [%s]: %x, %x, %x", bd2str(p_bd, &bdstr),
379                lmp_ver, mfct_set, lmp_subver);
380
381     if (btm_status == BTM_SUCCESS)
382     {
383         /* always update cache to ensure we have availability whenever BTM API
384            is not populated */
385         info.manufacturer = mfct_set;
386         info.sub_ver = lmp_subver;
387         info.version = lmp_ver;
388         BTIF_STORAGE_FILL_PROPERTY(&property,
389                             BT_PROPERTY_REMOTE_VERSION_INFO, sizeof(bt_remote_version_t),
390                             &info);
391         status = btif_storage_set_remote_device_property(p_bd, &property);
392         ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote version", status);
393     }
394 }
395
396
397 static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
398                                           DEV_CLASS dev_class, tBT_DEVICE_TYPE device_type)
399 {
400     int num_properties = 0;
401     bt_property_t properties[3];
402     bt_bdaddr_t bdaddr;
403     bt_status_t status;
404     UINT32 cod;
405     bt_device_type_t dev_type;
406
407     memset(properties, 0, sizeof(properties));
408     bdcpy(bdaddr.address, bd_addr);
409
410     /* remote name */
411     if (strlen((const char *) bd_name))
412     {
413         BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
414                             BT_PROPERTY_BDNAME, strlen((char *)bd_name), bd_name);
415         status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
416         ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device name", status);
417         num_properties++;
418     }
419
420     /* class of device */
421     cod = devclass2uint(dev_class);
422     if ( cod == 0) {
423         BTIF_TRACE_DEBUG1("%s():cod is 0, set as unclassified", __FUNCTION__);
424         cod = COD_UNCLASSIFIED;
425     }
426
427     BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
428                         BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
429     status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
430     ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device class", status);
431     num_properties++;
432
433     /* device type */
434     dev_type = device_type;
435     BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
436                         BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
437     status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
438     ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device type", status);
439     num_properties++;
440
441     HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
442                      status, &bdaddr, num_properties, properties);
443 }
444 /*******************************************************************************
445 **
446 ** Function         hid_remote_name_cback
447 **
448 ** Description      Remote name callback for HID device. Called in stack context
449 **                  Special handling for HID devices
450 **
451 ** Returns          void
452 **
453 *******************************************************************************/
454 static void hid_remote_name_cback(void *p_param)
455 {
456     BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
457
458     btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_HID_REMOTE_NAME,
459         (char *)p_param, sizeof(tBTM_REMOTE_DEV_NAME), NULL);
460 }
461
462 /*******************************************************************************
463 **
464 ** Function         btif_dm_cb_hid_remote_name
465 **
466 ** Description      Remote name callback for HID device. Called in btif context
467 **                  Special handling for HID devices
468 **
469 ** Returns          void
470 **
471 *******************************************************************************/
472 static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name)
473 {
474     BTIF_TRACE_DEBUG3("%s: status=%d pairing_cb.state=%d", __FUNCTION__, p_remote_name->status, pairing_cb.state);
475     if (pairing_cb.state == BT_BOND_STATE_BONDING)
476     {
477         bt_bdaddr_t remote_bd;
478
479         bdcpy(remote_bd.address, pairing_cb.bd_addr);
480
481         if (p_remote_name->status == BTM_SUCCESS)
482         {
483             bond_state_changed(BT_STATUS_SUCCESS, &remote_bd, BT_BOND_STATE_BONDED);
484         }
485         else
486             bond_state_changed(BT_STATUS_FAIL, &remote_bd, BT_BOND_STATE_NONE);
487     }
488 }
489
490 /*******************************************************************************
491 **
492 ** Function         btif_dm_cb_create_bond
493 **
494 ** Description      Create bond initiated from the BTIF thread context
495 **                  Special handling for HID devices
496 **
497 ** Returns          void
498 **
499 *******************************************************************************/
500 static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr)
501 {
502     BOOLEAN is_hid = check_cod(bd_addr, COD_HID_POINTING);
503
504
505     bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_BONDING);
506
507     if (is_hid){
508
509             int status;
510             status = btif_hh_connect(bd_addr);
511             if(status != BT_STATUS_SUCCESS)
512                 bond_state_changed(status, bd_addr, BT_BOND_STATE_NONE);
513     }
514     else
515     {
516 #if BLE_INCLUDED == TRUE
517         int device_type;
518         int addr_type;
519         bdstr_t bdstr;
520         bd2str(bd_addr, &bdstr);
521         if(btif_config_get_int("Remote", (char const *)&bdstr,"DevType", &device_type) &&
522            (btif_storage_get_remote_addr_type(bd_addr, &addr_type) == BT_STATUS_SUCCESS) &&
523            (device_type == BT_DEVICE_TYPE_BLE))
524         {
525             BTA_DmAddBleDevice(bd_addr->address, addr_type, BT_DEVICE_TYPE_BLE);
526         }
527 #endif
528         BTA_DmBond ((UINT8 *)bd_addr->address);
529     }
530     /*  Track  originator of bond creation  */
531     pairing_cb.is_local_initiated = TRUE;
532
533 }
534
535 /*******************************************************************************
536 **
537 ** Function         btif_dm_cb_remove_bond
538 **
539 ** Description      remove bond initiated from the BTIF thread context
540 **                  Special handling for HID devices
541 **
542 ** Returns          void
543 **
544 *******************************************************************************/
545 void btif_dm_cb_remove_bond(bt_bdaddr_t *bd_addr)
546 {
547      bdstr_t bdstr;
548      /*special handling for HID devices */
549      /*  VUP needs to be sent if its a HID Device. The HID HOST module will check if there
550      is a valid hid connection with this bd_addr. If yes VUP will be issued.*/
551 #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
552     if (btif_hh_virtual_unplug(bd_addr) != BT_STATUS_SUCCESS)
553 #endif
554     {
555          BTA_DmRemoveDevice((UINT8 *)bd_addr->address);
556     }
557 }
558
559 /*******************************************************************************
560 **
561 ** Function         search_devices_copy_cb
562 **
563 ** Description      Deep copy callback for search devices event
564 **
565 ** Returns          void
566 **
567 *******************************************************************************/
568 static void search_devices_copy_cb(UINT16 event, char *p_dest, char *p_src)
569 {
570     tBTA_DM_SEARCH *p_dest_data =  (tBTA_DM_SEARCH *) p_dest;
571     tBTA_DM_SEARCH *p_src_data =  (tBTA_DM_SEARCH *) p_src;
572
573     if (!p_src)
574         return;
575
576     BTIF_TRACE_DEBUG2("%s: event=%s", __FUNCTION__, dump_dm_search_event(event));
577     memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
578     switch (event)
579     {
580         case BTA_DM_INQ_RES_EVT:
581         {
582             if (p_src_data->inq_res.p_eir)
583             {
584                 p_dest_data->inq_res.p_eir = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
585                 memcpy(p_dest_data->inq_res.p_eir, p_src_data->inq_res.p_eir, HCI_EXT_INQ_RESPONSE_LEN);
586             }
587         }
588         break;
589
590         case BTA_DM_DISC_RES_EVT:
591         {
592             if (p_src_data->disc_res.raw_data_size && p_src_data->disc_res.p_raw_data)
593             {
594                 p_dest_data->disc_res.p_raw_data = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
595                 memcpy(p_dest_data->disc_res.p_raw_data,
596                     p_src_data->disc_res.p_raw_data, p_src_data->disc_res.raw_data_size);
597             }
598         }
599         break;
600     }
601 }
602
603 static void search_services_copy_cb(UINT16 event, char *p_dest, char *p_src)
604 {
605     tBTA_DM_SEARCH *p_dest_data =  (tBTA_DM_SEARCH *) p_dest;
606     tBTA_DM_SEARCH *p_src_data =  (tBTA_DM_SEARCH *) p_src;
607
608     if (!p_src)
609         return;
610     memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
611     switch (event)
612     {
613          case BTA_DM_DISC_RES_EVT:
614          {
615               if (p_src_data->disc_res.result == BTA_SUCCESS)
616               {
617                   if (p_src_data->disc_res.num_uuids > 0)
618                   {
619                        p_dest_data->disc_res.p_uuid_list =
620                                                         (UINT8*)(p_dest + sizeof(tBTA_DM_SEARCH));
621                        memcpy(p_dest_data->disc_res.p_uuid_list, p_src_data->disc_res.p_uuid_list,
622                               p_src_data->disc_res.num_uuids*MAX_UUID_SIZE);
623                        GKI_freebuf(p_src_data->disc_res.p_uuid_list);
624                   }
625                   if (p_src_data->disc_res.p_raw_data != NULL)
626                   {
627                       GKI_freebuf(p_src_data->disc_res.p_raw_data);
628                   }
629               }
630          } break;
631     }
632 }
633 /******************************************************************************
634 **
635 **  BTIF DM callback events
636 **
637 *****************************************************************************/
638
639 /*******************************************************************************
640 **
641 ** Function         btif_dm_pin_req_evt
642 **
643 ** Description      Executes pin request event in btif context
644 **
645 ** Returns          void
646 **
647 *******************************************************************************/
648 static void btif_dm_pin_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
649 {
650     bt_bdaddr_t bd_addr;
651     bt_bdname_t bd_name;
652     UINT32 cod;
653     bt_pin_code_t pin_code;
654
655     /* Remote properties update */
656     btif_update_remote_properties(p_pin_req->bd_addr, p_pin_req->bd_name,
657                                   p_pin_req->dev_class, BT_DEVICE_TYPE_BREDR);
658
659     bdcpy(bd_addr.address, p_pin_req->bd_addr);
660     memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
661
662     bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
663
664     cod = devclass2uint(p_pin_req->dev_class);
665
666     if ( cod == 0) {
667         BTIF_TRACE_DEBUG1("%s():cod is 0, set as unclassified", __FUNCTION__);
668         cod = COD_UNCLASSIFIED;
669     }
670
671     /* check for auto pair possiblity only if bond was initiated by local device */
672     if (pairing_cb.is_local_initiated)
673     {
674         if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
675             check_cod(&bd_addr, COD_AV_HANDSFREE) ||
676             check_cod(&bd_addr, COD_AV_HEADPHONES) ||
677             check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
678             check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
679             check_cod(&bd_addr, COD_HID_POINTING))
680         {
681             BTIF_TRACE_DEBUG1("%s()cod matches for auto pair", __FUNCTION__);
682             /*  Check if this device can be auto paired  */
683             if ((btif_storage_is_device_autopair_blacklisted(&bd_addr) == FALSE) &&
684                 (pairing_cb.autopair_attempts == 0))
685             {
686                 BTIF_TRACE_DEBUG1("%s() Attempting auto pair", __FUNCTION__);
687                 pin_code.pin[0] = 0x30;
688                 pin_code.pin[1] = 0x30;
689                 pin_code.pin[2] = 0x30;
690                 pin_code.pin[3] = 0x30;
691
692                 pairing_cb.autopair_attempts++;
693                 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
694                 return;
695             }
696         }
697         else if (check_cod(&bd_addr, COD_HID_KEYBOARD) ||
698                  check_cod(&bd_addr, COD_HID_COMBO))
699         {
700             if(( btif_storage_is_fixed_pin_zeros_keyboard (&bd_addr) == TRUE) &&
701                (pairing_cb.autopair_attempts == 0))
702             {
703                 BTIF_TRACE_DEBUG1("%s() Attempting auto pair", __FUNCTION__);
704                 pin_code.pin[0] = 0x30;
705                 pin_code.pin[1] = 0x30;
706                 pin_code.pin[2] = 0x30;
707                 pin_code.pin[3] = 0x30;
708
709                 pairing_cb.autopair_attempts++;
710                 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
711                 return;
712             }
713         }
714     }
715     HAL_CBACK(bt_hal_cbacks, pin_request_cb,
716                      &bd_addr, &bd_name, cod);
717 }
718
719 /*******************************************************************************
720 **
721 ** Function         btif_dm_ssp_cfm_req_evt
722 **
723 ** Description      Executes SSP confirm request event in btif context
724 **
725 ** Returns          void
726 **
727 *******************************************************************************/
728 static void btif_dm_ssp_cfm_req_evt(tBTA_DM_SP_CFM_REQ *p_ssp_cfm_req)
729 {
730     bt_bdaddr_t bd_addr;
731     bt_bdname_t bd_name;
732     UINT32 cod;
733     BOOLEAN is_incoming = !(pairing_cb.state == BT_BOND_STATE_BONDING);
734
735     BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
736
737     /* Remote properties update */
738     btif_update_remote_properties(p_ssp_cfm_req->bd_addr, p_ssp_cfm_req->bd_name,
739                                   p_ssp_cfm_req->dev_class, BT_DEVICE_TYPE_BREDR);
740
741     bdcpy(bd_addr.address, p_ssp_cfm_req->bd_addr);
742     memcpy(bd_name.name, p_ssp_cfm_req->bd_name, BD_NAME_LEN);
743
744     /* Set the pairing_cb based on the local & remote authentication requirements */
745     bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
746
747     /* if just_works and bonding bit is not set treat this as temporary */
748     if (p_ssp_cfm_req->just_works && !(p_ssp_cfm_req->loc_auth_req & BTM_AUTH_BONDS) &&
749         !(p_ssp_cfm_req->rmt_auth_req & BTM_AUTH_BONDS) &&
750         !(check_cod((bt_bdaddr_t*)&p_ssp_cfm_req->bd_addr, COD_HID_POINTING)))
751         pairing_cb.bond_type = BOND_TYPE_TEMPORARY;
752     else
753         pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
754
755     pairing_cb.is_ssp = TRUE;
756
757     /* If JustWorks auto-accept */
758     if (p_ssp_cfm_req->just_works)
759     {
760         /* Pairing consent for JustWorks needed if:
761          * 1. Incoming pairing is detected AND
762          * 2. local IO capabilities are DisplayYesNo AND
763          * 3. remote IO capabiltiies are DisplayOnly or NoInputNoOutput;
764          */
765         if ((is_incoming) && ((p_ssp_cfm_req->loc_io_caps == 0x01) &&
766                 (p_ssp_cfm_req->rmt_io_caps == 0x00 || p_ssp_cfm_req->rmt_io_caps == 0x03)))
767         {
768             BTIF_TRACE_EVENT3("%s: User consent needed for incoming pairing request. loc_io_caps: %d, rmt_io_caps: %d",
769                 __FUNCTION__, p_ssp_cfm_req->loc_io_caps, p_ssp_cfm_req->rmt_io_caps);
770         }
771         else
772         {
773             BTIF_TRACE_EVENT1("%s: Auto-accept JustWorks pairing", __FUNCTION__);
774             btif_dm_ssp_reply(&bd_addr, BT_SSP_VARIANT_CONSENT, TRUE, 0);
775             return;
776         }
777     }
778
779     cod = devclass2uint(p_ssp_cfm_req->dev_class);
780
781     if ( cod == 0) {
782         ALOGD("cod is 0, set as unclassified");
783         cod = COD_UNCLASSIFIED;
784     }
785
786     pairing_cb.sdp_attempts = 0;
787     HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
788                      (p_ssp_cfm_req->just_works ? BT_SSP_VARIANT_CONSENT : BT_SSP_VARIANT_PASSKEY_CONFIRMATION),
789                      p_ssp_cfm_req->num_val);
790 }
791
792 static void btif_dm_ssp_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
793 {
794     bt_bdaddr_t bd_addr;
795     bt_bdname_t bd_name;
796     UINT32 cod;
797
798     BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
799
800     /* Remote properties update */
801     btif_update_remote_properties(p_ssp_key_notif->bd_addr, p_ssp_key_notif->bd_name,
802                                   p_ssp_key_notif->dev_class, BT_DEVICE_TYPE_BREDR);
803
804     bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
805     memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
806
807     bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
808     pairing_cb.is_ssp = TRUE;
809     cod = devclass2uint(p_ssp_key_notif->dev_class);
810
811     if ( cod == 0) {
812         ALOGD("cod is 0, set as unclassified");
813         cod = COD_UNCLASSIFIED;
814     }
815
816     HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
817                      cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
818                      p_ssp_key_notif->passkey);
819 }
820 /*******************************************************************************
821 **
822 ** Function         btif_dm_auth_cmpl_evt
823 **
824 ** Description      Executes authentication complete event in btif context
825 **
826 ** Returns          void
827 **
828 *******************************************************************************/
829 static void btif_dm_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
830 {
831     /* Save link key, if not temporary */
832     bt_bdaddr_t bd_addr;
833     bt_status_t status = BT_STATUS_FAIL;
834     bt_bond_state_t state = BT_BOND_STATE_NONE;
835
836     bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
837     if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
838     {
839         if ((p_auth_cmpl->key_type < HCI_LKEY_TYPE_DEBUG_COMB)  || (p_auth_cmpl->key_type == HCI_LKEY_TYPE_AUTH_COMB) ||
840             (p_auth_cmpl->key_type == HCI_LKEY_TYPE_CHANGED_COMB) || pairing_cb.bond_type == BOND_TYPE_PERSISTENT)
841         {
842             bt_status_t ret;
843             BTIF_TRACE_DEBUG3("%s: Storing link key. key_type=0x%x, bond_type=%d",
844                 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.bond_type);
845             ret = btif_storage_add_bonded_device(&bd_addr,
846                                 p_auth_cmpl->key, p_auth_cmpl->key_type,
847                                 pairing_cb.pin_code_len);
848             ASSERTC(ret == BT_STATUS_SUCCESS, "storing link key failed", ret);
849         }
850         else
851         {
852             BTIF_TRACE_DEBUG3("%s: Temporary key. Not storing. key_type=0x%x, bond_type=%d",
853                 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.bond_type);
854             if(pairing_cb.bond_type == BOND_TYPE_TEMPORARY)
855             {
856                 BTIF_TRACE_DEBUG1("%s: sending BT_BOND_STATE_NONE for Temp pairing",
857                         __FUNCTION__);
858                 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
859                 return;
860             }
861         }
862     }
863     if (p_auth_cmpl->success)
864     {
865         status = BT_STATUS_SUCCESS;
866         state = BT_BOND_STATE_BONDED;
867
868         /* Trigger SDP on the device */
869         pairing_cb.sdp_attempts = 1;;
870
871         if(btif_dm_inquiry_in_progress)
872             btif_dm_cancel_discovery();
873
874         btif_dm_get_remote_services(&bd_addr);
875         /* Do not call bond_state_changed_cb yet. Wait till fetch remote service is complete */
876     }
877     else
878     {
879          /*Map the HCI fail reason  to  bt status  */
880         switch(p_auth_cmpl->fail_reason)
881         {
882             case HCI_ERR_PAGE_TIMEOUT:
883             case HCI_ERR_CONNECTION_TOUT:
884                 status =  BT_STATUS_RMT_DEV_DOWN;
885                 break;
886
887             /* map the auth failure codes, so we can retry pairing if necessary */
888             case HCI_ERR_AUTH_FAILURE:
889             case HCI_ERR_HOST_REJECT_SECURITY:
890             case HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE:
891             case HCI_ERR_UNIT_KEY_USED:
892             case HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED:
893             case HCI_ERR_INSUFFCIENT_SECURITY:
894                 BTIF_TRACE_DEBUG1(" %s() Authentication fail ", __FUNCTION__);
895                 if (pairing_cb.autopair_attempts  == 1)
896                 {
897                     BTIF_TRACE_DEBUG1("%s(): Adding device to blacklist ", __FUNCTION__);
898
899                     /* Add the device to dynamic black list only if this device belongs to Audio/pointing dev class  */
900                     if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
901                         check_cod(&bd_addr, COD_AV_HANDSFREE) ||
902                         check_cod(&bd_addr, COD_AV_HEADPHONES) ||
903                         check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
904                         check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
905                         check_cod(&bd_addr, COD_HID_POINTING))
906                     {
907                         btif_storage_add_device_to_autopair_blacklist (&bd_addr);
908                     }
909                     pairing_cb.autopair_attempts++;
910
911                     /* Create the Bond once again */
912                     BTIF_TRACE_DEBUG1("%s() auto pair failed. Reinitiate Bond", __FUNCTION__);
913                     btif_dm_cb_create_bond (&bd_addr);
914                     return;
915                 }
916                 else
917                 {
918                     /* if autopair attempts are more than 1, or not attempted */
919                     status =  BT_STATUS_AUTH_FAILURE;
920                 }
921                 break;
922
923             default:
924                 status =  BT_STATUS_FAIL;
925         }
926         bond_state_changed(status, &bd_addr, state);
927     }
928 }
929
930 /******************************************************************************
931 **
932 ** Function         btif_dm_search_devices_evt
933 **
934 ** Description      Executes search devices callback events in btif context
935 **
936 ** Returns          void
937 **
938 ******************************************************************************/
939 static void btif_dm_search_devices_evt (UINT16 event, char *p_param)
940 {
941     tBTA_DM_SEARCH *p_search_data;
942     BTIF_TRACE_EVENT2("%s event=%s", __FUNCTION__, dump_dm_search_event(event));
943
944     switch (event)
945     {
946         case BTA_DM_DISC_RES_EVT:
947         {
948             p_search_data = (tBTA_DM_SEARCH *)p_param;
949             /* Remote name update */
950             if (strlen((const char *) p_search_data->disc_res.bd_name))
951             {
952                 bt_property_t properties[1];
953                 bt_bdaddr_t bdaddr;
954                 bt_status_t status;
955
956                 properties[0].type = BT_PROPERTY_BDNAME;
957                 properties[0].val = p_search_data->disc_res.bd_name;
958                 properties[0].len = strlen((char *)p_search_data->disc_res.bd_name);
959                 bdcpy(bdaddr.address, p_search_data->disc_res.bd_addr);
960
961                 status = btif_storage_set_remote_device_property(&bdaddr, &properties[0]);
962                 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device property", status);
963                 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
964                                  status, &bdaddr, 1, properties);
965             }
966             /* TODO: Services? */
967         }
968         break;
969
970         case BTA_DM_INQ_RES_EVT:
971         {
972             /* inquiry result */
973             UINT32 cod;
974             UINT8 *p_eir_remote_name = NULL;
975             bt_bdname_t bdname;
976             bt_bdaddr_t bdaddr;
977             UINT8 remote_name_len;
978             UINT8 *p_cached_name = NULL;
979             tBTA_SERVICE_MASK services = 0;
980             bdstr_t bdstr;
981
982             p_search_data = (tBTA_DM_SEARCH *)p_param;
983             bdcpy(bdaddr.address, p_search_data->inq_res.bd_addr);
984
985             BTIF_TRACE_DEBUG3("%s() %s device_type = 0x%x\n", __FUNCTION__, bd2str(&bdaddr, &bdstr),
986 #if (BLE_INCLUDED == TRUE)
987                     p_search_data->inq_res.device_type);
988 #else
989                     BT_DEVICE_TYPE_BREDR);
990 #endif
991             bdname.name[0] = 0;
992
993             cod = devclass2uint (p_search_data->inq_res.dev_class);
994
995             if ( cod == 0) {
996                 ALOGD("cod is 0, set as unclassified");
997                 cod = COD_UNCLASSIFIED;
998             }
999
1000             if (!check_eir_remote_name(p_search_data, bdname.name, &remote_name_len))
1001                 check_cached_remote_name(p_search_data, bdname.name, &remote_name_len);
1002
1003             /* Check EIR for remote name and services */
1004             if (p_search_data->inq_res.p_eir)
1005             {
1006                 BTA_GetEirService(p_search_data->inq_res.p_eir, &services);
1007                 BTIF_TRACE_DEBUG2("%s()EIR BTA services = %08X", __FUNCTION__, (UINT32)services);
1008                 /* TODO:  Get the service list and check to see which uuids we got and send it back to the client. */
1009             }
1010
1011
1012             {
1013                 bt_property_t properties[5];
1014                 bt_device_type_t dev_type;
1015                 UINT8 addr_type;
1016                 uint32_t num_properties = 0;
1017                 bt_status_t status;
1018
1019                 memset(properties, 0, sizeof(properties));
1020                 /* BD_ADDR */
1021                 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1022                                     BT_PROPERTY_BDADDR, sizeof(bdaddr), &bdaddr);
1023                 num_properties++;
1024                 /* BD_NAME */
1025                 /* Don't send BDNAME if it is empty */
1026                 if (bdname.name[0])
1027                 {
1028                     BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1029                                                BT_PROPERTY_BDNAME,
1030                                                strlen((char *)bdname.name), &bdname);
1031                     num_properties++;
1032                 }
1033
1034                 /* DEV_CLASS */
1035                 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1036                                     BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
1037                 num_properties++;
1038                 /* DEV_TYPE */
1039 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1040                 /* FixMe: Assumption is that bluetooth.h and BTE enums match */
1041                 dev_type = p_search_data->inq_res.device_type;
1042                 addr_type = p_search_data->inq_res.ble_addr_type;
1043 #else
1044                 dev_type = BT_DEVICE_TYPE_BREDR;
1045 #endif
1046                 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1047                                     BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
1048                 num_properties++;
1049                 /* RSSI */
1050                 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1051                                     BT_PROPERTY_REMOTE_RSSI, sizeof(int8_t),
1052                                     &(p_search_data->inq_res.rssi));
1053                 num_properties++;
1054
1055                 status = btif_storage_add_remote_device(&bdaddr, num_properties, properties);
1056                 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device (inquiry)", status);
1057 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1058                 status = btif_storage_set_remote_addr_type(&bdaddr, addr_type);
1059                 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote addr type (inquiry)", status);
1060 #endif
1061                 /* Callback to notify upper layer of device */
1062                 HAL_CBACK(bt_hal_cbacks, device_found_cb,
1063                                  num_properties, properties);
1064             }
1065         }
1066         break;
1067
1068         case BTA_DM_INQ_CMPL_EVT:
1069         {
1070         }
1071         break;
1072         case BTA_DM_DISC_CMPL_EVT:
1073         {
1074             HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1075         }
1076         break;
1077         case BTA_DM_SEARCH_CANCEL_CMPL_EVT:
1078         {
1079            /* if inquiry is not in progress and we get a cancel event, then
1080             * it means we are done with inquiry, but remote_name fetches are in
1081             * progress
1082             *
1083             * if inquiry  is in progress, then we don't want to act on this cancel_cmpl_evt
1084             * but instead wait for the cancel_cmpl_evt via the Busy Level
1085             *
1086             */
1087            if (btif_dm_inquiry_in_progress == FALSE)
1088            {
1089                HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1090            }
1091         }
1092         break;
1093     }
1094 }
1095
1096 /*******************************************************************************
1097 **
1098 ** Function         btif_dm_search_services_evt
1099 **
1100 ** Description      Executes search services event in btif context
1101 **
1102 ** Returns          void
1103 **
1104 *******************************************************************************/
1105 static void btif_dm_search_services_evt(UINT16 event, char *p_param)
1106 {
1107     tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1108
1109     BTIF_TRACE_EVENT2("%s:  event = %d", __FUNCTION__, event);
1110     switch (event)
1111     {
1112         case BTA_DM_DISC_RES_EVT:
1113         {
1114             bt_uuid_t uuid_arr[BT_MAX_NUM_UUIDS]; /* Max 32 services */
1115             bt_property_t prop;
1116             uint32_t i = 0,  j = 0;
1117             bt_bdaddr_t bd_addr;
1118             bt_status_t ret;
1119
1120             bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1121
1122             BTIF_TRACE_DEBUG3("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
1123                     p_data->disc_res.result, p_data->disc_res.services);
1124             if  ((p_data->disc_res.result != BTA_SUCCESS) &&
1125                  (pairing_cb.state == BT_BOND_STATE_BONDING ) &&
1126                  (pairing_cb.sdp_attempts < BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING))
1127             {
1128                 BTIF_TRACE_WARNING1("%s:SDP failed after bonding re-attempting", __FUNCTION__);
1129                 pairing_cb.sdp_attempts++;
1130                 btif_dm_get_remote_services(&bd_addr);
1131                 return;
1132             }
1133             prop.type = BT_PROPERTY_UUIDS;
1134             prop.len = 0;
1135             if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0))
1136             {
1137                  prop.val = p_data->disc_res.p_uuid_list;
1138                  prop.len = p_data->disc_res.num_uuids * MAX_UUID_SIZE;
1139                  for (i=0; i < p_data->disc_res.num_uuids; i++)
1140                  {
1141                       char temp[256];
1142                       uuid_to_string((bt_uuid_t*)(p_data->disc_res.p_uuid_list + (i*MAX_UUID_SIZE)), temp);
1143                       BTIF_TRACE_ERROR2("Index: %d uuid:%s", i, temp);
1144                  }
1145             }
1146
1147             /* onUuidChanged requires getBondedDevices to be populated.
1148             ** bond_state_changed needs to be sent prior to remote_device_property
1149             */
1150             if ((pairing_cb.state == BT_BOND_STATE_BONDING) &&
1151                 (bdcmp(p_data->disc_res.bd_addr, pairing_cb.bd_addr) == 0)&&
1152                 pairing_cb.sdp_attempts > 0)
1153             {
1154                  BTIF_TRACE_DEBUG1("%s Remote Service SDP done. Call bond_state_changed_cb BONDED",
1155                                    __FUNCTION__);
1156                  pairing_cb.sdp_attempts  = 0;
1157                  bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDED);
1158             }
1159
1160             if(p_data->disc_res.num_uuids != 0)
1161             {
1162                 /* Also write this to the NVRAM */
1163                 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1164                 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1165                 /* Send the event to the BTIF */
1166                 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1167                                  BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1168             }
1169         }
1170         break;
1171
1172         case BTA_DM_DISC_CMPL_EVT:
1173             /* fixme */
1174         break;
1175
1176 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1177         case BTA_DM_DISC_BLE_RES_EVT:
1178              BTIF_TRACE_DEBUG2("%s:, services 0x%x)", __FUNCTION__,
1179                                 p_data->disc_ble_res.service.uu.uuid16);
1180              bt_uuid_t  uuid;
1181              int i = 0;
1182              int j = 15;
1183              if (p_data->disc_ble_res.service.uu.uuid16 == UUID_SERVCLASS_LE_HID)
1184              {
1185                 BTIF_TRACE_DEBUG1("%s: Found HOGP UUID",__FUNCTION__);
1186                 bt_property_t prop;
1187                 bt_bdaddr_t bd_addr;
1188                 char temp[256];
1189                 bt_status_t ret;
1190
1191                 bta_gatt_convert_uuid16_to_uuid128(uuid.uu,p_data->disc_ble_res.service.uu.uuid16);
1192
1193                 while(i < j )
1194                 {
1195                     unsigned char c = uuid.uu[j];
1196                     uuid.uu[j] = uuid.uu[i];
1197                     uuid.uu[i] = c;
1198                     i++;
1199                     j--;
1200                 }
1201
1202                 uuid_to_string(&uuid, temp);
1203                 BTIF_TRACE_ERROR1(" uuid:%s", temp);
1204
1205                 bdcpy(bd_addr.address, p_data->disc_ble_res.bd_addr);
1206                 prop.type = BT_PROPERTY_UUIDS;
1207                 prop.val = uuid.uu;
1208                 prop.len = MAX_UUID_SIZE;
1209
1210                 /* Also write this to the NVRAM */
1211                 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1212                 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1213
1214                 /* Send the event to the BTIF */
1215                 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1216                                  BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1217
1218             }
1219         break;
1220 #endif /* BLE_INCLUDED */
1221
1222         default:
1223         {
1224             ASSERTC(0, "unhandled search services event", event);
1225         }
1226         break;
1227     }
1228 }
1229
1230 /*******************************************************************************
1231 **
1232 ** Function         btif_dm_remote_service_record_evt
1233 **
1234 ** Description      Executes search service record event in btif context
1235 **
1236 ** Returns          void
1237 **
1238 *******************************************************************************/
1239 static void btif_dm_remote_service_record_evt(UINT16 event, char *p_param)
1240 {
1241     tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1242
1243     BTIF_TRACE_EVENT2("%s:  event = %d", __FUNCTION__, event);
1244     switch (event)
1245     {
1246         case BTA_DM_DISC_RES_EVT:
1247         {
1248             bt_service_record_t rec;
1249             bt_property_t prop;
1250             uint32_t i = 0;
1251             bt_bdaddr_t bd_addr;
1252
1253             memset(&rec, 0, sizeof(bt_service_record_t));
1254             bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1255
1256             BTIF_TRACE_DEBUG3("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
1257                     p_data->disc_res.result, p_data->disc_res.services);
1258             prop.type = BT_PROPERTY_SERVICE_RECORD;
1259             prop.val = (void*)&rec;
1260             prop.len = sizeof(rec);
1261
1262             /* disc_res.result is overloaded with SCN. Cannot check result */
1263             p_data->disc_res.services &= ~BTA_USER_SERVICE_MASK;
1264             /* TODO: Get the UUID as well */
1265             rec.channel = p_data->disc_res.result - 3;
1266             /* TODO: Need to get the service name using p_raw_data */
1267             rec.name[0] = 0;
1268
1269             HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1270                              BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1271         }
1272         break;
1273
1274         default:
1275         {
1276            ASSERTC(0, "unhandled remote service record event", event);
1277         }
1278         break;
1279     }
1280 }
1281
1282 /*******************************************************************************
1283 **
1284 ** Function         btif_dm_upstreams_cback
1285 **
1286 ** Description      Executes UPSTREAMS events in btif context
1287 **
1288 ** Returns          void
1289 **
1290 *******************************************************************************/
1291 static void btif_dm_upstreams_evt(UINT16 event, char* p_param)
1292 {
1293     tBTA_DM_SEC_EVT dm_event = (tBTA_DM_SEC_EVT)event;
1294     tBTA_DM_SEC *p_data = (tBTA_DM_SEC*)p_param;
1295     tBTA_SERVICE_MASK service_mask;
1296     uint32_t i;
1297     bt_bdaddr_t bd_addr;
1298
1299     BTIF_TRACE_EVENT1("btif_dm_upstreams_cback  ev: %s", dump_dm_event(event));
1300
1301     switch (event)
1302     {
1303         case BTA_DM_ENABLE_EVT:
1304         {
1305              BD_NAME bdname;
1306              bt_status_t status;
1307              bt_property_t prop;
1308              prop.type = BT_PROPERTY_BDNAME;
1309              prop.len = BD_NAME_LEN;
1310              prop.val = (void*)bdname;
1311
1312              status = btif_storage_get_adapter_property(&prop);
1313              if (status == BT_STATUS_SUCCESS)
1314              {
1315                  /* A name exists in the storage. Make this the device name */
1316                  BTA_DmSetDeviceName((char*)prop.val);
1317              }
1318              else
1319              {
1320                  /* Storage does not have a name yet.
1321                   * Use the default name and write it to the chip
1322                   */
1323                  BTA_DmSetDeviceName(btif_get_default_local_name());
1324              }
1325
1326              /* for each of the enabled services in the mask, trigger the profile
1327               * enable */
1328              service_mask = btif_get_enabled_services_mask();
1329              for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1330              {
1331                  if (service_mask &
1332                      (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1333                  {
1334                      btif_in_execute_service_request(i, TRUE);
1335                  }
1336              }
1337              /* clear control blocks */
1338              memset(&pairing_cb, 0, sizeof(btif_dm_pairing_cb_t));
1339
1340              /* This function will also trigger the adapter_properties_cb
1341              ** and bonded_devices_info_cb
1342              */
1343              btif_storage_load_bonded_devices();
1344
1345              btif_storage_load_autopair_device_list();
1346
1347              btif_enable_bluetooth_evt(p_data->enable.status, p_data->enable.bd_addr);
1348         }
1349         break;
1350
1351         case BTA_DM_DISABLE_EVT:
1352             /* for each of the enabled services in the mask, trigger the profile
1353              * disable */
1354             service_mask = btif_get_enabled_services_mask();
1355             for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1356             {
1357                 if (service_mask &
1358                     (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1359                 {
1360                     btif_in_execute_service_request(i, FALSE);
1361                 }
1362             }
1363             btif_disable_bluetooth_evt();
1364             break;
1365
1366         case BTA_DM_PIN_REQ_EVT:
1367             btif_dm_pin_req_evt(&p_data->pin_req);
1368             break;
1369
1370         case BTA_DM_AUTH_CMPL_EVT:
1371             btif_dm_auth_cmpl_evt(&p_data->auth_cmpl);
1372             break;
1373
1374         case BTA_DM_BOND_CANCEL_CMPL_EVT:
1375             if (pairing_cb.state == BT_BOND_STATE_BONDING)
1376             {
1377                 bdcpy(bd_addr.address, pairing_cb.bd_addr);
1378                 bond_state_changed(p_data->bond_cancel_cmpl.result, &bd_addr, BT_BOND_STATE_NONE);
1379             }
1380             break;
1381
1382         case BTA_DM_SP_CFM_REQ_EVT:
1383             btif_dm_ssp_cfm_req_evt(&p_data->cfm_req);
1384             break;
1385         case BTA_DM_SP_KEY_NOTIF_EVT:
1386             btif_dm_ssp_key_notif_evt(&p_data->key_notif);
1387             break;
1388
1389         case BTA_DM_DEV_UNPAIRED_EVT:
1390             bdcpy(bd_addr.address, p_data->link_down.bd_addr);
1391
1392             /*special handling for HID devices */
1393             #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
1394             btif_hh_remove_device(bd_addr);
1395             #endif
1396             #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1397             btif_storage_remove_ble_bonding_keys(&bd_addr);
1398             #endif
1399             btif_storage_remove_bonded_device(&bd_addr);
1400             bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1401             break;
1402
1403         case BTA_DM_BUSY_LEVEL_EVT:
1404         {
1405
1406             if (p_data->busy_level.level_flags & BTM_BL_INQUIRY_PAGING_MASK)
1407             {
1408                 if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_STARTED)
1409                 {
1410                        HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1411                                                 BT_DISCOVERY_STARTED);
1412                        btif_dm_inquiry_in_progress = TRUE;
1413                 }
1414                 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_CANCELLED)
1415                 {
1416                        HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1417                                                 BT_DISCOVERY_STOPPED);
1418                        btif_dm_inquiry_in_progress = FALSE;
1419                 }
1420                 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_COMPLETE)
1421                 {
1422                        btif_dm_inquiry_in_progress = FALSE;
1423                 }
1424             }
1425         }break;
1426
1427         case BTA_DM_LINK_UP_EVT:
1428             bdcpy(bd_addr.address, p_data->link_up.bd_addr);
1429             BTIF_TRACE_DEBUG0("BTA_DM_LINK_UP_EVT. Sending BT_ACL_STATE_CONNECTED");
1430
1431             btif_update_remote_version_property(&bd_addr);
1432
1433             HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1434                       &bd_addr, BT_ACL_STATE_CONNECTED);
1435             break;
1436
1437         case BTA_DM_LINK_DOWN_EVT:
1438             bdcpy(bd_addr.address, p_data->link_down.bd_addr);
1439             BTIF_TRACE_DEBUG0("BTA_DM_LINK_DOWN_EVT. Sending BT_ACL_STATE_DISCONNECTED");
1440             HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1441                       &bd_addr, BT_ACL_STATE_DISCONNECTED);
1442             break;
1443
1444         case BTA_DM_HW_ERROR_EVT:
1445             BTIF_TRACE_ERROR0("Received H/W Error. ");
1446             /* Flush storage data */
1447             btif_config_flush();
1448             usleep(100000); /* 100milliseconds */
1449             /* Killing the process to force a restart as part of fault tolerance */
1450             kill(getpid(), SIGKILL);
1451             break;
1452
1453 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1454         case BTA_DM_BLE_KEY_EVT:
1455             BTIF_TRACE_DEBUG1("BTA_DM_BLE_KEY_EVT key_type=0x%02x ", p_data->ble_key.key_type);
1456
1457             /* If this pairing is by-product of local initiated GATT client Read or Write,
1458             BTA would not have sent BTA_DM_BLE_SEC_REQ_EVT event and Bond state would not
1459             have setup properly. Setup pairing_cb and notify App about Bonding state now*/
1460             if (pairing_cb.state != BT_BOND_STATE_BONDING)
1461             {
1462                 BTIF_TRACE_DEBUG0("Bond state not sent to App so far.Notify the app now");
1463                 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t*)p_data->ble_key.bd_addr,
1464                                    BT_BOND_STATE_BONDING);
1465             }
1466             else if (memcmp (pairing_cb.bd_addr, p_data->ble_key.bd_addr, BD_ADDR_LEN)!=0)
1467             {
1468                 BTIF_TRACE_ERROR1("BD mismatch discard BLE key_type=%d ",p_data->ble_key.key_type);
1469                 break;
1470             }
1471
1472             switch (p_data->ble_key.key_type)
1473             {
1474                 case BTA_LE_KEY_PENC:
1475                     BTIF_TRACE_DEBUG0("Rcv BTA_LE_KEY_PENC");
1476                     pairing_cb.ble.is_penc_key_rcvd = TRUE;
1477                     memcpy(pairing_cb.ble.penc_key.ltk,p_data->ble_key.key_value.penc_key.ltk, 16);
1478                     memcpy(pairing_cb.ble.penc_key.rand, p_data->ble_key.key_value.penc_key.rand,8);
1479                     pairing_cb.ble.penc_key.ediv = p_data->ble_key.key_value.penc_key.ediv;
1480                     pairing_cb.ble.penc_key.sec_level = p_data->ble_key.key_value.penc_key.sec_level;
1481
1482                     for (i=0; i<16; i++)
1483                     {
1484                         BTIF_TRACE_DEBUG2("pairing_cb.ble.penc_key.ltk[%d]=0x%02x",i,pairing_cb.ble.penc_key.ltk[i]);
1485                     }
1486                     for (i=0; i<8; i++)
1487                     {
1488                         BTIF_TRACE_DEBUG2("pairing_cb.ble.penc_key.rand[%d]=0x%02x",i,pairing_cb.ble.penc_key.rand[i]);
1489                     }
1490                     BTIF_TRACE_DEBUG1("pairing_cb.ble.penc_key.ediv=0x%04x",pairing_cb.ble.penc_key.ediv);
1491                     BTIF_TRACE_DEBUG1("pairing_cb.ble.penc_key.sec_level=0x%02x",pairing_cb.ble.penc_key.sec_level);
1492                     BTIF_TRACE_DEBUG1("pairing_cb.ble.penc_key.key_size=0x%02x",pairing_cb.ble.penc_key.key_size);
1493                     break;
1494
1495                 case BTA_LE_KEY_PID:
1496                     BTIF_TRACE_DEBUG0("Rcv BTA_LE_KEY_PID");
1497                     pairing_cb.ble.is_pid_key_rcvd = TRUE;
1498                     memcpy(pairing_cb.ble.pid_key, p_data->ble_key.key_value.pid_key.irk, 16);
1499                     for (i=0; i<16; i++)
1500                     {
1501                         BTIF_TRACE_DEBUG2("pairing_cb.ble.pid_key[%d]=0x%02x",i,pairing_cb.ble.pid_key[i]);
1502                     }
1503                     break;
1504
1505                 case BTA_LE_KEY_PCSRK:
1506                     BTIF_TRACE_DEBUG0("Rcv BTA_LE_KEY_PCSRK");
1507                     pairing_cb.ble.is_pcsrk_key_rcvd = TRUE;
1508                     pairing_cb.ble.pcsrk_key.counter = p_data->ble_key.key_value.pcsrk_key.counter;
1509                     pairing_cb.ble.pcsrk_key.sec_level = p_data->ble_key.key_value.pcsrk_key.sec_level;
1510                     memcpy(pairing_cb.ble.pcsrk_key.csrk,p_data->ble_key.key_value.pcsrk_key.csrk,16);
1511
1512                     for (i=0; i<16; i++)
1513                     {
1514                         BTIF_TRACE_DEBUG2("pairing_cb.ble.pcsrk_key.csrk[%d]=0x%02x",i,pairing_cb.ble.pcsrk_key.csrk[i]);
1515                     }
1516                     BTIF_TRACE_DEBUG1("pairing_cb.ble.pcsrk_key.counter=0x%08x",pairing_cb.ble.pcsrk_key.counter);
1517                     BTIF_TRACE_DEBUG1("pairing_cb.ble.pcsrk_key.sec_level=0x%02x",pairing_cb.ble.pcsrk_key.sec_level);
1518                     break;
1519
1520                 case BTA_LE_KEY_LENC:
1521                     BTIF_TRACE_DEBUG0("Rcv BTA_LE_KEY_LENC");
1522                     pairing_cb.ble.is_lenc_key_rcvd = TRUE;
1523                     pairing_cb.ble.lenc_key.div = p_data->ble_key.key_value.lenc_key.div;
1524                     pairing_cb.ble.lenc_key.key_size = p_data->ble_key.key_value.lenc_key.key_size;
1525                     pairing_cb.ble.lenc_key.sec_level = p_data->ble_key.key_value.lenc_key.sec_level;
1526
1527                     BTIF_TRACE_DEBUG1("pairing_cb.ble.lenc_key.div=0x%04x",pairing_cb.ble.lenc_key.div);
1528                     BTIF_TRACE_DEBUG1("pairing_cb.ble.lenc_key.key_size=0x%02x",pairing_cb.ble.lenc_key.key_size);
1529                     BTIF_TRACE_DEBUG1("pairing_cb.ble.lenc_key.sec_level=0x%02x",pairing_cb.ble.lenc_key.sec_level);
1530                     break;
1531
1532
1533
1534                 case BTA_LE_KEY_LCSRK:
1535                     BTIF_TRACE_DEBUG0("Rcv BTA_LE_KEY_LCSRK");
1536                     pairing_cb.ble.is_lcsrk_key_rcvd = TRUE;
1537                     pairing_cb.ble.lcsrk_key.counter = p_data->ble_key.key_value.lcsrk_key.counter;
1538                     pairing_cb.ble.lcsrk_key.div = p_data->ble_key.key_value.lcsrk_key.div;
1539                     pairing_cb.ble.lcsrk_key.sec_level = p_data->ble_key.key_value.lcsrk_key.sec_level;
1540
1541                     BTIF_TRACE_DEBUG1("pairing_cb.ble.lcsrk_key.div=0x%04x",pairing_cb.ble.lcsrk_key.div);
1542                     BTIF_TRACE_DEBUG1("pairing_cb.ble.lcsrk_key.counter=0x%08x",pairing_cb.ble.lcsrk_key.counter);
1543                     BTIF_TRACE_DEBUG1("pairing_cb.ble.lcsrk_key.sec_level=0x%02x",pairing_cb.ble.lcsrk_key.sec_level);
1544
1545                     break;
1546
1547                 default:
1548                     BTIF_TRACE_ERROR1("unknown BLE key type (0x%02x)", p_data->ble_key.key_type);
1549                     break;
1550             }
1551
1552             break;
1553         case BTA_DM_BLE_SEC_REQ_EVT:
1554             BTIF_TRACE_DEBUG0("BTA_DM_BLE_SEC_REQ_EVT. ");
1555             btif_dm_ble_sec_req_evt(&p_data->ble_req);
1556             break;
1557         case BTA_DM_BLE_PASSKEY_NOTIF_EVT:
1558             BTIF_TRACE_DEBUG0("BTA_DM_BLE_PASSKEY_NOTIF_EVT. ");
1559             btif_dm_ble_key_notif_evt(&p_data->key_notif);
1560             break;
1561         case BTA_DM_BLE_PASSKEY_REQ_EVT:
1562             BTIF_TRACE_DEBUG0("BTA_DM_BLE_PASSKEY_REQ_EVT. ");
1563             btif_dm_ble_passkey_req_evt(&p_data->pin_req);
1564             break;
1565         case BTA_DM_BLE_OOB_REQ_EVT:
1566             BTIF_TRACE_DEBUG0("BTA_DM_BLE_OOB_REQ_EVT. ");
1567             break;
1568         case BTA_DM_BLE_LOCAL_IR_EVT:
1569             BTIF_TRACE_DEBUG0("BTA_DM_BLE_LOCAL_IR_EVT. ");
1570             ble_local_key_cb.is_id_keys_rcvd = TRUE;
1571             memcpy(&ble_local_key_cb.id_keys.irk[0], &p_data->ble_id_keys.irk[0], sizeof(BT_OCTET16));
1572             memcpy(&ble_local_key_cb.id_keys.ir[0], &p_data->ble_id_keys.ir[0], sizeof(BT_OCTET16));
1573             memcpy(&ble_local_key_cb.id_keys.dhk[0], &p_data->ble_id_keys.dhk[0], sizeof(BT_OCTET16));
1574             btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.irk[0],
1575                                             BTIF_DM_LE_LOCAL_KEY_IR,
1576                                             BT_OCTET16_LEN);
1577             btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.ir[0],
1578                                             BTIF_DM_LE_LOCAL_KEY_IRK,
1579                                             BT_OCTET16_LEN);
1580             btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.dhk[0],
1581                                             BTIF_DM_LE_LOCAL_KEY_DHK,
1582                                             BT_OCTET16_LEN);
1583             break;
1584         case BTA_DM_BLE_LOCAL_ER_EVT:
1585             BTIF_TRACE_DEBUG0("BTA_DM_BLE_LOCAL_ER_EVT. ");
1586             ble_local_key_cb.is_er_rcvd = TRUE;
1587             memcpy(&ble_local_key_cb.er[0], &p_data->ble_er[0], sizeof(BT_OCTET16));
1588             btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.er[0],
1589                                             BTIF_DM_LE_LOCAL_KEY_ER,
1590                                             BT_OCTET16_LEN);
1591             break;
1592
1593         case BTA_DM_BLE_AUTH_CMPL_EVT:
1594             BTIF_TRACE_DEBUG0("BTA_DM_BLE_KEY_EVT. ");
1595             btif_dm_ble_auth_cmpl_evt(&p_data->auth_cmpl);
1596             break;
1597 #endif
1598
1599         case BTA_DM_AUTHORIZE_EVT:
1600         case BTA_DM_SIG_STRENGTH_EVT:
1601         case BTA_DM_SP_RMT_OOB_EVT:
1602         case BTA_DM_SP_KEYPRESS_EVT:
1603         case BTA_DM_ROLE_CHG_EVT:
1604
1605         default:
1606             BTIF_TRACE_WARNING1( "btif_dm_cback : unhandled event (%d)", event );
1607             break;
1608     }
1609 } /* btui_security_cback() */
1610
1611
1612 /*******************************************************************************
1613 **
1614 ** Function         btif_dm_generic_evt
1615 **
1616 ** Description      Executes non-BTA upstream events in BTIF context
1617 **
1618 ** Returns          void
1619 **
1620 *******************************************************************************/
1621 static void btif_dm_generic_evt(UINT16 event, char* p_param)
1622 {
1623     BTIF_TRACE_EVENT2("%s: event=%d", __FUNCTION__, event);
1624     switch(event)
1625     {
1626         case BTIF_DM_CB_DISCOVERY_STARTED:
1627         {
1628             HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STARTED);
1629         }
1630         break;
1631
1632         case BTIF_DM_CB_CREATE_BOND:
1633         {
1634             btif_dm_cb_create_bond((bt_bdaddr_t *)p_param);
1635         }
1636         break;
1637
1638         case BTIF_DM_CB_REMOVE_BOND:
1639         {
1640             btif_dm_cb_remove_bond((bt_bdaddr_t *)p_param);
1641         }
1642         break;
1643
1644         case BTIF_DM_CB_HID_REMOTE_NAME:
1645         {
1646             btif_dm_cb_hid_remote_name((tBTM_REMOTE_DEV_NAME *)p_param);
1647         }
1648         break;
1649
1650         case BTIF_DM_CB_BOND_STATE_BONDING:
1651             {
1652                 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t *)p_param, BT_BOND_STATE_BONDING);
1653             }
1654             break;
1655         case BTIF_DM_CB_LE_TX_TEST:
1656         case BTIF_DM_CB_LE_RX_TEST:
1657             {
1658                 uint8_t status;
1659                 STREAM_TO_UINT8(status, p_param);
1660                 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
1661                       (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, 0);
1662             }
1663             break;
1664         case BTIF_DM_CB_LE_TEST_END:
1665             {
1666                 uint8_t status;
1667                 uint16_t count = 0;
1668                 STREAM_TO_UINT8(status, p_param);
1669                 if (status == 0)
1670                     STREAM_TO_UINT16(count, p_param);
1671                 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
1672                       (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, count);
1673             }
1674             break;
1675         default:
1676         {
1677             BTIF_TRACE_WARNING2("%s : Unknown event 0x%x", __FUNCTION__, event);
1678         }
1679         break;
1680     }
1681 }
1682
1683 /*******************************************************************************
1684 **
1685 ** Function         bte_dm_evt
1686 **
1687 ** Description      Switches context from BTE to BTIF for all DM events
1688 **
1689 ** Returns          void
1690 **
1691 *******************************************************************************/
1692
1693 void bte_dm_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC *p_data)
1694 {
1695     bt_status_t status;
1696
1697     /* switch context to btif task context (copy full union size for convenience) */
1698     status = btif_transfer_context(btif_dm_upstreams_evt, (uint16_t)event, (void*)p_data, sizeof(tBTA_DM_SEC), NULL);
1699
1700     /* catch any failed context transfers */
1701     ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
1702 }
1703
1704 /*******************************************************************************
1705 **
1706 ** Function         bte_search_devices_evt
1707 **
1708 ** Description      Switches context from BTE to BTIF for DM search events
1709 **
1710 ** Returns          void
1711 **
1712 *******************************************************************************/
1713 static void bte_search_devices_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
1714 {
1715     UINT16 param_len = 0;
1716
1717     if (p_data)
1718         param_len += sizeof(tBTA_DM_SEARCH);
1719     /* Allocate buffer to hold the pointers (deep copy). The pointers will point to the end of the tBTA_DM_SEARCH */
1720     switch (event)
1721     {
1722         case BTA_DM_INQ_RES_EVT:
1723         {
1724             if (p_data->inq_res.p_eir)
1725                 param_len += HCI_EXT_INQ_RESPONSE_LEN;
1726         }
1727         break;
1728
1729         case BTA_DM_DISC_RES_EVT:
1730         {
1731             if (p_data->disc_res.raw_data_size && p_data->disc_res.p_raw_data)
1732                 param_len += p_data->disc_res.raw_data_size;
1733         }
1734         break;
1735     }
1736     BTIF_TRACE_DEBUG3("%s event=%s param_len=%d", __FUNCTION__, dump_dm_search_event(event), param_len);
1737
1738     /* if remote name is available in EIR, set teh flag so that stack doesnt trigger RNR */
1739     if (event == BTA_DM_INQ_RES_EVT)
1740         p_data->inq_res.remt_name_not_required = check_eir_remote_name(p_data, NULL, NULL);
1741
1742     btif_transfer_context (btif_dm_search_devices_evt , (UINT16) event, (void *)p_data, param_len,
1743         (param_len > sizeof(tBTA_DM_SEARCH)) ? search_devices_copy_cb : NULL);
1744 }
1745
1746 /*******************************************************************************
1747 **
1748 ** Function         bte_dm_search_services_evt
1749 **
1750 ** Description      Switches context from BTE to BTIF for DM search services
1751 **                  event
1752 **
1753 ** Returns          void
1754 **
1755 *******************************************************************************/
1756 static void bte_dm_search_services_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
1757 {
1758     UINT16 param_len = 0;
1759    if (p_data)
1760        param_len += sizeof(tBTA_DM_SEARCH);
1761    switch (event)
1762    {
1763          case BTA_DM_DISC_RES_EVT:
1764          {
1765              if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0)) {
1766                   param_len += (p_data->disc_res.num_uuids * MAX_UUID_SIZE);
1767              }
1768          } break;
1769    }
1770    /* TODO: The only other member that needs a deep copy is the p_raw_data. But not sure
1771     * if raw_data is needed. */
1772    btif_transfer_context(btif_dm_search_services_evt, event, (char*)p_data, param_len,
1773          (param_len > sizeof(tBTA_DM_SEARCH)) ? search_services_copy_cb : NULL);
1774 }
1775
1776 /*******************************************************************************
1777 **
1778 ** Function         bte_dm_remote_service_record_evt
1779 **
1780 ** Description      Switches context from BTE to BTIF for DM search service
1781 **                  record event
1782 **
1783 ** Returns          void
1784 **
1785 *******************************************************************************/
1786 static void bte_dm_remote_service_record_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
1787 {
1788    /* TODO: The only member that needs a deep copy is the p_raw_data. But not sure yet if this is needed. */
1789    btif_transfer_context(btif_dm_remote_service_record_evt, event, (char*)p_data, sizeof(tBTA_DM_SEARCH), NULL);
1790 }
1791
1792 /*****************************************************************************
1793 **
1794 **   btif api functions (no context switch)
1795 **
1796 *****************************************************************************/
1797
1798 /*******************************************************************************
1799 **
1800 ** Function         btif_dm_start_discovery
1801 **
1802 ** Description      Start device discovery/inquiry
1803 **
1804 ** Returns          bt_status_t
1805 **
1806 *******************************************************************************/
1807 bt_status_t btif_dm_start_discovery(void)
1808 {
1809     tBTA_DM_INQ inq_params;
1810     tBTA_SERVICE_MASK services = 0;
1811
1812     BTIF_TRACE_EVENT1("%s", __FUNCTION__);
1813     /* TODO: Do we need to handle multiple inquiries at the same time? */
1814
1815     /* Set inquiry params and call API */
1816 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1817     inq_params.mode = BTA_DM_GENERAL_INQUIRY|BTA_BLE_GENERAL_INQUIRY;
1818 #if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
1819     inq_params.intl_duration[0]= BTIF_DM_INTERLEAVE_DURATION_BR_ONE;
1820     inq_params.intl_duration[1]= BTIF_DM_INTERLEAVE_DURATION_LE_ONE;
1821     inq_params.intl_duration[2]= BTIF_DM_INTERLEAVE_DURATION_BR_TWO;
1822     inq_params.intl_duration[3]= BTIF_DM_INTERLEAVE_DURATION_LE_TWO;
1823 #endif
1824 #else
1825     inq_params.mode = BTA_DM_GENERAL_INQUIRY;
1826 #endif
1827     inq_params.duration = BTIF_DM_DEFAULT_INQ_MAX_DURATION;
1828
1829     inq_params.max_resps = BTIF_DM_DEFAULT_INQ_MAX_RESULTS;
1830     inq_params.report_dup = TRUE;
1831
1832     inq_params.filter_type = BTA_DM_INQ_CLR;
1833     /* TODO: Filter device by BDA needs to be implemented here */
1834
1835     /* Will be enabled to TRUE once inquiry busy level has been received */
1836     btif_dm_inquiry_in_progress = FALSE;
1837     /* find nearby devices */
1838     BTA_DmSearch(&inq_params, services, bte_search_devices_evt);
1839
1840     return BT_STATUS_SUCCESS;
1841 }
1842
1843 /*******************************************************************************
1844 **
1845 ** Function         btif_dm_cancel_discovery
1846 **
1847 ** Description      Cancels search
1848 **
1849 ** Returns          bt_status_t
1850 **
1851 *******************************************************************************/
1852 bt_status_t btif_dm_cancel_discovery(void)
1853 {
1854     BTIF_TRACE_EVENT1("%s", __FUNCTION__);
1855     BTA_DmSearchCancel();
1856     return BT_STATUS_SUCCESS;
1857 }
1858
1859 /*******************************************************************************
1860 **
1861 ** Function         btif_dm_create_bond
1862 **
1863 ** Description      Initiate bonding with the specified device
1864 **
1865 ** Returns          bt_status_t
1866 **
1867 *******************************************************************************/
1868 bt_status_t btif_dm_create_bond(const bt_bdaddr_t *bd_addr)
1869 {
1870     bdstr_t bdstr;
1871
1872     BTIF_TRACE_EVENT2("%s: bd_addr=%s", __FUNCTION__, bd2str((bt_bdaddr_t *) bd_addr, &bdstr));
1873     if (pairing_cb.state != BT_BOND_STATE_NONE)
1874         return BT_STATUS_BUSY;
1875
1876     btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_CREATE_BOND,
1877                           (char *)bd_addr, sizeof(bt_bdaddr_t), NULL);
1878
1879     return BT_STATUS_SUCCESS;
1880 }
1881
1882 /*******************************************************************************
1883 **
1884 ** Function         btif_dm_cancel_bond
1885 **
1886 ** Description      Initiate bonding with the specified device
1887 **
1888 ** Returns          bt_status_t
1889 **
1890 *******************************************************************************/
1891
1892 bt_status_t btif_dm_cancel_bond(const bt_bdaddr_t *bd_addr)
1893 {
1894     bdstr_t bdstr;
1895
1896     BTIF_TRACE_EVENT2("%s: bd_addr=%s", __FUNCTION__, bd2str((bt_bdaddr_t *)bd_addr, &bdstr));
1897
1898     /* TODO:
1899     **  1. Restore scan modes
1900     **  2. special handling for HID devices
1901     */
1902     if (pairing_cb.state == BT_BOND_STATE_BONDING)
1903     {
1904
1905 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1906
1907         if (pairing_cb.is_ssp)
1908         {
1909             if (pairing_cb.is_le_only)
1910             {
1911                 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
1912             }
1913             else
1914                 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
1915         }
1916         else
1917         {
1918             if (pairing_cb.is_le_only)
1919             {
1920                 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
1921             }
1922             else
1923             {
1924                 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
1925             }
1926         /* Cancel bonding, in case it is in ACL connection setup state */
1927         BTA_DmBondCancel ((UINT8 *)bd_addr->address);
1928         }
1929
1930 #else
1931         if (pairing_cb.is_ssp)
1932         {
1933             BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
1934         }
1935         else
1936         {
1937             BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
1938         }
1939         /* Cancel bonding, in case it is in ACL connection setup state */
1940         BTA_DmBondCancel ((UINT8 *)bd_addr->address);
1941         btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
1942 #endif
1943     }
1944
1945     return BT_STATUS_SUCCESS;
1946 }
1947
1948 /*******************************************************************************
1949 **
1950 ** Function         btif_dm_hh_open_failed
1951 **
1952 ** Description      informs the upper layers if the HH have failed during bonding
1953 **
1954 ** Returns          none
1955 **
1956 *******************************************************************************/
1957
1958 void btif_dm_hh_open_failed(bt_bdaddr_t *bdaddr)
1959 {
1960     if (pairing_cb.state == BT_BOND_STATE_BONDING &&
1961             bdcmp(bdaddr->address, pairing_cb.bd_addr) == 0)
1962     {
1963         bond_state_changed(BT_STATUS_FAIL, bdaddr, BT_BOND_STATE_NONE);
1964     }
1965 }
1966
1967 /*******************************************************************************
1968 **
1969 ** Function         btif_dm_remove_bond
1970 **
1971 ** Description      Removes bonding with the specified device
1972 **
1973 ** Returns          bt_status_t
1974 **
1975 *******************************************************************************/
1976
1977 bt_status_t btif_dm_remove_bond(const bt_bdaddr_t *bd_addr)
1978 {
1979     bdstr_t bdstr;
1980
1981     BTIF_TRACE_EVENT2("%s: bd_addr=%s", __FUNCTION__, bd2str((bt_bdaddr_t *)bd_addr, &bdstr));
1982     btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_REMOVE_BOND,
1983                           (char *)bd_addr, sizeof(bt_bdaddr_t), NULL);
1984
1985     return BT_STATUS_SUCCESS;
1986 }
1987
1988 /*******************************************************************************
1989 **
1990 ** Function         btif_dm_pin_reply
1991 **
1992 ** Description      BT legacy pairing - PIN code reply
1993 **
1994 ** Returns          bt_status_t
1995 **
1996 *******************************************************************************/
1997
1998 bt_status_t btif_dm_pin_reply( const bt_bdaddr_t *bd_addr, uint8_t accept,
1999                                uint8_t pin_len, bt_pin_code_t *pin_code)
2000 {
2001     BTIF_TRACE_EVENT2("%s: accept=%d", __FUNCTION__, accept);
2002 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2003
2004     if (pairing_cb.is_le_only)
2005     {
2006         int i;
2007         UINT32 passkey = 0;
2008         int multi[] = {100000, 10000, 1000, 100, 10,1};
2009         BD_ADDR remote_bd_addr;
2010         bdcpy(remote_bd_addr, bd_addr->address);
2011         for (i = 0; i < 6; i++)
2012         {
2013             passkey += (multi[i] * (pin_code->pin[i] - '0'));
2014         }
2015         BTIF_TRACE_DEBUG1("btif_dm_pin_reply: passkey: %d", passkey);
2016         BTA_DmBlePasskeyReply(remote_bd_addr, accept, passkey);
2017
2018     }
2019     else
2020     {
2021         BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2022         if (accept)
2023             pairing_cb.pin_code_len = pin_len;
2024     }
2025 #else
2026     BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2027
2028     if (accept)
2029         pairing_cb.pin_code_len = pin_len;
2030 #endif
2031     return BT_STATUS_SUCCESS;
2032 }
2033
2034 /*******************************************************************************
2035 **
2036 ** Function         btif_dm_ssp_reply
2037 **
2038 ** Description      BT SSP Reply - Just Works, Numeric Comparison & Passkey Entry
2039 **
2040 ** Returns          bt_status_t
2041 **
2042 *******************************************************************************/
2043 bt_status_t btif_dm_ssp_reply(const bt_bdaddr_t *bd_addr,
2044                                  bt_ssp_variant_t variant, uint8_t accept,
2045                                  uint32_t passkey)
2046 {
2047     UNUSED(passkey);
2048
2049     if (variant == BT_SSP_VARIANT_PASSKEY_ENTRY)
2050     {
2051         /* This is not implemented in the stack.
2052          * For devices with display, this is not needed
2053         */
2054         BTIF_TRACE_WARNING1("%s: Not implemented", __FUNCTION__);
2055         return BT_STATUS_FAIL;
2056     }
2057     /* BT_SSP_VARIANT_CONSENT & BT_SSP_VARIANT_PASSKEY_CONFIRMATION supported */
2058     BTIF_TRACE_EVENT2("%s: accept=%d", __FUNCTION__, accept);
2059 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2060     if (pairing_cb.is_le_only)
2061     {
2062         if (accept)
2063             BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_GRANTED);
2064         else
2065             BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2066     }
2067     else
2068         BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
2069
2070 #else
2071     BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
2072 #endif
2073     return BT_STATUS_SUCCESS;
2074 }
2075
2076 /*******************************************************************************
2077 **
2078 ** Function         btif_dm_get_adapter_property
2079 **
2080 ** Description     Queries the BTA for the adapter property
2081 **
2082 ** Returns          bt_status_t
2083 **
2084 *******************************************************************************/
2085 bt_status_t btif_dm_get_adapter_property(bt_property_t *prop)
2086 {
2087     bt_status_t status;
2088
2089     BTIF_TRACE_EVENT2("%s: type=0x%x", __FUNCTION__, prop->type);
2090     switch (prop->type)
2091     {
2092         case BT_PROPERTY_BDNAME:
2093         {
2094             bt_bdname_t *bd_name = (bt_bdname_t*)prop->val;
2095             strcpy((char *)bd_name->name, btif_get_default_local_name());
2096             prop->len = strlen((char *)bd_name->name);
2097         }
2098         break;
2099
2100         case BT_PROPERTY_ADAPTER_SCAN_MODE:
2101         {
2102             /* if the storage does not have it. Most likely app never set it. Default is NONE */
2103             bt_scan_mode_t *mode = (bt_scan_mode_t*)prop->val;
2104             *mode = BT_SCAN_MODE_NONE;
2105             prop->len = sizeof(bt_scan_mode_t);
2106         }
2107         break;
2108
2109         case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
2110         {
2111             uint32_t *tmt = (uint32_t*)prop->val;
2112             *tmt = 120; /* default to 120s, if not found in NV */
2113             prop->len = sizeof(uint32_t);
2114         }
2115         break;
2116
2117         default:
2118             prop->len = 0;
2119             return BT_STATUS_FAIL;
2120     }
2121     return BT_STATUS_SUCCESS;
2122 }
2123
2124 /*******************************************************************************
2125 **
2126 ** Function         btif_dm_get_remote_services
2127 **
2128 ** Description      Start SDP to get remote services
2129 **
2130 ** Returns          bt_status_t
2131 **
2132 *******************************************************************************/
2133 bt_status_t btif_dm_get_remote_services(bt_bdaddr_t *remote_addr)
2134 {
2135     bdstr_t bdstr;
2136
2137     BTIF_TRACE_EVENT2("%s: remote_addr=%s", __FUNCTION__, bd2str(remote_addr, &bdstr));
2138
2139     BTA_DmDiscover(remote_addr->address, BTA_ALL_SERVICE_MASK,
2140                    bte_dm_search_services_evt, TRUE);
2141
2142     return BT_STATUS_SUCCESS;
2143 }
2144
2145 /*******************************************************************************
2146 **
2147 ** Function         btif_dm_get_remote_service_record
2148 **
2149 ** Description      Start SDP to get remote service record
2150 **
2151 **
2152 ** Returns          bt_status_t
2153 *******************************************************************************/
2154 bt_status_t btif_dm_get_remote_service_record(bt_bdaddr_t *remote_addr,
2155                                                     bt_uuid_t *uuid)
2156 {
2157     tSDP_UUID sdp_uuid;
2158     bdstr_t bdstr;
2159
2160     BTIF_TRACE_EVENT2("%s: remote_addr=%s", __FUNCTION__, bd2str(remote_addr, &bdstr));
2161
2162     sdp_uuid.len = MAX_UUID_SIZE;
2163     memcpy(sdp_uuid.uu.uuid128, uuid->uu, MAX_UUID_SIZE);
2164
2165     BTA_DmDiscoverUUID(remote_addr->address, &sdp_uuid,
2166                        bte_dm_remote_service_record_evt, TRUE);
2167
2168     return BT_STATUS_SUCCESS;
2169 }
2170
2171 void btif_dm_execute_service_request(UINT16 event, char *p_param)
2172 {
2173     BOOLEAN b_enable = FALSE;
2174     bt_status_t status;
2175     if (event == BTIF_DM_ENABLE_SERVICE)
2176     {
2177         b_enable = TRUE;
2178     }
2179     status = btif_in_execute_service_request(*((tBTA_SERVICE_ID*)p_param), b_enable);
2180     if (status == BT_STATUS_SUCCESS)
2181     {
2182         bt_property_t property;
2183         bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
2184
2185         /* Now send the UUID_PROPERTY_CHANGED event to the upper layer */
2186         BTIF_STORAGE_FILL_PROPERTY(&property, BT_PROPERTY_UUIDS,
2187                                     sizeof(local_uuids), local_uuids);
2188         btif_storage_get_adapter_property(&property);
2189         HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
2190                           BT_STATUS_SUCCESS, 1, &property);
2191     }
2192     return;
2193 }
2194
2195 #if (BTM_OOB_INCLUDED == TRUE)
2196 void btif_dm_set_oob_for_io_req(tBTA_OOB_DATA  *p_oob_data)
2197 {
2198     if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2199         oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2200     {
2201         *p_oob_data = FALSE;
2202     }
2203     else
2204     {
2205         *p_oob_data = TRUE;
2206     }
2207     BTIF_TRACE_DEBUG1("btif_dm_set_oob_for_io_req *p_oob_data=%d", *p_oob_data);
2208 }
2209 #endif /* BTM_OOB_INCLUDED */
2210
2211 #ifdef BTIF_DM_OOB_TEST
2212 void btif_dm_load_local_oob(void)
2213 {
2214     char prop_oob[PROPERTY_VALUE_MAX];
2215     property_get("service.brcm.bt.oob", prop_oob, "3");
2216     BTIF_TRACE_DEBUG1("btif_dm_load_local_oob prop_oob = %s",prop_oob);
2217     if (prop_oob[0] != '3')
2218     {
2219 #if (BTM_OOB_INCLUDED == TRUE)
2220         if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2221             oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2222         {
2223             BTIF_TRACE_DEBUG0("btif_dm_load_local_oob: read OOB, call BTA_DmLocalOob()");
2224             BTA_DmLocalOob();
2225         }
2226 #else
2227         BTIF_TRACE_ERROR0("BTM_OOB_INCLUDED is FALSE!!(btif_dm_load_local_oob)");
2228 #endif
2229     }
2230 }
2231
2232 void btif_dm_proc_loc_oob(BOOLEAN valid, BT_OCTET16 c, BT_OCTET16 r)
2233 {
2234     FILE *fp;
2235     char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2236     char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2237     char *path = NULL;
2238     char prop_oob[PROPERTY_VALUE_MAX];
2239     BTIF_TRACE_DEBUG1("btif_dm_proc_loc_oob: valid=%d", valid);
2240     if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2241         oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 &&
2242         valid)
2243     {
2244         BTIF_TRACE_DEBUG0("save local OOB data in memory");
2245         memcpy(oob_cb.sp_c, c, BT_OCTET16_LEN);
2246         memcpy(oob_cb.sp_r, r, BT_OCTET16_LEN);
2247         property_get("service.brcm.bt.oob", prop_oob, "3");
2248         BTIF_TRACE_DEBUG1("btif_dm_proc_loc_oob prop_oob = %s",prop_oob);
2249         if (prop_oob[0] == '1')
2250             path = path_a;
2251         else if (prop_oob[0] == '2')
2252             path = path_b;
2253         if (path)
2254         {
2255             fp = fopen(path, "wb+");
2256             if (fp == NULL)
2257             {
2258                 BTIF_TRACE_DEBUG1("btif_dm_proc_loc_oob: failed to save local OOB data to %s", path);
2259             }
2260             else
2261             {
2262                 BTIF_TRACE_DEBUG1("btif_dm_proc_loc_oob: save local OOB data into file %s",path);
2263                 fwrite (c , 1 , BT_OCTET16_LEN , fp );
2264                 fwrite (r , 1 , BT_OCTET16_LEN , fp );
2265                 fclose(fp);
2266             }
2267         }
2268     }
2269 }
2270 BOOLEAN btif_dm_proc_rmt_oob(BD_ADDR bd_addr,  BT_OCTET16 p_c, BT_OCTET16 p_r)
2271 {
2272     char t[128];
2273     FILE *fp;
2274     char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2275     char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2276     char *path = NULL;
2277     char prop_oob[PROPERTY_VALUE_MAX];
2278     BOOLEAN result = FALSE;
2279     bt_bdaddr_t bt_bd_addr;
2280     bdcpy(oob_cb.oob_bdaddr, bd_addr);
2281     property_get("service.brcm.bt.oob", prop_oob, "3");
2282     BTIF_TRACE_DEBUG1("btif_dm_proc_rmt_oob prop_oob = %s",prop_oob);
2283     if (prop_oob[0] == '1')
2284         path = path_b;
2285     else if (prop_oob[0] == '2')
2286         path = path_a;
2287     if (path)
2288     {
2289         fp = fopen(path, "rb");
2290         if (fp == NULL)
2291         {
2292             BTIF_TRACE_DEBUG1("btapp_dm_rmt_oob_reply: failed to read OOB keys from %s",path);
2293             return FALSE;
2294         }
2295         else
2296         {
2297             BTIF_TRACE_DEBUG1("btif_dm_proc_rmt_oob: read OOB data from %s",path);
2298             fread (p_c , 1 , BT_OCTET16_LEN , fp );
2299             fread (p_r , 1 , BT_OCTET16_LEN , fp );
2300             fclose(fp);
2301         }
2302         BTIF_TRACE_DEBUG0("----btif_dm_proc_rmt_oob: TRUE");
2303         sprintf(t, "%02x:%02x:%02x:%02x:%02x:%02x",
2304                 oob_cb.oob_bdaddr[0], oob_cb.oob_bdaddr[1], oob_cb.oob_bdaddr[2],
2305                 oob_cb.oob_bdaddr[3], oob_cb.oob_bdaddr[4], oob_cb.oob_bdaddr[5]);
2306         BTIF_TRACE_DEBUG1("----btif_dm_proc_rmt_oob: peer_bdaddr = %s", t);
2307         sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2308                 p_c[0], p_c[1], p_c[2],  p_c[3],  p_c[4],  p_c[5],  p_c[6],  p_c[7],
2309                 p_c[8], p_c[9], p_c[10], p_c[11], p_c[12], p_c[13], p_c[14], p_c[15]);
2310         BTIF_TRACE_DEBUG1("----btif_dm_proc_rmt_oob: c = %s",t);
2311         sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2312                 p_r[0], p_r[1], p_r[2],  p_r[3],  p_r[4],  p_r[5],  p_r[6],  p_r[7],
2313                 p_r[8], p_r[9], p_r[10], p_r[11], p_r[12], p_r[13], p_r[14], p_r[15]);
2314         BTIF_TRACE_DEBUG1("----btif_dm_proc_rmt_oob: r = %s",t);
2315         bdcpy(bt_bd_addr.address, bd_addr);
2316         btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_BOND_STATE_BONDING,
2317                               (char *)&bt_bd_addr, sizeof(bt_bdaddr_t), NULL);
2318         result = TRUE;
2319     }
2320     BTIF_TRACE_DEBUG1("btif_dm_proc_rmt_oob result=%d",result);
2321     return result;
2322 }
2323 #endif /*  BTIF_DM_OOB_TEST */
2324 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2325
2326 static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
2327 {
2328     bt_bdaddr_t bd_addr;
2329     bt_bdname_t bd_name;
2330     UINT32 cod;
2331
2332     BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
2333
2334     /* Remote name update */
2335     btif_update_remote_properties(p_ssp_key_notif->bd_addr , p_ssp_key_notif->bd_name,
2336                                           NULL, BT_DEVICE_TYPE_BLE);
2337     bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
2338     memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
2339
2340     bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2341     pairing_cb.is_ssp = FALSE;
2342     cod = COD_UNCLASSIFIED;
2343
2344     HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
2345               cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
2346               p_ssp_key_notif->passkey);
2347 }
2348
2349 /*******************************************************************************
2350 **
2351 ** Function         btif_dm_ble_auth_cmpl_evt
2352 **
2353 ** Description      Executes authentication complete event in btif context
2354 **
2355 ** Returns          void
2356 **
2357 *******************************************************************************/
2358 static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
2359 {
2360     /* Save link key, if not temporary */
2361     bt_bdaddr_t bd_addr;
2362     bt_status_t status = BT_STATUS_FAIL;
2363     bt_bond_state_t state = BT_BOND_STATE_NONE;
2364
2365     bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
2366     if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
2367     {
2368         /* store keys */
2369     }
2370     if (p_auth_cmpl->success)
2371     {
2372         status = BT_STATUS_SUCCESS;
2373         state = BT_BOND_STATE_BONDED;
2374
2375         btif_dm_save_ble_bonding_keys();
2376         BTA_GATTC_Refresh(bd_addr.address);
2377         btif_dm_get_remote_services(&bd_addr);
2378     }
2379     else
2380     {
2381         /*Map the HCI fail reason  to  bt status  */
2382         switch (p_auth_cmpl->fail_reason)
2383         {
2384             default:
2385                 btif_dm_remove_ble_bonding_keys();
2386                 status =  BT_STATUS_FAIL;
2387                 break;
2388         }
2389     }
2390     bond_state_changed(status, &bd_addr, state);
2391 }
2392
2393
2394
2395 void    btif_dm_load_ble_local_keys(void)
2396 {
2397     bt_status_t bt_status;
2398
2399     memset(&ble_local_key_cb, 0, sizeof(btif_dm_local_key_cb_t));
2400
2401     if (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_ER,(char*)&ble_local_key_cb.er[0],
2402                                        BT_OCTET16_LEN)== BT_STATUS_SUCCESS)
2403     {
2404         ble_local_key_cb.is_er_rcvd = TRUE;
2405         BTIF_TRACE_DEBUG1("%s BLE ER key loaded",__FUNCTION__ );
2406     }
2407
2408     if ((btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IR,(char*)&ble_local_key_cb.id_keys.ir[0],
2409                                         BT_OCTET16_LEN)== BT_STATUS_SUCCESS )&&
2410         (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IRK, (char*)&ble_local_key_cb.id_keys.irk[0],
2411                                         BT_OCTET16_LEN)== BT_STATUS_SUCCESS)&&
2412         (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_DHK,(char*)&ble_local_key_cb.id_keys.dhk[0],
2413                                         BT_OCTET16_LEN)== BT_STATUS_SUCCESS))
2414     {
2415         ble_local_key_cb.is_id_keys_rcvd = TRUE;
2416         BTIF_TRACE_DEBUG1("%s BLE ID keys loaded",__FUNCTION__ );
2417     }
2418
2419 }
2420 void    btif_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er,
2421                                    tBTA_BLE_LOCAL_ID_KEYS *p_id_keys)
2422 {
2423     if (ble_local_key_cb.is_er_rcvd )
2424     {
2425         memcpy(&er[0], &ble_local_key_cb.er[0], sizeof(BT_OCTET16));
2426         *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ER;
2427     }
2428
2429     if (ble_local_key_cb.is_id_keys_rcvd)
2430     {
2431         memcpy(&p_id_keys->ir[0], &ble_local_key_cb.id_keys.ir[0], sizeof(BT_OCTET16));
2432         memcpy(&p_id_keys->irk[0],  &ble_local_key_cb.id_keys.irk[0], sizeof(BT_OCTET16));
2433         memcpy(&p_id_keys->dhk[0],  &ble_local_key_cb.id_keys.dhk[0], sizeof(BT_OCTET16));
2434         *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ID;
2435     }
2436     BTIF_TRACE_DEBUG2("%s  *p_key_mask=0x%02x",__FUNCTION__,   *p_key_mask);
2437 }
2438
2439 void btif_dm_save_ble_bonding_keys(void)
2440 {
2441
2442     bt_bdaddr_t bd_addr;
2443
2444     BTIF_TRACE_DEBUG1("%s",__FUNCTION__ );
2445
2446     bdcpy(bd_addr.address, pairing_cb.bd_addr);
2447
2448     if (pairing_cb.ble.is_penc_key_rcvd)
2449     {
2450         btif_storage_add_ble_bonding_key(&bd_addr,
2451                                          (char *) &pairing_cb.ble.penc_key,
2452                                          BTIF_DM_LE_KEY_PENC,
2453                                          sizeof(btif_dm_ble_penc_keys_t));
2454     }
2455
2456     if (pairing_cb.ble.is_pid_key_rcvd)
2457     {
2458         btif_storage_add_ble_bonding_key(&bd_addr,
2459                                          (char *) &pairing_cb.ble.pid_key[0],
2460                                          BTIF_DM_LE_KEY_PID,
2461                                          BT_OCTET16_LEN);
2462     }
2463
2464
2465     if (pairing_cb.ble.is_pcsrk_key_rcvd)
2466     {
2467         btif_storage_add_ble_bonding_key(&bd_addr,
2468                                          (char *) &pairing_cb.ble.pcsrk_key,
2469                                          BTIF_DM_LE_KEY_PCSRK,
2470                                          sizeof(btif_dm_ble_pcsrk_keys_t));
2471     }
2472
2473
2474     if (pairing_cb.ble.is_lenc_key_rcvd)
2475     {
2476         btif_storage_add_ble_bonding_key(&bd_addr,
2477                                          (char *) &pairing_cb.ble.lenc_key,
2478                                          BTIF_DM_LE_KEY_LENC,
2479                                          sizeof(btif_dm_ble_lenc_keys_t));
2480     }
2481
2482     if (pairing_cb.ble.is_lcsrk_key_rcvd)
2483     {
2484         btif_storage_add_ble_bonding_key(&bd_addr,
2485                                          (char *) &pairing_cb.ble.lcsrk_key,
2486                                          BTIF_DM_LE_KEY_LCSRK,
2487                                          sizeof(btif_dm_ble_lcsrk_keys_t));
2488     }
2489
2490 }
2491
2492
2493 void btif_dm_remove_ble_bonding_keys(void)
2494 {
2495     bt_bdaddr_t bd_addr;
2496
2497     BTIF_TRACE_DEBUG1("%s",__FUNCTION__ );
2498
2499     bdcpy(bd_addr.address, pairing_cb.bd_addr);
2500     btif_storage_remove_ble_bonding_keys(&bd_addr);
2501 }
2502
2503
2504 /*******************************************************************************
2505 **
2506 ** Function         btif_dm_ble_sec_req_evt
2507 **
2508 ** Description      Eprocess security request event in btif context
2509 **
2510 ** Returns          void
2511 **
2512 *******************************************************************************/
2513 void btif_dm_ble_sec_req_evt(tBTA_DM_BLE_SEC_REQ *p_ble_req)
2514 {
2515     bt_bdaddr_t bd_addr;
2516     bt_bdname_t bd_name;
2517     UINT32 cod;
2518     BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
2519
2520     if (pairing_cb.state == BT_BOND_STATE_BONDING)
2521     {
2522         BTIF_TRACE_DEBUG1("%s Discard security request", __FUNCTION__);
2523         return;
2524     }
2525
2526     /* Remote name update */
2527     btif_update_remote_properties(p_ble_req->bd_addr,p_ble_req->bd_name,NULL,BT_DEVICE_TYPE_BLE);
2528
2529     bdcpy(bd_addr.address, p_ble_req->bd_addr);
2530     memcpy(bd_name.name, p_ble_req->bd_name, BD_NAME_LEN);
2531
2532     bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2533
2534     pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
2535     pairing_cb.is_le_only = TRUE;
2536     pairing_cb.is_ssp = TRUE;
2537
2538     cod = COD_UNCLASSIFIED;
2539
2540     HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
2541               BT_SSP_VARIANT_CONSENT, 0);
2542 }
2543
2544
2545
2546 /*******************************************************************************
2547 **
2548 ** Function         btif_dm_ble_passkey_req_evt
2549 **
2550 ** Description      Executes pin request event in btif context
2551 **
2552 ** Returns          void
2553 **
2554 *******************************************************************************/
2555 static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
2556 {
2557     bt_bdaddr_t bd_addr;
2558     bt_bdname_t bd_name;
2559     UINT32 cod;
2560
2561     /* Remote name update */
2562     btif_update_remote_properties(p_pin_req->bd_addr,p_pin_req->bd_name,NULL,BT_DEVICE_TYPE_BLE);
2563
2564     bdcpy(bd_addr.address, p_pin_req->bd_addr);
2565     memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
2566
2567     bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2568     pairing_cb.is_le_only = TRUE;
2569
2570     cod = COD_UNCLASSIFIED;
2571
2572     HAL_CBACK(bt_hal_cbacks, pin_request_cb,
2573               &bd_addr, &bd_name, cod);
2574 }
2575
2576
2577 void btif_dm_update_ble_remote_properties( BD_ADDR bd_addr, BD_NAME bd_name,
2578                                            tBT_DEVICE_TYPE dev_type)
2579 {
2580    btif_update_remote_properties(bd_addr,bd_name,NULL,dev_type);
2581 }
2582
2583 static void btif_dm_ble_tx_test_cback(void *p)
2584 {
2585     btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TX_TEST,
2586                           (char *)p, 1, NULL);
2587 }
2588
2589 static void btif_dm_ble_rx_test_cback(void *p)
2590 {
2591     btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_RX_TEST,
2592                           (char *)p, 1, NULL);
2593 }
2594
2595 static void btif_dm_ble_test_end_cback(void *p)
2596 {
2597     btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TEST_END,
2598                           (char *)p, 3, NULL);
2599 }
2600 /*******************************************************************************
2601 **
2602 ** Function         btif_le_test_mode
2603 **
2604 ** Description     Sends a HCI BLE Test command to the Controller
2605 **
2606 ** Returns          BT_STATUS_SUCCESS on success
2607 **
2608 *******************************************************************************/
2609 bt_status_t btif_le_test_mode(uint16_t opcode, uint8_t *buf, uint8_t len)
2610 {
2611      switch (opcode) {
2612          case HCI_BLE_TRANSMITTER_TEST:
2613              if (len != 3) return BT_STATUS_PARM_INVALID;
2614              BTM_BleTransmitterTest(buf[0],buf[1],buf[2], btif_dm_ble_tx_test_cback);
2615              break;
2616          case HCI_BLE_RECEIVER_TEST:
2617              if (len != 1) return BT_STATUS_PARM_INVALID;
2618              BTM_BleReceiverTest(buf[0], btif_dm_ble_rx_test_cback);
2619              break;
2620          case HCI_BLE_TEST_END:
2621              BTM_BleTestEnd((tBTM_CMPL_CB*) btif_dm_ble_test_end_cback);
2622              break;
2623          default:
2624              BTIF_TRACE_ERROR2("%s: Unknown LE Test Mode Command 0x%x", __FUNCTION__, opcode);
2625              return BT_STATUS_UNSUPPORTED;
2626      }
2627      return BT_STATUS_SUCCESS;
2628 }
2629
2630 #endif
2631
2632 void btif_dm_on_disable()
2633 {
2634     /* cancel any pending pairing requests */
2635     if (pairing_cb.state == BT_BOND_STATE_BONDING)
2636     {
2637         bt_bdaddr_t bd_addr;
2638
2639         BTIF_TRACE_DEBUG1("%s: Cancel pending pairing request", __FUNCTION__);
2640         bdcpy(bd_addr.address, pairing_cb.bd_addr);
2641         btif_dm_cancel_bond(&bd_addr);
2642     }
2643 }
2644
2645 static char* btif_get_default_local_name() {
2646     if (btif_default_local_name[0] == '\0')
2647     {
2648         int max_len = sizeof(btif_default_local_name) - 1;
2649         if (BTM_DEF_LOCAL_NAME[0] != '\0')
2650         {
2651             strncpy(btif_default_local_name, BTM_DEF_LOCAL_NAME, max_len);
2652         }
2653         else
2654         {
2655             char prop_model[PROPERTY_VALUE_MAX];
2656             property_get(PROPERTY_PRODUCT_MODEL, prop_model, "");
2657             strncpy(btif_default_local_name, prop_model, max_len);
2658         }
2659         btif_default_local_name[max_len] = '\0';
2660     }
2661     return btif_default_local_name;
2662 }