1 /*
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 #define LOG_TAG "BtGdModule"
17
18 #include "module.h"
19 #include "common/init_flags.h"
20 #include "dumpsys/init_flags.h"
21 #include "os/wakelock_manager.h"
22
23 using ::bluetooth::os::Handler;
24 using ::bluetooth::os::Thread;
25 using ::bluetooth::os::WakelockManager;
26
27 namespace bluetooth {
28
29 constexpr std::chrono::milliseconds kModuleStopTimeout = std::chrono::milliseconds(2000);
30
ModuleFactory(std::function<Module * ()> ctor)31 ModuleFactory::ModuleFactory(std::function<Module*()> ctor) : ctor_(ctor) {
32 }
33
GetHandler() const34 Handler* Module::GetHandler() const {
35 ASSERT_LOG(handler_ != nullptr, "Can't get handler when it's not started");
36 return handler_;
37 }
38
__anon313df30c0102(DumpsysDataBuilder* dumpsys_data_builder) 39 DumpsysDataFinisher EmptyDumpsysDataFinisher = [](DumpsysDataBuilder* dumpsys_data_builder) {};
GetDumpsysData(flatbuffers::FlatBufferBuilder * builder) const40 DumpsysDataFinisher Module::GetDumpsysData(flatbuffers::FlatBufferBuilder* builder) const {
41 return EmptyDumpsysDataFinisher;
42 }
43
GetModuleRegistry() const44 const ModuleRegistry* Module::GetModuleRegistry() const {
45 return registry_;
46 }
47
GetDependency(const ModuleFactory * module) const48 Module* Module::GetDependency(const ModuleFactory* module) const {
49 for (auto& dependency : dependencies_.list_) {
50 if (dependency == module) {
51 return registry_->Get(module);
52 }
53 }
54
55 ASSERT_LOG(false, "Module was not listed as a dependency in ListDependencies");
56 }
57
Get(const ModuleFactory * module) const58 Module* ModuleRegistry::Get(const ModuleFactory* module) const {
59 auto instance = started_modules_.find(module);
60 ASSERT(instance != started_modules_.end());
61 return instance->second;
62 }
63
IsStarted(const ModuleFactory * module) const64 bool ModuleRegistry::IsStarted(const ModuleFactory* module) const {
65 return started_modules_.find(module) != started_modules_.end();
66 }
67
Start(ModuleList * modules,Thread * thread)68 void ModuleRegistry::Start(ModuleList* modules, Thread* thread) {
69 for (auto it = modules->list_.begin(); it != modules->list_.end(); it++) {
70 Start(*it, thread);
71 }
72 }
73
set_registry_and_handler(Module * instance,Thread * thread) const74 void ModuleRegistry::set_registry_and_handler(Module* instance, Thread* thread) const {
75 instance->registry_ = this;
76 instance->handler_ = new Handler(thread);
77 }
78
Start(const ModuleFactory * module,Thread * thread)79 Module* ModuleRegistry::Start(const ModuleFactory* module, Thread* thread) {
80 auto started_instance = started_modules_.find(module);
81 if (started_instance != started_modules_.end()) {
82 return started_instance->second;
83 }
84
85 LOG_DEBUG("Constructing next module");
86 Module* instance = module->ctor_();
87 last_instance_ = "starting " + instance->ToString();
88 set_registry_and_handler(instance, thread);
89
90 LOG_DEBUG("Starting dependencies of %s", instance->ToString().c_str());
91 instance->ListDependencies(&instance->dependencies_);
92 Start(&instance->dependencies_, thread);
93
94 LOG_DEBUG("Finished starting dependencies and calling Start() of %s", instance->ToString().c_str());
95
96 instance->Start();
97 start_order_.push_back(module);
98 started_modules_[module] = instance;
99 LOG_DEBUG("Started %s", instance->ToString().c_str());
100 return instance;
101 }
102
StopAll()103 void ModuleRegistry::StopAll() {
104 // Since modules were brought up in dependency order, it is safe to tear down by going in reverse order.
105 for (auto it = start_order_.rbegin(); it != start_order_.rend(); it++) {
106 auto instance = started_modules_.find(*it);
107 ASSERT(instance != started_modules_.end());
108 last_instance_ = "stopping " + instance->second->ToString();
109
110 // Clear the handler before stopping the module to allow it to shut down gracefully.
111 LOG_INFO("Stopping Handler of Module %s", instance->second->ToString().c_str());
112 instance->second->handler_->Clear();
113 instance->second->handler_->WaitUntilStopped(kModuleStopTimeout);
114 LOG_INFO("Stopping Module %s", instance->second->ToString().c_str());
115 instance->second->Stop();
116 }
117 for (auto it = start_order_.rbegin(); it != start_order_.rend(); it++) {
118 auto instance = started_modules_.find(*it);
119 ASSERT(instance != started_modules_.end());
120 delete instance->second->handler_;
121 delete instance->second;
122 started_modules_.erase(instance);
123 }
124
125 ASSERT(started_modules_.empty());
126 start_order_.clear();
127 }
128
GetModuleHandler(const ModuleFactory * module) const129 os::Handler* ModuleRegistry::GetModuleHandler(const ModuleFactory* module) const {
130 auto started_instance = started_modules_.find(module);
131 if (started_instance != started_modules_.end()) {
132 return started_instance->second->GetHandler();
133 }
134 return nullptr;
135 }
136
DumpState(std::string * output) const137 void ModuleDumper::DumpState(std::string* output) const {
138 ASSERT(output != nullptr);
139
140 flatbuffers::FlatBufferBuilder builder(1024);
141 auto title = builder.CreateString(title_);
142
143 auto init_flags_offset = dumpsys::InitFlags::Dump(&builder);
144 auto wakelock_offset = WakelockManager::Get().GetDumpsysData(&builder);
145
146 std::queue<DumpsysDataFinisher> queue;
147 for (auto it = module_registry_.start_order_.rbegin(); it != module_registry_.start_order_.rend(); it++) {
148 auto instance = module_registry_.started_modules_.find(*it);
149 ASSERT(instance != module_registry_.started_modules_.end());
150 queue.push(instance->second->GetDumpsysData(&builder));
151 }
152
153 DumpsysDataBuilder data_builder(builder);
154 data_builder.add_title(title);
155 data_builder.add_init_flags(init_flags_offset);
156 data_builder.add_wakelock_manager_data(wakelock_offset);
157
158 while (!queue.empty()) {
159 queue.front()(&data_builder);
160 queue.pop();
161 }
162
163 builder.Finish(data_builder.Finish());
164 *output = std::string(builder.GetBufferPointer(), builder.GetBufferPointer() + builder.GetSize());
165 }
166
167 } // namespace bluetooth
168