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 <iomanip>
18 #include <sstream>
19 #include <type_traits>
20
21 #include "avrcp_packet.h"
22
23 namespace bluetooth {
24 namespace avrcp {
25
MakeBuilder(CType type,uint8_t subunit_type,uint8_t subunit_id,Opcode opcode,std::unique_ptr<::bluetooth::PacketBuilder> payload)26 std::unique_ptr<PacketBuilder> PacketBuilder::MakeBuilder(
27 CType type, uint8_t subunit_type, uint8_t subunit_id, Opcode opcode,
28 std::unique_ptr<::bluetooth::PacketBuilder> payload) {
29 std::unique_ptr<PacketBuilder> builder = std::unique_ptr<PacketBuilder>(
30 new PacketBuilder(type, subunit_type, subunit_id, opcode));
31
32 builder->payload_ = std::move(payload);
33
34 return builder;
35 }
36
size() const37 size_t PacketBuilder::size() const {
38 // The size of the header for an Packet is 3
39 return payload_->size() + Packet::kMinSize();
40 }
41
Serialize(const std::shared_ptr<::bluetooth::Packet> & pkt)42 bool PacketBuilder::Serialize(const std::shared_ptr<::bluetooth::Packet>& pkt) {
43 ReserveSpace(pkt, size());
44
45 // Push the header for the packet
46 PushHeader(pkt);
47
48 // Push the payload for the packet
49 return payload_->Serialize(pkt);
50 }
51
PushHeader(const std::shared_ptr<::bluetooth::Packet> & pkt)52 void PacketBuilder::PushHeader(
53 const std::shared_ptr<::bluetooth::Packet>& pkt) {
54 AddPayloadOctets1(pkt, static_cast<uint8_t>(c_type_));
55 AddPayloadOctets1(pkt, (subunit_type_ << 3) | subunit_id_);
56 AddPayloadOctets1(pkt, static_cast<uint8_t>(opcode_));
57 }
58
PushCompanyId(const std::shared_ptr<::bluetooth::Packet> & pkt,uint32_t company_id)59 bool PacketBuilder::PushCompanyId(
60 const std::shared_ptr<::bluetooth::Packet>& pkt, uint32_t company_id) {
61 company_id = base::ByteSwap(company_id);
62 for (int i = 0; i < 3; i++) {
63 company_id >>= 8;
64 AddPayloadOctets1(pkt, company_id & 0xFF);
65 }
66
67 return true;
68 }
69
Parse(std::shared_ptr<::bluetooth::Packet> pkt)70 std::shared_ptr<Packet> Packet::Parse(
71 std::shared_ptr<::bluetooth::Packet> pkt) {
72 return std::shared_ptr<Packet>(new Packet(pkt));
73 }
74
GetCType() const75 CType Packet::GetCType() const {
76 auto value = *begin() & 0x0F;
77 return static_cast<CType>(value);
78 }
79
GetSubunitType() const80 uint8_t Packet::GetSubunitType() const {
81 return *(begin() + static_cast<size_t>(1)) >> 3;
82 }
83
GetSubunitId() const84 uint8_t Packet::GetSubunitId() const {
85 return *(begin() + static_cast<size_t>(1)) & 0b00000111;
86 }
87
GetOpcode() const88 Opcode Packet::GetOpcode() const {
89 auto value = *(begin() + static_cast<size_t>(2));
90 return static_cast<Opcode>(value);
91 }
92
IsValid() const93 bool Packet::IsValid() const { return size() >= kMinSize(); }
94
ToString() const95 std::string Packet::ToString() const {
96 std::stringstream ss;
97 ss << "avrcp::Packet: " << std::endl;
98 ss << " └ cType = " << GetCType() << std::endl;
99 ss << " └ Subunit Type = " << loghex(GetSubunitType()) << std::endl;
100 ss << " └ Subunit ID = " << loghex(GetSubunitId()) << std::endl;
101 ss << " └ OpCode = " << GetOpcode() << std::endl;
102 ss << " └ Payload =";
103 for (auto it = begin() + static_cast<size_t>(3); it != end(); it++) {
104 ss << " " << loghex(*it);
105 }
106 ss << std::endl;
107
108 return ss.str();
109 }
110
GetPayloadIndecies() const111 std::pair<size_t, size_t> Packet::GetPayloadIndecies() const {
112 return std::pair<size_t, size_t>(packet_start_index_ + 3, packet_end_index_);
113 }
114
115 } // namespace avrcp
116 } // namespace bluetooth