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