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 #include "internal_include/bt_trace.h"
20 
21 namespace bluetooth {
22 namespace avrcp {
23 
24 std::unique_ptr<GetCurrentPlayerApplicationSettingValueResponseBuilder>
MakeBuilder(std::vector<PlayerAttribute> attributes,std::vector<uint8_t> values)25 GetCurrentPlayerApplicationSettingValueResponseBuilder::MakeBuilder(
26         std::vector<PlayerAttribute> attributes, std::vector<uint8_t> values) {
27   std::unique_ptr<GetCurrentPlayerApplicationSettingValueResponseBuilder> builder(
28           new GetCurrentPlayerApplicationSettingValueResponseBuilder(std::move(attributes),
29                                                                      std::move(values)));
30 
31   return builder;
32 }
33 
size() const34 size_t GetCurrentPlayerApplicationSettingValueResponseBuilder::size() const {
35   size_t len = VendorPacket::kMinSize();
36   len += sizeof(uint8_t);                       // Number of attributes size
37   len += attributes_.size() * sizeof(uint8_t);  // Attributes
38   len += values_.size() * sizeof(uint8_t);      // Attributes' values
39   return len;
40 }
41 
Serialize(const std::shared_ptr<::bluetooth::Packet> & pkt)42 bool GetCurrentPlayerApplicationSettingValueResponseBuilder::Serialize(
43         const std::shared_ptr<::bluetooth::Packet>& pkt) {
44   ReserveSpace(pkt, size());
45 
46   PacketBuilder::PushHeader(pkt);
47 
48   VendorPacketBuilder::PushHeader(pkt, size() - VendorPacket::kMinSize());
49 
50   AddPayloadOctets1(pkt, (uint8_t)attributes_.size());
51   for (size_t i = 0; i < attributes_.size(); i++) {
52     AddPayloadOctets1(pkt, (uint8_t)attributes_[i]);
53     AddPayloadOctets1(pkt, (uint8_t)values_[i]);
54   }
55 
56   return true;
57 }
58 
GetNumberOfRequestedAttributes() const59 uint8_t GetCurrentPlayerApplicationSettingValueRequest::GetNumberOfRequestedAttributes() 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()) {
77     return false;
78   }
79   if (size() < kMinSize()) {
80     return false;
81   }
82 
83   size_t num_attributes = GetNumberOfRequestedAttributes();
84   auto attr_start = begin() + VendorPacket::kMinSize() + static_cast<size_t>(1);
85 
86   return (num_attributes * sizeof(uint8_t)) == (size_t)(end() - attr_start);
87 }
88 
ToString() const89 std::string GetCurrentPlayerApplicationSettingValueRequest::ToString() const {
90   std::stringstream ss;
91   ss << "GetCurrentPlayerApplicationSettingValueRequest: " << std::endl;
92   ss << "  └ cType = " << GetCType() << std::endl;
93   ss << "  └ Subunit Type = " << loghex(GetSubunitType()) << std::endl;
94   ss << "  └ Subunit ID = " << loghex(GetSubunitId()) << std::endl;
95   ss << "  └ OpCode = " << GetOpcode() << std::endl;
96   ss << "  └ Company ID = " << loghex(GetCompanyId()) << std::endl;
97   ss << "  └ Command PDU = " << GetCommandPdu() << std::endl;
98   ss << "  └ PacketType = " << GetPacketType() << std::endl;
99   ss << "  └ Parameter Length = " << loghex(GetParameterLength()) << std::endl;
100   ss << "  └ Num Attributes = " << loghex(GetNumberOfRequestedAttributes()) << std::endl;
101 
102   auto attr_list = GetRequestedAttributes();
103   ss << "  └ Player Attribute List: Size: " << attr_list.size() << std::endl;
104   for (auto it = attr_list.begin(); it != attr_list.end(); it++) {
105     ss << "      └ " << static_cast<PlayerAttribute>(*it) << std::endl;
106   }
107   ss << std::endl;
108 
109   return ss.str();
110 }
111 
112 }  // namespace avrcp
113 }  // namespace bluetooth
114