Skip to content

Commit ba450e2

Browse files
committed
Add examples (#7)
1 parent f4b813c commit ba450e2

File tree

15 files changed

+584
-15
lines changed

15 files changed

+584
-15
lines changed

.github/workflows/build.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: build
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
11+
jobs:
12+
build:
13+
name: Compile and Test
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Clone Repository
17+
uses: actions/checkout@v2
18+
with:
19+
submodules: recursive
20+
fetch-depth: 0
21+
- name: Compile
22+
run: make
23+
- name: Install test framework
24+
run: make -C tests install
25+
- name: Unit Test
26+
run: make -C tests

.github/workflows/release.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: release
2+
3+
on:
4+
push:
5+
tags:
6+
- '*'
7+
8+
jobs:
9+
build:
10+
name: Create Release
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Clone Repository
14+
uses: actions/checkout@v2
15+
with:
16+
submodules: recursive
17+
fetch-depth: 0
18+
- name: Compile
19+
run: make
20+
- name: Install test framework
21+
run: make -C tests install
22+
- name: Unit Test
23+
run: make -C tests
24+
- name: Get Version
25+
id: get_version
26+
run: echo "::set-output name=VERSION::$(cat build/version.txt)"
27+
- name: Create Release
28+
id: create_release
29+
uses: actions/create-release@latest
30+
env:
31+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
32+
with:
33+
tag_name: ${{ steps.get_version.outputs.VERSION }}
34+
release_name: ${{ steps.get_version.outputs.VERSION }}
35+
body: |
36+
${{ steps.get_version.outputs.VERSION }}
37+
38+
## Features
39+
## Bug Fixes
40+
draft: false
41+
prerelease: false

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
build
22
*cscope.*
33
tests/cpputest
4+
.cache

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## v0.1.0 - August 24, 2022
2+
3+
The initial release.

Makefile

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ OD := $(CROSS_COMPILE_PREFIX)objdump
3333
NM := $(CROSS_COMPILE_PREFIX)nm
3434

3535
CFLAGS := \
36-
-mcpu=cortex-m0 \
37-
-mthumb \
38-
-mabi=aapcs \
39-
\
4036
-std=c99 \
4137
-static \
4238
-ffreestanding \
@@ -79,8 +75,20 @@ CFLAGS := \
7975
\
8076
-Wno-error=pedantic
8177

78+
ifneq ($(CROSS_COMPILE),)
79+
CFLAGS += \
80+
-mcpu=cortex-m0 \
81+
-mthumb \
82+
-mabi=aapcs
83+
endif
84+
85+
VERSION ?= $(shell git describe --long --tags --dirty --always)
86+
version-list := $(subst -, , $(VERSION))
87+
VERSION_TAG := $(strip $(word 1, $(version-list)))
88+
8289
all: $(OBJS)
8390
$(Q)$(SZ) -t --common $(sort $(OBJS))
91+
@echo $(VERSION_TAG) > $(BUILDIR)/version.txt
8492

8593
$(BUILDIR)/%.o: %.c $(MAKEFILE_LIST)
8694
$(info compiling $<)

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ union {
7878
int8_t i8;
7979
int16_t i16;
8080
int32_t i32;
81-
int64_t i64
81+
int64_t i64;
8282
float f32;
8383
double f64;
8484
uint8_t s[MTU];
@@ -94,14 +94,13 @@ cbor_writer_t writer;
9494
9595
cbor_writer_init(&reader, buf, sizeof(buf));
9696
97-
cbor_encode_map_indefinite(&writer);
97+
cbor_encode_map(&writer, 2);
9898
/* 1st */
9999
cbor_encode_text_string(&writer, "key");
100100
cbor_encode_text_string(&writer, "value");
101101
/* 2nd */
102102
cbor_encode_text_string(&writer, "age");
103103
cbor_encode_negative_integer(&writer, -1);
104-
cbor_encode_break(&writer);
105104
```
106105

107106
## Limitation

examples/complex.c

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
#include "example.h"
2+
3+
#include "cbor/decoder.h"
4+
#include "cbor/parser.h"
5+
#include "cbor/helper.h"
6+
7+
static void do_unknown(cbor_reader_t const *reader, cbor_item_t const *item, void *udt)
8+
{
9+
struct udt *p = (struct udt *)udt;
10+
if (item->type == CBOR_ITEM_SIMPLE_VALUE) {
11+
cbor_decode(reader, item, &p->type, sizeof(p->type));
12+
} else if (item->type == CBOR_ITEM_STRING && item->size == 16) {
13+
cbor_decode(reader, item, &p->uuid, sizeof(p->uuid));
14+
}
15+
}
16+
static void do_timestamp(cbor_reader_t const *reader, cbor_item_t const *item, void *udt)
17+
{
18+
struct udt *p = (struct udt *)udt;
19+
cbor_decode(reader, item, &p->time, sizeof(p->time));
20+
}
21+
static void do_acc_x(cbor_reader_t const *reader, cbor_item_t const *item, void *udt)
22+
{
23+
struct udt *p = (struct udt *)udt;
24+
cbor_decode(reader, item, &(p->data.acc.x), sizeof(p->data.acc.x));
25+
}
26+
static void do_acc_y(cbor_reader_t const *reader, cbor_item_t const *item, void *udt)
27+
{
28+
struct udt *p = (struct udt *)udt;
29+
cbor_decode(reader, item, &(p->data.acc.y), sizeof(p->data.acc.y));
30+
}
31+
static void do_acc_z(cbor_reader_t const *reader, cbor_item_t const *item, void *udt)
32+
{
33+
struct udt *p = (struct udt *)udt;
34+
cbor_decode(reader, item, &(p->data.acc.z), sizeof(p->data.acc.z));
35+
}
36+
static void do_gyr_x(cbor_reader_t const *reader, cbor_item_t const *item, void *udt)
37+
{
38+
struct udt *p = (struct udt *)udt;
39+
cbor_decode(reader, item, &(p->data.gyro.x), sizeof(p->data.gyro.x));
40+
}
41+
static void do_gyr_y(cbor_reader_t const *reader, cbor_item_t const *item, void *udt)
42+
{
43+
struct udt *p = (struct udt *)udt;
44+
cbor_decode(reader, item, &(p->data.gyro.y), sizeof(p->data.gyro.y));
45+
}
46+
static void do_gyr_z(cbor_reader_t const *reader, cbor_item_t const *item, void *udt)
47+
{
48+
struct udt *p = (struct udt *)udt;
49+
cbor_decode(reader, item, &(p->data.gyro.z), sizeof(p->data.gyro.z));
50+
}
51+
52+
static struct converter {
53+
void const *primary;
54+
void const *secondary;
55+
void (*run)(cbor_reader_t const *reader, cbor_item_t const *item, void *udt);
56+
} converters[] = {
57+
{ .primary = "t", .secondary = NULL, .run = do_timestamp },
58+
{ .primary = "x", .secondary = "acc", .run = do_acc_x },
59+
{ .primary = "y", .secondary = "acc", .run = do_acc_y },
60+
{ .primary = "z", .secondary = "acc", .run = do_acc_z },
61+
{ .primary = "x", .secondary = "gyro", .run = do_gyr_x },
62+
{ .primary = "y", .secondary = "gyro", .run = do_gyr_y },
63+
{ .primary = "z", .secondary = "gyro", .run = do_gyr_z },
64+
};
65+
66+
static void (*get_converter(void const *primary, size_t primary_len,
67+
void const *secondary, size_t secondary_len))
68+
(cbor_reader_t const *reader, cbor_item_t const *item, void *udt)
69+
{
70+
if ((primary == NULL && secondary == NULL) ||
71+
(primary_len == 0 && secondary_len == 0)) {
72+
return do_unknown;
73+
}
74+
75+
for (size_t i = 0; i < sizeof(converters) / sizeof(converters[0]); i++) {
76+
struct converter const *p = &converters[i];
77+
if (p->primary && memcmp(p->primary, primary, primary_len)) {
78+
continue;
79+
} else if (p->secondary && memcmp(p->secondary, secondary, secondary_len)) {
80+
continue;
81+
}
82+
83+
if (p->primary || p->secondary) {
84+
return p->run;
85+
}
86+
}
87+
88+
return do_unknown;
89+
}
90+
91+
static void convert(cbor_reader_t const *reader, cbor_item_t const *item,
92+
cbor_item_t const *parent, void *udt)
93+
{
94+
void const *primary = NULL;
95+
void const *secondary = NULL;
96+
size_t primary_len = 0;
97+
size_t secondary_len = 0;
98+
99+
if (parent != NULL && parent->type == CBOR_ITEM_MAP) {
100+
if ((item - parent) % 2) { /* key */
101+
return;
102+
}
103+
104+
if (parent->offset > 1) {
105+
secondary = cbor_decode_pointer(reader, parent-1);
106+
secondary_len = (parent-1)->size;
107+
}
108+
109+
primary = cbor_decode_pointer(reader, item-1);
110+
primary_len = (item-1)->size;
111+
}
112+
113+
get_converter(primary, primary_len, secondary, secondary_len)
114+
(reader, item, udt);
115+
}
116+
117+
/*
118+
sample_data[] = { 0x9f, 0xe1, 0x50, 0x12, 0x3e, 0x45, 0x67, 0xe8, 0x9b, 0x12,
119+
0xd3, 0xa4, 0x56, 0x42, 0x66, 0x14, 0x17, 0x40, 0x00, 0xbf,
120+
0x61, 0x74, 0x1a, 0x63, 0x04, 0x4b, 0xa5, 0x64, 0x64, 0x61,
121+
0x74, 0x61, 0xa2, 0x63, 0x61, 0x63, 0x63, 0xa3, 0x61, 0x78,
122+
0xf9, 0x00, 0x00, 0x61, 0x79, 0xf9, 0x00, 0x00, 0x61, 0x7a,
123+
0xfa, 0x41, 0x1c, 0xf5, 0xc3, 0x64, 0x67, 0x79, 0x72, 0x6f,
124+
0xa3, 0x61, 0x78, 0xfa, 0x40, 0xc9, 0x99, 0x9a, 0x61, 0x79,
125+
0xf9, 0x00, 0x00, 0x61, 0x7a, 0xf9, 0x00, 0x00, 0xff, 0xff
126+
};
127+
128+
generated by:
129+
cbor_writer_init(&writer, writer_buffer, sizeof(writer_buffer));
130+
cbor_encode_array_indefinite(&writer);
131+
cbor_encode_simple(&writer, 1);
132+
cbor_encode_byte_string(&writer, uuid, sizeof(uuid));
133+
cbor_encode_map_indefinite(&writer);
134+
cbor_encode_text_string(&writer, "t");
135+
cbor_encode_unsigned_integer(&writer, 1661225893);
136+
cbor_encode_text_string(&writer, "data");
137+
cbor_encode_map(&writer, 2);
138+
cbor_encode_text_string(&writer, "acc");
139+
cbor_encode_map(&writer, 3);
140+
cbor_encode_text_string(&writer, "x");
141+
cbor_encode_float(&writer, 0.f);
142+
cbor_encode_text_string(&writer, "y");
143+
cbor_encode_float(&writer, 0.f);
144+
cbor_encode_text_string(&writer, "z");
145+
cbor_encode_float(&writer, 9.81f);
146+
cbor_encode_text_string(&writer, "gyro");
147+
cbor_encode_map(&writer, 3);
148+
cbor_encode_text_string(&writer, "x");
149+
cbor_encode_float(&writer, 6.3f);
150+
cbor_encode_text_string(&writer, "y");
151+
cbor_encode_float(&writer, 0.f);
152+
cbor_encode_text_string(&writer, "z");
153+
cbor_encode_float(&writer, 0.f);
154+
cbor_encode_break(&writer);
155+
cbor_encode_break(&writer);
156+
157+
See the test case: tests/src/example_test.cpp
158+
*/
159+
void complex_example(void const *data, size_t datasize, void *udt)
160+
{
161+
cbor_reader_t reader;
162+
cbor_item_t items[32];
163+
size_t n;
164+
165+
cbor_reader_init(&reader, data, datasize);
166+
cbor_error_t err = cbor_parse(&reader, items, sizeof(items) / sizeof(items[0]), &n);
167+
if (err == CBOR_SUCCESS || err == CBOR_BREAK) {
168+
cbor_iterate(&reader, items, n, 0, convert, udt);
169+
}
170+
}

examples/example.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#ifndef CBOR_EXAMPLE_H
2+
#define CBOR_EXAMPLE_H
3+
4+
#if defined(__cplusplus)
5+
extern "C" {
6+
#endif
7+
8+
#include <stddef.h>
9+
#include <stdint.h>
10+
#include <string.h>
11+
12+
/* User-defined Data Type */
13+
struct udt {
14+
uint32_t time;
15+
uint8_t type;
16+
uint8_t uuid[16];
17+
struct {
18+
struct {
19+
float x;
20+
float y;
21+
float z;
22+
} acc;
23+
struct {
24+
float x;
25+
float y;
26+
float z;
27+
} gyro;
28+
} data;
29+
};
30+
31+
typedef void (*example_writer)(void const *data, size_t datasize);
32+
33+
void simple_example(example_writer print);
34+
void complex_example(void const *data, size_t datasize, void *udt);
35+
36+
#if defined(__cplusplus)
37+
}
38+
#endif
39+
40+
#endif /* CBOR_EXAMPLE_H */

0 commit comments

Comments
 (0)