OSDN Git Service

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