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 "avrcp_browse_packet.h"
18
19 #include <base/logging.h>
20
21 namespace bluetooth {
22 namespace avrcp {
23
MakeBuilder(BrowsePdu pdu,std::unique_ptr<::bluetooth::PacketBuilder> payload)24 std::unique_ptr<BrowsePacketBuilder> BrowsePacketBuilder::MakeBuilder(
25 BrowsePdu pdu, std::unique_ptr<::bluetooth::PacketBuilder> payload) {
26 std::unique_ptr<BrowsePacketBuilder> builder =
27 std::unique_ptr<BrowsePacketBuilder>(new BrowsePacketBuilder(pdu));
28
29 builder->payload_ = std::move(payload);
30
31 return builder;
32 }
33
size() const34 size_t BrowsePacketBuilder::size() const {
35 return BrowsePacket::kMinSize() + payload_->size();
36 }
37
Serialize(const std::shared_ptr<::bluetooth::Packet> & pkt)38 bool BrowsePacketBuilder::Serialize(
39 const std::shared_ptr<::bluetooth::Packet>& pkt) {
40 ReserveSpace(pkt, size());
41
42 PushHeader(pkt, payload_->size());
43
44 return payload_->Serialize(pkt);
45 }
46
PushHeader(const std::shared_ptr<::bluetooth::Packet> & pkt,uint16_t length)47 void BrowsePacketBuilder::PushHeader(
48 const std::shared_ptr<::bluetooth::Packet>& pkt, uint16_t length) {
49 AddPayloadOctets1(pkt, (uint8_t)pdu_);
50 AddPayloadOctets2(pkt, base::ByteSwap(length));
51 }
52
Parse(std::shared_ptr<::bluetooth::Packet> pkt)53 std::shared_ptr<BrowsePacket> BrowsePacket::Parse(
54 std::shared_ptr<::bluetooth::Packet> pkt) {
55 return std::shared_ptr<BrowsePacket>(new BrowsePacket(pkt));
56 }
57
GetPdu() const58 BrowsePdu BrowsePacket::GetPdu() const {
59 return static_cast<BrowsePdu>(*begin());
60 }
61
GetLength() const62 uint16_t BrowsePacket::GetLength() const {
63 auto it = begin() + static_cast<size_t>(1);
64 return it.extractBE<uint16_t>();
65 }
66
IsValid() const67 bool BrowsePacket::IsValid() const {
68 if (size() < kMinSize()) return false;
69 return size() == GetLength() + kMinSize();
70 }
71
ToString() const72 std::string BrowsePacket::ToString() const {
73 std::stringstream ss;
74 ss << "AvrcpBrowsePacket: " << std::endl;
75 ss << " └ PDU = " << GetPdu() << std::endl;
76 ss << " └ Length = " << GetLength() << std::endl;
77 ss << " └ Payload =";
78 for (auto it = begin() + static_cast<size_t>(3); it != end(); it++) {
79 ss << " " << loghex(*it);
80 }
81 ss << std::endl;
82
83 return ss.str();
84 }
85
GetPayloadIndecies() const86 std::pair<size_t, size_t> BrowsePacket::GetPayloadIndecies() const {
87 return std::pair<size_t, size_t>(packet_start_index_ + 3, packet_end_index_);
88 }
89
90 } // namespace avrcp
91 } // namespace bluetooth
92