blob: f12da973dd5d3d6cfdb82c9173d9e6e517a44466 [file] [log] [blame]
Zach Johnson429c94b2019-04-25 22:24:54 -07001/*
2 * Copyright 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "module.h"
18
Zach Johnson7ea20682019-04-29 14:48:42 -070019using ::bluetooth::os::Handler;
20using ::bluetooth::os::Thread;
21
Zach Johnson429c94b2019-04-25 22:24:54 -070022namespace bluetooth {
23
Hansong Zhang28f52442019-11-12 12:23:30 -080024constexpr std::chrono::milliseconds kModuleStopTimeout = std::chrono::milliseconds(20);
25
Zach Johnson429c94b2019-04-25 22:24:54 -070026ModuleFactory::ModuleFactory(std::function<Module*()> ctor) : ctor_(ctor) {
27}
28
Zongheng Wang6fd349a2019-11-12 16:14:01 -080029std::string Module::ToString() const {
30 return "Module";
31}
32
Myles Watsona61337d2019-10-18 11:21:47 -070033Handler* Module::GetHandler() const {
Hansong Zhang712e9e22019-05-29 17:52:24 -070034 ASSERT_LOG(handler_ != nullptr, "Can't get handler when it's not started");
Zach Johnson7ea20682019-04-29 14:48:42 -070035 return handler_;
36}
37
Myles Watsona61337d2019-10-18 11:21:47 -070038const ModuleRegistry* Module::GetModuleRegistry() const {
Zach Johnson84b448b2019-04-29 15:34:55 -070039 return registry_;
40}
41
42Module* Module::GetDependency(const ModuleFactory* module) const {
43 for (auto& dependency : dependencies_.list_) {
44 if (dependency == module) {
45 return registry_->Get(module);
46 }
47 }
48
49 ASSERT_LOG(false, "Module was not listed as a dependency in ListDependencies");
50}
51
52Module* ModuleRegistry::Get(const ModuleFactory* module) const {
53 auto instance = started_modules_.find(module);
54 ASSERT(instance != started_modules_.end());
55 return instance->second;
56}
57
58bool ModuleRegistry::IsStarted(const ModuleFactory* module) const {
59 return started_modules_.find(module) != started_modules_.end();
Zach Johnson429c94b2019-04-25 22:24:54 -070060}
61
Zach Johnson7ea20682019-04-29 14:48:42 -070062void ModuleRegistry::Start(ModuleList* modules, Thread* thread) {
Zach Johnson429c94b2019-04-25 22:24:54 -070063 for (auto it = modules->list_.begin(); it != modules->list_.end(); it++) {
Zach Johnson7ea20682019-04-29 14:48:42 -070064 Start(*it, thread);
Zach Johnson429c94b2019-04-25 22:24:54 -070065 }
66}
67
Myles Watson879fbfb2019-05-06 17:54:51 -070068void ModuleRegistry::set_registry_and_handler(Module* instance, Thread* thread) const {
69 instance->registry_ = this;
70 instance->handler_ = new Handler(thread);
71}
72
Zach Johnson84b448b2019-04-29 15:34:55 -070073Module* ModuleRegistry::Start(const ModuleFactory* module, Thread* thread) {
74 auto started_instance = started_modules_.find(module);
75 if (started_instance != started_modules_.end()) {
76 return started_instance->second;
Zach Johnsone0e158c2019-04-26 11:57:05 -070077 }
78
79 Module* instance = module->ctor_();
Myles Watson879fbfb2019-05-06 17:54:51 -070080 set_registry_and_handler(instance, thread);
Zach Johnson7ea20682019-04-29 14:48:42 -070081
Myles Watson879fbfb2019-05-06 17:54:51 -070082 instance->ListDependencies(&instance->dependencies_);
Zach Johnson84b448b2019-04-29 15:34:55 -070083 Start(&instance->dependencies_, thread);
Zach Johnsone0e158c2019-04-26 11:57:05 -070084
Zach Johnson84b448b2019-04-29 15:34:55 -070085 instance->Start();
Zach Johnsone0e158c2019-04-26 11:57:05 -070086 start_order_.push_back(module);
87 started_modules_[module] = instance;
Zach Johnson84b448b2019-04-29 15:34:55 -070088 return instance;
Zach Johnsone0e158c2019-04-26 11:57:05 -070089}
90
Zach Johnson429c94b2019-04-25 22:24:54 -070091void ModuleRegistry::StopAll() {
Myles Watson742fe6d2019-06-07 08:57:16 -070092 // Since modules were brought up in dependency order, it is safe to tear down by going in reverse order.
Zach Johnson429c94b2019-04-25 22:24:54 -070093 for (auto it = start_order_.rbegin(); it != start_order_.rend(); it++) {
94 auto instance = started_modules_.find(*it);
95 ASSERT(instance != started_modules_.end());
Zach Johnson84b448b2019-04-29 15:34:55 -070096
Myles Watson742fe6d2019-06-07 08:57:16 -070097 // Clear the handler before stopping the module to allow it to shut down gracefully.
98 instance->second->handler_->Clear();
99 instance->second->handler_->WaitUntilStopped(kModuleStopTimeout);
Zach Johnson84b448b2019-04-29 15:34:55 -0700100 instance->second->Stop();
Zach Johnson429c94b2019-04-25 22:24:54 -0700101
Zach Johnson7ea20682019-04-29 14:48:42 -0700102 delete instance->second->handler_;
Zach Johnson429c94b2019-04-25 22:24:54 -0700103 delete instance->second;
104 started_modules_.erase(instance);
105 }
106
107 ASSERT(started_modules_.empty());
108 start_order_.clear();
109}
Hansong Zhang70799832019-05-09 16:40:37 -0700110
111os::Handler* ModuleRegistry::GetModuleHandler(const ModuleFactory* module) const {
112 auto started_instance = started_modules_.find(module);
113 if (started_instance != started_modules_.end()) {
114 return started_instance->second->GetHandler();
115 }
116 return nullptr;
117}
Zach Johnson429c94b2019-04-25 22:24:54 -0700118} // namespace bluetooth