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