OSDN Git Service

am 2cfcf51e: Added check if the device address is random
[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     bt_status_t status = BT_STATUS_SUCCESS;
1005
1006     BTIF_TRACE_EVENT("execute storage request event : %d", event);
1007
1008     switch(event)
1009     {
1010         case BTIF_CORE_STORAGE_ADAPTER_WRITE:
1011         {
1012             btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
1013             bt_property_t *p_prop = &(p_req->write_req.prop);
1014             BTIF_TRACE_EVENT("type: %d, len %d, 0x%x", p_prop->type,
1015                                p_prop->len, p_prop->val);
1016
1017             status = btif_storage_set_adapter_property(p_prop);
1018             HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status, 1, p_prop);
1019         } break;
1020
1021         case BTIF_CORE_STORAGE_ADAPTER_READ:
1022         {
1023             btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
1024             char buf[512];
1025             bt_property_t prop;
1026             prop.type = p_req->read_req.type;
1027             prop.val = (void*)buf;
1028             prop.len = sizeof(buf);
1029             if (prop.type == BT_PROPERTY_LOCAL_LE_FEATURES)
1030             {
1031                 #if (BLE_INCLUDED == TRUE)
1032                 tBTM_BLE_VSC_CB cmn_vsc_cb;
1033                 bt_local_le_features_t local_le_features;
1034
1035                 /* LE features are not stored in storage. Should be retrived from stack */
1036                 BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
1037                 local_le_features.local_privacy_enabled = BTM_BleLocalPrivacyEnabled();
1038
1039                 prop.len = sizeof (bt_local_le_features_t);
1040                 if (cmn_vsc_cb.filter_support == 1)
1041                     local_le_features.max_adv_filter_supported = cmn_vsc_cb.max_filter;
1042                 else
1043                     local_le_features.max_adv_filter_supported = 0;
1044                 local_le_features.max_adv_instance = cmn_vsc_cb.adv_inst_max;
1045                 local_le_features.max_irk_list_size = cmn_vsc_cb.max_irk_list_sz;
1046                 local_le_features.rpa_offload_supported = cmn_vsc_cb.rpa_offloading;
1047                 local_le_features.scan_result_storage_size_hibyte =
1048                     (cmn_vsc_cb.tot_scan_results_strg >> 8) & (0xFF);
1049                 local_le_features.scan_result_storage_size_lobyte =
1050                     (cmn_vsc_cb.tot_scan_results_strg) & (0xFF);
1051                 local_le_features.activity_energy_info_supported = cmn_vsc_cb.energy_support;
1052                 memcpy(prop.val, &local_le_features, prop.len);
1053                 #endif
1054             }
1055             else
1056             {
1057                 status = btif_storage_get_adapter_property(&prop);
1058             }
1059             HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status, 1, &prop);
1060         } break;
1061
1062         case BTIF_CORE_STORAGE_ADAPTER_READ_ALL:
1063         {
1064             status = btif_in_get_adapter_properties();
1065         } break;
1066
1067         case BTIF_CORE_STORAGE_NOTIFY_STATUS:
1068         {
1069             HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, status, 0, NULL);
1070         } break;
1071
1072         default:
1073             BTIF_TRACE_ERROR("%s invalid event id (%d)", __FUNCTION__, event);
1074             break;
1075     }
1076 }
1077
1078 static void execute_storage_remote_request(UINT16 event, char *p_param)
1079 {
1080     bt_status_t status = BT_STATUS_FAIL;
1081     bt_property_t prop;
1082
1083     BTIF_TRACE_EVENT("execute storage remote request event : %d", event);
1084
1085     switch (event)
1086     {
1087         case BTIF_CORE_STORAGE_REMOTE_READ:
1088         {
1089             char buf[1024];
1090             btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
1091             prop.type = p_req->read_req.type;
1092             prop.val = (void*) buf;
1093             prop.len = sizeof(buf);
1094
1095             status = btif_storage_get_remote_device_property(&(p_req->read_req.bd_addr),
1096                                                              &prop);
1097             HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1098                             status, &(p_req->read_req.bd_addr), 1, &prop);
1099         }break;
1100         case BTIF_CORE_STORAGE_REMOTE_WRITE:
1101         {
1102            btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
1103            status = btif_storage_set_remote_device_property(&(p_req->write_req.bd_addr),
1104                                                             &(p_req->write_req.prop));
1105         }break;
1106         case BTIF_CORE_STORAGE_REMOTE_READ_ALL:
1107         {
1108            btif_storage_req_t *p_req = (btif_storage_req_t*)p_param;
1109            btif_in_get_remote_device_properties(&p_req->read_req.bd_addr);
1110         }break;
1111     }
1112 }
1113
1114 void btif_adapter_properties_evt(bt_status_t status, uint32_t num_props,
1115                                     bt_property_t *p_props)
1116 {
1117     HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
1118                      status, num_props, p_props);
1119
1120 }
1121 void btif_remote_properties_evt(bt_status_t status, bt_bdaddr_t *remote_addr,
1122                                    uint32_t num_props, bt_property_t *p_props)
1123 {
1124     HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1125                      status, remote_addr, num_props, p_props);
1126 }
1127
1128 /*******************************************************************************
1129 **
1130 ** Function         btif_in_storage_request_copy_cb
1131 **
1132 ** Description     Switch context callback function to perform the deep copy for
1133 **                 both the adapter and remote_device property API
1134 **
1135 ** Returns          None
1136 **
1137 *******************************************************************************/
1138 static void btif_in_storage_request_copy_cb(UINT16 event,
1139                                                  char *p_new_buf, char *p_old_buf)
1140 {
1141      btif_storage_req_t *new_req = (btif_storage_req_t*)p_new_buf;
1142      btif_storage_req_t *old_req = (btif_storage_req_t*)p_old_buf;
1143
1144      BTIF_TRACE_EVENT("%s", __FUNCTION__);
1145      switch (event)
1146      {
1147          case BTIF_CORE_STORAGE_REMOTE_WRITE:
1148          case BTIF_CORE_STORAGE_ADAPTER_WRITE:
1149          {
1150              bdcpy(new_req->write_req.bd_addr.address, old_req->write_req.bd_addr.address);
1151              /* Copy the member variables one at a time */
1152              new_req->write_req.prop.type = old_req->write_req.prop.type;
1153              new_req->write_req.prop.len = old_req->write_req.prop.len;
1154
1155              new_req->write_req.prop.val = (UINT8 *)(p_new_buf + sizeof(btif_storage_req_t));
1156              memcpy(new_req->write_req.prop.val, old_req->write_req.prop.val,
1157                     old_req->write_req.prop.len);
1158          }break;
1159      }
1160 }
1161
1162 /*******************************************************************************
1163 **
1164 ** Function         btif_get_adapter_properties
1165 **
1166 ** Description      Fetch all available properties (local & remote)
1167 **
1168 ** Returns          bt_status_t
1169 **
1170 *******************************************************************************/
1171
1172 bt_status_t btif_get_adapter_properties(void)
1173 {
1174     BTIF_TRACE_EVENT("%s", __FUNCTION__);
1175
1176     if (!btif_is_enabled())
1177         return BT_STATUS_NOT_READY;
1178
1179     return btif_transfer_context(execute_storage_request,
1180                                  BTIF_CORE_STORAGE_ADAPTER_READ_ALL,
1181                                  NULL, 0, NULL);
1182 }
1183
1184 /*******************************************************************************
1185 **
1186 ** Function         btif_get_adapter_property
1187 **
1188 ** Description      Fetches property value from local cache
1189 **
1190 ** Returns          bt_status_t
1191 **
1192 *******************************************************************************/
1193
1194 bt_status_t btif_get_adapter_property(bt_property_type_t type)
1195 {
1196     btif_storage_req_t req;
1197
1198     BTIF_TRACE_EVENT("%s %d", __FUNCTION__, type);
1199
1200     /* Allow get_adapter_property only for BDADDR and BDNAME if BT is disabled */
1201     if (!btif_is_enabled() && (type != BT_PROPERTY_BDADDR) && (type != BT_PROPERTY_BDNAME))
1202         return BT_STATUS_NOT_READY;
1203
1204     memset(&(req.read_req.bd_addr), 0, sizeof(bt_bdaddr_t));
1205     req.read_req.type = type;
1206
1207     return btif_transfer_context(execute_storage_request,
1208                                  BTIF_CORE_STORAGE_ADAPTER_READ,
1209                                 (char*)&req, sizeof(btif_storage_req_t), NULL);
1210 }
1211
1212 /*******************************************************************************
1213 **
1214 ** Function         btif_set_adapter_property
1215 **
1216 ** Description      Updates core stack with property value and stores it in
1217 **                  local cache
1218 **
1219 ** Returns          bt_status_t
1220 **
1221 *******************************************************************************/
1222
1223 bt_status_t btif_set_adapter_property(const bt_property_t *property)
1224 {
1225     btif_storage_req_t req;
1226     bt_status_t status = BT_STATUS_SUCCESS;
1227     int storage_req_id = BTIF_CORE_STORAGE_NOTIFY_STATUS; /* default */
1228     char bd_name[BTM_MAX_LOC_BD_NAME_LEN +1];
1229     UINT16  name_len = 0;
1230
1231     BTIF_TRACE_EVENT("btif_set_adapter_property type: %d, len %d, 0x%x",
1232                       property->type, property->len, property->val);
1233
1234     if (!btif_is_enabled())
1235         return BT_STATUS_NOT_READY;
1236
1237     switch(property->type)
1238     {
1239         case BT_PROPERTY_BDNAME:
1240             {
1241                 name_len = property->len > BTM_MAX_LOC_BD_NAME_LEN ? BTM_MAX_LOC_BD_NAME_LEN:
1242                                                                      property->len;
1243                 memcpy(bd_name,property->val, name_len);
1244                 bd_name[name_len] = '\0';
1245
1246                 BTIF_TRACE_EVENT("set property name : %s", (char *)bd_name);
1247
1248                 BTA_DmSetDeviceName((char *)bd_name);
1249
1250                 storage_req_id = BTIF_CORE_STORAGE_ADAPTER_WRITE;
1251             }
1252             break;
1253
1254         case BT_PROPERTY_ADAPTER_SCAN_MODE:
1255             {
1256                 bt_scan_mode_t mode = *(bt_scan_mode_t*)property->val;
1257                 tBTA_DM_DISC disc_mode;
1258                 tBTA_DM_CONN conn_mode;
1259
1260                 switch(mode)
1261                 {
1262                     case BT_SCAN_MODE_NONE:
1263                         disc_mode = BTA_DM_NON_DISC;
1264                         conn_mode = BTA_DM_NON_CONN;
1265                         break;
1266
1267                     case BT_SCAN_MODE_CONNECTABLE:
1268                         disc_mode = BTA_DM_NON_DISC;
1269                         conn_mode = BTA_DM_CONN;
1270                         break;
1271
1272                     case BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE:
1273                         disc_mode = BTA_DM_GENERAL_DISC;
1274                         conn_mode = BTA_DM_CONN;
1275                         break;
1276
1277                     default:
1278                         BTIF_TRACE_ERROR("invalid scan mode (0x%x)", mode);
1279                         return BT_STATUS_PARM_INVALID;
1280                 }
1281
1282                 BTIF_TRACE_EVENT("set property scan mode : %x", mode);
1283
1284                 BTA_DmSetVisibility(disc_mode, conn_mode, BTA_DM_IGNORE, BTA_DM_IGNORE);
1285
1286                 storage_req_id = BTIF_CORE_STORAGE_ADAPTER_WRITE;
1287             }
1288             break;
1289         case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
1290             {
1291                 /* Nothing to do beside store the value in NV.  Java
1292                    will change the SCAN_MODE property after setting timeout,
1293                    if required */
1294                 storage_req_id = BTIF_CORE_STORAGE_ADAPTER_WRITE;
1295             }
1296             break;
1297         case BT_PROPERTY_BDADDR:
1298         case BT_PROPERTY_UUIDS:
1299         case BT_PROPERTY_ADAPTER_BONDED_DEVICES:
1300         case BT_PROPERTY_REMOTE_FRIENDLY_NAME:
1301             /* no write support through HAL, these properties are only populated from BTA events */
1302             status = BT_STATUS_FAIL;
1303             break;
1304         default:
1305             BTIF_TRACE_ERROR("btif_get_adapter_property : invalid type %d",
1306             property->type);
1307             status = BT_STATUS_FAIL;
1308             break;
1309     }
1310
1311     if (storage_req_id != BTIF_CORE_STORAGE_NO_ACTION)
1312     {
1313         /* pass on to storage for updating local database */
1314
1315         memset(&(req.write_req.bd_addr), 0, sizeof(bt_bdaddr_t));
1316         memcpy(&(req.write_req.prop), property, sizeof(bt_property_t));
1317
1318         return btif_transfer_context(execute_storage_request,
1319                                      storage_req_id,
1320                                      (char*)&req,
1321                                      sizeof(btif_storage_req_t)+property->len,
1322                                      btif_in_storage_request_copy_cb);
1323     }
1324
1325     return status;
1326
1327 }
1328
1329 /*******************************************************************************
1330 **
1331 ** Function         btif_get_remote_device_property
1332 **
1333 ** Description      Fetches the remote device property from the NVRAM
1334 **
1335 ** Returns          bt_status_t
1336 **
1337 *******************************************************************************/
1338 bt_status_t btif_get_remote_device_property(bt_bdaddr_t *remote_addr,
1339                                                  bt_property_type_t type)
1340 {
1341     btif_storage_req_t req;
1342
1343     if (!btif_is_enabled())
1344         return BT_STATUS_NOT_READY;
1345
1346     memcpy(&(req.read_req.bd_addr), remote_addr, sizeof(bt_bdaddr_t));
1347     req.read_req.type = type;
1348     return btif_transfer_context(execute_storage_remote_request,
1349                                  BTIF_CORE_STORAGE_REMOTE_READ,
1350                                  (char*)&req, sizeof(btif_storage_req_t),
1351                                  NULL);
1352 }
1353
1354 /*******************************************************************************
1355 **
1356 ** Function         btif_get_remote_device_properties
1357 **
1358 ** Description      Fetches all the remote device properties from NVRAM
1359 **
1360 ** Returns          bt_status_t
1361 **
1362 *******************************************************************************/
1363 bt_status_t btif_get_remote_device_properties(bt_bdaddr_t *remote_addr)
1364 {
1365     btif_storage_req_t req;
1366
1367     if (!btif_is_enabled())
1368         return BT_STATUS_NOT_READY;
1369
1370     memcpy(&(req.read_req.bd_addr), remote_addr, sizeof(bt_bdaddr_t));
1371     return btif_transfer_context(execute_storage_remote_request,
1372                                  BTIF_CORE_STORAGE_REMOTE_READ_ALL,
1373                                  (char*)&req, sizeof(btif_storage_req_t),
1374                                  NULL);
1375 }
1376
1377 /*******************************************************************************
1378 **
1379 ** Function         btif_set_remote_device_property
1380 **
1381 ** Description      Writes the remote device property to NVRAM.
1382 **                  Currently, BT_PROPERTY_REMOTE_FRIENDLY_NAME is the only
1383 **                  remote device property that can be set
1384 **
1385 ** Returns          bt_status_t
1386 **
1387 *******************************************************************************/
1388 bt_status_t btif_set_remote_device_property(bt_bdaddr_t *remote_addr,
1389                                                  const bt_property_t *property)
1390 {
1391     btif_storage_req_t req;
1392
1393     if (!btif_is_enabled())
1394         return BT_STATUS_NOT_READY;
1395
1396     memcpy(&(req.write_req.bd_addr), remote_addr, sizeof(bt_bdaddr_t));
1397     memcpy(&(req.write_req.prop), property, sizeof(bt_property_t));
1398
1399     return btif_transfer_context(execute_storage_remote_request,
1400                                  BTIF_CORE_STORAGE_REMOTE_WRITE,
1401                                  (char*)&req,
1402                                  sizeof(btif_storage_req_t)+property->len,
1403                                  btif_in_storage_request_copy_cb);
1404 }
1405
1406
1407 /*******************************************************************************
1408 **
1409 ** Function         btif_get_remote_service_record
1410 **
1411 ** Description      Looks up the service matching uuid on the remote device
1412 **                  and fetches the SCN and service_name if the UUID is found
1413 **
1414 ** Returns          bt_status_t
1415 **
1416 *******************************************************************************/
1417 bt_status_t btif_get_remote_service_record(bt_bdaddr_t *remote_addr,
1418                                                bt_uuid_t *uuid)
1419 {
1420     if (!btif_is_enabled())
1421         return BT_STATUS_NOT_READY;
1422
1423     return btif_dm_get_remote_service_record(remote_addr, uuid);
1424 }
1425
1426
1427 /*******************************************************************************
1428 **
1429 ** Function         btif_get_enabled_services_mask
1430 **
1431 ** Description      Fetches currently enabled services
1432 **
1433 ** Returns          tBTA_SERVICE_MASK
1434 **
1435 *******************************************************************************/
1436
1437 tBTA_SERVICE_MASK btif_get_enabled_services_mask(void)
1438 {
1439     return btif_enabled_services;
1440 }
1441
1442 /*******************************************************************************
1443 **
1444 ** Function         btif_enable_service
1445 **
1446 ** Description      Enables the service 'service_ID' to the service_mask.
1447 **                  Upon BT enable, BTIF core shall invoke the BTA APIs to
1448 **                  enable the profiles
1449 **
1450 ** Returns          bt_status_t
1451 **
1452 *******************************************************************************/
1453 bt_status_t btif_enable_service(tBTA_SERVICE_ID service_id)
1454 {
1455     tBTA_SERVICE_ID *p_id = &service_id;
1456
1457     /* If BT is enabled, we need to switch to BTIF context and trigger the
1458      * enable for that profile
1459      *
1460      * Otherwise, we just set the flag. On BT_Enable, the DM will trigger
1461      * enable for the profiles that have been enabled */
1462
1463     btif_enabled_services |= (1 << service_id);
1464
1465     BTIF_TRACE_DEBUG("%s: current services:0x%x", __FUNCTION__, btif_enabled_services);
1466
1467     if (btif_is_enabled())
1468     {
1469         btif_transfer_context(btif_dm_execute_service_request,
1470                               BTIF_DM_ENABLE_SERVICE,
1471                               (char*)p_id, sizeof(tBTA_SERVICE_ID), NULL);
1472     }
1473
1474     return BT_STATUS_SUCCESS;
1475 }
1476 /*******************************************************************************
1477 **
1478 ** Function         btif_disable_service
1479 **
1480 ** Description      Disables the service 'service_ID' to the service_mask.
1481 **                  Upon BT disable, BTIF core shall invoke the BTA APIs to
1482 **                  disable the profiles
1483 **
1484 ** Returns          bt_status_t
1485 **
1486 *******************************************************************************/
1487 bt_status_t btif_disable_service(tBTA_SERVICE_ID service_id)
1488 {
1489     tBTA_SERVICE_ID *p_id = &service_id;
1490
1491     /* If BT is enabled, we need to switch to BTIF context and trigger the
1492      * disable for that profile so that the appropriate uuid_property_changed will
1493      * be triggerred. Otherwise, we just need to clear the service_id in the mask
1494      */
1495
1496     btif_enabled_services &=  (tBTA_SERVICE_MASK)(~(1<<service_id));
1497
1498     BTIF_TRACE_DEBUG("%s: Current Services:0x%x", __FUNCTION__, btif_enabled_services);
1499
1500     if (btif_is_enabled())
1501     {
1502         btif_transfer_context(btif_dm_execute_service_request,
1503                               BTIF_DM_DISABLE_SERVICE,
1504                               (char*)p_id, sizeof(tBTA_SERVICE_ID), NULL);
1505     }
1506
1507     return BT_STATUS_SUCCESS;
1508 }
1509
1510 /*******************************************************************************
1511 **
1512 ** Function         btif_config_hci_snoop_log
1513 **
1514 ** Description      enable or disable HCI snoop log
1515 **
1516 ** Returns          bt_status_t
1517 **
1518 *******************************************************************************/
1519 bt_status_t btif_config_hci_snoop_log(uint8_t enable)
1520 {
1521     bte_main_config_hci_logging(enable != 0,
1522              btif_core_state == BTIF_CORE_STATE_DISABLED);
1523     return BT_STATUS_SUCCESS;
1524 }