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