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