0

I am working on a project which includes the use of NanoPB. Currently I have the situation where in my protofile I have multiple callback fields. I now must encode and decode these callback fields using my own written callback functions.

My question is:

I have a message defined in the protofile which contains callback fields and non callback fields. If I create an callback encode function, should I make this for a specific field or for the entire message?

My protofile looks like this:

syntax = "proto2";

message stringCallback{
    required string name = 1;
    required string surname = 2;
    required int32 age = 3;
}

An example of encoding a string:

bool encode_string(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
{
    char *str = "Hello world!";
    
    if (!pb_encode_tag_for_field(stream, field))
        return false;
    
    return pb_encode_string(stream, (uint8_t*)str, strlen(str));
}
J.Meulenbeld
  • 185
  • 1
  • 17

1 Answers1

1

If I create an callback encode function, should I make this for a specific field or for the entire message?

Whatever is most suitable for your purpose.

The example callback you show is not particularly useful. If you only wanted to take a string from char*, you could just set (nanopb).type = FT_POINTER on the field.

If your callback actions are the same for multiple fields, by all means, reuse the same function. If there is a difference, make separate functions.

jpa
  • 10,351
  • 1
  • 28
  • 45
  • Okay just so I understand it correctly, I can have seperate callback functions for each field of the message. Then I link these to the fields by typing: `msg.name.funcs.encode = function` If I then encode a full msg it will call the encode callback functions of the fields I assigned it to? – J.Meulenbeld Aug 22 '22 at 11:19
  • 1
    @J.Meulenbeld Yes, callback for each field is called in its turn. The order is determined by the tag numbers in the `.proto` file. – jpa Aug 22 '22 at 11:29