I would like to implement communication protocol in C++ with the following conditions, can someone help/suggest me on how to implement it.
Basic conditions:
- Packet size is fixed to 12 bytes
- Every packet starts with a “magic” header.
- Byte order of data in the packet is from most significant to least significant
- You cannot assume that all incoming bytes are correct, consistent packets.
There may be other types of data between packets, for example: [packet] [packet] [control sequence] [packet] [spurious bytes] [control sequence] [packet]
Packet layout contains following information: Magic header[4B] 0xABBACFFC, Secondary Header[2B], Reserved[1B] 0xFF, Payload [4B], checksum [1B]
Secondary header layout: Protocol version [3 bit], Subsystem ID [5 bit], Component ID [5 bit], Telemetry type[3 bit]
Magic header:
- 0xABBACFFC marks the start of a packet Secondary header:
- Protocol versions:
- Supported protocol versions: 1, 2 Both versions have the same packet layout.
- List item
- Valid subsystem IDs:
- 1 - AOCS
- 3 - CDH
- 5 - COM
Valid component IDs for each subsystem:
- AOCS: 20, 21, 22, 23, 30, 31
- CDH: 0
- COM: 1, 2, 10, 20
Valid telemetry type: 1 - temperature (implies data of type IEEE 754 single precision floating-point)
Reserved field:
- One byte reserved for future protocol expansion.
- Must be set to 0xFF
Payload:
- If telemetry types is ‘1’ (temperature), the value in the 4 bytes of payload is IEEE 754 single precision floating-point.
Checksum:
- Simple exclusive-or (xor) of all bytes from the start of secondary header to the end of payload (including reserved field).
Interface description: For the exemplary code block fragment, I want to design and implement Parser class. tlm_parser.h:
class Parser {
// Implement me
};
tlm.h:
typedef struct {
uint8_t subsys_id;
uint8_t compo_id;
Float temperature;
} CompoTlm_t;
tlm_proc.cpp:
while ( 1 ) {
while (iface.existRcvData()) {
uint8_t byte = iface.getByte();
// Parse incoming bytes and detect packet
if (parser.detectPkt(byte)) {
// One, correct packet has been detected
CompoTlm_t tlm = {};
int err = parser.extractData(tlm);
if (err == 0 ) {
// Pass telemetry info to the upper layer
system.tlm.update(tlm);
} else {
// Error handling, etc.
...
...
}
}
}
system.time.sleep_ms( 100 );
}
- iface is an object of a class that is used to communicate with the peripheral devices
- existRcvData() method returns true if there are bytes in the Rx FIFO
- getByte() method fetches one byte from the Rx FIFO
- parser is an object of a class that you are asked to implement
- system is an object of a class that communicates with the hypothetical OS
- tlm is an object used to process system’s telemetry.
- update() method can be used to send latest telemetry to the system control Communication protocol description
Aucun commentaire:
Enregistrer un commentaire