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