Defining Protocol Stack
To summarise the protocol transport wrapping subject, let's define a custom protocol that wraps the message payload in the following way:
SYNC | SIZE | ID | PAYLOAD | CHECKSUMwhere:
All the fields are serialised using BIG endian.
SYNC - 2 bytes of synchronisation value to indicate beginning of the message,
must be "0xab 0xcd"
SIZE - 2 bytes, length of remaining data, including checksum and not
including SIZE field itself.
ID - 1 byte, numeric ID of the message.
PAYLOAD - any number of bytes, serialised message data
CHECKSUM - 2 bytes, arithmetic summary of all bytes starting (and including)
from SIZE field and ending after PAYLOAD field.
The protocol layer should wrap one another in the following way:

Please note, that CHECKSUM layer doesn't wrap SYNC because synchronisation prefix is not included in the checksum calculation.
Common Protocol Definitions
PAYLOAD Layer
ID Layer
SIZE Layer
Please note, that SIZE field definition uses comms::option::NumValueSerOffset option, which effectively adds 2 when size value is serialised, and subtracts it when remaining length is deserialised. It must be done, because SIZE value specifies number of remaining bytes, including the CHECKSUM value at the end.
CHECKSUM Layer
SYNC Layer
Processing Loop
The outermost layer defines a full protocol stack. It should be typedef-ed to avoid any confusion:
The processing loop may look like this:
The processing loop above is not the most efficient one, but it demonstrates what needs to be done and how our generic library can be used to identify and process the received message.
Writing Message
The write logic is even simpler.
Last updated
Was this helpful?