• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2#
3#   Copyright 2017 Google, Inc.
4#
5#   Licensed under the Apache License, Version 2.0 (the "License");
6#   you may not use this file except in compliance with the License.
7#   You may obtain a copy of the License at
8#
9#       http://www.apache.org/licenses/LICENSE-2.0
10#
11#   Unless required by applicable law or agreed to in writing, software
12#   distributed under the License is distributed on an "AS IS" BASIS,
13#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#   See the License for the specific language governing permissions and
15#   limitations under the License.
16
17import logging
18import time
19import acts_contrib.test_utils.bt.BleEnum as bleenum
20import acts_contrib.test_utils.instrumentation.device.command.instrumentation_command_builder as icb
21
22BLE_LOCATION_SCAN_ENABLE = 'settings put global ble_scan_always_enabled 1'
23BLE_LOCATION_SCAN_DISABLE = 'settings put global ble_scan_always_enabled 0'
24START_BLE_ADV = 'am start -n com.google.bletesting/.ActsCommandExecutor --es command ADVERTISE#2#2#30000'
25START_BLE_SCAN = 'am start -n com.google.bletesting/.ActsCommandExecutor --es command SCAN#2#10000'
26SCAN_DURATION = 10
27SCREEN_WAIT_TIME = 1
28
29
30class MediaControl(object):
31    """Media control using adb shell for power testing.
32
33    Object to control media play status using adb.
34    """
35    def __init__(self, android_device, music_file):
36        """Initialize the media_control class.
37
38        Args:
39            android_dut: android_device object
40            music_file: location of the music file
41        """
42        self.android_device = android_device
43        self.music_file = music_file
44
45    def player_on_foreground(self):
46        """Turn on screen and make sure media play is on foreground
47
48        All media control keycode only works when screen is on and media player
49        is on the foreground. Turn off screen first and turn it on to make sure
50        all operation is based on the same screen status. Otherwise, 'MENU' key
51        would block command to be sent.
52        """
53        self.android_device.droid.goToSleepNow()
54        time.sleep(SCREEN_WAIT_TIME)
55        self.android_device.droid.wakeUpNow()
56        time.sleep(SCREEN_WAIT_TIME)
57        self.android_device.send_keycode('MENU')
58        time.sleep(SCREEN_WAIT_TIME)
59
60    def play(self):
61        """Start playing music.
62
63        """
64        self.player_on_foreground()
65        PLAY = 'am start -a android.intent.action.VIEW -d file://{} -t audio/wav'.format(
66            self.music_file)
67        self.android_device.adb.shell(PLAY)
68
69    def pause(self):
70        """Pause music.
71
72        """
73        self.player_on_foreground()
74        self.android_device.send_keycode('MEDIA_PAUSE')
75
76    def resume(self):
77        """Pause music.
78
79        """
80        self.player_on_foreground()
81        self.android_device.send_keycode('MEDIA_PLAY')
82
83    def stop(self):
84        """Stop music and close media play.
85
86        """
87        self.player_on_foreground()
88        self.android_device.send_keycode('MEDIA_STOP')
89
90
91def start_apk_ble_adv(dut, adv_mode, adv_power_level, adv_duration):
92    """Trigger BLE advertisement from power-test.apk.
93
94    Args:
95        dut: Android device under test, type AndroidDevice obj
96        adv_mode: The BLE advertisement mode.
97            {0: 'LowPower', 1: 'Balanced', 2: 'LowLatency'}
98        adv_power_leve: The BLE advertisement TX power level.
99            {0: 'UltraLowTXPower', 1: 'LowTXPower', 2: 'MediumTXPower,
100            3: HighTXPower}
101        adv_duration: duration of advertisement in seconds, type int
102    """
103
104    adv_duration = str(adv_duration) + 's'
105    builder = icb.InstrumentationTestCommandBuilder.default()
106    builder.add_test_class(
107        "com.google.android.device.power.tests.ble.BleAdvertise")
108    builder.set_manifest_package("com.google.android.device.power")
109    builder.set_runner("androidx.test.runner.AndroidJUnitRunner")
110    builder.add_key_value_param("cool-off-duration", "0s")
111    builder.add_key_value_param("idle-duration", "0s")
112    builder.add_key_value_param(
113        "com.android.test.power.receiver.ADVERTISE_MODE", adv_mode)
114    builder.add_key_value_param("com.android.test.power.receiver.POWER_LEVEL",
115                                adv_power_level)
116    builder.add_key_value_param(
117        "com.android.test.power.receiver.ADVERTISING_DURATION", adv_duration)
118
119    adv_command = builder.build() + ' &'
120    logging.info('Start BLE {} at {} for {} seconds'.format(
121        bleenum.AdvertiseSettingsAdvertiseMode(adv_mode).name,
122        bleenum.AdvertiseSettingsAdvertiseTxPower(adv_power_level).name,
123        adv_duration))
124    dut.adb.shell_nb(adv_command)
125
126
127def start_apk_ble_scan(dut, scan_mode, scan_duration):
128    """Build the command to trigger BLE scan from power-test.apk.
129
130    Args:
131        dut: Android device under test, type AndroidDevice obj
132        scan_mode: The BLE scan mode.
133            {0: 'LowPower', 1: 'Balanced', 2: 'LowLatency', -1: 'Opportunistic'}
134        scan_duration: duration of scan in seconds, type int
135    Returns:
136        adv_command: the command for BLE scan
137    """
138    scan_duration = str(scan_duration) + 's'
139    builder = icb.InstrumentationTestCommandBuilder.default()
140    builder.set_proto_path()
141    builder.add_flag('--no-isolated-storage')
142    builder.add_test_class("com.google.android.device.power.tests.ble.BleScan")
143    builder.set_manifest_package("com.google.android.device.power")
144    builder.set_runner("androidx.test.runner.AndroidJUnitRunner")
145    builder.add_key_value_param("cool-off-duration", "0s")
146    builder.add_key_value_param("idle-duration", "0s")
147    builder.add_key_value_param("com.android.test.power.receiver.SCAN_MODE",
148                                scan_mode)
149    builder.add_key_value_param("com.android.test.power.receiver.MATCH_MODE",
150                                2)
151    builder.add_key_value_param(
152        "com.android.test.power.receiver.SCAN_DURATION", scan_duration)
153    builder.add_key_value_param(
154        "com.android.test.power.receiver.CALLBACK_TYPE", 1)
155    builder.add_key_value_param("com.android.test.power.receiver.FILTER",
156                                'true')
157
158    scan_command = builder.build() + ' &'
159    logging.info('Start BLE {} scans for {} seconds'.format(
160        bleenum.ScanSettingsScanMode(scan_mode).name, scan_duration))
161    dut.adb.shell_nb(scan_command)
162
163
164def establish_ble_connection(client_ad, server_ad, con_priority):
165    """Establish BLE connection using BLE_Test.apk.
166
167    Args:
168        client_ad: the Android device performing the BLE connection.
169        server_ad: the Android device accepting the BLE connection.
170        con_priority: The BLE Connection Priority.
171            {0: 'BALANCED'= Use the connection parameters recommended by the Bluetooth SIG,
172            1: 'HIGH'= Request a high priority, low latency connection,
173            2: 'LOW_POWER= Request low power, reduced data rate connection parameters }
174    """
175    server_ad.adb.shell(START_BLE_ADV)
176    time.sleep(5)
177    client_ad.adb.shell(START_BLE_SCAN)
178    time.sleep(SCAN_DURATION)
179    logging.info("Connection Priority is:{}".format(con_priority))
180    client_ad.adb.shell(
181        'am start -n com.google.bletesting/.ActsCommandExecutor '
182        '--es command GATTCONNECT#{}'.format(con_priority))
183    logging.info(
184        "BLE Connection Successful with Connection Priority:{}".format(
185            con_priority))
186