Skip to content

Commit

Permalink
fixup! [protobuf] SCons integration of nanopb
Browse files Browse the repository at this point in the history
  • Loading branch information
salkinium committed Jul 22, 2022
1 parent 0d546ce commit d80421f
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 11 deletions.
61 changes: 57 additions & 4 deletions examples/nucleo_f429zi/nanopb/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
* 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;
Expand All @@ -37,13 +40,14 @@ int main()
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));

/* Fill in the lucky number */
message.lucky_number = 13;
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.
Expand All @@ -59,12 +63,61 @@ int main()

/* Now we are ready to decode the message. */
status = pb_decode(&stream, SimpleMessage_fields, &message);
(void) status;
modm_assert(status, "pb.dec", "Decoding SimpleMessage failed!");
modm_assert(message.lucky_number == 42, "lucky_number", "Incorrect SimpleMessage values!");
}

while (true) {
{
/* 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;
}
7 changes: 6 additions & 1 deletion examples/nucleo_f429zi/nanopb/protocol/complex.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
// -----------------------------------------------------------------------------
syntax = "proto3";

import "nanopb.proto";

message ComplexMessage {
int32 unlucky_number = 41;
string query = 1 [(nanopb).max_size = 40];
int32 unlucky_number = 2;
bool toggle = 3;
double value = 4;
}

2 changes: 1 addition & 1 deletion examples/nucleo_f429zi/nanopb/protocol/simple.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
syntax = "proto3";

message SimpleMessage {
int32 lucky_number = 42;
int32 lucky_number = 1;
}

5 changes: 3 additions & 2 deletions ext/nanopb/nanopb.lb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ See https://github.com/nanopb/nanopb.
## Build System Integration
You can optionally point the build system to the protofile definition path using
the `modm:nanopb:source` option.
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"
Expand Down
6 changes: 3 additions & 3 deletions tools/build_script_generator/scons/site_tools/nanopb.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ def _nanopb_proto_emitter(target, source, env):
for protofile in source:
targets.append(os.path.join(outpath, protofile.name.replace(".proto", ".pb.cpp")))
targets.append(os.path.join(outpath, protofile.name.replace(".proto", ".pb.hpp")))
basename = os.path.splitext(str(protofile))[0]
if os.path.exists(basename + ".options"):
source.append(basename + ".options")
options = os.path.splitext(str(protofile))[0] + ".options"
if os.path.exists(options):
env.Depends(target=protofile, dependency=options)

return targets, source

Expand Down

0 comments on commit d80421f

Please sign in to comment.