NanoPB: How to handle nested messages in C++

See also: C version: How to handle nested messages in C

NanoPB is a code-size optimized Protocol Buffers implementation for embedded systems. This post shows how to handle nested messages in C++ with NanoPB.

Proto definition

First, create a .proto file with nested messages:

nested.proto
syntax = "proto3";

package example;

message Address {
  string street = 1;
  string city = 2;
  string country = 3;
}

message Person {
  string name = 1;
  uint32 age = 2;
  Address address = 3;
}

Generate NanoPB code

Generate the NanoPB code with a .options file to specify string buffer sizes:

Create nested.options:

example.txt
example.Address.street max_size:64
example.Address.city max_size:32
example.Address.country max_size:32
example.Person.name max_size:64

Then generate:

example.sh
protoc --nanopb_out=. nested.proto

This will generate nested.pb.h and nested.pb.c.

C++ example

Here’s a complete C++ example handling nested messages:

nested_example.cpp
#include <stdio.h>
#include <string.h>
#include "nested.pb.h"
#include "pb_encode.h"
#include "pb_decode.h"

int main() {
    // Buffer for encoded message
    uint8_t buffer[256];
    size_t message_length;
    
    // --- ENCODING ---
    example_Person person = example_Person_init_zero;
    
    // Set nested message values
    strncpy(person.name, "John Doe", sizeof(person.name) - 1);
    person.has_address = true;
    strncpy(person.address.street, "123 Main St", sizeof(person.address.street) - 1);
    strncpy(person.address.city, "Springfield", sizeof(person.address.city) - 1);
    strncpy(person.address.country, "USA", sizeof(person.address.country) - 1);
    
    // Create stream for encoding
    pb_ostream_t ostream = pb_ostream_from_buffer(buffer, sizeof(buffer));
    
    // Encode the message
    if (!pb_encode(&ostream, example_Person_fields, &person)) {
        printf("Encoding failed: %s\n", PB_GET_ERROR(&ostream));
        return 1;
    }
    
    message_length = ostream.bytes_written;
    printf("Encoded %zu bytes\n", message_length);
    
    // Print hex dump of encoded data
    printf("Encoded data: ");
    for (size_t i = 0; i < message_length; i++) {
        printf("%02x ", buffer[i]);
    }
    printf("\n");
    
    // --- DECODING ---
    example_Person decoded = example_Person_init_zero;
    
    // Create stream for decoding
    pb_istream_t istream = pb_istream_from_buffer(buffer, message_length);
    
    // Decode the message
    if (!pb_decode(&istream, example_Person_fields, &decoded)) {
        printf("Decoding failed: %s\n", PB_GET_ERROR(&istream));
        return 1;
    }
    
    // Print decoded values
    printf("Decoded values:\n");
    printf("  name: %s\n", decoded.name);
    printf("  age: %u\n", decoded.age);
    printf("  address:\n");
    printf("    street: %s\n", decoded.address.street);
    printf("    city: %s\n", decoded.address.city);
    printf("    country: %s\n", decoded.address.country);
    
    return 0;
}

Compile command

Compile the example with nanopb. NanoPB is typically used by including the source files directly in your project:

example.sh
g++ -o nested_example nested_example.cpp nested.pb.c pb_common.c pb_encode.c pb_decode.c -I.

Note: NanoPB source files (pb_common.c, pb_encode.c, pb_decode.c) need to be compiled directly with your project. You can obtain these from the NanoPB GitHub repository.

Python test script

To verify the encoding, you can use Python’s protobuf library:

test_nested.py
import nested_pb2

# Read the binary data
with open('encoded.bin', 'rb') as f:
    data = f.read()

# Decode
msg = nested_pb2.Person()
msg.ParseFromString(data)

print("Python decoded values:")
print(f"  name: {msg.name}")
print(f"  age: {msg.age}")
print(f"  address:")
print(f"    street: {msg.address.street}")
print(f"    city: {msg.address.city}")
print(f"    country: {msg.address.country}")

First, compile the Python protobuf definitions:

example.sh
protoc --python_out=. nested.proto

Then modify the C++ example to save the encoded data to a file:

example.cpp
// After encoding, add this:
FILE *f = fopen("encoded.bin", "wb");
fwrite(buffer, 1, message_length, f);
fclose(f);

Example with optional nested message

Here’s an example with an optional nested message:

nested_optional.proto
syntax = "proto3";

package example;

message Address {
  string street = 1;
  string city = 2;
}

message Person {
  string name = 1;
  optional Address address = 2;
}

Create nested_optional.options:

example.txt
example.Address.street max_size:64
example.Address.city max_size:32
example.Person.name max_size:64
nested_optional_example.cpp
#include <stdio.h>
#include <string.h>
#include "nested_optional.pb.h"
#include "pb_encode.h"
#include "pb_decode.h"

int main() {
    uint8_t buffer[256];
    size_t message_length;
    
    // --- ENCODING ---
    example_Person person = example_Person_init_zero;
    
    strncpy(person.name, "Jane Doe", sizeof(person.name) - 1);
    
    // Set optional nested message
    strncpy(person.address.street, "456 Oak Ave", sizeof(person.address.street) - 1);
    strncpy(person.address.city, "Boston", sizeof(person.address.city) - 1);
    person.has_address = true;  // Important: set has_* flag
    
    pb_ostream_t ostream = pb_ostream_from_buffer(buffer, sizeof(buffer));
    
    if (!pb_encode(&ostream, example_Person_fields, &person)) {
        printf("Encoding failed: %s\n", PB_GET_ERROR(&ostream));
        return 1;
    }
    
    message_length = ostream.bytes_written;
    printf("Encoded %zu bytes\n", message_length);
    
    // --- DECODING ---
    example_Person decoded = example_Person_init_zero;
    
    pb_istream_t istream = pb_istream_from_buffer(buffer, message_length);
    
    if (!pb_decode(&istream, example_Person_fields, &decoded)) {
        printf("Decoding failed: %s\n", PB_GET_ERROR(&istream));
        return 1;
    }
    
    printf("Decoded values:\n");
    printf("  name: %s\n", decoded.name);
    if (decoded.has_address) {
        printf("  address:\n");
        printf("    street: %s\n", decoded.address.street);
        printf("    city: %s\n", decoded.address.city);
    } else {
        printf("  address: (not set)\n");
    }
    
    return 0;
}

Key points

When to use nested messages

Expected output

example.txt
Encoded 35 bytes
Encoded data: 0a 08 4a 6f 68 6e 20 44 6f 65 10 1e 1a 1b 0a 0b 31 32 33 20 4d 61 69 6e 20 53 74 12 08 4e 65 77 20 59 6f 72 6b 1a 03 55 53 41 
Decoded values:
  name: John Doe
  age: 30
  address:
    street: 123 Main St
    city: New York
    country: USA

Expected output (optional nested)

example.txt
Encoded 27 bytes
Decoded values:
  name: Jane Doe
  address:
    street: 456 Oak Ave
    city: Boston

More NanoPB posts


Check out similar posts by category: Embedded, C/C++, Protocol Buffers