|
|
**Table of Contents**
|
|
|
|
|
|
[[_TOC_]]
|
|
|
|
|
|
----
|
|
|
# Introduction
|
|
|
|
|
|
## Protocol Buffer
|
|
|
|
|
|
[Protocol Buffer](https://developers.google.com/protocol-buffers/) is a tool developed by Google for serializing structured data which can then be used to transmit data from one medium to another. For example, sensor readings captured from a microcontroller could be sent to the cloud for logging and diagnostics after encoding the data in a serialized format for easier byte-byte transmission.
|
|
|
|
|
|
Protocol buffer can serialize data from variety of languages such as Java, Python, Objective-C C++, Dart, Go, Ruby, and C# along with running on any platform.
|
|
|
|
|
|
## Nanopb
|
|
|
[Nanopb](https://jpa.kapsi.fi/nanopb/) provides a C based library for encoding and decoding messages in Google's Protocol Buffers format with suitable for microcontrollers with less RAM and code space availability. Google's Protocol Buffer Tool can generate data structures for C++ and not for C, thus making it less suitable for Microcontrollers.
|
|
|
|
|
|
# Installation and Setup
|
|
|
|
|
|
## Setting up Nanopb
|
|
|
To use Nanopb, protocol compiler needs to be installed. Follow the [installation instructions](https://github.com/protocolbuffers/protobuf#protocol-compiler-installation) here.
|
|
|
|
|
|
Protocol Buffers messages are defined in a `proto file` as follows:
|
|
|
```
|
|
|
//foo.proto
|
|
|
message Foo {
|
|
|
required int id = 1;
|
|
|
}
|
|
|
```
|
|
|
To use Nanopb Headers(.h) and source files(.c) generation from `.proto` file is required.
|
|
|
Headers(.h) and source files(.c) are generated using a python script provided by Nanopb.
|
|
|
```
|
|
|
python generator/nanopb_generator.py foo.proto
|
|
|
```
|
|
|
Follow the [instructions here](ihttps://github.com/nanopb/nanopb#generating-the-headers).
|
|
|
|
|
|
**Note**: Sibros repository Integrates Nanopb library in [Bazel](https://www.bazel.build/). Refer to [Sibros Nanopb Bazel]() article for more information.
|
|
|
|
|
|
# Implementation and Examples
|
|
|
|
|
|
## Protofiles:
|
|
|
Let's say you want to transmit a Can message in a serialized protobuf format. Then we need to define the can message and its contents inside a proto file as follows:
|
|
|
|
|
|
```
|
|
|
//can.proto
|
|
|
message can_message {
|
|
|
required int64 timestamp_ms = 1;
|
|
|
|
|
|
required int32 bus_id = 2; // The bus number which received the CAN message
|
|
|
required int32 message_id = 3; // Standard 11-bit
|
|
|
required int32 data_byte = 4; // Usually 8 bytes but modified for simplified example
|
|
|
}
|
|
|
```
|
|
|
In this file a message to be transmitted is defined using `message` keyword, followed by message name. There are 3 members inside the message with int64 and int32 datatypes respectively. The member of the message has to be initialized if `required` keyword is used. Alternatively `option` keyword can also be used. The `required int64 timestamp_ms = 1` the number after equality operator is the tag of the message member, which are used to match fields when serializing and deserializing the data.
|
|
|
For more information about `proto` message, visit the [documentation.](https://developers.google.com/protocol-buffers/docs/overview).
|
|
|
|
|
|
On running the python script to generate header and source files from proto file, we get structures:
|
|
|
```
|
|
|
i
|
|
|
```
|
|
|
|
|
|
## Simple Getting started Example
|
|
|
Todo
|
|
|
|
|
|
## Using Callback for Nanopb
|
|
|
Todo
|
|
|
|
|
|
# External Resources
|
|
|
* [Protocol Buffer](https://developers.google.com/protocol-buffers/)
|
|
|
* [Nanopb](https://jpa.kapsi.fi/nanopb/)
|
|
|
* [Nanopb Github](https://github.com/nanopb/nanopb)
|
|
|
* [Nanopb simple callback example](https://stackoverflow.com/questions/45979984/creating-callbacks-and-structs-for-repeated-field-in-a-protobuf-message-in-nanop) |
|
|
\ No newline at end of file |
|
|
[[_TOC_]] |
|
|
\ No newline at end of file |