Add guest mode functionality (2/3)

Add a flag to enable() to start Bluetooth in restricted
mode. In restricted mode, all devices that are paired during
restricted mode are deleted upon leaving restricted mode.
Right now restricted mode is only entered while a guest
user is active.

Bug: 27410683
Change-Id: I8f23d28ef0aa3a8df13d469c73005c8e1b894d19
diff --git a/btif/include/btif_api.h b/btif/include/btif_api.h
index 2e41d22..0861e54 100644
--- a/btif/include/btif_api.h
+++ b/btif/include/btif_api.h
@@ -87,6 +87,23 @@
 
 /*******************************************************************************
 **
+** Function         is_restricted_mode
+**
+** Description      Checks if BT was enabled in restriced mode. In restricted
+**                  mode, bonds that are created are marked as temporary.
+**                  These bonds persist until we leave restricted mode, at
+**                  which point they will be deleted from the config. Also
+**                  while in restricted mode, the user can access devices
+**                  that are already paired before entering restricted mode,
+**                  but they cannot remove any of these devices.
+**
+** Returns          bool
+**
+*******************************************************************************/
+bool is_restricted_mode(void);
+
+/*******************************************************************************
+**
 ** Function         btif_get_adapter_properties
 **
 ** Description      Fetches all local adapter properties
diff --git a/btif/include/btif_storage.h b/btif/include/btif_storage.h
index f64c695..a7ba84e 100644
--- a/btif/include/btif_storage.h
+++ b/btif/include/btif_storage.h
@@ -314,6 +314,18 @@
 *******************************************************************************/
 BOOLEAN btif_storage_is_fixed_pin_zeros_keyboard(bt_bdaddr_t *remote_bd_addr);
 
+/*******************************************************************************
+**
+** Function         btif_storage_is_retricted_device
+**
+** Description      BTIF storage API - checks if this device is a restricted device
+**
+** Returns          TRUE  if the device is labled as restricted
+**                  FALSE otherwise
+**
+*******************************************************************************/
+BOOLEAN btif_storage_is_restricted_device(const bt_bdaddr_t *remote_bd_addr);
+
 #if (BLE_INCLUDED == TRUE)
 bt_status_t btif_storage_add_ble_bonding_key( bt_bdaddr_t *remote_bd_addr,
                                               char *key,
diff --git a/btif/src/bluetooth.c b/btif/src/bluetooth.c
index 5c730f9..a1c3498 100644
--- a/btif/src/bluetooth.c
+++ b/btif/src/bluetooth.c
@@ -45,6 +45,7 @@
 
 #include "btif_api.h"
 #include "bt_utils.h"
+#include "btif_storage.h"
 
 /************************************************************************************
 **  Constants & Macros
@@ -61,6 +62,7 @@
 ************************************************************************************/
 
 bt_callbacks_t *bt_hal_cbacks = NULL;
+bool restricted_mode = FALSE;
 
 /** Operating System specific callouts for resource management */
 bt_os_callouts_t *bt_os_callouts = NULL;
@@ -142,10 +144,11 @@
     return BT_STATUS_SUCCESS;
 }
 
