1 /*
2  * Copyright 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "bt_shim"
18 
19 #include "main/shim/shim.h"
20 
21 #include <bluetooth/log.h>
22 
23 #include "common/init_flags.h"
24 #include "main/shim/entry.h"
25 #include "main/shim/hci_layer.h"
26 #include "main/shim/stack.h"
27 #include "os/log.h"
28 #include "stack/include/main_thread.h"
29 
30 static const hci_t* hci;
31 
32 void btu_hci_msg_process(BT_HDR* p_msg);
33 
post_to_main_message_loop(const base::Location & from_here,BT_HDR * p_msg)34 static void post_to_main_message_loop(const base::Location& from_here,
35                                       BT_HDR* p_msg) {
36   if (do_in_main_thread(from_here, base::Bind(&btu_hci_msg_process, p_msg)) !=
37       BT_STATUS_SUCCESS) {
38     bluetooth::log::error(": do_in_main_thread failed from {}",
39                           from_here.ToString());
40   }
41 }
42 
ShimModuleStartUp()43 static future_t* ShimModuleStartUp() {
44   hci = bluetooth::shim::hci_layer_get_interface();
45   bluetooth::log::assert_that(hci, "could not get hci layer interface.");
46 
47   hci->set_data_cb(base::Bind(&post_to_main_message_loop));
48 
49   bluetooth::shim::Stack::GetInstance()->StartEverything();
50   return kReturnImmediate;
51 }
52 
GeneralShutDown()53 static future_t* GeneralShutDown() {
54   bluetooth::shim::Stack::GetInstance()->Stop();
55   return kReturnImmediate;
56 }
57 
58 EXPORT_SYMBOL extern const module_t gd_shim_module = {
59     .name = GD_SHIM_MODULE,
60     .init = kUnusedModuleApi,
61     .start_up = ShimModuleStartUp,
62     .shut_down = GeneralShutDown,
63     .clean_up = kUnusedModuleApi,
64     .dependencies = {kUnusedModuleDependencies}};
65 
is_gd_stack_started_up()66 bool bluetooth::shim::is_gd_stack_started_up() {
67   return bluetooth::shim::Stack::GetInstance()->IsRunning();
68 }
69 
is_gd_dumpsys_module_started()70 bool bluetooth::shim::is_gd_dumpsys_module_started() {
71   return bluetooth::shim::Stack::GetInstance()->IsDumpsysModuleStarted();
72 }
73 
is_classic_discovery_only_enabled()74 bool bluetooth::shim::is_classic_discovery_only_enabled() {
75   return bluetooth::common::init_flags::classic_discovery_only_is_enabled();
76 }
77