Merge "Assert if alarm callback is null"
diff --git a/binder/android/bluetooth/IBluetoothHeadset.aidl b/binder/android/bluetooth/IBluetoothHeadset.aidl
index f8dfb65..0f6954c 100644
--- a/binder/android/bluetooth/IBluetoothHeadset.aidl
+++ b/binder/android/bluetooth/IBluetoothHeadset.aidl
@@ -53,7 +53,7 @@
     void setForceScoAudio(boolean forced);
     boolean startScoUsingVirtualVoiceCall();
     boolean stopScoUsingVirtualVoiceCall();
-    oneway void phoneStateChanged(int numActive, int numHeld, int callState, String number, int type);
+    oneway void phoneStateChanged(int numActive, int numHeld, int callState, String number, int type, String name);
     void clccResponse(int index, int direction, int status, int mode, boolean mpty,
                       String number, int type);
     boolean setActiveDevice(in BluetoothDevice device);
diff --git a/bta/BUILD.gn b/bta/BUILD.gn
index afbb3a3..4a8b40b 100644
--- a/bta/BUILD.gn
+++ b/bta/BUILD.gn
@@ -130,9 +130,10 @@
 executable("net_test_bta") {
   testonly = true
   sources = [
-        "test/gatt/database_builder_test.cc",
-        "test/gatt/database_builder_sample_device_test.cc",
-        "test/gatt/database_test.cc",
+    "gatt/database_builder.cc",
+    "test/gatt/database_builder_test.cc",
+    "test/gatt/database_builder_sample_device_test.cc",
+    "test/gatt/database_test.cc",
   ]
 
   include_dirs = [
diff --git a/bta/ag/bta_ag_cmd.cc b/bta/ag/bta_ag_cmd.cc
index 763fabd..a1d3728 100644
--- a/bta/ag/bta_ag_cmd.cc
+++ b/bta/ag/bta_ag_cmd.cc
@@ -47,12 +47,6 @@
 /* Invalid Chld command */
 #define BTA_AG_INVALID_CHLD 255
 
-/* clip type constants */
-#define BTA_AG_CLIP_TYPE_MIN 128
-#define BTA_AG_CLIP_TYPE_MAX 175
-#define BTA_AG_CLIP_TYPE_DEFAULT 129
-#define BTA_AG_CLIP_TYPE_VOIP 255
-
 #define COLON_IDX_4_VGSVGM 4
 
 /* Local events which will not trigger a higher layer callback */
@@ -1430,24 +1424,10 @@
       /* tell sys to stop av if any */
       bta_sys_sco_use(BTA_ID_AG, p_scb->app_id, p_scb->peer_addr);
 
-      /* Store caller id string.
-       * Append type info at the end.
-       * Make sure a valid type info is passed.
-       * Otherwise add 129 as default type */
-      uint16_t clip_type = result.data.num;
-      if ((clip_type < BTA_AG_CLIP_TYPE_MIN) ||
-          (clip_type > BTA_AG_CLIP_TYPE_MAX)) {
-        if (clip_type != BTA_AG_CLIP_TYPE_VOIP) {
-          clip_type = BTA_AG_CLIP_TYPE_DEFAULT;
-        }
-      }
-
-      APPL_TRACE_DEBUG("CLIP type :%d", clip_type);
       p_scb->clip[0] = 0;
-      if (result.data.str[0] != 0)
-        snprintf(p_scb->clip, sizeof(p_scb->clip), "%s,%d", result.data.str,
-                 clip_type);
-
+      if (result.data.str[0] != 0) {
+        snprintf(p_scb->clip, sizeof(p_scb->clip), "%s", result.data.str);
+      }
       /* send callsetup indicator */
       if (p_scb->post_sco == BTA_AG_POST_SCO_CALL_END) {
         /* Need to sent 2 callsetup IND's(Call End and Incoming call) after SCO
diff --git a/bta/dm/bta_dm_act.cc b/bta/dm/bta_dm_act.cc
index 480e4a2..cc56c1a 100644
--- a/bta/dm/bta_dm_act.cc
+++ b/bta/dm/bta_dm_act.cc
@@ -4039,20 +4039,6 @@
     }
   }
 }
-/*******************************************************************************
- *
- * Function         bta_dm_ble_set_adv_params
- *
- * Description      This function set the adv parameters.
- *
- * Parameters:
- *
- ******************************************************************************/
-void bta_dm_ble_set_adv_params(uint16_t adv_int_min, uint16_t adv_int_max,
-                               tBLE_BD_ADDR* p_dir_bda) {
-  BTM_BleSetAdvParams(adv_int_min, adv_int_max, p_dir_bda,
-                      BTA_DM_BLE_ADV_CHNL_MAP);
-}
 
 /** This function set the maximum transmission packet size */
 void bta_dm_ble_set_data_length(const RawAddress& bd_addr,
diff --git a/bta/dm/bta_dm_int.h b/bta/dm/bta_dm_int.h
index 0e2e9e3..97d7f80 100644
--- a/bta/dm/bta_dm_int.h
+++ b/bta/dm/bta_dm_int.h
@@ -510,9 +510,6 @@
                                           uint16_t, uint16_t, uint16_t,
                                           uint16_t);
 extern void bta_dm_ble_config_local_privacy(bool);
-extern void bta_dm_ble_set_adv_params(uint16_t adv_int_min,
-                                      uint16_t adv_int_max,
-                                      tBLE_BD_ADDR* p_dir_bda);
 
 extern void bta_dm_ble_set_data_length(const RawAddress&, uint16_t);
 
diff --git a/bta/include/bta_ag_api.h b/bta/include/bta_ag_api.h
index b968ae6..c5d4f36 100644
--- a/bta/include/bta_ag_api.h
+++ b/bta/include/bta_ag_api.h
@@ -264,6 +264,12 @@
 #define BTA_AG_BTRH_READ 3     /* Read the current value */
 #define BTA_AG_BTRH_NO_RESP 4  /* Not in RH States (reply to read) */
 
+/* clip type constants */
+#define BTA_AG_CLIP_TYPE_MIN 128
+#define BTA_AG_CLIP_TYPE_MAX 175
+#define BTA_AG_CLIP_TYPE_DEFAULT 129
+#define BTA_AG_CLIP_TYPE_VOIP 255
+
 /* ASCII character string of arguments to the AT command or result */
 #ifndef BTA_AG_AT_MAX_LEN
 #define BTA_AG_AT_MAX_LEN 256
diff --git a/btif/include/btif_av.h b/btif/include/btif_av.h
index 486c2af..370d5f1 100644
--- a/btif/include/btif_av.h
+++ b/btif/include/btif_av.h
@@ -92,27 +92,6 @@
 void btif_av_clear_remote_suspend_flag(void);
 
 /**
- * Process AVRCP Open event.
- *
- * @param peer_address the peer address
- */
-void btif_av_avrcp_event_open(const RawAddress& peer_address);
-
-/**
- * Process AVRCP Close event.
- *
- * @param peer_address the peer address
- */
-void btif_av_avrcp_event_close(const RawAddress& peer_address);
-
-/**
- * Process AVRCP Remote Play event.
- *
- * @param peer_address the peer address
- */
-void btif_av_avrcp_event_remote_play(const RawAddress& peer_address);
-
-/**
  * Check whether the connected A2DP peer supports EDR.
  *
  * The value can be provided only if the remote peer is connected.
diff --git a/btif/src/btif_av.cc b/btif/src/btif_av.cc
index ff625d1..818abcf 100644
--- a/btif/src/btif_av.cc
+++ b/btif/src/btif_av.cc
@@ -2968,49 +2968,6 @@
   peer->ClearFlags(BtifAvPeer::kFlagRemoteSuspend);
 }
 
-void btif_av_avrcp_event_open(const RawAddress& peer_address) {
-  // TODO: We need a better demultipexing mechanism whether the remote device
-  // is an A2DP Source or a Sink.
-  if (btif_av_source.Enabled()) {
-    BtifAvPeer* peer =
-        btif_av_source.FindOrCreatePeer(peer_address, kBtaHandleUnknown);
-    if (peer != nullptr) {
-      btif_av_source_dispatch_sm_event(peer_address, BTIF_AV_AVRCP_OPEN_EVT);
-      return;
-    }
-  } else if (btif_av_sink.Enabled()) {
-    BtifAvPeer* peer =
-        btif_av_sink.FindOrCreatePeer(peer_address, kBtaHandleUnknown);
-    if (peer != nullptr) {
-      btif_av_sink_dispatch_sm_event(peer_address, BTIF_AV_AVRCP_OPEN_EVT);
-      return;
-    }
-  }
-  BTIF_TRACE_ERROR("%s: event ignored: cannot find or create peer state for %s",
-                   __func__, peer_address.ToString().c_str());
-}
-
-void btif_av_avrcp_event_close(const RawAddress& peer_address) {
-  // TODO: We need a better demultipexing mechanism whether the remote device
-  // is an A2DP Source or a Sink.
-  if (btif_av_source.Enabled()) {
-    btif_av_source_dispatch_sm_event(peer_address, BTIF_AV_AVRCP_CLOSE_EVT);
-  } else if (btif_av_sink.Enabled()) {
-    btif_av_sink_dispatch_sm_event(peer_address, BTIF_AV_AVRCP_CLOSE_EVT);
-  }
-}
-
-void btif_av_avrcp_event_remote_play(const RawAddress& peer_address) {
-  // TODO: We need a better demultipexing mechanism whether the remote device
-  // is an A2DP Source or a Sink.
-  if (btif_av_source.Enabled()) {
-    btif_av_source_dispatch_sm_event(peer_address,
-                                     BTIF_AV_AVRCP_REMOTE_PLAY_EVT);
-  } else if (btif_av_sink.Enabled()) {
-    btif_av_sink_dispatch_sm_event(peer_address, BTIF_AV_AVRCP_REMOTE_PLAY_EVT);
-  }
-}
-
 bool btif_av_is_peer_edr(const RawAddress& peer_address) {
   BtifAvPeer* peer = btif_av_find_peer(peer_address);
   if (peer == nullptr) {
diff --git a/btif/src/btif_hf.cc b/btif/src/btif_hf.cc
index ea55d2a..e0e9515 100644
--- a/btif/src/btif_hf.cc
+++ b/btif/src/btif_hf.cc
@@ -700,7 +700,7 @@
   bt_status_t PhoneStateChange(int num_active, int num_held,
                                bthf_call_state_t call_setup_state,
                                const char* number, bthf_call_addrtype_t type,
-                               RawAddress* bd_addr) override;
+                               const char* name, RawAddress* bd_addr) override;
 
   void Cleanup() override;
   bt_status_t SetScoAllowed(bool value) override;
@@ -1043,7 +1043,8 @@
 
 bt_status_t HeadsetInterface::PhoneStateChange(
     int num_active, int num_held, bthf_call_state_t call_setup_state,
-    const char* number, bthf_call_addrtype_t type, RawAddress* bd_addr) {
+    const char* number, bthf_call_addrtype_t type, const char* name,
+    RawAddress* bd_addr) {
   CHECK_BTHF_INIT();
   if (!bd_addr) {
     BTIF_TRACE_WARNING("%s: bd_addr is null", __func__);
@@ -1175,22 +1176,53 @@
           }
         }
         if (number) {
-          int xx = 0;
-          if ((type == BTHF_CALL_ADDRTYPE_INTERNATIONAL) && (*number != '+'))
-            xx = snprintf(ag_res.str, sizeof(ag_res.str), "\"+%s\"", number);
-          else
-            xx = snprintf(ag_res.str, sizeof(ag_res.str), "\"%s\"", number);
-          ag_res.num = type;
-          // 5 = [,][3_digit_type][null_terminator]
-          if (xx > static_cast<int>(sizeof(ag_res.str) - 5)) {
-            android_errorWriteLog(0x534e4554, "79431031");
-            xx = sizeof(ag_res.str) - 5;
-            // Null terminating the string
-            memset(&ag_res.str[xx], 0, 5);
+          std::ostringstream call_number_stream;
+          if ((type == BTHF_CALL_ADDRTYPE_INTERNATIONAL) && (*number != '+')) {
+            call_number_stream << "\"+";
+          } else {
+            call_number_stream << "\"";
           }
 
-          if (res == BTA_AG_CALL_WAIT_RES)
-            snprintf(&ag_res.str[xx], sizeof(ag_res.str) - xx, ",%d", type);
+          std::string name_str;
+          if (name) {
+            name_str.append(name);
+          }
+          std::string number_str(number);
+          // 13 = ["][+]["][,][3_digit_type][,,,]["]["][null_terminator]
+          int overflow_size =
+              13 + static_cast<int>(number_str.length() + name_str.length()) -
+              static_cast<int>(sizeof(ag_res.str));
+          if (overflow_size > 0) {
+            android_errorWriteLog(0x534e4554, "79431031");
+            int extra_overflow_size =
+                overflow_size - static_cast<int>(name_str.length());
+            if (extra_overflow_size > 0) {
+              number_str.resize(number_str.length() - extra_overflow_size);
+              name_str.clear();
+            } else {
+              name_str.resize(name_str.length() - overflow_size);
+            }
+          }
+          call_number_stream << number_str << "\"";
+
+          // Store caller id string and append type info.
+          // Make sure type info is valid, otherwise add 129 as default type
+          ag_res.num = static_cast<uint16_t>(type);
+          if ((ag_res.num < BTA_AG_CLIP_TYPE_MIN) ||
+              (ag_res.num > BTA_AG_CLIP_TYPE_MAX)) {
+            if (ag_res.num != BTA_AG_CLIP_TYPE_VOIP) {
+              ag_res.num = BTA_AG_CLIP_TYPE_DEFAULT;
+            }
+          }
+
+          if (res == BTA_AG_CALL_WAIT_RES || name_str.empty()) {
+            call_number_stream << "," << std::to_string(ag_res.num);
+          } else {
+            call_number_stream << "," << std::to_string(ag_res.num) << ",,,\""
+                               << name_str << "\"";
+          }
+          snprintf(ag_res.str, sizeof(ag_res.str), "%s",
+                   call_number_stream.str().c_str());
         }
         break;
       case BTHF_CALL_STATE_DIALING:
diff --git a/btif/src/btif_sock.cc b/btif/src/btif_sock.cc
index 90121b1..7deb29f 100644
--- a/btif/src/btif_sock.cc
+++ b/btif/src/btif_sock.cc
@@ -237,7 +237,8 @@
       btsock_l2cap_signaled(fd, flags, user_id);
       break;
     default:
-      CHECK(false && "Invalid socket type");
+      LOG(FATAL) << "Invalid socket type! type=" << type << " fd=" << fd
+                 << " flags=" << flags << " user_id=" << user_id;
       break;
   }
 }
diff --git a/hci/BUILD.gn b/hci/BUILD.gn
index fc9c2ce..e29e256 100644
--- a/hci/BUILD.gn
+++ b/hci/BUILD.gn
@@ -38,6 +38,7 @@
   ]
 
   deps = [
+    "//common",
     "//third_party/libchrome:base",
   ]
 }
diff --git a/include/hardware/bluetooth_headset_interface.h b/include/hardware/bluetooth_headset_interface.h
index a3e0e37..8527919 100644
--- a/include/hardware/bluetooth_headset_interface.h
+++ b/include/hardware/bluetooth_headset_interface.h
@@ -195,6 +195,7 @@
    * @param call_setup_state current call setup state
    * @param number phone number of the call
    * @param type type of the call
+   * @param name caller display name
    * @param bd_addr remote device address
    * @return BT_STATUS_SUCCESS on success
    */
@@ -202,6 +203,7 @@
                                        bthf_call_state_t call_setup_state,
                                        const char* number,
                                        bthf_call_addrtype_t type,
+                                       const char* name,
                                        RawAddress* bd_addr) = 0;
 
   /**
diff --git a/osi/BUILD.gn b/osi/BUILD.gn
index b840bac..c4fb0cb 100644
--- a/osi/BUILD.gn
+++ b/osi/BUILD.gn
@@ -52,6 +52,7 @@
   ]
 
   deps = [
+    "//common",
     "//third_party/libchrome:base",
   ]
 }
@@ -68,14 +69,12 @@
     "test/config_test.cc",
     "test/future_test.cc",
     "test/hash_map_utils_test.cc",
-    "test/leaky_bonded_queue_test.cc",
     "test/list_test.cc",
     "test/properties_test.cc",
     "test/rand_test.cc",
     "test/reactor_test.cc",
     "test/ringbuffer_test.cc",
     "test/thread_test.cc",
-    "test/time_test.cc",
   ]
 
   include_dirs = [
diff --git a/stack/BUILD.gn b/stack/BUILD.gn
index f25c6c8..88ad3e2 100644
--- a/stack/BUILD.gn
+++ b/stack/BUILD.gn
@@ -274,6 +274,9 @@
 executable("net_test_stack_smp") {
   testonly = true
   sources = [
+        "smp/p_256_curvepara.cc",
+        "smp/p_256_ecc_pp.cc",
+        "smp/p_256_multprecision.cc",
         "smp/smp_keys.cc",
         "smp/smp_api.cc",
         "smp/smp_main.cc",
@@ -283,6 +286,7 @@
 
   include_dirs = [
     "//",
+    "//linux_include",
     "//internal_include",
     "//btcore/include",
     "//hci/include",
diff --git a/stack/btm/btm_ble_bgconn.cc b/stack/btm/btm_ble_bgconn.cc
index af3b44e..0236ed7 100644
--- a/stack/btm/btm_ble_bgconn.cc
+++ b/stack/btm/btm_ble_bgconn.cc
@@ -154,6 +154,30 @@
   }
 }
 
+bool BTM_BackgroundConnectAddressKnown(const RawAddress& address) {
+  tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev(address);
+
+  //  not a known device, or a classic device, we assume public address
+  if (p_dev_rec == NULL || (p_dev_rec->device_type & BT_DEVICE_TYPE_BLE) == 0)
+    return true;
+
+  // bonded device with identity address known
+  if (p_dev_rec->ble.identity_addr != address &&
+      !p_dev_rec->ble.identity_addr.IsEmpty()) {
+    return true;
+  }
+
+  // Public address, Random Static, or Random Non-Resolvable Address known
+  if (p_dev_rec->ble.ble_addr_type == BLE_ADDR_PUBLIC ||
+      !BTM_BLE_IS_RESOLVE_BDA(address)) {
+    return true;
+  }
+
+  // Only Resolvable Private Address (RPA) is known, we don't allow it into
+  // the background connection procedure.
+  return false;
+}
+
 /*******************************************************************************
  *
  * Function         btm_add_dev_to_controller
@@ -162,33 +186,34 @@
  ******************************************************************************/
 bool btm_add_dev_to_controller(bool to_add, const RawAddress& bd_addr) {
   tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev(bd_addr);
-  bool started = false;
 
   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)) {
-        background_connection_add(p_dev_rec->ble.ble_addr_type, bd_addr);
-        started = true;
-        p_dev_rec->ble.in_controller_list |= BTM_WHITE_LIST_BIT;
-      } else if (p_dev_rec->ble.identity_addr != bd_addr &&
-                 !p_dev_rec->ble.identity_addr.IsEmpty()) {
+      if (p_dev_rec->ble.identity_addr != bd_addr &&
+          !p_dev_rec->ble.identity_addr.IsEmpty()) {
         background_connection_add(p_dev_rec->ble.identity_addr_type,
                                   p_dev_rec->ble.identity_addr);
-        started = true;
-        p_dev_rec->ble.in_controller_list |= BTM_WHITE_LIST_BIT;
-      }
-    } else {
-      if (p_dev_rec->ble.ble_addr_type == BLE_ADDR_PUBLIC ||
-          !BTM_BLE_IS_RESOLVE_BDA(bd_addr)) {
-        background_connection_remove(bd_addr);
-        started = true;
+      } else {
+        background_connection_add(p_dev_rec->ble.ble_addr_type, bd_addr);
+
+        if (p_dev_rec->ble.ble_addr_type == BLE_ADDR_RANDOM &&
+            BTM_BLE_IS_RESOLVE_BDA(bd_addr)) {
+          LOG(INFO) << __func__ << " addig RPA into white list";
+        }
       }
 
+      p_dev_rec->ble.in_controller_list |= BTM_WHITE_LIST_BIT;
+    } else {
       if (!p_dev_rec->ble.identity_addr.IsEmpty() &&
           p_dev_rec->ble.identity_addr != bd_addr) {
         background_connection_remove(p_dev_rec->ble.identity_addr);
-        started = true;
+      } else {
+        background_connection_remove(bd_addr);
+
+        if (p_dev_rec->ble.ble_addr_type == BLE_ADDR_RANDOM &&
+            BTM_BLE_IS_RESOLVE_BDA(bd_addr)) {
+          LOG(INFO) << __func__ << " removing RPA from white list";
+        }
       }
 
       p_dev_rec->ble.in_controller_list &= ~BTM_WHITE_LIST_BIT;
@@ -196,14 +221,13 @@
   } else {
     /* not a known device, i.e. attempt to connect to device never seen before
      */
-    started = true;
     if (to_add)
       background_connection_add(BLE_ADDR_PUBLIC, bd_addr);
     else
       background_connection_remove(bd_addr);
   }
 
-  return started;
+  return true;
 }
 
 /** White list add complete */
