1 /*
2  * Copyright 2020 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 <string>
18 
19 #include "l2cap/classic/internal/dumpsys_helper.h"
20 #include "l2cap/classic/internal/fixed_channel_impl.h"
21 #include "l2cap/classic/internal/link.h"
22 #include "l2cap/classic/internal/link_manager.h"
23 #include "l2cap/internal/dynamic_channel_impl.h"
24 #include "l2cap_classic_module_generated.h"
25 #include "os/log.h"
26 
DumpsysHelper(const LinkManager & link_manager)27 bluetooth::l2cap::classic::internal::DumpsysHelper::DumpsysHelper(const LinkManager& link_manager)
28     : link_manager_(link_manager) {}
29 
30 std::vector<flatbuffers::Offset<bluetooth::l2cap::classic::ChannelData>>
DumpActiveDynamicChannels(flatbuffers::FlatBufferBuilder * fb_builder,const l2cap::internal::DynamicChannelAllocator & channel_allocator) const31 bluetooth::l2cap::classic::internal::DumpsysHelper::DumpActiveDynamicChannels(
32     flatbuffers::FlatBufferBuilder* fb_builder,
33     const l2cap::internal::DynamicChannelAllocator& channel_allocator) const {
34   std::vector<flatbuffers::Offset<bluetooth::l2cap::classic::ChannelData>> channel_offsets;
35 
36   for (auto it = channel_allocator.channels_.cbegin(); it != channel_allocator.channels_.cend(); ++it) {
37     ChannelDataBuilder builder(*fb_builder);
38     builder.add_cid(it->first);
39     channel_offsets.push_back(builder.Finish());
40   }
41   return channel_offsets;
42 }
43 
44 std::vector<flatbuffers::Offset<::bluetooth::l2cap::classic::ChannelData>>
DumpActiveFixedChannels(flatbuffers::FlatBufferBuilder * fb_builder,const bluetooth::l2cap::internal::FixedChannelAllocator<bluetooth::l2cap::classic::internal::FixedChannelImpl,bluetooth::l2cap::classic::internal::Link> & channel_allocator) const45 bluetooth::l2cap::classic::internal::DumpsysHelper::DumpActiveFixedChannels(
46     flatbuffers::FlatBufferBuilder* fb_builder,
47     const bluetooth::l2cap::internal::FixedChannelAllocator<
48         bluetooth::l2cap::classic::internal::FixedChannelImpl,
49         bluetooth::l2cap::classic::internal::Link>& channel_allocator) const {
50   std::vector<flatbuffers::Offset<bluetooth::l2cap::classic::ChannelData>> channel_offsets;
51 
52   for (auto it = channel_allocator.channels_.cbegin(); it != channel_allocator.channels_.cend(); ++it) {
53     ChannelDataBuilder builder(*fb_builder);
54     builder.add_cid(it->first);
55     channel_offsets.push_back(builder.Finish());
56   }
57   return channel_offsets;
58 }
59 
60 std::vector<flatbuffers::Offset<bluetooth::l2cap::classic::LinkData>>
DumpActiveLinks(flatbuffers::FlatBufferBuilder * fb_builder) const61 bluetooth::l2cap::classic::internal::DumpsysHelper::DumpActiveLinks(flatbuffers::FlatBufferBuilder* fb_builder) const {
62   const std::unordered_map<hci::Address, Link>* links = &link_manager_.links_;
63 
64   std::vector<flatbuffers::Offset<LinkData>> link_offsets;
65 
66   for (auto it = links->cbegin(); it != links->cend(); ++it) {
67     auto link_address = fb_builder->CreateString(it->second.ToString());
68     auto dynamic_channel_offsets = DumpActiveDynamicChannels(fb_builder, it->second.dynamic_channel_allocator_);
69     auto dynamic_channels = fb_builder->CreateVector(dynamic_channel_offsets);
70 
71     auto fixed_channel_offsets = DumpActiveFixedChannels(fb_builder, it->second.fixed_channel_allocator_);
72     auto fixed_channels = fb_builder->CreateVector(fixed_channel_offsets);
73 
74     LinkDataBuilder builder(*fb_builder);
75     builder.add_address(link_address);
76     builder.add_dynamic_channels(dynamic_channels);
77     builder.add_fixed_channels(fixed_channels);
78     link_offsets.push_back(builder.Finish());
79   }
80   return link_offsets;
81 }
82