class HotplugManager(threading.Thread): _instance = None def __init__(self, lock, config_path): print config_path self.conf = ConfigObj(config_path) self.adb = ADB(self.conf['mainConfiguration'] ['adbConfiguration']['adbPath']) self.adb.restart_server() self.lock = lock self.storage = ZODB.DB(None) self.connection = self.storage.open() self.root = self.connection.root self.root.devices = BTrees.OOBTree.BTree() def __new__(cls, *args, **kwargs): if not cls._instance: cls._instance = super(HotplugManager, cls).__new__(cls, *args, **kwargs) cls._ins = super(HotplugManager, cls).__new__(cls, *args, **kwargs) return cls._instance @retry(libusb1.LIBUSB_ERROR_ACCESS, tries=2) def _usb_connection(self, device, event): """ TODO: Manage missing udev rules """ key = "%04x:%04x" % (device.getVendorID(), device.getProductID()) if event == 2: self.lock.acquire() try: # Delete key object from ZODB del self.root.devices[key] transaction.get().commit() except Exception as e: print str(e.message) transaction.abort() pass self.lock.release() elif event == 1 and device.getSerialNumber() is not None: self.adb.restart_server() devices = list(self.adb.get_devices())[1:][0] try: if device.getSerialNumber() in devices: self.adb.set_target_device(device.getSerialNumber()) version = self.adb.shell_command('getprop ro.build.version.release').rstrip() model = ''.join((self.adb.shell_command('settings get secure android_id')).splitlines()) self.lock.acquire() try: dm = DeviceManager(model, version) self.root.devices[str(key)] = dm transaction.commit() except (Exception, AttributeError) as e: print str(e.message) transaction.abort() pass self.lock.release() except (IndexError, AttributeError, TypeError, KeyError) as e: print str(e.message) pass self.adb.kill_server() def callback(self, context, device, event): try: print "Device %s: %s" % ( { libusb1.LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED: 'arrived', libusb1.LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT: 'left', }[event], device ) self._usb_connection(device, event) except (AttributeError, libusb1.USBError) as e: print str(e.message) pass
# set target device try: adb.set_target_device(devices[dev]) except Exception, e: print "\n[!] Error:\t- ADB: %s\t - Python: %s" % (adb.get_error(), e.args) adb.kill_server() exit(-5) print "[+] Using \"%s\" as target device " % (devices[dev]) # connect device with client by TCP/IP # set listening port adbPort = input("Enter an port number except for 5000: ") if adbPort == 5000: adb.kill_server() print "Port error" exit(-1) # listen to the port adb.listen_tcp(adbPort) # set the ip address for listening ip_addr = input("Enter device ip address: ") adb.connect_remote(ip_addr, adbPort) print "[+] Using \"%s\" as target device " % (devices[dev]) rain = input("Please disconnect usb : ") if rain in ("No" or "N" or "Nope"): adb.kill_server() exit(0)
dev = 0 # set target device try: adb.set_target_device(devices[dev]) except Exception, e: print "\n[!] Error:\t- ADB: %s\t - Python: %s" % (adb.get_error(),e.args) adb.kill_server() exit(-5) print "[+] Using \"%s\" as target device " % (devices[dev]) # connect device with client by TCP/IP # set listening port adbPort = input("Enter an port number except for 5000: ") if adbPort == 5000: adb.kill_server() print "Port error" exit(-1) # listen to the port adb.listen_tcp(adbPort) # set the ip address for listening ip_addr = input("Enter device ip address: ") adb.connect_remote(ip_addr, adbPort) print "[+] Using \"%s\" as target device " % (devices[dev]) rain = input("Please disconnect usb : ") if rain in ("No" or "N" or "Nope"): adb.kill_server() exit(0)