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 <base/sys_byteorder.h>
20 
21 #include "internal_include/bt_trace.h"
22 
23 namespace bluetooth {
24 namespace avrcp {
25 
MakeBuilder(Status status,uint16_t uid_counter,uint32_t num_items_in_folder,uint8_t folder_depth,std::string folder_name)26 std::unique_ptr<SetBrowsedPlayerResponseBuilder> SetBrowsedPlayerResponseBuilder::MakeBuilder(
27         Status status, uint16_t uid_counter, uint32_t num_items_in_folder, uint8_t folder_depth,
28         std::string folder_name) {
29   std::unique_ptr<SetBrowsedPlayerResponseBuilder> builder(new SetBrowsedPlayerResponseBuilder(
30           status, uid_counter, num_items_in_folder, folder_depth, folder_name));
31 
32   return builder;
33 }
34 
size() const35 size_t SetBrowsedPlayerResponseBuilder::size() const {
36   size_t len = BrowsePacket::kMinSize();
37   len += 1;  // Status
38 
39   // If the status isn't success the rest of the fields are ommited
40   if (status_ != Status::NO_ERROR) {
41     return len;
42   }
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(const std::shared_ptr<::bluetooth::Packet>& pkt) {
59   ReserveSpace(pkt, size());
60 
61   BrowsePacketBuilder::PushHeader(pkt, size() - BrowsePacket::kMinSize());
62 
63   AddPayloadOctets1(pkt, (uint8_t)status_);
64 
65   if (status_ != Status::NO_ERROR) {
66     return true;
67   }
68   AddPayloadOctets2(pkt, base::ByteSwap(uid_counter_));
69   AddPayloadOctets4(pkt, base::ByteSwap(num_items_in_folder_));
70   AddPayloadOctets2(pkt, base::ByteSwap((uint16_t)0x006a));  // UTF-8
71   AddPayloadOctets1(pkt, folder_depth_);
72 
73   // Skip adding the folder name if the folder depth is 0
74   if (folder_depth_ == 0) {
75     return true;
76   }
77   uint16_t folder_name_len = folder_name_.size();
78   AddPayloadOctets2(pkt, base::ByteSwap(folder_name_len));
79   for (auto it = folder_name_.begin(); it != folder_name_.end(); it++) {
80     AddPayloadOctets1(pkt, *it);
81   }
82 
83   return true;
84 }
85 
GetPlayerId() const86 uint16_t SetBrowsedPlayerRequest::GetPlayerId() const {
87   auto it = begin() + BrowsePacket::kMinSize();
88   return it.extractBE<uint16_t>();
89 }
90 
IsValid() const91 bool SetBrowsedPlayerRequest::IsValid() const {
92   if (!BrowsePacket::IsValid()) {
93     return false;
94   }
95   return size() == kMinSize();
96 }
97 
ToString() const98 std::string SetBrowsedPlayerRequest::ToString() const {
99   std::stringstream ss;
100   ss << "SetBrowsedPlayerRequestPacket: " << std::endl;
101   ss << "  └ PDU = " << GetPdu() << std::endl;
102   ss << "  └ Length = " << GetLength() << std::endl;
103   ss << "  └ Player ID = " << loghex(GetPlayerId()) << std::endl;
104   ss << std::endl;
105 
106   return ss.str();
107 }
108 
109 }  // namespace avrcp
110 }  // namespace bluetooth
111