Home > OS >  NanoPB Callback functions
NanoPB Callback functions

Time:08-24

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));
}

CodePudding user response:

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.

  • Related