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 "play_item.h"
18 
19 namespace bluetooth {
20 namespace avrcp {
21 
MakeBuilder(Status status)22 std::unique_ptr<PlayItemResponseBuilder> PlayItemResponseBuilder::MakeBuilder(
23     Status status) {
24   std::unique_ptr<PlayItemResponseBuilder> builder(
25       new PlayItemResponseBuilder(status));
26 
27   return builder;
28 }
29 
size() const30 size_t PlayItemResponseBuilder::size() const {
31   size_t len = VendorPacket::kMinSize();
32   len += 1;  // Status
33   return len;
34 }
35 
Serialize(const std::shared_ptr<::bluetooth::Packet> & pkt)36 bool PlayItemResponseBuilder::Serialize(
37     const std::shared_ptr<::bluetooth::Packet>& pkt) {
38   ReserveSpace(pkt, size());
39 
40   PacketBuilder::PushHeader(pkt);
41 
42   VendorPacketBuilder::PushHeader(pkt, size() - VendorPacket::kMinSize());
43 
44   AddPayloadOctets1(pkt, (uint8_t)status_);
45 
46   return true;
47 }
48 
GetScope() const49 Scope PlayItemRequest::GetScope() const {
50   auto it = begin() + VendorPacket::kMinSize();
51   return static_cast<Scope>(*it);
52 }
53 
GetUid() const54 uint64_t PlayItemRequest::GetUid() const {
55   auto it = begin() + VendorPacket::kMinSize() + static_cast<size_t>(1);
56   return it.extractBE<uint64_t>();
57 }
58 
GetUidCounter() const59 uint16_t PlayItemRequest::GetUidCounter() const {
60   auto it = begin() + VendorPacket::kMinSize() + static_cast<size_t>(9);
61   return it.extractBE<uint16_t>();
62 }
63 
IsValid() const64 bool PlayItemRequest::IsValid() const {
65   if (!VendorPacket::IsValid()) return false;
66   return size() == kMinSize();
67 }
68 
ToString() const69 std::string PlayItemRequest::ToString() const {
70   std::stringstream ss;
71   ss << "PlayItemRequest: " << std::endl;
72   ss << "  └ cType = " << GetCType() << std::endl;
73   ss << "  └ Subunit Type = " << loghex(GetSubunitType()) << std::endl;
74   ss << "  └ Subunit ID = " << loghex(GetSubunitId()) << std::endl;
75   ss << "  └ OpCode = " << GetOpcode() << std::endl;
76   ss << "  └ Company ID = " << loghex(GetCompanyId()) << std::endl;
77   ss << "  └ Command PDU = " << GetCommandPdu() << std::endl;
78   ss << "  └ PacketType = " << GetPacketType() << std::endl;
79   ss << "  └ Parameter Length = " << loghex(GetParameterLength()) << std::endl;
80   ss << "  └ Scope = " << GetScope() << std::endl;
81   ss << "  └ UID = " << loghex(GetUid()) << std::endl;
82   ss << "  └ UID Counter = " << loghex(GetUidCounter()) << std::endl;
83   ss << std::endl;
84 
85   return ss.str();
86 }
87 
88 }  // namespace avrcp
89 }  // namespace bluetooth
90