def reset_usb(): """Reset USB bus for a device serial.""" if environment.is_android_cuttlefish() or environment.is_android_emulator(): # Nothing to do here. return True # App Engine does not let us import this. import fcntl # We need to get latest device path since it could be changed in reboots or # adb root restarts. try: device_path = get_device_path() except IOError: # We may reach this state if the device is no longer available. device_path = None if not device_path: # Try pulling from cache (if available). device_path = environment.get_value('DEVICE_PATH') if not device_path: logs.log_warn('No device path found, unable to reset usb.') return False try: with open(device_path, 'w') as f: fcntl.ioctl(f, USBDEVFS_RESET) except: logs.log_warn('Failed to reset usb.') return False # Wait for usb to recover. wait_for_device(recover=False) return True
def hard_reset(): """Perform a hard reset of the device.""" if environment.is_android_cuttlefish() or environment.is_android_emulator(): # There is no recovery step at this point for a cuttlefish bot, so just exit # and wait for reimage on next iteration. bad_state_reached() # For physical device. # Try hard-reset via sysrq-trigger (requires root). hard_reset_sysrq_cmd = get_adb_command_line( 'shell echo b \\> /proc/sysrq-trigger') execute_command(hard_reset_sysrq_cmd, timeout=RECOVERY_CMD_TIMEOUT) # Try soft-reset now (does not require root). soft_reset_cmd = get_adb_command_line('reboot') execute_command(soft_reset_cmd, timeout=RECOVERY_CMD_TIMEOUT)
def run_command(cmd, log_output=False, log_error=True, timeout=None, recover=True): """Run a command in adb shell.""" if isinstance(cmd, list): cmd = ' '.join([str(i) for i in cmd]) if log_output: logs.log('Running: adb %s' % cmd) if not timeout: timeout = ADB_TIMEOUT output = execute_command(get_adb_command_line(cmd), timeout, log_error) if not recover or environment.is_android_emulator(): if log_output: logs.log('Output: (%s)' % output) return output device_not_found_string_with_serial = DEVICE_NOT_FOUND_STRING.format( serial=environment.get_value('ANDROID_SERIAL')) if (output in [ DEVICE_HANG_STRING, DEVICE_OFFLINE_STRING, device_not_found_string_with_serial ]): logs.log_warn('Unable to query device, resetting device connection.') if reset_device_connection(): # Device has successfully recovered, re-run command to get output. # Continue execution and validate output next for |None| condition. output = execute_command(get_adb_command_line(cmd), timeout, log_error) else: output = DEVICE_HANG_STRING if output is DEVICE_HANG_STRING: # Handle the case where our command execution hung. This is usually when # device goes into a bad state and only way to recover is to restart it. logs.log_warn('Unable to query device, restarting device to recover.') hard_reset() # Wait until we've booted and try the command again. wait_until_fully_booted() output = execute_command(get_adb_command_line(cmd), timeout, log_error) if log_output: logs.log('Output: (%s)' % output) return output
def factory_reset(): """Reset device to factory state.""" if environment.is_android_cuttlefish() or environment.is_android_emulator(): # We cannot recover from this since there can be cases like userdata image # corruption in /data/data. Till the bug is fixed, we just need to wait # for reimage in next iteration. bad_state_reached() # A device can be stuck in a boot loop due to a bad clang library update. # Reverting that can bring a device back to good state. revert_asan_device_setup_if_needed() run_as_root() run_shell_command([ 'am', 'broadcast', '-a', 'android.intent.action.MASTER_CLEAR', '-n', 'android/com.android.server.MasterClearReceiver' ]) # Wait until the reset is complete. time.sleep(FACTORY_RESET_WAIT)
def run_platform_init_scripts(): """Run platform specific initialization scripts.""" logs.log('Running platform initialization scripts.') plt = environment.platform() if environment.is_android_emulator(): # Nothing to do here since emulator is not started yet. pass elif environment.is_android(): android_init.run() elif plt == 'CHROMEOS': chromeos_init.run() elif plt == 'FUCHSIA': fuchsia_init.run() elif plt == 'LINUX': linux_init.run() elif plt == 'MAC': mac_init.run() elif plt == 'WINDOWS': windows_init.run() else: raise RuntimeError('Unsupported platform') logs.log('Completed running platform initialization scripts.')