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 #pragma once 18 19 #include <base/logging.h> 20 #include <base/macros.h> 21 #include <iostream> 22 23 #include "hardware/avrcp/avrcp_common.h" 24 #include "hardware/avrcp/avrcp_logging_helper.h" 25 #include "packet/base/iterator.h" 26 #include "packet/base/packet.h" 27 #include "packet/base/packet_builder.h" 28 29 namespace bluetooth { 30 namespace avrcp { 31 32 class PacketBuilder : public ::bluetooth::PacketBuilder { 33 public: 34 virtual ~PacketBuilder() = default; 35 36 static std::unique_ptr<PacketBuilder> MakeBuilder( 37 CType cType, uint8_t subunit_type, uint8_t subunit_id, Opcode opcode, 38 std::unique_ptr<::bluetooth::PacketBuilder> packet); 39 40 virtual size_t size() const override; 41 virtual bool Serialize( 42 const std::shared_ptr<::bluetooth::Packet>& pkt) override; 43 44 protected: 45 CType c_type_; 46 uint8_t subunit_type_ : 5; 47 uint8_t subunit_id_ : 3; 48 Opcode opcode_; 49 std::unique_ptr<::bluetooth::PacketBuilder> payload_; 50 51 void PushHeader(const std::shared_ptr<::bluetooth::Packet>& pkt); 52 bool PushCompanyId(const std::shared_ptr<::bluetooth::Packet>& pkt, 53 uint32_t company_id); 54 PacketBuilder(CType type,uint8_t subunit_type,uint8_t subunit_id,Opcode opcode)55 PacketBuilder(CType type, uint8_t subunit_type, uint8_t subunit_id, 56 Opcode opcode) 57 : c_type_(type), 58 subunit_type_(subunit_type), 59 subunit_id_(subunit_id), 60 opcode_(opcode){}; 61 }; 62 63 class Packet : public ::bluetooth::Packet { 64 public: 65 virtual ~Packet() = default; 66 67 // TODO (apanicke): Right now we can use this to build an AvrcpPacket from 68 // another packet type. In the future, we can remove this in favor of 69 // getting an AVRCP Packet directly from an AVCTP Packet 70 static std::shared_ptr<Packet> Parse( 71 std::shared_ptr<::bluetooth::Packet> pkt); 72 73 /** 74 * Avrcp Packet Layout 75 * CType c_type_; 76 * uint8_t subunit_type_ : 5; 77 * uint8_t subunit_id_ : 3; 78 * Opcode opcode_; 79 * uint8_t[] payload_; 80 */ kMinSize()81 static constexpr size_t kMinSize() { return 3; }; 82 83 // Getter Functions 84 CType GetCType() const; 85 uint8_t GetSubunitType() const; 86 uint8_t GetSubunitId() const; 87 Opcode GetOpcode() const; 88 89 // Overloaded Functions 90 virtual bool IsValid() const override; 91 virtual std::string ToString() const override; 92 93 protected: 94 using ::bluetooth::Packet::Packet; 95 PullCompanyId(Iterator it)96 static inline uint32_t PullCompanyId(Iterator it) { 97 uint32_t value = 0; 98 for (int i = 0; i < 3; i++) { 99 value <<= 8; 100 value |= *it++; 101 } 102 return value; 103 } 104 105 private: 106 virtual std::pair<size_t, size_t> GetPayloadIndecies() const override; 107 DISALLOW_COPY_AND_ASSIGN(Packet); 108 }; 109 110 } // namespace avrcp 111 } // namespace bluetooth 112