Fix the logic for removing bonded devices

* Removed btif_storage_is_device_bonded(), because it is not needed,
  and it was giving the wrong answer in use cases like Smart Setup
  with BR/EDR connections.

* Added a call to btif_storage_remove_ble_bonding_keys()
  within btif_storage_remove_bonded_device() so the bonded device
  state is properly removed.

* Don't save the BLE bonding keys if it is temporary bonding

Bug: 22233299
Change-Id: I33d9f76a124acc60173f0acaa517bc29ee6603e8
diff --git a/btif/include/btif_storage.h b/btif/include/btif_storage.h
index b650cae..54dc5d6 100644
--- a/btif/include/btif_storage.h
+++ b/btif/include/btif_storage.h
@@ -133,18 +133,6 @@
 *******************************************************************************/
 bt_status_t btif_storage_remove_bonded_device(bt_bdaddr_t *remote_bd_addr);
 
-/******************************************************************************
-**
-** Function         btif_storage_is_device_bonded
-**
-** Description      BTIF storage API - checks if device present in bonded list
-**
-** Returns          TRUE if the device is bonded,
-**                  FALSE otherwise
-**
-*******************************************************************************/
-BOOLEAN btif_storage_is_device_bonded(bt_bdaddr_t *remote_bd_addr);
-
 /*******************************************************************************
 **
 ** Function         btif_storage_remove_bonded_device
diff --git a/btif/src/btif_dm.c b/btif/src/btif_dm.c
index b7ceb87..1f3893e 100644
--- a/btif/src/btif_dm.c
+++ b/btif/src/btif_dm.c
@@ -54,6 +54,7 @@
 #include "include/stack_config.h"
 #include "osi/include/log.h"
 #include "osi/include/allocator.h"
+#include "stack/btm/btm_int.h"
 
 /******************************************************************************
 **  Constants & Macros
@@ -92,10 +93,6 @@
 
 #define MAX_SDP_BL_ENTRIES 3
 
-#define BOND_TYPE_UNKNOWN     0
-#define BOND_TYPE_PERSISTENT  1
-#define BOND_TYPE_TEMPORARY   2
-
 #define ENCRYPTED_BREDR       2
 #define ENCRYPTED_LE          4
 
@@ -104,7 +101,7 @@
     bt_bond_state_t state;
     bt_bdaddr_t static_bdaddr;
     BD_ADDR bd_addr;
-    UINT8 bond_type;
+    tBTM_BOND_TYPE bond_type;
     UINT8 pin_code_len;
     UINT8 is_ssp;
     UINT8 auth_req;
@@ -967,6 +964,8 @@
     else
         pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
 
+    btm_set_bond_type_dev(p_ssp_cfm_req->bd_addr, pairing_cb.bond_type);
+
     pairing_cb.is_ssp = TRUE;
 
     /* If JustWorks auto-accept */
@@ -1083,9 +1082,7 @@
             {
                 BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing",
                         __FUNCTION__);
-                if (btif_storage_is_device_bonded(&bd_addr) == TRUE) {
-                    btif_storage_remove_bonded_device(&bd_addr);
-                }
+                btif_storage_remove_bonded_device(&bd_addr);
                 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
                 return;
             }
@@ -1702,6 +1699,7 @@
             if (pairing_cb.state == BT_BOND_STATE_BONDING)
             {
                 bdcpy(bd_addr.address, pairing_cb.bd_addr);
+                btm_set_bond_type_dev(pairing_cb.bd_addr, BOND_TYPE_UNKNOWN);
                 bond_state_changed(p_data->bond_cancel_cmpl.result, &bd_addr, BT_BOND_STATE_NONE);
             }
             break;
@@ -1715,14 +1713,12 @@
 
         case BTA_DM_DEV_UNPAIRED_EVT:
             bdcpy(bd_addr.address, p_data->link_down.bd_addr);
+            btm_set_bond_type_dev(p_data->link_down.bd_addr, BOND_TYPE_UNKNOWN);
 
             /*special handling for HID devices */
             #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
             btif_hh_remove_device(bd_addr);
             #endif
-            #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
-            btif_storage_remove_ble_bonding_keys(&bd_addr);
-            #endif
             btif_storage_remove_bonded_device(&bd_addr);
             bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
             break;
