1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-23 07:58:09 +00:00

Slightly refactored mixin structure, added debugging output

This commit is contained in:
slush0 2014-02-13 17:20:40 +01:00
parent 9b8462f71e
commit a117057dea
2 changed files with 24 additions and 10 deletions

View File

@ -32,5 +32,4 @@ class TrezorTest(unittest.TestCase):
print "--------------" print "--------------"
def tearDown(self): def tearDown(self):
self.debug_transport.close() self.client.close()
self.transport.close()

View File

@ -14,6 +14,10 @@ def get_buttonrequest_value(code):
# Converts integer code to its string representation of ButtonRequestType # Converts integer code to its string representation of ButtonRequestType
return [ k for k, v in types.ButtonRequestType.items() if v == code][0] return [ k for k, v in types.ButtonRequestType.items() if v == code][0]
def pprint(msg):
ser = msg.SerializeToString()
return "<%s> (%d bytes):\n%s" % (msg.__class__.__name__, len(ser), msg)
class CallException(Exception): class CallException(Exception):
def __init__(self, code, message): def __init__(self, code, message):
super(CallException, self).__init__() super(CallException, self).__init__()
@ -54,9 +58,9 @@ class expect(object):
class BaseClient(object): class BaseClient(object):
# Implements very basic layer of sending raw protobuf # Implements very basic layer of sending raw protobuf
# messages to device and getting its response back. # messages to device and getting its response back.
def __init__(self, transport, *args, **kwargs): def __init__(self, transport, **kwargs):
self.transport = transport self.transport = transport
super(BaseClient, self).__init__(*args, **kwargs) super(BaseClient, self).__init__() # *args, **kwargs)
def call(self, msg): def call(self, msg):
try: try:
@ -97,6 +101,9 @@ class TextUIMixin(object):
# by implementing your own GuiMixin with # by implementing your own GuiMixin with
# graphical widgets for every type of these callbacks. # graphical widgets for every type of these callbacks.
def __init__(self, *args, **kwargs):
super(TextUIMixin, self).__init__(*args, **kwargs)
def callback_ButtonRequest(self, msg): def callback_ButtonRequest(self, msg):
print "Sending ButtonAck for %s " % get_buttonrequest_value(msg.code) print "Sending ButtonAck for %s " % get_buttonrequest_value(msg.code)
return proto.ButtonAck() return proto.ButtonAck()
@ -148,6 +155,12 @@ class DebugLinkMixin(object):
def setup_debuglink(self, button, pin_correct): def setup_debuglink(self, button, pin_correct):
self.button = button # True -> YES button, False -> NO button self.button = button # True -> YES button, False -> NO button
self.pin_correct = pin_correct self.pin_correct = pin_correct
def call(self, msg):
print "SENDING", pprint(msg)
ret = super(DebugLinkMixin, self).call(msg)
print "RECEIVED", pprint(ret)
return ret
def callback_ButtonRequest(self, msg): def callback_ButtonRequest(self, msg):
if self.expected_buttonrequests != None: if self.expected_buttonrequests != None:
@ -177,16 +190,16 @@ class DebugLinkMixin(object):
return proto.PinMatrixAck(pin=pin) return proto.PinMatrixAck(pin=pin)
def callback_PassphraseRequest(self, msg): def callback_PassphraseRequest(self, msg):
pass raise Exception("Not implemented yet")
def callback_WordRequest(self, msg): def callback_WordRequest(self, msg):
pass raise Exception("Not implemented yet")
class ProtocolMixin(object): class ProtocolMixin(object):
PRIME_DERIVATION_FLAG = 0x80000000 PRIME_DERIVATION_FLAG = 0x80000000
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(ProtocolMixin, self).__init__() # *args, **kwargs) super(ProtocolMixin, self).__init__(*args, **kwargs)
self.init_device() self.init_device()
def get_tx_func_placeholder(txhash): def get_tx_func_placeholder(txhash):
@ -453,10 +466,10 @@ class ProtocolMixin(object):
raise Exception("Unexpected result " % resp) raise Exception("Unexpected result " % resp)
class TrezorClient(BaseClient, ProtocolMixin, TextUIMixin): class TrezorClient(ProtocolMixin, TextUIMixin, BaseClient):
pass pass
class TrezorDebugClient(BaseClient, ProtocolMixin, DebugLinkMixin): class TrezorDebugClient(ProtocolMixin, DebugLinkMixin, BaseClient):
pass pass
''' '''
@ -478,7 +491,9 @@ class TrezorClient(object):
resp = self.transport.read_blocking() resp = self.transport.read_blocking()
if isinstance(resp, proto.ButtonRequest): if isinstance(resp, proto.ButtonRequest):
if expected_buttonrequests != None: _pprint(self, msg):
ser = msg.SerializeToString()
if expected_buttonrequests != None:
try: try:
exp = expected_buttonrequests.pop(0) exp = expected_buttonrequests.pop(0)
if resp.code != exp: if resp.code != exp: