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