Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Sibros internal repository integrates Nanopb library with Bazel. Custom cc_nanopb_library Bazel rule is used to auto-generate C strucutres structures from .proto file in the form of an header(.h) file. Refer to the Bazel BUILD file in the Nanopb examples to generate the headers(.h) and source files(.c).

...

Implementation and Examples

...

Note: The examples should work out-of-the-box (if bazel targets are correctly defined by the user) for Sibro's Employees since all Bazel rules and BUILD files are setup. However, others need to create a Makefile to build and link all the source files (and also Nanopb library) for creating a binary target. I will add a Makefile for all the examples soon.

...

In this example, a single CAN message with a single byte of data is serialized to protobuf format using Nanopb. The source files for this example can be found here

Proto Files

Proto files are used to structure the protocol buffer data using protocol buffer language. We need to define the CAN message and its contents inside a proto file as follows:

...

Refer to the complete Nanopb generated header here.

Encoding data using Nanopb

...

proto_can_message_fields argument to the pb_encode() function is an auto-generated Struct field which will encoding specification for Nanopb. Visit [Nanopb Docs]](https://github.com/nanopb/nanopb/blob/master/pb.h#L315 ) for more information on proto_can_message_fields. Also visit the auto-generated Nanopb header file for reference.

At this point data bytes are encoded in a serialized format and stored in the serial_data_packet_s memory structure.Refer the source files for more context.

Decoding

To verify the integrity of encoded data, we decode the encoded packet. First, we create a stream that reads from the encoded buffer.

...

Code Block
  can_msg->message_id = proto_msg->message_id;
  can_msg->bus_id = proto_msg->bus_id;
  can_msg->timestamp_ms = proto_msg->timestamp_ms;
  can_msg->data = proto_msg->data_byte;

Example - 2 : Using Callbacks for Nanopb

...

A CAN message usually has a 8 byte data field (if using CAN-FD the data field is 64 bytes). Thus we modify the CAN message in the proto file such that the CAN proto message can encode 8 bytes of data at once.

...

Example - 3 : Nested Callback Structures

...

Consider a scenario when we need to send multiple CAN frames, each frame containing multiple data bytes and some other information (for eg. software version information) to the cloud in the form of encoded data bytes.

...