def stop(cls): if cls._process and cls.is_running(): cls._process.kill() cls._process = None logger.info('udevadm process down') else: logger.warn('udevadm process already down without killing')
def query(cls, serial_no: str) -> str: """ serial no -> device name, else 'null' """ logger.info('query device [%s]', serial_no) if serial_no in cls._container_dict: return cls._container_dict[serial_no].device_name # if not found, return str 'null' return 'null'
def inner(): UDevDetector.start() while UD4D_STATUS: event_content = UDevDetector.read_event() logger.info(','.join(event_content)) event_object = UEvent(event_content) UEventManager.add_event(event_object) UDevDetector.stop()
def remove(cls, serial_no: str): if serial_no in cls._container_dict: if cls._container_dict[serial_no].container is not None: cls._container_dict[serial_no].container.remove(force=True) logger.info('device container [%s] removed', serial_no) del cls._container_dict[serial_no] else: logger.warn('device container [%s] not existed', serial_no)
def start(cls): cls._process = subprocess.Popen( [ u_config.UDEVADM_PATH, 'monitor', '-u', '--subsystem-match=usb', '--environment' ], stdout=subprocess.PIPE, ) time.sleep(1) assert cls.is_running(), 'udevadm not running' logger.info('udevadm process up') # ignore unused header cls.read_event()
def safe_ud4d(): UDevDetector.start() logger.info('udev detector started') yield UDevDetector DeviceContainerManager.remove_all() logger.info('all device container removed') UDevDetector.stop() logger.info('udev detector stopped')
def add_event(cls, uevent: UEvent): event_id = uevent.get_event_id() action = uevent.get_action_name() # only 'add' and 'unbind' # todo: sometimes 'bind' does not appear ??? if action == 'add' and uevent.is_android(): cls._event_dict[event_id] = uevent logger.info('event id [%s] added', event_id) # build docker container container = DeviceContainer(uevent.get_serial_no(), uevent.get_dev_name()) DeviceContainerManager.add(container) elif action == 'unbind' and event_id in cls._event_dict: # remove docker container DeviceContainerManager.remove(cls._event_dict[event_id].get_serial_no()) del cls._event_dict[event_id] logger.info('event id [%s] ended', event_id) else: # ignore other events pass
def get_dev_name(self) -> str: if hasattr(self, 'DEVNAME'): return self.DEVNAME logger.info('no device name found, return device path instand') return self.DEVPATH
def add(cls, device_container: DeviceContainer): cls._container_dict[device_container.serial_no] = device_container device_container.container and logger.info('device container [%s] running', device_container.serial_no)
import docker from ud4d import config as u_config from ud4d.logger import logger # device image name device_image_name = u_config.DEVICE_IMAGE_NAME logger.info('device image name: %s', device_image_name) # docker client client = docker.from_env() # check try: device_image = client.images.get(device_image_name) except docker.errors.ImageNotFound as e: logger.error('device image not found: %s, you should build it first', device_image_name) raise e class DeviceContainer(object): """ android device container object """ def __init__(self, serial_no: str, device_name: str): self.serial_no = serial_no self.device_name = device_name self.container = None if not u_config.SIMPLE_MODE: self.container = client.containers.run( device_image,