OSDN Git Service

am bf1b3520: am 4baec1a8: Merge "Remove HOGP device from background connection list...
[android-x86/system-bt.git] / btif / src / btif_core.c
1 /******************************************************************************
2  *
3  *  Copyright (C) 2014 The Android Open Source Project
4  *  Copyright (C) 2009-2012 Broadcom Corporation
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at:
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  ******************************************************************************/
19
20 /************************************************************************************
21  *
22  *  Filename:      btif_core.c
23  *
24  *  Description:   Contains core functionality related to interfacing between
25  *                 Bluetooth HAL and BTE core stack.
26  *
27  ***********************************************************************************/
28
29 #include <stdlib.h>
30 #include <hardware/bluetooth.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <fcntl.h>
35 #include <dirent.h>
36 #include <ctype.h>
37 #include <cutils/properties.h>
38
39 #define LOG_TAG "BTIF_CORE"
40 #include "btif_api.h"
41 #include "bt_utils.h"
42 #include "bta_api.h"
43 #include "gki.h"
44 #include "btu.h"
45 #include "bte.h"
46 #include "bd.h"
47 #include "btif_av.h"
48 #include "btif_storage.h"
49 #include "btif_util.h"
50 #include "btif_sock.h"
51 #include "btif_pan.h"
52 #include "btif_mce.h"
53 #include "btif_profile_queue.h"
54 #include "btif_config.h"
55 /************************************************************************************
56 **  Constants & Macros
57 ************************************************************************************/
58
59 #ifndef BTIF_TASK_STACK_SIZE
60 #define BTIF_TASK_STACK_SIZE       0x2000         /* In bytes */
61 #endif
62
63 #ifndef BTE_DID_CONF_FILE
64 #define BTE_DID_CONF_FILE "/etc/bluetooth/bt_did.conf"
65 #endif
66
67 #define BTIF_TASK_STR        ((INT8 *) "BTIF")
68
69 /************************************************************************************
70 **  Local type definitions
71 ************************************************************************************/
72
73 /* These type definitions are used when passing data from the HAL to BTIF context
74 *  in the downstream path for the adapter and remote_device property APIs */
75
76 typedef struct {
77   bt_bdaddr_t bd_addr;
78   bt_property_type_t type;
79 } btif_storage_read_t;
80
81 typedef struct {
82   bt_bdaddr_t bd_addr;
83   bt_property_t prop;
84 } btif_storage_write_t;
85
86 typedef union {
87   btif_storage_read_t read_req;
88   btif_storage_write_t write_req;
89 } btif_storage_req_t;
90
91 typedef enum {
92     BTIF_CORE_STATE_DISABLED = 0,
93     BTIF_CORE_STATE_ENABLING,
94     BTIF_CORE_STATE_ENABLED,
95     BTIF_CORE_STATE_DISABLING
96 } btif_core_state_t;
97
98 /************************************************************************************
99 **  Static variables
100 ************************************************************************************/
101
102 bt_bdaddr_t btif_local_bd_addr;
103
104 static UINT32 btif_task_stack[(BTIF_TASK_STACK_SIZE + 3) / 4];
105
106 /* holds main adapter state */
107 static btif_core_state_t btif_core_state = BTIF_CORE_STATE_DISABLED;
108
109 static int btif_shutdown_pending = 0;
110 static tBTA_SERVICE_MASK btif_enabled_services = 0;
111
112 /*
113 * This variable should be set to 1, if the Bluedroid+BTIF libraries are to
114 * function in DUT mode.
115 *
116 * To set this, the btif_init_bluetooth needs to be called with argument as 1
117 */
118 static UINT8 btif_dut_mode = 0;
119
120 /************************************************************************************
121 **  Static functions
122 ************************************************************************************/
123 static bt_status_t btif_associate_evt(void);
124 static bt_status_t btif_disassociate_evt(void);
125
126 /* sends message to btif task */
127 static void btif_sendmsg(void *p_msg);
128
129 /************************************************************************************
130 **  Externs
131 ************************************************************************************/
132 extern void bte_load_did_conf(const char *p_path);
133
134 /** TODO: Move these to _common.h */
135 void bte_main_boot_entry(void);
136 void bte_main_enable();
137 void bte_main_disable(void);
138 void bte_main_shutdown(void);
139 #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
140 void bte_main_enable_lpm(BOOLEAN enable);
141 #endif
142 void bte_main_postload_cfg(void);
143 void btif_dm_execute_service_request(UINT16 event, char *p_param);
144 #ifdef BTIF_DM_OOB_TEST
145 void btif_dm_load_local_oob(void);
146 #endif
147 void bte_main_config_hci_logging(BOOLEAN enable, BOOLEAN bt_disabled);
148
149 /************************************************************************************
150 **  Functions
151 ************************************************************************************/
152
153
154 /*****************************************************************************
155 **   Context switching functions
156 *****************************************************************************/
157
158
159 /*******************************************************************************
160 **
161 ** Function         btif_context_switched
162 **
163 ** Description      Callback used to execute transferred context callback
164 **
165 **                  p_msg : message to be executed in btif context
166 **
167 ** Returns          void
168 **
169 *******************************************************************************/
170
171 static void btif_context_switched(void *p_msg)
172 {
173     tBTIF_CONTEXT_SWITCH_CBACK *p;
174
175     BTIF_TRACE_VERBOSE("btif_context_switched");
176
177     p = (tBTIF_CONTEXT_SWITCH_CBACK *) p_msg;
178
179     /* each callback knows how to parse the data */
180     if (p->p_cb)
181         p->p_cb(p->event, p->p_param);
182 }
183
184
185 /*******************************************************************************
186 **
187 ** Function         btif_transfer_context
188 **
189 ** Description      This function switches context to btif task
190 **
191 **                  p_cback   : callback used to process message in btif context
192 **                  event     : event id of message
193 **                  p_params  : parameter area passed to callback (copied)
194 **                  param_len : length of parameter area
195 **                  p_copy_cback : If set this function will be invoked for deep copy
196 **
197 ** Returns          void
198 **
199 *******************************************************************************/
200
201 bt_status_t btif_transfer_context (tBTIF_CBACK *p_cback, UINT16 event, char* p_params, int param_len, tBTIF_COPY_CBACK *p_copy_cback)
202 {
203     tBTIF_CONTEXT_SWITCH_CBACK *p_msg;
204
205     BTIF_TRACE_VERBOSE("btif_transfer_context event %d, len %d", event, param_len);
206
207     /* allocate and send message that will be executed in btif context */
208     if ((p_msg = (tBTIF_CONTEXT_SWITCH_CBACK *) GKI_getbuf(sizeof(tBTIF_CONTEXT_SWITCH_CBACK) + param_len)) != NULL)
209     {
210         p_msg->hdr.event = BT_EVT_CONTEXT_SWITCH_EVT; /* internal event */
211         p_msg->p_cb = p_cback;
212
213         p_msg->event = event;                         /* callback event */
214
215         /* check if caller has provided a copy callback to do the deep copy */
216         if (p_copy_cback)
217         {
218             p_copy_cback(event, p_msg->p_param, p_params);
219         }
220         else if (p_params)
221         {
222             memcpy(p_msg->p_param, p_params, param_len);  /* callback parameter data */
223         }
224
225         btif_sendmsg(p_msg);
226         return BT_STATUS_SUCCESS;
227     }
228     else
229     {
230         /* let caller deal with a failed allocation */
231         return BT_STATUS_NOMEM;
232     }
233 }
234
235 /*******************************************************************************
236 **
237 ** Function         btif_is_dut_mode
238 **
239 ** Description      checks if BTIF is currently in DUT mode
240 **
241 ** Returns          1 if test mode, otherwize 0
242 **
243 *******************************************************************************/
244
245 UINT8 btif_is_dut_mode(void)
246 {
247     return (btif_dut_mode == 1);
248 }
249
250 /*******************************************************************************
251 **
252 ** Function         btif_is_enabled
253 **
254 ** Description      checks if main adapter is fully enabled
255 **
256 ** Returns          1 if fully enabled, otherwize 0
257 **
258 *******************************************************************************/
259
260 int btif_is_enabled(void)
261 {
262     return ((!btif_is_dut_mode()) && (btif_core_state == BTIF_CORE_STATE_ENABLED));
263 }
264
265 /*******************************************************************************
266 **
267 ** Function         btif_task
268 **
269 ** Description      BTIF task handler managing all messages being passed
270 **                  Bluetooth HAL and BTA.
271 **
272 ** Returns          void
273 **
274 *******************************************************************************/
275
276 static void btif_task(UINT32 params)
277 {
278     UINT16   event;
279     BT_HDR   *p_msg;
280     UNUSED(params);
281
282     BTIF_TRACE_DEBUG("btif task starting");
283
284     btif_associate_evt();
285
286     for(;;)
287     {
288         /* wait for specified events */
289         event = GKI_wait(0xFFFF, 0);
290
291         /*
292          * Wait for the trigger to init chip and stack. This trigger will
293          * be received by btu_task once the UART is opened and ready
294          */
295         if (event == BT_EVT_TRIGGER_STACK_INIT)
296         {
297             BTIF_TRACE_DEBUG("btif_task: received trigger stack init event");
298             #if (BLE_INCLUDED == TRUE)
299             btif_dm_load_ble_local_keys();
300             #endif
301             BTA_EnableBluetooth(bte_dm_evt);
302         }
303
304         /*
305          * Failed to initialize controller hardware, reset state and bring
306          * down all threads
307          */
308         if (event == BT_EVT_HARDWARE_INIT_FAIL)
309         {
310             BTIF_TRACE_DEBUG("btif_task: hardware init failed");
311             bte_main_disable();
312             btif_queue_release();
313             GKI_task_self_cleanup(BTIF_TASK);
314             bte_main_shutdown();
315             btif_dut_mode = 0;
316             btif_core_state = BTIF_CORE_STATE_DISABLED;
317             HAL_CBACK(bt_hal_cbacks,adapter_state_changed_cb,BT_STATE_OFF);
318             break;
319         }
320
321         if (event & EVENT_MASK(GKI_SHUTDOWN_EVT))
322             break;
323
324         if(event & TASK_MBOX_1_EVT_MASK)
325         {
326             while((p_msg = GKI_read_mbox(BTU_BTIF_MBOX)) != NULL)
327             {
328                 BTIF_TRACE_VERBOSE("btif task fetched event %x", p_msg->event);
329
330                 switch (p_msg->event)
331                 {
332                     case BT_EVT_CONTEXT_SWITCH_EVT:
333                         btif_context_switched(p_msg);
334                         break;
335                     default:
336                         BTIF_TRACE_ERROR("unhandled btif event (%d)", p_msg->event & BT_EVT_MASK);
337                         break;
338                 }
339
340                 GKI_freebuf(p_msg);
341             }
342         }
343     }
344
345     btif_disassociate_evt();
346
347     BTIF_TRACE_DEBUG("btif task exiting");
348 }
349
350
351 /*******************************************************************************
352 **
353 ** Function         btif_sendmsg
354 **
355 ** Description      Sends msg to BTIF task
356 **
357 ** Returns          void
358 **
359 *******************************************************************************/
360
361 void btif_sendmsg(void *p_msg)
362 {
363     GKI_send_msg(BTIF_TASK, BTU_BTIF_MBOX, p_msg);
364 }
365
366 static void btif_fetch_local_bdaddr(bt_bdaddr_t *local_addr)
367 {
368     char val[256];
369     uint8_t valid_bda = FALSE;
370     int val_size = 0;
371     const uint8_t null_bdaddr[BD_ADDR_LEN] = {0,0,0,0,0,0};
372
373     /* Get local bdaddr storage path from property */
374     if (property_get(PROPERTY_BT_BDADDR_PATH, val, NULL))
375     {
376         int addr_fd;
377
378         BTIF_TRACE_DEBUG("local bdaddr is stored in %s", val);
379
380         if ((addr_fd = open(val, O_RDONLY)) != -1)
381         {
382             memset(val, 0, sizeof(val));
383             read(addr_fd, val, FACTORY_BT_BDADDR_STORAGE_LEN);
384             str2bd(val, local_addr);
385             /* If this is not a reserved/special bda, then use it */
386             if (memcmp(local_addr->address, null_bdaddr, BD_ADDR_LEN) != 0)
387             {
388                 valid_bda = TRUE;
389                 BTIF_TRACE_DEBUG("Got Factory BDA %02X:%02X:%02X:%02X:%02X:%02X",
390                     local_addr->address[0], local_addr->address[1], local_addr->address[2],
391                     local_addr->address[3], local_addr->address[4], local_addr->address[5]);
392             }
393
394             close(addr_fd);
395         }
396     }
397
398     if(!valid_bda)
399     {
400         val_size = sizeof(val);
401         if(btif_config_get_str("Local", "Adapter", "Address", val, &val_size))
402         {
403             str2bd(val, local_addr);
404             BTIF_TRACE_DEBUG("local bdaddr from bt_config.xml is  %s", val);
405             return;
406         }
407      }
408
409     /* No factory BDADDR found. Look for previously generated random BDA */
410     if ((!valid_bda) && \
411         (property_get(PERSIST_BDADDR_PROPERTY, val, NULL)))
412     {
413         str2bd(val, local_addr);
414         valid_bda = TRUE;
415         BTIF_TRACE_DEBUG("Got prior random BDA %02X:%02X:%02X:%02X:%02X:%02X",
416             local_addr->address[0], local_addr->address[1], local_addr->address[2],
417             local_addr->address[3], local_addr->address[4], local_addr->address[5]);
418     }
419
420     /* Generate new BDA if necessary */
421     if (!valid_bda)
422     {
423         bdstr_t bdstr;
424         /* Seed the random number generator */
425         srand((unsigned int) (time(0)));
426
427         /* No autogen BDA. Generate one now. */
428         local_addr->address[0] = 0x22;
429         local_addr->address[1] = 0x22;
430         local_addr->address[2] = (uint8_t) ((rand() >> 8) & 0xFF);
431         local_addr->address[3] = (uint8_t) ((rand() >> 8) & 0xFF);
432         local_addr->address[4] = (uint8_t) ((rand() >> 8) & 0xFF);
433         local_addr->address[5] = (uint8_t) ((rand() >> 8) & 0xFF);
434
435         /* Convert to ascii, and store as a persistent property */
436         bd2str(local_addr, &bdstr);
437
438         BTIF_TRACE_DEBUG("No preset BDA. Generating BDA: %s for prop %s",
439              (char*)bdstr, PERSIST_BDADDR_PROPERTY);
440
441         if (property_set(PERSIST_BDADDR_PROPERTY, (char*)bdstr) < 0)
442             BTIF_TRACE_ERROR("Failed to set random BDA in prop %s",PERSIST_BDADDR_PROPERTY);
443     }
444
445     //save the bd address to config file
446     bdstr_t bdstr;
447     bd2str(local_addr, &bdstr);
448     val_size = sizeof(val);
449     if (btif_config_get_str("Local", "Adapter", "Address", val, &val_size))
450     {
451         if (strcmp(bdstr, val) ==0)
452         {
453             // BDA is already present in the config file.
454             return;
455         }
456     }
457     btif_config_set_str("Local", "Adapter", "Address", bdstr);
458     btif_config_save();
459 }
460
461 /*****************************************************************************
462 **
463 **   btif core api functions
464 **
465 *****************************************************************************/
466
467 /*******************************************************************************
468 **
469 ** Function         btif_init_bluetooth
470 **
471 ** Description      Creates BTIF task and prepares BT scheduler for startup
472 **
473 ** Returns          bt_status_t
474 **
475 *******************************************************************************/
476
477 bt_status_t btif_init_bluetooth()
478 {
479     UINT8 status;
480     btif_config_init();
481     bte_main_boot_entry();
482
483     /* As part of the init, fetch the local BD ADDR */
484     memset(&btif_local_bd_addr, 0, sizeof(bt_bdaddr_t));
485     btif_fetch_local_bdaddr(&btif_local_bd_addr);
486
487     /* start btif task */
488     status = GKI_create_task(btif_task, BTIF_TASK, BTIF_TASK_STR,
489                 (UINT16 *) ((UINT8 *)btif_task_stack + BTIF_TASK_STACK_SIZE),
490                 sizeof(btif_task_stack));
491
492     if (status != GKI_SUCCESS)
493         return BT_STATUS_FAIL;
494
495     return BT_STATUS_SUCCESS;
496 }
497
498 /*******************************************************************************
499 **
500 ** Function         btif_associate_evt
501 **
502 ** Description      Event indicating btif_task is up
503 **                  Attach btif_task to JVM
504 **
505 ** Returns          void
506 **
507 *******************************************************************************/
508
509 static bt_status_t btif_associate_evt(void)
510 {
511     BTIF_TRACE_DEBUG("%s: notify ASSOCIATE_JVM", __FUNCTION__);
512     HAL_CBACK(bt_hal_cbacks, thread_evt_cb, ASSOCIATE_JVM);
513
514     return BT_STATUS_SUCCESS;
515 }
516
517
518 /*******************************************************************************
519 **
520 ** Function         btif_enable_bluetooth
521 **
522 ** Description      Performs chip power on and kickstarts OS scheduler
523 **
524 ** Returns          bt_status_t
525 **
526 *******************************************************************************/
527
528 bt_status_t btif_enable_bluetooth(void)
529 {
530     BTIF_TRACE_DEBUG("BTIF ENABLE BLUETOOTH");
531
532     if (btif_core_state != BTIF_CORE_STATE_DISABLED)
533     {
534         ALOGD("not disabled\n");
535         return BT_STATUS_DONE;
536     }
537
538     btif_core_state = BTIF_CORE_STATE_ENABLING;
539
540     /* Create the GKI tasks and run them */
541     bte_main_enable();
542
543     return BT_STATUS_SUCCESS;
544 }
545
546
547 /*******************************************************************************
548 **
549 ** Function         btif_enable_bluetooth_evt
550 **
551 ** Description      Event indicating bluetooth enable is completed
552 **                  Notifies HAL user with updated adapter state
553 **
554 ** Returns          void
555 **
556 *******************************************************************************/
557
558 void btif_enable_bluetooth_evt(tBTA_STATUS status, BD_ADDR local_bd)
559 {
560     bt_bdaddr_t bd_addr;
561     bdstr_t bdstr;
562
563     bdcpy(bd_addr.address, local_bd);
564     BTIF_TRACE_DEBUG("%s: status %d, local bd [%s]", __FUNCTION__, status,
565                                                      bd2str(&bd_addr, &bdstr));
566
567     if (bdcmp(btif_local_bd_addr.address,local_bd))
568     {
569         bdstr_t buf;
570         bt_property_t prop;
571
572         /**
573          * The Controller's BDADDR does not match to the BTIF's initial BDADDR!
574          * This could be because the factory BDADDR was stored separatley in
575          * the Controller's non-volatile memory rather than in device's file
576          * system.
577          **/
578         BTIF_TRACE_WARNING("***********************************************");
579         BTIF_TRACE_WARNING("BTIF init BDA was %02X:%02X:%02X:%02X:%02X:%02X",
580             btif_local_bd_addr.address[0], btif_local_bd_addr.address[1],
581             btif_local_bd_addr.address[2], btif_local_bd_addr.address[3],
582             btif_local_bd_addr.address[4], btif_local_bd_addr.address[5]);
583         BTIF_TRACE_WARNING("Controller BDA is %02X:%02X:%02X:%02X:%02X:%02X",
584             local_bd[0], local_bd[1], local_bd[2],
585             local_bd[3], local_bd[4], local_bd[5]);
586         BTIF_TRACE_WARNING("***********************************************");
587
588         bdcpy(btif_local_bd_addr.address, local_bd);
589
590         //save the bd address to config file
591         bd2str(&btif_local_bd_addr, &buf);
592         btif_config_set_str("Local", "Adapter", "Address", buf);
593         btif_config_save();
594
595         //fire HAL callback for property change
596         memcpy(buf, &btif_local_bd_addr, sizeof(bt_bdaddr_t));
597         prop.type = BT_PROPERTY_BDADDR;
598         prop.val = (void*)buf;
599         prop.len = sizeof(bt_bdaddr_t);
600         HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, BT_STATUS_SUCCESS, 1, &prop);
601     }
602
603     bte_main_postload_cfg();
604 #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
605     bte_main_enable_lpm(TRUE);
606 #endif
607     /* add passing up bd address as well ? */
608
609     /* callback to HAL */
610     if (status == BTA_SUCCESS)
611     {
612         /* initialize a2dp service */
613         btif_av_init();
614
615         /* init rfcomm & l2cap api */
616         btif_sock_init();
617
618         /* init pan */
619         btif_pan_init();
620
621         /* load did configuration */
622         bte_load_did_conf(BTE_DID_CONF_FILE);
623
624 #ifdef BTIF_DM_OOB_TEST
625         btif_dm_load_local_oob();
626 #endif
627         /* now fully enabled, update state */
628         btif_core_state = BTIF_CORE_STATE_ENABLED;
629
630         HAL_CBACK(bt_hal_cbacks, adapter_state_changed_cb, BT_STATE_ON);
631     }
632     else
633     {
634         /* cleanup rfcomm & l2cap api */
635         btif_sock_cleanup();
636
637         btif_pan_cleanup();
638
639         /* we failed to enable, reset state */
640         btif_core_state = BTIF_CORE_STATE_DISABLED;
641
642         HAL_CBACK(bt_hal_cbacks, adapter_state_changed_cb, BT_STATE_OFF);
643     }
644 }
645
646 /*******************************************************************************
647 **
648 ** Function         btif_disable_bluetooth
649 **
650 ** Description      Inititates shutdown of Bluetooth system.
651 **                  Any active links will be dropped and device entering
652 **                  non connectable/discoverable mode
653 **
654 ** Returns          void
655 **
656 *******************************************************************************/
657 bt_status_t btif_disable_bluetooth(void)
658 {
659     tBTA_STATUS status;
660
661     if (!btif_is_enabled())
662     {
663         BTIF_TRACE_ERROR("btif_disable_bluetooth : not yet enabled");
664         return BT_STATUS_NOT_READY;
665     }
666
667     BTIF_TRACE_DEBUG("BTIF DISABLE BLUETOOTH");
668
669     btif_dm_on_disable();
670     btif_core_state = BTIF_CORE_STATE_DISABLING;
671
672     /* cleanup rfcomm & l2cap api */
673     btif_sock_cleanup();
674
675     btif_pan_cleanup();
676
677     status = BTA_DisableBluetooth();
678
679     btif_config_flush();
680
681     if (status != BTA_SUCCESS)
682     {
683         BTIF_TRACE_ERROR("disable bt failed (%d)", status);
684
685         /* reset the original state to allow attempting disable again */
686         btif_core_state = BTIF_CORE_STATE_ENABLED;
687
688         return BT_STATUS_FAIL;
689     }
690     return BT_STATUS_SUCCESS;
691 }
692
693 /*******************************************************************************
694 **
695 ** Function         btif_disable_bluetooth_evt
696 **
697 ** Description      Event notifying BT disable is now complete.
698 **                  Terminates main stack tasks and notifies HAL
699 **                  user with updated BT state.
700 **
701 ** Returns          void
702 **
703 *******************************************************************************/
704
705 void btif_disable_bluetooth_evt(void)
706 {
707     BTIF_TRACE_DEBUG("%s", __FUNCTION__);
708
709 #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
710     bte_main_enable_lpm(FALSE);
711 #endif
712
713 #if (BLE_INCLUDED == TRUE)
714      BTA_VendorCleanup();
715 #endif
716
717      bte_main_disable();
718
719     /* update local state */
720     btif_core_state = BTIF_CORE_STATE_DISABLED;
721
722     /* callback to HAL */
723     HAL_CBACK(bt_hal_cbacks, adapter_state_changed_cb, BT_STATE_OFF);
724
725     if (btif_shutdown_pending)
726     {
727         BTIF_TRACE_DEBUG("%s: calling btif_shutdown_bluetooth", __FUNCTION__);
728         btif_shutdown_bluetooth();
729     }
730 }
731
732
733 /*******************************************************************************
734 **
735 ** Function         btif_shutdown_bluetooth
736 **
737 ** Description      Finalizes BT scheduler shutdown and terminates BTIF
738 **                  task.
739 **
740 ** Returns          void
741 **
742 *******************************************************************************/
743
744 bt_status_t btif_shutdown_bluetooth(void)
745 {
746     BTIF_TRACE_DEBUG("%s", __FUNCTION__);
747
748     if (btif_core_state == BTIF_CORE_STATE_DISABLING)
749     {
750         BTIF_TRACE_WARNING("shutdown during disabling");
751         /* shutdown called before disabling is done */
752         btif_shutdown_pending = 1;
753         return BT_STATUS_NOT_READY;
754     }
755
756     if (btif_is_enabled())
757     {
758         BTIF_TRACE_WARNING("shutdown while still enabled, initiate disable");
759
760         /* shutdown called prior to disabling, initiate disable */
761         btif_disable_bluetooth();
762         btif_shutdown_pending = 1;
763         return BT_STATUS_NOT_READY;
764     }
765
766     btif_shutdown_pending = 0;
767
768     if (btif_core_state == BTIF_CORE_STATE_ENABLING)
769     {
770         // Java layer abort BT ENABLING, could be due to ENABLE TIMEOUT
771         // Direct call from cleanup()@bluetooth.c
772         // bring down HCI/Vendor lib
773         bte_main_disable();
774         btif_core_state = BTIF_CORE_STATE_DISABLED;
775         HAL_CBACK(bt_hal_cbacks, adapter_state_changed_cb, BT_STATE_OFF);
776     }
777
778     GKI_destroy_task(BTIF_TASK);
779     btif_queue_release();
780     bte_main_shutdown();
781
782     btif_dut_mode = 0;
783
784     bt_utils_cleanup();
785
786     BTIF_TRACE_DEBUG("%s done", __FUNCTION__);
787
788     return BT_STATUS_SUCCESS;
789 }
790
791
792 /*******************************************************************************
793 **
794 ** Function         btif_disassociate_evt
795 **
796 ** Description      Event indicating btif_task is going down
797 **                  Detach btif_task to JVM
798 **
799 ** Returns          void
800 **
801 *******************************************************************************/
802
803 static bt_status_t btif_disassociate_evt(void)
804 {
805     BTIF_TRACE_DEBUG("%s: notify DISASSOCIATE_JVM", __FUNCTION__);
806
807     HAL_CBACK(bt_hal_cbacks, thread_evt_cb, DISASSOCIATE_JVM);
808
809     /* shutdown complete, all events notified and we reset HAL callbacks */
810     bt_hal_cbacks = NULL;
811
812     return BT_STATUS_SUCCESS;
813 }
814
815 /****************************************************************************
816 **
817 **   BTIF Test Mode APIs
818 **
819 *****************************************************************************/
820 /*******************************************************************************
821 **
822 ** Function         btif_dut_mode_cback
823 **
824 ** Description     Callback invoked on completion of vendor specific test mode command
825 **
826 ** Returns          None
827 **
828 *******************************************************************************/
829 static void btif_dut_mode_cback( tBTM_VSC_CMPL *p )
830 {
831     UNUSED(p);
832     /* For now nothing to be done. */
833 }
834
835 /*******************************************************************************
836 **
837 ** Function         btif_dut_mode_configure
838 **
839 ** Description      Configure Test Mode - 'enable' to 1 puts the device in test mode and 0 exits
840 **                       test mode
841 **
842 ** Returns          BT_STATUS_SUCCESS on success
843 **
844 *******************************************************************************/
845 bt_status_t btif_dut_mode_configure(uint8_t enable)
846 {
847     BTIF_TRACE_DEBUG("%s", __FUNCTION__);
848
849     if (btif_core_state != BTIF_CORE_STATE_ENABLED) {
850         BTIF_TRACE_ERROR("btif_dut_mode_configure : Bluetooth not enabled");
851         return BT_STATUS_NOT_READY;
852     }
853
854     btif_dut_mode = enable;
855     if (enable == 1) {
856         BTA_EnableTestMode();
857     } else {
858         BTA_DisableTestMode();
859     }
860     return BT_STATUS_SUCCESS;
861 }
862
863 /*******************************************************************************
864 **
865 ** Function         btif_dut_mode_send
866 **
867 ** Description     Sends a HCI Vendor specific command to the controller
868 **
869 ** Returns          BT_STATUS_SUCCESS on success
870 **
871 *******************************************************************************/
872 bt_status_t btif_dut_mode_send(uint16_t opcode, uint8_t *buf, uint8_t len)
873 {
874     /* TODO: Check that opcode is a vendor command group */
875     BTIF_TRACE_DEBUG("%s", __FUNCTION__);
876     if (!btif_is_dut_mode()) {
877          BTIF_TRACE_ERROR("Bluedroid HAL needs to be init with test_mode set to 1.");
878          return BT_STATUS_FAIL;
879     }
880     BTM_VendorSpecificCommand(opcode, len, buf, btif_dut_mode_cback);
881     return BT_STATUS_SUCCESS;
882 }
883
884 /*****************************************************************************
885 **
886 **   btif api adapter property functions
887 **
888 *****************************************************************************/
889
890 static bt_status_t btif_in_get_adapter_properties(void)
891 {
892     bt_property_t properties[6];
893     uint32_t num_props;
894
895     bt_bdaddr_t addr;
896     bt_bdname_t name;
897     bt_scan_mode_t mode;
898     uint32_t disc_timeout;
899     bt_bdaddr_t bonded_devices[BTM_SEC_MAX_DEVICE_RECORDS];
900     bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
901     num_props = 0;
902
903     /* BD_ADDR */
904     BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_BDADDR,
905                                sizeof(addr), &addr);
906     btif_storage_get_adapter_property(&properties[num_props]);
907     num_props++;
908
909     /* BD_NAME */
910     BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_BDNAME,
911                                sizeof(name), &name);
912     btif_storage_get_adapter_property(&properties[num_props]);
913     num_props++;
914
915     /* SCAN_MODE */
916     BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_ADAPTER_SCAN_MODE,
917                                sizeof(mode), &mode);
918     btif_storage_get_adapter_property(&properties[num_props]);
919     num_props++;
920
921     /* DISC_TIMEOUT */
922     BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT,
923                                sizeof(disc_timeout), &disc_timeout);
924     btif_storage_get_adapter_property(&properties[num_props]);
925     num_props++;
926
927     /* BONDED_DEVICES */
928     BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_ADAPTER_BONDED_DEVICES,
929                                sizeof(bonded_devices), bonded_devices);
930     btif_storage_get_adapter_property(&properties[num_props]);
931     num_props++;
932
933     /* LOCAL UUIDs */
934     BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_UUIDS,
935                                sizeof(local_uuids), local_uuids);
936     btif_storage_get_adapter_property(&properties[num_props]);
937     num_props++;
938
939     HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
940                      BT_STATUS_SUCCESS, num_props, properties);
941
942     return BT_STATUS_SUCCESS;
943 }
944
945 static bt_status_t btif_in_get_remote_device_properties(bt_bdaddr_t *bd_addr)
946 {
947     bt_property_t remote_properties[8];
948     uint32_t num_props = 0;
949
950     bt_bdname_t name, alias;
951     uint32_t cod, devtype;
952     bt_uuid_t remote_uuids[BT_MAX_NUM_UUIDS];
953
954     memset(remote_properties, 0, sizeof(remote_properties));
955     BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_BDNAME,
956                                sizeof(name), &name);
957     btif_storage_get_remote_device_property(bd_addr,
958                                             &remote_properties[num_props]);
959     num_props++;
960
961     BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_REMOTE_FRIENDLY_NAME,
962                                sizeof(alias), &alias);
963     btif_storage_get_remote_device_property(bd_addr,
964                                             &remote_properties[num_props]);
965     num_props++;
966
967     BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_CLASS_OF_DEVICE,
968                                sizeof(cod), &cod);
969     btif_storage_get_remote_device_property(bd_addr,
970                                             &remote_properties[num_props]);
971     num_props++;
972
973     BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_TYPE_OF_DEVICE,
974                                sizeof(devtype), &devtype);
975     btif_storage_get_remote_device_property(bd_addr,
976                                             &remote_properties[num_props]);
977     num_props++;
978
979     BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_UUIDS,
980                                sizeof(remote_uuids), remote_uuids);
981     btif_storage_get_remote_device_property(bd_addr,
982                                             &remote_properties[num_props]);
983     num_props++;
984
985     HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
986                      BT_STATUS_SUCCESS, bd_addr, num_props, remote_properties);
987
988     return BT_STATUS_SUCCESS;
989 }
990
991
992 /*******************************************************************************
993 **
994 ** Function         execute_storage_request
995 **
996 ** Description      Executes adapter storage request in BTIF context
997 **
998 ** Returns          bt_status_t
999 **
1000 *******************************************************************************/
1001
1002 static void execute_storage_request(UINT16 event, char *p_param)
1003 {
1004     uint8_t is_local;
1005     int num_entries = 0;
1006     bt_status_t status = BT_STATUS_SUCCESS;
1007
1008     BTIF_TRACE_EVENT("execute storage request event : %d", event);
1009
1010     switch(event)
1011     {
1012         case BTIF_CORE_STORAGE_ADAPTER_WRITE:
1013         {
1014             btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
1015             bt_property_t *p_prop = &(p_req->write_req.prop);
1016             BTIF_TRACE_EVENT("type: %d, len %d, 0x%x", p_prop->type,
1017                                p_prop->len, p_prop->val);
1018
1019             status = btif_storage_set_adapter_property(p_prop);
1020             HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status, 1, p_prop);
1021         } break;
1022
1023         case BTIF_CORE_STORAGE_ADAPTER_READ:
1024         {
1025             btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
1026             char buf[512];
1027             bt_property_t prop;
1028             prop.type = p_req->read_req.type;
1029             prop.val = (void*)buf;
1030             prop.len = sizeof(buf);
1031             if (prop.type == BT_PROPERTY_LOCAL_LE_FEATURES)
1032             {
1033                 #if (BLE_INCLUDED == TRUE)
1034                 tBTM_BLE_VSC_CB cmn_vsc_cb;
1035                 bt_local_le_features_t local_le_features;
1036
1037                 /* LE features are not stored in storage. Should be retrived from stack */
1038                 BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
1039                 local_le_features.local_privacy_enabled = BTM_BleLocalPrivacyEnabled();
1040
1041                 prop.len = sizeof (bt_local_le_features_t);
1042                 if (cmn_vsc_cb.filter_support == 1)
1043                     local_le_features.max_adv_filter_supported = cmn_vsc_cb.max_filter;
1044                 else
1045                     local_le_features.max_adv_filter_supported = 0;
1046                 local_le_features.max_adv_instance = cmn_vsc_cb.adv_inst_max;
1047                 local_le_features.max_irk_list_size = cmn_vsc_cb.max_irk_list_sz;
1048                 local_le_features.rpa_offload_supported = cmn_vsc_cb.rpa_offloading;
1049                 local_le_features.scan_result_storage_size_hibyte =
1050                     (cmn_vsc_cb.tot_scan_results_strg >> 8) & (0xFF);
1051                 local_le_features.scan_result_storage_size_lobyte =
1052                     (cmn_vsc_cb.tot_scan_results_strg) & (0xFF);
1053                 local_le_features.activity_energy_info_supported = cmn_vsc_cb.energy_support;
1054                 memcpy(prop.val, &local_le_features, prop.len);
1055                 #endif
1056             }
1057             else
1058             {
1059                 status = btif_storage_get_adapter_property(&prop);
1060             }
1061             HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status, 1, &prop);
1062         } break;
1063
1064         case BTIF_CORE_STORAGE_ADAPTER_READ_ALL:
1065         {
1066             status = btif_in_get_adapter_properties();
1067         } break;
1068
1069         case BTIF_CORE_STORAGE_NOTIFY_STATUS:
1070         {
1071             HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status, 0, NULL);
1072         } break;
1073
1074         default:
1075             BTIF_TRACE_ERROR("%s invalid event id (%d)", __FUNCTION__, event);
1076             break;
1077     }
1078 }
1079
1080 static void execute_storage_remote_request(UINT16 event, char *p_param)
1081 {
1082     bt_status_t status = BT_STATUS_FAIL;
1083     bt_property_t prop;
1084
1085     BTIF_TRACE_EVENT("execute storage remote request event : %d", event);
1086
1087     switch (event)
1088     {
1089         case BTIF_CORE_STORAGE_REMOTE_READ:
1090         {
1091             char buf[1024];
1092             btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
1093             prop.type = p_req->read_req.type;
1094             prop.val = (void*) buf;
1095             prop.len = sizeof(buf);
1096
1097             status = btif_storage_get_remote_device_property(&(p_req->read_req.bd_addr),
1098                                                              &prop);
1099             HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1100                             status, &(p_req->read_req.bd_addr), 1, &prop);
1101         }break;
1102         case BTIF_CORE_STORAGE_REMOTE_WRITE:
1103         {
1104            btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
1105            status = btif_storage_set_remote_device_property(&(p_req->write_req.bd_addr),
1106                                                             &(p_req->write_req.prop));
1107         }break;
1108         case BTIF_CORE_STORAGE_REMOTE_READ_ALL:
1109         {
1110            btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
1111            btif_in_get_remote_device_properties(&p_req->read_req.bd_addr);
1112         }break;
1113     }
1114 }
1115
1116 void btif_adapter_properties_evt(bt_status_t status, uint32_t num_props,
1117                                     bt_property_t *p_props)
1118 {
1119     HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
1120                      status, num_props, p_props);
1121
1122 }
1123 void btif_remote_properties_evt(bt_status_t status, bt_bdaddr_t *remote_addr,
1124                                    uint32_t num_props, bt_property_t *p_props)
1125 {
1126     HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1127                      status, remote_addr, num_props, p_props);
1128 }
1129
1130 /*******************************************************************************
1131 **
1132 ** Function         btif_in_storage_request_copy_cb
1133 **
1134 ** Description     Switch context callback function to perform the deep copy for
1135 **                 both the adapter and remote_device property API
1136 **
1137 ** Returns          None
1138 **
1139 *******************************************************************************/
1140 static void btif_in_storage_request_copy_cb(UINT16 event,
1141                                                  char *p_new_buf, char *p_old_buf)
1142 {
1143      btif_storage_req_t *new_req = (btif_storage_req_t*)p_new_buf;
1144      btif_storage_req_t *old_req = (btif_storage_req_t*)p_old_buf;
1145
1146      BTIF_TRACE_EVENT("%s", __FUNCTION__);
1147      switch (event)
1148      {
1149          case BTIF_CORE_STORAGE_REMOTE_WRITE:
1150          case BTIF_CORE_STORAGE_ADAPTER_WRITE:
1151          {
1152              bdcpy(new_req->write_req.bd_addr.address, old_req->write_req.bd_addr.address);
1153              /* Copy the member variables one at a time */
1154              new_req->write_req.prop.type = old_req->write_req.prop.type;
1155              new_req->write_req.prop.len = old_req->write_req.prop.len;
1156
1157              new_req->write_req.prop.val = (UINT8 *)(p_new_buf + sizeof(btif_storage_req_t));
1158              memcpy(new_req->write_req.prop.val, old_req->write_req.prop.val,
1159                     old_req->write_req.prop.len);
1160          }break;
1161      }
1162 }
1163
1164 /*******************************************************************************
1165 **
1166 ** Function         btif_get_adapter_properties
1167 **
1168 ** Description      Fetch all available properties (local & remote)
1169 **
1170 ** Returns          bt_status_t
1171 **
1172 *******************************************************************************/
1173
1174 bt_status_t btif_get_adapter_properties(void)
1175 {
1176     BTIF_TRACE_EVENT("%s", __FUNCTION__);
1177
1178     if (!btif_is_enabled())
1179         return BT_STATUS_NOT_READY;
1180
1181     return btif_transfer_context(execute_storage_request,
1182                                  BTIF_CORE_STORAGE_ADAPTER_READ_ALL,
1183                                  NULL, 0, NULL);
1184 }
1185
1186 /*******************************************************************************
1187 **
1188 ** Function         btif_get_adapter_property
1189 **
1190 ** Description      Fetches property value from local cache
1191 **
1192 ** Returns          bt_status_t
1193 **
1194 *******************************************************************************/
1195
1196 bt_status_t btif_get_adapter_property(bt_property_type_t type)
1197 {
1198     btif_storage_req_t req;
1199
1200     BTIF_TRACE_EVENT("%s %d", __FUNCTION__, type);
1201
1202     /* Allow get_adapter_property only for BDADDR and BDNAME if BT is disabled */
1203     if (!btif_is_enabled() && (type != BT_PROPERTY_BDADDR) && (type != BT_PROPERTY_BDNAME))
1204         return BT_STATUS_NOT_READY;
1205
1206     memset(&(req.read_req.bd_addr), 0, sizeof(bt_bdaddr_t));
1207     req.read_req.type = type;
1208
1209     return btif_transfer_context(execute_storage_request,
1210                                  BTIF_CORE_STORAGE_ADAPTER_READ,
1211                                 (char*)&req, sizeof(btif_storage_req_t), NULL);
1212 }
1213
1214 /*******************************************************************************
1215 **
1216 ** Function         btif_set_adapter_property
1217 **
1218 ** Description      Updates core stack with property value and stores it in
1219 **                  local cache
1220 **
1221 ** Returns          bt_status_t
1222 **
1223 *******************************************************************************/
1224
1225 bt_status_t btif_set_adapter_property(const bt_property_t *property)
1226 {
1227     btif_storage_req_t req;
1228     bt_status_t status = BT_STATUS_SUCCESS;
1229     int storage_req_id = BTIF_CORE_STORAGE_NOTIFY_STATUS; /* default */
1230     char bd_name[BTM_MAX_LOC_BD_NAME_LEN +1];
1231     UINT16  name_len = 0;
1232
1233     BTIF_TRACE_EVENT("btif_set_adapter_property type: %d, len %d, 0x%x",
1234                       property->type, property->len, property->val);
1235
1236     if (!btif_is_enabled())
1237         return BT_STATUS_NOT_READY;
1238
1239     switch(property->type)
1240     {
1241         case BT_PROPERTY_BDNAME:
1242             {
1243                 name_len = property->len > BTM_MAX_LOC_BD_NAME_LEN ? BTM_MAX_LOC_BD_NAME_LEN:
1244                                                                      property->len;
1245                 memcpy(bd_name,property->val, name_len);
1246                 bd_name[name_len] = '\0';
1247
1248                 BTIF_TRACE_EVENT("set property name : %s", (char *)bd_name);
1249
1250                 BTA_DmSetDeviceName((char *)bd_name);
1251
1252                 storage_req_id = BTIF_CORE_STORAGE_ADAPTER_WRITE;
1253             }
1254             break;
1255
1256         case BT_PROPERTY_ADAPTER_SCAN_MODE:
1257             {
1258                 bt_scan_mode_t mode = *(bt_scan_mode_t*)property->val;
1259                 tBTA_DM_DISC disc_mode;
1260                 tBTA_DM_CONN conn_mode;
1261
1262                 switch(mode)
1263                 {
1264                     case BT_SCAN_MODE_NONE:
1265                         disc_mode = BTA_DM_NON_DISC;
1266                         conn_mode = BTA_DM_NON_CONN;
1267                         break;
1268
1269                     case BT_SCAN_MODE_CONNECTABLE:
1270                         disc_mode = BTA_DM_NON_DISC;
1271                         conn_mode = BTA_DM_CONN;
1272                         break;
1273
1274                     case BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE:
1275                         disc_mode = BTA_DM_GENERAL_DISC;
1276                         conn_mode = BTA_DM_CONN;
1277                         break;
1278
1279                     default:
1280                         BTIF_TRACE_ERROR("invalid scan mode (0x%x)", mode);
1281                         return BT_STATUS_PARM_INVALID;
1282                 }
1283
1284                 BTIF_TRACE_EVENT("set property scan mode : %x", mode);
1285
1286                 BTA_DmSetVisibility(disc_mode, conn_mode, BTA_DM_IGNORE, BTA_DM_IGNORE);
1287
1288                 storage_req_id = BTIF_CORE_STORAGE_ADAPTER_WRITE;
1289             }
1290             break;
1291         case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
1292             {
1293                 /* Nothing to do beside store the value in NV.  Java
1294                    will change the SCAN_MODE property after setting timeout,
1295                    if required */
1296                 storage_req_id = BTIF_CORE_STORAGE_ADAPTER_WRITE;
1297             }
1298             break;
1299         case BT_PROPERTY_BDADDR:
1300         case BT_PROPERTY_UUIDS:
1301         case BT_PROPERTY_ADAPTER_BONDED_DEVICES:
1302         case BT_PROPERTY_REMOTE_FRIENDLY_NAME:
1303             /* no write support through HAL, these properties are only populated from BTA events */
1304             status = BT_STATUS_FAIL;
1305             break;
1306         default:
1307             BTIF_TRACE_ERROR("btif_get_adapter_property : invalid type %d",
1308             property->type);
1309             status = BT_STATUS_FAIL;
1310             break;
1311     }
1312
1313     if (storage_req_id != BTIF_CORE_STORAGE_NO_ACTION)
1314     {
1315         int btif_status;
1316         /* pass on to storage for updating local database */
1317
1318         memset(&(req.write_req.bd_addr), 0, sizeof(bt_bdaddr_t));
1319         memcpy(&(req.write_req.prop), property, sizeof(bt_property_t));
1320
1321         return btif_transfer_context(execute_storage_request,
1322                                      storage_req_id,
1323                                      (char*)&req,
1324                                      sizeof(btif_storage_req_t)+property->len,
1325                                      btif_in_storage_request_copy_cb);
1326     }
1327
1328     return status;
1329
1330 }
1331
1332 /*******************************************************************************
1333 **
1334 ** Function         btif_get_remote_device_property
1335 **
1336 ** Description      Fetches the remote device property from the NVRAM
1337 **
1338 ** Returns          bt_status_t
1339 **
1340 *******************************************************************************/
1341 bt_status_t btif_get_remote_device_property(bt_bdaddr_t *remote_addr,
1342                                                  bt_property_type_t type)
1343 {
1344     btif_storage_req_t req;
1345
1346     if (!btif_is_enabled())
1347         return BT_STATUS_NOT_READY;
1348
1349     memcpy(&(req.read_req.bd_addr), remote_addr, sizeof(bt_bdaddr_t));
1350     req.read_req.type = type;
1351     return btif_transfer_context(execute_storage_remote_request,
1352                                  BTIF_CORE_STORAGE_REMOTE_READ,
1353                                  (char*)&req, sizeof(btif_storage_req_t),
1354                                  NULL);
1355 }
1356
1357 /*******************************************************************************
1358 **
1359 ** Function         btif_get_remote_device_properties
1360 **
1361 ** Description      Fetches all the remote device properties from NVRAM
1362 **
1363 ** Returns          bt_status_t
1364 **
1365 *******************************************************************************/
1366 bt_status_t btif_get_remote_device_properties(bt_bdaddr_t *remote_addr)
1367 {
1368     btif_storage_req_t req;
1369
1370     if (!btif_is_enabled())
1371         return BT_STATUS_NOT_READY;
1372
1373     memcpy(&(req.read_req.bd_addr), remote_addr, sizeof(bt_bdaddr_t));
1374     return btif_transfer_context(execute_storage_remote_request,
1375                                  BTIF_CORE_STORAGE_REMOTE_READ_ALL,
1376                                  (char*)&req, sizeof(btif_storage_req_t),
1377                                  NULL);
1378 }
1379
1380 /*******************************************************************************
1381 **
1382 ** Function         btif_set_remote_device_property
1383 **
1384 ** Description      Writes the remote device property to NVRAM.
1385 **                  Currently, BT_PROPERTY_REMOTE_FRIENDLY_NAME is the only
1386 **                  remote device property that can be set
1387 **
1388 ** Returns          bt_status_t
1389 **
1390 *******************************************************************************/
1391 bt_status_t btif_set_remote_device_property(bt_bdaddr_t *remote_addr,
1392                                                  const bt_property_t *property)
1393 {
1394     btif_storage_req_t req;
1395
1396     if (!btif_is_enabled())
1397         return BT_STATUS_NOT_READY;
1398
1399     memcpy(&(req.write_req.bd_addr), remote_addr, sizeof(bt_bdaddr_t));
1400     memcpy(&(req.write_req.prop), property, sizeof(bt_property_t));
1401
1402     return btif_transfer_context(execute_storage_remote_request,
1403                                  BTIF_CORE_STORAGE_REMOTE_WRITE,
1404                                  (char*)&req,
1405                                  sizeof(btif_storage_req_t)+property->len,
1406                                  btif_in_storage_request_copy_cb);
1407 }
1408
1409
1410 /*******************************************************************************
1411 **
1412 ** Function         btif_get_remote_service_record
1413 **
1414 ** Description      Looks up the service matching uuid on the remote device
1415 **                  and fetches the SCN and service_name if the UUID is found
1416 **
1417 ** Returns          bt_status_t
1418 **
1419 *******************************************************************************/
1420 bt_status_t btif_get_remote_service_record(bt_bdaddr_t *remote_addr,
1421                                                bt_uuid_t *uuid)
1422 {
1423     if (!btif_is_enabled())
1424         return BT_STATUS_NOT_READY;
1425
1426     return btif_dm_get_remote_service_record(remote_addr, uuid);
1427 }
1428
1429
1430 /*******************************************************************************
1431 **
1432 ** Function         btif_get_enabled_services_mask
1433 **
1434 ** Description      Fetches currently enabled services
1435 **
1436 ** Returns          tBTA_SERVICE_MASK
1437 **
1438 *******************************************************************************/
1439
1440 tBTA_SERVICE_MASK btif_get_enabled_services_mask(void)
1441 {
1442     return btif_enabled_services;
1443 }
1444
1445 /*******************************************************************************
1446 **
1447 ** Function         btif_enable_service
1448 **
1449 ** Description      Enables the service 'service_ID' to the service_mask.
1450 **                  Upon BT enable, BTIF core shall invoke the BTA APIs to
1451 **                  enable the profiles
1452 **
1453 ** Returns          bt_status_t
1454 **
1455 *******************************************************************************/
1456 bt_status_t btif_enable_service(tBTA_SERVICE_ID service_id)
1457 {
1458     tBTA_SERVICE_ID *p_id = &service_id;
1459
1460     /* If BT is enabled, we need to switch to BTIF context and trigger the
1461      * enable for that profile
1462      *
1463      * Otherwise, we just set the flag. On BT_Enable, the DM will trigger
1464      * enable for the profiles that have been enabled */
1465
1466     btif_enabled_services |= (1 << service_id);
1467
1468     BTIF_TRACE_DEBUG("%s: current services:0x%x", __FUNCTION__, btif_enabled_services);
1469
1470     if (btif_is_enabled())
1471     {
1472         btif_transfer_context(btif_dm_execute_service_request,
1473                               BTIF_DM_ENABLE_SERVICE,
1474                               (char*)p_id, sizeof(tBTA_SERVICE_ID), NULL);
1475     }
1476
1477     return BT_STATUS_SUCCESS;
1478 }
1479 /*******************************************************************************
1480 **
1481 ** Function         btif_disable_service
1482 **
1483 ** Description      Disables the service 'service_ID' to the service_mask.
1484 **                  Upon BT disable, BTIF core shall invoke the BTA APIs to
1485 **                  disable the profiles
1486 **
1487 ** Returns          bt_status_t
1488 **
1489 *******************************************************************************/
1490 bt_status_t btif_disable_service(tBTA_SERVICE_ID service_id)
1491 {
1492     tBTA_SERVICE_ID *p_id = &service_id;
1493
1494     /* If BT is enabled, we need to switch to BTIF context and trigger the
1495      * disable for that profile so that the appropriate uuid_property_changed will
1496      * be triggerred. Otherwise, we just need to clear the service_id in the mask
1497      */
1498
1499     btif_enabled_services &=  (tBTA_SERVICE_MASK)(~(1<<service_id));
1500
1501     BTIF_TRACE_DEBUG("%s: Current Services:0x%x", __FUNCTION__, btif_enabled_services);
1502
1503     if (btif_is_enabled())
1504     {
1505         btif_transfer_context(btif_dm_execute_service_request,
1506                               BTIF_DM_DISABLE_SERVICE,
1507                               (char*)p_id, sizeof(tBTA_SERVICE_ID), NULL);
1508     }
1509
1510     return BT_STATUS_SUCCESS;
1511 }
1512
1513 /*******************************************************************************
1514 **
1515 ** Function         btif_config_hci_snoop_log
1516 **
1517 ** Description      enable or disable HCI snoop log
1518 **
1519 ** Returns          bt_status_t
1520 **
1521 *******************************************************************************/
1522 bt_status_t btif_config_hci_snoop_log(uint8_t enable)
1523 {
1524     bte_main_config_hci_logging(enable != 0,
1525              btif_core_state == BTIF_CORE_STATE_DISABLED);
1526     return BT_STATUS_SUCCESS;
1527 }