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 "vendor_packet.h"
20 
21 namespace bluetooth {
22 namespace avrcp {
23 
24 class RegisterNotificationResponse : public VendorPacket {
25  public:
26   virtual ~RegisterNotificationResponse() = default;
27 
28   /**
29    *  Register Notificaiton Request Packet Layout
30    *   AvrcpPacket:
31    *     CType c_type_;
32    *     uint8_t subunit_type_ : 5;
33    *     uint8_t subunit_id_ : 3;
34    *     Opcode opcode_;
35    *   VendorPacket:
36    *     uint8_t company_id[3];
37    *     uint8_t command_pdu;
38    *     uint8_t packet_type;
39    *     uint16_t param_length;
40    *   RegisterNotificationRequestPacket:
41    *     uint8_t event_id;
42    *     uint8_t[] data;  // Length changes based on the event_id
43    */
kMinSize()44   static constexpr size_t kMinSize() { return VendorPacket::kMinSize() + 1; }
45 
46   // TODO (apanicke): Add other getters when implementing AVRCP Controller
47   bool IsInterim() const;
48   Event GetEvent() const;
49   uint8_t GetVolume() const;
50 
51   virtual bool IsValid() const override;
52   virtual std::string ToString() const override;
53 
54  protected:
55   using VendorPacket::VendorPacket;
56 };
57 
58 class RegisterNotificationResponseBuilder : public VendorPacketBuilder {
59  public:
60   virtual ~RegisterNotificationResponseBuilder() = default;
61 
62   static std::unique_ptr<RegisterNotificationResponseBuilder>
63   MakePlaybackStatusBuilder(bool interim, uint8_t play_status);
64 
65   static std::unique_ptr<RegisterNotificationResponseBuilder>
66   MakeTrackChangedBuilder(bool interim, uint64_t track_uid);
67 
68   static std::unique_ptr<RegisterNotificationResponseBuilder>
69   MakePlaybackPositionBuilder(bool interim, uint32_t playback_pos);
70 
71   static std::unique_ptr<RegisterNotificationResponseBuilder>
72   MakePlayerSettingChangedBuilder(bool interim,
73                                   std::vector<PlayerAttribute> attributes,
74                                   std::vector<uint8_t> values);
75 
76   static std::unique_ptr<RegisterNotificationResponseBuilder>
77   MakeNowPlayingBuilder(bool interim);
78 
79   static std::unique_ptr<RegisterNotificationResponseBuilder>
80   MakeAvailablePlayersBuilder(bool interim);
81 
82   static std::unique_ptr<RegisterNotificationResponseBuilder>
83   MakeAddressedPlayerBuilder(bool interim, uint16_t player_id,
84                              uint16_t uid_counter);
85 
86   static std::unique_ptr<RegisterNotificationResponseBuilder>
87   MakeUidsChangedBuilder(bool interim, uint16_t uid_counter);
88 
89   virtual size_t size() const override;
90   virtual bool Serialize(
91       const std::shared_ptr<::bluetooth::Packet>& pkt) override;
92 
93  protected:
94   Event event_;
95   union {
96     uint8_t play_status;
97     uint64_t track_uid;
98     uint32_t playback_pos;
99     struct {
100       uint8_t number_of_attributes;
101       PlayerAttribute attributes[4];
102       uint8_t values[4];
103     } player_settings;
104     struct {
105       uint16_t player_id;
106       uint16_t uid_counter;
107     } addressed_player;
108     uint16_t uid_counter;
109   } data_union_;
110 
RegisterNotificationResponseBuilder(bool interim,Event event)111   RegisterNotificationResponseBuilder(bool interim, Event event)
112       : VendorPacketBuilder(interim ? CType::INTERIM : CType::CHANGED,
113                             CommandPdu::REGISTER_NOTIFICATION,
114                             PacketType::SINGLE),
115         event_(event){};
116 };
117 
118 class RegisterNotificationRequest : public VendorPacket {
119  public:
120   virtual ~RegisterNotificationRequest() = default;
121 
122   /**
123    *  Register Notificaiton Request Packet Layout
124    *   AvrcpPacket:
125    *     CType c_type_;
126    *     uint8_t subunit_type_ : 5;
127    *     uint8_t subunit_id_ : 3;
128    *     Opcode opcode_;
129    *   VendorPacket:
130    *     uint8_t company_id[3];
131    *     uint8_t command_pdu;
132    *     uint8_t packet_type;
133    *     uint16_t param_length;
134    *   RegisterNotificationRequestPacket:
135    *     uint8_t event_id;
136    *     uint32_t interval;  // Only used for PLAYBACK_POS_CHANGED
137    */
kMinSize()138   static constexpr size_t kMinSize() { return VendorPacket::kMinSize() + 5; }
139 
140   // Getter Functions
141   Event GetEventRegistered() const;
142   uint32_t GetInterval() const;
143 
144   // Overloaded Functions
145   virtual bool IsValid() const override;
146   virtual std::string ToString() const override;
147 
148  protected:
149   using VendorPacket::VendorPacket;
150 };
151 
152 class RegisterNotificationRequestBuilder : public VendorPacketBuilder {
153  public:
154   virtual ~RegisterNotificationRequestBuilder() = default;
155 
156   static std::unique_ptr<RegisterNotificationRequestBuilder> MakeBuilder(
157       Event event, uint32_t interval);
158 
159   virtual size_t size() const override;
160   virtual bool Serialize(
161       const std::shared_ptr<::bluetooth::Packet>& pkt) override;
162 
163  protected:
164   Event event_;
165   uint32_t interval_;
166 
RegisterNotificationRequestBuilder(Event event,uint32_t interval)167   RegisterNotificationRequestBuilder(Event event, uint32_t interval)
168       : VendorPacketBuilder(CType::NOTIFY, CommandPdu::REGISTER_NOTIFICATION,
169                             PacketType::SINGLE),
170         event_(event),
171         interval_(interval){};
172 };
173 
174 }  // namespace avrcp
175 }  // namespace bluetooth