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 
17 #include "module.h"
18 
19 using ::bluetooth::os::Handler;
20 using ::bluetooth::os::Thread;
21 
22 namespace bluetooth {
23 
24 constexpr std::chrono::milliseconds kModuleStopTimeout = std::chrono::milliseconds(2000);
25 
ModuleFactory(std::function<Module * ()> ctor)26 ModuleFactory::ModuleFactory(std::function<Module*()> ctor) : ctor_(ctor) {
27 }
28 
ToString() const29 std::string Module::ToString() const {
30   return "Module";
31 }
32 
GetHandler() const33 Handler* Module::GetHandler() const {
34   ASSERT_LOG(handler_ != nullptr, "Can't get handler when it's not started");
35   return handler_;
36 }
37 
__anon313df30c0102(DumpsysDataBuilder* dumpsys_data_builder) 38 DumpsysDataFinisher EmptyDumpsysDataFinisher = [](DumpsysDataBuilder* dumpsys_data_builder) {};
GetDumpsysData(flatbuffers::FlatBufferBuilder * builder) const39 DumpsysDataFinisher Module::GetDumpsysData(flatbuffers::FlatBufferBuilder* builder) const {
40   return EmptyDumpsysDataFinisher;
41 }
42 
GetModuleRegistry() const43 const ModuleRegistry* Module::GetModuleRegistry() const {
44   return registry_;
45 }
46 
GetDependency(const ModuleFactory * module) const47 Module* Module::GetDependency(const ModuleFactory* module) const {
48   for (auto& dependency : dependencies_.list_) {
49     if (dependency == module) {
50       return registry_->Get(module);
51     }
52   }
53 
54   ASSERT_LOG(false, "Module was not listed as a dependency in ListDependencies");
55 }
56 
Get(const ModuleFactory * module) const57 Module* ModuleRegistry::Get(const ModuleFactory* module) const {
58   auto instance = started_modules_.find(module);
59   ASSERT(instance != started_modules_.end());
60   return instance->second;
61 }
62 
IsStarted(const ModuleFactory * module) const63 bool ModuleRegistry::IsStarted(const ModuleFactory* module) const {
64   return started_modules_.find(module) != started_modules_.end();
65 }
66 
Start(ModuleList * modules,Thread * thread)67 void ModuleRegistry::Start(ModuleList* modules, Thread* thread) {
68   for (auto it = modules->list_.begin(); it != modules->list_.end(); it++) {
69     Start(*it, thread);
70   }
71 }
72 
set_registry_and_handler(Module * instance,Thread * thread) const73 void ModuleRegistry::set_registry_and_handler(Module* instance, Thread* thread) const {
74   instance->registry_ = this;
75   instance->handler_ = new Handler(thread);
76 }
77 
Start(const ModuleFactory * module,Thread * thread)78 Module* ModuleRegistry::Start(const ModuleFactory* module, Thread* thread) {
79   auto started_instance = started_modules_.find(module);
80   if (started_instance != started_modules_.end()) {
81     return started_instance->second;
82   }
83 
84   Module* instance = module->ctor_();
85   set_registry_and_handler(instance, thread);
86 
87   instance->ListDependencies(&instance->dependencies_);
88   Start(&instance->dependencies_, thread);
89 
90   instance->Start();
91   start_order_.push_back(module);
92   started_modules_[module] = instance;
93   return instance;
94 }
95 
StopAll()96 void ModuleRegistry::StopAll() {
97   // Since modules were brought up in dependency order, it is safe to tear down by going in reverse order.
98   for (auto it = start_order_.rbegin(); it != start_order_.rend(); it++) {
99     auto instance = started_modules_.find(*it);
100     ASSERT(instance != started_modules_.end());
101 
102     // Clear the handler before stopping the module to allow it to shut down gracefully.
103     LOG_INFO("Stopping Handler of Module %s", instance->second->ToString().c_str());
104     instance->second->handler_->Clear();
105     instance->second->handler_->WaitUntilStopped(kModuleStopTimeout);
106     LOG_INFO("Stopping Module %s", instance->second->ToString().c_str());
107     instance->second->Stop();
108   }
109   for (auto it = start_order_.rbegin(); it != start_order_.rend(); it++) {
110     auto instance = started_modules_.find(*it);
111     ASSERT(instance != started_modules_.end());
112     delete instance->second->handler_;
113     delete instance->second;
114     started_modules_.erase(instance);
115   }
116 
117   ASSERT(started_modules_.empty());
118   start_order_.clear();
119 }
120 
GetModuleHandler(const ModuleFactory * module) const121 os::Handler* ModuleRegistry::GetModuleHandler(const ModuleFactory* module) const {
122   auto started_instance = started_modules_.find(module);
123   if (started_instance != started_modules_.end()) {
124     return started_instance->second->GetHandler();
125   }
126   return nullptr;
127 }
128 
DumpState(std::string * output) const129 void ModuleDumper::DumpState(std::string* output) const {
130   ASSERT(output != nullptr);
131 
132   flatbuffers::FlatBufferBuilder builder(1024);
133   auto title = builder.CreateString(title_);
134 
135   std::queue<DumpsysDataFinisher> queue;
136   for (auto it = module_registry_.start_order_.rbegin(); it != module_registry_.start_order_.rend(); it++) {
137     auto instance = module_registry_.started_modules_.find(*it);
138     ASSERT(instance != module_registry_.started_modules_.end());
139     queue.push(instance->second->GetDumpsysData(&builder));
140   }
141 
142   DumpsysDataBuilder data_builder(builder);
143   data_builder.add_title(title);
144 
145   while (!queue.empty()) {
146     queue.front()(&data_builder);
147     queue.pop();
148   }
149 
150   builder.Finish(data_builder.Finish());
151   *output = std::string(builder.GetBufferPointer(), builder.GetBufferPointer() + builder.GetSize());
152 }
153 
154 }  // namespace bluetooth
155