1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

Merge pull request #11600 from bergzand/pr/pkg/nanocbor

nanocbor: Initial support for the nanocbor package
This commit is contained in:
Kaspar Schleiser 2019-07-05 15:07:04 +02:00 committed by GitHub
commit 91d982733f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 236 additions and 0 deletions

12
pkg/nanocbor/Makefile Normal file
View File

@ -0,0 +1,12 @@
PKG_NAME = nanocbor
PKG_URL = https://github.com/bergzand/nanocbor
PKG_VERSION = 44d9b1cfaa6829c5d1063f3c0e4a1733cfa409e3
PKG_LICENSE = LGPL-2.1
.PHONY: all
all: git-download
"$(MAKE)" -C $(PKG_BUILDDIR)/src \
-f $(RIOTPKG)/nanocbor/Makefile.$(PKG_NAME)
include $(RIOTBASE)/pkg/pkg.mk

View File

@ -0,0 +1,2 @@
INCLUDES += -I$(RIOTPKG)/nanocbor/include
INCLUDES += -I$(PKGDIRBASE)/nanocbor/include

View File

@ -0,0 +1,6 @@
MODULE = nanocbor
SRC = decoder.c \
encoder.c
include $(RIOTBASE)/Makefile.base

22
pkg/nanocbor/doc.txt Normal file
View File

@ -0,0 +1,22 @@
/**
* @defgroup pkg_nanocbor NanoCBOR library
* @ingroup pkg
* @ingroup sys
* @brief CBOR encoder and decoder library for tiny devices
*
* This package contains the NanoCBOR library for a small CBOR encoder and
* decoder. Flash footprint for encoding and decoding should be below 1K each.
*
* At the moment 8 bit and 16 bit MCU support is broken by upstream, see the
* [issue](https://github.com/bergzand/NanoCBOR/issues/18).
*
* ## Usage
*
* Just add it as a package in your application:
*
* ```makefile
* USEPKG += nanocbor
* ```
*
* @see https://github.com/bergzand/nanocbor
*/

View File

@ -0,0 +1,81 @@
/*
* Copyright (C) 2019 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.
*/
/**
* @defgroup nanocbor_config NanoCBOR configuration header
* @brief Provides compile-time configuration for nanocbor
*/
#ifndef NANOCBOR_CONFIG_H
#define NANOCBOR_CONFIG_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
#ifndef NANOCBOR_RECURSION_MAX
#define NANOCBOR_RECURSION_MAX 10
#endif
/**
* @brief library providing htonll, be64toh or equivalent. Must also provide
* the reverse operation (ntohll, htobe64 or equivalent)
*/
#ifndef NANOCBOR_BYTEORDER_HEADER
#define NANOCBOR_BYTEORDER_HEADER "byteorder.h"
#endif
/**
* @brief call providing htonll or be64toh or equivalent functionality
*
* must take a uint64_t big endian and return it in host endianess
*/
#ifndef NANOCBOR_BE64TOH_FUNC
#define NANOCBOR_BE64TOH_FUNC(be) (ntohll(be))
#endif
/**
* @brief call providing htonll or htobe64 or equivalent functionality
*
* must take a uint64_t in host endianess and return the big endian value
*/
#ifndef NANOCBOR_HTOBE64_FUNC
#define NANOCBOR_HTOBE64_FUNC(be) (htonll(be))
#endif
/**
* @brief call providing htonll or htobe32 or equivalent functionality
*
* must take a uint32_t in host endianess and return the big endian value
*/
#ifndef NANOCBOR_HTOBE32_FUNC
#define NANOCBOR_HTOBE32_FUNC(he) htonl(he)
#endif
/**
* @brief configuration for size_t SIZE_MAX equivalent
*/
#ifndef NANOCBOR_SIZE_SIZET
#if (SIZE_MAX == UINT16_MAX)
#define NANOCBOR_SIZE_SIZET NANOCBOR_SIZE_SHORT
#elif (SIZE_MAX == UINT32_MAX)
#define NANOCBOR_SIZE_SIZET NANOCBOR_SIZE_WORD
#elif (SIZE_MAX == UINT64_MAX)
#define NANOCBOR_SIZE_SIZET NANOCBOR_SIZE_LONG
#else
#error ERROR: unable to determine maximum size of size_t
#endif
#endif
#ifdef __cplusplus
}
#endif
#endif /* NANOCBOR_CONFIG_H */

