NewDeviceAction.java revision 03e8a834da8189b3a20023cee31e78a17a45b07b
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 service {@link HdmiControlService} instance
59     * @param sourceAddress logical address to be used as source address
60     * @param deviceLogicalAddress logical address of the device in interest
61     * @param devicePhysicalAddress physical address of the device in interest
62     */
63    NewDeviceAction(HdmiControlService service, int sourceAddress, int deviceLogicalAddress,
64            int devicePhysicalAddress) {
65        super(service, sourceAddress);
66        mDeviceLogicalAddress = deviceLogicalAddress;
67        mDevicePhysicalAddress = devicePhysicalAddress;
68        mVendorId = HdmiCec.UNKNOWN_VENDOR_ID;
69    }
70
71    @Override
72    public boolean start() {
73        sendCommand(
74                HdmiCecMessageBuilder.buildGiveOsdNameCommand(mSourceAddress,
75                        mDeviceLogicalAddress));
76        mState = STATE_WAITING_FOR_SET_OSD_NAME;
77        addTimer(mState, TIMEOUT_MS);
78        return true;
79    }
80
81    @Override
82    public boolean processCommand(HdmiCecMessage cmd) {
83        // For the logical device in interest, we want two more pieces of information -
84        // osd name and vendor id. They are requested in sequence. In case we don't
85        // get the expected responses (either by timeout or by receiving <feature abort> command),
86        // set them to a default osd name and unknown vendor id respectively.
87        int opcode = cmd.getOpcode();
88        int src = cmd.getSource();
89        byte[] params = cmd.getParams();
90
91        if (mDeviceLogicalAddress != src) {
92            return false;
93        }
94
95        if (mState == STATE_WAITING_FOR_SET_OSD_NAME) {
96            if (opcode == HdmiCec.MESSAGE_SET_OSD_NAME) {
97                try {
98                    mDisplayName = new String(params, "US-ASCII");
99                } catch (UnsupportedEncodingException e) {
100                    Slog.e(TAG, "Failed to get OSD name: " + e.getMessage());
101                }
102                mState = STATE_WAITING_FOR_DEVICE_VENDOR_ID;
103                requestVendorId();
104                return true;
105            } else if (opcode == HdmiCec.MESSAGE_FEATURE_ABORT) {
106                int requestOpcode = params[1] & 0xff;
107                if (requestOpcode == HdmiCec.MESSAGE_SET_OSD_NAME) {
108                    mState = STATE_WAITING_FOR_DEVICE_VENDOR_ID;
109                    requestVendorId();
110                    return true;
111                }
112            }
113        } else if (mState == STATE_WAITING_FOR_DEVICE_VENDOR_ID) {
114            if (opcode == HdmiCec.MESSAGE_DEVICE_VENDOR_ID) {
115                if (params.length == 3) {
116                    mVendorId = ((params[0] & 0xff) << 16) + ((params[1] & 0xff) << 8)
117                        + (params[2] & 0xff);
118                } else {
119                    Slog.e(TAG, "Failed to get device vendor ID: ");
120                }
121                addDeviceInfo();
122                finish();
123                return true;
124            } else if (opcode == HdmiCec.MESSAGE_FEATURE_ABORT) {
125                int requestOpcode = params[1] & 0xff;
126                if (requestOpcode == HdmiCec.MESSAGE_DEVICE_VENDOR_ID) {
127                    addDeviceInfo();
128                    finish();
129                    return true;
130                }
131            }
132        }
133        return false;
134    }
135
136    private void requestVendorId() {
137        sendCommand(HdmiCecMessageBuilder.buildGiveDeviceVendorIdCommand(mSourceAddress,
138                mDeviceLogicalAddress));
139        addTimer(mState, TIMEOUT_MS);
140    }
141
142    private void addDeviceInfo() {
143        if (mDisplayName == null) {
144            mDisplayName = HdmiCec.getDefaultDeviceName(mDeviceLogicalAddress);
145        }
146        mService.addDeviceInfo(new HdmiCecDeviceInfo(
147                mDeviceLogicalAddress, mDevicePhysicalAddress,
148                HdmiCec.getTypeFromAddress(mDeviceLogicalAddress),
149                mVendorId, mDisplayName));
150    }
151
152    @Override
153    public void handleTimerEvent(int state) {
154        if (mState == STATE_NONE || mState != state) {
155            return;
156        }
157        if (state == STATE_WAITING_FOR_SET_OSD_NAME) {
158            // Osd name request timed out. Try vendor id
159            mState = STATE_WAITING_FOR_DEVICE_VENDOR_ID;
160            requestVendorId();
161        } else if (state == STATE_WAITING_FOR_DEVICE_VENDOR_ID) {
162            // vendor id timed out. Go ahead creating the device info what we've got so far.
163            addDeviceInfo();
164            finish();
165        }
166    }
167}
168