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 #include "os/log.h"
32 
33 using bluetooth::common::MessageLoopThread;
34 using namespace bluetooth;
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) { metadata.clear(); }
54 
get_module(const char * name)55 const module_t* get_module(const char* name) {
56   module_t* module = (module_t*)dlsym(RTLD_DEFAULT, name);
57   log::assert_that(module != nullptr, "assert failed: module != nullptr");
58   return module;
59 }
60 
module_init(const module_t * module)61 bool module_init(const module_t* module) {
62   log::assert_that(module != NULL, "assert failed: module != NULL");
63   log::assert_that(
64       get_module_state(module) == MODULE_STATE_NONE,
65       "assert failed: get_module_state(module) == MODULE_STATE_NONE");
66 
67   if (!call_lifecycle_function(module->init)) {
68     log::error("Failed to initialize module \"{}\"", 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   log::assert_that(module != NULL, "assert failed: 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   log::assert_that(get_module_state(module) == MODULE_STATE_INITIALIZED ||
84                        module->init == NULL,
85                    "assert failed: get_module_state(module) == "
86                    "MODULE_STATE_INITIALIZED || module->init == NULL");
87 
88   log::info("Starting module \"{}\"", module->name);
89   if (!call_lifecycle_function(module->start_up)) {
90     log::error("Failed to start up module \"{}\"", module->name);
91     return false;
92   }
93   log::info("Started module \"{}\"", 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   log::assert_that(module != NULL, "assert failed: module != NULL");
101   module_state_t state = get_module_state(module);
102   log::assert_that(state <= MODULE_STATE_STARTED,
103                    "assert failed: state <= MODULE_STATE_STARTED");
104 
105   // Only something to do if the module was actually started
106   if (state < MODULE_STATE_STARTED) return;
107 
108   log::info("Shutting down module \"{}\"", module->name);
109   if (!call_lifecycle_function(module->shut_down)) {
110     log::error("Failed to shutdown module \"{}\". Continuing anyway.",
111                module->name);
112   }
113   log::info("Shutdown of module \"{}\" completed", module->name);
114 
115   set_module_state(module, MODULE_STATE_INITIALIZED);
116 }
117 
module_clean_up(const module_t * module)118 void module_clean_up(const module_t* module) {
119   log::assert_that(module != NULL, "assert failed: module != NULL");
120   module_state_t state = get_module_state(module);
121   log::assert_that(state <= MODULE_STATE_INITIALIZED,
122                    "assert failed: 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("Cleaning up module \"{}\"", module->name);
128   if (!call_lifecycle_function(module->clean_up)) {
129     log::error("Failed to cleanup module \"{}\". Continuing anyway.",
130                module->name);
131   }
132   log::info("Cleanup of module \"{}\" completed", module->name);
133 
134   set_module_state(module, MODULE_STATE_NONE);
135 }
136 
call_lifecycle_function(module_lifecycle_fn function)137 static bool call_lifecycle_function(module_lifecycle_fn function) {
138   // A NULL lifecycle function means it isn't needed, so assume success
139   if (!function) return true;
140 
141   future_t* future = function();
142 
143   // A NULL future means synchronous success
144   if (!future) return true;
145 
146   // Otherwise fall back to the future
147   return future_await(future);
148 }
149 
get_module_state(const module_t * module)150 static module_state_t get_module_state(const module_t* module) {
151   std::lock_guard<std::mutex> lock(metadata_mutex);
152   auto map_ptr = metadata.find(module);
153 
154   return (map_ptr != metadata.end()) ? map_ptr->second : MODULE_STATE_NONE;
155 }
156 
set_module_state(const module_t * module,module_state_t state)157 static void set_module_state(const module_t* module, module_state_t state) {
158   std::lock_guard<std::mutex> lock(metadata_mutex);
159   metadata[module] = state;
160 }
161