From 0e93532c3929550a63281d6140206e676cde6d11 Mon Sep 17 00:00:00 2001
From: Abhishek Pandit-Subedi <abhishekpandit@google.com>
Date: Sun, 23 Oct 2022 14:46:39 -0700
Subject: [PATCH] floss: Allow HID reconnect during suspend

Raw addresses for paired devices aren't only stored as public addresses.
Since we filter for LE HID attributes, provide both the RawAddress and
LE address type when we provide the list of devices that can wake the
system.

In order for LeAddressManager to re-arm connection, we need to add these
devices via |AcceptLeConnectionFrom| instead of
|AddDeviceToFilterAcceptList|.

Bug: 231345733
Tag: #floss
Test: Manual test on ChromeOS.
Change-Id: I8ae8bc0f71ed07260f5f1880ab6bb8f27ffff972
---
 system/bta/dm/bta_dm_act.cc                |  5 +++--
 system/bta/dm/bta_dm_api.cc                |  3 ++-
 system/bta/dm/bta_dm_int.h                 |  3 ++-
 system/bta/include/bta_api.h               |  3 ++-
 system/btif/include/btif_storage.h         |  3 ++-
 system/btif/src/btif_storage.cc            | 15 ++++++++++++---
 system/main/shim/btm_api.cc                | 12 +++++++++---
 system/main/shim/btm_api.h                 |  3 ++-
 system/test/mock/mock_main_shim_btm_api.cc |  2 +-
 9 files changed, 35 insertions(+), 14 deletions(-)

