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