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