1

How to properly encode data with NanoPB when having several nested 'repeated' fields?

This is my schema:

message Report {

  message SensorData {
     required uint32 sensorid = 1;
     required uint32 sample = 2;
  }

  message DeviceData {
    required uint32 devid = 1;
    repeated SensorData sensor_data = 2;
  }

  required uint32 reportnum = 1;
  repeated DeviceData dev_data = 2;

}

I have already made a working version in which SensorData fields are embedded inside DeviceData message based on the server.c example from the NanoPB source. This way I have only one repeated field and everything works fine. However this way I have to repeat the 'devid' field for every sensorid and every 'sample', instead of giving it just one time and then loop through an array of SensorData messages. However I am struggling to encode this with NanoPB, the decoding part is in Python. Can someone give me an example how to properly encode data in this case?

quinz
  • 1,282
  • 4
  • 21
  • 33

1 Answers1

0

For me the simplest way to do this was by statically defining the size of the array's using a nanopb options file. After that you can access each element just like an array.

report.dev_data[i].devid[j] = 1234;
report.dev_data[i].sensor_data[j] = 9876;
Bart
  • 1,405
  • 6
  • 32