-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb2.py
472 lines (357 loc) · 13.2 KB
/
web2.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
from pyscript import document, window
from js import jQuery
from pyodide.ffi.wrappers import add_event_listener
from atrtool import *
atr_obj: ATR
_updaters = []
def updater(selectors: list[str]) -> callable:
"""
Decorator function to gather object update listeners.
"""
def add_updater(func: callable):
global _updaters
_updaters.append({"func": func, "selectors": selectors})
return func
return add_updater
def apply_updaters():
global _updaters
for info in _updaters:
selectors = info["selectors"]
func = info["func"]
for sel in selectors:
el = document.querySelector(sel)
add_event_listener(el, "change", func)
# call from all field update handlers after modifying data structure
def update_text():
global atr_obj
textbox = document.getElementById("hextext")
data = atr_obj.to_bytes()
textbox.value = data.hex()
from js import params_changed
def ui_update_all():
global atr_obj
ui_update_params()
ui_update_hist()
params_changed(None) # update baud display
ui_update_protos()
def ui_update_params():
global atr_obj
el = document.querySelector("#bitorder")
if atr_obj.TS.bitorder == TS.BitOrder.LSB_first:
el.value = "normal"
elif atr_obj.TS.bitorder == TS.BitOrder.MSB_first:
el.value = "reverse"
check_el = document.querySelector("#params_active")
if "TA1" in atr_obj.params:
check_el.checked = True
inst = atr_obj.params["TA1"]
Fi_el = document.querySelector("#params_Fi")
Di_el = document.querySelector("#params_Di")
Fi_el.value = inst.Fi
Di_el.value = inst.Di
else:
check_el.checked = False
pass # maybe reset data to default value
jQuery("#params_active").trigger("change")
check_el = document.querySelector("#egt_active")
if "TC1" in atr_obj.params:
check_el.checked = True
inst = atr_obj.params["TC1"]
data_el = document.querySelector("#egt")
data_el.value = inst.N
else:
check_el.checked = False
pass # maybe reset data to default value
jQuery("#egt_active").trigger("change")
check_el = document.querySelector("#negotiate_mode_active")
if "TA2" in atr_obj.params:
check_el.checked = True
inst: TA2 = atr_obj.params["TA2"]
nego_el = document.querySelector("#negotiate_mode")
speed_el = document.querySelector("#use_speed_param")
proto_el = document.querySelector("#preferred_proto")
if inst.specific_mode:
nego_el.value = "false"
else:
nego_el.value = "true"
if inst.use_param:
speed_el.value = "true"
else:
speed_el.value = "false"
proto_el.value = inst.T
else:
check_el.checked = False
pass # maybe reset data to default value
jQuery("#negotiate_mode_active").trigger("change")
# T=15 also defines global interface characteristics
if 15 in atr_obj.protocols:
prot: Teq15 = atr_obj.protocols[15]
check_el = document.querySelector("#clockstop_active")
if prot.clock_stop is not None:
check_el.checked = True
clkstp_el = document.querySelector("#clockstop")
clockstop = prot.clock_stop
if clockstop == Teq15.ClockStop.NOT_SUPPORTED:
clkstp_el.value = "none"
elif clockstop == Teq15.ClockStop.STATE_H:
clkstp_el.value = "high"
elif clockstop == Teq15.ClockStop.STATE_L:
clkstp_el.value = "low"
elif clockstop == Teq15.ClockStop.NO_PREFERENCE:
clkstp_el.value = "either"
card_classes = prot.card_class
a_el = document.querySelector("#class_a_enable")
b_el = document.querySelector("#class_b_enable")
c_el = document.querySelector("#class_c_enable")
a_el.checked = Teq15.CardClass.CLASS_A in card_classes
b_el.checked = Teq15.CardClass.CLASS_B in card_classes
c_el.checked = Teq15.CardClass.CLASS_C in card_classes
else:
check_el.checked = False
pass # maybe reset data to default value
jQuery("#clockstop_active").trigger("change")
# # for SPU byte
# check_el = document.querySelector("#SPU_active")
# if prot.card_class is not None:
# check_el.checked = True
# clock_el = document.querySelector("")
# else:
# check_el.checked = False
# pass # maybe reset data to default value
# jQuery("#SPU_active").trigger("change")
def ui_update_hist():
global atr_obj
data = atr_obj.hist_bytes
check_el = document.querySelector("#hist_active")
data_el = document.querySelector("#histtext")
if data:
check_el.checked = True
data_el.value = data.hex()
else:
check_el.checked = False
data_el.value = ""
jQuery("#hist_active").trigger("change")
def ui_update_protos():
global atr_obj
things = [
{"num": 0, "func": ui_update_proto_Teq0},
{"num": 1, "func": ui_update_proto_Teq1},
]
for info in things:
num = info["num"]
func = info["func"]
check_sel = f"#Teq{num}_check"
check_el = document.querySelector(check_sel)
if num in atr_obj.protocols:
check_el.checked = True
func()
else:
check_el.checked = False
jQuery(check_sel).trigger("change")
def ui_update_proto_Teq0():
global atr_obj
prot: Teq0 = atr_obj.protocols[0]
check_el = document.querySelector("#Teq0_wti_active")
wti_el = document.querySelector("#Teq0_wti")
if prot.waiting_time is not None:
check_el.checked = True
wti_el.value = prot.waiting_time
else:
check_el.checked = False
# reset data to default value
wti_el.value = 10
jQuery("#Teq0_wti_active").trigger("change")
def ui_update_proto_Teq1():
global atr_obj
prot: Teq1 = atr_obj.protocols[1]
check_el = document.querySelector("#Teq1_ifsc_active")
ifsc_el = document.querySelector("#Teq1_ifsc")
if prot.ifs_max_size is not None:
check_el.checked = True
ifsc_el.value = prot.ifs_max_size
else:
check_el.checked = False
# reset data to default value
ifsc_el.value = 32
jQuery("#Teq1_ifsc_active").trigger("change")
check_el = document.querySelector("#Teq1_cwi_active")
cwi_el = document.querySelector("#Teq1_cwi")
bwi_el = document.querySelector("#Teq1_bwi")
if prot.character_wait_time is not None:
check_el.checked = True
cwi_el.value = prot.character_wait_time
bwi_el.value = prot.block_wait_time
else:
check_el.checked = False
# reset data to default value
cwi_el.value = 13
bwi_el.value = 4
jQuery("#Teq1_cwi_active").trigger("change")
check_el = document.querySelector("#Teq1_redundancy_active")
redundancy_el = document.querySelector("#Teq1_redundancy")
if prot.ifs_max_size is not None:
check_el.checked = True
redundancy = prot.redundancy
if redundancy == Teq1.RedundancyCode.LRC:
redundancy_el.value = "lrc"
elif redundancy == Teq1.RedundancyCode.CRC:
redundancy_el.value = "crc"
else:
check_el.checked = False
# reset data to default value
redundancy_el.value = "lrc"
jQuery("#Teq1_redundancy_active").trigger("change")
@updater(["#bitorder"])
def ui_rev_update_bitorder(event):
global atr_obj
el = document.querySelector("#bitorder")
value = el.value
if value == "normal":
atr_obj.TS.bitorder = TS.BitOrder.LSB_first
elif value == "reverse":
atr_obj.TS.bitorder = TS.BitOrder.MSB_first
update_text()
@updater(["#params_active", "#params_Fi", "#params_Di"])
def ui_rev_update_params(event):
global atr_obj
check_el = document.querySelector("#params_active")
active = check_el.checked
if active:
Fi_el = document.querySelector("#params_Fi")
Di_el = document.querySelector("#params_Di")
Fi = int(Fi_el.value)
Di = int(Di_el.value)
obj = TA1()
obj.Fi = Fi
obj.Di = Di
atr_obj.params["TA1"] = obj
else:
atr_obj.params.pop("TA1")
update_text()
@updater(["#egt_active", "#egt"])
def ui_rev_update_params(event):
global atr_obj
check_el = document.querySelector("#egt_active")
active = check_el.checked
if active:
el = document.querySelector("#egt")
val = int(el.value)
obj = TC1()
obj.N = val
# atr_obj.params.update({"TC1": obj})
atr_obj.params["TC1"] = obj
else:
atr_obj.params.pop("TC1")
update_text()
@updater(["#negotiate_mode_active", "#negotiate_mode", "#use_speed_param", "#preferred_proto"])
def ui_rev_update_negotiate(event):
global atr_obj
check_el = document.querySelector("#negotiate_mode_active")
active = check_el.checked
if active:
nego_el = document.querySelector("#negotiate_mode")
speed_el = document.querySelector("#use_speed_param")
proto_el = document.querySelector("#preferred_proto")
nego_val = bool(nego_el.value)
speed_val = bool(speed_el.value)
proto_val = int(proto_el.value or '0')
obj = TA2()
obj.specific_mode = not nego_val
obj.use_param = speed_val
obj.T = proto_val
atr_obj.params["TA2"] = obj
else:
atr_obj.params.pop("TA2")
update_text()
@updater(["#clockstop_active", "#clockstop", "#class_a_enable", "#class_b_enable", "#class_c_enable"])
def ui_rev_update_Teq15(event):
global atr_obj
check_el = document.querySelector("#clockstop_active")
active = check_el.checked
if active:
clkstp_el = document.querySelector("#clockstop")
if clkstp_el.value == "none":
clockstop = Teq15.ClockStop.NOT_SUPPORTED
elif clkstp_el.value == "high":
clockstop = Teq15.ClockStop.STATE_H
elif clkstp_el.value == "low":
clockstop = Teq15.ClockStop.STATE_L
elif clkstp_el.value == "either":
clockstop = Teq15.ClockStop.NO_PREFERENCE
else:
raise ValueError
a_el = document.querySelector("#class_a_enable")
b_el = document.querySelector("#class_b_enable")
c_el = document.querySelector("#class_c_enable")
card_classes = Teq15.CardClass(0)
if a_el.checked:
card_classes |= Teq15.CardClass.CLASS_A
if b_el.checked:
card_classes |= Teq15.CardClass.CLASS_B
if c_el.checked:
card_classes |= Teq15.CardClass.CLASS_C
obj = Teq15()
obj.clock_stop = clockstop
obj.card_class = card_classes
atr_obj.protocols[15] = obj
else:
atr_obj.protocols.pop(15)
update_text()
@updater(["#hist_active", "#histtext"])
def ui_rev_update_hist(event):
global atr_obj
check_el = document.querySelector("#hist_active")
active = check_el.checked
if active:
data_el = document.querySelector("#histtext")
data = bytes.fromhex(data_el.value)
atr_obj.hist_bytes = data
else:
atr_obj.hist_bytes = b''
update_text()
@updater(["#Teq0_check", "#Teq0_wti_active", "#Teq0_wti"])
def ui_rev_update_Teq0(event):
global atr_obj
check_el = document.querySelector("#Teq0_check")
active = check_el.checked
if active:
obj = Teq0()
check_el = document.querySelector("#Teq0_wti_active")
if check_el.checked:
wti_el = document.querySelector("#Teq0_wti")
obj.waiting_time = int(wti_el.value)
atr_obj.protocols[0] = obj
else:
atr_obj.protocols.pop(0)
update_text()
@updater(["#Teq1_check", "#Teq1_ifsc_active", "#Teq1_ifsc", "#Teq1_cwi_active", "#Teq1_cwi", "#Teq1_bwi_active", "#Teq1_bwi", "#Teq1_redundancy_active", "#Teq1_redundancy"])
def ui_rev_update_Teq1(event):
global atr_obj
check_el = document.querySelector("#Teq1_check")
active = check_el.checked
if active:
obj = Teq1()
check_el = document.querySelector("#Teq1_ifsc_active")
if check_el.checked:
ifsc_el = document.querySelector("#Teq1_ifsc")
obj.ifs_max_size = int(ifsc_el.value)
check_el = document.querySelector("#Teq1_cwi_active")
if check_el.checked:
cwi_el = document.querySelector("#Teq1_cwi")
bwi_el = document.querySelector("#Teq1_bwi")
obj.character_wait_time = int(cwi_el.value)
obj.block_wait_time = int(bwi_el.value)
check_el = document.querySelector("#Teq1_redundancy_active")
if check_el.checked:
redundancy_el = document.querySelector("#Teq1_redundancy")
if redundancy_el.value == "lrc":
redundancy = Teq1.RedundancyCode.LRC
elif redundancy_el.value == "crc":
redundancy = Teq1.RedundancyCode.CRC
else:
raise ValueError
obj.redundancy = redundancy
atr_obj.protocols[1] = obj
else:
atr_obj.protocols.pop(1)
update_text()