Home | History | Annotate | Download | only in usb
      1 /*
      2  * Copyright (C) 2015 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  * Describes the status of a USB port.
     24  * <p>
     25  * This object is immutable.
     26  * </p>
     27  *
     28  * @hide
     29  */
     30 public final class UsbPortStatus implements Parcelable {
     31     private final int mCurrentMode;
     32     private final int mCurrentPowerRole;
     33     private final int mCurrentDataRole;
     34     private final int mSupportedRoleCombinations;
     35 
     36     /** @hide */
     37     public UsbPortStatus(int currentMode, int currentPowerRole, int currentDataRole,
     38             int supportedRoleCombinations) {
     39         mCurrentMode = currentMode;
     40         mCurrentPowerRole = currentPowerRole;
     41         mCurrentDataRole = currentDataRole;
     42         mSupportedRoleCombinations = supportedRoleCombinations;
     43     }
     44 
     45     /**
     46      * Returns true if there is anything connected to the port.
     47      *
     48      * @return True if there is anything connected to the port.
     49      */
     50     public boolean isConnected() {
     51         return mCurrentMode != 0;
     52     }
     53 
     54     /**
     55      * Gets the current mode of the port.
     56      *
     57      * @return The current mode: {@link UsbPort#MODE_DFP}, {@link UsbPort#MODE_UFP},
     58      * or 0 if nothing is connected.
     59      */
     60     public int getCurrentMode() {
     61         return mCurrentMode;
     62     }
     63 
     64     /**
     65      * Gets the current power role of the port.
     66      *
     67      * @return The current power role: {@link UsbPort#POWER_ROLE_SOURCE},
     68      * {@link UsbPort#POWER_ROLE_SINK}, or 0 if nothing is connected.
     69      */
     70     public int getCurrentPowerRole() {
     71         return mCurrentPowerRole;
     72     }
     73 
     74     /**
     75      * Gets the current data role of the port.
     76      *
     77      * @return The current data role: {@link UsbPort#DATA_ROLE_HOST},
     78      * {@link UsbPort#DATA_ROLE_DEVICE}, or 0 if nothing is connected.
     79      */
     80     public int getCurrentDataRole() {
     81         return mCurrentDataRole;
     82     }
     83 
     84     /**
     85      * Returns true if the specified power and data role combination is supported
     86      * given what is currently connected to the port.
     87      *
     88      * @param powerRole The power role to check: {@link UsbPort#POWER_ROLE_SOURCE}
     89      * or {@link UsbPort#POWER_ROLE_SINK}, or 0 if no power role.
     90      * @param dataRole The data role to check: either {@link UsbPort#DATA_ROLE_HOST}
     91      * or {@link UsbPort#DATA_ROLE_DEVICE}, or 0 if no data role.
     92      */
     93     public boolean isRoleCombinationSupported(int powerRole, int dataRole) {
     94         return (mSupportedRoleCombinations &
     95                 UsbPort.combineRolesAsBit(powerRole, dataRole)) != 0;
     96     }
     97 
     98     /** @hide */
     99     public int getSupportedRoleCombinations() {
    100         return mSupportedRoleCombinations;
    101     }
    102 
    103     @Override
    104     public String toString() {
    105         return "UsbPortStatus{connected=" + isConnected()
    106                 + ", currentMode=" + UsbPort.modeToString(mCurrentMode)
    107                 + ", currentPowerRole=" + UsbPort.powerRoleToString(mCurrentPowerRole)
    108                 + ", currentDataRole=" + UsbPort.dataRoleToString(mCurrentDataRole)
    109                 + ", supportedRoleCombinations="
    110                         + UsbPort.roleCombinationsToString(mSupportedRoleCombinations)
    111                 + "}";
    112     }
    113 
    114     @Override
    115     public int describeContents() {
    116         return 0;
    117     }
    118 
    119     @Override
    120     public void writeToParcel(Parcel dest, int flags) {
    121         dest.writeInt(mCurrentMode);
    122         dest.writeInt(mCurrentPowerRole);
    123         dest.writeInt(mCurrentDataRole);
    124         dest.writeInt(mSupportedRoleCombinations);
    125     }
    126 
    127     public static final Parcelable.Creator<UsbPortStatus> CREATOR =
    128             new Parcelable.Creator<UsbPortStatus>() {
    129         @Override
    130         public UsbPortStatus createFromParcel(Parcel in) {
    131             int currentMode = in.readInt();
    132             int currentPowerRole = in.readInt();
    133             int currentDataRole = in.readInt();
    134             int supportedRoleCombinations = in.readInt();
    135             return new UsbPortStatus(currentMode, currentPowerRole, currentDataRole,
    136                     supportedRoleCombinations);
    137         }
    138 
    139         @Override
    140         public UsbPortStatus[] newArray(int size) {
    141             return new UsbPortStatus[size];
    142         }
    143     };
    144 }
    145