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_browsed_player.h"
18 
19 #include "internal_include/bt_trace.h"
20 
21 namespace bluetooth {
22 namespace avrcp {
23 
24 std::unique_ptr<SetBrowsedPlayerResponseBuilder>
MakeBuilder(Status status,uint16_t uid_counter,uint32_t num_items_in_folder,uint8_t folder_depth,std::string folder_name)25 SetBrowsedPlayerResponseBuilder::MakeBuilder(Status status,
26                                              uint16_t uid_counter,
27                                              uint32_t num_items_in_folder,
28                                              uint8_t folder_depth,
29                                              std::string folder_name) {
30   std::unique_ptr<SetBrowsedPlayerResponseBuilder> builder(
31       new SetBrowsedPlayerResponseBuilder(
32           status, uid_counter, num_items_in_folder, folder_depth, folder_name));
33 
34   return builder;
35 }
36 
size() const37 size_t SetBrowsedPlayerResponseBuilder::size() const {
38   size_t len = BrowsePacket::kMinSize();
39   len += 1;  // Status
40 
41   // If the status isn't success the rest of the fields are ommited
42   if (status_ != Status::NO_ERROR) return len;
43 
44   len += 2;  // UID Counter
45   len += 4;  // Number of items in folder
46   len += 2;  // UTF-8 Character Set
47   len += 1;  // Folder Depth
48 
49   // This is only included if the folder returned isn't the root folder
50   if (folder_depth_ != 0) {
51     len += 2;                    // Folder Name Size;
52     len += folder_name_.size();  // Folder Name
53   }
54 
55   return len;
56 }
57 
Serialize(const std::shared_ptr<::bluetooth::Packet> & pkt)58 bool SetBrowsedPlayerResponseBuilder::Serialize(
59     const std::shared_ptr<::bluetooth::Packet>& pkt) {
60   ReserveSpace(pkt, size());
61 
62   BrowsePacketBuilder::PushHeader(pkt, size() - BrowsePacket::kMinSize());
63 
64   AddPayloadOctets1(pkt, (uint8_t)status_);
65 
66   if (status_ != Status::NO_ERROR) return true;
67   AddPayloadOctets2(pkt, base::ByteSwap(uid_counter_));
68   AddPayloadOctets4(pkt, base::ByteSwap(num_items_in_folder_));
69   AddPayloadOctets2(pkt, base::ByteSwap((uint16_t)0x006a));  // UTF-8
70   AddPayloadOctets1(pkt, folder_depth_);
71 
72   // Skip adding the folder name if the folder depth is 0
73   if (folder_depth_ == 0) return true;
74   uint16_t folder_name_len = folder_name_.size();
75   AddPayloadOctets2(pkt, base::ByteSwap(folder_name_len));
76   for (auto it = folder_name_.begin(); it != folder_name_.end(); it++) {
77     AddPayloadOctets1(pkt, *it);
78   }
79 
80   return true;
81 }
82 
GetPlayerId() const83 uint16_t SetBrowsedPlayerRequest::GetPlayerId() const {
84   auto it = begin() + BrowsePacket::kMinSize();
85   return it.extractBE<uint16_t>();
86 }
87 
IsValid() const88 bool SetBrowsedPlayerRequest::IsValid() const {
89   if (!BrowsePacket::IsValid()) return false;
90   return size() == kMinSize();
91 }
92 
ToString() const93 std::string SetBrowsedPlayerRequest::ToString() const {
94   std::stringstream ss;
95   ss << "SetBrowsedPlayerRequestPacket: " << std::endl;
96   ss << "  └ PDU = " << GetPdu() << std::endl;
97   ss << "  └ Length = " << GetLength() << std::endl;
98   ss << "  └ Player ID = " << loghex(GetPlayerId()) << std::endl;
99   ss << std::endl;
100 
101   return ss.str();
102 }
103 
104 }  // namespace avrcp
105 }  // namespace bluetooth
106