Debian Codegraph @

core/java/android/hardware/usb/UsbPortStatus.java

raw symbols SCIP
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.Nullable;
22 import android.annotation.SystemApi;
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 static final String TAG = "UsbPortStatus";
40 private final int mCurrentMode;
41 private final @UsbPowerRole int mCurrentPowerRole;
42 private final @UsbDataRole int mCurrentDataRole;
43 private final int mSupportedRoleCombinations;
44 private final @ContaminantProtectionStatus int mContaminantProtectionStatus;
45 private final @ContaminantDetectionStatus int mContaminantDetectionStatus;
46 private final boolean mPowerTransferLimited;
47 private final @UsbDataStatus int mUsbDataStatus;
48 private final @PowerBrickConnectionStatus int mPowerBrickConnectionStatus;
49
50 /**
51 * Power role: This USB port does not have a power role.
52 */
53 public static final int POWER_ROLE_NONE = 0;
54
55 /**
56 * Power role: This USB port can act as a source (provide power).
57 */
58 public static final int POWER_ROLE_SOURCE = 1;
59
60 /**
61 * Power role: This USB port can act as a sink (receive power).
62 */
63 public static final int POWER_ROLE_SINK = 2;
64
65 @IntDef(prefix = { "POWER_ROLE_" }, value = {
66 POWER_ROLE_NONE,
67 POWER_ROLE_SOURCE,
68 POWER_ROLE_SINK
69 })
70 @Retention(RetentionPolicy.SOURCE)
71 @interface UsbPowerRole{}
72
73 /**
74 * Power role: This USB port does not have a data role.
75 */
76 public static final int DATA_ROLE_NONE = 0;
77
78 /**
79 * Data role: This USB port can act as a host (access data services).
80 */
81 public static final int DATA_ROLE_HOST = 1;
82
83 /**
84 * Data role: This USB port can act as a device (offer data services).
85 */
86 public static final int DATA_ROLE_DEVICE = 2;
87
88 @IntDef(prefix = { "DATA_ROLE_" }, value = {
89 DATA_ROLE_NONE,
90 DATA_ROLE_HOST,
91 DATA_ROLE_DEVICE
92 })
93 @Retention(RetentionPolicy.SOURCE)
94 @interface UsbDataRole{}
95
96 /**
97 * There is currently nothing connected to this USB port.
98 */
99 public static final int MODE_NONE = 0;
100
101 /**
102 * This USB port can act as an upstream facing port (device).
103 *
104 * <p> Implies that the port supports the {@link #POWER_ROLE_SINK} and
105 * {@link #DATA_ROLE_DEVICE} combination of roles (and possibly others as well).
106 */
107 public static final int MODE_UFP = 1 << 0;
108
109 /**
110 * This USB port can act as a downstream facing port (host).
111 *
112 * <p> Implies that the port supports the {@link #POWER_ROLE_SOURCE} and
113 * {@link #DATA_ROLE_HOST} combination of roles (and possibly others as well).
114 */
115 public static final int MODE_DFP = 1 << 1;
116
117 /**
118 * This USB port can act either as an downstream facing port (host) or as
119 * an upstream facing port (device).
120 *
121 * <p> Implies that the port supports the {@link #POWER_ROLE_SOURCE} and
122 * {@link #DATA_ROLE_HOST} combination of roles and the {@link #POWER_ROLE_SINK} and
123 * {@link #DATA_ROLE_DEVICE} combination of roles (and possibly others as well).
124 *
125 * @hide
126 */
127 public static final int MODE_DUAL = MODE_UFP | MODE_DFP;
128
129 /**
130 * This USB port can support USB Type-C Audio accessory.
131 */
132 public static final int MODE_AUDIO_ACCESSORY = 1 << 2;
133
134 /**
135 * This USB port can support USB Type-C debug accessory.
136 */
137 public static final int MODE_DEBUG_ACCESSORY = 1 << 3;
138
139 /**
140 * Contaminant presence detection not supported by the device.
141 * @hide
142 */
143 public static final int CONTAMINANT_DETECTION_NOT_SUPPORTED = 0;
144
145 /**
146 * Contaminant presence detection supported but disabled.
147 * @hide
148 */
149 public static final int CONTAMINANT_DETECTION_DISABLED = 1;
150
151 /**
152 * Contaminant presence enabled but not detected.
153 * @hide
154 */
155 public static final int CONTAMINANT_DETECTION_NOT_DETECTED = 2;
156
157 /**
158 * Contaminant presence enabled and detected.
159 * @hide
160 */
161 public static final int CONTAMINANT_DETECTION_DETECTED = 3;
162
163 /**
164 * Contaminant protection - No action performed upon detection of
165 * contaminant presence.
166 * @hide
167 */
168 public static final int CONTAMINANT_PROTECTION_NONE = 0;
169
170 /**
171 * Contaminant protection - Port is forced to sink upon detection of
172 * contaminant presence.
173 * @hide
174 */
175 public static final int CONTAMINANT_PROTECTION_SINK = 1 << 0;
176
177 /**
178 * Contaminant protection - Port is forced to source upon detection of
179 * contaminant presence.
180 * @hide
181 */
182 public static final int CONTAMINANT_PROTECTION_SOURCE = 1 << 1;
183
184 /**
185 * Contaminant protection - Port is disabled upon detection of
186 * contaminant presence.
187 * @hide
188 */
189 public static final int CONTAMINANT_PROTECTION_FORCE_DISABLE = 1 << 2;
190
191 /**
192 * Contaminant protection - Port is disabled upon detection of
193 * contaminant presence.
194 * @hide
195 */
196 public static final int CONTAMINANT_PROTECTION_DISABLED = 1 << 3;
197
198 /**
199 * USB data status is not known.
200 */
201 public static final int DATA_STATUS_UNKNOWN = 0;
202
203 /**
204 * USB data is enabled.
205 */
206 public static final int DATA_STATUS_ENABLED = 1 << 0;
207
208 /**
209 * USB data is disabled as the port is too hot.
210 */
211 public static final int DATA_STATUS_DISABLED_OVERHEAT = 1 << 1;
212
213 /**
214 * USB data is disabled due to contaminated port.
215 */
216 public static final int DATA_STATUS_DISABLED_CONTAMINANT = 1 << 2;
217
218 /**
219 * USB data is disabled due to docking event.
220 */
221 public static final int DATA_STATUS_DISABLED_DOCK = 1 << 3;
222
223 /**
224 * USB data is disabled by
225 * {@link UsbPort#enableUsbData UsbPort.enableUsbData}.
226 */
227 public static final int DATA_STATUS_DISABLED_FORCE = 1 << 4;
228
229 /**
230 * USB data is disabled for debug.
231 */
232 public static final int DATA_STATUS_DISABLED_DEBUG = 1 << 5;
233
234 /**
235 * Unknown whether a power brick is connected.
236 */
237 public static final int POWER_BRICK_STATUS_UNKNOWN = 0;
238
239 /**
240 * The connected device is a power brick.
241 */
242 public static final int POWER_BRICK_STATUS_CONNECTED = 1;
243
244 /**
245 * The connected device is not power brick.
246 */
247 public static final int POWER_BRICK_STATUS_DISCONNECTED = 2;
248
249 @IntDef(prefix = { "CONTAMINANT_DETECTION_" }, value = {
250 CONTAMINANT_DETECTION_NOT_SUPPORTED,
251 CONTAMINANT_DETECTION_DISABLED,
252 CONTAMINANT_DETECTION_NOT_DETECTED,
253 CONTAMINANT_DETECTION_DETECTED,
254 })
255 @Retention(RetentionPolicy.SOURCE)
256 @interface ContaminantDetectionStatus{}
257
258 @IntDef(prefix = { "CONTAMINANT_PROTECTION_" }, flag = true, value = {
259 CONTAMINANT_PROTECTION_NONE,
260 CONTAMINANT_PROTECTION_SINK,
261 CONTAMINANT_PROTECTION_SOURCE,
262 CONTAMINANT_PROTECTION_FORCE_DISABLE,
263 CONTAMINANT_PROTECTION_DISABLED,
264 })
265 @Retention(RetentionPolicy.SOURCE)
266 @interface ContaminantProtectionStatus{}
267
268 @IntDef(prefix = { "MODE_" }, value = {
269 MODE_NONE,
270 MODE_DFP,
271 MODE_UFP,
272 MODE_AUDIO_ACCESSORY,
273 MODE_DEBUG_ACCESSORY,
274 })
275 @Retention(RetentionPolicy.SOURCE)
276 @interface UsbPortMode{}
277
278 /** @hide */
279 @IntDef(prefix = { "DATA_STATUS_" }, flag = true, value = {
280 DATA_STATUS_UNKNOWN,
281 DATA_STATUS_ENABLED,
282 DATA_STATUS_DISABLED_OVERHEAT,
283 DATA_STATUS_DISABLED_CONTAMINANT,
284 DATA_STATUS_DISABLED_DOCK,
285 DATA_STATUS_DISABLED_FORCE,
286 DATA_STATUS_DISABLED_DEBUG
287 })
288 @Retention(RetentionPolicy.SOURCE)
289 @interface UsbDataStatus{}
290
291 /** @hide */
292 @IntDef(prefix = { "POWER_BRICK_STATUS_" }, value = {
293 POWER_BRICK_STATUS_UNKNOWN,
294 POWER_BRICK_STATUS_DISCONNECTED,
295 POWER_BRICK_STATUS_CONNECTED,
296 })
297 @Retention(RetentionPolicy.SOURCE)
298 @interface PowerBrickConnectionStatus{}
299
300 /** @hide */
301 public UsbPortStatus(int currentMode, int currentPowerRole, int currentDataRole,
302 int supportedRoleCombinations, int contaminantProtectionStatus,
303 int contaminantDetectionStatus, @UsbDataStatus int usbDataStatus,
304 boolean powerTransferLimited,
305 @PowerBrickConnectionStatus int powerBrickConnectionStatus) {
306 mCurrentMode = currentMode;
307 mCurrentPowerRole = currentPowerRole;
308 mCurrentDataRole = currentDataRole;
309 mSupportedRoleCombinations = supportedRoleCombinations;
310 mContaminantProtectionStatus = contaminantProtectionStatus;
311 mContaminantDetectionStatus = contaminantDetectionStatus;
312 mUsbDataStatus = usbDataStatus;
313 mPowerTransferLimited = powerTransferLimited;
314 mPowerBrickConnectionStatus = powerBrickConnectionStatus;
315 }
316
317 /** @hide */
318 public UsbPortStatus(int currentMode, int currentPowerRole, int currentDataRole,
319 int supportedRoleCombinations, int contaminantProtectionStatus,
320 int contaminantDetectionStatus) {
321 mCurrentMode = currentMode;
322 mCurrentPowerRole = currentPowerRole;
323 mCurrentDataRole = currentDataRole;
324 mSupportedRoleCombinations = supportedRoleCombinations;
325 mContaminantProtectionStatus = contaminantProtectionStatus;
326 mContaminantDetectionStatus = contaminantDetectionStatus;
327 mUsbDataStatus = DATA_STATUS_UNKNOWN;
328 mPowerBrickConnectionStatus = POWER_BRICK_STATUS_UNKNOWN;
329 mPowerTransferLimited = false;
330 }
331
332 /**
333 * Returns true if there is anything connected to the port.
334 *
335 * @return {@code true} iff there is anything connected to the port.
336 */
337 public boolean isConnected() {
338 return mCurrentMode != 0;
339 }
340
341 /**
342 * Gets the current mode of the port.
343 *
344 * @return The current mode: {@link #MODE_DFP}, {@link #MODE_UFP},
345 * {@link #MODE_AUDIO_ACCESSORY}, {@link #MODE_DEBUG_ACCESSORY}, or {@link {@link #MODE_NONE} if
346 * nothing is connected.
347 */
348 public @UsbPortMode int getCurrentMode() {
349 return mCurrentMode;
350 }
351
352 /**
353 * Gets the current power role of the port.
354 *
355 * @return The current power role: {@link #POWER_ROLE_SOURCE}, {@link #POWER_ROLE_SINK}, or
356 * {@link #POWER_ROLE_NONE} if nothing is connected.
357 */
358 public @UsbPowerRole int getCurrentPowerRole() {
359 return mCurrentPowerRole;
360 }
361
362 /**
363 * Gets the current data role of the port.
364 *
365 * @return The current data role: {@link #DATA_ROLE_HOST}, {@link #DATA_ROLE_DEVICE}, or
366 * {@link #DATA_ROLE_NONE} if nothing is connected.
367 */
368 public @UsbDataRole int getCurrentDataRole() {
369 return mCurrentDataRole;
370 }
371
372 /**
373 * Returns true if the specified power and data role combination is supported
374 * given what is currently connected to the port.
375 *
376 * @param powerRole The power role to check: {@link #POWER_ROLE_SOURCE} or
377 * {@link #POWER_ROLE_SINK}, or {@link #POWER_ROLE_NONE} if no power role.
378 * @param dataRole The data role to check: either {@link #DATA_ROLE_HOST} or
379 * {@link #DATA_ROLE_DEVICE}, or {@link #DATA_ROLE_NONE} if no data role.
380 */
381 public boolean isRoleCombinationSupported(@UsbPowerRole int powerRole,
382 @UsbDataRole int dataRole) {
383 return (mSupportedRoleCombinations &
384 UsbPort.combineRolesAsBit(powerRole, dataRole)) != 0;
385 }
386
387 /**
388 * Get the supported role combinations.
389 */
390 public int getSupportedRoleCombinations() {
391 return mSupportedRoleCombinations;
392 }
393
394 /**
395 * Returns contaminant detection status.
396 *
397 * @hide
398 */
399 public @ContaminantDetectionStatus int getContaminantDetectionStatus() {
400 return mContaminantDetectionStatus;
401 }
402
403 /**
404 * Returns contamiant protection status.
405 *
406 * @hide
407 */
408 public @ContaminantProtectionStatus int getContaminantProtectionStatus() {
409 return mContaminantProtectionStatus;
410 }
411
412 /**
413 * Returns UsbData status.
414 *
415 * @return Current USB data status of the port with one or more of the following values
416 * {@link #DATA_STATUS_UNKNOWN}, {@link #DATA_STATUS_ENABLED},
417 * {@link #DATA_STATUS_DISABLED_OVERHEAT}, {@link #DATA_STATUS_DISABLED_CONTAMINANT},
418 * {@link #DATA_STATUS_DISABLED_DOCK}, {@link #DATA_STATUS_DISABLED_FORCE},
419 * {@link #DATA_STATUS_DISABLED_DEBUG}
420 */
421 public @UsbDataStatus int getUsbDataStatus() {
422 return mUsbDataStatus;
423 }
424
425 /**
426 * Returns whether power transfer is limited.
427 *
428 * @return true when power transfer is limited.
429 * false otherwise.
430 */
431 public boolean isPowerTransferLimited() {
432 return mPowerTransferLimited;
433 }
434
435 /**
436 * Returns the connection status of the power brick.
437 *
438 * @return {@link #POWER_BRICK_STATUS_UNKNOWN}
439 * or {@link #POWER_BRICK_STATUS_CONNECTED}
440 * or {@link #POWER_BRICK_STATUS_DISCONNECTED}
441 */
442 public @PowerBrickConnectionStatus int getPowerBrickConnectionStatus() {
443 return mPowerBrickConnectionStatus;
444 }
445
446 @NonNull
447 @Override
448 public String toString() {
449 return "UsbPortStatus{connected=" + isConnected()
450 + ", currentMode=" + UsbPort.modeToString(mCurrentMode)
451 + ", currentPowerRole=" + UsbPort.powerRoleToString(mCurrentPowerRole)
452 + ", currentDataRole=" + UsbPort.dataRoleToString(mCurrentDataRole)
453 + ", supportedRoleCombinations="
454 + UsbPort.roleCombinationsToString(mSupportedRoleCombinations)
455 + ", contaminantDetectionStatus="
456 + getContaminantDetectionStatus()
457 + ", contaminantProtectionStatus="
458 + getContaminantProtectionStatus()
459 + ", usbDataStatus="
460 + UsbPort.usbDataStatusToString(getUsbDataStatus())
461 + ", isPowerTransferLimited="
462 + isPowerTransferLimited()
463 +", powerBrickConnectionStatus="
464 + UsbPort
465 .powerBrickConnectionStatusToString(getPowerBrickConnectionStatus())
466 + "}";
467 }
468
469 @Override
470 public int describeContents() {
471 return 0;
472 }
473
474 @Override
475 public void writeToParcel(Parcel dest, int flags) {
476 dest.writeInt(mCurrentMode);
477 dest.writeInt(mCurrentPowerRole);
478 dest.writeInt(mCurrentDataRole);
479 dest.writeInt(mSupportedRoleCombinations);
480 dest.writeInt(mContaminantProtectionStatus);
481 dest.writeInt(mContaminantDetectionStatus);
482 dest.writeInt(mUsbDataStatus);
483 dest.writeBoolean(mPowerTransferLimited);
484 dest.writeInt(mPowerBrickConnectionStatus);
485 }
486
487 public static final @NonNull Parcelable.Creator<UsbPortStatus> CREATOR =
488 new Parcelable.Creator<UsbPortStatus>() {
489 @Override
490 public UsbPortStatus createFromParcel(Parcel in) {
491 int currentMode = in.readInt();
492 int currentPowerRole = in.readInt();
493 int currentDataRole = in.readInt();
494 int supportedRoleCombinations = in.readInt();
495 int contaminantProtectionStatus = in.readInt();
496 int contaminantDetectionStatus = in.readInt();
497 int usbDataStatus = in.readInt();
498 boolean powerTransferLimited = in.readBoolean();
499 int powerBrickConnectionStatus = in.readInt();
500 return new UsbPortStatus(currentMode, currentPowerRole, currentDataRole,
501 supportedRoleCombinations, contaminantProtectionStatus,
502 contaminantDetectionStatus, usbDataStatus, powerTransferLimited,
503 powerBrickConnectionStatus);
504 }
505
506 @Override
507 public UsbPortStatus[] newArray(int size) {
508 return new UsbPortStatus[size];
509 }
510 };
511
512 /**
513 * Builder is used to create {@link UsbPortStatus} objects.
514 *
515 * @hide
516 */
517 public static final class Builder {
518 private @UsbPortMode int mCurrentMode;
519 private @UsbPowerRole int mCurrentPowerRole;
520 private @UsbDataRole int mCurrentDataRole;
521 private int mSupportedRoleCombinations;
522 private @ContaminantProtectionStatus int mContaminantProtectionStatus;
523 private @ContaminantDetectionStatus int mContaminantDetectionStatus;
524 private boolean mPowerTransferLimited;
525 private @UsbDataStatus int mUsbDataStatus;
526 private @PowerBrickConnectionStatus int mPowerBrickConnectionStatus;
527
528 public Builder() {
529 mCurrentMode = MODE_NONE;
530 mCurrentPowerRole = POWER_ROLE_NONE;
531 mCurrentDataRole = DATA_ROLE_NONE;
532 mContaminantProtectionStatus = CONTAMINANT_PROTECTION_NONE;
533 mContaminantDetectionStatus = CONTAMINANT_DETECTION_NOT_SUPPORTED;
534 mUsbDataStatus = DATA_STATUS_UNKNOWN;
535 mPowerBrickConnectionStatus = POWER_BRICK_STATUS_UNKNOWN;
536 }
537
538 /**
539 * Sets the current mode of {@link UsbPortStatus}
540 *
541 * @return Instance of {@link Builder}
542 */
543 @NonNull
544 public Builder setCurrentMode(@UsbPortMode int currentMode) {
545 mCurrentMode = currentMode;
546 return this;
547 }
548
549 /**
550 * Sets the current power role and data role of {@link UsbPortStatus}
551 *
552 * @return Instance of {@link Builder}
553 */
554 @NonNull
555 public Builder setCurrentRoles(@UsbPowerRole int currentPowerRole,
556 @UsbDataRole int currentDataRole) {
557 mCurrentPowerRole = currentPowerRole;
558 mCurrentDataRole = currentDataRole;
559 return this;
560 }
561
562 /**
563 * Sets supported role combinations of {@link UsbPortStatus}
564 *
565 * @return Instance of {@link Builder}
566 */
567 @NonNull
568 public Builder setSupportedRoleCombinations(int supportedRoleCombinations) {
569 mSupportedRoleCombinations = supportedRoleCombinations;
570 return this;
571 }
572
573 /**
574 * Sets current contaminant status of {@link UsbPortStatus}
575 *
576 * @return Instance of {@link Builder}
577 */
578 @NonNull
579 public Builder setContaminantStatus(
580 @ContaminantProtectionStatus int contaminantProtectionStatus,
581 @ContaminantDetectionStatus int contaminantDetectionStatus) {
582 mContaminantProtectionStatus = contaminantProtectionStatus;
583 mContaminantDetectionStatus = contaminantDetectionStatus;
584 return this;
585 }
586
587 /**
588 * Sets power limit power transfer of {@link UsbPortStatus}
589 *
590 * @return Instance of {@link Builder}
591 */
592 @NonNull
593 public Builder setPowerTransferLimited(boolean powerTransferLimited) {
594 mPowerTransferLimited = powerTransferLimited;
595 return this;
596 }
597
598 /**
599 * Sets the USB data status of {@link UsbPortStatus}
600 *
601 * @return Instance of {@link Builder}
602 */
603 @NonNull
604 public Builder setUsbDataStatus(@UsbDataStatus int usbDataStatus) {
605 mUsbDataStatus = usbDataStatus;
606 return this;
607 }
608
609 /**
610 * Sets the power brick connection status of {@link UsbPortStatus}
611 *
612 * @return Instance of {@link Builder}
613 */
614 @NonNull
615 public Builder setPowerBrickConnectionStatus(
616 @PowerBrickConnectionStatus int powerBrickConnectionStatus) {
617 mPowerBrickConnectionStatus = powerBrickConnectionStatus;
618 return this;
619 }
620
621 /**
622 * Creates the {@link UsbPortStatus} object.
623 */
624 @NonNull
625 public UsbPortStatus build() {
626 UsbPortStatus status = new UsbPortStatus(mCurrentMode, mCurrentPowerRole,
627 mCurrentDataRole, mSupportedRoleCombinations, mContaminantProtectionStatus,
628 mContaminantDetectionStatus, mUsbDataStatus, mPowerTransferLimited,
629 mPowerBrickConnectionStatus);
630 return status;
631 }
632 };
633 }