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 */
16package com.android.server.usb.descriptors;
17
18import com.android.server.usb.descriptors.report.ReportCanvas;
19import com.android.server.usb.descriptors.report.UsbStrings;
20
21/**
22 * @hide
23 * An audio class-specific Format Interface.
24 *   Subclasses: UsbACFormatI and UsbACFormatII.
25 * see audio10.pdf section 4.5.3 & & Frmts10.pdf
26 */
27public class UsbASFormat extends UsbACInterface {
28    private static final String TAG = "UsbASFormat";
29
30    private final byte mFormatType;   // 3:1 FORMAT_TYPE_*
31
32    public static final byte FORMAT_TYPE_I      = 1;
33    public static final byte FORMAT_TYPE_II     = 2;
34    // these showed up in USB 2.0
35    public static final byte FORMAT_TYPE_III    = 3;
36    public static final byte FORMAT_TYPE_IV     = 4;
37
38    // "extended" formats
39    public static final byte EXT_FORMAT_TYPE_I      = (byte) 0x81;
40    public static final byte EXT_FORMAT_TYPE_II     = (byte) 0x82;
41    public static final byte EXT_FORMAT_TYPE_III    = (byte) 0x83;
42
43    public UsbASFormat(int length, byte type, byte subtype, byte formatType, int mSubclass) {
44        super(length, type, subtype, mSubclass);
45        mFormatType = formatType;
46    }
47
48    public byte getFormatType() {
49        return mFormatType;
50    }
51
52    public int[] getSampleRates() {
53        return null;
54    }
55
56    public int[] getBitDepths() {
57        return null;
58    }
59
60    public int[] getChannelCounts() {
61        return null;
62    }
63
64    /**
65     * Allocates the audio-class format subtype associated with the format type read from the
66     * stream.
67     */
68    public static UsbDescriptor allocDescriptor(UsbDescriptorParser parser,
69            ByteStream stream, int length, byte type,
70            byte subtype, int subclass) {
71
72        byte formatType = stream.getByte();
73        int acInterfaceSpec = parser.getACInterfaceSpec();
74
75        switch (formatType) {
76            case FORMAT_TYPE_I:
77                if (acInterfaceSpec == UsbDeviceDescriptor.USBSPEC_2_0) {
78                    return new Usb20ASFormatI(length, type, subtype, formatType, subclass);
79                } else {
80                    return new Usb10ASFormatI(length, type, subtype, formatType, subclass);
81                }
82
83            case FORMAT_TYPE_II:
84                if (acInterfaceSpec == UsbDeviceDescriptor.USBSPEC_2_0) {
85                    return new Usb20ASFormatII(length, type, subtype, formatType, subclass);
86                } else {
87                    return new Usb10ASFormatII(length, type, subtype, formatType, subclass);
88                }
89
90            // USB 2.0 Exclusive Format Types
91            case FORMAT_TYPE_III:
92                return new Usb20ASFormatIII(length, type, subtype, formatType, subclass);
93
94            case FORMAT_TYPE_IV:
95                //TODO - implement this type.
96            default:
97                return new UsbASFormat(length, type, subtype, formatType, subclass);
98        }
99    }
100
101    @Override
102    public void report(ReportCanvas canvas) {
103        super.report(canvas);
104
105        canvas.writeParagraph(UsbStrings.getFormatName(getFormatType()), /*emphasis*/false);
106    }
107}
108