From 313e4e052fa1758c3d20bb680e851a5dd06687dc Mon Sep 17 00:00:00 2001 From: Koen Zandberg Date: Thu, 30 Nov 2023 14:15:34 +0100 Subject: [PATCH 1/3] mjson: Initial include of package --- pkg/Kconfig | 1 + pkg/mjson/Kconfig | 9 +++++++++ pkg/mjson/Makefile | 10 ++++++++++ pkg/mjson/Makefile.include | 1 + pkg/mjson/doc.txt | 16 ++++++++++++++++ pkg/mjson/mjson.mk | 7 +++++++ 6 files changed, 44 insertions(+) create mode 100644 pkg/mjson/Kconfig create mode 100644 pkg/mjson/Makefile create mode 100644 pkg/mjson/Makefile.include create mode 100644 pkg/mjson/doc.txt create mode 100644 pkg/mjson/mjson.mk diff --git a/pkg/Kconfig b/pkg/Kconfig index 537594aa575f..4feea70743fa 100644 --- a/pkg/Kconfig +++ b/pkg/Kconfig @@ -54,6 +54,7 @@ rsource "micro-ecc/Kconfig" rsource "microcoap/Kconfig" rsource "micropython/Kconfig" rsource "minmea/Kconfig" +rsource "mjson/Kconfig" rsource "monocypher/Kconfig" rsource "mynewt-core/Kconfig" rsource "nanocbor/Kconfig" diff --git a/pkg/mjson/Kconfig b/pkg/mjson/Kconfig new file mode 100644 index 000000000000..8efce0a8bf64 --- /dev/null +++ b/pkg/mjson/Kconfig @@ -0,0 +1,9 @@ +# Copyright (c) 2023 Koen Zandberg +# +# This file is subject to the terms and conditions of the GNU Lesser +# General Public License v2.1. See the file LICENSE in the top level +# directory for more details. + +config PACKAGE_MJSON + bool "mjson" + depends on TEST_KCONFIG diff --git a/pkg/mjson/Makefile b/pkg/mjson/Makefile new file mode 100644 index 000000000000..f2d4fd40fc77 --- /dev/null +++ b/pkg/mjson/Makefile @@ -0,0 +1,10 @@ +PKG_NAME=mjson +PKG_URL=https://github.com/cesanta/mjson +# v1.2.7 +PKG_VERSION=032a2eaca1a989e56d88218f2a3cb91a68afebe8 +PKG_LICENSE=MIT + +include $(RIOTBASE)/pkg/pkg.mk + +all: + $(QQ)"$(MAKE)" -C $(PKG_SOURCE_DIR)/src -f $(CURDIR)/$(PKG_NAME).mk diff --git a/pkg/mjson/Makefile.include b/pkg/mjson/Makefile.include new file mode 100644 index 000000000000..33bd7b90f039 --- /dev/null +++ b/pkg/mjson/Makefile.include @@ -0,0 +1 @@ +INCLUDES += -I$(PKGDIRBASE)/mjson/src diff --git a/pkg/mjson/doc.txt b/pkg/mjson/doc.txt new file mode 100644 index 000000000000..043973b2ab64 --- /dev/null +++ b/pkg/mjson/doc.txt @@ -0,0 +1,16 @@ +/** + * @defgroup pkg_mjson mjson + * @ingroup pkg + * @brief mjson - a JSON parser + emitter + JSON-RPC engine + * + * # Introduction + * + * mjson is a small, embedded-friendly JSON parser, emitter and JSON-RPC + * engine. + * + * # License + * + * Licensed under MIT. + * + * @see https://github.com/cesanta/mjson + */ diff --git a/pkg/mjson/mjson.mk b/pkg/mjson/mjson.mk new file mode 100644 index 000000000000..f57a2d5d907c --- /dev/null +++ b/pkg/mjson/mjson.mk @@ -0,0 +1,7 @@ +MODULE = mjson + +# The mjson_merge function requires an alloca implementation, disabled to +# prevent stack memory allocations +CFLAGS += -DMJSON_ENABLE_MERGE=0 + +include $(RIOTBASE)/Makefile.base From 385f6ad488e4d36d2d32e8a3ad2ef1502efaf21e Mon Sep 17 00:00:00 2001 From: Koen Zandberg Date: Thu, 30 Nov 2023 14:16:28 +0100 Subject: [PATCH 2/3] tests/pkg/mjson: Add test for mjson package --- tests/pkg/mjson/Makefile | 6 +++ tests/pkg/mjson/app.config.test | 2 + tests/pkg/mjson/main.c | 80 +++++++++++++++++++++++++++++++++ tests/pkg/mjson/tests/01-run.py | 14 ++++++ 4 files changed, 102 insertions(+) create mode 100644 tests/pkg/mjson/Makefile create mode 100644 tests/pkg/mjson/app.config.test create mode 100644 tests/pkg/mjson/main.c create mode 100755 tests/pkg/mjson/tests/01-run.py diff --git a/tests/pkg/mjson/Makefile b/tests/pkg/mjson/Makefile new file mode 100644 index 000000000000..cac857c6c43c --- /dev/null +++ b/tests/pkg/mjson/Makefile @@ -0,0 +1,6 @@ +include ../Makefile.pkg_common + +USEMODULE += embunit +USEPKG += mjson + +include $(RIOTBASE)/Makefile.include diff --git a/tests/pkg/mjson/app.config.test b/tests/pkg/mjson/app.config.test new file mode 100644 index 000000000000..a81192b504c9 --- /dev/null +++ b/tests/pkg/mjson/app.config.test @@ -0,0 +1,2 @@ +CONFIG_MODULE_EMBUNIT=y +CONFIG_PACKAGE_MJSON=y diff --git a/tests/pkg/mjson/main.c b/tests/pkg/mjson/main.c new file mode 100644 index 000000000000..91fb5754c5be --- /dev/null +++ b/tests/pkg/mjson/main.c @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2023 Koen Zandberg + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup tests + * @{ + * + * @file + * @brief Test application for mjson + * + * @author Koen Zandberg + * + * @} + */ + +#include +#include "embUnit.h" +#include "mjson.h" + +void test_mjson_encode(void) +{ + /* Print into a statically allocated buffer */ + char buf[100]; + mjson_snprintf(buf, sizeof(buf), "{%Q:%d}", "a", 123); + + static const char expected[] = "{\"a\":123}"; + + TEST_ASSERT_EQUAL_INT(0, strcmp(expected, buf)); +} + +void test_mjson_decode(void) +{ + static const char input[] = "{\"a\":1,\"b\":[2,false]}"; /* {"a":1,"b":[2,false]} */ + + double dval = 0; + int res = mjson_get_number(input, strlen(input), "$.a", &dval); + TEST_ASSERT_EQUAL_INT(1, res); + TEST_ASSERT_EQUAL_INT(1, dval); + + const char *buf; + int buf_len; + res = mjson_find(input, strlen(input), "$.b", &buf, &buf_len); + TEST_ASSERT_EQUAL_INT(91, res); + TEST_ASSERT_EQUAL_INT(0, memcmp(buf, "[2,false]", buf_len)); + + res = mjson_get_number(input, strlen(input), "$.b[0]", &dval); + TEST_ASSERT_EQUAL_INT(1, res); + TEST_ASSERT_EQUAL_INT(2, dval); + + int ival = 0; + res = mjson_get_bool(input, strlen(input), "$.b[1]", &ival); + TEST_ASSERT_EQUAL_INT(1, res); + TEST_ASSERT_EQUAL_INT(0, ival); + +} + +Test *tests_mjson(void) +{ + EMB_UNIT_TESTFIXTURES(fixtures) { + new_TestFixture(test_mjson_encode), + new_TestFixture(test_mjson_decode), + }; + + EMB_UNIT_TESTCALLER(mjson_tests, NULL, NULL, fixtures); + return (Test*)&mjson_tests; +} + +int main(void) +{ + TESTS_START(); + TESTS_RUN(tests_mjson()); + TESTS_END(); + + return 0; +} diff --git a/tests/pkg/mjson/tests/01-run.py b/tests/pkg/mjson/tests/01-run.py new file mode 100755 index 000000000000..593eef26217f --- /dev/null +++ b/tests/pkg/mjson/tests/01-run.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2023 Koen Zandberg +# +# This file is subject to the terms and conditions of the GNU Lesser +# General Public License v2.1. See the file LICENSE in the top level +# directory for more details. + +import sys +from testrunner import run_check_unittests + + +if __name__ == "__main__": + sys.exit(run_check_unittests()) From 18c1548077dc218f25d7644847332362a0968050 Mon Sep 17 00:00:00 2001 From: MrKevinWeiss Date: Mon, 18 Dec 2023 13:56:25 +0100 Subject: [PATCH 3/3] tests/pkg/mjson: Add BOARD_INSUFFICIENT_MEMORY list --- tests/pkg/mjson/Makefile.ci | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 tests/pkg/mjson/Makefile.ci diff --git a/tests/pkg/mjson/Makefile.ci b/tests/pkg/mjson/Makefile.ci new file mode 100644 index 000000000000..8d800e187928 --- /dev/null +++ b/tests/pkg/mjson/Makefile.ci @@ -0,0 +1,6 @@ +BOARD_INSUFFICIENT_MEMORY := \ + atmega8 \ + nucleo-l011k4 \ + samd10-xmini \ + stm32f030f4-demo \ + #