summaryrefslogtreecommitdiff
path: root/services/core/java/com/android/server/hdmi/HdmiControlShellCommand.java
blob: bf6633e072e1e093d452ac3b647705039efc0df3 (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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
 * 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 com.android.server.hdmi;


import android.hardware.hdmi.HdmiControlManager;
import android.hardware.hdmi.IHdmiControlCallback;
import android.hardware.hdmi.IHdmiControlService;
import android.os.RemoteException;
import android.os.ShellCommand;
import android.util.Slog;

import java.io.PrintWriter;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

final class HdmiControlShellCommand extends ShellCommand {

    private static final String TAG = "HdmiShellCommand";

    private final IHdmiControlService.Stub mBinderService;

    final CountDownLatch mLatch;
    AtomicInteger mCecResult;
    IHdmiControlCallback.Stub mHdmiControlCallback;

    HdmiControlShellCommand(IHdmiControlService.Stub binderService) {
        mBinderService = binderService;
        mLatch = new CountDownLatch(1);
        mCecResult = new AtomicInteger();
        mHdmiControlCallback =
                new IHdmiControlCallback.Stub() {
                    @Override
                    public void onComplete(int result) {
                        getOutPrintWriter().println(" done (" + getResultString(result) + ")");
                        mCecResult.set(result);
                        mLatch.countDown();
                    }
                };
    }

    @Override
    public int onCommand(String cmd) {
        if (cmd == null) {
            return handleDefaultCommands(cmd);
        }

        try {
            return handleShellCommand(cmd);
        } catch (Exception e) {
            getErrPrintWriter().println(
                    "Caught error for command '" + cmd + "': " + e.getMessage());
            Slog.e(TAG, "Error handling hdmi_control shell command: " + cmd, e);
            return 1;
        }
    }

    @Override
    public void onHelp() {
        PrintWriter pw = getOutPrintWriter();

        pw.println("HdmiControlManager (hdmi_control) commands:");
        pw.println("  help");
        pw.println("      Print this help text.");
        pw.println("  onetouchplay, otp");
        pw.println("      Send the \"One Touch Play\" feature from a source to the TV");
        pw.println("  vendorcommand --device_type <originating device type>");
        pw.println("                --destination <destination device>");
        pw.println("                --args <vendor specific arguments>");
        pw.println("                [--id <true if vendor command should be sent with vendor id>]");
        pw.println("      Send a Vendor Command to the given target device");
        pw.println("  cec_setting get <setting name>");
        pw.println("      Get the current value of a CEC setting");
        pw.println("  cec_setting set <setting name> <value>");
        pw.println("      Set the value of a CEC setting");
        pw.println("  setsystemaudiomode, setsam [on|off]");
        pw.println("      Sets the System Audio Mode feature on or off on TV devices");
        pw.println("  setarc [on|off]");
        pw.println("      Sets the ARC feature on or off on TV devices");
        pw.println("  deviceselect <device id>");
        pw.println("      Switch to device with given id");
        pw.println("      The device's id is represented by its logical address.");
        pw.println("  history_size get");
        pw.println("      Gets the number of messages that can be stored in dumpsys history");
        pw.println("  history_size set <new_size>");
        pw.println("      Changes the number of messages that can be stored in dumpsys history to"
                       + " new_size");
    }

    private int handleShellCommand(String cmd) throws RemoteException {
        PrintWriter pw = getOutPrintWriter();

        switch (cmd) {
            case "otp":
            case "onetouchplay":
                return oneTouchPlay(pw);
            case "vendorcommand":
                return vendorCommand(pw);
            case "cec_setting":
                return cecSetting(pw);
            case "setsystemaudiomode":
            case "setsam":
                return setSystemAudioMode(pw);
            case "setarc":
                return setArcMode(pw);
            case "deviceselect":
                return deviceSelect(pw);
            case "history_size":
                return historySize(pw);
        }

        return handleDefaultCommands(cmd);
    }

    private int deviceSelect(PrintWriter pw) throws RemoteException {
        if (getRemainingArgsCount() != 1) {
            throw new IllegalArgumentException("Expected exactly 1 argument.");
        }
        int deviceId = Integer.parseInt(getNextArg());

        pw.print("Sending Device Select...");
        mBinderService.deviceSelect(deviceId, mHdmiControlCallback);

        if (!receiveCallback("Device Select")) {
            return 1;
        }

        return mCecResult.get() == HdmiControlManager.RESULT_SUCCESS ? 0 : 1;
    }

    private int oneTouchPlay(PrintWriter pw) throws RemoteException {
        pw.print("Sending One Touch Play...");
        mBinderService.oneTouchPlay(mHdmiControlCallback);

        if (!receiveCallback("One Touch Play")) {
            return 1;
        }

        return mCecResult.get() == HdmiControlManager.RESULT_SUCCESS ? 0 : 1;
    }

    private int vendorCommand(PrintWriter pw) throws RemoteException {
        if (6 > getRemainingArgsCount()) {
            throw new IllegalArgumentException("Expected 3 arguments.");
        }

        int deviceType = -1;
        int destination = -1;
        String parameters = "";
        boolean hasVendorId = false;

        String arg = getNextOption();
        while (arg != null) {
            switch (arg) {
                case "-t":
                case "--device_type":
                    deviceType = Integer.parseInt(getNextArgRequired());
                    break;
                case "-d":
                case "--destination":
                    destination = Integer.parseInt(getNextArgRequired());
                    break;
                case "-a":
                case "--args":
                    parameters = getNextArgRequired();
                    break;
                case "-i":
                case "--id":
                    hasVendorId = Boolean.parseBoolean(getNextArgRequired());
                    break;
                default:
                    throw new IllegalArgumentException("Unknown argument: " + arg);
            }
            arg = getNextArg();
        }

        String[] parts = parameters.split(":");
        byte[] params = new byte[parts.length];
        for (int i = 0; i < params.length; i++) {
            params[i] = (byte) Integer.parseInt(parts[i], 16);
        }

        pw.println("Sending <Vendor Command>");
        mBinderService.sendVendorCommand(deviceType, destination, params, hasVendorId);
        return 0;
    }

    private int cecSetting(PrintWriter pw) throws RemoteException {
        if (getRemainingArgsCount() < 1) {
            throw new IllegalArgumentException("Expected at least 1 argument (operation).");
        }
        String operation = getNextArgRequired();
        switch (operation) {
            case "get": {
                String setting = getNextArgRequired();
                try {
                    String value = mBinderService.getCecSettingStringValue(setting);
                    pw.println(setting + " = " + value);
                } catch (IllegalArgumentException e) {
                    int intValue = mBinderService.getCecSettingIntValue(setting);
                    pw.println(setting + " = " + intValue);
                }
                return 0;
            }
            case "set": {
                String setting = getNextArgRequired();
                String value = getNextArgRequired();
                try {
                    mBinderService.setCecSettingStringValue(setting, value);
                    pw.println(setting + " = " + value);
                } catch (IllegalArgumentException e) {
                    int intValue = Integer.parseInt(value);
                    mBinderService.setCecSettingIntValue(setting, intValue);
                    pw.println(setting + " = " + intValue);
                }
                return 0;
            }
            default:
                throw new IllegalArgumentException("Unknown operation: " + operation);
        }
    }

    private int setSystemAudioMode(PrintWriter pw) throws RemoteException {
        if (1 > getRemainingArgsCount()) {
            throw new IllegalArgumentException(
                    "Please indicate if System Audio Mode should be turned \"on\" or \"off\".");
        }

        String arg = getNextArg();
        if (arg.equals("on")) {
            pw.println("Setting System Audio Mode on");
            mBinderService.setSystemAudioMode(true, mHdmiControlCallback);
        } else if (arg.equals("off")) {
            pw.println("Setting System Audio Mode off");
            mBinderService.setSystemAudioMode(false, mHdmiControlCallback);
        } else {
            throw new IllegalArgumentException(
                    "Please indicate if System Audio Mode should be turned \"on\" or \"off\".");
        }

        if (!receiveCallback("Set System Audio Mode")) {
            return 1;
        }

        return mCecResult.get() == HdmiControlManager.RESULT_SUCCESS ? 0 : 1;
    }

    private int setArcMode(PrintWriter pw) throws RemoteException {
        if (1 > getRemainingArgsCount()) {
            throw new IllegalArgumentException(
                    "Please indicate if ARC mode should be turned \"on\" or \"off\".");
        }

        String arg = getNextArg();
        if (arg.equals("on")) {
            pw.println("Setting ARC mode on");
            mBinderService.setArcMode(true);
        } else if (arg.equals("off")) {
            pw.println("Setting ARC mode off");
            mBinderService.setArcMode(false);
        } else {
            throw new IllegalArgumentException(
                    "Please indicate if ARC mode should be turned \"on\" or \"off\".");
        }

        return 0;
    }

    private int historySize(PrintWriter pw) throws RemoteException {
        if (1 > getRemainingArgsCount()) {
            throw new IllegalArgumentException("Use 'set' or 'get' for the command action");
        }

        String operation = getNextArgRequired();
        switch (operation) {
            case "get": {
                int value = mBinderService.getMessageHistorySize();
                pw.println("CEC dumpsys message history size = " + value);
                return 0;
            }
            case "set": {
                String arg = getNextArgRequired();
                int value;
                try {
                    value = Integer.parseInt(arg);
                } catch (NumberFormatException nfe) {
                    pw.println("Cannot set CEC dumpsys message history size to " + arg);
                    return 1;
                }
                if (mBinderService.setMessageHistorySize(value)) {
                    pw.println("Setting CEC dumpsys message history size to " + value);
                } else {
                    pw.println(
                            "Message history size not changed, was it lower than the minimum "
                                    + "size?");
                }
                return 0;
            }
            default:
                throw new IllegalArgumentException("Unknown operation: " + operation);
        }
    }

    private boolean receiveCallback(String command) {
        try {
            if (!mLatch.await(HdmiConfig.TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
                getErrPrintWriter().println(command + " timed out.");
                return false;
            }
        } catch (InterruptedException e) {
            getErrPrintWriter().println("Caught InterruptedException");
            Thread.currentThread().interrupt();
        }
        return true;
    }

    private String getResultString(int result) {
        switch (result) {
            case HdmiControlManager.RESULT_SUCCESS:
                return "Success";
            case HdmiControlManager.RESULT_TIMEOUT:
                return "Timeout";
            case HdmiControlManager.RESULT_SOURCE_NOT_AVAILABLE:
                return "Source not available";
            case HdmiControlManager.RESULT_TARGET_NOT_AVAILABLE:
                return "Target not available";
            case HdmiControlManager.RESULT_EXCEPTION:
                return "Exception";
            case HdmiControlManager.RESULT_INCORRECT_MODE:
                return "Incorrect mode";
            case HdmiControlManager.RESULT_COMMUNICATION_FAILED:
                return "Communication Failed";
        }
        return Integer.toString(result);
    }
}