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