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