Zach Johnson | 72f308e | 2014-09-22 22:14:04 -0700 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
Jakub Pawlowski | 5b790fe | 2017-09-18 09:00:20 -0700 | [diff] [blame] | 3 | * Copyright 2014 Google, Inc. |
Zach Johnson | 72f308e | 2014-09-22 22:14:04 -0700 | [diff] [blame] | 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 | #pragma once |
| 20 | |
| 21 | #include <stdbool.h> |
| 22 | |
Jack He | 6d11052 | 2018-07-25 21:43:01 -0700 | [diff] [blame] | 23 | #include "common/message_loop_thread.h" |
Chris Manton | 55fe5dc | 2014-11-06 17:08:20 -0800 | [diff] [blame] | 24 | #include "osi/include/future.h" |
| 25 | #include "osi/include/thread.h" |
Zach Johnson | 72f308e | 2014-09-22 22:14:04 -0700 | [diff] [blame] | 26 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 27 | typedef future_t* (*module_lifecycle_fn)(void); |
Zach Johnson | 72f308e | 2014-09-22 22:14:04 -0700 | [diff] [blame] | 28 | |
Pavlin Radoslavov | b2a292b | 2016-10-14 19:34:48 -0700 | [diff] [blame] | 29 | #define BTCORE_MAX_MODULE_DEPENDENCIES 10 |
| 30 | |
Zach Johnson | 72f308e | 2014-09-22 22:14:04 -0700 | [diff] [blame] | 31 | typedef struct { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 32 | const char* name; |
Zach Johnson | 72f308e | 2014-09-22 22:14:04 -0700 | [diff] [blame] | 33 | module_lifecycle_fn init; |
| 34 | module_lifecycle_fn start_up; |
| 35 | module_lifecycle_fn shut_down; |
| 36 | module_lifecycle_fn clean_up; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 37 | const char* dependencies[BTCORE_MAX_MODULE_DEPENDENCIES]; |
Zach Johnson | 72f308e | 2014-09-22 22:14:04 -0700 | [diff] [blame] | 38 | } module_t; |
| 39 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 40 | // Prepares module management. Must be called before doing anything with |
| 41 | // modules. |
Zach Johnson | 72f308e | 2014-09-22 22:14:04 -0700 | [diff] [blame] | 42 | void module_management_start(void); |
| 43 | // Cleans up all module management resources. |
| 44 | void module_management_stop(void); |
| 45 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 46 | const module_t* get_module(const char* name); |
Zach Johnson | 72f308e | 2014-09-22 22:14:04 -0700 | [diff] [blame] | 47 | |
| 48 | // Initialize the provided module. |module| may not be NULL |
| 49 | // and must not be initialized. |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 50 | bool module_init(const module_t* module); |
Zach Johnson | 72f308e | 2014-09-22 22:14:04 -0700 | [diff] [blame] | 51 | // Start up the provided module. |module| may not be NULL |
| 52 | // and must be initialized or have no init function. |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 53 | bool module_start_up(const module_t* module); |
Zach Johnson | 72f308e | 2014-09-22 22:14:04 -0700 | [diff] [blame] | 54 | // Shut down the provided module. |module| may not be NULL. |
| 55 | // If not started, does nothing. |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 56 | void module_shut_down(const module_t* module); |
Zach Johnson | 72f308e | 2014-09-22 22:14:04 -0700 | [diff] [blame] | 57 | // Clean up the provided module. |module| may not be NULL. |
| 58 | // If not initialized, does nothing. |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 59 | void module_clean_up(const module_t* module); |
Zach Johnson | 72f308e | 2014-09-22 22:14:04 -0700 | [diff] [blame] | 60 | |
Zach Johnson | 3c511e6 | 2014-09-28 15:46:25 -0700 | [diff] [blame] | 61 | // Temporary callbacked wrapper for module start up, so real modules can be |
| 62 | // spliced into the current janky startup sequence. Runs on a separate thread, |
| 63 | // which terminates when the module start up has finished. When module startup |
| 64 | // has finished, |callback| is called within the context of |callback_thread| |
| 65 | // with |FUTURE_SUCCESS| or |FUTURE_FAIL| depending on whether startup succeeded |
| 66 | // or not. |
Jack He | 6d11052 | 2018-07-25 21:43:01 -0700 | [diff] [blame] | 67 | void module_start_up_callbacked_wrapper( |
| 68 | const module_t* module, |
| 69 | bluetooth::common::MessageLoopThread* callback_thread, thread_fn callback); |