summaryrefslogtreecommitdiff
path: root/system/gd/l2cap/classic/l2cap_classic_module.cc
blob: 5c43373ccf07a5cdd6087ef8408fea0dd89f3d7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * Copyright 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#define LOG_TAG "l2cap2"

#include <future>
#include <memory>

#include "common/bidi_queue.h"
#include "hci/acl_manager.h"
#include "hci/address.h"
#include "hci/hci_layer.h"
#include "hci/hci_packets.h"
#include "l2cap/classic/internal/dumpsys_helper.h"
#include "l2cap/classic/internal/dynamic_channel_service_manager_impl.h"
#include "l2cap/classic/internal/fixed_channel_service_manager_impl.h"
#include "l2cap/classic/internal/link_manager.h"
#include "l2cap/classic/l2cap_classic_module.h"
#include "l2cap/internal/parameter_provider.h"
#include "l2cap_classic_module_generated.h"
#include "module.h"
#include "os/handler.h"
#include "os/log.h"

namespace bluetooth {
namespace l2cap {
namespace classic {

const ModuleFactory L2capClassicModule::Factory = ModuleFactory([]() { return new L2capClassicModule(); });

static SecurityEnforcementRejectAllImpl default_security_module_impl_;

struct L2capClassicModule::impl {
  impl(os::Handler* l2cap_handler, hci::AclManager* acl_manager)
      : l2cap_handler_(l2cap_handler), acl_manager_(acl_manager) {
    dynamic_channel_service_manager_impl_.SetSecurityEnforcementInterface(&default_security_module_impl_);
    dumpsys_helper_ = std::make_unique<internal::DumpsysHelper>(link_manager_);
  }
  os::Handler* l2cap_handler_;
  hci::AclManager* acl_manager_;
  l2cap::internal::ParameterProvider parameter_provider_;
  internal::FixedChannelServiceManagerImpl fixed_channel_service_manager_impl_{l2cap_handler_};
  internal::DynamicChannelServiceManagerImpl dynamic_channel_service_manager_impl_{l2cap_handler_};
  internal::LinkManager link_manager_{l2cap_handler_, acl_manager_, &fixed_channel_service_manager_impl_,
                                      &dynamic_channel_service_manager_impl_, &parameter_provider_};
  std::unique_ptr<internal::DumpsysHelper> dumpsys_helper_;

  struct SecurityInterfaceImpl : public SecurityInterface {
    SecurityInterfaceImpl(impl* module_impl) : module_impl_(module_impl) {}

    void RegisterLinkSecurityInterfaceListener(os::Handler* handler, LinkSecurityInterfaceListener* listener) {
      ASSERT(!registered_);
      module_impl_->link_manager_.RegisterLinkSecurityInterfaceListener(handler, listener);
      registered_ = true;
    }

    void InitiateConnectionForSecurity(hci::Address remote) override {
      ASSERT(registered_);
      module_impl_->link_manager_.InitiateConnectionForSecurity(remote);
    }

    void Unregister() override {
      ASSERT(registered_);
      module_impl_->link_manager_.RegisterLinkSecurityInterfaceListener(nullptr, nullptr);
      registered_ = false;
    }
    impl* module_impl_;
    bool registered_ = false;
  } security_interface_impl_{this};

  void Dump(
      std::promise<flatbuffers::Offset<L2capClassicModuleData>> promise,
      flatbuffers::FlatBufferBuilder* fb_builder) const;
};

L2capClassicModule::L2capClassicModule() {}

L2capClassicModule::~L2capClassicModule() {}

void L2capClassicModule::ListDependencies(ModuleList* list) const {
  list->add<hci::AclManager>();
}

void L2capClassicModule::Start() {
  pimpl_ = std::make_unique<impl>(GetHandler(), GetDependency<hci::AclManager>());
}

void L2capClassicModule::Stop() {
  pimpl_.reset();
}

std::string L2capClassicModule::ToString() const {
  return "L2cap Classic Module";
}

std::unique_ptr<FixedChannelManager> L2capClassicModule::GetFixedChannelManager() {
  return std::unique_ptr<FixedChannelManager>(new FixedChannelManager(&pimpl_->fixed_channel_service_manager_impl_,
                                                                      &pimpl_->link_manager_, pimpl_->l2cap_handler_));
}

std::unique_ptr<DynamicChannelManager> L2capClassicModule::GetDynamicChannelManager() {
  return std::unique_ptr<DynamicChannelManager>(new DynamicChannelManager(
      &pimpl_->dynamic_channel_service_manager_impl_, &pimpl_->link_manager_, pimpl_->l2cap_handler_));
}

void L2capClassicModule::InjectSecurityEnforcementInterface(
    SecurityEnforcementInterface* security_enforcement_interface) {
  if (security_enforcement_interface != nullptr) {
    pimpl_->dynamic_channel_service_manager_impl_.SetSecurityEnforcementInterface(security_enforcement_interface);
  } else {
    pimpl_->dynamic_channel_service_manager_impl_.SetSecurityEnforcementInterface(&default_security_module_impl_);
  }
}

SecurityInterface* L2capClassicModule::GetSecurityInterface(
    os::Handler* handler, LinkSecurityInterfaceListener* listener) {
  pimpl_->security_interface_impl_.RegisterLinkSecurityInterfaceListener(handler, listener);
  return &pimpl_->security_interface_impl_;
}

void L2capClassicModule::SetLinkPropertyListener(os::Handler* handler, LinkPropertyListener* listener) {
  pimpl_->link_manager_.RegisterLinkPropertyListener(handler, listener);
}

void L2capClassicModule::impl::Dump(
    std::promise<flatbuffers::Offset<L2capClassicModuleData>> promise,
    flatbuffers::FlatBufferBuilder* fb_builder) const {
  auto title = fb_builder->CreateString("----- L2cap Classic Dumpsys -----");

  std::vector<flatbuffers::Offset<bluetooth::l2cap::classic::LinkData>> link_offsets =
      dumpsys_helper_->DumpActiveLinks(fb_builder);

  auto active_links = fb_builder->CreateVector(link_offsets);

  L2capClassicModuleDataBuilder builder(*fb_builder);
  builder.add_title(title);
  builder.add_active_links(active_links);
  flatbuffers::Offset<L2capClassicModuleData> dumpsys_data = builder.Finish();

  promise.set_value(dumpsys_data);
}

DumpsysDataFinisher L2capClassicModule::GetDumpsysData(flatbuffers::FlatBufferBuilder* fb_builder) const {
  ASSERT(fb_builder != nullptr);

  std::promise<flatbuffers::Offset<L2capClassicModuleData>> promise;
  auto future = promise.get_future();
  pimpl_->Dump(std::move(promise), fb_builder);

  auto dumpsys_data = future.get();

  return [dumpsys_data](DumpsysDataBuilder* dumpsys_builder) {
    dumpsys_builder->add_l2cap_classic_dumpsys_data(dumpsys_data);
  };
}

}  // namespace classic
}  // namespace l2cap
}  // namespace bluetooth