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