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 "get_total_number_of_items.h"
18
19 namespace bluetooth {
20 namespace avrcp {
21
22 std::unique_ptr<GetTotalNumberOfItemsResponseBuilder>
MakeBuilder(Status status,uint16_t uid_counter,uint32_t num_items_in_folder)23 GetTotalNumberOfItemsResponseBuilder::MakeBuilder(
24 Status status, uint16_t uid_counter, uint32_t num_items_in_folder) {
25 std::unique_ptr<GetTotalNumberOfItemsResponseBuilder> builder(
26 new GetTotalNumberOfItemsResponseBuilder(status, uid_counter,
27 num_items_in_folder));
28
29 return builder;
30 }
31
size() const32 size_t GetTotalNumberOfItemsResponseBuilder::size() const {
33 size_t len = BrowsePacket::kMinSize();
34 len += 1; // Status
35
36 if (status_ != Status::NO_ERROR) return len;
37
38 len += 2; // UID Counter
39 len += 4; // Number of items in folder
40 return len;
41 }
42
Serialize(const std::shared_ptr<::bluetooth::Packet> & pkt)43 bool GetTotalNumberOfItemsResponseBuilder::Serialize(
44 const std::shared_ptr<::bluetooth::Packet>& pkt) {
45 ReserveSpace(pkt, size());
46
47 BrowsePacketBuilder::PushHeader(pkt, size() - BrowsePacket::kMinSize());
48
49 AddPayloadOctets1(pkt, (uint8_t)status_);
50
51 if (status_ != Status::NO_ERROR) return true;
52 AddPayloadOctets2(pkt, base::ByteSwap(uid_counter_));
53 AddPayloadOctets4(pkt, base::ByteSwap(num_items_in_folder_));
54 return true;
55 }
56
GetScope() const57 Scope GetTotalNumberOfItemsRequest::GetScope() const {
58 auto it = begin() + BrowsePacket::kMinSize();
59 return static_cast<Scope>(*it);
60 }
61
IsValid() const62 bool GetTotalNumberOfItemsRequest::IsValid() const {
63 if (!BrowsePacket::IsValid()) return false;
64 return size() == kMinSize();
65 }
66
ToString() const67 std::string GetTotalNumberOfItemsRequest::ToString() const {
68 std::stringstream ss;
69 ss << "GetTotalNumberOfItemsRequest: " << std::endl;
70 ss << " └ PDU = " << GetPdu() << std::endl;
71 ss << " └ Length = " << GetLength() << std::endl;
72 ss << " └ Scope = " << GetScope() << std::endl;
73 ss << std::endl;
74
75 return ss.str();
76 }
77
78 } // namespace avrcp
79 } // namespace bluetooth
80