mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-11-27 09:58:27 +00:00
fix protobuf_json while handling enums
use error field from trezord
This commit is contained in:
parent
a527456db5
commit
70dd320c1f
@ -85,6 +85,8 @@ def pb2json(pb):
|
|||||||
for field,value in fields:
|
for field,value in fields:
|
||||||
if field.type == FD.TYPE_MESSAGE:
|
if field.type == FD.TYPE_MESSAGE:
|
||||||
ftype = pb2json
|
ftype = pb2json
|
||||||
|
elif field.type == FD.TYPE_ENUM:
|
||||||
|
ftype = lambda x: field.enum_type.values[x].name
|
||||||
elif field.type in _ftype2js:
|
elif field.type in _ftype2js:
|
||||||
ftype = _ftype2js[field.type]
|
ftype = _ftype2js[field.type]
|
||||||
else:
|
else:
|
||||||
@ -109,10 +111,10 @@ _ftype2js = {
|
|||||||
FD.TYPE_FIXED32: float,
|
FD.TYPE_FIXED32: float,
|
||||||
FD.TYPE_BOOL: bool,
|
FD.TYPE_BOOL: bool,
|
||||||
FD.TYPE_STRING: unicode,
|
FD.TYPE_STRING: unicode,
|
||||||
#FD.TYPE_MESSAGE: pb2json, #handled specially
|
#FD.TYPE_MESSAGE handled specially
|
||||||
FD.TYPE_BYTES: lambda x: binascii.hexlify(x),
|
FD.TYPE_BYTES: lambda x: binascii.hexlify(x),
|
||||||
FD.TYPE_UINT32: int,
|
FD.TYPE_UINT32: int,
|
||||||
FD.TYPE_ENUM: str,
|
# FD.TYPE_ENUM: handled specially
|
||||||
FD.TYPE_SFIXED32: float,
|
FD.TYPE_SFIXED32: float,
|
||||||
FD.TYPE_SFIXED64: float,
|
FD.TYPE_SFIXED64: float,
|
||||||
FD.TYPE_SINT32: int,
|
FD.TYPE_SINT32: int,
|
||||||
@ -129,7 +131,7 @@ _js2ftype = {
|
|||||||
FD.TYPE_FIXED32: float,
|
FD.TYPE_FIXED32: float,
|
||||||
FD.TYPE_BOOL: bool,
|
FD.TYPE_BOOL: bool,
|
||||||
FD.TYPE_STRING: unicode,
|
FD.TYPE_STRING: unicode,
|
||||||
# FD.TYPE_MESSAGE: json2pb, #handled specially
|
# FD.TYPE_MESSAGE handled specially
|
||||||
FD.TYPE_BYTES: lambda x: binascii.unhexlify(x),
|
FD.TYPE_BYTES: lambda x: binascii.unhexlify(x),
|
||||||
FD.TYPE_UINT32: int,
|
FD.TYPE_UINT32: int,
|
||||||
FD.TYPE_ENUM: lambda x: getattr(types, x),
|
FD.TYPE_ENUM: lambda x: getattr(types, x),
|
||||||
|
@ -11,6 +11,9 @@ import messages_pb2 as proto
|
|||||||
TREZORD_HOST = 'http://localhost:21324'
|
TREZORD_HOST = 'http://localhost:21324'
|
||||||
CONFIG_URL = 'https://mytrezor.com/data/plugin/config_signed.bin'
|
CONFIG_URL = 'https://mytrezor.com/data/plugin/config_signed.bin'
|
||||||
|
|
||||||
|
def get_error(resp):
|
||||||
|
return ' (error=%d str=%s)' % (resp.status_code, resp.json()['error'])
|
||||||
|
|
||||||
class BridgeTransport(Transport):
|
class BridgeTransport(Transport):
|
||||||
def __init__(self, device, *args, **kwargs):
|
def __init__(self, device, *args, **kwargs):
|
||||||
|
|
||||||
@ -22,11 +25,11 @@ class BridgeTransport(Transport):
|
|||||||
|
|
||||||
r = requests.post(TREZORD_HOST + '/configure', data=config)
|
r = requests.post(TREZORD_HOST + '/configure', data=config)
|
||||||
if r.status_code != 200:
|
if r.status_code != 200:
|
||||||
raise Exception('trezord: Could not configure')
|
raise Exception('trezord: Could not configure' + get_error(r))
|
||||||
|
|
||||||
r = requests.get(TREZORD_HOST + '/enumerate')
|
r = requests.get(TREZORD_HOST + '/enumerate')
|
||||||
if r.status_code != 200:
|
if r.status_code != 200:
|
||||||
raise Exception('trezord: Could not enumerate devices')
|
raise Exception('trezord: Could not enumerate devices' + get_error(r))
|
||||||
enum = r.json()
|
enum = r.json()
|
||||||
|
|
||||||
if len(enum) < 1:
|
if len(enum) < 1:
|
||||||
@ -41,14 +44,14 @@ class BridgeTransport(Transport):
|
|||||||
def _open(self):
|
def _open(self):
|
||||||
r = requests.post(TREZORD_HOST + '/acquire/%s' % self.path)
|
r = requests.post(TREZORD_HOST + '/acquire/%s' % self.path)
|
||||||
if r.status_code != 200:
|
if r.status_code != 200:
|
||||||
raise Exception('trezord: Could not acquire session')
|
raise Exception('trezord: Could not acquire session' + get_error(r))
|
||||||
resp = r.json()
|
resp = r.json()
|
||||||
self.session = resp['session']
|
self.session = resp['session']
|
||||||
|
|
||||||
def _close(self):
|
def _close(self):
|
||||||
r = requests.post(TREZORD_HOST + '/release/%s' % self.session)
|
r = requests.post(TREZORD_HOST + '/release/%s' % self.session)
|
||||||
if r.status_code != 200:
|
if r.status_code != 200:
|
||||||
raise Exception('trezord: Could not release session')
|
raise Exception('trezord: Could not release session' + get_error(r))
|
||||||
else:
|
else:
|
||||||
self.session = None
|
self.session = None
|
||||||
|
|
||||||
@ -61,7 +64,7 @@ class BridgeTransport(Transport):
|
|||||||
payload = '{"type": "%s","message": %s}' % (cls, json.dumps(msg))
|
payload = '{"type": "%s","message": %s}' % (cls, json.dumps(msg))
|
||||||
r = requests.post(TREZORD_HOST + '/call/%s' % self.session, data=payload)
|
r = requests.post(TREZORD_HOST + '/call/%s' % self.session, data=payload)
|
||||||
if r.status_code != 200:
|
if r.status_code != 200:
|
||||||
raise Exception('trezord: Could not write message')
|
raise Exception('trezord: Could not write message' + get_error(r))
|
||||||
else:
|
else:
|
||||||
self.response = r.json()
|
self.response = r.json()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user