1 /*
2  * Copyright 2018 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 <algorithm>
18 
19 #include "get_element_attributes_packet.h"
20 
21 namespace bluetooth {
22 namespace avrcp {
23 
GetIdentifier() const24 uint64_t GetElementAttributesRequest::GetIdentifier() const {
25   auto it = begin() + VendorPacket::kMinSize();
26   return it.extract<uint64_t>();
27 }
28 
GetNumAttributes() const29 uint8_t GetElementAttributesRequest::GetNumAttributes() const {
30   auto it = begin() + VendorPacket::kMinSize() + static_cast<size_t>(8);
31   return it.extract<uint8_t>();
32 }
33 
GetAttributesRequested() const34 std::vector<Attribute> GetElementAttributesRequest::GetAttributesRequested()
35     const {
36   auto it = begin() + VendorPacket::kMinSize() + static_cast<size_t>(8);
37 
38   size_t number_of_attributes = it.extract<uint8_t>();
39 
40   std::vector<Attribute> attribute_list;
41 
42   for (size_t i = 0; i < number_of_attributes; i++) {
43     attribute_list.push_back((Attribute)it.extractBE<uint32_t>());
44   }
45 
46   return attribute_list;
47 }
48 
IsValid() const49 bool GetElementAttributesRequest::IsValid() const {
50   if (!VendorPacket::IsValid()) return false;
51   if (size() < kMinSize()) return false;
52 
53   size_t num_attributes = GetNumAttributes();
54   auto attr_start = begin() + VendorPacket::kMinSize() + static_cast<size_t>(9);
55 
56   // Casting the int returned from end - attr_start should be fine. If an
57   // overflow occurs we can definitly say the packet is invalid
58   return (num_attributes * sizeof(Attribute)) == (size_t)(end() - attr_start);
59 }
60 
ToString() const61 std::string GetElementAttributesRequest::ToString() const {
62   std::stringstream ss;
63   ss << "RegisterNotificationPacket: " << std::endl;
64   ss << "  └ cType = " << GetCType() << std::endl;
65   ss << "  └ Subunit Type = " << loghex(GetSubunitType()) << std::endl;
66   ss << "  └ Subunit ID = " << loghex(GetSubunitId()) << std::endl;
67   ss << "  └ OpCode = " << GetOpcode() << std::endl;
68   ss << "  └ Company ID = " << loghex(GetCompanyId()) << std::endl;
69   ss << "  └ Command PDU = " << GetCommandPdu() << std::endl;
70   ss << "  └ PacketType = " << GetPacketType() << std::endl;
71   ss << "  └ Parameter Length = " << GetParameterLength() << std::endl;
72   ss << "  └ Identifier = " << loghex(GetIdentifier()) << std::endl;
73 
74   auto attr_list = GetAttributesRequested();
75 
76   ss << "  └ Attribute List: Size: " << attr_list.size() << std::endl;
77   for (auto it = attr_list.begin(); it != attr_list.end(); it++) {
78     ss << "      └ " << loghex((uint32_t)(*it)) << std::endl;
79   }
80   ss << std::endl;
81 
82   return ss.str();
83 }
84 
85 std::unique_ptr<GetElementAttributesResponseBuilder>
MakeBuilder(size_t mtu)86 GetElementAttributesResponseBuilder::MakeBuilder(size_t mtu) {
87   std::unique_ptr<GetElementAttributesResponseBuilder> builder(
88       new GetElementAttributesResponseBuilder(mtu));
89 
90   return builder;
91 }
92 
AddAttributeEntry(AttributeEntry entry)93 bool GetElementAttributesResponseBuilder::AddAttributeEntry(
94     AttributeEntry entry) {
95   CHECK_LT(entries_.size(), size_t(0xFF))
96       << __func__ << ": attribute entry overflow";
97 
98   size_t remaining_space = mtu_ - size();
99   if (entry.size() > remaining_space) {
100     entry.resize(remaining_space);
101   }
102 
103   if (entry.empty()) {
104     return false;
105   }
106 
107   entries_.insert(entry);
108   return true;
109 }
110 
AddAttributeEntry(Attribute attribute,std::string value)111 bool GetElementAttributesResponseBuilder::AddAttributeEntry(Attribute attribute,
112                                                             std::string value) {
113   return AddAttributeEntry(AttributeEntry(attribute, value));
114 }
115 
size() const116 size_t GetElementAttributesResponseBuilder::size() const {
117   size_t attr_list_size = 0;
118 
119   for (auto& attribute_entry : entries_) {
120     attr_list_size += attribute_entry.size();
121   }
122 
123   return VendorPacket::kMinSize() + 1 + attr_list_size;
124 }
125 
Serialize(const std::shared_ptr<::bluetooth::Packet> & pkt)126 bool GetElementAttributesResponseBuilder::Serialize(
127     const std::shared_ptr<::bluetooth::Packet>& pkt) {
128   ReserveSpace(pkt, size());
129 
130   PacketBuilder::PushHeader(pkt);
131 
132   VendorPacketBuilder::PushHeader(pkt, size() - VendorPacket::kMinSize());
133 
134   AddPayloadOctets1(pkt, entries_.size());
135   for (const auto& attribute_entry : entries_) {
136     PushAttributeValue(pkt, attribute_entry);
137   }
138 
139   return true;
140 }
141 
142 }  // namespace avrcp
143 }  // namespace bluetooth
144