btif_core.c revision d773c2cfd3675aaec431a42b79b26b24e0555ed9
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
76typedef struct {
77  bt_bdaddr_t bd_addr;
78  bt_property_type_t type;
79} btif_storage_read_t;
80
81typedef struct {
82  bt_bdaddr_t bd_addr;
83  bt_property_t prop;
84} btif_storage_write_t;
85
86typedef union {
87  btif_storage_read_t read_req;
88  btif_storage_write_t write_req;
89} btif_storage_req_t;
90
91typedef 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
102bt_bdaddr_t btif_local_bd_addr;
103
104static UINT32 btif_task_stack[(BTIF_TASK_STACK_SIZE + 3) / 4];
105
106/* holds main adapter state */
107static btif_core_state_t btif_core_state = BTIF_CORE_STATE_DISABLED;
108
109static int btif_shutdown_pending = 0;
110static 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*/
118static UINT8 btif_dut_mode = 0;
119
120/************************************************************************************
121**  Static functions
122************************************************************************************/
123static bt_status_t btif_associate_evt(void);
124static bt_status_t btif_disassociate_evt(void);
125
126/* sends message to btif task */
127static void btif_sendmsg(void *p_msg);
128
129/************************************************************************************
130**  Externs
131************************************************************************************/
132extern void bte_load_did_conf(const char *p_path);
133
134/** TODO: Move these to _common.h */
135void bte_main_boot_entry(void);
136void bte_main_enable();
137void bte_main_disable(void);
138void bte_main_shutdown(void);
139#if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
140void bte_main_enable_lpm(BOOLEAN enable);
141#endif
142void bte_main_postload_cfg(void);
143void btif_dm_execute_service_request(UINT16 event, char *p_param);
144#ifdef BTIF_DM_OOB_TEST
145void btif_dm_load_local_oob(void);
146#endif
147void 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
171static 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
201bt_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
245UINT8 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
260int 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
276static 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
361void btif_sendmsg(void *p_msg)
362{
363    GKI_send_msg(BTIF_TASK, BTU_BTIF_MBOX, p_msg);
364}
365
366static 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
477bt_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
509static 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
528bt_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
558void 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*******************************************************************************/
657bt_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
705void 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    bte_main_disable();
714
715    /* update local state */
716    btif_core_state = BTIF_CORE_STATE_DISABLED;
717
718    /* callback to HAL */
719    HAL_CBACK(bt_hal_cbacks, adapter_state_changed_cb, BT_STATE_OFF);
720
721    if (btif_shutdown_pending)
722    {
723        BTIF_TRACE_DEBUG("%s: calling btif_shutdown_bluetooth", __FUNCTION__);
724        btif_shutdown_bluetooth();
725    }
726}
727
728
729/*******************************************************************************
730**
731** Function         btif_shutdown_bluetooth
732**
733** Description      Finalizes BT scheduler shutdown and terminates BTIF
734**                  task.
735**
736** Returns          void
737**
738*******************************************************************************/
739
740bt_status_t btif_shutdown_bluetooth(void)
741{
742    BTIF_TRACE_DEBUG("%s", __FUNCTION__);
743
744    if (btif_core_state == BTIF_CORE_STATE_DISABLING)
745    {
746        BTIF_TRACE_WARNING("shutdown during disabling");
747        /* shutdown called before disabling is done */
748        btif_shutdown_pending = 1;
749        return BT_STATUS_NOT_READY;
750    }
751
752    if (btif_is_enabled())
753    {
754        BTIF_TRACE_WARNING("shutdown while still enabled, initiate disable");
755
756        /* shutdown called prior to disabling, initiate disable */
757        btif_disable_bluetooth();
758        btif_shutdown_pending = 1;
759        return BT_STATUS_NOT_READY;
760    }
761
762    btif_shutdown_pending = 0;
763
764    if (btif_core_state == BTIF_CORE_STATE_ENABLING)
765    {
766        // Java layer abort BT ENABLING, could be due to ENABLE TIMEOUT
767        // Direct call from cleanup()@bluetooth.c
768        // bring down HCI/Vendor lib
769        bte_main_disable();
770        btif_core_state = BTIF_CORE_STATE_DISABLED;
771        HAL_CBACK(bt_hal_cbacks, adapter_state_changed_cb, BT_STATE_OFF);
772    }
773
774    GKI_destroy_task(BTIF_TASK);
775    btif_queue_release();
776    bte_main_shutdown();
777
778    btif_dut_mode = 0;
779
780    bt_utils_cleanup();
781
782    BTIF_TRACE_DEBUG("%s done", __FUNCTION__);
783
784    return BT_STATUS_SUCCESS;
785}
786
787
788/*******************************************************************************
789**
790** Function         btif_disassociate_evt
791**
792** Description      Event indicating btif_task is going down
793**                  Detach btif_task to JVM
794**
795** Returns          void
796**
797*******************************************************************************/
798
799static bt_status_t btif_disassociate_evt(void)
800{
801    BTIF_TRACE_DEBUG("%s: notify DISASSOCIATE_JVM", __FUNCTION__);
802
803    HAL_CBACK(bt_hal_cbacks, thread_evt_cb, DISASSOCIATE_JVM);
804
805    /* shutdown complete, all events notified and we reset HAL callbacks */
806    bt_hal_cbacks = NULL;
807
808    return BT_STATUS_SUCCESS;
809}
810
811/****************************************************************************
812**
813**   BTIF Test Mode APIs
814**
815*****************************************************************************/
816/*******************************************************************************
817**
818** Function         btif_dut_mode_cback
819**
820** Description     Callback invoked on completion of vendor specific test mode command
821**
822** Returns          None
823**
824*******************************************************************************/
825static void btif_dut_mode_cback( tBTM_VSC_CMPL *p )
826{
827    UNUSED(p);
828    /* For now nothing to be done. */
829}
830
831/*******************************************************************************
832**
833** Function         btif_dut_mode_configure
834**
835** Description      Configure Test Mode - 'enable' to 1 puts the device in test mode and 0 exits
836**                       test mode
837**
838** Returns          BT_STATUS_SUCCESS on success
839**
840*******************************************************************************/
841bt_status_t btif_dut_mode_configure(uint8_t enable)
842{
843    BTIF_TRACE_DEBUG("%s", __FUNCTION__);
844
845    if (btif_core_state != BTIF_CORE_STATE_ENABLED) {
846        BTIF_TRACE_ERROR("btif_dut_mode_configure : Bluetooth not enabled");
847        return BT_STATUS_NOT_READY;
848    }
849
850    btif_dut_mode = enable;
851    if (enable == 1) {
852        BTA_EnableTestMode();
853    } else {
854        BTA_DisableTestMode();
855    }
856    return BT_STATUS_SUCCESS;
857}
858
859/*******************************************************************************
860**
861** Function         btif_dut_mode_send
862**
863** Description     Sends a HCI Vendor specific command to the controller
864**
865** Returns          BT_STATUS_SUCCESS on success
866**
867*******************************************************************************/
868bt_status_t btif_dut_mode_send(uint16_t opcode, uint8_t *buf, uint8_t len)
869{
870    /* TODO: Check that opcode is a vendor command group */
871    BTIF_TRACE_DEBUG("%s", __FUNCTION__);
872    if (!btif_is_dut_mode()) {
873         BTIF_TRACE_ERROR("Bluedroid HAL needs to be init with test_mode set to 1.");
874         return BT_STATUS_FAIL;
875    }
876    BTM_VendorSpecificCommand(opcode, len, buf, btif_dut_mode_cback);
877    return BT_STATUS_SUCCESS;
878}
879
880/*****************************************************************************
881**
882**   btif api adapter property functions
883**
884*****************************************************************************/
885
886static bt_status_t btif_in_get_adapter_properties(void)
887{
888    bt_property_t properties[6];
889    uint32_t num_props;
890
891    bt_bdaddr_t addr;
892    bt_bdname_t name;
893    bt_scan_mode_t mode;
894    uint32_t disc_timeout;
895    bt_bdaddr_t bonded_devices[BTM_SEC_MAX_DEVICE_RECORDS];
896    bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
897    num_props = 0;
898
899    /* BD_ADDR */
900    BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_BDADDR,
901                               sizeof(addr), &addr);
902    btif_storage_get_adapter_property(&properties[num_props]);
903    num_props++;
904
905    /* BD_NAME */
906    BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_BDNAME,
907                               sizeof(name), &name);
908    btif_storage_get_adapter_property(&properties[num_props]);
909    num_props++;
910
911    /* SCAN_MODE */
912    BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_ADAPTER_SCAN_MODE,
913                               sizeof(mode), &mode);
914    btif_storage_get_adapter_property(&properties[num_props]);
915    num_props++;
916
917    /* DISC_TIMEOUT */
918    BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT,
919                               sizeof(disc_timeout), &disc_timeout);
920    btif_storage_get_adapter_property(&properties[num_props]);
921    num_props++;
922
923    /* BONDED_DEVICES */
924    BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_ADAPTER_BONDED_DEVICES,
925                               sizeof(bonded_devices), bonded_devices);
926    btif_storage_get_adapter_property(&properties[num_props]);
927    num_props++;
928
929    /* LOCAL UUIDs */
930    BTIF_STORAGE_FILL_PROPERTY(&properties[num_props], BT_PROPERTY_UUIDS,
931                               sizeof(local_uuids), local_uuids);
932    btif_storage_get_adapter_property(&properties[num_props]);
933    num_props++;
934
935    HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
936                     BT_STATUS_SUCCESS, num_props, properties);
937
938    return BT_STATUS_SUCCESS;
939}
940
941static bt_status_t btif_in_get_remote_device_properties(bt_bdaddr_t *bd_addr)
942{
943    bt_property_t remote_properties[8];
944    uint32_t num_props = 0;
945
946    bt_bdname_t name, alias;
947    uint32_t cod, devtype;
948    bt_uuid_t remote_uuids[BT_MAX_NUM_UUIDS];
949
950    memset(remote_properties, 0, sizeof(remote_properties));
951    BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_BDNAME,
952                               sizeof(name), &name);
953    btif_storage_get_remote_device_property(bd_addr,
954                                            &remote_properties[num_props]);
955    num_props++;
956
957    BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_REMOTE_FRIENDLY_NAME,
958                               sizeof(alias), &alias);
959    btif_storage_get_remote_device_property(bd_addr,
960                                            &remote_properties[num_props]);
961    num_props++;
962
963    BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_CLASS_OF_DEVICE,
964                               sizeof(cod), &cod);
965    btif_storage_get_remote_device_property(bd_addr,
966                                            &remote_properties[num_props]);
967    num_props++;
968
969    BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_TYPE_OF_DEVICE,
970                               sizeof(devtype), &devtype);
971    btif_storage_get_remote_device_property(bd_addr,
972                                            &remote_properties[num_props]);
973    num_props++;
974
975    BTIF_STORAGE_FILL_PROPERTY(&remote_properties[num_props], BT_PROPERTY_UUIDS,
976                               sizeof(remote_uuids), remote_uuids);
977    btif_storage_get_remote_device_property(bd_addr,
978                                            &remote_properties[num_props]);
979    num_props++;
980
981    HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
982                     BT_STATUS_SUCCESS, bd_addr, num_props, remote_properties);
983
984    return BT_STATUS_SUCCESS;
985}
986
987
988/*******************************************************************************
989**
990** Function         execute_storage_request
991**
992** Description      Executes adapter storage request in BTIF context
993**
994** Returns          bt_status_t
995**
996*******************************************************************************/
997
998static void execute_storage_request(UINT16 event, char *p_param)
999{
1000    uint8_t is_local;
1001    int num_entries = 0;
1002    bt_status_t status = BT_STATUS_SUCCESS;
1003
1004    BTIF_TRACE_EVENT("execute storage request event : %d", event);
1005
1006    switch(event)
1007    {
1008        case BTIF_CORE_STORAGE_ADAPTER_WRITE:
1009        {
1010            btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
1011            bt_property_t *p_prop = &(p_req->write_req.prop);
1012            BTIF_TRACE_EVENT("type: %d, len %d, 0x%x", p_prop->type,
1013                               p_prop->len, p_prop->val);
1014
1015            status = btif_storage_set_adapter_property(p_prop);
1016            HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status, 1, p_prop);
1017        } break;
1018
1019        case BTIF_CORE_STORAGE_ADAPTER_READ:
1020        {
1021            btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
1022            char buf[512];
1023            bt_property_t prop;
1024            prop.type = p_req->read_req.type;
1025            prop.val = (void*)buf;
1026            prop.len = sizeof(buf);
1027            if (prop.type == BT_PROPERTY_LOCAL_LE_FEATURES)
1028            {
1029                #if (BLE_INCLUDED == TRUE)
1030                tBTM_BLE_VSC_CB cmn_vsc_cb;
1031                bt_local_le_features_t local_le_features;
1032
1033                /* LE features are not stored in storage. Should be retrived from stack */
1034                BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
1035                local_le_features.local_privacy_enabled = BTM_BleLocalPrivacyEnabled();
1036
1037                prop.len = sizeof (bt_local_le_features_t);
1038                if (cmn_vsc_cb.filter_support == 1)
1039                    local_le_features.max_adv_filter_supported = cmn_vsc_cb.max_filter;
1040                else
1041                    local_le_features.max_adv_filter_supported = 0;
1042                local_le_features.max_adv_instance = cmn_vsc_cb.adv_inst_max;
1043                local_le_features.max_irk_list_size = cmn_vsc_cb.max_irk_list_sz;
1044                local_le_features.rpa_offload_supported = cmn_vsc_cb.rpa_offloading;
1045                local_le_features.scan_result_storage_size_hibyte =
1046                    (cmn_vsc_cb.tot_scan_results_strg >> 8) & (0xFF);
1047                local_le_features.scan_result_storage_size_lobyte =
1048                    (cmn_vsc_cb.tot_scan_results_strg) & (0xFF);
1049                local_le_features.activity_energy_info_supported = cmn_vsc_cb.energy_support;
1050                memcpy(prop.val, &local_le_features, prop.len);
1051                #endif
1052            }
1053            else
1054            {
1055                status = btif_storage_get_adapter_property(&prop);
1056            }
1057            HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status, 1, &prop);
1058        } break;
1059
1060        case BTIF_CORE_STORAGE_ADAPTER_READ_ALL:
1061        {
1062            status = btif_in_get_adapter_properties();
1063        } break;
1064
1065        case BTIF_CORE_STORAGE_NOTIFY_STATUS:
1066        {
1067            HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status, 0, NULL);
1068        } break;
1069
1070        default:
1071            BTIF_TRACE_ERROR("%s invalid event id (%d)", __FUNCTION__, event);
1072            break;
1073    }
1074}
1075
1076static void execute_storage_remote_request(UINT16 event, char *p_param)
1077{
1078    bt_status_t status = BT_STATUS_FAIL;
1079    bt_property_t prop;
1080
1081    BTIF_TRACE_EVENT("execute storage remote request event : %d", event);
1082
1083    switch (event)
1084    {
1085        case BTIF_CORE_STORAGE_REMOTE_READ:
1086        {
1087            char buf[1024];
1088            btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
1089            prop.type = p_req->read_req.type;
1090            prop.val = (void*) buf;
1091            prop.len = sizeof(buf);
1092
1093            status = btif_storage_get_remote_device_property(&(p_req->read_req.bd_addr),
1094                                                             &prop);
1095            HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1096                            status, &(p_req->read_req.bd_addr), 1, &prop);
1097        }break;
1098        case BTIF_CORE_STORAGE_REMOTE_WRITE:
1099        {
1100           btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
1101           status = btif_storage_set_remote_device_property(&(p_req->write_req.bd_addr),
1102                                                            &(p_req->write_req.prop));
1103        }break;
1104        case BTIF_CORE_STORAGE_REMOTE_READ_ALL:
1105        {
1106           btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
1107           btif_in_get_remote_device_properties(&p_req->read_req.bd_addr);
1108        }break;
1109    }
1110}
1111
1112void btif_adapter_properties_evt(bt_status_t status, uint32_t num_props,
1113                                    bt_property_t *p_props)
1114{
1115    HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
1116                     status, num_props, p_props);
1117
1118}
1119void btif_remote_properties_evt(bt_status_t status, bt_bdaddr_t *remote_addr,
1120                                   uint32_t num_props, bt_property_t *p_props)
1121{
1122    HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1123                     status, remote_addr, num_props, p_props);
1124}
1125
1126/*******************************************************************************
1127**
1128** Function         btif_in_storage_request_copy_cb
1129**
1130** Description     Switch context callback function to perform the deep copy for
1131**                 both the adapter and remote_device property API
1132**
1133** Returns          None
1134**
1135*******************************************************************************/
1136static void btif_in_storage_request_copy_cb(UINT16 event,
1137                                                 char *p_new_buf, char *p_old_buf)
1138{
1139     btif_storage_req_t *new_req = (btif_storage_req_t*)p_new_buf;
1140     btif_storage_req_t *old_req = (btif_storage_req_t*)p_old_buf;
1141
1142     BTIF_TRACE_EVENT("%s", __FUNCTION__);
1143     switch (event)
1144     {
1145         case BTIF_CORE_STORAGE_REMOTE_WRITE:
1146         case BTIF_CORE_STORAGE_ADAPTER_WRITE:
1147         {
1148             bdcpy(new_req->write_req.bd_addr.address, old_req->write_req.bd_addr.address);
1149             /* Copy the member variables one at a time */
1150             new_req->write_req.prop.type = old_req->write_req.prop.type;
1151             new_req->write_req.prop.len = old_req->write_req.prop.len;
1152
1153             new_req->write_req.prop.val = (UINT8 *)(p_new_buf + sizeof(btif_storage_req_t));
1154             memcpy(new_req->write_req.prop.val, old_req->write_req.prop.val,
1155                    old_req->write_req.prop.len);
1156         }break;
1157     }
1158}
1159
1160/*******************************************************************************
1161**
1162** Function         btif_get_adapter_properties
1163**
1164** Description      Fetch all available properties (local & remote)
1165**
1166** Returns          bt_status_t
1167**
1168*******************************************************************************/
1169
1170bt_status_t btif_get_adapter_properties(void)
1171{
1172    BTIF_TRACE_EVENT("%s", __FUNCTION__);
1173
1174    if (!btif_is_enabled())
1175        return BT_STATUS_NOT_READY;
1176
1177    return btif_transfer_context(execute_storage_request,
1178                                 BTIF_CORE_STORAGE_ADAPTER_READ_ALL,
1179                                 NULL, 0, NULL);
1180}
1181
1182/*******************************************************************************
1183**
1184** Function         btif_get_adapter_property
1185**
1186** Description      Fetches property value from local cache
1187**
1188** Returns          bt_status_t
1189**
1190*******************************************************************************/
1191
1192bt_status_t btif_get_adapter_property(bt_property_type_t type)
1193{
1194    btif_storage_req_t req;
1195
1196    BTIF_TRACE_EVENT("%s %d", __FUNCTION__, type);
1197
1198    /* Allow get_adapter_property only for BDADDR and BDNAME if BT is disabled */
1199    if (!btif_is_enabled() && (type != BT_PROPERTY_BDADDR) && (type != BT_PROPERTY_BDNAME))
1200        return BT_STATUS_NOT_READY;
1201
1202    memset(&(req.read_req.bd_addr), 0, sizeof(bt_bdaddr_t));
1203    req.read_req.type = type;
1204
1205    return btif_transfer_context(execute_storage_request,
1206                                 BTIF_CORE_STORAGE_ADAPTER_READ,
1207                                (char*)&req, sizeof(btif_storage_req_t), NULL);
1208}
1209
1210/*******************************************************************************
1211**
1212** Function         btif_set_adapter_property
1213**
1214** Description      Updates core stack with property value and stores it in
1215**                  local cache
1216**
1217** Returns          bt_status_t
1218**
1219*******************************************************************************/
1220
1221bt_status_t btif_set_adapter_property(const bt_property_t *property)
1222{
1223    btif_storage_req_t req;
1224    bt_status_t status = BT_STATUS_SUCCESS;
1225    int storage_req_id = BTIF_CORE_STORAGE_NOTIFY_STATUS; /* default */
1226    char bd_name[BTM_MAX_LOC_BD_NAME_LEN +1];
1227    UINT16  name_len = 0;
1228
1229    BTIF_TRACE_EVENT("btif_set_adapter_property type: %d, len %d, 0x%x",
1230                      property->type, property->len, property->val);
1231
1232    if (!btif_is_enabled())
1233        return BT_STATUS_NOT_READY;
1234
1235    switch(property->type)
1236    {
1237        case BT_PROPERTY_BDNAME:
1238            {
1239                name_len = property->len > BTM_MAX_LOC_BD_NAME_LEN ? BTM_MAX_LOC_BD_NAME_LEN:
1240                                                                     property->len;
1241                memcpy(bd_name,property->val, name_len);
1242                bd_name[name_len] = '\0';
1243
1244                BTIF_TRACE_EVENT("set property name : %s", (char *)bd_name);
1245
1246                BTA_DmSetDeviceName((char *)bd_name);
1247
1248                storage_req_id = BTIF_CORE_STORAGE_ADAPTER_WRITE;
1249            }
1250            break;
1251
1252        case BT_PROPERTY_ADAPTER_SCAN_MODE:
1253            {
1254                bt_scan_mode_t mode = *(bt_scan_mode_t*)property->val;
1255                tBTA_DM_DISC disc_mode;
1256                tBTA_DM_CONN conn_mode;
1257
1258                switch(mode)
1259                {
1260                    case BT_SCAN_MODE_NONE:
1261                        disc_mode = BTA_DM_NON_DISC;
1262                        conn_mode = BTA_DM_NON_CONN;
1263                        break;
1264
1265                    case BT_SCAN_MODE_CONNECTABLE:
1266                        disc_mode = BTA_DM_NON_DISC;
1267                        conn_mode = BTA_DM_CONN;
1268                        break;
1269
1270                    case BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE:
1271                        disc_mode = BTA_DM_GENERAL_DISC;
1272                        conn_mode = BTA_DM_CONN;
1273                        break;
1274
1275                    default:
1276                        BTIF_TRACE_ERROR("invalid scan mode (0x%x)", mode);
1277                        return BT_STATUS_PARM_INVALID;
1278                }
1279
1280                BTIF_TRACE_EVENT("set property scan mode : %x", mode);
1281
1282                BTA_DmSetVisibility(disc_mode, conn_mode, BTA_DM_IGNORE, BTA_DM_IGNORE);
1283
1284                storage_req_id = BTIF_CORE_STORAGE_ADAPTER_WRITE;
1285            }
1286            break;
1287        case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
1288            {
1289                /* Nothing to do beside store the value in NV.  Java
1290                   will change the SCAN_MODE property after setting timeout,
1291                   if required */
1292                storage_req_id = BTIF_CORE_STORAGE_ADAPTER_WRITE;
1293            }
1294            break;
1295        case BT_PROPERTY_BDADDR:
1296        case BT_PROPERTY_UUIDS:
1297        case BT_PROPERTY_ADAPTER_BONDED_DEVICES:
1298        case BT_PROPERTY_REMOTE_FRIENDLY_NAME:
1299            /* no write support through HAL, these properties are only populated from BTA events */
1300            status = BT_STATUS_FAIL;
1301            break;
1302        default:
1303            BTIF_TRACE_ERROR("btif_get_adapter_property : invalid type %d",
1304            property->type);
1305            status = BT_STATUS_FAIL;
1306            break;
1307    }
1308
1309    if (storage_req_id != BTIF_CORE_STORAGE_NO_ACTION)
1310    {
1311        int btif_status;
1312        /* pass on to storage for updating local database */
1313
1314        memset(&(req.write_req.bd_addr), 0, sizeof(bt_bdaddr_t));
1315        memcpy(&(req.write_req.prop), property, sizeof(bt_property_t));
1316
1317        return btif_transfer_context(execute_storage_request,
1318                                     storage_req_id,
1319                                     (char*)&req,
1320                                     sizeof(btif_storage_req_t)+property->len,
1321                                     btif_in_storage_request_copy_cb);
1322    }
1323
1324    return status;
1325
1326}
1327
1328/*******************************************************************************
1329**
1330** Function         btif_get_remote_device_property
1331**
1332** Description      Fetches the remote device property from the NVRAM
1333**
1334** Returns          bt_status_t
1335**
1336*******************************************************************************/
1337bt_status_t btif_get_remote_device_property(bt_bdaddr_t *remote_addr,
1338                                                 bt_property_type_t type)
1339{
1340    btif_storage_req_t req;
1341
1342    if (!btif_is_enabled())
1343        return BT_STATUS_NOT_READY;
1344
1345    memcpy(&(req.read_req.bd_addr), remote_addr, sizeof(bt_bdaddr_t));
1346    req.read_req.type = type;
1347    return btif_transfer_context(execute_storage_remote_request,
1348                                 BTIF_CORE_STORAGE_REMOTE_READ,
1349                                 (char*)&req, sizeof(btif_storage_req_t),
1350                                 NULL);
1351}
1352
1353/*******************************************************************************
1354**
1355** Function         btif_get_remote_device_properties
1356**
1357** Description      Fetches all the remote device properties from NVRAM
1358**
1359** Returns          bt_status_t
1360**
1361*******************************************************************************/
1362bt_status_t btif_get_remote_device_properties(bt_bdaddr_t *remote_addr)
1363{
1364    btif_storage_req_t req;
1365
1366    if (!btif_is_enabled())
1367        return BT_STATUS_NOT_READY;
1368
1369    memcpy(&(req.read_req.bd_addr), remote_addr, sizeof(bt_bdaddr_t));
1370    return btif_transfer_context(execute_storage_remote_request,
1371                                 BTIF_CORE_STORAGE_REMOTE_READ_ALL,
1372                                 (char*)&req, sizeof(btif_storage_req_t),
1373                                 NULL);
1374}
1375
1376/*******************************************************************************
1377**
1378** Function         btif_set_remote_device_property
1379**
1380** Description      Writes the remote device property to NVRAM.
1381**                  Currently, BT_PROPERTY_REMOTE_FRIENDLY_NAME is the only
1382**                  remote device property that can be set
1383**
1384** Returns          bt_status_t
1385**
1386*******************************************************************************/
1387bt_status_t btif_set_remote_device_property(bt_bdaddr_t *remote_addr,
1388                                                 const bt_property_t *property)
1389{
1390    btif_storage_req_t req;
1391
1392    if (!btif_is_enabled())
1393        return BT_STATUS_NOT_READY;
1394
1395    memcpy(&(req.write_req.bd_addr), remote_addr, sizeof(bt_bdaddr_t));
1396    memcpy(&(req.write_req.prop), property, sizeof(bt_property_t));
1397
1398    return btif_transfer_context(execute_storage_remote_request,
1399                                 BTIF_CORE_STORAGE_REMOTE_WRITE,
1400                                 (char*)&req,
1401                                 sizeof(btif_storage_req_t)+property->len,
1402                                 btif_in_storage_request_copy_cb);
1403}
1404
1405
1406/*******************************************************************************
1407**
1408** Function         btif_get_remote_service_record
1409**
1410** Description      Looks up the service matching uuid on the remote device
1411**                  and fetches the SCN and service_name if the UUID is found
1412**
1413** Returns          bt_status_t
1414**
1415*******************************************************************************/
1416bt_status_t btif_get_remote_service_record(bt_bdaddr_t *remote_addr,
1417                                               bt_uuid_t *uuid)
1418{
1419    if (!btif_is_enabled())
1420        return BT_STATUS_NOT_READY;
1421
1422    return btif_dm_get_remote_service_record(remote_addr, uuid);
1423}
1424
1425
1426/*******************************************************************************
1427**
1428** Function         btif_get_enabled_services_mask
1429**
1430** Description      Fetches currently enabled services
1431**
1432** Returns          tBTA_SERVICE_MASK
1433**
1434*******************************************************************************/
1435
1436tBTA_SERVICE_MASK btif_get_enabled_services_mask(void)
1437{
1438    return btif_enabled_services;
1439}
1440
1441/*******************************************************************************
1442**
1443** Function         btif_enable_service
1444**
1445** Description      Enables the service 'service_ID' to the service_mask.
1446**                  Upon BT enable, BTIF core shall invoke the BTA APIs to
1447**                  enable the profiles
1448**
1449** Returns          bt_status_t
1450**
1451*******************************************************************************/
1452bt_status_t btif_enable_service(tBTA_SERVICE_ID service_id)
1453{
1454    tBTA_SERVICE_ID *p_id = &service_id;
1455
1456    /* If BT is enabled, we need to switch to BTIF context and trigger the
1457     * enable for that profile
1458     *
1459     * Otherwise, we just set the flag. On BT_Enable, the DM will trigger
1460     * enable for the profiles that have been enabled */
1461
1462    btif_enabled_services |= (1 << service_id);
1463
1464    BTIF_TRACE_DEBUG("%s: current services:0x%x", __FUNCTION__, btif_enabled_services);
1465
1466    if (btif_is_enabled())
1467    {
1468        btif_transfer_context(btif_dm_execute_service_request,
1469                              BTIF_DM_ENABLE_SERVICE,
1470                              (char*)p_id, sizeof(tBTA_SERVICE_ID), NULL);
1471    }
1472
1473    return BT_STATUS_SUCCESS;
1474}
1475/*******************************************************************************
1476**
1477** Function         btif_disable_service
1478**
1479** Description      Disables the service 'service_ID' to the service_mask.
1480**                  Upon BT disable, BTIF core shall invoke the BTA APIs to
1481**                  disable the profiles
1482**
1483** Returns          bt_status_t
1484**
1485*******************************************************************************/
1486bt_status_t btif_disable_service(tBTA_SERVICE_ID service_id)
1487{
1488    tBTA_SERVICE_ID *p_id = &service_id;
1489
1490    /* If BT is enabled, we need to switch to BTIF context and trigger the
1491     * disable for that profile so that the appropriate uuid_property_changed will
1492     * be triggerred. Otherwise, we just need to clear the service_id in the mask
1493     */
1494
1495    btif_enabled_services &=  (tBTA_SERVICE_MASK)(~(1<<service_id));
1496
1497    BTIF_TRACE_DEBUG("%s: Current Services:0x%x", __FUNCTION__, btif_enabled_services);
1498
1499    if (btif_is_enabled())
1500    {
1501        btif_transfer_context(btif_dm_execute_service_request,
1502                              BTIF_DM_DISABLE_SERVICE,
1503                              (char*)p_id, sizeof(tBTA_SERVICE_ID), NULL);
1504    }
1505
1506    return BT_STATUS_SUCCESS;
1507}
1508
1509/*******************************************************************************
1510**
1511** Function         btif_config_hci_snoop_log
1512**
1513** Description      enable or disable HCI snoop log
1514**
1515** Returns          bt_status_t
1516**
1517*******************************************************************************/
1518bt_status_t btif_config_hci_snoop_log(uint8_t enable)
1519{
1520    bte_main_config_hci_logging(enable != 0,
1521             btif_core_state == BTIF_CORE_STATE_DISABLED);
1522    return BT_STATUS_SUCCESS;
1523}
1524