• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright 2014 Google, Inc.
4  *
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:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  ******************************************************************************/
18 
19 #define LOG_TAG "bt_core_module"
20 
21 #include <base/logging.h>
22 #include <dlfcn.h>
23 #include <string.h>
24 
25 #include <mutex>
26 #include <unordered_map>
27 
28 #include "btcore/include/module.h"
29 #include "common/message_loop_thread.h"
30 #include "osi/include/allocator.h"
31 #include "osi/include/log.h"
32 #include "osi/include/osi.h"
33 
34 using bluetooth::common::MessageLoopThread;
35 
36 typedef enum {
37   MODULE_STATE_NONE = 0,
38   MODULE_STATE_INITIALIZED = 1,
39   MODULE_STATE_STARTED = 2
40 } module_state_t;
41 
42 static std::unordered_map<const module_t*, module_state_t> metadata;
43 
44 // TODO(jamuraa): remove this lock after the startup sequence is clean
45 static std::mutex metadata_mutex;
46 
47 static bool call_lifecycle_function(module_lifecycle_fn function);
48 static module_state_t get_module_state(const module_t* module);
49 static void set_module_state(const module_t* module, module_state_t state);
50 
module_management_start(void)51 void module_management_start(void) {}
52 
module_management_stop(void)53 void module_management_stop(void) {
54   metadata.clear();
55 }
56 
get_module(const char * name)57 const module_t* get_module(const char* name) {
58   module_t* module = (module_t*)dlsym(RTLD_DEFAULT, name);
59   CHECK(module);
60   return module;
61 }
62 
module_init(const module_t * module)63 bool module_init(const module_t* module) {
64   CHECK(module != NULL);
65   CHECK(get_module_state(module) == MODULE_STATE_NONE);
66 
67   if (!call_lifecycle_function(module->init)) {
68     LOG_ERROR(LOG_TAG, "%s Failed to initialize module \"%s\"", __func__,
69               module->name);
70     return false;
71   }
72 
73   set_module_state(module, MODULE_STATE_INITIALIZED);
74   return true;
75 }
76 
module_start_up(const module_t * module)77 bool module_start_up(const module_t* module) {
78   CHECK(module != NULL);
79   // TODO(zachoverflow): remove module->init check once automagic order/call is
80   // in place.
81   // This hack is here so modules which don't require init don't have to have
82   // useless calls
83   // as we're converting the startup sequence.
84   CHECK(get_module_state(module) == MODULE_STATE_INITIALIZED ||
85         module->init == NULL);
86 
87   LOG_INFO(LOG_TAG, "%s Starting module \"%s\"", __func__, module->name);
88   if (!call_lifecycle_function(module->start_up)) {
89     LOG_ERROR(LOG_TAG, "%s Failed to start up module \"%s\"", __func__,
90               module->name);
91     return false;
92   }
93   LOG_INFO(LOG_TAG, "%s Started module \"%s\"", __func__, module->name);
94 
95   set_module_state(module, MODULE_STATE_STARTED);
96   return true;
97 }
98 
module_shut_down(const module_t * module)99 void module_shut_down(const module_t* module) {
100   CHECK(module != NULL);
101   module_state_t state = get_module_state(module);
102   CHECK(state <= MODULE_STATE_STARTED);
103 
104   // Only something to do if the module was actually started
105   if (state < MODULE_STATE_STARTED) return;
106 
107   LOG_INFO(LOG_TAG, "%s Shutting down module \"%s\"", __func__, module->name);
108   if (!call_lifecycle_function(module->shut_down)) {
109     LOG_ERROR(LOG_TAG,
110               "%s Failed to shutdown module \"%s\". Continuing anyway.",
111               __func__, module->name);
112   }
113   LOG_INFO(LOG_TAG, "%s Shutdown of module \"%s\" completed", __func__,
114            module->name);
115 
116   set_module_state(module, MODULE_STATE_INITIALIZED);
117 }
118 
module_clean_up(const module_t * module)119 void module_clean_up(const module_t* module) {
120   CHECK(module != NULL);
121   module_state_t state = get_module_state(module);
122   CHECK(state <= MODULE_STATE_INITIALIZED);
123 
124   // Only something to do if the module was actually initialized
125   if (state < MODULE_STATE_INITIALIZED) return;
126 
127   LOG_INFO(LOG_TAG, "%s Cleaning up module \"%s\"", __func__, module->name);
128   if (!call_lifecycle_function(module->clean_up)) {
129     LOG_ERROR(LOG_TAG, "%s Failed to cleanup module \"%s\". Continuing anyway.",
130               __func__, module->name);
131   }
132   LOG_INFO(LOG_TAG, "%s Cleanup of module \"%s\" completed", __func__,
133            module->name);
134 
135   set_module_state(module, MODULE_STATE_NONE);
136 }
137 
call_lifecycle_function(module_lifecycle_fn function)138 static bool call_lifecycle_function(module_lifecycle_fn function) {
139   // A NULL lifecycle function means it isn't needed, so assume success
140   if (!function) return true;
141 
142   future_t* future = function();
143 
144   // A NULL future means synchronous success
145   if (!future) return true;
146 
147   // Otherwise fall back to the future
148   return future_await(future);
149 }
150 
get_module_state(const module_t * module)151 static module_state_t get_module_state(const module_t* module) {
152   std::lock_guard<std::mutex> lock(metadata_mutex);
153   auto map_ptr = metadata.find(module);
154 
155   return (map_ptr != metadata.end()) ? map_ptr->second : MODULE_STATE_NONE;
156 }
157 
set_module_state(const module_t * module,module_state_t state)158 static void set_module_state(const module_t* module, module_state_t state) {
159   std::lock_guard<std::mutex> lock(metadata_mutex);
160   metadata[module] = state;
161 }
162 
163 // TODO(zachoverflow): remove when everything modulized
164 // Temporary callback-wrapper-related code
165 class CallbackWrapper {
166  public:
CallbackWrapper(const module_t * module,MessageLoopThread * callback_thread,thread_fn callback)167   explicit CallbackWrapper(const module_t* module,
168                            MessageLoopThread* callback_thread,
169                            thread_fn callback)
170       : module(module),
171         lifecycle_thread("bt_module_lifecycle_thread"),
172         callback_thread(callback_thread),
173         callback(callback),
174         success(false) {}
175   const module_t* module;
176   MessageLoopThread lifecycle_thread;
177   // we don't own this thread
178   MessageLoopThread* callback_thread;
179   thread_fn callback;
180   bool success;
181 };
182 
post_result_to_callback(std::shared_ptr<CallbackWrapper> wrapper)183 static void post_result_to_callback(std::shared_ptr<CallbackWrapper> wrapper) {
184   CHECK(wrapper);
185   wrapper->lifecycle_thread.ShutDown();
186   wrapper->callback(wrapper->success ? FUTURE_SUCCESS : FUTURE_FAIL);
187 }
188 
run_wrapped_start_up(std::shared_ptr<CallbackWrapper> wrapper)189 static void run_wrapped_start_up(std::shared_ptr<CallbackWrapper> wrapper) {
190   CHECK(wrapper);
191   wrapper->success = module_start_up(wrapper->module);
192   // Post the result back to the callback
193   wrapper->callback_thread->DoInThread(
194       FROM_HERE, base::BindOnce(post_result_to_callback, wrapper));
195 }
196 
module_start_up_callbacked_wrapper(const module_t * module,MessageLoopThread * callback_thread,thread_fn callback)197 void module_start_up_callbacked_wrapper(const module_t* module,
198                                         MessageLoopThread* callback_thread,
199                                         thread_fn callback) {
200   std::shared_ptr<CallbackWrapper> wrapper =
201       std::make_shared<CallbackWrapper>(module, callback_thread, callback);
202   wrapper->lifecycle_thread.StartUp();
203   // Run the actual module start up
204   wrapper->lifecycle_thread.DoInThread(
205       FROM_HERE, base::BindOnce(run_wrapped_start_up, wrapper));
206 }
207