diff options
author | Daniel Friesel <derf@finalrewind.org> | 2018-11-13 10:41:05 +0100 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2018-11-13 10:41:05 +0100 |
commit | 63d7c555e375a5a6b62809271111fec309ffe2e4 (patch) | |
tree | 694a0eef498cfbe3ca7ecc6348702277d1011f39 /src/app | |
parent | 9ce1f1ea246d0f3dcee78eaa483e2bf9b0af35f5 (diff) |
add NanoPB Protocol Buffers implementation
Diffstat (limited to 'src/app')
-rw-r--r-- | src/app/prototest/Makefile.inc | 25 | ||||
-rw-r--r-- | src/app/prototest/main.cc | 99 |
2 files changed, 83 insertions, 41 deletions
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 <pb.h> +#include "nanopb.pb.h" +#include <pb_encode.h> +#include <pb_decode.h> +#endif +#ifdef PROTOTEST_XDR #include "object/stdbuf.h" #include "object/xdrstream.h" #include "object/xdrinput.h" +#endif + +#include <stdint.h> +#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<std::uint8_t> 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<XDRStream> 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 |