简体   繁体   中英

NanoPB Callback functions

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM