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 "avrcp_packet.h"
20 
21 namespace bluetooth {
22 namespace avrcp {
23 
24 class VendorPacketBuilder : public avrcp::PacketBuilder {
25  public:
26   virtual ~VendorPacketBuilder() = default;
27 
28   static std::unique_ptr<VendorPacketBuilder> MakeBuilder(
29       CType ctype, CommandPdu pdu, PacketType packet_type,
30       std::unique_ptr<::bluetooth::PacketBuilder> payload);
31 
32   virtual size_t size() const override;
33   virtual bool Serialize(
34       const std::shared_ptr<::bluetooth::Packet>& pkt) override;
35 
36  protected:
37   CommandPdu pdu_;
38   PacketType packet_type_;
39   uint16_t param_length_;
40 
41   void PushHeader(const std::shared_ptr<::bluetooth::Packet>& pkt,
42                   uint16_t parameter_length);
43 
44   // Helper function used a couple other AVRCP packet builders
45   bool PushAttributeValue(const std::shared_ptr<::bluetooth::Packet>& pkt,
46                           const AttributeEntry& entry);
47 
VendorPacketBuilder(CType ctype,CommandPdu pdu,PacketType packet_type)48   VendorPacketBuilder(CType ctype, CommandPdu pdu, PacketType packet_type)
49       : PacketBuilder(ctype, 0x09, 0x00, Opcode::VENDOR),
50         pdu_(pdu),
51         packet_type_(packet_type){};
52 };
53 
54 class VendorPacket : public avrcp::Packet {
55  public:
56   virtual ~VendorPacket() = default;
57 
58   /**
59    * Avrcp Vendor Packet Layout
60    *   AvrcpPacket:
61    *     CType c_type_;
62    *     uint8_t subunit_type_ : 5;
63    *     uint8_t subunit_id_ : 3;
64    *     Opcode opcode_;
65    *   VendorPacket:
66    *     uint8_t company_id[3];
67    *     uint8_t command_pdu;
68    *     uint8_t packet_type;
69    *     uint16_t parameter_length;
70    *   uint8_t[] payload;
71    */
kMinSize()72   static constexpr size_t kMinSize() { return Packet::kMinSize() + 7; };
73 
74   // Getter Functions
75   uint32_t GetCompanyId() const;
76   CommandPdu GetCommandPdu() const;
77   PacketType GetPacketType() const;
78   uint16_t GetParameterLength() const;
79 
80   // Overloaded functions
81   virtual bool IsValid() const override;
82   virtual std::string ToString() const override;
83 
84  protected:
85   using Packet::Packet;
86 };
87 
88 }  // namespace avrcp
89 }  // namespace bluetooth