summaryrefslogtreecommitdiff
path: root/hostsidetests/hdmicec/src/android/hdmicec/cts/targetprep/CecPortDiscoverer.java
blob: fcdac7c3b56658f8e2dc7d20da498e5fc80f3e7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.hdmicec.cts.targetprep;

import android.hdmicec.cts.BaseHdmiCecCtsTest;
import android.hdmicec.cts.CecMessage;
import android.hdmicec.cts.HdmiCecClientWrapper;
import android.hdmicec.cts.HdmiCecConstants;
import android.hdmicec.cts.LogicalAddress;
import android.hdmicec.cts.error.CecClientWrapperException;
import android.hdmicec.cts.error.ErrorCodes;

import com.android.tradefed.device.DeviceNotAvailableException;
import com.android.tradefed.device.ITestDevice;
import com.android.tradefed.invoker.TestInformation;
import com.android.tradefed.log.LogUtil.CLog;
import com.android.tradefed.targetprep.BaseTargetPreparer;
import com.android.tradefed.targetprep.TargetSetupError;
import com.android.tradefed.util.RunUtil;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/* Sets up the CEC tests by discovering which port the CEC adapter connected to */
public class CecPortDiscoverer extends BaseTargetPreparer {

    private static final int TIMEOUT_MILLIS = 15000;
    private static final int MAX_RETRY_COUNT = 3;

    private File mCecMapDir = HdmiCecConstants.CEC_MAP_FOLDER;
    private File mDeviceEntry = null;
    private File mPortEntry = null;

    private String instructionsOnError =
            "\nIn case the setup is valid according to the README and "
                    + "the test should have run, please verify that\n"
                    + "1. cec-client is not running already on the port the DUT is connected to\n"
                    + "2. "
                    + HdmiCecConstants.CEC_MAP_FOLDER
                    + " has been cleared of stale mappings (in case a"
                    + " test was interrupted)\n";

    /** {@inheritDoc} */
    @Override
    public void setUp(TestInformation testInfo)
            throws TargetSetupError, DeviceNotAvailableException {
        ITestDevice device = testInfo.getDevice();
        if (!device.hasFeature("feature:android.hardware.hdmi.cec")
                || !device.hasFeature("feature:android.software.leanback")
                || isTvEmulator(device)) {
            // We are testing non-HDMI devices, so don't check for adapter availability
            return;
        }
        synchronized (CecPortDiscoverer.class) {
            if (!mCecMapDir.exists()) {
                mCecMapDir.mkdirs();
            }
            initValidClient(device);
        }
    }

    /**
     * Check if the DUT is an emulator.
     * @param device The DUT.
     * @return true If the DUT is an emulator.
     * @throws DeviceNotAvailableException
     */
    public static boolean isTvEmulator(ITestDevice device) throws DeviceNotAvailableException {
        return !device.executeShellCommand("getprop " + HdmiCecConstants.PROPERTY_BUILD_FINGERPRINT
                        + " | grep \"cf_x86\"")
                .isEmpty();
    }

    /** {@inheritDoc} */
    @Override
    public void tearDown(TestInformation testInfo, Throwable e) {
        if (mDeviceEntry != null) {
            mDeviceEntry.delete();
        }
        if (mPortEntry != null) {
            mPortEntry.delete();
        }
    }

