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