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.annotation.IntDef; 20 import android.annotation.SystemApi; 21 import android.hardware.usb.V1_0.Constants; 22 import android.os.Parcel; 23 import android.os.Parcelable; 24 25 import com.android.internal.annotations.Immutable; 26 27 import java.lang.annotation.Retention; 28 import java.lang.annotation.RetentionPolicy; 29 30 /** 31 * Describes the status of a USB port. 32 * 33 * @hide 34 */ 35 @Immutable 36 @SystemApi 37 public final class UsbPortStatus implements Parcelable { 38 private final int mCurrentMode; 39 private final @UsbPowerRole int mCurrentPowerRole; 40 private final @UsbDataRole int mCurrentDataRole; 41 private final int mSupportedRoleCombinations; 42 private final @ContaminantProtectionStatus int mContaminantProtectionStatus; 43 private final @ContaminantDetectionStatus int mContaminantDetectionStatus; 44 45 /** 46 * Power role: This USB port does not have a power role. 47 */ 48 public static final int POWER_ROLE_NONE = Constants.PortPowerRole.NONE; 49 50 /** 51 * Power role: This USB port can act as a source (provide power). 52 */ 53 public static final int POWER_ROLE_SOURCE = Constants.PortPowerRole.SOURCE; 54 55 /** 56 * Power role: This USB port can act as a sink (receive power). 57 */ 58 public static final int POWER_ROLE_SINK = Constants.PortPowerRole.SINK; 59 60 @IntDef(prefix = { "POWER_ROLE_" }, value = { 61 POWER_ROLE_NONE, 62 POWER_ROLE_SOURCE, 63 POWER_ROLE_SINK 64 }) 65 @Retention(RetentionPolicy.SOURCE) 66 @interface UsbPowerRole{} 67 68 /** 69 * Power role: This USB port does not have a data role. 70 */ 71 public static final int DATA_ROLE_NONE = Constants.PortDataRole.NONE; 72 73 /** 74 * Data role: This USB port can act as a host (access data services). 75 */ 76 public static final int DATA_ROLE_HOST = Constants.PortDataRole.HOST; 77 78 /** 79 * Data role: This USB port can act as a device (offer data services). 80 */ 81 public static final int DATA_ROLE_DEVICE = Constants.PortDataRole.DEVICE; 82 83 @IntDef(prefix = { "DATA_ROLE_" }, value = { 84 DATA_ROLE_NONE, 85 DATA_ROLE_HOST, 86 DATA_ROLE_DEVICE 87 }) 88 @Retention(RetentionPolicy.SOURCE) 89 @interface UsbDataRole{} 90 91 /** 92 * There is currently nothing connected to this USB port. 93 */ 94 public static final int MODE_NONE = Constants.PortMode.NONE; 95 96 /** 97 * This USB port can act as a downstream facing port (host). 98 * 99 * <p> Implies that the port supports the {@link #POWER_ROLE_SOURCE} and 100 * {@link #DATA_ROLE_HOST} combination of roles (and possibly others as well). 101 */ 102 public static final int MODE_DFP = Constants.PortMode.DFP; 103 104 /** 105 * This USB port can act as an upstream facing port (device). 106 * 107 * <p> Implies that the port supports the {@link #POWER_ROLE_SINK} and 108 * {@link #DATA_ROLE_DEVICE} combination of roles (and possibly others as well). 109 */ 110 public static final int MODE_UFP = Constants.PortMode.UFP; 111 112 /** 113 * This USB port can act either as an downstream facing port (host) or as 114 * an upstream facing port (device). 115 * 116 * <p> Implies that the port supports the {@link #POWER_ROLE_SOURCE} and 117 * {@link #DATA_ROLE_HOST} combination of roles and the {@link #POWER_ROLE_SINK} and 118 * {@link #DATA_ROLE_DEVICE} combination of roles (and possibly others as well). 119 * 120 * @hide 121 */ 122 public static final int MODE_DUAL = Constants.PortMode.DRP; 123 124 /** 125 * This USB port can support USB Type-C Audio accessory. 126 */ 127 public static final int MODE_AUDIO_ACCESSORY = 128 android.hardware.usb.V1_1.Constants.PortMode_1_1.AUDIO_ACCESSORY; 129 130 /** 131 * This USB port can support USB Type-C debug accessory. 132 */ 133 public static final int MODE_DEBUG_ACCESSORY = 134 android.hardware.usb.V1_1.Constants.PortMode_1_1.DEBUG_ACCESSORY; 135 136 /** 137 * Contaminant presence detection not supported by the device. 138 * @hide 139 */ 140 public static final int CONTAMINANT_DETECTION_NOT_SUPPORTED = 141 android.hardware.usb.V1_2.Constants.ContaminantDetectionStatus.NOT_SUPPORTED; 142 143 /** 144 * Contaminant presence detection supported but disabled. 145 * @hide 146 */ 147 public static final int CONTAMINANT_DETECTION_DISABLED = 148 android.hardware.usb.V1_2.Constants.ContaminantDetectionStatus.DISABLED; 149 150 /** 151 * Contaminant presence enabled but not detected. 152 * @hide 153 */ 154 public static final int CONTAMINANT_DETECTION_NOT_DETECTED = 155 android.hardware.usb.V1_2.Constants.ContaminantDetectionStatus.NOT_DETECTED; 156 157 /** 158 * Contaminant presence enabled and detected. 159 * @hide 160 */ 161 public static final int CONTAMINANT_DETECTION_DETECTED = 162 android.hardware.usb.V1_2.Constants.ContaminantDetectionStatus.DETECTED; 163 164 /** 165 * Contaminant protection - No action performed upon detection of 166 * contaminant presence. 167 * @hide 168 */ 169 public static final int CONTAMINANT_PROTECTION_NONE = 170 android.hardware.usb.V1_2.Constants.ContaminantProtectionStatus.NONE; 171 172 /** 173 * Contaminant protection - Port is forced to sink upon detection of 174 * contaminant presence. 175 * @hide 176 */ 177 public static final int CONTAMINANT_PROTECTION_SINK = 178 android.hardware.usb.V1_2.Constants.ContaminantProtectionStatus.FORCE_SINK; 179 180 /** 181 * Contaminant protection - Port is forced to source upon detection of 182 * contaminant presence. 183 * @hide 184 */ 185 public static final int CONTAMINANT_PROTECTION_SOURCE = 186 android.hardware.usb.V1_2.Constants.ContaminantProtectionStatus.FORCE_SOURCE; 187 188 /** 189 * Contaminant protection - Port is disabled upon detection of 190 * contaminant presence. 191 * @hide 192 */ 193 public static final int CONTAMINANT_PROTECTION_FORCE_DISABLE = 194 android.hardware.usb.V1_2.Constants.ContaminantProtectionStatus.FORCE_DISABLE; 195 196 /** 197 * Contaminant protection - Port is disabled upon detection of 198 * contaminant presence. 199 * @hide 200 */ 201 public static final int CONTAMINANT_PROTECTION_DISABLED = 202 android.hardware.usb.V1_2.Constants.ContaminantProtectionStatus.DISABLED; 203 204 @IntDef(prefix = { "CONTAMINANT_DETECION_" }, flag = true, value = { 205 CONTAMINANT_DETECTION_NOT_SUPPORTED, 206 CONTAMINANT_DETECTION_DISABLED, 207 CONTAMINANT_DETECTION_NOT_DETECTED, 208 CONTAMINANT_DETECTION_DETECTED, 209 }) 210 @Retention(RetentionPolicy.SOURCE) 211 @interface ContaminantDetectionStatus{} 212 213 @IntDef(prefix = { "CONTAMINANT_PROTECTION_" }, flag = true, value = { 214 CONTAMINANT_PROTECTION_NONE, 215 CONTAMINANT_PROTECTION_SINK, 216 CONTAMINANT_PROTECTION_SOURCE, 217 CONTAMINANT_PROTECTION_FORCE_DISABLE, 218 CONTAMINANT_PROTECTION_DISABLED, 219 }) 220 @Retention(RetentionPolicy.SOURCE) 221 @interface ContaminantProtectionStatus{} 222 223 @IntDef(prefix = { "MODE_" }, flag = true, value = { 224 MODE_NONE, 225 MODE_DFP, 226 MODE_UFP, 227 MODE_AUDIO_ACCESSORY, 228 MODE_DEBUG_ACCESSORY, 229 }) 230 @Retention(RetentionPolicy.SOURCE) 231 @interface UsbPortMode{} 232 233 /** @hide */ UsbPortStatus(int currentMode, int currentPowerRole, int currentDataRole, int supportedRoleCombinations, int contaminantProtectionStatus, int contaminantDetectionStatus)234 public UsbPortStatus(int currentMode, int currentPowerRole, int currentDataRole, 235 int supportedRoleCombinations, int contaminantProtectionStatus, 236 int contaminantDetectionStatus) { 237 mCurrentMode = currentMode; 238 mCurrentPowerRole = currentPowerRole; 239 mCurrentDataRole = currentDataRole; 240 mSupportedRoleCombinations = supportedRoleCombinations; 241 mContaminantProtectionStatus = contaminantProtectionStatus; 242 mContaminantDetectionStatus = contaminantDetectionStatus; 243 } 244 245 /** 246 * Returns true if there is anything connected to the port. 247 * 248 * @return {@code true} iff there is anything connected to the port. 249 */ isConnected()250 public boolean isConnected() { 251 return mCurrentMode != 0; 252 } 253 254 /** 255 * Gets the current mode of the port. 256 * 257 * @return The current mode: {@link #MODE_DFP}, {@link #MODE_UFP}, 258 * {@link #MODE_AUDIO_ACCESSORY}, {@link #MODE_DEBUG_ACCESSORY}, or {@link {@link #MODE_NONE} if 259 * nothing is connected. 260 */ getCurrentMode()261 public @UsbPortMode int getCurrentMode() { 262 return mCurrentMode; 263 } 264 265 /** 266 * Gets the current power role of the port. 267 * 268 * @return The current power role: {@link #POWER_ROLE_SOURCE}, {@link #POWER_ROLE_SINK}, or 269 * {@link #POWER_ROLE_NONE} if nothing is connected. 270 */ getCurrentPowerRole()271 public @UsbPowerRole int getCurrentPowerRole() { 272 return mCurrentPowerRole; 273 } 274 275 /** 276 * Gets the current data role of the port. 277 * 278 * @return The current data role: {@link #DATA_ROLE_HOST}, {@link #DATA_ROLE_DEVICE}, or 279 * {@link #DATA_ROLE_NONE} if nothing is connected. 280 */ getCurrentDataRole()281 public @UsbDataRole int getCurrentDataRole() { 282 return mCurrentDataRole; 283 } 284 285 /** 286 * Returns true if the specified power and data role combination is supported 287 * given what is currently connected to the port. 288 * 289 * @param powerRole The power role to check: {@link #POWER_ROLE_SOURCE} or 290 * {@link #POWER_ROLE_SINK}, or {@link #POWER_ROLE_NONE} if no power role. 291 * @param dataRole The data role to check: either {@link #DATA_ROLE_HOST} or 292 * {@link #DATA_ROLE_DEVICE}, or {@link #DATA_ROLE_NONE} if no data role. 293 */ isRoleCombinationSupported(@sbPowerRole int powerRole, @UsbDataRole int dataRole)294 public boolean isRoleCombinationSupported(@UsbPowerRole int powerRole, 295 @UsbDataRole int dataRole) { 296 return (mSupportedRoleCombinations & 297 UsbPort.combineRolesAsBit(powerRole, dataRole)) != 0; 298 } 299 300 /** 301 * Get the supported role combinations. 302 */ getSupportedRoleCombinations()303 public int getSupportedRoleCombinations() { 304 return mSupportedRoleCombinations; 305 } 306 307 /** 308 * Returns contaminant detection status. 309 * 310 * @hide 311 */ getContaminantDetectionStatus()312 public @ContaminantDetectionStatus int getContaminantDetectionStatus() { 313 return mContaminantDetectionStatus; 314 } 315 316 /** 317 * Returns contamiant protection status. 318 * 319 * @hide 320 */ getContaminantProtectionStatus()321 public @ContaminantProtectionStatus int getContaminantProtectionStatus() { 322 return mContaminantProtectionStatus; 323 } 324 325 @Override toString()326 public String toString() { 327 return "UsbPortStatus{connected=" + isConnected() 328 + ", currentMode=" + UsbPort.modeToString(mCurrentMode) 329 + ", currentPowerRole=" + UsbPort.powerRoleToString(mCurrentPowerRole) 330 + ", currentDataRole=" + UsbPort.dataRoleToString(mCurrentDataRole) 331 + ", supportedRoleCombinations=" 332 + UsbPort.roleCombinationsToString(mSupportedRoleCombinations) 333 + ", contaminantDetectionStatus=" 334 + getContaminantDetectionStatus() 335 + ", contaminantProtectionStatus=" 336 + getContaminantProtectionStatus() 337 + "}"; 338 } 339 340 @Override describeContents()341 public int describeContents() { 342 return 0; 343 } 344 345 @Override writeToParcel(Parcel dest, int flags)346 public void writeToParcel(Parcel dest, int flags) { 347 dest.writeInt(mCurrentMode); 348 dest.writeInt(mCurrentPowerRole); 349 dest.writeInt(mCurrentDataRole); 350 dest.writeInt(mSupportedRoleCombinations); 351 dest.writeInt(mContaminantProtectionStatus); 352 dest.writeInt(mContaminantDetectionStatus); 353 } 354 355 public static final @android.annotation.NonNull Parcelable.Creator<UsbPortStatus> CREATOR = 356 new Parcelable.Creator<UsbPortStatus>() { 357 @Override 358 public UsbPortStatus createFromParcel(Parcel in) { 359 int currentMode = in.readInt(); 360 int currentPowerRole = in.readInt(); 361 int currentDataRole = in.readInt(); 362 int supportedRoleCombinations = in.readInt(); 363 int contaminantProtectionStatus = in.readInt(); 364 int contaminantDetectionStatus = in.readInt(); 365 return new UsbPortStatus(currentMode, currentPowerRole, currentDataRole, 366 supportedRoleCombinations, contaminantProtectionStatus, 367 contaminantDetectionStatus); 368 } 369 370 @Override 371 public UsbPortStatus[] newArray(int size) { 372 return new UsbPortStatus[size]; 373 } 374 }; 375 } 376