forked from virtualabs/ndh2k14-badge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole.py
More file actions
executable file
·92 lines (76 loc) · 2.53 KB
/
console.py
File metadata and controls
executable file
·92 lines (76 loc) · 2.53 KB
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
#!/usr/bin/python
import sys
import usb.core
from time import sleep
from threading import Thread,Lock
bmRequestTypeIn = usb.util.build_request_type(usb.util.CTRL_IN, usb.util.CTRL_TYPE_VENDOR, usb.util.CTRL_RECIPIENT_INTERFACE)
bmRequestTypeOut = usb.util.build_request_type(usb.util.CTRL_OUT, usb.util.CTRL_TYPE_VENDOR, usb.util.CTRL_RECIPIENT_INTERFACE)
class Badge:
def __init__(self, device):
self.dev = device
self.lock = Lock()
def send(self, data):
if len(data) >= 31:
data = data[:30]
self.lock.acquire()
self.dev.ctrl_transfer(bmRequestTypeOut, 0x61, 0, 0, data)
self.lock.release()
def recv(self):
self.lock.acquire()
ret = self.dev.ctrl_transfer(bmRequestTypeIn, 0x60, 0, 0, 16)
self.lock.release()
raw = ''.join(chr(x) for x in ret)
if len(raw) > 0:
return raw
else:
return None
class OutputMonitor(Thread):
def __init__(self, badge, manager):
Thread.__init__(self)
self.badge = badge
self.manager = manager
self.cancel = False
def run(self):
while not self.cancel:
try:
data = self.badge.recv()
if data is not None:
sys.stdout.write(data)
sys.stdout.flush()
sleep(0.01)
except Exception as e:
print("You got an exception: %s" % e)
self.manager.exit()
class InputMonitor(Thread):
def __init__(self, badge, manager):
Thread.__init__(self)
self.badge = badge
self.manager = manager
self.cancel = False
def run(self):
while not self.cancel:
line = sys.stdin.readline()
if line == 'exit\n':
self.manager.exit()
break
if len(line) > 0:
self.badge.send(line)
class Console:
def run(self):
# find device
print('*** Ndh2k14 serial console ***')
print(' - waiting for device ...')
device = None
while device is None:
device = usb.core.find(idProduct=0x05dc, idVendor=0x16c0)
print(' - device detected, spawning console')
self.badge = Badge(device)
self.outputmon = OutputMonitor(self.badge, self)
self.inputmon = InputMonitor(self.badge, self)
self.outputmon.start()
self.inputmon.start()
def exit(self):
self.inputmon.cancel = True
self.outputmon.cancel = True
c = Console()
c.run()