From f52bbd084c517c16d7d07f3b423c38fc9b86c9ab Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Sun, 16 Sep 2018 14:49:05 +0200 Subject: [PATCH] tests/pkg_nanopb: initial commit --- tests/pkg_nanopb/Makefile | 5 ++ tests/pkg_nanopb/README.md | 10 +++ tests/pkg_nanopb/main.c | 113 +++++++++++++++++++++++++++++++ tests/pkg_nanopb/simple.proto | 8 +++ tests/pkg_nanopb/tests/01-run.py | 12 ++++ 5 files changed, 148 insertions(+) create mode 100644 tests/pkg_nanopb/Makefile create mode 100644 tests/pkg_nanopb/README.md create mode 100644 tests/pkg_nanopb/main.c create mode 100644 tests/pkg_nanopb/simple.proto create mode 100755 tests/pkg_nanopb/tests/01-run.py diff --git a/tests/pkg_nanopb/Makefile b/tests/pkg_nanopb/Makefile new file mode 100644 index 0000000000..db58d06f4c --- /dev/null +++ b/tests/pkg_nanopb/Makefile @@ -0,0 +1,5 @@ +include ../Makefile.tests_common + +USEPKG += nanopb + +include $(RIOTBASE)/Makefile.include diff --git a/tests/pkg_nanopb/README.md b/tests/pkg_nanopb/README.md new file mode 100644 index 0000000000..ba4952af25 --- /dev/null +++ b/tests/pkg_nanopb/README.md @@ -0,0 +1,10 @@ +# Introduction + +This is a test application for the nanoPb library. +The library provides a Google Protocol Buffers encoder / decoder. + +# Prerequisites + +Install the protobuf compiler and the protobuf python bindings. +On Debian/ubuntu, the corresponding packages are `protobuf-compiler` and +`python-protobuf`. On Arch, it is `protobuf` and `python-protobuf`. diff --git a/tests/pkg_nanopb/main.c b/tests/pkg_nanopb/main.c new file mode 100644 index 0000000000..edbf037a72 --- /dev/null +++ b/tests/pkg_nanopb/main.c @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2011 Petteri Aimonen + * + * This software is provided 'as-is', without any express or + * implied warranty. In no event will the authors be held liable + * for any damages arising from the use of this software. + * + * Permission is granted to anyone to use this software for any + * purpose, including commercial applications, and to alter it and + * redistribute it freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you + * must not claim that you wrote the original software. If you use + * this software in a product, an acknowledgment in the product + * documentation would be appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and + * must not be misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. + * + * This file has been taken from + * https://github.com/nanopb/nanopb/tree/master/examples/simple + * (commit 5866b34) and changed to integrate well with RIOT (and comply to + * RIOT's coding conventions). + * + * + */ + +/** + * @ingroup tests + * @{ + * + * @file + * @brief nanopb test application + * + * @author Petteri Aimonen + * @author Kaspar Schleiser (RIOT adaption) + * + * @} + */ + +#include +#include +#include +#include "simple.pb.h" + +int main(void) +{ + /* This is the buffer where we will store our message. */ + uint8_t buffer[128]; + size_t message_length; + bool status; + + /* Encode our message */ + { + /* 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 = 13; + + /* Now we are ready to encode the message! */ + status = pb_encode(&stream, SimpleMessage_fields, &message); + message_length = stream.bytes_written; + + /* Then just check for any errors.. */ + if (!status) + { + printf("Encoding failed: %s\n", PB_GET_ERROR(&stream)); + return 1; + } + } + + /* 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); + + /* Check for errors... */ + if (!status) + { + printf("Decoding failed: %s\n", PB_GET_ERROR(&stream)); + return 1; + } + + /* Print the data contained in the message. */ + printf("Your lucky number was %d!\n", (int)message.lucky_number); + } + + return 0; +} diff --git a/tests/pkg_nanopb/simple.proto b/tests/pkg_nanopb/simple.proto new file mode 100644 index 0000000000..f6bc03d976 --- /dev/null +++ b/tests/pkg_nanopb/simple.proto @@ -0,0 +1,8 @@ +// A very simple protocol definition, consisting of only +// one message. + +syntax = "proto2"; + +message SimpleMessage { + required int32 lucky_number = 1; +} diff --git a/tests/pkg_nanopb/tests/01-run.py b/tests/pkg_nanopb/tests/01-run.py new file mode 100755 index 0000000000..a5763e873f --- /dev/null +++ b/tests/pkg_nanopb/tests/01-run.py @@ -0,0 +1,12 @@ +#!/usr/bin/env python3 + +import sys +from testrunner import run + + +def testfunc(child): + child.expect_exact('Your lucky number was 13!') + + +if __name__ == "__main__": + sys.exit(run(testfunc))