HdmiUtils.java revision 092b445ef898e3c1e5b2918b554480940f0f5a28
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
17package com.android.server.hdmi;
18
19import android.hardware.hdmi.HdmiCec;
20import android.hardware.hdmi.HdmiCecMessage;
21import android.util.Slog;
22
23/**
24 * Various utilities to handle HDMI CEC messages.
25 */
26final class HdmiUtils {
27
28    private HdmiUtils() { /* cannot be instantiated */ }
29
30    /**
31     * Verify if the given address is for the given device type.  If not it will throw
32     * {@link IllegalArgumentException}.
33     *
34     * @param logicalAddress the logical address to verify
35     * @param deviceType the device type to check
36     * @throw IllegalArgumentException
37     */
38    static void verifyAddressType(int logicalAddress, int deviceType) {
39        int actualDeviceType = HdmiCec.getTypeFromAddress(logicalAddress);
40        if (actualDeviceType != deviceType) {
41            throw new IllegalArgumentException("Device type missmatch:[Expected:" + deviceType
42                    + ", Actual:" + actualDeviceType);
43        }
44    }
45
46    /**
47     * Check if the given CEC message come from the given address.
48     *
49     * @param cmd the CEC message to check
50     * @param expectedAddress the expected source address of the given message
51     * @param tag the tag of caller module (for log message)
52     * @return true if the CEC message comes from the given address
53     */
54    static boolean checkCommandSource(HdmiCecMessage cmd, int expectedAddress, String tag) {
55        int src = cmd.getSource();
56        if (src != expectedAddress) {
57            Slog.w(tag, "Invalid source [Expected:" + expectedAddress + ", Actual:" + src + "]");
58            return false;
59        }
60        return true;
61    }
62
63    /**
64     * Parse the parameter block of CEC message as [System Audio Status].
65     *
66     * @param cmd the CEC message to parse
67     * @return true if the given parameter has [ON] value
68     */
69    static boolean parseCommandParamSystemAudioStatus(HdmiCecMessage cmd) {
70        // TODO: Handle the exception when the length is wrong.
71        return cmd.getParams().length > 0
72                && cmd.getParams()[0] == HdmiConstants.SYSTEM_AUDIO_STATUS_ON;
73    }
74
75    /**
76     * Assemble two bytes into single integer value.
77     *
78     * @param data to be assembled
79     * @return assembled value
80     */
81    static int twoBytesToInt(byte[] data) {
82        return ((data[0] & 0xFF) << 8) | (data[1] & 0xFF);
83    }
84
85    /**
86     * Assemble three bytes into single integer value.
87     *
88     * @param data to be assembled
89     * @return assembled value
90     */
91    static int threeBytesToInt(byte[] data) {
92        return ((data[0] & 0xFF) << 16) | ((data[1] & 0xFF) << 8) | (data[2] & 0xFF);
93    }
94}
95