aboutsummaryrefslogtreecommitdiff
path: root/gd/l2cap/classic/internal/dumpsys_helper.cc
blob: d69d690d82c18c9153230c9f3f20b846258a0d76 (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
/*
 * Copyright 2020 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.
 */

#include <string>

#include "l2cap/classic/internal/dumpsys_helper.h"
#include "l2cap/classic/internal/fixed_channel_impl.h"
#include "l2cap/classic/internal/link.h"
#include "l2cap/classic/internal/link_manager.h"
#include "l2cap/internal/dynamic_channel_impl.h"
#include "l2cap_classic_module_generated.h"
#include "os/log.h"

bluetooth::l2cap::classic::internal::DumpsysHelper::DumpsysHelper(const LinkManager& link_manager)
    : link_manager_(link_manager) {}

std::vector<flatbuffers::Offset<bluetooth::l2cap::classic::ChannelData>>
bluetooth::l2cap::classic::internal::DumpsysHelper::DumpActiveDynamicChannels(
    flatbuffers::FlatBufferBuilder* fb_builder,
    const l2cap::internal::DynamicChannelAllocator& channel_allocator) const {
  std::vector<flatbuffers::Offset<bluetooth::l2cap::classic::ChannelData>> channel_offsets;

  for (auto it = channel_allocator.channels_.cbegin(); it != channel_allocator.channels_.cend(); ++it) {
    ChannelDataBuilder builder(*fb_builder);
    builder.add_cid(it->first);
    channel_offsets.push_back(builder.Finish());
  }
  return channel_offsets;
}

std::vector<flatbuffers::Offset<::bluetooth::l2cap::classic::ChannelData>>
bluetooth::l2cap::classic::internal::DumpsysHelper::DumpActiveFixedChannels(
    flatbuffers::FlatBufferBuilder* fb_builder,
    const bluetooth::l2cap::internal::FixedChannelAllocator<
        bluetooth::l2cap::classic::internal::FixedChannelImpl,
        bluetooth::l2cap::classic::internal::Link>& channel_allocator) const {
  std::vector<flatbuffers::Offset<bluetooth::l2cap::classic::ChannelData>> channel_offsets;

  for (auto it = channel_allocator.channels_.cbegin(); it != channel_allocator.channels_.cend(); ++it) {
    ChannelDataBuilder builder(*fb_builder);
    builder.add_cid(it->first);
    channel_offsets.push_back(builder.Finish());
  }
  return channel_offsets;
}

std::vector<flatbuffers::Offset<bluetooth::l2cap::classic::LinkData>>
bluetooth::l2cap::classic::internal::DumpsysHelper::DumpActiveLinks(flatbuffers::FlatBufferBuilder* fb_builder) const {
  const std::unordered_map<hci::Address, Link>* links = &link_manager_.links_;

  std::vector<flatbuffers::Offset<LinkData>> link_offsets;

  for (auto it = links->cbegin(); it != links->cend(); ++it) {
    auto link_address = fb_builder->CreateString(it->second.ToString());
    auto dynamic_channel_offsets = DumpActiveDynamicChannels(fb_builder, it->second.dynamic_channel_allocator_);
    auto dynamic_channels = fb_builder->CreateVector(dynamic_channel_offsets);

    auto fixed_channel_offsets = DumpActiveFixedChannels(fb_builder, it->second.fixed_channel_allocator_);
    auto fixed_channels = fb_builder->CreateVector(fixed_channel_offsets);

    LinkDataBuilder builder(*fb_builder);
    builder.add_address(link_address);
    builder.add_dynamic_channels(dynamic_channels);
    builder.add_fixed_channels(fixed_channels);
    link_offsets.push_back(builder.Finish());
  }
  return link_offsets;
}