Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shell: introduce shell_xxx_impl wrapper functions for output macros #75340

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions include/zephyr/shell/shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,8 @@ void shell_hexdump(const struct shell *sh, const uint8_t *data, size_t len);
* @param[in] ... List of parameters to print.
*/
#define shell_info(_sh, _ft, ...) \
shell_fprintf(_sh, SHELL_INFO, _ft "\n", ##__VA_ARGS__)
shell_info_impl(_sh, _ft "\n", ##__VA_ARGS__)
void __printf_like(2, 3) shell_info_impl(const struct shell *sh, const char *fmt, ...);

/**
* @brief Print normal message to the shell.
Expand All @@ -1105,7 +1106,8 @@ void shell_hexdump(const struct shell *sh, const uint8_t *data, size_t len);
* @param[in] ... List of parameters to print.
*/
#define shell_print(_sh, _ft, ...) \
shell_fprintf(_sh, SHELL_NORMAL, _ft "\n", ##__VA_ARGS__)
shell_print_impl(_sh, _ft "\n", ##__VA_ARGS__)
void __printf_like(2, 3) shell_print_impl(const struct shell *sh, const char *fmt, ...);

/**
* @brief Print warning message to the shell.
Expand All @@ -1117,7 +1119,8 @@ void shell_hexdump(const struct shell *sh, const uint8_t *data, size_t len);
* @param[in] ... List of parameters to print.
*/
#define shell_warn(_sh, _ft, ...) \
shell_fprintf(_sh, SHELL_WARNING, _ft "\n", ##__VA_ARGS__)
shell_warn_impl(_sh, _ft "\n", ##__VA_ARGS__)
void __printf_like(2, 3) shell_warn_impl(const struct shell *sh, const char *fmt, ...);

/**
* @brief Print error message to the shell.
Expand All @@ -1129,7 +1132,8 @@ void shell_hexdump(const struct shell *sh, const uint8_t *data, size_t len);
* @param[in] ... List of parameters to print.
*/
#define shell_error(_sh, _ft, ...) \
shell_fprintf(_sh, SHELL_ERROR, _ft "\n", ##__VA_ARGS__)
shell_error_impl(_sh, _ft "\n", ##__VA_ARGS__)
void __printf_like(2, 3) shell_error_impl(const struct shell *sh, const char *fmt, ...);

/**
* @brief Process function, which should be executed when data is ready in the
Expand Down
65 changes: 53 additions & 12 deletions subsys/shell/shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -1545,8 +1545,13 @@ void shell_vfprintf(const struct shell *sh, enum shell_vt100_color color,
k_mutex_unlock(&sh->ctx->wr_mtx);
}

/* This function mustn't be used from shell context to avoid deadlock.
* However it can be used in shell command handlers.
/* These functions mustn't be used from shell context to avoid deadlock:
* - shell_fprintf_impl
* - shell_info_impl
* - shell_print_impl
* - shell_warn_impl
* - shell_error_impl
* However, they can be used in shell command handlers.
*/
void shell_fprintf_impl(const struct shell *sh, enum shell_vt100_color color,
const char *fmt, ...)
Expand All @@ -1558,42 +1563,78 @@ void shell_fprintf_impl(const struct shell *sh, enum shell_vt100_color color,
va_end(args);
}

void shell_info_impl(const struct shell *sh, const char *fmt, ...)
{
va_list args;

va_start(args, fmt);
shell_vfprintf(sh, SHELL_INFO, fmt, args);
va_end(args);
}

void shell_print_impl(const struct shell *sh, const char *fmt, ...)
{
va_list args;

va_start(args, fmt);
shell_vfprintf(sh, SHELL_NORMAL, fmt, args);
va_end(args);
}

void shell_warn_impl(const struct shell *sh, const char *fmt, ...)
{
va_list args;

va_start(args, fmt);
shell_vfprintf(sh, SHELL_WARNING, fmt, args);
va_end(args);
}

void shell_error_impl(const struct shell *sh, const char *fmt, ...)
{
va_list args;

va_start(args, fmt);
shell_vfprintf(sh, SHELL_ERROR, fmt, args);
va_end(args);
}

void shell_hexdump_line(const struct shell *sh, unsigned int offset,
const uint8_t *data, size_t len)
{
__ASSERT_NO_MSG(sh);

int i;

shell_fprintf(sh, SHELL_NORMAL, "%08X: ", offset);
shell_print_impl(sh, "%08X: ", offset);

for (i = 0; i < SHELL_HEXDUMP_BYTES_IN_LINE; i++) {
if (i > 0 && !(i % 8)) {
shell_fprintf(sh, SHELL_NORMAL, " ");
shell_print_impl(sh, " ");
}

if (i < len) {
shell_fprintf(sh, SHELL_NORMAL, "%02x ",
data[i] & 0xFF);
shell_print_impl(sh, "%02x ",
data[i] & 0xFF);
} else {
shell_fprintf(sh, SHELL_NORMAL, " ");
shell_print_impl(sh, " ");
}
}

shell_fprintf(sh, SHELL_NORMAL, "|");
shell_print_impl(sh, "|");

for (i = 0; i < SHELL_HEXDUMP_BYTES_IN_LINE; i++) {
if (i > 0 && !(i % 8)) {
shell_fprintf(sh, SHELL_NORMAL, " ");
shell_print_impl(sh, " ");
}

if (i < len) {
char c = data[i];

shell_fprintf(sh, SHELL_NORMAL, "%c",
isprint((int)c) != 0 ? c : '.');
shell_print_impl(sh, "%c",
isprint((int)c) != 0 ? c : '.');
} else {
shell_fprintf(sh, SHELL_NORMAL, " ");
shell_print_impl(sh, " ");
}
}

Expand Down
Loading