Add a simple TinyCBOR Test Example

This commit is contained in:
Alan Carvalho de Assis 2023-04-26 13:11:23 -03:00 committed by Xiang Xiao
parent 98dde40a88
commit 679dbdac0a
5 changed files with 199 additions and 1 deletions

30
examples/cbortest/Kconfig Normal file
View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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 <nuttx/config.h>
#include <stdio.h>
#include <fcntl.h>
#include <wchar.h>
#include <syslog.h>
#include <unistd.h>
#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;
}

View File

@ -1,2 +1,2 @@
/minmea
/tinycbor
/*.zip