-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[protobuf] SCons integration of nanopb
Co-authored-by: Niklas Hauser <[email protected]>
- Loading branch information
Showing
14 changed files
with
403 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* Copyright (c) 2022, Lucas Moesch | ||
* Copyright (c) 2022, Niklas Hauser | ||
* | ||
* This file is part of the modm project. | ||
* | ||
* This file originated from the nanopb project. | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
#include <modm/board.hpp> | ||
#include <pb_encode.h> | ||
#include <pb_decode.h> | ||
#include <simple.pb.hpp> | ||
#include <complex.pb.hpp> | ||
|
||
int main() | ||
{ | ||
Board::initialize(); | ||
|
||
/* This is the buffer where we will store our message. */ | ||
uint8_t buffer[128]; | ||
size_t message_length; | ||
bool status; | ||
|
||
{ | ||
/* Allocate space on the stack to store the message data. | ||
* | ||
* Nanopb generates simple struct definitions for all the messages. | ||
* - check out the contents of simple.pb.h! | ||
* It is a good idea to always initialize your structures | ||
* so that you do not have garbage data from RAM in there. | ||
*/ | ||
SimpleMessage message = SimpleMessage_init_zero; | ||
|
||
/* Create a stream that will write to our buffer. */ | ||
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); | ||
|
||
/* Fill in the lucky number */ | ||
message.lucky_number = 42; | ||
|
||
/* Now we are ready to encode the message! */ | ||
status = pb_encode(&stream, SimpleMessage_fields, &message); | ||
modm_assert(status, "pb.enc", "Encoding SimpleMessage failed!"); | ||
message_length = stream.bytes_written; | ||
modm_assert(message_length, "pb.len", "Empty SimpleMessage buffer!"); | ||
} | ||
{ | ||
/* Now we could transmit the message over network, store it in a file or | ||
* wrap it to a pigeon's leg. | ||
*/ | ||
|
||
/* But because we are lazy, we will just decode it immediately. */ | ||
|
||
/* Allocate space for the decoded message. */ | ||
SimpleMessage message = SimpleMessage_init_zero; | ||
|
||
/* Create a stream that reads from the buffer. */ | ||
pb_istream_t stream = pb_istream_from_buffer(buffer, message_length); | ||
|
||
/* Now we are ready to decode the message. */ | ||
status = pb_decode(&stream, SimpleMessage_fields, &message); | ||
modm_assert(status, "pb.dec", "Decoding SimpleMessage failed!"); | ||
modm_assert(message.lucky_number == 42, "lucky_number", "Incorrect SimpleMessage values!"); | ||
} | ||
|
||
{ | ||
/* Allocate space on the stack to store the message data. | ||
* | ||
* Nanopb generates simple struct definitions for all the messages. | ||
* - check out the contents of simple.pb.h! | ||
* It is a good idea to always initialize your structures | ||
* so that you do not have garbage data from RAM in there. | ||
*/ | ||
ComplexMessage message = ComplexMessage_init_zero; | ||
|
||
/* Create a stream that will write to our buffer. */ | ||
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); | ||
|
||
/* Fill in the unlucky number */ | ||
std::strcpy(message.query, "Hello World"); | ||
message.unlucky_number = 13; | ||
message.toggle = true; | ||
message.value = 4.00012f; | ||
|
||
/* Now we are ready to encode the message! */ | ||
status = pb_encode(&stream, ComplexMessage_fields, &message); | ||
modm_assert(status, "pb.enc", "Encoding ComplexMessage failed!"); | ||
message_length = stream.bytes_written; | ||
modm_assert(message_length, "pb.len", "Empty ComplexMessage buffer!"); | ||
} | ||
{ | ||
/* Now we could transmit the message over network, store it in a file or | ||
* wrap it to a pigeon's leg. | ||
*/ | ||
|
||
/* But because we are lazy, we will just decode it immediately. */ | ||
|
||
/* Allocate space for the decoded message. */ | ||
ComplexMessage message = ComplexMessage_init_zero; | ||
|
||
/* Create a stream that reads from the buffer. */ | ||
pb_istream_t stream = pb_istream_from_buffer(buffer, message_length); | ||
|
||
/* Now we are ready to decode the message. */ | ||
status = pb_decode(&stream, ComplexMessage_fields, &message); | ||
modm_assert(status, "pb.dec", "Decoding ComplexMessage failed!"); | ||
|
||
modm_assert(!strcmp(message.query, "Hello World"), "query", "Incorrect ComplexMessage values!"); | ||
modm_assert(message.unlucky_number == 13, "unlucky_number", "Incorrect ComplexMessage values!"); | ||
modm_assert(message.toggle == true, "toggle", "Incorrect ComplexMessage values!"); | ||
modm_assert(int(message.value) == 4, "value", "Incorrect ComplexMessage values!"); | ||
} | ||
|
||
while (true) | ||
{ | ||
/* tumbleweed */ | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<library> | ||
<extends>modm:nucleo-f429zi</extends> | ||
<options> | ||
<option name="modm:build:build.path">../../../build/nucleo_f429zi/nanopb</option> | ||
<option name="modm:nanopb:sources">protocol/simple.proto,protocol/complex.proto</option> | ||
</options> | ||
<modules> | ||
<module>modm:build:scons</module> | ||
<module>modm:nanopb</module> | ||
</modules> | ||
</library> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// -*- coding: utf-8 -*- | ||
// | ||
// Copyright (c) 2022, Niklas Hauser | ||
// | ||
// This file is part of the modm project. | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
// ----------------------------------------------------------------------------- | ||
syntax = "proto3"; | ||
|
||
import "nanopb.proto"; | ||
|
||
message ComplexMessage { | ||
string query = 1 [(nanopb).max_size = 40]; | ||
int32 unlucky_number = 2; | ||
bool toggle = 3; | ||
double value = 4; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// -*- coding: utf-8 -*- | ||
// | ||
// Copyright (c) 2022, Lucas Moesch | ||
// | ||
// This file is part of the modm project. | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
// ----------------------------------------------------------------------------- | ||
syntax = "proto3"; | ||
|
||
message SimpleMessage { | ||
int32 lucky_number = 1; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (c) 2021, Lucas Moesch | ||
# Copyright (c) 2022, Niklas Hauser | ||
# | ||
# This file is part of the modm project. | ||
# | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
# ----------------------------------------------------------------------------- | ||
import os | ||
import subprocess | ||
import tempfile | ||
|
||
from pathlib import Path | ||
|
||
def init(module): | ||
module.name = ":nanopb" | ||
module.description = """ | ||
# Nanopb - Protocol Buffers for Embedded Systems | ||
Nanopb is a small code-size Protocol Buffers (protobuf) implementation in ansi | ||
C. It is especially suitable for use in microcontrollers, but fits any memory | ||
restricted system. | ||
See https://github.com/nanopb/nanopb. | ||
## Build System Integration | ||
You can optionally point the build system to multiple protofiles using a | ||
comma-separated list of paths in the `modm:nanopb:source` option. | ||
You can specify the output location using the `modm:nanopb:path` option. | ||
See the `modm:build` submodules for further details. | ||
!!! bug "Currently only with SCons support" | ||
Only the `modm:build:scons` module currently supports this feature. | ||
""" | ||
|
||
def prepare(module, options): | ||
module.add_set_option( | ||
PathOption(name="sources", absolute=True, empty_ok=True, | ||
description="Comma-separated list of paths to protofiles."), | ||
default="") | ||
module.add_option( | ||
PathOption(name="path", default="generated/nanopb", absolute=True, | ||
description="Path to the generated messages folder")) | ||
return True | ||
|
||
def build(env): | ||
env.collect(":build:path.include", "modm/ext/nanopb") | ||
env.outbasepath = "modm/ext/nanopb" | ||
|
||
env.copy("nanopb/pb_common.c", dest="pb_common.c") | ||
env.copy("nanopb/pb_common.h", dest="pb_common.h") | ||
env.copy("nanopb/pb_decode.c", dest="pb_decode.c") | ||
env.copy("nanopb/pb_decode.h", dest="pb_decode.h") | ||
env.copy("nanopb/pb_encode.c", dest="pb_encode.c") | ||
env.copy("nanopb/pb_encode.h", dest="pb_encode.h") | ||
env.copy("nanopb/pb.h", dest="pb.h") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.