Do you have any suggestions on how to design several different packet types without too much redundancy while keeping the order intact?
For example I would like to do something among the lines of:
struct __attribute__((packed)) GenericPacket {
uint8_t start_seq[2]; // always {0xBE, 0xEF}
uint8_t body_size;
uint8_t packet_type;
uint8_t header_byte;
uint8_t body[body_size];
uint8_t end_seq[2]; // always {0xEF, 0xBE}
};
struct __attribute__((packed)) GenericEntityPacket : public GenericPacket {
// Replace uint8_t body[body_size]; with:
uint8_t entity_id;
uint8_t reserved[3];
uint8_t remaining_body[body_size-4];
};
// Example: BE EF 08 03 00 58 00 00 00 00 00 C0 40 EF BE
struct __attribute__((packed)) FloatEntityPacket : public GenericEntityPacket {
// Replace `uint8_t remaining_body[body_size-4];` with
float value;
};
// Example: BE EF 05 03 00 59 00 00 01 EF BE
struct __attribute__((packed)) BoolEntityPacket : public GenericEntityPacket {
// Replace `uint8_t value[body_size-4];` with
uint8 unused[3];
bool value;
};
// Example: BE EF 05 03 00 5C 00 00 00 04 EF BE
struct __attribute__((packed)) Uint8tEntityPacket : public GenericEntityPacket {
// Replace `uint8_t value[body_size-4];` with
uint8 unused[3];
uint8_t value;
};
// Example: BE EF 01 18 01 02 EF BE
struct __attribute__((packed)) ToggleTunerPacket : public GenericPacket {
// Replace `uint8_t header_byte;` with:
uint8_t state;
// Replace `uint8_t body[body_size];` with
uint8_t channel;
};
// Example: BE EF 00 1E 02 EF BE
struct __attribute__((packed)) SdRecordPacket : public GenericPacket {
// Replace `uint8_t header_byte;` with:
uint8_t state;
// Remove `uint8_t body[body_size];`
};
// Example: BE EF 01 1A 01 04 EF BE
struct __attribute__((packed)) SnapshotPacket : public GenericPacket {
// Replace `uint8_t header_byte;` with:
uint8_t action;
// Replace `uint8_t body[body_size];` with
uint8_t slot;
};
Edit: Moved to How to elegantly inherit from struct overriding/adding fields