NewDeviceAction.java revision a1fa91fe263c483cf13066e2847a440de2cd52a5
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.buildGetOsdNameCommand(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]; 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] << 16) + (params[1] << 8) + params[2]; 117 } else { 118 Slog.e(TAG, "Failed to get device vendor ID: "); 119 } 120 addDeviceInfo(); 121 finish(); 122 return true; 123 } else if (opcode == HdmiCec.MESSAGE_FEATURE_ABORT) { 124 int requestOpcode = params[1]; 125 if (requestOpcode == HdmiCec.MESSAGE_DEVICE_VENDOR_ID) { 126 addDeviceInfo(); 127 finish(); 128 return true; 129 } 130 } 131 } 132 return false; 133 } 134 135 private void requestVendorId() { 136 sendCommand(HdmiCecMessageBuilder.buildGiveDeviceVendorIdCommand(mSourceAddress, 137 mDeviceLogicalAddress)); 138 addTimer(mState, TIMEOUT_MS); 139 } 140 141 private void addDeviceInfo() { 142 if (mDisplayName == null) { 143 mDisplayName = HdmiCec.getDefaultDeviceName(mDeviceLogicalAddress); 144 } 145 mService.addDeviceInfo(new HdmiCecDeviceInfo( 146 mDeviceLogicalAddress, mDevicePhysicalAddress, 147 HdmiCec.getTypeFromAddress(mDeviceLogicalAddress), 148 mVendorId, mDisplayName)); 149 } 150 151 @Override 152 public void handleTimerEvent(int state) { 153 if (mState == STATE_NONE || mState != state) { 154 return; 155 } 156 if (state == STATE_WAITING_FOR_SET_OSD_NAME) { 157 // Osd name request timed out. Try vendor id 158 mState = STATE_WAITING_FOR_DEVICE_VENDOR_ID; 159 requestVendorId(); 160 } else if (state == STATE_WAITING_FOR_DEVICE_VENDOR_ID) { 161 // vendor id timed out. Go ahead creating the device info what we've got so far. 162 addDeviceInfo(); 163 finish(); 164 } 165 } 166} 167