1#
2# Copyright (C) 2018 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
17import logging
18
19from host_controller import common
20from host_controller.command_processor import base_command_processor
21from host_controller.utils.usb import usb_utils
22
23from vts.utils.python.common import cmd_utils
24
25
26class CommandAdb(base_command_processor.BaseCommandProcessor):
27    """Command processor for adb command.
28
29    Attributes:
30        arg_parser: ConsoleArgumentParser object, argument parser.
31        command: string, command name which this processor will handle.
32        command_detail: string, detailed explanation for the command.
33    """
34
35    command = "adb"
36    command_detail = "Runs an ADB command."
37
38    # @Override
39    def SetUp(self):
40        """Initializes the parser for device command."""
41        self.arg_parser.add_argument(
42            "--serial",
43            "-s",
44            default=None,
45            help="The target device serial to run the command.")
46        self.arg_parser.add_argument(
47            "--timeout",
48            type=float,
49            default=common.DEFAULT_DEVICE_TIMEOUT_SECS,
50            help="The maximum timeout value of this command in seconds. "
51            "Set to 0 to disable the timeout functionality.")
52        self.arg_parser.add_argument(
53            "command",
54            metavar="COMMAND",
55            nargs="+",
56            help="The command to be executed. If the command contains "
57            "arguments starting with \"-\", place the command at end of line "
58            "after \"--\".")
59
60    # @Override
61    def Run(self, arg_line):
62        """Runs an adb command."""
63        args = self.arg_parser.ParseLine(arg_line)
64        cmd_list = ["adb"]
65        if args.serial:
66            if "," in args.serial:
67                logging.error("Only one serial can be specified")
68                return False
69            cmd_list.append("-s %s" % args.serial)
70        cmd_list.extend(self.ReplaceVars(args.command))
71        if args.timeout == 0:
72            stdout, stderr, retcode = cmd_utils.ExecuteOneShellCommand(
73                " ".join(cmd_list))
74        else:
75            stdout, stderr, retcode = cmd_utils.ExecuteOneShellCommand(
76                " ".join(cmd_list), args.timeout,
77                usb_utils.ResetUsbDeviceOfSerial_Callback, args.serial)
78        if stdout:
79            logging.info(stdout)
80        if stderr:
81            logging.error(stderr)
82            if self.console.job_pool and args.serial:
83                self.console.device_status[
84                    args.serial] = common._DEVICE_STATUS_DICT["error"]
85        if retcode != 0:
86            return False
87