blob: 96a1109c0daacc7378ccceb4f5d56200e2126365 [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
Hansong Zhangaa0875c2019-06-06 21:44:55 -070024#include "common/bind.h"
Zach Johnson7ea20682019-04-29 14:48:42 -070025#include "os/handler.h"
Hansong Zhangaa0875c2019-06-06 21:44:55 -070026#include "os/log.h"
Zach Johnson7ea20682019-04-29 14:48:42 -070027#include "os/thread.h"
Zach Johnson429c94b2019-04-25 22:24:54 -070028
29namespace bluetooth {
30
Myles Watson879fbfb2019-05-06 17:54:51 -070031const std::chrono::milliseconds kModuleStopTimeout = std::chrono::milliseconds(20);
32
Zach Johnson429c94b2019-04-25 22:24:54 -070033class Module;
34class ModuleRegistry;
35
36class ModuleFactory {
37 friend ModuleRegistry;
38 public:
39 ModuleFactory(std::function<Module*()> ctor);
40
41 private:
42 std::function<Module*()> ctor_;
43};
44
45class ModuleList {
46 friend ModuleRegistry;
Zach Johnson84b448b2019-04-29 15:34:55 -070047 friend Module;
Zach Johnson429c94b2019-04-25 22:24:54 -070048 public:
49 template <class T>
50 void add() {
51 list_.push_back(&T::Factory);
52 }
53
54 private:
55 std::vector<const ModuleFactory*> list_;
56};
57
58// Each leaf node module must have a factory like so:
59//
60// static const ModuleFactory Factory;
61//
62// which will provide a constructor for the module registry to call.
Zach Johnson7ea20682019-04-29 14:48:42 -070063// The module registry will also use the factory as the identifier
Zach Johnson429c94b2019-04-25 22:24:54 -070064// for that module.
65class Module {
66 friend ModuleRegistry;
67 public:
68 virtual ~Module() = default;
69 protected:
70 // Populate the provided list with modules that must start before yours
71 virtual void ListDependencies(ModuleList* list) = 0;
72
Zach Johnson84b448b2019-04-29 15:34:55 -070073 // You can grab your started dependencies during or after this call
74 // using GetDependency(), or access the module registry via GetModuleRegistry()
75 virtual void Start() = 0;
Zach Johnson429c94b2019-04-25 22:24:54 -070076
77 // Release all resources, you're about to be deleted
Zach Johnson84b448b2019-04-29 15:34:55 -070078 virtual void Stop() = 0;
Zach Johnson7ea20682019-04-29 14:48:42 -070079
80 ::bluetooth::os::Handler* GetHandler();
81
Myles Watson879fbfb2019-05-06 17:54:51 -070082 const ModuleRegistry* GetModuleRegistry();
Zach Johnson84b448b2019-04-29 15:34:55 -070083
84 template <class T>
85 T* GetDependency() const {
86 return static_cast<T*>(GetDependency(&T::Factory));
87 }
88
Zach Johnson7ea20682019-04-29 14:48:42 -070089 private:
Zach Johnson84b448b2019-04-29 15:34:55 -070090 Module* GetDependency(const ModuleFactory* module) const;
91
Hansong Zhang712e9e22019-05-29 17:52:24 -070092 ::bluetooth::os::Handler* handler_ = nullptr;
Zach Johnson84b448b2019-04-29 15:34:55 -070093 ModuleList dependencies_;
Myles Watson879fbfb2019-05-06 17:54:51 -070094 const ModuleRegistry* registry_;
Zach Johnson429c94b2019-04-25 22:24:54 -070095};
96
97class ModuleRegistry {
Zach Johnson84b448b2019-04-29 15:34:55 -070098 friend Module;
99 friend class StackManager;
Zach Johnson429c94b2019-04-25 22:24:54 -0700100 public:
101 template <class T>
Zach Johnson429c94b2019-04-25 22:24:54 -0700102 bool IsStarted() const {
103 return IsStarted(&T::Factory);
104 }
105
106 bool IsStarted(const ModuleFactory* factory) const;
107
108 // Start all the modules on this list and their dependencies
109 // in dependency order
Zach Johnson7ea20682019-04-29 14:48:42 -0700110 void Start(ModuleList* modules, ::bluetooth::os::Thread* thread);
Zach Johnson429c94b2019-04-25 22:24:54 -0700111
Zach Johnsone0e158c2019-04-26 11:57:05 -0700112 template <class T>
Zach Johnson84b448b2019-04-29 15:34:55 -0700113 T* Start(::bluetooth::os::Thread* thread) {
114 return static_cast<T*>(Start(&T::Factory, thread));
Zach Johnsone0e158c2019-04-26 11:57:05 -0700115 }
116
Zach Johnson84b448b2019-04-29 15:34:55 -0700117 Module* Start(const ModuleFactory* id, ::bluetooth::os::Thread* thread);
Zach Johnsone0e158c2019-04-26 11:57:05 -0700118
Zach Johnson429c94b2019-04-25 22:24:54 -0700119 // Stop all running modules in reverse order of start
120 void StopAll();
121
Myles Watsonec3dcbf2019-05-15 07:31:57 -0700122 protected:
Hansong Zhang30fa2582019-05-07 16:40:45 -0700123 Module* Get(const ModuleFactory* module) const;
Myles Watsonec3dcbf2019-05-15 07:31:57 -0700124
Myles Watson879fbfb2019-05-06 17:54:51 -0700125 void set_registry_and_handler(Module* instance, ::bluetooth::os::Thread* thread) const;
126
Hansong Zhang70799832019-05-09 16:40:37 -0700127 os::Handler* GetModuleHandler(const ModuleFactory* module) const;
128
Zach Johnson429c94b2019-04-25 22:24:54 -0700129 std::map<const ModuleFactory*, Module*> started_modules_;
130 std::vector<const ModuleFactory*> start_order_;
131};
132
Myles Watsonec3dcbf2019-05-15 07:31:57 -0700133class TestModuleRegistry : public ModuleRegistry {
134 public:
135 void InjectTestModule(const ModuleFactory* module, Module* instance) {
136 start_order_.push_back(module);
137 started_modules_[module] = instance;
Myles Watson879fbfb2019-05-06 17:54:51 -0700138 set_registry_and_handler(instance, &test_thread);
Myles Watsonec3dcbf2019-05-15 07:31:57 -0700139 }
140
141 Module* GetModuleUnderTest(const ModuleFactory* module) const {
142 return Get(module);
143 }
144
Hansong Zhang70799832019-05-09 16:40:37 -0700145 os::Handler* GetTestModuleHandler(const ModuleFactory* module) const {
146 return GetModuleHandler(module);
Myles Watsonec3dcbf2019-05-15 07:31:57 -0700147 }
148
149 os::Thread& GetTestThread() {
150 return test_thread;
151 }
152
Hansong Zhang70799832019-05-09 16:40:37 -0700153 bool SynchronizeModuleHandler(const ModuleFactory* module, std::chrono::milliseconds timeout) const {
154 std::promise<void> promise;
Hansong Zhangaa0875c2019-06-06 21:44:55 -0700155 auto future = promise.get_future();
Hansong Zhang70799832019-05-09 16:40:37 -0700156 os::Handler* handler = GetTestModuleHandler(module);
Hansong Zhangaa0875c2019-06-06 21:44:55 -0700157 handler->Post(common::BindOnce(&std::promise<void>::set_value, common::Unretained(&promise)));
158 return future.wait_for(timeout) == std::future_status::ready;
Hansong Zhang70799832019-05-09 16:40:37 -0700159 }
160
Hansong Zhangaa0875c2019-06-06 21:44:55 -0700161 private:
Myles Watsonec3dcbf2019-05-15 07:31:57 -0700162 os::Thread test_thread{"test_thread", os::Thread::Priority::NORMAL};
163};
164
Zach Johnson429c94b2019-04-25 22:24:54 -0700165} // namespace bluetooth