Jinsuk Kim | c70d229 | 2014-04-30 15:43:16 +0900 | [diff] [blame] | 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 | package com.android.server.hdmi; |
| 17 | |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 18 | import android.hardware.hdmi.HdmiDeviceInfo; |
Jinsuk Kim | c70d229 | 2014-04-30 15:43:16 +0900 | [diff] [blame] | 19 | import android.util.Slog; |
| 20 | |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 21 | import com.android.server.hdmi.HdmiCecLocalDevice.ActiveSource; |
Jinsuk Kim | c70d229 | 2014-04-30 15:43:16 +0900 | [diff] [blame] | 22 | import 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 Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 31 | * <p>The result is made in the form of {@link HdmiDeviceInfo} object, and passed to service |
Jinsuk Kim | c70d229 | 2014-04-30 15:43:16 +0900 | [diff] [blame] | 32 | * for the management through its life cycle. |
| 33 | * |
| 34 | * <p>Package-private, accessed by {@link HdmiControlService} only. |
| 35 | */ |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 36 | final class NewDeviceAction extends HdmiCecFeatureAction { |
Jinsuk Kim | c70d229 | 2014-04-30 15:43:16 +0900 | [diff] [blame] | 37 | |
| 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 Kim | bdf27fb | 2014-10-20 10:00:04 +0900 | [diff] [blame] | 50 | private final int mDeviceType; |
Jinsuk Kim | c70d229 | 2014-04-30 15:43:16 +0900 | [diff] [blame] | 51 | |
| 52 | private int mVendorId; |
| 53 | private String mDisplayName; |
Yuncheol Heo | 4635038 | 2014-12-03 22:29:45 +0900 | [diff] [blame] | 54 | private int mTimeoutRetry; |
Jinsuk Kim | c70d229 | 2014-04-30 15:43:16 +0900 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * Constructor. |
| 58 | * |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 59 | * @param source {@link HdmiCecLocalDevice} instance |
Jinsuk Kim | c70d229 | 2014-04-30 15:43:16 +0900 | [diff] [blame] | 60 | * @param deviceLogicalAddress logical address of the device in interest |
| 61 | * @param devicePhysicalAddress physical address of the device in interest |
Jinsuk Kim | bdf27fb | 2014-10-20 10:00:04 +0900 | [diff] [blame] | 62 | * @param deviceType type of the device |
Jinsuk Kim | c70d229 | 2014-04-30 15:43:16 +0900 | [diff] [blame] | 63 | */ |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 64 | NewDeviceAction(HdmiCecLocalDevice source, int deviceLogicalAddress, |
Jinsuk Kim | bdf27fb | 2014-10-20 10:00:04 +0900 | [diff] [blame] | 65 | int devicePhysicalAddress, int deviceType) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 66 | super(source); |
Jinsuk Kim | c70d229 | 2014-04-30 15:43:16 +0900 | [diff] [blame] | 67 | mDeviceLogicalAddress = deviceLogicalAddress; |
| 68 | mDevicePhysicalAddress = devicePhysicalAddress; |
Jinsuk Kim | bdf27fb | 2014-10-20 10:00:04 +0900 | [diff] [blame] | 69 | mDeviceType = deviceType; |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 70 | mVendorId = Constants.UNKNOWN_VENDOR_ID; |
Jinsuk Kim | c70d229 | 2014-04-30 15:43:16 +0900 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public boolean start() { |
Yuncheol Heo | 4635038 | 2014-12-03 22:29:45 +0900 | [diff] [blame] | 75 | requestOsdName(true); |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | private void requestOsdName(boolean firstTry) { |
| 80 | if (firstTry) { |
| 81 | mTimeoutRetry = 0; |
| 82 | } |
Jinsuk Kim | c70d229 | 2014-04-30 15:43:16 +0900 | [diff] [blame] | 83 | mState = STATE_WAITING_FOR_SET_OSD_NAME; |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 84 | if (mayProcessCommandIfCached(mDeviceLogicalAddress, Constants.MESSAGE_SET_OSD_NAME)) { |
Yuncheol Heo | 4635038 | 2014-12-03 22:29:45 +0900 | [diff] [blame] | 85 | return; |
Jungshik Jang | a466929 | 2014-06-10 14:48:30 +0900 | [diff] [blame] | 86 | } |
| 87 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 88 | sendCommand(HdmiCecMessageBuilder.buildGiveOsdNameCommand(getSourceAddress(), |
Jungshik Jang | a466929 | 2014-06-10 14:48:30 +0900 | [diff] [blame] | 89 | mDeviceLogicalAddress)); |
Jinsuk Kim | 5fba96d | 2014-07-11 11:51:34 +0900 | [diff] [blame] | 90 | addTimer(mState, HdmiConfig.TIMEOUT_MS); |
Jinsuk Kim | c70d229 | 2014-04-30 15:43:16 +0900 | [diff] [blame] | 91 | } |
| 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 Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 108 | if (opcode == Constants.MESSAGE_SET_OSD_NAME) { |
Jinsuk Kim | c70d229 | 2014-04-30 15:43:16 +0900 | [diff] [blame] | 109 | 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 Heo | 4635038 | 2014-12-03 22:29:45 +0900 | [diff] [blame] | 114 | requestVendorId(true); |
Jinsuk Kim | c70d229 | 2014-04-30 15:43:16 +0900 | [diff] [blame] | 115 | return true; |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 116 | } else if (opcode == Constants.MESSAGE_FEATURE_ABORT) { |
Jungshik Jang | 339227d | 2014-08-25 15:37:20 +0900 | [diff] [blame] | 117 | int requestOpcode = params[0] & 0xFF; |
| 118 | if (requestOpcode == Constants.MESSAGE_GIVE_OSD_NAME) { |
Yuncheol Heo | 4635038 | 2014-12-03 22:29:45 +0900 | [diff] [blame] | 119 | requestVendorId(true); |
Jinsuk Kim | c70d229 | 2014-04-30 15:43:16 +0900 | [diff] [blame] | 120 | return true; |
| 121 | } |
| 122 | } |
| 123 | } else if (mState == STATE_WAITING_FOR_DEVICE_VENDOR_ID) { |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 124 | if (opcode == Constants.MESSAGE_DEVICE_VENDOR_ID) { |
Yuncheol Heo | 75a77e7 | 2014-07-09 18:27:53 +0900 | [diff] [blame] | 125 | mVendorId = HdmiUtils.threeBytesToInt(params); |
Jinsuk Kim | c70d229 | 2014-04-30 15:43:16 +0900 | [diff] [blame] | 126 | addDeviceInfo(); |
| 127 | finish(); |
| 128 | return true; |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 129 | } else if (opcode == Constants.MESSAGE_FEATURE_ABORT) { |
Jungshik Jang | 339227d | 2014-08-25 15:37:20 +0900 | [diff] [blame] | 130 | int requestOpcode = params[0] & 0xFF; |
| 131 | if (requestOpcode == Constants.MESSAGE_GIVE_DEVICE_VENDOR_ID) { |
Jinsuk Kim | c70d229 | 2014-04-30 15:43:16 +0900 | [diff] [blame] | 132 | addDeviceInfo(); |
| 133 | finish(); |
| 134 | return true; |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | return false; |
| 139 | } |
| 140 | |
Jungshik Jang | a466929 | 2014-06-10 14:48:30 +0900 | [diff] [blame] | 141 | private boolean mayProcessCommandIfCached(int destAddress, int opcode) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 142 | HdmiCecMessage message = getCecMessageCache().getMessage(destAddress, opcode); |
Jungshik Jang | a466929 | 2014-06-10 14:48:30 +0900 | [diff] [blame] | 143 | if (message != null) { |
| 144 | return processCommand(message); |
| 145 | } |
| 146 | return false; |
| 147 | } |
| 148 | |
Yuncheol Heo | 4635038 | 2014-12-03 22:29:45 +0900 | [diff] [blame] | 149 | private void requestVendorId(boolean firstTry) { |
| 150 | if (firstTry) { |
| 151 | mTimeoutRetry = 0; |
| 152 | } |
Jungshik Jang | a466929 | 2014-06-10 14:48:30 +0900 | [diff] [blame] | 153 | // 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 Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 156 | if (mayProcessCommandIfCached(mDeviceLogicalAddress, |
| 157 | Constants.MESSAGE_DEVICE_VENDOR_ID)) { |
Jungshik Jang | a466929 | 2014-06-10 14:48:30 +0900 | [diff] [blame] | 158 | return; |
| 159 | } |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 160 | sendCommand(HdmiCecMessageBuilder.buildGiveDeviceVendorIdCommand(getSourceAddress(), |
Jungshik Jang | a1fa91f | 2014-05-08 20:56:41 +0900 | [diff] [blame] | 161 | mDeviceLogicalAddress)); |
Jinsuk Kim | 5fba96d | 2014-07-11 11:51:34 +0900 | [diff] [blame] | 162 | addTimer(mState, HdmiConfig.TIMEOUT_MS); |
Jinsuk Kim | c70d229 | 2014-04-30 15:43:16 +0900 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | private void addDeviceInfo() { |
Jinsuk Kim | 7cd4a58 | 2015-01-29 09:50:25 +0900 | [diff] [blame] | 166 | // 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 Kim | c70d229 | 2014-04-30 15:43:16 +0900 | [diff] [blame] | 172 | if (mDisplayName == null) { |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 173 | mDisplayName = HdmiUtils.getDefaultDeviceName(mDeviceLogicalAddress); |
Jinsuk Kim | c70d229 | 2014-04-30 15:43:16 +0900 | [diff] [blame] | 174 | } |
Jungshik Jang | 339227d | 2014-08-25 15:37:20 +0900 | [diff] [blame] | 175 | HdmiDeviceInfo deviceInfo = new HdmiDeviceInfo( |
Jinsuk Kim | c70d229 | 2014-04-30 15:43:16 +0900 | [diff] [blame] | 176 | mDeviceLogicalAddress, mDevicePhysicalAddress, |
Jinsuk Kim | 2b15201 | 2014-07-25 08:22:26 +0900 | [diff] [blame] | 177 | tv().getPortId(mDevicePhysicalAddress), |
Jinsuk Kim | bdf27fb | 2014-10-20 10:00:04 +0900 | [diff] [blame] | 178 | mDeviceType, mVendorId, mDisplayName); |
Jungshik Jang | 339227d | 2014-08-25 15:37:20 +0900 | [diff] [blame] | 179 | tv().addCecDevice(deviceInfo); |
Jungshik Jang | 97affee | 2014-07-11 17:04:43 +0900 | [diff] [blame] | 180 | |
Jinsuk Kim | 7fa3a66 | 2014-11-07 15:20:24 +0900 | [diff] [blame] | 181 | // Consume CEC messages we already got for this newly found device. |
| 182 | tv().processDelayedMessages(mDeviceLogicalAddress); |
| 183 | |
Jungshik Jang | 97affee | 2014-07-11 17:04:43 +0900 | [diff] [blame] | 184 | if (HdmiUtils.getTypeFromAddress(mDeviceLogicalAddress) |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 185 | == HdmiDeviceInfo.DEVICE_AUDIO_SYSTEM) { |
Jungshik Jang | 339227d | 2014-08-25 15:37:20 +0900 | [diff] [blame] | 186 | tv().onNewAvrAdded(deviceInfo); |
Jungshik Jang | 97affee | 2014-07-11 17:04:43 +0900 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
Jinsuk Kim | c70d229 | 2014-04-30 15:43:16 +0900 | [diff] [blame] | 190 | @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 Heo | 4635038 | 2014-12-03 22:29:45 +0900 | [diff] [blame] | 196 | if (++mTimeoutRetry < HdmiConfig.TIMEOUT_RETRY) { |
| 197 | requestOsdName(false); |
| 198 | return; |
| 199 | } |
Jinsuk Kim | c70d229 | 2014-04-30 15:43:16 +0900 | [diff] [blame] | 200 | // Osd name request timed out. Try vendor id |
Yuncheol Heo | 4635038 | 2014-12-03 22:29:45 +0900 | [diff] [blame] | 201 | requestVendorId(true); |
Jinsuk Kim | c70d229 | 2014-04-30 15:43:16 +0900 | [diff] [blame] | 202 | } else if (state == STATE_WAITING_FOR_DEVICE_VENDOR_ID) { |
Yuncheol Heo | 4635038 | 2014-12-03 22:29:45 +0900 | [diff] [blame] | 203 | if (++mTimeoutRetry < HdmiConfig.TIMEOUT_RETRY) { |
| 204 | requestVendorId(false); |
| 205 | return; |
| 206 | } |
Jinsuk Kim | c70d229 | 2014-04-30 15:43:16 +0900 | [diff] [blame] | 207 | // vendor id timed out. Go ahead creating the device info what we've got so far. |
| 208 | addDeviceInfo(); |
| 209 | finish(); |
| 210 | } |
| 211 | } |
Jungshik Jang | 97affee | 2014-07-11 17:04:43 +0900 | [diff] [blame] | 212 | |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 213 | boolean isActionOf(ActiveSource activeSource) { |
| 214 | return (mDeviceLogicalAddress == activeSource.logicalAddress) |
| 215 | && (mDevicePhysicalAddress == activeSource.physicalAddress); |
Jungshik Jang | 97affee | 2014-07-11 17:04:43 +0900 | [diff] [blame] | 216 | } |
Jinsuk Kim | c70d229 | 2014-04-30 15:43:16 +0900 | [diff] [blame] | 217 | } |