I have message of the form:
message OuterMessage {
repeated InnerMessage msg = 1;
}
I would like to compose an instance of OuterMessage by appending already encoded instances of InnerMessage. Is there a legit way to do this with nanopbs?
I have message of the form:
message OuterMessage {
repeated InnerMessage msg = 1;
}
I would like to compose an instance of OuterMessage by appending already encoded instances of InnerMessage. Is there a legit way to do this with nanopbs?
There are a few ways:
Encode outer message manually by calling pb_encode_tag_for_field()
and pb_encode_string()
. In protobuf, submessages are equivalent to strings in the wire format.
Use callbacks for the repeated field. In the callback you can do the same as 1. above, but also get automatic encoding of any other fields in the outer message.
Encode just the header (tag and length) to a separate buffer, send it and then send your already encoded message. This avoids having to copy the data around.
Set option (nanopb).type_override = TYPE_BYTES
on the submessage field, to generate it as if was a bytes
field instead. Then you can set the encoded content inside that. But this approach is quite inefficient, as it results in copying the message data multiple times.