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 <pisit@ndrsolution.com>
This commit is contained in:
Pisit Sawangvonganan 2024-06-21 15:49:40 +07:00 committed by Benjamin Cabé
commit 28ca65de1c
3 changed files with 183 additions and 1 deletions

View file

@ -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)

View file

@ -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 <zephyr/shell/shell_backend.h>
#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);
}

View file

@ -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 <zephyr/shell/shell.h>
/**
* @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 */