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 "btcore/include/module.h"
22 
23 #include <base/logging.h>
24 #include <dlfcn.h>
25 #include <string.h>
26 
27 #include <mutex>
28 #include <unordered_map>
29 
30 #include "common/message_loop_thread.h"
31 #include "osi/include/allocator.h"
32 #include "osi/include/log.h"
33 #include "osi/include/osi.h"
34 
35 using bluetooth::common::MessageLoopThread;
36 
37 typedef enum {
38   MODULE_STATE_NONE = 0,
39   MODULE_STATE_INITIALIZED = 1,
40   MODULE_STATE_STARTED = 2
41 } module_state_t;
42 
43 static std::unordered_map<const module_t*, module_state_t> metadata;
44 
45 // TODO(jamuraa): remove this lock after the startup sequence is clean
46 static std::mutex metadata_mutex;
47 
48 static bool call_lifecycle_function(module_lifecycle_fn function);
49 static module_state_t get_module_state(const module_t* module);
50 static void set_module_state(const module_t* module, module_state_t state);
51 
module_management_start(void)52 void module_management_start(void) {}
53 
module_management_stop(void)54 void module_management_stop(void) { metadata.clear(); }
55 
get_module(const char * name)56 const module_t* get_module(const char* name) {
57   module_t* module = (module_t*)dlsym(RTLD_DEFAULT, name);
58   CHECK(module);
59   return module;
60 }
61 
module_init(const module_t * module)62 bool module_init(const module_t* module) {
63   CHECK(module != NULL);
64   CHECK(get_module_state(module) == MODULE_STATE_NONE);
65 
66   if (!call_lifecycle_function(module->init)) {
67     LOG_ERROR("%s Failed to initialize module \"%s\"", __func__, module->name);
68     return false;
69   }
70 
71   set_module_state(module, MODULE_STATE_INITIALIZED);
72   return true;
73 }
74 
module_start_up(const module_t * module)75 bool module_start_up(const module_t* module) {
76   CHECK(module != NULL);
77   // TODO(zachoverflow): remove module->init check once automagic order/call is
78   // in place.
79   // This hack is here so modules which don't require init don't have to have
80   // useless calls
81   // as we're converting the startup sequence.
82   CHECK(get_module_state(module) == MODULE_STATE_INITIALIZED ||
83         module->init == NULL);
84 
85   LOG_INFO("%s Starting module \"%s\"", __func__, module->name);
86   if (!call_lifecycle_function(module->start_up)) {
87     LOG_ERROR("%s Failed to start up module \"%s\"", __func__, module->name);
88     return false;
89   }
90   LOG_INFO("%s Started module \"%s\"", __func__, module->name);
91 
92   set_module_state(module, MODULE_STATE_STARTED);
93   return true;
94 }
95 
module_shut_down(const module_t * module)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);
100 
101   // Only something to do if the module was actually started
102   if (state < MODULE_STATE_STARTED) return;
103 
104   LOG_INFO("%s Shutting down module \"%s\"", __func__, module->name);
105   if (!call_lifecycle_function(module->shut_down)) {
106     LOG_ERROR("%s Failed to shutdown module \"%s\". Continuing anyway.",
107               __func__, module->name);
108   }
109   LOG_INFO("%s Shutdown of module \"%s\" completed", __func__, module->name);
110 
111   set_module_state(module, MODULE_STATE_INITIALIZED);
112 }
113 
module_clean_up(const module_t * module)114 void module_clean_up(const module_t* module) {
115   CHECK(module != NULL);
116   module_state_t state = get_module_state(module);
117   CHECK(state <= MODULE_STATE_INITIALIZED);
118 
119   // Only something to do if the module was actually initialized
120   if (state < MODULE_STATE_INITIALIZED) return;
121 
122   LOG_INFO("%s Cleaning up module \"%s\"", __func__, module->name);
123   if (!call_lifecycle_function(module->clean_up)) {
124     LOG_ERROR("%s Failed to cleanup module \"%s\". Continuing anyway.",
125               __func__, module->name);
126   }
127   LOG_INFO("%s Cleanup of module \"%s\" completed", __func__, module->name);
128 
129   set_module_state(module, MODULE_STATE_NONE);
130 }
131 
call_lifecycle_function(module_lifecycle_fn function)132 static bool call_lifecycle_function(module_lifecycle_fn function) {
133   // A NULL lifecycle function means it isn't needed, so assume success
134   if (!function) return true;
135 
136   future_t* future = function();
137 
138   // A NULL future means synchronous success
139   if (!future) return true;
140 
141   // Otherwise fall back to the future
142   return future_await(future);
143 }
144 
get_module_state(const module_t * module)145 static module_state_t get_module_state(const module_t* module) {
146   std::lock_guard<std::mutex> lock(metadata_mutex);
147   auto map_ptr = metadata.find(module);
148 
149   return (map_ptr != metadata.end()) ? map_ptr->second : MODULE_STATE_NONE;
150 }
151 
set_module_state(const module_t * module,module_state_t state)152 static void set_module_state(const module_t* module, module_state_t state) {
153   std::lock_guard<std::mutex> lock(metadata_mutex);
154   metadata[module] = state;
155 }
156