- 由于nanopb在资源受限平台的应用很适合,很多人就考虑使用nanopb进行数据消息的编解码,但是由于RAM受限,不像protobuf-c的实现,可以随意使用RAM,nanopb对于repeated消息的处理,可以分为两种方案,一种是通过额外的*.options描述文件来指定特殊成员的max_size大小,另外一种方案,就是callback的方式来实现,不指定大小,而是由编码阶段决定具体的大小。
- nanopb官方提供的test以及example示例程序,都没有提到repeated消息包含多层repeated消息时,如何使用callback的方式实现encode和decode,这里给大家演示一下具体的实现方案,详细代码已经放到gitcode
- 比如,这里的测试程序,gwDevSceneInfo消息里面包含的btActionMsg消息就是repeated,这都很正常,一层repeated很容易就使用callback实现,但是btActionMsg消息里面,又包含了repeated Datapoint 消息,这样在encode和decode时候,很多人就不知道怎么实现了。
syntax = "proto3";
enum messageDpType{
DP_TYPE_RAW=0;
DP_TYPE_BOOL=1;
DP_TYPE_VALUE=2;
DP_TYPE_STRING=3;
DP_TYPE_ENUM=4;
}
message Datapoint{
uint32 dpid=1;
messageDpType type=2;
uint32 len=3;
oneof t_value{
string v_raw=4;
bool v_bool=5;
int32 v_value=6;
string v_string=7;
int32 v_enum=8;
}
}
message btActionMsg{
oneof actionId{
string deviceId=1;
int32 groupId=2;
string sceneId=3;
}
string deviceName=4;
string devicePid=5;
bool online=6;
int32 dp_size=7;
repeated Datapoint dp=8;
}
message gwDevSceneInfo{
string sceneId=1;
string sceneName=2;
repeated btActionMsg sceneAction=3;
}