Home | History | Annotate | Download | only in usb
      1 /*
      2  * Copyright (C) 2014 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 
     17 package android.hardware.usb;
     18 
     19 import android.os.Parcel;
     20 import android.os.Parcelable;
     21 
     22 /**
     23  * A class representing a configuration on a {@link UsbDevice}.
     24  * A USB configuration can have one or more interfaces, each one providing a different
     25  * piece of functionality, separate from the other interfaces.
     26  * An interface will have one or more {@link UsbEndpoint}s, which are the
     27  * channels by which the host transfers data with the device.
     28  *
     29  * <div class="special reference">
     30  * <h3>Developer Guides</h3>
     31  * <p>For more information about communicating with USB hardware, read the
     32  * <a href="{@docRoot}guide/topics/usb/index.html">USB</a> developer guide.</p>
     33  * </div>
     34  */
     35 public class UsbConfiguration implements Parcelable {
     36 
     37     private final int mId;
     38     private final String mName;
     39     private final int mAttributes;
     40     private final int mMaxPower;
     41     private Parcelable[] mInterfaces;
     42 
     43     /**
     44      * Mask for "self-powered" bit in the configuration's attributes.
     45      * @see #getAttributes
     46      */
     47     private static final int ATTR_SELF_POWERED = 1 << 6;
     48 
     49     /**
     50      * Mask for "remote wakeup" bit in the configuration's attributes.
     51      * @see #getAttributes
     52      */
     53     private static final int ATTR_REMOTE_WAKEUP = 1 << 5;
     54 
     55     /**
     56      * UsbConfiguration should only be instantiated by UsbService implementation
     57      * @hide
     58      */
     59     public UsbConfiguration(int id, String name, int attributes, int maxPower) {
     60         mId = id;
     61         mName = name;
     62         mAttributes = attributes;
     63         mMaxPower = maxPower;
     64     }
     65 
     66     /**
     67      * Returns the configuration's ID field.
     68      * This is an integer that uniquely identifies the configuration on the device.
     69      *
     70      * @return the configuration's ID
     71      */
     72     public int getId() {
     73         return mId;
     74     }
     75 
     76     /**
     77      * Returns the configuration's name.
     78      *
     79      * @return the configuration's name
     80      */
     81     public String getName() {
     82         return mName;
     83     }
     84 
     85     /**
     86      * Returns the self-powered attribute value configuration's attributes field.
     87      * This attribute indicates that the device has a power source other than the USB connection.
     88      *
     89      * @return the configuration's self-powered attribute
     90      */
     91     public boolean isSelfPowered() {
     92         return (mAttributes & ATTR_SELF_POWERED) != 0;
     93     }
     94 
     95     /**
     96      * Returns the remote-wakeup attribute value configuration's attributes field.
     97      * This attributes that the device may signal the host to wake from suspend.
     98      *
     99      * @return the configuration's remote-wakeup attribute
    100      */
    101     public boolean isRemoteWakeup() {
    102         return (mAttributes & ATTR_REMOTE_WAKEUP) != 0;
    103     }
    104 
    105     /**
    106      * Returns the configuration's max power consumption, in milliamps.
    107      *
    108      * @return the configuration's max power
    109      */
    110     public int getMaxPower() {
    111         return mMaxPower * 2;
    112     }
    113 
    114     /**
    115      * Returns the number of {@link UsbInterface}s this configuration contains.
    116      *
    117      * @return the number of endpoints
    118      */
    119     public int getInterfaceCount() {
    120         return mInterfaces.length;
    121     }
    122 
    123     /**
    124      * Returns the {@link UsbInterface} at the given index.
    125      *
    126      * @return the interface
    127      */
    128     public UsbInterface getInterface(int index) {
    129         return (UsbInterface)mInterfaces[index];
    130     }
    131 
    132     /**
    133      * Only used by UsbService implementation
    134      * @hide
    135      */
    136     public void setInterfaces(Parcelable[] interfaces) {
    137         mInterfaces = interfaces;
    138     }
    139 
    140     @Override
    141     public String toString() {
    142         StringBuilder builder = new StringBuilder("UsbConfiguration[mId=" + mId +
    143                 ",mName=" + mName + ",mAttributes=" + mAttributes +
    144                 ",mMaxPower=" + mMaxPower + ",mInterfaces=[");
    145         for (int i = 0; i < mInterfaces.length; i++) {
    146             builder.append("\n");
    147             builder.append(mInterfaces[i].toString());
    148         }
    149         builder.append("]");
    150         return builder.toString();
    151     }
    152 
    153     public static final Parcelable.Creator<UsbConfiguration> CREATOR =
    154         new Parcelable.Creator<UsbConfiguration>() {
    155         public UsbConfiguration createFromParcel(Parcel in) {
    156             int id = in.readInt();
    157             String name = in.readString();
    158             int attributes = in.readInt();
    159             int maxPower = in.readInt();
    160             Parcelable[] interfaces = in.readParcelableArray(UsbInterface.class.getClassLoader());
    161             UsbConfiguration configuration = new UsbConfiguration(id, name, attributes, maxPower);
    162             configuration.setInterfaces(interfaces);
    163             return configuration;
    164         }
    165 
    166         public UsbConfiguration[] newArray(int size) {
    167             return new UsbConfiguration[size];
    168         }
    169     };
    170 
    171     public int describeContents() {
    172         return 0;
    173     }
    174 
    175     public void writeToParcel(Parcel parcel, int flags) {
    176         parcel.writeInt(mId);
    177         parcel.writeString(mName);
    178         parcel.writeInt(mAttributes);
    179         parcel.writeInt(mMaxPower);
    180         parcel.writeParcelableArray(mInterfaces, 0);
    181    }
    182 }
    183