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