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