diff options
author | Tom Hughes <tomhughes@chromium.org> | 2022-07-08 10:33:14 -0700 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2022-07-14 23:31:03 +0000 |
commit | 078ce31b33405582d5b1c3229a135300609d7d40 (patch) | |
tree | 246331bbca08e7286315ae942597ed58ac6052c6 /include/printf.h | |
parent | bc70d8d06b8038e0d06ced247e039c1237a14a7f (diff) | |
download | chrome-ec-078ce31b33405582d5b1c3229a135300609d7d40.tar.gz |
printf: Add snprintf_hex_buffer
This function replicate the behavior of the non-standard hex buffer
format "%ph" used in EC and make it possible to replace "%ph" by
printing the bytes into a buffer and then printing the resulting string
with "%s".
A followup commit will replace usage of "%ph" with these functions.
Using standard format specifiers makes it easier to switch between the
"builtin" EC standard library and the C standard library provided by the
toolchain (or Zephyr).
BRANCH=none
BUG=b:238433667, b:234181908
TEST=make run-printf
Signed-off-by: Tom Hughes <tomhughes@chromium.org>
Change-Id: I46135b25c84abe4ca7fb16ebb78af535622f50c8
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3756179
Reviewed-by: Scott Collyer <scollyer@chromium.org>
Diffstat (limited to 'include/printf.h')
-rw-r--r-- | include/printf.h | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/include/printf.h b/include/printf.h index b6f065b3ba..6df6405503 100644 --- a/include/printf.h +++ b/include/printf.h @@ -12,6 +12,7 @@ #include <stdbool.h> #include <stddef.h> /* For size_t */ #include "common.h" +#include "console.h" /* The declaration of snprintf is changed to crec_snprintf for Zephyr, * so include stdio.h from Zephyr. @@ -178,4 +179,26 @@ int snprintf_timestamp(char *str, size_t size, uint64_t timestamp); */ int snprintf_timestamp_now(char *str, size_t size); +/** + * Prints bytes as a hex string in the provided buffer. + * + * Guarantees NUL-termination if size != 0. + * + * @param[out] str Destination string + * @param[in] size Size of @str in bytes + * @param[in] params Data to print + * @return Length of string written to @str, not including terminating NUL. + * @return -EC_ERROR_OVERFLOW when @str buffer is not large enough. + * @return -EC_ERROR_INVAL when @size is 0. + */ +int snprintf_hex_buffer(char *str, size_t size, + const struct hex_buffer_params *params); + +/** + * @param[in] num_bytes + * @return number of bytes needed to store @num_bytes as a string (including + * terminating '\0'). + */ +size_t hex_str_buf_size(size_t num_bytes); + #endif /* __CROS_EC_PRINTF_H */ |