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 <set>
20 
21 #include "vendor_packet.h"
22 
23 namespace bluetooth {
24 namespace avrcp {
25 
26 class GetCapabilitiesRequestBuilder : public VendorPacketBuilder {
27  public:
28   virtual ~GetCapabilitiesRequestBuilder() = default;
29 
30   static std::unique_ptr<GetCapabilitiesRequestBuilder> MakeBuilder(
31       Capability capability);
32 
33   virtual size_t size() const override;
34   virtual bool Serialize(
35       const std::shared_ptr<::bluetooth::Packet>& pkt) override;
36 
37  protected:
38   Capability capability_;
39 
GetCapabilitiesRequestBuilder(Capability capability)40   GetCapabilitiesRequestBuilder(Capability capability)
41       : VendorPacketBuilder(CType::STATUS, CommandPdu::GET_CAPABILITIES,
42                             PacketType::SINGLE),
43         capability_(capability){};
44 };
45 
46 class GetCapabilitiesRequest : public VendorPacket {
47  public:
48   virtual ~GetCapabilitiesRequest() = default;
49 
50   /**
51    *  Get Capabilities Response Packet Layout
52    *   AvrcpPacket:
53    *     CType c_type_;
54    *     uint8_t subunit_type_ : 5;
55    *     uint8_t subunit_id_ : 3;
56    *     Opcode opcode_;
57    *   VendorPacket:
58    *     uint8_t company_id[3];
59    *     uint8_t command_pdu;
60    *     uint8_t packet_type;
61    *     uint16_t param_length;
62    *   GetCapabilitiesRequestPacket:
63    *     uint8_t capability_requested:
64    */
kMinSize()65   static constexpr size_t kMinSize() { return VendorPacket::kMinSize() + 1; };
66 
67   // Getter Functions
68   Capability GetCapabilityRequested() const;
69 
70   // Overloaded Functions
71   virtual bool IsValid() const override;
72   virtual std::string ToString() const override;
73 
74  protected:
75   using VendorPacket::VendorPacket;
76 };
77 
78 class GetCapabilitiesResponseBuilder : public VendorPacketBuilder {
79  public:
80   virtual ~GetCapabilitiesResponseBuilder() = default;
81 
82   static std::unique_ptr<GetCapabilitiesResponseBuilder> MakeCompanyIdBuilder(
83       uint32_t company_id_element);
84 
85   static std::unique_ptr<GetCapabilitiesResponseBuilder>
86   MakeEventsSupportedBuilder(Event event);
87 
88   GetCapabilitiesResponseBuilder* AddCompanyId(uint32_t company_id);
89   GetCapabilitiesResponseBuilder* AddEvent(Event event);
90 
91   virtual size_t size() const override;
92   virtual bool Serialize(
93       const std::shared_ptr<::bluetooth::Packet>& pkt) override;
94 
95  private:
96   Capability capability_;
97   std::set<uint32_t> elements_;
98 
GetCapabilitiesResponseBuilder(Capability capability)99   GetCapabilitiesResponseBuilder(Capability capability)
100       : VendorPacketBuilder(CType::STABLE, CommandPdu::GET_CAPABILITIES,
101                             PacketType::SINGLE),
102         capability_(capability){};
103 };
104 
105 class GetCapabilitiesResponse : public VendorPacket {
106  public:
107   /**
108    * Get Capabilities Response Packet Layout
109    *   AvrcpPacket:
110    *     CType c_type_;
111    *     uint8_t subunit_type_ : 5;
112    *     uint8_t subunit_id_ : 3;
113    *     Opcode opcode_;
114    *   VendorPacket:
115    *     uint8_t company_id[3];
116    *     uint8_t command_pdu;
117    *     uint8_t packet_type;
118    *     uint16_t param_length;
119    *   GetCapabilitiesRequestPacket;
120    *     uint8_t capability_requested;
121    *     uint16_t capability_count;
122    *     union {
123    *        uint8_t event_supported;
124    *        uint8_t company_id[3];
125    *     } capability_array[];
126    */
kMinSize()127   static constexpr size_t kMinSize() { return VendorPacket::kMinSize() + 2; };
128 
129   // TODO: Implement these for AVRCP Controller
130   // virtual uint8_t GetCapabilityReturned() const;
131   // virtual uint8_t GetCapabilityCount() const;
132   // virtual std::vector<Event> GetEventsSupported() const;
133   // virtual std::vector<uint32_t> GetCompanyIds() const;
134 
135   virtual std::string ToString() const override;
136 };
137 
138 }  // namespace avrcp
139 }  // namespace bluetooth