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