Jungshik Jang | 67ea521 | 2014-05-15 14:05:24 +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 | |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 19 | import android.hardware.hdmi.HdmiDeviceInfo; |
Jungshik Jang | 67ea521 | 2014-05-15 14:05:24 +0900 | [diff] [blame] | 20 | import android.util.Slog; |
| 21 | |
| 22 | /** |
| 23 | * Feature action that handles enabling/disabling of ARC transmission channel. |
| 24 | * Once TV gets <Initiate ARC>, TV sends <Report ARC Initiated> to AV Receiver. |
| 25 | * If it fails or it gets <Terminate ARC>, TV just disables ARC. |
| 26 | */ |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 27 | final class SetArcTransmissionStateAction extends HdmiCecFeatureAction { |
Jungshik Jang | 67ea521 | 2014-05-15 14:05:24 +0900 | [diff] [blame] | 28 | private static final String TAG = "SetArcTransmissionStateAction"; |
| 29 | |
| 30 | // State in which the action sent <Rerpot Arc Initiated> and |
| 31 | // is waiting for time out. If it receives <Feature Abort> within timeout |
| 32 | // ARC should be disabled. |
| 33 | private static final int STATE_WAITING_TIMEOUT = 1; |
| 34 | |
| 35 | private final boolean mEnabled; |
| 36 | private final int mAvrAddress; |
| 37 | |
| 38 | /** |
| 39 | * @Constructor |
| 40 | * |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 41 | * @param source {@link HdmiCecLocalDevice} instance |
Jungshik Jang | 67ea521 | 2014-05-15 14:05:24 +0900 | [diff] [blame] | 42 | * @param enabled whether to enable ARC Transmission channel |
| 43 | */ |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 44 | SetArcTransmissionStateAction(HdmiCecLocalDevice source, int avrAddress, |
Jungshik Jang | 67ea521 | 2014-05-15 14:05:24 +0900 | [diff] [blame] | 45 | boolean enabled) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 46 | super(source); |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 47 | HdmiUtils.verifyAddressType(getSourceAddress(), HdmiDeviceInfo.DEVICE_TV); |
| 48 | HdmiUtils.verifyAddressType(avrAddress, HdmiDeviceInfo.DEVICE_AUDIO_SYSTEM); |
Jungshik Jang | 67ea521 | 2014-05-15 14:05:24 +0900 | [diff] [blame] | 49 | mAvrAddress = avrAddress; |
| 50 | mEnabled = enabled; |
| 51 | } |
| 52 | |
Jungshik Jang | 67ea521 | 2014-05-15 14:05:24 +0900 | [diff] [blame] | 53 | @Override |
| 54 | boolean start() { |
Jinsuk Kim | 71651d3 | 2015-03-27 08:21:15 +0900 | [diff] [blame] | 55 | // Seq #37. |
Jungshik Jang | 67ea521 | 2014-05-15 14:05:24 +0900 | [diff] [blame] | 56 | if (mEnabled) { |
Jungshik Jang | 2e8f1b6 | 2014-09-03 08:28:02 +0900 | [diff] [blame] | 57 | // Enable ARC status immediately after sending <Report Arc Initiated>. |
| 58 | // If AVR responds with <Feature Abort>, disable ARC status again. |
| 59 | // This is different from spec that says that turns ARC status to |
| 60 | // "Enabled" if <Report ARC Initiated> is acknowledged and no |
| 61 | // <Feature Abort> is received. |
| 62 | // But implemented this way to save the time having to wait for |
| 63 | // <Feature Abort>. |
| 64 | setArcStatus(true); |
| 65 | // If succeeds to send <Report ARC Initiated>, wait general timeout |
| 66 | // to check whether there is no <Feature Abort> for <Report ARC Initiated>. |
| 67 | mState = STATE_WAITING_TIMEOUT; |
| 68 | addTimer(mState, HdmiConfig.TIMEOUT_MS); |
Jungshik Jang | d643f76 | 2014-05-22 19:28:09 +0900 | [diff] [blame] | 69 | sendReportArcInitiated(); |
Jungshik Jang | 67ea521 | 2014-05-15 14:05:24 +0900 | [diff] [blame] | 70 | } else { |
| 71 | setArcStatus(false); |
| 72 | finish(); |
| 73 | } |
| 74 | return true; |
| 75 | } |
| 76 | |
Jungshik Jang | d643f76 | 2014-05-22 19:28:09 +0900 | [diff] [blame] | 77 | private void sendReportArcInitiated() { |
| 78 | HdmiCecMessage command = |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 79 | HdmiCecMessageBuilder.buildReportArcInitiated(getSourceAddress(), mAvrAddress); |
Jungshik Jang | d643f76 | 2014-05-22 19:28:09 +0900 | [diff] [blame] | 80 | sendCommand(command, new HdmiControlService.SendMessageCallback() { |
| 81 | @Override |
| 82 | public void onSendCompleted(int error) { |
Jungshik Jang | 2e8f1b6 | 2014-09-03 08:28:02 +0900 | [diff] [blame] | 83 | if (error != Constants.SEND_RESULT_SUCCESS) { |
Jungshik Jang | d643f76 | 2014-05-22 19:28:09 +0900 | [diff] [blame] | 84 | // If fails to send <Report ARC Initiated>, disable ARC and |
| 85 | // send <Report ARC Terminated> directly. |
Jungshik Jang | d643f76 | 2014-05-22 19:28:09 +0900 | [diff] [blame] | 86 | setArcStatus(false); |
Jungshik Jang | 2e8f1b6 | 2014-09-03 08:28:02 +0900 | [diff] [blame] | 87 | HdmiLogger.debug("Failed to send <Report Arc Initiated>."); |
Jungshik Jang | d643f76 | 2014-05-22 19:28:09 +0900 | [diff] [blame] | 88 | finish(); |
| 89 | } |
| 90 | } |
| 91 | }); |
| 92 | } |
| 93 | |
Jungshik Jang | 67ea521 | 2014-05-15 14:05:24 +0900 | [diff] [blame] | 94 | private void setArcStatus(boolean enabled) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 95 | boolean wasEnabled = tv().setArcStatus(enabled); |
Jungshik Jang | c94ac5c | 2014-08-27 13:48:37 +0900 | [diff] [blame] | 96 | Slog.i(TAG, "Change arc status [old:" + wasEnabled + ", new:" + enabled + "]"); |
Jungshik Jang | 67ea521 | 2014-05-15 14:05:24 +0900 | [diff] [blame] | 97 | |
| 98 | // If enabled before and set to "disabled" and send <Report Arc Terminated> to |
| 99 | // av reciever. |
| 100 | if (!enabled && wasEnabled) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 101 | sendCommand(HdmiCecMessageBuilder.buildReportArcTerminated(getSourceAddress(), |
| 102 | mAvrAddress)); |
Jungshik Jang | 67ea521 | 2014-05-15 14:05:24 +0900 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | boolean processCommand(HdmiCecMessage cmd) { |
| 108 | if (mState != STATE_WAITING_TIMEOUT) { |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | int opcode = cmd.getOpcode(); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 113 | if (opcode == Constants.MESSAGE_FEATURE_ABORT) { |
Jungshik Jang | 339227d | 2014-08-25 15:37:20 +0900 | [diff] [blame] | 114 | int originalOpcode = cmd.getParams()[0] & 0xFF; |
| 115 | if (originalOpcode == Constants.MESSAGE_REPORT_ARC_INITIATED) { |
Jungshik Jang | 2e8f1b6 | 2014-09-03 08:28:02 +0900 | [diff] [blame] | 116 | HdmiLogger.debug("Feature aborted for <Report Arc Initiated>"); |
Jungshik Jang | 339227d | 2014-08-25 15:37:20 +0900 | [diff] [blame] | 117 | setArcStatus(false); |
| 118 | finish(); |
| 119 | return true; |
| 120 | } |
Jungshik Jang | 67ea521 | 2014-05-15 14:05:24 +0900 | [diff] [blame] | 121 | } |
Jungshik Jang | 339227d | 2014-08-25 15:37:20 +0900 | [diff] [blame] | 122 | return false; |
Jungshik Jang | 67ea521 | 2014-05-15 14:05:24 +0900 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | @Override |
| 126 | void handleTimerEvent(int state) { |
| 127 | if (mState != state || mState != STATE_WAITING_TIMEOUT) { |
| 128 | return; |
| 129 | } |
| 130 | // Expire timeout for <Feature Abort>. |
| 131 | finish(); |
| 132 | } |
| 133 | } |