1 /******************************************************************************
3 * Copyright 2014 Google, Inc.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 ******************************************************************************/
19 #define LOG_TAG "bt_core_module"
21 #include <base/logging.h>
26 #include <unordered_map>
28 #include "btcore/include/module.h"
29 #include "osi/include/allocator.h"
30 #include "osi/include/log.h"
31 #include "osi/include/osi.h"
34 MODULE_STATE_NONE = 0,
35 MODULE_STATE_INITIALIZED = 1,
36 MODULE_STATE_STARTED = 2
39 static std::unordered_map<const module_t*, module_state_t> metadata;
41 // TODO(jamuraa): remove this lock after the startup sequence is clean
42 static std::mutex metadata_mutex;
44 static bool call_lifecycle_function(module_lifecycle_fn function);
45 static module_state_t get_module_state(const module_t* module);
46 static void set_module_state(const module_t* module, module_state_t state);
48 void module_management_start(void) {}
50 void module_management_stop(void) {
54 const module_t* get_module(const char* name) {
55 module_t* module = (module_t*)dlsym(RTLD_DEFAULT, name);
60 bool module_init(const module_t* module) {
61 CHECK(module != NULL);
62 CHECK(get_module_state(module) == MODULE_STATE_NONE);
64 if (!call_lifecycle_function(module->init)) {
65 LOG_ERROR(LOG_TAG, "%s Failed to initialize module \"%s\"", __func__,
70 set_module_state(module, MODULE_STATE_INITIALIZED);
74 bool module_start_up(const module_t* module) {
75 CHECK(module != NULL);
76 // TODO(zachoverflow): remove module->init check once automagic order/call is
78 // This hack is here so modules which don't require init don't have to have
80 // as we're converting the startup sequence.
81 CHECK(get_module_state(module) == MODULE_STATE_INITIALIZED ||
82 module->init == NULL);
84 LOG_INFO(LOG_TAG, "%s Starting module \"%s\"", __func__, module->name);
85 if (!call_lifecycle_function(module->start_up)) {
86 LOG_ERROR(LOG_TAG, "%s Failed to start up module \"%s\"", __func__,
90 LOG_INFO(LOG_TAG, "%s Started module \"%s\"", __func__, module->name);
92 set_module_state(module, MODULE_STATE_STARTED);
96 void module_shut_down(const module_t* module) {
97 CHECK(module != NULL);
98 module_state_t state = get_module_state(module);
99 CHECK(state <= MODULE_STATE_STARTED);
101 // Only something to do if the module was actually started
102 if (state < MODULE_STATE_STARTED) return;
104 LOG_INFO(LOG_TAG, "%s Shutting down module \"%s\"", __func__, module->name);
105 if (!call_lifecycle_function(module->shut_down)) {
107 "%s Failed to shutdown module \"%s\". Continuing anyway.",
108 __func__, module->name);
110 LOG_INFO(LOG_TAG, "%s Shutdown of module \"%s\" completed", __func__,
113 set_module_state(module, MODULE_STATE_INITIALIZED);
116 void module_clean_up(const module_t* module) {
117 CHECK(module != NULL);
118 module_state_t state = get_module_state(module);
119 CHECK(state <= MODULE_STATE_INITIALIZED);
121 // Only something to do if the module was actually initialized
122 if (state < MODULE_STATE_INITIALIZED) return;
124 LOG_INFO(LOG_TAG, "%s Cleaning up module \"%s\"", __func__, module->name);
125 if (!call_lifecycle_function(module->clean_up)) {
126 LOG_ERROR(LOG_TAG, "%s Failed to cleanup module \"%s\". Continuing anyway.",
127 __func__, module->name);
129 LOG_INFO(LOG_TAG, "%s Cleanup of module \"%s\" completed", __func__,
132 set_module_state(module, MODULE_STATE_NONE);
135 static bool call_lifecycle_function(module_lifecycle_fn function) {
136 // A NULL lifecycle function means it isn't needed, so assume success
137 if (!function) return true;
139 future_t* future = function();
141 // A NULL future means synchronous success
142 if (!future) return true;
144 // Otherwise fall back to the future
145 return future_await(future);
148 static module_state_t get_module_state(const module_t* module) {
149 std::lock_guard<std::mutex> lock(metadata_mutex);
150 auto map_ptr = metadata.find(module);
152 return (map_ptr != metadata.end()) ? map_ptr->second : MODULE_STATE_NONE;
155 static void set_module_state(const module_t* module, module_state_t state) {
156 std::lock_guard<std::mutex> lock(metadata_mutex);
157 metadata[module] = state;
160 // TODO(zachoverflow): remove when everything modulized
161 // Temporary callback-wrapper-related code
164 const module_t* module;
165 thread_t* lifecycle_thread;
166 thread_t* callback_thread; // we don't own this thread
169 } callbacked_wrapper_t;
171 static void run_wrapped_start_up(void* context);
172 static void post_result_to_callback(void* context);
174 void module_start_up_callbacked_wrapper(const module_t* module,
175 thread_t* callback_thread,
176 thread_fn callback) {
177 callbacked_wrapper_t* wrapper =
178 (callbacked_wrapper_t*)osi_calloc(sizeof(callbacked_wrapper_t));
180 wrapper->module = module;
181 wrapper->lifecycle_thread = thread_new("module_wrapper");
182 wrapper->callback_thread = callback_thread;
183 wrapper->callback = callback;
185 // Run the actual module start up
186 thread_post(wrapper->lifecycle_thread, run_wrapped_start_up, wrapper);
189 static void run_wrapped_start_up(void* context) {
192 callbacked_wrapper_t* wrapper = (callbacked_wrapper_t*)context;
193 wrapper->success = module_start_up(wrapper->module);
195 // Post the result back to the callback
196 thread_post(wrapper->callback_thread, post_result_to_callback, wrapper);
199 static void post_result_to_callback(void* context) {
202 callbacked_wrapper_t* wrapper = (callbacked_wrapper_t*)context;
204 // Save the values we need for callback
205 void* result = wrapper->success ? FUTURE_SUCCESS : FUTURE_FAIL;
206 thread_fn callback = wrapper->callback;
208 // Clean up the resources we used
209 thread_free(wrapper->lifecycle_thread);