From 63d7c555e375a5a6b62809271111fec309ffe2e4 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Tue, 13 Nov 2018 10:41:05 +0100 Subject: add NanoPB Protocol Buffers implementation --- src/app/prototest/Makefile.inc | 25 ++++++++++- src/app/prototest/main.cc | 99 +++++++++++++++++++++++++----------------- 2 files changed, 83 insertions(+), 41 deletions(-) (limited to 'src/app') diff --git a/src/app/prototest/Makefile.inc b/src/app/prototest/Makefile.inc index ae7794e..0940fd2 100644 --- a/src/app/prototest/Makefile.inc +++ b/src/app/prototest/Makefile.inc @@ -1,2 +1,25 @@ loop ?= 1 -TARGETS += src/os/object/xdrstream.cc src/os/object/xdrinput.cc + +ifeq (${prototest_arduinojson}, 1) + COMMON_FLAGS += -DPROTOTEST_ARDUINOJSON +endif + +ifeq (${prototest_modernjson}, 1) + COMMON_FLAGS += -DPROTOTEST_MODERNJSON +endif + +ifeq (${prototest_nanopb}, 1) + COMMON_FLAGS += -DPROTOTEST_NANOPB + TARGETS += src/app/prototest/nanopb.pb.cc src/lib/nanopb/pb_common.cc + TARGETS += src/lib/nanopb/pb_decode.cc src/lib/nanopb/pb_encode.cc + INCLUDES += -Iinclude/lib/nanopb +endif + +ifeq (${prototest_xdr}, 1) + COMMON_FLAGS += -DPROTOTEST_XDR + TARGETS += src/os/object/xdrstream.cc src/os/object/xdrinput.cc +endif + +%.pb.cc: %.proto + protoc --plugin=protoc-gen-nanopb=${HOME}/var/ess/protocol-modeling/nanopb/generator/protoc-gen-nanopb --nanopb_out=. src/app/prototest/nanopb.proto + mv src/app/prototest/nanopb.pb.c src/app/prototest/nanopb.pb.cc diff --git a/src/app/prototest/main.cc b/src/app/prototest/main.cc index 43c24f9..fe33ae0 100644 --- a/src/app/prototest/main.cc +++ b/src/app/prototest/main.cc @@ -1,13 +1,30 @@ #include "arch.h" #include "driver/gpio.h" #include "driver/stdout.h" + +#ifdef PROTOTEST_ARDUINOJSON #include "lib/ArduinoJson.h" +#endif +#ifdef PROTOTEST_MODERNJSON #include "lib/modernjson/json.h" +#endif +#ifdef PROTOTEST_NANOPB +#include +#include "nanopb.pb.h" +#include +#include +#endif +#ifdef PROTOTEST_XDR #include "object/stdbuf.h" #include "object/xdrstream.h" #include "object/xdrinput.h" +#endif + +#include +#ifdef PROTOTEST_XDR char buf[256]; +#endif // TODOs // @@ -25,55 +42,19 @@ char buf[256]; void loop(void) { - static unsigned int ts = 0; - char json[] = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}"; - ArduinoJson::StaticJsonBuffer<200> jsonBuffer; - ArduinoJson::JsonObject& root = jsonBuffer.parseObject(json); - const char *sensor = root["sensor"]; - kout << "sensor: " << sensor << endl; + static uint16_t ts = 0; /* - * nlohmann modernjson serialization + * XDR */ - nlohmann::json js1; - js1["sensor"] = "gps"; - js1["time"] = ts; - js1["data"] = {48.756080, 2.302038}; - kout << js1.dump() << endl; - - nlohmann::json js2 = { - {"sensor", "gps"}, - {"time", ts}, - {"data", {48.756080, 2.302038} } - }; - kout << js2.dump() << endl; - - nlohmann::json j = R"({"compact": true, "schema": 0})"_json; - j["ts"] = ts; - std::vector v_cbor = nlohmann::json::to_cbor(j); - - kout << "CBOR vector is " << hex; - for (unsigned int i = 0; i < v_cbor.size(); i++) { - kout << v_cbor[i] << " "; - } - kout << endl; - - kout << "manual JSON: {\"sensor\":\"gps\",\"time\":" << dec << ts; - kout << ",\"data\":[" << 48.756080 << "," << 2.302038 << "]}" << endl; - - /* - * nlohmann modernjson deserialization - */ - - auto jd1 = R"({"compact": true, "schema": 0})"_json; - +#ifdef PROTOTEST_XDR BufferOutput foostream(buf); XDRInput input(buf); char test[] = "Obai World!"; - foostream << 123 << -2 << ts << 0 << 4294967296 << 0; + foostream << (uint32_t)123 << (int16_t)-2 << ts << (uint16_t)0 << (uint64_t)4294967296 << (uint16_t)0; foostream.setNextArrayLen(3); foostream << fixed << "Hai"; foostream.setNextArrayLen(sizeof(test)); @@ -97,6 +78,44 @@ void loop(void) uint32_t len = input.get_opaque_length(); kout << ", " << input.get_opaque(len); kout << endl; +#endif + + /* + * ArduinoJSON + */ + +#ifdef PROTOTEST_ARDUINOJSON + char json[] = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}"; + ArduinoJson::StaticJsonBuffer<200> jsonBuffer; + ArduinoJson::JsonObject& root = jsonBuffer.parseObject(json); + const char *sensor = root["sensor"]; + kout << "sensor: " << sensor << endl; +#endif + + /* + * NanoPB + */ + +#ifdef PROTOTEST_NANOPB + + uint8_t buf[128]; + size_t len; + bool status; + + { + TestMessage msg = TestMessage_init_zero; + pb_ostream_t stream = pb_ostream_from_buffer(buf, sizeof(buf)); + msg.number = 423; + status = pb_encode(&stream, TestMessage_fields, &msg); + len = stream.bytes_written; + kout << len << " bytes written" << endl; + } + +#endif + + /* + * Common + */ gpio.led_toggle(1); #ifdef TIMER_S -- cgit v1.2.3