From ccb93068fef2d01c6666c8ba0d849540aa5a8e46 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Tue, 25 Apr 2017 11:15:15 -0400 Subject: [PATCH] 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 --- klippy/console.py | 3 +++ klippy/msgproto.py | 45 ++++++++++++++++++++++++--------------------- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/klippy/console.py b/klippy/console.py index b3ac9c19..4d68b8de 100755 --- a/klippy/console.py +++ b/klippy/console.py @@ -25,6 +25,7 @@ class KeyboardReader: self.eval_globals = {} def connect(self, eventtime): self.ser.connect() + self.ser.handle_default = self.handle_default self.mcu_freq = self.ser.msgparser.get_constant_float('CLOCK_FREQ') mcu = self.ser.msgparser.get_constant('MCU') self.pins = pins.get_pin_map(mcu) @@ -33,6 +34,8 @@ class KeyboardReader: def output(self, msg): sys.stdout.write("%s\n" % (msg,)) sys.stdout.flush() + def handle_default(self, params): + self.output(self.ser.msgparser.format_params(params)) def update_evals(self, eventtime): self.eval_globals['freq'] = self.mcu_freq self.eval_globals['clock'] = self.ser.get_clock(eventtime) diff --git a/klippy/msgproto.py b/klippy/msgproto.py index b0bce35b..abb3dcd8 100644 --- a/klippy/msgproto.py +++ b/klippy/msgproto.py @@ -1,6 +1,6 @@ # Protocol definitions for firmware communication # -# Copyright (C) 2016 Kevin O'Connor +# Copyright (C) 2016,2017 Kevin O'Connor # # This file may be distributed under the terms of the GNU GPLv3 license. import json, zlib, logging @@ -106,8 +106,8 @@ class MessageFormat: self.name = parts[0] argparts = [arg.split('=') for arg in parts[1:]] self.param_types = [MessageTypes[fmt] for name, fmt in argparts] - self.param_names = [name for name, fmt in argparts] - self.name_to_type = dict(zip(self.param_names, self.param_types)) + self.param_names = [(name, MessageTypes[fmt]) for name, fmt in argparts] + self.name_to_type = dict(self.param_names) def encode(self, *params): out = [] out.append(self.msgid) @@ -117,26 +117,24 @@ class MessageFormat: def encode_by_name(self, **params): out = [] 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]) return out def parse(self, s, pos): pos += 1 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) out[name] = v return out, pos - def dump(self, s, pos): - pos += 1 + def format_params(self, params): out = [] - for t in self.param_types: - v, pos = t.parse(s, pos) + for name, t in self.param_names: + v = params[name] if not t.is_int: v = repr(v) out.append(v) - outmsg = self.debugformat % tuple(out) - return outmsg, pos + return self.debugformat % tuple(out) class OutputFormat: name = '#output' @@ -167,14 +165,8 @@ class OutputFormat: out.append(v) outmsg = self.debugformat % tuple(out) return {'#msg': outmsg}, pos - def dump(self, s, pos): - pos += 1 - out = [] - for t in self.param_types: - v, pos = t.parse(s, pos) - out.append(v) - outmsg = self.debugformat % tuple(out) - return outmsg, pos + def format_params(self, params): + return "#output %s" % (params['#msg'],) class UnknownFormat: name = '#unknown' @@ -182,6 +174,8 @@ class UnknownFormat: msgid = s[pos] msg = str(bytearray(s)) return {'#msgid': msgid, '#msg': msg}, len(s)-MESSAGE_TRAILER_SIZE + def format_params(self, params): + return "#unknown %s" % (repr(params['#msg']),) class MessageParser: error = error @@ -221,11 +215,20 @@ class MessageParser: while 1: msgid = s[pos] mid = self.messages_by_id.get(msgid, self.unknown) - params, pos = mid.dump(s, pos) - out.append("%s" % (params,)) + params, pos = mid.parse(s, pos) + out.append(mid.format_params(params)) if pos >= len(s)-MESSAGE_TRAILER_SIZE: break 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): msgid = s[MESSAGE_HEADER_SIZE] mid = self.messages_by_id.get(msgid, self.unknown)