Zephyr Project API  3.2.0
A Scalable Open Source RTOS
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
39
42
45
48
49#ifdef CONFIG_CBPRINTF_PACKAGE_HEADER_STORE_CREATION_FLAGS
51 uint32_t pkg_flags;
52#endif
53} __packed;
54
62
63 void *raw;
64
65#if defined(CONFIG_CBPRINTF_PACKAGE_HEADER_STORE_CREATION_FLAGS) && !defined(CONFIG_64BIT)
66 void *raw2[2];
67#endif
68
69#ifdef CONFIG_CBPRINTF_PACKAGE_HEADER_STORE_CREATION_FLAGS
70#ifdef __xtensa__
71 /*
72 * On Xtensa, the first argument needs to be aligned to 8-byte.
73 * With 32-bit pointers, we need another 4 bytes padding so
74 * that whole struct cbprintf_package_hdr_ext is of multiple of
75 * 8 bytes.
76 */
77 uint32_t xtensa_padding;
78#endif
79#endif
80
81} __packed;
82
90
92 char *fmt;
93
94 /*
95 * When extending this struct, make sure this align
96 * to pointer size.
97 */
98} __packed;
99
100/* Z_C_GENERIC is used there */
102
103#ifdef __cplusplus
104extern "C" {
105#endif
106
114#ifdef __xtensa__
115#define CBPRINTF_PACKAGE_ALIGNMENT 16
116#else
117#define CBPRINTF_PACKAGE_ALIGNMENT \
118 Z_POW2_CEIL(COND_CODE_1(CONFIG_CBPRINTF_PACKAGE_LONGDOUBLE, \
119 (sizeof(long double)), (MAX(sizeof(double), sizeof(long long)))))
120#endif
121
122BUILD_ASSERT(Z_IS_POW2(CBPRINTF_PACKAGE_ALIGNMENT));
123
132#define CBPRINTF_PACKAGE_CONST_CHAR_RO BIT(0)
133
135#define CBPRINTF_PACKAGE_ADD_RO_STR_POS BIT(1)
136
141#define CBPRINTF_PACKAGE_ADD_RW_STR_POS BIT(2)
142
143#define Z_CBPRINTF_PACKAGE_FIRST_RO_STR_BITS 3
144#define Z_CBPRINTF_PACKAGE_FIRST_RO_STR_OFFSET 3
145#define Z_CBPRINTF_PACKAGE_FIRST_RO_STR_MASK BIT_MASK(Z_CBPRINTF_PACKAGE_FIRST_RO_STR_BITS)
146
154#define CBPRINTF_PACKAGE_FIRST_RO_STR_CNT(n) \
155 (n << Z_CBPRINTF_PACKAGE_FIRST_RO_STR_OFFSET)
156
160#define Z_CBPRINTF_PACKAGE_FIRST_RO_STR_CNT_GET(flags) \
161 (((flags) >> Z_CBPRINTF_PACKAGE_FIRST_RO_STR_OFFSET) & Z_CBPRINTF_PACKAGE_FIRST_RO_STR_MASK)
162
169#define CBPRINTF_PACKAGE_ADD_STRING_IDXS \
170 (CBPRINTF_PACKAGE_ADD_RO_STR_POS | CBPRINTF_PACKAGE_CONST_CHAR_RO)
171
177#define CBPRINTF_PACKAGE_ARGS_ARE_TAGGED BIT(6)
178
193#define CBPRINTF_PACKAGE_CONVERT_RO_STR BIT(0)
194#define CBPRINTF_PACKAGE_COPY_RO_STR CBPRINTF_PACKAGE_CONVERT_RO_STR __DEPRECATED_MACRO
195
206#define CBPRINTF_PACKAGE_CONVERT_RW_STR BIT(1)
207#define CBPRINTF_PACKAGE_COPY_RW_STR CBPRINTF_PACKAGE_CONVERT_RW_STR __DEPRECATED_MACRO
208
214#define CBPRINTF_PACKAGE_CONVERT_KEEP_RO_STR BIT(2)
215#define CBPRINTF_PACKAGE_COPY_KEEP_RO_STR CBPRINTF_PACKAGE_CONVERT_KEEP_RO_STR __DEPRECATED_MACRO
216
234#define CBPRINTF_PACKAGE_CONVERT_PTR_CHECK BIT(3)
235
247#define Z_CBVPRINTF_PROCESS_FLAG_TAGGED_ARGS BIT(0)
248
252
270#ifdef __CHECKER__
271typedef int (*cbprintf_cb)(int c, void *ctx);
272#else
273typedef int (*cbprintf_cb)(/* int c, void *ctx */);
274#endif
275
284typedef int (*cbprintf_convert_cb)(const void *buf, size_t len, void *ctx);
285
304typedef int (*cbvprintf_external_formatter_func)(cbprintf_cb out, void *ctx,
305 const char *fmt, va_list ap);
306
324#define CBPRINTF_MUST_RUNTIME_PACKAGE(flags, ... /* fmt, ... */) \
325 Z_CBPRINTF_MUST_RUNTIME_PACKAGE(flags, __VA_ARGS__)
326
356#define CBPRINTF_STATIC_PACKAGE(packaged, inlen, outlen, align_offset, flags, \
357 ... /* fmt, ... */) \
358 Z_CBPRINTF_STATIC_PACKAGE(packaged, inlen, outlen, \
359 align_offset, flags, __VA_ARGS__)
360
401__printf_like(4, 5)
402int cbprintf_package(void *packaged,
403 size_t len,
405 const char *format,
406 ...);
407
442int cbvprintf_package(void *packaged,
443 size_t len,
445 const char *format,
446 va_list ap);
447
483int cbprintf_package_convert(void *in_packaged,
484 size_t in_len,
486 void *ctx,
488 uint16_t *strl,
489 size_t strl_len);
490
491/* @interal Context used for package copying. */
492struct z_cbprintf_buf_desc {
493 void *buf;
494 size_t size;
495 size_t off;
496};
497
498/* @internal Function callback used for package copying. */
499static inline int z_cbprintf_cpy(const void *buf, size_t len, void *ctx)
500{
501 struct z_cbprintf_buf_desc *desc = (struct z_cbprintf_buf_desc *)ctx;
502
503 if ((desc->size - desc->off) < len) {
504 return -ENOSPC;
505 }
506
507 memcpy(&((uint8_t *)desc->buf)[desc->off], buf, len);
508 desc->off += len;
509
510 return len;
511}
512
543static inline int cbprintf_package_copy(void *in_packaged,
544 size_t in_len,
545 void *packaged,
546 size_t len,
548 uint16_t *strl,
549 size_t strl_len)
550{
551 struct z_cbprintf_buf_desc buf_desc = {
552 .buf = packaged,
553 .size = len,
554 .off = 0,
555 };
556
557 return cbprintf_package_convert(in_packaged, in_len,
558 packaged ? z_cbprintf_cpy : NULL, &buf_desc,
559 flags, strl, strl_len);
560}
561
591static inline int cbprintf_fsc_package(void *in_packaged,
592 size_t in_len,
593 void *packaged,
594 size_t len)
595{
596 return cbprintf_package_copy(in_packaged, in_len, packaged, len,
599}
600
623 void *ctx,
624 void *packaged);
625
652__printf_like(3, 4)
653int cbprintf(cbprintf_cb out, void *ctx, const char *format, ...);
654
683int z_cbvprintf_impl(cbprintf_cb out, void *ctx, const char *format,
684 va_list ap, uint32_t flags);
685
711#ifdef CONFIG_PICOLIBC
712int cbvprintf(cbprintf_cb out, void *ctx, const char *format, va_list ap);
713#else
714static inline
715int cbvprintf(cbprintf_cb out, void *ctx, const char *format, va_list ap)
716{
717 return z_cbvprintf_impl(out, ctx, format, ap, 0);
718}
719#endif
720
748static inline
750 const char *format, va_list ap)
751{
752 return z_cbvprintf_impl(out, ctx, format, ap,
753 Z_CBVPRINTF_PROCESS_FLAG_TAGGED_ARGS);
754}
755
773static inline
774int cbpprintf(cbprintf_cb out, void *ctx, void *packaged)
775{
776#if defined(CONFIG_CBPRINTF_PACKAGE_SUPPORT_TAGGED_ARGUMENTS)
777 union cbprintf_package_hdr *hdr =
778 (union cbprintf_package_hdr *)packaged;
779
780 if ((hdr->desc.pkg_flags & CBPRINTF_PACKAGE_ARGS_ARE_TAGGED)
783 ctx, packaged);
784 }
785#endif
786
787 return cbpprintf_external(out, cbvprintf, ctx, packaged);
788}
789
790#ifdef CONFIG_CBPRINTF_LIBC_SUBSTS
791
792#ifdef CONFIG_PICOLIBC
793
794#define fprintfcb(stream, ...) fprintf(stream, __VA_ARGS__)
795#define vfprintfcb(stream, format, ap) (stream, format, ap)
796#define printfcb(format, ...) printf(format, __VA_ARGS__)
797#define vprintfcb(format, ap) vfprintf(format, ap)
798#define snprintfcb(str, size, ...) snprintf(str, size, __VA_ARGS__)
799#define vsnprintfcb(str, size, format, ap) vsnprintf(str, size, format, ap)
800
801#else
802
821__printf_like(2, 3)
822int fprintfcb(FILE * stream, const char *format, ...);
823
841int vfprintfcb(FILE *stream, const char *format, va_list ap);
842
859__printf_like(1, 2)
860int printfcb(const char *format, ...);
861
877int vprintfcb(const char *format, va_list ap);
878
902__printf_like(3, 4)
903int snprintfcb(char *str, size_t size, const char *format, ...);
904
927int vsnprintfcb(char *str, size_t size, const char *format, va_list ap);
928
929#endif /* CONFIG_PICOLIBC */
930#endif /* CONFIG_CBPRINTF_LIBC_SUBSTS */
931
936#ifdef __cplusplus
937}
938#endif
939
940#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:193
#define CBPRINTF_PACKAGE_CONVERT_RW_STR
Append read-write strings from source package to destination package.
Definition: cbprintf.h:206
#define CBPRINTF_PACKAGE_ARGS_ARE_TAGGED
Indicate the incoming arguments are tagged.
Definition: cbprintf.h:177
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:304
static int cbpprintf(cbprintf_cb out, void *ctx, void *packaged)
Generate the output for a previously captured format operation.
Definition: cbprintf.h:774
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:117
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:591
int(* cbprintf_convert_cb)(const void *buf, size_t len, void *ctx)
Signature for a cbprintf multibyte callback function.
Definition: cbprintf.h:284
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:543
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:715
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:273
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:749
#define ENOSPC
Definition: errno.h:67
flags
Definition: http_parser.h:131
char c
Definition: printk.c:103
__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:36
uint8_t len
Definition: cbprintf.h:38
uint8_t str_cnt
Definition: cbprintf.h:41
uint8_t ro_str_cnt
Definition: cbprintf.h:44
uint8_t rw_str_cnt
Definition: cbprintf.h:47
cbprintf package header with format string pointer.
Definition: cbprintf.h:87
union cbprintf_package_hdr hdr
Definition: cbprintf.h:89
char * fmt
Definition: cbprintf.h:92
Macros to abstract toolchain specific capabilities.
cbprintf package header
Definition: cbprintf.h:59
void * raw
Definition: cbprintf.h:63
struct cbprintf_package_desc desc
Definition: cbprintf.h:61