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 #include <memory>
18 
19 #include "hci/acl_manager.h"
20 #include "l2cap/internal/parameter_provider.h"
21 #include "l2cap/le/internal/dynamic_channel_service_manager_impl.h"
22 #include "l2cap/le/internal/fixed_channel_service_manager_impl.h"
23 #include "l2cap/le/internal/link_manager.h"
24 #include "l2cap/le/security_enforcement_interface.h"
25 #include "module.h"
26 #include "os/handler.h"
27 
28 #include "l2cap/le/l2cap_le_module.h"
29 
30 namespace bluetooth {
31 namespace l2cap {
32 namespace le {
33 
__anon0e5c2f240102() 34 const ModuleFactory L2capLeModule::Factory = ModuleFactory([]() { return new L2capLeModule(); });
35 
36 /**
37  * A default implementation which cannot satisfy any security level except
38  * NO_SECURITY_WHATSOEVER_PLAINTEXT_TRANSPORT_OK.
39  */
40 class SecurityEnforcementRejectAllImpl : public SecurityEnforcementInterface {
41  public:
Enforce(hci::AddressWithType,SecurityPolicy policy,ResultCallback result_callback)42   void Enforce(
43       hci::AddressWithType /* remote */,
44       SecurityPolicy policy,
45       ResultCallback result_callback) override {
46     if (policy == SecurityPolicy::NO_SECURITY_WHATSOEVER_PLAINTEXT_TRANSPORT_OK) {
47       result_callback(true);
48     } else {
49       result_callback(false);
50     }
51   }
52 };
53 static SecurityEnforcementRejectAllImpl default_security_module_impl_;
54 
55 struct L2capLeModule::impl {
implbluetooth::l2cap::le::L2capLeModule::impl56   impl(os::Handler* l2cap_handler, hci::AclManager* acl_manager)
57       : l2cap_handler_(l2cap_handler), acl_manager_(acl_manager) {
58     dynamic_channel_service_manager_impl_.SetSecurityEnforcementInterface(&default_security_module_impl_);
59   }
60   os::Handler* l2cap_handler_;
61   hci::AclManager* acl_manager_;
62   l2cap::internal::ParameterProvider parameter_provider_;
63   internal::FixedChannelServiceManagerImpl fixed_channel_service_manager_impl_{l2cap_handler_};
64   internal::DynamicChannelServiceManagerImpl dynamic_channel_service_manager_impl_{l2cap_handler_};
65   internal::LinkManager link_manager_{l2cap_handler_,
66                                       acl_manager_,
67                                       &fixed_channel_service_manager_impl_,
68                                       &dynamic_channel_service_manager_impl_,
69                                       &parameter_provider_};
70 };
71 
L2capLeModule()72 L2capLeModule::L2capLeModule() {}
~L2capLeModule()73 L2capLeModule::~L2capLeModule() {}
74 
ListDependencies(ModuleList * list) const75 void L2capLeModule::ListDependencies(ModuleList* list) const {
76   list->add<hci::AclManager>();
77 }
78 
Start()79 void L2capLeModule::Start() {
80   pimpl_ = std::make_unique<impl>(GetHandler(), GetDependency<hci::AclManager>());
81 }
82 
Stop()83 void L2capLeModule::Stop() {
84   pimpl_.reset();
85 }
86 
ToString() const87 std::string L2capLeModule::ToString() const {
88   return "L2cap Le Module";
89 }
90 
GetFixedChannelManager()91 std::unique_ptr<FixedChannelManager> L2capLeModule::GetFixedChannelManager() {
92   return std::unique_ptr<FixedChannelManager>(new FixedChannelManager(&pimpl_->fixed_channel_service_manager_impl_,
93                                                                       &pimpl_->link_manager_, pimpl_->l2cap_handler_));
94 }
95 
GetDynamicChannelManager()96 std::unique_ptr<DynamicChannelManager> L2capLeModule::GetDynamicChannelManager() {
97   return std::unique_ptr<DynamicChannelManager>(new DynamicChannelManager(
98       &pimpl_->dynamic_channel_service_manager_impl_, &pimpl_->link_manager_, pimpl_->l2cap_handler_));
99 }
100 
InjectSecurityEnforcementInterface(SecurityEnforcementInterface * security_enforcement_interface)101 void L2capLeModule::InjectSecurityEnforcementInterface(SecurityEnforcementInterface* security_enforcement_interface) {
102   if (security_enforcement_interface != nullptr) {
103     pimpl_->dynamic_channel_service_manager_impl_.SetSecurityEnforcementInterface(security_enforcement_interface);
104   } else {
105     pimpl_->dynamic_channel_service_manager_impl_.SetSecurityEnforcementInterface(&default_security_module_impl_);
106   }
107 }
108 
SetLinkPropertyListener(os::Handler * handler,LinkPropertyListener * listener)109 void L2capLeModule::SetLinkPropertyListener(os::Handler* handler, LinkPropertyListener* listener) {
110   pimpl_->link_manager_.RegisterLinkPropertyListener(handler, listener);
111 }
112 
113 }  // namespace le
114 }  // namespace l2cap
115 }  // namespace bluetooth
116