diff --git a/stack/btm/btm_ble_gap.cc b/stack/btm/btm_ble_gap.cc
index 381659c..9f9e8a3 100644
--- a/stack/btm/btm_ble_gap.cc
+++ b/stack/btm/btm_ble_gap.cc
@@ -833,64 +833,6 @@
   return evt_type;
 }
 
-/*******************************************************************************
- *
- * Function         BTM_BleSetAdvParams
- *
- * Description      This function is called to set advertising parameters.
- *
- * Parameters       adv_int_min: minimum advertising interval
- *                  adv_int_max: maximum advertising interval
- *                  p_dir_bda: connectable direct initiator's LE device address
- *                  chnl_map: advertising channel map.
- *
- * Returns          void
- *
- ******************************************************************************/
-tBTM_STATUS BTM_BleSetAdvParams(uint16_t adv_int_min, uint16_t adv_int_max,
-                                const RawAddress& p_dir_bda,
-                                tBTM_BLE_ADV_CHNL_MAP chnl_map) {
-  tBTM_LE_RANDOM_CB* p_addr_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
-  tBTM_BLE_INQ_CB* p_cb = &btm_cb.ble_ctr_cb.inq_var;
-  tBTM_STATUS status = BTM_SUCCESS;
-  RawAddress address = RawAddress::kEmpty;
-  tBLE_ADDR_TYPE init_addr_type = BLE_ADDR_PUBLIC;
-  tBLE_ADDR_TYPE own_addr_type = p_addr_cb->own_addr_type;
-  uint8_t adv_mode = p_cb->adv_mode;
-
-  BTM_TRACE_EVENT("BTM_BleSetAdvParams");
-
-  if (!controller_get_interface()->supports_ble()) return BTM_ILLEGAL_VALUE;
-
-  if (!BTM_BLE_ISVALID_PARAM(adv_int_min, BTM_BLE_ADV_INT_MIN,
-                             BTM_BLE_ADV_INT_MAX) ||
-      !BTM_BLE_ISVALID_PARAM(adv_int_max, BTM_BLE_ADV_INT_MIN,
-                             BTM_BLE_ADV_INT_MAX)) {
-    return BTM_ILLEGAL_VALUE;
-  }
-
-  p_cb->adv_interval_min = adv_int_min;
-  p_cb->adv_interval_max = adv_int_max;
-  p_cb->adv_chnl_map = chnl_map;
-  p_cb->direct_bda.bda = p_dir_bda;
-
-  BTM_TRACE_EVENT("update params for an active adv");
-
-  btm_ble_stop_adv();
-
-  p_cb->evt_type = btm_set_conn_mode_adv_init_addr(
-      p_cb, address, &init_addr_type, &own_addr_type);
-
-  /* update adv params */
-  btsnd_hcic_ble_write_adv_params(
-      p_cb->adv_interval_min, p_cb->adv_interval_max, p_cb->evt_type,
-      own_addr_type, init_addr_type, address, p_cb->adv_chnl_map, p_cb->afp);
-
-  if (adv_mode == BTM_BLE_ADV_ENABLE) btm_ble_start_adv();
-
-  return status;
-}
-
 /**
  * This function is called to set scan parameters. |cb| is called with operation
  * status
diff --git a/stack/gatt/gatt_api.cc b/stack/gatt/gatt_api.cc
index 852f801..ae7af8c 100644
--- a/stack/gatt/gatt_api.cc
+++ b/stack/gatt/gatt_api.cc
@@ -36,6 +36,7 @@
 
 using bluetooth::Uuid;
 
+extern bool BTM_BackgroundConnectAddressKnown(const RawAddress& address);
 /**
  * Add an service handle range to the list in decending order of the start
  * handle. Return reference to the newly added element.
@@ -1099,25 +1100,47 @@
 bool GATT_Connect(tGATT_IF gatt_if, const RawAddress& bd_addr, bool is_direct,
                   tBT_TRANSPORT transport, bool opportunistic,
                   uint8_t initiating_phys) {
-  LOG(INFO) << __func__ << "gatt_if=" << +gatt_if << " " << bd_addr;
+  LOG(INFO) << __func__ << "gatt_if=" << +gatt_if << ", address=" << bd_addr;
 
   /* Make sure app is registered */
   tGATT_REG* p_reg = gatt_get_regcb(gatt_if);
   if (!p_reg) {
     LOG(ERROR) << "gatt_if = " << +gatt_if << " is not registered";
-    return (false);
+    return false;
   }
 
