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