NewDeviceAction.java revision 092b445ef898e3c1e5b2918b554480940f0f5a28
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        mState = STATE_WAITING_FOR_SET_OSD_NAME;
74        if (mayProcessCommandIfCached(mDeviceLogicalAddress, HdmiCec.MESSAGE_SET_OSD_NAME)) {
75            return true;
76        }
77
78        sendCommand(HdmiCecMessageBuilder.buildGiveOsdNameCommand(mSourceAddress,
79                mDeviceLogicalAddress));
80        addTimer(mState, TIMEOUT_MS);
81        return true;
82    }
83
84    @Override
85    public boolean processCommand(HdmiCecMessage cmd) {
86        // For the logical device in interest, we want two more pieces of information -
87        // osd name and vendor id. They are requested in sequence. In case we don't
88        // get the expected responses (either by timeout or by receiving <feature abort> command),
89        // set them to a default osd name and unknown vendor id respectively.
90        int opcode = cmd.getOpcode();
91        int src = cmd.getSource();
92        byte[] params = cmd.getParams();
93
94        if (mDeviceLogicalAddress != src) {
95            return false;
96        }
97
98        if (mState == STATE_WAITING_FOR_SET_OSD_NAME) {
99            if (opcode == HdmiCec.MESSAGE_SET_OSD_NAME) {
100                try {
101                    mDisplayName = new String(params, "US-ASCII");
102                } catch (UnsupportedEncodingException e) {
103                    Slog.e(TAG, "Failed to get OSD name: " + e.getMessage());
104                }
105                requestVendorId();
106                return true;
107            } else if (opcode == HdmiCec.MESSAGE_FEATURE_ABORT) {
108                int requestOpcode = params[1] & 0xFF;
109                if (requestOpcode == HdmiCec.MESSAGE_SET_OSD_NAME) {
110                    requestVendorId();
111                    return true;
112                }
113            }
114        } else if (mState == STATE_WAITING_FOR_DEVICE_VENDOR_ID) {
115            if (opcode == HdmiCec.MESSAGE_DEVICE_VENDOR_ID) {
116                if (params.length == 3) {
117                    mVendorId = HdmiUtils.threeBytesToInt(params);
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 boolean mayProcessCommandIfCached(int destAddress, int opcode) {
137        HdmiCecMessage message = mService.getCecMessageCache().getMessage(destAddress, opcode);
138        if (message != null) {
139            return processCommand(message);
140        }
141        return false;
142    }
143
144    private void requestVendorId() {
145        // At first, transit to waiting status for <Device Vendor Id>.
146        mState = STATE_WAITING_FOR_DEVICE_VENDOR_ID;
147        // If the message is already in cache, process it.
148        if (mayProcessCommandIfCached(mDeviceLogicalAddress, HdmiCec.MESSAGE_DEVICE_VENDOR_ID)) {
149            return;
150        }
151        sendCommand(HdmiCecMessageBuilder.buildGiveDeviceVendorIdCommand(mSourceAddress,
152                mDeviceLogicalAddress));
153        addTimer(mState, TIMEOUT_MS);
154    }
155
156    private void addDeviceInfo() {
157        if (mDisplayName == null) {
158            mDisplayName = HdmiCec.getDefaultDeviceName(mDeviceLogicalAddress);
159        }
160        mService.addCecDevice(new HdmiCecDeviceInfo(
161                mDeviceLogicalAddress, mDevicePhysicalAddress,
162                HdmiCec.getTypeFromAddress(mDeviceLogicalAddress),
163                mVendorId, mDisplayName));
164    }
165
166    @Override
167    public void handleTimerEvent(int state) {
168        if (mState == STATE_NONE || mState != state) {
169            return;
170        }
171        if (state == STATE_WAITING_FOR_SET_OSD_NAME) {
172            // Osd name request timed out. Try vendor id
173            requestVendorId();
174        } else if (state == STATE_WAITING_FOR_DEVICE_VENDOR_ID) {
175            // vendor id timed out. Go ahead creating the device info what we've got so far.
176            addDeviceInfo();
177            finish();
178        }
179    }
180}
181