简体   繁体   English

NanoPB 回调函数

[英]NanoPB Callback functions

I am working on a project which includes the use of NanoPB.我正在做一个项目,其中包括使用 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.我在 protofile 中定义了一条消息,其中包含回调字段和非回调字段。 If I create an callback encode function, should I make this for a specific field or for the entire message?如果我创建一个回调编码 function,我应该为特定字段还是整个消息创建这个?

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?如果我创建一个回调编码 function,我应该为特定字段还是整个消息创建这个?

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.如果您只想从char*中获取一个字符串,您可以在该字段上设置(nanopb).type = FT_POINTER

If your callback actions are the same for multiple fields, by all means, reuse the same function.如果多个字段的回调操作相同,请务必重用相同的 function。 If there is a difference, make separate functions.如果有差异,请制作单独的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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