forked from mudita/QAMuditaHarness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
306 lines (282 loc) · 6.94 KB
/
utils.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
# For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
import time
from .interface.CDCSerial import Keytype
from .interface.defs import key_codes
# assuming that the harness is actually in the menu
application_keypath = {
"calllog": [
"left",
"enter"
],
"contacts": [
"enter"
],
"messages": [
"right",
"enter"
],
"music": [
"down",
"left",
"enter"
],
"meditation": [
"down",
"enter"
],
"settings": [
"down",
"right",
"enter"
],
"tools": [
"up",
"left",
"enter"
],
"alarm": [
"up",
"enter"
],
"calendar": [
"up",
"right",
"enter"
],
"ICE call": [
"fnLeft",
"fnLeft",
"enter",
"fnLeft"
]
}
keymap = {
"A": "2",
"B": "22",
"C": "222",
"D": "3",
"E": "33",
"F": "333",
"G": "4",
"H": "44",
"I": "444",
"J": "5",
"K": "55",
"L": "555",
"M": "6",
"N": "66",
"O": "666",
"P": "7",
"Q": "77",
"R": "777",
"S": "7777",
"T": "8",
"U": "88",
"V": "888",
"W": "9",
"X": "99",
"Y": "999",
"Z": "9999",
" ": "0",
".": "1",
",": "11",
"_": "111",
":": "1111",
";": "11111",
")": "111111",
"(": "1111111",
"?": "11111111",
"!": "111111111",
"#": "1111111111",
"/": "11111111111",
"*": "111111111111",
"+": "1111111111111",
"del": key_codes["#"],
"caps": key_codes["*"],
}
special_chars_keymap = {
U'.': "",
U',': "d",
U'\'': "dd",
U'?': "ddd",
U'!': "dddd",
U'"': "ddddd",
U'-': "dddddd",
U'(': "s",
U')': "sd",
U'@': "sdd",
U'/': "sddd",
U':': "sdddd",
U'_': "sddddd",
U';': "sdddddd",
U'': "ss",
U'+': "ssd",
U'&': "ssdd",
U'%': "ssddd",
U'*': "ssdddd",
U'<': "ssddddd",
U'>': "ssdddddd",
U'=': "sss",
U'£': "sssd",
U'€': "sssdd",
U'$': "sssddd",
U'[': "sssdddd",
U']': "sssddddd",
U'{': "sssdddddd",
U'}': "ssss",
U'\'': "ssssd",
U'^': "ssssdd",
U'~': "ssssddd",
U'`': "ssssdddd",
U'į': "ssssddddd",
U'§': "ssssdddddd",
U'…': "sssss",
U'#': "sssssd",
U'|': "sssssdd",
U'÷': "sssssddd",
U'·': "sssssdddd",
U'°': "sssssddddd",
U'¿': "sssssdddddd",
U'¡': "ssssss",
U'ą': "ssssssd",
U'à': "ssssssdd",
U'á': "ssssssddd",
U'ä': "ssssssdddd",
U'â': "ssssssddddd",
U'ć': "ssssssdddddd",
U'ç': "sssssss",
U'ę': "sssssssd",
U'é': "sssssssdd",
U'è': "sssssssddd",
U'ê': "sssssssdddd",
U'ë': "sssssssddddd",
U'î': "sssssssdddddd",
U'ï': "ssssssss",
U'í': "ssssssssd",
U'ł': "ssssssssdd",
U'ń': "ssssssssddd",
U'ñ': "ssssssssdddd",
U'ó': "ssssssssddddd",
U'ô': "ssssssssdddddd",
U'ö': "sssssssss",
U'ś': "sssssssssd",
U'û': "sssssssssdd",
U'ú': "sssssssssddd",
U'ù': "sssssssssdddd",
U'ü': "sssssssssddddd",
U'ÿ': "sssssssssdddddd",
U'ż': "ssssssssss",
U'ź': "ssssssssssd",
U'ß': "ssssssssssdd"
}
emojis_keymap = {
U'😁': "",
U'😂': "d",
U'😃': "dd",
U'😄': "ddd",
U'😅': "dddd",
U'😆': "ddddd",
U'😉': "dddddd",
U'😊': "s",
U'😋': "sd",
U'😌': "sdd",
U'😍': "sddd",
U'😏': "sdddd",
U'😒': "sddddd",
U'😓': "sdddddd",
U'😔': "ss",
U'😖': "ssd",
U'😘': "ssdd",
U'😚': "ssddd",
U'😜': "ssdddd",
U'😝': "ssddddd",
U'😼': "ssdddddd"
}
def send_keystoke(keypath, connection):
for key in keypath:
connection.send_key_code(key_codes[key])
time.sleep(0.3)
last_char = '\0'
def clear_last_char():
global last_char
last_char = '\0'
def send_char(char: str, connection):
global last_char
key_type = Keytype.short_press
if char.isascii():
if char.isdigit():
key_type = Keytype.long_press
if last_char is char:
print("repeated key!")
connection.send_key_code(key_codes["right"])
connection.send_key_code(int(char), key_type)
connection.send_key_code(key_codes["right"])
last_char = char
elif char.islower():
tmp = char.upper()
# toggle to lowercase mode
connection.send_key_code(key_codes["*"], key_type)
if last_char is keymap[tmp][0]:
print("repeated key!")
connection.send_key_code(key_codes["right"], key_type)
for key in keymap[tmp]:
connection.send_key_code(int(key), key_type)
last_char = keymap[tmp][0]
# toggle to uppercase mode
connection.send_key_code(key_codes["*"], key_type)
connection.send_key_code(key_codes["*"], key_type)
else:
if last_char is keymap[char][0]:
print("repeated key!")
connection.send_key_code(key_codes["right"], key_type)
for key in keymap[char]:
connection.send_key_code(int(key), key_type)
last_char = keymap[char][0]
else:
connection.send_key_code(key_codes["*"], Keytype.long_press)
if char in special_chars_keymap:
for key in special_chars_keymap[char]:
connection.send_key_code(ord(key), key_type)
elif char in emojis_keymap:
connection.send_key_code(key_codes["fnLeft"], Keytype.short_press)
for key in emojis_keymap[char]:
connection.send_key_code(ord(key), key_type)
else:
print("Not supported char {} !!!".format(char))
connection.send_key_code(key_codes["enter"], key_type)
def send_number(number: str, connection):
if number.isnumeric():
for digit in number:
connection.send_key_code(int(digit))
time.sleep(0.3)
def validate_pin(pin):
if len(pin) != 4:
raise RuntimeError("Pin length not equal to 4")
for digit in pin:
if not isinstance(digit, int):
raise TypeError("Pin could be only set of digits")
### timeout from https://stackoverflow.com/a/601168/5752094
from contextlib import contextmanager
import signal
class Timeout(Exception):
'''
usage:
try:
with Timeout.limit(10):
long_function_call()
except Timeout as e:
print("Timed out!")
'''
@classmethod
@contextmanager
def limit(cls, seconds: int):
assert seconds >= 1, "Timeout must be at least 1 second !"
def signal_handler(signum, frame):
raise Timeout("Timed out!")
signal.signal(signal.SIGALRM, signal_handler)
signal.alarm(seconds)
try:
yield
finally:
signal.alarm(0)