blob: 10e4b6e386c75c16d14290529c68ab3278f48edd [file] [log] [blame]
Jungshik Jang0792d372014-04-23 17:57:26 +09001/*
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
17package com.android.server.hdmi;
18
Jinsuk Kim0340bbc2014-06-05 11:07:47 +090019import android.hardware.hdmi.HdmiPortInfo;
Jungshik Jang0792d372014-04-23 17:57:26 +090020import android.os.Handler;
Jungshik Jang02bb4262014-05-23 16:48:31 +090021import android.os.Looper;
Jungshik Jang4085d0e2014-05-27 19:52:39 +090022import android.os.MessageQueue;
Yuncheol Heoece603b2014-05-23 20:10:19 +090023import android.util.Slog;
Jungshik Jang7d9a8432014-04-29 15:12:43 +090024import android.util.SparseArray;
Jungshik Jange9c77c82014-04-24 20:30:09 +090025
Jungshik Jang0f8b4b72014-05-28 17:58:58 +090026import com.android.internal.util.Predicate;
Jungshik Janga5b74142014-06-23 18:03:10 +090027import com.android.server.hdmi.HdmiAnnotations.IoThreadOnly;
28import com.android.server.hdmi.HdmiAnnotations.ServiceThreadOnly;
Jungshik Jang02bb4262014-05-23 16:48:31 +090029import com.android.server.hdmi.HdmiControlService.DevicePollingCallback;
30
Jungshik Jang3f74ab02014-04-30 14:31:02 +090031import libcore.util.EmptyArray;
32
Jungshik Jang7d9a8432014-04-29 15:12:43 +090033import java.util.ArrayList;
Jungshik Jang7d9a8432014-04-29 15:12:43 +090034import java.util.List;
Jungshik Jang0792d372014-04-23 17:57:26 +090035
36/**
37 * Manages HDMI-CEC command and behaviors. It converts user's command into CEC command
38 * and pass it to CEC HAL so that it sends message to other device. For incoming
39 * message it translates the message and delegates it to proper module.
40 *
Jungshik Jang02bb4262014-05-23 16:48:31 +090041 * <p>It should be careful to access member variables on IO thread because
42 * it can be accessed from system thread as well.
43 *
Jungshik Jang0792d372014-04-23 17:57:26 +090044 * <p>It can be created only by {@link HdmiCecController#create}
45 *
46 * <p>Declared as package-private, accessed by {@link HdmiControlService} only.
47 */
Jungshik Janga9095ba2014-05-02 13:06:22 +090048final class HdmiCecController {
Jungshik Jang0792d372014-04-23 17:57:26 +090049 private static final String TAG = "HdmiCecController";
50
Jungshik Jang3ee65722014-06-03 16:22:30 +090051 /**
52 * Interface to report allocated logical address.
53 */
54 interface AllocateAddressCallback {
55 /**
56 * Called when a new logical address is allocated.
57 *
58 * @param deviceType requested device type to allocate logical address
59 * @param logicalAddress allocated logical address. If it is
Jungshik Jang42230722014-07-07 17:40:25 +090060 * {@link Constants#ADDR_UNREGISTERED}, it means that
Jungshik Jang3ee65722014-06-03 16:22:30 +090061 * it failed to allocate logical address for the given device type
62 */
63 void onAllocated(int deviceType, int logicalAddress);
64 }
65
Jungshik Jang3f74ab02014-04-30 14:31:02 +090066 private static final byte[] EMPTY_BODY = EmptyArray.BYTE;
67
Jungshik Jang3f74ab02014-04-30 14:31:02 +090068 private static final int NUM_LOGICAL_ADDRESS = 16;
69
Jungshik Jang0f8b4b72014-05-28 17:58:58 +090070 // Predicate for whether the given logical address is remote device's one or not.
71 private final Predicate<Integer> mRemoteDeviceAddressPredicate = new Predicate<Integer>() {
72 @Override
73 public boolean apply(Integer address) {
74 return !isAllocatedLocalDeviceAddress(address);
75 }
76 };
77
78 // Predicate whether the given logical address is system audio's one or not
79 private final Predicate<Integer> mSystemAudioAddressPredicate = new Predicate<Integer>() {
80 @Override
81 public boolean apply(Integer address) {
Jinsuk Kimc0c20d02014-07-04 14:34:31 +090082 return HdmiUtils.getTypeFromAddress(address) == Constants.ADDR_AUDIO_SYSTEM;
Jungshik Jang0f8b4b72014-05-28 17:58:58 +090083 }
84 };
85
Jungshik Jang0792d372014-04-23 17:57:26 +090086 // Handler instance to process synchronous I/O (mainly send) message.
87 private Handler mIoHandler;
88
89 // Handler instance to process various messages coming from other CEC
90 // device or issued by internal state change.
Jungshik Jange9c77c82014-04-24 20:30:09 +090091 private Handler mControlHandler;
Jungshik Jang0792d372014-04-23 17:57:26 +090092
93 // Stores the pointer to the native implementation of the service that
94 // interacts with HAL.
Jungshik Jang02bb4262014-05-23 16:48:31 +090095 private volatile long mNativePtr;
Jungshik Jang0792d372014-04-23 17:57:26 +090096
Jungshik Janga1fa91f2014-05-08 20:56:41 +090097 private HdmiControlService mService;
98
Jinsuk Kim7fe2ae02014-05-26 17:33:05 +090099 // Stores the local CEC devices in the system. Device type is used for key.
100 private final SparseArray<HdmiCecLocalDevice> mLocalDevices = new SparseArray<>();
Jungshik Jang7d9a8432014-04-29 15:12:43 +0900101
Jungshik Jang0792d372014-04-23 17:57:26 +0900102 // Private constructor. Use HdmiCecController.create().
103 private HdmiCecController() {
104 }
105
106 /**
107 * A factory method to get {@link HdmiCecController}. If it fails to initialize
108 * inner device or has no device it will return {@code null}.
109 *
110 * <p>Declared as package-private, accessed by {@link HdmiControlService} only.
Jungshik Jange9c77c82014-04-24 20:30:09 +0900111 * @param service {@link HdmiControlService} instance used to create internal handler
112 * and to pass callback for incoming message or event.
Jungshik Jang0792d372014-04-23 17:57:26 +0900113 * @return {@link HdmiCecController} if device is initialized successfully. Otherwise,
114 * returns {@code null}.
115 */
Jungshik Jange9c77c82014-04-24 20:30:09 +0900116 static HdmiCecController create(HdmiControlService service) {
Jinsuk Kim2918e9e2014-05-20 16:45:45 +0900117 HdmiCecController controller = new HdmiCecController();
Jungshik Jang4085d0e2014-05-27 19:52:39 +0900118 long nativePtr = nativeInit(controller, service.getServiceLooper().getQueue());
Jungshik Jang0792d372014-04-23 17:57:26 +0900119 if (nativePtr == 0L) {
Jinsuk Kim2918e9e2014-05-20 16:45:45 +0900120 controller = null;
Jungshik Jang0792d372014-04-23 17:57:26 +0900121 return null;
122 }
123
Jinsuk Kim2918e9e2014-05-20 16:45:45 +0900124 controller.init(service, nativePtr);
125 return controller;
126 }
127
128 private void init(HdmiControlService service, long nativePtr) {
129 mService = service;
130 mIoHandler = new Handler(service.getServiceLooper());
131 mControlHandler = new Handler(service.getServiceLooper());
132 mNativePtr = nativePtr;
Jungshik Jang0792d372014-04-23 17:57:26 +0900133 }
134
Jungshik Janga5b74142014-06-23 18:03:10 +0900135 @ServiceThreadOnly
Jungshik Jang3ee65722014-06-03 16:22:30 +0900136 void addLocalDevice(int deviceType, HdmiCecLocalDevice device) {
Jungshik Janga5b74142014-06-23 18:03:10 +0900137 assertRunOnServiceThread();
Jungshik Jang3ee65722014-06-03 16:22:30 +0900138 mLocalDevices.put(deviceType, device);
Jungshik Jang3f74ab02014-04-30 14:31:02 +0900139 }
140
141 /**
142 * Allocate a new logical address of the given device type. Allocated
Jungshik Jang3ee65722014-06-03 16:22:30 +0900143 * address will be reported through {@link AllocateAddressCallback}.
Jungshik Jang3f74ab02014-04-30 14:31:02 +0900144 *
145 * <p> Declared as package-private, accessed by {@link HdmiControlService} only.
146 *
147 * @param deviceType type of device to used to determine logical address
148 * @param preferredAddress a logical address preferred to be allocated.
Jungshik Jang42230722014-07-07 17:40:25 +0900149 * If sets {@link Constants#ADDR_UNREGISTERED}, scans
Jungshik Jang3f74ab02014-04-30 14:31:02 +0900150 * the smallest logical address matched with the given device type.
151 * Otherwise, scan address will start from {@code preferredAddress}
152 * @param callback callback interface to report allocated logical address to caller
153 */
Jungshik Janga5b74142014-06-23 18:03:10 +0900154 @ServiceThreadOnly
Jungshik Jangd643f762014-05-22 19:28:09 +0900155 void allocateLogicalAddress(final int deviceType, final int preferredAddress,
Jungshik Jang3ee65722014-06-03 16:22:30 +0900156 final AllocateAddressCallback callback) {
Jungshik Jang02bb4262014-05-23 16:48:31 +0900157 assertRunOnServiceThread();
158
Jungshik Jangd643f762014-05-22 19:28:09 +0900159 runOnIoThread(new Runnable() {
160 @Override
161 public void run() {
162 handleAllocateLogicalAddress(deviceType, preferredAddress, callback);
163 }
164 });
165 }
166
Jungshik Janga5b74142014-06-23 18:03:10 +0900167 @IoThreadOnly
Jungshik Jangd643f762014-05-22 19:28:09 +0900168 private void handleAllocateLogicalAddress(final int deviceType, int preferredAddress,
Jungshik Jang3ee65722014-06-03 16:22:30 +0900169 final AllocateAddressCallback callback) {
Jungshik Jang02bb4262014-05-23 16:48:31 +0900170 assertRunOnIoThread();
Jungshik Jangd643f762014-05-22 19:28:09 +0900171 int startAddress = preferredAddress;
172 // If preferred address is "unregistered", start address will be the smallest
173 // address matched with the given device type.
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900174 if (preferredAddress == Constants.ADDR_UNREGISTERED) {
Jungshik Jangd643f762014-05-22 19:28:09 +0900175 for (int i = 0; i < NUM_LOGICAL_ADDRESS; ++i) {
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900176 if (deviceType == HdmiUtils.getTypeFromAddress(i)) {
Jungshik Jangd643f762014-05-22 19:28:09 +0900177 startAddress = i;
178 break;
179 }
180 }
181 }
182
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900183 int logicalAddress = Constants.ADDR_UNREGISTERED;
Jungshik Jangd643f762014-05-22 19:28:09 +0900184 // Iterates all possible addresses which has the same device type.
185 for (int i = 0; i < NUM_LOGICAL_ADDRESS; ++i) {
186 int curAddress = (startAddress + i) % NUM_LOGICAL_ADDRESS;
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900187 if (curAddress != Constants.ADDR_UNREGISTERED
188 && deviceType == HdmiUtils.getTypeFromAddress(curAddress)) {
Jungshik Jang8e93c842014-08-06 15:48:33 +0900189 int failedPollingCount = 0;
190 for (int j = 0; j < HdmiConfig.ADDRESS_ALLOCATION_RETRY; ++j) {
191 if (!sendPollMessage(curAddress, curAddress, 1)) {
192 failedPollingCount++;
193 }
194 }
195
196 // Pick logical address if failed ratio is more than a half of all retries.
197 if (failedPollingCount * 2 > HdmiConfig.ADDRESS_ALLOCATION_RETRY) {
Jungshik Jangd643f762014-05-22 19:28:09 +0900198 logicalAddress = curAddress;
199 break;
200 }
201 }
202 }
203
204 final int assignedAddress = logicalAddress;
205 if (callback != null) {
206 runOnServiceThread(new Runnable() {
Jungshik Jang8e93c842014-08-06 15:48:33 +0900207 @Override
Jungshik Jangd643f762014-05-22 19:28:09 +0900208 public void run() {
209 callback.onAllocated(deviceType, assignedAddress);
210 }
211 });
212 }
Jungshik Jang3f74ab02014-04-30 14:31:02 +0900213 }
214
Jungshik Jange9c77c82014-04-24 20:30:09 +0900215 private static byte[] buildBody(int opcode, byte[] params) {
216 byte[] body = new byte[params.length + 1];
217 body[0] = (byte) opcode;
218 System.arraycopy(params, 0, body, 1, params.length);
219 return body;
220 }
Jungshik Jang0792d372014-04-23 17:57:26 +0900221
Jungshik Jang7d9a8432014-04-29 15:12:43 +0900222
Jinsuk Kim0340bbc2014-06-05 11:07:47 +0900223 HdmiPortInfo[] getPortInfos() {
224 return nativeGetPortInfos(mNativePtr);
225 }
226
Jungshik Janga9095ba2014-05-02 13:06:22 +0900227 /**
Jinsuk Kim7fe2ae02014-05-26 17:33:05 +0900228 * Return the locally hosted logical device of a given type.
229 *
230 * @param deviceType logical device type
231 * @return {@link HdmiCecLocalDevice} instance if the instance of the type is available;
232 * otherwise null.
233 */
234 HdmiCecLocalDevice getLocalDevice(int deviceType) {
235 return mLocalDevices.get(deviceType);
236 }
237
238 /**
Jungshik Janga9095ba2014-05-02 13:06:22 +0900239 * Add a new logical address to the device. Device's HW should be notified
240 * when a new logical address is assigned to a device, so that it can accept
241 * a command having available destinations.
242 *
243 * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
244 *
245 * @param newLogicalAddress a logical address to be added
246 * @return 0 on success. Otherwise, returns negative value
247 */
Jungshik Janga5b74142014-06-23 18:03:10 +0900248 @ServiceThreadOnly
Jungshik Janga9095ba2014-05-02 13:06:22 +0900249 int addLogicalAddress(int newLogicalAddress) {
Jungshik Jang02bb4262014-05-23 16:48:31 +0900250 assertRunOnServiceThread();
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900251 if (HdmiUtils.isValidAddress(newLogicalAddress)) {
Jungshik Janga9095ba2014-05-02 13:06:22 +0900252 return nativeAddLogicalAddress(mNativePtr, newLogicalAddress);
253 } else {
254 return -1;
255 }
256 }
257
258 /**
259 * Clear all logical addresses registered in the device.
260 *
261 * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
262 */
Jungshik Janga5b74142014-06-23 18:03:10 +0900263 @ServiceThreadOnly
Jungshik Janga9095ba2014-05-02 13:06:22 +0900264 void clearLogicalAddress() {
Jungshik Jang02bb4262014-05-23 16:48:31 +0900265 assertRunOnServiceThread();
Jinsuk Kim7fe2ae02014-05-26 17:33:05 +0900266 for (int i = 0; i < mLocalDevices.size(); ++i) {
267 mLocalDevices.valueAt(i).clearAddress();
Jinsuk Kim2918e9e2014-05-20 16:45:45 +0900268 }
Jungshik Janga9095ba2014-05-02 13:06:22 +0900269 nativeClearLogicalAddress(mNativePtr);
270 }
271
Jungshik Jang4fc1d102014-07-09 19:24:50 +0900272 @ServiceThreadOnly
273 void clearLocalDevices() {
274 assertRunOnServiceThread();
275 mLocalDevices.clear();
276 }
277
Jungshik Janga9095ba2014-05-02 13:06:22 +0900278 /**
279 * Return the physical address of the device.
280 *
281 * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
282 *
283 * @return CEC physical address of the device. The range of success address
284 * is between 0x0000 and 0xFFFF. If failed it returns -1
285 */
Jungshik Janga5b74142014-06-23 18:03:10 +0900286 @ServiceThreadOnly
Jungshik Janga9095ba2014-05-02 13:06:22 +0900287 int getPhysicalAddress() {
Jungshik Jang02bb4262014-05-23 16:48:31 +0900288 assertRunOnServiceThread();
Jungshik Janga9095ba2014-05-02 13:06:22 +0900289 return nativeGetPhysicalAddress(mNativePtr);
290 }
291
292 /**
293 * Return CEC version of the device.
294 *
295 * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
296 */
Jungshik Janga5b74142014-06-23 18:03:10 +0900297 @ServiceThreadOnly
Jungshik Janga9095ba2014-05-02 13:06:22 +0900298 int getVersion() {
Jungshik Jang02bb4262014-05-23 16:48:31 +0900299 assertRunOnServiceThread();
Jungshik Janga9095ba2014-05-02 13:06:22 +0900300 return nativeGetVersion(mNativePtr);
301 }
302
303 /**
304 * Return vendor id of the device.
305 *
306 * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
307 */
Jungshik Janga5b74142014-06-23 18:03:10 +0900308 @ServiceThreadOnly
Jungshik Janga9095ba2014-05-02 13:06:22 +0900309 int getVendorId() {
Jungshik Jang02bb4262014-05-23 16:48:31 +0900310 assertRunOnServiceThread();
Jungshik Janga9095ba2014-05-02 13:06:22 +0900311 return nativeGetVendorId(mNativePtr);
312 }
313
Jungshik Jang02bb4262014-05-23 16:48:31 +0900314 /**
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900315 * Set an option to CEC HAL.
Jungshik Jang092b4452014-06-11 15:19:17 +0900316 *
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900317 * @param flag key of option
318 * @param value value of option
Jungshik Jang092b4452014-06-11 15:19:17 +0900319 */
Jungshik Janga5b74142014-06-23 18:03:10 +0900320 @ServiceThreadOnly
Jungshik Jang092b4452014-06-11 15:19:17 +0900321 void setOption(int flag, int value) {
322 assertRunOnServiceThread();
323 nativeSetOption(mNativePtr, flag, value);
324 }
325
326 /**
327 * Configure ARC circuit in the hardware logic to start or stop the feature.
328 *
329 * @param enabled whether to enable/disable ARC
330 */
Jungshik Janga5b74142014-06-23 18:03:10 +0900331 @ServiceThreadOnly
Jungshik Jang092b4452014-06-11 15:19:17 +0900332 void setAudioReturnChannel(boolean enabled) {
333 assertRunOnServiceThread();
334 nativeSetAudioReturnChannel(mNativePtr, enabled);
335 }
336
337 /**
338 * Return the connection status of the specified port
339 *
340 * @param port port number to check connection status
341 * @return true if connected; otherwise, return false
342 */
Jungshik Janga5b74142014-06-23 18:03:10 +0900343 @ServiceThreadOnly
Jungshik Jang092b4452014-06-11 15:19:17 +0900344 boolean isConnected(int port) {
345 assertRunOnServiceThread();
346 return nativeIsConnected(mNativePtr, port);
347 }
348
349 /**
Jungshik Jang02bb4262014-05-23 16:48:31 +0900350 * Poll all remote devices. It sends &lt;Polling Message&gt; to all remote
351 * devices.
352 *
353 * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
354 *
355 * @param callback an interface used to get a list of all remote devices' address
Jungshik Jang1de51422014-07-03 11:14:26 +0900356 * @param sourceAddress a logical address of source device where sends polling message
Jungshik Jang0f8b4b72014-05-28 17:58:58 +0900357 * @param pickStrategy strategy how to pick polling candidates
Jungshik Jang02bb4262014-05-23 16:48:31 +0900358 * @param retryCount the number of retry used to send polling message to remote devices
359 */
Jungshik Janga5b74142014-06-23 18:03:10 +0900360 @ServiceThreadOnly
Jungshik Jang1de51422014-07-03 11:14:26 +0900361 void pollDevices(DevicePollingCallback callback, int sourceAddress, int pickStrategy,
362 int retryCount) {
Jungshik Jang02bb4262014-05-23 16:48:31 +0900363 assertRunOnServiceThread();
Jungshik Jang02bb4262014-05-23 16:48:31 +0900364
Jungshik Jang0f8b4b72014-05-28 17:58:58 +0900365 // Extract polling candidates. No need to poll against local devices.
366 List<Integer> pollingCandidates = pickPollCandidates(pickStrategy);
Jungshik Jang1de51422014-07-03 11:14:26 +0900367 runDevicePolling(sourceAddress, pollingCandidates, retryCount, callback);
Jungshik Jang02bb4262014-05-23 16:48:31 +0900368 }
369
Jungshik Jangcc5ef8c2014-05-27 13:27:36 +0900370 /**
371 * Return a list of all {@link HdmiCecLocalDevice}s.
372 *
373 * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
374 */
Jungshik Janga5b74142014-06-23 18:03:10 +0900375 @ServiceThreadOnly
Jungshik Jangcc5ef8c2014-05-27 13:27:36 +0900376 List<HdmiCecLocalDevice> getLocalDeviceList() {
377 assertRunOnServiceThread();
Jungshik Jang79c58a42014-06-16 16:45:36 +0900378 return HdmiUtils.sparseArrayToList(mLocalDevices);
Jungshik Jangcc5ef8c2014-05-27 13:27:36 +0900379 }
380
Jungshik Jang0f8b4b72014-05-28 17:58:58 +0900381 private List<Integer> pickPollCandidates(int pickStrategy) {
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900382 int strategy = pickStrategy & Constants.POLL_STRATEGY_MASK;
Jungshik Jang0f8b4b72014-05-28 17:58:58 +0900383 Predicate<Integer> pickPredicate = null;
384 switch (strategy) {
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900385 case Constants.POLL_STRATEGY_SYSTEM_AUDIO:
Jungshik Jang0f8b4b72014-05-28 17:58:58 +0900386 pickPredicate = mSystemAudioAddressPredicate;
387 break;
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900388 case Constants.POLL_STRATEGY_REMOTES_DEVICES:
Jungshik Jang0f8b4b72014-05-28 17:58:58 +0900389 default: // The default is POLL_STRATEGY_REMOTES_DEVICES.
390 pickPredicate = mRemoteDeviceAddressPredicate;
391 break;
392 }
393
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900394 int iterationStrategy = pickStrategy & Constants.POLL_ITERATION_STRATEGY_MASK;
Jungshik Jang0f8b4b72014-05-28 17:58:58 +0900395 ArrayList<Integer> pollingCandidates = new ArrayList<>();
396 switch (iterationStrategy) {
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900397 case Constants.POLL_ITERATION_IN_ORDER:
398 for (int i = Constants.ADDR_TV; i <= Constants.ADDR_SPECIFIC_USE; ++i) {
Jungshik Jang0f8b4b72014-05-28 17:58:58 +0900399 if (pickPredicate.apply(i)) {
400 pollingCandidates.add(i);
401 }
402 }
403 break;
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900404 case Constants.POLL_ITERATION_REVERSE_ORDER:
Jungshik Jang0f8b4b72014-05-28 17:58:58 +0900405 default: // The default is reverse order.
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900406 for (int i = Constants.ADDR_SPECIFIC_USE; i >= Constants.ADDR_TV; --i) {
Jungshik Jang0f8b4b72014-05-28 17:58:58 +0900407 if (pickPredicate.apply(i)) {
408 pollingCandidates.add(i);
409 }
410 }
411 break;
412 }
413 return pollingCandidates;
414 }
415
Jungshik Janga5b74142014-06-23 18:03:10 +0900416 @ServiceThreadOnly
Jungshik Jang02bb4262014-05-23 16:48:31 +0900417 private boolean isAllocatedLocalDeviceAddress(int address) {
Jungshik Janga5b74142014-06-23 18:03:10 +0900418 assertRunOnServiceThread();
Jinsuk Kim7fe2ae02014-05-26 17:33:05 +0900419 for (int i = 0; i < mLocalDevices.size(); ++i) {
420 if (mLocalDevices.valueAt(i).isAddressOf(address)) {
Jungshik Jang02bb4262014-05-23 16:48:31 +0900421 return true;
422 }
423 }
424 return false;
425 }
426
Jungshik Janga5b74142014-06-23 18:03:10 +0900427 @ServiceThreadOnly
Jungshik Jang1de51422014-07-03 11:14:26 +0900428 private void runDevicePolling(final int sourceAddress,
429 final List<Integer> candidates, final int retryCount,
Jungshik Jang02bb4262014-05-23 16:48:31 +0900430 final DevicePollingCallback callback) {
431 assertRunOnServiceThread();
432 runOnIoThread(new Runnable() {
433 @Override
434 public void run() {
435 final ArrayList<Integer> allocated = new ArrayList<>();
436 for (Integer address : candidates) {
Jungshik Jang1de51422014-07-03 11:14:26 +0900437 if (sendPollMessage(sourceAddress, address, retryCount)) {
Jungshik Jang02bb4262014-05-23 16:48:31 +0900438 allocated.add(address);
439 }
440 }
441 if (callback != null) {
442 runOnServiceThread(new Runnable() {
443 @Override
444 public void run() {
445 callback.onPollingFinished(allocated);
446 }
447 });
448 }
449 }
450 });
451 }
452
Jungshik Janga5b74142014-06-23 18:03:10 +0900453 @IoThreadOnly
Jungshik Jang1de51422014-07-03 11:14:26 +0900454 private boolean sendPollMessage(int sourceAddress, int destinationAddress, int retryCount) {
Jungshik Jang02bb4262014-05-23 16:48:31 +0900455 assertRunOnIoThread();
456 for (int i = 0; i < retryCount; ++i) {
Jungshik Jang1de51422014-07-03 11:14:26 +0900457 // <Polling Message> is a message which has empty body.
Jungshik Jang02bb4262014-05-23 16:48:31 +0900458 // If sending <Polling Message> failed (NAK), it becomes
459 // new logical address for the device because no device uses
460 // it as logical address of the device.
Jungshik Jang1de51422014-07-03 11:14:26 +0900461 if (nativeSendCecCommand(mNativePtr, sourceAddress, destinationAddress, EMPTY_BODY)
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900462 == Constants.SEND_RESULT_SUCCESS) {
Jungshik Jang02bb4262014-05-23 16:48:31 +0900463 return true;
464 }
465 }
466 return false;
467 }
468
469 private void assertRunOnIoThread() {
470 if (Looper.myLooper() != mIoHandler.getLooper()) {
471 throw new IllegalStateException("Should run on io thread.");
472 }
473 }
474
475 private void assertRunOnServiceThread() {
476 if (Looper.myLooper() != mControlHandler.getLooper()) {
477 throw new IllegalStateException("Should run on service thread.");
478 }
479 }
480
481 // Run a Runnable on IO thread.
482 // It should be careful to access member variables on IO thread because
483 // it can be accessed from system thread as well.
Jungshik Jangd643f762014-05-22 19:28:09 +0900484 private void runOnIoThread(Runnable runnable) {
485 mIoHandler.post(runnable);
486 }
487
488 private void runOnServiceThread(Runnable runnable) {
489 mControlHandler.post(runnable);
490 }
491
Jungshik Janga1fa91f2014-05-08 20:56:41 +0900492 private boolean isAcceptableAddress(int address) {
Jinsuk Kim2918e9e2014-05-20 16:45:45 +0900493 // Can access command targeting devices available in local device or broadcast command.
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900494 if (address == Constants.ADDR_BROADCAST) {
Jinsuk Kim2918e9e2014-05-20 16:45:45 +0900495 return true;
496 }
Jungshik Jang02bb4262014-05-23 16:48:31 +0900497 return isAllocatedLocalDeviceAddress(address);
Jungshik Janga1fa91f2014-05-08 20:56:41 +0900498 }
499
Jungshik Janga5b74142014-06-23 18:03:10 +0900500 @ServiceThreadOnly
Jungshik Jange9c77c82014-04-24 20:30:09 +0900501 private void onReceiveCommand(HdmiCecMessage message) {
Jungshik Jang02bb4262014-05-23 16:48:31 +0900502 assertRunOnServiceThread();
Yuncheol Heo6aae6522014-08-05 14:48:37 +0900503 if (isAcceptableAddress(message.getDestination()) && mService.handleCecCommand(message)) {
Jungshik Janga1fa91f2014-05-08 20:56:41 +0900504 return;
505 }
Yuncheol Heo6aae6522014-08-05 14:48:37 +0900506 // Not handled message, so we will reply it with <Feature Abort>.
507 maySendFeatureAbortCommand(message, Constants.ABORT_UNRECOGNIZED_OPCODE);
508 }
Jungshik Jang1827fdc2014-07-17 13:58:14 +0900509
Yuncheol Heo6aae6522014-08-05 14:48:37 +0900510 @ServiceThreadOnly
511 void maySendFeatureAbortCommand(HdmiCecMessage message, int reason) {
512 assertRunOnServiceThread();
513 // Swap the source and the destination.
514 int src = message.getDestination();
515 int dest = message.getSource();
516 if (src == Constants.ADDR_BROADCAST || dest == Constants.ADDR_UNREGISTERED) {
517 // Don't reply <Feature Abort> from the unregistered devices or for the broadcasted
518 // messages. See CEC 12.2 Protocol General Rules for detail.
519 return;
520 }
521 int originalOpcode = message.getOpcode();
522 if (originalOpcode == Constants.MESSAGE_FEATURE_ABORT) {
523 return;
524 }
525 sendCommand(
526 HdmiCecMessageBuilder.buildFeatureAbortCommand(src, dest, originalOpcode, reason));
Jungshik Jange9c77c82014-04-24 20:30:09 +0900527 }
528
Jungshik Janga5b74142014-06-23 18:03:10 +0900529 @ServiceThreadOnly
Jinsuk Kim2918e9e2014-05-20 16:45:45 +0900530 void sendCommand(HdmiCecMessage cecMessage) {
Jungshik Janga5b74142014-06-23 18:03:10 +0900531 assertRunOnServiceThread();
Jinsuk Kim2918e9e2014-05-20 16:45:45 +0900532 sendCommand(cecMessage, null);
533 }
534
Jungshik Janga5b74142014-06-23 18:03:10 +0900535 @ServiceThreadOnly
Jungshik Jangd643f762014-05-22 19:28:09 +0900536 void sendCommand(final HdmiCecMessage cecMessage,
537 final HdmiControlService.SendMessageCallback callback) {
Jungshik Janga5b74142014-06-23 18:03:10 +0900538 assertRunOnServiceThread();
Jungshik Jangd643f762014-05-22 19:28:09 +0900539 runOnIoThread(new Runnable() {
540 @Override
541 public void run() {
542 byte[] body = buildBody(cecMessage.getOpcode(), cecMessage.getParams());
Jungshik Jang8ed86c42014-07-11 11:56:46 +0900543 int i = 0;
544 int errorCode = Constants.SEND_RESULT_SUCCESS;
545 do {
546 errorCode = nativeSendCecCommand(mNativePtr, cecMessage.getSource(),
547 cecMessage.getDestination(), body);
548 if (errorCode == Constants.SEND_RESULT_SUCCESS) {
549 break;
550 }
551 } while (i++ < HdmiConfig.RETRANSMISSION_COUNT);
552
553 final int finalError = errorCode;
554 if (finalError != Constants.SEND_RESULT_SUCCESS) {
Yuncheol Heoece603b2014-05-23 20:10:19 +0900555 Slog.w(TAG, "Failed to send " + cecMessage);
556 }
Jungshik Jangd643f762014-05-22 19:28:09 +0900557 if (callback != null) {
558 runOnServiceThread(new Runnable() {
559 @Override
560 public void run() {
Jungshik Jang8ed86c42014-07-11 11:56:46 +0900561 callback.onSendCompleted(finalError);
Jungshik Jangd643f762014-05-22 19:28:09 +0900562 }
563 });
564 }
565 }
566 });
Jungshik Jange9c77c82014-04-24 20:30:09 +0900567 }
568
Jungshik Jang0792d372014-04-23 17:57:26 +0900569 /**
Jungshik Jange9c77c82014-04-24 20:30:09 +0900570 * Called by native when incoming CEC message arrived.
Jungshik Jang0792d372014-04-23 17:57:26 +0900571 */
Jungshik Janga5b74142014-06-23 18:03:10 +0900572 @ServiceThreadOnly
Jungshik Jange9c77c82014-04-24 20:30:09 +0900573 private void handleIncomingCecCommand(int srcAddress, int dstAddress, byte[] body) {
Jungshik Jang4085d0e2014-05-27 19:52:39 +0900574 assertRunOnServiceThread();
575 onReceiveCommand(HdmiCecMessageBuilder.of(srcAddress, dstAddress, body));
Jungshik Jange9c77c82014-04-24 20:30:09 +0900576 }
577
578 /**
579 * Called by native when a hotplug event issues.
580 */
Jungshik Jang42230722014-07-07 17:40:25 +0900581 @ServiceThreadOnly
582 private void handleHotplug(int port, boolean connected) {
583 assertRunOnServiceThread();
584 mService.onHotplug(port, connected);
Jungshik Jang0792d372014-04-23 17:57:26 +0900585 }
586
Jungshik Jang4085d0e2014-05-27 19:52:39 +0900587 private static native long nativeInit(HdmiCecController handler, MessageQueue messageQueue);
Jungshik Janga9095ba2014-05-02 13:06:22 +0900588 private static native int nativeSendCecCommand(long controllerPtr, int srcAddress,
Jungshik Jange9c77c82014-04-24 20:30:09 +0900589 int dstAddress, byte[] body);
Jungshik Janga9095ba2014-05-02 13:06:22 +0900590 private static native int nativeAddLogicalAddress(long controllerPtr, int logicalAddress);
591 private static native void nativeClearLogicalAddress(long controllerPtr);
592 private static native int nativeGetPhysicalAddress(long controllerPtr);
593 private static native int nativeGetVersion(long controllerPtr);
594 private static native int nativeGetVendorId(long controllerPtr);
Jinsuk Kim0340bbc2014-06-05 11:07:47 +0900595 private static native HdmiPortInfo[] nativeGetPortInfos(long controllerPtr);
Jungshik Jang092b4452014-06-11 15:19:17 +0900596 private static native void nativeSetOption(long controllerPtr, int flag, int value);
597 private static native void nativeSetAudioReturnChannel(long controllerPtr, boolean flag);
598 private static native boolean nativeIsConnected(long controllerPtr, int port);
Jungshik Jang0792d372014-04-23 17:57:26 +0900599}