• 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 #define LOG_TAG "l2cap2"
17 
18 #include <future>
19 #include <memory>
20 
21 #include "common/bidi_queue.h"
22 #include "hci/acl_manager.h"
23 #include "hci/address.h"
24 #include "hci/hci_layer.h"
25 #include "hci/hci_packets.h"
26 #include "l2cap/classic/internal/dumpsys_helper.h"
27 #include "l2cap/classic/internal/dynamic_channel_service_manager_impl.h"
28 #include "l2cap/classic/internal/fixed_channel_service_manager_impl.h"
29 #include "l2cap/classic/internal/link_manager.h"
30 #include "l2cap/classic/l2cap_classic_module.h"
31 #include "l2cap/internal/parameter_provider.h"
32 #include "l2cap_classic_module_generated.h"
33 #include "module.h"
34 #include "os/handler.h"
35 #include "os/log.h"
36 
37 namespace bluetooth {
38 namespace l2cap {
39 namespace classic {
40 
__anonc88e85ab0102() 41 const ModuleFactory L2capClassicModule::Factory = ModuleFactory([]() { return new L2capClassicModule(); });
42 
43 static SecurityEnforcementRejectAllImpl default_security_module_impl_;
44 
45 struct L2capClassicModule::impl {
implbluetooth::l2cap::classic::L2capClassicModule::impl46   impl(os::Handler* l2cap_handler, hci::AclManager* acl_manager)
47       : l2cap_handler_(l2cap_handler), acl_manager_(acl_manager) {
48     dynamic_channel_service_manager_impl_.SetSecurityEnforcementInterface(&default_security_module_impl_);
49     dumpsys_helper_ = std::make_unique<internal::DumpsysHelper>(link_manager_);
50   }
51   os::Handler* l2cap_handler_;
52   hci::AclManager* acl_manager_;
53   l2cap::internal::ParameterProvider parameter_provider_;
54   internal::FixedChannelServiceManagerImpl fixed_channel_service_manager_impl_{l2cap_handler_};
55   internal::DynamicChannelServiceManagerImpl dynamic_channel_service_manager_impl_{l2cap_handler_};
56   internal::LinkManager link_manager_{l2cap_handler_, acl_manager_, &fixed_channel_service_manager_impl_,
57                                       &dynamic_channel_service_manager_impl_, &parameter_provider_};
58   std::unique_ptr<internal::DumpsysHelper> dumpsys_helper_;
59 
60   struct SecurityInterfaceImpl : public SecurityInterface {
SecurityInterfaceImplbluetooth::l2cap::classic::L2capClassicModule::impl::SecurityInterfaceImpl61     SecurityInterfaceImpl(impl* module_impl) : module_impl_(module_impl) {}
62 
RegisterLinkSecurityInterfaceListenerbluetooth::l2cap::classic::L2capClassicModule::impl::SecurityInterfaceImpl63     void RegisterLinkSecurityInterfaceListener(os::Handler* handler, LinkSecurityInterfaceListener* listener) {
64       ASSERT(!registered_);
65       module_impl_->link_manager_.RegisterLinkSecurityInterfaceListener(handler, listener);
66       registered_ = true;
67     }
68 
InitiateConnectionForSecuritybluetooth::l2cap::classic::L2capClassicModule::impl::SecurityInterfaceImpl69     void InitiateConnectionForSecurity(hci::Address remote) override {
70       ASSERT(registered_);
71       module_impl_->link_manager_.InitiateConnectionForSecurity(remote);
72     }
73 
Unregisterbluetooth::l2cap::classic::L2capClassicModule::impl::SecurityInterfaceImpl74     void Unregister() override {
75       ASSERT(registered_);
76       module_impl_->link_manager_.RegisterLinkSecurityInterfaceListener(nullptr, nullptr);
77       registered_ = false;
78     }
79     impl* module_impl_;
80     bool registered_ = false;
81   } security_interface_impl_{this};
82 
83   void Dump(
84       std::promise<flatbuffers::Offset<L2capClassicModuleData>> promise,
85       flatbuffers::FlatBufferBuilder* fb_builder) const;
86 };
87 
L2capClassicModule()88 L2capClassicModule::L2capClassicModule() {}
89 
~L2capClassicModule()90 L2capClassicModule::~L2capClassicModule() {}
91 
ListDependencies(ModuleList * list) const92 void L2capClassicModule::ListDependencies(ModuleList* list) const {
93   list->add<hci::AclManager>();
94 }
95 
Start()96 void L2capClassicModule::Start() {
97   pimpl_ = std::make_unique<impl>(GetHandler(), GetDependency<hci::AclManager>());
98 }
99 
Stop()100 void L2capClassicModule::Stop() {
101   pimpl_.reset();
102 }
103 
ToString() const104 std::string L2capClassicModule::ToString() const {
105   return "L2cap Classic Module";
106 }
107 
GetFixedChannelManager()108 std::unique_ptr<FixedChannelManager> L2capClassicModule::GetFixedChannelManager() {
109   return std::unique_ptr<FixedChannelManager>(new FixedChannelManager(&pimpl_->fixed_channel_service_manager_impl_,
110                                                                       &pimpl_->link_manager_, pimpl_->l2cap_handler_));
111 }
112 
GetDynamicChannelManager()113 std::unique_ptr<DynamicChannelManager> L2capClassicModule::GetDynamicChannelManager() {
114   return std::unique_ptr<DynamicChannelManager>(new DynamicChannelManager(
115       &pimpl_->dynamic_channel_service_manager_impl_, &pimpl_->link_manager_, pimpl_->l2cap_handler_));
116 }
117 
InjectSecurityEnforcementInterface(SecurityEnforcementInterface * security_enforcement_interface)118 void L2capClassicModule::InjectSecurityEnforcementInterface(
119     SecurityEnforcementInterface* security_enforcement_interface) {
120   if (security_enforcement_interface != nullptr) {
121     pimpl_->dynamic_channel_service_manager_impl_.SetSecurityEnforcementInterface(security_enforcement_interface);
122   } else {
123     pimpl_->dynamic_channel_service_manager_impl_.SetSecurityEnforcementInterface(&default_security_module_impl_);
124   }
125 }
126 
GetSecurityInterface(os::Handler * handler,LinkSecurityInterfaceListener * listener)127 SecurityInterface* L2capClassicModule::GetSecurityInterface(
128     os::Handler* handler, LinkSecurityInterfaceListener* listener) {
129   pimpl_->security_interface_impl_.RegisterLinkSecurityInterfaceListener(handler, listener);
130   return &pimpl_->security_interface_impl_;
131 }
132 
SetLinkPropertyListener(os::Handler * handler,LinkPropertyListener * listener)133 void L2capClassicModule::SetLinkPropertyListener(os::Handler* handler, LinkPropertyListener* listener) {
134   pimpl_->link_manager_.RegisterLinkPropertyListener(handler, listener);
135 }
136 
Dump(std::promise<flatbuffers::Offset<L2capClassicModuleData>> promise,flatbuffers::FlatBufferBuilder * fb_builder) const137 void L2capClassicModule::impl::Dump(
138     std::promise<flatbuffers::Offset<L2capClassicModuleData>> promise,
139     flatbuffers::FlatBufferBuilder* fb_builder) const {
140   auto title = fb_builder->CreateString("----- L2cap Classic Dumpsys -----");
141 
142   std::vector<flatbuffers::Offset<bluetooth::l2cap::classic::LinkData>> link_offsets =
143       dumpsys_helper_->DumpActiveLinks(fb_builder);
144 
145   auto active_links = fb_builder->CreateVector(link_offsets);
146 
147   L2capClassicModuleDataBuilder builder(*fb_builder);
148   builder.add_title(title);
149   builder.add_active_links(active_links);
150   flatbuffers::Offset<L2capClassicModuleData> dumpsys_data = builder.Finish();
151 
152   promise.set_value(dumpsys_data);
153 }
154 
GetDumpsysData(flatbuffers::FlatBufferBuilder * fb_builder) const155 DumpsysDataFinisher L2capClassicModule::GetDumpsysData(flatbuffers::FlatBufferBuilder* fb_builder) const {
156   ASSERT(fb_builder != nullptr);
157 
158   std::promise<flatbuffers::Offset<L2capClassicModuleData>> promise;
159   auto future = promise.get_future();
160   pimpl_->Dump(std::move(promise), fb_builder);
161 
162   auto dumpsys_data = future.get();
163 
164   return [dumpsys_data](DumpsysDataBuilder* dumpsys_builder) {
165     dumpsys_builder->add_l2cap_classic_dumpsys_data(dumpsys_data);
166   };
167 }
168 
169 }  // namespace classic
170 }  // namespace l2cap
171 }  // namespace bluetooth
172