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 #include "internal_include/bt_trace.h"
20 
21 namespace bluetooth {
22 namespace avrcp {
23 
MakeBuilder(Status status)24 std::unique_ptr<PlayItemResponseBuilder> PlayItemResponseBuilder::MakeBuilder(Status status) {
25   std::unique_ptr<PlayItemResponseBuilder> builder(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(const std::shared_ptr<::bluetooth::Packet>& pkt) {
37   ReserveSpace(pkt, size());
38 
39   PacketBuilder::PushHeader(pkt);
40 
41   VendorPacketBuilder::PushHeader(pkt, size() - VendorPacket::kMinSize());
42 
43   AddPayloadOctets1(pkt, (uint8_t)status_);
44 
45   return true;
46 }
47 
GetScope() const48 Scope PlayItemRequest::GetScope() const {
49   auto it = begin() + VendorPacket::kMinSize();
50   return static_cast<Scope>(*it);
51 }
52 
GetUid() const53 uint64_t PlayItemRequest::GetUid() const {
54   auto it = begin() + VendorPacket::kMinSize() + static_cast<size_t>(1);
55   return it.extractBE<uint64_t>();
56 }
57 
GetUidCounter() const58 uint16_t PlayItemRequest::GetUidCounter() const {
59   auto it = begin() + VendorPacket::kMinSize() + static_cast<size_t>(9);
60   return it.extractBE<uint16_t>();
61 }
62 
IsValid() const63 bool PlayItemRequest::IsValid() const {
64   if (!VendorPacket::IsValid()) {
65     return false;
66   }
67   return size() == kMinSize();
68 }
69 
ToString() const70 std::string PlayItemRequest::ToString() const {
71   std::stringstream ss;
72   ss << "PlayItemRequest: " << std::endl;
73   ss << "  └ cType = " << GetCType() << std::endl;
74   ss << "  └ Subunit Type = " << loghex(GetSubunitType()) << std::endl;
75   ss << "  └ Subunit ID = " << loghex(GetSubunitId()) << std::endl;
76   ss << "  └ OpCode = " << GetOpcode() << std::endl;
77   ss << "  └ Company ID = " << loghex(GetCompanyId()) << std::endl;
78   ss << "  └ Command PDU = " << GetCommandPdu() << std::endl;
79   ss << "  └ PacketType = " << GetPacketType() << std::endl;
80   ss << "  └ Parameter Length = " << loghex(GetParameterLength()) << std::endl;
81   ss << "  └ Scope = " << GetScope() << std::endl;
82   ss << "  └ UID = " << loghex(GetUid()) << std::endl;
83   ss << "  └ UID Counter = " << loghex(GetUidCounter()) << std::endl;
84   ss << std::endl;
85 
86   return ss.str();
87 }
88 
89 }  // namespace avrcp
90 }  // namespace bluetooth
91