2
votes

I do some embedded coding and quite frequently have to parse binary packets of data. They are usually pretty simple, so I wrote parsers by hand. Unfortunately I have to do it over and over for slightly different protocols which is dull and error-prone.

Packets look something like this:

| startByte | size | datadatadata | checksum |

Data can have it's own structure (which I would also like to serialize).

So I thought, maybe there is some simple way of doing it? Ideally I want to be able to write packet format in the class, make an object of that class and serialize it into buffer. And deserialize it, of course.

What are the difficulties:

  • Packets may have different length (but length is either a field in the packet or a constant).
  • Packets contain some fields which must be calculated (like CRC or checksum).
  • Packets are received one byte at time and can be corrupted, so deserialization should be able to find valid packet.
  • It's an embedded code for microcontroller, so I'm restricted to C++ without fancy stuff (like Boost or heavy classes from standart library).

So, my question is:

  • Is there anything ready-to-use for my case?
  • If there is not, should I try and build it myself? If so, in what direction should I think (like, lexers, parser generators or something else)?
1
I'd suggest using some efficient serialization format like google protobuf. Check the nanopb c-API for small empedded systems. - πάντα ῥεῖ
if the question is about the wrapping/unwrapping of the datadatadata, it looks like a simple enough task to do it by hand. Your data structures should derive from an "serializable" interface, and your wrapper will work with those classes based on whatever discriminates between the flavours of the packet. If you are asking about the data serialization itself, then there is no fast and ready answer to this problem. If you are trying to choose the format, consider ASN.1 (BER, PER), XML, JSON, YAML. All have pros and cons, and related libraries. - ArunasR
πάντα ῥεῖ that looks interesting. Can protobuf process calculated fields like checksum? - Amomum
arunasr, no, the question is about parsing the whole packet. Serializaton of data is indeed rather simple thing. - Amomum
you end up doing more work/code/cycles than simply parsing it as you go. - old_timer

1 Answers

0
votes

There is a framework called redBlocks that offers quite what you are seeking. It contains a component that can be used to build a parser and a serializer from a single message definition:

There are also components for CRC calculation.

The library requires C++ and is meant for embedded applications.