HdmiUtils.java revision 79c58a4b97f27ede6a1b680d2fece9c2a0edf7b7
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;
22import android.util.SparseArray;
23
24import java.util.ArrayList;
25import java.util.Collections;
26import java.util.List;
27
28/**
29 * Various utilities to handle HDMI CEC messages.
30 */
31final class HdmiUtils {
32
33    private HdmiUtils() { /* cannot be instantiated */ }
34
35    /**
36     * Verify if the given address is for the given device type.  If not it will throw
37     * {@link IllegalArgumentException}.
38     *
39     * @param logicalAddress the logical address to verify
40     * @param deviceType the device type to check
41     * @throw IllegalArgumentException
42     */
43    static void verifyAddressType(int logicalAddress, int deviceType) {
44        int actualDeviceType = HdmiCec.getTypeFromAddress(logicalAddress);
45        if (actualDeviceType != deviceType) {
46            throw new IllegalArgumentException("Device type missmatch:[Expected:" + deviceType
47                    + ", Actual:" + actualDeviceType);
48        }
49    }
50
51    /**
52     * Check if the given CEC message come from the given address.
53     *
54     * @param cmd the CEC message to check
55     * @param expectedAddress the expected source address of the given message
56     * @param tag the tag of caller module (for log message)
57     * @return true if the CEC message comes from the given address
58     */
59    static boolean checkCommandSource(HdmiCecMessage cmd, int expectedAddress, String tag) {
60        int src = cmd.getSource();
61        if (src != expectedAddress) {
62            Slog.w(tag, "Invalid source [Expected:" + expectedAddress + ", Actual:" + src + "]");
63            return false;
64        }
65        return true;
66    }
67
68    /**
69     * Parse the parameter block of CEC message as [System Audio Status].
70     *
71     * @param cmd the CEC message to parse
72     * @return true if the given parameter has [ON] value
73     */
74    static boolean parseCommandParamSystemAudioStatus(HdmiCecMessage cmd) {
75        // TODO: Handle the exception when the length is wrong.
76        return cmd.getParams().length > 0
77                && cmd.getParams()[0] == HdmiConstants.SYSTEM_AUDIO_STATUS_ON;
78    }
79
80    /**
81     * Convert integer array to list of {@link Integer}.
82     *
83     * <p>The result is immutable.
84     *
85     * @param is integer array
86     * @return {@link List} instance containing the elements in the given array
87     */
88    static List<Integer> asImmutableList(final int[] is) {
89        ArrayList<Integer> list = new ArrayList<>(is.length);
90        for (int type : is) {
91            list.add(type);
92        }
93        return Collections.unmodifiableList(list);
94    }
95
96    /**
97     * Assemble two bytes into single integer value.
98     *
99     * @param data to be assembled
100     * @return assembled value
101     */
102    static int twoBytesToInt(byte[] data) {
103        return ((data[0] & 0xFF) << 8) | (data[1] & 0xFF);
104    }
105
106    /**
107     * Assemble three bytes into single integer value.
108     *
109     * @param data to be assembled
110     * @return assembled value
111     */
112    static int threeBytesToInt(byte[] data) {
113        return ((data[0] & 0xFF) << 16) | ((data[1] & 0xFF) << 8) | (data[2] & 0xFF);
114    }
115
116    static <T> List<T> sparseArrayToList(SparseArray<T> array) {
117        ArrayList<T> list = new ArrayList<>();
118        for (int i = 0; i < array.size(); ++i) {
119            list.add(array.valueAt(i));
120        }
121        return list;
122    }
123
124}
125