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> |
Hansong Zhang | 7079983 | 2019-05-09 16:40:37 -0700 | [diff] [blame] | 22 | #include <vector> |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 23 | |
| 24 | #include "os/log.h" |
Zach Johnson | 7ea2068 | 2019-04-29 14:48:42 -0700 | [diff] [blame] | 25 | #include "os/handler.h" |
| 26 | #include "os/thread.h" |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 27 | |
| 28 | namespace bluetooth { |
| 29 | |
| 30 | class Module; |
| 31 | class ModuleRegistry; |
| 32 | |
| 33 | class ModuleFactory { |
| 34 | friend ModuleRegistry; |
| 35 | public: |
| 36 | ModuleFactory(std::function<Module*()> ctor); |
| 37 | |
| 38 | private: |
| 39 | std::function<Module*()> ctor_; |
| 40 | }; |
| 41 | |
| 42 | class ModuleList { |
| 43 | friend ModuleRegistry; |
Zach Johnson | 84b448b | 2019-04-29 15:34:55 -0700 | [diff] [blame] | 44 | friend Module; |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 45 | public: |
| 46 | template <class T> |
| 47 | void add() { |
| 48 | list_.push_back(&T::Factory); |
| 49 | } |
| 50 | |
| 51 | private: |
| 52 | std::vector<const ModuleFactory*> list_; |
| 53 | }; |
| 54 | |
| 55 | // Each leaf node module must have a factory like so: |
| 56 | // |
| 57 | // static const ModuleFactory Factory; |
| 58 | // |
| 59 | // which will provide a constructor for the module registry to call. |
Zach Johnson | 7ea2068 | 2019-04-29 14:48:42 -0700 | [diff] [blame] | 60 | // The module registry will also use the factory as the identifier |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 61 | // for that module. |
| 62 | class Module { |
| 63 | friend ModuleRegistry; |
| 64 | public: |
| 65 | virtual ~Module() = default; |
| 66 | protected: |
| 67 | // Populate the provided list with modules that must start before yours |
| 68 | virtual void ListDependencies(ModuleList* list) = 0; |
| 69 | |
Zach Johnson | 84b448b | 2019-04-29 15:34:55 -0700 | [diff] [blame] | 70 | // You can grab your started dependencies during or after this call |
| 71 | // using GetDependency(), or access the module registry via GetModuleRegistry() |
| 72 | virtual void Start() = 0; |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 73 | |
| 74 | // Release all resources, you're about to be deleted |
Zach Johnson | 84b448b | 2019-04-29 15:34:55 -0700 | [diff] [blame] | 75 | virtual void Stop() = 0; |
Zach Johnson | 7ea2068 | 2019-04-29 14:48:42 -0700 | [diff] [blame] | 76 | |
| 77 | ::bluetooth::os::Handler* GetHandler(); |
| 78 | |
Zach Johnson | 84b448b | 2019-04-29 15:34:55 -0700 | [diff] [blame] | 79 | ModuleRegistry* GetModuleRegistry(); |
| 80 | |
| 81 | template <class T> |
| 82 | T* GetDependency() const { |
| 83 | return static_cast<T*>(GetDependency(&T::Factory)); |
| 84 | } |
| 85 | |
Zach Johnson | 7ea2068 | 2019-04-29 14:48:42 -0700 | [diff] [blame] | 86 | private: |
Zach Johnson | 84b448b | 2019-04-29 15:34:55 -0700 | [diff] [blame] | 87 | Module* GetDependency(const ModuleFactory* module) const; |
| 88 | |
Hansong Zhang | 712e9e2 | 2019-05-29 17:52:24 -0700 | [diff] [blame^] | 89 | ::bluetooth::os::Handler* handler_ = nullptr; |
Zach Johnson | 84b448b | 2019-04-29 15:34:55 -0700 | [diff] [blame] | 90 | ModuleList dependencies_; |
| 91 | ModuleRegistry* registry_; |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | class ModuleRegistry { |
Zach Johnson | 84b448b | 2019-04-29 15:34:55 -0700 | [diff] [blame] | 95 | friend Module; |
| 96 | friend class StackManager; |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 97 | public: |
| 98 | template <class T> |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 99 | bool IsStarted() const { |
| 100 | return IsStarted(&T::Factory); |
| 101 | } |
| 102 | |
| 103 | bool IsStarted(const ModuleFactory* factory) const; |
| 104 | |
| 105 | // Start all the modules on this list and their dependencies |
| 106 | // in dependency order |
Zach Johnson | 7ea2068 | 2019-04-29 14:48:42 -0700 | [diff] [blame] | 107 | void Start(ModuleList* modules, ::bluetooth::os::Thread* thread); |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 108 | |
Zach Johnson | e0e158c | 2019-04-26 11:57:05 -0700 | [diff] [blame] | 109 | template <class T> |
Zach Johnson | 84b448b | 2019-04-29 15:34:55 -0700 | [diff] [blame] | 110 | T* Start(::bluetooth::os::Thread* thread) { |
| 111 | return static_cast<T*>(Start(&T::Factory, thread)); |
Zach Johnson | e0e158c | 2019-04-26 11:57:05 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Zach Johnson | 84b448b | 2019-04-29 15:34:55 -0700 | [diff] [blame] | 114 | Module* Start(const ModuleFactory* id, ::bluetooth::os::Thread* thread); |
Zach Johnson | e0e158c | 2019-04-26 11:57:05 -0700 | [diff] [blame] | 115 | |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 116 | // Stop all running modules in reverse order of start |
| 117 | void StopAll(); |
| 118 | |
Myles Watson | ec3dcbf | 2019-05-15 07:31:57 -0700 | [diff] [blame] | 119 | protected: |
Hansong Zhang | 30fa258 | 2019-05-07 16:40:45 -0700 | [diff] [blame] | 120 | Module* Get(const ModuleFactory* module) const; |
Myles Watson | ec3dcbf | 2019-05-15 07:31:57 -0700 | [diff] [blame] | 121 | |
Hansong Zhang | 7079983 | 2019-05-09 16:40:37 -0700 | [diff] [blame] | 122 | os::Handler* GetModuleHandler(const ModuleFactory* module) const; |
| 123 | |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 124 | std::map<const ModuleFactory*, Module*> started_modules_; |
| 125 | std::vector<const ModuleFactory*> start_order_; |
| 126 | }; |
| 127 | |
Myles Watson | ec3dcbf | 2019-05-15 07:31:57 -0700 | [diff] [blame] | 128 | class TestModuleRegistry : public ModuleRegistry { |
| 129 | public: |
| 130 | void InjectTestModule(const ModuleFactory* module, Module* instance) { |
| 131 | start_order_.push_back(module); |
| 132 | started_modules_[module] = instance; |
| 133 | } |
| 134 | |
| 135 | Module* GetModuleUnderTest(const ModuleFactory* module) const { |
| 136 | return Get(module); |
| 137 | } |
| 138 | |
Hansong Zhang | 7079983 | 2019-05-09 16:40:37 -0700 | [diff] [blame] | 139 | os::Handler* GetTestModuleHandler(const ModuleFactory* module) const { |
| 140 | return GetModuleHandler(module); |
Myles Watson | ec3dcbf | 2019-05-15 07:31:57 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | os::Thread& GetTestThread() { |
| 144 | return test_thread; |
| 145 | } |
| 146 | |
| 147 | template <class T> |
| 148 | T* StartTestModule() { |
| 149 | return Start<T>(&test_thread); |
| 150 | } |
| 151 | |
Hansong Zhang | 7079983 | 2019-05-09 16:40:37 -0700 | [diff] [blame] | 152 | 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 Watson | ec3dcbf | 2019-05-15 07:31:57 -0700 | [diff] [blame] | 159 | os::Thread test_thread{"test_thread", os::Thread::Priority::NORMAL}; |
| 160 | }; |
| 161 | |
Zach Johnson | 429c94b | 2019-04-25 22:24:54 -0700 | [diff] [blame] | 162 | } // namespace bluetooth |