dimanche 30 mai 2021

Communication protocol in c++

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:

  1. 0xABBACFFC marks the start of a packet Secondary header:
  2. Protocol versions:
  3. Supported protocol versions: 1, 2 Both versions have the same packet layout.
  4. List item
  5. Valid subsystem IDs:
  6. 1 - AOCS
  7. 3 - CDH
  8. 5 - COM

Valid component IDs for each subsystem:

  1. AOCS: 20, 21, 22, 23, 30, 31
  2. CDH: 0
  3. COM: 1, 2, 10, 20

Valid telemetry type: 1 - temperature (implies data of type IEEE 754 single precision floating-point)

Reserved field:

  1. One byte reserved for future protocol expansion.
  2. 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 );
}
  1. iface is an object of a class that is used to communicate with the peripheral devices
  2. existRcvData() method returns true if there are bytes in the Rx FIFO
  3. getByte() method fetches one byte from the Rx FIFO
  4. parser is an object of a class that you are asked to implement
  5. system is an object of a class that communicates with the hypothetical OS
  6. tlm is an object used to process system’s telemetry.
  7. update() method can be used to send latest telemetry to the system control Communication protocol description

Aucun commentaire:

Enregistrer un commentaire