-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdevice.py
119 lines (107 loc) · 3.93 KB
/
device.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from application import application
import yaml
import subprocess
import time
import adbutils
class device:
name = ''
ip = ''
port = str(5555)
enabled = True
timeout = 3600
applications = []
arch = 'arm64-v8a'
dpi = 'nodpi'
api_level = 0
auth_port = ''
auth_code = ''
adb_server_host = '127.0.0.1'
adb_server_port = 5037
def __init__(self, vars):
self.name = vars['name']
self.ip = vars['ip']
if('enabled' in vars):
self.enabled = vars['enabled']
if('port' in vars):
self.port = str(vars['port'])
if('arch' in vars):
self.arch = vars['arch']
if('dpi' in vars):
self.dpi = vars['dpi']
if('api_level' in vars):
self.api_level = vars['api_level']
if('auth_code' in vars):
self.auth_code = str(vars['auth_code'])
if('auth_port' in vars):
self.auth_port = str(vars['auth_port'])
self.applications = []
for app in vars['applications'] :
self.applications.append(application(app))
def load_devices():
# Get configs
try:
with open('config/config.yml', 'r') as file:
config = yaml.safe_load(file)
# set from config
devices = config['devices']
# get all devices
all_devices = [];
for dev in devices:
dev = device(dev)
dev.timeout = config['timeout']
all_devices.append(dev)
return all_devices
except Exception:
print('Failed loading devices or no device found. Please check your config.yml')
return 0
def connect_adb(self):
i=0
while i < 5:
state = self.try_adb_connection()
if state == 1:
return 1
else:
if i < 4:
time.sleep(2)
i=i+1
else:
print('Connection failed. Please check your device for a popup. Android versions > 11 should reboot the server with auth_port & auth_code')
return 0
def try_adb_connection(self):
subprocess.run(["adb/linux/adb", "start-server"], stdout=subprocess.PIPE, text=True)
adb = adbutils.AdbClient(host=self.adb_server_host, port=self.adb_server_port)
output = adb.connect(self.ip + ':' + self.port, timeout=30.0)
if output.find('failed to connect') == 0:
if self.auth_port != '' and self.auth_code != '':
print('Performing first pair with port ' + self.auth_port + ' and code ' + self.auth_code)
self.adb_pair(self.auth_port,self.auth_code);
output = adb.connect(self.ip + ':' + self.port, timeout=30.0)
if self.adb_status() == 1:
print('Connection succesfull')
return 1
else:
return 0
else:
if self.adb_status() == 1:
print('Connection succesfull')
return 1
else:
return 0
def adb_pair(self, port, code):
# For android 11+ do new pairing method
adb = subprocess.run(["adb/linux/adb", "pair", self.ip + ':' + port], stdout=subprocess.PIPE, text=True, input=code)
if adb.stdout.find('Successfully paired') != -1:
print('Pairing..')
time.sleep(5)
print('Successfully paired to ' + self.name)
return 1
else:
print('Device not found.. ')
return 0
def adb_status(self):
adb = adbutils.AdbClient(host=self.adb_server_host, port=self.adb_server_port)
devices = adb.device_list()
for adbdevice in devices:
if adbdevice.serial.find(self.ip) == 0:
return 1
return 0