1 /*
2  * Copyright (c) 2018 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_INCLUDE_SHELL_FPRINTF_H_
8 #define ZEPHYR_INCLUDE_SHELL_FPRINTF_H_
9 
10 #include <zephyr/kernel.h>
11 #include <stdbool.h>
12 #include <stddef.h>
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 typedef void (*shell_fprintf_fwrite)(const void *user_ctx,
19 				     const char *data,
20 				     size_t length);
21 
22 struct shell_fprintf_control_block {
23 	size_t buffer_cnt;
24 	bool autoflush;
25 };
26 /**
27  * @brief fprintf context
28  */
29 struct shell_fprintf {
30 	uint8_t *buffer;
31 	size_t buffer_size;
32 	shell_fprintf_fwrite fwrite;
33 	const void *user_ctx;
34 	struct shell_fprintf_control_block *ctrl_blk;
35 };
36 
37 /**
38  * @brief Macro for defining shell_fprintf instance.
39  *
40  * @param _name		Instance name.
41  * @param _user_ctx	Pointer to user data.
42  * @param _buf		Pointer to output buffer
43  * @param _size		Size of output buffer.
44  * @param _autoflush	Indicator if buffer shall be automatically flush.
45  * @param _fwrite	Pointer to function sending data stream.
46  */
47 #define Z_SHELL_FPRINTF_DEFINE(_name, _user_ctx, _buf, _size,	\
48 			    _autoflush, _fwrite)		\
49 	static struct shell_fprintf_control_block		\
50 				_name##_shell_fprintf_ctx = {	\
51 		.autoflush = _autoflush,			\
52 		.buffer_cnt = 0					\
53 	};							\
54 	static const struct shell_fprintf _name = {		\
55 		.buffer = _buf,					\
56 		.buffer_size = _size,				\
57 		.fwrite = _fwrite,				\
58 		.user_ctx = _user_ctx,				\
59 		.ctrl_blk = &_name##_shell_fprintf_ctx		\
60 	}
61 
62 /**
63  * @brief fprintf like function which send formatted data stream to output.
64  *
65  * @param sh_fprintf	fprintf instance.
66  * @param fmt		Format string.
67  * @param args		List of parameters to print.
68  */
69 void z_shell_fprintf_fmt(const struct shell_fprintf *sh_fprintf,
70 			 char const *fmt, va_list args);
71 
72 /**
73  * @brief function flushing data stored in io_buffer.
74  *
75  * @param sh_fprintf	fprintf instance
76  */
77 void z_shell_fprintf_buffer_flush(const struct shell_fprintf *sh_fprintf);
78 
79 #ifdef __cplusplus
80 }
81 #endif
82 
83 #endif /* ZEPHYR_INCLUDE_SHELL_FPRINTF_H_ */
84