Shim: Add GD scanning init flags

Tag: #gd-refactor
Bug: 159815595
Test: cert/run --host
Change-Id: I758d69ff5ab7bb6029444aa699fc4d638ba21a2a
diff --git a/system/gd/common/init_flags.fbs b/system/gd/common/init_flags.fbs
index c13054d..69c4077 100644
--- a/system/gd/common/init_flags.fbs
+++ b/system/gd/common/init_flags.fbs
@@ -5,6 +5,7 @@
 table InitFlagsData {
     title:string;
     gd_advertising_enabled:bool;
+    gd_scanning_enabled:bool;
     gd_security_enabled:bool;
     gd_acl_enabled:bool;
     gd_hci_enabled:bool;
diff --git a/system/gd/dumpsys/init_flags.cc b/system/gd/dumpsys/init_flags.cc
index 3c13ad9..7833423 100644
--- a/system/gd/dumpsys/init_flags.cc
+++ b/system/gd/dumpsys/init_flags.cc
@@ -24,6 +24,7 @@
   common::InitFlagsDataBuilder builder(*fb_builder);
   builder.add_title(title);
   builder.add_gd_advertising_enabled(bluetooth::common::init_flags::gd_advertising_is_enabled());
+  builder.add_gd_scanning_enabled(bluetooth::common::init_flags::gd_scanning_is_enabled());
   builder.add_gd_security_enabled(bluetooth::common::init_flags::gd_security_is_enabled());
   builder.add_gd_acl_enabled(bluetooth::common::init_flags::gd_acl_is_enabled());
   builder.add_gd_hci_enabled(bluetooth::common::init_flags::gd_hci_is_enabled());
diff --git a/system/gd/rust/common/src/init_flags.rs b/system/gd/rust/common/src/init_flags.rs
index 9367f90..4016db60 100644
--- a/system/gd/rust/common/src/init_flags.rs
+++ b/system/gd/rust/common/src/init_flags.rs
@@ -77,6 +77,7 @@
     flags: {
         gd_core,
         gd_advertising,
+        gd_scanning,
         gd_security,
         gd_acl,
         gd_l2cap,
diff --git a/system/gd/rust/shim/src/init_flags.rs b/system/gd/rust/shim/src/init_flags.rs
index e598af6..282c1d9 100644
--- a/system/gd/rust/shim/src/init_flags.rs
+++ b/system/gd/rust/shim/src/init_flags.rs
@@ -7,6 +7,7 @@
         fn gd_core_is_enabled() -> bool;
         fn gd_security_is_enabled() -> bool;
         fn gd_advertising_is_enabled() -> bool;
+        fn gd_scanning_is_enabled() -> bool;
         fn gd_acl_is_enabled() -> bool;
         fn gd_l2cap_is_enabled() -> bool;
         fn gd_hci_is_enabled() -> bool;
diff --git a/system/main/shim/hci_layer.cc b/system/main/shim/hci_layer.cc
index 2f0bd18..9e6bffe 100644
--- a/system/main/shim/hci_layer.cc
+++ b/system/main/shim/hci_layer.cc
@@ -264,23 +264,24 @@
     case bluetooth::hci::SubeventCode::REMOTE_CONNECTION_PARAMETER_REQUEST:
       return bluetooth::shim::is_gd_acl_enabled() ||
              bluetooth::shim::is_gd_l2cap_enabled() ||
-             bluetooth::shim::is_gd_advertising_enabled();
+             bluetooth::shim::is_gd_advertising_enabled() ||
+             bluetooth::shim::is_gd_scanning_enabled();
     case bluetooth::hci::SubeventCode::ADVERTISING_SET_TERMINATED:
     case bluetooth::hci::SubeventCode::SCAN_REQUEST_RECEIVED:
       return bluetooth::shim::is_gd_acl_enabled() ||
              bluetooth::shim::is_gd_l2cap_enabled() ||
              bluetooth::shim::is_gd_advertising_enabled();
+    case bluetooth::hci::SubeventCode::SCAN_TIMEOUT:
     case bluetooth::hci::SubeventCode::ADVERTISING_REPORT:
     case bluetooth::hci::SubeventCode::DIRECTED_ADVERTISING_REPORT:
     case bluetooth::hci::SubeventCode::EXTENDED_ADVERTISING_REPORT:
     case bluetooth::hci::SubeventCode::PERIODIC_ADVERTISING_REPORT:
     case bluetooth::hci::SubeventCode::PERIODIC_ADVERTISING_SYNC_ESTABLISHED:
     case bluetooth::hci::SubeventCode::PERIODIC_ADVERTISING_SYNC_LOST:
-      // TODO return bluetooth::shim::is_gd_scanning_enabled();
+      return bluetooth::shim::is_gd_scanning_enabled();
     case bluetooth::hci::SubeventCode::READ_REMOTE_FEATURES_COMPLETE:
     case bluetooth::hci::SubeventCode::READ_LOCAL_P256_PUBLIC_KEY_COMPLETE:
     case bluetooth::hci::SubeventCode::GENERATE_DHKEY_COMPLETE:
-    case bluetooth::hci::SubeventCode::SCAN_TIMEOUT:
     case bluetooth::hci::SubeventCode::CHANNEL_SELECTION_ALGORITHM:
     case bluetooth::hci::SubeventCode::CONNECTIONLESS_IQ_REPORT:
     case bluetooth::hci::SubeventCode::CONNECTION_IQ_REPORT:
@@ -313,6 +314,16 @@
   return false;
 }
 
+static bool event_already_registered_in_le_scanning_manager(
+    bluetooth::hci::EventCode event_code) {
+  for (auto event : bluetooth::hci::AclConnectionEvents) {
+    if (event == event_code) {
+      return bluetooth::shim::is_gd_scanning_enabled();
+    }
+  }
+  return false;
+}
+
 }  // namespace
 
 namespace cpp {
@@ -490,9 +501,10 @@
 static void register_for_acl() {
   hci_queue_end = bluetooth::shim::GetHciLayer()->GetAclQueueEnd();
 
-  // if gd advertising enabled, hci_queue_end will be register in
+  // if gd advertising/scanning enabled, hci_queue_end will be register in
   // AclManager::impl::Start
   if (!bluetooth::shim::is_gd_advertising_enabled() &&
+      !bluetooth::shim::is_gd_scanning_enabled() &&
       !bluetooth::shim::is_gd_l2cap_enabled()) {
     hci_queue_end->RegisterDequeue(bluetooth::shim::GetGdShimHandler(),
                                    bluetooth::common::Bind(acl_data_callback));
@@ -523,6 +535,8 @@
       } else if (event_already_registered_in_le_advertising_manager(
                      event_code)) {
         continue;
+      } else if (event_already_registered_in_le_scanning_manager(event_code)) {
+        continue;
       }
       bluetooth::shim::GetHciLayer()->UnregisterEventHandler(event_code);
     }
@@ -707,6 +721,8 @@
       continue;
     } else if (event_already_registered_in_le_advertising_manager(event_code)) {
       continue;
+    } else if (event_already_registered_in_le_scanning_manager(event_code)) {
+      continue;
     }
 
     if (bluetooth::common::init_flags::gd_rust_is_enabled()) {
diff --git a/system/main/shim/shim.cc b/system/main/shim/shim.cc
index 973bc75..598ec3f 100644
--- a/system/main/shim/shim.cc
+++ b/system/main/shim/shim.cc
@@ -58,6 +58,12 @@
   return bluetooth::common::init_flags::gd_advertising_is_enabled();
 }
 
