Skip to content

Commit 28ca65d

Browse files
ndrs-pstkartben
authored andcommitted
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 <[email protected]>
1 parent 6996436 commit 28ca65d

File tree

3 files changed

+183
-1
lines changed

3 files changed

+183
-1
lines changed

subsys/bluetooth/host/shell/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# SPDX-License-Identifier: Apache-2.0
22

33
zephyr_library()
4-
zephyr_library_sources(bt.c)
4+
zephyr_library_sources(
5+
bt.c
6+
bt_shell_private.c
7+
)
58
zephyr_library_sources_ifdef(CONFIG_BT_CONN gatt.c)
69
zephyr_library_sources_ifdef(CONFIG_BT_L2CAP_DYNAMIC_CHANNEL l2cap.c)
710
zephyr_library_sources_ifdef(CONFIG_BT_ISO iso.c)
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* @file bt_shell_private.c
3+
* @brief Bluetooth shell private module
4+
*
5+
* Provide common function which can be shared using privately inside Bluetooth shell.
6+
*/
7+
8+
/*
9+
* Copyright (c) 2024 Nordic Semiconductor ASA
10+
*
11+
* SPDX-License-Identifier: Apache-2.0
12+
*/
13+
14+
#include <zephyr/shell/shell_backend.h>
15+
#include "bt_shell_private.h"
16+
17+
static void wall_vfprintf(enum shell_vt100_color color, const char *fmt, va_list args)
18+
{
19+
int count;
20+
const struct shell *sh;
21+
22+
count = shell_backend_count_get();
23+
for (int i = 0; i < count; i++) {
24+
va_list args_copy;
25+
26+
va_copy(args_copy, args); /* Create a copy of 'args' to safely reuse */
27+
sh = shell_backend_get(i);
28+
shell_vfprintf(sh, color, fmt, args_copy);
29+
va_end(args_copy); /* Clean up to prevent resource leaks */
30+
}
31+
}
32+
33+
void bt_shell_hexdump(const uint8_t *data, size_t len)
34+
{
35+
int count;
36+
const struct shell *sh;
37+
38+
count = shell_backend_count_get();
39+
for (int i = 0; i < count; i++) {
40+
sh = shell_backend_get(i);
41+
shell_hexdump(sh, data, len);
42+
}
43+
}
44+
45+
void bt_shell_fprintf(enum shell_vt100_color color,
46+
const char *fmt, ...)
47+
{
48+
va_list args;
49+
50+
va_start(args, fmt);
51+
wall_vfprintf(color, fmt, args);
52+
va_end(args);
53+
}
54+
55+
void bt_shell_fprintf_info(const char *fmt, ...)
56+
{
57+
va_list args;
58+
59+
va_start(args, fmt);
60+
wall_vfprintf(SHELL_INFO, fmt, args);
61+
va_end(args);
62+
}
63+
64+
void bt_shell_fprintf_print(const char *fmt, ...)
65+
{
66+
va_list args;
67+
68+
va_start(args, fmt);
69+
wall_vfprintf(SHELL_NORMAL, fmt, args);
70+
va_end(args);
71+
}
72+
73+
void bt_shell_fprintf_warn(const char *fmt, ...)
74+
{
75+
va_list args;
76+
77+
va_start(args, fmt);
78+
wall_vfprintf(SHELL_WARNING, fmt, args);
79+
va_end(args);
80+
}
81+
82+
void bt_shell_fprintf_error(const char *fmt, ...)
83+
{
84+
va_list args;
85+
86+
va_start(args, fmt);
87+
wall_vfprintf(SHELL_ERROR, fmt, args);
88+
va_end(args);
89+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef __BT_SHELL_PRIVATE_H
8+
#define __BT_SHELL_PRIVATE_H
9+
10+
#include <zephyr/shell/shell.h>
11+
12+
/**
13+
* @brief printf-like function which sends formatted data stream to the shell.
14+
* (Bluetooth context specific)
15+
*
16+
* This function can be used from the command handler or from threads, but not
17+
* from an interrupt context.
18+
*
19+
* @param[in] color Printed text color.
20+
* @param[in] fmt Format string.
21+
* @param[in] ... List of parameters to print.
22+
*/
23+
__printf_like(2, 3) void bt_shell_fprintf(enum shell_vt100_color color,
24+
const char *fmt, ...);
25+
26+
/**
27+
* @brief printf-like function which sends formatted data stream to the shell.
28+
* (Bluetooth context specific)
29+
*
30+
* This function can be used from the command handler or from threads, but not
31+
* from an interrupt context.
32+
*
33+
* @param[in] fmt Format string.
34+
* @param[in] ... List of parameters to print.
35+
*/
36+
__printf_like(1, 2) void bt_shell_fprintf_info(const char *fmt, ...);
37+
__printf_like(1, 2) void bt_shell_fprintf_print(const char *fmt, ...);
38+
__printf_like(1, 2) void bt_shell_fprintf_warn(const char *fmt, ...);
39+
__printf_like(1, 2) void bt_shell_fprintf_error(const char *fmt, ...);
40+
41+
/**
42+
* @brief Print data in hexadecimal format.
43+
* (Bluetooth context specific)
44+
*
45+
* @param[in] data Pointer to data.
46+
* @param[in] len Length of data.
47+
*/
48+
void bt_shell_hexdump(const uint8_t *data, size_t len);
49+
50+
/**
51+
* @brief Print info message to the shell.
52+
* (Bluetooth context specific)
53+
*
54+
* @param[in] _ft Format string.
55+
* @param[in] ... List of parameters to print.
56+
*/
57+
#define bt_shell_info(_ft, ...) \
58+
bt_shell_fprintf_info(_ft "\n", ##__VA_ARGS__)
59+
60+
/**
61+
* @brief Print normal message to the shell.
62+
* (Bluetooth context specific)
63+
*
64+
* @param[in] _ft Format string.
65+
* @param[in] ... List of parameters to print.
66+
*/
67+
#define bt_shell_print(_ft, ...) \
68+
bt_shell_fprintf_print(_ft "\n", ##__VA_ARGS__)
69+
70+
/**
71+
* @brief Print warning message to the shell.
72+
* (Bluetooth context specific)
73+
*
74+
* @param[in] _ft Format string.
75+
* @param[in] ... List of parameters to print.
76+
*/
77+
#define bt_shell_warn(_ft, ...) \
78+
bt_shell_fprintf_warn(_ft "\n", ##__VA_ARGS__)
79+
80+
/**
81+
* @brief Print error message to the shell.
82+
* (Bluetooth context specific)
83+
*
84+
* @param[in] _ft Format string.
85+
* @param[in] ... List of parameters to print.
86+
*/
87+
#define bt_shell_error(_ft, ...) \
88+
bt_shell_fprintf_error(_ft "\n", ##__VA_ARGS__)
89+
90+
#endif /* __BT_SHELL_PRIVATE_H */

0 commit comments

Comments
 (0)