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#include <zcbor_decode.h>
8#include <zcbor_encode.h>
9
10#if defined(ZCBOR_VERSION_MAJOR) && defined(ZCBOR_VERSION_MINOR) && ZCBOR_VERSION_MAJOR == 0 \
11 && ZCBOR_VERSION_MINOR < 8
12
14#define ZCBOR_MAJOR_TYPE(header_byte) ((zcbor_major_type_t) (((header_byte) >> 5) & 0x7))
15
16#define ZCBOR_STATE_D_COMPAT(name, num_backups, payload, payload_size, elem_count, n_flags) \
17 ZCBOR_STATE_D(name, num_backups, payload, payload_size, elem_count)
18
19static inline bool zcbor_is_indefinite_length_array(zcbor_state_t *state)
20{
21 return state->indefinite_length_array;
22}
23
24static inline bool zcbor_tstr_put_term_compat(zcbor_state_t *state, char const *str, size_t maxlen)
25{
26 return zcbor_tstr_put_term(state, str);
27}
28
29#else /* zcbor <0.8.0 */
30
31#define ZCBOR_STATE_D_COMPAT(name, num_backups, payload, payload_size, elem_count, n_flags) \
32 ZCBOR_STATE_D(name, num_backups, payload, payload_size, elem_count, n_flags)
33
34static inline bool zcbor_is_indefinite_length_array(zcbor_state_t *state)
35{
36 return state->decode_state.indefinite_length_array;
37}
38
39static inline bool zcbor_tstr_put_term_compat(zcbor_state_t *state, char const *str, size_t maxlen)
40{
41 return zcbor_tstr_put_term(state, str, maxlen);
42}
43
44#endif /* zcbor <0.8.0 */
45
46enum
47{
50};
51
53{
54 uint8_t type;
55 union
56 {
57 uint32_t u32;
58 struct zcbor_string tstr;
59 };
60};
61
63{
65 int (*decode)(zcbor_state_t *zsd, void *value);
66 void *value;
67};
68
75static inline bool zcbor_list_or_map_end(zcbor_state_t *state)
76{
78 {
79 return *state->payload == 0xff;
80 }
81
82 return state->elem_count == 0;
83}
84
96int zcbor_map_int64_decode(zcbor_state_t *zsd, void *value);
97
110int zcbor_map_tstr_decode(zcbor_state_t *zsd, void *value);
111
127int zcbor_map_decode(zcbor_state_t *zsd, struct zcbor_map_entry *entries, size_t num_entries);
128
136#define ZCBOR_U32_MAP_ENTRY(_u32, _decode, _value) \
137 { \
138 .key = \
139 { \
140 .type = ZCBOR_MAP_KEY_TYPE_U32, \
141 .u32 = _u32, \
142 }, \
143 .decode = _decode, .value = _value, \
144 }
145
153#define ZCBOR_TSTR_LIT_MAP_ENTRY(_tstr_lit, _decode, _value) \
154 { \
155 .key = \
156 { \
157 .type = ZCBOR_MAP_KEY_TYPE_TSTR, \
158 .tstr = {(const uint8_t *) _tstr_lit, sizeof(_tstr_lit) - 1}, \
159 }, \
160 .decode = _decode, .value = _value, \
161 }
Definition zcbor_utils.h:63
struct zcbor_map_key key
Definition zcbor_utils.h:64
int(* decode)(zcbor_state_t *zsd, void *value)
Definition zcbor_utils.h:65
void * value
Definition zcbor_utils.h:66
uint32_t u32
Definition zcbor_utils.h:57
struct zcbor_string tstr
Definition zcbor_utils.h:58
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:34
@ ZCBOR_MAP_KEY_TYPE_U32
Definition zcbor_utils.h:48
@ ZCBOR_MAP_KEY_TYPE_TSTR
Definition zcbor_utils.h:49
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:75
static bool zcbor_tstr_put_term_compat(zcbor_state_t *state, char const *str, size_t maxlen)
Definition zcbor_utils.h:39