Skip to content

Commit

Permalink
Add examples (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
onkwon authored Aug 24, 2022
1 parent f4b813c commit dfef2e9
Show file tree
Hide file tree
Showing 15 changed files with 584 additions and 15 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: build

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
build:
name: Compile and Test
runs-on: ubuntu-latest
steps:
- name: Clone Repository
uses: actions/checkout@v2
with:
submodules: recursive
fetch-depth: 0
- name: Compile
run: make
- name: Install test framework
run: make -C tests install
- name: Unit Test
run: make -C tests
41 changes: 41 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: release

on:
push:
tags:
- '*'

jobs:
build:
name: Create Release
runs-on: ubuntu-latest
steps:
- name: Clone Repository
uses: actions/checkout@v2
with:
submodules: recursive
fetch-depth: 0
- name: Compile
run: make
- name: Install test framework
run: make -C tests install
- name: Unit Test
run: make -C tests
- name: Get Version
id: get_version
run: echo "::set-output name=VERSION::$(cat build/version.txt)"
- name: Create Release
id: create_release
uses: actions/create-release@latest
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ steps.get_version.outputs.VERSION }}
release_name: ${{ steps.get_version.outputs.VERSION }}
body: |
${{ steps.get_version.outputs.VERSION }}
## Features
## Bug Fixes
draft: false
prerelease: false
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
build
*cscope.*
tests/cpputest
.cache
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## v0.1.0 - August 24, 2022

The initial release.
16 changes: 12 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ OD := $(CROSS_COMPILE_PREFIX)objdump
NM := $(CROSS_COMPILE_PREFIX)nm

CFLAGS := \
-mcpu=cortex-m0 \
-mthumb \
-mabi=aapcs \
\
-std=c99 \
-static \
-ffreestanding \
Expand Down Expand Up @@ -79,8 +75,20 @@ CFLAGS := \
\
-Wno-error=pedantic

ifneq ($(CROSS_COMPILE),)
CFLAGS += \
-mcpu=cortex-m0 \
-mthumb \
-mabi=aapcs
endif

VERSION ?= $(shell git describe --long --tags --dirty --always)
version-list := $(subst -, , $(VERSION))
VERSION_TAG := $(strip $(word 1, $(version-list))).$(word 2, $(version-list))

all: $(OBJS)
$(Q)$(SZ) -t --common $(sort $(OBJS))
@echo $(VERSION_TAG) > $(BUILDIR)/version.txt

