Golioth Firmware SDK
Loading...
Searching...
No Matches
zcbor_utils.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023 Golioth, Inc.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifdef __cplusplus
8extern "C"
9{
10#endif
11
12#include <zcbor_decode.h>
13#include <zcbor_encode.h>
14
15#if defined(ZCBOR_VERSION_MAJOR) && defined(ZCBOR_VERSION_MINOR) && ZCBOR_VERSION_MAJOR == 0 \
16 && ZCBOR_VERSION_MINOR < 8
17
19#define ZCBOR_MAJOR_TYPE(header_byte) ((zcbor_major_type_t) (((header_byte) >> 5) & 0x7))
20
21#define ZCBOR_STATE_D_COMPAT(name, num_backups, payload, payload_size, elem_count, n_flags) \
22 ZCBOR_STATE_D(name, num_backups, payload, payload_size, elem_count)
23
24static inline bool zcbor_is_indefinite_length_array(zcbor_state_t *state)
25{
26 return state->indefinite_length_array;
27}
28
29static inline bool zcbor_tstr_put_term_compat(zcbor_state_t *state, char const *str, size_t maxlen)
30{
31 return zcbor_tstr_put_term(state, str);
32}
33
34#else /* zcbor <0.8.0 */
35
36#define ZCBOR_STATE_D_COMPAT(name, num_backups, payload, payload_size, elem_count, n_flags) \
37 ZCBOR_STATE_D(name, num_backups, payload, payload_size, elem_count, n_flags)
38
39static inline bool zcbor_is_indefinite_length_array(zcbor_state_t *state)
40{
41 return state->decode_state.indefinite_length_array;
42}
43
44static inline bool zcbor_tstr_put_term_compat(zcbor_state_t *state, char const *str, size_t maxlen)
45{
46 return zcbor_tstr_put_term(state, str, maxlen);
47}
48
49#endif /* zcbor <0.8.0 */
50
51enum
52{
55};
56
58{
59 uint8_t type;
60 union
61 {
62 uint32_t u32;
63 struct zcbor_string tstr;
64 };
65};
66
68{
70 int (*decode)(zcbor_state_t *zsd, void *value);
71 void *value;
72};
73
80static inline bool zcbor_list_or_map_end(zcbor_state_t *state)
81{
83 {
84 return *state->payload == 0xff;
85 }
86
87 return state->elem_count == 0;
88}
89
101int zcbor_map_int64_decode(zcbor_state_t *zsd, void *value);
102
115int zcbor_map_tstr_decode(zcbor_state_t *zsd, void *value);
116
132int zcbor_map_decode(zcbor_state_t *zsd, struct zcbor_map_entry *entries, size_t num_entries);
133
141#define ZCBOR_U32_MAP_ENTRY(_u32, _decode, _value) \
142 { \
143 .key = \
144 { \
145 .type = ZCBOR_MAP_KEY_TYPE_U32, \
146 .u32 = _u32, \
147 }, \
148 .decode = _decode, .value = _value, \
149 }
150
158#define ZCBOR_TSTR_LIT_MAP_ENTRY(_tstr_lit, _decode, _value) \
159 { \
160 .key = \
161 { \
162 .type = ZCBOR_MAP_KEY_TYPE_TSTR, \
163 .tstr = {(const uint8_t *) _tstr_lit, sizeof(_tstr_lit) - 1}, \
164 }, \
165 .decode = _decode, .value = _value, \
166 }
167
168#ifdef __cplusplus
169}
170#endif
Definition zcbor_utils.h:68
struct zcbor_map_key key
Definition zcbor_utils.h:69
int(* decode)(zcbor_state_t *zsd, void *value)
Definition zcbor_utils.h:70
void * value
Definition zcbor_utils.h:71
uint32_t u32
Definition zcbor_utils.h:62
struct zcbor_string tstr
Definition zcbor_utils.h:63
int zcbor_map_tstr_decode(zcbor_state_t *zsd, void *value)
Decode text string value from CBOR map.
static bool zcbor_is_indefinite_length_array(zcbor_state_t *state)
Definition zcbor_utils.h:39
@ ZCBOR_MAP_KEY_TYPE_U32
Definition zcbor_utils.h:53
@ ZCBOR_MAP_KEY_TYPE_TSTR
Definition zcbor_utils.h:54
int zcbor_map_int64_decode(zcbor_state_t *zsd, void *value)
Decode int64_t value from CBOR map.
int zcbor_map_decode(zcbor_state_t *zsd, struct zcbor_map_entry *entries, size_t num_entries)
Decode CBOR map with specified entries.
static bool zcbor_list_or_map_end(zcbor_state_t *state)
Check the end of CBOR list or map.
Definition zcbor_utils.h:80
static bool zcbor_tstr_put_term_compat(zcbor_state_t *state, char const *str, size_t maxlen)
Definition zcbor_utils.h:44