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 <bluetooth/log.h>
20 
21 #include <iomanip>
22 #include <iostream>
23 #include <sstream>
24 #include <string>
25 #include <type_traits>
26 
27 #include "avrcp_common.h"
28 #include "internal_include/bt_trace.h"
29 #include "macros.h"
30 
31 namespace bluetooth {
32 namespace avrcp {
33 
CTypeText(const CType & type)34 inline std::string CTypeText(const CType& type) {
35   switch (type) {
36     CASE_RETURN_TEXT(CType::CONTROL);
37     CASE_RETURN_TEXT(CType::STATUS);
38     CASE_RETURN_TEXT(CType::NOTIFY);
39     CASE_RETURN_TEXT(CType::ACCEPTED);
40     CASE_RETURN_TEXT(CType::REJECTED);
41     CASE_RETURN_TEXT(CType::STABLE);
42     CASE_RETURN_TEXT(CType::CHANGED);
43     CASE_RETURN_TEXT(CType::INTERIM);
44     default:
45       return "Unknown CType: " + loghex((uint8_t)type);
46   }
47 }
48 
49 inline std::ostream& operator<<(std::ostream& os, const CType& type) {
50   return os << CTypeText(type);
51 }
52 
OpcodeText(const Opcode & opcode)53 inline std::string OpcodeText(const Opcode& opcode) {
54   switch (opcode) {
55     CASE_RETURN_TEXT(Opcode::VENDOR);
56     CASE_RETURN_TEXT(Opcode::UNIT_INFO);
57     CASE_RETURN_TEXT(Opcode::SUBUNIT_INFO);
58     CASE_RETURN_TEXT(Opcode::PASS_THROUGH);
59     default:
60       return "Unknown Opcode: " + loghex((uint8_t)opcode);
61   }
62 }
63 
64 inline std::ostream& operator<<(std::ostream& os, const Opcode& opcode) {
65   return os << OpcodeText(opcode);
66 }
67 
CommandPduText(const CommandPdu & pdu)68 inline std::string CommandPduText(const CommandPdu& pdu) {
69   switch (pdu) {
70     CASE_RETURN_TEXT(CommandPdu::GET_CAPABILITIES);
71     CASE_RETURN_TEXT(CommandPdu::LIST_PLAYER_APPLICATION_SETTING_ATTRIBUTES);
72     CASE_RETURN_TEXT(CommandPdu::LIST_PLAYER_APPLICATION_SETTING_VALUES);
73     CASE_RETURN_TEXT(CommandPdu::GET_CURRENT_PLAYER_APPLICATION_SETTING_VALUE);
74     CASE_RETURN_TEXT(CommandPdu::SET_PLAYER_APPLICATION_SETTING_VALUE);
75     CASE_RETURN_TEXT(CommandPdu::GET_ELEMENT_ATTRIBUTES);
76     CASE_RETURN_TEXT(CommandPdu::GET_PLAY_STATUS);
77     CASE_RETURN_TEXT(CommandPdu::REGISTER_NOTIFICATION);
78     CASE_RETURN_TEXT(CommandPdu::SET_ABSOLUTE_VOLUME);
79     CASE_RETURN_TEXT(CommandPdu::SET_ADDRESSED_PLAYER);
80     CASE_RETURN_TEXT(CommandPdu::PLAY_ITEM);
81     default:
82       return "Unknown Command PDU: " + loghex((uint8_t)pdu);
83   }
84 }
85 
86 inline std::ostream& operator<<(std::ostream& os, const CommandPdu& pdu) {
87   return os << CommandPduText(pdu);
88 }
89 
PacketTypeText(const PacketType & type)90 inline std::string PacketTypeText(const PacketType& type) {
91   switch (type) {
92     CASE_RETURN_TEXT(PacketType::SINGLE);
93     default:
94       return "Unknown Packet Type: " + loghex((uint8_t)type);
95   }
96 }
97 
98 inline std::ostream& operator<<(std::ostream& os, const PacketType& type) {
99   return os << PacketTypeText(type);
100 }
101 
CapabilityText(const Capability & cap)102 inline std::string CapabilityText(const Capability& cap) {
103   switch (cap) {
104     CASE_RETURN_TEXT(Capability::COMPANY_ID);
105     CASE_RETURN_TEXT(Capability::EVENTS_SUPPORTED);
106     default:
107       return "Unknown Capability: " + loghex((uint8_t)cap);
108   }
109 }
110 
111 inline std::ostream& operator<<(std::ostream& os, const Capability& cap) {
112   return os << CapabilityText(cap);
113 }
114 
EventText(const Event & event)115 inline std::string EventText(const Event& event) {
116   switch (event) {
117     CASE_RETURN_TEXT(Event::PLAYBACK_STATUS_CHANGED);
118     CASE_RETURN_TEXT(Event::TRACK_CHANGED);
119     CASE_RETURN_TEXT(Event::PLAYBACK_POS_CHANGED);
120     CASE_RETURN_TEXT(Event::PLAYER_APPLICATION_SETTING_CHANGED);
121     CASE_RETURN_TEXT(Event::NOW_PLAYING_CONTENT_CHANGED);
122     CASE_RETURN_TEXT(Event::AVAILABLE_PLAYERS_CHANGED);
123     CASE_RETURN_TEXT(Event::ADDRESSED_PLAYER_CHANGED);
124     CASE_RETURN_TEXT(Event::UIDS_CHANGED);
125     CASE_RETURN_TEXT(Event::VOLUME_CHANGED);
126     default:
127       return "Unknown Event: " + loghex((uint8_t)event);
128   }
129 }
130 
131 inline std::ostream& operator<<(std::ostream& os, const Event& event) {
132   return os << EventText(event);
133 }
134 
AttributeText(const Attribute & attr)135 inline std::string AttributeText(const Attribute& attr) {
136   switch (attr) {
137     CASE_RETURN_TEXT(Attribute::TITLE);
138     CASE_RETURN_TEXT(Attribute::ARTIST_NAME);
139     CASE_RETURN_TEXT(Attribute::ALBUM_NAME);
140     CASE_RETURN_TEXT(Attribute::TRACK_NUMBER);
141     CASE_RETURN_TEXT(Attribute::TOTAL_NUMBER_OF_TRACKS);
142     CASE_RETURN_TEXT(Attribute::GENRE);
143     CASE_RETURN_TEXT(Attribute::PLAYING_TIME);
144     CASE_RETURN_TEXT(Attribute::DEFAULT_COVER_ART);
145     default:
146       return "Unknown Attribute Value: " + loghex((uint32_t)attr);
147   }
148 }
149 
150 inline std::ostream& operator<<(std::ostream& os, const Attribute& attr) {
151   return os << AttributeText(attr);
152 }
153 
StatusText(const Status & status)154 inline std::string StatusText(const Status& status) {
155   switch (status) {
156     CASE_RETURN_TEXT(Status::INVALID_COMMAND);
157     CASE_RETURN_TEXT(Status::INVALID_PARAMETER);
158     CASE_RETURN_TEXT(Status::PARAMETER_CONTENT_ERROR);
159     CASE_RETURN_TEXT(Status::INTERNAL_ERROR);
160     CASE_RETURN_TEXT(Status::NO_ERROR);
161     CASE_RETURN_TEXT(Status::UIDS_CHANGED);
162     CASE_RETURN_TEXT(Status::RESERVED);
163     CASE_RETURN_TEXT(Status::INVALID_DIRECTION);
164     CASE_RETURN_TEXT(Status::NOT_A_DIRECTORY);
165     CASE_RETURN_TEXT(Status::DOES_NOT_EXIST);
166     CASE_RETURN_TEXT(Status::INVALID_SCOPE);
167     CASE_RETURN_TEXT(Status::RANGE_OUT_OF_BOUNDS);
168     CASE_RETURN_TEXT(Status::FOLDER_ITEM_NOT_PLAYABLE);
169     CASE_RETURN_TEXT(Status::MEDIA_IN_USE);
170     CASE_RETURN_TEXT(Status::NOW_PLAYING_LIST_FULL);
171     CASE_RETURN_TEXT(Status::SEARCH_NOT_SUPPORTED);
172     CASE_RETURN_TEXT(Status::SEARCH_IN_PROGRESS);
173     CASE_RETURN_TEXT(Status::INVALID_PLAYER_ID);
174     CASE_RETURN_TEXT(Status::PLAYER_NOT_BROWSABLE);
175     CASE_RETURN_TEXT(Status::PLAYER_NOT_ADDRESSED);
176     CASE_RETURN_TEXT(Status::NO_VALID_SEARCH_RESULTS);
177     CASE_RETURN_TEXT(Status::NO_AVAILABLE_PLAYERS);
178     CASE_RETURN_TEXT(Status::ADDRESSED_PLAYER_CHANGED);
179     default:
180       return "Unknown Status: " + loghex((uint8_t)status);
181   }
182 }
183 
184 inline std::ostream& operator<<(std::ostream& os, const Status& status) {
185   return os << StatusText(status);
186 }
187 
BrowsePduText(const BrowsePdu & pdu)188 inline std::string BrowsePduText(const BrowsePdu& pdu) {
189   switch (pdu) {
190     CASE_RETURN_TEXT(BrowsePdu::SET_BROWSED_PLAYER);
191     CASE_RETURN_TEXT(BrowsePdu::GET_FOLDER_ITEMS);
192     CASE_RETURN_TEXT(BrowsePdu::CHANGE_PATH);
193     CASE_RETURN_TEXT(BrowsePdu::GET_ITEM_ATTRIBUTES);
194     default:
195       return "Unknown Browse PDU: " + loghex((uint8_t)pdu);
196   }
197 }
198 
199 inline std::ostream& operator<<(std::ostream& os, const BrowsePdu& pdu) {
200   return os << BrowsePduText(pdu);
201 }
202 
ScopeText(const Scope & scope)203 inline std::string ScopeText(const Scope& scope) {
204   switch (scope) {
205     CASE_RETURN_TEXT(Scope::MEDIA_PLAYER_LIST);
206     CASE_RETURN_TEXT(Scope::VFS);
207     CASE_RETURN_TEXT(Scope::SEARCH);
208     CASE_RETURN_TEXT(Scope::NOW_PLAYING);
209     default:
210       return "Unknown Scope: " + loghex((uint8_t)scope);
211   }
212 }
213 
214 inline std::ostream& operator<<(std::ostream& os, const Scope& pdu) {
215   return os << ScopeText(pdu);
216 }
217 
DirectionText(const Direction & dir)218 inline std::string DirectionText(const Direction& dir) {
219   switch (dir) {
220     CASE_RETURN_TEXT(Direction::UP);
221     CASE_RETURN_TEXT(Direction::DOWN);
222     default:
223       return "Unknown Direction: " + loghex((uint8_t)dir);
224   }
225 }
226 
227 inline std::ostream& operator<<(std::ostream& os, const Direction& dir) {
228   return os << DirectionText(dir);
229 }
230 
KeyStateText(const KeyState & state)231 inline std::string KeyStateText(const KeyState& state) {
232   switch (state) {
233     CASE_RETURN_TEXT(KeyState::PUSHED);
234     CASE_RETURN_TEXT(KeyState::RELEASED);
235     default:
236       return "Unknown KeyState: " + loghex((uint8_t)state);
237   }
238 }
239 
240 inline std::ostream& operator<<(std::ostream& os, const KeyState& dir) {
241   return os << KeyStateText(dir);
242 }
243 
PlayerAttributeText(const PlayerAttribute & attr)244 inline std::string PlayerAttributeText(const PlayerAttribute& attr) {
245   switch (attr) {
246     CASE_RETURN_TEXT(PlayerAttribute::EQUALIZER);
247     CASE_RETURN_TEXT(PlayerAttribute::REPEAT);
248     CASE_RETURN_TEXT(PlayerAttribute::SHUFFLE);
249     CASE_RETURN_TEXT(PlayerAttribute::SCAN);
250   }
251   return "Unknown Player Attribute: " + loghex((uint8_t)attr);
252 }
253 
254 inline std::ostream& operator<<(std::ostream& os, const PlayerAttribute& attr) {
255   return os << PlayerAttributeText(attr);
256 }
257 
PlayerRepeatValueText(const PlayerRepeatValue & val)258 inline std::string PlayerRepeatValueText(const PlayerRepeatValue& val) {
259   switch (val) {
260     CASE_RETURN_TEXT(PlayerRepeatValue::OFF);
261     CASE_RETURN_TEXT(PlayerRepeatValue::SINGLE);
262     CASE_RETURN_TEXT(PlayerRepeatValue::ALL);
263     CASE_RETURN_TEXT(PlayerRepeatValue::GROUP);
264   }
265   return "Unknown Player Repeat Value: " + loghex((uint8_t)val);
266 }
267 
268 inline std::ostream& operator<<(std::ostream& os,
269                                 const PlayerRepeatValue& val) {
270   return os << PlayerRepeatValueText(val);
271 }
272 
PlayerShuffleValueText(const PlayerShuffleValue & val)273 inline std::string PlayerShuffleValueText(const PlayerShuffleValue& val) {
274   switch (val) {
275     CASE_RETURN_TEXT(PlayerShuffleValue::OFF);
276     CASE_RETURN_TEXT(PlayerShuffleValue::ALL);
277     CASE_RETURN_TEXT(PlayerShuffleValue::GROUP);
278   }
279   return "Unknown Player Shuffle Value: " + loghex((uint8_t)val);
280 }
281 
282 inline std::ostream& operator<<(std::ostream& os,
283                                 const PlayerShuffleValue& val) {
284   return os << PlayerShuffleValueText(val);
285 }
286 
287 }  // namespace avrcp
288 }  // namespace bluetooth
289 
290 namespace fmt {
291 template <>
292 struct formatter<bluetooth::avrcp::CType> : ostream_formatter {};
293 template <>
294 struct formatter<bluetooth::avrcp::Opcode> : ostream_formatter {};
295 template <>
296 struct formatter<bluetooth::avrcp::CommandPdu> : ostream_formatter {};
297 template <>
298 struct formatter<bluetooth::avrcp::PacketType> : ostream_formatter {};
299 template <>
300 struct formatter<bluetooth::avrcp::Capability> : ostream_formatter {};
301 template <>
302 struct formatter<bluetooth::avrcp::Event> : ostream_formatter {};
303 template <>
304 struct formatter<bluetooth::avrcp::Attribute> : ostream_formatter {};
305 template <>
306 struct formatter<bluetooth::avrcp::Status> : ostream_formatter {};
307 template <>
308 struct formatter<bluetooth::avrcp::BrowsePdu> : ostream_formatter {};
309 template <>
310 struct formatter<bluetooth::avrcp::Scope> : ostream_formatter {};
311 template <>
312 struct formatter<bluetooth::avrcp::Direction> : ostream_formatter {};
313 template <>
314 struct formatter<bluetooth::avrcp::KeyState> : ostream_formatter {};
315 template <>
316 struct formatter<bluetooth::avrcp::PlayerAttribute> : ostream_formatter {};
317 template <>
318 struct formatter<bluetooth::avrcp::PlayerRepeatValue> : ostream_formatter {};
319 template <>
320 struct formatter<bluetooth::avrcp::PlayerShuffleValue> : ostream_formatter {};
321 }  // namespace fmt
322