    private void initValidClient(ITestDevice device)
            throws TargetSetupError, DeviceNotAvailableException {

        List<String> launchCommand = new ArrayList();
        Process mCecClient;
        /* This is a semi-functional object only, the methods that we can use are limited. */
        HdmiCecClientWrapper cecClientWrapper = new HdmiCecClientWrapper();

        launchCommand.add("cec-client");
        String serialNo = "";

        try {
            List<String> comPorts = cecClientWrapper.getValidCecClientPorts();

            if (comPorts.size() == 0) {
                throw new TargetSetupError("No adapters connected to host.");
            }

            int targetDeviceType =
                    BaseHdmiCecCtsTest.getTargetLogicalAddress(device).getDeviceType();
            int toDevice;
            launchCommand.add("-t");
            launchCommand.add("r");
            launchCommand.add("-t");
            if (targetDeviceType == HdmiCecConstants.CEC_DEVICE_TYPE_TV) {
                toDevice = LogicalAddress.PLAYBACK_1.getLogicalAddressAsInt();
                launchCommand.add("p");
            } else {
                toDevice = LogicalAddress.TV.getLogicalAddressAsInt();
                launchCommand.add("x");
            }

            serialNo = device.getProperty("ro.serialno");
            String serialNoHashCode = String.valueOf(serialNo.hashCode());
            String serialNoParam = CecMessage.convertStringToHexParams(serialNoHashCode);
            /*
             * formatParams prefixes with a ':' that we do not want in the vendorcommand
             * command line utility.
             */
            serialNoParam = serialNoParam.substring(1);
            StringBuilder sendVendorCommand = new StringBuilder("cmd hdmi_control vendorcommand ");
            sendVendorCommand.append(" -t " + targetDeviceType);
            sendVendorCommand.append(" -d " + toDevice);
            sendVendorCommand.append(" -a " + serialNoParam);

            for (String port : comPorts) {
                launchCommand.add(port);
                boolean portBeingRetried = true;
                int retryCount = 0;
                do {
                    File adapterMapping = new File(mCecMapDir, getPortFilename(port));
                    /*
                     * Check for the mapping before each iteration. It is possible that another DUT
                     * got mapped to this port while this DUT is still trying to discover if this is
                     * the right port.
                     */
                    if (adapterMapping.exists()) {
                        /* Exit the current port's retry loop */
                        launchCommand.remove(port);
                        break;
                    }
                    mCecClient = RunUtil.getDefault().runCmdInBackground(launchCommand);
                    try (BufferedReader inputConsole =
                            new BufferedReader(
                                    new InputStreamReader(mCecClient.getInputStream()))) {

                        /* Wait for the client to become ready */
                        if (cecClientWrapper.checkConsoleOutput(
                                "waiting for input", TIMEOUT_MILLIS, inputConsole)) {

                            device.executeShellCommand(sendVendorCommand.toString());
                            if (cecClientWrapper.checkConsoleOutput(
                                    serialNoParam, TIMEOUT_MILLIS, inputConsole)) {
                                if (targetDeviceType != HdmiCecConstants.CEC_DEVICE_TYPE_TV) {
                                    // Timeout in milliseconds
                                    long getVersionTimeout = 3000;
                                    BufferedWriter outputConsole =
                                            new BufferedWriter(
                                                    new OutputStreamWriter(
                                                            mCecClient.getOutputStream()));

                                    String getVersionMessage = "tx 10:9f";
                                    cecClientWrapper.sendConsoleMessage(
                                            getVersionMessage, outputConsole);
                                    String getVersionResponse = "01:9e";
                                    if (cecClientWrapper.checkConsoleOutput(
                                            getVersionResponse, getVersionTimeout, inputConsole)) {
                                        throw new Exception(
                                                "Setup error! The sink device (TV) in the test setup"
                                                    + " seems to have CEC enabled. Please disable"
                                                    + " and retry tests.");
                                    }
                                }

                                writeMapping(port, serialNo);
                                return;
                            }
                            /* Since it did not find the required message. Check another port */
                            portBeingRetried = false;
                        } else {
                            CLog.e("Console did not get ready!");
                            throw new CecClientWrapperException(ErrorCodes.CecPortBusy);
                        }
                    } catch (CecClientWrapperException cwe) {
                        if (cwe.getErrorCode() != ErrorCodes.CecPortBusy) {
                            retryCount = MAX_RETRY_COUNT;
                        } else {
                            retryCount++;
                        }
                        if (retryCount >= MAX_RETRY_COUNT) {
                            /* We have retried enough number of times. Check another port */
                            portBeingRetried = false;
                        } else {
                            /* Give a break before checking the port again. */
                            TimeUnit.MILLISECONDS.sleep(TIMEOUT_MILLIS);
                        }
                    } finally {
                        /* Kill the unwanted cec-client process. */
                        Process killProcess = mCecClient.destroyForcibly();
                        killProcess.waitFor(60, TimeUnit.SECONDS);
                    }
                } while (portBeingRetried);
                launchCommand.remove(port);
            }
        } catch (IOException | InterruptedException e) {
            throw new TargetSetupError(
                    "Caught "
                            + e.getClass().getSimpleName()
                            + ". "
                            + "Could not get adapter mapping for device"
                            + serialNo
                            + "."
                            + instructionsOnError,
                    e);
        } catch (Exception generic) {
            throw new TargetSetupError(
                    "Caught an exception with message '"
                            + generic.getMessage()
                            + "'. "
                            + "Could not get adapter mapping for device"
                            + serialNo
                            + "."
                            + instructionsOnError,
                    generic);
        }
        throw new TargetSetupError(
                "Device " + serialNo + " not connected to any adapter!" + instructionsOnError);
    }

    private String getPortFilename(String port) {
        /* Returns only the name of the port, ignoring the path */
        return new File(port).getName();
    }

    private void writeMapping(String port, String serialNo) throws TargetSetupError {
        mDeviceEntry = new File(mCecMapDir, serialNo);
        mPortEntry = new File(mCecMapDir, getPortFilename(port));
        try (BufferedWriter device = new BufferedWriter(new FileWriter(mDeviceEntry));
                BufferedWriter adapter = new BufferedWriter(new FileWriter(mPortEntry))) {
            mDeviceEntry.createNewFile();
            device.write(port);
            device.flush();
            adapter.write(serialNo);
            adapter.flush();
        } catch (IOException ioe) {
            throw new TargetSetupError(
                    "Could not create mapping file " + mCecMapDir + "/" + mDeviceEntry.getName());
        }
    }
}