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