Commit 2cc7578d authored by Birte Kristina Friesel's avatar Birte Kristina Friesel
Browse files

prototest: switch to full autogeneration

parent 32ce0a8d
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -30,6 +30,10 @@ ifeq (${prototest_capnproto_c}, 1)
	INCLUDES += -Iinclude/lib/capnp-c
endif

ifeq (${prototest_manualjson}, 1)
	COMMON_FLAGS += -DPROTOTEST_MANUALJSON
endif

ifeq (${prototest_modernjson}, 1)
	COMMON_FLAGS += -DPROTOTEST_MODERNJSON
	ostream = 1
+4 −373
Original line number Diff line number Diff line
@@ -10,6 +10,9 @@
#include <capnp_c.h>
#include "capnp_c_bench.capnp.h"
#endif
#ifdef PROTOTEST_MANUALJSON
#include "object/stdbuf.h"
#endif
#ifdef PROTOTEST_MODERNJSON
#include "lib/modernjson/json.h"
#endif
@@ -37,26 +40,6 @@
#include "prototest_global.cc.inc"
#endif

#ifdef PROTOTEST_ARDUINOJSON
char buf[256];
#endif
#ifdef PROTOTEST_XDR
char buf[256];
#endif

#ifdef PROTOTEST_NANOPB
bool encode_hurr(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
{
if (!pb_encode_tag_for_field(stream, field)) return false;
return pb_encode_string(stream, (uint8_t*)"durr", 4);
}
bool encode_sensor(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
{
if (!pb_encode_tag_for_field(stream, field)) return false;
return pb_encode_string(stream, (uint8_t*)"gps", 3);
}
#endif

// TODOs
//
// Code -> JSON
@@ -74,364 +57,12 @@ return pb_encode_string(stream, (uint8_t*)"gps", 3);
void loop(void)
{
	static uint16_t ts = 0;
	uint32_t tmp;
	ts++;

#ifdef PROTOTEST_INCLUDE_LOCAL
#include "prototest_local.cc.inc"
#endif

	/*
	 * XDR
	 */


kout << "{";
kout << "\"data\":[";
kout << "48.75608" << ",";
kout << "2.302038";
kout << "]" << ",";
kout << "\"nesting\":[";
kout << "[";
kout << "1";
kout << "]" << ",";
kout << "[";
kout << "2" << ",";
kout << "2";
kout << "]" << ",";
kout << "{";
kout << "\"foo\":{";
kout << "\"hurr\":\"durr\"";
kout << "}";
kout << "}";
kout << "]" << ",";
kout << "\"sensor\":\"gps\"" << ",";
kout << "\"time\":" << dec << ts;
kout << "}" << endl;

#ifdef PROTOTEST_CAPNPROTO_C
	uint8_t buf[1024];
	for (unsigned int i = 0; i < sizeof(buf); i++) {
		buf[i] = 0;
	}

struct capn c;
capn_init_malloc(&c);
capn_ptr cr = capn_root(&c);
struct capn_segment *cs = cr.seg;

struct Benchmark benchmark;
benchmark.data = capn_new_list64(cs, 2);
capn_set64(benchmark.data, 0, capn_from_f64(48.756080));
capn_set64(benchmark.data, 1, capn_from_f64(2.302038));
benchmark.nesting = new_Benchmark_Nesting(cs);
struct Benchmark_Nesting nesting;
nesting.foo = new_Benchmark_Nesting_Foo(cs);
struct Benchmark_Nesting_Foo foo;
capn_text hurr_text;
hurr_text.len = 4;
hurr_text.str = "durr";
hurr_text.seg = NULL;
foo.hurr = hurr_text;

foo.qwop = 9001;

write_Benchmark_Nesting_Foo(&foo, nesting.foo);
write_Benchmark_Nesting(&nesting, benchmark.nesting);
capn_text sensor_text;
sensor_text.len = 3;
sensor_text.str = "gps";
sensor_text.seg = NULL;
benchmark.sensor = sensor_text;

benchmark.time = ts;

Benchmark_ptr benchmark_ptr = new_Benchmark(cs);
write_Benchmark(&benchmark, benchmark_ptr);
capn_setp(cr, 0, benchmark_ptr.p);


	unsigned int sz = capn_write_mem(&c, buf, sizeof(buf), 0 /* packed */);
	capn_free(&c);

	kout << "capnproto is " << hex;
	for (unsigned int i = 0; i < sz; i++) {
		kout << buf[i];
	}
	kout << endl;
#endif

#ifdef PROTOTEST_XDR
	BufferOutput<XDRStream> xdrstream(buf);
	xdrstream.setNextArrayLen(2);
	xdrstream << 48.75608;
	xdrstream << 2.302038;
	xdrstream.setNextArrayLen(3);
	xdrstream.setNextArrayLen(1);
	xdrstream << 1;
	xdrstream.setNextArrayLen(2);
	xdrstream << 2;
	xdrstream << 2;
	xdrstream.setNextArrayLen(4);
	xdrstream << variable << "durr";
	xdrstream.setNextArrayLen(3);
	xdrstream << variable << "gps";
	xdrstream << ts;

	kout << "xdrstream is " << hex;
	for (unsigned int i = 0; i < 64; i += 4) {
		kout << (unsigned char)buf[i] << (unsigned char)buf[i+1];
		kout << (unsigned char)buf[i+2] << (unsigned char)buf[i+3] << " ";
	}
	kout << endl;

	XDRInput input(buf);
	kout << dec;
	kout << "foostream = " << input.get_uint32() << " = " << 123;
	kout << ", " << input.get_int32() << " = " << -2;
	kout << ", " << input.get_uint32() << " = " << ts;
	kout << ", " << input.get_uint32();
	kout << ", " << input.get_uint64();
	kout << ", " << input.get_uint32();
	kout << ", " << input.get_opaque(3);
	uint32_t len = input.get_opaque_length();
	kout << ", " << input.get_opaque(len);
	kout << endl;
#endif

	/*
	 * ArduinoJSON
	 */

#ifdef PROTOTEST_ARDUINOJSON
	for (unsigned int i = 0; i < 128; i++) {
		buf[i] = 0;
	}

	{
		counter.start();
		ArduinoJson::StaticJsonBuffer<200> jsonBuffer;

		ArduinoJson::JsonObject& root = jsonBuffer.createObject();
		ArduinoJson::JsonArray& rootl = root.createNestedArray("data");
		rootl.add(48.75608);
		rootl.add(2.302038);
		ArduinoJson::JsonArray& rootl_ = root.createNestedArray("nesting");
		ArduinoJson::JsonArray& rootl_l = rootl_.createNestedArray();
		rootl_l.add(1);
		ArduinoJson::JsonArray& rootl_l_ = rootl_.createNestedArray();
		rootl_l_.add(2);
		rootl_l_.add(2);
		ArduinoJson::JsonObject& rootl_o = rootl_.createNestedObject();
		ArduinoJson::JsonObject& rootl_oo = rootl_o.createNestedObject("foo");
		rootl_oo["hurr"] = "durr";
		root["sensor"] = "gps";
		root["time"] = ts;


/*
		ArduinoJson::JsonObject& root = jsonBuffer.createObject();
		root["sensor"] = "gps";
		root["time"] = ts;
		ArduinoJson::JsonArray& data = root.createNestedArray("data");
		data.add(48.756080);
		data.add(2.302038);
		*/

		root.printTo(buf);
		tmp = counter.stop();
		kout << "ArduinoJSON serialization took " << tmp << " >" << counter.overflowed << " cycles" << endl;
		kout << "buf is " << buf << endl;
	}

	{
		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

	/*
	 * ModernJSON
	 */

#ifdef PROTOTEST_MODERNJSON
	nlohmann::json js1;
	js1["data"] = {48.756080, 2.302038};
	js1["nesting"][0] = {1};
	js1["nesting"][1] = {2, 2};
	js1["nesting"][2]["foo"]["hurr"] = "durr";
	js1["sensor"] = "gps";
	js1["time"] = ts;
	kout << "string:" << js1.dump() << endl;

	std::vector<std::uint8_t> v_cbor = nlohmann::json::to_cbor(js1);
	kout << "CBOR:" << hex;
	for (unsigned int i = 0; i < v_cbor.size(); i++) {
		kout << v_cbor[i] << " ";
	}
	kout << endl;

	std::vector<std::uint8_t> v_msgpack = nlohmann::json::to_msgpack(js1);
	kout << "MsgPack:" << hex;
	for (unsigned int i = 0; i < v_msgpack.size(); i++) {
		kout << v_msgpack[i] << " ";
	}
	kout << endl;

	std::vector<std::uint8_t> v_ubjson = nlohmann::json::to_ubjson(js1);
	kout << "UBJSON:" << hex;
	for (unsigned int i = 0; i < v_ubjson.size(); i++) {
		kout << v_ubjson[i] << " ";
	}
	kout << endl;
	kout << "UBJSON: " << hex;
	for (unsigned int i = 0; i < v_ubjson.size(); i++) {
		kout << (char)v_ubjson[i] << " ";
	}
	kout << endl;
#endif

	/*
	 * NanoPB
	 */

#ifdef PROTOTEST_NANOPB

	uint8_t buf[128];
	size_t len;
	bool status;

	{
		Benchmark msg = Benchmark_init_zero;
		pb_ostream_t stream = pb_ostream_from_buffer(buf, sizeof(buf));
		msg.data[0] = 48.75608;
		msg.data[1] = 2.302038;
		msg.nesting.foo.hurr.funcs.encode = encode_hurr;
		msg.nesting.foo.qwop = 9001;
		msg.sensor.funcs.encode = encode_sensor;
		msg.time = ts;
		status = pb_encode(&stream, Benchmark_fields, &msg);
		len = stream.bytes_written;
		kout << len << " bytes written" << endl;
	}

#endif

	/*
	 * MPack
	 */

#ifdef PROTOTEST_MPACK
	char buf[128];
	for (unsigned int i = 0; i < 128; i++) {
		buf[i] = 0;
	}
	mpack_writer_t writer;
	mpack_writer_init(&writer, buf, sizeof(buf));
	mpack_start_map(&writer, 4);
	mpack_write_cstr(&writer, "data");
	mpack_start_array(&writer, 2);
	mpack_write_float(&writer, 48.756080);
	mpack_write_float(&writer, 2.302038);
	mpack_finish_array(&writer);
	mpack_write_cstr(&writer, "nesting");
	mpack_start_map(&writer, 1);
	mpack_write_cstr(&writer, "foo");
	mpack_start_map(&writer, 2);
	mpack_write_cstr(&writer, "hurr");
	mpack_write_cstr(&writer, "durr");
	mpack_write_cstr(&writer, "qwop");
	mpack_write_uint(&writer, 9001);
	mpack_finish_map(&writer);
	mpack_finish_map(&writer);
	mpack_write_cstr(&writer, "sensor");
	mpack_write_cstr(&writer, "gps");
	mpack_write_cstr(&writer, "time");
	mpack_write_uint(&writer, ts);
	mpack_finish_map(&writer);

	if (mpack_writer_destroy(&writer) != mpack_ok) {
		kout << "Encoding failed" << endl;
	}
	kout << "mpack is " << hex;
	for (unsigned int i = 0; i < 128; i++) {
		kout << (uint8_t)buf[i];
	}
	kout << endl;

#endif

	/*
	 * UBJSON
	 */

#ifdef PROTOTEST_UBJSON

	uint8_t buf[192];
	for (unsigned int i = 0; i < sizeof(buf); i++) {
		buf[i] = 0;
	}

	ubjw_context_t* ctx = ubjw_open_memory(buf, buf + sizeof(buf));
	ubjw_begin_object(ctx, UBJ_MIXED, 0);
	ubjw_write_key(ctx, "data");

	ubjw_begin_array(ctx, UBJ_FLOAT32, 0);
	ubjw_write_float64(ctx, 48.75608);
	ubjw_write_float64(ctx, 2.302038);
	ubjw_end(ctx);

	ubjw_write_key(ctx, "nesting");
	ubjw_begin_array(ctx, UBJ_MIXED, 0);

	ubjw_begin_array(ctx, UBJ_MIXED, 0);
	ubjw_write_integer(ctx, 1);
	ubjw_end(ctx);

	ubjw_begin_array(ctx, UBJ_MIXED, 0);
	ubjw_write_integer(ctx, 2);
	ubjw_write_integer(ctx, 2);
	ubjw_end(ctx);

	ubjw_begin_object(ctx, UBJ_MIXED, 0);
	ubjw_write_key(ctx, "foo");

	ubjw_begin_object(ctx, UBJ_MIXED, 0);
	ubjw_write_key(ctx, "hurr");
	ubjw_write_string(ctx, "durr");
	ubjw_end(ctx);
	ubjw_end(ctx);
	ubjw_end(ctx);

	ubjw_write_key(ctx, "sensor");
	ubjw_write_string(ctx, "gps");
	ubjw_write_key(ctx, "time");
	ubjw_write_integer(ctx, ts);

	ubjw_end(ctx);

	kout << "ubjw_close_context: " << ubjw_close_context(ctx) << endl;

	kout << "ubjson is " << hex;
	for (unsigned int i = 0; i < sizeof(buf); i++) {
		kout << (uint8_t)buf[i];
	}
	kout << endl;
	kout << "ubjson is " << (char*)buf << endl;

#endif

	/*
	 * Common
	 */

	gpio.led_toggle(1);
#ifdef TIMER_S
	kout << dec << uptime.get_s() << endl;
#endif
	ts++;
}

int main(void)