Skip to content

Commit

Permalink
Rename zcbor_print() to zcbor_log()
Browse files Browse the repository at this point in the history
and zcbor_print_assert() to zcbor_log_assert()

Signed-off-by: Øyvind Rønningstad <oyvind.ronningstad@nordicsemi.no>
  • Loading branch information
oyvindronningstad committed Dec 6, 2023
1 parent 62f2f5d commit e78bb66
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 43 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ If using zcbor with Zephyr, use the [Kconfig options](https://github.com/zephyrp
Name | Description
------------------------- | -----------
`ZCBOR_CANONICAL` | Assume canonical encoding (AKA "deterministically encoded CBOR"). When encoding lists and maps, do not use indefinite length encoding. Enabling `ZCBOR_CANONICAL` increases code size and makes the encoding library more often use state backups. When decoding, ensure that the incoming data conforms to canonical encoding, i.e. no indefinite length encoding, and always using minimal length encoding (e.g. not using 16 bits to encode a value < 256). Note: the map ordering constraint in canonical encoding is not checked.
`ZCBOR_VERBOSE` | Print messages on encoding/decoding errors (`zcbor_print()`), and also a trace message (`zcbor_trace()`) for each decoded value, and in each generated function (when using code generation). Requires `printk` as found in Zephyr.
`ZCBOR_VERBOSE` | Print log messages on encoding/decoding errors (`zcbor_log()`), and also a trace message (`zcbor_trace()`) for each decoded value, and in each generated function (when using code generation). Requires `printk` as found in Zephyr.
`ZCBOR_ASSERTS` | Enable asserts (`zcbor_assert()`). When they fail, the assert statements instruct the current function to return a `ZCBOR_ERR_ASSERTION` error. If `ZCBOR_VERBOSE` is enabled, a message is printed.
`ZCBOR_STOP_ON_ERROR` | Enable the `stop_on_error` functionality. This makes all functions abort their execution if called when an error has already happened.
`ZCBOR_BIG_ENDIAN` | All decoded values are returned as big-endian. The default is little-endian.
Expand Down
12 changes: 6 additions & 6 deletions include/zcbor_print.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,32 @@ extern "C" {
(size_t)state->payload_end - (size_t)state->payload, *state->payload, state->elem_count, \
state->constant_state ? state->constant_state->error : 0, __FILE__, __LINE__))

#define zcbor_print_assert(expr, ...) \
#define zcbor_log_assert(expr, ...) \
do { \
zcbor_do_print("ASSERTION \n \"" #expr \
"\"\nfailed at %s:%d with message:\n ", \
__FILE__, __LINE__); \
zcbor_do_print(__VA_ARGS__);\
} while(0)
#define zcbor_print(...) zcbor_do_print(__VA_ARGS__)
#define zcbor_log(...) zcbor_do_print(__VA_ARGS__)
#else
#define zcbor_trace() ((void)state)
#define zcbor_print_assert(...)
#define zcbor_print(...)
#define zcbor_log_assert(...)
#define zcbor_log(...)
#endif

