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