NewDeviceAction.java revision 92b77cf9cbf512e7141cad6fef5a38d0682dde43
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 */
16package com.android.server.hdmi;
17
18import android.hardware.hdmi.HdmiCec;
19import android.hardware.hdmi.HdmiCecDeviceInfo;
20import android.hardware.hdmi.HdmiCecMessage;
21import android.util.Slog;
22
23import java.io.UnsupportedEncodingException;
24
25/**
26 * Feature action that discovers the information of a newly found logical device.
27 *
28 * This action is created when receiving <Report Physical Address>, a CEC command a newly
29 * connected HDMI-CEC device broadcasts to announce its advent. Additional commands are issued in
30 * this action to gather more information on the device such as OSD name and device vendor ID.
31 *
32 * <p>The result is made in the form of {@link HdmiCecDeviceInfo} object, and passed to service
33 * for the management through its life cycle.
34 *
35 * <p>Package-private, accessed by {@link HdmiControlService} only.
36 */
37final class NewDeviceAction extends FeatureAction {
38
39    private static final String TAG = "NewDeviceAction";
40
41    // State in which the action sent <Give OSD Name> and is waiting for <Set OSD Name>
42    // that contains the name of the device for display on screen.
43    static final int STATE_WAITING_FOR_SET_OSD_NAME = 1;
44
45    // State in which the action sent <Give Device Vendor ID> and is waiting for
46    // <Device Vendor ID> that contains the vendor ID of the device.
47    static final int STATE_WAITING_FOR_DEVICE_VENDOR_ID = 2;
48
49    private final int mDeviceLogicalAddress;
50    private final int mDevicePhysicalAddress;
51
52    private int mVendorId;
53    private String mDisplayName;
54
55    /**
56     * Constructor.
57     *
58     * @param source {@link HdmiCecLocalDevice} instance
59     * @param deviceLogicalAddress logical address of the device in interest
60     * @param devicePhysicalAddress physical address of the device in interest
61     * @param requireRoutingChange whether to initiate routing change or not
62     */
63    NewDeviceAction(HdmiCecLocalDevice source, int deviceLogicalAddress,
64            int devicePhysicalAddress) {
65        super(source);
66        mDeviceLogicalAddress = deviceLogicalAddress;
67        mDevicePhysicalAddress = devicePhysicalAddress;
68        mVendorId = HdmiCec.UNKNOWN_VENDOR_ID;
69    }
70
71    @Override
72    public boolean start() {
73        if (HdmiCec.getTypeFromAddress(getSourceAddress()) == HdmiCec.DEVICE_AUDIO_SYSTEM) {
74            if (tv().getAvrDeviceInfo() == null) {
75                // TODO: Start system audio initiation action
76            }
77
78            // If new device is connected through ARC enabled port,
79            // initiates ARC channel establishment.
80            if (tv().isConnectedToArcPort(mDevicePhysicalAddress)) {
81                addAndStartAction(new RequestArcInitiationAction(localDevice(),
82                                mDeviceLogicalAddress));
83            }
84        }
85
86        mState = STATE_WAITING_FOR_SET_OSD_NAME;
87        if (mayProcessCommandIfCached(mDeviceLogicalAddress, HdmiCec.MESSAGE_SET_OSD_NAME)) {
88            return true;
89        }
90
91        sendCommand(HdmiCecMessageBuilder.buildGiveOsdNameCommand(getSourceAddress(),
92                mDeviceLogicalAddress));
93        addTimer(mState, TIMEOUT_MS);
94        return true;
95    }
96
97    @Override
98    public boolean processCommand(HdmiCecMessage cmd) {
99        // For the logical device in interest, we want two more pieces of information -
100        // osd name and vendor id. They are requested in sequence. In case we don't
101        // get the expected responses (either by timeout or by receiving <feature abort> command),
102        // set them to a default osd name and unknown vendor id respectively.
103        int opcode = cmd.getOpcode();
104        int src = cmd.getSource();
105        byte[] params = cmd.getParams();
106
107        if (mDeviceLogicalAddress != src) {
108            return false;
109        }
110
111        if (mState == STATE_WAITING_FOR_SET_OSD_NAME) {
112            if (opcode == HdmiCec.MESSAGE_SET_OSD_NAME) {
113                try {
114                    mDisplayName = new String(params, "US-ASCII");
115                } catch (UnsupportedEncodingException e) {
116                    Slog.e(TAG, "Failed to get OSD name: " + e.getMessage());
117                }
118                requestVendorId();
119                return true;
120            } else if (opcode == HdmiCec.MESSAGE_FEATURE_ABORT) {
121                int requestOpcode = params[1] & 0xFF;
122                if (requestOpcode == HdmiCec.MESSAGE_SET_OSD_NAME) {
123                    requestVendorId();
124                    return true;
125                }
126            }
127        } else if (mState == STATE_WAITING_FOR_DEVICE_VENDOR_ID) {
128            if (opcode == HdmiCec.MESSAGE_DEVICE_VENDOR_ID) {
129                if (params.length == 3) {
130                    mVendorId = HdmiUtils.threeBytesToInt(params);
131                } else {
132                    Slog.e(TAG, "Failed to get device vendor ID: ");
133                }
134                addDeviceInfo();
135                finish();
136                return true;
137            } else if (opcode == HdmiCec.MESSAGE_FEATURE_ABORT) {
138                int requestOpcode = params[1] & 0xFF;
139                if (requestOpcode == HdmiCec.MESSAGE_DEVICE_VENDOR_ID) {
140                    addDeviceInfo();
141                    finish();
142                    return true;
143                }
144            }
145        }
146        return false;
147    }
148
149    private boolean mayProcessCommandIfCached(int destAddress, int opcode) {
150        HdmiCecMessage message = getCecMessageCache().getMessage(destAddress, opcode);
151        if (message != null) {
152            return processCommand(message);
153        }
154        return false;
155    }
156
157    private void requestVendorId() {
158        // At first, transit to waiting status for <Device Vendor Id>.
159        mState = STATE_WAITING_FOR_DEVICE_VENDOR_ID;
160        // If the message is already in cache, process it.
161        if (mayProcessCommandIfCached(mDeviceLogicalAddress, HdmiCec.MESSAGE_DEVICE_VENDOR_ID)) {
162            return;
163        }
164        sendCommand(HdmiCecMessageBuilder.buildGiveDeviceVendorIdCommand(getSourceAddress(),
165                mDeviceLogicalAddress));
166        addTimer(mState, TIMEOUT_MS);
167    }
168
169    private void addDeviceInfo() {
170        if (mDisplayName == null) {
171            mDisplayName = HdmiCec.getDefaultDeviceName(mDeviceLogicalAddress);
172        }
173        tv().addCecDevice(new HdmiCecDeviceInfo(
174                mDeviceLogicalAddress, mDevicePhysicalAddress,
175                HdmiCec.getTypeFromAddress(mDeviceLogicalAddress),
176                mVendorId, mDisplayName));
177    }
178
179    @Override
180    public void handleTimerEvent(int state) {
181        if (mState == STATE_NONE || mState != state) {
182            return;
183        }
184        if (state == STATE_WAITING_FOR_SET_OSD_NAME) {
185            // Osd name request timed out. Try vendor id
186            requestVendorId();
187        } else if (state == STATE_WAITING_FOR_DEVICE_VENDOR_ID) {
188            // vendor id timed out. Go ahead creating the device info what we've got so far.
189            addDeviceInfo();
190            finish();
191        }
192    }
193}
194