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