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 | |
Jinsuk Kim | 0340bbc | 2014-06-05 11:07:47 +0900 | [diff] [blame] | 19 | import android.hardware.hdmi.HdmiPortInfo; |
Jungshik Jang | 0792d37 | 2014-04-23 17:57:26 +0900 | [diff] [blame] | 20 | import android.os.Handler; |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 21 | import android.os.Looper; |
Jungshik Jang | 4085d0e | 2014-05-27 19:52:39 +0900 | [diff] [blame] | 22 | import android.os.MessageQueue; |
Yuncheol Heo | ece603b | 2014-05-23 20:10:19 +0900 | [diff] [blame] | 23 | import android.util.Slog; |
Jungshik Jang | 7d9a843 | 2014-04-29 15:12:43 +0900 | [diff] [blame] | 24 | import android.util.SparseArray; |
Jungshik Jang | e9c77c8 | 2014-04-24 20:30:09 +0900 | [diff] [blame] | 25 | |
Terry Heo | 959d2db | 2014-08-28 16:45:41 +0900 | [diff] [blame^] | 26 | import com.android.internal.util.IndentingPrintWriter; |
Jungshik Jang | 0f8b4b7 | 2014-05-28 17:58:58 +0900 | [diff] [blame] | 27 | import com.android.internal.util.Predicate; |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 28 | import com.android.server.hdmi.HdmiAnnotations.IoThreadOnly; |
| 29 | import com.android.server.hdmi.HdmiAnnotations.ServiceThreadOnly; |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 30 | import com.android.server.hdmi.HdmiControlService.DevicePollingCallback; |
| 31 | |
Jungshik Jang | 3f74ab0 | 2014-04-30 14:31:02 +0900 | [diff] [blame] | 32 | import libcore.util.EmptyArray; |
| 33 | |
Terry Heo | 959d2db | 2014-08-28 16:45:41 +0900 | [diff] [blame^] | 34 | import java.io.FileDescriptor; |
| 35 | import java.io.PrintWriter; |
Jungshik Jang | 7d9a843 | 2014-04-29 15:12:43 +0900 | [diff] [blame] | 36 | import java.util.ArrayList; |
Jungshik Jang | 7d9a843 | 2014-04-29 15:12:43 +0900 | [diff] [blame] | 37 | import java.util.List; |
Jungshik Jang | 0792d37 | 2014-04-23 17:57:26 +0900 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Manages HDMI-CEC command and behaviors. It converts user's command into CEC command |
| 41 | * and pass it to CEC HAL so that it sends message to other device. For incoming |
| 42 | * message it translates the message and delegates it to proper module. |
| 43 | * |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 44 | * <p>It should be careful to access member variables on IO thread because |
| 45 | * it can be accessed from system thread as well. |
| 46 | * |
Jungshik Jang | 0792d37 | 2014-04-23 17:57:26 +0900 | [diff] [blame] | 47 | * <p>It can be created only by {@link HdmiCecController#create} |
| 48 | * |
| 49 | * <p>Declared as package-private, accessed by {@link HdmiControlService} only. |
| 50 | */ |
Jungshik Jang | a9095ba | 2014-05-02 13:06:22 +0900 | [diff] [blame] | 51 | final class HdmiCecController { |
Jungshik Jang | 0792d37 | 2014-04-23 17:57:26 +0900 | [diff] [blame] | 52 | private static final String TAG = "HdmiCecController"; |
| 53 | |
Jungshik Jang | 3ee6572 | 2014-06-03 16:22:30 +0900 | [diff] [blame] | 54 | /** |
| 55 | * Interface to report allocated logical address. |
| 56 | */ |
| 57 | interface AllocateAddressCallback { |
| 58 | /** |
| 59 | * Called when a new logical address is allocated. |
| 60 | * |
| 61 | * @param deviceType requested device type to allocate logical address |
| 62 | * @param logicalAddress allocated logical address. If it is |
Jungshik Jang | 4223072 | 2014-07-07 17:40:25 +0900 | [diff] [blame] | 63 | * {@link Constants#ADDR_UNREGISTERED}, it means that |
Jungshik Jang | 3ee6572 | 2014-06-03 16:22:30 +0900 | [diff] [blame] | 64 | * it failed to allocate logical address for the given device type |
| 65 | */ |
| 66 | void onAllocated(int deviceType, int logicalAddress); |
| 67 | } |
| 68 | |
Jungshik Jang | 3f74ab0 | 2014-04-30 14:31:02 +0900 | [diff] [blame] | 69 | private static final byte[] EMPTY_BODY = EmptyArray.BYTE; |
| 70 | |
Jungshik Jang | 3f74ab0 | 2014-04-30 14:31:02 +0900 | [diff] [blame] | 71 | private static final int NUM_LOGICAL_ADDRESS = 16; |
| 72 | |
Jungshik Jang | 0f8b4b7 | 2014-05-28 17:58:58 +0900 | [diff] [blame] | 73 | // Predicate for whether the given logical address is remote device's one or not. |
| 74 | private final Predicate<Integer> mRemoteDeviceAddressPredicate = new Predicate<Integer>() { |
| 75 | @Override |
| 76 | public boolean apply(Integer address) { |
| 77 | return !isAllocatedLocalDeviceAddress(address); |
| 78 | } |
| 79 | }; |
| 80 | |
| 81 | // Predicate whether the given logical address is system audio's one or not |
| 82 | private final Predicate<Integer> mSystemAudioAddressPredicate = new Predicate<Integer>() { |
| 83 | @Override |
| 84 | public boolean apply(Integer address) { |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 85 | return HdmiUtils.getTypeFromAddress(address) == Constants.ADDR_AUDIO_SYSTEM; |
Jungshik Jang | 0f8b4b7 | 2014-05-28 17:58:58 +0900 | [diff] [blame] | 86 | } |
| 87 | }; |
| 88 | |
Jungshik Jang | 0792d37 | 2014-04-23 17:57:26 +0900 | [diff] [blame] | 89 | // Handler instance to process synchronous I/O (mainly send) message. |
| 90 | private Handler mIoHandler; |
| 91 | |
| 92 | // Handler instance to process various messages coming from other CEC |
| 93 | // device or issued by internal state change. |
Jungshik Jang | e9c77c8 | 2014-04-24 20:30:09 +0900 | [diff] [blame] | 94 | private Handler mControlHandler; |
Jungshik Jang | 0792d37 | 2014-04-23 17:57:26 +0900 | [diff] [blame] | 95 | |
| 96 | // Stores the pointer to the native implementation of the service that |
| 97 | // interacts with HAL. |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 98 | private volatile long mNativePtr; |
Jungshik Jang | 0792d37 | 2014-04-23 17:57:26 +0900 | [diff] [blame] | 99 | |
Jungshik Jang | 7df5286 | 2014-08-11 14:35:27 +0900 | [diff] [blame] | 100 | private final HdmiControlService mService; |
Jungshik Jang | a1fa91f | 2014-05-08 20:56:41 +0900 | [diff] [blame] | 101 | |
Jinsuk Kim | 7fe2ae0 | 2014-05-26 17:33:05 +0900 | [diff] [blame] | 102 | // Stores the local CEC devices in the system. Device type is used for key. |
| 103 | private final SparseArray<HdmiCecLocalDevice> mLocalDevices = new SparseArray<>(); |
Jungshik Jang | 7d9a843 | 2014-04-29 15:12:43 +0900 | [diff] [blame] | 104 | |
Jungshik Jang | c94ac5c | 2014-08-27 13:48:37 +0900 | [diff] [blame] | 105 | @IoThreadOnly |
| 106 | private final HdmiLogger mIoThreadLogger = new HdmiLogger(TAG); |
| 107 | @ServiceThreadOnly |
| 108 | private final HdmiLogger mServiceThreadLogger = new HdmiLogger(TAG); |
| 109 | |
Jungshik Jang | 0792d37 | 2014-04-23 17:57:26 +0900 | [diff] [blame] | 110 | // Private constructor. Use HdmiCecController.create(). |
Jungshik Jang | 7df5286 | 2014-08-11 14:35:27 +0900 | [diff] [blame] | 111 | private HdmiCecController(HdmiControlService service) { |
| 112 | mService = service; |
Jungshik Jang | 0792d37 | 2014-04-23 17:57:26 +0900 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /** |
| 116 | * A factory method to get {@link HdmiCecController}. If it fails to initialize |
| 117 | * inner device or has no device it will return {@code null}. |
| 118 | * |
| 119 | * <p>Declared as package-private, accessed by {@link HdmiControlService} only. |
Jungshik Jang | e9c77c8 | 2014-04-24 20:30:09 +0900 | [diff] [blame] | 120 | * @param service {@link HdmiControlService} instance used to create internal handler |
| 121 | * and to pass callback for incoming message or event. |
Jungshik Jang | 0792d37 | 2014-04-23 17:57:26 +0900 | [diff] [blame] | 122 | * @return {@link HdmiCecController} if device is initialized successfully. Otherwise, |
| 123 | * returns {@code null}. |
| 124 | */ |
Jungshik Jang | e9c77c8 | 2014-04-24 20:30:09 +0900 | [diff] [blame] | 125 | static HdmiCecController create(HdmiControlService service) { |
Jungshik Jang | 7df5286 | 2014-08-11 14:35:27 +0900 | [diff] [blame] | 126 | HdmiCecController controller = new HdmiCecController(service); |
Jungshik Jang | 4085d0e | 2014-05-27 19:52:39 +0900 | [diff] [blame] | 127 | long nativePtr = nativeInit(controller, service.getServiceLooper().getQueue()); |
Jungshik Jang | 0792d37 | 2014-04-23 17:57:26 +0900 | [diff] [blame] | 128 | if (nativePtr == 0L) { |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 129 | controller = null; |
Jungshik Jang | 0792d37 | 2014-04-23 17:57:26 +0900 | [diff] [blame] | 130 | return null; |
| 131 | } |
| 132 | |
Jungshik Jang | 7df5286 | 2014-08-11 14:35:27 +0900 | [diff] [blame] | 133 | controller.init(nativePtr); |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 134 | return controller; |
| 135 | } |
| 136 | |
Jungshik Jang | 7df5286 | 2014-08-11 14:35:27 +0900 | [diff] [blame] | 137 | private void init(long nativePtr) { |
| 138 | mIoHandler = new Handler(mService.getServiceLooper()); |
| 139 | mControlHandler = new Handler(mService.getServiceLooper()); |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 140 | mNativePtr = nativePtr; |
Jungshik Jang | 0792d37 | 2014-04-23 17:57:26 +0900 | [diff] [blame] | 141 | } |
| 142 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 143 | @ServiceThreadOnly |
Jungshik Jang | 3ee6572 | 2014-06-03 16:22:30 +0900 | [diff] [blame] | 144 | void addLocalDevice(int deviceType, HdmiCecLocalDevice device) { |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 145 | assertRunOnServiceThread(); |
Jungshik Jang | 3ee6572 | 2014-06-03 16:22:30 +0900 | [diff] [blame] | 146 | mLocalDevices.put(deviceType, device); |
Jungshik Jang | 3f74ab0 | 2014-04-30 14:31:02 +0900 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Allocate a new logical address of the given device type. Allocated |
Jungshik Jang | 3ee6572 | 2014-06-03 16:22:30 +0900 | [diff] [blame] | 151 | * address will be reported through {@link AllocateAddressCallback}. |
Jungshik Jang | 3f74ab0 | 2014-04-30 14:31:02 +0900 | [diff] [blame] | 152 | * |
| 153 | * <p> Declared as package-private, accessed by {@link HdmiControlService} only. |
| 154 | * |
| 155 | * @param deviceType type of device to used to determine logical address |
| 156 | * @param preferredAddress a logical address preferred to be allocated. |
Jungshik Jang | 4223072 | 2014-07-07 17:40:25 +0900 | [diff] [blame] | 157 | * If sets {@link Constants#ADDR_UNREGISTERED}, scans |
Jungshik Jang | 3f74ab0 | 2014-04-30 14:31:02 +0900 | [diff] [blame] | 158 | * the smallest logical address matched with the given device type. |
| 159 | * Otherwise, scan address will start from {@code preferredAddress} |
| 160 | * @param callback callback interface to report allocated logical address to caller |
| 161 | */ |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 162 | @ServiceThreadOnly |
Jungshik Jang | d643f76 | 2014-05-22 19:28:09 +0900 | [diff] [blame] | 163 | void allocateLogicalAddress(final int deviceType, final int preferredAddress, |
Jungshik Jang | 3ee6572 | 2014-06-03 16:22:30 +0900 | [diff] [blame] | 164 | final AllocateAddressCallback callback) { |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 165 | assertRunOnServiceThread(); |
| 166 | |
Jungshik Jang | d643f76 | 2014-05-22 19:28:09 +0900 | [diff] [blame] | 167 | runOnIoThread(new Runnable() { |
| 168 | @Override |
| 169 | public void run() { |
| 170 | handleAllocateLogicalAddress(deviceType, preferredAddress, callback); |
| 171 | } |
| 172 | }); |
| 173 | } |
| 174 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 175 | @IoThreadOnly |
Jungshik Jang | d643f76 | 2014-05-22 19:28:09 +0900 | [diff] [blame] | 176 | private void handleAllocateLogicalAddress(final int deviceType, int preferredAddress, |
Jungshik Jang | 3ee6572 | 2014-06-03 16:22:30 +0900 | [diff] [blame] | 177 | final AllocateAddressCallback callback) { |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 178 | assertRunOnIoThread(); |
Jungshik Jang | d643f76 | 2014-05-22 19:28:09 +0900 | [diff] [blame] | 179 | int startAddress = preferredAddress; |
| 180 | // If preferred address is "unregistered", start address will be the smallest |
| 181 | // address matched with the given device type. |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 182 | if (preferredAddress == Constants.ADDR_UNREGISTERED) { |
Jungshik Jang | d643f76 | 2014-05-22 19:28:09 +0900 | [diff] [blame] | 183 | for (int i = 0; i < NUM_LOGICAL_ADDRESS; ++i) { |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 184 | if (deviceType == HdmiUtils.getTypeFromAddress(i)) { |
Jungshik Jang | d643f76 | 2014-05-22 19:28:09 +0900 | [diff] [blame] | 185 | startAddress = i; |
| 186 | break; |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 191 | int logicalAddress = Constants.ADDR_UNREGISTERED; |
Jungshik Jang | d643f76 | 2014-05-22 19:28:09 +0900 | [diff] [blame] | 192 | // Iterates all possible addresses which has the same device type. |
| 193 | for (int i = 0; i < NUM_LOGICAL_ADDRESS; ++i) { |
| 194 | int curAddress = (startAddress + i) % NUM_LOGICAL_ADDRESS; |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 195 | if (curAddress != Constants.ADDR_UNREGISTERED |
| 196 | && deviceType == HdmiUtils.getTypeFromAddress(curAddress)) { |
Jungshik Jang | 8e93c84 | 2014-08-06 15:48:33 +0900 | [diff] [blame] | 197 | int failedPollingCount = 0; |
| 198 | for (int j = 0; j < HdmiConfig.ADDRESS_ALLOCATION_RETRY; ++j) { |
| 199 | if (!sendPollMessage(curAddress, curAddress, 1)) { |
| 200 | failedPollingCount++; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | // Pick logical address if failed ratio is more than a half of all retries. |
| 205 | if (failedPollingCount * 2 > HdmiConfig.ADDRESS_ALLOCATION_RETRY) { |
Jungshik Jang | d643f76 | 2014-05-22 19:28:09 +0900 | [diff] [blame] | 206 | logicalAddress = curAddress; |
| 207 | break; |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | final int assignedAddress = logicalAddress; |
Jungshik Jang | c94ac5c | 2014-08-27 13:48:37 +0900 | [diff] [blame] | 213 | mIoThreadLogger.debug( |
| 214 | String.format("New logical address for device [%d]: [preferred:%d, assigned:%d]", |
| 215 | deviceType, preferredAddress, assignedAddress)); |
Jungshik Jang | d643f76 | 2014-05-22 19:28:09 +0900 | [diff] [blame] | 216 | if (callback != null) { |
| 217 | runOnServiceThread(new Runnable() { |
Jungshik Jang | 8e93c84 | 2014-08-06 15:48:33 +0900 | [diff] [blame] | 218 | @Override |
Jungshik Jang | d643f76 | 2014-05-22 19:28:09 +0900 | [diff] [blame] | 219 | public void run() { |
| 220 | callback.onAllocated(deviceType, assignedAddress); |
| 221 | } |
| 222 | }); |
| 223 | } |
Jungshik Jang | 3f74ab0 | 2014-04-30 14:31:02 +0900 | [diff] [blame] | 224 | } |
| 225 | |
Jungshik Jang | e9c77c8 | 2014-04-24 20:30:09 +0900 | [diff] [blame] | 226 | private static byte[] buildBody(int opcode, byte[] params) { |
| 227 | byte[] body = new byte[params.length + 1]; |
| 228 | body[0] = (byte) opcode; |
| 229 | System.arraycopy(params, 0, body, 1, params.length); |
| 230 | return body; |
| 231 | } |
Jungshik Jang | 0792d37 | 2014-04-23 17:57:26 +0900 | [diff] [blame] | 232 | |
Jungshik Jang | 7d9a843 | 2014-04-29 15:12:43 +0900 | [diff] [blame] | 233 | |
Jinsuk Kim | 0340bbc | 2014-06-05 11:07:47 +0900 | [diff] [blame] | 234 | HdmiPortInfo[] getPortInfos() { |
| 235 | return nativeGetPortInfos(mNativePtr); |
| 236 | } |
| 237 | |
Jungshik Jang | a9095ba | 2014-05-02 13:06:22 +0900 | [diff] [blame] | 238 | /** |
Jinsuk Kim | 7fe2ae0 | 2014-05-26 17:33:05 +0900 | [diff] [blame] | 239 | * Return the locally hosted logical device of a given type. |
| 240 | * |
| 241 | * @param deviceType logical device type |
| 242 | * @return {@link HdmiCecLocalDevice} instance if the instance of the type is available; |
| 243 | * otherwise null. |
| 244 | */ |
| 245 | HdmiCecLocalDevice getLocalDevice(int deviceType) { |
| 246 | return mLocalDevices.get(deviceType); |
| 247 | } |
| 248 | |
| 249 | /** |
Jungshik Jang | a9095ba | 2014-05-02 13:06:22 +0900 | [diff] [blame] | 250 | * Add a new logical address to the device. Device's HW should be notified |
| 251 | * when a new logical address is assigned to a device, so that it can accept |
| 252 | * a command having available destinations. |
| 253 | * |
| 254 | * <p>Declared as package-private. accessed by {@link HdmiControlService} only. |
| 255 | * |
| 256 | * @param newLogicalAddress a logical address to be added |
| 257 | * @return 0 on success. Otherwise, returns negative value |
| 258 | */ |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 259 | @ServiceThreadOnly |
Jungshik Jang | a9095ba | 2014-05-02 13:06:22 +0900 | [diff] [blame] | 260 | int addLogicalAddress(int newLogicalAddress) { |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 261 | assertRunOnServiceThread(); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 262 | if (HdmiUtils.isValidAddress(newLogicalAddress)) { |
Jungshik Jang | a9095ba | 2014-05-02 13:06:22 +0900 | [diff] [blame] | 263 | return nativeAddLogicalAddress(mNativePtr, newLogicalAddress); |
| 264 | } else { |
| 265 | return -1; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Clear all logical addresses registered in the device. |
| 271 | * |
| 272 | * <p>Declared as package-private. accessed by {@link HdmiControlService} only. |
| 273 | */ |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 274 | @ServiceThreadOnly |
Jungshik Jang | a9095ba | 2014-05-02 13:06:22 +0900 | [diff] [blame] | 275 | void clearLogicalAddress() { |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 276 | assertRunOnServiceThread(); |
Jinsuk Kim | 7fe2ae0 | 2014-05-26 17:33:05 +0900 | [diff] [blame] | 277 | for (int i = 0; i < mLocalDevices.size(); ++i) { |
| 278 | mLocalDevices.valueAt(i).clearAddress(); |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 279 | } |
Jungshik Jang | a9095ba | 2014-05-02 13:06:22 +0900 | [diff] [blame] | 280 | nativeClearLogicalAddress(mNativePtr); |
| 281 | } |
| 282 | |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 283 | @ServiceThreadOnly |
| 284 | void clearLocalDevices() { |
| 285 | assertRunOnServiceThread(); |
| 286 | mLocalDevices.clear(); |
| 287 | } |
| 288 | |
Jungshik Jang | a9095ba | 2014-05-02 13:06:22 +0900 | [diff] [blame] | 289 | /** |
| 290 | * Return the physical address of the device. |
| 291 | * |
| 292 | * <p>Declared as package-private. accessed by {@link HdmiControlService} only. |
| 293 | * |
| 294 | * @return CEC physical address of the device. The range of success address |
| 295 | * is between 0x0000 and 0xFFFF. If failed it returns -1 |
| 296 | */ |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 297 | @ServiceThreadOnly |
Jungshik Jang | a9095ba | 2014-05-02 13:06:22 +0900 | [diff] [blame] | 298 | int getPhysicalAddress() { |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 299 | assertRunOnServiceThread(); |
Jungshik Jang | a9095ba | 2014-05-02 13:06:22 +0900 | [diff] [blame] | 300 | return nativeGetPhysicalAddress(mNativePtr); |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Return CEC version of the device. |
| 305 | * |
| 306 | * <p>Declared as package-private. accessed by {@link HdmiControlService} only. |
| 307 | */ |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 308 | @ServiceThreadOnly |
Jungshik Jang | a9095ba | 2014-05-02 13:06:22 +0900 | [diff] [blame] | 309 | int getVersion() { |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 310 | assertRunOnServiceThread(); |
Jungshik Jang | a9095ba | 2014-05-02 13:06:22 +0900 | [diff] [blame] | 311 | return nativeGetVersion(mNativePtr); |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Return vendor id of the device. |
| 316 | * |
| 317 | * <p>Declared as package-private. accessed by {@link HdmiControlService} only. |
| 318 | */ |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 319 | @ServiceThreadOnly |
Jungshik Jang | a9095ba | 2014-05-02 13:06:22 +0900 | [diff] [blame] | 320 | int getVendorId() { |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 321 | assertRunOnServiceThread(); |
Jungshik Jang | a9095ba | 2014-05-02 13:06:22 +0900 | [diff] [blame] | 322 | return nativeGetVendorId(mNativePtr); |
| 323 | } |
| 324 | |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 325 | /** |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 326 | * Set an option to CEC HAL. |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 327 | * |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 328 | * @param flag key of option |
| 329 | * @param value value of option |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 330 | */ |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 331 | @ServiceThreadOnly |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 332 | void setOption(int flag, int value) { |
| 333 | assertRunOnServiceThread(); |
| 334 | nativeSetOption(mNativePtr, flag, value); |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Configure ARC circuit in the hardware logic to start or stop the feature. |
| 339 | * |
| 340 | * @param enabled whether to enable/disable ARC |
| 341 | */ |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 342 | @ServiceThreadOnly |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 343 | void setAudioReturnChannel(boolean enabled) { |
| 344 | assertRunOnServiceThread(); |
| 345 | nativeSetAudioReturnChannel(mNativePtr, enabled); |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Return the connection status of the specified port |
| 350 | * |
| 351 | * @param port port number to check connection status |
| 352 | * @return true if connected; otherwise, return false |
| 353 | */ |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 354 | @ServiceThreadOnly |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 355 | boolean isConnected(int port) { |
| 356 | assertRunOnServiceThread(); |
| 357 | return nativeIsConnected(mNativePtr, port); |
| 358 | } |
| 359 | |
| 360 | /** |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 361 | * Poll all remote devices. It sends <Polling Message> to all remote |
| 362 | * devices. |
| 363 | * |
| 364 | * <p>Declared as package-private. accessed by {@link HdmiControlService} only. |
| 365 | * |
| 366 | * @param callback an interface used to get a list of all remote devices' address |
Jungshik Jang | 1de5142 | 2014-07-03 11:14:26 +0900 | [diff] [blame] | 367 | * @param sourceAddress a logical address of source device where sends polling message |
Jungshik Jang | 0f8b4b7 | 2014-05-28 17:58:58 +0900 | [diff] [blame] | 368 | * @param pickStrategy strategy how to pick polling candidates |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 369 | * @param retryCount the number of retry used to send polling message to remote devices |
| 370 | */ |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 371 | @ServiceThreadOnly |
Jungshik Jang | 1de5142 | 2014-07-03 11:14:26 +0900 | [diff] [blame] | 372 | void pollDevices(DevicePollingCallback callback, int sourceAddress, int pickStrategy, |
| 373 | int retryCount) { |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 374 | assertRunOnServiceThread(); |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 375 | |
Jungshik Jang | 0f8b4b7 | 2014-05-28 17:58:58 +0900 | [diff] [blame] | 376 | // Extract polling candidates. No need to poll against local devices. |
| 377 | List<Integer> pollingCandidates = pickPollCandidates(pickStrategy); |
Jungshik Jang | 1de5142 | 2014-07-03 11:14:26 +0900 | [diff] [blame] | 378 | runDevicePolling(sourceAddress, pollingCandidates, retryCount, callback); |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 379 | } |
| 380 | |
Jungshik Jang | cc5ef8c | 2014-05-27 13:27:36 +0900 | [diff] [blame] | 381 | /** |
| 382 | * Return a list of all {@link HdmiCecLocalDevice}s. |
| 383 | * |
| 384 | * <p>Declared as package-private. accessed by {@link HdmiControlService} only. |
| 385 | */ |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 386 | @ServiceThreadOnly |
Jungshik Jang | cc5ef8c | 2014-05-27 13:27:36 +0900 | [diff] [blame] | 387 | List<HdmiCecLocalDevice> getLocalDeviceList() { |
| 388 | assertRunOnServiceThread(); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 389 | return HdmiUtils.sparseArrayToList(mLocalDevices); |
Jungshik Jang | cc5ef8c | 2014-05-27 13:27:36 +0900 | [diff] [blame] | 390 | } |
| 391 | |
Jungshik Jang | 0f8b4b7 | 2014-05-28 17:58:58 +0900 | [diff] [blame] | 392 | private List<Integer> pickPollCandidates(int pickStrategy) { |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 393 | int strategy = pickStrategy & Constants.POLL_STRATEGY_MASK; |
Jungshik Jang | 0f8b4b7 | 2014-05-28 17:58:58 +0900 | [diff] [blame] | 394 | Predicate<Integer> pickPredicate = null; |
| 395 | switch (strategy) { |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 396 | case Constants.POLL_STRATEGY_SYSTEM_AUDIO: |
Jungshik Jang | 0f8b4b7 | 2014-05-28 17:58:58 +0900 | [diff] [blame] | 397 | pickPredicate = mSystemAudioAddressPredicate; |
| 398 | break; |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 399 | case Constants.POLL_STRATEGY_REMOTES_DEVICES: |
Jungshik Jang | 0f8b4b7 | 2014-05-28 17:58:58 +0900 | [diff] [blame] | 400 | default: // The default is POLL_STRATEGY_REMOTES_DEVICES. |
| 401 | pickPredicate = mRemoteDeviceAddressPredicate; |
| 402 | break; |
| 403 | } |
| 404 | |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 405 | int iterationStrategy = pickStrategy & Constants.POLL_ITERATION_STRATEGY_MASK; |
Jungshik Jang | 0f8b4b7 | 2014-05-28 17:58:58 +0900 | [diff] [blame] | 406 | ArrayList<Integer> pollingCandidates = new ArrayList<>(); |
| 407 | switch (iterationStrategy) { |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 408 | case Constants.POLL_ITERATION_IN_ORDER: |
| 409 | for (int i = Constants.ADDR_TV; i <= Constants.ADDR_SPECIFIC_USE; ++i) { |
Jungshik Jang | 0f8b4b7 | 2014-05-28 17:58:58 +0900 | [diff] [blame] | 410 | if (pickPredicate.apply(i)) { |
| 411 | pollingCandidates.add(i); |
| 412 | } |
| 413 | } |
| 414 | break; |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 415 | case Constants.POLL_ITERATION_REVERSE_ORDER: |
Jungshik Jang | 0f8b4b7 | 2014-05-28 17:58:58 +0900 | [diff] [blame] | 416 | default: // The default is reverse order. |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 417 | for (int i = Constants.ADDR_SPECIFIC_USE; i >= Constants.ADDR_TV; --i) { |
Jungshik Jang | 0f8b4b7 | 2014-05-28 17:58:58 +0900 | [diff] [blame] | 418 | if (pickPredicate.apply(i)) { |
| 419 | pollingCandidates.add(i); |
| 420 | } |
| 421 | } |
| 422 | break; |
| 423 | } |
| 424 | return pollingCandidates; |
| 425 | } |
| 426 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 427 | @ServiceThreadOnly |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 428 | private boolean isAllocatedLocalDeviceAddress(int address) { |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 429 | assertRunOnServiceThread(); |
Jinsuk Kim | 7fe2ae0 | 2014-05-26 17:33:05 +0900 | [diff] [blame] | 430 | for (int i = 0; i < mLocalDevices.size(); ++i) { |
| 431 | if (mLocalDevices.valueAt(i).isAddressOf(address)) { |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 432 | return true; |
| 433 | } |
| 434 | } |
| 435 | return false; |
| 436 | } |
| 437 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 438 | @ServiceThreadOnly |
Jungshik Jang | 1de5142 | 2014-07-03 11:14:26 +0900 | [diff] [blame] | 439 | private void runDevicePolling(final int sourceAddress, |
| 440 | final List<Integer> candidates, final int retryCount, |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 441 | final DevicePollingCallback callback) { |
| 442 | assertRunOnServiceThread(); |
| 443 | runOnIoThread(new Runnable() { |
| 444 | @Override |
| 445 | public void run() { |
| 446 | final ArrayList<Integer> allocated = new ArrayList<>(); |
| 447 | for (Integer address : candidates) { |
Jungshik Jang | 1de5142 | 2014-07-03 11:14:26 +0900 | [diff] [blame] | 448 | if (sendPollMessage(sourceAddress, address, retryCount)) { |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 449 | allocated.add(address); |
| 450 | } |
| 451 | } |
Jungshik Jang | c94ac5c | 2014-08-27 13:48:37 +0900 | [diff] [blame] | 452 | mIoThreadLogger.debug("DevicePollingResult:" + allocated); |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 453 | if (callback != null) { |
| 454 | runOnServiceThread(new Runnable() { |
| 455 | @Override |
| 456 | public void run() { |
| 457 | callback.onPollingFinished(allocated); |
| 458 | } |
| 459 | }); |
| 460 | } |
| 461 | } |
| 462 | }); |
| 463 | } |
| 464 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 465 | @IoThreadOnly |
Jungshik Jang | 1de5142 | 2014-07-03 11:14:26 +0900 | [diff] [blame] | 466 | private boolean sendPollMessage(int sourceAddress, int destinationAddress, int retryCount) { |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 467 | assertRunOnIoThread(); |
| 468 | for (int i = 0; i < retryCount; ++i) { |
Jungshik Jang | 1de5142 | 2014-07-03 11:14:26 +0900 | [diff] [blame] | 469 | // <Polling Message> is a message which has empty body. |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 470 | // If sending <Polling Message> failed (NAK), it becomes |
| 471 | // new logical address for the device because no device uses |
| 472 | // it as logical address of the device. |
Jungshik Jang | 1de5142 | 2014-07-03 11:14:26 +0900 | [diff] [blame] | 473 | if (nativeSendCecCommand(mNativePtr, sourceAddress, destinationAddress, EMPTY_BODY) |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 474 | == Constants.SEND_RESULT_SUCCESS) { |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 475 | return true; |
| 476 | } |
| 477 | } |
| 478 | return false; |
| 479 | } |
| 480 | |
| 481 | private void assertRunOnIoThread() { |
| 482 | if (Looper.myLooper() != mIoHandler.getLooper()) { |
| 483 | throw new IllegalStateException("Should run on io thread."); |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | private void assertRunOnServiceThread() { |
| 488 | if (Looper.myLooper() != mControlHandler.getLooper()) { |
| 489 | throw new IllegalStateException("Should run on service thread."); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | // Run a Runnable on IO thread. |
| 494 | // It should be careful to access member variables on IO thread because |
| 495 | // it can be accessed from system thread as well. |
Jungshik Jang | d643f76 | 2014-05-22 19:28:09 +0900 | [diff] [blame] | 496 | private void runOnIoThread(Runnable runnable) { |
| 497 | mIoHandler.post(runnable); |
| 498 | } |
| 499 | |
| 500 | private void runOnServiceThread(Runnable runnable) { |
| 501 | mControlHandler.post(runnable); |
| 502 | } |
| 503 | |
Jungshik Jang | a1fa91f | 2014-05-08 20:56:41 +0900 | [diff] [blame] | 504 | private boolean isAcceptableAddress(int address) { |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 505 | // Can access command targeting devices available in local device or broadcast command. |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 506 | if (address == Constants.ADDR_BROADCAST) { |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 507 | return true; |
| 508 | } |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 509 | return isAllocatedLocalDeviceAddress(address); |
Jungshik Jang | a1fa91f | 2014-05-08 20:56:41 +0900 | [diff] [blame] | 510 | } |
| 511 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 512 | @ServiceThreadOnly |
Jungshik Jang | e9c77c8 | 2014-04-24 20:30:09 +0900 | [diff] [blame] | 513 | private void onReceiveCommand(HdmiCecMessage message) { |
Jungshik Jang | 02bb426 | 2014-05-23 16:48:31 +0900 | [diff] [blame] | 514 | assertRunOnServiceThread(); |
Yuncheol Heo | 6aae652 | 2014-08-05 14:48:37 +0900 | [diff] [blame] | 515 | if (isAcceptableAddress(message.getDestination()) && mService.handleCecCommand(message)) { |
Jungshik Jang | a1fa91f | 2014-05-08 20:56:41 +0900 | [diff] [blame] | 516 | return; |
| 517 | } |
Yuncheol Heo | 6aae652 | 2014-08-05 14:48:37 +0900 | [diff] [blame] | 518 | // Not handled message, so we will reply it with <Feature Abort>. |
| 519 | maySendFeatureAbortCommand(message, Constants.ABORT_UNRECOGNIZED_OPCODE); |
| 520 | } |
Jungshik Jang | 1827fdc | 2014-07-17 13:58:14 +0900 | [diff] [blame] | 521 | |
Yuncheol Heo | 6aae652 | 2014-08-05 14:48:37 +0900 | [diff] [blame] | 522 | @ServiceThreadOnly |
| 523 | void maySendFeatureAbortCommand(HdmiCecMessage message, int reason) { |
| 524 | assertRunOnServiceThread(); |
| 525 | // Swap the source and the destination. |
| 526 | int src = message.getDestination(); |
| 527 | int dest = message.getSource(); |
| 528 | if (src == Constants.ADDR_BROADCAST || dest == Constants.ADDR_UNREGISTERED) { |
| 529 | // Don't reply <Feature Abort> from the unregistered devices or for the broadcasted |
| 530 | // messages. See CEC 12.2 Protocol General Rules for detail. |
| 531 | return; |
| 532 | } |
| 533 | int originalOpcode = message.getOpcode(); |
| 534 | if (originalOpcode == Constants.MESSAGE_FEATURE_ABORT) { |
| 535 | return; |
| 536 | } |
| 537 | sendCommand( |
| 538 | HdmiCecMessageBuilder.buildFeatureAbortCommand(src, dest, originalOpcode, reason)); |
Jungshik Jang | e9c77c8 | 2014-04-24 20:30:09 +0900 | [diff] [blame] | 539 | } |
| 540 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 541 | @ServiceThreadOnly |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 542 | void sendCommand(HdmiCecMessage cecMessage) { |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 543 | assertRunOnServiceThread(); |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 544 | sendCommand(cecMessage, null); |
| 545 | } |
| 546 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 547 | @ServiceThreadOnly |
Jungshik Jang | d643f76 | 2014-05-22 19:28:09 +0900 | [diff] [blame] | 548 | void sendCommand(final HdmiCecMessage cecMessage, |
| 549 | final HdmiControlService.SendMessageCallback callback) { |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 550 | assertRunOnServiceThread(); |
Jungshik Jang | d643f76 | 2014-05-22 19:28:09 +0900 | [diff] [blame] | 551 | runOnIoThread(new Runnable() { |
| 552 | @Override |
| 553 | public void run() { |
Jungshik Jang | c94ac5c | 2014-08-27 13:48:37 +0900 | [diff] [blame] | 554 | mIoThreadLogger.debug("SendCommand:" + cecMessage); |
Jungshik Jang | d643f76 | 2014-05-22 19:28:09 +0900 | [diff] [blame] | 555 | byte[] body = buildBody(cecMessage.getOpcode(), cecMessage.getParams()); |
Jungshik Jang | 8ed86c4 | 2014-07-11 11:56:46 +0900 | [diff] [blame] | 556 | int i = 0; |
| 557 | int errorCode = Constants.SEND_RESULT_SUCCESS; |
| 558 | do { |
| 559 | errorCode = nativeSendCecCommand(mNativePtr, cecMessage.getSource(), |
| 560 | cecMessage.getDestination(), body); |
| 561 | if (errorCode == Constants.SEND_RESULT_SUCCESS) { |
| 562 | break; |
| 563 | } |
| 564 | } while (i++ < HdmiConfig.RETRANSMISSION_COUNT); |
| 565 | |
| 566 | final int finalError = errorCode; |
| 567 | if (finalError != Constants.SEND_RESULT_SUCCESS) { |
Yuncheol Heo | ece603b | 2014-05-23 20:10:19 +0900 | [diff] [blame] | 568 | Slog.w(TAG, "Failed to send " + cecMessage); |
| 569 | } |
Jungshik Jang | d643f76 | 2014-05-22 19:28:09 +0900 | [diff] [blame] | 570 | if (callback != null) { |
| 571 | runOnServiceThread(new Runnable() { |
| 572 | @Override |
| 573 | public void run() { |
Jungshik Jang | 8ed86c4 | 2014-07-11 11:56:46 +0900 | [diff] [blame] | 574 | callback.onSendCompleted(finalError); |
Jungshik Jang | d643f76 | 2014-05-22 19:28:09 +0900 | [diff] [blame] | 575 | } |
| 576 | }); |
| 577 | } |
| 578 | } |
| 579 | }); |
Jungshik Jang | e9c77c8 | 2014-04-24 20:30:09 +0900 | [diff] [blame] | 580 | } |
| 581 | |
Jungshik Jang | 0792d37 | 2014-04-23 17:57:26 +0900 | [diff] [blame] | 582 | /** |
Jungshik Jang | e9c77c8 | 2014-04-24 20:30:09 +0900 | [diff] [blame] | 583 | * Called by native when incoming CEC message arrived. |
Jungshik Jang | 0792d37 | 2014-04-23 17:57:26 +0900 | [diff] [blame] | 584 | */ |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 585 | @ServiceThreadOnly |
Jungshik Jang | e9c77c8 | 2014-04-24 20:30:09 +0900 | [diff] [blame] | 586 | private void handleIncomingCecCommand(int srcAddress, int dstAddress, byte[] body) { |
Jungshik Jang | 4085d0e | 2014-05-27 19:52:39 +0900 | [diff] [blame] | 587 | assertRunOnServiceThread(); |
Jungshik Jang | c94ac5c | 2014-08-27 13:48:37 +0900 | [diff] [blame] | 588 | HdmiCecMessage command = HdmiCecMessageBuilder.of(srcAddress, dstAddress, body); |
| 589 | mServiceThreadLogger.debug("ReceiveCommand:" + command); |
| 590 | onReceiveCommand(command); |
Jungshik Jang | e9c77c8 | 2014-04-24 20:30:09 +0900 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | /** |
| 594 | * Called by native when a hotplug event issues. |
| 595 | */ |
Jungshik Jang | 4223072 | 2014-07-07 17:40:25 +0900 | [diff] [blame] | 596 | @ServiceThreadOnly |
| 597 | private void handleHotplug(int port, boolean connected) { |
| 598 | assertRunOnServiceThread(); |
Jungshik Jang | c94ac5c | 2014-08-27 13:48:37 +0900 | [diff] [blame] | 599 | mServiceThreadLogger.debug( |
| 600 | "Hotplug event:[port:" + port + " , connected:" + connected + "]"); |
Jungshik Jang | 4223072 | 2014-07-07 17:40:25 +0900 | [diff] [blame] | 601 | mService.onHotplug(port, connected); |
Jungshik Jang | 0792d37 | 2014-04-23 17:57:26 +0900 | [diff] [blame] | 602 | } |
| 603 | |
Terry Heo | 959d2db | 2014-08-28 16:45:41 +0900 | [diff] [blame^] | 604 | void dump(final IndentingPrintWriter pw) { |
| 605 | for (int i = 0; i < mLocalDevices.size(); ++i) { |
| 606 | pw.println("HdmiCecLocalDevice #" + i + ":"); |
| 607 | pw.increaseIndent(); |
| 608 | mLocalDevices.valueAt(i).dump(pw); |
| 609 | pw.decreaseIndent(); |
| 610 | } |
| 611 | } |
| 612 | |
Jungshik Jang | 4085d0e | 2014-05-27 19:52:39 +0900 | [diff] [blame] | 613 | private static native long nativeInit(HdmiCecController handler, MessageQueue messageQueue); |
Jungshik Jang | a9095ba | 2014-05-02 13:06:22 +0900 | [diff] [blame] | 614 | private static native int nativeSendCecCommand(long controllerPtr, int srcAddress, |
Jungshik Jang | e9c77c8 | 2014-04-24 20:30:09 +0900 | [diff] [blame] | 615 | int dstAddress, byte[] body); |
Jungshik Jang | a9095ba | 2014-05-02 13:06:22 +0900 | [diff] [blame] | 616 | private static native int nativeAddLogicalAddress(long controllerPtr, int logicalAddress); |
| 617 | private static native void nativeClearLogicalAddress(long controllerPtr); |
| 618 | private static native int nativeGetPhysicalAddress(long controllerPtr); |
| 619 | private static native int nativeGetVersion(long controllerPtr); |
| 620 | private static native int nativeGetVendorId(long controllerPtr); |
Jinsuk Kim | 0340bbc | 2014-06-05 11:07:47 +0900 | [diff] [blame] | 621 | private static native HdmiPortInfo[] nativeGetPortInfos(long controllerPtr); |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 622 | private static native void nativeSetOption(long controllerPtr, int flag, int value); |
| 623 | private static native void nativeSetAudioReturnChannel(long controllerPtr, boolean flag); |
| 624 | private static native boolean nativeIsConnected(long controllerPtr, int port); |
Jungshik Jang | 0792d37 | 2014-04-23 17:57:26 +0900 | [diff] [blame] | 625 | } |