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_gd_shim"
18
19 #include "gd/att/att_module.h"
20 #include "gd/hal/hci_hal.h"
21 #include "gd/hci/acl_manager.h"
22 #include "gd/hci/hci_layer.h"
23 #include "gd/hci/le_advertising_manager.h"
24 #include "gd/hci/le_scanning_manager.h"
25 #include "gd/l2cap/classic/l2cap_classic_module.h"
26 #include "gd/l2cap/le/l2cap_le_module.h"
27 #include "gd/neighbor/connectability.h"
28 #include "gd/neighbor/discoverability.h"
29 #include "gd/neighbor/inquiry.h"
30 #include "gd/neighbor/name.h"
31 #include "gd/neighbor/name_db.h"
32 #include "gd/neighbor/page.h"
33 #include "gd/neighbor/scan.h"
34 #include "gd/os/log.h"
35 #include "gd/security/security_module.h"
36 #include "gd/shim/dumpsys.h"
37 #include "gd/shim/l2cap.h"
38 #include "gd/storage/storage_module.h"
39
40 #include "main/shim/stack.h"
41
42 namespace bluetooth {
43 namespace shim {
44
GetInstance()45 Stack* Stack::GetInstance() {
46 static Stack instance;
47 return &instance;
48 }
49
StartIdleMode()50 void Stack::StartIdleMode() {
51 std::lock_guard<std::mutex> lock(mutex_);
52 ASSERT_LOG(!is_running_, "%s Gd stack already running", __func__);
53 LOG_INFO("%s Starting Gd stack", __func__);
54 ModuleList modules;
55 modules.add<storage::StorageModule>();
56 Start(&modules);
57 // Make sure the leaf modules are started
58 ASSERT(stack_manager_.GetInstance<storage::StorageModule>() != nullptr);
59 is_running_ = true;
60 }
61
StartEverything()62 void Stack::StartEverything() {
63 std::lock_guard<std::mutex> lock(mutex_);
64 ASSERT_LOG(!is_running_, "%s Gd stack already running", __func__);
65 LOG_INFO("%s Starting Gd stack", __func__);
66 ModuleList modules;
67 modules.add<att::AttModule>();
68 modules.add<hal::HciHal>();
69 modules.add<hci::AclManager>();
70 modules.add<hci::HciLayer>();
71 modules.add<hci::LeAdvertisingManager>();
72 modules.add<hci::LeScanningManager>();
73 modules.add<l2cap::classic::L2capClassicModule>();
74 modules.add<l2cap::le::L2capLeModule>();
75 modules.add<neighbor::ConnectabilityModule>();
76 modules.add<neighbor::DiscoverabilityModule>();
77 modules.add<neighbor::InquiryModule>();
78 modules.add<neighbor::NameModule>();
79 modules.add<neighbor::NameDbModule>();
80 modules.add<neighbor::PageModule>();
81 modules.add<neighbor::ScanModule>();
82 modules.add<security::SecurityModule>();
83 modules.add<storage::StorageModule>();
84 modules.add<shim::Dumpsys>();
85 modules.add<shim::L2cap>();
86 Start(&modules);
87 // Make sure the leaf modules are started
88 ASSERT(stack_manager_.GetInstance<storage::StorageModule>() != nullptr);
89 ASSERT(stack_manager_.GetInstance<shim::L2cap>() != nullptr);
90 ASSERT(stack_manager_.GetInstance<shim::Dumpsys>() != nullptr);
91 btm_ = new Btm(stack_handler_,
92 stack_manager_.GetInstance<neighbor::InquiryModule>());
93 is_running_ = true;
94 }
95
Start(ModuleList * modules)96 void Stack::Start(ModuleList* modules) {
97 ASSERT_LOG(!is_running_, "%s Gd stack already running", __func__);
98 LOG_DEBUG("%s Starting Gd stack", __func__);
99
100 stack_thread_ =
101 new os::Thread("gd_stack_thread", os::Thread::Priority::NORMAL);
102 stack_manager_.StartUp(modules, stack_thread_);
103
104 stack_handler_ = new os::Handler(stack_thread_);
105
106 LOG_INFO("%s Successfully toggled Gd stack", __func__);
107 }
108
Stop()109 void Stack::Stop() {
110 std::lock_guard<std::mutex> lock(mutex_);
111 ASSERT_LOG(is_running_, "%s Gd stack not running", __func__);
112 is_running_ = false;
113
114 delete btm_;
115 btm_ = nullptr;
116
117 stack_handler_->Clear();
118 delete stack_handler_;
119 stack_handler_ = nullptr;
120
121 stack_manager_.ShutDown();
122 stack_thread_->Stop();
123 delete stack_thread_;
124 stack_thread_ = nullptr;
125
126 LOG_INFO("%s Successfully shut down Gd stack", __func__);
127 }
128
IsRunning()129 bool Stack::IsRunning() {
130 std::lock_guard<std::mutex> lock(mutex_);
131 return is_running_;
132 }
133
GetStackManager()134 StackManager* Stack::GetStackManager() {
135 std::lock_guard<std::mutex> lock(mutex_);
136 ASSERT(is_running_);
137 return &stack_manager_;
138 }
139
GetBtm()140 Btm* Stack::GetBtm() {
141 std::lock_guard<std::mutex> lock(mutex_);
142 ASSERT(is_running_);
143 return btm_;
144 }
145
GetHandler()146 os::Handler* Stack::GetHandler() {
147 std::lock_guard<std::mutex> lock(mutex_);
148 ASSERT(is_running_);
149 return stack_handler_;
150 }
151
152 } // namespace shim
153 } // namespace bluetooth
154