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