-  if (is_direct)
-    return gatt_act_connect(p_reg, bd_addr, transport, opportunistic,
-                            initiating_phys);
-
-  // is not direct
-  if (transport != BT_TRANSPORT_LE) {
+  if (!is_direct && transport != BT_TRANSPORT_LE) {
     LOG(ERROR) << "Unsupported transport for background connection";
     return false;
   }
-  return gatt_auto_connect_dev_add(gatt_if, bd_addr);
+
+  bool ret;
+  if (is_direct) {
+    ret = gatt_act_connect(p_reg, bd_addr, transport, initiating_phys);
+  } else {
+    if (!BTM_BackgroundConnectAddressKnown(bd_addr)) {
+      //  RPA can rotate, and cause device to "expire" in the background
+      //  connection list. RPA is allowed for direct connect, as such request
+      //  times out after 30 seconds
+      LOG(INFO) << "Can't add RPA to background connection.";
+      ret = true;
+    } else {
+      ret = gatt::connection_manager::background_connect_add(gatt_if, bd_addr);
+    }
+  }
+
+  tGATT_TCB* p_tcb = gatt_find_tcb_by_addr(bd_addr, transport);
+  // background connections don't necesarly create tcb
+  if (!p_tcb) return ret;
+
+  if (ret) {
+    if (!opportunistic)
+      gatt_update_app_use_link_flag(p_reg->gatt_if, p_tcb, true, !is_direct);
+    else
+      VLOG(1) << __func__
+              << ": connection is opportunistic, not updating app usage";
+  }
+  return ret;
 }
 
 /*******************************************************************************
diff --git a/stack/gatt/gatt_int.h b/stack/gatt/gatt_int.h
index 6f882b6..198b6d1 100644
--- a/stack/gatt/gatt_int.h
+++ b/stack/gatt/gatt_int.h
@@ -396,8 +396,7 @@
 /* from gatt_main.cc */
 extern bool gatt_disconnect(tGATT_TCB* p_tcb);
 extern bool gatt_act_connect(tGATT_REG* p_reg, const RawAddress& bd_addr,
-                             tBT_TRANSPORT transport, bool opportunistic,
-                             int8_t initiating_phys);
+                             tBT_TRANSPORT transport, int8_t initiating_phys);
 extern bool gatt_connect(const RawAddress& rem_bda, tGATT_TCB* p_tcb,
                          tBT_TRANSPORT transport, uint8_t initiating_phys);
 extern void gatt_data_process(tGATT_TCB& p_tcb, BT_HDR* p_buf);
