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 "device/include/controller.h"
20 
21 #include "gd/att/att_module.h"
22 #include "gd/btaa/activity_attribution.h"
23 #include "gd/common/init_flags.h"
24 #include "gd/hal/hci_hal.h"
25 #include "gd/hci/acl_manager.h"
26 #include "gd/hci/controller.h"
27 #include "gd/hci/hci_layer.h"
28 #include "gd/hci/le_advertising_manager.h"
29 #include "gd/hci/le_scanning_manager.h"
30 #include "gd/hci/vendor_specific_event_manager.h"
31 #include "gd/l2cap/classic/l2cap_classic_module.h"
32 #include "gd/l2cap/le/l2cap_le_module.h"
33 #include "gd/neighbor/connectability.h"
34 #include "gd/neighbor/discoverability.h"
35 #include "gd/neighbor/inquiry.h"
36 #include "gd/neighbor/name.h"
37 #include "gd/neighbor/name_db.h"
38 #include "gd/neighbor/page.h"
39 #include "gd/neighbor/scan.h"
40 #include "gd/os/log.h"
41 #include "gd/security/security_module.h"
42 #include "gd/shim/dumpsys.h"
43 #include "gd/storage/storage_module.h"
44 
45 #include "main/shim/acl_legacy_interface.h"
46 #include "main/shim/activity_attribution.h"
47 #include "main/shim/hci_layer.h"
48 #include "main/shim/helpers.h"
49 #include "main/shim/l2c_api.h"
50 #include "main/shim/le_advertising_manager.h"
51 #include "main/shim/le_scanning_manager.h"
52 #include "main/shim/shim.h"
53 #include "main/shim/stack.h"
54 
55 namespace bluetooth {
56 namespace shim {
57 
GetInstance()58 Stack* Stack::GetInstance() {
59   static Stack instance;
60   return &instance;
61 }
62 
StartIdleMode()63 void Stack::StartIdleMode() {
64   std::lock_guard<std::recursive_mutex> lock(mutex_);
65   ASSERT_LOG(!is_running_, "%s Gd stack already running", __func__);
66   LOG_INFO("%s Starting Gd stack", __func__);
67   ModuleList modules;
68   modules.add<storage::StorageModule>();
69   Start(&modules);
70   // Make sure the leaf modules are started
71   ASSERT(stack_manager_.GetInstance<storage::StorageModule>() != nullptr);
72   is_running_ = true;
73 }
74 
StartEverything()75 void Stack::StartEverything() {
76   if (common::init_flags::gd_rust_is_enabled()) {
77     if (rust_stack_ == nullptr) {
78       rust_stack_ = new ::rust::Box<rust::Stack>(rust::stack_create());
79     }
80     rust::stack_start(**rust_stack_);
81 
82     if (common::init_flags::gd_hci_is_enabled()) {
83       rust_hci_ = new ::rust::Box<rust::Hci>(rust::get_hci(**rust_stack_));
84     }
85     if (common::init_flags::gd_controller_is_enabled()) {
86       rust_controller_ = new ::rust::Box<rust::Controller>(
87           rust::get_controller(**rust_stack_));
88     }
89     bluetooth::shim::hci_on_reset_complete();
90     return;
91   }
92 
93   std::lock_guard<std::recursive_mutex> lock(mutex_);
94   ASSERT_LOG(!is_running_, "%s Gd stack already running", __func__);
95   LOG_INFO("%s Starting Gd stack", __func__);
96   ModuleList modules;
97   if (common::init_flags::gd_hci_is_enabled()) {
98     modules.add<hal::HciHal>();
99     modules.add<hci::HciLayer>();
100     modules.add<storage::StorageModule>();
101     modules.add<shim::Dumpsys>();
102     modules.add<hci::VendorSpecificEventManager>();
103   }
104   if (common::init_flags::gd_controller_is_enabled()) {
105     modules.add<hci::Controller>();
106   }
107   if (common::init_flags::gd_acl_is_enabled()) {
108     modules.add<hci::AclManager>();
109   }
110   if (common::init_flags::gd_l2cap_is_enabled()) {
111     modules.add<l2cap::classic::L2capClassicModule>();
112     modules.add<l2cap::le::L2capLeModule>();
113     modules.add<hci::LeAdvertisingManager>();
114   }
115   if (common::init_flags::gd_security_is_enabled()) {
116     modules.add<security::SecurityModule>();
117   }
118   if (common::init_flags::gd_advertising_is_enabled()) {
119     modules.add<hci::LeAdvertisingManager>();
120   }
121   if (common::init_flags::gd_scanning_is_enabled()) {
122     modules.add<hci::LeScanningManager>();
123   }
124   if (common::init_flags::btaa_hci_is_enabled()) {
125     modules.add<activity_attribution::ActivityAttribution>();
126   }
127   if (common::init_flags::gd_core_is_enabled()) {
128     modules.add<att::AttModule>();
129     modules.add<neighbor::ConnectabilityModule>();
130     modules.add<neighbor::DiscoverabilityModule>();
131     modules.add<neighbor::InquiryModule>();
132     modules.add<neighbor::NameModule>();
133     modules.add<neighbor::NameDbModule>();
134     modules.add<neighbor::PageModule>();
135     modules.add<neighbor::ScanModule>();
136     modules.add<storage::StorageModule>();
137   }
138   Start(&modules);
139   is_running_ = true;
140   // Make sure the leaf modules are started
141   ASSERT(stack_manager_.GetInstance<storage::StorageModule>() != nullptr);
142   ASSERT(stack_manager_.GetInstance<shim::Dumpsys>() != nullptr);
143   if (common::init_flags::gd_core_is_enabled()) {
144     btm_ = new Btm(stack_handler_,
145                    stack_manager_.GetInstance<neighbor::InquiryModule>());
146   }
147   if (common::init_flags::gd_acl_is_enabled()) {
148     if (!common::init_flags::gd_core_is_enabled()) {
149       acl_ = new legacy::Acl(
150           stack_handler_, legacy::GetAclInterface(),
151           controller_get_interface()->get_ble_acceptlist_size());
152     }
153   }
154   if (!common::init_flags::gd_core_is_enabled()) {
155     bluetooth::shim::hci_on_reset_complete();
156   }
157 
158   if (common::init_flags::gd_advertising_is_enabled()) {
159     bluetooth::shim::init_advertising_manager();
160   }
161   if (common::init_flags::gd_scanning_is_enabled()) {
162     bluetooth::shim::init_scanning_manager();
163   }
164   if (common::init_flags::gd_l2cap_is_enabled() &&
165       !common::init_flags::gd_core_is_enabled()) {
166     L2CA_UseLegacySecurityModule();
167   }
168   if (common::init_flags::btaa_hci_is_enabled()) {
169     bluetooth::shim::init_activity_attribution();
170   }
171 }
172 
Start(ModuleList * modules)173 void Stack::Start(ModuleList* modules) {
174   ASSERT_LOG(!is_running_, "%s Gd stack already running", __func__);
175   LOG_INFO("%s Starting Gd stack", __func__);
176 
177   stack_thread_ =
178       new os::Thread("gd_stack_thread", os::Thread::Priority::NORMAL);
179   stack_manager_.StartUp(modules, stack_thread_);
180 
181   stack_handler_ = new os::Handler(stack_thread_);
182 
183   LOG_INFO("%s Successfully toggled Gd stack", __func__);
184 }
185 
Stop()186 void Stack::Stop() {
187   if (common::init_flags::gd_rust_is_enabled()) {
188     if (rust_stack_ != nullptr) {
189       rust::stack_stop(**rust_stack_);
190     }
191     return;
192   }
193 
194   std::lock_guard<std::recursive_mutex> lock(mutex_);
195   if (!common::init_flags::gd_core_is_enabled()) {
196     bluetooth::shim::hci_on_shutting_down();
197   }
198 
199   // Make sure gd acl flag is enabled and we started it up
200   if (common::init_flags::gd_acl_is_enabled() && acl_ != nullptr) {
201     acl_->FinalShutdown();
202     delete acl_;
203     acl_ = nullptr;
204   }
205 
206   ASSERT_LOG(is_running_, "%s Gd stack not running", __func__);
207   is_running_ = false;
208 
209   delete btm_;
210   btm_ = nullptr;
211 
212   stack_handler_->Clear();
213 
214   stack_manager_.ShutDown();
215 
216   delete stack_handler_;
217   stack_handler_ = nullptr;
218 
219   stack_thread_->Stop();
220   delete stack_thread_;
221   stack_thread_ = nullptr;
222 
223   LOG_INFO("%s Successfully shut down Gd stack", __func__);
224 }
225 
IsRunning()226 bool Stack::IsRunning() {
227   std::lock_guard<std::recursive_mutex> lock(mutex_);
228   return is_running_;
229 }
230 
GetStackManager()231 StackManager* Stack::GetStackManager() {
232   std::lock_guard<std::recursive_mutex> lock(mutex_);
233   ASSERT(is_running_);
234   return &stack_manager_;
235 }
236 
GetStackManager() const237 const StackManager* Stack::GetStackManager() const {
238   std::lock_guard<std::recursive_mutex> lock(mutex_);
239   ASSERT(is_running_);
240   return &stack_manager_;
241 }
242 
GetAcl()243 legacy::Acl* Stack::GetAcl() {
244   std::lock_guard<std::recursive_mutex> lock(mutex_);
245   ASSERT(is_running_);
246   ASSERT_LOG(acl_ != nullptr, "Acl shim layer has not been created");
247   return acl_;
248 }
249 
LinkPolicy()250 LinkPolicyInterface* Stack::LinkPolicy() {
251   std::lock_guard<std::recursive_mutex> lock(mutex_);
252   ASSERT(is_running_);
253   ASSERT_LOG(acl_ != nullptr, "Acl shim layer has not been created");
254   return acl_;
255 }
256 
GetBtm()257 Btm* Stack::GetBtm() {
258   std::lock_guard<std::recursive_mutex> lock(mutex_);
259   ASSERT(is_running_);
260   return btm_;
261 }
262 
GetHandler()263 os::Handler* Stack::GetHandler() {
264   std::lock_guard<std::recursive_mutex> lock(mutex_);
265   ASSERT(is_running_);
266   return stack_handler_;
267 }
268 
IsDumpsysModuleStarted() const269 bool Stack::IsDumpsysModuleStarted() const {
270   std::lock_guard<std::recursive_mutex> lock(mutex_);
271   return GetStackManager()->IsStarted<Dumpsys>();
272 }
273 
274 }  // namespace shim
275 }  // namespace bluetooth
276