From 7ad302e7ceb40dabfb28acb644c1c41e6bd46faf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20R=C3=B8nningstad?= Date: Wed, 29 Nov 2023 12:07:38 +0100 Subject: [PATCH 1/5] zcbor_print.h: Move printing code to a new file zcbor_print.h MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From zcbor_debug.h (deleted) and zcbor_common.h Signed-off-by: Øyvind Rønningstad --- include/zcbor_common.h | 40 ----- include/zcbor_debug.h | 103 ------------- include/zcbor_print.h | 145 ++++++++++++++++++ samples/pet/src/pet_decode.c | 1 + samples/pet/src/pet_encode.c | 1 + src/zcbor_common.c | 1 + src/zcbor_decode.c | 1 + src/zcbor_encode.c | 1 + .../decode/test1_suit_old_formats/src/main.c | 1 - tests/decode/test2_suit/src/main.c | 2 +- tests/decode/test3_simple/src/main.c | 1 - tests/decode/test5_corner_cases/src/main.c | 1 - tests/decode/test7_suit9_simple/src/main.c | 2 +- tests/decode/test8_suit12/src/main.c | 1 - tests/decode/test9_manifest14/src/main.c | 1 - tests/encode/test1_suit/src/main.c | 2 +- tests/encode/test2_simple/src/main.c | 1 - tests/encode/test3_corner_cases/src/main.c | 1 - tests/encode/test4_senml/src/main.c | 1 - tests/unit/test1_unit_tests/src/main.c | 2 +- tests/unit/test3_float16/src/main.c | 1 - zcbor/zcbor.py | 3 +- 22 files changed, 156 insertions(+), 157 deletions(-) delete mode 100644 include/zcbor_debug.h create mode 100644 include/zcbor_print.h diff --git a/include/zcbor_common.h b/include/zcbor_common.h index d5059ce1..b77b199b 100644 --- a/include/zcbor_common.h +++ b/include/zcbor_common.h @@ -40,46 +40,6 @@ struct zcbor_string_fragment { /** Size to use in struct zcbor_string_fragment when the real size is unknown. */ #define ZCBOR_STRING_FRAGMENT_UNKNOWN_LENGTH SIZE_MAX -#ifdef ZCBOR_VERBOSE -#include -#define zcbor_trace() (printk("bytes left: %zu, byte: 0x%x, elem_count: 0x%x, err: %d, %s:%d\n",\ - (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, ...) \ -do { \ - printk("ASSERTION \n \"" #expr \ - "\"\nfailed at %s:%d with message:\n ", \ - __FILE__, __LINE__); \ - printk(__VA_ARGS__);\ -} while(0) -#define zcbor_print(...) printk(__VA_ARGS__) -#else -#define zcbor_trace() ((void)state) -#define zcbor_print_assert(...) -#define zcbor_print(...) -#endif - -#ifdef ZCBOR_ASSERTS -#define zcbor_assert(expr, ...) \ -do { \ - if (!(expr)) { \ - zcbor_print_assert(expr, __VA_ARGS__); \ - ZCBOR_FAIL(); \ - } \ -} while(0) -#define zcbor_assert_state(expr, ...) \ -do { \ - if (!(expr)) { \ - zcbor_print_assert(expr, __VA_ARGS__); \ - ZCBOR_ERR(ZCBOR_ERR_ASSERTION); \ - } \ -} while(0) -#else -#define zcbor_assert(expr, ...) -#define zcbor_assert_state(expr, ...) -#endif - #ifndef MIN #define MIN(a, b) (((a) < (b)) ? (a) : (b)) #endif diff --git a/include/zcbor_debug.h b/include/zcbor_debug.h deleted file mode 100644 index 158a37a0..00000000 --- a/include/zcbor_debug.h +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright (c) 2020 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#ifndef ZCBOR_DEBUG_H__ -#define ZCBOR_DEBUG_H__ - -#include -#include -#include -#include "zcbor_common.h" - -#ifdef __cplusplus -extern "C" { -#endif - -__attribute__((used)) -static void zcbor_print_compare_lines(const uint8_t *str1, const uint8_t *str2, uint32_t size) -{ - for (uint32_t j = 0; j < size; j++) { - printk ("%x ", str1[j]); - } - printk("\r\n"); - for (uint32_t j = 0; j < size; j++) { - printk ("%x ", str2[j]); - } - printk("\r\n"); - for (uint32_t j = 0; j < size; j++) { - printk ("%x ", str1[j] != str2[j]); - } - printk("\r\n"); - printk("\r\n"); -} - -__attribute__((used)) -static void zcbor_print_compare_strings(const uint8_t *str1, const uint8_t *str2, uint32_t size) -{ - for (uint32_t i = 0; i <= size / 16; i++) { - printk("line %d (char %d)\r\n", i, i*16); - zcbor_print_compare_lines(&str1[i*16], &str2[i*16], - MIN(16, (size - i*16))); - } - printk("\r\n"); -} - -__attribute__((used)) -static void zcbor_print_compare_strings_diff(const uint8_t *str1, const uint8_t *str2, uint32_t size) -{ - bool printed = false; - for (uint32_t i = 0; i <= size / 16; i++) { - if (memcmp(&str1[i*16], &str2[i*16], MIN(16, (size - i*16)) != 0)) { - printk("line %d (char %d)\r\n", i, i*16); - zcbor_print_compare_lines(&str1[i*16], &str2[i*16], - MIN(16, (size - i*16))); - printed = true; - } - } - if (printed) { - printk("\r\n"); - } -} - -__attribute__((used)) -static const char *zcbor_error_str(int error) -{ - #define ZCBOR_ERR_CASE(err) case err: { \ - return #err; /* The literal is static per C99 6.4.5 paragraph 5. */\ - } - switch(error) { - ZCBOR_ERR_CASE(ZCBOR_SUCCESS) - ZCBOR_ERR_CASE(ZCBOR_ERR_NO_BACKUP_MEM) - ZCBOR_ERR_CASE(ZCBOR_ERR_NO_BACKUP_ACTIVE) - ZCBOR_ERR_CASE(ZCBOR_ERR_LOW_ELEM_COUNT) - ZCBOR_ERR_CASE(ZCBOR_ERR_HIGH_ELEM_COUNT) - ZCBOR_ERR_CASE(ZCBOR_ERR_INT_SIZE) - ZCBOR_ERR_CASE(ZCBOR_ERR_FLOAT_SIZE) - ZCBOR_ERR_CASE(ZCBOR_ERR_ADDITIONAL_INVAL) - ZCBOR_ERR_CASE(ZCBOR_ERR_NO_PAYLOAD) - ZCBOR_ERR_CASE(ZCBOR_ERR_PAYLOAD_NOT_CONSUMED) - ZCBOR_ERR_CASE(ZCBOR_ERR_WRONG_TYPE) - ZCBOR_ERR_CASE(ZCBOR_ERR_WRONG_VALUE) - ZCBOR_ERR_CASE(ZCBOR_ERR_WRONG_RANGE) - ZCBOR_ERR_CASE(ZCBOR_ERR_ITERATIONS) - ZCBOR_ERR_CASE(ZCBOR_ERR_ASSERTION) - } - #undef ZCBOR_ERR_CASE - - return "ZCBOR_ERR_UNKNOWN"; -} - -__attribute__((used)) -static void zcbor_print_error(int error) -{ - printk("%s\r\n", zcbor_error_str(error)); -} - -#ifdef __cplusplus -} -#endif - -#endif /* ZCBOR_DEBUG_H__ */ diff --git a/include/zcbor_print.h b/include/zcbor_print.h new file mode 100644 index 00000000..13a8d90b --- /dev/null +++ b/include/zcbor_print.h @@ -0,0 +1,145 @@ +/* + * Copyright (c) 2023 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef ZCBOR_PRINT_H__ +#define ZCBOR_PRINT_H__ + + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef ZCBOR_PRINT_FUNC +#include +#define zcbor_do_print(...) printk(__VA_ARGS__) +#else +#define zcbor_do_print(...) ZCBOR_PRINT_FUNC(__VA_ARGS__) +#endif + +#ifdef ZCBOR_VERBOSE +#define zcbor_trace() (zcbor_do_print("bytes left: %zu, byte: 0x%x, elem_count: 0x%zx, err: %d, %s:%d\n",\ + (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, ...) \ +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__) +#else +#define zcbor_trace() ((void)state) +#define zcbor_print_assert(...) +#define zcbor_print(...) +#endif + +#ifdef ZCBOR_ASSERTS +#define zcbor_assert(expr, ...) \ +do { \ + if (!(expr)) { \ + zcbor_print_assert(expr, __VA_ARGS__); \ + ZCBOR_FAIL(); \ + } \ +} while(0) +#define zcbor_assert_state(expr, ...) \ +do { \ + if (!(expr)) { \ + zcbor_print_assert(expr, __VA_ARGS__); \ + ZCBOR_ERR(ZCBOR_ERR_ASSERTION); \ + } \ +} while(0) +#else +#define zcbor_assert(expr, ...) +#define zcbor_assert_state(expr, ...) +#endif + +__attribute__((used)) +static void zcbor_print_compare_lines(const uint8_t *str1, const uint8_t *str2, uint32_t size) +{ + for (uint32_t j = 0; j < size; j++) { + zcbor_do_print("%x ", str1[j]); + } + zcbor_do_print("\r\n"); + for (uint32_t j = 0; j < size; j++) { + zcbor_do_print("%x ", str2[j]); + } + zcbor_do_print("\r\n"); + for (uint32_t j = 0; j < size; j++) { + zcbor_do_print("%x ", str1[j] != str2[j]); + } + zcbor_do_print("\r\n"); + zcbor_do_print("\r\n"); +} + +__attribute__((used)) +static void zcbor_print_compare_strings(const uint8_t *str1, const uint8_t *str2, uint32_t size) +{ + for (uint32_t i = 0; i <= size / 16; i++) { + zcbor_do_print("line %d (char %d)\r\n", i, i*16); + zcbor_print_compare_lines(&str1[i*16], &str2[i*16], + MIN(16, (size - i*16))); + } + zcbor_do_print("\r\n"); +} + +__attribute__((used)) +static void zcbor_print_compare_strings_diff(const uint8_t *str1, const uint8_t *str2, uint32_t size) +{ + bool printed = false; + for (uint32_t i = 0; i <= size / 16; i++) { + if (memcmp(&str1[i*16], &str2[i*16], MIN(16, (size - i*16)) != 0)) { + zcbor_do_print("line %d (char %d)\r\n", i, i*16); + zcbor_print_compare_lines(&str1[i*16], &str2[i*16], + MIN(16, (size - i*16))); + printed = true; + } + } + if (printed) { + zcbor_do_print("\r\n"); + } +} + +__attribute__((used)) +static const char *zcbor_error_str(int error) +{ + #define ZCBOR_ERR_CASE(err) case err: \ + return #err; /* The literal is static per C99 6.4.5 paragraph 5. */\ + + switch(error) { + ZCBOR_ERR_CASE(ZCBOR_SUCCESS) + ZCBOR_ERR_CASE(ZCBOR_ERR_NO_BACKUP_MEM) + ZCBOR_ERR_CASE(ZCBOR_ERR_NO_BACKUP_ACTIVE) + ZCBOR_ERR_CASE(ZCBOR_ERR_LOW_ELEM_COUNT) + ZCBOR_ERR_CASE(ZCBOR_ERR_HIGH_ELEM_COUNT) + ZCBOR_ERR_CASE(ZCBOR_ERR_INT_SIZE) + ZCBOR_ERR_CASE(ZCBOR_ERR_FLOAT_SIZE) + ZCBOR_ERR_CASE(ZCBOR_ERR_ADDITIONAL_INVAL) + ZCBOR_ERR_CASE(ZCBOR_ERR_NO_PAYLOAD) + ZCBOR_ERR_CASE(ZCBOR_ERR_PAYLOAD_NOT_CONSUMED) + ZCBOR_ERR_CASE(ZCBOR_ERR_WRONG_TYPE) + ZCBOR_ERR_CASE(ZCBOR_ERR_WRONG_VALUE) + ZCBOR_ERR_CASE(ZCBOR_ERR_WRONG_RANGE) + ZCBOR_ERR_CASE(ZCBOR_ERR_ITERATIONS) + ZCBOR_ERR_CASE(ZCBOR_ERR_ASSERTION) + } + #undef ZCBOR_ERR_CASE + + return "ZCBOR_ERR_UNKNOWN"; +} + +__attribute__((used)) +static void zcbor_print_error(int error) +{ + zcbor_do_print("%s\r\n", zcbor_error_str(error)); +} + +#ifdef __cplusplus +} +#endif + +#endif /* ZCBOR_PRINT_H__ */ diff --git a/samples/pet/src/pet_decode.c b/samples/pet/src/pet_decode.c index 417a39a2..47ebaf49 100644 --- a/samples/pet/src/pet_decode.c +++ b/samples/pet/src/pet_decode.c @@ -14,6 +14,7 @@ #include #include "zcbor_decode.h" #include "pet_decode.h" +#include "zcbor_print.h" #if DEFAULT_MAX_QTY != 3 #error "The type file was generated with a different default_max_qty than this file" diff --git a/samples/pet/src/pet_encode.c b/samples/pet/src/pet_encode.c index 9accf9d8..d2777bdb 100644 --- a/samples/pet/src/pet_encode.c +++ b/samples/pet/src/pet_encode.c @@ -14,6 +14,7 @@ #include #include "zcbor_encode.h" #include "pet_encode.h" +#include "zcbor_print.h" #if DEFAULT_MAX_QTY != 3 #error "The type file was generated with a different default_max_qty than this file" diff --git a/src/zcbor_common.c b/src/zcbor_common.c index e90beca5..634094fd 100644 --- a/src/zcbor_common.c +++ b/src/zcbor_common.c @@ -9,6 +9,7 @@ #include #include #include "zcbor_common.h" +#include "zcbor_print.h" _Static_assert((sizeof(size_t) == sizeof(void *)), "This code needs size_t to be the same length as pointers."); diff --git a/src/zcbor_decode.c b/src/zcbor_decode.c index 417509aa..146d2fff 100644 --- a/src/zcbor_decode.c +++ b/src/zcbor_decode.c @@ -10,6 +10,7 @@ #include #include "zcbor_decode.h" #include "zcbor_common.h" +#include "zcbor_print.h" /** Return value length from additional value. diff --git a/src/zcbor_encode.c b/src/zcbor_encode.c index 46b15f4b..38d3c5b4 100644 --- a/src/zcbor_encode.c +++ b/src/zcbor_encode.c @@ -10,6 +10,7 @@ #include #include "zcbor_encode.h" #include "zcbor_common.h" +#include "zcbor_print.h" _Static_assert((sizeof(size_t) == sizeof(void *)), "This code needs size_t to be the same length as pointers."); diff --git a/tests/decode/test1_suit_old_formats/src/main.c b/tests/decode/test1_suit_old_formats/src/main.c index 4f0fc226..599ecf1e 100644 --- a/tests/decode/test1_suit_old_formats/src/main.c +++ b/tests/decode/test1_suit_old_formats/src/main.c @@ -7,7 +7,6 @@ #include #include "manifest-moran3_decode.h" #include "manifest-moran4_decode.h" -#include "zcbor_debug.h" // Enables use of print functions when debugging tests. uint8_t test_vector2[] = { diff --git a/tests/decode/test2_suit/src/main.c b/tests/decode/test2_suit/src/main.c index bee4b887..95557929 100644 --- a/tests/decode/test2_suit/src/main.c +++ b/tests/decode/test2_suit/src/main.c @@ -6,7 +6,7 @@ #include #include "manifest2_decode.h" -#include "zcbor_debug.h" // Enables use of print functions when debugging tests. +#include "zcbor_print.h" /* draft-ietf-suit-manifest-02 Example 0 */ diff --git a/tests/decode/test3_simple/src/main.c b/tests/decode/test3_simple/src/main.c index c8c51e9a..0dd120ac 100644 --- a/tests/decode/test3_simple/src/main.c +++ b/tests/decode/test3_simple/src/main.c @@ -7,7 +7,6 @@ #include #include "pet_decode.h" #include "serial/serial_recovery_decode.h" -#include "zcbor_debug.h" // Enables use of print functions when debugging tests. uint8_t serial_rec_input1[] = { /* "data" */ diff --git a/tests/decode/test5_corner_cases/src/main.c b/tests/decode/test5_corner_cases/src/main.c index a7090322..3f4ab57b 100644 --- a/tests/decode/test5_corner_cases/src/main.c +++ b/tests/decode/test5_corner_cases/src/main.c @@ -8,7 +8,6 @@ #include #include #include -#include // Enables use of print functions when debugging tests. #define CONCAT_BYTE(a,b) a ## b diff --git a/tests/decode/test7_suit9_simple/src/main.c b/tests/decode/test7_suit9_simple/src/main.c index 6295d0a5..134b98f7 100644 --- a/tests/decode/test7_suit9_simple/src/main.c +++ b/tests/decode/test7_suit9_simple/src/main.c @@ -6,7 +6,7 @@ #include #include "manifest9_simple_decode.h" -#include "zcbor_debug.h" // Enables use of print functions when debugging tests. +#include "zcbor_print.h" /* draft-ietf-suit-manifest-09 Example B.2 */ diff --git a/tests/decode/test8_suit12/src/main.c b/tests/decode/test8_suit12/src/main.c index 8702c66c..59df8594 100644 --- a/tests/decode/test8_suit12/src/main.c +++ b/tests/decode/test8_suit12/src/main.c @@ -7,7 +7,6 @@ #include #include "manifest12_decode.h" -#include "zcbor_debug.h" // Enables use of print functions when debugging tests. /* Test vector discovered via fuzzing with AFL. */ uint8_t crash_vector0[] = { diff --git a/tests/decode/test9_manifest14/src/main.c b/tests/decode/test9_manifest14/src/main.c index 8baef1b2..f8f9ca24 100644 --- a/tests/decode/test9_manifest14/src/main.c +++ b/tests/decode/test9_manifest14/src/main.c @@ -10,7 +10,6 @@ #else #include "manifest14_decode.h" #endif -#include "zcbor_debug.h" // Enables use of print functions when debugging tests. #include diff --git a/tests/encode/test1_suit/src/main.c b/tests/encode/test1_suit/src/main.c index 9d80191f..3763bee2 100644 --- a/tests/encode/test1_suit/src/main.c +++ b/tests/encode/test1_suit/src/main.c @@ -7,7 +7,7 @@ #include #include "manifest3_decode.h" #include "manifest3_encode.h" -#include "zcbor_debug.h" // Enables use of print functions when debugging tests. +#include "zcbor_print.h" /* draft-ietf-suit-manifest-02 Example 0 */ uint8_t test_vector0_02[] = { diff --git a/tests/encode/test2_simple/src/main.c b/tests/encode/test2_simple/src/main.c index 748c3e1b..93ee566b 100644 --- a/tests/encode/test2_simple/src/main.c +++ b/tests/encode/test2_simple/src/main.c @@ -7,7 +7,6 @@ #include #include #include -#include // Enables use of print functions when debugging tests. #define CONCAT_BYTE(a,b) a ## b diff --git a/tests/encode/test3_corner_cases/src/main.c b/tests/encode/test3_corner_cases/src/main.c index 71c33eed..63b83b8f 100644 --- a/tests/encode/test3_corner_cases/src/main.c +++ b/tests/encode/test3_corner_cases/src/main.c @@ -6,7 +6,6 @@ #include #include "corner_cases_encode.h" -#include "zcbor_debug.h" // Enables use of print functions when debugging tests. #define CONCAT_BYTE(a,b) a ## b diff --git a/tests/encode/test4_senml/src/main.c b/tests/encode/test4_senml/src/main.c index a93c49d1..8c70ef54 100644 --- a/tests/encode/test4_senml/src/main.c +++ b/tests/encode/test4_senml/src/main.c @@ -6,7 +6,6 @@ #include #include "senml_encode.h" -#include "zcbor_debug.h" // Enables use of print functions when debugging tests. diff --git a/tests/unit/test1_unit_tests/src/main.c b/tests/unit/test1_unit_tests/src/main.c index f4cd9182..15d9fcde 100644 --- a/tests/unit/test1_unit_tests/src/main.c +++ b/tests/unit/test1_unit_tests/src/main.c @@ -7,7 +7,7 @@ #include #include "zcbor_decode.h" #include "zcbor_encode.h" -#include "zcbor_debug.h" +#include "zcbor_print.h" ZTEST(zcbor_unit_tests, test_int64) { diff --git a/tests/unit/test3_float16/src/main.c b/tests/unit/test3_float16/src/main.c index 958d8612..984c4564 100644 --- a/tests/unit/test3_float16/src/main.c +++ b/tests/unit/test3_float16/src/main.c @@ -18,7 +18,6 @@ ZTEST(zcbor_unit_tests3, test_skip) #include #include "zcbor_decode.h" #include "zcbor_encode.h" -#include "zcbor_debug.h" #include diff --git a/zcbor/zcbor.py b/zcbor/zcbor.py index f227434c..01dabd7c 100755 --- a/zcbor/zcbor.py +++ b/zcbor/zcbor.py @@ -2664,6 +2664,7 @@ def render_c_file(self, header_file_name, mode): #include #include "zcbor_{mode}.h" #include "{header_file_name}" +#include "zcbor_print.h" #if DEFAULT_MAX_QTY != {self.default_max_qty} #error "The type file was generated with a different default_max_qty than this file" @@ -3101,7 +3102,7 @@ def add_mode_to_fname(filename, mode): copyfile(Path(h_code_dir, "zcbor_encode.h"), Path(new_h_code_dir, "zcbor_encode.h")) copyfile(Path(h_code_dir, "zcbor_common.h"), Path(new_h_code_dir, "zcbor_common.h")) copyfile(Path(h_code_dir, "zcbor_tags.h"), Path(new_h_code_dir, "zcbor_tags.h")) - copyfile(Path(h_code_dir, "zcbor_debug.h"), Path(new_h_code_dir, "zcbor_debug.h")) + copyfile(Path(h_code_dir, "zcbor_print.h"), Path(new_h_code_dir, "zcbor_print.h")) c_code_dir = new_c_code_dir h_code_dir = new_h_code_dir From 98ee098c6cad27a0569344222ea5bcc26e403528 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20R=C3=B8nningstad?= Date: Wed, 22 Nov 2023 14:24:37 +0100 Subject: [PATCH 2/5] Rename zcbor_print() to zcbor_log() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit and zcbor_print_assert() to zcbor_log_assert() Signed-off-by: Øyvind Rønningstad --- README.md | 2 +- include/zcbor_print.h | 12 +++++------ samples/pet/src/pet_decode.c | 2 +- samples/pet/src/pet_encode.c | 2 +- src/zcbor_common.c | 8 ++++---- src/zcbor_decode.c | 24 +++++++++++----------- src/zcbor_encode.c | 12 +++++------ tests/decode/test2_suit/src/main.c | 8 ++++---- tests/decode/test7_suit9_simple/src/main.c | 8 ++++---- tests/encode/test1_suit/src/main.c | 6 +++--- zcbor/zcbor.py | 2 +- 11 files changed, 43 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index c87ed2dc..c4f1c128 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/include/zcbor_print.h b/include/zcbor_print.h index 13a8d90b..eefbd8e3 100644 --- a/include/zcbor_print.h +++ b/include/zcbor_print.h @@ -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) diff --git a/samples/pet/src/pet_decode.c b/samples/pet/src/pet_decode.c index 47ebaf49..c9cf9965 100644 --- a/samples/pet/src/pet_decode.c +++ b/samples/pet/src/pet_decode.c @@ -26,7 +26,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))) diff --git a/samples/pet/src/pet_encode.c b/samples/pet/src/pet_encode.c index d2777bdb..42ac74ee 100644 --- a/samples/pet/src/pet_encode.c +++ b/samples/pet/src/pet_encode.c @@ -26,7 +26,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) diff --git a/src/zcbor_common.c b/src/zcbor_common.c index 634094fd..b67c68c1 100644 --- a/src/zcbor_common.c +++ b/src/zcbor_common.c @@ -50,7 +50,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); } @@ -61,7 +61,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); } } @@ -74,7 +74,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); } @@ -290,7 +290,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; } diff --git a/src/zcbor_decode.c b/src/zcbor_decode.c index 146d2fff..9f9a9ebc 100644 --- a/src/zcbor_decode.c +++ b/src/zcbor_decode.c @@ -212,7 +212,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; @@ -282,7 +282,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; @@ -329,7 +329,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; @@ -381,7 +381,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); @@ -472,7 +472,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; } @@ -488,7 +488,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; } @@ -838,7 +838,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; } @@ -979,7 +979,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); } @@ -1031,7 +1031,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); } @@ -1072,7 +1072,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; } @@ -1456,11 +1456,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; } diff --git a/src/zcbor_encode.c b/src/zcbor_encode.c index 38d3c5b4..6024afe3 100644 --- a/src/zcbor_encode.c +++ b/src/zcbor_encode.c @@ -29,7 +29,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; } @@ -141,7 +141,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; @@ -390,14 +390,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); } @@ -456,7 +456,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; @@ -597,7 +597,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; } diff --git a/tests/decode/test2_suit/src/main.c b/tests/decode/test2_suit/src/main.c index 95557929..b1075d82 100644 --- a/tests/decode/test2_suit/src/main.c +++ b/tests/decode/test2_suit/src/main.c @@ -45,8 +45,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), @@ -125,7 +125,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 @@ -166,7 +166,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( diff --git a/tests/decode/test7_suit9_simple/src/main.c b/tests/decode/test7_suit9_simple/src/main.c index 134b98f7..d28e49ba 100644 --- a/tests/decode/test7_suit9_simple/src/main.c +++ b/tests/decode/test7_suit9_simple/src/main.c @@ -96,8 +96,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, @@ -116,8 +116,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, diff --git a/tests/encode/test1_suit/src/main.c b/tests/encode/test1_suit/src/main.c index 3763bee2..b70cf803 100644 --- a/tests/encode/test1_suit/src/main.c +++ b/tests/encode/test1_suit/src/main.c @@ -244,7 +244,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, @@ -338,8 +338,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."); diff --git a/zcbor/zcbor.py b/zcbor/zcbor.py index 01dabd7c..f4e4c827 100755 --- a/zcbor/zcbor.py +++ b/zcbor/zcbor.py @@ -2624,7 +2624,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 ""} From 4abdf4b500fabde3817537cfd0c37b26cc4aaaa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20R=C3=B8nningstad?= Date: Tue, 28 Nov 2023 15:49:48 +0100 Subject: [PATCH 3/5] zcbor_print.h: Move from printk to printf MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Needs cleaning up of format strings. Signed-off-by: Øyvind Rønningstad --- README.md | 2 +- include/zcbor_print.h | 4 ++-- src/zcbor_common.c | 2 +- src/zcbor_decode.c | 13 +++++++------ src/zcbor_encode.c | 8 ++++---- tests/unit/test1_unit_tests/CMakeLists.txt | 5 +---- tests/unit/test1_unit_tests/src/main.c | 4 ++-- tests/unit/test2_cpp/src/main.cpp | 10 +++++----- tests/unit/test3_float16/src/main.c | 8 ++++---- 9 files changed, 27 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index c4f1c128..00c86ee4 100644 --- a/README.md +++ b/README.md @@ -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 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_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). `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. diff --git a/include/zcbor_print.h b/include/zcbor_print.h index eefbd8e3..bbdb8ee7 100644 --- a/include/zcbor_print.h +++ b/include/zcbor_print.h @@ -13,8 +13,8 @@ extern "C" { #endif #ifndef ZCBOR_PRINT_FUNC -#include -#define zcbor_do_print(...) printk(__VA_ARGS__) +#include +#define zcbor_do_print(...) printf(__VA_ARGS__) #else #define zcbor_do_print(...) ZCBOR_PRINT_FUNC(__VA_ARGS__) #endif diff --git a/src/zcbor_common.c b/src/zcbor_common.c index b67c68c1..5fdce6cd 100644 --- a/src/zcbor_common.c +++ b/src/zcbor_common.c @@ -74,7 +74,7 @@ bool zcbor_process_backup(zcbor_state_t *state, uint32_t flags, } if (local_copy.elem_count > max_elem_count) { - zcbor_log("elem_count: %" PRIuFAST32 " (expected max %" PRIuFAST32 ")\r\n", + zcbor_log("elem_count: %zu (expected max %zu)\r\n", local_copy.elem_count, max_elem_count); ZCBOR_ERR(ZCBOR_ERR_HIGH_ELEM_COUNT); } diff --git a/src/zcbor_decode.c b/src/zcbor_decode.c index 9f9a9ebc..ff19cb91 100644 --- a/src/zcbor_decode.c +++ b/src/zcbor_decode.c @@ -8,6 +8,7 @@ #include #include #include +#include #include "zcbor_decode.h" #include "zcbor_common.h" #include "zcbor_print.h" @@ -124,7 +125,7 @@ static bool value_extract(zcbor_state_t *state, zcbor_trace(); zcbor_assert_state(result_len != 0, "0-length result not supported.\r\n"); zcbor_assert_state(result_len <= 8, "result sizes above 8 bytes not supported.\r\n"); - zcbor_assert_state(result != NULL, NULL); + zcbor_assert_state(result != NULL, "result cannot be NULL.\r\n"); INITIAL_CHECKS(); ZCBOR_ERR_IF((state->elem_count == 0), ZCBOR_ERR_LOW_ELEM_COUNT); @@ -212,7 +213,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_log("uint with size %d failed.\r\n", result_size); + zcbor_log("uint with size %zu failed.\r\n", result_size); ZCBOR_FAIL(); } return true; @@ -838,7 +839,7 @@ static bool try_key(zcbor_state_t *state, void *key_result, zcbor_decoder_t key_ return false; } - zcbor_log("Found element at index %d.\n", get_current_index(state, 1)); + zcbor_log("Found element at index %zu.\n", get_current_index(state, 1)); return true; } @@ -979,7 +980,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_log("unprocessed element(s) in map: [%d] = 0x%02x\n", + zcbor_log("unprocessed element(s) in map: [%zu] = 0x%02x\n", i, state->decode_state.map_search_elem_state[i]); ZCBOR_ERR(ZCBOR_ERR_ELEMS_NOT_PROCESSED); } @@ -1456,11 +1457,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_log("Found %" PRIuFAST32 " elements.\r\n", i); + zcbor_log("Found %zu elements.\r\n", i); return true; } } - zcbor_log("Found %" PRIuFAST32 " elements.\r\n", max_decode); + zcbor_log("Found %zu elements.\r\n", max_decode); *num_decode = max_decode; return true; } diff --git a/src/zcbor_encode.c b/src/zcbor_encode.c index 6024afe3..5430e74f 100644 --- a/src/zcbor_encode.c +++ b/src/zcbor_encode.c @@ -46,7 +46,7 @@ static bool encode_header_byte(zcbor_state_t *state, ZCBOR_CHECK_ERROR(); ZCBOR_CHECK_PAYLOAD(); - zcbor_assert_state(additional < 32, NULL); + zcbor_assert_state(additional < 32, "Unsupported additional value: %d\r\n", additional); *(state->payload_mut++) = (uint8_t)((major_type << 5) | (additional & 0x1F)); return true; @@ -141,7 +141,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_log("uint with size %d failed.\r\n", uint_size); + zcbor_log("uint with size %zu failed.\r\n", uint_size); ZCBOR_FAIL(); } return true; @@ -390,7 +390,7 @@ static bool list_map_end_encode(zcbor_state_t *state, size_t max_num, ZCBOR_FAIL(); } - zcbor_log("list_count: %" PRIuFAST32 "\r\n", list_count); + zcbor_log("list_count: %zu\r\n", list_count); /** If max_num is smaller than the actual number of encoded elements, @@ -597,7 +597,7 @@ bool zcbor_multi_encode(const size_t num_encode, zcbor_encoder_t encoder, ZCBOR_FAIL(); } } - zcbor_log("Encoded %" PRIuFAST32 " elements.\n", num_encode); + zcbor_log("Encoded %zu elements.\n", num_encode); return true; } diff --git a/tests/unit/test1_unit_tests/CMakeLists.txt b/tests/unit/test1_unit_tests/CMakeLists.txt index 57035a6b..36b6eab2 100644 --- a/tests/unit/test1_unit_tests/CMakeLists.txt +++ b/tests/unit/test1_unit_tests/CMakeLists.txt @@ -23,7 +23,4 @@ target_link_libraries(app PRIVATE zcbor) zephyr_compile_definitions(ZCBOR_STOP_ON_ERROR) -if (NOT VERBOSE AND CONFIG_MINIMAL_LIBC) - # VERBOSE means including printk which doesn't build with these options. - target_compile_options(zcbor PRIVATE -Wpedantic -Wconversion -Wall -Wextra -Wdouble-promotion) -endif() +target_compile_options(zcbor PRIVATE -Wpedantic -Wconversion -Wall -Wextra -Wdouble-promotion) diff --git a/tests/unit/test1_unit_tests/src/main.c b/tests/unit/test1_unit_tests/src/main.c index 15d9fcde..2e44dd95 100644 --- a/tests/unit/test1_unit_tests/src/main.c +++ b/tests/unit/test1_unit_tests/src/main.c @@ -177,7 +177,7 @@ ZTEST(zcbor_unit_tests, test_size64) #else ZTEST(zcbor_unit_tests, test_size64) { - printk("Skip on non-64-bit builds.\n"); + printf("Skip on non-64-bit builds.\n"); } #endif @@ -662,7 +662,7 @@ ZTEST(zcbor_unit_tests, test_bstr_cbor_fragments) ZTEST(zcbor_unit_tests, test_canonical_list) { #ifndef ZCBOR_CANONICAL - printk("Skip on non-canonical builds.\n"); + printf("Skip on non-canonical builds.\n"); #else uint8_t payload1[100]; uint8_t payload2[100]; diff --git a/tests/unit/test2_cpp/src/main.cpp b/tests/unit/test2_cpp/src/main.cpp index 3e94fb4d..c2ce9898 100644 --- a/tests/unit/test2_cpp/src/main.cpp +++ b/tests/unit/test2_cpp/src/main.cpp @@ -6,7 +6,7 @@ #include #include -#include +#include #include int main(void) @@ -64,7 +64,7 @@ int main(void) bool ret = zcbor_multi_encode_minmax(1, 1, &one, (zcbor_encoder_t *)zcbor_int32_put, state_e, (void*)15, 0); if (!ret) { - printk("Encode error: %d\r\n", zcbor_peek_error(state_e)); + printf("Encode error: %d\r\n", zcbor_peek_error(state_e)); return 1; } @@ -101,7 +101,7 @@ int main(void) ret = zcbor_present_decode(&one_b, (zcbor_decoder_t *)zcbor_int32_expect, state_d, (void*)15); if (!ret) { - printk("Decode error: %d\r\n", zcbor_peek_error(state_d)); + printf("Decode error: %d\r\n", zcbor_peek_error(state_d)); return 1; } @@ -114,11 +114,11 @@ int main(void) int int_ret = cbor_decode_Pet(input, sizeof(input), &pet, NULL); if (int_ret != ZCBOR_SUCCESS) { - printk("Decode error: %d\r\n", int_ret); + printf("Decode error: %d\r\n", int_ret); return 1; } - printk("Success!\r\n"); + printf("Success!\r\n"); return 0; } diff --git a/tests/unit/test3_float16/src/main.c b/tests/unit/test3_float16/src/main.c index 984c4564..37926532 100644 --- a/tests/unit/test3_float16/src/main.c +++ b/tests/unit/test3_float16/src/main.c @@ -10,7 +10,7 @@ ZTEST(zcbor_unit_tests3, test_skip) { - printk("Skip on VERBOSE builds because of print output volume.\n"); + printf("Skip on VERBOSE builds because of print output volume.\n"); } #else /* ZCBOR_VERBOSE */ @@ -134,8 +134,8 @@ void print_percent(uint32_t i) { if ((i % 0x800000) == 0) { static const uint8_t move_cursor_up[] = {0x1b, 0x5b, 0x41, 0}; - printk("%s", move_cursor_up); - printk("\r%d%%\n", (int)((double)(i) / 0x80000000 * 100)); + printf("%s", move_cursor_up); + printf("\r%d%%\n", (int)((double)(i) / 0x80000000 * 100)); } } @@ -182,7 +182,7 @@ static void do_test_nan(uint32_t i) ZTEST(zcbor_unit_tests3, test_float16_encode) { zassert_equal((size_t)&_binary_fp_bytes_encode_bin_size, 31742 * 4 * 2, NULL); - printk("\n"); + printf("\n"); for (uint32_t i = START_ZERO; i < START_NONZERO_FINITE; i++) { do_test(i, 0); From 54c2b7b98b07a854c07a1ed9118373138700548f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20R=C3=B8nningstad?= Date: Tue, 19 Dec 2023 14:16:22 +0100 Subject: [PATCH 4/5] zcbor_print.h: Improve zcbor_trace() and other tracing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Make printout more terse, and make the most printed instance (value_extract) not print the whole path. Pass state parameter to macro. Add more trace printing to zcbor_decode.c, and to errors. Signed-off-by: Øyvind Rønningstad --- include/zcbor_common.h | 6 ++- include/zcbor_print.h | 15 +++++-- samples/pet/src/pet_decode.c | 2 +- samples/pet/src/pet_encode.c | 2 +- src/zcbor_common.c | 12 ++++-- src/zcbor_decode.c | 81 +++++++++++++++++++++++++++++++++++- src/zcbor_encode.c | 2 +- zcbor/zcbor.py | 2 +- 8 files changed, 108 insertions(+), 14 deletions(-) diff --git a/include/zcbor_common.h b/include/zcbor_common.h index b77b199b..87988971 100644 --- a/include/zcbor_common.h +++ b/include/zcbor_common.h @@ -169,19 +169,22 @@ typedef enum */ #define ZCBOR_FAIL() \ do {\ - zcbor_trace(); \ + zcbor_log("ZCBOR_FAIL "); \ + zcbor_trace_file(state); \ return false; \ } while(0) #define ZCBOR_FAIL_IF(expr) \ do {\ if (expr) { \ + zcbor_log("ZCBOR_FAIL_IF(" #expr ") "); \ ZCBOR_FAIL(); \ } \ } while(0) #define ZCBOR_ERR(err) \ do { \ + zcbor_log("ZCBOR_ERR(%d) ", err); \ zcbor_error(state, err); \ ZCBOR_FAIL(); \ } while(0) @@ -189,6 +192,7 @@ do { \ #define ZCBOR_ERR_IF(expr, err) \ do {\ if (expr) { \ + zcbor_log("ZCBOR_ERR_IF(" #expr ", %d) ", err); \ ZCBOR_ERR(err); \ } \ } while(0) diff --git a/include/zcbor_print.h b/include/zcbor_print.h index bbdb8ee7..ee7db693 100644 --- a/include/zcbor_print.h +++ b/include/zcbor_print.h @@ -20,9 +20,17 @@ extern "C" { #endif #ifdef ZCBOR_VERBOSE -#define zcbor_trace() (zcbor_do_print("bytes left: %zu, byte: 0x%x, elem_count: 0x%zx, err: %d, %s:%d\n",\ +#define zcbor_trace_raw(state) (zcbor_do_print("rem: %zu, cur: 0x%x, ec: 0x%zx, err: %d",\ (size_t)state->payload_end - (size_t)state->payload, *state->payload, state->elem_count, \ - state->constant_state ? state->constant_state->error : 0, __FILE__, __LINE__)) + state->constant_state ? state->constant_state->error : 0)) +#define zcbor_trace(state, appendix) do { \ + zcbor_trace_raw(state); \ + zcbor_do_print(", %s\n", appendix); \ +} while(0) +#define zcbor_trace_file(state) do { \ + zcbor_trace_raw(state); \ + zcbor_do_print(", %s:%d\n", __FILE__, __LINE__); \ +} while(0) #define zcbor_log_assert(expr, ...) \ do { \ @@ -33,7 +41,8 @@ do { \ } while(0) #define zcbor_log(...) zcbor_do_print(__VA_ARGS__) #else -#define zcbor_trace() ((void)state) +#define zcbor_trace(state, appendix) +#define zcbor_trace_file(state) ((void)state) #define zcbor_log_assert(...) #define zcbor_log(...) #endif diff --git a/samples/pet/src/pet_decode.c b/samples/pet/src/pet_decode.c index c9cf9965..6e885236 100644 --- a/samples/pet/src/pet_decode.c +++ b/samples/pet/src/pet_decode.c @@ -37,7 +37,7 @@ static bool decode_Pet( || (((*result).species_choice == Pet_species_other_c) && ((1)))) || (zcbor_error(state, ZCBOR_ERR_WRONG_VALUE), false)))))) || (zcbor_list_map_end_force_decode(state), false)) && zcbor_list_end_decode(state)))); if (!tmp_result) - zcbor_trace(); + zcbor_trace_file(state); return tmp_result; } diff --git a/samples/pet/src/pet_encode.c b/samples/pet/src/pet_encode.c index 42ac74ee..42485c40 100644 --- a/samples/pet/src/pet_encode.c +++ b/samples/pet/src/pet_encode.c @@ -38,7 +38,7 @@ static bool encode_Pet( : false))))) || (zcbor_list_map_end_force_encode(state), false)) && zcbor_list_end_encode(state, 3)))); if (!tmp_result) - zcbor_trace(); + zcbor_trace_file(state); return tmp_result; } diff --git a/src/zcbor_common.c b/src/zcbor_common.c index 5fdce6cd..9784d490 100644 --- a/src/zcbor_common.c +++ b/src/zcbor_common.c @@ -39,6 +39,8 @@ bool zcbor_new_backup(zcbor_state_t *state, size_t new_elem_count) state->elem_count = new_elem_count; + zcbor_log("New backup (level %zu)\n", i); + return true; } @@ -54,11 +56,13 @@ bool zcbor_process_backup(zcbor_state_t *state, uint32_t flags, ZCBOR_ERR(ZCBOR_ERR_NO_BACKUP_ACTIVE); } - if (flags & ZCBOR_FLAG_RESTORE) { - /* use the backup at current_backup - 1, since otherwise, the - * 0th backup would be unused. */ - size_t i = state->constant_state->current_backup - 1; + /* use the backup at current_backup - 1, since otherwise, the + * 0th backup would be unused. */ + size_t i = state->constant_state->current_backup - 1; + zcbor_log("Process backup (level %zu, flags 0x%x)\n", i, flags); + + if (flags & ZCBOR_FLAG_RESTORE) { if (!(flags & ZCBOR_FLAG_KEEP_PAYLOAD)) { if (state->constant_state->backup_list[i].payload_moved) { zcbor_log("Payload pointer out of date.\r\n"); diff --git a/src/zcbor_decode.c b/src/zcbor_decode.c index ff19cb91..92f9fe51 100644 --- a/src/zcbor_decode.c +++ b/src/zcbor_decode.c @@ -88,6 +88,8 @@ do { \ ZCBOR_FAIL(); \ } while(0) +#define PRINT_FUNC() zcbor_log("%s ", __func__); + static void endian_copy(uint8_t *dst, const uint8_t *src, size_t src_len) { @@ -122,7 +124,7 @@ static void endian_copy(uint8_t *dst, const uint8_t *src, size_t src_len) static bool value_extract(zcbor_state_t *state, void *const result, size_t result_len) { - zcbor_trace(); + zcbor_trace(state, "value_extract"); zcbor_assert_state(result_len != 0, "0-length result not supported.\r\n"); zcbor_assert_state(result_len <= 8, "result sizes above 8 bytes not supported.\r\n"); zcbor_assert_state(result != NULL, "result cannot be NULL.\r\n"); @@ -161,6 +163,7 @@ static bool value_extract(zcbor_state_t *state, bool zcbor_int_decode(zcbor_state_t *state, void *result, size_t result_size) { + PRINT_FUNC(); INITIAL_CHECKS(); zcbor_major_type_t major_type = ZCBOR_MAJOR_TYPE(*state->payload); uint8_t *result_uint8 = (uint8_t *)result; @@ -198,18 +201,21 @@ bool zcbor_int_decode(zcbor_state_t *state, void *result, size_t result_size) bool zcbor_int32_decode(zcbor_state_t *state, int32_t *result) { + PRINT_FUNC(); return zcbor_int_decode(state, result, sizeof(*result)); } bool zcbor_int64_decode(zcbor_state_t *state, int64_t *result) { + PRINT_FUNC(); return zcbor_int_decode(state, result, sizeof(*result)); } bool zcbor_uint_decode(zcbor_state_t *state, void *result, size_t result_size) { + PRINT_FUNC(); INITIAL_CHECKS_WITH_TYPE(ZCBOR_MAJOR_TYPE_PINT); if (!value_extract(state, result, result_size)) { @@ -222,12 +228,14 @@ bool zcbor_uint_decode(zcbor_state_t *state, void *result, size_t result_size) bool zcbor_uint32_decode(zcbor_state_t *state, uint32_t *result) { + PRINT_FUNC(); return zcbor_uint_decode(state, result, sizeof(*result)); } bool zcbor_int32_expect_union(zcbor_state_t *state, int32_t result) { + PRINT_FUNC(); if (!zcbor_union_elem_code(state)) { ZCBOR_FAIL(); } @@ -237,6 +245,7 @@ bool zcbor_int32_expect_union(zcbor_state_t *state, int32_t result) bool zcbor_int64_expect_union(zcbor_state_t *state, int64_t result) { + PRINT_FUNC(); if (!zcbor_union_elem_code(state)) { ZCBOR_FAIL(); } @@ -246,6 +255,7 @@ bool zcbor_int64_expect_union(zcbor_state_t *state, int64_t result) bool zcbor_uint32_expect_union(zcbor_state_t *state, uint32_t result) { + PRINT_FUNC(); if (!zcbor_union_elem_code(state)) { ZCBOR_FAIL(); } @@ -255,6 +265,7 @@ bool zcbor_uint32_expect_union(zcbor_state_t *state, uint32_t result) bool zcbor_uint64_expect_union(zcbor_state_t *state, uint64_t result) { + PRINT_FUNC(); if (!zcbor_union_elem_code(state)) { ZCBOR_FAIL(); } @@ -264,18 +275,21 @@ bool zcbor_uint64_expect_union(zcbor_state_t *state, uint64_t result) bool zcbor_int32_expect(zcbor_state_t *state, int32_t expected) { + PRINT_FUNC(); return zcbor_int64_expect(state, expected); } bool zcbor_int32_pexpect(zcbor_state_t *state, int32_t *expected) { + PRINT_FUNC(); return zcbor_int32_expect(state, *expected); } bool zcbor_int64_expect(zcbor_state_t *state, int64_t expected) { + PRINT_FUNC(); int64_t actual; if (!zcbor_int64_decode(state, &actual)) { @@ -292,12 +306,14 @@ bool zcbor_int64_expect(zcbor_state_t *state, int64_t expected) bool zcbor_int64_pexpect(zcbor_state_t *state, int64_t *expected) { + PRINT_FUNC(); return zcbor_int64_expect(state, *expected); } bool zcbor_uint64_decode(zcbor_state_t *state, uint64_t *result) { + PRINT_FUNC(); return zcbor_uint_decode(state, result, sizeof(*result)); } @@ -305,6 +321,7 @@ bool zcbor_uint64_decode(zcbor_state_t *state, uint64_t *result) #ifdef ZCBOR_SUPPORTS_SIZE_T bool zcbor_size_decode(zcbor_state_t *state, size_t *result) { + PRINT_FUNC(); return zcbor_uint_decode(state, result, sizeof(*result)); } #endif @@ -312,18 +329,21 @@ bool zcbor_size_decode(zcbor_state_t *state, size_t *result) bool zcbor_uint32_expect(zcbor_state_t *state, uint32_t expected) { + PRINT_FUNC(); return zcbor_uint64_expect(state, expected); } bool zcbor_uint32_pexpect(zcbor_state_t *state, uint32_t *expected) { + PRINT_FUNC(); return zcbor_uint32_expect(state, *expected); } bool zcbor_uint64_expect(zcbor_state_t *state, uint64_t expected) { + PRINT_FUNC(); uint64_t actual; if (!zcbor_uint64_decode(state, &actual)) { @@ -339,6 +359,7 @@ bool zcbor_uint64_expect(zcbor_state_t *state, uint64_t expected) bool zcbor_uint64_pexpect(zcbor_state_t *state, uint64_t *expected) { + PRINT_FUNC(); return zcbor_uint64_expect(state, *expected); } @@ -346,12 +367,14 @@ bool zcbor_uint64_pexpect(zcbor_state_t *state, uint64_t *expected) #ifdef ZCBOR_SUPPORTS_SIZE_T bool zcbor_size_expect(zcbor_state_t *state, size_t expected) { + PRINT_FUNC(); return zcbor_uint64_expect(state, expected); } bool zcbor_size_pexpect(zcbor_state_t *state, size_t *expected) { + PRINT_FUNC(); return zcbor_size_expect(state, *expected); } #endif @@ -394,6 +417,7 @@ static bool str_start_decode_with_overflow_check(zcbor_state_t *state, bool zcbor_bstr_start_decode(zcbor_state_t *state, struct zcbor_string *result) { + PRINT_FUNC(); struct zcbor_string dummy; if (result == NULL) { result = &dummy; @@ -438,6 +462,7 @@ static bool start_decode_fragment(zcbor_state_t *state, struct zcbor_string_fragment *result, zcbor_major_type_t exp_major_type) { + PRINT_FUNC(); if(!str_start_decode(state, &result->fragment, exp_major_type)) { ZCBOR_FAIL(); } @@ -453,6 +478,7 @@ static bool start_decode_fragment(zcbor_state_t *state, bool zcbor_bstr_start_decode_fragment(zcbor_state_t *state, struct zcbor_string_fragment *result) { + PRINT_FUNC(); if (!start_decode_fragment(state, result, ZCBOR_MAJOR_TYPE_BSTR)) { ZCBOR_FAIL(); } @@ -541,42 +567,49 @@ static bool str_expect(zcbor_state_t *state, struct zcbor_string *result, bool zcbor_bstr_decode(zcbor_state_t *state, struct zcbor_string *result) { + PRINT_FUNC(); return str_decode(state, result, ZCBOR_MAJOR_TYPE_BSTR); } bool zcbor_bstr_decode_fragment(zcbor_state_t *state, struct zcbor_string_fragment *result) { + PRINT_FUNC(); return str_decode_fragment(state, result, ZCBOR_MAJOR_TYPE_BSTR); } bool zcbor_bstr_expect(zcbor_state_t *state, struct zcbor_string *expected) { + PRINT_FUNC(); return str_expect(state, expected, ZCBOR_MAJOR_TYPE_BSTR); } bool zcbor_tstr_decode(zcbor_state_t *state, struct zcbor_string *result) { + PRINT_FUNC(); return str_decode(state, result, ZCBOR_MAJOR_TYPE_TSTR); } bool zcbor_tstr_decode_fragment(zcbor_state_t *state, struct zcbor_string_fragment *result) { + PRINT_FUNC(); return str_decode_fragment(state, result, ZCBOR_MAJOR_TYPE_TSTR); } bool zcbor_tstr_expect(zcbor_state_t *state, struct zcbor_string *expected) { + PRINT_FUNC(); return str_expect(state, expected, ZCBOR_MAJOR_TYPE_TSTR); } bool zcbor_bstr_expect_ptr(zcbor_state_t *state, char const *ptr, size_t len) { + PRINT_FUNC(); struct zcbor_string zs = { .value = (const uint8_t *)ptr, .len = len }; return zcbor_bstr_expect(state, &zs); @@ -585,6 +618,7 @@ bool zcbor_bstr_expect_ptr(zcbor_state_t *state, char const *ptr, size_t len) bool zcbor_tstr_expect_ptr(zcbor_state_t *state, char const *ptr, size_t len) { + PRINT_FUNC(); struct zcbor_string zs = { .value = (const uint8_t *)ptr, .len = len }; return zcbor_tstr_expect(state, &zs); @@ -593,12 +627,14 @@ bool zcbor_tstr_expect_ptr(zcbor_state_t *state, char const *ptr, size_t len) bool zcbor_bstr_expect_term(zcbor_state_t *state, char const *string, size_t maxlen) { + PRINT_FUNC(); return zcbor_bstr_expect_ptr(state, string, strnlen(string, maxlen)); } bool zcbor_tstr_expect_term(zcbor_state_t *state, char const *string, size_t maxlen) { + PRINT_FUNC(); return zcbor_tstr_expect_ptr(state, string, strnlen(string, maxlen)); } @@ -639,12 +675,14 @@ static bool list_map_start_decode(zcbor_state_t *state, bool zcbor_list_start_decode(zcbor_state_t *state) { + PRINT_FUNC(); return list_map_start_decode(state, ZCBOR_MAJOR_TYPE_LIST); } bool zcbor_map_start_decode(zcbor_state_t *state) { + PRINT_FUNC(); bool ret = list_map_start_decode(state, ZCBOR_MAJOR_TYPE_MAP); if (ret && !state->decode_state.indefinite_length_array) { @@ -680,6 +718,7 @@ static bool allocate_map_flags(zcbor_state_t *state, size_t elem_count); bool zcbor_unordered_map_start_decode(zcbor_state_t *state) { + PRINT_FUNC(); ZCBOR_FAIL_IF(!zcbor_map_start_decode(state)); #ifdef ZCBOR_MAP_SMART_SEARCH @@ -846,6 +885,7 @@ static bool try_key(zcbor_state_t *state, void *key_result, zcbor_decoder_t key_ bool zcbor_unordered_map_search(zcbor_decoder_t key_decoder, zcbor_state_t *state, void *key_result) { + PRINT_FUNC(); /* elem_count cannot be odd since the map consists of key-value-pairs. * This might mean that this function was called while pointing at a value (instead * of a key). */ @@ -957,12 +997,14 @@ static bool list_map_end_decode(zcbor_state_t *state) bool zcbor_list_end_decode(zcbor_state_t *state) { + PRINT_FUNC(); return list_map_end_decode(state); } bool zcbor_map_end_decode(zcbor_state_t *state) { + PRINT_FUNC(); return list_map_end_decode(state); } @@ -1010,6 +1052,8 @@ bool zcbor_list_map_end_force_decode(zcbor_state_t *state) bool zcbor_simple_decode(zcbor_state_t *state, uint8_t *result) { + PRINT_FUNC(); + PRINT_FUNC(); INITIAL_CHECKS_WITH_TYPE(ZCBOR_MAJOR_TYPE_SIMPLE); /* Simple values must be 0-23 (additional is 0-23) or 24-255 (additional is 24). @@ -1025,6 +1069,7 @@ bool zcbor_simple_decode(zcbor_state_t *state, uint8_t *result) bool zcbor_simple_expect(zcbor_state_t *state, uint8_t expected) { + PRINT_FUNC(); uint8_t actual; if (!zcbor_simple_decode(state, &actual)) { @@ -1042,12 +1087,14 @@ bool zcbor_simple_expect(zcbor_state_t *state, uint8_t expected) bool zcbor_simple_pexpect(zcbor_state_t *state, uint8_t *expected) { + PRINT_FUNC(); return zcbor_simple_expect(state, *expected); } bool zcbor_nil_expect(zcbor_state_t *state, void *unused) { + PRINT_FUNC(); (void)unused; return zcbor_simple_expect(state, 22); } @@ -1055,6 +1102,7 @@ bool zcbor_nil_expect(zcbor_state_t *state, void *unused) bool zcbor_undefined_expect(zcbor_state_t *state, void *unused) { + PRINT_FUNC(); (void)unused; return zcbor_simple_expect(state, 23); } @@ -1062,6 +1110,7 @@ bool zcbor_undefined_expect(zcbor_state_t *state, void *unused) bool zcbor_bool_decode(zcbor_state_t *state, bool *result) { + PRINT_FUNC(); uint8_t value; if (!zcbor_simple_decode(state, &value)) { @@ -1080,12 +1129,14 @@ bool zcbor_bool_decode(zcbor_state_t *state, bool *result) bool zcbor_bool_expect(zcbor_state_t *state, bool expected) { + PRINT_FUNC(); return zcbor_simple_expect(state, (uint8_t)(!!expected) + ZCBOR_BOOL_TO_SIMPLE); } bool zcbor_bool_pexpect(zcbor_state_t *state, bool *expected) { + PRINT_FUNC(); return zcbor_bool_expect(state, *expected); } @@ -1100,7 +1151,7 @@ static bool float_check(zcbor_state_t *state, uint8_t additional_val) bool zcbor_float16_bytes_decode(zcbor_state_t *state, uint16_t *result) { - + PRINT_FUNC(); ZCBOR_FAIL_IF(!float_check(state, ZCBOR_VALUE_IS_2_BYTES)); if (!value_extract(state, result, sizeof(*result))) { @@ -1113,6 +1164,7 @@ bool zcbor_float16_bytes_decode(zcbor_state_t *state, uint16_t *result) bool zcbor_float16_bytes_expect(zcbor_state_t *state, uint16_t expected) { + PRINT_FUNC(); uint16_t actual; if (!zcbor_float16_bytes_decode(state, &actual)) { @@ -1127,12 +1179,14 @@ bool zcbor_float16_bytes_expect(zcbor_state_t *state, uint16_t expected) bool zcbor_float16_bytes_pexpect(zcbor_state_t *state, uint16_t *expected) { + PRINT_FUNC(); return zcbor_float16_bytes_expect(state, *expected); } bool zcbor_float16_decode(zcbor_state_t *state, float *result) { + PRINT_FUNC(); uint16_t value16; if (!zcbor_float16_bytes_decode(state, &value16)) { @@ -1146,6 +1200,7 @@ bool zcbor_float16_decode(zcbor_state_t *state, float *result) bool zcbor_float16_expect(zcbor_state_t *state, float expected) { + PRINT_FUNC(); float actual; if (!zcbor_float16_decode(state, &actual)) { @@ -1160,12 +1215,14 @@ bool zcbor_float16_expect(zcbor_state_t *state, float expected) bool zcbor_float16_pexpect(zcbor_state_t *state, float *expected) { + PRINT_FUNC(); return zcbor_float16_expect(state, *expected); } bool zcbor_float32_decode(zcbor_state_t *state, float *result) { + PRINT_FUNC(); ZCBOR_FAIL_IF(!float_check(state, ZCBOR_VALUE_IS_4_BYTES)); if (!value_extract(state, result, sizeof(*result))) { @@ -1178,6 +1235,7 @@ bool zcbor_float32_decode(zcbor_state_t *state, float *result) bool zcbor_float32_expect(zcbor_state_t *state, float expected) { + PRINT_FUNC(); float actual; if (!zcbor_float32_decode(state, &actual)) { @@ -1192,12 +1250,14 @@ bool zcbor_float32_expect(zcbor_state_t *state, float expected) bool zcbor_float32_pexpect(zcbor_state_t *state, float *expected) { + PRINT_FUNC(); return zcbor_float32_expect(state, *expected); } bool zcbor_float16_32_decode(zcbor_state_t *state, float *result) { + PRINT_FUNC(); if (zcbor_float16_decode(state, result)) { /* Do nothing */ } else if (!zcbor_float32_decode(state, result)) { @@ -1210,6 +1270,7 @@ bool zcbor_float16_32_decode(zcbor_state_t *state, float *result) bool zcbor_float16_32_expect(zcbor_state_t *state, float expected) { + PRINT_FUNC(); if (zcbor_float16_expect(state, expected)) { /* Do nothing */ } else if (!zcbor_float32_expect(state, expected)) { @@ -1222,12 +1283,14 @@ bool zcbor_float16_32_expect(zcbor_state_t *state, float expected) bool zcbor_float16_32_pexpect(zcbor_state_t *state, float *expected) { + PRINT_FUNC(); return zcbor_float16_32_expect(state, *expected); } bool zcbor_float64_decode(zcbor_state_t *state, double *result) { + PRINT_FUNC(); ZCBOR_FAIL_IF(!float_check(state, ZCBOR_VALUE_IS_8_BYTES)); if (!value_extract(state, result, sizeof(*result))) { @@ -1240,6 +1303,7 @@ bool zcbor_float64_decode(zcbor_state_t *state, double *result) bool zcbor_float64_expect(zcbor_state_t *state, double expected) { + PRINT_FUNC(); double actual; if (!zcbor_float64_decode(state, &actual)) { @@ -1254,12 +1318,14 @@ bool zcbor_float64_expect(zcbor_state_t *state, double expected) bool zcbor_float64_pexpect(zcbor_state_t *state, double *expected) { + PRINT_FUNC(); return zcbor_float64_expect(state, *expected); } bool zcbor_float32_64_decode(zcbor_state_t *state, double *result) { + PRINT_FUNC(); float float_result; if (zcbor_float32_decode(state, &float_result)) { @@ -1274,6 +1340,7 @@ bool zcbor_float32_64_decode(zcbor_state_t *state, double *result) bool zcbor_float32_64_expect(zcbor_state_t *state, double expected) { + PRINT_FUNC(); if (zcbor_float64_expect(state, expected)) { /* Do nothing */ } else if (!zcbor_float32_expect(state, (float)expected)) { @@ -1286,12 +1353,14 @@ bool zcbor_float32_64_expect(zcbor_state_t *state, double expected) bool zcbor_float32_64_pexpect(zcbor_state_t *state, double *expected) { + PRINT_FUNC(); return zcbor_float32_64_expect(state, *expected); } bool zcbor_float_decode(zcbor_state_t *state, double *result) { + PRINT_FUNC(); float float_result; if (zcbor_float16_decode(state, &float_result)) { @@ -1308,6 +1377,7 @@ bool zcbor_float_decode(zcbor_state_t *state, double *result) bool zcbor_float_expect(zcbor_state_t *state, double expected) { + PRINT_FUNC(); if (zcbor_float16_expect(state, (float)expected)) { /* Do nothing */ } else if (zcbor_float32_expect(state, (float)expected)) { @@ -1322,12 +1392,14 @@ bool zcbor_float_expect(zcbor_state_t *state, double expected) bool zcbor_float_pexpect(zcbor_state_t *state, double *expected) { + PRINT_FUNC(); return zcbor_float_expect(state, *expected); } bool zcbor_any_skip(zcbor_state_t *state, void *result) { + PRINT_FUNC(); zcbor_assert_state(result == NULL, "'any' type cannot be returned, only skipped.\r\n"); (void)result; @@ -1408,6 +1480,7 @@ bool zcbor_any_skip(zcbor_state_t *state, void *result) bool zcbor_tag_decode(zcbor_state_t *state, uint32_t *result) { + PRINT_FUNC(); INITIAL_CHECKS_WITH_TYPE(ZCBOR_MAJOR_TYPE_TAG); if (!value_extract(state, result, sizeof(*result))) { @@ -1420,6 +1493,7 @@ bool zcbor_tag_decode(zcbor_state_t *state, uint32_t *result) bool zcbor_tag_expect(zcbor_state_t *state, uint32_t expected) { + PRINT_FUNC(); uint32_t actual; if (!zcbor_tag_decode(state, &actual)) { @@ -1434,6 +1508,7 @@ bool zcbor_tag_expect(zcbor_state_t *state, uint32_t expected) bool zcbor_tag_pexpect(zcbor_state_t *state, uint32_t *expected) { + PRINT_FUNC(); return zcbor_tag_expect(state, *expected); } @@ -1446,6 +1521,7 @@ bool zcbor_multi_decode(size_t min_decode, void *result, size_t result_len) { + PRINT_FUNC(); ZCBOR_CHECK_ERROR(); for (size_t i = 0; i < max_decode; i++) { uint8_t const *payload_bak = state->payload; @@ -1472,6 +1548,7 @@ bool zcbor_present_decode(bool *present, zcbor_state_t *state, void *result) { + PRINT_FUNC(); size_t num_decode = 0; bool retval = zcbor_multi_decode(0, 1, &num_decode, decoder, state, result, 0); diff --git a/src/zcbor_encode.c b/src/zcbor_encode.c index 5430e74f..44411ea3 100644 --- a/src/zcbor_encode.c +++ b/src/zcbor_encode.c @@ -69,7 +69,7 @@ static bool value_encode_len(zcbor_state_t *state, zcbor_major_type_t major_type ZCBOR_FAIL(); } state->payload_mut--; - zcbor_trace(); + zcbor_trace(state, "value_encode_len"); state->payload_mut++; #ifdef ZCBOR_BIG_ENDIAN diff --git a/zcbor/zcbor.py b/zcbor/zcbor.py index f4e4c827..191d0460 100755 --- a/zcbor/zcbor.py +++ b/zcbor/zcbor.py @@ -2631,7 +2631,7 @@ def render_function(self, xcoder, mode): bool tmp_result = ({ body }); if (!tmp_result) - zcbor_trace(); + zcbor_trace_file(state); return tmp_result; }}""".replace(" \n", "") # call replace() to remove empty lines. From 6eb5d5bcc6f98e055016a7f11226f184e982145f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20R=C3=B8nningstad?= Date: Tue, 19 Dec 2023 15:18:19 +0100 Subject: [PATCH 5/5] Refactor printing of errors from generated functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Print from generated code, and include function name Signed-off-by: Øyvind Rønningstad --- samples/pet/src/pet_decode.c | 6 +++++- samples/pet/src/pet_encode.c | 6 +++++- src/zcbor_common.c | 1 - zcbor/zcbor.py | 6 +++++- 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/samples/pet/src/pet_decode.c b/samples/pet/src/pet_decode.c index 6e885236..8f6e50dd 100644 --- a/samples/pet/src/pet_decode.c +++ b/samples/pet/src/pet_decode.c @@ -36,8 +36,12 @@ static bool decode_Pet( || (((*result).species_choice == Pet_species_dog_c) && ((1))) || (((*result).species_choice == Pet_species_other_c) && ((1)))) || (zcbor_error(state, ZCBOR_ERR_WRONG_VALUE), false)))))) || (zcbor_list_map_end_force_decode(state), false)) && zcbor_list_end_decode(state)))); - if (!tmp_result) + if (!tmp_result) { zcbor_trace_file(state); + zcbor_log("%s error: %s\r\n", __func__, zcbor_error_str(zcbor_peek_error(state))); + } else { + zcbor_log("%s success\r\n", __func__); + } return tmp_result; } diff --git a/samples/pet/src/pet_encode.c b/samples/pet/src/pet_encode.c index 42485c40..e006a490 100644 --- a/samples/pet/src/pet_encode.c +++ b/samples/pet/src/pet_encode.c @@ -37,8 +37,12 @@ static bool encode_Pet( : (((*input).species_choice == Pet_species_other_c) ? ((zcbor_uint32_put(state, (3)))) : false))))) || (zcbor_list_map_end_force_encode(state), false)) && zcbor_list_end_encode(state, 3)))); - if (!tmp_result) + if (!tmp_result) { zcbor_trace_file(state); + zcbor_log("%s error: %s\r\n", __func__, zcbor_error_str(zcbor_peek_error(state))); + } else { + zcbor_log("%s success\r\n", __func__); + } return tmp_result; } diff --git a/src/zcbor_common.c b/src/zcbor_common.c index 9784d490..1caf8066 100644 --- a/src/zcbor_common.c +++ b/src/zcbor_common.c @@ -294,7 +294,6 @@ 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_log("Return error: %d\r\n", err); return err; } diff --git a/zcbor/zcbor.py b/zcbor/zcbor.py index 191d0460..baedb7f5 100755 --- a/zcbor/zcbor.py +++ b/zcbor/zcbor.py @@ -2630,8 +2630,12 @@ def render_function(self, xcoder, mode): bool tmp_result = ({ body }); - if (!tmp_result) + if (!tmp_result) {{ zcbor_trace_file(state); + zcbor_log("%s error: %s\\r\\n", __func__, zcbor_error_str(zcbor_peek_error(state))); + }} else {{ + zcbor_log("%s success\\r\\n", __func__); + }} return tmp_result; }}""".replace(" \n", "") # call replace() to remove empty lines.