I have a tricky one, and several passes through the docs haven't made it obvious if, and how one might use construct.
Have a binary protocol with a start of frame byte value, and end of frame byte value, and a escape value (prefixed to escape SOF, EOF, and the escape value itself). I currently unescape the frame before parsing with construct.
SOF -> Command ID -> Variable number of bytes depending on command -> EOF -> Checksum LSB -> Checksum MSB
So today I have this:
frame = Struct("frame",
UBInt8("start_of_frame"), # Is there a way to _require_ this to be a constant value?
UBInt8("command"),
Switch("next", lambda ctx: ctx["command"], {
5: Construct_def_command_5
10: Construct_def_command_10
}, Pass, ),
UBInt8("end_of_frame"),
UBInt16("checksum"),
)
But now I find out that it is actually allowable to concatenate commands together inside of one frame. There is no way to find the start of the subsequent commands without knowing how many bytes were consumed by the previous command. I think I'm handling the variable length commands well, but I'm not sure how to handle the variable _number_ of commands.
SOF -> Command ID -> Variable number of bytes depending on command - > Command ID -> Variable number of bytes depending on command -> Repeat X times -> EOF -> Checksum LSB
If this do-able? Or should I break down and switch to a hand built parser?
Bonus points if someone gives me a hint on how to automatically verify/create the checksum.
Thanks
Mark