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("%s Failed to initialize module \"%s\"", __func__, module->name);
69     return false;
70   }
71 
72   set_module_state(module, MODULE_STATE_INITIALIZED);
73   return true;
74 }
75 
module_start_up(const module_t * module)76 bool module_start_up(const module_t* module) {
77   CHECK(module != NULL);
78   // TODO(zachoverflow): remove module->init check once automagic order/call is
79   // in place.
80   // This hack is here so modules which don't require init don't have to have
81   // useless calls
82   // as we're converting the startup sequence.
83   CHECK(get_module_state(module) == MODULE_STATE_INITIALIZED ||
84         module->init == NULL);
85 
86   LOG_INFO("%s Starting module \"%s\"", __func__, module->name);
87   if (!call_lifecycle_function(module->start_up)) {
88     LOG_ERROR("%s Failed to start up module \"%s\"", __func__, module->name);
89     return false;
90   }
91   LOG_INFO("%s Started module \"%s\"", __func__, module->name);
92 
93   set_module_state(module, MODULE_STATE_STARTED);
94   return true;
95 }
96 
module_shut_down(const module_t * module)97 void module_shut_down(const module_t* module) {
98   CHECK(module != NULL);
99   module_state_t state = get_module_state(module);
100   CHECK(state <= MODULE_STATE_STARTED);
101 
102   // Only something to do if the module was actually started
103   if (state < MODULE_STATE_STARTED) return;
104 
105   LOG_INFO("%s Shutting down module \"%s\"", __func__, module->name);
106   if (!call_lifecycle_function(module->shut_down)) {
107     LOG_ERROR("%s Failed to shutdown module \"%s\". Continuing anyway.",
108               __func__, module->name);
109   }
110   LOG_INFO("%s Shutdown of module \"%s\" completed", __func__, module->name);
111 
112   set_module_state(module, MODULE_STATE_INITIALIZED);
113 }
114 
module_clean_up(const module_t * module)115 void module_clean_up(const module_t* module) {
116   CHECK(module != NULL);
117   module_state_t state = get_module_state(module);
118   CHECK(state <= MODULE_STATE_INITIALIZED);
119 
120   // Only something to do if the module was actually initialized
121   if (state < MODULE_STATE_INITIALIZED) return;
122 
123   LOG_INFO("%s Cleaning up module \"%s\"", __func__, module->name);
124   if (!call_lifecycle_function(module->clean_up)) {
125     LOG_ERROR("%s Failed to cleanup module \"%s\". Continuing anyway.",
126               __func__, module->name);
127   }
128   LOG_INFO("%s Cleanup of module \"%s\" completed", __func__, module->name);
129 
130   set_module_state(module, MODULE_STATE_NONE);
131 }
132 
call_lifecycle_function(module_lifecycle_fn function)133 static bool call_lifecycle_function(module_lifecycle_fn function) {
134   // A NULL lifecycle function means it isn't needed, so assume success
135   if (!function) return true;
136 
137   future_t* future = function();
138 
139   // A NULL future means synchronous success
140   if (!future) return true;
141 
142   // Otherwise fall back to the future
143   return future_await(future);
144 }
145 
get_module_state(const module_t * module)146 static module_state_t get_module_state(const module_t* module) {
147   std::lock_guard<std::mutex> lock(metadata_mutex);
148   auto map_ptr = metadata.find(module);
149 
150   return (map_ptr != metadata.end()) ? map_ptr->second : MODULE_STATE_NONE;
151 }
152 
set_module_state(const module_t * module,module_state_t state)153 static void set_module_state(const module_t* module, module_state_t state) {
154   std::lock_guard<std::mutex> lock(metadata_mutex);
155   metadata[module] = state;
156 }
157 
158 // TODO(zachoverflow): remove when everything modulized
159 // Temporary callback-wrapper-related code
160 class CallbackWrapper {
161  public:
CallbackWrapper(const module_t * module,MessageLoopThread * callback_thread,thread_fn callback)162   explicit CallbackWrapper(const module_t* module,
163                            MessageLoopThread* callback_thread,
164                            thread_fn callback)
165       : module(module),
166         lifecycle_thread("bt_module_lifecycle_thread"),
167         callback_thread(callback_thread),
168         callback(callback),
169         success(false) {}
170   const module_t* module;
171   MessageLoopThread lifecycle_thread;
172   // we don't own this thread
173   MessageLoopThread* callback_thread;
174   thread_fn callback;
175   bool success;
176 };
177 
post_result_to_callback(std::shared_ptr<CallbackWrapper> wrapper)178 static void post_result_to_callback(std::shared_ptr<CallbackWrapper> wrapper) {
179   CHECK(wrapper);
180   wrapper->lifecycle_thread.ShutDown();
181   wrapper->callback(wrapper->success ? FUTURE_SUCCESS : FUTURE_FAIL);
182 }
183 
run_wrapped_start_up(std::shared_ptr<CallbackWrapper> wrapper)184 static void run_wrapped_start_up(std::shared_ptr<CallbackWrapper> wrapper) {
185   CHECK(wrapper);
186   wrapper->success = module_start_up(wrapper->module);
187   // Post the result back to the callback
188   wrapper->callback_thread->DoInThread(
189       FROM_HERE, base::BindOnce(post_result_to_callback, wrapper));
190 }
191 
module_start_up_callbacked_wrapper(const module_t * module,MessageLoopThread * callback_thread,thread_fn callback)192 void module_start_up_callbacked_wrapper(const module_t* module,
193                                         MessageLoopThread* callback_thread,
194                                         thread_fn callback) {
195   std::shared_ptr<CallbackWrapper> wrapper =
196       std::make_shared<CallbackWrapper>(module, callback_thread, callback);
197   wrapper->lifecycle_thread.StartUp();
198   // Run the actual module start up
199   wrapper->lifecycle_thread.DoInThread(
200       FROM_HERE, base::BindOnce(run_wrapped_start_up, wrapper));
201 }
202