在 c 中使用 nanopb 的消息中使用重复字段规则 [英] using repeated field rule in a message with nanopb in c

查看:242
本文介绍了在 c 中使用 nanopb 的消息中使用重复字段规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难理解如何使用重复字段规则.例如,这是我的 .proto:

消息测试{重复浮点值 = 1;}

现在,我正在初始化一个新的 Test 对象:

Test test = test_init_zero()

最后,我想分配一些值.例如:

float values[] = { 1.0, 2.2, 5.5, 7.13 }

我的问题是如何分配它们?是不是像

test.value = 值//或者test.value[0] = values[0]//... 等等.

然后,我如何读回它们?

解决方案

这取决于您如何定义 proto 文件中的重复字段.根据 nanopb docs,您可以指定 像您一样重复字段,然后在编码/解码过程中使用回调函数分别处理每个项目,或者您使用特定于nanopb的设置,因此具有固定长度的数组:

<块引用>

  1. 默认情况下,任何类型的字符串、字节和重复字段都映射到回调函数.
  2. 如果在 .proto 文件中指定了特殊选项 (nanopb).max_size,则字符串映射到以空字符结尾的字符数组,字节映射到包含一个字符数组和一个大小字段.
  3. 如果 (nanopb).fixed_length 设置为 true 并且 (nanopb).max_size 也设置了,则字节映射到内联固定大小的字节数组.
  4. 如果在重复字段上指定了特殊选项 (nanopb).max_count,它将映射到任何类型的重复数组.将为存储的实际条目数创建另一个字段.

比如字节数组需要使用max_size:

所需字节数据 = 1 [(nanopb).max_size = 40, (nanopb).fixed_length = true];

当使用 nanopb 编译时,这将创建以下字段:

//字节数组在 nanopb 中得到特殊处理pb_byte_t 数据[40];

或者,对于 float,您可以根据规则 4 使用 max_count:

重复浮点数据 = 1 [(nanopb).max_count = 40];

然后你会得到:

size_t data_count;浮动数据[40];

如果你像你一样简单地定义一个 repeated 字段,那么 nanopb 将创建一个回调函数:

//重复浮点值 = 1;pb_callback_t 值;

这意味着您必须提供自己的函数来处理每个传入的项目:

yourobject.value.arg = &custom_args;yourobject.value.funcs.decode = custom_function_for_decoding;

I'm having a hard time in realizing how to use the repeated field rule. for example, this is my .proto:

message Test
{
   repeated float   value = 1;
}

now, I'm initialize a new Test object:

Test test = test_init_zero()

Finally, I want to assign some values. For example:

float values[] = { 1.0, 2.2, 5.5, 7.13 }

My question is how can I assign them? is it like

test.value = values
//or
test.value[0] = values[0] //... etc.

and then, how do I read them back?

解决方案

This depends on how you define the repeated field inside the proto file. According to nanopb docs, you either just specify the repeated field like you did, and then use a callback function to handle each item separately during encoding/decoding, or you use nanopb-specific settings so have a fixed length array:

  1. Strings, bytes and repeated fields of any type map to callback functions by default.
  2. If there is a special option (nanopb).max_size specified in the .proto file, string maps to null-terminated char array and bytes map to a structure containing a char array and a size field.
  3. If (nanopb).fixed_length is set to true and (nanopb).max_size is also set, then bytes map to an inline byte array of fixed size.
  4. If there is a special option (nanopb).max_count specified on a repeated field, it maps to an array of whatever type is being repeated. Another field will be created for the actual number of entries stored.

For example, byte arrays need to use max_size:

required bytes data = 1 [(nanopb).max_size = 40, (nanopb).fixed_length = true];

And this would create the following field, when compiled using nanopb:

// byte arrays get a special treatment in nanopb
pb_byte_t data[40];

Or, for a float, you would use max_count according to rule 4.:

repeated float data = 1 [(nanopb).max_count = 40];

And then you'll get:

size_t data_count;
float data[40];

If you simply define a repeated field like you did, then nanopb will create a callback function:

// repeated float value = 1;
pb_callback_t value;

Which means you will have to provide your own function which will handle each incoming item:

yourobject.value.arg = &custom_args;
yourobject.value.funcs.decode = custom_function_for_decoding;

这篇关于在 c 中使用 nanopb 的消息中使用重复字段规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