forked from LordAmit/mobile-monkey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
telnet_connector.py
145 lines (118 loc) · 3.48 KB
/
telnet_connector.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
'''
telnet to adb connection and management module
'''
import telnetlib
from enum import Enum
import time
import config_reader as config
import util
PRINT_FLAG = True
class NetworkStatus(Enum):
'''
Network status possible values
'''
gsm = 0
hscsd = 1
gprs = 2
umts = 3
edge = 4
hsdpa = 5
lte = 6
evdo = 7
full = 8
class NetworkDelay(Enum):
'''
Network delay constants
'''
gsm = 0
edge = 1
umts = 2
none = 3
class GsmProfile(Enum):
'''
Gsm profile constants
'''
STRENGTH0 = 0
STRENGTH1 = 1
STRENGTH2 = 2
STRENGTH3 = 3
STRENGTH4 = 4
class TelnetAdb:
'''
Telnet to Android connector API
'''
GSM_BREAK = 10
telnet = None
def __init__(self, host: str='localhost',
port: int=int(config.EMULATOR_PORT))-> None:
'''initiates the telnet to adb connection at provided `host`:`port`'''
self.__establish_connection(host, port)
def __establish_connection(self, host: str, port: int):
try:
self.telnet = telnetlib.Telnet(host, int(port))
except ConnectionRefusedError:
raise ConnectionRefusedError("Connection refused")
self.__telnet_read_until()
self.__telnet_write_command('auth ' + config.TELNET_KEY)
def __telnet_write_command(self, command: str):
util.debug_print(command, flag=PRINT_FLAG)
telnet_command = bytes("{}\n".format(command), "ascii")
self.telnet.write(telnet_command)
def __telnet_read_until(self):
print(self.telnet.read_until(b'OK'))
def set_connection_speed(self, speed: NetworkStatus):
'''
sets connection speed, where `speed` is `NetworkStatus` type
'''
self.__telnet_write_command(
'network speed ' + str(speed.name))
def set_connection_delay(self, delay: NetworkDelay):
'''
sets connection delay, where `delay` is `NetworkDelay` type
'''
self.__telnet_write_command(
'network delay ' + delay.name)
def set_gsm_profile(self, profile: GsmProfile):
'''
sets GSM profile, where `profile` is `GsmProfile` type.
takes around 15 seconds for android OS to detect it
'''
self.__telnet_write_command('gsm signal-profile ' + str(profile.value))
time.sleep(self.GSM_BREAK)
def kill_avd(self):
'''
self destructs
'''
self.__telnet_write_command('kill')
def reset_gsm_profile(self):
'''
resets GSM profile to signal strength 4
'''
self.__telnet_write_command('gsm signal-profile 4')
time.sleep(self.GSM_BREAK)
def reset_network_delay(self):
'''
resets network delay to 0
'''
self.__telnet_write_command('network delay 0')
def reset_network_speed(self):
'''
resets network speed to full
'''
self.__telnet_write_command('network speed full')
def reset_all(self):
'''
resets by calling
>>> self.reset_network_delay()
>>> self.reset_network_speed()
>>> self.reset_gsm_profile()
'''
print("setting network delay to 0")
self.reset_network_delay()
print("setting network speed to full")
self.reset_network_speed()
print("resetting GSM profile")
self.reset_gsm_profile()
if __name__ == '__main__':
TNC = TelnetAdb(config.LOCALHOST, int(config.EMULATOR_PORT))
TNC.kill_avd()