Wirepas SDK
tinycbor/app.c
/* Copyright 2019 Wirepas Ltd. All Rights Reserved.
*
* See file LICENSE.txt for full license details.
*
*/
/*
* \file app.c
* \brief This file demonstrate the use of the tiny cbor library to decode
* a buffer.
*/
#include <stdio.h>
#include "api.h"
#include "cbor.h"
#define DEBUG_LOG_MODULE_NAME "CBOR APP"
#define DEBUG_LOG_MAX_LEVEL LVL_INFO
#include "debug_log.h"
static uint8_t m_cbor_buffer[] = {0xA5, // map(5)
0x00, //Id 0
0x50, //16 bytes array
0x00,
0x01,
0x02,
0x03,
0x04,
0x05,
0x06,
0x07,
0x08,
0x09,
0x0A,
0x0B,
0x0C,
0x0D,
0x0E,
0x0F,
0x01, //Id 1
0x6B, //10 characters string
'T',
'e',
's',
't',
' ',
'S',
't',
'r',
'i',
'n',
'g',
0x02, //Id 2
0x18, //Unsigned int
0x1F,
0x03, //Id 3
0x20, //Signed int
0x04, //Id 4
0x1A, //Large unsigned int
0x07,
0x5B,
0xCD,
0x15};
static bool dumprecursive(CborValue *it, int nestingLevel)
{
char indent[32];
int idx = 0, lvl = nestingLevel;
while (lvl--)
{
indent[idx++] = ' ';
indent[idx++] = ' ';
}
indent[idx] = '\0';
while (!cbor_value_at_end(it))
{
CborError err;
switch (type)
{
{
/* Recursive type. */
CborValue recursed;
LOG(LVL_INFO, "%s%s", indent,
type == CborArrayType ? "Array[" : "Map[");
err = cbor_value_enter_container(it, &recursed);
if (err != CborNoError)
{
/* Parse error. */
return err;
}
err = dumprecursive(&recursed, nestingLevel + 1);
if (err != CborNoError)
{
/* Parse error. */
return err;
}
err = cbor_value_leave_container(it, &recursed);
if (err != CborNoError)
{
/* Parse error. */
return err;
}
LOG(LVL_INFO, "%s]", indent);
continue;
}
{
int val;
err = cbor_value_get_int_checked(it, &val);
if (err != CborNoError)
{
/* Parse error. */
return err;
}
LOG(LVL_INFO, "%sInteger: %d", indent, val);
break;
}
{
uint8_t data[64];
size_t n = 64;
err = cbor_value_copy_byte_string(it, data, &n, it);
if (err != CborNoError)
{
/* Parse error. */
return err;
}
LOG(LVL_INFO, "%sByteString: ", indent);
LOG_BUFFER(LVL_INFO, data, n);
continue;
}
{
char data[64];
size_t n = 64;
err = cbor_value_copy_text_string(it, data, &n, it);
if (err != CborNoError)
{
/* Parse error. */
return err;
}
LOG(LVL_INFO, "%sTextString: %s", indent, data);
continue;
}
{
CborTag tag;
cbor_value_get_tag(it, &tag); // can't fail
LOG(LVL_INFO, "%sTag(%d)", indent, tag);
break;
}
{
uint8_t type;
cbor_value_get_simple_type(it, &type); // can't fail
LOG(LVL_INFO, "%sSimple(%u)", indent, type);
break;
}
{
LOG(LVL_INFO, "%sNull", indent);
break;
}
{
LOG(LVL_INFO, "%sUndefined", indent);
break;
}
{
bool val;
cbor_value_get_boolean(it, &val); // can't fail
LOG(LVL_INFO, "%sBoolean: %s", indent, val ? "true" : "false");
break;
}
{
/* Not managed. */
LOG(LVL_WARNING, "%sFloat: (not managed)", indent);
break;
}
/* Can't happen. */
LOG(LVL_INFO, "%sInvalidType", indent);
break;
}
if (err != CborNoError)
{
/* Parse error. */
return err;
}
}
return CborNoError;
}
void App_init(const app_global_functions_t * functions)
{
(void) functions;
int len = sizeof(m_cbor_buffer);
CborParser parser;
CborValue value;
CborError err;
LOG(LVL_INFO, "TinyCBOR app started");
err = cbor_parser_init(m_cbor_buffer, len, 0, &parser, &value);
if (err == CborNoError)
{
}
if (err == CborNoError)
{
err = dumprecursive(&value, 0);
}
if (err != CborNoError)
{
LOG(LVL_ERROR, "CBOR parsing failure at offset %ld: %s",
value.ptr - m_cbor_buffer,
}
}
static CborType cbor_value_get_type(const CborValue *value)
Definition cbor.h:318
uint64_t CborTag
Definition cbor.h:100
static CborError cbor_value_get_simple_type(const CborValue *value, uint8_t *result)
Definition cbor.h:340
CborType
Definition cbor.h:82
@ CborFloatType
Definition cbor.h:94
@ CborUndefinedType
Definition cbor.h:92
@ CborMapType
Definition cbor.h:87
@ CborIntegerType
Definition cbor.h:83
@ CborByteStringType
Definition cbor.h:84
@ CborInvalidType
Definition cbor.h:97
@ CborNullType
Definition cbor.h:91
@ CborDoubleType
Definition cbor.h:95
@ CborArrayType
Definition cbor.h:86
@ CborTagType
Definition cbor.h:88
@ CborTextStringType
Definition cbor.h:85
@ CborBooleanType
Definition cbor.h:90
@ CborSimpleType
Definition cbor.h:89
@ CborHalfFloatType
Definition cbor.h:93
CBOR_API CborError cbor_value_enter_container(const CborValue *it, CborValue *recursed)
CBOR_API const char * cbor_error_string(CborError error)
static CborError cbor_value_get_boolean(const CborValue *value, bool *result)
Definition cbor.h:330
CBOR_API CborError cbor_value_leave_container(CborValue *it, const CborValue *recursed)
CBOR_API CborError cbor_value_validate(const CborValue *it, uint32_t flags)
static CborError cbor_value_get_tag(const CborValue *value, CborTag *result)
Definition cbor.h:396
static CborError cbor_value_copy_text_string(const CborValue *value, char *buffer, size_t *buflen, CborValue *next)
Definition cbor.h:430
CborError
Definition cbor.h:152
@ CborNoError
Definition cbor.h:153
CBOR_API CborError cbor_parser_init(const uint8_t *buffer, size_t size, uint32_t flags, CborParser *parser, CborValue *it)
CBOR_API CborError cbor_value_get_int_checked(const CborValue *value, int *result)
const uint8_t * ptr
Definition cbor.h:286
@ CborValidateStrictMode
Definition cbor.h:543
static CborError cbor_value_copy_byte_string(const CborValue *value, uint8_t *buffer, size_t *buflen, CborValue *next)
Definition cbor.h:436
static bool cbor_value_at_end(const CborValue *it)
Definition cbor.h:298
CBOR_API CborError cbor_value_advance_fixed(CborValue *it)
#define LOG_INIT()
Definition debug_log.h:66
#define LOG_BUFFER(level, buffer, size)
Print a buffer if its severity is lower or equal to DEBUG_LOG_MAX_LEVEL.
Definition debug_log.h:206
#define LVL_WARNING
Definition debug_log.h:84
#define LVL_INFO
Definition debug_log.h:83
#define LOG_FLUSH(level)
Actively wait that Uart buffer as been sent.
Definition debug_log.h:226
#define LVL_ERROR
Definition debug_log.h:85
#define LOG(level, fmt,...)
Print a log message if its severity is lower or equal to DEBUG_LOG_MAX_LEVEL.
Definition debug_log.h:173
List of global functions, passed to App_entrypoint()
Definition wms_app.h:158