1 /*
2  * Copyright (C) 2017 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 package com.android.server.usb.descriptors.report;
17 
18 import com.android.server.usb.descriptors.UsbACInterface;
19 import com.android.server.usb.descriptors.UsbASFormat;
20 import com.android.server.usb.descriptors.UsbDescriptor;
21 import com.android.server.usb.descriptors.UsbTerminalTypes;
22 
23 import java.util.HashMap;
24 
25 /**
26  * @hide
27  * A class to provide human-readable strings for various USB constants.
28  */
29 public final class UsbStrings {
30     private static final String TAG = "UsbStrings";
31 
32     private static HashMap<Byte, String> sDescriptorNames;
33     private static HashMap<Byte, String> sACControlInterfaceNames;
34     private static HashMap<Byte, String> sACStreamingInterfaceNames;
35     private static HashMap<Integer, String> sClassNames;
36     private static HashMap<Integer, String> sAudioSubclassNames;
37     private static HashMap<Integer, String> sAudioEncodingNames;
38     private static HashMap<Integer, String> sTerminalNames;
39     private static HashMap<Integer, String> sFormatNames;
40 
41     static {
allocUsbStrings()42         allocUsbStrings();
43     }
44 
initDescriptorNames()45     private static void initDescriptorNames() {
46         sDescriptorNames = new HashMap<Byte, String>();
47         sDescriptorNames.put(UsbDescriptor.DESCRIPTORTYPE_DEVICE, "Device");
48         sDescriptorNames.put(UsbDescriptor.DESCRIPTORTYPE_CONFIG, "Config");
49         sDescriptorNames.put(UsbDescriptor.DESCRIPTORTYPE_STRING, "String");
50         sDescriptorNames.put(UsbDescriptor.DESCRIPTORTYPE_INTERFACE, "Interface");
51         sDescriptorNames.put(UsbDescriptor.DESCRIPTORTYPE_ENDPOINT, "Endpoint");
52         sDescriptorNames.put(UsbDescriptor.DESCRIPTORTYPE_BOS, "BOS (whatever that means)");
53         sDescriptorNames.put(UsbDescriptor.DESCRIPTORTYPE_INTERFACEASSOC,
54                 "Interface Association");
55         sDescriptorNames.put(UsbDescriptor.DESCRIPTORTYPE_CAPABILITY, "Capability");
56         sDescriptorNames.put(UsbDescriptor.DESCRIPTORTYPE_HID, "HID");
57         sDescriptorNames.put(UsbDescriptor.DESCRIPTORTYPE_REPORT, "Report");
58         sDescriptorNames.put(UsbDescriptor.DESCRIPTORTYPE_PHYSICAL, "Physical");
59         sDescriptorNames.put(UsbDescriptor.DESCRIPTORTYPE_CLASSSPECIFIC_INTERFACE,
60                 "Class-specific Interface");
61         sDescriptorNames.put(UsbDescriptor.DESCRIPTORTYPE_CLASSSPECIFIC_ENDPOINT,
62                 "Class-specific Endpoint");
63         sDescriptorNames.put(UsbDescriptor.DESCRIPTORTYPE_HUB, "Hub");
64         sDescriptorNames.put(UsbDescriptor.DESCRIPTORTYPE_SUPERSPEED_HUB, "Superspeed Hub");
65         sDescriptorNames.put(UsbDescriptor.DESCRIPTORTYPE_ENDPOINT_COMPANION,
66                 "Endpoint Companion");
67     }
68 
initACControlInterfaceNames()69     private static void initACControlInterfaceNames() {
70         sACControlInterfaceNames = new HashMap<Byte, String>();
71         sACControlInterfaceNames.put(UsbACInterface.ACI_UNDEFINED, "Undefined");
72         sACControlInterfaceNames.put(UsbACInterface.ACI_HEADER, "Header");
73         sACControlInterfaceNames.put(UsbACInterface.ACI_INPUT_TERMINAL, "Input Terminal");
74         sACControlInterfaceNames.put(UsbACInterface.ACI_OUTPUT_TERMINAL, "Output Terminal");
75         sACControlInterfaceNames.put(UsbACInterface.ACI_MIXER_UNIT, "Mixer Unit");
76         sACControlInterfaceNames.put(UsbACInterface.ACI_SELECTOR_UNIT, "Selector Unit");
77         sACControlInterfaceNames.put(UsbACInterface.ACI_FEATURE_UNIT, "Feature Unit");
78         sACControlInterfaceNames.put(UsbACInterface.ACI_PROCESSING_UNIT, "Processing Unit");
79         sACControlInterfaceNames.put(UsbACInterface.ACI_EXTENSION_UNIT, "Extension Unit");
80         sACControlInterfaceNames.put(UsbACInterface.ACI_CLOCK_SOURCE, "Clock Source");
81         sACControlInterfaceNames.put(UsbACInterface.ACI_CLOCK_SELECTOR, "Clock Selector");
82         sACControlInterfaceNames.put(UsbACInterface.ACI_CLOCK_MULTIPLIER, "Clock Multiplier");
83         sACControlInterfaceNames.put(UsbACInterface.ACI_SAMPLE_RATE_CONVERTER,
84                 "Sample Rate Converter");
85     }
86 
initACStreamingInterfaceNames()87     private static void initACStreamingInterfaceNames() {
88         sACStreamingInterfaceNames = new HashMap<Byte, String>();
89         sACStreamingInterfaceNames.put(UsbACInterface.ASI_UNDEFINED, "Undefined");
90         sACStreamingInterfaceNames.put(UsbACInterface.ASI_GENERAL, "General");
91         sACStreamingInterfaceNames.put(UsbACInterface.ASI_FORMAT_TYPE, "Format Type");
92         sACStreamingInterfaceNames.put(UsbACInterface.ASI_FORMAT_SPECIFIC, "Format Specific");
93     }
94 
initClassNames()95     private static void initClassNames() {
96         sClassNames = new HashMap<Integer, String>();
97         sClassNames.put(UsbDescriptor.CLASSID_DEVICE, "Device");
98         sClassNames.put(UsbDescriptor.CLASSID_AUDIO, "Audio");
99         sClassNames.put(UsbDescriptor.CLASSID_COM, "Communications");
100         sClassNames.put(UsbDescriptor.CLASSID_HID, "HID");
101         sClassNames.put(UsbDescriptor.CLASSID_PHYSICAL, "Physical");
102         sClassNames.put(UsbDescriptor.CLASSID_IMAGE, "Image");
103         sClassNames.put(UsbDescriptor.CLASSID_PRINTER, "Printer");
104         sClassNames.put(UsbDescriptor.CLASSID_STORAGE, "Storage");
105         sClassNames.put(UsbDescriptor.CLASSID_HUB, "Hub");
106         sClassNames.put(UsbDescriptor.CLASSID_CDC_CONTROL, "CDC Control");
107         sClassNames.put(UsbDescriptor.CLASSID_SMART_CARD, "Smart Card");
108         sClassNames.put(UsbDescriptor.CLASSID_SECURITY, "Security");
109         sClassNames.put(UsbDescriptor.CLASSID_VIDEO, "Video");
110         sClassNames.put(UsbDescriptor.CLASSID_HEALTHCARE, "Healthcare");
111         sClassNames.put(UsbDescriptor.CLASSID_AUDIOVIDEO, "Audio/Video");
112         sClassNames.put(UsbDescriptor.CLASSID_BILLBOARD, "Billboard");
113         sClassNames.put(UsbDescriptor.CLASSID_TYPECBRIDGE, "Type C Bridge");
114         sClassNames.put(UsbDescriptor.CLASSID_DIAGNOSTIC, "Diagnostic");
115         sClassNames.put(UsbDescriptor.CLASSID_WIRELESS, "Wireless");
116         sClassNames.put(UsbDescriptor.CLASSID_MISC, "Misc");
117         sClassNames.put(UsbDescriptor.CLASSID_APPSPECIFIC, "Application Specific");
118         sClassNames.put(UsbDescriptor.CLASSID_VENDSPECIFIC, "Vendor Specific");
119     }
120 
initAudioSubclassNames()121     private static void initAudioSubclassNames() {
122         sAudioSubclassNames = new HashMap<Integer, String>();
123         sAudioSubclassNames.put(UsbDescriptor.AUDIO_SUBCLASS_UNDEFINED, "Undefinded");
124         sAudioSubclassNames.put(UsbDescriptor.AUDIO_AUDIOCONTROL, "Audio Control");
125         sAudioSubclassNames.put(UsbDescriptor.AUDIO_AUDIOSTREAMING, "Audio Streaming");
126         sAudioSubclassNames.put(UsbDescriptor.AUDIO_MIDISTREAMING, "MIDI Streaming");
127     }
128 
initAudioEncodingNames()129     private static void initAudioEncodingNames() {
130         sAudioEncodingNames = new HashMap<Integer, String>();
131         sAudioEncodingNames.put(UsbACInterface.FORMAT_I_UNDEFINED, "Format I Undefined");
132         sAudioEncodingNames.put(UsbACInterface.FORMAT_I_PCM, "Format I PCM");
133         sAudioEncodingNames.put(UsbACInterface.FORMAT_I_PCM8, "Format I PCM8");
134         sAudioEncodingNames.put(UsbACInterface.FORMAT_I_IEEE_FLOAT, "Format I FLOAT");
135         sAudioEncodingNames.put(UsbACInterface.FORMAT_I_ALAW, "Format I ALAW");
136         sAudioEncodingNames.put(UsbACInterface.FORMAT_I_MULAW, "Format I MuLAW");
137         sAudioEncodingNames.put(UsbACInterface.FORMAT_II_UNDEFINED, "FORMAT_II Undefined");
138         sAudioEncodingNames.put(UsbACInterface.FORMAT_II_MPEG, "FORMAT_II MPEG");
139         sAudioEncodingNames.put(UsbACInterface.FORMAT_II_AC3, "FORMAT_II AC3");
140         sAudioEncodingNames.put(UsbACInterface.FORMAT_III_UNDEFINED, "FORMAT_III Undefined");
141         sAudioEncodingNames.put(UsbACInterface.FORMAT_III_IEC1937AC3, "FORMAT_III IEC1937 AC3");
142         sAudioEncodingNames.put(UsbACInterface.FORMAT_III_IEC1937_MPEG1_Layer1,
143                 "FORMAT_III MPEG1 Layer 1");
144         sAudioEncodingNames.put(UsbACInterface.FORMAT_III_IEC1937_MPEG1_Layer2,
145                 "FORMAT_III MPEG1 Layer 2");
146         sAudioEncodingNames.put(UsbACInterface.FORMAT_III_IEC1937_MPEG2_EXT,
147                 "FORMAT_III MPEG2 EXT");
148         sAudioEncodingNames.put(UsbACInterface.FORMAT_III_IEC1937_MPEG2_Layer1LS,
149                 "FORMAT_III MPEG2 Layer1LS");
150     }
151 
initTerminalNames()152     private static void initTerminalNames() {
153         sTerminalNames = new HashMap<Integer, String>();
154         sTerminalNames.put(UsbTerminalTypes.TERMINAL_USB_STREAMING, "USB Streaming");
155 
156         sTerminalNames.put(UsbTerminalTypes.TERMINAL_IN_UNDEFINED, "Undefined");
157         sTerminalNames.put(UsbTerminalTypes.TERMINAL_IN_MIC, "Microphone");
158         sTerminalNames.put(UsbTerminalTypes.TERMINAL_IN_DESKTOP_MIC, "Desktop Microphone");
159         sTerminalNames.put(UsbTerminalTypes.TERMINAL_IN_PERSONAL_MIC,
160                 "Personal (headset) Microphone");
161         sTerminalNames.put(UsbTerminalTypes.TERMINAL_IN_OMNI_MIC, "Omni Microphone");
162         sTerminalNames.put(UsbTerminalTypes.TERMINAL_IN_MIC_ARRAY, "Microphone Array");
163         sTerminalNames.put(UsbTerminalTypes.TERMINAL_IN_PROC_MIC_ARRAY,
164                 "Proecessing Microphone Array");
165 
166         sTerminalNames.put(UsbTerminalTypes.TERMINAL_OUT_UNDEFINED, "Undefined");
167         sTerminalNames.put(UsbTerminalTypes.TERMINAL_OUT_SPEAKER, "Speaker");
168         sTerminalNames.put(UsbTerminalTypes.TERMINAL_OUT_HEADPHONES, "Headphones");
169         sTerminalNames.put(UsbTerminalTypes.TERMINAL_OUT_HEADMOUNTED, "Head Mounted Speaker");
170         sTerminalNames.put(UsbTerminalTypes.TERMINAL_OUT_DESKTOPSPEAKER, "Desktop Speaker");
171         sTerminalNames.put(UsbTerminalTypes.TERMINAL_OUT_ROOMSPEAKER, "Room Speaker");
172         sTerminalNames.put(UsbTerminalTypes.TERMINAL_OUT_COMSPEAKER, "Communications Speaker");
173         sTerminalNames.put(UsbTerminalTypes.TERMINAL_OUT_LFSPEAKER, "Low Frequency Speaker");
174 
175         sTerminalNames.put(UsbTerminalTypes.TERMINAL_BIDIR_UNDEFINED, "Undefined");
176         sTerminalNames.put(UsbTerminalTypes.TERMINAL_BIDIR_HANDSET, "Handset");
177         sTerminalNames.put(UsbTerminalTypes.TERMINAL_BIDIR_HEADSET, "Headset");
178         sTerminalNames.put(UsbTerminalTypes.TERMINAL_BIDIR_SKRPHONE, "Speaker Phone");
179         sTerminalNames.put(UsbTerminalTypes.TERMINAL_BIDIR_SKRPHONE_SUPRESS,
180                 "Speaker Phone (echo supressing)");
181         sTerminalNames.put(UsbTerminalTypes.TERMINAL_BIDIR_SKRPHONE_CANCEL,
182                 "Speaker Phone (echo canceling)");
183 
184         sTerminalNames.put(UsbTerminalTypes.TERMINAL_TELE_UNDEFINED, "Undefined");
185         sTerminalNames.put(UsbTerminalTypes.TERMINAL_TELE_PHONELINE, "Phone Line");
186         sTerminalNames.put(UsbTerminalTypes.TERMINAL_TELE_PHONE, "Telephone");
187         sTerminalNames.put(UsbTerminalTypes.TERMINAL_TELE_DOWNLINEPHONE, "Down Line Phone");
188 
189         sTerminalNames.put(UsbTerminalTypes.TERMINAL_EXTERN_UNDEFINED, "Undefined");
190         sTerminalNames.put(UsbTerminalTypes.TERMINAL_EXTERN_ANALOG, "Analog Connector");
191         sTerminalNames.put(UsbTerminalTypes.TERMINAL_EXTERN_DIGITAL, "Digital Connector");
192         sTerminalNames.put(UsbTerminalTypes.TERMINAL_EXTERN_LINE, "Line Connector");
193         sTerminalNames.put(UsbTerminalTypes.TERMINAL_EXTERN_LEGACY, "Legacy Audio Connector");
194         sTerminalNames.put(UsbTerminalTypes.TERMINAL_EXTERN_SPIDF, "S/PIDF Interface");
195         sTerminalNames.put(UsbTerminalTypes.TERMINAL_EXTERN_1394DA, "1394 Audio");
196         sTerminalNames.put(UsbTerminalTypes.TERMINAL_EXTERN_1394DV, "1394 Audio/Video");
197 
198         sTerminalNames.put(UsbTerminalTypes.TERMINAL_EMBED_UNDEFINED, "Undefined");
199         sTerminalNames.put(UsbTerminalTypes.TERMINAL_EMBED_CALNOISE, "Calibration Nose");
200         sTerminalNames.put(UsbTerminalTypes.TERMINAL_EMBED_EQNOISE, "EQ Noise");
201         sTerminalNames.put(UsbTerminalTypes.TERMINAL_EMBED_CDPLAYER, "CD Player");
202         sTerminalNames.put(UsbTerminalTypes.TERMINAL_EMBED_DAT, "DAT");
203         sTerminalNames.put(UsbTerminalTypes.TERMINAL_EMBED_DCC, "DCC");
204         sTerminalNames.put(UsbTerminalTypes.TERMINAL_EMBED_MINIDISK, "Mini Disk");
205         sTerminalNames.put(UsbTerminalTypes.TERMINAL_EMBED_ANALOGTAPE, "Analog Tap");
206         sTerminalNames.put(UsbTerminalTypes.TERMINAL_EMBED_PHONOGRAPH, "Phonograph");
207         sTerminalNames.put(UsbTerminalTypes.TERMINAL_EMBED_VCRAUDIO, "VCR Audio");
208         sTerminalNames.put(UsbTerminalTypes.TERMINAL_EMBED_VIDDISKAUDIO, "Video Disk Audio");
209         sTerminalNames.put(UsbTerminalTypes.TERMINAL_EMBED_DVDAUDIO, "DVD Audio");
210         sTerminalNames.put(UsbTerminalTypes.TERMINAL_EMBED_TVAUDIO, "TV Audio");
211         sTerminalNames.put(UsbTerminalTypes.TERMINAL_EMBED_SATELLITEAUDIO, "Satellite Audio");
212         sTerminalNames.put(UsbTerminalTypes.TERMINAL_EMBED_CABLEAUDIO, "Cable Tuner Audio");
213         sTerminalNames.put(UsbTerminalTypes.TERMINAL_EMBED_DSSAUDIO, "DSS Audio");
214         sTerminalNames.put(UsbTerminalTypes.TERMINAL_EMBED_RADIOTRANSMITTER, "Radio Transmitter");
215         sTerminalNames.put(UsbTerminalTypes.TERMINAL_EMBED_MULTITRACK, "Multitrack Recorder");
216         sTerminalNames.put(UsbTerminalTypes.TERMINAL_EMBED_SYNTHESIZER, "Synthesizer");
217     }
218 
219     /**
220      * Retrieves the terminal name for the specified terminal type ID.
221      */
getTerminalName(int terminalType)222     public static String getTerminalName(int terminalType) {
223         String name = sTerminalNames.get(terminalType);
224         return name != null
225                 ? name
226                 : "Unknown Terminal Type 0x" + Integer.toHexString(terminalType);
227     }
228 
initFormatNames()229     private static void initFormatNames() {
230         sFormatNames = new HashMap<Integer, String>();
231 
232         sFormatNames.put((int) UsbASFormat.FORMAT_TYPE_I, "FORMAT_TYPE_I");
233         sFormatNames.put((int) UsbASFormat.FORMAT_TYPE_II, "FORMAT_TYPE_II");
234         sFormatNames.put((int) UsbASFormat.FORMAT_TYPE_III, "FORMAT_TYPE_III");
235         sFormatNames.put((int) UsbASFormat.FORMAT_TYPE_IV, "FORMAT_TYPE_IV");
236         sFormatNames.put((int) UsbASFormat.EXT_FORMAT_TYPE_I, "EXT_FORMAT_TYPE_I");
237         sFormatNames.put((int) UsbASFormat.EXT_FORMAT_TYPE_II, "EXT_FORMAT_TYPE_II");
238         sFormatNames.put((int) UsbASFormat.EXT_FORMAT_TYPE_III, "EXT_FORMAT_TYPE_III");
239     }
240 
241     /**
242      * Retrieves the name for the specified format (encoding) type ID.
243      */
getFormatName(int format)244     public static String getFormatName(int format) {
245         String name = sFormatNames.get(format);
246         return name != null
247                 ? name
248                 : "Unknown Format Type 0x" + Integer.toHexString(format);
249     }
250 
251     /**
252      * Initializes string tables.
253      */
allocUsbStrings()254     private static void allocUsbStrings() {
255         initDescriptorNames();
256         initACControlInterfaceNames();
257         initACStreamingInterfaceNames();
258         initClassNames();
259         initAudioSubclassNames();
260         initAudioEncodingNames();
261         initTerminalNames();
262         initFormatNames();
263     }
264 
265     /**
266      * Retrieves the name for the specified descriptor ID.
267      */
getDescriptorName(byte descriptorID)268     public static String getDescriptorName(byte descriptorID) {
269         String name = sDescriptorNames.get(descriptorID);
270         int iDescriptorID = descriptorID & 0xFF;
271         return name != null
272             ? name
273             : "Unknown Descriptor [0x" + Integer.toHexString(iDescriptorID)
274                 + ":" + iDescriptorID + "]";
275     }
276 
277     /**
278      * Retrieves the audio-class control interface name for the specified audio-class subtype.
279      */
getACControlInterfaceName(byte subtype)280     public static String getACControlInterfaceName(byte subtype) {
281         String name = sACControlInterfaceNames.get(subtype);
282         int iSubType = subtype & 0xFF;
283         return name != null
284                 ? name
285                 : "Unknown subtype [0x" + Integer.toHexString(iSubType)
286                     + ":" + iSubType + "]";
287     }
288 
289     /**
290      * Retrieves the audio-class streaming interface name for the specified audio-class subtype.
291      */
getACStreamingInterfaceName(byte subtype)292     public static String getACStreamingInterfaceName(byte subtype) {
293         String name = sACStreamingInterfaceNames.get(subtype);
294         int iSubType = subtype & 0xFF;
295         return name != null
296                 ? name
297                 : "Unknown Subtype [0x" + Integer.toHexString(iSubType) + ":"
298                     + iSubType + "]";
299     }
300 
301     /**
302      * Retrieves the name for the specified USB class ID.
303      */
getClassName(int classID)304     public static String getClassName(int classID) {
305         String name = sClassNames.get(classID);
306         int iClassID = classID & 0xFF;
307         return name != null
308                 ? name
309                 : "Unknown Class ID [0x" + Integer.toHexString(iClassID) + ":"
310                     + iClassID + "]";
311     }
312 
313     /**
314      * Retrieves the name for the specified USB audio subclass ID.
315      */
getAudioSubclassName(int subClassID)316     public static String getAudioSubclassName(int subClassID) {
317         String name = sAudioSubclassNames.get(subClassID);
318         int iSubclassID = subClassID & 0xFF;
319         return name != null
320                 ? name
321                 : "Unknown Audio Subclass [0x" + Integer.toHexString(iSubclassID) + ":"
322                     + iSubclassID + "]";
323     }
324 
325     /**
326      * Retrieves the name for the specified USB audio format ID.
327      */
getAudioFormatName(int formatID)328     public static String getAudioFormatName(int formatID) {
329         String name = sAudioEncodingNames.get(formatID);
330         return name != null
331                 ? name
332                 : "Unknown Format (encoding) ID [0x" + Integer.toHexString(formatID) + ":"
333                     + formatID + "]";
334     }
335 
336     /**
337      * Retrieves the name for the specified USB audio interface subclass ID.
338      */
getACInterfaceSubclassName(int subClassID)339     public static String getACInterfaceSubclassName(int subClassID) {
340         return subClassID == UsbDescriptor.AUDIO_AUDIOCONTROL ? "AC Control" : "AC Streaming";
341     }
342 }
343