RequestArcAction.java revision 358164c09e367199cd3d4af6381a9f342ac9f0ef
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.HdmiCec;
20import android.hardware.hdmi.HdmiCecMessage;
21import android.util.Slog;
22
23/**
24 * Base feature action class for <Request ARC Initiation>/<Request ARC Termination>.
25 */
26abstract class RequestArcAction extends FeatureAction {
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 service {@link HdmiControlService} instance
39     * @param sourceAddress logical address to be used as source address. It should
40     *                      TV type
41     * @param avrAddress address of AV receiver. It should be AUDIO_SYSTEM type
42     * @throw IllegalArugmentException if device type of sourceAddress and avrAddress
43     *                      is invalid
44     */
45    RequestArcAction(HdmiControlService service, int sourceAddress, int avrAddress) {
46        super(service, sourceAddress);
47        verifyAddressType(sourceAddress, HdmiCec.DEVICE_TV);
48        verifyAddressType(avrAddress, HdmiCec.DEVICE_AUDIO_SYSTEM);
49        mAvrAddress = avrAddress;
50    }
51
52    private static void verifyAddressType(int logicalAddress, int deviceType) {
53        int actualDeviceType = HdmiCec.getTypeFromAddress(logicalAddress);
54        if (actualDeviceType != deviceType) {
55            throw new IllegalArgumentException("Device type missmatch:[Expected:" + deviceType
56                    + ", Actual:" + actualDeviceType);
57        }
58    }
59
60    @Override
61    boolean processCommand(HdmiCecMessage cmd) {
62        if (mState != STATE_WATING_FOR_REQUEST_ARC_REQUEST_RESPONSE) {
63            return false;
64        }
65
66        int src = cmd.getSource();
67        if (src != mAvrAddress) {
68            Slog.w(TAG, "Invalid source [Expected:" + mAvrAddress + ", Actual:" + src + "]");
69            return false;
70        }
71        int opcode = cmd.getOpcode();
72        switch (opcode) {
73            // Handles only <Feature Abort> here and, both <Initiate ARC> and <Terminate ARC>
74            // are handled in HdmiControlService itself because both can be
75            // received wihtout <Request ARC Initiation> or <Request ARC Termination>.
76            case HdmiCec.MESSAGE_FEATURE_ABORT:
77                disableArcTransmission();
78                finish();
79                return true;
80            default:
81                Slog.w(TAG, "Unsupported opcode:" + cmd.toString());
82        }
83        return false;
84    }
85
86    protected final void disableArcTransmission() {
87        // Start Set ARC Transmission State action.
88        SetArcTransmissionStateAction action = new SetArcTransmissionStateAction(mService,
89                mSourceAddress, mAvrAddress, false);
90        mService.addAndStartAction(action);
91    }
92
93    @Override
94    final void handleTimerEvent(int state) {
95        if (mState != state || state != STATE_WATING_FOR_REQUEST_ARC_REQUEST_RESPONSE) {
96            return;
97        }
98        disableArcTransmission();
99        finish();
100    }
101}
102