[roll third_party/pigweed] pw_protobuf: Add helper for map entry write

Expose low level APIs in pw_protobuf for estimating field size and
writing length-delimited field key and length prefix. Use them to
implement a helper function in pw_software_update for writing proto
map<string, bytes> entries.

Context: UpdateBundle needs to construct the following Manifest
proto message from blob storage:

message Manifest {
  optional SnapshotMetadata snapshot_metadata = 1;
  map<string, TargetsMetadata> targets_metadata = 2;

For wire format generation, it is essentially equivalent to the
following definition with a nested `Entry` message.

message Entry {
    string key = 1;
    bytes value = 2;

message Manifest {
    optional bytes snapshot_metadata = 1;
    repeated Entry targets_metadata = 2;

Although protobuf::StreamEncoder has capability for nested message
encoding, it requires a scratch buffer that shall be at least the
largest sub-message size. In this case, it will be largest
target metadata in the update software bundle, which however, can be
fairly large and difficult to estimate. To avoid the issue, the CL
takes an approach to construct the message from lower level.
Specifically, the CL constructs the delimited field key and length
prefix for `Entry` on its own and write to output, then followed by
writing a regular string field of `key` and bytes field of `value` via
the normal StreamEncoder approach.

Original-Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/59681

third_party/pigweed Rolled-Commits: 03e1b265752f327..931f4d54ab96caa
Roller-URL: https://ci.chromium.org/b/8836171134356305969
Cq-Cl-Tag: roller-builder:pigweed-sample-project-roller
Cq-Cl-Tag: roller-bid:8836171134356305969
CQ-Do-Not-Cancel-Tryjobs: true
Change-Id: I7a948abf33ef6556681f174c4d382faf9c618f52
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/sample_project/+/60760
Bot-Commit: Pigweed Integration Roller <pigweed-integration-roller@pigweed.google.com.iam.gserviceaccount.com>
Commit-Queue: Pigweed Integration Roller <pigweed-integration-roller@pigweed.google.com.iam.gserviceaccount.com>
1 file changed
tree: 1bf38c64ecdf0b44a29e140f88e6c8397aba650d
  1. .gitattributes
  2. .gitignore
  3. .gitmodules
  4. .gn
  5. BUILD.gn
  8. README.md
  9. activate.bat
  10. banner.txt
  11. bootstrap.bat
  12. bootstrap.sh
  13. build_overrides/
  14. env_setup.json
  15. images/
  16. navbar.md
  17. source/
  18. targets/
  19. third_party/
  20. tools/
  21. workshop/

Pigweed Sample Project

This repository outlines the recommended way of using Pigweed in a new or existing project. Feel free to fork this repository, or read it as a reference.

For more information see the Pigweed Getting started guide.

Check back for more complex examples and features coming soon!

Repository setup

Clone this repo with --recursive to get all required submodules.

git clone --recursive https://pigweed.googlesource.com/pigweed/sample_project

This will pull the Pigweed source repository into third_party/pigweed. If you already cloned but forgot to --recursive run git submodule update --init to pull all submodules.

Environment Setup

After cloning the build tools will need to be installed with the bootstrap scripts. This is only required after the initial clone or updating Pigweed.



Linux & Mac

source bootstrap.sh

bootstrap.bat and bootstrap.sh call the respective Pigweed bootstrap scripts. Feel free to modify them to set up your development environment for the first time.

After the initial bootstrap, use the activate scripts to setup your shell for development.



Linux & Mac

source activate.sh


Make the project yours with your own banner! Edit banner.txt as desired :-D.


Generate the build files with gn gen out once, unless the build configuration has changed. Then, use ninja to build everything with ninja -C out.

The sample project examples have dependencies, each with some required build arguments. See the Arduino, and RPC sections for more setup information. The build arguments can be set with gn args out.

Pigweed's File System Watcher

Speed up iteration running pw watch on a new terminal window. This utility builds targets and runs tests when their files are modified. See the pw_watch documentation for more information.


Sample presubmit checks are included in tools/presubmit_checks.py. To install them run python tools/presubmit_checks.py --install. See the pw_presubmit module documentation for more information.

Sample Application

The sample application in source/main.cc uses the sample module simple_counter. Look at source/BUILD.gn and source/simple_counter/BUILD.gn to see how these are built respectively. The key part is in the root BUILD.gn, which creates the host target using the host toolchain. A toolchain is required for each target.

Build the project and run the application.


Sample Test

The simple_counter module has tests defined in source/simple_counter_tests.cc. Look at source/simple_counter/BUILD.gn for an example of how a test is defined. The root BUILD.gn groups all the host tests together.

Build the project and run the tests.


Arduino Example

1. Install an Arduino Core

To build for Arduino boards you must install a core. At this time only the Teensyduino core is supported.

Check the Pigweed pw_arduino_build module documentation for detailed installation instructions. Cores should be installed into third_party/piwgweed/third_party/arduino.

Run this to install the Teensy core:

arduino_builder install-core --prefix ./third_party/pigweed/third_party/arduino/cores/ --core-name teensy

2. Build

To build for a Teensy 4.0 board run the following.

Linux & Mac

gn gen out --args='
  pw_arduino_build_PACKAGE_NAME = "teensy/avr"
ninja -C out


Run gn gen out which will open a text editor. Paste in the following, save and close the editor.

pw_arduino_build_PACKAGE_NAME = "teensy/avr"

Where pw_arduino_build_BOARD= is one of:

  • "teensy31" - Teensy 3.2 / 3.1
  • "teensy35" - Teensy 3.5
  • "teensy36" - Teensy 3.6
  • "teensy40" - Teensy 4.0
  • "teensy41" - Teensy 4.1

3. Run a Test

Manual Test

Tests can be manually flashed an run with the arduino_unit_test_runner and the .elf file.

arduino_unit_test_runner --verbose \
    --config-file out/arduino_debug/gen/arduino_builder_config.json \
    --upload-tool teensyloader \

Single line:

arduino_unit_test_runner --verbose --config-file out/arduino_debug/gen/arduino_builder_config.json --upload-tool teensyloader out/arduino_debug_tests/obj/source/simple_counter/test/simple_counter_test.elf

Flash Only

A given binary can be flashed without checking for test pass or fail messages with the --flash-only option:

arduino_unit_test_runner --verbose \
  --config out/arduino_debug/gen/arduino_builder_config.json \
  --upload-tool teensyloader \
  --flash-only \

Single line:

arduino_unit_test_runner --verbose --config out/arduino_debug/gen/arduino_builder_config.json --upload-tool teensyloader --flash-only out/arduino_debug_tests/obj/source/simple_counter/test/simple_counter_test.elf

Unit Test Server

If you would like to use the unit test server to automatically run your tests you must set the pw_arduino_use_test_server=true build arg and startup the test server. Then in a second window start the pw watch command.

  1. Start the test server in it's own terminal window.

    gn gen out --args='
      pw_arduino_build_PACKAGE_NAME = "teensy/avr"
    arduino_test_server --verbose --config-file ./out/arduino_debug/gen/arduino_builder_config.json
  2. Start pw watch in a separate terminal.

    pw watch

For additional details check the Pigweed arduino_builder documentation in:

Tokenized Logging

Log entries in the sample app are tokenized to save binary space. The included tokens database, source/tokenizer_database.csv, is updated on each build. See the Pigweed pw_tokenizer for more information.

Optionally, the database can be created manually using the binary or the .elf file.

python -m pw_tokenizer.database create \
    --database source/tokenizer_database.csv \

Single line:

python -m pw_tokenizer.database create --database source/tokenizer_database.csv out/host_clang_debug/obj/source/bin/hello_world

Running the app shows log entries similiar to $kgjLdg==. These can be saved to a file and then detokenized.

./out/host_clang_debug/obj/source/bin/hello_world > log.txt
python -m pw_tokenizer.detokenize base64 source/tokenizer_database.csv -i log.txt

Or can be detokenized in realtime.

./out/host_clang_debug/obj/source/bin/hello_world | python \
    -m pw_tokenizer.detokenize base64 source/tokenizer_database.csv

RPC Example

The sample project uses nanopb for its pw_rpc dependency. The nanopb repo is conveniently included as a git submodule. This installation can be overridden following the instructions in third_party/pigweed/third_party/nanopb/BUILD.gn. Then set dir_pw_third_party_nanopb to the new installation location when building. For example:

gn gen out --args='dir_pw_third_party_nanopb="third_party/nanopb"'

The sample application registers the EchoService, which echoes any RPC message data sent to it. To test it out build for and flash the desired board, then run:

python third_party/pigweed/pw_hdlc/rpc_example/example_script.py --device /dev/ttyACM0 --baud 115200

At the time of writing, the example_script.py does not parse log statements if they are not framed in the HDLC protocol used by RPC. There is ongoing work on an RPC log service that will handle sending logs in HDLC frames to an RPC channel. In addition, the sample application uses tokenized logging, which means that logs need to be detokenized after the RPC HDLC frames are decoded on the host side. To do that flash the hello_word application and use two terminals to parse RPCs and detokenize logs respectively.

Terminal 1: receive RPCs.

source activate.sh
python -m pw_hdlc.rpc_console \
       -o logfile.txt \
       -d /dev/ttyACM0 \

Test Echo RPC:


Terminal 2: decode the logfile.

source activate.sh
tail -f logfile.txt | python -m pw_tokenizer.detokenize \
    base64 source/tokenizer_database.csv