msgproto: Rework dump() so it also works with params
Always call the regular .parse() method for each message type during dump() - add a new .format_params() method for dumping a verbose representation of the parsed message. This allows the new format_params() to also be used with data already parsed. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
parent
a6fe355801
commit
ccb93068fe
|
@ -25,6 +25,7 @@ class KeyboardReader:
|
||||||
self.eval_globals = {}
|
self.eval_globals = {}
|
||||||
def connect(self, eventtime):
|
def connect(self, eventtime):
|
||||||
self.ser.connect()
|
self.ser.connect()
|
||||||
|
self.ser.handle_default = self.handle_default
|
||||||
self.mcu_freq = self.ser.msgparser.get_constant_float('CLOCK_FREQ')
|
self.mcu_freq = self.ser.msgparser.get_constant_float('CLOCK_FREQ')
|
||||||
mcu = self.ser.msgparser.get_constant('MCU')
|
mcu = self.ser.msgparser.get_constant('MCU')
|
||||||
self.pins = pins.get_pin_map(mcu)
|
self.pins = pins.get_pin_map(mcu)
|
||||||
|
@ -33,6 +34,8 @@ class KeyboardReader:
|
||||||
def output(self, msg):
|
def output(self, msg):
|
||||||
sys.stdout.write("%s\n" % (msg,))
|
sys.stdout.write("%s\n" % (msg,))
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
|
def handle_default(self, params):
|
||||||
|
self.output(self.ser.msgparser.format_params(params))
|
||||||
def update_evals(self, eventtime):
|
def update_evals(self, eventtime):
|
||||||
self.eval_globals['freq'] = self.mcu_freq
|
self.eval_globals['freq'] = self.mcu_freq
|
||||||
self.eval_globals['clock'] = self.ser.get_clock(eventtime)
|
self.eval_globals['clock'] = self.ser.get_clock(eventtime)
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
# Protocol definitions for firmware communication
|
# Protocol definitions for firmware communication
|
||||||
#
|
#
|
||||||
# Copyright (C) 2016 Kevin O'Connor <kevin@koconnor.net>
|
# Copyright (C) 2016,2017 Kevin O'Connor <kevin@koconnor.net>
|
||||||
#
|
#
|
||||||
# This file may be distributed under the terms of the GNU GPLv3 license.
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
||||||
import json, zlib, logging
|
import json, zlib, logging
|
||||||
|
@ -106,8 +106,8 @@ class MessageFormat:
|
||||||
self.name = parts[0]
|
self.name = parts[0]
|
||||||
argparts = [arg.split('=') for arg in parts[1:]]
|
argparts = [arg.split('=') for arg in parts[1:]]
|
||||||
self.param_types = [MessageTypes[fmt] for name, fmt in argparts]
|
self.param_types = [MessageTypes[fmt] for name, fmt in argparts]
|
||||||
self.param_names = [name for name, fmt in argparts]
|
self.param_names = [(name, MessageTypes[fmt]) for name, fmt in argparts]
|
||||||
self.name_to_type = dict(zip(self.param_names, self.param_types))
|
self.name_to_type = dict(self.param_names)
|
||||||
def encode(self, *params):
|
def encode(self, *params):
|
||||||
out = []
|
out = []
|
||||||
out.append(self.msgid)
|
out.append(self.msgid)
|
||||||
|
@ -117,26 +117,24 @@ class MessageFormat:
|
||||||
def encode_by_name(self, **params):
|
def encode_by_name(self, **params):
|
||||||
out = []
|
out = []
|
||||||
out.append(self.msgid)
|
out.append(self.msgid)
|
||||||
for name, t in zip(self.param_names, self.param_types):
|
for name, t in self.param_names:
|
||||||
t.encode(out, params[name])
|
t.encode(out, params[name])
|
||||||
return out
|
return out
|
||||||
def parse(self, s, pos):
|
def parse(self, s, pos):
|
||||||
pos += 1
|
pos += 1
|
||||||
out = {}
|
out = {}
|
||||||
for t, name in zip(self.param_types, self.param_names):
|
for name, t in self.param_names:
|
||||||
v, pos = t.parse(s, pos)
|
v, pos = t.parse(s, pos)
|
||||||
out[name] = v
|
out[name] = v
|
||||||
return out, pos
|
return out, pos
|
||||||
def dump(self, s, pos):
|
def format_params(self, params):
|
||||||
pos += 1
|
|
||||||
out = []
|
out = []
|
||||||
for t in self.param_types:
|
for name, t in self.param_names:
|
||||||
v, pos = t.parse(s, pos)
|
v = params[name]
|
||||||
if not t.is_int:
|
if not t.is_int:
|
||||||
v = repr(v)
|
v = repr(v)
|
||||||
out.append(v)
|
out.append(v)
|
||||||
outmsg = self.debugformat % tuple(out)
|
return self.debugformat % tuple(out)
|
||||||
return outmsg, pos
|
|
||||||
|
|
||||||
class OutputFormat:
|
class OutputFormat:
|
||||||
name = '#output'
|
name = '#output'
|
||||||
|
@ -167,14 +165,8 @@ class OutputFormat:
|
||||||
out.append(v)
|
out.append(v)
|
||||||
outmsg = self.debugformat % tuple(out)
|
outmsg = self.debugformat % tuple(out)
|
||||||
return {'#msg': outmsg}, pos
|
return {'#msg': outmsg}, pos
|
||||||
def dump(self, s, pos):
|
def format_params(self, params):
|
||||||
pos += 1
|
return "#output %s" % (params['#msg'],)
|
||||||
out = []
|
|
||||||
for t in self.param_types:
|
|
||||||
v, pos = t.parse(s, pos)
|
|
||||||
out.append(v)
|
|
||||||
outmsg = self.debugformat % tuple(out)
|
|
||||||
return outmsg, pos
|
|
||||||
|
|
||||||
class UnknownFormat:
|
class UnknownFormat:
|
||||||
name = '#unknown'
|
name = '#unknown'
|
||||||
|
@ -182,6 +174,8 @@ class UnknownFormat:
|
||||||
msgid = s[pos]
|
msgid = s[pos]
|
||||||
msg = str(bytearray(s))
|
msg = str(bytearray(s))
|
||||||
return {'#msgid': msgid, '#msg': msg}, len(s)-MESSAGE_TRAILER_SIZE
|
return {'#msgid': msgid, '#msg': msg}, len(s)-MESSAGE_TRAILER_SIZE
|
||||||
|
def format_params(self, params):
|
||||||
|
return "#unknown %s" % (repr(params['#msg']),)
|
||||||
|
|
||||||
class MessageParser:
|
class MessageParser:
|
||||||
error = error
|
error = error
|
||||||
|
@ -221,11 +215,20 @@ class MessageParser:
|
||||||
while 1:
|
while 1:
|
||||||
msgid = s[pos]
|
msgid = s[pos]
|
||||||
mid = self.messages_by_id.get(msgid, self.unknown)
|
mid = self.messages_by_id.get(msgid, self.unknown)
|
||||||
params, pos = mid.dump(s, pos)
|
params, pos = mid.parse(s, pos)
|
||||||
out.append("%s" % (params,))
|
out.append(mid.format_params(params))
|
||||||
if pos >= len(s)-MESSAGE_TRAILER_SIZE:
|
if pos >= len(s)-MESSAGE_TRAILER_SIZE:
|
||||||
break
|
break
|
||||||
return out
|
return out
|
||||||
|
def format_params(self, params):
|
||||||
|
name = params.get('#name')
|
||||||
|
mid = self.messages_by_name.get(name)
|
||||||
|
if mid is not None:
|
||||||
|
return mid.format_params(params)
|
||||||
|
msg = params.get('#msg')
|
||||||
|
if msg is not None:
|
||||||
|
return "%s %s" % (name, msg)
|
||||||
|
return str(params)
|
||||||
def parse(self, s):
|
def parse(self, s):
|
||||||
msgid = s[MESSAGE_HEADER_SIZE]
|
msgid = s[MESSAGE_HEADER_SIZE]
|
||||||
mid = self.messages_by_id.get(msgid, self.unknown)
|
mid = self.messages_by_id.get(msgid, self.unknown)
|
||||||
|
|
Loading…
Reference in New Issue