Skip to content

Commit

Permalink
zcbor_print.h: Move from printk to printf
Browse files Browse the repository at this point in the history
Needs cleaning up of format strings.

Signed-off-by: Øyvind Rønningstad <oyvind.ronningstad@nordicsemi.no>
  • Loading branch information
oyvindronningstad committed Dec 6, 2023
1 parent e78bb66 commit 54b0062
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 26 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 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.
Expand Down
4 changes: 2 additions & 2 deletions include/zcbor_print.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ extern "C" {
#endif

#ifndef ZCBOR_PRINT_FUNC
#include <zephyr/sys/printk.h>
#define zcbor_do_print(...) printk(__VA_ARGS__)
#include <stdio.h>
#define zcbor_do_print(...) printf(__VA_ARGS__)
#else
#define zcbor_do_print(...) ZCBOR_PRINT_FUNC(__VA_ARGS__)
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/zcbor_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,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);
}
Expand Down
9 changes: 5 additions & 4 deletions src/zcbor_decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
#include <inttypes.h>
#include "zcbor_decode.h"
#include "zcbor_common.h"

Expand Down Expand Up @@ -211,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_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;
Expand Down Expand Up @@ -837,7 +838,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;
}

Expand Down Expand Up @@ -1455,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_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;
}
Expand Down
6 changes: 3 additions & 3 deletions src/zcbor_encode.c
Original file line number Diff line number Diff line change
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_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;
Expand Down Expand Up @@ -389,7 +389,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,
Expand Down Expand Up @@ -596,7 +596,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;
}

Expand Down
5 changes: 1 addition & 4 deletions tests/unit/test1_unit_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 2 additions & 2 deletions tests/unit/test1_unit_tests/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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];
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/test2_cpp/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <zcbor_decode.h>
#include <zcbor_encode.h>
#include <zephyr/sys/printk.h>
#include <stdio.h>
#include <pet_decode.h>

int main(void)
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}
8 changes: 4 additions & 4 deletions tests/unit/test3_float16/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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));
}
}

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 54b0062

Please sign in to comment.