@@ -1763,6 +1759,7 @@
 
         case BTA_DM_LINK_DOWN_EVT:
             bdcpy(bd_addr.address, p_data->link_down.bd_addr);
+            btm_set_bond_type_dev(p_data->link_down.bd_addr, BOND_TYPE_UNKNOWN);
             BTIF_TRACE_DEBUG("BTA_DM_LINK_DOWN_EVT. Sending BT_ACL_STATE_DISCONNECTED");
             HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
                       &bd_addr, BT_ACL_STATE_DISCONNECTED);
@@ -2871,7 +2868,16 @@
         bdcpy(bdaddr.address, p_auth_cmpl->bd_addr);
         if (btif_storage_get_remote_addr_type(&bdaddr, &addr_type) != BT_STATUS_SUCCESS)
             btif_storage_set_remote_addr_type(&bdaddr, p_auth_cmpl->addr_type);
-        btif_dm_save_ble_bonding_keys();
+
+        /* Test for temporary bonding */
+        if (btm_get_bond_type_dev(p_auth_cmpl->bd_addr) == BOND_TYPE_TEMPORARY) {
+            BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing",
+                             __func__);
+            btif_storage_remove_bonded_device(&bdaddr);
+            state = BT_BOND_STATE_NONE;
+        } else {
+            btif_dm_save_ble_bonding_keys();
+        }
         BTA_GATTC_Refresh(bd_addr.address);
         btif_dm_get_remote_services(&bd_addr);
     }
@@ -3055,6 +3061,7 @@
     pairing_cb.is_le_only = TRUE;
     pairing_cb.is_le_nc = FALSE;
     pairing_cb.is_ssp = TRUE;
+    btm_set_bond_type_dev(p_ble_req->bd_addr, pairing_cb.bond_type);
 
     cod = COD_UNCLASSIFIED;
 
diff --git a/btif/src/btif_storage.c b/btif/src/btif_storage.c
index a00391c..687af94 100644
--- a/btif/src/btif_storage.c
+++ b/btif/src/btif_storage.c
@@ -591,23 +591,6 @@
  * the property->type.
  *******************************************************************************/
 
-/*****************************************************************************
-**
-** Function         btif_storage_is_device_bonded
-**
-** Description      BTIF storage API - checks if device present in bonded list
-**
-** Returns          TRUE if the device is bonded,
-**                  FALSE otherwise
-**
-*******************************************************************************/
-BOOLEAN btif_storage_is_device_bonded(bt_bdaddr_t *remote_bd_addr)
-{
-    bdstr_t bdstr;
-    bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
-    return (btif_in_fetch_bonded_device(bdstr) == BT_STATUS_SUCCESS);
-}
-
 /*******************************************************************************
 **
 ** Function         btif_storage_get_adapter_property
@@ -843,6 +826,11 @@
     bdstr_t bdstr;
     bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
     BTIF_TRACE_DEBUG("in bd addr:%s", bdstr);
+
+#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
+    btif_storage_remove_ble_bonding_keys(remote_bd_addr);
+#endif
+
     int ret = 1;
     if(btif_config_exist(bdstr, "LinkKeyType"))
         ret &= btif_config_remove(bdstr, "LinkKeyType");
diff --git a/stack/btm/btm_dev.c b/stack/btm/btm_dev.c
index c8265e4..0189bda 100644
--- a/stack/btm/btm_dev.c
+++ b/stack/btm/btm_dev.c
@@ -98,6 +98,7 @@
             return(FALSE);
     }
 
+    p_dev_rec->bond_type = BOND_TYPE_UNKNOWN;           /* Default value */
     p_dev_rec->timestamp = btm_cb.dev_rec_count++;
 
     if (dev_class)
@@ -288,6 +289,7 @@
 
     }
 
+    p_dev_rec->bond_type = BOND_TYPE_UNKNOWN;           /* Default value */
     p_dev_rec->sec_flags = BTM_SEC_IN_USE;
 
     /* Check with the BT manager if details about remote device are known */
