IA2: Break out module dumper from module definition

Module dumper works *with* modules but is not a module

Module should have no knowledge of wakelocks

Bug: 304519090
Test: m .

Change-Id: I8122c1f47d204e107d410760c6b1f726025a3976
diff --git a/system/gd/Android.bp b/system/gd/Android.bp
index c3d3815..9bf92b7 100644
--- a/system/gd/Android.bp
+++ b/system/gd/Android.bp
@@ -148,6 +148,7 @@
         ":BluetoothStorageSources",
         ":BluetoothSyspropsSources",
         "module.cc",
+        "module_dumper.cc",
         "stack_manager.cc",
     ],
     generated_headers: [
diff --git a/system/gd/BUILD.gn b/system/gd/BUILD.gn
index 3697efa..7a72d9f 100644
--- a/system/gd/BUILD.gn
+++ b/system/gd/BUILD.gn
@@ -55,6 +55,7 @@
 static_library("libbluetooth_gd") {
   sources = [
     "module.cc",
+    "module_dumper.cc",
     "stack_manager.cc",
   ]
 
diff --git a/system/gd/hci/controller_test.cc b/system/gd/hci/controller_test.cc
index b3b56dd..1de5a4c 100644
--- a/system/gd/hci/controller_test.cc
+++ b/system/gd/hci/controller_test.cc
@@ -18,25 +18,21 @@
 
 #include <gtest/gtest.h>
 
-#include <algorithm>
 #include <chrono>
 #include <future>
-#include <map>
 #include <memory>
 
 #include "common/bind.h"
-#include "common/callback.h"
 #include "common/init_flags.h"
 #include "hci/address.h"
 #include "hci/hci_layer.h"
+#include "module_dumper.h"
 #include "os/thread.h"
 #include "packet/raw_builder.h"
 
 using namespace bluetooth;
 using namespace std::chrono_literals;
 
-using common::BidiQueue;
-using common::BidiQueueEnd;
 using packet::kLittleEndian;
 using packet::PacketView;
 using packet::RawBuilder;
diff --git a/system/gd/module.cc b/system/gd/module.cc
index e1b3812..a5bb92e 100644
--- a/system/gd/module.cc
+++ b/system/gd/module.cc
@@ -18,11 +18,9 @@
 #include "module.h"
 
 #include "common/init_flags.h"
-#include "os/wakelock_manager.h"
 
 using ::bluetooth::os::Handler;
 using ::bluetooth::os::Thread;
-using ::bluetooth::os::WakelockManager;
 
 namespace bluetooth {
 
@@ -135,45 +133,4 @@
   return nullptr;
 }
 
-void ModuleDumper::DumpState(std::string* output) const {
-  ASSERT(output != nullptr);
-
-  flatbuffers::FlatBufferBuilder builder(1024);
-  auto title = builder.CreateString(title_);
-
-  common::InitFlagsDataBuilder init_flags_builder(builder);
-  init_flags_builder.add_title(builder.CreateString("----- Init Flags -----"));
-  std::vector<flatbuffers::Offset<common::InitFlagValue>> flags;
-  for (const auto& flag : common::init_flags::dump()) {
-    flags.push_back(common::CreateInitFlagValue(
-        builder,
-        builder.CreateString(std::string(flag.flag)),
-        builder.CreateString(std::string(flag.value))));
-  }
-  init_flags_builder.add_values(builder.CreateVector(flags));
-  auto init_flags_offset = init_flags_builder.Finish();
-
-  auto wakelock_offset = WakelockManager::Get().GetDumpsysData(&builder);
-
-  std::queue<DumpsysDataFinisher> queue;
-  for (auto it = module_registry_.start_order_.rbegin(); it != module_registry_.start_order_.rend(); it++) {
-    auto instance = module_registry_.started_modules_.find(*it);
-    ASSERT(instance != module_registry_.started_modules_.end());
-    queue.push(instance->second->GetDumpsysData(&builder));
-  }
-
-  DumpsysDataBuilder data_builder(builder);
-  data_builder.add_title(title);
-  data_builder.add_init_flags(init_flags_offset);
-  data_builder.add_wakelock_manager_data(wakelock_offset);
-
-  while (!queue.empty()) {
-    queue.front()(&data_builder);
-    queue.pop();
-  }
-
-  builder.Finish(data_builder.Finish());
-  *output = std::string(builder.GetBufferPointer(), builder.GetBufferPointer() + builder.GetSize());
-}
-
 }  // namespace bluetooth
diff --git a/system/gd/module.h b/system/gd/module.h
index 7695f1a..7083fa0 100644
--- a/system/gd/module.h
+++ b/system/gd/module.h
@@ -161,17 +161,6 @@
   std::string last_instance_;
 };
 
