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 "set_absolute_volume.h"
18 
19 namespace bluetooth {
20 namespace avrcp {
21 
22 std::unique_ptr<SetAbsoluteVolumeRequestBuilder>
MakeBuilder(uint8_t volume)23 SetAbsoluteVolumeRequestBuilder::MakeBuilder(uint8_t volume) {
24   std::unique_ptr<SetAbsoluteVolumeRequestBuilder> builder(
25       new SetAbsoluteVolumeRequestBuilder(volume & 0x7F));
26 
27   return builder;
28 }
29 
size() const30 size_t SetAbsoluteVolumeRequestBuilder::size() const {
31   return VendorPacket::kMinSize() + 1;
32 }
33 
Serialize(const std::shared_ptr<::bluetooth::Packet> & pkt)34 bool SetAbsoluteVolumeRequestBuilder::Serialize(
35     const std::shared_ptr<::bluetooth::Packet>& pkt) {
36   ReserveSpace(pkt, size());
37 
38   PacketBuilder::PushHeader(pkt);
39 
40   VendorPacketBuilder::PushHeader(pkt, size() - VendorPacket::kMinSize());
41 
42   AddPayloadOctets1(pkt, volume_);
43 
44   return true;
45 }
46 
GetVolume() const47 uint8_t SetAbsoluteVolumeResponse::GetVolume() const {
48   auto it = begin() + VendorPacket::kMinSize();
49   return *it;
50 }
51 
IsValid() const52 bool SetAbsoluteVolumeResponse::IsValid() const {
53   if (!VendorPacket::IsValid()) return false;
54   if (GetCType() != CType::ACCEPTED) return false;
55   return size() == kMinSize();
56 }
57 
ToString() const58 std::string SetAbsoluteVolumeResponse::ToString() const {
59   std::stringstream ss;
60   ss << "SetAbsoluteVolumeResponse: " << std::endl;
61   ss << "  └ cType = " << GetCType() << std::endl;
62   ss << "  └ Subunit Type = " << loghex(GetSubunitType()) << std::endl;
63   ss << "  └ Subunit ID = " << loghex(GetSubunitId()) << std::endl;
64   ss << "  └ OpCode = " << GetOpcode() << std::endl;
65   ss << "  └ Company ID = " << loghex(GetCompanyId()) << std::endl;
66   ss << "  └ Command PDU = " << GetCommandPdu() << std::endl;
67   ss << "  └ PacketType = " << GetPacketType() << std::endl;
68   ss << "  └ Parameter Length = " << loghex(GetParameterLength()) << std::endl;
69   ss << "  └ Volume = " << GetVolume() << std::endl;
70   ss << std::endl;
71 
72   return ss.str();
73 }
74 
75 }  // namespace avrcp
76 }  // namespace bluetooth