aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--klippy/mcu.py2
-rw-r--r--klippy/msgproto.py61
2 files changed, 31 insertions, 32 deletions
diff --git a/klippy/mcu.py b/klippy/mcu.py
index d7a679ac..6b106245 100644
--- a/klippy/mcu.py
+++ b/klippy/mcu.py
@@ -87,7 +87,7 @@ class CommandWrapper:
if cmd_queue is None:
cmd_queue = serial.get_default_command_queue()
self._cmd_queue = cmd_queue
- self._msgtag = msgparser.lookup_msgtag(msgformat) & 0xffffffff
+ self._msgtag = msgparser.lookup_msgid(msgformat) & 0xffffffff
def send(self, data=(), minclock=0, reqclock=0):
cmd = self._cmd.encode(data)
self._serial.raw_send(cmd, minclock, reqclock, self._cmd_queue)
diff --git a/klippy/msgproto.py b/klippy/msgproto.py
index f8a12530..0fe76593 100644
--- a/klippy/msgproto.py
+++ b/klippy/msgproto.py
@@ -1,6 +1,6 @@
# Protocol definitions for firmware communication
#
-# Copyright (C) 2016-2021 Kevin O'Connor <kevin@koconnor.net>
+# Copyright (C) 2016-2024 Kevin O'Connor <kevin@koconnor.net>
#
# This file may be distributed under the terms of the GNU GPLv3 license.
import json, zlib, logging
@@ -160,8 +160,8 @@ def convert_msg_format(msgformat):
return msgformat
class MessageFormat:
- def __init__(self, msgid, msgformat, enumerations={}):
- self.msgid = msgid
+ def __init__(self, msgid_bytes, msgformat, enumerations={}):
+ self.msgid_bytes = msgid_bytes
self.msgformat = msgformat
self.debugformat = convert_msg_format(msgformat)
self.name = msgformat.split()[0]
@@ -169,19 +169,17 @@ class MessageFormat:
self.param_types = [t for name, t in self.param_names]
self.name_to_type = dict(self.param_names)
def encode(self, params):
- out = []
- out.append(self.msgid)
+ out = list(self.msgid_bytes)
for i, t in enumerate(self.param_types):
t.encode(out, params[i])
return out
def encode_by_name(self, **params):
- out = []
- out.append(self.msgid)
+ out = list(self.msgid_bytes)
for name, t in self.param_names:
t.encode(out, params[name])
return out
def parse(self, s, pos):
- pos += 1
+ pos += len(self.msgid_bytes)
out = {}
for name, t in self.param_names:
v, pos = t.parse(s, pos)
@@ -198,13 +196,13 @@ class MessageFormat:
class OutputFormat:
name = '#output'
- def __init__(self, msgid, msgformat):
- self.msgid = msgid
+ def __init__(self, msgid_bytes, msgformat):
+ self.msgid_bytes = msgid_bytes
self.msgformat = msgformat
self.debugformat = convert_msg_format(msgformat)
self.param_types = lookup_output_params(msgformat)
def parse(self, s, pos):
- pos += 1
+ pos += len(self.msgid_bytes)
out = []
for t in self.param_types:
v, pos = t.parse(s, pos)
@@ -219,7 +217,7 @@ class OutputFormat:
class UnknownFormat:
name = '#unknown'
def parse(self, s, pos):
- msgid = s[pos]
+ msgid, param_pos = PT_int32().parse(s, pos)
msg = bytes(bytearray(s))
return {'#msgid': msgid, '#msg': msg}, len(s)-MESSAGE_TRAILER_SIZE
def format_params(self, params):
@@ -234,7 +232,8 @@ class MessageParser:
self.messages = []
self.messages_by_id = {}
self.messages_by_name = {}
- self.msgtag_by_format = {}
+ self.msgid_by_format = {}
+ self.msgid_parser = PT_int32()
self.config = {}
self.version = self.build_versions = ""
self.raw_identify_data = ""
@@ -266,7 +265,7 @@ class MessageParser:
out = ["seq: %02x" % (msgseq,)]
pos = MESSAGE_HEADER_SIZE
while 1:
- msgid = s[pos]
+ msgid, param_pos = self.msgid_parser.parse(s, pos)
mid = self.messages_by_id.get(msgid, self.unknown)
params, pos = mid.parse(s, pos)
out.append(mid.format_params(params))
@@ -283,14 +282,14 @@ class MessageParser:
return "%s %s" % (name, msg)
return str(params)
def parse(self, s):
- msgid = s[MESSAGE_HEADER_SIZE]
+ msgid, param_pos = self.msgid_parser.parse(s, MESSAGE_HEADER_SIZE)
mid = self.messages_by_id.get(msgid, self.unknown)
params, pos = mid.parse(s, MESSAGE_HEADER_SIZE)
if pos != len(s)-MESSAGE_TRAILER_SIZE:
self._error("Extra data at end of message")
params['#name'] = mid.name
return params
- def encode(self, seq, cmd):
+ def encode_msgblock(self, seq, cmd):
msglen = MESSAGE_MIN + len(cmd)
seq = (seq & MESSAGE_SEQ_MASK) | MESSAGE_DEST
out = [msglen, seq] + cmd
@@ -317,11 +316,11 @@ class MessageParser:
self._error("Command format mismatch: %s vs %s",
msgformat, mp.msgformat)
return mp
- def lookup_msgtag(self, msgformat):
- msgtag = self.msgtag_by_format.get(msgformat)
- if msgtag is None:
+ def lookup_msgid(self, msgformat):
+ msgid = self.msgid_by_format.get(msgformat)
+ if msgid is None:
self._error("Unknown command: %s", msgformat)
- return msgtag
+ return msgid
def create_command(self, msg):
parts = msg.strip().split()
if not parts:
@@ -372,22 +371,22 @@ class MessageParser:
start_value, count = value
for i in range(count):
enums[enum_root + str(start_enum + i)] = start_value + i
- def _init_messages(self, messages, command_tags=[], output_tags=[]):
- for msgformat, msgtag in messages.items():
+ def _init_messages(self, messages, command_ids=[], output_ids=[]):
+ for msgformat, msgid in messages.items():
msgtype = 'response'
- if msgtag in command_tags:
+ if msgid in command_ids:
msgtype = 'command'
- elif msgtag in output_tags:
+ elif msgid in output_ids:
msgtype = 'output'
- self.messages.append((msgtag, msgtype, msgformat))
- if msgtag < -32 or msgtag > 95:
- self._error("Multi-byte msgtag not supported")
- self.msgtag_by_format[msgformat] = msgtag
- msgid = msgtag & 0x7f
+ self.messages.append((msgid, msgtype, msgformat))
+ self.msgid_by_format[msgformat] = msgid
+ msgid_bytes = []
+ self.msgid_parser.encode(msgid_bytes, msgid)
if msgtype == 'output':
- self.messages_by_id[msgid] = OutputFormat(msgid, msgformat)
+ self.messages_by_id[msgid] = OutputFormat(msgid_bytes,
+ msgformat)
else:
- msg = MessageFormat(msgid, msgformat, self.enumerations)
+ msg = MessageFormat(msgid_bytes, msgformat, self.enumerations)
self.messages_by_id[msgid] = msg
self.messages_by_name[msg.name] = msg
def process_identify(self, data, decompress=True):