KPN Things Device SDK
cbor.h
Go to the documentation of this file.
1/* _ __ ____ _ _
2 * | |/ / | _ \ | \ | |
3 * | ' / | |_) | | \| |
4 * | . \ | __/ | |\ |
5 * |_|\_\ |_| |_| \_|
6 *
7 * (c) 2020 KPN
8 * License: MIT License.
9 * Author: Joseph Verburg, Jan Bogaerts
10 *
11 * cbor parsing and rendering header
12 */
13
14
15#ifndef SENMLCBOR
16#define SENMLCBOR
17
18#ifdef ARDUINO
19 #include <Stream.h>
20#endif
21#include <stddef.h>
22#include <stdint.h>
23
24#define SENML_BVER_LABEL -1
25#define SENML_CBOR_BN_LABEL -2
26#define SENML_CBOR_BT_LABEL -3
27#define SENML_CBOR_BU_LABEL -4
28#define SENML_CBOR_BV_LABEL -5
29#define SENML_CBOR_BS_LABEL -16
30#define SENML_CBOR_N_LABEL 0
31#define SENML_CBOR_U_LABEL 1
32#define SENML_CBOR_V_LABEL 2
33#define SENML_CBOR_VS_LABEL 3
34#define SENML_CBOR_VB_LABEL 4
35#define SENML_CBOR_S_LABEL 5
36#define SENML_CBOR_T_LABEL 6
37#define SENML_CBOR_UT_LABEL 7
38#define SENML_CBOR_VD_LABEL 8
39#define THINGSML_CBOR_I_LABEL 23
40
41#define CBOR_TYPE_MASK 0xE0 /* top 3 bits */
42#define CBOR_INFO_MASK 0x1F /* low 5 bits */
43
44/* Major types (cf. section 2.1) */
45/* Major type 0: Unsigned integers */
46#define CBOR_UINT8_FOLLOWS 24 /* 0x18 */
47#define CBOR_UINT16_FOLLOWS 25 /* 0x19 */
48#define CBOR_UINT32_FOLLOWS 26 /* 0x1a */
49#define CBOR_UINT64_FOLLOWS 27 /* 0x1b */
50
51#define CBOR_BYTE_FOLLOWS 24 /* indicator that the next byte is part of this item */
52
53/* Jump Table for Initial Byte (cf. table 5) */
54#define CBOR_UINT 0x00 /* type 0 */
55#define CBOR_NEGINT 0x20 /* type 1 */
56#define CBOR_BYTES 0x40 /* type 2 */
57#define CBOR_TEXT 0x60 /* type 3 */
58#define CBOR_ARRAY 0x80 /* type 4 */
59#define CBOR_MAP 0xA0 /* type 5 */
60#define CBOR_TAG 0xC0 /* type 6 */
61#define CBOR_7 0xE0 /* type 7 (float and other types) */
62
63#define CBOR_VAR_FOLLOWS 31 /* 0x1f */
64
65/* Major type 6: Semantic tagging */
66#define CBOR_DATETIME_STRING_FOLLOWS 0
67#define CBOR_DATETIME_EPOCH_FOLLOWS 1
68
69/* Major type 7: Float and other types */
70#define CBOR_FALSE (CBOR_7 | 20)
71#define CBOR_TRUE (CBOR_7 | 21)
72#define CBOR_NULL (CBOR_7 | 22)
73#define CBOR_UNDEFINED (CBOR_7 | 23)
74/* CBOR_BYTE_FOLLOWS == 24 */
75#define CBOR_FLOAT16 (CBOR_7 | 25)
76#define CBOR_FLOAT32 (CBOR_7 | 26)
77#define CBOR_FLOAT64 (CBOR_7 | 27)
78#define CBOR_BREAK (CBOR_7 | 31)
79
80#define CBOR_TYPE (peekChar() & CBOR_TYPE_MASK)
81
82
83
84bool is_lossless_to_half_float(double value);
85
86bool is_lossless_to_float(double value);
87
105size_t cbor_serialize_array(size_t array_length);
106
107
124size_t cbor_serialize_map(size_t map_length);
125
126
134size_t cbor_serialize_int(int val);
135
136
145size_t cbor_serialize_unicode_string(const char *val);
146
147
155size_t cbor_serialize_double(double val);
156
157
165size_t cbor_serialize_float(float val);
166
167
168
176size_t cbor_serialize_half_float(float val);
177
185size_t cbor_serialize_bool(bool val);
186
195size_t cbor_serialize_byte_string(const char *val, int length);
196
197
198//read integer
199size_t decode_int(uint64_t *val);
200
208size_t cbor_deserialize_int64_t(int64_t *val);
209
217size_t cbor_deserialize_uint64_t(uint64_t *val);
218
219
226
227size_t cbor_deserialize_float_half(float *val);
228size_t cbor_deserialize_float(float *val);
229size_t cbor_deserialize_double(double *val);
230
231#endif // SENMLCBOR
232
233
234
235
236
237
238
size_t cbor_serialize_int(int val)
Serializes an integer.
Definition: cbor.cpp:321
size_t cbor_serialize_unicode_string(const char *val)
Serializes a unicode string.
Definition: cbor.cpp:334
size_t cbor_serialize_byte_string(const char *val, int length)
Serializes a byte string.
Definition: cbor.cpp:457
size_t cbor_deserialize_float(float *val)
Definition: cbor.cpp:382
bool is_lossless_to_half_float(double value)
Definition: cbor.cpp:185
size_t cbor_serialize_map(size_t map_length)
Serialize map of length map_length.
Definition: cbor.cpp:315
size_t cbor_deserialize_uint64_t(uint64_t *val)
Deserialize unsigned 64 bit values from stream to val.
Definition: cbor.cpp:442
size_t cbor_serialize_float(float val)
Serializes a single precision floating point value.
Definition: cbor.cpp:348
size_t cbor_deserialize_float_half(float *val)
Definition: cbor.cpp:365
size_t cbor_at_break()
check that the char at the current position is a break char
size_t cbor_deserialize_int64_t(int64_t *val)
Deserialize signed 64 bit values from stream to val.
Definition: cbor.cpp:425
size_t cbor_serialize_double(double val)
Serializes a double precision floating point value.
Definition: cbor.cpp:356
size_t cbor_serialize_half_float(float val)
Serializes a half precision floating point value.
Definition: cbor.cpp:339
size_t cbor_deserialize_double(double *val)
Definition: cbor.cpp:400
bool is_lossless_to_float(double value)
Definition: cbor.cpp:191
size_t cbor_serialize_array(size_t array_length)
Serialize array of length array_length.
Definition: cbor.cpp:309
size_t decode_int(uint64_t *val)
Definition: cbor.cpp:257
size_t cbor_serialize_bool(bool val)
Serializes a boolean value.
Definition: cbor.cpp:450