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