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