diff options
author | 2024-12-18 15:25:19 -0800 | |
---|---|---|
committer | 2024-12-19 00:21:25 +0000 | |
commit | 95f43babee4d3dcd63c0bc4d2aeb560f4104b022 (patch) | |
tree | 9117de333421e98ddb8f086083f1defdd564a6ff | |
parent | 0f1bd20ec62508d53c669b10faa543c468c1a4c4 (diff) |
system/gd: Merge StackManager into shim::Stack
StackManager is no longer used as an indenpendent object
and its functionalities can be added to shim::Stack.
Bug: 333555245
Test: m com.android.btservices
Flag: EXEMPT, no logical change
Change-Id: If1422266331d483bbf1412e38530d9e03227cd3c
-rw-r--r-- | system/gd/Android.bp | 2 | ||||
-rw-r--r-- | system/gd/BUILD.gn | 1 | ||||
-rw-r--r-- | system/gd/module.h | 5 | ||||
-rw-r--r-- | system/gd/stack_manager.cc | 105 | ||||
-rw-r--r-- | system/gd/stack_manager.h | 50 | ||||
-rw-r--r-- | system/gd/stack_manager_unittest.cc | 58 | ||||
-rw-r--r-- | system/main/shim/acl_api.cc | 1 | ||||
-rw-r--r-- | system/main/shim/entry.cc | 30 | ||||
-rw-r--r-- | system/main/shim/stack.cc | 101 | ||||
-rw-r--r-- | system/main/shim/stack.h | 25 | ||||
-rw-r--r-- | system/test/mock/mock_main_shim_stack.cc | 4 |
11 files changed, 117 insertions, 265 deletions
diff --git a/system/gd/Android.bp b/system/gd/Android.bp index 316c0826de..6b8825c1ab 100644 --- a/system/gd/Android.bp +++ b/system/gd/Android.bp @@ -135,7 +135,6 @@ cc_defaults { ":BluetoothPacketSources", ":BluetoothStorageSources", "module.cc", - "stack_manager.cc", ], shared_libs: [ "libcrypto", @@ -338,7 +337,6 @@ cc_test { ":TestCommonMockFunctions", ":TestMockStackMetrics", "module_unittest.cc", - "stack_manager_unittest.cc", ], static_libs: [ "bluetooth_flags_c_lib_for_test", diff --git a/system/gd/BUILD.gn b/system/gd/BUILD.gn index 529a252e01..2ee45fde74 100644 --- a/system/gd/BUILD.gn +++ b/system/gd/BUILD.gn @@ -53,7 +53,6 @@ group("gd_default_deps") { static_library("libbluetooth_gd") { sources = [ "module.cc", - "stack_manager.cc", ] include_dirs = [ "." ] diff --git a/system/gd/module.h b/system/gd/module.h index 80b2369edf..e708035a77 100644 --- a/system/gd/module.h +++ b/system/gd/module.h @@ -32,6 +32,9 @@ #include "os/thread.h" namespace bluetooth { +namespace shim { +class Stack; +} // namespace shim class Module; class ModuleRegistry; @@ -122,7 +125,7 @@ private: class ModuleRegistry { friend Module; - friend class StackManager; + friend shim::Stack; public: template <class T> diff --git a/system/gd/stack_manager.cc b/system/gd/stack_manager.cc deleted file mode 100644 index 856d10a3c4..0000000000 --- a/system/gd/stack_manager.cc +++ /dev/null @@ -1,105 +0,0 @@ -/* - * 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. - */ - -#include "stack_manager.h" - -#include <bluetooth/log.h> - -#include <chrono> -#include <future> -#include <queue> - -#include "common/bind.h" -#include "module.h" -#include "os/handler.h" -#include "os/system_properties.h" -#include "os/thread.h" -#include "os/wakelock_manager.h" - -using ::bluetooth::os::Handler; -using ::bluetooth::os::Thread; -using ::bluetooth::os::WakelockManager; - -namespace bluetooth { - -void StackManager::StartUp(ModuleList* modules, Thread* stack_thread) { - management_thread_ = new Thread("management_thread", Thread::Priority::NORMAL); - handler_ = new Handler(management_thread_); - - WakelockManager::Get().Acquire(); - - std::promise<void> promise; - auto future = promise.get_future(); - handler_->Post(common::BindOnce(&StackManager::handle_start_up, common::Unretained(this), modules, - stack_thread, std::move(promise))); - - auto init_status = future.wait_for( - std::chrono::milliseconds(get_gd_stack_timeout_ms(/* is_start = */ true))); - - WakelockManager::Get().Release(); - - log::info("init_status == {}", int(init_status)); - - log::assert_that(init_status == std::future_status::ready, "Can't start stack, last instance: {}", - registry_.last_instance_); - - log::info("init complete"); -} - -void StackManager::handle_start_up(ModuleList* modules, Thread* stack_thread, - std::promise<void> promise) { - registry_.Start(modules, stack_thread); - promise.set_value(); -} - -void StackManager::ShutDown() { - WakelockManager::Get().Acquire(); - - std::promise<void> promise; - auto future = promise.get_future(); - handler_->Post(common::BindOnce(&StackManager::handle_shut_down, common::Unretained(this), - std::move(promise))); - - auto stop_status = future.wait_for( - std::chrono::milliseconds(get_gd_stack_timeout_ms(/* is_start = */ false))); - - WakelockManager::Get().Release(); - WakelockManager::Get().CleanUp(); - - log::assert_that(stop_status == std::future_status::ready, "Can't stop stack, last instance: {}", - registry_.last_instance_); - - handler_->Clear(); - handler_->WaitUntilStopped(std::chrono::milliseconds(2000)); - delete handler_; - delete management_thread_; -} - -void StackManager::handle_shut_down(std::promise<void> promise) { - registry_.StopAll(); - promise.set_value(); -} - -std::chrono::milliseconds StackManager::get_gd_stack_timeout_ms(bool is_start) { - auto gd_timeout = os::GetSystemPropertyUint32( - is_start ? "bluetooth.gd.start_timeout" : "bluetooth.gd.stop_timeout", - /* default_value = */ is_start ? 3000 : 5000); - return std::chrono::milliseconds(gd_timeout * - os::GetSystemPropertyUint32("ro.hw_timeout_multiplier", - /* default_value = */ 1)); -} - -} // namespace bluetooth diff --git a/system/gd/stack_manager.h b/system/gd/stack_manager.h deleted file mode 100644 index 892efc3c3b..0000000000 --- a/system/gd/stack_manager.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - * 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 "module.h" -#include "os/handler.h" -#include "os/thread.h" - -namespace bluetooth { - -class StackManager { -public: - void StartUp(ModuleList* modules, os::Thread* stack_thread); - void ShutDown(); - - template <class T> - T* GetInstance() const { - return static_cast<T*>(registry_.Get(&T::Factory)); - } - - template <class T> - bool IsStarted() const { - return registry_.IsStarted(&T::Factory); - } - -private: - os::Thread* management_thread_ = nullptr; - os::Handler* handler_ = nullptr; - ModuleRegistry registry_; - - void handle_start_up(ModuleList* modules, os::Thread* stack_thread, std::promise<void> promise); - void handle_shut_down(std::promise<void> promise); - static std::chrono::milliseconds get_gd_stack_timeout_ms(bool is_start); -}; - -} // namespace bluetooth diff --git a/system/gd/stack_manager_unittest.cc b/system/gd/stack_manager_unittest.cc deleted file mode 100644 index f3fb3b5c82..0000000000 --- a/system/gd/stack_manager_unittest.cc +++ /dev/null @@ -1,58 +0,0 @@ -/* - * 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. - */ - -#include "stack_manager.h" - -#include "gtest/gtest.h" -#include "os/thread.h" - -namespace bluetooth { -namespace { - -TEST(StackManagerTest, DISABLED_start_and_shutdown_no_module) { - StackManager stack_manager; - ModuleList module_list; - os::Thread thread{"test_thread", os::Thread::Priority::NORMAL}; - stack_manager.StartUp(&module_list, &thread); - stack_manager.ShutDown(); -} - -class TestModuleNoDependency : public Module { -public: - static const ModuleFactory Factory; - -protected: - void ListDependencies(ModuleList* /* list */) const {} - void Start() override {} - void Stop() override {} - std::string ToString() const override { return std::string("TestModuleDep"); } -}; - -const ModuleFactory TestModuleNoDependency::Factory = - ModuleFactory([]() { return new TestModuleNoDependency(); }); - -TEST(StackManagerTest, DISABLED_get_module_instance) { - StackManager stack_manager; - ModuleList module_list; - module_list.add<TestModuleNoDependency>(); - os::Thread thread{"test_thread", os::Thread::Priority::NORMAL}; - stack_manager.StartUp(&module_list, &thread); - EXPECT_NE(stack_manager.GetInstance<TestModuleNoDependency>(), nullptr); - stack_manager.ShutDown(); -} - -} // namespace -} // namespace bluetooth diff --git a/system/main/shim/acl_api.cc b/system/main/shim/acl_api.cc index 90d9c7b9f5..08249592e7 100644 --- a/system/main/shim/acl_api.cc +++ b/system/main/shim/acl_api.cc @@ -111,7 +111,6 @@ void bluetooth::shim::ACL_ConfigureLePrivacy(bool is_le_privacy_enabled) { android::sysprop::bluetooth::Ble::random_address_rotation_interval_max().value_or(15)); Stack::GetInstance() - ->GetStackManager() ->GetInstance<bluetooth::hci::AclManager>() ->SetPrivacyPolicyForInitiatorAddress(address_policy, empty_address_with_type, minimum_rotation_time, maximum_rotation_time); diff --git a/system/main/shim/entry.cc b/system/main/shim/entry.cc index 8d16bf8a46..ff0014b481 100644 --- a/system/main/shim/entry.cc +++ b/system/main/shim/entry.cc @@ -38,51 +38,45 @@ namespace shim { os::Handler* GetGdShimHandler() { return Stack::GetInstance()->GetHandler(); } hci::LeAdvertisingManager* GetAdvertising() { - return Stack::GetInstance()->GetStackManager()->GetInstance<hci::LeAdvertisingManager>(); + return Stack::GetInstance()->GetInstance<hci::LeAdvertisingManager>(); } hci::ControllerInterface* GetController() { - return Stack::GetInstance()->GetStackManager()->GetInstance<hci::Controller>(); + return Stack::GetInstance()->GetInstance<hci::Controller>(); } -hci::HciInterface* GetHciLayer() { - return Stack::GetInstance()->GetStackManager()->GetInstance<hci::HciLayer>(); -} +hci::HciInterface* GetHciLayer() { return Stack::GetInstance()->GetInstance<hci::HciLayer>(); } hci::RemoteNameRequestModule* GetRemoteNameRequest() { - return Stack::GetInstance()->GetStackManager()->GetInstance<hci::RemoteNameRequestModule>(); + return Stack::GetInstance()->GetInstance<hci::RemoteNameRequestModule>(); } hci::LeScanningManager* GetScanning() { - return Stack::GetInstance()->GetStackManager()->GetInstance<hci::LeScanningManager>(); + return Stack::GetInstance()->GetInstance<hci::LeScanningManager>(); } hci::DistanceMeasurementManager* GetDistanceMeasurementManager() { - return Stack::GetInstance()->GetStackManager()->GetInstance<hci::DistanceMeasurementManager>(); + return Stack::GetInstance()->GetInstance<hci::DistanceMeasurementManager>(); } -hal::SnoopLogger* GetSnoopLogger() { - return Stack::GetInstance()->GetStackManager()->GetInstance<hal::SnoopLogger>(); -} +hal::SnoopLogger* GetSnoopLogger() { return Stack::GetInstance()->GetInstance<hal::SnoopLogger>(); } lpp::LppOffloadInterface* GetLppOffloadManager() { - return Stack::GetInstance()->GetStackManager()->GetInstance<lpp::LppOffloadManager>(); + return Stack::GetInstance()->GetInstance<lpp::LppOffloadManager>(); } storage::StorageModule* GetStorage() { - return Stack::GetInstance()->GetStackManager()->GetInstance<storage::StorageModule>(); + return Stack::GetInstance()->GetInstance<storage::StorageModule>(); } -hci::AclManager* GetAclManager() { - return Stack::GetInstance()->GetStackManager()->GetInstance<hci::AclManager>(); -} +hci::AclManager* GetAclManager() { return Stack::GetInstance()->GetInstance<hci::AclManager>(); } metrics::CounterMetrics* GetCounterMetrics() { - return Stack::GetInstance()->GetStackManager()->GetInstance<metrics::CounterMetrics>(); + return Stack::GetInstance()->GetInstance<metrics::CounterMetrics>(); } hci::MsftExtensionManager* GetMsftExtensionManager() { - return Stack::GetInstance()->GetStackManager()->GetInstance<hci::MsftExtensionManager>(); + return Stack::GetInstance()->GetInstance<hci::MsftExtensionManager>(); } } // namespace shim diff --git a/system/main/shim/stack.cc b/system/main/shim/stack.cc index 969cc3ac6a..6a2b897d2e 100644 --- a/system/main/shim/stack.cc +++ b/system/main/shim/stack.cc @@ -23,6 +23,9 @@ #include <fcntl.h> #include <unistd.h> +#include <chrono> +#include <future> +#include <queue> #include <string> #include "common/strings.h" @@ -47,6 +50,7 @@ #include "main/shim/le_advertising_manager.h" #include "main/shim/le_scanning_manager.h" #include "metrics/counter_metrics.h" +#include "os/system_properties.h" #include "os/wakelock_manager.h" #include "storage/storage_module.h" @@ -54,11 +58,13 @@ #include "sysprops/sysprops_module.h" #endif +using ::bluetooth::os::Handler; +using ::bluetooth::os::Thread; +using ::bluetooth::os::WakelockManager; + namespace bluetooth { namespace shim { -using ::bluetooth::common::StringFormat; - struct Stack::impl { Acl* acl_ = nullptr; }; @@ -99,10 +105,9 @@ void Stack::StartEverything() { Start(&modules); is_running_ = true; // Make sure the leaf modules are started - log::assert_that(stack_manager_.GetInstance<storage::StorageModule>() != nullptr, - "assert failed: stack_manager_.GetInstance<storage::StorageModule>() != " - "nullptr"); - if (stack_manager_.IsStarted<hci::Controller>()) { + log::assert_that(GetInstance<storage::StorageModule>() != nullptr, + "assert failed: GetInstance<storage::StorageModule>() != nullptr"); + if (IsStarted<hci::Controller>()) { pimpl_->acl_ = new Acl(stack_handler_, GetAclInterface(), GetController()->GetLeFilterAcceptListSize(), GetController()->GetLeResolvingListSize()); @@ -122,7 +127,7 @@ void Stack::StartModuleStack(const ModuleList* modules, const os::Thread* thread stack_thread_ = const_cast<os::Thread*>(thread); log::info("Starting Gd stack"); - stack_manager_.StartUp(const_cast<ModuleList*>(modules), stack_thread_); + StartUp(const_cast<ModuleList*>(modules), stack_thread_); stack_handler_ = new os::Handler(stack_thread_); num_modules_ = modules->NumModules(); @@ -134,7 +139,7 @@ void Stack::Start(ModuleList* modules) { log::info("Starting Gd stack"); stack_thread_ = new os::Thread("gd_stack_thread", os::Thread::Priority::REAL_TIME); - stack_manager_.StartUp(modules, stack_thread_); + StartUp(modules, stack_thread_); stack_handler_ = new os::Handler(stack_thread_); @@ -157,7 +162,7 @@ void Stack::Stop() { stack_handler_->Clear(); - stack_manager_.ShutDown(); + ShutDown(); delete stack_handler_; stack_handler_ = nullptr; @@ -174,18 +179,6 @@ bool Stack::IsRunning() { return is_running_; } -StackManager* Stack::GetStackManager() { - std::lock_guard<std::recursive_mutex> lock(mutex_); - log::assert_that(is_running_, "assert failed: is_running_"); - return &stack_manager_; -} - -const StackManager* Stack::GetStackManager() const { - std::lock_guard<std::recursive_mutex> lock(mutex_); - log::assert_that(is_running_, "assert failed: is_running_"); - return &stack_manager_; -} - Acl* Stack::GetAcl() { std::lock_guard<std::recursive_mutex> lock(mutex_); log::assert_that(is_running_, "assert failed: is_running_"); @@ -216,5 +209,71 @@ void Stack::Dump(int fd, std::promise<void> promise) const { } } +void Stack::StartUp(ModuleList* modules, Thread* stack_thread) { + management_thread_ = new Thread("management_thread", Thread::Priority::NORMAL); + handler_ = new Handler(management_thread_); + + WakelockManager::Get().Acquire(); + + std::promise<void> promise; + auto future = promise.get_future(); + handler_->Post(common::BindOnce(&Stack::handle_start_up, common::Unretained(this), modules, + stack_thread, std::move(promise))); + + auto init_status = future.wait_for( + std::chrono::milliseconds(get_gd_stack_timeout_ms(/* is_start = */ true))); + + WakelockManager::Get().Release(); + + log::info("init_status == {}", int(init_status)); + + log::assert_that(init_status == std::future_status::ready, "Can't start stack, last instance: {}", + registry_.last_instance_); + + log::info("init complete"); +} + +void Stack::handle_start_up(ModuleList* modules, Thread* stack_thread, std::promise<void> promise) { + registry_.Start(modules, stack_thread); + promise.set_value(); +} + +void Stack::ShutDown() { + WakelockManager::Get().Acquire(); + + std::promise<void> promise; + auto future = promise.get_future(); + handler_->Post( + common::BindOnce(&Stack::handle_shut_down, common::Unretained(this), std::move(promise))); + + auto stop_status = future.wait_for( + std::chrono::milliseconds(get_gd_stack_timeout_ms(/* is_start = */ false))); + + WakelockManager::Get().Release(); + WakelockManager::Get().CleanUp(); + + log::assert_that(stop_status == std::future_status::ready, "Can't stop stack, last instance: {}", + registry_.last_instance_); + + handler_->Clear(); + handler_->WaitUntilStopped(std::chrono::milliseconds(2000)); + delete handler_; + delete management_thread_; +} + +void Stack::handle_shut_down(std::promise<void> promise) { + registry_.StopAll(); + promise.set_value(); +} + +std::chrono::milliseconds Stack::get_gd_stack_timeout_ms(bool is_start) { + auto gd_timeout = os::GetSystemPropertyUint32( + is_start ? "bluetooth.gd.start_timeout" : "bluetooth.gd.stop_timeout", + /* default_value = */ is_start ? 3000 : 5000); + return std::chrono::milliseconds(gd_timeout * + os::GetSystemPropertyUint32("ro.hw_timeout_multiplier", + /* default_value = */ 1)); +} + } // namespace shim } // namespace bluetooth diff --git a/system/main/shim/stack.h b/system/main/shim/stack.h index 2f53941eb1..728f32ee66 100644 --- a/system/main/shim/stack.h +++ b/system/main/shim/stack.h @@ -22,7 +22,6 @@ #include "module.h" #include "os/handler.h" #include "os/thread.h" -#include "stack_manager.h" // The shim layer implementation on the Gd stack side. namespace bluetooth { @@ -48,8 +47,15 @@ public: void Stop(); bool IsRunning(); - StackManager* GetStackManager(); - const StackManager* GetStackManager() const; + template <class T> + T* GetInstance() const { + return static_cast<T*>(registry_.Get(&T::Factory)); + } + + template <class T> + bool IsStarted() const { + return registry_.IsStarted(&T::Factory); + } Acl* GetAcl(); @@ -67,11 +73,22 @@ private: std::shared_ptr<impl> pimpl_; mutable std::recursive_mutex mutex_; - StackManager stack_manager_; bool is_running_ = false; os::Thread* stack_thread_ = nullptr; os::Handler* stack_handler_ = nullptr; size_t num_modules_{0}; + + void StartUp(ModuleList* modules, os::Thread* stack_thread); + void ShutDown(); + + os::Thread* management_thread_ = nullptr; + os::Handler* handler_ = nullptr; + ModuleRegistry registry_; + + void handle_start_up(ModuleList* modules, os::Thread* stack_thread, std::promise<void> promise); + void handle_shut_down(std::promise<void> promise); + static std::chrono::milliseconds get_gd_stack_timeout_ms(bool is_start); + void Start(ModuleList* modules); }; diff --git a/system/test/mock/mock_main_shim_stack.cc b/system/test/mock/mock_main_shim_stack.cc index f53120c865..03ec09a1b3 100644 --- a/system/test/mock/mock_main_shim_stack.cc +++ b/system/test/mock/mock_main_shim_stack.cc @@ -37,10 +37,6 @@ void Stack::Stop() {} bool Stack::IsRunning() { return stack_thread_ != nullptr; } -StackManager* Stack::GetStackManager() { return nullptr; } - -const StackManager* Stack::GetStackManager() const { return nullptr; } - Acl* Stack::GetAcl() { return testing::acl_; } os::Handler* Stack::GetHandler() { return stack_handler_; } |