@@ -336,6 +338,7 @@
 *******************************************************************************/
 void btm_sec_free_dev (tBTM_SEC_DEV_REC *p_dev_rec)
 {
+    p_dev_rec->bond_type = BOND_TYPE_UNKNOWN;
     p_dev_rec->sec_flags = 0;
 
 #if BLE_INCLUDED == TRUE
@@ -499,8 +502,10 @@
 
                 p_target_rec->new_encryption_key_is_p256 = temp_rec.new_encryption_key_is_p256;
                 p_target_rec->no_smp_on_br = temp_rec.no_smp_on_br;
+                p_target_rec->bond_type = temp_rec.bond_type;
                 /* mark the combined record as unused */
                 p_dev_rec->sec_flags &= ~BTM_SEC_IN_USE;
+                p_dev_rec->bond_type = BOND_TYPE_UNKNOWN;
                 break;
             }
 
@@ -512,6 +517,7 @@
                     p_target_rec->ble.ble_addr_type = p_dev_rec->ble.ble_addr_type;
                     p_target_rec->device_type |= p_dev_rec->device_type;
                     p_dev_rec->sec_flags &= ~BTM_SEC_IN_USE;
+                    p_dev_rec->bond_type = BOND_TYPE_UNKNOWN;
                 }
                 break;
             }
@@ -594,4 +600,43 @@
     return(p_oldest);
 }
 
+/*******************************************************************************
+**
+** Function         btm_get_bond_type_dev
+**
+** Description      Get the bond type for a device in the device database
+**                  with specified BD address
+**
+** Returns          The device bond type if known, otherwise BOND_TYPE_UNKNOWN
+**
+*******************************************************************************/
+tBTM_BOND_TYPE btm_get_bond_type_dev(BD_ADDR bd_addr)
+{
+    tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev(bd_addr);
 
+    if (p_dev_rec == NULL)
+        return BOND_TYPE_UNKNOWN;
+
+    return p_dev_rec->bond_type;
+}
+
+/*******************************************************************************
+**
+** Function         btm_set_bond_type_dev
+**
+** Description      Set the bond type for a device in the device database
+**                  with specified BD address
+**
+** Returns          TRUE on success, otherwise FALSE
+**
+*******************************************************************************/
+BOOLEAN btm_set_bond_type_dev(BD_ADDR bd_addr, tBTM_BOND_TYPE bond_type)
+{
+    tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev(bd_addr);
+
+    if (p_dev_rec == NULL)
+        return FALSE;
+
+    p_dev_rec->bond_type = bond_type;
+    return TRUE;
+}
diff --git a/stack/btm/btm_int.h b/stack/btm/btm_int.h
index 0cc1773..65f0895 100644
--- a/stack/btm/btm_int.h
+++ b/stack/btm/btm_int.h
@@ -492,6 +492,15 @@
 
 #endif  /* BLE_INCLUDED */
 
+/* Peering bond type */
+enum
+{
+    BOND_TYPE_UNKNOWN,
+    BOND_TYPE_PERSISTENT,
+    BOND_TYPE_TEMPORARY
+};
+typedef UINT8 tBTM_BOND_TYPE;
+
 /*
 ** Define structure for Security Device Record.
 ** A record exists for each device authenticated with this device
@@ -586,6 +595,7 @@
     BOOLEAN no_smp_on_br;       /* if set to TRUE then SMP on BR/EDR doesn't */
                                 /* work, i.e. link keys crosspairing */
                                 /* SC BR/EDR->SC LE doesn't happen */
+    tBTM_BOND_TYPE bond_type;   /* peering bond type */
 
 #if BLE_INCLUDED == TRUE
     tBTM_SEC_BLE        ble;
@@ -1034,6 +1044,9 @@
 extern tBTM_SEC_DEV_REC  *btm_find_dev (BD_ADDR bd_addr);
 extern tBTM_SEC_DEV_REC  *btm_find_or_alloc_dev (BD_ADDR bd_addr);
 extern tBTM_SEC_DEV_REC  *btm_find_dev_by_handle (UINT16 handle);
+extern tBTM_BOND_TYPE     btm_get_bond_type_dev(BD_ADDR bd_addr);
+extern BOOLEAN            btm_set_bond_type_dev(BD_ADDR bd_addr,
+                                                tBTM_BOND_TYPE bond_type);
 
 /* Internal functions provided by btm_sec.c
 **********************************************