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 
20 #include <bluetooth/log.h>
21 
22 #include "common/init_flags.h"
23 
24 using ::bluetooth::os::Handler;
25 using ::bluetooth::os::Thread;
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   log::assert_that(handler_ != nullptr, "Can't get handler when it's not started");
36   return handler_;
37 }
38 
GetModuleRegistry() const39 const ModuleRegistry* Module::GetModuleRegistry() const {
40   return registry_;
41 }
42 
GetDependency(const ModuleFactory * module) const43 Module* Module::GetDependency(const ModuleFactory* module) const {
44   for (auto& dependency : dependencies_.list_) {
45     if (dependency == module) {
46       return registry_->Get(module);
47     }
48   }
49 
50   log::fatal("Module was not listed as a dependency in ListDependencies");
51 }
52 
53 bluetooth::DumpsysDataFinisher EmptyDumpsysDataFinisher =
__anon6baa15a20102(bluetooth::DumpsysDataBuilder* ) 54     [](bluetooth::DumpsysDataBuilder* /* dumpsys_data_builder */) {};
55 
GetDumpsysData(flatbuffers::FlatBufferBuilder *) const56 DumpsysDataFinisher Module::GetDumpsysData(flatbuffers::FlatBufferBuilder* /* builder */) const {
57   return EmptyDumpsysDataFinisher;
58 }
59 
Get(const ModuleFactory * module) const60 Module* ModuleRegistry::Get(const ModuleFactory* module) const {
61   auto instance = started_modules_.find(module);
62   log::assert_that(
63       instance != started_modules_.end(),
64       "Request for module not started up, maybe not in Start(ModuleList)?");
65   return instance->second;
66 }
67 
IsStarted(const ModuleFactory * module) const68 bool ModuleRegistry::IsStarted(const ModuleFactory* module) const {
69   return started_modules_.find(module) != started_modules_.end();
70 }
71 
Start(ModuleList * modules,Thread * thread)72 void ModuleRegistry::Start(ModuleList* modules, Thread* thread) {
73   for (auto it = modules->list_.begin(); it != modules->list_.end(); it++) {
74     Start(*it, thread);
75   }
76 }
77 
set_registry_and_handler(Module * instance,Thread * thread) const78 void ModuleRegistry::set_registry_and_handler(Module* instance, Thread* thread) const {
79   instance->registry_ = this;
80   instance->handler_ = new Handler(thread);
81 }
82 
Start(const ModuleFactory * module,Thread * thread)83 Module* ModuleRegistry::Start(const ModuleFactory* module, Thread* thread) {
84   auto started_instance = started_modules_.find(module);
85   if (started_instance != started_modules_.end()) {
86     return started_instance->second;
87   }
88 
89   log::info("Constructing next module");
90   Module* instance = module->ctor_();
91   set_registry_and_handler(instance, thread);
92 
93   log::info("Starting dependencies of {}", instance->ToString());
94   instance->ListDependencies(&instance->dependencies_);
95   Start(&instance->dependencies_, thread);
96 
97   log::info("Finished starting dependencies and calling Start() of {}", instance->ToString());
98 
99   last_instance_ = "starting " + instance->ToString();
100   instance->Start();
101   start_order_.push_back(module);
102   started_modules_[module] = instance;
103   log::info("Started {}", instance->ToString());
104   return instance;
105 }
106 
StopAll()107 void ModuleRegistry::StopAll() {
108   // Since modules were brought up in dependency order, it is safe to tear down by going in reverse order.
109   for (auto it = start_order_.rbegin(); it != start_order_.rend(); it++) {
110     auto instance = started_modules_.find(*it);
111     log::assert_that(
112         instance != started_modules_.end(), "assert failed: instance != started_modules_.end()");
113     last_instance_ = "stopping " + instance->second->ToString();
114 
115     // Clear the handler before stopping the module to allow it to shut down gracefully.
116     log::info("Stopping Handler of Module {}", instance->second->ToString());
117     instance->second->handler_->Clear();
118     instance->second->handler_->WaitUntilStopped(kModuleStopTimeout);
119     log::info("Stopping Module {}", instance->second->ToString());
120     instance->second->Stop();
121   }
122   for (auto it = start_order_.rbegin(); it != start_order_.rend(); it++) {
123     auto instance = started_modules_.find(*it);
124     log::assert_that(
125         instance != started_modules_.end(), "assert failed: instance != started_modules_.end()");
126     delete instance->second->handler_;
127     delete instance->second;
128     started_modules_.erase(instance);
129   }
130 
131   log::assert_that(started_modules_.empty(), "assert failed: started_modules_.empty()");
132   start_order_.clear();
133 }
134 
GetModuleHandler(const ModuleFactory * module) const135 os::Handler* ModuleRegistry::GetModuleHandler(const ModuleFactory* module) const {
136   auto started_instance = started_modules_.find(module);
137   if (started_instance != started_modules_.end()) {
138     return started_instance->second->GetHandler();
139   }
140   return nullptr;
141 }
142 
143 }  // namespace bluetooth
144