From 679dbdac0ac8a55caa5973f96a30554e914be883 Mon Sep 17 00:00:00 2001 From: Alan Carvalho de Assis Date: Wed, 26 Apr 2023 13:11:23 -0300 Subject: [PATCH] Add a simple TinyCBOR Test Example --- examples/cbortest/Kconfig | 30 ++++++++ examples/cbortest/Make.defs | 23 +++++++ examples/cbortest/Makefile | 34 +++++++++ examples/cbortest/cbortest_main.c | 111 ++++++++++++++++++++++++++++++ fsutils/libtinycbor/.gitignore | 2 +- 5 files changed, 199 insertions(+), 1 deletion(-) create mode 100644 examples/cbortest/Kconfig create mode 100644 examples/cbortest/Make.defs create mode 100644 examples/cbortest/Makefile create mode 100644 examples/cbortest/cbortest_main.c diff --git a/examples/cbortest/Kconfig b/examples/cbortest/Kconfig new file mode 100644 index 000000000..5c6cb6870 --- /dev/null +++ b/examples/cbortest/Kconfig @@ -0,0 +1,30 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +config EXAMPLES_TINYCBOR_TEST + tristate "TinyCBOR Test Example" + default n + depends on FSUTILS_TINYCBOR_LIB + ---help--- + Enable the TinyCBOR test example + +if EXAMPLES_TINYCBOR_TEST + +config EXAMPLES_TINYCBOR_TEST_PROGNAME + string "Program name" + default "cbor" + ---help--- + This is the name of the program that will be used when the NSH ELF + program is installed. + +config EXAMPLES_TINYCBOR_TEST_PRIORITY + int "TINYCBOR_TEST task priority" + default 100 + +config EXAMPLES_TINYCBOR_TEST_STACKSIZE + int "TINYCBOR_TEST stack size" + default DEFAULT_TASK_STACKSIZE + +endif diff --git a/examples/cbortest/Make.defs b/examples/cbortest/Make.defs new file mode 100644 index 000000000..feecd1f03 --- /dev/null +++ b/examples/cbortest/Make.defs @@ -0,0 +1,23 @@ +############################################################################ +# apps/examples/cbortest/Make.defs +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +ifneq ($(CONFIG_EXAMPLES_TINYCBOR_TEST),) +CONFIGURED_APPS += $(APPDIR)/examples/cbortest +endif diff --git a/examples/cbortest/Makefile b/examples/cbortest/Makefile new file mode 100644 index 000000000..d2f76a343 --- /dev/null +++ b/examples/cbortest/Makefile @@ -0,0 +1,34 @@ +############################################################################ +# apps/examples/cbortest/Makefile +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +include $(APPDIR)/Make.defs + +# TINYCBOR_TEST built-in application info + +PROGNAME = $(CONFIG_EXAMPLES_TINYCBOR_TEST_PROGNAME) +PRIORITY = $(CONFIG_EXAMPLES_TINYCBOR_TEST_PRIORITY) +STACKSIZE = $(CONFIG_EXAMPLES_TINYCBOR_TEST_STACKSIZE) +MODULE = $(CONFIG_EXAMPLES_TINYCBOR_TEST) + +# TINYCBOR_TEST Example + +MAINSRC = cbortest_main.c + +include $(APPDIR)/Application.mk diff --git a/examples/cbortest/cbortest_main.c b/examples/cbortest/cbortest_main.c new file mode 100644 index 000000000..ce9d763c5 --- /dev/null +++ b/examples/cbortest/cbortest_main.c @@ -0,0 +1,111 @@ +/**************************************************************************** + * apps/examples/cbortest/cbortest_main.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include +#include + +#include "tinycbor/cbor.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define MINMEA_MAX_LENGTH 256 + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * cbortest_main + ****************************************************************************/ + +int main(int argc, FAR char *argv[]) +{ + CborError res; + CborEncoder encoder; + CborEncoder map_encoder; + uint8_t output[50]; + size_t output_len; + int i; + + printf("TinyCBOR test: Encoding { \"t\": 1234 }\n"); + + /* Init our CBOR Encoder */ + + cbor_encoder_init(&encoder, output, sizeof(output), 0); + + /* Create a Map Encoder that maps keys to values, 1 = Key-Value Pairs */ + + res = cbor_encoder_create_map(&encoder, &map_encoder, 1); + + /* Check for any error */ + + assert(res == CborNoError); + + /* First Key-Value Pair: Map the Key */ + + res = cbor_encode_text_stringz(&map_encoder, "t"); + + /* Check for any error */ + + assert(res == CborNoError); + + /* First Key-Value Pair: Map the Value */ + + res = cbor_encode_int(&map_encoder, 1234); + + /* Check for any error */ + + assert(res == CborNoError); + + /* Close the Map Encoder */ + + res = cbor_encoder_close_container(&encoder, &map_encoder); + + /* Check for any error */ + + assert(res == CborNoError); + + /* How many bytes were encoded */ + + output_len = cbor_encoder_get_buffer_size(&encoder, output); + printf("CBOR Output: %d bytes\n", output_len); + + /* Dump the encoded CBOR output (6 bytes): */ + + printf("Expected sequence: 0xa1 0x61 0x74 0x19 0x04 0xd2\n"); + + for (i = 0; i < output_len; i++) + { + printf(" 0x%02x\n", output[i]); + } + + return 0; +} diff --git a/fsutils/libtinycbor/.gitignore b/fsutils/libtinycbor/.gitignore index 89885ca8a..8252e6cbd 100644 --- a/fsutils/libtinycbor/.gitignore +++ b/fsutils/libtinycbor/.gitignore @@ -1,2 +1,2 @@ -/minmea +/tinycbor /*.zip