user2316667
user2316667

Reputation: 5634

Can you include meta-data into a generated flat buffer header?

I am currently sending data between my PC and an ARM M4 Microcontroller via UART. I've defined my own protocol where each message looks like this:
[START_CHAR LEN TYPE SEQ DATA CRC]
The START_CHAR and LEN fields help me determine when the data ends, after which I look up the TYPE (constant offset of 3) to figure out what data came in order to unpack it into a message class.

Now I'm looking into flatbuffers and it seems perfect except that I cannot encode the TYPE into the message without including it inside the actual message. Here is what I am trying to do:

namespace FlatMessage;

uint8 const TYPE = 50; // does not compile

table String {
  value:string;
}

root_type String;

I could create an Enum but that is messy. Thank you!

[EDIT] I should add that I could just change the protocol to have an END_CHAR but I need to support the TYPE field for legacy reasons.

Well actually, I suppose I would still need the type to figure out how to deserialize it as a flatbuffer.

e.g.

  uint8_t *buf = builder.GetBufferPointer(); // I can do this with END_CHAR because I could get the buffer.

  auto receive_string = GetString(buf); // But I wouldn't know what the type is. e.g. this could be GetCoolString(buf).

Upvotes: 2

Views: 638

Answers (1)

Aardappel
Aardappel

Reputation: 6074

You have a couple of options to store a type with a FlatBuffer:

  • Prefix a buffer yourself with a type.
  • Use the file_identifier feature of FlatBuffers, to make it possible to identify the type of FlatBuffer.
  • Store the type in FlatBuffers itself, by using a union type. Make the root table have a single union field.

Upvotes: 2

Related Questions