Zephyr Project API  3.1.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
33/* Z_C_GENERIC is used there */
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
47#ifdef __xtensa__
48#define CBPRINTF_PACKAGE_ALIGNMENT 16
49#else
50#define CBPRINTF_PACKAGE_ALIGNMENT \
51 Z_POW2_CEIL(COND_CODE_1(CONFIG_CBPRINTF_PACKAGE_LONGDOUBLE, \
52 (sizeof(long double)), (MAX(sizeof(double), sizeof(long long)))))
53#endif
54
55BUILD_ASSERT(Z_IS_POW2(CBPRINTF_PACKAGE_ALIGNMENT));
56
65#define CBPRINTF_PACKAGE_CONST_CHAR_RO BIT(0)
66
68#define CBPRINTF_PACKAGE_ADD_RO_STR_POS BIT(1)
69
74#define CBPRINTF_PACKAGE_ADD_RW_STR_POS BIT(2)
75
76#define Z_CBPRINTF_PACKAGE_FIRST_RO_STR_BITS 3
77#define Z_CBPRINTF_PACKAGE_FIRST_RO_STR_OFFSET 3
78#define Z_CBPRINTF_PACKAGE_FIRST_RO_STR_MASK BIT_MASK(Z_CBPRINTF_PACKAGE_FIRST_RO_STR_BITS)
79
87#define CBPRINTF_PACKAGE_FIRST_RO_STR_CNT(n) \
88 (n << Z_CBPRINTF_PACKAGE_FIRST_RO_STR_OFFSET)
89
93#define Z_CBPRINTF_PACKAGE_FIRST_RO_STR_CNT_GET(flags) \
94 (((flags) >> Z_CBPRINTF_PACKAGE_FIRST_RO_STR_OFFSET) & Z_CBPRINTF_PACKAGE_FIRST_RO_STR_MASK)
95
102#define CBPRINTF_PACKAGE_ADD_STRING_IDXS \
103 (CBPRINTF_PACKAGE_ADD_RO_STR_POS | CBPRINTF_PACKAGE_CONST_CHAR_RO)
104
119#define CBPRINTF_PACKAGE_COPY_RO_STR BIT(0)
120
131#define CBPRINTF_PACKAGE_COPY_RW_STR BIT(1)
132
138#define CBPRINTF_PACKAGE_COPY_KEEP_RO_STR BIT(2)
139
159#ifdef __CHECKER__
160typedef int (*cbprintf_cb)(int c, void *ctx);
161#else
162typedef int (*cbprintf_cb)(/* int c, void *ctx */);
163#endif
164
173typedef int (*cbprintf_convert_cb)(const void *buf, size_t len, void *ctx);
174
193typedef int (*cbvprintf_exteral_formatter_func)(cbprintf_cb out, void *ctx,
194 const char *fmt, va_list ap);
195
213#define CBPRINTF_MUST_RUNTIME_PACKAGE(flags, ... /* fmt, ... */) \
214 Z_CBPRINTF_MUST_RUNTIME_PACKAGE(flags, __VA_ARGS__)
215
245#define CBPRINTF_STATIC_PACKAGE(packaged, inlen, outlen, align_offset, flags, \
246 ... /* fmt, ... */) \
247 Z_CBPRINTF_STATIC_PACKAGE(packaged, inlen, outlen, \
248 align_offset, flags, __VA_ARGS__)
249
290__printf_like(4, 5)
291int cbprintf_package(void *packaged,
292 size_t len,
294 const char *format,
295 ...);
296
331int cbvprintf_package(void *packaged,
332 size_t len,
334 const char *format,
335 va_list ap);
336
372int cbprintf_package_convert(void *in_packaged,
373 size_t in_len,
375 void *ctx,
377 uint16_t *strl,
378 size_t strl_len);
379
380/* @interal Context used for package copying. */
381struct z_cbprintf_buf_desc {
382 void *buf;
383 size_t size;
384 size_t off;
385};
386
387/* @internal Function callback used for package copying. */
388static inline int z_cbprintf_cpy(const void *buf, size_t len, void *ctx)
389{
390 struct z_cbprintf_buf_desc *desc = (struct z_cbprintf_buf_desc *)ctx;
391
392 if ((desc->size - desc->off) < len) {
393 return -ENOSPC;
394 }
395
396 memcpy(&((uint8_t *)desc->buf)[desc->off], (void *)buf, len);
397 desc->off += len;
398
399 return len;
400}
401
432static inline int cbprintf_package_copy(void *in_packaged,
433 size_t in_len,
434 void *packaged,
435 size_t len,
437 uint16_t *strl,
438 size_t strl_len)
439{
440 struct z_cbprintf_buf_desc buf_desc = {
441 .buf = packaged,
442 .size = len
443 };
444
445 return cbprintf_package_convert(in_packaged, in_len,
446 packaged ? z_cbprintf_cpy : NULL, &buf_desc,
447 flags, strl, strl_len);
448}
449
479static inline int cbprintf_fsc_package(void *in_packaged,
480 size_t in_len,
481 void *packaged,
482 size_t len)
483{
484 return cbprintf_package_copy(in_packaged, in_len, packaged, len,
487}
488
511 void *ctx,
512 void *packaged);
513
540__printf_like(3, 4)
541int cbprintf(cbprintf_cb out, void *ctx, const char *format, ...);
542
568int cbvprintf(cbprintf_cb out, void *ctx, const char *format, va_list ap);
569
587static inline
588int cbpprintf(cbprintf_cb out, void *ctx, void *packaged)
589{
590 return cbpprintf_external(out, cbvprintf, ctx, packaged);
591}
592
593#ifdef CONFIG_CBPRINTF_LIBC_SUBSTS
594
613__printf_like(2, 3)
614int fprintfcb(FILE * stream, const char *format, ...);
615
633int vfprintfcb(FILE *stream, const char *format, va_list ap);
634
651__printf_like(1, 2)
652int printfcb(const char *format, ...);
653
669int vprintfcb(const char *format, va_list ap);
670
694__printf_like(3, 4)
695int snprintfcb(char *str, size_t size, const char *format, ...);
696
719int vsnprintfcb(char *str, size_t size, const char *format, va_list ap);
720
721#endif /* CONFIG_CBPRINTF_LIBC_SUBSTS */
722
727#ifdef __cplusplus
728}
729#endif
730
731#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_COPY_RW_STR
Append read-write strings from source package to destination package.
Definition: cbprintf.h:131
#define CBPRINTF_PACKAGE_COPY_RO_STR
Append read-only strings from source package to destination package.
Definition: cbprintf.h:119
int cbpprintf_external(cbprintf_cb out, cbvprintf_exteral_formatter_func formatter, void *ctx, void *packaged)
Generate the output for a previously captured format operation using an external formatter.
int cbprintf(cbprintf_cb out, void *ctx, const char *format,...)
*printf-like output through a callback.
static int cbpprintf(cbprintf_cb out, void *ctx, void *packaged)
Generate the output for a previously captured format operation.
Definition: cbprintf.h:588
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:50
int cbvprintf(cbprintf_cb out, void *ctx, const char *format, va_list ap)
varargs-aware *printf-like output through a callback.
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:479
int(* cbprintf_convert_cb)(const void *buf, size_t len, void *ctx)
Signature for a cbprintf multibyte callback function.
Definition: cbprintf.h:173
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:432
int snprintfcb(char *str, size_t size, const char *format,...)
snprintf using Zephyrs cbprintf infrastructure.
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:162
int(* cbvprintf_exteral_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:193
int cbprintf_package(void *packaged, size_t len, uint32_t flags, const char *format,...)
Capture state required to output formatted data later.
#define ENOSPC
Definition: errno.h:67
flags
Definition: http_parser.h:131
char c
Definition: printk.c:71
__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)
Macros to abstract toolchain specific capabilities.