diff --git a/module/device/connection.py b/module/device/connection.py index ecbf772cc..16495887a 100644 --- a/module/device/connection.py +++ b/module/device/connection.py @@ -112,7 +112,7 @@ class Connection(ConnectionAttr): self.detect_device() # Connect - self.adb_connect(self.serial) + self.adb_connect() logger.attr('AdbDevice', self.adb) # Package @@ -610,7 +610,7 @@ class Connection(ConnectionAttr): return self.adb_command(cmd) @Config.when(DEVICE_OVER_HTTP=False) - def adb_connect(self, serial): + def adb_connect(self): """ Connect to a serial, try 3 times at max. If there's an old ADB server running while Alas is using a newer one, which happens on Chinese emulators, @@ -626,7 +626,9 @@ class Connection(ConnectionAttr): for device in self.list_device(): if device.status == 'offline': logger.warning(f'Device {device.serial} is offline, disconnect it before connecting') - self.adb_disconnect(device.serial) + msg = self.adb_client.disconnect(device.serial) + if msg: + logger.info(msg) elif device.status == 'unauthorized': logger.error(f'Device {device.serial} is unauthorized, please accept ADB debugging on your device') elif device.status == 'device': @@ -635,45 +637,58 @@ class Connection(ConnectionAttr): logger.warning(f'Device {device.serial} is is having a unknown status: {device.status}') # Skip for emulator-5554 - if 'emulator-' in serial: - logger.info(f'"{serial}" is a `emulator-*` serial, skip adb connect') + if 'emulator-' in self.serial: + logger.info(f'"{self.serial}" is a `emulator-*` serial, skip adb connect') return True - if re.match(r'^[a-zA-Z0-9]+$', serial): - logger.info(f'"{serial}" seems to be a Android serial, skip adb connect') + if re.match(r'^[a-zA-Z0-9]+$', self.serial): + logger.info(f'"{self.serial}" seems to be a Android serial, skip adb connect') return True # Try to connect for _ in range(3): - msg = self.adb_client.connect(serial) + msg = self.adb_client.connect(self.serial) logger.info(msg) + # Connected to 127.0.0.1:59865 + # Already connected to 127.0.0.1:59865 if 'connected' in msg: - # Connected to 127.0.0.1:59865 - # Already connected to 127.0.0.1:59865 return True + # bad port number '598265' in '127.0.0.1:598265' elif 'bad port' in msg: - # bad port number '598265' in '127.0.0.1:598265' - logger.error(msg) possible_reasons('Serial incorrect, might be a typo') raise RequestHumanTakeover + # cannot connect to 127.0.0.1:55555: + # No connection could be made because the target machine actively refused it. (10061) elif '(10061)' in msg: - # cannot connect to 127.0.0.1:55555: - # No connection could be made because the target machine actively refused it. (10061) - logger.info(msg) + # MuMu12 may switch serial if port is occupied + # Brute force connect nearby ports to handle serial switches + if self.is_mumu12_family: + before = self.serial + for port_offset in [1, -1, 2, -2]: + port = self.port + port_offset + serial = self.serial.replace(str(self.port), str(port)) + msg = self.adb_client.connect(serial) + logger.info(msg) + if 'connected' in msg: + break + self.detect_device() + if self.serial != before: + return True + # No such device logger.warning('No such device exists, please restart the emulator or set a correct serial') raise EmulatorNotRunningError # Failed to connect - logger.warning(f'Failed to connect {serial} after 3 trial, assume connected') + logger.warning(f'Failed to connect {self.serial} after 3 trial, assume connected') self.detect_device() return False @Config.when(DEVICE_OVER_HTTP=True) - def adb_connect(self, serial): + def adb_connect(self): # No adb connect if over http return True - def adb_disconnect(self, serial): - msg = self.adb_client.disconnect(serial) + def adb_disconnect(self): + msg = self.adb_client.disconnect(self.serial) if msg: logger.info(msg) @@ -703,11 +718,11 @@ class Connection(ConnectionAttr): # Restart Adb self.adb_restart() # Connect to device - self.adb_connect(self.serial) + self.adb_connect() self.detect_device() else: - self.adb_disconnect(self.serial) - self.adb_connect(self.serial) + self.adb_disconnect() + self.adb_connect() self.detect_device() @Config.when(DEVICE_OVER_HTTP=True) @@ -982,7 +997,7 @@ class Connection(ConnectionAttr): for device in available.select(may_mumu12_family=True): if -2 <= device.port - self.port <= 2: # Port switched - logger.info(f'MuMu12 port switches from {self.serial} to {device.serial}') + logger.info(f'MuMu12 serial switched {self.serial} -> {device.serial}') del_cached_property(self, 'port') del_cached_property(self, 'is_mumu12_family') del_cached_property(self, 'is_mumu_family')