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.hdmi;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.annotation.SystemApi;
22 import android.hardware.hdmi.HdmiControlManager.HdmiCecVersion;
23 import android.os.Parcel;
24 import android.os.Parcelable;
25 
26 /**
27  * A class to encapsulate device information for HDMI devices including CEC and MHL. In terms of
28  * CEC, this container includes basic information such as logical address, physical address and
29  * device type, and additional information like vendor id and osd name. In terms of MHL device, this
30  * container includes adopter id and device type. Otherwise, it keeps the information of other type
31  * devices for which only port ID, physical address are meaningful.
32  *
33  * @hide
34  */
35 @SystemApi
36 public class HdmiDeviceInfo implements Parcelable {
37 
38     /** TV device type. */
39     public static final int DEVICE_TV = 0;
40 
41     /** Recording device type. */
42     public static final int DEVICE_RECORDER = 1;
43 
44     /** Device type reserved for future usage. */
45     public static final int DEVICE_RESERVED = 2;
46 
47     /** Tuner device type. */
48     public static final int DEVICE_TUNER = 3;
49 
50     /** Playback device type. */
51     public static final int DEVICE_PLAYBACK = 4;
52 
53     /** Audio system device type. */
54     public static final int DEVICE_AUDIO_SYSTEM = 5;
55 
56     /** @hide Pure CEC switch device type. */
57     public static final int DEVICE_PURE_CEC_SWITCH = 6;
58 
59     /** @hide Video processor device type. */
60     public static final int DEVICE_VIDEO_PROCESSOR = 7;
61 
62     // Value indicating the device is not an active source.
63     public static final int DEVICE_INACTIVE = -1;
64 
65     /**
66      * Logical address used to indicate the source comes from internal device. The logical address
67      * of TV(0) is used.
68      */
69     public static final int ADDR_INTERNAL = 0;
70 
71     /**
72      * Physical address used to indicate the source comes from internal device. The physical address
73      * of TV(0) is used.
74      */
75     public static final int PATH_INTERNAL = 0x0000;
76 
77     /** Invalid physical address (routing path) */
78     public static final int PATH_INVALID = 0xFFFF;
79 
80     /** Invalid port ID */
81     public static final int PORT_INVALID = -1;
82 
83     /** Invalid device ID */
84     public static final int ID_INVALID = 0xFFFF;
85 
86     /** Device info used to indicate an inactivated device. */
87     public static final HdmiDeviceInfo INACTIVE_DEVICE = new HdmiDeviceInfo();
88 
89     private static final int HDMI_DEVICE_TYPE_CEC = 0;
90     private static final int HDMI_DEVICE_TYPE_MHL = 1;
91     private static final int HDMI_DEVICE_TYPE_HARDWARE = 2;
92 
93     // Type used to indicate the device that has relinquished its active source status.
94     private static final int HDMI_DEVICE_TYPE_INACTIVE = 100;
95 
96     // Offset used for id value. MHL devices, for instance, will be assigned the value from
97     // ID_OFFSET_MHL.
98     private static final int ID_OFFSET_CEC = 0x0;
99     private static final int ID_OFFSET_MHL = 0x80;
100     private static final int ID_OFFSET_HARDWARE = 0xC0;
101 
102     // Common parameters for all device.
103     private final int mId;
104     private final int mHdmiDeviceType;
105     private final int mPhysicalAddress;
106     private final int mPortId;
107 
108     // CEC only parameters.
109     private final int mLogicalAddress;
110     private final int mDeviceType;
111     @HdmiCecVersion
112     private final int mHdmiCecVersion;
113     private final int mVendorId;
114     private final String mDisplayName;
115     private final int mDevicePowerStatus;
116 
117     // MHL only parameters.
118     private final int mDeviceId;
119     private final int mAdopterId;
120 
121     /**
122      * A helper class to deserialize {@link HdmiDeviceInfo} for a parcel.
123      */
124     public static final @android.annotation.NonNull Parcelable.Creator<HdmiDeviceInfo> CREATOR =
125             new Parcelable.Creator<HdmiDeviceInfo>() {
126                 @Override
127                 public HdmiDeviceInfo createFromParcel(Parcel source) {
128                     int hdmiDeviceType = source.readInt();
129                     int physicalAddress = source.readInt();
130                     int portId = source.readInt();
131 
132                     switch (hdmiDeviceType) {
133                         case HDMI_DEVICE_TYPE_CEC:
134                             int logicalAddress = source.readInt();
135                             int deviceType = source.readInt();
136                             int vendorId = source.readInt();
137                             int powerStatus = source.readInt();
138                             String displayName = source.readString();
139                             int cecVersion = source.readInt();
140                             return new HdmiDeviceInfo(logicalAddress, physicalAddress, portId,
141                                     deviceType, vendorId, displayName, powerStatus, cecVersion);
142                         case HDMI_DEVICE_TYPE_MHL:
143                             int deviceId = source.readInt();
144                             int adopterId = source.readInt();
145                             return new HdmiDeviceInfo(physicalAddress, portId, adopterId, deviceId);
146                         case HDMI_DEVICE_TYPE_HARDWARE:
147                             return new HdmiDeviceInfo(physicalAddress, portId);
148                         case HDMI_DEVICE_TYPE_INACTIVE:
149                             return HdmiDeviceInfo.INACTIVE_DEVICE;
150                         default:
151                             return null;
152                     }
153                 }
154 
155                 @Override
156                 public HdmiDeviceInfo[] newArray(int size) {
157                     return new HdmiDeviceInfo[size];
158                 }
159             };
160 
161     /**
162      * Constructor. Used to initialize the instance for CEC device.
163      *
164      * @param logicalAddress logical address of HDMI-CEC device
165      * @param physicalAddress physical address of HDMI-CEC device
166      * @param portId HDMI port ID (1 for HDMI1)
167      * @param deviceType type of device
168      * @param vendorId vendor id of device. Used for vendor specific command.
169      * @param displayName name of device
170      * @param powerStatus device power status
171      * @hide
172      */
HdmiDeviceInfo(int logicalAddress, int physicalAddress, int portId, int deviceType, int vendorId, String displayName, int powerStatus, int hdmiCecVersion)173     public HdmiDeviceInfo(int logicalAddress, int physicalAddress, int portId, int deviceType,
174             int vendorId, String displayName, int powerStatus, int hdmiCecVersion) {
175         mHdmiDeviceType = HDMI_DEVICE_TYPE_CEC;
176         mPhysicalAddress = physicalAddress;
177         mPortId = portId;
178 
179         mId = idForCecDevice(logicalAddress);
180         mLogicalAddress = logicalAddress;
181         mDeviceType = deviceType;
182         mHdmiCecVersion = hdmiCecVersion;
183         mVendorId = vendorId;
184         mDevicePowerStatus = powerStatus;
185         mDisplayName = displayName;
186 
187         mDeviceId = -1;
188         mAdopterId = -1;
189     }
190 
191     /**
192      * Constructor. Used to initialize the instance for CEC device.
193      *
194      * @param logicalAddress logical address of HDMI-CEC device
195      * @param physicalAddress physical address of HDMI-CEC device
196      * @param portId HDMI port ID (1 for HDMI1)
197      * @param deviceType type of device
198      * @param vendorId vendor id of device. Used for vendor specific command.
199      * @param displayName name of device
200      * @param powerStatus device power status
201      * @hide
202      */
HdmiDeviceInfo(int logicalAddress, int physicalAddress, int portId, int deviceType, int vendorId, String displayName, int powerStatus)203     public HdmiDeviceInfo(int logicalAddress, int physicalAddress, int portId, int deviceType,
204             int vendorId, String displayName, int powerStatus) {
205         this(logicalAddress, physicalAddress, portId, deviceType,
206                 vendorId, displayName, powerStatus, HdmiControlManager.HDMI_CEC_VERSION_1_4_B);
207     }
208 
209     /**
210      * Constructor. Used to initialize the instance for CEC device.
211      *
212      * @param logicalAddress  logical address of HDMI-CEC device
213      * @param physicalAddress physical address of HDMI-CEC device
214      * @param portId          HDMI port ID (1 for HDMI1)
215      * @param deviceType      type of device
216      * @param vendorId        vendor id of device. Used for vendor specific command.
217      * @param displayName     name of device
218      * @hide
219      */
HdmiDeviceInfo(int logicalAddress, int physicalAddress, int portId, int deviceType, int vendorId, String displayName)220     public HdmiDeviceInfo(int logicalAddress, int physicalAddress, int portId, int deviceType,
221             int vendorId, String displayName) {
222         this(logicalAddress, physicalAddress, portId, deviceType,
223                 vendorId, displayName, HdmiControlManager.POWER_STATUS_UNKNOWN,
224                 HdmiControlManager.HDMI_CEC_VERSION_1_4_B);
225     }
226 
227     /**
228      * Constructor. Used to initialize the instance for device representing hardware port.
229      *
230      * @param physicalAddress physical address of the port
231      * @param portId HDMI port ID (1 for HDMI1)
232      * @hide
233      */
HdmiDeviceInfo(int physicalAddress, int portId)234     public HdmiDeviceInfo(int physicalAddress, int portId) {
235         mHdmiDeviceType = HDMI_DEVICE_TYPE_HARDWARE;
236         mPhysicalAddress = physicalAddress;
237         mPortId = portId;
238 
239         mId = idForHardware(portId);
240         mLogicalAddress = -1;
241         mDeviceType = DEVICE_RESERVED;
242         mHdmiCecVersion = HdmiControlManager.HDMI_CEC_VERSION_1_4_B;
243         mVendorId = 0;
244         mDevicePowerStatus = HdmiControlManager.POWER_STATUS_UNKNOWN;
245         mDisplayName = "HDMI" + portId;
246 
247         mDeviceId = -1;
248         mAdopterId = -1;
249     }
250 
251     /**
252      * Constructor. Used to initialize the instance for MHL device.
253      *
254      * @param physicalAddress physical address of HDMI device
255      * @param portId portId HDMI port ID (1 for HDMI1)
256      * @param adopterId adopter id of MHL
257      * @param deviceId device id of MHL
258      * @hide
259      */
HdmiDeviceInfo(int physicalAddress, int portId, int adopterId, int deviceId)260     public HdmiDeviceInfo(int physicalAddress, int portId, int adopterId, int deviceId) {
261         mHdmiDeviceType = HDMI_DEVICE_TYPE_MHL;
262         mPhysicalAddress = physicalAddress;
263         mPortId = portId;
264 
265         mId = idForMhlDevice(portId);
266         mLogicalAddress = -1;
267         mDeviceType = DEVICE_RESERVED;
268         mHdmiCecVersion = HdmiControlManager.HDMI_CEC_VERSION_1_4_B;
269         mVendorId = 0;
270         mDevicePowerStatus = HdmiControlManager.POWER_STATUS_UNKNOWN;
271         mDisplayName = "Mobile";
272 
273         mDeviceId = adopterId;
274         mAdopterId = deviceId;
275     }
276 
277     /**
278      * Constructor. Used to initialize the instance representing an inactivated device.
279      * Can be passed input change listener to indicate the active source yielded
280      * its status, hence the listener should take an appropriate action such as
281      * switching to other input.
282      */
HdmiDeviceInfo()283     public HdmiDeviceInfo() {
284         mHdmiDeviceType = HDMI_DEVICE_TYPE_INACTIVE;
285         mPhysicalAddress = PATH_INVALID;
286         mId = ID_INVALID;
287 
288         mLogicalAddress = -1;
289         mDeviceType = DEVICE_INACTIVE;
290         mHdmiCecVersion = HdmiControlManager.HDMI_CEC_VERSION_1_4_B;
291         mPortId = PORT_INVALID;
292         mDevicePowerStatus = HdmiControlManager.POWER_STATUS_UNKNOWN;
293         mDisplayName = "Inactive";
294         mVendorId = 0;
295 
296         mDeviceId = -1;
297         mAdopterId = -1;
298     }
299 
300     /**
301      * Returns the id of the device.
302      */
getId()303     public int getId() {
304         return mId;
305     }
306 
307     /**
308      * Returns the id to be used for CEC device.
309      *
310      * @param address logical address of CEC device
311      * @return id for CEC device
312      */
idForCecDevice(int address)313     public static int idForCecDevice(int address) {
314         // The id is generated based on the logical address.
315         return ID_OFFSET_CEC + address;
316     }
317 
318     /**
319      * Returns the id to be used for MHL device.
320      *
321      * @param portId port which the MHL device is connected to
322      * @return id for MHL device
323      */
idForMhlDevice(int portId)324     public static int idForMhlDevice(int portId) {
325         // The id is generated based on the port id since there can be only one MHL device per port.
326         return ID_OFFSET_MHL + portId;
327     }
328 
329     /**
330      * Returns the id to be used for hardware port.
331      *
332      * @param portId port id
333      * @return id for hardware port
334      */
idForHardware(int portId)335     public static int idForHardware(int portId) {
336         return ID_OFFSET_HARDWARE + portId;
337     }
338 
339     /**
340      * Returns the CEC logical address of the device.
341      */
getLogicalAddress()342     public int getLogicalAddress() {
343         return mLogicalAddress;
344     }
345 
346     /**
347      * Returns the physical address of the device.
348      */
getPhysicalAddress()349     public int getPhysicalAddress() {
350         return mPhysicalAddress;
351     }
352 
353     /**
354      * Returns the port ID.
355      */
getPortId()356     public int getPortId() {
357         return mPortId;
358     }
359 
360     /**
361      * Returns CEC type of the device. For more details, refer constants between {@link #DEVICE_TV}
362      * and {@link #DEVICE_INACTIVE}.
363      */
getDeviceType()364     public int getDeviceType() {
365         return mDeviceType;
366     }
367 
368     /**
369      * Returns the CEC version the device supports.
370      *
371      * @hide
372      */
373     @HdmiCecVersion
getCecVersion()374     public int getCecVersion() {
375         return mHdmiCecVersion;
376     }
377 
378     /**
379      * Returns device's power status. It should be one of the following values.
380      * <ul>
381      * <li>{@link HdmiControlManager#POWER_STATUS_ON}
382      * <li>{@link HdmiControlManager#POWER_STATUS_STANDBY}
383      * <li>{@link HdmiControlManager#POWER_STATUS_TRANSIENT_TO_ON}
384      * <li>{@link HdmiControlManager#POWER_STATUS_TRANSIENT_TO_STANDBY}
385      * <li>{@link HdmiControlManager#POWER_STATUS_UNKNOWN}
386      * </ul>
387      */
getDevicePowerStatus()388     public int getDevicePowerStatus() {
389         return mDevicePowerStatus;
390     }
391 
392     /**
393      * Returns MHL device id. Return -1 for non-MHL device.
394      */
getDeviceId()395     public int getDeviceId() {
396         return mDeviceId;
397     }
398 
399     /**
400      * Returns MHL adopter id. Return -1 for non-MHL device.
401      */
getAdopterId()402     public int getAdopterId() {
403         return mAdopterId;
404     }
405 
406     /**
407      * Returns {@code true} if the device is of a type that can be an input source.
408      */
isSourceType()409     public boolean isSourceType() {
410         if (isCecDevice()) {
411             return mDeviceType == DEVICE_PLAYBACK
412                     || mDeviceType == DEVICE_RECORDER
413                     || mDeviceType == DEVICE_TUNER;
414         } else if (isMhlDevice()) {
415             return true;
416         } else {
417             return false;
418         }
419     }
420 
421     /**
422      * Returns {@code true} if the device represents an HDMI-CEC device. {@code false} if the device
423      * is either MHL or other device.
424      */
isCecDevice()425     public boolean isCecDevice() {
426         return mHdmiDeviceType == HDMI_DEVICE_TYPE_CEC;
427     }
428 
429     /**
430      * Returns {@code true} if the device represents an MHL device. {@code false} if the device is
431      * either CEC or other device.
432      */
isMhlDevice()433     public boolean isMhlDevice() {
434         return mHdmiDeviceType == HDMI_DEVICE_TYPE_MHL;
435     }
436 
437     /**
438      * Return {@code true} if the device represents an inactivated device that relinquishes
439      * its status as active source by &lt;Active Source&gt; (HDMI-CEC) or Content-off (MHL).
440      */
isInactivated()441     public boolean isInactivated() {
442         return mHdmiDeviceType == HDMI_DEVICE_TYPE_INACTIVE;
443     }
444 
445     /**
446      * Returns display (OSD) name of the device.
447      */
getDisplayName()448     public String getDisplayName() {
449         return mDisplayName;
450     }
451 
452     /**
453      * Returns vendor id of the device. Vendor id is used to distinguish devices built by other
454      * manufactures. This is required for vendor-specific command on CEC standard.
455      */
getVendorId()456     public int getVendorId() {
457         return mVendorId;
458     }
459 
460     /**
461      * Describes the kinds of special objects contained in this Parcelable's marshalled
462      * representation.
463      */
464     @Override
describeContents()465     public int describeContents() {
466         return 0;
467     }
468 
469     /**
470      * Serializes this object into a {@link Parcel}.
471      *
472      * @param dest The Parcel in which the object should be written.
473      * @param flags Additional flags about how the object should be written. May be 0 or
474      *            {@link Parcelable#PARCELABLE_WRITE_RETURN_VALUE}.
475      */
476     @Override
writeToParcel(Parcel dest, int flags)477     public void writeToParcel(Parcel dest, int flags) {
478         dest.writeInt(mHdmiDeviceType);
479         dest.writeInt(mPhysicalAddress);
480         dest.writeInt(mPortId);
481         switch (mHdmiDeviceType) {
482             case HDMI_DEVICE_TYPE_CEC:
483                 dest.writeInt(mLogicalAddress);
484                 dest.writeInt(mDeviceType);
485                 dest.writeInt(mVendorId);
486                 dest.writeInt(mDevicePowerStatus);
487                 dest.writeString(mDisplayName);
488                 dest.writeInt(mHdmiCecVersion);
489                 break;
490             case HDMI_DEVICE_TYPE_MHL:
491                 dest.writeInt(mDeviceId);
492                 dest.writeInt(mAdopterId);
493                 break;
494             case HDMI_DEVICE_TYPE_INACTIVE:
495                 // flow through
496             default:
497                 // no-op
498         }
499     }
500 
501     @NonNull
502     @Override
toString()503     public String toString() {
504         StringBuilder s = new StringBuilder();
505         switch (mHdmiDeviceType) {
506             case HDMI_DEVICE_TYPE_CEC:
507                 s.append("CEC: ");
508                 s.append("logical_address: ").append(String.format("0x%02X", mLogicalAddress));
509                 s.append(" ");
510                 s.append("device_type: ").append(mDeviceType).append(" ");
511                 s.append("cec_version: ").append(mHdmiCecVersion).append(" ");
512                 s.append("vendor_id: ").append(mVendorId).append(" ");
513                 s.append("display_name: ").append(mDisplayName).append(" ");
514                 s.append("power_status: ").append(mDevicePowerStatus).append(" ");
515                 break;
516             case HDMI_DEVICE_TYPE_MHL:
517                 s.append("MHL: ");
518                 s.append("device_id: ").append(String.format("0x%04X", mDeviceId)).append(" ");
519                 s.append("adopter_id: ").append(String.format("0x%04X", mAdopterId)).append(" ");
520                 break;
521 
522             case HDMI_DEVICE_TYPE_HARDWARE:
523                 s.append("Hardware: ");
524                 break;
525             case HDMI_DEVICE_TYPE_INACTIVE:
526                 s.append("Inactivated: ");
527                 break;
528             default:
529                 return "";
530         }
531         s.append("physical_address: ").append(String.format("0x%04X", mPhysicalAddress));
532         s.append(" ");
533         s.append("port_id: ").append(mPortId);
534         return s.toString();
535     }
536 
537     @Override
equals(@ullable Object obj)538     public boolean equals(@Nullable Object obj) {
539         if (!(obj instanceof HdmiDeviceInfo)) {
540             return false;
541         }
542 
543         HdmiDeviceInfo other = (HdmiDeviceInfo) obj;
544         return mHdmiDeviceType == other.mHdmiDeviceType
545                 && mPhysicalAddress == other.mPhysicalAddress
546                 && mPortId == other.mPortId
547                 && mLogicalAddress == other.mLogicalAddress
548                 && mDeviceType == other.mDeviceType
549                 && mHdmiCecVersion == other.mHdmiCecVersion
550                 && mVendorId == other.mVendorId
551                 && mDevicePowerStatus == other.mDevicePowerStatus
552                 && mDisplayName.equals(other.mDisplayName)
553                 && mDeviceId == other.mDeviceId
554                 && mAdopterId == other.mAdopterId;
555     }
556 
557     @Override
hashCode()558     public int hashCode() {
559         return java.util.Objects.hash(
560                 mHdmiDeviceType,
561                 mPhysicalAddress,
562                 mPortId,
563                 mLogicalAddress,
564                 mDeviceType,
565                 mHdmiCecVersion,
566                 mVendorId,
567                 mDevicePowerStatus,
568                 mDisplayName,
569                 mDeviceId,
570                 mAdopterId);
571     }
572 }
573