+bool bluetooth::shim::is_gd_scanning_enabled() {
+  // TODO enable when scanning module ready
+  // return bluetooth::common::init_flags::gd_scanning_is_enabled();
+  return false;
+}
+
 bool bluetooth::shim::is_gd_security_enabled() {
   return bluetooth::common::init_flags::gd_security_is_enabled();
 }
diff --git a/system/main/shim/shim.h b/system/main/shim/shim.h
index 7ce0f72..9ac63ff 100644
--- a/system/main/shim/shim.h
+++ b/system/main/shim/shim.h
@@ -46,6 +46,7 @@
  * @return true if using gd shim core, false if using legacy.
  */
 bool is_gd_advertising_enabled();
+bool is_gd_scanning_enabled();
 bool is_gd_security_enabled();
 bool is_gd_acl_enabled();
 bool is_gd_hci_enabled();
diff --git a/system/main/shim/stack.cc b/system/main/shim/stack.cc
index e1982d4..7fa8efe 100644
--- a/system/main/shim/stack.cc
+++ b/system/main/shim/stack.cc
@@ -102,9 +102,11 @@
   if (common::init_flags::gd_advertising_is_enabled()) {
     modules.add<hci::LeAdvertisingManager>();
   }
+  if (common::init_flags::gd_scanning_is_enabled()) {
+    modules.add<hci::LeScanningManager>();
+  }
   if (common::init_flags::gd_core_is_enabled()) {
     modules.add<att::AttModule>();
-    modules.add<hci::LeScanningManager>();
     modules.add<neighbor::ConnectabilityModule>();
     modules.add<neighbor::DiscoverabilityModule>();
     modules.add<neighbor::InquiryModule>();
diff --git a/system/stack/test/common/mock_main_shim.cc b/system/stack/test/common/mock_main_shim.cc
index ee6ef30..ba1f038 100644
--- a/system/stack/test/common/mock_main_shim.cc
+++ b/system/stack/test/common/mock_main_shim.cc
@@ -48,6 +48,10 @@
   mock_function_count_map[__func__]++;
   return false;
 }
+bool bluetooth::shim::is_gd_scanning_enabled() {
+  mock_function_count_map[__func__]++;
+  return false;
+}
 bool bluetooth::shim::is_gd_controller_enabled() {
   mock_function_count_map[__func__]++;
   return false;