$(BUILDIR)/%.o: %.c $(MAKEFILE_LIST)
$(info compiling $<)
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ union {
int8_t i8;
int16_t i16;
int32_t i32;
int64_t i64
int64_t i64;
float f32;
double f64;
uint8_t s[MTU];
Expand All @@ -94,14 +94,13 @@ cbor_writer_t writer;
cbor_writer_init(&reader, buf, sizeof(buf));
cbor_encode_map_indefinite(&writer);
cbor_encode_map(&writer, 2);
/* 1st */
cbor_encode_text_string(&writer, "key");
cbor_encode_text_string(&writer, "value");
/* 2nd */
cbor_encode_text_string(&writer, "age");
cbor_encode_negative_integer(&writer, -1);
cbor_encode_break(&writer);
```

## Limitation
Expand Down
170 changes: 170 additions & 0 deletions examples/complex.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
#include "example.h"

#include "cbor/decoder.h"
#include "cbor/parser.h"
#include "cbor/helper.h"

static void do_unknown(cbor_reader_t const *reader, cbor_item_t const *item, void *udt)
{
struct udt *p = (struct udt *)udt;
if (item->type == CBOR_ITEM_SIMPLE_VALUE) {
cbor_decode(reader, item, &p->type, sizeof(p->type));
} else if (item->type == CBOR_ITEM_STRING && item->size == 16) {
cbor_decode(reader, item, &p->uuid, sizeof(p->uuid));
}
}
static void do_timestamp(cbor_reader_t const *reader, cbor_item_t const *item, void *udt)
{
struct udt *p = (struct udt *)udt;
cbor_decode(reader, item, &p->time, sizeof(p->time));
}
static void do_acc_x(cbor_reader_t const *reader, cbor_item_t const *item, void *udt)
{
struct udt *p = (struct udt *)udt;
cbor_decode(reader, item, &(p->data.acc.x), sizeof(p->data.acc.x));
}
static void do_acc_y(cbor_reader_t const *reader, cbor_item_t const *item, void *udt)
{
struct udt *p = (struct udt *)udt;
cbor_decode(reader, item, &(p->data.acc.y), sizeof(p->data.acc.y));
}
static void do_acc_z(cbor_reader_t const *reader, cbor_item_t const *item, void *udt)
{
struct udt *p = (struct udt *)udt;
cbor_decode(reader, item, &(p->data.acc.z), sizeof(p->data.acc.z));
}
static void do_gyr_x(cbor_reader_t const *reader, cbor_item_t const *item, void *udt)
{
struct udt *p = (struct udt *)udt;
cbor_decode(reader, item, &(p->data.gyro.x), sizeof(p->data.gyro.x));
}
static void do_gyr_y(cbor_reader_t const *reader, cbor_item_t const *item, void *udt)
{
struct udt *p = (struct udt *)udt;
cbor_decode(reader, item, &(p->data.gyro.y), sizeof(p->data.gyro.y));
}
static void do_gyr_z(cbor_reader_t const *reader, cbor_item_t const *item, void *udt)
{
struct udt *p = (struct udt *)udt;
cbor_decode(reader, item, &(p->data.gyro.z), sizeof(p->data.gyro.z));
}

static struct converter {
void const *primary;
void const *secondary;
void (*run)(cbor_reader_t const *reader, cbor_item_t const *item, void *udt);
} converters[] = {
{ .primary = "t", .secondary = NULL, .run = do_timestamp },
{ .primary = "x", .secondary = "acc", .run = do_acc_x },
{ .primary = "y", .secondary = "acc", .run = do_acc_y },
{ .primary = "z", .secondary = "acc", .run = do_acc_z },
{ .primary = "x", .secondary = "gyro", .run = do_gyr_x },
{ .primary = "y", .secondary = "gyro", .run = do_gyr_y },
{ .primary = "z", .secondary = "gyro", .run = do_gyr_z },
};

static void (*get_converter(void const *primary, size_t primary_len,
void const *secondary, size_t secondary_len))
(cbor_reader_t const *reader, cbor_item_t const *item, void *udt)
{
if ((primary == NULL && secondary == NULL) ||
(primary_len == 0 && secondary_len == 0)) {
return do_unknown;
}

for (size_t i = 0; i < sizeof(converters) / sizeof(converters[0]); i++) {
struct converter const *p = &converters[i];
if (p->primary && memcmp(p->primary, primary, primary_len)) {
continue;
} else if (p->secondary && memcmp(p->secondary, secondary, secondary_len)) {
continue;
}

if (p->primary || p->secondary) {
return p->run;
}
}

return do_unknown;
}

static void convert(cbor_reader_t const *reader, cbor_item_t const *item,
cbor_item_t const *parent, void *udt)
{
void const *primary = NULL;
void const *secondary = NULL;
size_t primary_len = 0;
size_t secondary_len = 0;

if (parent != NULL && parent->type == CBOR_ITEM_MAP) {
if ((item - parent) % 2) { /* key */
return;
}

if (parent->offset > 1) {
secondary = cbor_decode_pointer(reader, parent-1);
secondary_len = (parent-1)->size;
}

primary = cbor_decode_pointer(reader, item-1);
primary_len = (item-1)->size;
}

get_converter(primary, primary_len, secondary, secondary_len)
(reader, item, udt);
}

/*
sample_data[] = { 0x9f, 0xe1, 0x50, 0x12, 0x3e, 0x45, 0x67, 0xe8, 0x9b, 0x12,
0xd3, 0xa4, 0x56, 0x42, 0x66, 0x14, 0x17, 0x40, 0x00, 0xbf,
0x61, 0x74, 0x1a, 0x63, 0x04, 0x4b, 0xa5, 0x64, 0x64, 0x61,
0x74, 0x61, 0xa2, 0x63, 0x61, 0x63, 0x63, 0xa3, 0x61, 0x78,
0xf9, 0x00, 0x00, 0x61, 0x79, 0xf9, 0x00, 0x00, 0x61, 0x7a,
0xfa, 0x41, 0x1c, 0xf5, 0xc3, 0x64, 0x67, 0x79, 0x72, 0x6f,
0xa3, 0x61, 0x78, 0xfa, 0x40, 0xc9, 0x99, 0x9a, 0x61, 0x79,
0xf9, 0x00, 0x00, 0x61, 0x7a, 0xf9, 0x00, 0x00, 0xff, 0xff
};
generated by:
cbor_writer_init(&writer, writer_buffer, sizeof(writer_buffer));
cbor_encode_array_indefinite(&writer);
cbor_encode_simple(&writer, 1);
cbor_encode_byte_string(&writer, uuid, sizeof(uuid));
cbor_encode_map_indefinite(&writer);
cbor_encode_text_string(&writer, "t");
cbor_encode_unsigned_integer(&writer, 1661225893);
cbor_encode_text_string(&writer, "data");
cbor_encode_map(&writer, 2);
cbor_encode_text_string(&writer, "acc");
cbor_encode_map(&writer, 3);
cbor_encode_text_string(&writer, "x");
cbor_encode_float(&writer, 0.f);
cbor_encode_text_string(&writer, "y");
cbor_encode_float(&writer, 0.f);
cbor_encode_text_string(&writer, "z");
cbor_encode_float(&writer, 9.81f);
cbor_encode_text_string(&writer, "gyro");
cbor_encode_map(&writer, 3);
cbor_encode_text_string(&writer, "x");
cbor_encode_float(&writer, 6.3f);
cbor_encode_text_string(&writer, "y");
cbor_encode_float(&writer, 0.f);
cbor_encode_text_string(&writer, "z");
cbor_encode_float(&writer, 0.f);
cbor_encode_break(&writer);
cbor_encode_break(&writer);
See the test case: tests/src/example_test.cpp
*/
void complex_example(void const *data, size_t datasize, void *udt)
{
cbor_reader_t reader;
cbor_item_t items[32];
size_t n;

cbor_reader_init(&reader, data, datasize);
cbor_error_t err = cbor_parse(&reader, items, sizeof(items) / sizeof(items[0]), &n);
if (err == CBOR_SUCCESS || err == CBOR_BREAK) {
cbor_iterate(&reader, items, n, 0, convert, udt);
}
}
40 changes: 40 additions & 0 deletions examples/example.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#ifndef CBOR_EXAMPLE_H
#define CBOR_EXAMPLE_H

#if defined(__cplusplus)
extern "C" {
#endif

#include <stddef.h>
#include <stdint.h>
#include <string.h>

/* User-defined Data Type */
struct udt {
uint32_t time;
uint8_t type;
uint8_t uuid[16];
struct {
struct {
float x;
float y;
float z;
} acc;
struct {
float x;
float y;
float z;
} gyro;
} data;
};

typedef void (*example_writer)(void const *data, size_t datasize);

void simple_example(example_writer print);
void complex_example(void const *data, size_t datasize, void *udt);

#if defined(__cplusplus)
}
#endif

#endif /* CBOR_EXAMPLE_H */
Loading

0 comments on commit dfef2e9

Please sign in to comment.