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