1#!/usr/bin/python 2 3# Copyright 2017 The Chromium OS Authors. All rights reserved. 4# Use of this source code is governed by a BSD-style license that can be 5# found in the LICENSE file. 6 7import argparse 8import logging 9import pipes 10import time 11 12import common 13from autotest_lib.client.bin import utils 14 15_ADB_POLLING_INTERVAL_SECONDS = 10 16_ADB_CONNECT_INTERVAL_SECONDS = 1 17 18 19def _is_adb_connected(): 20 """Return true if adb is connected to the container.""" 21 output = utils.system_output('adb get-state', ignore_status=True) 22 logging.debug('adb get-state: %s', output) 23 return output.strip() == 'device' 24 25 26def _ensure_adb_connected(target): 27 """Ensures adb is connected to the container, reconnects otherwise.""" 28 while not _is_adb_connected(): 29 logging.info('adb not connected. attempting to reconnect') 30 output = utils.system_output('adb connect %s' % pipes.quote(target), 31 ignore_status=True) 32 logging.debug('adb connect %s: %s', target, output) 33 time.sleep(_ADB_CONNECT_INTERVAL_SECONDS) 34 35 36if __name__ == '__main__': 37 logging.basicConfig(level=logging.DEBUG) 38 parser = argparse.ArgumentParser(description='ensure adb is connected') 39 parser.add_argument('target', help='Device to connect to') 40 args = parser.parse_args() 41 42 logging.info('Starting adb_keepalive for target %s', args.target) 43 44 while True: 45 try: 46 time.sleep(_ADB_POLLING_INTERVAL_SECONDS) 47 _ensure_adb_connected(args.target) 48 except KeyboardInterrupt: 49 logging.info('Shutting down') 50 break 51