OSDN Git Service

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