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