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 "main/shim/stack.h"
20 
21 #include <bluetooth/log.h>
22 #include <com_android_bluetooth_flags.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 
26 #include <string>
27 
28 #include "common/strings.h"
29 #include "hal/hci_hal.h"
30 #include "hci/acl_manager.h"
31 #include "hci/acl_manager/acl_scheduler.h"
32 #include "hci/controller.h"
33 #include "hci/controller_interface.h"
34 #include "hci/distance_measurement_manager.h"
35 #include "hci/hci_layer.h"
36 #include "hci/le_advertising_manager.h"
37 #include "hci/le_scanning_manager.h"
38 #include "hci/msft.h"
39 #include "hci/remote_name_request.h"
40 #include "lpp/lpp_offload_manager.h"
41 #include "main/shim/acl.h"
42 #include "main/shim/acl_interface.h"
43 #include "main/shim/distance_measurement_manager.h"
44 #include "main/shim/entry.h"
45 #include "main/shim/hci_layer.h"
46 #include "main/shim/le_advertising_manager.h"
47 #include "main/shim/le_scanning_manager.h"
48 #include "metrics/counter_metrics.h"
49 #include "shim/dumpsys.h"
50 #include "storage/storage_module.h"
51 #if TARGET_FLOSS
52 #include "sysprops/sysprops_module.h"
53 #endif
54 
55 namespace bluetooth {
56 namespace shim {
57 
58 using ::bluetooth::common::StringFormat;
59 
60 struct Stack::impl {
61   Acl* acl_ = nullptr;
62 };
63 
Stack()64 Stack::Stack() { pimpl_ = std::make_shared<Stack::impl>(); }
65 
GetInstance()66 Stack* Stack::GetInstance() {
67   static Stack instance;
68   return &instance;
69 }
70 
StartEverything()71 void Stack::StartEverything() {
72   std::lock_guard<std::recursive_mutex> lock(mutex_);
73   log::assert_that(!is_running_, "Gd stack already running");
74   log::info("Starting Gd stack");
75   ModuleList modules;
76 
77 #if TARGET_FLOSS
78   modules.add<sysprops::SyspropsModule>();
79 #else
80   if (com::android::bluetooth::flags::socket_settings_api()) {  // Added with aosp/3286716
81     modules.add<lpp::LppOffloadManager>();
82   }
83 #endif
84   modules.add<metrics::CounterMetrics>();
85   modules.add<hal::HciHal>();
86   modules.add<hci::HciLayer>();
87   modules.add<storage::StorageModule>();
88   modules.add<shim::Dumpsys>();
89 
90   modules.add<hci::Controller>();
91   modules.add<hci::acl_manager::AclScheduler>();
92   modules.add<hci::AclManager>();
93   modules.add<hci::RemoteNameRequestModule>();
94   modules.add<hci::LeAdvertisingManager>();
95   modules.add<hci::MsftExtensionManager>();
96   modules.add<hci::LeScanningManager>();
97   modules.add<hci::DistanceMeasurementManager>();
98   Start(&modules);
99   is_running_ = true;
100   // Make sure the leaf modules are started
101   log::assert_that(stack_manager_.GetInstance<storage::StorageModule>() != nullptr,
102                    "assert failed: stack_manager_.GetInstance<storage::StorageModule>() != "
103                    "nullptr");
104   log::assert_that(stack_manager_.GetInstance<shim::Dumpsys>() != nullptr,
105                    "assert failed: stack_manager_.GetInstance<shim::Dumpsys>() != nullptr");
106   if (stack_manager_.IsStarted<hci::Controller>()) {
107     pimpl_->acl_ =
108             new Acl(stack_handler_, GetAclInterface(), GetController()->GetLeFilterAcceptListSize(),
109                     GetController()->GetLeResolvingListSize());
110   } else {
111     log::error("Unable to create shim ACL layer as Controller has not started");
112   }
113 
114   bluetooth::shim::hci_on_reset_complete();
115   bluetooth::shim::init_advertising_manager();
116   bluetooth::shim::init_scanning_manager();
117   bluetooth::shim::init_distance_measurement_manager();
118 }
119 
StartModuleStack(const ModuleList * modules,const os::Thread * thread)120 void Stack::StartModuleStack(const ModuleList* modules, const os::Thread* thread) {
121   std::lock_guard<std::recursive_mutex> lock(mutex_);
122   log::assert_that(!is_running_, "Gd stack already running");
123   stack_thread_ = const_cast<os::Thread*>(thread);
124   log::info("Starting Gd stack");
125 
126   stack_manager_.StartUp(const_cast<ModuleList*>(modules), stack_thread_);
127   stack_handler_ = new os::Handler(stack_thread_);
128 
129   num_modules_ = modules->NumModules();
130   is_running_ = true;
131 }
132 
Start(ModuleList * modules)133 void Stack::Start(ModuleList* modules) {
134   log::assert_that(!is_running_, "Gd stack already running");
135   log::info("Starting Gd stack");
136 
137   stack_thread_ = new os::Thread("gd_stack_thread", os::Thread::Priority::REAL_TIME);
138   stack_manager_.StartUp(modules, stack_thread_);
139 
140   stack_handler_ = new os::Handler(stack_thread_);
141 
142   log::info("Successfully toggled Gd stack");
143 }
144 
Stop()145 void Stack::Stop() {
146   std::lock_guard<std::recursive_mutex> lock(mutex_);
147   bluetooth::shim::hci_on_shutting_down();
148 
149   // Make sure gd acl flag is enabled and we started it up
150   if (pimpl_->acl_ != nullptr) {
151     pimpl_->acl_->FinalShutdown();
152     delete pimpl_->acl_;
153     pimpl_->acl_ = nullptr;
154   }
155 
156   log::assert_that(is_running_, "Gd stack not running");
157   is_running_ = false;
158 
159   stack_handler_->Clear();
160 
161   stack_manager_.ShutDown();
162 
163   delete stack_handler_;
164   stack_handler_ = nullptr;
165 
166   stack_thread_->Stop();
167   delete stack_thread_;
168   stack_thread_ = nullptr;
169 
170   log::info("Successfully shut down Gd stack");
171 }
172 
IsRunning()173 bool Stack::IsRunning() {
174   std::lock_guard<std::recursive_mutex> lock(mutex_);
175   return is_running_;
176 }
177 
GetStackManager()178 StackManager* Stack::GetStackManager() {
179   std::lock_guard<std::recursive_mutex> lock(mutex_);
180   log::assert_that(is_running_, "assert failed: is_running_");
181   return &stack_manager_;
182 }
183 
GetStackManager() const184 const StackManager* Stack::GetStackManager() const {
185   std::lock_guard<std::recursive_mutex> lock(mutex_);
186   log::assert_that(is_running_, "assert failed: is_running_");
187   return &stack_manager_;
188 }
189 
GetAcl()190 Acl* Stack::GetAcl() {
191   std::lock_guard<std::recursive_mutex> lock(mutex_);
192   log::assert_that(is_running_, "assert failed: is_running_");
193   log::assert_that(pimpl_->acl_ != nullptr, "Acl shim layer has not been created");
194   return pimpl_->acl_;
195 }
196 
GetHandler()197 os::Handler* Stack::GetHandler() {
198   std::lock_guard<std::recursive_mutex> lock(mutex_);
199   log::assert_that(is_running_, "assert failed: is_running_");
200   return stack_handler_;
201 }
202 
IsDumpsysModuleStarted() const203 bool Stack::IsDumpsysModuleStarted() const {
204   std::lock_guard<std::recursive_mutex> lock(mutex_);
205   return GetStackManager()->IsStarted<Dumpsys>();
206 }
207 
LockForDumpsys(std::function<void ()> dumpsys_callback)208 bool Stack::LockForDumpsys(std::function<void()> dumpsys_callback) {
209   std::lock_guard<std::recursive_mutex> lock(mutex_);
210   if (is_running_) {
211     dumpsys_callback();
212   }
213   return is_running_;
214 }
215 
216 }  // namespace shim
217 }  // namespace bluetooth
218