Zephyr API Documentation  3.6.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
cbprintf.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef ZEPHYR_INCLUDE_SYS_CBPRINTF_H_
8#define ZEPHYR_INCLUDE_SYS_CBPRINTF_H_
9
10#include <stdarg.h>
11#include <stddef.h>
12#include <stdint.h>
13#include <zephyr/toolchain.h>
14#include <string.h>
15
16#ifdef CONFIG_CBPRINTF_LIBC_SUBSTS
17#include <stdio.h>
18#endif /* CONFIG_CBPRINTF_LIBC_SUBSTS */
19
20/* Determine if _Generic is supported using macro from toolchain.h.
21 *
22 * @note Z_C_GENERIC is also set for C++ where functionality is implemented
23 * using overloading and templates.
24 */
25#ifndef Z_C_GENERIC
26#if defined(__cplusplus) || TOOLCHAIN_HAS_C_GENERIC
27#define Z_C_GENERIC 1
28#else
29#define Z_C_GENERIC 0
30#endif
31#endif
32
33#ifdef __xtensa__
34#define Z_PKG_HDR_EXT_XTENSA_ALIGNMENT 8
35#ifdef CONFIG_CBPRINTF_PACKAGE_HEADER_STORE_CREATION_FLAGS
36#define Z_PKG_DESC_XTENSA_PADDING 1
37#else
38#define Z_PKG_DESC_XTENSA_PADDING 0
39#endif
40#endif /* __xtensa__ */
41
48
51
54
57
58#ifdef CONFIG_CBPRINTF_PACKAGE_HEADER_STORE_CREATION_FLAGS
60 uint32_t pkg_flags;
61#endif
62#ifdef __xtensa__
63 /*
64 * On Xtensa, the first argument needs to be aligned to 8-byte.
65 * With 32-bit pointers, we need another 4 bytes padding so
66 * that whole struct cbprintf_package_hdr_ext is of multiple of
67 * 8 bytes.
68 */
69 uint32_t xtensa_padding[Z_PKG_DESC_XTENSA_PADDING];
70#endif
71
72} __packed;
73
81
82 void *raw;
83
84#if defined(CONFIG_CBPRINTF_PACKAGE_HEADER_STORE_CREATION_FLAGS) && !defined(CONFIG_64BIT)
85 void *raw2[2];
86#endif
87
88} __packed;
89
90
91
99
101 char *fmt;
102
103 /*
104 * When extending this struct, make sure this align
105 * to pointer size.
106 */
107} __packed;
108
109
115#ifdef __xtensa__
116BUILD_ASSERT(sizeof(struct cbprintf_package_hdr_ext) % Z_PKG_HDR_EXT_XTENSA_ALIGNMENT == 0,
117 "Package header size on Xtensa must be aligned");
118#endif
123/* Z_C_GENERIC is used there */
125
126#ifdef __cplusplus
127extern "C" {
128#endif
129
137#ifdef __xtensa__
138#define CBPRINTF_PACKAGE_ALIGNMENT 16
139#else
140#define CBPRINTF_PACKAGE_ALIGNMENT \
141 Z_POW2_CEIL(COND_CODE_1(CONFIG_CBPRINTF_PACKAGE_LONGDOUBLE, \
142 (sizeof(long double)), (MAX(sizeof(double), sizeof(long long)))))
143#endif
144
145BUILD_ASSERT(Z_IS_POW2(CBPRINTF_PACKAGE_ALIGNMENT));
146
147
156#define CBPRINTF_PACKAGE_CONST_CHAR_RO BIT(0)
157
159#define CBPRINTF_PACKAGE_ADD_RO_STR_POS BIT(1)
160
165#define CBPRINTF_PACKAGE_ADD_RW_STR_POS BIT(2)
166
167#define Z_CBPRINTF_PACKAGE_FIRST_RO_STR_BITS 3
168#define Z_CBPRINTF_PACKAGE_FIRST_RO_STR_OFFSET 3
169#define Z_CBPRINTF_PACKAGE_FIRST_RO_STR_MASK BIT_MASK(Z_CBPRINTF_PACKAGE_FIRST_RO_STR_BITS)
170
178#define CBPRINTF_PACKAGE_FIRST_RO_STR_CNT(n) \
179 (n << Z_CBPRINTF_PACKAGE_FIRST_RO_STR_OFFSET)
180
184#define Z_CBPRINTF_PACKAGE_FIRST_RO_STR_CNT_GET(flags) \
185 (((flags) >> Z_CBPRINTF_PACKAGE_FIRST_RO_STR_OFFSET) & Z_CBPRINTF_PACKAGE_FIRST_RO_STR_MASK)
186
193#define CBPRINTF_PACKAGE_ADD_STRING_IDXS \
194 (CBPRINTF_PACKAGE_ADD_RO_STR_POS | CBPRINTF_PACKAGE_CONST_CHAR_RO)
195
201#define CBPRINTF_PACKAGE_ARGS_ARE_TAGGED BIT(6)
202
218#define CBPRINTF_PACKAGE_CONVERT_RO_STR BIT(0)
220#define CBPRINTF_PACKAGE_COPY_RO_STR CBPRINTF_PACKAGE_CONVERT_RO_STR __DEPRECATED_MACRO
221
232#define CBPRINTF_PACKAGE_CONVERT_RW_STR BIT(1)
234#define CBPRINTF_PACKAGE_COPY_RW_STR CBPRINTF_PACKAGE_CONVERT_RW_STR __DEPRECATED_MACRO
235
241#define CBPRINTF_PACKAGE_CONVERT_KEEP_RO_STR BIT(2)
243#define CBPRINTF_PACKAGE_COPY_KEEP_RO_STR CBPRINTF_PACKAGE_CONVERT_KEEP_RO_STR __DEPRECATED_MACRO
244
262#define CBPRINTF_PACKAGE_CONVERT_PTR_CHECK BIT(3)
263
276#define Z_CBVPRINTF_PROCESS_FLAG_TAGGED_ARGS BIT(0)
277
281
299#ifdef __CHECKER__
300typedef int (*cbprintf_cb)(int c, void *ctx);
301#else
302typedef int (*cbprintf_cb)(/* int c, void *ctx */);
303#endif
304
313typedef int (*cbprintf_convert_cb)(const void *buf, size_t len, void *ctx);
314
333typedef int (*cbvprintf_external_formatter_func)(cbprintf_cb out, void *ctx,
334 const char *fmt, va_list ap);
335
353#define CBPRINTF_MUST_RUNTIME_PACKAGE(flags, ... /* fmt, ... */) \
354 Z_CBPRINTF_MUST_RUNTIME_PACKAGE(flags, __VA_ARGS__)
355
385#define CBPRINTF_STATIC_PACKAGE(packaged, inlen, outlen, align_offset, flags, \
386 ... /* fmt, ... */) \
387 Z_CBPRINTF_STATIC_PACKAGE(packaged, inlen, outlen, \
388 align_offset, flags, __VA_ARGS__)
389
430__printf_like(4, 5)
431int cbprintf_package(void *packaged,
432 size_t len,
434 const char *format,
435 ...);
436
471int cbvprintf_package(void *packaged,
472 size_t len,
474 const char *format,
475 va_list ap);
476
512int cbprintf_package_convert(void *in_packaged,
513 size_t in_len,
515 void *ctx,
517 uint16_t *strl,
518 size_t strl_len);
519
520/* @interal Context used for package copying. */
521struct z_cbprintf_buf_desc {
522 void *buf;
523 size_t size;
524 size_t off;
525};
526
527/* @internal Function callback used for package copying. */
528static inline int z_cbprintf_cpy(const void *buf, size_t len, void *ctx)
529{
530 struct z_cbprintf_buf_desc *desc = (struct z_cbprintf_buf_desc *)ctx;
531
532 if ((desc->size - desc->off) < len) {
533 return -ENOSPC;
534 }
535
536 memcpy(&((uint8_t *)desc->buf)[desc->off], buf, len);
537 desc->off += len;
538
539 return len;
540}
541
572static inline int cbprintf_package_copy(void *in_packaged,
573 size_t in_len,
574 void *packaged,
575 size_t len,
577 uint16_t *strl,
578 size_t strl_len)
579{
580 struct z_cbprintf_buf_desc buf_desc = {
581 .buf = packaged,
582 .size = len,
583 .off = 0,
584 };
585
586 return cbprintf_package_convert(in_packaged, in_len,
587 packaged ? z_cbprintf_cpy : NULL, &buf_desc,
588 flags, strl, strl_len);
589}
590
620static inline int cbprintf_fsc_package(void *in_packaged,
621 size_t in_len,
622 void *packaged,
623 size_t len)
624{
625 return cbprintf_package_copy(in_packaged, in_len, packaged, len,
628}
629
652 void *ctx,
653 void *packaged);
654
681__printf_like(3, 4)
682int cbprintf(cbprintf_cb out, void *ctx, const char *format, ...);
683
712int z_cbvprintf_impl(cbprintf_cb out, void *ctx, const char *format,
713 va_list ap, uint32_t flags);
714
740#ifdef CONFIG_PICOLIBC
741int cbvprintf(cbprintf_cb out, void *ctx, const char *format, va_list ap);
742#else
743static inline
744int cbvprintf(cbprintf_cb out, void *ctx, const char *format, va_list ap)
745{
746 return z_cbvprintf_impl(out, ctx, format, ap, 0);
747}
748#endif
749
777static inline
779 const char *format, va_list ap)
780{
781 return z_cbvprintf_impl(out, ctx, format, ap,
782 Z_CBVPRINTF_PROCESS_FLAG_TAGGED_ARGS);
783}
784
802static inline
803int cbpprintf(cbprintf_cb out, void *ctx, void *packaged)
804{
805#if defined(CONFIG_CBPRINTF_PACKAGE_SUPPORT_TAGGED_ARGUMENTS)
806 union cbprintf_package_hdr *hdr =
807 (union cbprintf_package_hdr *)packaged;
808
809 if ((hdr->desc.pkg_flags & CBPRINTF_PACKAGE_ARGS_ARE_TAGGED)
812 ctx, packaged);
813 }
814#endif
815
816 return cbpprintf_external(out, cbvprintf, ctx, packaged);
817}
818
819#ifdef CONFIG_CBPRINTF_LIBC_SUBSTS
820
821#ifdef CONFIG_PICOLIBC
822
823#define fprintfcb(stream, ...) fprintf(stream, __VA_ARGS__)
824#define vfprintfcb(stream, format, ap) vfprintf(stream, format, ap)
825#define printfcb(format, ...) printf(format, __VA_ARGS__)
826#define vprintfcb(format, ap) vprintf(format, ap)
827#define snprintfcb(str, size, ...) snprintf(str, size, __VA_ARGS__)
828#define vsnprintfcb(str, size, format, ap) vsnprintf(str, size, format, ap)
829
830#else
831
850__printf_like(2, 3)
851int fprintfcb(FILE * stream, const char *format, ...);
852
870int vfprintfcb(FILE *stream, const char *format, va_list ap);
871
888__printf_like(1, 2)
889int printfcb(const char *format, ...);
890
906int vprintfcb(const char *format, va_list ap);
907
931__printf_like(3, 4)
932int snprintfcb(char *str, size_t size, const char *format, ...);
933
956int vsnprintfcb(char *str, size_t size, const char *format, va_list ap);
957
958#endif /* CONFIG_PICOLIBC */
959#endif /* CONFIG_CBPRINTF_LIBC_SUBSTS */
960
965#ifdef __cplusplus
966}
967#endif
968
969#endif /* ZEPHYR_INCLUDE_SYS_CBPRINTF_H_ */
irp nz macro MOVR cc s mov cc s endm endr irp aw macro LDR aa off
Definition: asm-macro-32-bit-gnu.h:17
#define CBPRINTF_PACKAGE_CONVERT_RO_STR
Append read-only strings from source package to destination package.
Definition: cbprintf.h:218
#define CBPRINTF_PACKAGE_CONVERT_RW_STR
Append read-write strings from source package to destination package.
Definition: cbprintf.h:232
#define CBPRINTF_PACKAGE_ARGS_ARE_TAGGED
Indicate the incoming arguments are tagged.
Definition: cbprintf.h:201
int cbprintf(cbprintf_cb out, void *ctx, const char *format,...)
*printf-like output through a callback.
int(* cbvprintf_external_formatter_func)(cbprintf_cb out, void *ctx, const char *fmt, va_list ap)
Signature for a external formatter function identical to cbvprintf.
Definition: cbprintf.h:333
static int cbpprintf(cbprintf_cb out, void *ctx, void *packaged)
Generate the output for a previously captured format operation.
Definition: cbprintf.h:803
int printfcb(const char *format,...)
printf using Zephyrs cbprintf infrastructure.
int vfprintfcb(FILE *stream, const char *format, va_list ap)
vfprintf using Zephyrs cbprintf infrastructure.
int fprintfcb(FILE *stream, const char *format,...)
fprintf using Zephyrs cbprintf infrastructure.
int vsnprintfcb(char *str, size_t size, const char *format, va_list ap)
vsnprintf using Zephyrs cbprintf infrastructure.
#define CBPRINTF_PACKAGE_ALIGNMENT
Required alignment of the buffer used for packaging.
Definition: cbprintf.h:140
int cbprintf_package_convert(void *in_packaged, size_t in_len, cbprintf_convert_cb cb, void *ctx, uint32_t flags, uint16_t *strl, size_t strl_len)
Convert a package.
static int cbprintf_fsc_package(void *in_packaged, size_t in_len, void *packaged, size_t len)
Convert package to fully self-contained (fsc) package.
Definition: cbprintf.h:620
int(* cbprintf_convert_cb)(const void *buf, size_t len, void *ctx)
Signature for a cbprintf multibyte callback function.
Definition: cbprintf.h:313
static int cbprintf_package_copy(void *in_packaged, size_t in_len, void *packaged, size_t len, uint32_t flags, uint16_t *strl, size_t strl_len)
Copy package with optional appending of strings.
Definition: cbprintf.h:572
static int cbvprintf(cbprintf_cb out, void *ctx, const char *format, va_list ap)
varargs-aware *printf-like output through a callback.
Definition: cbprintf.h:744
int snprintfcb(char *str, size_t size, const char *format,...)
snprintf using Zephyrs cbprintf infrastructure.
int cbpprintf_external(cbprintf_cb out, cbvprintf_external_formatter_func formatter, void *ctx, void *packaged)
Generate the output for a previously captured format operation using an external formatter.
int vprintfcb(const char *format, va_list ap)
vprintf using Zephyrs cbprintf infrastructure.
int cbvprintf_package(void *packaged, size_t len, uint32_t flags, const char *format, va_list ap)
Capture state required to output formatted data later.
int(* cbprintf_cb)()
Signature for a cbprintf callback function.
Definition: cbprintf.h:302
int cbprintf_package(void *packaged, size_t len, uint32_t flags, const char *format,...)
Capture state required to output formatted data later.
static int cbvprintf_tagged_args(cbprintf_cb out, void *ctx, const char *format, va_list ap)
varargs-aware *printf-like output through a callback with tagged arguments.
Definition: cbprintf.h:778
#define ENOSPC
No space left on device.
Definition: errno.h:67
flags
Definition: parser.h:96
__UINT32_TYPE__ uint32_t
Definition: stdint.h:90
__UINT8_TYPE__ uint8_t
Definition: stdint.h:88
__UINT16_TYPE__ uint16_t
Definition: stdint.h:89
int FILE
Definition: stdio.h:22
void * memcpy(void *ZRESTRICT d, const void *ZRESTRICT s, size_t n)
cbprintf package descriptor.
Definition: cbprintf.h:45
uint8_t len
Package length (in 32 bit words)
Definition: cbprintf.h:47
uint8_t str_cnt
Number of appended strings in the package.
Definition: cbprintf.h:50
uint8_t ro_str_cnt
Number of read-only strings, indexes appended to the package.
Definition: cbprintf.h:53
uint8_t rw_str_cnt
Number of read-write strings, indexes appended to the package.
Definition: cbprintf.h:56
cbprintf package header with format string pointer.
Definition: cbprintf.h:96
union cbprintf_package_hdr hdr
Header of package.
Definition: cbprintf.h:98
char * fmt
Pointer to format string.
Definition: cbprintf.h:101
Macros to abstract toolchain specific capabilities.
cbprintf package header
Definition: cbprintf.h:78
void * raw
Definition: cbprintf.h:82
struct cbprintf_package_desc desc
Header description.
Definition: cbprintf.h:80