Home | History | Annotate | Download | only in descriptors
      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;
     17 
     18 import android.hardware.usb.UsbConfiguration;
     19 import android.hardware.usb.UsbInterface;
     20 import android.util.Log;
     21 
     22 import com.android.server.usb.descriptors.report.ReportCanvas;
     23 
     24 import java.util.ArrayList;
     25 
     26 /**
     27  * @hide
     28  * An USB Config Descriptor.
     29  * see usb11.pdf section 9.6.2
     30  */
     31 public final class UsbConfigDescriptor extends UsbDescriptor {
     32     private static final String TAG = "UsbConfigDescriptor";
     33     private static final boolean DEBUG = false;
     34 
     35     private int mTotalLength;    // 2:2 Total length in bytes of data returned
     36     private byte mNumInterfaces; // 4:1 Number of Interfaces
     37     private int mConfigValue;    // 5:1 Value to use as an argument to select this configuration
     38     private byte mConfigIndex;   // 6:1 Index of String Descriptor describing this configuration
     39     private int mAttribs;        // 7:1 D7 Reserved, set to 1. (USB 1.0 Bus Powered)
     40                                  //     D6 Self Powered
     41                                  //     D5 Remote Wakeup
     42                                  //     D4..0 Reserved, set to 0.
     43     private int mMaxPower;       // 8:1 Maximum Power Consumption in 2mA units
     44 
     45     private ArrayList<UsbInterfaceDescriptor> mInterfaceDescriptors =
     46             new ArrayList<UsbInterfaceDescriptor>();
     47 
     48     UsbConfigDescriptor(int length, byte type) {
     49         super(length, type);
     50         mHierarchyLevel = 2;
     51     }
     52 
     53     public int getTotalLength() {
     54         return mTotalLength;
     55     }
     56 
     57     public byte getNumInterfaces() {
     58         return mNumInterfaces;
     59     }
     60 
     61     public int getConfigValue() {
     62         return mConfigValue;
     63     }
     64 
     65     public byte getConfigIndex() {
     66         return mConfigIndex;
     67     }
     68 
     69     public int getAttribs() {
     70         return mAttribs;
     71     }
     72 
     73     public int getMaxPower() {
     74         return mMaxPower;
     75     }
     76 
     77     void addInterfaceDescriptor(UsbInterfaceDescriptor interfaceDesc) {
     78         mInterfaceDescriptors.add(interfaceDesc);
     79     }
     80 
     81     UsbConfiguration toAndroid(UsbDescriptorParser parser) {
     82         if (DEBUG) {
     83             Log.d(TAG, "  toAndroid()");
     84         }
     85         String name = parser.getDescriptorString(mConfigIndex);
     86         UsbConfiguration config = new
     87                 UsbConfiguration(mConfigValue, name, mAttribs, mMaxPower);
     88         UsbInterface[] interfaces = new UsbInterface[mInterfaceDescriptors.size()];
     89         if (DEBUG) {
     90             Log.d(TAG, "    " + mInterfaceDescriptors.size() + " interfaces.");
     91         }
     92         for (int index = 0; index < mInterfaceDescriptors.size(); index++) {
     93             interfaces[index] = mInterfaceDescriptors.get(index).toAndroid(parser);
     94         }
     95         config.setInterfaces(interfaces);
     96         return config;
     97     }
     98 
     99     @Override
    100     public int parseRawDescriptors(ByteStream stream) {
    101         mTotalLength = stream.unpackUsbShort();
    102         mNumInterfaces = stream.getByte();
    103         mConfigValue = stream.getUnsignedByte();
    104         mConfigIndex = stream.getByte();
    105         mAttribs = stream.getUnsignedByte();
    106         mMaxPower = stream.getUnsignedByte();
    107 
    108         return mLength;
    109     }
    110 
    111     @Override
    112     public void report(ReportCanvas canvas) {
    113         super.report(canvas);
    114 
    115         canvas.openList();
    116         canvas.writeListItem("Config # " + getConfigValue());
    117         canvas.writeListItem(getNumInterfaces() + " Interfaces.");
    118         canvas.writeListItem("Attributes: " + ReportCanvas.getHexString(getAttribs()));
    119         canvas.closeList();
    120     }
    121 }
    122