|
| 1 | +import os |
| 2 | +import platform |
| 3 | +import subprocess as sp |
| 4 | + |
| 5 | +from custom_ci import custom_input, custom_print |
| 6 | + |
| 7 | + |
| 8 | +def init(mode, tcp_ip='', tcp_port=''): |
| 9 | + custom_print( |
| 10 | + f'>>> I am in device_serial_id.init({mode=!s}, {tcp_ip=!s}, {tcp_port=!s})', is_print=False) |
| 11 | + # Detect OS |
| 12 | + is_windows = False |
| 13 | + is_linux = False |
| 14 | + if platform.system() == 'Windows': |
| 15 | + is_windows = True |
| 16 | + if platform.system() == 'Linux': |
| 17 | + is_linux = True |
| 18 | + |
| 19 | + # Global command line helpers |
| 20 | + curr_dir = os.path.dirname(os.path.realpath(__file__)) |
| 21 | + root_dir = os.path.abspath(os.path.join(curr_dir, '..')) |
| 22 | + |
| 23 | + if(is_windows): |
| 24 | + adb = f'{root_dir}\\bin\\adb.exe' |
| 25 | + else: |
| 26 | + adb = 'adb' |
| 27 | + |
| 28 | + # Kill server before getting list to avoid daemon texts. |
| 29 | + os.system(f'{adb} kill-server') |
| 30 | + os.system(f'{adb} start-server') |
| 31 | + |
| 32 | + combo = f'{tcp_ip}:{tcp_port}' |
| 33 | + cmd = '' |
| 34 | + if mode == 'USB': |
| 35 | + cmd = f'{adb} devices' |
| 36 | + elif mode == 'TCP': |
| 37 | + cmd = f'{adb} connect {combo}' |
| 38 | + else: |
| 39 | + pass |
| 40 | + # FIXME: Wrong choice. |
| 41 | + proc = sp.Popen(cmd.split(), stdin=sp.PIPE, stdout=sp.PIPE, |
| 42 | + stderr=sp.PIPE, shell=False) |
| 43 | + output, error = proc.communicate() |
| 44 | + output = output.decode('utf-8') |
| 45 | + error = error.decode('utf-8') |
| 46 | + |
| 47 | + if len(output) == 0 or error: |
| 48 | + output = None |
| 49 | + custom_print(error, 'red') |
| 50 | + kill_me() |
| 51 | + |
| 52 | + if mode == 'USB': |
| 53 | + output = [x.strip() for x in output.split('\n') if len(x.strip()) > 0] |
| 54 | + |
| 55 | + if(len(output) == 1): |
| 56 | + custom_print( |
| 57 | + 'Could not find any connected device. Is USB Debugging on?', 'red') |
| 58 | + return '' |
| 59 | + |
| 60 | + device_to_connect = None |
| 61 | + i = 1 |
| 62 | + if(len(output) == 2): |
| 63 | + if(output[1].split()[1] == 'offline'): |
| 64 | + custom_print( |
| 65 | + 'Device is offline, try turning off USB debugging and turn on again.', 'yellow') |
| 66 | + kill_me() |
| 67 | + if(output[1].split()[1] == 'unauthorized'): |
| 68 | + custom_print( |
| 69 | + 'Device unauthorized. Please check the confirmation dialog on your device.', 'red') |
| 70 | + kill_me() |
| 71 | + return output[1].split()[0] |
| 72 | + |
| 73 | + custom_print(output[0]) |
| 74 | + custom_print('\n', is_get_time=False) |
| 75 | + if device_to_connect is None: |
| 76 | + for index, device in enumerate(output[1:]): |
| 77 | + name = f'{adb} -s {device.split()[0]} shell getprop ro.product.model' |
| 78 | + custom_print( |
| 79 | + f'{index}. {device.split()[0]} {device.split()[1]} {sp.getoutput(name).strip()}') |
| 80 | + |
| 81 | + while device_to_connect is None: |
| 82 | + device_index = int(custom_input( |
| 83 | + 'Enter device number (for ex: 2): ')) |
| 84 | + if device_index <= 0 or device_index + 1 > len(output): |
| 85 | + continue |
| 86 | + device_to_connect = output[device_index] |
| 87 | + |
| 88 | + if(device_to_connect.split()[1] == 'offline'): |
| 89 | + custom_print( |
| 90 | + 'Device is offline, try turning off USB debugging and turn on again.', 'yellow') |
| 91 | + kill_me() |
| 92 | + if(device_to_connect.split()[1] == 'unauthorized'): |
| 93 | + custom_print( |
| 94 | + 'Device unauthorized. Please check the confirmation dialog on your device.', 'red') |
| 95 | + kill_me() |
| 96 | + return device_to_connect.split()[0] |
| 97 | + |
| 98 | + elif mode == 'TCP': |
| 99 | + output = [x.strip() for x in output.split() if len(x.strip()) > 0] |
| 100 | + if('connected' in (x.lower() for x in output)): |
| 101 | + return combo |
| 102 | + if('authenticate' in (x.lower() for x in output)): |
| 103 | + custom_print( |
| 104 | + 'Device unauthorized. Please check the confirmation dialog on your device.', 'red') |
| 105 | + kill_me() |
| 106 | + if('refused' in (x.lower() for x in output)): |
| 107 | + custom_print( |
| 108 | + 'Could not find any connected device. Either USB Debugging is off or device is not running ADB over TCP', 'red') |
| 109 | + return '' |
| 110 | + ''' Possible outputs |
| 111 | + ['connected', 'to', '192.168.43.130:5555'] |
| 112 | + ['failed', 'to', 'authenticate', 'to', '192.168.43.130:5555'] |
| 113 | + ['cannot', 'connect', 'to', '192.168.43.130:5555:', 'No', 'connection', 'could', 'be', 'made', 'because', 'the', 'target', 'machine', 'actively', 'refused', 'it.', '(10061)'] |
| 114 | + ''' |
| 115 | + else: |
| 116 | + pass |
| 117 | + # FIXME: Wrong choice. |
| 118 | + |
| 119 | + |
| 120 | +def kill_me(): |
| 121 | + custom_print( |
| 122 | + '>>> I am in device_serial_id.kill_me()', is_print=False) |
| 123 | + custom_print('\n', is_get_time=False) |
| 124 | + custom_print('Exiting...') |
| 125 | + custom_print( |
| 126 | + 'Turn off USB debugging [and USB debugging (Security Settings)] if you\'re done.', 'cyan') |
| 127 | + custom_input('Hit \"Enter\" key to continue....', 'cyan') |
| 128 | + quit() |
0 commit comments