Zephyr API Documentation 4.5.0-rc1
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#ifdef __xtensa__
21#define Z_PKG_HDR_EXT_XTENSA_ALIGNMENT 8
22#ifdef CONFIG_CBPRINTF_PACKAGE_HEADER_STORE_CREATION_FLAGS
23#define Z_PKG_DESC_XTENSA_PADDING 1
24#else
25#define Z_PKG_DESC_XTENSA_PADDING 0
26#endif
27#endif /* __xtensa__ */
28
35
38
41
44
45#ifdef CONFIG_CBPRINTF_PACKAGE_HEADER_STORE_CREATION_FLAGS
47 uint32_t pkg_flags;
48#endif
49#ifdef __xtensa__
50 /*
51 * On Xtensa, the first argument needs to be aligned to 8-byte.
52 * With 32-bit pointers, we need another 4 bytes padding so
53 * that whole struct cbprintf_package_hdr_ext is of multiple of
54 * 8 bytes.
55 */
56 uint32_t xtensa_padding[Z_PKG_DESC_XTENSA_PADDING];
57#endif
58
59} __packed;
60
68
69 void *raw;
70
71#if defined(CONFIG_CBPRINTF_PACKAGE_HEADER_STORE_CREATION_FLAGS) && !defined(CONFIG_64BIT)
72 void *raw2[2];
73#endif
74
75} __packed;
76
77
78
86
88 char *fmt;
89
90 /*
91 * When extending this struct, make sure this align
92 * to pointer size.
93 */
94} __packed;
95
96
102#ifdef __xtensa__
103BUILD_ASSERT(sizeof(struct cbprintf_package_hdr_ext) % Z_PKG_HDR_EXT_XTENSA_ALIGNMENT == 0,
104 "Package header size on Xtensa must be aligned");
105#endif
109
111
112#ifdef __cplusplus
113extern "C" {
114#endif
115
121
123#ifdef __xtensa__
124#define CBPRINTF_PACKAGE_ALIGNMENT 16
125#else
126#define CBPRINTF_PACKAGE_ALIGNMENT \
127 Z_POW2_CEIL(COND_CODE_1(CONFIG_CBPRINTF_PACKAGE_LONGDOUBLE, \
128 (sizeof(long double)), (MAX(sizeof(double), sizeof(long long)))))
129#endif
130
131BUILD_ASSERT(Z_IS_POW2(CBPRINTF_PACKAGE_ALIGNMENT));
132
133
137
142#define CBPRINTF_PACKAGE_CONST_CHAR_RO BIT(0)
143
145#define CBPRINTF_PACKAGE_ADD_RO_STR_POS BIT(1)
146
151#define CBPRINTF_PACKAGE_ADD_RW_STR_POS BIT(2)
152
153#define Z_CBPRINTF_PACKAGE_FIRST_RO_STR_BITS 3
154#define Z_CBPRINTF_PACKAGE_FIRST_RO_STR_OFFSET 3
155#define Z_CBPRINTF_PACKAGE_FIRST_RO_STR_MASK BIT_MASK(Z_CBPRINTF_PACKAGE_FIRST_RO_STR_BITS)
156
164#define CBPRINTF_PACKAGE_FIRST_RO_STR_CNT(n) \
165 (n << Z_CBPRINTF_PACKAGE_FIRST_RO_STR_OFFSET)
166
170#define Z_CBPRINTF_PACKAGE_FIRST_RO_STR_CNT_GET(flags) \
171 (((flags) >> Z_CBPRINTF_PACKAGE_FIRST_RO_STR_OFFSET) & Z_CBPRINTF_PACKAGE_FIRST_RO_STR_MASK)
172
179#define CBPRINTF_PACKAGE_ADD_STRING_IDXS \
180 (CBPRINTF_PACKAGE_ADD_RO_STR_POS | CBPRINTF_PACKAGE_CONST_CHAR_RO)
181
187#define CBPRINTF_PACKAGE_ARGS_ARE_TAGGED BIT(6)
188
190
195
204#define CBPRINTF_PACKAGE_CONVERT_RO_STR BIT(0)
205
216#define CBPRINTF_PACKAGE_CONVERT_RW_STR BIT(1)
217
223#define CBPRINTF_PACKAGE_CONVERT_KEEP_RO_STR BIT(2)
224
242#define CBPRINTF_PACKAGE_CONVERT_PTR_CHECK BIT(3)
243
245
250
256#define Z_CBVPRINTF_PROCESS_FLAG_TAGGED_ARGS BIT(0)
257
259
261
278typedef int (*cbprintf_cb)(int c, void *ctx);
279
288typedef int (*cbprintf_convert_cb)(const void *buf, size_t len, void *ctx);
289
308typedef int (*cbvprintf_external_formatter_func)(cbprintf_cb out, void *ctx,
309 const char *fmt, va_list ap);
310
328#define CBPRINTF_MUST_RUNTIME_PACKAGE(flags, ... /* fmt, ... */) \
329 Z_CBPRINTF_MUST_RUNTIME_PACKAGE(flags, __VA_ARGS__)
330
360#define CBPRINTF_STATIC_PACKAGE(packaged, inlen, outlen, align_offset, flags, \
361 ... /* fmt, ... */) \
362 Z_CBPRINTF_STATIC_PACKAGE(packaged, inlen, outlen, \
363 align_offset, flags, __VA_ARGS__)
364
405__printf_like(4, 5)
406int cbprintf_package(void *packaged,
407 size_t len,
409 const char *format,
410 ...);
411
446int cbvprintf_package(void *packaged,
447 size_t len,
449 const char *format,
450 va_list ap);
451
487int cbprintf_package_convert(void *in_packaged,
488 size_t in_len,
490 void *ctx,
492 uint16_t *strl,
493 size_t strl_len);
494
495/* @internal Context used for package copying. */
496struct z_cbprintf_buf_desc {
497 void *buf;
498 size_t size;
499 size_t off;
500};
501
502/* @internal Function callback used for package copying. */
503static inline int z_cbprintf_cpy(const void *buf, size_t len, void *ctx)
504{
505 struct z_cbprintf_buf_desc *desc = (struct z_cbprintf_buf_desc *)ctx;
506
507 if (len == 0) {
508 return 0;
509 }
510
511 if ((desc->size - desc->off) < len) {
512 return -ENOSPC;
513 }
514
515 memcpy(&((uint8_t *)desc->buf)[desc->off], buf, len);
516 desc->off += len;
517
518 return len;
519}
520
551static inline int cbprintf_package_copy(void *in_packaged,
552 size_t in_len,
553 void *packaged,
554 size_t len,
556 uint16_t *strl,
557 size_t strl_len)
558{
559 struct z_cbprintf_buf_desc buf_desc = {
560 .buf = packaged,
561 .size = len,
562 .off = 0,
563 };
564
565 return cbprintf_package_convert(in_packaged, in_len,
566 packaged ? z_cbprintf_cpy : NULL, &buf_desc,
567 flags, strl, strl_len);
568}
569
599static inline int cbprintf_fsc_package(void *in_packaged,
600 size_t in_len,
601 void *packaged,
602 size_t len)
603{
604 return cbprintf_package_copy(in_packaged, in_len, packaged, len,
607}
608
631 void *ctx,
632 void *packaged);
633
660__printf_like(3, 4)
661int cbprintf(cbprintf_cb out, void *ctx, const char *format, ...);
662
691int z_cbvprintf_impl(cbprintf_cb out, void *ctx, const char *format,
692 va_list ap, uint32_t flags);
693
719#ifdef CONFIG_PICOLIBC
720int cbvprintf(cbprintf_cb out, void *ctx, const char *format, va_list ap);
721#else
722static inline
723int cbvprintf(cbprintf_cb out, void *ctx, const char *format, va_list ap)
724{
725 return z_cbvprintf_impl(out, ctx, format, ap, 0);
726}
727#endif
728
756static inline
758 const char *format, va_list ap)
759{
760 return z_cbvprintf_impl(out, ctx, format, ap,
761 Z_CBVPRINTF_PROCESS_FLAG_TAGGED_ARGS);
762}
763
781static inline
782int cbpprintf(cbprintf_cb out, void *ctx, void *packaged)
783{
784#if defined(CONFIG_CBPRINTF_PACKAGE_SUPPORT_TAGGED_ARGUMENTS)
785 union cbprintf_package_hdr *hdr =
786 (union cbprintf_package_hdr *)packaged;
787
788 if ((hdr->desc.pkg_flags & CBPRINTF_PACKAGE_ARGS_ARE_TAGGED)
791 ctx, packaged);
792 }
793#endif
794
795 return cbpprintf_external(out, cbvprintf, ctx, packaged);
796}
797
798#ifdef CONFIG_CBPRINTF_LIBC_SUBSTS
799
800#ifdef CONFIG_PICOLIBC
801
802#define fprintfcb(stream, ...) fprintf(stream, __VA_ARGS__)
803#define vfprintfcb(stream, format, ap) vfprintf(stream, format, ap)
804#define printfcb(format, ...) printf(format, __VA_ARGS__)
805#define vprintfcb(format, ap) vprintf(format, ap)
806#define snprintfcb(str, size, ...) snprintf(str, size, __VA_ARGS__)
807#define vsnprintfcb(str, size, format, ap) vsnprintf(str, size, format, ap)
808
809#else
810
829__printf_like(2, 3)
830int fprintfcb(FILE * stream, const char *format, ...);
831
849int vfprintfcb(FILE *stream, const char *format, va_list ap);
850
867__printf_like(1, 2)
868int printfcb(const char *format, ...);
869
885int vprintfcb(const char *format, va_list ap);
886
910__printf_like(3, 4)
911int snprintfcb(char *str, size_t size, const char *format, ...);
912
935int vsnprintfcb(char *str, size_t size, const char *format, va_list ap);
936
937#endif /* CONFIG_PICOLIBC */
938#endif /* CONFIG_CBPRINTF_LIBC_SUBSTS */
939
943
944#ifdef __cplusplus
945}
946#endif
947
948#endif /* ZEPHYR_INCLUDE_SYS_CBPRINTF_H_ */
Argument-type tags used by the cbprintf packaging format.
Internal helpers backing the cbprintf() implementation.
#define CBPRINTF_PACKAGE_CONVERT_RO_STR
Append read-only strings from source package to destination package.
Definition cbprintf.h:204
#define CBPRINTF_PACKAGE_CONVERT_RW_STR
Append read-write strings from source package to destination package.
Definition cbprintf.h:216
#define CBPRINTF_PACKAGE_ARGS_ARE_TAGGED
Indicate the incoming arguments are tagged.
Definition cbprintf.h:187
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:308
int(* cbprintf_cb)(int c, void *ctx)
Signature for a cbprintf callback function.
Definition cbprintf.h:278
static int cbpprintf(cbprintf_cb out, void *ctx, void *packaged)
Generate the output for a previously captured format operation.
Definition cbprintf.h:782
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:126
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:599
int(* cbprintf_convert_cb)(const void *buf, size_t len, void *ctx)
Signature for a cbprintf multibyte callback function.
Definition cbprintf.h:288
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:551
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:723
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_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:757
#define ENOSPC
No space left on device.
Definition errno.h:67
flags
Definition parser.h:86
__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:32
uint8_t len
Package length (in 32 bit words).
Definition cbprintf.h:34
uint8_t str_cnt
Number of appended strings in the package.
Definition cbprintf.h:37
uint8_t ro_str_cnt
Number of read-only strings, indexes appended to the package.
Definition cbprintf.h:40
uint8_t rw_str_cnt
Number of read-write strings, indexes appended to the package.
Definition cbprintf.h:43
cbprintf package header with format string pointer.
Definition cbprintf.h:83
union cbprintf_package_hdr hdr
Header of package.
Definition cbprintf.h:85
char * fmt
Pointer to format string.
Definition cbprintf.h:88
Macros to abstract toolchain specific capabilities.
cbprintf package header
Definition cbprintf.h:65
void * raw
Definition cbprintf.h:69
struct cbprintf_package_desc desc
Header description.
Definition cbprintf.h:67