HdmiCecMessage.java revision 8e93c84739902f5adaa499b474f39e3c4807bc1c
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 libcore.util.EmptyArray; 20 21import java.util.Arrays; 22 23/** 24 * A class to encapsulate HDMI-CEC message used for the devices connected via 25 * HDMI cable to communicate with one another. A message is defined by its 26 * source and destination address, command (or opcode), and optional parameters. 27 */ 28public final class HdmiCecMessage { 29 public static final byte[] EMPTY_PARAM = EmptyArray.BYTE; 30 31 private static final int MAX_MESSAGE_PARAM_LENGTH = 14; 32 33 private final int mSource; 34 private final int mDestination; 35 36 private final int mOpcode; 37 private final byte[] mParams; 38 39 /** 40 * Constructor. 41 */ 42 public HdmiCecMessage(int source, int destination, int opcode, byte[] params) { 43 mSource = source; 44 mDestination = destination; 45 mOpcode = opcode & 0xFF; 46 47 if (params.length > MAX_MESSAGE_PARAM_LENGTH) { 48 throw new IllegalArgumentException( 49 "Param length should be at most 13 but current param length is " 50 + params.length); 51 } 52 mParams = Arrays.copyOf(params, params.length); 53 } 54 55 /** 56 * Return the source address field of the message. It is the logical address 57 * of the device which generated the message. 58 * 59 * @return source address 60 */ 61 public int getSource() { 62 return mSource; 63 } 64 65 /** 66 * Return the destination address field of the message. It is the logical address 67 * of the device to which the message is sent. 68 * 69 * @return destination address 70 */ 71 public int getDestination() { 72 return mDestination; 73 } 74 75 /** 76 * Return the opcode field of the message. It is the type of the message that 77 * tells the destination device what to do. 78 * 79 * @return opcode 80 */ 81 public int getOpcode() { 82 return mOpcode; 83 } 84 85 /** 86 * Return the parameter field of the message. The contents of parameter varies 87 * from opcode to opcode, and is used together with opcode to describe 88 * the action for the destination device to take. 89 * 90 * @return parameter 91 */ 92 public byte[] getParams() { 93 return mParams; 94 } 95 96 @Override 97 public String toString() { 98 StringBuffer s = new StringBuffer(); 99 s.append(String.format("<%s> src: %d, dst: %d", 100 opcodeToString(mOpcode), mSource, mDestination)); 101 if (mParams.length > 0) { 102 s.append(", params:"); 103 for (byte data : mParams) { 104 s.append(String.format(" %02X", data)); 105 } 106 } 107 return s.toString(); 108 } 109 110 private static String opcodeToString(int opcode) { 111 switch (opcode) { 112 case Constants.MESSAGE_FEATURE_ABORT: 113 return "Feature Abort"; 114 case Constants.MESSAGE_CEC_VERSION: 115 return "CEC Version"; 116 case Constants.MESSAGE_REQUEST_ARC_INITIATION: 117 return "Request ARC Initiation"; 118 case Constants.MESSAGE_REQUEST_ARC_TERMINATION: 119 return "Request ARC Termination"; 120 case Constants.MESSAGE_REPORT_ARC_INITIATED: 121 return "Report ARC Initiated"; 122 case Constants.MESSAGE_REPORT_ARC_TERMINATED: 123 return "Report ARC Terminated"; 124 case Constants.MESSAGE_TEXT_VIEW_ON: 125 return "Text View On"; 126 case Constants.MESSAGE_ACTIVE_SOURCE: 127 return "Active Source"; 128 case Constants.MESSAGE_GIVE_DEVICE_POWER_STATUS: 129 return "Give Device Power Status"; 130 case Constants.MESSAGE_VENDOR_COMMAND: 131 return "Vendor Command"; 132 case Constants.MESSAGE_VENDOR_COMMAND_WITH_ID: 133 return "Vendor Command With ID"; 134 default: 135 return String.format("Opcode: %02X", opcode); 136 } 137 } 138} 139 140