From d8a6cea42a8e2aed49462556df93b824e41e4b51 Mon Sep 17 00:00:00 2001
From: Chris Manton <cmanton@google.com>
Date: Sat, 30 Oct 2021 16:43:10 -0700
Subject: [PATCH] Add SupportsBlePrivacy for gd privacy policy

Add LE_SET_PRIVACY_MODE if controller supports it

Bug: 203417776
Tag: #refactor
Test: gd/cert/run
BYPASS_LONG_LINES_REASON: Bluetooth likes 120 lines

Change-Id: I9c7a8a82c9d53a32d2bb5396a7495350f6944ff2
---
 system/gd/hci/acl_manager/le_impl.h | 7 ++++++-
 system/gd/hci/le_address_manager.cc | 9 +++++++++
 system/gd/hci/le_address_manager.h  | 5 ++++-
 3 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/system/gd/hci/acl_manager/le_impl.h b/system/gd/hci/acl_manager/le_impl.h
index 6fb1de3378f..f251f7ce71e 100644
--- a/system/gd/hci/acl_manager/le_impl.h
+++ b/system/gd/hci/acl_manager/le_impl.h
@@ -636,7 +636,12 @@ struct le_impl : public bluetooth::hci::LeAddressManagerCallback {
       std::chrono::milliseconds minimum_rotation_time,
       std::chrono::milliseconds maximum_rotation_time) {
     le_address_manager_->SetPrivacyPolicyForInitiatorAddress(
-        address_policy, fixed_address, rotation_irk, minimum_rotation_time, maximum_rotation_time);
+        address_policy,
+        fixed_address,
+        rotation_irk,
+        controller_->SupportsBlePrivacy(),
+        minimum_rotation_time,
+        maximum_rotation_time);
   }
 
   // TODO(jpawlowski): remove once we have config file abstraction in cert tests
diff --git a/system/gd/hci/le_address_manager.cc b/system/gd/hci/le_address_manager.cc
index c7600c5119b..c8b3ec655e4 100644
--- a/system/gd/hci/le_address_manager.cc
+++ b/system/gd/hci/le_address_manager.cc
@@ -48,12 +48,14 @@ void LeAddressManager::SetPrivacyPolicyForInitiatorAddress(
     AddressPolicy address_policy,
     AddressWithType fixed_address,
     crypto_toolbox::Octet16 rotation_irk,
+    bool supports_ble_privacy,
     std::chrono::milliseconds minimum_rotation_time,
     std::chrono::milliseconds maximum_rotation_time) {
   ASSERT(address_policy_ == AddressPolicy::POLICY_NOT_SET);
   ASSERT(address_policy != AddressPolicy::POLICY_NOT_SET);
   ASSERT_LOG(registered_clients_.empty(), "Policy must be set before clients are registered.");
   address_policy_ = address_policy;
+  supports_ble_privacy_ = supports_ble_privacy;
   LOG_INFO("SetPrivacyPolicyForInitiatorAddress with policy %d", address_policy);
 
   switch (address_policy_) {
@@ -391,6 +393,13 @@ void LeAddressManager::AddDeviceToResolvingList(
   Command command = {CommandType::ADD_DEVICE_TO_RESOLVING_LIST, std::move(packet_builder)};
   cached_commands_.push(std::move(command));
 
+  if (supports_ble_privacy_) {
+    auto packet_builder =
+        hci::LeSetPrivacyModeBuilder::Create(peer_identity_address_type, peer_identity_address, PrivacyMode::DEVICE);
+    Command command = {CommandType::LE_SET_PRIVACY_MODE, std::move(packet_builder)};
+    cached_commands_.push(std::move(command));
+  }
+
   // Enable Address resolution
   auto enable_builder = hci::LeSetAddressResolutionEnableBuilder::Create(hci::Enable::ENABLED);
   Command enable = {CommandType::SET_ADDRESS_RESOLUTION_ENABLE, std::move(enable_builder)};
diff --git a/system/gd/hci/le_address_manager.h b/system/gd/hci/le_address_manager.h
index 9b54f0ee85a..8aaa11f1fbc 100644
--- a/system/gd/hci/le_address_manager.h
+++ b/system/gd/hci/le_address_manager.h
@@ -57,6 +57,7 @@ class LeAddressManager {
       AddressPolicy address_policy,
       AddressWithType fixed_address,
       crypto_toolbox::Octet16 rotation_irk,
+      bool supports_ble_privacy,
       std::chrono::milliseconds minimum_rotation_time,
       std::chrono::milliseconds maximum_rotation_time);
   // TODO(jpawlowski): remove once we have config file abstraction in cert tests
@@ -105,7 +106,8 @@ class LeAddressManager {
     ADD_DEVICE_TO_RESOLVING_LIST,
     REMOVE_DEVICE_FROM_RESOLVING_LIST,
     CLEAR_RESOLVING_LIST,
-    SET_ADDRESS_RESOLUTION_ENABLE
+    SET_ADDRESS_RESOLUTION_ENABLE,
+    LE_SET_PRIVACY_MODE
   };
 
   struct Command {
@@ -144,6 +146,7 @@ class LeAddressManager {
   uint8_t connect_list_size_;
   uint8_t resolving_list_size_;
   std::queue<Command> cached_commands_;
+  bool supports_ble_privacy_{false};
 };
 
 }  // namespace hci
-- 
GitLab