Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 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 | #pragma once |
| 18 | |
| 19 | #include <functional> |
Hansong Zhang | 7079983 | 2019-05-09 16:40:37 -0700 | [diff] [blame] | 20 | #include <future> |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 21 | #include <map> |
Zongheng Wang | 6fd349a | 2019-11-12 16:14:01 -0800 | [diff] [blame^] | 22 | #include <string> |
Hansong Zhang | 7079983 | 2019-05-09 16:40:37 -0700 | [diff] [blame] | 23 | #include <vector> |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 24 | |
Hansong Zhang | aa0875c | 2019-06-06 21:44:55 -0700 | [diff] [blame] | 25 | #include "common/bind.h" |
Zach Johnson | 7ea2068 | 2019-04-29 14:48:42 -0700 | [diff] [blame] | 26 | #include "os/handler.h" |
Hansong Zhang | aa0875c | 2019-06-06 21:44:55 -0700 | [diff] [blame] | 27 | #include "os/log.h" |
Zach Johnson | 7ea2068 | 2019-04-29 14:48:42 -0700 | [diff] [blame] | 28 | #include "os/thread.h" |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 29 | |
| 30 | namespace bluetooth { |
| 31 | |
| 32 | class Module; |
| 33 | class ModuleRegistry; |
| 34 | |
| 35 | class ModuleFactory { |
| 36 | friend ModuleRegistry; |
| 37 | public: |
| 38 | ModuleFactory(std::function<Module*()> ctor); |
| 39 | |
| 40 | private: |
| 41 | std::function<Module*()> ctor_; |
| 42 | }; |
| 43 | |
| 44 | class ModuleList { |
| 45 | friend ModuleRegistry; |
Zach Johnson | 84b448b | 2019-04-29 15:34:55 -0700 | [diff] [blame] | 46 | friend Module; |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 47 | 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 Johnson | 7ea2068 | 2019-04-29 14:48:42 -0700 | [diff] [blame] | 62 | // The module registry will also use the factory as the identifier |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 63 | // for that module. |
| 64 | class 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 Johnson | 84b448b | 2019-04-29 15:34:55 -0700 | [diff] [blame] | 72 | // 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 Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 75 | |
| 76 | // Release all resources, you're about to be deleted |
Zach Johnson | 84b448b | 2019-04-29 15:34:55 -0700 | [diff] [blame] | 77 | virtual void Stop() = 0; |
Zach Johnson | 7ea2068 | 2019-04-29 14:48:42 -0700 | [diff] [blame] | 78 | |
Zongheng Wang | 6fd349a | 2019-11-12 16:14:01 -0800 | [diff] [blame^] | 79 | virtual std::string ToString() const; |
| 80 | |
Myles Watson | a61337d | 2019-10-18 11:21:47 -0700 | [diff] [blame] | 81 | ::bluetooth::os::Handler* GetHandler() const; |
Zach Johnson | 7ea2068 | 2019-04-29 14:48:42 -0700 | [diff] [blame] | 82 | |
Myles Watson | a61337d | 2019-10-18 11:21:47 -0700 | [diff] [blame] | 83 | const ModuleRegistry* GetModuleRegistry() const; |
Zach Johnson | 84b448b | 2019-04-29 15:34:55 -0700 | [diff] [blame] | 84 | |
| 85 | template <class T> |
| 86 | T* GetDependency() const { |
| 87 | return static_cast<T*>(GetDependency(&T::Factory)); |
| 88 | } |
| 89 | |
Zach Johnson | 7ea2068 | 2019-04-29 14:48:42 -0700 | [diff] [blame] | 90 | private: |
Zach Johnson | 84b448b | 2019-04-29 15:34:55 -0700 | [diff] [blame] | 91 | Module* GetDependency(const ModuleFactory* module) const; |
| 92 | |
Hansong Zhang | 712e9e2 | 2019-05-29 17:52:24 -0700 | [diff] [blame] | 93 | ::bluetooth::os::Handler* handler_ = nullptr; |
Zach Johnson | 84b448b | 2019-04-29 15:34:55 -0700 | [diff] [blame] | 94 | ModuleList dependencies_; |
Myles Watson | 879fbfb | 2019-05-06 17:54:51 -0700 | [diff] [blame] | 95 | const ModuleRegistry* registry_; |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | class ModuleRegistry { |
Zach Johnson | 84b448b | 2019-04-29 15:34:55 -0700 | [diff] [blame] | 99 | friend Module; |
| 100 | friend class StackManager; |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 101 | public: |
| 102 | template <class T> |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 103 | bool IsStarted() const { |
| 104 | return IsStarted(&T::Factory); |
| 105 | } |
| 106 | |
| 107 | bool IsStarted(const ModuleFactory* factory) const; |
| 108 | |
| 109 | // Start all the modules on this list and their dependencies |
| 110 | // in dependency order |
Zach Johnson | 7ea2068 | 2019-04-29 14:48:42 -0700 | [diff] [blame] | 111 | void Start(ModuleList* modules, ::bluetooth::os::Thread* thread); |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 112 | |
Zach Johnson | e0e158c | 2019-04-26 11:57:05 -0700 | [diff] [blame] | 113 | template <class T> |
Zach Johnson | 84b448b | 2019-04-29 15:34:55 -0700 | [diff] [blame] | 114 | T* Start(::bluetooth::os::Thread* thread) { |
| 115 | return static_cast<T*>(Start(&T::Factory, thread)); |
Zach Johnson | e0e158c | 2019-04-26 11:57:05 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Zach Johnson | 84b448b | 2019-04-29 15:34:55 -0700 | [diff] [blame] | 118 | Module* Start(const ModuleFactory* id, ::bluetooth::os::Thread* thread); |
Zach Johnson | e0e158c | 2019-04-26 11:57:05 -0700 | [diff] [blame] | 119 | |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 120 | // Stop all running modules in reverse order of start |
| 121 | void StopAll(); |
| 122 | |
Myles Watson | ec3dcbf | 2019-05-15 07:31:57 -0700 | [diff] [blame] | 123 | protected: |
Hansong Zhang | 30fa258 | 2019-05-07 16:40:45 -0700 | [diff] [blame] | 124 | Module* Get(const ModuleFactory* module) const; |
Myles Watson | ec3dcbf | 2019-05-15 07:31:57 -0700 | [diff] [blame] | 125 | |
Myles Watson | 879fbfb | 2019-05-06 17:54:51 -0700 | [diff] [blame] | 126 | void set_registry_and_handler(Module* instance, ::bluetooth::os::Thread* thread) const; |
| 127 | |
Hansong Zhang | 7079983 | 2019-05-09 16:40:37 -0700 | [diff] [blame] | 128 | os::Handler* GetModuleHandler(const ModuleFactory* module) const; |
| 129 | |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 130 | std::map<const ModuleFactory*, Module*> started_modules_; |
| 131 | std::vector<const ModuleFactory*> start_order_; |
| 132 | }; |
| 133 | |
Myles Watson | ec3dcbf | 2019-05-15 07:31:57 -0700 | [diff] [blame] | 134 | class TestModuleRegistry : public ModuleRegistry { |
| 135 | public: |
| 136 | void InjectTestModule(const ModuleFactory* module, Module* instance) { |
| 137 | start_order_.push_back(module); |
| 138 | started_modules_[module] = instance; |
Myles Watson | 879fbfb | 2019-05-06 17:54:51 -0700 | [diff] [blame] | 139 | set_registry_and_handler(instance, &test_thread); |
Myles Watson | ec3dcbf | 2019-05-15 07:31:57 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | Module* GetModuleUnderTest(const ModuleFactory* module) const { |
| 143 | return Get(module); |
| 144 | } |
| 145 | |
Hansong Zhang | 7079983 | 2019-05-09 16:40:37 -0700 | [diff] [blame] | 146 | os::Handler* GetTestModuleHandler(const ModuleFactory* module) const { |
| 147 | return GetModuleHandler(module); |
Myles Watson | ec3dcbf | 2019-05-15 07:31:57 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | os::Thread& GetTestThread() { |
| 151 | return test_thread; |
| 152 | } |
| 153 | |
Hansong Zhang | 7079983 | 2019-05-09 16:40:37 -0700 | [diff] [blame] | 154 | bool SynchronizeModuleHandler(const ModuleFactory* module, std::chrono::milliseconds timeout) const { |
| 155 | std::promise<void> promise; |
Hansong Zhang | aa0875c | 2019-06-06 21:44:55 -0700 | [diff] [blame] | 156 | auto future = promise.get_future(); |
Hansong Zhang | 7079983 | 2019-05-09 16:40:37 -0700 | [diff] [blame] | 157 | os::Handler* handler = GetTestModuleHandler(module); |
Hansong Zhang | aa0875c | 2019-06-06 21:44:55 -0700 | [diff] [blame] | 158 | handler->Post(common::BindOnce(&std::promise<void>::set_value, common::Unretained(&promise))); |
| 159 | return future.wait_for(timeout) == std::future_status::ready; |
Hansong Zhang | 7079983 | 2019-05-09 16:40:37 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Hansong Zhang | aa0875c | 2019-06-06 21:44:55 -0700 | [diff] [blame] | 162 | private: |
Myles Watson | ec3dcbf | 2019-05-15 07:31:57 -0700 | [diff] [blame] | 163 | os::Thread test_thread{"test_thread", os::Thread::Priority::NORMAL}; |
| 164 | }; |
| 165 | |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 166 | } // namespace bluetooth |