blob: 6753368911b9f9015b7d2ccf79ecaebc9f1ee721 [file] [log] [blame]
Jinsuk Kimc70d2292014-04-30 15:43:16 +09001/*
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
Jungshik Jang61f4fbd2014-08-06 19:21:12 +090018import android.hardware.hdmi.HdmiDeviceInfo;
Jinsuk Kimc70d2292014-04-30 15:43:16 +090019import android.util.Slog;
20
Jinsuk Kim72b7d732014-07-24 09:15:35 +090021import com.android.server.hdmi.HdmiCecLocalDevice.ActiveSource;
Jinsuk Kimc70d2292014-04-30 15:43:16 +090022import java.io.UnsupportedEncodingException;
23
24/**
25 * Feature action that discovers the information of a newly found logical device.
26 *
27 * This action is created when receiving <Report Physical Address>, a CEC command a newly
28 * connected HDMI-CEC device broadcasts to announce its advent. Additional commands are issued in
29 * this action to gather more information on the device such as OSD name and device vendor ID.
30 *
Jungshik Jang61f4fbd2014-08-06 19:21:12 +090031 * <p>The result is made in the form of {@link HdmiDeviceInfo} object, and passed to service
Jinsuk Kimc70d2292014-04-30 15:43:16 +090032 * for the management through its life cycle.
33 *
34 * <p>Package-private, accessed by {@link HdmiControlService} only.
35 */
Jungshik Jangb509c2e2014-08-07 13:45:01 +090036final class NewDeviceAction extends HdmiCecFeatureAction {
Jinsuk Kimc70d2292014-04-30 15:43:16 +090037
38 private static final String TAG = "NewDeviceAction";
39
40 // State in which the action sent <Give OSD Name> and is waiting for <Set OSD Name>
41 // that contains the name of the device for display on screen.
42 static final int STATE_WAITING_FOR_SET_OSD_NAME = 1;
43
44 // State in which the action sent <Give Device Vendor ID> and is waiting for
45 // <Device Vendor ID> that contains the vendor ID of the device.
46 static final int STATE_WAITING_FOR_DEVICE_VENDOR_ID = 2;
47
48 private final int mDeviceLogicalAddress;
49 private final int mDevicePhysicalAddress;
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +090050 private final int mDeviceType;
Jinsuk Kimc70d2292014-04-30 15:43:16 +090051
52 private int mVendorId;
53 private String mDisplayName;
Yuncheol Heo46350382014-12-03 22:29:45 +090054 private int mTimeoutRetry;
Jinsuk Kimc70d2292014-04-30 15:43:16 +090055
56 /**
57 * Constructor.
58 *
Jungshik Jang79c58a42014-06-16 16:45:36 +090059 * @param source {@link HdmiCecLocalDevice} instance
Jinsuk Kimc70d2292014-04-30 15:43:16 +090060 * @param deviceLogicalAddress logical address of the device in interest
61 * @param devicePhysicalAddress physical address of the device in interest
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +090062 * @param deviceType type of the device
Jinsuk Kimc70d2292014-04-30 15:43:16 +090063 */
Jungshik Jang79c58a42014-06-16 16:45:36 +090064 NewDeviceAction(HdmiCecLocalDevice source, int deviceLogicalAddress,
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +090065 int devicePhysicalAddress, int deviceType) {
Jungshik Jang79c58a42014-06-16 16:45:36 +090066 super(source);
Jinsuk Kimc70d2292014-04-30 15:43:16 +090067 mDeviceLogicalAddress = deviceLogicalAddress;
68 mDevicePhysicalAddress = devicePhysicalAddress;
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +090069 mDeviceType = deviceType;
Jinsuk Kimc0c20d02014-07-04 14:34:31 +090070 mVendorId = Constants.UNKNOWN_VENDOR_ID;
Jinsuk Kimc70d2292014-04-30 15:43:16 +090071 }
72
73 @Override
74 public boolean start() {
Yuncheol Heo46350382014-12-03 22:29:45 +090075 requestOsdName(true);
76 return true;
77 }
78
79 private void requestOsdName(boolean firstTry) {
80 if (firstTry) {
81 mTimeoutRetry = 0;
82 }
Jinsuk Kimc70d2292014-04-30 15:43:16 +090083 mState = STATE_WAITING_FOR_SET_OSD_NAME;
Jinsuk Kimc0c20d02014-07-04 14:34:31 +090084 if (mayProcessCommandIfCached(mDeviceLogicalAddress, Constants.MESSAGE_SET_OSD_NAME)) {
Yuncheol Heo46350382014-12-03 22:29:45 +090085 return;
Jungshik Janga4669292014-06-10 14:48:30 +090086 }
87
Jungshik Jang79c58a42014-06-16 16:45:36 +090088 sendCommand(HdmiCecMessageBuilder.buildGiveOsdNameCommand(getSourceAddress(),
Jungshik Janga4669292014-06-10 14:48:30 +090089 mDeviceLogicalAddress));
Jinsuk Kim5fba96d2014-07-11 11:51:34 +090090 addTimer(mState, HdmiConfig.TIMEOUT_MS);
Jinsuk Kimc70d2292014-04-30 15:43:16 +090091 }
92
93 @Override
94 public boolean processCommand(HdmiCecMessage cmd) {
95 // For the logical device in interest, we want two more pieces of information -
96 // osd name and vendor id. They are requested in sequence. In case we don't
97 // get the expected responses (either by timeout or by receiving <feature abort> command),
98 // set them to a default osd name and unknown vendor id respectively.
99 int opcode = cmd.getOpcode();
100 int src = cmd.getSource();
101 byte[] params = cmd.getParams();
102
103 if (mDeviceLogicalAddress != src) {
104 return false;
105 }
106
107 if (mState == STATE_WAITING_FOR_SET_OSD_NAME) {
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900108 if (opcode == Constants.MESSAGE_SET_OSD_NAME) {
Jinsuk Kimc70d2292014-04-30 15:43:16 +0900109 try {
110 mDisplayName = new String(params, "US-ASCII");
111 } catch (UnsupportedEncodingException e) {
112 Slog.e(TAG, "Failed to get OSD name: " + e.getMessage());
113 }
Yuncheol Heo46350382014-12-03 22:29:45 +0900114 requestVendorId(true);
Jinsuk Kimc70d2292014-04-30 15:43:16 +0900115 return true;
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900116 } else if (opcode == Constants.MESSAGE_FEATURE_ABORT) {
Jungshik Jang339227d2014-08-25 15:37:20 +0900117 int requestOpcode = params[0] & 0xFF;
118 if (requestOpcode == Constants.MESSAGE_GIVE_OSD_NAME) {
Yuncheol Heo46350382014-12-03 22:29:45 +0900119 requestVendorId(true);
Jinsuk Kimc70d2292014-04-30 15:43:16 +0900120 return true;
121 }
122 }
123 } else if (mState == STATE_WAITING_FOR_DEVICE_VENDOR_ID) {
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900124 if (opcode == Constants.MESSAGE_DEVICE_VENDOR_ID) {
Yuncheol Heo75a77e72014-07-09 18:27:53 +0900125 mVendorId = HdmiUtils.threeBytesToInt(params);
Jinsuk Kimc70d2292014-04-30 15:43:16 +0900126 addDeviceInfo();
127 finish();
128 return true;
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900129 } else if (opcode == Constants.MESSAGE_FEATURE_ABORT) {
Jungshik Jang339227d2014-08-25 15:37:20 +0900130 int requestOpcode = params[0] & 0xFF;
131 if (requestOpcode == Constants.MESSAGE_GIVE_DEVICE_VENDOR_ID) {
Jinsuk Kimc70d2292014-04-30 15:43:16 +0900132 addDeviceInfo();
133 finish();
134 return true;
135 }
136 }
137 }
138 return false;
139 }
140
Jungshik Janga4669292014-06-10 14:48:30 +0900141 private boolean mayProcessCommandIfCached(int destAddress, int opcode) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900142 HdmiCecMessage message = getCecMessageCache().getMessage(destAddress, opcode);
Jungshik Janga4669292014-06-10 14:48:30 +0900143 if (message != null) {
144 return processCommand(message);
145 }
146 return false;
147 }
148
Yuncheol Heo46350382014-12-03 22:29:45 +0900149 private void requestVendorId(boolean firstTry) {
150 if (firstTry) {
151 mTimeoutRetry = 0;
152 }
Jungshik Janga4669292014-06-10 14:48:30 +0900153 // At first, transit to waiting status for <Device Vendor Id>.
154 mState = STATE_WAITING_FOR_DEVICE_VENDOR_ID;
155 // If the message is already in cache, process it.
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900156 if (mayProcessCommandIfCached(mDeviceLogicalAddress,
157 Constants.MESSAGE_DEVICE_VENDOR_ID)) {
Jungshik Janga4669292014-06-10 14:48:30 +0900158 return;
159 }
Jungshik Jang79c58a42014-06-16 16:45:36 +0900160 sendCommand(HdmiCecMessageBuilder.buildGiveDeviceVendorIdCommand(getSourceAddress(),
Jungshik Janga1fa91f2014-05-08 20:56:41 +0900161 mDeviceLogicalAddress));
Jinsuk Kim5fba96d2014-07-11 11:51:34 +0900162 addTimer(mState, HdmiConfig.TIMEOUT_MS);
Jinsuk Kimc70d2292014-04-30 15:43:16 +0900163 }
164
165 private void addDeviceInfo() {
Jinsuk Kim7cd4a582015-01-29 09:50:25 +0900166 // The device should be in the device list with default information.
167 if (!tv().isInDeviceList(mDeviceLogicalAddress, mDevicePhysicalAddress)) {
168 Slog.w(TAG, String.format("Device not found (%02x, %04x)",
169 mDeviceLogicalAddress, mDevicePhysicalAddress));
170 return;
171 }
Jinsuk Kimc70d2292014-04-30 15:43:16 +0900172 if (mDisplayName == null) {
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900173 mDisplayName = HdmiUtils.getDefaultDeviceName(mDeviceLogicalAddress);
Jinsuk Kimc70d2292014-04-30 15:43:16 +0900174 }
Jungshik Jang339227d2014-08-25 15:37:20 +0900175 HdmiDeviceInfo deviceInfo = new HdmiDeviceInfo(
Jinsuk Kimc70d2292014-04-30 15:43:16 +0900176 mDeviceLogicalAddress, mDevicePhysicalAddress,
Jinsuk Kim2b152012014-07-25 08:22:26 +0900177 tv().getPortId(mDevicePhysicalAddress),
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900178 mDeviceType, mVendorId, mDisplayName);
Jungshik Jang339227d2014-08-25 15:37:20 +0900179 tv().addCecDevice(deviceInfo);
Jungshik Jang97affee2014-07-11 17:04:43 +0900180
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900181 // Consume CEC messages we already got for this newly found device.
182 tv().processDelayedMessages(mDeviceLogicalAddress);
183
Jungshik Jang97affee2014-07-11 17:04:43 +0900184 if (HdmiUtils.getTypeFromAddress(mDeviceLogicalAddress)
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900185 == HdmiDeviceInfo.DEVICE_AUDIO_SYSTEM) {
Jungshik Jang339227d2014-08-25 15:37:20 +0900186 tv().onNewAvrAdded(deviceInfo);
Jungshik Jang97affee2014-07-11 17:04:43 +0900187 }
188 }
189
Jinsuk Kimc70d2292014-04-30 15:43:16 +0900190 @Override
191 public void handleTimerEvent(int state) {
192 if (mState == STATE_NONE || mState != state) {
193 return;
194 }
195 if (state == STATE_WAITING_FOR_SET_OSD_NAME) {
Yuncheol Heo46350382014-12-03 22:29:45 +0900196 if (++mTimeoutRetry < HdmiConfig.TIMEOUT_RETRY) {
197 requestOsdName(false);
198 return;
199 }
Jinsuk Kimc70d2292014-04-30 15:43:16 +0900200 // Osd name request timed out. Try vendor id
Yuncheol Heo46350382014-12-03 22:29:45 +0900201 requestVendorId(true);
Jinsuk Kimc70d2292014-04-30 15:43:16 +0900202 } else if (state == STATE_WAITING_FOR_DEVICE_VENDOR_ID) {
Yuncheol Heo46350382014-12-03 22:29:45 +0900203 if (++mTimeoutRetry < HdmiConfig.TIMEOUT_RETRY) {
204 requestVendorId(false);
205 return;
206 }
Jinsuk Kimc70d2292014-04-30 15:43:16 +0900207 // vendor id timed out. Go ahead creating the device info what we've got so far.
208 addDeviceInfo();
209 finish();
210 }
211 }
Jungshik Jang97affee2014-07-11 17:04:43 +0900212
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900213 boolean isActionOf(ActiveSource activeSource) {
214 return (mDeviceLogicalAddress == activeSource.logicalAddress)
215 && (mDevicePhysicalAddress == activeSource.physicalAddress);
Jungshik Jang97affee2014-07-11 17:04:43 +0900216 }
Jinsuk Kimc70d2292014-04-30 15:43:16 +0900217}