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"""Functionalities to reliably reboot the device.""" 5*8975f5c5SAndroid Build Coastguard Worker 6*8975f5c5SAndroid Build Coastguard Workerimport enum 7*8975f5c5SAndroid Build Coastguard Worker 8*8975f5c5SAndroid Build Coastguard Workerfrom typing import Optional 9*8975f5c5SAndroid Build Coastguard Worker 10*8975f5c5SAndroid Build Coastguard Workerclass BootMode(enum.Enum): 11*8975f5c5SAndroid Build Coastguard Worker """Specifies boot mode for device.""" 12*8975f5c5SAndroid Build Coastguard Worker REGULAR = enum.auto() 13*8975f5c5SAndroid Build Coastguard Worker RECOVERY = enum.auto() 14*8975f5c5SAndroid Build Coastguard Worker BOOTLOADER = enum.auto() 15*8975f5c5SAndroid Build Coastguard Worker 16*8975f5c5SAndroid Build Coastguard Worker 17*8975f5c5SAndroid Build Coastguard Workerclass StateTransitionError(Exception): 18*8975f5c5SAndroid Build Coastguard Worker """Raised when target does not transition to desired state.""" 19*8975f5c5SAndroid Build Coastguard Worker 20*8975f5c5SAndroid Build Coastguard Worker 21*8975f5c5SAndroid Build Coastguard Workerdef boot_device(target_id: Optional[str], 22*8975f5c5SAndroid Build Coastguard Worker mode: BootMode, 23*8975f5c5SAndroid Build Coastguard Worker serial_num: Optional[str] = None, 24*8975f5c5SAndroid Build Coastguard Worker must_boot: bool = False) -> None: 25*8975f5c5SAndroid Build Coastguard Worker """Boot device into desired mode. 26*8975f5c5SAndroid Build Coastguard Worker 27*8975f5c5SAndroid Build Coastguard Worker Args: 28*8975f5c5SAndroid Build Coastguard Worker target_id: Optional target_id of device. 29*8975f5c5SAndroid Build Coastguard Worker mode: Desired boot mode. 30*8975f5c5SAndroid Build Coastguard Worker must_boot: Forces device to boot, regardless of current state. 31*8975f5c5SAndroid Build Coastguard Worker Raises: 32*8975f5c5SAndroid Build Coastguard Worker StateTransitionError: When final state of device is not desired. 33*8975f5c5SAndroid Build Coastguard Worker """ 34*8975f5c5SAndroid Build Coastguard Worker # Avoid cycle dependency. 35*8975f5c5SAndroid Build Coastguard Worker # This file will be replaced with serial_boot_device quite soon, later one 36*8975f5c5SAndroid Build Coastguard Worker # should be much more reliable comparing to ffx target list and ssh. So 37*8975f5c5SAndroid Build Coastguard Worker # changing the file structure is not necessary in the current situation. 38*8975f5c5SAndroid Build Coastguard Worker # pylint: disable=cyclic-import, import-outside-toplevel 39*8975f5c5SAndroid Build Coastguard Worker # pylint: disable=wrong-import-position 40*8975f5c5SAndroid Build Coastguard Worker import serial_boot_device 41*8975f5c5SAndroid Build Coastguard Worker if not serial_boot_device.boot_device(target_id, serial_num, mode, 42*8975f5c5SAndroid Build Coastguard Worker must_boot): 43*8975f5c5SAndroid Build Coastguard Worker raise StateTransitionError( 44*8975f5c5SAndroid Build Coastguard Worker f'Could not get device to desired state {mode}.') 45