• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <base/logging.h>
18 
19 #include "register_notification_packet.h"
20 
21 namespace bluetooth {
22 namespace avrcp {
23 
IsInterim() const24 bool RegisterNotificationResponse::IsInterim() const {
25   return GetCType() == CType::INTERIM;
26 }
27 
GetEvent() const28 Event RegisterNotificationResponse::GetEvent() const {
29   auto value = *(begin() + VendorPacket::kMinSize());
30   return static_cast<Event>(value);
31 }
32 
GetVolume() const33 uint8_t RegisterNotificationResponse::GetVolume() const {
34   CHECK(GetEvent() == Event::VOLUME_CHANGED);
35   auto it = begin() + VendorPacket::kMinSize() + static_cast<size_t>(1);
36   return *it;
37 }
38 
IsValid() const39 bool RegisterNotificationResponse::IsValid() const {
40   if (!VendorPacket::IsValid()) return false;
41   if (size() < kMinSize()) return false;
42   if (GetCType() != CType::INTERIM && GetCType() != CType::CHANGED && GetCType() != CType::REJECTED) {
43     return false;
44   }
45 
46   switch (GetEvent()) {
47     case Event::VOLUME_CHANGED:
48       return size() == (kMinSize() + 1);
49     default:
50       // TODO (apanicke): Add the remaining events when implementing AVRCP
51       // Controller
52       return false;
53   }
54 }
55 
ToString() const56 std::string RegisterNotificationResponse::ToString() const {
57   std::stringstream ss;
58   ss << "RegisterNotificationResponse: " << std::endl;
59   ss << "  └ cType = " << GetCType() << std::endl;
60   ss << "  └ Subunit Type = " << loghex(GetSubunitType()) << std::endl;
61   ss << "  └ Subunit ID = " << loghex(GetSubunitId()) << std::endl;
62   ss << "  └ OpCode = " << GetOpcode() << std::endl;
63   ss << "  └ Company ID = " << loghex(GetCompanyId()) << std::endl;
64   ss << "  └ Command PDU = " << GetCommandPdu() << std::endl;
65   ss << "  └ PacketType = " << GetPacketType() << std::endl;
66   ss << "  └ Parameter Length = " << loghex(GetParameterLength()) << std::endl;
67   ss << "  └ Event Registered = " << GetEvent() << std::endl;
68   ss << std::endl;
69 
70   return ss.str();
71 }
72 
73 std::unique_ptr<RegisterNotificationResponseBuilder>
MakePlaybackStatusBuilder(bool interim,uint8_t play_status)74 RegisterNotificationResponseBuilder::MakePlaybackStatusBuilder(
75     bool interim, uint8_t play_status) {
76   std::unique_ptr<RegisterNotificationResponseBuilder> builder(
77       new RegisterNotificationResponseBuilder(interim,
78                                               Event::PLAYBACK_STATUS_CHANGED));
79 
80   builder->data_ = play_status;
81   return builder;
82 }
83 
84 std::unique_ptr<RegisterNotificationResponseBuilder>
MakeTrackChangedBuilder(bool interim,uint64_t track_uid)85 RegisterNotificationResponseBuilder::MakeTrackChangedBuilder(
86     bool interim, uint64_t track_uid) {
87   std::unique_ptr<RegisterNotificationResponseBuilder> builder(
88       new RegisterNotificationResponseBuilder(interim, Event::TRACK_CHANGED));
89 
90   builder->data_ = track_uid;
91   return builder;
92 }
93 
94 std::unique_ptr<RegisterNotificationResponseBuilder>
MakePlaybackPositionBuilder(bool interim,uint32_t playback_pos)95 RegisterNotificationResponseBuilder::MakePlaybackPositionBuilder(
96     bool interim, uint32_t playback_pos) {
97   std::unique_ptr<RegisterNotificationResponseBuilder> builder(
98       new RegisterNotificationResponseBuilder(interim,
99                                               Event::PLAYBACK_POS_CHANGED));
100 
101   builder->data_ = playback_pos;
102   return builder;
103 }
104 
105 std::unique_ptr<RegisterNotificationResponseBuilder>
MakeNowPlayingBuilder(bool interim)106 RegisterNotificationResponseBuilder::MakeNowPlayingBuilder(bool interim) {
107   std::unique_ptr<RegisterNotificationResponseBuilder> builder(
108       new RegisterNotificationResponseBuilder(
109           interim, Event::NOW_PLAYING_CONTENT_CHANGED));
110   return builder;
111 }
112 
113 std::unique_ptr<RegisterNotificationResponseBuilder>
MakeAvailablePlayersBuilder(bool interim)114 RegisterNotificationResponseBuilder::MakeAvailablePlayersBuilder(bool interim) {
115   std::unique_ptr<RegisterNotificationResponseBuilder> builder(
116       new RegisterNotificationResponseBuilder(
117           interim, Event::AVAILABLE_PLAYERS_CHANGED));
118   return builder;
119 }
120 
121 std::unique_ptr<RegisterNotificationResponseBuilder>
MakeAddressedPlayerBuilder(bool interim,uint16_t player_id,uint16_t uid_counter)122 RegisterNotificationResponseBuilder::MakeAddressedPlayerBuilder(
123     bool interim, uint16_t player_id, uint16_t uid_counter) {
124   std::unique_ptr<RegisterNotificationResponseBuilder> builder(
125       new RegisterNotificationResponseBuilder(interim,
126                                               Event::ADDRESSED_PLAYER_CHANGED));
127   builder->data_ = ((uint32_t)player_id) << 16;
128   builder->data_ |= uid_counter;
129   return builder;
130 }
131 
132 std::unique_ptr<RegisterNotificationResponseBuilder>
MakeUidsChangedBuilder(bool interim,uint16_t uid_counter)133 RegisterNotificationResponseBuilder::MakeUidsChangedBuilder(
134     bool interim, uint16_t uid_counter) {
135   std::unique_ptr<RegisterNotificationResponseBuilder> builder(
136       new RegisterNotificationResponseBuilder(interim, Event::UIDS_CHANGED));
137 
138   builder->data_ = uid_counter;
139   return builder;
140 }
141 
size() const142 size_t RegisterNotificationResponseBuilder::size() const {
143   size_t data_size = 0;
144 
145   // We specifically avoid having a default case here in order to ensure that
146   // there is an error in case an event isn't handled.
147   switch (event_) {
148     case Event::PLAYBACK_STATUS_CHANGED:
149       data_size = 1;
150       break;
151     case Event::TRACK_CHANGED:
152       data_size = 8;
153       break;
154     case Event::PLAYBACK_POS_CHANGED:
155       data_size = 4;
156       break;
157     case Event::PLAYER_APPLICATION_SETTING_CHANGED:
158       LOG(FATAL) << "Player Application Notification Not Implemented";
159       break;
160     case Event::NOW_PLAYING_CONTENT_CHANGED:
161       data_size = 0;
162       break;
163     case Event::AVAILABLE_PLAYERS_CHANGED:
164       data_size = 0;
165       break;
166     case Event::ADDRESSED_PLAYER_CHANGED:
167       data_size = 4;
168       break;
169     case Event::UIDS_CHANGED:
170       data_size = 2;
171       break;
172     case Event::VOLUME_CHANGED:
173       LOG(FATAL) << "Volume Changed Notification Not Implemented";
174       break;
175   }
176 
177   return VendorPacket::kMinSize() + 1 + data_size;
178 }
179 
Serialize(const std::shared_ptr<::bluetooth::Packet> & pkt)180 bool RegisterNotificationResponseBuilder::Serialize(
181     const std::shared_ptr<::bluetooth::Packet>& pkt) {
182   ReserveSpace(pkt, size());
183 
184   PacketBuilder::PushHeader(pkt);
185 
186   VendorPacketBuilder::PushHeader(pkt, size() - VendorPacket::kMinSize());
187 
188   AddPayloadOctets1(pkt, static_cast<uint8_t>(event_));
189   switch (event_) {
190     case Event::PLAYBACK_STATUS_CHANGED: {
191       uint8_t playback_status = data_ & 0xFF;
192       AddPayloadOctets1(pkt, playback_status);
193       break;
194     }
195     case Event::TRACK_CHANGED: {
196       AddPayloadOctets8(pkt, base::ByteSwap(data_));
197       break;
198     }
199     case Event::PLAYBACK_POS_CHANGED: {
200       uint32_t playback_pos = data_ & 0xFFFFFFFF;
201       AddPayloadOctets4(pkt, base::ByteSwap(playback_pos));
202       break;
203     }
204     case Event::PLAYER_APPLICATION_SETTING_CHANGED:
205       break;  // No additional data
206     case Event::NOW_PLAYING_CONTENT_CHANGED:
207       break;  // No additional data
208     case Event::AVAILABLE_PLAYERS_CHANGED:
209       break;  // No additional data
210     case Event::ADDRESSED_PLAYER_CHANGED: {
211       uint16_t uid_counter = data_ & 0xFFFF;
212       uint16_t player_id = (data_ >> 16) & 0xFFFF;
213       AddPayloadOctets2(pkt, base::ByteSwap(player_id));
214       AddPayloadOctets2(pkt, base::ByteSwap(uid_counter));
215       break;
216     }
217     case Event::UIDS_CHANGED: {
218       uint16_t uid_counter = data_ & 0xFFFF;
219       AddPayloadOctets2(pkt, base::ByteSwap(uid_counter));
220       break;
221     }
222     case Event::VOLUME_CHANGED:
223       // TODO (apanicke): Add Volume Changed builder for when we are controller.
224       LOG(FATAL) << "Volume Changed Notification Not Implemented";
225       break;
226   }
227 
228   return true;
229 }
230 
GetEventRegistered() const231 Event RegisterNotificationRequest::GetEventRegistered() const {
232   auto value = *(begin() + VendorPacket::kMinSize());
233   return static_cast<Event>(value);
234 }
235 
GetInterval() const236 uint32_t RegisterNotificationRequest::GetInterval() const {
237   auto it = begin() + VendorPacket::kMinSize() + static_cast<size_t>(1);
238   return it.extractBE<uint32_t>();
239 }
240 
IsValid() const241 bool RegisterNotificationRequest::IsValid() const {
242   return (size() == kMinSize());
243 }
244 
ToString() const245 std::string RegisterNotificationRequest::ToString() const {
246   std::stringstream ss;
247   ss << "RegisterNotificationPacket: " << std::endl;
248   ss << "  └ cType = " << GetCType() << std::endl;
249   ss << "  └ Subunit Type = " << loghex(GetSubunitType()) << std::endl;
250   ss << "  └ Subunit ID = " << loghex(GetSubunitId()) << std::endl;
251   ss << "  └ OpCode = " << GetOpcode() << std::endl;
252   ss << "  └ Company ID = " << loghex(GetCompanyId()) << std::endl;
253   ss << "  └ Command PDU = " << GetCommandPdu() << std::endl;
254   ss << "  └ PacketType = " << GetPacketType() << std::endl;
255   ss << "  └ Parameter Length = " << loghex(GetParameterLength()) << std::endl;
256   ss << "  └ Event Registered = " << GetEventRegistered() << std::endl;
257   ss << "  └ Interval = " << loghex(GetInterval()) << std::endl;
258   ss << std::endl;
259 
260   return ss.str();
261 }
262 
263 std::unique_ptr<RegisterNotificationRequestBuilder>
MakeBuilder(Event event,uint32_t interval)264 RegisterNotificationRequestBuilder::MakeBuilder(Event event,
265                                                 uint32_t interval) {
266   std::unique_ptr<RegisterNotificationRequestBuilder> builder(
267       new RegisterNotificationRequestBuilder(event, interval));
268 
269   return builder;
270 }
271 
size() const272 size_t RegisterNotificationRequestBuilder::size() const {
273   return RegisterNotificationRequest::kMinSize();
274 }
275 
Serialize(const std::shared_ptr<::bluetooth::Packet> & pkt)276 bool RegisterNotificationRequestBuilder::Serialize(
277     const std::shared_ptr<::bluetooth::Packet>& pkt) {
278   ReserveSpace(pkt, size());
279 
280   PacketBuilder::PushHeader(pkt);
281 
282   VendorPacketBuilder::PushHeader(pkt, size() - VendorPacket::kMinSize());
283 
284   AddPayloadOctets1(pkt, static_cast<uint8_t>(event_));
285 
286   AddPayloadOctets4(pkt, base::ByteSwap(interval_));
287 
288   return true;
289 }
290 
291 }  // namespace avrcp
292 }  // namespace bluetooth
293