My protobuf file is
message Msg{
// User Authentication data as bytes.
bytes MsgData = 1 [(nanopb).max_size = 2048];
}
When I generate the C API, the relevant parts are:
#define PB_BYTES_ARRAY_T(n) struct { pb_size_t size; pb_byte_t bytes[n]; }
/* Struct definitions */
typedef PB_BYTES_ARRAY_T(2048) Msg_AuthenticationData_t;
/* Maximum encoded size of messages (where known) */
#define Msg_size 2051
QUESTION
Who sets the field pb_size_t size
? And why isn't it set in the macro somehow?
Since the size of the payload is known - 2048 in this case - shouldn't pb_size_t size
always by 2048?
Note: I understand that 2051 is for the encoded size and 2048 is for the size of payload.
CodePudding user response:
Since the size of the payload is known - 2048 in this case - shouldn't pb_size_t size always by 2048?
When you set (nanopb).max_size = 2048
, it is the maximum size. The actual size of the data can be anything from 0 to 2048 bytes.
The .size
field should be set by your code prior to encoding, and it will be set by nanopb
when decoding a protobuf message.
If the length of the data is always the same, you can additionally set (nanopb).fixed_length = true
. Then the .size
field is not generated. Any incoming messages with length different than max_size
will cause pb_decode()
to return an error.