• 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 #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 
__anondea3aaf50102() 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 remote,SecurityPolicy policy,ResultCallback result_callback)42   void Enforce(hci::AddressWithType remote, SecurityPolicy policy, ResultCallback result_callback) override {
43     if (policy == SecurityPolicy::NO_SECURITY_WHATSOEVER_PLAINTEXT_TRANSPORT_OK) {
44       result_callback.InvokeIfNotEmpty(true);
45     } else {
46       result_callback.InvokeIfNotEmpty(false);
47     }
48   }
49 };
50 static SecurityEnforcementRejectAllImpl default_security_module_impl_;
51 
52 struct L2capLeModule::impl {
implbluetooth::l2cap::le::L2capLeModule::impl53   impl(os::Handler* l2cap_handler, hci::AclManager* acl_manager)
54       : l2cap_handler_(l2cap_handler), acl_manager_(acl_manager) {
55     dynamic_channel_service_manager_impl_.SetSecurityEnforcementInterface(&default_security_module_impl_);
56   }
57   os::Handler* l2cap_handler_;
58   hci::AclManager* acl_manager_;
59   l2cap::internal::ParameterProvider parameter_provider_;
60   internal::FixedChannelServiceManagerImpl fixed_channel_service_manager_impl_{l2cap_handler_};
61   internal::DynamicChannelServiceManagerImpl dynamic_channel_service_manager_impl_{l2cap_handler_};
62   internal::LinkManager link_manager_{l2cap_handler_,
63                                       acl_manager_,
64                                       &fixed_channel_service_manager_impl_,
65                                       &dynamic_channel_service_manager_impl_,
66                                       &parameter_provider_};
67 };
68 
L2capLeModule()69 L2capLeModule::L2capLeModule() {}
~L2capLeModule()70 L2capLeModule::~L2capLeModule() {}
71 
ListDependencies(ModuleList * list)72 void L2capLeModule::ListDependencies(ModuleList* list) {
73   list->add<hci::AclManager>();
74 }
75 
Start()76 void L2capLeModule::Start() {
77   pimpl_ = std::make_unique<impl>(GetHandler(), GetDependency<hci::AclManager>());
78 }
79 
Stop()80 void L2capLeModule::Stop() {
81   pimpl_.reset();
82 }
83 
ToString() const84 std::string L2capLeModule::ToString() const {
85   return "L2cap Le Module";
86 }
87 
GetFixedChannelManager()88 std::unique_ptr<FixedChannelManager> L2capLeModule::GetFixedChannelManager() {
89   return std::unique_ptr<FixedChannelManager>(new FixedChannelManager(&pimpl_->fixed_channel_service_manager_impl_,
90                                                                       &pimpl_->link_manager_, pimpl_->l2cap_handler_));
91 }
92 
GetDynamicChannelManager()93 std::unique_ptr<DynamicChannelManager> L2capLeModule::GetDynamicChannelManager() {
94   return std::unique_ptr<DynamicChannelManager>(new DynamicChannelManager(
95       &pimpl_->dynamic_channel_service_manager_impl_, &pimpl_->link_manager_, pimpl_->l2cap_handler_));
96 }
97 
InjectSecurityEnforcementInterface(SecurityEnforcementInterface * security_enforcement_interface)98 void L2capLeModule::InjectSecurityEnforcementInterface(SecurityEnforcementInterface* security_enforcement_interface) {
99   if (security_enforcement_interface != nullptr) {
100     pimpl_->dynamic_channel_service_manager_impl_.SetSecurityEnforcementInterface(security_enforcement_interface);
101   } else {
102     pimpl_->dynamic_channel_service_manager_impl_.SetSecurityEnforcementInterface(&default_security_module_impl_);
103   }
104 }
105 
SetLinkPropertyListener(os::Handler * handler,LinkPropertyListener * listener)106 void L2capLeModule::SetLinkPropertyListener(os::Handler* handler, LinkPropertyListener* listener) {
107   pimpl_->link_manager_.RegisterLinkPropertyListener(handler, listener);
108 }
109 
110 }  // namespace le
111 }  // namespace l2cap
112 }  // namespace bluetooth
113