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
17package com.android.server.hdmi;
18
19import android.hardware.hdmi.HdmiDeviceInfo;
20import android.util.Slog;
21
22/**
23 * Base feature action class for <Request ARC Initiation>/<Request ARC Termination>.
24 */
25abstract class RequestArcAction extends HdmiCecFeatureAction {
26    private static final String TAG = "RequestArcAction";
27
28    // State in which waits for ARC response.
29    protected static final int STATE_WATING_FOR_REQUEST_ARC_REQUEST_RESPONSE = 1;
30
31    // Logical address of AV Receiver.
32    protected final int mAvrAddress;
33
34    /**
35     * @Constructor
36     *
37     * @param source {@link HdmiCecLocalDevice} instance
38     * @param avrAddress address of AV receiver. It should be AUDIO_SYSTEM type
39     * @throw IllegalArugmentException if device type of sourceAddress and avrAddress
40     *                      is invalid
41     */
42    RequestArcAction(HdmiCecLocalDevice source, int avrAddress) {
43        super(source);
44        HdmiUtils.verifyAddressType(getSourceAddress(), HdmiDeviceInfo.DEVICE_TV);
45        HdmiUtils.verifyAddressType(avrAddress, HdmiDeviceInfo.DEVICE_AUDIO_SYSTEM);
46        mAvrAddress = avrAddress;
47    }
48
49    @Override
50    boolean processCommand(HdmiCecMessage cmd) {
51        if (mState != STATE_WATING_FOR_REQUEST_ARC_REQUEST_RESPONSE
52                || !HdmiUtils.checkCommandSource(cmd, mAvrAddress, TAG)) {
53            return false;
54        }
55        int opcode = cmd.getOpcode();
56        switch (opcode) {
57            // Handles only <Feature Abort> here and, both <Initiate ARC> and <Terminate ARC>
58            // are handled in HdmiControlService itself because both can be
59            // received without <Request ARC Initiation> or <Request ARC Termination>.
60            case Constants.MESSAGE_FEATURE_ABORT:
61                int originalOpcode = cmd.getParams()[0] & 0xFF;
62                if (originalOpcode == Constants.MESSAGE_REQUEST_ARC_INITIATION
63                        || originalOpcode == Constants.MESSAGE_REQUEST_ARC_TERMINATION) {
64                    disableArcTransmission();
65                    finish();
66                    return true;
67                } else {
68                    return false;
69                }
70        }
71        return false;
72    }
73
74    protected final void disableArcTransmission() {
75        // Start Set ARC Transmission State action.
76        SetArcTransmissionStateAction action = new SetArcTransmissionStateAction(localDevice(),
77                mAvrAddress, false);
78        addAndStartAction(action);
79    }
80
81    @Override
82    final void handleTimerEvent(int state) {
83        if (mState != state || state != STATE_WATING_FOR_REQUEST_ARC_REQUEST_RESPONSE) {
84            return;
85        }
86        HdmiLogger.debug("[T]RequestArcAction.");
87        disableArcTransmission();
88        finish();
89    }
90}
91