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 "set_player_application_setting_value.h"
18 
19 namespace bluetooth {
20 namespace avrcp {
21 
22 std::unique_ptr<SetPlayerApplicationSettingValueResponseBuilder>
MakeBuilder()23 SetPlayerApplicationSettingValueResponseBuilder::MakeBuilder() {
24   std::unique_ptr<SetPlayerApplicationSettingValueResponseBuilder> builder(
25       new SetPlayerApplicationSettingValueResponseBuilder());
26 
27   return builder;
28 }
29 
size() const30 size_t SetPlayerApplicationSettingValueResponseBuilder::size() const {
31   return VendorPacket::kMinSize();
32 }
33 
Serialize(const std::shared_ptr<::bluetooth::Packet> & pkt)34 bool SetPlayerApplicationSettingValueResponseBuilder::Serialize(
35     const std::shared_ptr<::bluetooth::Packet>& pkt) {
36   ReserveSpace(pkt, size());
37 
38   PacketBuilder::PushHeader(pkt);
39 
40   VendorPacketBuilder::PushHeader(pkt, 0);
41 
42   return true;
43 }
44 
45 uint8_t
GetNumberOfRequestedAttributes() const46 SetPlayerApplicationSettingValueRequest::GetNumberOfRequestedAttributes()
47     const {
48   auto it = begin() + VendorPacket::kMinSize();
49   return *it;
50 }
51 
52 std::vector<PlayerAttribute>
GetRequestedAttributes() const53 SetPlayerApplicationSettingValueRequest::GetRequestedAttributes() const {
54   auto it = begin() + VendorPacket::kMinSize() +
55             static_cast<size_t>(1);  // Point to the first attribute
56   std::vector<PlayerAttribute> attribute_list;
57 
58   for (; it < end(); it++) {
59     attribute_list.push_back(static_cast<PlayerAttribute>(*it));
60     it++;  // Skip value
61   }
62 
63   return attribute_list;
64 }
65 
66 std::vector<uint8_t>
GetRequestedValues() const67 SetPlayerApplicationSettingValueRequest::GetRequestedValues() const {
68   auto it = begin() + VendorPacket::kMinSize() +
69             static_cast<size_t>(1);  // Point to the first attribute
70   std::vector<uint8_t> values_list;
71 
72   for (; it < end(); it++) {
73     it++;  // Skip attribute
74     values_list.push_back(static_cast<uint8_t>(*it));
75   }
76 
77   return values_list;
78 }
79 
IsValid() const80 bool SetPlayerApplicationSettingValueRequest::IsValid() const {
81   if (!VendorPacket::IsValid()) return false;
82   if (size() < kMinSize()) return false;
83 
84   size_t num_of_attrs = GetNumberOfRequestedAttributes();
85   auto attr_start = begin() + VendorPacket::kMinSize() + static_cast<size_t>(1);
86 
87   return (num_of_attrs * 2 * sizeof(uint8_t)) == (size_t)(end() - attr_start);
88 }
89 
ToString() const90 std::string SetPlayerApplicationSettingValueRequest::ToString() const {
91   std::stringstream ss;
92   ss << "SetPlayerApplicationSettingValueRequest: " << std::endl;
93   ss << "  └ cType = " << GetCType() << std::endl;
94   ss << "  └ Subunit Type = " << loghex(GetSubunitType()) << std::endl;
95   ss << "  └ Subunit ID = " << loghex(GetSubunitId()) << std::endl;
96   ss << "  └ OpCode = " << GetOpcode() << std::endl;
97   ss << "  └ Company ID = " << loghex(GetCompanyId()) << std::endl;
98   ss << "  └ Command PDU = " << GetCommandPdu() << std::endl;
99   ss << "  └ PacketType = " << GetPacketType() << std::endl;
100   ss << "  └ Parameter Length = " << loghex(GetParameterLength()) << std::endl;
101   ss << "  └ Num Attributes = " << loghex(GetNumberOfRequestedAttributes())
102      << std::endl;
103 
104   auto attribute_list_ = GetRequestedAttributes();
105   auto values_list_ = GetRequestedValues();
106   ss << "  └ Player Attributes and Values List: Size: "
107      << attribute_list_.size() << std::endl;
108   for (size_t i = 0; i < attribute_list_.size(); i++) {
109     ss << "      └ " << static_cast<PlayerAttribute>(attribute_list_.at(i))
110        << ": " << std::to_string(values_list_.at(i)) << std::endl;
111   }
112   ss << std::endl;
113 
114   return ss.str();
115 }
116 
117 }  // namespace avrcp
118 }  // namespace bluetooth
119