-static int enable( void )
-{
+static int enable(bool start_restricted) {
     ALOGI("enable");
 
+    restricted_mode = start_restricted;
+
     /* sanity check */
     if (interface_ready() == FALSE)
         return BT_STATUS_NOT_READY;
@@ -175,6 +178,10 @@
     return;
 }
 
+bool is_restricted_mode() {
+  return restricted_mode;
+}
+
 static int get_adapter_properties(void)
 {
     /* sanity check */
@@ -285,6 +292,9 @@
 
 static int remove_bond(const bt_bdaddr_t *bd_addr)
 {
+    if (is_restricted_mode() && !btif_storage_is_restricted_device(bd_addr))
+        return BT_STATUS_SUCCESS;
+
     /* sanity check */
     if (interface_ready() == FALSE)
         return BT_STATUS_NOT_READY;
diff --git a/btif/src/btif_config.c b/btif/src/btif_config.c
index 629f4c6..69f94b5 100644
--- a/btif/src/btif_config.c
+++ b/btif/src/btif_config.c
@@ -105,6 +105,7 @@
 static int save_cfg();
 static void load_cfg();
 static short find_next_node(const cfg_node* p, short start, char* name, int* bytes);
+static void btif_config_remove_restricted();
 #ifdef UNIT_TEST
 static void cfg_test_load();
 static void cfg_test_write();
@@ -148,6 +149,9 @@
             //cfg_test_read();
             exit(0);
         #endif
+
+        if (!is_restricted_mode())
+          btif_config_remove_restricted();
     }
     return pth >= 0;
 }
@@ -989,3 +993,36 @@
 
 
 #endif
+
+static void btif_config_remove_restricted() {
+    int section_index = -1;
+    if ((section_index = find_inode(&root, "Remote")) < 0)
+        return;
+
+    cfg_node* remote_node = &root.child[section_index];
+    int count = GET_CHILD_COUNT(remote_node);
+    for (int i = 0; i < count; i ++) {
+        cfg_node* bdaddr_node = &remote_node->child[i];
+        if (find_inode(bdaddr_node, "Restricted") != -1) {
+            free_child(bdaddr_node, 0, GET_CHILD_COUNT(bdaddr_node));
+            free_child(remote_node, i, 1);
+        }
+    }
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/btif/src/btif_storage.c b/btif/src/btif_storage.c
index 5b1bf96..9427eb4 100644
--- a/btif/src/btif_storage.c
+++ b/btif/src/btif_storage.c
@@ -787,6 +787,13 @@
     int ret = btif_config_set_int("Remote", bdstr, "LinkKeyType", (int)key_type);
     ret &= btif_config_set_int("Remote", bdstr, "PinLength", (int)pin_length);
     ret &= btif_config_set("Remote", bdstr, "LinkKey", (const char*)link_key, sizeof(LINK_KEY), BTIF_CFG_TYPE_BIN);
+
+    if (is_restricted_mode()) {
+        BTIF_TRACE_WARNING("%s: '%s' pairing will be removed if unrestricted",
+                         __func__, bdstr);
+        btif_config_set_int("Remote", bdstr, "Restricted", 1);
+    }
+
     /* write bonded info immediately */
     btif_config_flush();
     return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
@@ -1828,3 +1835,20 @@
 }
 
 
+/*******************************************************************************
+**
+** Function         btif_storage_is_restricted_device
+**
+** Description      BTIF storage API - checks if this device is a restricted device
+**
+** Returns          TRUE  if the device is labeled as restricted
+**                  FALSE otherwise
+**
+*******************************************************************************/
+BOOLEAN btif_storage_is_restricted_device(const bt_bdaddr_t *remote_bd_addr)
+{
+    bdstr_t bdstr;
+    bd2str(remote_bd_addr, &bdstr);
+
+    return btif_config_exist("Remote", bdstr, "Restricted");
+}
diff --git a/test/bluedroidtest/bluedroidtest.c b/test/bluedroidtest/bluedroidtest.c
index ac727d8..022738a 100644
--- a/test/bluedroidtest/bluedroidtest.c
+++ b/test/bluedroidtest/bluedroidtest.c
@@ -560,7 +560,7 @@
         bdt_log("Bluetooth is already enabled");
         return;
     }
-    status = sBtInterface->enable();
+    status = sBtInterface->enable(false);
 
     check_return_status(status);
 }
diff --git a/test/suite/cases/adapter.c b/test/suite/cases/adapter.c
index 492d8f4..6664c3b 100644
--- a/test/suite/cases/adapter.c
+++ b/test/suite/cases/adapter.c
@@ -24,7 +24,7 @@
 bool adapter_enable_disable() {
   int error;
 
-  CALL_AND_WAIT(error = bt_interface->enable(), adapter_state_changed);
+  CALL_AND_WAIT(error = bt_interface->enable(false), adapter_state_changed);
   TASSERT(error == BT_STATUS_SUCCESS, "Error enabling Bluetooth: %d", error);
   TASSERT(adapter_get_state() == BT_STATE_ON, "Adapter did not turn on.");
 
diff --git a/test/suite/main.c b/test/suite/main.c
index 7a3af5b..f09d8b4 100644
--- a/test/suite/main.c
+++ b/test/suite/main.c
@@ -89,7 +89,7 @@
   // Run the full test suite.
   for (size_t i = 0; i < test_suite_size; ++i) {
     callbacks_init();
-    CALL_AND_WAIT(bt_interface->enable(), adapter_state_changed);
+    CALL_AND_WAIT(bt_interface->enable(false), adapter_state_changed);
     if (test_suite[i].function()) {
       printf("[%4d] %-64s [%sPASS%s]\n", ++case_num, test_suite[i].function_name, GREEN, GRAY);
       ++pass;