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