2013-09-01 00:46:53 +00:00
|
|
|
import trezor_pb2 as proto
|
2013-01-05 14:42:49 +00:00
|
|
|
from transport import NotImplementedException
|
2012-12-13 19:05:04 +00:00
|
|
|
|
2012-12-03 15:36:03 +00:00
|
|
|
def pin_info(pin):
|
2013-09-01 00:46:53 +00:00
|
|
|
print "Device asks for PIN %s" % pin
|
2012-12-03 15:36:03 +00:00
|
|
|
|
|
|
|
def button_press(yes_no):
|
|
|
|
print "User pressed", '"y"' if yes_no else '"n"'
|
|
|
|
|
|
|
|
class DebugLink(object):
|
2013-09-01 00:46:53 +00:00
|
|
|
def __init__(self, transport, pin_func=pin_info, button_func=button_press):
|
2012-12-03 15:36:03 +00:00
|
|
|
self.transport = transport
|
2012-12-13 19:05:04 +00:00
|
|
|
|
2012-12-03 15:36:03 +00:00
|
|
|
self.pin_func = pin_func
|
|
|
|
self.button_func = button_func
|
|
|
|
|
|
|
|
def read_pin(self):
|
2013-09-01 00:46:53 +00:00
|
|
|
self.transport.write(proto.DebugLinkGetState(pin=True, matrix=True))
|
|
|
|
obj = self.transport.read_blocking()
|
2012-12-13 19:05:04 +00:00
|
|
|
print "Read PIN:", obj.pin
|
2013-09-01 00:46:53 +00:00
|
|
|
print "Read matrix:", obj.matrix
|
|
|
|
|
2013-09-01 01:34:36 +00:00
|
|
|
return (obj.pin, obj.matrix)
|
|
|
|
|
|
|
|
def read_pin_encoded(self):
|
|
|
|
pin, matrix = self.read_pin()
|
|
|
|
|
2013-09-01 00:46:53 +00:00
|
|
|
# Now we have real PIN and PIN matrix.
|
|
|
|
# We have to encode that into encoded pin,
|
|
|
|
# because application must send back positions
|
|
|
|
# on keypad, not a real PIN.
|
2013-09-01 01:34:36 +00:00
|
|
|
pin_encoded = ''.join([ str(matrix.index(p) + 1) for p in pin])
|
2013-09-01 00:46:53 +00:00
|
|
|
|
|
|
|
print "Encoded PIN:", pin_encoded
|
|
|
|
self.pin_func(pin_encoded)
|
|
|
|
|
|
|
|
return pin_encoded
|
2012-12-03 15:36:03 +00:00
|
|
|
|
|
|
|
def press_button(self, yes_no):
|
2012-12-13 19:05:04 +00:00
|
|
|
print "Pressing", yes_no
|
2012-12-03 15:36:03 +00:00
|
|
|
self.button_func(yes_no)
|
|
|
|
self.transport.write(proto.DebugLinkDecision(yes_no=yes_no))
|
2012-12-13 19:05:04 +00:00
|
|
|
|
2012-12-03 15:36:03 +00:00
|
|
|
def press_yes(self):
|
|
|
|
self.press_button(True)
|
|
|
|
|
|
|
|
def press_no(self):
|
2013-10-11 01:51:45 +00:00
|
|
|
self.press_button(False)
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self.transport.write(proto.DebugLinkStop())
|