-class ModuleDumper {
- public:
-  ModuleDumper(const ModuleRegistry& module_registry, const char* title)
-      : module_registry_(module_registry), title_(title) {}
-  void DumpState(std::string* output) const;
-
- private:
-  const ModuleRegistry& module_registry_;
-  const std::string title_;
-};
-
 class TestModuleRegistry : public ModuleRegistry {
  public:
   void InjectTestModule(const ModuleFactory* module, Module* instance) {
diff --git a/system/gd/module_dumper.cc b/system/gd/module_dumper.cc
new file mode 100644
index 0000000..220d366
--- /dev/null
+++ b/system/gd/module_dumper.cc
@@ -0,0 +1,70 @@
+/*
+ * Copyright 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#define LOG_TAG "BtGdModule"
+
+#include "module_dumper.h"
+
+#include "common/init_flags.h"
+#include "module.h"
+#include "os/wakelock_manager.h"
+
+using ::bluetooth::os::WakelockManager;
+
+namespace bluetooth {
+
+void ModuleDumper::DumpState(std::string* output) const {
+  ASSERT(output != nullptr);
+
+  flatbuffers::FlatBufferBuilder builder(1024);
+  auto title = builder.CreateString(title_);
+
+  common::InitFlagsDataBuilder init_flags_builder(builder);
+  init_flags_builder.add_title(builder.CreateString("----- Init Flags -----"));
+  std::vector<flatbuffers::Offset<common::InitFlagValue>> flags;
+  for (const auto& flag : common::init_flags::dump()) {
+    flags.push_back(common::CreateInitFlagValue(
+        builder,
+        builder.CreateString(std::string(flag.flag)),
+        builder.CreateString(std::string(flag.value))));
+  }
+  init_flags_builder.add_values(builder.CreateVector(flags));
+  auto init_flags_offset = init_flags_builder.Finish();
+
+  auto wakelock_offset = WakelockManager::Get().GetDumpsysData(&builder);
+
+  std::queue<DumpsysDataFinisher> queue;
+  for (auto it = module_registry_.start_order_.rbegin(); it != module_registry_.start_order_.rend();
+       it++) {
+    auto instance = module_registry_.started_modules_.find(*it);
+    ASSERT(instance != module_registry_.started_modules_.end());
+    queue.push(instance->second->GetDumpsysData(&builder));
+  }
+
+  DumpsysDataBuilder data_builder(builder);
+  data_builder.add_title(title);
+  data_builder.add_init_flags(init_flags_offset);
+  data_builder.add_wakelock_manager_data(wakelock_offset);
+
+  while (!queue.empty()) {
+    queue.front()(&data_builder);
+    queue.pop();
+  }
+
+  builder.Finish(data_builder.Finish());
+  *output = std::string(builder.GetBufferPointer(), builder.GetBufferPointer() + builder.GetSize());
+}
+
+}  // namespace bluetooth
diff --git a/system/gd/module_dumper.h b/system/gd/module_dumper.h
new file mode 100644
index 0000000..95863c9
--- /dev/null
+++ b/system/gd/module_dumper.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <string>
+
+namespace bluetooth {
+
+class ModuleRegistry;
+
+class ModuleDumper {
+ public:
+  ModuleDumper(const ModuleRegistry& module_registry, const char* title)
+      : module_registry_(module_registry), title_(title) {}
+  void DumpState(std::string* output) const;
+
+ private:
+  const ModuleRegistry& module_registry_;
+  const std::string title_;
+};
+
+}  // namespace bluetooth
diff --git a/system/gd/module_unittest.cc b/system/gd/module_unittest.cc
index 3f9d9cd..b558983 100644
--- a/system/gd/module_unittest.cc
+++ b/system/gd/module_unittest.cc
@@ -15,16 +15,16 @@
  */
 
 #include "module.h"
+
+#include <functional>
+#include <string>
+
+#include "gtest/gtest.h"
+#include "module_dumper.h"
 #include "module_unittest_generated.h"
 #include "os/handler.h"
 #include "os/thread.h"
 
-#include "gtest/gtest.h"
-
-#include <functional>
-#include <future>
-#include <string>
-
 using ::bluetooth::os::Thread;
 
 namespace bluetooth {
diff --git a/system/gd/shim/dumpsys.cc b/system/gd/shim/dumpsys.cc
index a1df110..b8c785b 100644
--- a/system/gd/shim/dumpsys.cc
+++ b/system/gd/shim/dumpsys.cc
@@ -22,6 +22,7 @@
 
 #include "dumpsys/filter.h"
 #include "module.h"
+#include "module_dumper.h"
 #include "os/log.h"
 #include "os/system_properties.h"
 #include "shim/dumpsys.h"