1/******************************************************************************
2 *
3 *  Copyright (C) 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 <assert.h>
22#include <dlfcn.h>
23#include <pthread.h>
24#include <string.h>
25
26#include "osi/include/allocator.h"
27#include "osi/include/hash_map.h"
28#include "btcore/include/module.h"
29#include "osi/include/osi.h"
30#include "osi/include/hash_functions.h"
31#include "osi/include/log.h"
32
33typedef enum {
34  MODULE_STATE_NONE = 0,
35  MODULE_STATE_INITIALIZED = 1,
36  MODULE_STATE_STARTED = 2
37} module_state_t;
38
39static const size_t number_of_metadata_buckets = 42;
40static hash_map_t *metadata;
41// Include this lock for now for correctness, while the startup sequence is being refactored
42static pthread_mutex_t metadata_lock;
43
44static bool call_lifecycle_function(module_lifecycle_fn function);
45static module_state_t get_module_state(const module_t *module);
46static void set_module_state(const module_t *module, module_state_t state);
47
48void module_management_start(void) {
49  metadata = hash_map_new(
50    number_of_metadata_buckets,
51    hash_function_pointer,
52    NULL,
53    osi_free,
54    NULL
55  );
56
57  pthread_mutex_init(&metadata_lock, NULL);
58}
59
60void module_management_stop(void) {
61  if (!metadata)
62    return;
63
64  hash_map_free(metadata);
65  metadata = NULL;
66
67  pthread_mutex_destroy(&metadata_lock);
68}
69
70const module_t *get_module(const char *name) {
71  module_t* module = (module_t *)dlsym(RTLD_DEFAULT, name);
72  assert(module);
73  return module;
74}
75
76bool module_init(const module_t *module) {
77  assert(metadata != NULL);
78  assert(module != NULL);
79  assert(get_module_state(module) == MODULE_STATE_NONE);
80
81  if (!call_lifecycle_function(module->init)) {
82    LOG_ERROR("%s failed to initialize \"%s\"", __func__, module->name);
83    return false;
84  }
85
86  set_module_state(module, MODULE_STATE_INITIALIZED);
87  return true;
88}
89
90bool module_start_up(const module_t *module) {
91  assert(metadata != NULL);
92  assert(module != NULL);
93  // TODO(zachoverflow): remove module->init check once automagic order/call is in place.
94  // This hack is here so modules which don't require init don't have to have useless calls
95  // as we're converting the startup sequence.
96  assert(get_module_state(module) == MODULE_STATE_INITIALIZED || module->init == NULL);
97
98  if (!call_lifecycle_function(module->start_up)) {
99    LOG_ERROR("%s failed to start up \"%s\"", __func__, module->name);
100    return false;
101  }
102
103  set_module_state(module, MODULE_STATE_STARTED);
104  return true;
105}
106
107void module_shut_down(const module_t *module) {
108  assert(metadata != NULL);
109  assert(module != NULL);
110  module_state_t state = get_module_state(module);
111  assert(state <= MODULE_STATE_STARTED);
112
113  // Only something to do if the module was actually started
114  if (state < MODULE_STATE_STARTED)
115    return;
116
117  if (!call_lifecycle_function(module->shut_down))
118    LOG_ERROR("%s found \"%s\" reported failure during shutdown. Continuing anyway.", __func__, module->name);
119
120  set_module_state(module, MODULE_STATE_INITIALIZED);
121}
122
123void module_clean_up(const module_t *module) {
124  assert(metadata != NULL);
125  assert(module != NULL);
126  module_state_t state = get_module_state(module);
127  assert(state <= MODULE_STATE_INITIALIZED);
128
129  // Only something to do if the module was actually initialized
130  if (state < MODULE_STATE_INITIALIZED)
131    return;
132
133  if (!call_lifecycle_function(module->clean_up))
134    LOG_ERROR("%s found \"%s\" reported failure during cleanup. Continuing anyway.", __func__, module->name);
135
136  set_module_state(module, MODULE_STATE_NONE);
137}
138
139static bool call_lifecycle_function(module_lifecycle_fn function) {
140  // A NULL lifecycle function means it isn't needed, so assume success
141  if (!function)
142    return true;
143
144  future_t *future = function();
145
146  // A NULL future means synchronous success
147  if (!future)
148    return true;
149
150  // Otherwise fall back to the future
151  return future_await(future);
152}
153
154static module_state_t get_module_state(const module_t *module) {
155  pthread_mutex_lock(&metadata_lock);
156  module_state_t *state_ptr = hash_map_get(metadata, module);
157  pthread_mutex_unlock(&metadata_lock);
158
159  return state_ptr ? *state_ptr : MODULE_STATE_NONE;
160}
161
162static void set_module_state(const module_t *module, module_state_t state) {
163  pthread_mutex_lock(&metadata_lock);
164
165  module_state_t *state_ptr = hash_map_get(metadata, module);
166  if (!state_ptr) {
167    state_ptr = osi_malloc(sizeof(module_state_t));
168    hash_map_set(metadata, module, state_ptr);
169  }
170
171  pthread_mutex_unlock(&metadata_lock);
172
173  *state_ptr = state;
174}
175
176// TODO(zachoverflow): remove when everything modulized
177// Temporary callback-wrapper-related code
178
179typedef struct {
180  const module_t *module;
181  thread_t *lifecycle_thread;
182  thread_t *callback_thread; // we don't own this thread
183  thread_fn callback;
184  bool success;
185} callbacked_wrapper_t;
186
187static void run_wrapped_start_up(void *context);
188static void post_result_to_callback(void *context);
189
190void module_start_up_callbacked_wrapper(
191    const module_t *module,
192    thread_t *callback_thread,
193    thread_fn callback) {
194  callbacked_wrapper_t *wrapper = osi_calloc(sizeof(callbacked_wrapper_t));
195
196  wrapper->module = module;
197  wrapper->lifecycle_thread = thread_new("module_wrapper");
198  wrapper->callback_thread = callback_thread;
199  wrapper->callback = callback;
200
201  // Run the actual module start up
202  thread_post(wrapper->lifecycle_thread, run_wrapped_start_up, wrapper);
203}
204
205static void run_wrapped_start_up(void *context) {
206  assert(context);
207
208  callbacked_wrapper_t *wrapper = context;
209  wrapper->success = module_start_up(wrapper->module);
210
211  // Post the result back to the callback
212  thread_post(wrapper->callback_thread, post_result_to_callback, wrapper);
213}
214
215static void post_result_to_callback(void *context) {
216  assert(context);
217
218  callbacked_wrapper_t *wrapper = context;
219
220  // Save the values we need for callback
221  void *result = wrapper->success ? FUTURE_SUCCESS : FUTURE_FAIL;
222  thread_fn callback = wrapper->callback;
223
224  // Clean up the resources we used
225  thread_stop(wrapper->lifecycle_thread);
226  osi_free(wrapper);
227
228  callback(result);
229}
230