Home | History | Annotate | Download | only in tree
      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.tree;
     17 
     18 import com.android.server.usb.descriptors.UsbACInterface;
     19 import com.android.server.usb.descriptors.UsbConfigDescriptor;
     20 import com.android.server.usb.descriptors.UsbDescriptor;
     21 import com.android.server.usb.descriptors.UsbDescriptorParser;
     22 import com.android.server.usb.descriptors.UsbDeviceDescriptor;
     23 import com.android.server.usb.descriptors.UsbEndpointDescriptor;
     24 import com.android.server.usb.descriptors.UsbInterfaceDescriptor;
     25 import com.android.server.usb.descriptors.report.ReportCanvas;
     26 
     27 import java.util.ArrayList;
     28 
     29 /*
     30  * The general layout of the tree looks like this, though no guarentee about
     31  * ordering of descriptors beyond the Device -> Config -> Interface.
     32  *
     33  * Device Descriptor
     34  *   +- Config Descriptor
     35  *       +- Interface Descriptor
     36  *       |   +- Audio Class Interface
     37  *       |   +- Audio Class Interface
     38  *       |   +- Audio Class Interface
     39  *       |   +- Endpoint Descriptor
     40  *       |   +- Endpoint Descriptor
     41  *       +- Interface Descriptor
     42  *           +- Endpoint Descriptor
     43  */
     44 /**
     45  * @hide
     46  *
     47  * A class which builds a tree representation from the results of a (linear)
     48  * parse of USB descriptors.
     49  *
     50  * @see {@link com.android.server.usb.descriptors.UsbDescriptorsParser UsbDescriptorsParser}
     51  */
     52 public final class UsbDescriptorsTree {
     53     private static final String TAG = "UsbDescriptorsTree";
     54 
     55     private UsbDescriptorsDeviceNode mDeviceNode;
     56     private UsbDescriptorsConfigNode mConfigNode;   // being parsed
     57     private UsbDescriptorsInterfaceNode mInterfaceNode; // being parsed
     58 
     59     /**
     60      * Adds THE device descriptor as the root of the tree.
     61      */
     62     private void addDeviceDescriptor(UsbDeviceDescriptor deviceDescriptor) {
     63         mDeviceNode = new UsbDescriptorsDeviceNode(deviceDescriptor);
     64     }
     65 
     66     /**
     67      * Adds A config descriptor to the tree.
     68      */
     69     private void addConfigDescriptor(UsbConfigDescriptor configDescriptor) {
     70         mConfigNode = new UsbDescriptorsConfigNode(configDescriptor);
     71         mDeviceNode.addConfigDescriptorNode(mConfigNode);
     72     }
     73 
     74     /**
     75      * Adds AN interface descriptor to the current configuration in the tree.
     76      */
     77     private void addInterfaceDescriptor(UsbInterfaceDescriptor interfaceDescriptor) {
     78         mInterfaceNode = new UsbDescriptorsInterfaceNode(interfaceDescriptor);
     79         mConfigNode.addInterfaceNode(mInterfaceNode);
     80     }
     81 
     82     /**
     83      * Adds an endpoint descriptor to the current interface in the tree.
     84      */
     85     private void addEndpointDescriptor(UsbEndpointDescriptor endpointDescriptor) {
     86         mInterfaceNode.addEndpointNode(new UsbDescriptorsEndpointNode(endpointDescriptor));
     87     }
     88 
     89     /**
     90      * Adds an audio-class interface descriptor to the current interface in the tree.
     91      */
     92     private void addACInterface(UsbACInterface acInterface) {
     93         mInterfaceNode.addACInterfaceNode(new UsbDescriptorsACInterfaceNode(acInterface));
     94     }
     95 
     96     /**
     97      * Parses the linear descriptor list contained in the parser argument, into a tree
     98      * representation corresponding to the logical structure of the USB descriptors.
     99      */
    100     public void parse(UsbDescriptorParser parser) {
    101 
    102         ArrayList<UsbDescriptor> descriptors = parser.getDescriptors();
    103 
    104         for (int descrIndex = 0; descrIndex < descriptors.size(); descrIndex++) {
    105             UsbDescriptor descriptor = descriptors.get(descrIndex);
    106             switch (descriptor.getType()) {
    107                 //
    108                 // Basic Descriptors
    109                 //
    110                 case UsbDescriptor.DESCRIPTORTYPE_DEVICE:
    111                     addDeviceDescriptor((UsbDeviceDescriptor) descriptor);
    112                     break;
    113 
    114                 case UsbDescriptor.DESCRIPTORTYPE_CONFIG:
    115                     addConfigDescriptor((UsbConfigDescriptor) descriptor);
    116                     break;
    117 
    118                 case UsbDescriptor.DESCRIPTORTYPE_INTERFACE:
    119                     addInterfaceDescriptor((UsbInterfaceDescriptor) descriptor);
    120                     break;
    121 
    122                 case UsbDescriptor.DESCRIPTORTYPE_ENDPOINT:
    123                     addEndpointDescriptor((UsbEndpointDescriptor) descriptor);
    124                     break;
    125 
    126                 //
    127                 // Audio Class Descriptors
    128                 //
    129                 case UsbDescriptor.DESCRIPTORTYPE_AUDIO_INTERFACE:
    130                     addACInterface((UsbACInterface) descriptor);
    131                     break;
    132 
    133                 case UsbDescriptor.DESCRIPTORTYPE_AUDIO_ENDPOINT:
    134                     break;
    135             }
    136         }
    137     }
    138 
    139     /**
    140      * Generate a report of the descriptors tree.
    141      */
    142     public void report(ReportCanvas canvas) {
    143         mDeviceNode.report(canvas);
    144     }
    145 }
    146