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 <fcntl.h>
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <string>
25
26 #include "gd/att/att_module.h"
27 #include "gd/btaa/activity_attribution.h"
28 #include "gd/common/init_flags.h"
29 #include "gd/common/strings.h"
30 #include "gd/hal/hci_hal.h"
31 #include "gd/hci/acl_manager.h"
32 #include "gd/hci/controller.h"
33 #include "gd/hci/hci_layer.h"
34 #include "gd/hci/le_advertising_manager.h"
35 #include "gd/hci/le_scanning_manager.h"
36 #include "gd/hci/vendor_specific_event_manager.h"
37 #include "gd/l2cap/classic/l2cap_classic_module.h"
38 #include "gd/l2cap/le/l2cap_le_module.h"
39 #include "gd/metrics/counter_metrics.h"
40 #include "gd/neighbor/connectability.h"
41 #include "gd/neighbor/discoverability.h"
42 #include "gd/neighbor/inquiry.h"
43 #include "gd/neighbor/name.h"
44 #include "gd/neighbor/name_db.h"
45 #include "gd/neighbor/page.h"
46 #include "gd/neighbor/scan.h"
47 #include "gd/os/log.h"
48 #include "gd/security/security_module.h"
49 #include "gd/shim/dumpsys.h"
50 #include "gd/storage/storage_module.h"
51
52 #include "main/shim/acl_legacy_interface.h"
53 #include "main/shim/activity_attribution.h"
54 #include "main/shim/hci_layer.h"
55 #include "main/shim/helpers.h"
56 #include "main/shim/l2c_api.h"
57 #include "main/shim/le_advertising_manager.h"
58 #include "main/shim/le_scanning_manager.h"
59 #include "main/shim/shim.h"
60 #include "main/shim/stack.h"
61
62 namespace bluetooth {
63 namespace shim {
64
65 using ::bluetooth::common::InitFlags;
66 using ::bluetooth::common::StringFormat;
67
68 namespace {
69 // PID file format
70 constexpr char pid_file_format[] = "/var/run/bluetooth/bluetooth%d.pid";
71
CreatePidFile()72 void CreatePidFile() {
73 std::string pid_file =
74 StringFormat(pid_file_format, InitFlags::GetAdapterIndex());
75 int pid_fd_ = open(pid_file.c_str(), O_WRONLY | O_CREAT, 0644);
76 if (!pid_fd_) return;
77
78 pid_t my_pid = getpid();
79 dprintf(pid_fd_, "%d\n", my_pid);
80 close(pid_fd_);
81
82 LOG_INFO("%s - Created pid file %s", __func__, pid_file.c_str());
83 }
84
RemovePidFile()85 void RemovePidFile() {
86 std::string pid_file =
87 StringFormat(pid_file_format, InitFlags::GetAdapterIndex());
88 unlink(pid_file.c_str());
89 LOG_INFO("%s - Deleted pid file %s", __func__, pid_file.c_str());
90 }
91 } // namespace
92
GetInstance()93 Stack* Stack::GetInstance() {
94 static Stack instance;
95 return &instance;
96 }
97
StartIdleMode()98 void Stack::StartIdleMode() {
99 std::lock_guard<std::recursive_mutex> lock(mutex_);
100 ASSERT_LOG(!is_running_, "%s Gd stack already running", __func__);
101 LOG_INFO("%s Starting Gd stack", __func__);
102 ModuleList modules;
103 modules.add<metrics::CounterMetrics>();
104 modules.add<storage::StorageModule>();
105 Start(&modules);
106 // Make sure the leaf modules are started
107 ASSERT(stack_manager_.GetInstance<storage::StorageModule>() != nullptr);
108 is_running_ = true;
109 }
110
StartEverything()111 void Stack::StartEverything() {
112 if (common::init_flags::gd_rust_is_enabled()) {
113 if (rust_stack_ == nullptr) {
114 rust_stack_ = new ::rust::Box<rust::Stack>(rust::stack_create());
115 }
116 rust::stack_start(**rust_stack_);
117
118 rust_hci_ = new ::rust::Box<rust::Hci>(rust::get_hci(**rust_stack_));
119 rust_controller_ =
120 new ::rust::Box<rust::Controller>(rust::get_controller(**rust_stack_));
121 bluetooth::shim::hci_on_reset_complete();
122
123 // Create pid since we're up and running
124 CreatePidFile();
125
126 // Create the acl shim layer
127 acl_ = new legacy::Acl(
128 stack_handler_, legacy::GetAclInterface(),
129 controller_get_interface()->get_ble_acceptlist_size(),
130 controller_get_interface()->get_ble_resolving_list_max_size());
131 return;
132 }
133
134 std::lock_guard<std::recursive_mutex> lock(mutex_);
135 ASSERT_LOG(!is_running_, "%s Gd stack already running", __func__);
136 LOG_INFO("%s Starting Gd stack", __func__);
137 ModuleList modules;
138
139 modules.add<metrics::CounterMetrics>();
140 modules.add<hal::HciHal>();
141 modules.add<hci::HciLayer>();
142 modules.add<storage::StorageModule>();
143 modules.add<shim::Dumpsys>();
144 modules.add<hci::VendorSpecificEventManager>();
145
146 modules.add<hci::Controller>();
147 modules.add<hci::AclManager>();
148 if (common::init_flags::gd_l2cap_is_enabled()) {
149 modules.add<l2cap::classic::L2capClassicModule>();
150 modules.add<l2cap::le::L2capLeModule>();
151 modules.add<hci::LeAdvertisingManager>();
152 }
153 if (common::init_flags::gd_security_is_enabled()) {
154 modules.add<security::SecurityModule>();
155 }
156 modules.add<hci::LeAdvertisingManager>();
157 modules.add<hci::LeScanningManager>();
158 if (common::init_flags::btaa_hci_is_enabled()) {
159 modules.add<activity_attribution::ActivityAttribution>();
160 }
161 if (common::init_flags::gd_core_is_enabled()) {
162 modules.add<att::AttModule>();
163 modules.add<neighbor::ConnectabilityModule>();
164 modules.add<neighbor::DiscoverabilityModule>();
165 modules.add<neighbor::InquiryModule>();
166 modules.add<neighbor::NameModule>();
167 modules.add<neighbor::NameDbModule>();
168 modules.add<neighbor::PageModule>();
169 modules.add<neighbor::ScanModule>();
170 modules.add<storage::StorageModule>();
171 }
172 Start(&modules);
173 is_running_ = true;
174 // Make sure the leaf modules are started
175 ASSERT(stack_manager_.GetInstance<storage::StorageModule>() != nullptr);
176 ASSERT(stack_manager_.GetInstance<shim::Dumpsys>() != nullptr);
177 if (common::init_flags::gd_core_is_enabled()) {
178 btm_ = new Btm(stack_handler_,
179 stack_manager_.GetInstance<neighbor::InquiryModule>());
180 }
181 if (!common::init_flags::gd_core_is_enabled()) {
182 if (stack_manager_.IsStarted<hci::Controller>()) {
183 acl_ = new legacy::Acl(
184 stack_handler_, legacy::GetAclInterface(),
185 controller_get_interface()->get_ble_acceptlist_size(),
186 controller_get_interface()->get_ble_resolving_list_max_size());
187 } else {
188 LOG_ERROR(
189 "Unable to create shim ACL layer as Controller has not started");
190 }
191 }
192
193 if (!common::init_flags::gd_core_is_enabled()) {
194 bluetooth::shim::hci_on_reset_complete();
195 }
196
197 bluetooth::shim::init_advertising_manager();
198 bluetooth::shim::init_scanning_manager();
199
200 if (common::init_flags::gd_l2cap_is_enabled() &&
201 !common::init_flags::gd_core_is_enabled()) {
202 L2CA_UseLegacySecurityModule();
203 }
204 if (common::init_flags::btaa_hci_is_enabled()) {
205 bluetooth::shim::init_activity_attribution();
206 }
207
208 // Create pid since we're up and running
209 CreatePidFile();
210 }
211
Start(ModuleList * modules)212 void Stack::Start(ModuleList* modules) {
213 ASSERT_LOG(!is_running_, "%s Gd stack already running", __func__);
214 LOG_INFO("%s Starting Gd stack", __func__);
215
216 stack_thread_ =
217 new os::Thread("gd_stack_thread", os::Thread::Priority::REAL_TIME);
218 stack_manager_.StartUp(modules, stack_thread_);
219
220 stack_handler_ = new os::Handler(stack_thread_);
221
222 LOG_INFO("%s Successfully toggled Gd stack", __func__);
223 }
224
Stop()225 void Stack::Stop() {
226 // First remove pid file so clients no stack is going down
227 RemovePidFile();
228
229 if (common::init_flags::gd_rust_is_enabled()) {
230 if (rust_stack_ != nullptr) {
231 rust::stack_stop(**rust_stack_);
232 }
233 return;
234 }
235
236 std::lock_guard<std::recursive_mutex> lock(mutex_);
237 if (!common::init_flags::gd_core_is_enabled()) {
238 bluetooth::shim::hci_on_shutting_down();
239 }
240
241 // Make sure gd acl flag is enabled and we started it up
242 if (acl_ != nullptr) {
243 acl_->FinalShutdown();
244 delete acl_;
245 acl_ = nullptr;
246 }
247
248 ASSERT_LOG(is_running_, "%s Gd stack not running", __func__);
249 is_running_ = false;
250
251 delete btm_;
252 btm_ = nullptr;
253
254 stack_handler_->Clear();
255
256 stack_manager_.ShutDown();
257
258 delete stack_handler_;
259 stack_handler_ = nullptr;
260
261 stack_thread_->Stop();
262 delete stack_thread_;
263 stack_thread_ = nullptr;
264
265 LOG_INFO("%s Successfully shut down Gd stack", __func__);
266 }
267
IsRunning()268 bool Stack::IsRunning() {
269 std::lock_guard<std::recursive_mutex> lock(mutex_);
270 return is_running_;
271 }
272
GetStackManager()273 StackManager* Stack::GetStackManager() {
274 std::lock_guard<std::recursive_mutex> lock(mutex_);
275 ASSERT(is_running_);
276 return &stack_manager_;
277 }
278
GetStackManager() const279 const StackManager* Stack::GetStackManager() const {
280 std::lock_guard<std::recursive_mutex> lock(mutex_);
281 ASSERT(is_running_);
282 return &stack_manager_;
283 }
284
GetAcl()285 legacy::Acl* Stack::GetAcl() {
286 std::lock_guard<std::recursive_mutex> lock(mutex_);
287 ASSERT(is_running_);
288 ASSERT_LOG(acl_ != nullptr, "Acl shim layer has not been created");
289 return acl_;
290 }
291
LinkPolicy()292 LinkPolicyInterface* Stack::LinkPolicy() {
293 std::lock_guard<std::recursive_mutex> lock(mutex_);
294 ASSERT(is_running_);
295 ASSERT_LOG(acl_ != nullptr, "Acl shim layer has not been created");
296 return acl_;
297 }
298
GetBtm()299 Btm* Stack::GetBtm() {
300 std::lock_guard<std::recursive_mutex> lock(mutex_);
301 ASSERT(is_running_);
302 return btm_;
303 }
304
GetHandler()305 os::Handler* Stack::GetHandler() {
306 std::lock_guard<std::recursive_mutex> lock(mutex_);
307 ASSERT(is_running_);
308 return stack_handler_;
309 }
310
IsDumpsysModuleStarted() const311 bool Stack::IsDumpsysModuleStarted() const {
312 std::lock_guard<std::recursive_mutex> lock(mutex_);
313 return GetStackManager()->IsStarted<Dumpsys>();
314 }
315
316 } // namespace shim
317 } // namespace bluetooth
318