#ifdef ZCBOR_ASSERTS
#define zcbor_assert(expr, ...) \
do { \
if (!(expr)) { \
zcbor_print_assert(expr, __VA_ARGS__); \
zcbor_log_assert(expr, __VA_ARGS__); \
ZCBOR_FAIL(); \
} \
} while(0)
#define zcbor_assert_state(expr, ...) \
do { \
if (!(expr)) { \
zcbor_print_assert(expr, __VA_ARGS__); \
zcbor_log_assert(expr, __VA_ARGS__); \
ZCBOR_ERR(ZCBOR_ERR_ASSERTION); \
} \
} while(0)
Expand Down
2 changes: 1 addition & 1 deletion samples/pet/src/pet_decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static bool decode_Pet(zcbor_state_t *state, struct Pet *result);
static bool decode_Pet(
zcbor_state_t *state, struct Pet *result)
{
zcbor_print("%s\r\n", __func__);
zcbor_log("%s\r\n", __func__);

bool tmp_result = (((zcbor_list_start_decode(state) && ((((zcbor_list_start_decode(state) && ((zcbor_multi_decode(1, 3, &(*result).names_count, (zcbor_decoder_t *)zcbor_tstr_decode, state, (&(*result).names), sizeof(struct zcbor_string))) || (zcbor_list_map_end_force_decode(state), false)) && zcbor_list_end_decode(state)))
&& ((zcbor_bstr_decode(state, (&(*result).birthday)))
Expand Down
2 changes: 1 addition & 1 deletion samples/pet/src/pet_encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static bool encode_Pet(zcbor_state_t *state, const struct Pet *input);
static bool encode_Pet(
zcbor_state_t *state, const struct Pet *input)
{
zcbor_print("%s\r\n", __func__);
zcbor_log("%s\r\n", __func__);

bool tmp_result = (((zcbor_list_start_encode(state, 3) && ((((zcbor_list_start_encode(state, 3) && ((zcbor_multi_encode_minmax(1, 3, &(*input).names_count, (zcbor_encoder_t *)zcbor_tstr_encode, state, (&(*input).names), sizeof(struct zcbor_string))) || (zcbor_list_map_end_force_encode(state), false)) && zcbor_list_end_encode(state, 3)))
&& (((((((*input).birthday.len >= 8)
Expand Down
8 changes: 4 additions & 4 deletions src/zcbor_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ bool zcbor_process_backup(zcbor_state_t *state, uint32_t flags,
zcbor_state_t local_copy = *state;

if (state->constant_state->current_backup == 0) {
zcbor_print("No backups available.\r\n");
zcbor_log("No backups available.\r\n");
ZCBOR_ERR(ZCBOR_ERR_NO_BACKUP_ACTIVE);
}

Expand All @@ -60,7 +60,7 @@ bool zcbor_process_backup(zcbor_state_t *state, uint32_t flags,

if (!(flags & ZCBOR_FLAG_KEEP_PAYLOAD)) {
if (state->constant_state->backup_list[i].payload_moved) {
zcbor_print("Payload pointer out of date.\r\n");
zcbor_log("Payload pointer out of date.\r\n");
ZCBOR_ERR(ZCBOR_ERR_PAYLOAD_OUTDATED);
}
}
Expand All @@ -73,7 +73,7 @@ bool zcbor_process_backup(zcbor_state_t *state, uint32_t flags,
}

if (local_copy.elem_count > max_elem_count) {
zcbor_print("elem_count: %" PRIuFAST32 " (expected max %" PRIuFAST32 ")\r\n",
zcbor_log("elem_count: %" PRIuFAST32 " (expected max %" PRIuFAST32 ")\r\n",
local_copy.elem_count, max_elem_count);
ZCBOR_ERR(ZCBOR_ERR_HIGH_ELEM_COUNT);
}
Expand Down Expand Up @@ -289,7 +289,7 @@ int zcbor_entry_function(const uint8_t *payload, size_t payload_len,
int err = zcbor_pop_error(state);

err = (err == ZCBOR_SUCCESS) ? ZCBOR_ERR_UNKNOWN : err;
zcbor_print("Return error: %d\r\n", err);
zcbor_log("Return error: %d\r\n", err);
return err;
}

Expand Down
24 changes: 12 additions & 12 deletions src/zcbor_decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ bool zcbor_uint_decode(zcbor_state_t *state, void *result, size_t result_size)
INITIAL_CHECKS_WITH_TYPE(ZCBOR_MAJOR_TYPE_PINT);

if (!value_extract(state, result, result_size)) {
zcbor_print("uint with size %d failed.\r\n", result_size);
zcbor_log("uint with size %d failed.\r\n", result_size);
ZCBOR_FAIL();
}
return true;
Expand Down Expand Up @@ -281,7 +281,7 @@ bool zcbor_int64_expect(zcbor_state_t *state, int64_t expected)
}

if (actual != expected) {
zcbor_print("%" PRIi64 " != %" PRIi64 "\r\n", actual, expected);
zcbor_log("%" PRIi64 " != %" PRIi64 "\r\n", actual, expected);
ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE);
}
return true;
Expand Down Expand Up @@ -328,7 +328,7 @@ bool zcbor_uint64_expect(zcbor_state_t *state, uint64_t expected)
ZCBOR_FAIL();
}
if (actual != expected) {
zcbor_print("%" PRIu64 " != %" PRIu64 "\r\n", actual, expected);
zcbor_log("%" PRIu64 " != %" PRIu64 "\r\n", actual, expected);
ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE);
}
return true;
Expand Down Expand Up @@ -380,7 +380,7 @@ static bool str_start_decode_with_overflow_check(zcbor_state_t *state,
/* Casting to size_t is safe since str_start_decode() checks that
* payload_end is bigger that payload. */
if (result->len > (size_t)(state->payload_end - state->payload)) {
zcbor_print("error: 0x%zu > 0x%zu\r\n",
zcbor_log("error: 0x%zu > 0x%zu\r\n",
result->len,
(state->payload_end - state->payload));
ERR_RESTORE(ZCBOR_ERR_NO_PAYLOAD);
Expand Down Expand Up @@ -471,7 +471,7 @@ void zcbor_next_fragment(zcbor_state_t *state,
result->fragment.len = result->total_len - result->offset;

partition_fragment(state, result);
zcbor_print("New fragment length %zu\r\n", result->fragment.len);
zcbor_log("New fragment length %zu\r\n", result->fragment.len);

state->payload += result->fragment.len;
}
Expand All @@ -487,7 +487,7 @@ void zcbor_bstr_next_fragment(zcbor_state_t *state,
result->fragment.len = result->total_len - result->offset;

partition_fragment(state, result);
zcbor_print("fragment length %zu\r\n", result->fragment.len);
zcbor_log("fragment length %zu\r\n", result->fragment.len);
state->payload_end = state->payload + result->fragment.len;
}

Expand Down Expand Up @@ -837,7 +837,7 @@ static bool try_key(zcbor_state_t *state, void *key_result, zcbor_decoder_t key_
return false;
}

zcbor_print("Found element at index %d.\n", get_current_index(state, 1));
zcbor_log("Found element at index %d.\n", get_current_index(state, 1));
return true;
}

Expand Down Expand Up @@ -978,7 +978,7 @@ bool zcbor_unordered_map_end_decode(zcbor_state_t *state)

for (size_t i = 0; i < zcbor_flags_to_bytes(state->decode_state.map_elem_count); i++) {
if (state->decode_state.map_search_elem_state[i] != 0) {
zcbor_print("unprocessed element(s) in map: [%d] = 0x%02x\n",
zcbor_log("unprocessed element(s) in map: [%d] = 0x%02x\n",
i, state->decode_state.map_search_elem_state[i]);
ZCBOR_ERR(ZCBOR_ERR_ELEMS_NOT_PROCESSED);
}
Expand Down Expand Up @@ -1030,7 +1030,7 @@ bool zcbor_simple_expect(zcbor_state_t *state, uint8_t expected)
}

if (actual != expected) {
zcbor_print("simple value %u != %u\r\n", actual, expected);
zcbor_log("simple value %u != %u\r\n", actual, expected);
ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE);
}

Expand Down Expand Up @@ -1071,7 +1071,7 @@ bool zcbor_bool_decode(zcbor_state_t *state, bool *result)
}
*result = value;

zcbor_print("boolval: %u\r\n", *result);
zcbor_log("boolval: %u\r\n", *result);
return true;
}

Expand Down Expand Up @@ -1455,11 +1455,11 @@ bool zcbor_multi_decode(size_t min_decode,
state->payload = payload_bak;
state->elem_count = elem_count_bak;
ZCBOR_ERR_IF(i < min_decode, ZCBOR_ERR_ITERATIONS);
zcbor_print("Found %" PRIuFAST32 " elements.\r\n", i);
zcbor_log("Found %" PRIuFAST32 " elements.\r\n", i);
return true;
}
}
zcbor_print("Found %" PRIuFAST32 " elements.\r\n", max_decode);
zcbor_log("Found %" PRIuFAST32 " elements.\r\n", max_decode);
*num_decode = max_decode;
return true;
}
Expand Down
12 changes: 6 additions & 6 deletions src/zcbor_encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static uint8_t log2ceil(size_t val)
case 8: return 3;
}

zcbor_print("Should not come here.\r\n");
zcbor_log("Should not come here.\r\n");
return 0;
}

Expand Down Expand Up @@ -140,7 +140,7 @@ bool zcbor_int_encode(zcbor_state_t *state, const void *input_int, size_t int_si
bool zcbor_uint_encode(zcbor_state_t *state, const void *input_uint, size_t uint_size)
{
if (!value_encode(state, ZCBOR_MAJOR_TYPE_PINT, input_uint, uint_size)) {
zcbor_print("uint with size %d failed.\r\n", uint_size);
zcbor_log("uint with size %d failed.\r\n", uint_size);
ZCBOR_FAIL();
}
return true;
Expand Down Expand Up @@ -389,14 +389,14 @@ static bool list_map_end_encode(zcbor_state_t *state, size_t max_num,
ZCBOR_FAIL();
}

zcbor_print("list_count: %" PRIuFAST32 "\r\n", list_count);
zcbor_log("list_count: %" PRIuFAST32 "\r\n", list_count);


/** If max_num is smaller than the actual number of encoded elements,
* the value_encode() below will corrupt the data if the encoded
* header is larger than the previously encoded header. */
if (header_len > max_header_len) {
zcbor_print("max_num too small.\r\n");
zcbor_log("max_num too small.\r\n");
ZCBOR_ERR(ZCBOR_ERR_HIGH_ELEM_COUNT);
}

Expand Down Expand Up @@ -455,7 +455,7 @@ bool zcbor_list_map_end_force_encode(zcbor_state_t *state)
bool zcbor_simple_encode(zcbor_state_t *state, uint8_t *input)
{
if (!value_encode(state, ZCBOR_MAJOR_TYPE_SIMPLE, input, sizeof(*input))) {
zcbor_print("Error encoding %u (0x%p)\r\n", *input, input);
zcbor_log("Error encoding %u (0x%p)\r\n", *input, input);
ZCBOR_FAIL();
}
return true;
Expand Down Expand Up @@ -596,7 +596,7 @@ bool zcbor_multi_encode(const size_t num_encode, zcbor_encoder_t encoder,
ZCBOR_FAIL();
}
}
zcbor_print("Encoded %" PRIuFAST32 " elements.\n", num_encode);
zcbor_log("Encoded %" PRIuFAST32 " elements.\n", num_encode);
return true;
}

Expand Down
8 changes: 4 additions & 4 deletions tests/decode/test2_suit/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ ZTEST(cbor_decode_test2, test_5)
uint8_t expected_component1[] = {0x00, 0x34, 0x01};
int res;

zcbor_print("test_vector at: 0x%zu\r\n", (size_t)test_vector1);
zcbor_print("test_vector end at: 0x%zu\r\n",
zcbor_log("test_vector at: 0x%zu\r\n", (size_t)test_vector1);
zcbor_log("test_vector end at: 0x%zu\r\n",
((size_t)test_vector1) + sizeof(test_vector1));
memset(&outerwrapper1, 0, sizeof(outerwrapper1));
res = cbor_decode_SUIT_Outer_Wrapper(test_vector1, sizeof(test_vector1),
Expand Down Expand Up @@ -124,7 +124,7 @@ ZTEST(cbor_decode_test2, test_5)
sizeof(expected_component1),
"component elem 1 doesn't match.");

zcbor_print("\r\n");
zcbor_log("\r\n");
zassert_equal(1, manifest
->SUIT_Manifest_suit_common
.SUIT_Manifest_suit_common_cbor
Expand Down Expand Up @@ -165,7 +165,7 @@ ZTEST(cbor_decode_test2, test_5)
.SUIT_Command_SUIT_Directive_m
.suit_directive_set_parameters_m_l_map_SUIT_Parameters_m_count,
"Should be two vars (parameters).");
zcbor_print("\r\n");
zcbor_log("\r\n");

memset(&sequence, 0, sizeof(sequence));
res = cbor_decode_SUIT_Command_Sequence(
Expand Down
8 changes: 4 additions & 4 deletions tests/decode/test7_suit9_simple/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ ZTEST(cbor_decode_test7, test_suit9_simple2)
{
int res;

zcbor_print("test_vector at: 0x%zx\r\n", (size_t)test_vector2);
zcbor_print("test_vector end at: 0x%zx\r\n",
zcbor_log("test_vector at: 0x%zx\r\n", (size_t)test_vector2);
zcbor_log("test_vector end at: 0x%zx\r\n",
((size_t)test_vector2) + sizeof(test_vector2));
memset(&envelope1, 0, sizeof(envelope1));
res = cbor_decode_SUIT_Envelope(test_vector2,
Expand All @@ -115,8 +115,8 @@ ZTEST(cbor_decode_test7, test_suit9_simple5)
{
int res;

zcbor_print("test_vector at: 0x%zx\r\n", (size_t)test_vector5);
zcbor_print("test_vector end at: 0x%zx\r\n",
zcbor_log("test_vector at: 0x%zx\r\n", (size_t)test_vector5);
zcbor_log("test_vector end at: 0x%zx\r\n",
((size_t)test_vector5) + sizeof(test_vector5));
memset(&envelope1, 0, sizeof(envelope1));
res = cbor_decode_SUIT_Envelope(test_vector5,
Expand Down
6 changes: 3 additions & 3 deletions tests/encode/test1_suit/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ void test_command_sequence(struct zcbor_string *sequence_str,
return;
}

zcbor_print("\r\ntest %s\r\n", name);
zcbor_log("\r\ntest %s\r\n", name);

memset(&sequence1, 0, sizeof(sequence1));
int res = cbor_decode_SUIT_Command_Sequence(sequence_str->value,
Expand Down Expand Up @@ -337,8 +337,8 @@ void test_manifest(const uint8_t *input, uint32_t len)
int res;
size_t out_len;

zcbor_print("test_vector at: 0x%zx\r\n", (size_t)input);
zcbor_print("test_vector end at: 0x%zx\r\n",
zcbor_log("test_vector at: 0x%zx\r\n", (size_t)input);
zcbor_log("test_vector end at: 0x%zx\r\n",
((size_t)input) + len);
res = cbor_decode_SUIT_Outer_Wrapper(input, len, &outerwrapper1, NULL);
zassert_equal(ZCBOR_SUCCESS, res, "top-level decoding failed.");
Expand Down
2 changes: 1 addition & 1 deletion zcbor/zcbor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2651,7 +2651,7 @@ def render_function(self, xcoder, mode):
xcoder.type_name
if struct_ptr_name(mode) in body else "void"} *{struct_ptr_name(mode)})
{{
zcbor_print("%s\\r\\n", __func__);
zcbor_log("%s\\r\\n", __func__);
{"struct zcbor_string tmp_str;" if "tmp_str" in body else ""}
{"bool int_res;" if "int_res" in body else ""}
Expand Down

0 comments on commit e78bb66

Please sign in to comment.