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