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