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)
{
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,
}
}
cbor_value_get_type
static CborType cbor_value_get_type(const CborValue *value)
Definition: cbor.h:318
CborIntegerType
@ CborIntegerType
Definition: cbor.h:83
LVL_INFO
#define LVL_INFO
Definition: debug_log.h:83
LVL_ERROR
#define LVL_ERROR
Definition: debug_log.h:85
CborValue
Definition: cbor.h:283
CborTag
uint64_t CborTag
Definition: cbor.h:100
cbor_value_validate
CBOR_API CborError cbor_value_validate(const CborValue *it, uint32_t flags)
LOG_BUFFER
#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:193
CborNoError
@ CborNoError
Definition: cbor.h:153
cbor_value_get_simple_type
static CborError cbor_value_get_simple_type(const CborValue *value, uint8_t *result)
Definition: cbor.h:340
cbor.h
node_configuration.h
CborValidateStrictMode
@ CborValidateStrictMode
Definition: cbor.h:543
cbor_value_enter_container
CBOR_API CborError cbor_value_enter_container(const CborValue *it, CborValue *recursed)
CborArrayType
@ CborArrayType
Definition: cbor.h:86
cbor_value_copy_byte_string
static CborError cbor_value_copy_byte_string(const CborValue *value, uint8_t *buffer, size_t *buflen, CborValue *next)
Definition: cbor.h:436
CborError
CborError
Definition: cbor.h:152
cbor_parser_init
CBOR_API CborError cbor_parser_init(const uint8_t *buffer, size_t size, uint32_t flags, CborParser *parser, CborValue *it)
cbor_value_advance_fixed
CBOR_API CborError cbor_value_advance_fixed(CborValue *it)
CborBooleanType
@ CborBooleanType
Definition: cbor.h:90
cbor_value_get_boolean
static CborError cbor_value_get_boolean(const CborValue *value, bool *result)
Definition: cbor.h:330
CborParser
Definition: cbor.h:276
CborType
CborType
Definition: cbor.h:82
app_global_functions_t
List of global functions, passed to App_entrypoint()
Definition: wms_app.h:157
LOG_FLUSH
#define LOG_FLUSH(level)
Actively wait that Uart buffer as been sent.
Definition: debug_log.h:213
CborByteStringType
@ CborByteStringType
Definition: cbor.h:84
debug_log.h
CborValue::ptr
const uint8_t * ptr
Definition: cbor.h:286
CborSimpleType
@ CborSimpleType
Definition: cbor.h:89
CborNullType
@ CborNullType
Definition: cbor.h:91
cbor_value_leave_container
CBOR_API CborError cbor_value_leave_container(CborValue *it, const CborValue *recursed)
cbor_error_string
const CBOR_API char * cbor_error_string(CborError error)
cbor_value_copy_text_string
static CborError cbor_value_copy_text_string(const CborValue *value, char *buffer, size_t *buflen, CborValue *next)
Definition: cbor.h:430
LOG_INIT
#define LOG_INIT()
Definition: debug_log.h:66
LVL_WARNING
#define LVL_WARNING
Definition: debug_log.h:84
CborFloatType
@ CborFloatType
Definition: cbor.h:94
LOG
#define LOG(level, fmt,...)
Print a log message if its severity is lower or equal to DEBUG_LOG_MAX_LEVEL.
Definition: debug_log.h:172
cbor_value_get_int_checked
CBOR_API CborError cbor_value_get_int_checked(const CborValue *value, int *result)
CborMapType
@ CborMapType
Definition: cbor.h:87
CborHalfFloatType
@ CborHalfFloatType
Definition: cbor.h:93
CborInvalidType
@ CborInvalidType
Definition: cbor.h:97
cbor_value_at_end
static bool cbor_value_at_end(const CborValue *it)
Definition: cbor.h:298
CborTextStringType
@ CborTextStringType
Definition: cbor.h:85
CborTagType
@ CborTagType
Definition: cbor.h:88
cbor_value_get_tag
static CborError cbor_value_get_tag(const CborValue *value, CborTag *result)
Definition: cbor.h:396
CborDoubleType
@ CborDoubleType
Definition: cbor.h:95
api.h
CborUndefinedType
@ CborUndefinedType
Definition: cbor.h:92