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> MakePlaybackStatusBuilder(
63           bool interim, uint8_t play_status);
64 
65   static std::unique_ptr<RegisterNotificationResponseBuilder> MakeTrackChangedBuilder(
66           bool interim, uint64_t track_uid);
67 
68   static std::unique_ptr<RegisterNotificationResponseBuilder> MakePlaybackPositionBuilder(
69           bool interim, uint32_t playback_pos);
70 
71   static std::unique_ptr<RegisterNotificationResponseBuilder> MakePlayerSettingChangedBuilder(
72           bool interim, std::vector<PlayerAttribute> attributes, std::vector<uint8_t> values);
73 
74   static std::unique_ptr<RegisterNotificationResponseBuilder> MakeNowPlayingBuilder(bool interim);
75 
76   static std::unique_ptr<RegisterNotificationResponseBuilder> MakeAvailablePlayersBuilder(
77           bool interim);
78 
79   static std::unique_ptr<RegisterNotificationResponseBuilder> MakeAddressedPlayerBuilder(
80           bool interim, uint16_t player_id, uint16_t uid_counter);
81 
82   static std::unique_ptr<RegisterNotificationResponseBuilder> MakeUidsChangedBuilder(
83           bool interim, uint16_t uid_counter);
84 
85   virtual size_t size() const override;
86   virtual bool Serialize(const std::shared_ptr<::bluetooth::Packet>& pkt) override;
87 
88 protected:
89   Event event_;
90   union {
91     uint8_t play_status;
92     uint64_t track_uid;
93     uint32_t playback_pos;
94     struct {
95       uint8_t number_of_attributes;
96       PlayerAttribute attributes[4];
97       uint8_t values[4];
98     } player_settings;
99     struct {
100       uint16_t player_id;
101       uint16_t uid_counter;
102     } addressed_player;
103     uint16_t uid_counter;
104   } data_union_;
105 
RegisterNotificationResponseBuilder(bool interim,Event event)106   RegisterNotificationResponseBuilder(bool interim, Event event)
107       : VendorPacketBuilder(interim ? CType::INTERIM : CType::CHANGED,
108                             CommandPdu::REGISTER_NOTIFICATION, PacketType::SINGLE),
109         event_(event) {}
110 };
111 
112 class RegisterNotificationRequest : public VendorPacket {
113 public:
114   virtual ~RegisterNotificationRequest() = default;
115 
116   /**
117    *  Register Notificaiton Request Packet Layout
118    *   AvrcpPacket:
119    *     CType c_type_;
120    *     uint8_t subunit_type_ : 5;
121    *     uint8_t subunit_id_ : 3;
122    *     Opcode opcode_;
123    *   VendorPacket:
124    *     uint8_t company_id[3];
125    *     uint8_t command_pdu;
126    *     uint8_t packet_type;
127    *     uint16_t param_length;
128    *   RegisterNotificationRequestPacket:
129    *     uint8_t event_id;
130    *     uint32_t interval;  // Only used for PLAYBACK_POS_CHANGED
131    */
kMinSize()132   static constexpr size_t kMinSize() { return VendorPacket::kMinSize() + 5; }
133 
134   // Getter Functions
135   Event GetEventRegistered() const;
136   uint32_t GetInterval() const;
137 
138   // Overloaded Functions
139   virtual bool IsValid() const override;
140   virtual std::string ToString() const override;
141 
142 protected:
143   using VendorPacket::VendorPacket;
144 };
145 
146 class RegisterNotificationRequestBuilder : public VendorPacketBuilder {
147 public:
148   virtual ~RegisterNotificationRequestBuilder() = default;
149 
150   static std::unique_ptr<RegisterNotificationRequestBuilder> MakeBuilder(Event event,
151                                                                          uint32_t interval);
152 
153   virtual size_t size() const override;
154   virtual bool Serialize(const std::shared_ptr<::bluetooth::Packet>& pkt) override;
155 
156 protected:
157   Event event_;
158   uint32_t interval_;
159 
RegisterNotificationRequestBuilder(Event event,uint32_t interval)160   RegisterNotificationRequestBuilder(Event event, uint32_t interval)
161       : VendorPacketBuilder(CType::NOTIFY, CommandPdu::REGISTER_NOTIFICATION, PacketType::SINGLE),
162         event_(event),
163         interval_(interval) {}
164 };
165 
166 }  // namespace avrcp
167 }  // namespace bluetooth
168