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)?
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