blob: 046ee8c5e176228277a29be50cddc813e51ae4e9 [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#pragma once
18
19#include <functional>
Hansong Zhang70799832019-05-09 16:40:37 -070020#include <future>
Zach Johnson429c94b2019-04-25 22:24:54 -070021#include <map>
Hansong Zhang70799832019-05-09 16:40:37 -070022#include <vector>
Zach Johnson429c94b2019-04-25 22:24:54 -070023
24#include "os/log.h"
Zach Johnson7ea20682019-04-29 14:48:42 -070025#include "os/handler.h"
26#include "os/thread.h"
Zach Johnson429c94b2019-04-25 22:24:54 -070027
28namespace bluetooth {
29
Myles Watson879fbfb2019-05-06 17:54:51 -070030const std::chrono::milliseconds kModuleStopTimeout = std::chrono::milliseconds(20);
31
Zach Johnson429c94b2019-04-25 22:24:54 -070032class Module;
33class ModuleRegistry;
34
35class ModuleFactory {
36 friend ModuleRegistry;
37 public:
38 ModuleFactory(std::function<Module*()> ctor);
39
40 private:
41 std::function<Module*()> ctor_;
42};
43
44class ModuleList {
45 friend ModuleRegistry;
Zach Johnson84b448b2019-04-29 15:34:55 -070046 friend Module;
Zach Johnson429c94b2019-04-25 22:24:54 -070047 public:
48 template <class T>
49 void add() {
50 list_.push_back(&T::Factory);
51 }
52
53 private:
54 std::vector<const ModuleFactory*> list_;
55};
56
57// Each leaf node module must have a factory like so:
58//
59// static const ModuleFactory Factory;
60//
61// which will provide a constructor for the module registry to call.
Zach Johnson7ea20682019-04-29 14:48:42 -070062// The module registry will also use the factory as the identifier
Zach Johnson429c94b2019-04-25 22:24:54 -070063// for that module.
64class Module {
65 friend ModuleRegistry;
66 public:
67 virtual ~Module() = default;
68 protected:
69 // Populate the provided list with modules that must start before yours
70 virtual void ListDependencies(ModuleList* list) = 0;
71
Zach Johnson84b448b2019-04-29 15:34:55 -070072 // You can grab your started dependencies during or after this call
73 // using GetDependency(), or access the module registry via GetModuleRegistry()
74 virtual void Start() = 0;
Zach Johnson429c94b2019-04-25 22:24:54 -070075
76 // Release all resources, you're about to be deleted
Zach Johnson84b448b2019-04-29 15:34:55 -070077 virtual void Stop() = 0;
Zach Johnson7ea20682019-04-29 14:48:42 -070078
79 ::bluetooth::os::Handler* GetHandler();
80
Myles Watson879fbfb2019-05-06 17:54:51 -070081 const ModuleRegistry* GetModuleRegistry();
Zach Johnson84b448b2019-04-29 15:34:55 -070082
83 template <class T>
84 T* GetDependency() const {
85 return static_cast<T*>(GetDependency(&T::Factory));
86 }
87
Zach Johnson7ea20682019-04-29 14:48:42 -070088 private:
Zach Johnson84b448b2019-04-29 15:34:55 -070089 Module* GetDependency(const ModuleFactory* module) const;
90
Hansong Zhang712e9e22019-05-29 17:52:24 -070091 ::bluetooth::os::Handler* handler_ = nullptr;
Zach Johnson84b448b2019-04-29 15:34:55 -070092 ModuleList dependencies_;
Myles Watson879fbfb2019-05-06 17:54:51 -070093 const ModuleRegistry* registry_;
Zach Johnson429c94b2019-04-25 22:24:54 -070094};
95
96class ModuleRegistry {
Zach Johnson84b448b2019-04-29 15:34:55 -070097 friend Module;
98 friend class StackManager;
Zach Johnson429c94b2019-04-25 22:24:54 -070099 public:
100 template <class T>
Zach Johnson429c94b2019-04-25 22:24:54 -0700101 bool IsStarted() const {
102 return IsStarted(&T::Factory);
103 }
104
105 bool IsStarted(const ModuleFactory* factory) const;
106
107 // Start all the modules on this list and their dependencies
108 // in dependency order
Zach Johnson7ea20682019-04-29 14:48:42 -0700109 void Start(ModuleList* modules, ::bluetooth::os::Thread* thread);
Zach Johnson429c94b2019-04-25 22:24:54 -0700110
Zach Johnsone0e158c2019-04-26 11:57:05 -0700111 template <class T>
Zach Johnson84b448b2019-04-29 15:34:55 -0700112 T* Start(::bluetooth::os::Thread* thread) {
113 return static_cast<T*>(Start(&T::Factory, thread));
Zach Johnsone0e158c2019-04-26 11:57:05 -0700114 }
115
Zach Johnson84b448b2019-04-29 15:34:55 -0700116 Module* Start(const ModuleFactory* id, ::bluetooth::os::Thread* thread);
Zach Johnsone0e158c2019-04-26 11:57:05 -0700117
Zach Johnson429c94b2019-04-25 22:24:54 -0700118 // Stop all running modules in reverse order of start
119 void StopAll();
120
Myles Watsonec3dcbf2019-05-15 07:31:57 -0700121 protected:
Hansong Zhang30fa2582019-05-07 16:40:45 -0700122 Module* Get(const ModuleFactory* module) const;
Myles Watsonec3dcbf2019-05-15 07:31:57 -0700123
Myles Watson879fbfb2019-05-06 17:54:51 -0700124 void set_registry_and_handler(Module* instance, ::bluetooth::os::Thread* thread) const;
125
Hansong Zhang70799832019-05-09 16:40:37 -0700126 os::Handler* GetModuleHandler(const ModuleFactory* module) const;
127
Zach Johnson429c94b2019-04-25 22:24:54 -0700128 std::map<const ModuleFactory*, Module*> started_modules_;
129 std::vector<const ModuleFactory*> start_order_;
130};
131
Myles Watsonec3dcbf2019-05-15 07:31:57 -0700132class TestModuleRegistry : public ModuleRegistry {
133 public:
134 void InjectTestModule(const ModuleFactory* module, Module* instance) {
135 start_order_.push_back(module);
136 started_modules_[module] = instance;
Myles Watson879fbfb2019-05-06 17:54:51 -0700137 set_registry_and_handler(instance, &test_thread);
Myles Watsonec3dcbf2019-05-15 07:31:57 -0700138 }
139
140 Module* GetModuleUnderTest(const ModuleFactory* module) const {
141 return Get(module);
142 }
143
Hansong Zhang70799832019-05-09 16:40:37 -0700144 os::Handler* GetTestModuleHandler(const ModuleFactory* module) const {
145 return GetModuleHandler(module);
Myles Watsonec3dcbf2019-05-15 07:31:57 -0700146 }
147
148 os::Thread& GetTestThread() {
149 return test_thread;
150 }
151
Hansong Zhang70799832019-05-09 16:40:37 -0700152 bool SynchronizeModuleHandler(const ModuleFactory* module, std::chrono::milliseconds timeout) const {
153 std::promise<void> promise;
154 os::Handler* handler = GetTestModuleHandler(module);
155 handler->Post([&promise] { promise.set_value(); });
156 return promise.get_future().wait_for(timeout) == std::future_status::ready;
157 }
158
Myles Watsonec3dcbf2019-05-15 07:31:57 -0700159 os::Thread test_thread{"test_thread", os::Thread::Priority::NORMAL};
160};
161
Zach Johnson429c94b2019-04-25 22:24:54 -0700162} // namespace bluetooth