blob: f869424fc139f50921abf235dd83f4b5f6a7424a [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
Jungshik Jange9c77c82014-04-24 20:30:09 +090019import android.hardware.hdmi.HdmiCec;
Jungshik Jang7d9a8432014-04-29 15:12:43 +090020import android.hardware.hdmi.HdmiCecDeviceInfo;
Jungshik Jange9c77c82014-04-24 20:30:09 +090021import android.hardware.hdmi.HdmiCecMessage;
Jungshik Jang0792d372014-04-23 17:57:26 +090022import android.os.Handler;
Jungshik Jang02bb4262014-05-23 16:48:31 +090023import android.os.Looper;
Jungshik Jang4085d0e2014-05-27 19:52:39 +090024import android.os.MessageQueue;
Yuncheol Heoece603b2014-05-23 20:10:19 +090025import android.util.Slog;
Jungshik Jang7d9a8432014-04-29 15:12:43 +090026import android.util.SparseArray;
Jungshik Jange9c77c82014-04-24 20:30:09 +090027
Jungshik Jang0f8b4b72014-05-28 17:58:58 +090028import com.android.internal.util.Predicate;
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
60 * {@link HdmiCec#ADDR_UNREGISTERED}, it means that
61 * 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 Jange9c77c82014-04-24 20:30:09 +090068 // A message to pass cec send command to IO looper.
69 private static final int MSG_SEND_CEC_COMMAND = 1;
Jungshik Jang3f74ab02014-04-30 14:31:02 +090070 // A message to delegate logical allocation to IO looper.
71 private static final int MSG_ALLOCATE_LOGICAL_ADDRESS = 2;
Jungshik Jange9c77c82014-04-24 20:30:09 +090072
73 // Message types to handle incoming message in main service looper.
74 private final static int MSG_RECEIVE_CEC_COMMAND = 1;
Jungshik Jang3f74ab02014-04-30 14:31:02 +090075 // A message to report allocated logical address to main control looper.
76 private final static int MSG_REPORT_LOGICAL_ADDRESS = 2;
Jungshik Jange9c77c82014-04-24 20:30:09 +090077
Jungshik Jang3f74ab02014-04-30 14:31:02 +090078 private static final int NUM_LOGICAL_ADDRESS = 16;
79
Jungshik Jang02bb4262014-05-23 16:48:31 +090080 private static final int RETRY_COUNT_FOR_LOGICAL_ADDRESS_ALLOCATION = 3;
81
Jungshik Jang0f8b4b72014-05-28 17:58:58 +090082 // 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
85 public boolean apply(Integer address) {
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
93 public boolean apply(Integer address) {
94 return HdmiCec.getTypeFromAddress(address) == HdmiCec.ADDR_AUDIO_SYSTEM;
95 }
96 };
97
Jungshik Jang0792d372014-04-23 17:57:26 +090098 // 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 Jange9c77c82014-04-24 20:30:09 +0900103 private Handler mControlHandler;
Jungshik Jang0792d372014-04-23 17:57:26 +0900104
105 // Stores the pointer to the native implementation of the service that
106 // interacts with HAL.
Jungshik Jang02bb4262014-05-23 16:48:31 +0900107 private volatile long mNativePtr;
Jungshik Jang0792d372014-04-23 17:57:26 +0900108
Jungshik Janga1fa91f2014-05-08 20:56:41 +0900109 private HdmiControlService mService;
110
Jungshik Jang1a4485d2014-05-26 11:02:36 +0900111 // Map-like container of all cec devices including local ones.
112 // A logical address of device is used as key of container.
Jungshik Jang02bb4262014-05-23 16:48:31 +0900113 private final SparseArray<HdmiCecDeviceInfo> mDeviceInfos = new SparseArray<>();
Jinsuk Kim2918e9e2014-05-20 16:45:45 +0900114
Jinsuk Kim7fe2ae02014-05-26 17:33:05 +0900115 // Stores the local CEC devices in the system. Device type is used for key.
116 private final SparseArray<HdmiCecLocalDevice> mLocalDevices = new SparseArray<>();
Jungshik Jang7d9a8432014-04-29 15:12:43 +0900117
Jungshik Jang0792d372014-04-23 17:57:26 +0900118 // Private constructor. Use HdmiCecController.create().
119 private HdmiCecController() {
120 }
121
122 /**
123 * A factory method to get {@link HdmiCecController}. If it fails to initialize
124 * inner device or has no device it will return {@code null}.
125 *
126 * <p>Declared as package-private, accessed by {@link HdmiControlService} only.
Jungshik Jange9c77c82014-04-24 20:30:09 +0900127 * @param service {@link HdmiControlService} instance used to create internal handler
128 * and to pass callback for incoming message or event.
Jungshik Jang0792d372014-04-23 17:57:26 +0900129 * @return {@link HdmiCecController} if device is initialized successfully. Otherwise,
130 * returns {@code null}.
131 */
Jungshik Jange9c77c82014-04-24 20:30:09 +0900132 static HdmiCecController create(HdmiControlService service) {
Jinsuk Kim2918e9e2014-05-20 16:45:45 +0900133 HdmiCecController controller = new HdmiCecController();
Jungshik Jang4085d0e2014-05-27 19:52:39 +0900134 long nativePtr = nativeInit(controller, service.getServiceLooper().getQueue());
Jungshik Jang0792d372014-04-23 17:57:26 +0900135 if (nativePtr == 0L) {
Jinsuk Kim2918e9e2014-05-20 16:45:45 +0900136 controller = null;
Jungshik Jang0792d372014-04-23 17:57:26 +0900137 return null;
138 }
139
Jinsuk Kim2918e9e2014-05-20 16:45:45 +0900140 controller.init(service, nativePtr);
141 return controller;
142 }
143
144 private void init(HdmiControlService service, long nativePtr) {
145 mService = service;
146 mIoHandler = new Handler(service.getServiceLooper());
147 mControlHandler = new Handler(service.getServiceLooper());
148 mNativePtr = nativePtr;
Jungshik Jang0792d372014-04-23 17:57:26 +0900149 }
150
Jungshik Jang3ee65722014-06-03 16:22:30 +0900151 void addLocalDevice(int deviceType, HdmiCecLocalDevice device) {
152 mLocalDevices.put(deviceType, device);
Jungshik Jang3f74ab02014-04-30 14:31:02 +0900153 }
154
155 /**
156 * Allocate a new logical address of the given device type. Allocated
Jungshik Jang3ee65722014-06-03 16:22:30 +0900157 * address will be reported through {@link AllocateAddressCallback}.
Jungshik Jang3f74ab02014-04-30 14:31:02 +0900158 *
159 * <p> Declared as package-private, accessed by {@link HdmiControlService} only.
160 *
161 * @param deviceType type of device to used to determine logical address
162 * @param preferredAddress a logical address preferred to be allocated.
163 * If sets {@link HdmiCec#ADDR_UNREGISTERED}, scans
164 * the smallest logical address matched with the given device type.
165 * Otherwise, scan address will start from {@code preferredAddress}
166 * @param callback callback interface to report allocated logical address to caller
167 */
Jungshik Jangd643f762014-05-22 19:28:09 +0900168 void allocateLogicalAddress(final int deviceType, final int preferredAddress,
Jungshik Jang3ee65722014-06-03 16:22:30 +0900169 final AllocateAddressCallback callback) {
Jungshik Jang02bb4262014-05-23 16:48:31 +0900170 assertRunOnServiceThread();
171
Jungshik Jangd643f762014-05-22 19:28:09 +0900172 runOnIoThread(new Runnable() {
173 @Override
174 public void run() {
175 handleAllocateLogicalAddress(deviceType, preferredAddress, callback);
176 }
177 });
178 }
179
180 private void handleAllocateLogicalAddress(final int deviceType, int preferredAddress,
Jungshik Jang3ee65722014-06-03 16:22:30 +0900181 final AllocateAddressCallback callback) {
Jungshik Jang02bb4262014-05-23 16:48:31 +0900182 assertRunOnIoThread();
Jungshik Jangd643f762014-05-22 19:28:09 +0900183 int startAddress = preferredAddress;
184 // If preferred address is "unregistered", start address will be the smallest
185 // address matched with the given device type.
186 if (preferredAddress == HdmiCec.ADDR_UNREGISTERED) {
187 for (int i = 0; i < NUM_LOGICAL_ADDRESS; ++i) {
188 if (deviceType == HdmiCec.getTypeFromAddress(i)) {
189 startAddress = i;
190 break;
191 }
192 }
193 }
194
195 int logicalAddress = HdmiCec.ADDR_UNREGISTERED;
196 // Iterates all possible addresses which has the same device type.
197 for (int i = 0; i < NUM_LOGICAL_ADDRESS; ++i) {
198 int curAddress = (startAddress + i) % NUM_LOGICAL_ADDRESS;
199 if (curAddress != HdmiCec.ADDR_UNREGISTERED
Jinsuk Kim0bc8b072014-05-27 17:23:27 +0900200 && deviceType == HdmiCec.getTypeFromAddress(curAddress)) {
Jungshik Jang02bb4262014-05-23 16:48:31 +0900201 if (!sendPollMessage(curAddress, RETRY_COUNT_FOR_LOGICAL_ADDRESS_ALLOCATION)) {
Jungshik Jangd643f762014-05-22 19:28:09 +0900202 logicalAddress = curAddress;
203 break;
204 }
205 }
206 }
207
208 final int assignedAddress = logicalAddress;
209 if (callback != null) {
210 runOnServiceThread(new Runnable() {
211 @Override
212 public void run() {
213 callback.onAllocated(deviceType, assignedAddress);
214 }
215 });
216 }
Jungshik Jang3f74ab02014-04-30 14:31:02 +0900217 }
218
Jungshik Jange9c77c82014-04-24 20:30:09 +0900219 private static byte[] buildBody(int opcode, byte[] params) {
220 byte[] body = new byte[params.length + 1];
221 body[0] = (byte) opcode;
222 System.arraycopy(params, 0, body, 1, params.length);
223 return body;
224 }
Jungshik Jang0792d372014-04-23 17:57:26 +0900225
Jungshik Jang7d9a8432014-04-29 15:12:43 +0900226 /**
227 * Add a new {@link HdmiCecDeviceInfo}. It returns old device info which has the same
228 * logical address as new device info's.
229 *
230 * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
231 *
232 * @param deviceInfo a new {@link HdmiCecDeviceInfo} to be added.
233 * @return {@code null} if it is new device. Otherwise, returns old {@HdmiCecDeviceInfo}
234 * that has the same logical address as new one has.
235 */
236 HdmiCecDeviceInfo addDeviceInfo(HdmiCecDeviceInfo deviceInfo) {
Jungshik Jang02bb4262014-05-23 16:48:31 +0900237 assertRunOnServiceThread();
Jungshik Jang7d9a8432014-04-29 15:12:43 +0900238 HdmiCecDeviceInfo oldDeviceInfo = getDeviceInfo(deviceInfo.getLogicalAddress());
239 if (oldDeviceInfo != null) {
240 removeDeviceInfo(deviceInfo.getLogicalAddress());
241 }
242 mDeviceInfos.append(deviceInfo.getLogicalAddress(), deviceInfo);
243 return oldDeviceInfo;
244 }
245
246 /**
247 * Remove a device info corresponding to the given {@code logicalAddress}.
248 * It returns removed {@link HdmiCecDeviceInfo} if exists.
249 *
250 * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
251 *
252 * @param logicalAddress logical address of device to be removed
253 * @return removed {@link HdmiCecDeviceInfo} it exists. Otherwise, returns {@code null}
254 */
255 HdmiCecDeviceInfo removeDeviceInfo(int logicalAddress) {
Jungshik Jang02bb4262014-05-23 16:48:31 +0900256 assertRunOnServiceThread();
Jungshik Jang7d9a8432014-04-29 15:12:43 +0900257 HdmiCecDeviceInfo deviceInfo = mDeviceInfos.get(logicalAddress);
258 if (deviceInfo != null) {
259 mDeviceInfos.remove(logicalAddress);
260 }
261 return deviceInfo;
262 }
263
264 /**
Jungshik Jangcc5ef8c2014-05-27 13:27:36 +0900265 * Clear all device info.
266 *
267 * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
268 */
269 void clearDeviceInfoList() {
270 assertRunOnServiceThread();
271 mDeviceInfos.clear();
272 }
273
274 /**
Jungshik Jang02bb4262014-05-23 16:48:31 +0900275 * Return a list of all {@link HdmiCecDeviceInfo}.
Jungshik Jang7d9a8432014-04-29 15:12:43 +0900276 *
277 * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
Jungshik Jang0f8b4b72014-05-28 17:58:58 +0900278 *
279 * @param includeLocalDevice whether to add local device or not
Jungshik Jang7d9a8432014-04-29 15:12:43 +0900280 */
Jungshik Jang0f8b4b72014-05-28 17:58:58 +0900281 List<HdmiCecDeviceInfo> getDeviceInfoList(boolean includeLocalDevice) {
Jungshik Jang02bb4262014-05-23 16:48:31 +0900282 assertRunOnServiceThread();
Jungshik Jang0f8b4b72014-05-28 17:58:58 +0900283 if (includeLocalDevice) {
284 return sparseArrayToList(mDeviceInfos);
285 } else {
286 ArrayList<HdmiCecDeviceInfo> infoList = new ArrayList<>();
287 for (int i = 0; i < mDeviceInfos.size(); ++i) {
288 HdmiCecDeviceInfo info = mDeviceInfos.valueAt(i);
289 if (mRemoteDeviceAddressPredicate.apply(info.getLogicalAddress())) {
290 infoList.add(info);
291 }
292 }
293 return infoList;
294 }
Jungshik Jang7d9a8432014-04-29 15:12:43 +0900295 }
296
297 /**
298 * Return a {@link HdmiCecDeviceInfo} corresponding to the given {@code logicalAddress}.
299 *
300 * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
301 *
302 * @param logicalAddress logical address to be retrieved
303 * @return {@link HdmiCecDeviceInfo} matched with the given {@code logicalAddress}.
304 * Returns null if no logical address matched
305 */
306 HdmiCecDeviceInfo getDeviceInfo(int logicalAddress) {
Jungshik Jang02bb4262014-05-23 16:48:31 +0900307 assertRunOnServiceThread();
Jungshik Jang7d9a8432014-04-29 15:12:43 +0900308 return mDeviceInfos.get(logicalAddress);
309 }
310
Jungshik Janga9095ba2014-05-02 13:06:22 +0900311 /**
Jinsuk Kim7fe2ae02014-05-26 17:33:05 +0900312 * Return the locally hosted logical device of a given type.
313 *
314 * @param deviceType logical device type
315 * @return {@link HdmiCecLocalDevice} instance if the instance of the type is available;
316 * otherwise null.
317 */
318 HdmiCecLocalDevice getLocalDevice(int deviceType) {
319 return mLocalDevices.get(deviceType);
320 }
321
322 /**
Jungshik Janga9095ba2014-05-02 13:06:22 +0900323 * Add a new logical address to the device. Device's HW should be notified
324 * when a new logical address is assigned to a device, so that it can accept
325 * a command having available destinations.
326 *
327 * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
328 *
329 * @param newLogicalAddress a logical address to be added
330 * @return 0 on success. Otherwise, returns negative value
331 */
332 int addLogicalAddress(int newLogicalAddress) {
Jungshik Jang02bb4262014-05-23 16:48:31 +0900333 assertRunOnServiceThread();
Jungshik Janga9095ba2014-05-02 13:06:22 +0900334 if (HdmiCec.isValidAddress(newLogicalAddress)) {
335 return nativeAddLogicalAddress(mNativePtr, newLogicalAddress);
336 } else {
337 return -1;
338 }
339 }
340
341 /**
342 * Clear all logical addresses registered in the device.
343 *
344 * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
345 */
346 void clearLogicalAddress() {
Jungshik Jang02bb4262014-05-23 16:48:31 +0900347 assertRunOnServiceThread();
Jungshik Janga1fa91f2014-05-08 20:56:41 +0900348 // TODO: consider to backup logical address so that new logical address
349 // allocation can use it as preferred address.
Jinsuk Kim7fe2ae02014-05-26 17:33:05 +0900350 for (int i = 0; i < mLocalDevices.size(); ++i) {
351 mLocalDevices.valueAt(i).clearAddress();
Jinsuk Kim2918e9e2014-05-20 16:45:45 +0900352 }
Jungshik Janga9095ba2014-05-02 13:06:22 +0900353 nativeClearLogicalAddress(mNativePtr);
354 }
355
356 /**
357 * Return the physical address of the device.
358 *
359 * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
360 *
361 * @return CEC physical address of the device. The range of success address
362 * is between 0x0000 and 0xFFFF. If failed it returns -1
363 */
364 int getPhysicalAddress() {
Jungshik Jang02bb4262014-05-23 16:48:31 +0900365 assertRunOnServiceThread();
Jungshik Janga9095ba2014-05-02 13:06:22 +0900366 return nativeGetPhysicalAddress(mNativePtr);
367 }
368
369 /**
370 * Return CEC version of the device.
371 *
372 * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
373 */
374 int getVersion() {
Jungshik Jang02bb4262014-05-23 16:48:31 +0900375 assertRunOnServiceThread();
Jungshik Janga9095ba2014-05-02 13:06:22 +0900376 return nativeGetVersion(mNativePtr);
377 }
378
379 /**
380 * Return vendor id of the device.
381 *
382 * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
383 */
384 int getVendorId() {
Jungshik Jang02bb4262014-05-23 16:48:31 +0900385 assertRunOnServiceThread();
Jungshik Janga9095ba2014-05-02 13:06:22 +0900386 return nativeGetVendorId(mNativePtr);
387 }
388
Jungshik Jang02bb4262014-05-23 16:48:31 +0900389 /**
Jungshik Jang092b4452014-06-11 15:19:17 +0900390 * Pass a option to CEC HAL.
391 *
392 * @param flag a key of option. For more details, look at
393 * {@link HdmiConstants#FLAG_HDMI_OPTION_WAKEUP} to
394 * {@link HdmiConstants#FLAG_HDMI_OPTION_SYSTEM_CEC_CONTROL}
395 * @param value a value of option. Actual value varies flag. For more
396 * details, look at description of flags
397 */
398 void setOption(int flag, int value) {
399 assertRunOnServiceThread();
400 nativeSetOption(mNativePtr, flag, value);
401 }
402
403 /**
404 * Configure ARC circuit in the hardware logic to start or stop the feature.
405 *
406 * @param enabled whether to enable/disable ARC
407 */
408 void setAudioReturnChannel(boolean enabled) {
409 assertRunOnServiceThread();
410 nativeSetAudioReturnChannel(mNativePtr, enabled);
411 }
412
413 /**
414 * Return the connection status of the specified port
415 *
416 * @param port port number to check connection status
417 * @return true if connected; otherwise, return false
418 */
419 boolean isConnected(int port) {
420 assertRunOnServiceThread();
421 return nativeIsConnected(mNativePtr, port);
422 }
423
424 /**
Jungshik Jang02bb4262014-05-23 16:48:31 +0900425 * Poll all remote devices. It sends &lt;Polling Message&gt; to all remote
426 * devices.
427 *
428 * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
429 *
430 * @param callback an interface used to get a list of all remote devices' address
Jungshik Jang0f8b4b72014-05-28 17:58:58 +0900431 * @param pickStrategy strategy how to pick polling candidates
Jungshik Jang02bb4262014-05-23 16:48:31 +0900432 * @param retryCount the number of retry used to send polling message to remote devices
433 */
Jungshik Jang0f8b4b72014-05-28 17:58:58 +0900434 void pollDevices(DevicePollingCallback callback, int pickStrategy, int retryCount) {
Jungshik Jang02bb4262014-05-23 16:48:31 +0900435 assertRunOnServiceThread();
Jungshik Jang02bb4262014-05-23 16:48:31 +0900436
Jungshik Jang0f8b4b72014-05-28 17:58:58 +0900437 // Extract polling candidates. No need to poll against local devices.
438 List<Integer> pollingCandidates = pickPollCandidates(pickStrategy);
Jungshik Jang02bb4262014-05-23 16:48:31 +0900439 runDevicePolling(pollingCandidates, retryCount, callback);
440 }
441
Jungshik Jangcc5ef8c2014-05-27 13:27:36 +0900442 /**
443 * Return a list of all {@link HdmiCecLocalDevice}s.
444 *
445 * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
446 */
447 List<HdmiCecLocalDevice> getLocalDeviceList() {
448 assertRunOnServiceThread();
449 return sparseArrayToList(mLocalDevices);
450 }
451
Jungshik Jang0f8b4b72014-05-28 17:58:58 +0900452 private List<Integer> pickPollCandidates(int pickStrategy) {
453 int strategy = pickStrategy & HdmiControlService.POLL_STRATEGY_MASK;
454 Predicate<Integer> pickPredicate = null;
455 switch (strategy) {
456 case HdmiControlService.POLL_STRATEGY_SYSTEM_AUDIO:
457 pickPredicate = mSystemAudioAddressPredicate;
458 break;
459 case HdmiControlService.POLL_STRATEGY_REMOTES_DEVICES:
460 default: // The default is POLL_STRATEGY_REMOTES_DEVICES.
461 pickPredicate = mRemoteDeviceAddressPredicate;
462 break;
463 }
464
465 int iterationStrategy = pickStrategy & HdmiControlService.POLL_ITERATION_STRATEGY_MASK;
466 ArrayList<Integer> pollingCandidates = new ArrayList<>();
467 switch (iterationStrategy) {
468 case HdmiControlService.POLL_ITERATION_IN_ORDER:
469 for (int i = HdmiCec.ADDR_TV; i <= HdmiCec.ADDR_SPECIFIC_USE; ++i) {
470 if (pickPredicate.apply(i)) {
471 pollingCandidates.add(i);
472 }
473 }
474 break;
475 case HdmiControlService.POLL_ITERATION_REVERSE_ORDER:
476 default: // The default is reverse order.
477 for (int i = HdmiCec.ADDR_SPECIFIC_USE; i >= HdmiCec.ADDR_TV; --i) {
478 if (pickPredicate.apply(i)) {
479 pollingCandidates.add(i);
480 }
481 }
482 break;
483 }
484 return pollingCandidates;
485 }
486
Jungshik Jangcc5ef8c2014-05-27 13:27:36 +0900487 private static <T> List<T> sparseArrayToList(SparseArray<T> array) {
488 ArrayList<T> list = new ArrayList<>();
489 for (int i = 0; i < array.size(); ++i) {
490 list.add(array.valueAt(i));
491 }
492 return list;
493 }
494
Jungshik Jang02bb4262014-05-23 16:48:31 +0900495 private boolean isAllocatedLocalDeviceAddress(int address) {
Jinsuk Kim7fe2ae02014-05-26 17:33:05 +0900496 for (int i = 0; i < mLocalDevices.size(); ++i) {
497 if (mLocalDevices.valueAt(i).isAddressOf(address)) {
Jungshik Jang02bb4262014-05-23 16:48:31 +0900498 return true;
499 }
500 }
501 return false;
502 }
503
504 private void runDevicePolling(final List<Integer> candidates, final int retryCount,
505 final DevicePollingCallback callback) {
506 assertRunOnServiceThread();
507 runOnIoThread(new Runnable() {
508 @Override
509 public void run() {
510 final ArrayList<Integer> allocated = new ArrayList<>();
511 for (Integer address : candidates) {
512 if (sendPollMessage(address, retryCount)) {
513 allocated.add(address);
514 }
515 }
516 if (callback != null) {
517 runOnServiceThread(new Runnable() {
518 @Override
519 public void run() {
520 callback.onPollingFinished(allocated);
521 }
522 });
523 }
524 }
525 });
526 }
527
528 private boolean sendPollMessage(int address, int retryCount) {
529 assertRunOnIoThread();
530 for (int i = 0; i < retryCount; ++i) {
531 // <Polling Message> is a message which has empty body and
532 // uses same address for both source and destination address.
533 // If sending <Polling Message> failed (NAK), it becomes
534 // new logical address for the device because no device uses
535 // it as logical address of the device.
536 if (nativeSendCecCommand(mNativePtr, address, address, EMPTY_BODY)
537 == HdmiControlService.SEND_RESULT_SUCCESS) {
538 return true;
539 }
540 }
541 return false;
542 }
543
544 private void assertRunOnIoThread() {
545 if (Looper.myLooper() != mIoHandler.getLooper()) {
546 throw new IllegalStateException("Should run on io thread.");
547 }
548 }
549
550 private void assertRunOnServiceThread() {
551 if (Looper.myLooper() != mControlHandler.getLooper()) {
552 throw new IllegalStateException("Should run on service thread.");
553 }
554 }
555
556 // Run a Runnable on IO thread.
557 // It should be careful to access member variables on IO thread because
558 // it can be accessed from system thread as well.
Jungshik Jangd643f762014-05-22 19:28:09 +0900559 private void runOnIoThread(Runnable runnable) {
560 mIoHandler.post(runnable);
561 }
562
563 private void runOnServiceThread(Runnable runnable) {
564 mControlHandler.post(runnable);
565 }
566
Jungshik Janga1fa91f2014-05-08 20:56:41 +0900567 private boolean isAcceptableAddress(int address) {
Jinsuk Kim2918e9e2014-05-20 16:45:45 +0900568 // Can access command targeting devices available in local device or broadcast command.
569 if (address == HdmiCec.ADDR_BROADCAST) {
570 return true;
571 }
Jungshik Jang02bb4262014-05-23 16:48:31 +0900572 return isAllocatedLocalDeviceAddress(address);
Jungshik Janga1fa91f2014-05-08 20:56:41 +0900573 }
574
Jungshik Jange9c77c82014-04-24 20:30:09 +0900575 private void onReceiveCommand(HdmiCecMessage message) {
Jungshik Jang02bb4262014-05-23 16:48:31 +0900576 assertRunOnServiceThread();
Jungshik Jang4085d0e2014-05-27 19:52:39 +0900577 if (isAcceptableAddress(message.getDestination())
578 && mService.handleCecCommand(message)) {
Jungshik Janga1fa91f2014-05-08 20:56:41 +0900579 return;
580 }
Jungshik Jange9c77c82014-04-24 20:30:09 +0900581
Jinsuk Kim3a2f7432014-05-29 06:52:45 +0900582 if (message.getDestination() != HdmiCec.ADDR_BROADCAST) {
583 int sourceAddress = message.getDestination();
584 // Reply <Feature Abort> to initiator (source) for all requests.
585 HdmiCecMessage cecMessage = HdmiCecMessageBuilder.buildFeatureAbortCommand(
586 sourceAddress, message.getSource(), message.getOpcode(),
Yuncheol Heo63a2e062014-05-27 23:06:01 +0900587 HdmiConstants.ABORT_REFUSED);
Jinsuk Kim3a2f7432014-05-29 06:52:45 +0900588 sendCommand(cecMessage, null);
589 }
Jungshik Jange9c77c82014-04-24 20:30:09 +0900590 }
591
Jinsuk Kim2918e9e2014-05-20 16:45:45 +0900592 void sendCommand(HdmiCecMessage cecMessage) {
593 sendCommand(cecMessage, null);
594 }
595
Jungshik Jangd643f762014-05-22 19:28:09 +0900596 void sendCommand(final HdmiCecMessage cecMessage,
597 final HdmiControlService.SendMessageCallback callback) {
598 runOnIoThread(new Runnable() {
599 @Override
600 public void run() {
601 byte[] body = buildBody(cecMessage.getOpcode(), cecMessage.getParams());
602 final int error = nativeSendCecCommand(mNativePtr, cecMessage.getSource(),
603 cecMessage.getDestination(), body);
Yuncheol Heoece603b2014-05-23 20:10:19 +0900604 if (error != HdmiControlService.SEND_RESULT_SUCCESS) {
605 Slog.w(TAG, "Failed to send " + cecMessage);
606 }
Jungshik Jangd643f762014-05-22 19:28:09 +0900607 if (callback != null) {
608 runOnServiceThread(new Runnable() {
609 @Override
610 public void run() {
611 callback.onSendCompleted(error);
612 }
613 });
614 }
615 }
616 });
Jungshik Jange9c77c82014-04-24 20:30:09 +0900617 }
618
Jungshik Jang0792d372014-04-23 17:57:26 +0900619 /**
Jungshik Jange9c77c82014-04-24 20:30:09 +0900620 * Called by native when incoming CEC message arrived.
Jungshik Jang0792d372014-04-23 17:57:26 +0900621 */
Jungshik Jange9c77c82014-04-24 20:30:09 +0900622 private void handleIncomingCecCommand(int srcAddress, int dstAddress, byte[] body) {
Jungshik Jang4085d0e2014-05-27 19:52:39 +0900623 assertRunOnServiceThread();
624 onReceiveCommand(HdmiCecMessageBuilder.of(srcAddress, dstAddress, body));
Jungshik Jange9c77c82014-04-24 20:30:09 +0900625 }
626
627 /**
628 * Called by native when a hotplug event issues.
629 */
630 private void handleHotplug(boolean connected) {
Jungshik Jang67ea5212014-05-15 14:05:24 +0900631 // TODO: once add port number to cec HAL interface, pass port number
632 // to the service.
633 mService.onHotplug(0, connected);
Jungshik Jang0792d372014-04-23 17:57:26 +0900634 }
635
Jungshik Jang4085d0e2014-05-27 19:52:39 +0900636 private static native long nativeInit(HdmiCecController handler, MessageQueue messageQueue);
Jungshik Janga9095ba2014-05-02 13:06:22 +0900637 private static native int nativeSendCecCommand(long controllerPtr, int srcAddress,
Jungshik Jange9c77c82014-04-24 20:30:09 +0900638 int dstAddress, byte[] body);
Jungshik Janga9095ba2014-05-02 13:06:22 +0900639 private static native int nativeAddLogicalAddress(long controllerPtr, int logicalAddress);
640 private static native void nativeClearLogicalAddress(long controllerPtr);
641 private static native int nativeGetPhysicalAddress(long controllerPtr);
642 private static native int nativeGetVersion(long controllerPtr);
643 private static native int nativeGetVendorId(long controllerPtr);
Jungshik Jang092b4452014-06-11 15:19:17 +0900644 private static native void nativeSetOption(long controllerPtr, int flag, int value);
645 private static native void nativeSetAudioReturnChannel(long controllerPtr, boolean flag);
646 private static native boolean nativeIsConnected(long controllerPtr, int port);
647
Jungshik Jang0792d372014-04-23 17:57:26 +0900648}