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