All Projects → bergzand → NanoCBOR

bergzand / NanoCBOR

Licence: CC0-1.0 license
CBOR library aimed at heavily constrained devices

Programming Languages

c
50402 projects - #5 most used programming language
python
139335 projects - #7 most used programming language
Meson
512 projects

Projects that are alternatives of or similar to NanoCBOR

dockage
embedded document/json store
Stars: ✭ 20 (-37.5%)
Mutual labels:  embedded
fat32
[New Version] This is FAT32 FileSystem Library, which is #![no_std] and does not use alloc.
Stars: ✭ 25 (-21.87%)
Mutual labels:  embedded
bx-github-ci
This tutorial provides one example on how a CI (Continuous Integration) workflow with the IAR Build Tools for Linux can be set up on GitHub. The IAR Build Tools on Linux are available for Arm, RISC-V and Renesas (RH850, RL78 and RX).
Stars: ✭ 20 (-37.5%)
Mutual labels:  embedded
micropython-micropower
Support for building ultra low power systems based on the Pyboard (1.x and D series).
Stars: ✭ 44 (+37.5%)
Mutual labels:  embedded
cassette
A simple, single-future, non-blocking executor intended for building state machines. Designed to be no-std and embedded friendly.
Stars: ✭ 47 (+46.88%)
Mutual labels:  embedded
knurling-session-20q4
Building your own embedded CO2 measuring device. With Rust!
Stars: ✭ 15 (-53.12%)
Mutual labels:  embedded
mish
A no-std libm implementation in Rust
Stars: ✭ 14 (-56.25%)
Mutual labels:  embedded
miZy
miZy - tiny fast embedded linux
Stars: ✭ 106 (+231.25%)
Mutual labels:  embedded
utilities
Utilities add-in for the Casio Prizm (fx-CG 10 and 20)
Stars: ✭ 34 (+6.25%)
Mutual labels:  embedded
circuitpython
CircuitPython - a Python implementation for teaching coding with microcontrollers
Stars: ✭ 3,097 (+9578.13%)
Mutual labels:  embedded
tensorflow-on-orangepi-zero
Tensorflow for Orange PI Zero
Stars: ✭ 16 (-50%)
Mutual labels:  embedded
CapableRobot USBHub Driver
www.crowdsupply.com/capable-robot-components/programmable-usb-hub
Stars: ✭ 17 (-46.87%)
Mutual labels:  embedded
PureDB
PureDB is a portable and tiny set of libraries for creating and reading constant databases.
Stars: ✭ 27 (-15.62%)
Mutual labels:  embedded
pycose
A Python implementation of the COSE specification (CBOR Object Signing and Encryption) described in RFC 8152.
Stars: ✭ 27 (-15.62%)
Mutual labels:  cbor
embedded-sps
Embedded i2c Driver for Sensirion Particulate Matter Sensors - Download the Zip Package from the Release Page
Stars: ✭ 36 (+12.5%)
Mutual labels:  embedded
w1-gpio-cl
Command line configured kernel mode 1-wire bus master driver. w1-gpio standard Linux module enhancement/substitution.
Stars: ✭ 17 (-46.87%)
Mutual labels:  embedded
xForth
Experimental Forth cross compiler for tiny devices
Stars: ✭ 53 (+65.63%)
Mutual labels:  embedded
atat
no_std crate for parsing AT commands
Stars: ✭ 50 (+56.25%)
Mutual labels:  embedded
nrf52832-pac
Peripheral Access Crate for the nRF52832 microcontroller
Stars: ✭ 21 (-34.37%)
Mutual labels:  embedded
stm32f7xx-hal
A Rust embedded-hal HAL for all MCUs in the STM32 F7 family
Stars: ✭ 71 (+121.88%)
Mutual labels:  embedded

NanoCBOR

NanoCBOR is a tiny CBOR library aimed at embedded and heavily constrained devices. It is optimized for 32 bit architectures but should run fine on 8 bit and 16 bit architectures. NanoCBOR is optimized for decoding known CBOR structures while optimizing the flash footprint of both NanoCBOR and the code using NanoCBOR.

The decoder of NanoCBOR should compile to 600-800 bytes on a Cortex-M0+ MCU, depending on whether floating point decoding is required.

Usage

To achieve the small code size, two patterns are used throughout the decode library.

  • Every decode call will first check the type and refuse to decode if the CBOR element is not of the required type.
  • Every decode call will, on succesfull decode, advance the decode context to the next CBOR element.

This allows using code to call decode functions and check the return code of the function without requiring an if value of type, decode value, advance to next item dance, and requiring only a single call to decode an expected type and advance to the next element.

Start the decoding of a buffer with:

nanocbor_value_t decoder;
nanocbor_decoder_init(&decoder, buffer, buffer_len);

Where buffer is an const uint8_t array containing an CBOR structure.

To decode an int32_t from a cbor structure and bail out if the element is not of the integer type:

int32_t value = 0;
if (nanocbor_get_int32(&decoder, &value) < 0) {
  return ERR_INVALID_STRUCTURE;
}
return use_value(value);

Iterating over an CBOR array and calling a function passing every element is as simple as:

nanocbor_value_t arr; /* Array value instance */

if (nanocbor_enter_array(&decoder, &arr) < 0) {
    return ERR_INVALID_STRUCTURE;
}
while (!nanocbor_at_end(&arr)) {
    handle_array_element(&arr);
}

Decoding a map is similar to an array, except that every map entry consists of two CBOR elements requiring separate decoding. For example, a map using integers as keys and strings as values can be decoded with:

while (!nanocbor_at_end(&map)) {
    int32_t key;
    const char *value;
    size_t value_len;
    if (nanocbor_get_int32(&map, &integer_key) < 0) {
        return ERR_INVALID_STRUCTURE;
    }
    if (nanocbor_get_tstr(&map, &value, &value_len) < 0) {
        return ERR_INVALID_STRUCTURE;
    }
    handle_map_element(key, value, value_len);
}

Dependencies:

Only dependency are two functions to provide endian conversion. These are not provided by the library and have to be configured in the header file. On a bare metal ARM platform, __builtin_bswap64 and __builtin_bswap32 can be used for this conversion.

Contributing

Open an issue, PR, the usual.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].