@@ -467,8 +466,6 @@
 extern tGATTS_SRV_CHG* gatt_add_srv_chg_clt(tGATTS_SRV_CHG* p_srv_chg);
 
 /* for background connection */
-extern bool gatt_auto_connect_dev_add(tGATT_IF gatt_if,
-                                      const RawAddress& bd_addr);
 extern bool gatt_auto_connect_dev_remove(tGATT_IF gatt_if,
                                          const RawAddress& bd_addr);
 
diff --git a/stack/gatt/gatt_main.cc b/stack/gatt/gatt_main.cc
index 51877ee..375d3df 100644
--- a/stack/gatt/gatt_main.cc
+++ b/stack/gatt/gatt_main.cc
@@ -343,48 +343,37 @@
 
 /** GATT connection initiation */
 bool gatt_act_connect(tGATT_REG* p_reg, const RawAddress& bd_addr,
-                      tBT_TRANSPORT transport, bool opportunistic,
-                      int8_t initiating_phys) {
-  bool ret = false;
-
+                      tBT_TRANSPORT transport, int8_t initiating_phys) {
   tGATT_TCB* p_tcb = gatt_find_tcb_by_addr(bd_addr, transport);
   if (p_tcb != NULL) {
-    ret = true;
-    uint8_t st = gatt_get_ch_state(p_tcb);
-
     /* before link down, another app try to open a GATT connection */
+    uint8_t st = gatt_get_ch_state(p_tcb);
     if (st == GATT_CH_OPEN && p_tcb->app_hold_link.empty() &&
         transport == BT_TRANSPORT_LE) {
       if (!gatt_connect(bd_addr, p_tcb, transport, initiating_phys))
-        ret = false;
+        return false;
     } else if (st == GATT_CH_CLOSING) {
       /* need to complete the closing first */
-      ret = false;
+      return false;
     }
-  } else {
-    p_tcb = gatt_allocate_tcb_by_bdaddr(bd_addr, transport);
-    if (!p_tcb) {
-      ret = 0;
-      LOG(ERROR) << "Max TCB for gatt_if [ " << +p_reg->gatt_if << "] reached.";
-    } else {
-      if (!gatt_connect(bd_addr, p_tcb, transport, initiating_phys)) {
-        LOG(ERROR) << "gatt_connect failed";
-        fixed_queue_free(p_tcb->pending_ind_q, NULL);
-        *p_tcb = tGATT_TCB();
-      } else
-        ret = true;
-    }
+
+    return true;
   }
 
-  if (ret) {
-    if (!opportunistic)
-      gatt_update_app_use_link_flag(p_reg->gatt_if, p_tcb, true, false);
-    else
-      VLOG(1) << __func__
-              << ": connection is opportunistic, not updating app usage";
+  p_tcb = gatt_allocate_tcb_by_bdaddr(bd_addr, transport);
+  if (!p_tcb) {
+    LOG(ERROR) << "Max TCB for gatt_if [ " << +p_reg->gatt_if << "] reached.";
+    return false;
   }
 
-  return ret;
+  if (!gatt_connect(bd_addr, p_tcb, transport, initiating_phys)) {
+    LOG(ERROR) << "gatt_connect failed";
+    fixed_queue_free(p_tcb->pending_ind_q, NULL);
+    *p_tcb = tGATT_TCB();
+    return false;
+  }
+
+  return true;
 }
 
 /** This callback function is called by L2CAP to indicate that the ATT fixed
diff --git a/stack/gatt/gatt_utils.cc b/stack/gatt/gatt_utils.cc
index 246dea9..91fecc4 100644
--- a/stack/gatt/gatt_utils.cc
+++ b/stack/gatt/gatt_utils.cc
@@ -1296,26 +1296,6 @@
     return (uint8_t*)"Op Code Exceed Max";
 }
 
-/**
- * This function add a device for background connection procedure.
- *
- * Parameters       p_reg: application record,
- *                  bd_addr: peer device address.
- *
- * Returns          true if connection started; false otherwise.
- */
-bool gatt_auto_connect_dev_add(tGATT_IF gatt_if, const RawAddress& bd_addr) {
-  VLOG(1) << __func__;
-
-  tGATT_TCB* p_tcb = gatt_find_tcb_by_addr(bd_addr, BT_TRANSPORT_LE);
-  bool ret = gatt::connection_manager::background_connect_add(gatt_if, bd_addr);
-  if (ret && p_tcb != NULL) {
-    /* if a connected device, update the link holding number */
-    gatt_update_app_use_link_flag(gatt_if, p_tcb, true, true);
-  }
-  return ret;
-}
-
 /** Remove the application interface for the specified background device */
 bool gatt_auto_connect_dev_remove(tGATT_IF gatt_if, const RawAddress& bd_addr) {
   tGATT_TCB* p_tcb = gatt_find_tcb_by_addr(bd_addr, BT_TRANSPORT_LE);
diff --git a/stack/include/btm_ble_api.h b/stack/include/btm_ble_api.h
index 3e58152..12a2222 100644
--- a/stack/include/btm_ble_api.h
+++ b/stack/include/btm_ble_api.h
@@ -77,22 +77,6 @@
 
 /*******************************************************************************
  *
- * Function         BTM_BleSetAdvParams
- *
- * Description      This function is called to set advertising parameters.
- *
- * Parameters:       None.
- *
- * Returns          void
- *
- ******************************************************************************/
-extern tBTM_STATUS BTM_BleSetAdvParams(uint16_t adv_int_min,
-                                       uint16_t adv_int_max,
-                                       tBLE_BD_ADDR* p_dir_bda,
-                                       tBTM_BLE_ADV_CHNL_MAP chnl_map);
-
-/*******************************************************************************
- *
  * Function         BTM_BleObtainVendorCapabilities
  *
  * Description      This function is called to obatin vendor capabilties
diff --git a/types/BUILD.gn b/types/BUILD.gn
index 617bbb6..fa9a4af 100644
--- a/types/BUILD.gn
+++ b/types/BUILD.gn
@@ -20,8 +20,9 @@
   ]
 
   sources = [
-    "raw_address.cc",
     "bluetooth/uuid.cc",
+    "le_address.cc",
+    "raw_address.cc",
   ]
 
   include_dirs = [