1 /*
2 * Copyright 2023 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_current_player_application_setting_value.h"
18
19 namespace bluetooth {
20 namespace avrcp {
21
22 std::unique_ptr<GetCurrentPlayerApplicationSettingValueResponseBuilder>
MakeBuilder(std::vector<PlayerAttribute> attributes,std::vector<uint8_t> values)23 GetCurrentPlayerApplicationSettingValueResponseBuilder::MakeBuilder(
24 std::vector<PlayerAttribute> attributes, std::vector<uint8_t> values) {
25 std::unique_ptr<GetCurrentPlayerApplicationSettingValueResponseBuilder>
26 builder(new GetCurrentPlayerApplicationSettingValueResponseBuilder(
27 std::move(attributes), std::move(values)));
28
29 return builder;
30 }
31
size() const32 size_t GetCurrentPlayerApplicationSettingValueResponseBuilder::size() const {
33 size_t len = VendorPacket::kMinSize();
34 len += sizeof(uint8_t); // Number of attributes size
35 len += attributes_.size() * sizeof(uint8_t); // Attributes
36 len += values_.size() * sizeof(uint8_t); // Attributes' values
37 return len;
38 }
39
Serialize(const std::shared_ptr<::bluetooth::Packet> & pkt)40 bool GetCurrentPlayerApplicationSettingValueResponseBuilder::Serialize(
41 const std::shared_ptr<::bluetooth::Packet>& pkt) {
42 ReserveSpace(pkt, size());
43
44 PacketBuilder::PushHeader(pkt);
45
46 VendorPacketBuilder::PushHeader(pkt, size() - VendorPacket::kMinSize());
47
48 AddPayloadOctets1(pkt, (uint8_t)attributes_.size());
49 for (size_t i = 0; i < attributes_.size(); i++) {
50 AddPayloadOctets1(pkt, (uint8_t)attributes_[i]);
51 AddPayloadOctets1(pkt, (uint8_t)values_[i]);
52 }
53
54 return true;
55 }
56
57 uint8_t
GetNumberOfRequestedAttributes() const58 GetCurrentPlayerApplicationSettingValueRequest::GetNumberOfRequestedAttributes()
59 const {
60 auto it = begin() + VendorPacket::kMinSize();
61 return *it;
62 }
63
64 std::vector<PlayerAttribute>
GetRequestedAttributes() const65 GetCurrentPlayerApplicationSettingValueRequest::GetRequestedAttributes() const {
66 auto it = begin() + VendorPacket::kMinSize();
67 uint8_t number_of_attributes = static_cast<uint8_t>(it.extract8());
68 std::vector<PlayerAttribute> attribute_list;
69 for (size_t i = 0; i < number_of_attributes; i++) {
70 attribute_list.push_back((PlayerAttribute)it.extract8());
71 }
72 return attribute_list;
73 }
74
IsValid() const75 bool GetCurrentPlayerApplicationSettingValueRequest::IsValid() const {
76 if (!VendorPacket::IsValid()) return false;
77 if (size() < kMinSize()) return false;
78
79 size_t num_attributes = GetNumberOfRequestedAttributes();
80 auto attr_start = begin() + VendorPacket::kMinSize() + static_cast<size_t>(1);
81
82 return (num_attributes * sizeof(uint8_t)) == (size_t)(end() - attr_start);
83 }
84
ToString() const85 std::string GetCurrentPlayerApplicationSettingValueRequest::ToString() const {
86 std::stringstream ss;
87 ss << "GetCurrentPlayerApplicationSettingValueRequest: " << std::endl;
88 ss << " └ cType = " << GetCType() << std::endl;
89 ss << " └ Subunit Type = " << loghex(GetSubunitType()) << std::endl;
90 ss << " └ Subunit ID = " << loghex(GetSubunitId()) << std::endl;
91 ss << " └ OpCode = " << GetOpcode() << std::endl;
92 ss << " └ Company ID = " << loghex(GetCompanyId()) << std::endl;
93 ss << " └ Command PDU = " << GetCommandPdu() << std::endl;
94 ss << " └ PacketType = " << GetPacketType() << std::endl;
95 ss << " └ Parameter Length = " << loghex(GetParameterLength()) << std::endl;
96 ss << " └ Num Attributes = " << loghex(GetNumberOfRequestedAttributes())
97 << std::endl;
98
99 auto attr_list = GetRequestedAttributes();
100 ss << " └ Player Attribute List: Size: " << attr_list.size() << std::endl;
101 for (auto it = attr_list.begin(); it != attr_list.end(); it++) {
102 ss << " └ " << static_cast<PlayerAttribute>(*it) << std::endl;
103 }
104 ss << std::endl;
105
106 return ss.str();
107 }
108
109 } // namespace avrcp
110 } // namespace bluetooth
111