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 
wall_vfprintf(enum shell_vt100_color color,const char * fmt,va_list args)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 
bt_shell_hexdump(const uint8_t * data,size_t len)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 
bt_shell_fprintf(enum shell_vt100_color color,const char * fmt,...)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 
bt_shell_fprintf_info(const char * fmt,...)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 
bt_shell_fprintf_print(const char * fmt,...)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 
bt_shell_fprintf_warn(const char * fmt,...)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 
bt_shell_fprintf_error(const char * fmt,...)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 }
90