diff --git a/system/bta/dm/bta_dm_act.cc b/system/bta/dm/bta_dm_act.cc
index 3a818581a6d..ba8a93c3060 100644
--- a/system/bta/dm/bta_dm_act.cc
+++ b/system/bta/dm/bta_dm_act.cc
@@ -4183,10 +4183,11 @@ void bta_dm_set_event_filter_connection_setup_all_devices() {
  *
  * Description     Allow the device to be woken by HID devices
  *
- * Parameters      std::vector or RawAddress
+ * Parameters      std::vector of (Address, Address Type)
  *
  *******************************************************************************/
-void bta_dm_allow_wake_by_hid(std::vector<RawAddress> le_hid_devices) {
+void bta_dm_allow_wake_by_hid(
+    std::vector<std::pair<RawAddress, uint8_t>> le_hid_devices) {
   // Autoplumbed
   bluetooth::shim::BTM_AllowWakeByHid(le_hid_devices);
 }
diff --git a/system/bta/dm/bta_dm_api.cc b/system/bta/dm/bta_dm_api.cc
index 6be4aba7f9c..6be4b3cb97b 100644
--- a/system/bta/dm/bta_dm_api.cc
+++ b/system/bta/dm/bta_dm_api.cc
@@ -739,7 +739,8 @@ void BTA_DmSetEventFilterConnectionSetupAllDevices() {
       base::Bind(bta_dm_set_event_filter_connection_setup_all_devices));
 }
 
-void BTA_DmAllowWakeByHid(std::vector<RawAddress> le_hid_devices) {
+void BTA_DmAllowWakeByHid(
+    std::vector<std::pair<RawAddress, uint8_t>> le_hid_devices) {
   APPL_TRACE_API("BTA_DmAllowWakeByHid");
   do_in_main_thread(FROM_HERE,
                     base::Bind(bta_dm_allow_wake_by_hid, le_hid_devices));
diff --git a/system/bta/dm/bta_dm_int.h b/system/bta/dm/bta_dm_int.h
index 2491af1d57d..0d4a3d909f5 100644
--- a/system/bta/dm/bta_dm_int.h
+++ b/system/bta/dm/bta_dm_int.h
@@ -547,7 +547,8 @@ extern void bta_dm_clear_filter_accept_list(void);
 extern void bta_dm_disconnect_all_acls(void);
 extern void bta_dm_le_rand(LeRandCallback cb);
 extern void bta_dm_set_event_filter_connection_setup_all_devices();
-extern void bta_dm_allow_wake_by_hid(std::vector<RawAddress> le_hid_devices);
+extern void bta_dm_allow_wake_by_hid(
+    std::vector<std::pair<RawAddress, uint8_t>> le_hid_devices);
 extern void bta_dm_restore_filter_accept_list();
 extern void bta_dm_set_default_event_mask();
 extern void bta_dm_set_event_filter_inquiry_result_all_devices();
diff --git a/system/bta/include/bta_api.h b/system/bta/include/bta_api.h
index 9cf3fa1be85..effbc30787b 100644
--- a/system/bta/include/bta_api.h
+++ b/system/bta/include/bta_api.h
@@ -1298,7 +1298,8 @@ extern void BTA_DmSetEventFilterConnectionSetupAllDevices();
  * Parameters
  *
  *******************************************************************************/
-extern void BTA_DmAllowWakeByHid(std::vector<RawAddress> le_hid_devices);
+extern void BTA_DmAllowWakeByHid(
+    std::vector<std::pair<RawAddress, uint8_t>> le_hid_devices);
 
 /*******************************************************************************
  *
diff --git a/system/btif/include/btif_storage.h b/system/btif/include/btif_storage.h
index 1c113a92360..f155d9fee57 100644
--- a/system/btif/include/btif_storage.h
+++ b/system/btif/include/btif_storage.h
@@ -250,7 +250,8 @@ bt_status_t btif_storage_remove_hid_info(const RawAddress& remote_bd_addr);
  * Returns          std::vector of RawAddress
  *
  ******************************************************************************/
-std::vector<RawAddress> btif_storage_get_hid_device_addresses(void);
+std::vector<std::pair<RawAddress, uint8_t>>
+btif_storage_get_hid_device_addresses(void);
 
 /** Loads information about bonded hearing aid devices */
 void btif_storage_load_bonded_hearing_aids();
diff --git a/system/btif/src/btif_storage.cc b/system/btif/src/btif_storage.cc
index 13dd0a66fc7..1bb1356c2ea 100644
--- a/system/btif/src/btif_storage.cc
+++ b/system/btif/src/btif_storage.cc
@@ -1711,13 +1711,22 @@ bt_status_t btif_storage_remove_hid_info(const RawAddress& remote_bd_addr) {
  * Returns          std::vector of RawAddress
  *
  ******************************************************************************/
-std::vector<RawAddress> btif_storage_get_hid_device_addresses(void) {
-  std::vector<RawAddress> hid_addresses;
+
+extern bool btif_get_address_type(const RawAddress& bda,
+                                  tBLE_ADDR_TYPE* p_addr_type);
+
+std::vector<std::pair<RawAddress, uint8_t>>
+btif_storage_get_hid_device_addresses(void) {
+  std::vector<std::pair<RawAddress, uint8_t>> hid_addresses;
   for (const auto& bd_addr : btif_config_get_paired_devices()) {
     auto name = bd_addr.ToString();
     int value;
     if (!btif_config_get_int(name, "HidAttrMask", &value)) continue;
-    hid_addresses.push_back(bd_addr);
+
+    tBLE_ADDR_TYPE type = BLE_ADDR_PUBLIC;
+    btif_get_address_type(bd_addr, &type);
+
+    hid_addresses.push_back({bd_addr, type});
     LOG_DEBUG("Remote device: %s", PRIVATE_ADDRESS(bd_addr));
   }
   return hid_addresses;
diff --git a/system/main/shim/btm_api.cc b/system/main/shim/btm_api.cc
index e832fdc35d9..0972101d38f 100644
--- a/system/main/shim/btm_api.cc
+++ b/system/main/shim/btm_api.cc
@@ -1366,13 +1366,19 @@ tBTM_STATUS bluetooth::shim::BTM_SetEventFilterConnectionSetupAllDevices() {
 }
 
 tBTM_STATUS bluetooth::shim::BTM_AllowWakeByHid(
-    std::vector<RawAddress> le_hid_devices) {
+    std::vector<std::pair<RawAddress, uint8_t>> le_hid_devices) {
   // Autoplumbed
   controller_get_interface()->allow_wake_by_hid();
   // Allow BLE HID
   for (auto hid_address : le_hid_devices) {
-    Stack::GetInstance()->GetAcl()->AddDeviceToFilterAcceptList(
-        ToAddressWithType(hid_address, BLE_ADDR_PUBLIC));
+    std::promise<bool> accept_promise;
+    auto accept_future = accept_promise.get_future();
+
+    Stack::GetInstance()->GetAcl()->AcceptLeConnectionFrom(
+        ToAddressWithType(hid_address.first, hid_address.second),
+        /*is_direct=*/false, std::move(accept_promise));
+
+    accept_future.wait();
   }
   return BTM_SUCCESS;
 }
diff --git a/system/main/shim/btm_api.h b/system/main/shim/btm_api.h
index 88831403328..9fe29f22ace 100644
--- a/system/main/shim/btm_api.h
+++ b/system/main/shim/btm_api.h
@@ -1889,7 +1889,8 @@ tBTM_STATUS BTM_SetEventFilterConnectionSetupAllDevices(void);
  * Parameters      std::vector of RawAddress
  *
  *******************************************************************************/
-tBTM_STATUS BTM_AllowWakeByHid(std::vector<RawAddress> le_hid_devices);
+tBTM_STATUS BTM_AllowWakeByHid(
+    std::vector<std::pair<RawAddress, uint8_t>> le_hid_devices);
 
 /*******************************************************************************
  *
diff --git a/system/test/mock/mock_main_shim_btm_api.cc b/system/test/mock/mock_main_shim_btm_api.cc
index 89ce3926383..dc71b6b39e6 100644
--- a/system/test/mock/mock_main_shim_btm_api.cc
+++ b/system/test/mock/mock_main_shim_btm_api.cc
@@ -456,7 +456,7 @@ tBTM_STATUS bluetooth::shim::BTM_SetEventFilterConnectionSetupAllDevices() {
 }
 
 tBTM_STATUS bluetooth::shim::BTM_AllowWakeByHid(
-    std::vector<RawAddress> le_hid_devices) {
+    std::vector<std::pair<RawAddress, uint8_t>> le_hid_devices) {
   mock_function_count_map[__func__]++;
   return BTM_SUCCESS;
 }
-- 
GitLab