From 28ca65de1ca680bda5ce1315568aac70df7d6bc6 Mon Sep 17 00:00:00 2001 From: Pisit Sawangvonganan Date: Fri, 21 Jun 2024 15:49:40 +0700 Subject: [PATCH] bluetooth: shell: add `bt_shell_private.c` and `bt_shell_private.h` Introduced `bt_shell_private.c` and `bt_shell_private.h` to provide common functions for the Bluetooth `shell_wall_print`. These functions are equivalent to `shell_fprintf`, `shell_info`, `shell_print`, `shell_warn`, `shell_error` and `shell_hexdump` but without requiring the `sh` parameter. The cost of the newly added `bt_shell_fprintf_info` ... `_error` functions will be negligible when there are many individual calls that need to pass both the `sh` and `color` parameters each time. Signed-off-by: Pisit Sawangvonganan --- subsys/bluetooth/host/shell/CMakeLists.txt | 5 +- .../bluetooth/host/shell/bt_shell_private.c | 89 ++++++++++++++++++ .../bluetooth/host/shell/bt_shell_private.h | 90 +++++++++++++++++++ 3 files changed, 183 insertions(+), 1 deletion(-) create mode 100644 subsys/bluetooth/host/shell/bt_shell_private.c create mode 100644 subsys/bluetooth/host/shell/bt_shell_private.h diff --git a/subsys/bluetooth/host/shell/CMakeLists.txt b/subsys/bluetooth/host/shell/CMakeLists.txt index 98f1e6ac2a5..73ff18086f3 100644 --- a/subsys/bluetooth/host/shell/CMakeLists.txt +++ b/subsys/bluetooth/host/shell/CMakeLists.txt @@ -1,7 +1,10 @@ # SPDX-License-Identifier: Apache-2.0 zephyr_library() -zephyr_library_sources(bt.c) +zephyr_library_sources( + bt.c + bt_shell_private.c + ) zephyr_library_sources_ifdef(CONFIG_BT_CONN gatt.c) zephyr_library_sources_ifdef(CONFIG_BT_L2CAP_DYNAMIC_CHANNEL l2cap.c) zephyr_library_sources_ifdef(CONFIG_BT_ISO iso.c) diff --git a/subsys/bluetooth/host/shell/bt_shell_private.c b/subsys/bluetooth/host/shell/bt_shell_private.c new file mode 100644 index 00000000000..a01b968a27a --- /dev/null +++ b/subsys/bluetooth/host/shell/bt_shell_private.c @@ -0,0 +1,89 @@ +/** + * @file bt_shell_private.c + * @brief Bluetooth shell private module + * + * Provide common function which can be shared using privately inside Bluetooth shell. + */ + +/* + * Copyright (c) 2024 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include "bt_shell_private.h" + +static void wall_vfprintf(enum shell_vt100_color color, const char *fmt, va_list args) +{ + int count; + const struct shell *sh; + + count = shell_backend_count_get(); + for (int i = 0; i < count; i++) { + va_list args_copy; + + va_copy(args_copy, args); /* Create a copy of 'args' to safely reuse */ + sh = shell_backend_get(i); + shell_vfprintf(sh, color, fmt, args_copy); + va_end(args_copy); /* Clean up to prevent resource leaks */ + } +} + +void bt_shell_hexdump(const uint8_t *data, size_t len) +{ + int count; + const struct shell *sh; + + count = shell_backend_count_get(); + for (int i = 0; i < count; i++) { + sh = shell_backend_get(i); + shell_hexdump(sh, data, len); + } +} + +void bt_shell_fprintf(enum shell_vt100_color color, + const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + wall_vfprintf(color, fmt, args); + va_end(args); +} + +void bt_shell_fprintf_info(const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + wall_vfprintf(SHELL_INFO, fmt, args); + va_end(args); +} + +void bt_shell_fprintf_print(const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + wall_vfprintf(SHELL_NORMAL, fmt, args); + va_end(args); +} + +void bt_shell_fprintf_warn(const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + wall_vfprintf(SHELL_WARNING, fmt, args); + va_end(args); +} + +void bt_shell_fprintf_error(const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + wall_vfprintf(SHELL_ERROR, fmt, args); + va_end(args); +} diff --git a/subsys/bluetooth/host/shell/bt_shell_private.h b/subsys/bluetooth/host/shell/bt_shell_private.h new file mode 100644 index 00000000000..69460797686 --- /dev/null +++ b/subsys/bluetooth/host/shell/bt_shell_private.h @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2024 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef __BT_SHELL_PRIVATE_H +#define __BT_SHELL_PRIVATE_H + +#include + +/** + * @brief printf-like function which sends formatted data stream to the shell. + * (Bluetooth context specific) + * + * This function can be used from the command handler or from threads, but not + * from an interrupt context. + * + * @param[in] color Printed text color. + * @param[in] fmt Format string. + * @param[in] ... List of parameters to print. + */ +__printf_like(2, 3) void bt_shell_fprintf(enum shell_vt100_color color, + const char *fmt, ...); + +/** + * @brief printf-like function which sends formatted data stream to the shell. + * (Bluetooth context specific) + * + * This function can be used from the command handler or from threads, but not + * from an interrupt context. + * + * @param[in] fmt Format string. + * @param[in] ... List of parameters to print. + */ +__printf_like(1, 2) void bt_shell_fprintf_info(const char *fmt, ...); +__printf_like(1, 2) void bt_shell_fprintf_print(const char *fmt, ...); +__printf_like(1, 2) void bt_shell_fprintf_warn(const char *fmt, ...); +__printf_like(1, 2) void bt_shell_fprintf_error(const char *fmt, ...); + +/** + * @brief Print data in hexadecimal format. + * (Bluetooth context specific) + * + * @param[in] data Pointer to data. + * @param[in] len Length of data. + */ +void bt_shell_hexdump(const uint8_t *data, size_t len); + +/** + * @brief Print info message to the shell. + * (Bluetooth context specific) + * + * @param[in] _ft Format string. + * @param[in] ... List of parameters to print. + */ +#define bt_shell_info(_ft, ...) \ + bt_shell_fprintf_info(_ft "\n", ##__VA_ARGS__) + +/** + * @brief Print normal message to the shell. + * (Bluetooth context specific) + * + * @param[in] _ft Format string. + * @param[in] ... List of parameters to print. + */ +#define bt_shell_print(_ft, ...) \ + bt_shell_fprintf_print(_ft "\n", ##__VA_ARGS__) + +/** + * @brief Print warning message to the shell. + * (Bluetooth context specific) + * + * @param[in] _ft Format string. + * @param[in] ... List of parameters to print. + */ +#define bt_shell_warn(_ft, ...) \ + bt_shell_fprintf_warn(_ft "\n", ##__VA_ARGS__) + +/** + * @brief Print error message to the shell. + * (Bluetooth context specific) + * + * @param[in] _ft Format string. + * @param[in] ... List of parameters to print. + */ +#define bt_shell_error(_ft, ...) \ + bt_shell_fprintf_error(_ft "\n", ##__VA_ARGS__) + +#endif /* __BT_SHELL_PRIVATE_H */