summaryrefslogtreecommitdiff
path: root/harnesses/host_controller/command_processor/command_dut.py
blob: a722b41a1477f593794c44d308502d0827e71a73 (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
#
# Copyright (C) 2018 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.
#

import logging

from host_controller import common
from host_controller.command_processor import base_command_processor

from vts.utils.python.common import cmd_utils
from vts.utils.python.controllers import adb
from vts.utils.python.controllers import android_device

# Default index of setStreamVolume() from IAudioService.aidl (1 based)
SETSTREAMVOLUME_INDEX_DEFAULT = 3

# Indices of each stream type (can be checked with "adb shell dumpsys audio" on the command line)
STREAM_TYPE_CALL = 0
STREAM_TYPE_RINGTONE = 2
STREAM_TYPE_MEDIA = 3
STREAM_TYPE_ALARM = 4

STREAM_TYPE_LIST = [
    STREAM_TYPE_CALL,
    STREAM_TYPE_RINGTONE,
    STREAM_TYPE_MEDIA,
    STREAM_TYPE_ALARM,
]


class CommandDUT(base_command_processor.BaseCommandProcessor):
    """Command processor for DUT command.

    Attributes:
        arg_parser: ConsoleArgumentParser object, argument parser.
        console: cmd.Cmd console object.
        command: string, command name which this processor will handle.
        command_detail: string, detailed explanation for the command.
    """

    command = "dut"
    command_detail = "Performs certain operations on DUT (Device Under Test)."

    # @Override
    def SetUp(self):
        """Initializes the parser for dut command."""
        self.arg_parser.add_argument(
            "--operation",
            choices=("wifi_on", "wifi_off", 'volume_mute', 'volume_max'),
            default="",
            required=True,
            help="Operation to perform.")
        self.arg_parser.add_argument(
            "--serial", default="", required=True, help="The device serial.")
        self.arg_parser.add_argument(
            "--ap",
            default="",  # Required only for wifi_on
            help="Access point (AP) name for 'wifi_on' operation.")
        self.arg_parser.add_argument(
            "--volume_levels",
            type=int,
            default=30,  # Required only for volume_mute and volume_max
            help="The number of volume control levels.")
        self.arg_parser.add_argument(
            "--version",
            type=float,
            default=8.0,
            help="System version information of the device on which "
            "the test will run.")

    # @Override
    def Run(self, arg_line):
        """Performs the requested operation on the selected DUT."""
        args = self.arg_parser.ParseLine(arg_line)
        device = android_device.AndroidDevice(
            args.serial, device_callback_port=-1)
        boot_complete = device.waitForBootCompletion()
        if not boot_complete:
            logging.error("Device %s failed to bootup.", args.serial)
            self.console.device_status[
                args.serial] = common._DEVICE_STATUS_DICT["error"]
            self.console.vti_endpoint_client.SetJobStatusFromLeasedTo(
                "bootup-err")
            return False

        adb_proxy = adb.AdbProxy(serial=args.serial)
        adb_proxy.root()
        try:
            if args.operation == "wifi_on":
                adb_proxy.shell("svc wifi enable")
                if args.ap:
                    adb_proxy.install(
                        "../testcases/DATA/app/WifiUtil/WifiUtil.apk")
                    adb_proxy.shell(
                        "am instrument -e method \"connectToNetwork\" "
                        "-e ssid %s "
                        "-w com.android.tradefed.utils.wifi/.WifiUtil" %
                        args.ap)
            elif args.operation == "wifi_off":
                adb_proxy.shell("svc wifi disable")
            elif args.operation == "volume_mute":
                for _ in range(args.volume_levels):
                    adb_proxy.shell("input keyevent 25")
                self.SetOtherVolumes(adb_proxy, 0, args.version)
            elif args.operation == "volume_max":
                for _ in range(args.volume_levels):
                    adb_proxy.shell("input keyevent 24")
                self.SetOtherVolumes(adb_proxy, args.volume_levels,
                                     args.version)
        except adb.AdbError as e:
            logging.exception(e)
            return False

    def SetOtherVolumes(self, adb_proxy, volume_level, version=None):
        """Sets device's call/media/alarm volumes a certain level.

        Args:
            adb_proxy: AdbProxy, used for interacting with the device via adb.
            volume_level: int, volume level value.
            version: float, Android system version value. The index of
                     setStreamVolume() depends on the Android version.
        """
        setStreamVolume_index = SETSTREAMVOLUME_INDEX_DEFAULT
        if version and version >= 9.0:
            setStreamVolume_index = 7
        for stream_type in STREAM_TYPE_LIST:
            adb_volume_command = "service call audio %s i32 %s i32 %s i32 1" % (
                setStreamVolume_index, stream_type, volume_level)
            adb_proxy.shell(adb_volume_command)