View File

@ -0,0 +1,16 @@
include ../Makefile.tests_common
TEST_ON_CI_WHITELIST += all
# No 8 bit and 16 bit support
BOARD_BLACKLIST := arduino-duemilanove arduino-leonardo \
arduino-mega2560 arduino-nano \
arduino-uno chronos jiminy-mega256rfr2 mega-xplained \
msb-430 msb-430h telosb waspmote-pro wsn430-v1_3b \
wsn430-v1_4 z1
USEPKG += nanocbor
# Used for verification
USEMODULE += embunit
include $(RIOTBASE)/Makefile.include

79
tests/pkg_nanocbor/main.c Normal file
View File

@ -0,0 +1,79 @@
/*
* Copyright (C) 2018 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 Short NanoCBOR library test
*
* @author Koen Zandberg <koen@bergzand.net>
*
* @}
*/
#include <nanocbor/nanocbor.h>
#include <string.h>
#include "embUnit.h"
static uint8_t buf[64];
static uint8_t expected[] = {
0x9f, 0xf5, 0xf4, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x3a, 0x7f, 0xff, 0xff,
0xff, 0xa4, 0x08, 0x18, 0x1e, 0x38, 0x1d, 0x19, 0x01, 0xf4, 0x39, 0x01,
0xf3, 0x75, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20,
0x6c, 0x6f, 0x6e, 0x67, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0xfa,
0x3e, 0xae, 0x14, 0x7b, 0x46, 0x62, 0x79, 0x74, 0x65, 0x7a, 0x00, 0xf6,
0xff
};
void test_nanocbor_encode(void)
{
nanocbor_encoder_t enc;
nanocbor_encoder_init(&enc, buf, sizeof(buf));
nanocbor_fmt_array_indefinite(&enc);
nanocbor_fmt_bool(&enc, true);
nanocbor_fmt_bool(&enc, false);
nanocbor_fmt_uint(&enc, UINT32_MAX);
nanocbor_fmt_int(&enc, INT32_MIN);
nanocbor_fmt_map(&enc, 4);
nanocbor_fmt_uint(&enc, 8);
nanocbor_fmt_int(&enc, 30);
nanocbor_fmt_int(&enc, -30);
nanocbor_fmt_int(&enc, 500);
nanocbor_fmt_int(&enc, -500);
nanocbor_put_tstr(&enc, "this is a long string");
nanocbor_fmt_float(&enc, 0.34);
nanocbor_put_bstr(&enc, (uint8_t*)"bytez", sizeof("bytez"));
nanocbor_fmt_null(&enc);
nanocbor_fmt_end_indefinite(&enc);
size_t required = nanocbor_encoded_len(&enc);
TEST_ASSERT_EQUAL_INT(required, sizeof(expected));
TEST_ASSERT_EQUAL_INT(0, memcmp(buf, expected, sizeof(expected)));
}
Test *tests_nanocbor(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
new_TestFixture(test_nanocbor_encode),
};
EMB_UNIT_TESTCALLER(nanocbor_tests, NULL, NULL, fixtures);
return (Test *)&nanocbor_tests;
}
int main(void)
{
TESTS_START();
TESTS_RUN(tests_nanocbor());
TESTS_END();
return 0;
}

View File

@ -0,0 +1,18 @@
#!/usr/bin/env python3
# Copyright (C) 2017 Freie Universität Berlin
#
# 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
def testfunc(child):
child.expect(r'OK \(\d+ tests\)')
if __name__ == "__main__":
sys.exit(run(testfunc))