• 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 "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 "check.h"
31 #include "common/message_loop_thread.h"
32 #include "osi/include/allocator.h"
33 #include "osi/include/log.h"
34 #include "osi/include/osi.h"
35 
36 using bluetooth::common::MessageLoopThread;
37 
38 typedef enum {
39   MODULE_STATE_NONE = 0,
40   MODULE_STATE_INITIALIZED = 1,
41   MODULE_STATE_STARTED = 2
42 } module_state_t;
43 
44 static std::unordered_map<const module_t*, module_state_t> metadata;
45 
46 // TODO(jamuraa): remove this lock after the startup sequence is clean
47 static std::mutex metadata_mutex;
48 
49 static bool call_lifecycle_function(module_lifecycle_fn function);
50 static module_state_t get_module_state(const module_t* module);
51 static void set_module_state(const module_t* module, module_state_t state);
52 
module_management_start(void)53 void module_management_start(void) {}
54 
module_management_stop(void)55 void module_management_stop(void) { metadata.clear(); }
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