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 <iomanip>
20 #include <iostream>
21 #include <sstream>
22 #include <string>
23 #include <type_traits>
24 
25 #include "avrcp_common.h"
26 #include "bt_trace.h"
27 
28 namespace bluetooth {
29 namespace avrcp {
30 
31 #define CASE_RETURN_TEXT(code) \
32   case code:                   \
33     return #code
34 
CTypeText(const CType & type)35 inline std::string CTypeText(const CType& type) {
36   switch (type) {
37     CASE_RETURN_TEXT(CType::CONTROL);
38     CASE_RETURN_TEXT(CType::STATUS);
39     CASE_RETURN_TEXT(CType::NOTIFY);
40     CASE_RETURN_TEXT(CType::ACCEPTED);
41     CASE_RETURN_TEXT(CType::REJECTED);
42     CASE_RETURN_TEXT(CType::STABLE);
43     CASE_RETURN_TEXT(CType::CHANGED);
44     CASE_RETURN_TEXT(CType::INTERIM);
45     default:
46       return "Unknown CType: " + loghex((uint8_t)type);
47   }
48 }
49 
50 inline std::ostream& operator<<(std::ostream& os, const CType& type) {
51   return os << CTypeText(type);
52 }
53 
OpcodeText(const Opcode & opcode)54 inline std::string OpcodeText(const Opcode& opcode) {
55   switch (opcode) {
56     CASE_RETURN_TEXT(Opcode::VENDOR);
57     CASE_RETURN_TEXT(Opcode::UNIT_INFO);
58     CASE_RETURN_TEXT(Opcode::SUBUNIT_INFO);
59     CASE_RETURN_TEXT(Opcode::PASS_THROUGH);
60     default:
61       return "Unknown Opcode: " + loghex((uint8_t)opcode);
62   }
63 }
64 
65 inline std::ostream& operator<<(std::ostream& os, const Opcode& opcode) {
66   return os << OpcodeText(opcode);
67 }
68 
CommandPduText(const CommandPdu & pdu)69 inline std::string CommandPduText(const CommandPdu& pdu) {
70   switch (pdu) {
71     CASE_RETURN_TEXT(CommandPdu::GET_CAPABILITIES);
72     CASE_RETURN_TEXT(CommandPdu::LIST_APPLICATION_SETTING_ATTRIBUTES);
73     CASE_RETURN_TEXT(CommandPdu::GET_ELEMENT_ATTRIBUTES);
74     CASE_RETURN_TEXT(CommandPdu::GET_PLAY_STATUS);
75     CASE_RETURN_TEXT(CommandPdu::REGISTER_NOTIFICATION);
76     CASE_RETURN_TEXT(CommandPdu::SET_ABSOLUTE_VOLUME);
77     CASE_RETURN_TEXT(CommandPdu::SET_ADDRESSED_PLAYER);
78     CASE_RETURN_TEXT(CommandPdu::PLAY_ITEM);
79     default:
80       return "Unknown Command PDU: " + loghex((uint8_t)pdu);
81   }
82 }
83 
84 inline std::ostream& operator<<(std::ostream& os, const CommandPdu& pdu) {
85   return os << CommandPduText(pdu);
86 }
87 
PacketTypeText(const PacketType & type)88 inline std::string PacketTypeText(const PacketType& type) {
89   switch (type) {
90     CASE_RETURN_TEXT(PacketType::SINGLE);
91     default:
92       return "Unknown Packet Type: " + loghex((uint8_t)type);
93   }
94 }
95 
96 inline std::ostream& operator<<(std::ostream& os, const PacketType& type) {
97   return os << PacketTypeText(type);
98 }
99 
CapabilityText(const Capability & cap)100 inline std::string CapabilityText(const Capability& cap) {
101   switch (cap) {
102     CASE_RETURN_TEXT(Capability::COMPANY_ID);
103     CASE_RETURN_TEXT(Capability::EVENTS_SUPPORTED);
104     default:
105       return "Unknown Capability: " + loghex((uint8_t)cap);
106   }
107 }
108 
109 inline std::ostream& operator<<(std::ostream& os, const Capability& cap) {
110   return os << CapabilityText(cap);
111 }
112 
EventText(const Event & event)113 inline std::string EventText(const Event& event) {
114   switch (event) {
115     CASE_RETURN_TEXT(Event::PLAYBACK_STATUS_CHANGED);
116     CASE_RETURN_TEXT(Event::TRACK_CHANGED);
117     CASE_RETURN_TEXT(Event::PLAYBACK_POS_CHANGED);
118     CASE_RETURN_TEXT(Event::PLAYER_APPLICATION_SETTING_CHANGED);
119     CASE_RETURN_TEXT(Event::NOW_PLAYING_CONTENT_CHANGED);
120     CASE_RETURN_TEXT(Event::AVAILABLE_PLAYERS_CHANGED);
121     CASE_RETURN_TEXT(Event::ADDRESSED_PLAYER_CHANGED);
122     CASE_RETURN_TEXT(Event::UIDS_CHANGED);
123     CASE_RETURN_TEXT(Event::VOLUME_CHANGED);
124     default:
125       return "Unknown Event: " + loghex((uint8_t)event);
126   }
127 }
128 
129 inline std::ostream& operator<<(std::ostream& os, const Event& event) {
130   return os << EventText(event);
131 }
132 
AttributeText(const Attribute & attr)133 inline std::string AttributeText(const Attribute& attr) {
134   switch (attr) {
135     CASE_RETURN_TEXT(Attribute::TITLE);
136     CASE_RETURN_TEXT(Attribute::ARTIST_NAME);
137     CASE_RETURN_TEXT(Attribute::ALBUM_NAME);
138     CASE_RETURN_TEXT(Attribute::TRACK_NUMBER);
139     CASE_RETURN_TEXT(Attribute::TOTAL_NUMBER_OF_TRACKS);
140     CASE_RETURN_TEXT(Attribute::GENRE);
141     CASE_RETURN_TEXT(Attribute::PLAYING_TIME);
142     CASE_RETURN_TEXT(Attribute::DEFAULT_COVER_ART);
143     default:
144       return "Unknown Attribute Value: " + loghex((uint32_t)attr);
145   }
146 }
147 
148 inline std::ostream& operator<<(std::ostream& os, const Attribute& attr) {
149   return os << AttributeText(attr);
150 }
151 
StatusText(const Status & status)152 inline std::string StatusText(const Status& status) {
153   switch (status) {
154     CASE_RETURN_TEXT(Status::INVALID_COMMAND);
155     CASE_RETURN_TEXT(Status::INVALID_PARAMETER);
156     CASE_RETURN_TEXT(Status::PARAMETER_CONTENT_ERROR);
157     CASE_RETURN_TEXT(Status::INTERNAL_ERROR);
158     CASE_RETURN_TEXT(Status::NO_ERROR);
159     CASE_RETURN_TEXT(Status::UIDS_CHANGED);
160     CASE_RETURN_TEXT(Status::RESERVED);
161     CASE_RETURN_TEXT(Status::INVALID_DIRECTION);
162     CASE_RETURN_TEXT(Status::NOT_A_DIRECTORY);
163     CASE_RETURN_TEXT(Status::DOES_NOT_EXIST);
164     CASE_RETURN_TEXT(Status::INVALID_SCOPE);
165     CASE_RETURN_TEXT(Status::RANGE_OUT_OF_BOUNDS);
166     CASE_RETURN_TEXT(Status::FOLDER_ITEM_NOT_PLAYABLE);
167     CASE_RETURN_TEXT(Status::MEDIA_IN_USE);
168     CASE_RETURN_TEXT(Status::NOW_PLAYING_LIST_FULL);
169     CASE_RETURN_TEXT(Status::SEARCH_NOT_SUPPORTED);
170     CASE_RETURN_TEXT(Status::SEARCH_IN_PROGRESS);
171     CASE_RETURN_TEXT(Status::INVALID_PLAYER_ID);
172     CASE_RETURN_TEXT(Status::PLAYER_NOT_BROWSABLE);
173     CASE_RETURN_TEXT(Status::PLAYER_NOT_ADDRESSED);
174     CASE_RETURN_TEXT(Status::NO_VALID_SEARCH_RESULTS);
175     CASE_RETURN_TEXT(Status::NO_AVAILABLE_PLAYERS);
176     CASE_RETURN_TEXT(Status::ADDRESSED_PLAYER_CHANGED);
177     default:
178       return "Unknown Status: " + loghex((uint8_t)status);
179   }
180 }
181 
182 inline std::ostream& operator<<(std::ostream& os, const Status& status) {
183   return os << StatusText(status);
184 }
185 
BrowsePduText(const BrowsePdu & pdu)186 inline std::string BrowsePduText(const BrowsePdu& pdu) {
187   switch (pdu) {
188     CASE_RETURN_TEXT(BrowsePdu::SET_BROWSED_PLAYER);
189     CASE_RETURN_TEXT(BrowsePdu::GET_FOLDER_ITEMS);
190     CASE_RETURN_TEXT(BrowsePdu::CHANGE_PATH);
191     CASE_RETURN_TEXT(BrowsePdu::GET_ITEM_ATTRIBUTES);
192     default:
193       return "Unknown Browse PDU: " + loghex((uint8_t)pdu);
194   }
195 }
196 
197 inline std::ostream& operator<<(std::ostream& os, const BrowsePdu& pdu) {
198   return os << BrowsePduText(pdu);
199 }
200 
ScopeText(const Scope & scope)201 inline std::string ScopeText(const Scope& scope) {
202   switch (scope) {
203     CASE_RETURN_TEXT(Scope::MEDIA_PLAYER_LIST);
204     CASE_RETURN_TEXT(Scope::VFS);
205     CASE_RETURN_TEXT(Scope::SEARCH);
206     CASE_RETURN_TEXT(Scope::NOW_PLAYING);
207     default:
208       return "Unknown Scope: " + loghex((uint8_t)scope);
209   }
210 }
211 
212 inline std::ostream& operator<<(std::ostream& os, const Scope& pdu) {
213   return os << ScopeText(pdu);
214 }
215 
DirectionText(const Direction & dir)216 inline std::string DirectionText(const Direction& dir) {
217   switch (dir) {
218     CASE_RETURN_TEXT(Direction::UP);
219     CASE_RETURN_TEXT(Direction::DOWN);
220     default:
221       return "Unknown Direction: " + loghex((uint8_t)dir);
222   }
223 }
224 
225 inline std::ostream& operator<<(std::ostream& os, const Direction& dir) {
226   return os << DirectionText(dir);
227 }
228 
KeyStateText(const KeyState & state)229 inline std::string KeyStateText(const KeyState& state) {
230   switch (state) {
231     CASE_RETURN_TEXT(KeyState::PUSHED);
232     CASE_RETURN_TEXT(KeyState::RELEASED);
233     default:
234       return "Unknown KeyState: " + loghex((uint8_t)state);
235   }
236 }
237 
238 inline std::ostream& operator<<(std::ostream& os, const KeyState& dir) {
239   return os << KeyStateText(dir);
240 }
241 
242 }  // namespace avrcp
243 }  // namespace bluetooth
244