HdmiCecMessage.java revision 5f75cbd8593e83eaf17cfac07186a3b6a7b7b1f1
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 final int mSource;
32    private final int mDestination;
33
34    private final int mOpcode;
35    private final byte[] mParams;
36
37    /**
38     * Constructor.
39     */
40    public HdmiCecMessage(int source, int destination, int opcode, byte[] params) {
41        mSource = source;
42        mDestination = destination;
43        mOpcode = opcode & 0xFF;
44        mParams = Arrays.copyOf(params, params.length);
45    }
46
47    /**
48     * Return the source address field of the message. It is the logical address
49     * of the device which generated the message.
50     *
51     * @return source address
52     */
53    public int getSource() {
54        return mSource;
55    }
56
57    /**
58     * Return the destination address field of the message. It is the logical address
59     * of the device to which the message is sent.
60     *
61     * @return destination address
62     */
63    public int getDestination() {
64        return mDestination;
65    }
66
67    /**
68     * Return the opcode field of the message. It is the type of the message that
69     * tells the destination device what to do.
70     *
71     * @return opcode
72     */
73    public int getOpcode() {
74        return mOpcode;
75    }
76
77    /**
78     * Return the parameter field of the message. The contents of parameter varies
79     * from opcode to opcode, and is used together with opcode to describe
80     * the action for the destination device to take.
81     *
82     * @return parameter
83     */
84    public byte[] getParams() {
85        return mParams;
86    }
87
88    @Override
89    public String toString() {
90        StringBuffer s = new StringBuffer();
91        s.append(String.format("<%s> src: %d, dst: %d",
92                opcodeToString(mOpcode), mSource, mDestination));
93        if (mParams.length > 0) {
94            s.append(", params:");
95            for (byte data : mParams) {
96                s.append(String.format(" %02X", data));
97            }
98        }
99        return s.toString();
100    }
101
102    private static String opcodeToString(int opcode) {
103        switch (opcode) {
104            case Constants.MESSAGE_FEATURE_ABORT:
105                return "Feature Abort";
106            case Constants.MESSAGE_CEC_VERSION:
107                return "CEC Version";
108            case Constants.MESSAGE_REQUEST_ARC_INITIATION:
109                return "Request ARC Initiation";
110            case Constants.MESSAGE_REQUEST_ARC_TERMINATION:
111                return "Request ARC Termination";
112            case Constants.MESSAGE_REPORT_ARC_INITIATED:
113                return "Report ARC Initiated";
114            case Constants.MESSAGE_REPORT_ARC_TERMINATED:
115                return "Report ARC Terminated";
116            case Constants.MESSAGE_TEXT_VIEW_ON:
117                return "Text View On";
118            case Constants.MESSAGE_ACTIVE_SOURCE:
119                return "Active Source";
120            case Constants.MESSAGE_GIVE_DEVICE_POWER_STATUS:
121                return "Give Device Power Status";
122            case Constants.MESSAGE_VENDOR_COMMAND:
123                return "Vendor Command";
124            case Constants.MESSAGE_VENDOR_COMMAND_WITH_ID:
125                return "Vendor Command With ID";
126            default:
127                return String.format("Opcode: %02X", opcode);
128        }
129    }
130}
131
132