1*8975f5c5SAndroid Build Coastguard Worker# Copyright 2023 The Chromium Authors 2*8975f5c5SAndroid Build Coastguard Worker# Use of this source code is governed by a BSD-style license that can be 3*8975f5c5SAndroid Build Coastguard Worker# found in the LICENSE file. 4*8975f5c5SAndroid Build Coastguard Worker"""Tests the connection of a target.""" 5*8975f5c5SAndroid Build Coastguard Worker 6*8975f5c5SAndroid Build Coastguard Workerimport logging 7*8975f5c5SAndroid Build Coastguard Workerimport time 8*8975f5c5SAndroid Build Coastguard Worker 9*8975f5c5SAndroid Build Coastguard Workerfrom typing import Optional 10*8975f5c5SAndroid Build Coastguard Worker 11*8975f5c5SAndroid Build Coastguard Workerfrom boot_device import boot_device, BootMode 12*8975f5c5SAndroid Build Coastguard Workerfrom common import run_ffx_command 13*8975f5c5SAndroid Build Coastguard Worker 14*8975f5c5SAndroid Build Coastguard Worker 15*8975f5c5SAndroid Build Coastguard Workerdef test_connection(target_id: Optional[str], wait_sec: int = 60) -> None: 16*8975f5c5SAndroid Build Coastguard Worker """Runs echo tests to verify that the device can be connected to. 17*8975f5c5SAndroid Build Coastguard Worker 18*8975f5c5SAndroid Build Coastguard Worker Devices may not be connectable right after being discovered by ffx, e.g. 19*8975f5c5SAndroid Build Coastguard Worker after a `ffx target wait`, so this function retries up to |wait_sec| before 20*8975f5c5SAndroid Build Coastguard Worker throwing an exception. 21*8975f5c5SAndroid Build Coastguard Worker """ 22*8975f5c5SAndroid Build Coastguard Worker start_sec = time.time() 23*8975f5c5SAndroid Build Coastguard Worker while time.time() - start_sec < wait_sec: 24*8975f5c5SAndroid Build Coastguard Worker if run_ffx_command(cmd=('target', 'echo'), 25*8975f5c5SAndroid Build Coastguard Worker target_id=target_id, 26*8975f5c5SAndroid Build Coastguard Worker check=False).returncode == 0: 27*8975f5c5SAndroid Build Coastguard Worker return 28*8975f5c5SAndroid Build Coastguard Worker time.sleep(10) 29*8975f5c5SAndroid Build Coastguard Worker 30*8975f5c5SAndroid Build Coastguard Worker run_ffx_command(cmd=('target', 'echo'), target_id=target_id) 31*8975f5c5SAndroid Build Coastguard Worker 32*8975f5c5SAndroid Build Coastguard Worker 33*8975f5c5SAndroid Build Coastguard Workerdef test_device_connection(target_id: Optional[str]) -> None: 34*8975f5c5SAndroid Build Coastguard Worker """Runs test_connection against the target_id and restarts the device if 35*8975f5c5SAndroid Build Coastguard Worker it cannot be connected.""" 36*8975f5c5SAndroid Build Coastguard Worker start_sec = time.time() 37*8975f5c5SAndroid Build Coastguard Worker while time.time() - start_sec < 1800: 38*8975f5c5SAndroid Build Coastguard Worker # pylint: disable=bare-except 39*8975f5c5SAndroid Build Coastguard Worker # First, test_connection with ffx target echo. 40*8975f5c5SAndroid Build Coastguard Worker try: 41*8975f5c5SAndroid Build Coastguard Worker test_connection(target_id=target_id, wait_sec=600) 42*8975f5c5SAndroid Build Coastguard Worker return 43*8975f5c5SAndroid Build Coastguard Worker except: 44*8975f5c5SAndroid Build Coastguard Worker # If anything wrong, reboot the device and try again. 45*8975f5c5SAndroid Build Coastguard Worker try: 46*8975f5c5SAndroid Build Coastguard Worker boot_device(target_id, BootMode.REGULAR, must_boot=True) 47*8975f5c5SAndroid Build Coastguard Worker except: 48*8975f5c5SAndroid Build Coastguard Worker # If unfortunately, the reboot failed, it's still worth 49*8975f5c5SAndroid Build Coastguard Worker # continuing the test rather than failing here. 50*8975f5c5SAndroid Build Coastguard Worker pass 51*8975f5c5SAndroid Build Coastguard Worker logging.warning( 52*8975f5c5SAndroid Build Coastguard Worker run_ffx_command(cmd=('target', 'wait'), 53*8975f5c5SAndroid Build Coastguard Worker target_id=target_id, 54*8975f5c5SAndroid Build Coastguard Worker check=False, 55*8975f5c5SAndroid Build Coastguard Worker capture_output=True).stdout) 56