From 65e350e6af3a8caebf22ec1b7630c0a54dff2e07 Mon Sep 17 00:00:00 2001 From: Luiz Augusto von Dentz Date: Tue, 19 Mar 2019 18:24:34 +0200 Subject: [PATCH] Shell: Introduce shell_hexdump This introduces shell_hexdump API which can be used to print an array such as a network buffer. Signed-off-by: Luiz Augusto von Dentz --- include/shell/shell.h | 9 +++++++++ subsys/shell/shell.c | 22 ++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/include/shell/shell.h b/include/shell/shell.h index 17d21b8bcec..0429c6b4b9e 100644 --- a/include/shell/shell.h +++ b/include/shell/shell.h @@ -624,6 +624,15 @@ int shell_stop(const struct shell *shell); void shell_fprintf(const struct shell *shell, enum shell_vt100_color color, const char *fmt, ...); +/** + * @brief Print data in hexadecimal format. + * + * @param[in] shell Pointer to the shell instance. + * @param[in] data Pointer to data. + * @param[in] len Length of data. + */ +void shell_hexdump(const struct shell *shell, const u8_t *data, size_t len); + /** * @brief Print info message to the shell. * diff --git a/subsys/shell/shell.c b/subsys/shell/shell.c index f68ee9ecb83..73cae22139d 100644 --- a/subsys/shell/shell.c +++ b/subsys/shell/shell.c @@ -1355,6 +1355,28 @@ void shell_fprintf(const struct shell *shell, enum shell_vt100_color color, k_mutex_unlock(&shell->ctx->wr_mtx); } +void shell_hexdump(const struct shell *shell, const u8_t *data, size_t len) +{ + int n = 0; + + while (len--) { + if (n % 16 == 0) { + shell_fprintf(shell, SHELL_NORMAL, "%08X: ", n); + } + + shell_fprintf(shell, SHELL_NORMAL, "%02X ", *data++); + + n++; + if (n % 16 == 0) { + shell_print(shell, ""); + } + } + + if (n % 16) { + shell_print(shell, ""); + } +} + int shell_prompt_change(const struct shell *shell, const char *prompt) { __ASSERT_NO_MSG(shell);