Make autoconnect work properly for devices not having security record

Change-Id: I16f71da85afe780383a5d84a904b44aabe4af9f7
diff --git a/stack/btm/btm_ble_bgconn.c b/stack/btm/btm_ble_bgconn.c
index c413837..039f50c 100644
--- a/stack/btm/btm_ble_bgconn.c
+++ b/stack/btm/btm_ble_bgconn.c
@@ -154,45 +154,33 @@
     BD_ADDR             dummy_bda = {0};
     tBT_DEVICE_TYPE dev_type;
 
-    if (p_dev_rec != NULL &&
-        p_dev_rec->device_type & BT_DEVICE_TYPE_BLE)
-    {
-        if (to_add)
-        {
-            if (p_dev_rec->ble.ble_addr_type == BLE_ADDR_PUBLIC || !BTM_BLE_IS_RESOLVE_BDA(bd_addr))
-            {
-                started = btsnd_hcic_ble_add_white_list (p_dev_rec->ble.ble_addr_type, bd_addr);
+    if (p_dev_rec != NULL && p_dev_rec->device_type & BT_DEVICE_TYPE_BLE) {
+        if (to_add) {
+            if (p_dev_rec->ble.ble_addr_type == BLE_ADDR_PUBLIC || !BTM_BLE_IS_RESOLVE_BDA(bd_addr)) {
+                started = btsnd_hcic_ble_add_white_list(p_dev_rec->ble.ble_addr_type, bd_addr);
+                p_dev_rec->ble.in_controller_list |= BTM_WHITE_LIST_BIT;
+            } else if (memcmp(p_dev_rec->ble.static_addr, bd_addr, BD_ADDR_LEN) != 0 &&
+                memcmp(p_dev_rec->ble.static_addr, dummy_bda, BD_ADDR_LEN) != 0) {
+                started = btsnd_hcic_ble_add_white_list(p_dev_rec->ble.static_addr_type,
+                                                        p_dev_rec->ble.static_addr);
                 p_dev_rec->ble.in_controller_list |= BTM_WHITE_LIST_BIT;
             }
-            else if (memcmp(p_dev_rec->ble.static_addr, bd_addr, BD_ADDR_LEN) != 0 &&
-                memcmp(p_dev_rec->ble.static_addr, dummy_bda, BD_ADDR_LEN) != 0)
-            {
-                started = btsnd_hcic_ble_add_white_list (p_dev_rec->ble.static_addr_type,
-                                                         p_dev_rec->ble.static_addr);
-                p_dev_rec->ble.in_controller_list |= BTM_WHITE_LIST_BIT;
-            }
-        }
-        else
-        {
+        } else {
             if (p_dev_rec->ble.ble_addr_type == BLE_ADDR_PUBLIC || !BTM_BLE_IS_RESOLVE_BDA(bd_addr))
-            {
-                started = btsnd_hcic_ble_remove_from_white_list (p_dev_rec->ble.ble_addr_type, bd_addr);
-            }
+                started = btsnd_hcic_ble_remove_from_white_list(p_dev_rec->ble.ble_addr_type, bd_addr);
+
             if (memcmp(p_dev_rec->ble.static_addr, dummy_bda, BD_ADDR_LEN) != 0 &&
                 memcmp(p_dev_rec->ble.static_addr, bd_addr, BD_ADDR_LEN) != 0)
-            {
-                started = btsnd_hcic_ble_remove_from_white_list (p_dev_rec->ble.static_addr_type, p_dev_rec->ble.static_addr);
-            }
+                started = btsnd_hcic_ble_remove_from_white_list(p_dev_rec->ble.static_addr_type, p_dev_rec->ble.static_addr);
+
             p_dev_rec->ble.in_controller_list &= ~BTM_WHITE_LIST_BIT;
         }
-    }    /* if not a known device, shall we add it? */
-    else
-    {
-        BTM_ReadDevInfo(bd_addr, &dev_type, &addr_type);
-
-        started = btsnd_hcic_ble_remove_from_white_list (addr_type, bd_addr);
+    } else {
+        /* not a known device, i.e. attempt to connect to device never seen before */
+        UINT8 addr_type = BTM_IS_PUBLIC_BDA(bd_addr) ? BLE_ADDR_PUBLIC : BLE_ADDR_RANDOM;
+        started = btsnd_hcic_ble_remove_from_white_list(addr_type, bd_addr);
         if (to_add)
-            started = btsnd_hcic_ble_add_white_list (addr_type, bd_addr);
+            started = btsnd_hcic_ble_add_white_list(addr_type, bd_addr);
     }
 
     return started;
diff --git a/stack/btm/btm_ble_int.h b/stack/btm/btm_ble_int.h
index 2ac53dc..3b16abb 100644
--- a/stack/btm/btm_ble_int.h
+++ b/stack/btm/btm_ble_int.h
@@ -85,6 +85,10 @@
 #define BLE_RESOLVE_ADDR_MASK                0xc0   /* bit 6, and bit7 */
 #define BTM_BLE_IS_RESOLVE_BDA(x)           ((x[0] & BLE_RESOLVE_ADDR_MASK) == BLE_RESOLVE_ADDR_MSB)
 
+#define BLE_PUBLIC_ADDR_MSB_MASK            0xC0
+#define BLE_PUBLIC_ADDR_MSB                 0x80   /*  most significant bit, bit7, bit6 is 10 to be public address*/
+#define BTM_IS_PUBLIC_BDA(x)               ((x[0]  & BLE_PUBLIC_ADDR_MSB) == BLE_PUBLIC_ADDR_MSB_MASK)
+
 /* LE scan activity bit mask, continue with LE inquiry bits */
 #define BTM_LE_SELECT_CONN_ACTIVE      0x40     /* selection connection is in progress */
 #define BTM_LE_OBSERVE_ACTIVE          0x80     /* observe is in progress */