Jungshik Jang | 0792d37 | 2014-04-23 17:57:26 +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 | |
| 17 | package com.android.server.hdmi; |
| 18 | |
Jungshik Jang | e9c77c8 | 2014-04-24 20:30:09 +0900 | [diff] [blame] | 19 | import android.hardware.hdmi.HdmiCec; |
Jungshik Jang | 7d9a843 | 2014-04-29 15:12:43 +0900 | [diff] [blame] | 20 | import android.hardware.hdmi.HdmiCecDeviceInfo; |
Jungshik Jang | e9c77c8 | 2014-04-24 20:30:09 +0900 | [diff] [blame] | 21 | import android.hardware.hdmi.HdmiCecMessage; |
Jungshik Jang | 0792d37 | 2014-04-23 17:57:26 +0900 | [diff] [blame] | 22 | import android.os.Handler; |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 23 | import android.os.Looper; |
Jungshik Jang | 4085d0e | 2014-05-27 19:52:39 +0900 | [diff] [blame] | 24 | import android.os.MessageQueue; |
Yuncheol Heo | ece603b | 2014-05-23 20:10:19 +0900 | [diff] [blame] | 25 | import android.util.Slog; |
Jungshik Jang | 7d9a843 | 2014-04-29 15:12:43 +0900 | [diff] [blame] | 26 | import android.util.SparseArray; |
Jungshik Jang | e9c77c8 | 2014-04-24 20:30:09 +0900 | [diff] [blame] | 27 | |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 28 | import com.android.server.hdmi.HdmiControlService.DevicePollingCallback; |
| 29 | |
Jungshik Jang | 3f74ab0 | 2014-04-30 14:31:02 +0900 | [diff] [blame] | 30 | import libcore.util.EmptyArray; |
| 31 | |
Jungshik Jang | 7d9a843 | 2014-04-29 15:12:43 +0900 | [diff] [blame] | 32 | import java.util.ArrayList; |
Jungshik Jang | 7d9a843 | 2014-04-29 15:12:43 +0900 | [diff] [blame] | 33 | import java.util.List; |
Jungshik Jang | 0792d37 | 2014-04-23 17:57:26 +0900 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Manages HDMI-CEC command and behaviors. It converts user's command into CEC command |
| 37 | * and pass it to CEC HAL so that it sends message to other device. For incoming |
| 38 | * message it translates the message and delegates it to proper module. |
| 39 | * |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 40 | * <p>It should be careful to access member variables on IO thread because |
| 41 | * it can be accessed from system thread as well. |
| 42 | * |
Jungshik Jang | 0792d37 | 2014-04-23 17:57:26 +0900 | [diff] [blame] | 43 | * <p>It can be created only by {@link HdmiCecController#create} |
| 44 | * |
| 45 | * <p>Declared as package-private, accessed by {@link HdmiControlService} only. |
| 46 | */ |
Jungshik Jang | a9095ba | 2014-05-02 13:06:22 +0900 | [diff] [blame] | 47 | final class HdmiCecController { |
Jungshik Jang | 0792d37 | 2014-04-23 17:57:26 +0900 | [diff] [blame] | 48 | private static final String TAG = "HdmiCecController"; |
| 49 | |
Jungshik Jang | 3f74ab0 | 2014-04-30 14:31:02 +0900 | [diff] [blame] | 50 | private static final byte[] EMPTY_BODY = EmptyArray.BYTE; |
| 51 | |
Jungshik Jang | e9c77c8 | 2014-04-24 20:30:09 +0900 | [diff] [blame] | 52 | // A message to pass cec send command to IO looper. |
| 53 | private static final int MSG_SEND_CEC_COMMAND = 1; |
Jungshik Jang | 3f74ab0 | 2014-04-30 14:31:02 +0900 | [diff] [blame] | 54 | // A message to delegate logical allocation to IO looper. |
| 55 | private static final int MSG_ALLOCATE_LOGICAL_ADDRESS = 2; |
Jungshik Jang | e9c77c8 | 2014-04-24 20:30:09 +0900 | [diff] [blame] | 56 | |
| 57 | // Message types to handle incoming message in main service looper. |
| 58 | private final static int MSG_RECEIVE_CEC_COMMAND = 1; |
Jungshik Jang | 3f74ab0 | 2014-04-30 14:31:02 +0900 | [diff] [blame] | 59 | // A message to report allocated logical address to main control looper. |
| 60 | private final static int MSG_REPORT_LOGICAL_ADDRESS = 2; |
Jungshik Jang | e9c77c8 | 2014-04-24 20:30:09 +0900 | [diff] [blame] | 61 | |
Jungshik Jang | 3f74ab0 | 2014-04-30 14:31:02 +0900 | [diff] [blame] | 62 | private static final int NUM_LOGICAL_ADDRESS = 16; |
| 63 | |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 64 | private static final int RETRY_COUNT_FOR_LOGICAL_ADDRESS_ALLOCATION = 3; |
| 65 | |
Jungshik Jang | 0792d37 | 2014-04-23 17:57:26 +0900 | [diff] [blame] | 66 | // Handler instance to process synchronous I/O (mainly send) message. |
| 67 | private Handler mIoHandler; |
| 68 | |
| 69 | // Handler instance to process various messages coming from other CEC |
| 70 | // device or issued by internal state change. |
Jungshik Jang | e9c77c8 | 2014-04-24 20:30:09 +0900 | [diff] [blame] | 71 | private Handler mControlHandler; |
Jungshik Jang | 0792d37 | 2014-04-23 17:57:26 +0900 | [diff] [blame] | 72 | |
| 73 | // Stores the pointer to the native implementation of the service that |
| 74 | // interacts with HAL. |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 75 | private volatile long mNativePtr; |
Jungshik Jang | 0792d37 | 2014-04-23 17:57:26 +0900 | [diff] [blame] | 76 | |
Jungshik Jang | a1fa91f | 2014-05-08 20:56:41 +0900 | [diff] [blame] | 77 | private HdmiControlService mService; |
| 78 | |
Jungshik Jang | 1a4485d | 2014-05-26 11:02:36 +0900 | [diff] [blame] | 79 | // Map-like container of all cec devices including local ones. |
| 80 | // A logical address of device is used as key of container. |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 81 | private final SparseArray<HdmiCecDeviceInfo> mDeviceInfos = new SparseArray<>(); |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 82 | |
Jinsuk Kim | 7fe2ae0 | 2014-05-26 17:33:05 +0900 | [diff] [blame] | 83 | // Stores the local CEC devices in the system. Device type is used for key. |
| 84 | private final SparseArray<HdmiCecLocalDevice> mLocalDevices = new SparseArray<>(); |
Jungshik Jang | 7d9a843 | 2014-04-29 15:12:43 +0900 | [diff] [blame] | 85 | |
Jungshik Jang | 0792d37 | 2014-04-23 17:57:26 +0900 | [diff] [blame] | 86 | // Private constructor. Use HdmiCecController.create(). |
| 87 | private HdmiCecController() { |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * A factory method to get {@link HdmiCecController}. If it fails to initialize |
| 92 | * inner device or has no device it will return {@code null}. |
| 93 | * |
| 94 | * <p>Declared as package-private, accessed by {@link HdmiControlService} only. |
Jungshik Jang | e9c77c8 | 2014-04-24 20:30:09 +0900 | [diff] [blame] | 95 | * @param service {@link HdmiControlService} instance used to create internal handler |
| 96 | * and to pass callback for incoming message or event. |
Jungshik Jang | 0792d37 | 2014-04-23 17:57:26 +0900 | [diff] [blame] | 97 | * @return {@link HdmiCecController} if device is initialized successfully. Otherwise, |
| 98 | * returns {@code null}. |
| 99 | */ |
Jungshik Jang | e9c77c8 | 2014-04-24 20:30:09 +0900 | [diff] [blame] | 100 | static HdmiCecController create(HdmiControlService service) { |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 101 | HdmiCecController controller = new HdmiCecController(); |
Jungshik Jang | 4085d0e | 2014-05-27 19:52:39 +0900 | [diff] [blame] | 102 | long nativePtr = nativeInit(controller, service.getServiceLooper().getQueue()); |
Jungshik Jang | 0792d37 | 2014-04-23 17:57:26 +0900 | [diff] [blame] | 103 | if (nativePtr == 0L) { |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 104 | controller = null; |
Jungshik Jang | 0792d37 | 2014-04-23 17:57:26 +0900 | [diff] [blame] | 105 | return null; |
| 106 | } |
| 107 | |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 108 | controller.init(service, nativePtr); |
| 109 | return controller; |
| 110 | } |
| 111 | |
| 112 | private void init(HdmiControlService service, long nativePtr) { |
| 113 | mService = service; |
| 114 | mIoHandler = new Handler(service.getServiceLooper()); |
| 115 | mControlHandler = new Handler(service.getServiceLooper()); |
| 116 | mNativePtr = nativePtr; |
Jungshik Jang | 0792d37 | 2014-04-23 17:57:26 +0900 | [diff] [blame] | 117 | } |
| 118 | |
Jungshik Jang | 3f74ab0 | 2014-04-30 14:31:02 +0900 | [diff] [blame] | 119 | /** |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 120 | * Perform initialization for each hosted device. |
Jinsuk Kim | a8a5e50 | 2014-05-15 16:51:49 +0900 | [diff] [blame] | 121 | * |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 122 | * @param deviceTypes array of device types |
Jinsuk Kim | a8a5e50 | 2014-05-15 16:51:49 +0900 | [diff] [blame] | 123 | */ |
| 124 | void initializeLocalDevices(int[] deviceTypes) { |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 125 | assertRunOnServiceThread(); |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 126 | for (int type : deviceTypes) { |
| 127 | HdmiCecLocalDevice device = HdmiCecLocalDevice.create(this, type); |
| 128 | if (device == null) { |
| 129 | continue; |
Jinsuk Kim | a8a5e50 | 2014-05-15 16:51:49 +0900 | [diff] [blame] | 130 | } |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 131 | // TODO: Consider restoring the local device addresses from persistent storage |
| 132 | // to allocate the same addresses again if possible. |
| 133 | device.setPreferredAddress(HdmiCec.ADDR_UNREGISTERED); |
Jinsuk Kim | 7fe2ae0 | 2014-05-26 17:33:05 +0900 | [diff] [blame] | 134 | mLocalDevices.put(type, device); |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 135 | device.init(); |
Jinsuk Kim | a8a5e50 | 2014-05-15 16:51:49 +0900 | [diff] [blame] | 136 | } |
Jinsuk Kim | a8a5e50 | 2014-05-15 16:51:49 +0900 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /** |
Jungshik Jang | 3f74ab0 | 2014-04-30 14:31:02 +0900 | [diff] [blame] | 140 | * Interface to report allocated logical address. |
| 141 | */ |
| 142 | interface AllocateLogicalAddressCallback { |
| 143 | /** |
| 144 | * Called when a new logical address is allocated. |
| 145 | * |
| 146 | * @param deviceType requested device type to allocate logical address |
| 147 | * @param logicalAddress allocated logical address. If it is |
| 148 | * {@link HdmiCec#ADDR_UNREGISTERED}, it means that |
| 149 | * it failed to allocate logical address for the given device type |
| 150 | */ |
| 151 | void onAllocated(int deviceType, int logicalAddress); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Allocate a new logical address of the given device type. Allocated |
| 156 | * address will be reported through {@link AllocateLogicalAddressCallback}. |
| 157 | * |
| 158 | * <p> Declared as package-private, accessed by {@link HdmiControlService} only. |
| 159 | * |
| 160 | * @param deviceType type of device to used to determine logical address |
| 161 | * @param preferredAddress a logical address preferred to be allocated. |
| 162 | * If sets {@link HdmiCec#ADDR_UNREGISTERED}, scans |
| 163 | * the smallest logical address matched with the given device type. |
| 164 | * Otherwise, scan address will start from {@code preferredAddress} |
| 165 | * @param callback callback interface to report allocated logical address to caller |
| 166 | */ |
Jungshik Jang | d643f76 | 2014-05-22 19:28:09 +0900 | [diff] [blame] | 167 | void allocateLogicalAddress(final int deviceType, final int preferredAddress, |
| 168 | final AllocateLogicalAddressCallback callback) { |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 169 | assertRunOnServiceThread(); |
| 170 | |
Jungshik Jang | d643f76 | 2014-05-22 19:28:09 +0900 | [diff] [blame] | 171 | runOnIoThread(new Runnable() { |
| 172 | @Override |
| 173 | public void run() { |
| 174 | handleAllocateLogicalAddress(deviceType, preferredAddress, callback); |
| 175 | } |
| 176 | }); |
| 177 | } |
| 178 | |
| 179 | private void handleAllocateLogicalAddress(final int deviceType, int preferredAddress, |
| 180 | final AllocateLogicalAddressCallback callback) { |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 181 | assertRunOnIoThread(); |
Jungshik Jang | d643f76 | 2014-05-22 19:28:09 +0900 | [diff] [blame] | 182 | int startAddress = preferredAddress; |
| 183 | // If preferred address is "unregistered", start address will be the smallest |
| 184 | // address matched with the given device type. |
| 185 | if (preferredAddress == HdmiCec.ADDR_UNREGISTERED) { |
| 186 | for (int i = 0; i < NUM_LOGICAL_ADDRESS; ++i) { |
| 187 | if (deviceType == HdmiCec.getTypeFromAddress(i)) { |
| 188 | startAddress = i; |
| 189 | break; |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | int logicalAddress = HdmiCec.ADDR_UNREGISTERED; |
| 195 | // Iterates all possible addresses which has the same device type. |
| 196 | for (int i = 0; i < NUM_LOGICAL_ADDRESS; ++i) { |
| 197 | int curAddress = (startAddress + i) % NUM_LOGICAL_ADDRESS; |
| 198 | if (curAddress != HdmiCec.ADDR_UNREGISTERED |
Jinsuk Kim | 0bc8b07 | 2014-05-27 17:23:27 +0900 | [diff] [blame] | 199 | && deviceType == HdmiCec.getTypeFromAddress(curAddress)) { |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 200 | if (!sendPollMessage(curAddress, RETRY_COUNT_FOR_LOGICAL_ADDRESS_ALLOCATION)) { |
Jungshik Jang | d643f76 | 2014-05-22 19:28:09 +0900 | [diff] [blame] | 201 | logicalAddress = curAddress; |
| 202 | break; |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | final int assignedAddress = logicalAddress; |
| 208 | if (callback != null) { |
| 209 | runOnServiceThread(new Runnable() { |
| 210 | @Override |
| 211 | public void run() { |
| 212 | callback.onAllocated(deviceType, assignedAddress); |
| 213 | } |
| 214 | }); |
| 215 | } |
Jungshik Jang | 3f74ab0 | 2014-04-30 14:31:02 +0900 | [diff] [blame] | 216 | } |
| 217 | |
Jungshik Jang | e9c77c8 | 2014-04-24 20:30:09 +0900 | [diff] [blame] | 218 | private static byte[] buildBody(int opcode, byte[] params) { |
| 219 | byte[] body = new byte[params.length + 1]; |
| 220 | body[0] = (byte) opcode; |
| 221 | System.arraycopy(params, 0, body, 1, params.length); |
| 222 | return body; |
| 223 | } |
Jungshik Jang | 0792d37 | 2014-04-23 17:57:26 +0900 | [diff] [blame] | 224 | |
Jungshik Jang | 7d9a843 | 2014-04-29 15:12:43 +0900 | [diff] [blame] | 225 | /** |
| 226 | * Add a new {@link HdmiCecDeviceInfo}. It returns old device info which has the same |
| 227 | * logical address as new device info's. |
| 228 | * |
| 229 | * <p>Declared as package-private. accessed by {@link HdmiControlService} only. |
| 230 | * |
| 231 | * @param deviceInfo a new {@link HdmiCecDeviceInfo} to be added. |
| 232 | * @return {@code null} if it is new device. Otherwise, returns old {@HdmiCecDeviceInfo} |
| 233 | * that has the same logical address as new one has. |
| 234 | */ |
| 235 | HdmiCecDeviceInfo addDeviceInfo(HdmiCecDeviceInfo deviceInfo) { |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 236 | assertRunOnServiceThread(); |
Jungshik Jang | 7d9a843 | 2014-04-29 15:12:43 +0900 | [diff] [blame] | 237 | HdmiCecDeviceInfo oldDeviceInfo = getDeviceInfo(deviceInfo.getLogicalAddress()); |
| 238 | if (oldDeviceInfo != null) { |
| 239 | removeDeviceInfo(deviceInfo.getLogicalAddress()); |
| 240 | } |
| 241 | mDeviceInfos.append(deviceInfo.getLogicalAddress(), deviceInfo); |
| 242 | return oldDeviceInfo; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Remove a device info corresponding to the given {@code logicalAddress}. |
| 247 | * It returns removed {@link HdmiCecDeviceInfo} if exists. |
| 248 | * |
| 249 | * <p>Declared as package-private. accessed by {@link HdmiControlService} only. |
| 250 | * |
| 251 | * @param logicalAddress logical address of device to be removed |
| 252 | * @return removed {@link HdmiCecDeviceInfo} it exists. Otherwise, returns {@code null} |
| 253 | */ |
| 254 | HdmiCecDeviceInfo removeDeviceInfo(int logicalAddress) { |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 255 | assertRunOnServiceThread(); |
Jungshik Jang | 7d9a843 | 2014-04-29 15:12:43 +0900 | [diff] [blame] | 256 | HdmiCecDeviceInfo deviceInfo = mDeviceInfos.get(logicalAddress); |
| 257 | if (deviceInfo != null) { |
| 258 | mDeviceInfos.remove(logicalAddress); |
| 259 | } |
| 260 | return deviceInfo; |
| 261 | } |
| 262 | |
| 263 | /** |
Jungshik Jang | cc5ef8c | 2014-05-27 13:27:36 +0900 | [diff] [blame^] | 264 | * Clear all device info. |
| 265 | * |
| 266 | * <p>Declared as package-private. accessed by {@link HdmiControlService} only. |
| 267 | */ |
| 268 | void clearDeviceInfoList() { |
| 269 | assertRunOnServiceThread(); |
| 270 | mDeviceInfos.clear(); |
| 271 | } |
| 272 | |
| 273 | /** |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 274 | * Return a list of all {@link HdmiCecDeviceInfo}. |
Jungshik Jang | 7d9a843 | 2014-04-29 15:12:43 +0900 | [diff] [blame] | 275 | * |
| 276 | * <p>Declared as package-private. accessed by {@link HdmiControlService} only. |
| 277 | */ |
| 278 | List<HdmiCecDeviceInfo> getDeviceInfoList() { |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 279 | assertRunOnServiceThread(); |
Jungshik Jang | cc5ef8c | 2014-05-27 13:27:36 +0900 | [diff] [blame^] | 280 | return sparseArrayToList(mDeviceInfos); |
Jungshik Jang | 7d9a843 | 2014-04-29 15:12:43 +0900 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Return a {@link HdmiCecDeviceInfo} corresponding to the given {@code logicalAddress}. |
| 285 | * |
| 286 | * <p>Declared as package-private. accessed by {@link HdmiControlService} only. |
| 287 | * |
| 288 | * @param logicalAddress logical address to be retrieved |
| 289 | * @return {@link HdmiCecDeviceInfo} matched with the given {@code logicalAddress}. |
| 290 | * Returns null if no logical address matched |
| 291 | */ |
| 292 | HdmiCecDeviceInfo getDeviceInfo(int logicalAddress) { |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 293 | assertRunOnServiceThread(); |
Jungshik Jang | 7d9a843 | 2014-04-29 15:12:43 +0900 | [diff] [blame] | 294 | return mDeviceInfos.get(logicalAddress); |
| 295 | } |
| 296 | |
Jungshik Jang | a9095ba | 2014-05-02 13:06:22 +0900 | [diff] [blame] | 297 | /** |
Jinsuk Kim | 7fe2ae0 | 2014-05-26 17:33:05 +0900 | [diff] [blame] | 298 | * Return the locally hosted logical device of a given type. |
| 299 | * |
| 300 | * @param deviceType logical device type |
| 301 | * @return {@link HdmiCecLocalDevice} instance if the instance of the type is available; |
| 302 | * otherwise null. |
| 303 | */ |
| 304 | HdmiCecLocalDevice getLocalDevice(int deviceType) { |
| 305 | return mLocalDevices.get(deviceType); |
| 306 | } |
| 307 | |
| 308 | /** |
Jungshik Jang | a9095ba | 2014-05-02 13:06:22 +0900 | [diff] [blame] | 309 | * Add a new logical address to the device. Device's HW should be notified |
| 310 | * when a new logical address is assigned to a device, so that it can accept |
| 311 | * a command having available destinations. |
| 312 | * |
| 313 | * <p>Declared as package-private. accessed by {@link HdmiControlService} only. |
| 314 | * |
| 315 | * @param newLogicalAddress a logical address to be added |
| 316 | * @return 0 on success. Otherwise, returns negative value |
| 317 | */ |
| 318 | int addLogicalAddress(int newLogicalAddress) { |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 319 | assertRunOnServiceThread(); |
Jungshik Jang | a9095ba | 2014-05-02 13:06:22 +0900 | [diff] [blame] | 320 | if (HdmiCec.isValidAddress(newLogicalAddress)) { |
| 321 | return nativeAddLogicalAddress(mNativePtr, newLogicalAddress); |
| 322 | } else { |
| 323 | return -1; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Clear all logical addresses registered in the device. |
| 329 | * |
| 330 | * <p>Declared as package-private. accessed by {@link HdmiControlService} only. |
| 331 | */ |
| 332 | void clearLogicalAddress() { |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 333 | assertRunOnServiceThread(); |
Jungshik Jang | a1fa91f | 2014-05-08 20:56:41 +0900 | [diff] [blame] | 334 | // TODO: consider to backup logical address so that new logical address |
| 335 | // allocation can use it as preferred address. |
Jinsuk Kim | 7fe2ae0 | 2014-05-26 17:33:05 +0900 | [diff] [blame] | 336 | for (int i = 0; i < mLocalDevices.size(); ++i) { |
| 337 | mLocalDevices.valueAt(i).clearAddress(); |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 338 | } |
Jungshik Jang | a9095ba | 2014-05-02 13:06:22 +0900 | [diff] [blame] | 339 | nativeClearLogicalAddress(mNativePtr); |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Return the physical address of the device. |
| 344 | * |
| 345 | * <p>Declared as package-private. accessed by {@link HdmiControlService} only. |
| 346 | * |
| 347 | * @return CEC physical address of the device. The range of success address |
| 348 | * is between 0x0000 and 0xFFFF. If failed it returns -1 |
| 349 | */ |
| 350 | int getPhysicalAddress() { |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 351 | assertRunOnServiceThread(); |
Jungshik Jang | a9095ba | 2014-05-02 13:06:22 +0900 | [diff] [blame] | 352 | return nativeGetPhysicalAddress(mNativePtr); |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * Return CEC version of the device. |
| 357 | * |
| 358 | * <p>Declared as package-private. accessed by {@link HdmiControlService} only. |
| 359 | */ |
| 360 | int getVersion() { |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 361 | assertRunOnServiceThread(); |
Jungshik Jang | a9095ba | 2014-05-02 13:06:22 +0900 | [diff] [blame] | 362 | return nativeGetVersion(mNativePtr); |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * Return vendor id of the device. |
| 367 | * |
| 368 | * <p>Declared as package-private. accessed by {@link HdmiControlService} only. |
| 369 | */ |
| 370 | int getVendorId() { |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 371 | assertRunOnServiceThread(); |
Jungshik Jang | a9095ba | 2014-05-02 13:06:22 +0900 | [diff] [blame] | 372 | return nativeGetVendorId(mNativePtr); |
| 373 | } |
| 374 | |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 375 | /** |
| 376 | * Poll all remote devices. It sends <Polling Message> to all remote |
| 377 | * devices. |
| 378 | * |
| 379 | * <p>Declared as package-private. accessed by {@link HdmiControlService} only. |
| 380 | * |
| 381 | * @param callback an interface used to get a list of all remote devices' address |
| 382 | * @param retryCount the number of retry used to send polling message to remote devices |
| 383 | */ |
| 384 | void pollDevices(DevicePollingCallback callback, int retryCount) { |
| 385 | assertRunOnServiceThread(); |
| 386 | // Extract polling candidates. No need to poll against local devices. |
| 387 | ArrayList<Integer> pollingCandidates = new ArrayList<>(); |
| 388 | for (int i = HdmiCec.ADDR_SPECIFIC_USE; i >= HdmiCec.ADDR_TV; --i) { |
| 389 | if (!isAllocatedLocalDeviceAddress(i)) { |
| 390 | pollingCandidates.add(i); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | runDevicePolling(pollingCandidates, retryCount, callback); |
| 395 | } |
| 396 | |
Jungshik Jang | cc5ef8c | 2014-05-27 13:27:36 +0900 | [diff] [blame^] | 397 | /** |
| 398 | * Return a list of all {@link HdmiCecLocalDevice}s. |
| 399 | * |
| 400 | * <p>Declared as package-private. accessed by {@link HdmiControlService} only. |
| 401 | */ |
| 402 | List<HdmiCecLocalDevice> getLocalDeviceList() { |
| 403 | assertRunOnServiceThread(); |
| 404 | return sparseArrayToList(mLocalDevices); |
| 405 | } |
| 406 | |
| 407 | private static <T> List<T> sparseArrayToList(SparseArray<T> array) { |
| 408 | ArrayList<T> list = new ArrayList<>(); |
| 409 | for (int i = 0; i < array.size(); ++i) { |
| 410 | list.add(array.valueAt(i)); |
| 411 | } |
| 412 | return list; |
| 413 | } |
| 414 | |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 415 | private boolean isAllocatedLocalDeviceAddress(int address) { |
Jinsuk Kim | 7fe2ae0 | 2014-05-26 17:33:05 +0900 | [diff] [blame] | 416 | for (int i = 0; i < mLocalDevices.size(); ++i) { |
| 417 | if (mLocalDevices.valueAt(i).isAddressOf(address)) { |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 418 | return true; |
| 419 | } |
| 420 | } |
| 421 | return false; |
| 422 | } |
| 423 | |
| 424 | private void runDevicePolling(final List<Integer> candidates, final int retryCount, |
| 425 | final DevicePollingCallback callback) { |
| 426 | assertRunOnServiceThread(); |
| 427 | runOnIoThread(new Runnable() { |
| 428 | @Override |
| 429 | public void run() { |
| 430 | final ArrayList<Integer> allocated = new ArrayList<>(); |
| 431 | for (Integer address : candidates) { |
| 432 | if (sendPollMessage(address, retryCount)) { |
| 433 | allocated.add(address); |
| 434 | } |
| 435 | } |
| 436 | if (callback != null) { |
| 437 | runOnServiceThread(new Runnable() { |
| 438 | @Override |
| 439 | public void run() { |
| 440 | callback.onPollingFinished(allocated); |
| 441 | } |
| 442 | }); |
| 443 | } |
| 444 | } |
| 445 | }); |
| 446 | } |
| 447 | |
| 448 | private boolean sendPollMessage(int address, int retryCount) { |
| 449 | assertRunOnIoThread(); |
| 450 | for (int i = 0; i < retryCount; ++i) { |
| 451 | // <Polling Message> is a message which has empty body and |
| 452 | // uses same address for both source and destination address. |
| 453 | // If sending <Polling Message> failed (NAK), it becomes |
| 454 | // new logical address for the device because no device uses |
| 455 | // it as logical address of the device. |
| 456 | if (nativeSendCecCommand(mNativePtr, address, address, EMPTY_BODY) |
| 457 | == HdmiControlService.SEND_RESULT_SUCCESS) { |
| 458 | return true; |
| 459 | } |
| 460 | } |
| 461 | return false; |
| 462 | } |
| 463 | |
| 464 | private void assertRunOnIoThread() { |
| 465 | if (Looper.myLooper() != mIoHandler.getLooper()) { |
| 466 | throw new IllegalStateException("Should run on io thread."); |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | private void assertRunOnServiceThread() { |
| 471 | if (Looper.myLooper() != mControlHandler.getLooper()) { |
| 472 | throw new IllegalStateException("Should run on service thread."); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | // Run a Runnable on IO thread. |
| 477 | // It should be careful to access member variables on IO thread because |
| 478 | // it can be accessed from system thread as well. |
Jungshik Jang | d643f76 | 2014-05-22 19:28:09 +0900 | [diff] [blame] | 479 | private void runOnIoThread(Runnable runnable) { |
| 480 | mIoHandler.post(runnable); |
| 481 | } |
| 482 | |
| 483 | private void runOnServiceThread(Runnable runnable) { |
| 484 | mControlHandler.post(runnable); |
| 485 | } |
| 486 | |
Jungshik Jang | a1fa91f | 2014-05-08 20:56:41 +0900 | [diff] [blame] | 487 | private boolean isAcceptableAddress(int address) { |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 488 | // Can access command targeting devices available in local device or broadcast command. |
| 489 | if (address == HdmiCec.ADDR_BROADCAST) { |
| 490 | return true; |
| 491 | } |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 492 | return isAllocatedLocalDeviceAddress(address); |
Jungshik Jang | a1fa91f | 2014-05-08 20:56:41 +0900 | [diff] [blame] | 493 | } |
| 494 | |
Jungshik Jang | e9c77c8 | 2014-04-24 20:30:09 +0900 | [diff] [blame] | 495 | private void onReceiveCommand(HdmiCecMessage message) { |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 496 | assertRunOnServiceThread(); |
Jungshik Jang | 4085d0e | 2014-05-27 19:52:39 +0900 | [diff] [blame] | 497 | if (isAcceptableAddress(message.getDestination()) |
| 498 | && mService.handleCecCommand(message)) { |
Jungshik Jang | a1fa91f | 2014-05-08 20:56:41 +0900 | [diff] [blame] | 499 | return; |
| 500 | } |
Jungshik Jang | e9c77c8 | 2014-04-24 20:30:09 +0900 | [diff] [blame] | 501 | |
| 502 | // TODO: Use device's source address for broadcast message. |
| 503 | int sourceAddress = message.getDestination() != HdmiCec.ADDR_BROADCAST ? |
| 504 | message.getDestination() : 0; |
| 505 | // Reply <Feature Abort> to initiator (source) for all requests. |
Jungshik Jang | a1fa91f | 2014-05-08 20:56:41 +0900 | [diff] [blame] | 506 | HdmiCecMessage cecMessage = HdmiCecMessageBuilder.buildFeatureAbortCommand |
| 507 | (sourceAddress, message.getSource(), message.getOpcode(), |
| 508 | HdmiCecMessageBuilder.ABORT_REFUSED); |
Jungshik Jang | d643f76 | 2014-05-22 19:28:09 +0900 | [diff] [blame] | 509 | sendCommand(cecMessage, null); |
Jungshik Jang | e9c77c8 | 2014-04-24 20:30:09 +0900 | [diff] [blame] | 510 | } |
| 511 | |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 512 | void sendCommand(HdmiCecMessage cecMessage) { |
| 513 | sendCommand(cecMessage, null); |
| 514 | } |
| 515 | |
Jungshik Jang | d643f76 | 2014-05-22 19:28:09 +0900 | [diff] [blame] | 516 | void sendCommand(final HdmiCecMessage cecMessage, |
| 517 | final HdmiControlService.SendMessageCallback callback) { |
| 518 | runOnIoThread(new Runnable() { |
| 519 | @Override |
| 520 | public void run() { |
| 521 | byte[] body = buildBody(cecMessage.getOpcode(), cecMessage.getParams()); |
| 522 | final int error = nativeSendCecCommand(mNativePtr, cecMessage.getSource(), |
| 523 | cecMessage.getDestination(), body); |
Yuncheol Heo | ece603b | 2014-05-23 20:10:19 +0900 | [diff] [blame] | 524 | if (error != HdmiControlService.SEND_RESULT_SUCCESS) { |
| 525 | Slog.w(TAG, "Failed to send " + cecMessage); |
| 526 | } |
Jungshik Jang | d643f76 | 2014-05-22 19:28:09 +0900 | [diff] [blame] | 527 | if (callback != null) { |
| 528 | runOnServiceThread(new Runnable() { |
| 529 | @Override |
| 530 | public void run() { |
| 531 | callback.onSendCompleted(error); |
| 532 | } |
| 533 | }); |
| 534 | } |
| 535 | } |
| 536 | }); |
Jungshik Jang | e9c77c8 | 2014-04-24 20:30:09 +0900 | [diff] [blame] | 537 | } |
| 538 | |
Jungshik Jang | 0792d37 | 2014-04-23 17:57:26 +0900 | [diff] [blame] | 539 | /** |
Jungshik Jang | e9c77c8 | 2014-04-24 20:30:09 +0900 | [diff] [blame] | 540 | * Called by native when incoming CEC message arrived. |
Jungshik Jang | 0792d37 | 2014-04-23 17:57:26 +0900 | [diff] [blame] | 541 | */ |
Jungshik Jang | e9c77c8 | 2014-04-24 20:30:09 +0900 | [diff] [blame] | 542 | private void handleIncomingCecCommand(int srcAddress, int dstAddress, byte[] body) { |
Jungshik Jang | 4085d0e | 2014-05-27 19:52:39 +0900 | [diff] [blame] | 543 | assertRunOnServiceThread(); |
| 544 | onReceiveCommand(HdmiCecMessageBuilder.of(srcAddress, dstAddress, body)); |
Jungshik Jang | e9c77c8 | 2014-04-24 20:30:09 +0900 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | /** |
| 548 | * Called by native when a hotplug event issues. |
| 549 | */ |
| 550 | private void handleHotplug(boolean connected) { |
Jungshik Jang | 67ea521 | 2014-05-15 14:05:24 +0900 | [diff] [blame] | 551 | // TODO: once add port number to cec HAL interface, pass port number |
| 552 | // to the service. |
| 553 | mService.onHotplug(0, connected); |
Jungshik Jang | 0792d37 | 2014-04-23 17:57:26 +0900 | [diff] [blame] | 554 | } |
| 555 | |
Jungshik Jang | 4085d0e | 2014-05-27 19:52:39 +0900 | [diff] [blame] | 556 | private static native long nativeInit(HdmiCecController handler, MessageQueue messageQueue); |
Jungshik Jang | a9095ba | 2014-05-02 13:06:22 +0900 | [diff] [blame] | 557 | private static native int nativeSendCecCommand(long controllerPtr, int srcAddress, |
Jungshik Jang | e9c77c8 | 2014-04-24 20:30:09 +0900 | [diff] [blame] | 558 | int dstAddress, byte[] body); |
Jungshik Jang | a9095ba | 2014-05-02 13:06:22 +0900 | [diff] [blame] | 559 | private static native int nativeAddLogicalAddress(long controllerPtr, int logicalAddress); |
| 560 | private static native void nativeClearLogicalAddress(long controllerPtr); |
| 561 | private static native int nativeGetPhysicalAddress(long controllerPtr); |
| 562 | private static native int nativeGetVersion(long controllerPtr); |
| 563 | private static native int nativeGetVendorId(long controllerPtr); |
Jungshik Jang | 0792d37 | 2014-04-23 17:57:26 +0900 | [diff] [blame] | 564 | } |