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
42 /**
43 * @brief cbprintf package descriptor.
44 */
45 struct cbprintf_package_desc {
46 /** Package length (in 32 bit words) */
47 uint8_t len;
48
49 /** Number of appended strings in the package. */
50 uint8_t str_cnt;
51
52 /** Number of read-only strings, indexes appended to the package */
53 uint8_t ro_str_cnt;
54
55 /** Number of read-write strings, indexes appended to the package */
56 uint8_t rw_str_cnt;
57
58 #ifdef CONFIG_CBPRINTF_PACKAGE_HEADER_STORE_CREATION_FLAGS
59 /** Flags used to create the package */
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
74 /** @brief cbprintf package header
75 *
76 * cbprintf package header, without the format string pointer.
77 */
78 union cbprintf_package_hdr {
79 /** Header description */
80 struct cbprintf_package_desc desc;
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
92 /** @brief cbprintf package header with format string pointer.
93 *
94 * cbprintf package header with format string pointer.
95 */
96 struct cbprintf_package_hdr_ext {
97 /** Header of package */
98 union cbprintf_package_hdr hdr;
99
100 /** Pointer to format string */
101 char *fmt;
102
103 /*
104 * When extending this struct, make sure this align
105 * to pointer size.
106 */
107 } __packed;
108
109
110 /**
111 * @cond INTERNAL_HIDDEN
112 *
113 * Assert that the package hdr does indeed align properly.
114 */
115 #ifdef __xtensa__
116 BUILD_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
119 /**
120 * @endcond
121 */
122
123 /* Z_C_GENERIC is used there */
124 #include <zephyr/sys/cbprintf_internal.h>
125
126 #ifdef __cplusplus
127 extern "C" {
128 #endif
129
130 /**
131 * @defgroup cbprintf_apis Formatted Output APIs
132 * @ingroup utilities
133 * @{
134 */
135
136 /** @brief Required alignment of the buffer used for packaging. */
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
145 BUILD_ASSERT(Z_IS_POW2(CBPRINTF_PACKAGE_ALIGNMENT));
146
147
148 /**@defgroup CBPRINTF_PACKAGE_FLAGS Package flags
149 * @{
150 */
151
152 /** @brief Assume that const char pointer is pointing to read only (constant) strings.
153 *
154 * Flag is valid only for @ref CBPRINTF_STATIC_PACKAGE.
155 */
156 #define CBPRINTF_PACKAGE_CONST_CHAR_RO BIT(0)
157
158 /** @brief Append locations (within the package) of read-only string pointers. */
159 #define CBPRINTF_PACKAGE_ADD_RO_STR_POS BIT(1)
160
161 /** @brief Append locations (within the package) of read-write string pointers.
162 *
163 * When this flag is not used then read-write strings are appended to the package.
164 */
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
171 /** @brief Indicate that @p n first string format arguments are char pointers to
172 * read-only location.
173 *
174 * Runtime algorithm (address analysis) is skipped for those strings.
175 *
176 * @param n Number of string arguments considered as read-only.
177 */
178 #define CBPRINTF_PACKAGE_FIRST_RO_STR_CNT(n) \
179 (n << Z_CBPRINTF_PACKAGE_FIRST_RO_STR_OFFSET)
180
181 /** @brief Get number of first format string arguments which are known to be read-only
182 * string.
183 */
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
187 /** @brief Append indexes of read-only string arguments in the package.
188 *
189 * When used, package contains locations of read-only string arguments. Package
190 * with that information can be converted to fully self-contain package using
191 * @ref cbprintf_fsc_package.
192 */
193 #define CBPRINTF_PACKAGE_ADD_STRING_IDXS \
194 (CBPRINTF_PACKAGE_ADD_RO_STR_POS | CBPRINTF_PACKAGE_CONST_CHAR_RO)
195
196 /** @brief Indicate the incoming arguments are tagged.
197 *
198 * When set, this indicates that the incoming arguments are tagged, and
199 * need to be processed accordingly.
200 */
201 #define CBPRINTF_PACKAGE_ARGS_ARE_TAGGED BIT(6)
202
203 /**@} */
204
205 /**
206 * @defgroup CBPRINTF_PACKAGE_CONVERT_FLAGS Package convert flags
207 * @{
208 */
209
210 /** @brief Append read-only strings from source package to destination package.
211 *
212 * If package was created with @ref CBPRINTF_PACKAGE_ADD_RO_STR_POS
213 * or @ref CBPRINTF_PACKAGE_ADD_RW_STR_POS it contains arrays of indexes where
214 * string address can be found in the package. When flag is set, read-only strings
215 * are copied into destination package. Address of strings indicated as read-write
216 * are also checked and if determined to be read-only they are also copied.
217 */
218 #define CBPRINTF_PACKAGE_CONVERT_RO_STR BIT(0)
219 /** @deprecated Use @ref CBPRINTF_PACKAGE_CONVERT_RO_STR instead. */
220 #define CBPRINTF_PACKAGE_COPY_RO_STR CBPRINTF_PACKAGE_CONVERT_RO_STR __DEPRECATED_MACRO
221
222 /** @brief Append read-write strings from source package to destination package.
223 *
224 * If package was created with @ref CBPRINTF_PACKAGE_ADD_RW_STR_POS it contains
225 * arrays of indexes where string address can be found in the package. When flag
226 * is set, list of read-write strings is examined and if they are not determined
227 * to be read-only, they are copied into the destination package.
228 * If @ref CBPRINTF_PACKAGE_CONVERT_RO_STR is not set, remaining string locations
229 * are considered as pointing to read-only location and they are copy to the
230 * package if @ref CBPRINTF_PACKAGE_CONVERT_KEEP_RO_STR is set.
231 */
232 #define CBPRINTF_PACKAGE_CONVERT_RW_STR BIT(1)
233 /** @deprecated Use @ref CBPRINTF_PACKAGE_CONVERT_RW_STR instead. */
234 #define CBPRINTF_PACKAGE_COPY_RW_STR CBPRINTF_PACKAGE_CONVERT_RW_STR __DEPRECATED_MACRO
235
236 /** @brief Keep read-only location indexes in the package.
237 *
238 * If it is set read-only string pointers are kept in the package after copy. If
239 * not set they are discarded.
240 */
241 #define CBPRINTF_PACKAGE_CONVERT_KEEP_RO_STR BIT(2)
242 /** @deprecated Use @ref CBPRINTF_PACKAGE_CONVERT_KEEP_RO_STR instead. */
243 #define CBPRINTF_PACKAGE_COPY_KEEP_RO_STR CBPRINTF_PACKAGE_CONVERT_KEEP_RO_STR __DEPRECATED_MACRO
244
245 /** @brief Check format string if %p argument was treated as %s in the package.
246 *
247 * Static packaging is done based only on types of arguments used for a format
248 * string. Without looking into format specifiers present in the string. Because
249 * of that if (unsigned) char pointer is used for %p it will be considered as
250 * a string location and during conversion an attempt to append a string to a
251 * package may be performed. This can lead to misbehavior, in the best case
252 * package will be bigger and in the worst case memory fault or security violation
253 * may occur.
254 *
255 * When this flag is set, format string will be checked to detect cases when
256 * string candidate is a pointer used for %p and string appending from unexpected
257 * location is avoided. Additionally, an log warning is generated to encourage
258 * user to cast such argument to void *. It is recommended because there are
259 * configurations where string is not accessible and inspection cannot be done.
260 * In those cases there are no means to detect such cases.
261 */
262 #define CBPRINTF_PACKAGE_CONVERT_PTR_CHECK BIT(3)
263
264 /**@} */
265
266 /**
267 * @defgroup Z_CBVPRINTF_PROCESS_FLAGS cbvprintf processing flags.
268 * @{
269 */
270
271 /** @brief Indicates the arguments are tagged.
272 *
273 * This tells z_cbvprintf_impl() that the incoming arguments are
274 * tagged, and should be processed accordingly.
275 */
276 #define Z_CBVPRINTF_PROCESS_FLAG_TAGGED_ARGS BIT(0)
277
278 /**@} */
279
280 #include <zephyr/sys/cbprintf_enums.h>
281
282 /** @brief Signature for a cbprintf callback function.
283 *
284 * This function expects two parameters:
285 *
286 * * @p c a character to output. The output behavior should be as if
287 * this was cast to an unsigned char.
288 * * @p ctx a pointer to an object that provides context for the
289 * output operation.
290 *
291 * The declaration does not specify the parameter types. This allows a
292 * function like @c fputc to be used without requiring all context pointers to
293 * be to a @c FILE object.
294 *
295 * @return the value of @p c cast to an unsigned char then back to
296 * int, or a negative error code that will be returned from
297 * cbprintf().
298 */
299 #ifdef __CHECKER__
300 typedef int (*cbprintf_cb)(int c, void *ctx);
301 #else
302 typedef int (*cbprintf_cb)(/* int c, void *ctx */);
303 #endif
304
305 /* Create local cbprintf_cb type to make calng-based compilers happy when handles
306 * OUTC() macro (see below). With strict rules (Wincompatible-function-pointer-types-strict)
307 * it's prohibited to pass arguments with mismatched types.
308 */
309 typedef int (*cbprintf_cb_local)(int c, void *ctx);
310
311 /** @brief Signature for a cbprintf multibyte callback function.
312 *
313 * @param buf data.
314 * @param len data length.
315 * @param ctx a pointer to an object that provides context for the operation.
316 *
317 * return Amount of copied data or negative error code.
318 */
319 typedef int (*cbprintf_convert_cb)(const void *buf, size_t len, void *ctx);
320
321 /** @brief Signature for a external formatter function identical to cbvprintf.
322 *
323 * This function expects the following parameters:
324 *
325 * @param out the function used to emit each generated character.
326 *
327 * @param ctx a pointer to an object that provides context for the
328 * external formatter.
329 *
330 * @param fmt a standard ISO C format string with characters and
331 * conversion specifications.
332 *
333 * @param ap captured stack arguments corresponding to the conversion
334 * specifications found within @p fmt.
335 *
336 * @return vprintf like return values: the number of characters printed,
337 * or a negative error value returned from external formatter.
338 */
339 typedef int (*cbvprintf_external_formatter_func)(cbprintf_cb out, void *ctx,
340 const char *fmt, va_list ap);
341
342 /** @brief Determine if string must be packaged in run time.
343 *
344 * Static packaging can be applied if size of the package can be determined
345 * at compile time. In general, package size can be determined at compile time
346 * if there are no string arguments which might be copied into package body if
347 * they are considered transient.
348 *
349 * @note By default any char pointers are considered to be pointing at transient
350 * strings. This can be narrowed down to non const pointers by using
351 * @ref CBPRINTF_PACKAGE_CONST_CHAR_RO.
352 *
353 * @param ... String with arguments.
354 * @param flags option flags. See @ref CBPRINTF_PACKAGE_FLAGS.
355 *
356 * @retval 1 if string must be packaged in run time.
357 * @retval 0 string can be statically packaged.
358 */
359 #define CBPRINTF_MUST_RUNTIME_PACKAGE(flags, ... /* fmt, ... */) \
360 Z_CBPRINTF_MUST_RUNTIME_PACKAGE(flags, __VA_ARGS__)
361
362 /** @brief Statically package string.
363 *
364 * Build string package from formatted string. It assumes that formatted
365 * string is in the read only memory.
366 *
367 * If _Generic is not supported then runtime packaging is performed.
368 *
369 * @param packaged pointer to where the packaged data can be stored. Pass a null
370 * pointer to skip packaging but still calculate the total space required.
371 * The data stored here is relocatable, that is it can be moved to another
372 * contiguous block of memory. It must be aligned to the size of the longest
373 * argument. It is recommended to use CBPRINTF_PACKAGE_ALIGNMENT for alignment.
374 *
375 * @param inlen set to the number of bytes available at @p packaged. If
376 * @p packaged is NULL the value is ignored.
377 *
378 * @param outlen variable updated to the number of bytes required to completely
379 * store the packed information. If input buffer was too small it is set to
380 * -ENOSPC.
381 *
382 * @param align_offset input buffer alignment offset in bytes. Where offset 0
383 * means that buffer is aligned to CBPRINTF_PACKAGE_ALIGNMENT. Xtensa requires
384 * that @p packaged is aligned to CBPRINTF_PACKAGE_ALIGNMENT so it must be
385 * multiply of CBPRINTF_PACKAGE_ALIGNMENT or 0.
386 *
387 * @param flags option flags. See @ref CBPRINTF_PACKAGE_FLAGS.
388 *
389 * @param ... formatted string with arguments. Format string must be constant.
390 */
391 #define CBPRINTF_STATIC_PACKAGE(packaged, inlen, outlen, align_offset, flags, \
392 ... /* fmt, ... */) \
393 Z_CBPRINTF_STATIC_PACKAGE(packaged, inlen, outlen, \
394 align_offset, flags, __VA_ARGS__)
395
396 /** @brief Capture state required to output formatted data later.
397 *
398 * Like cbprintf() but instead of processing the arguments and emitting the
399 * formatted results immediately all arguments are captured so this can be
400 * done in a different context, e.g. when the output function can block.
401 *
402 * In addition to the values extracted from arguments this will ensure that
403 * copies are made of the necessary portions of any string parameters that are
404 * not confirmed to be stored in read-only memory (hence assumed to be safe to
405 * refer to directly later).
406 *
407 * @param packaged pointer to where the packaged data can be stored. Pass a
408 * null pointer to store nothing but still calculate the total space required.
409 * The data stored here is relocatable, that is it can be moved to another
410 * contiguous block of memory. However, under condition that alignment is
411 * maintained. It must be aligned to at least the size of a pointer.
412 *
413 * @param len this must be set to the number of bytes available at @p packaged
414 * if it is not null. If @p packaged is null then it indicates hypothetical
415 * buffer alignment offset in bytes compared to CBPRINTF_PACKAGE_ALIGNMENT
416 * alignment. Buffer alignment offset impacts returned size of the package.
417 * Xtensa requires that buffer is always aligned to CBPRINTF_PACKAGE_ALIGNMENT
418 * so it must be multiply of CBPRINTF_PACKAGE_ALIGNMENT or 0 when @p packaged is
419 * null.
420 *
421 * @param flags option flags. See @ref CBPRINTF_PACKAGE_FLAGS.
422 *
423 * @param format a standard ISO C format string with characters and conversion
424 * specifications.
425 *
426 * @param ... arguments corresponding to the conversion specifications found
427 * within @p format.
428 *
429 * @retval nonegative the number of bytes successfully stored at @p packaged.
430 * This will not exceed @p len.
431 * @retval -EINVAL if @p format is not acceptable
432 * @retval -EFAULT if @p packaged alignment is not acceptable
433 * @retval -ENOSPC if @p packaged was not null and the space required to store
434 * exceed @p len.
435 */
436 __printf_like(4, 5)
437 int cbprintf_package(void *packaged,
438 size_t len,
439 uint32_t flags,
440 const char *format,
441 ...);
442
443 /** @brief Capture state required to output formatted data later.
444 *
445 * Like cbprintf() but instead of processing the arguments and emitting the
446 * formatted results immediately all arguments are captured so this can be
447 * done in a different context, e.g. when the output function can block.
448 *
449 * In addition to the values extracted from arguments this will ensure that
450 * copies are made of the necessary portions of any string parameters that are
451 * not confirmed to be stored in read-only memory (hence assumed to be safe to
452 * refer to directly later).
453 *
454 * @param packaged pointer to where the packaged data can be stored. Pass a
455 * null pointer to store nothing but still calculate the total space required.
456 * The data stored here is relocatable, that is it can be moved to another
457 * contiguous block of memory. The pointer must be aligned to a multiple of
458 * the largest element in the argument list.
459 *
460 * @param len this must be set to the number of bytes available at @p packaged.
461 * Ignored if @p packaged is NULL.
462 *
463 * @param flags option flags. See @ref CBPRINTF_PACKAGE_FLAGS.
464 *
465 * @param format a standard ISO C format string with characters and conversion
466 * specifications.
467 *
468 * @param ap captured stack arguments corresponding to the conversion
469 * specifications found within @p format.
470 *
471 * @retval nonegative the number of bytes successfully stored at @p packaged.
472 * This will not exceed @p len.
473 * @retval -EINVAL if @p format is not acceptable
474 * @retval -ENOSPC if @p packaged was not null and the space required to store
475 * exceed @p len.
476 */
477 int cbvprintf_package(void *packaged,
478 size_t len,
479 uint32_t flags,
480 const char *format,
481 va_list ap);
482
483 /** @brief Convert a package.
484 *
485 * Converting may include appending strings used in the package to the package body.
486 * If input package was created with @ref CBPRINTF_PACKAGE_ADD_RO_STR_POS or
487 * @ref CBPRINTF_PACKAGE_ADD_RW_STR_POS, it contains information where strings
488 * are located within the package. This information can be used to copy strings
489 * during the conversion.
490 *
491 * @p cb is called with portions of the output package. At the end of the conversion
492 * @p cb is called with null buffer.
493 *
494 * @param in_packaged Input package.
495 *
496 * @param in_len Input package length. If 0 package length will be retrieved
497 * from the @p in_packaged
498 *
499 * @param cb callback called with portions of the converted package. If null only
500 * length of the output package is calculated.
501 *
502 * @param ctx Context provided to the @p cb.
503 *
504 * @param flags Flags. See @ref CBPRINTF_PACKAGE_CONVERT_FLAGS.
505 *
506 * @param[in, out] strl if @p packaged is null, it is a pointer to the array where
507 * @p strl_len first string lengths will is stored. If @p packaged is not null,
508 * it contains lengths of first @p strl_len strings. It can be used to optimize
509 * copying so that string length is calculated only once (at length calculation
510 * phase when @p packaged is null.)
511 *
512 * @param strl_len Number of elements in @p strl array.
513 *
514 * @retval Positive output package size.
515 * @retval -ENOSPC if @p packaged was not null and the space required to store
516 * exceed @p len.
517 */
518 int cbprintf_package_convert(void *in_packaged,
519 size_t in_len,
520 cbprintf_convert_cb cb,
521 void *ctx,
522 uint32_t flags,
523 uint16_t *strl,
524 size_t strl_len);
525
526 /* @internal Context used for package copying. */
527 struct z_cbprintf_buf_desc {
528 void *buf;
529 size_t size;
530 size_t off;
531 };
532
533 /* @internal Function callback used for package copying. */
z_cbprintf_cpy(const void * buf,size_t len,void * ctx)534 static inline int z_cbprintf_cpy(const void *buf, size_t len, void *ctx)
535 {
536 struct z_cbprintf_buf_desc *desc = (struct z_cbprintf_buf_desc *)ctx;
537
538 if ((desc->size - desc->off) < len) {
539 return -ENOSPC;
540 }
541
542 memcpy(&((uint8_t *)desc->buf)[desc->off], buf, len);
543 desc->off += len;
544
545 return len;
546 }
547
548 /** @brief Copy package with optional appending of strings.
549 *
550 * @ref cbprintf_package_convert is used to convert and store converted package
551 * in the new location.
552 *
553 * @param in_packaged Input package.
554 *
555 * @param in_len Input package length. If 0 package length will be retrieved
556 * from the @p in_packaged
557 *
558 * @param[out] packaged Output package. If null only length of the output package
559 * is calculated.
560 *
561 * @param len Available space in the location pointed by @p packaged. Not used when
562 * @p packaged is null.
563 *
564 * @param flags Flags. See @ref CBPRINTF_PACKAGE_CONVERT_FLAGS.
565 *
566 * @param[in, out] strl if @p packaged is null, it is a pointer to the array where
567 * @p strl_len first string lengths will is stored. If @p packaged is not null,
568 * it contains lengths of first @p strl_len strings. It can be used to optimize
569 * copying so that string length is calculated only once (at length calculation
570 * phase when @p packaged is null.)
571 *
572 * @param strl_len Number of elements in @p strl array.
573 *
574 * @retval Positive Output package size.
575 * @retval -ENOSPC if @p packaged was not null and the space required to store
576 * exceed @p len.
577 */
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)578 static inline int cbprintf_package_copy(void *in_packaged,
579 size_t in_len,
580 void *packaged,
581 size_t len,
582 uint32_t flags,
583 uint16_t *strl,
584 size_t strl_len)
585 {
586 struct z_cbprintf_buf_desc buf_desc = {
587 .buf = packaged,
588 .size = len,
589 .off = 0,
590 };
591
592 return cbprintf_package_convert(in_packaged, in_len,
593 packaged ? z_cbprintf_cpy : NULL, &buf_desc,
594 flags, strl, strl_len);
595 }
596
597 /** @brief Convert package to fully self-contained (fsc) package.
598 *
599 * Package may not be self contain since strings by default are stored by address.
600 * Package may be partially self-contained when transient (not read only) strings
601 * are appended to the package. Such package can be decoded only when there is an
602 * access to read-only strings.
603 *
604 * Fully self-contained has (fsc) contains all strings used in the package. A package
605 * can be converted to fsc package if it was create with @ref CBPRINTF_PACKAGE_ADD_RO_STR_POS
606 * flag. Such package will contain necessary data to find read only strings in
607 * the package and copy them into the package body.
608 *
609 * @param in_packaged pointer to original package created with
610 * @ref CBPRINTF_PACKAGE_ADD_RO_STR_POS.
611 *
612 * @param in_len @p in_packaged length.
613 *
614 * @param packaged pointer to location where fully self-contained version of the
615 * input package will be written. Pass a null pointer to calculate space required.
616 *
617 * @param len must be set to the number of bytes available at @p packaged. Not
618 * used if @p packaged is null.
619 *
620 * @retval nonegative the number of bytes successfully stored at @p packaged.
621 * This will not exceed @p len. If @p packaged is null, calculated length.
622 * @retval -ENOSPC if @p packaged was not null and the space required to store
623 * exceed @p len.
624 * @retval -EINVAL if @p in_packaged is null.
625 */
cbprintf_fsc_package(void * in_packaged,size_t in_len,void * packaged,size_t len)626 static inline int cbprintf_fsc_package(void *in_packaged,
627 size_t in_len,
628 void *packaged,
629 size_t len)
630 {
631 return cbprintf_package_copy(in_packaged, in_len, packaged, len,
632 CBPRINTF_PACKAGE_CONVERT_RO_STR |
633 CBPRINTF_PACKAGE_CONVERT_RW_STR, NULL, 0);
634 }
635
636 /** @brief Generate the output for a previously captured format
637 * operation using an external formatter.
638 *
639 * @param out the function used to emit each generated character.
640 *
641 * @param formatter external formatter function.
642 *
643 * @param ctx a pointer to an object that provides context for the
644 * external formatter.
645 *
646 * @param packaged the data required to generate the formatted output, as
647 * captured by cbprintf_package() or cbvprintf_package(). The alignment
648 * requirement on this data is the same as when it was initially created.
649 *
650 * @note Memory indicated by @p packaged will be modified in a non-destructive
651 * way, meaning that it could still be reused with this function again.
652 *
653 * @return printf like return values: the number of characters printed,
654 * or a negative error value returned from external formatter.
655 */
656 int cbpprintf_external(cbprintf_cb out,
657 cbvprintf_external_formatter_func formatter,
658 void *ctx,
659 void *packaged);
660
661 /** @brief *printf-like output through a callback.
662 *
663 * This is essentially printf() except the output is generated
664 * character-by-character using the provided @p out function. This allows
665 * formatting text of unbounded length without incurring the cost of a
666 * temporary buffer.
667 *
668 * All formatting specifiers of C99 are recognized, and most are supported if
669 * the functionality is enabled.
670 *
671 * @note The functionality of this function is significantly reduced
672 * when @kconfig{CONFIG_CBPRINTF_NANO} is selected.
673 *
674 * @param out the function used to emit each generated character.
675 *
676 * @param ctx context provided when invoking out
677 *
678 * @param format a standard ISO C format string with characters and conversion
679 * specifications.
680 *
681 * @param ... arguments corresponding to the conversion specifications found
682 * within @p format.
683 *
684 * @return the number of characters printed, or a negative error value
685 * returned from invoking @p out.
686 */
687 __printf_like(3, 4)
688 int cbprintf(cbprintf_cb out, void *ctx, const char *format, ...);
689
690 /** @brief varargs-aware *printf-like output through a callback.
691 *
692 * This is essentially vsprintf() except the output is generated
693 * character-by-character using the provided @p out function. This allows
694 * formatting text of unbounded length without incurring the cost of a
695 * temporary buffer.
696 *
697 * @note This function is available only when
698 * @kconfig{CONFIG_CBPRINTF_LIBC_SUBSTS} is selected.
699 *
700 * @note The functionality of this function is significantly reduced when
701 * @kconfig{CONFIG_CBPRINTF_NANO} is selected.
702 *
703 * @param out the function used to emit each generated character.
704 *
705 * @param ctx context provided when invoking out
706 *
707 * @param format a standard ISO C format string with characters and conversion
708 * specifications.
709 *
710 * @param ap a reference to the values to be converted.
711 *
712 * @param flags flags on how to process the inputs.
713 * @see Z_CBVPRINTF_PROCESS_FLAGS.
714 *
715 * @return the number of characters generated, or a negative error value
716 * returned from invoking @p out.
717 */
718 int z_cbvprintf_impl(cbprintf_cb out, void *ctx, const char *format,
719 va_list ap, uint32_t flags);
720
721 /** @brief varargs-aware *printf-like output through a callback.
722 *
723 * This is essentially vsprintf() except the output is generated
724 * character-by-character using the provided @p out function. This allows
725 * formatting text of unbounded length without incurring the cost of a
726 * temporary buffer.
727 *
728 * @note This function is available only when
729 * @kconfig{CONFIG_CBPRINTF_LIBC_SUBSTS} is selected.
730 *
731 * @note The functionality of this function is significantly reduced when
732 * @kconfig{CONFIG_CBPRINTF_NANO} is selected.
733 *
734 * @param out the function used to emit each generated character.
735 *
736 * @param ctx context provided when invoking out
737 *
738 * @param format a standard ISO C format string with characters and conversion
739 * specifications.
740 *
741 * @param ap a reference to the values to be converted.
742 *
743 * @return the number of characters generated, or a negative error value
744 * returned from invoking @p out.
745 */
746 #ifdef CONFIG_PICOLIBC
747 int cbvprintf(cbprintf_cb out, void *ctx, const char *format, va_list ap);
748 #else
749 static inline
cbvprintf(cbprintf_cb out,void * ctx,const char * format,va_list ap)750 int cbvprintf(cbprintf_cb out, void *ctx, const char *format, va_list ap)
751 {
752 return z_cbvprintf_impl(out, ctx, format, ap, 0);
753 }
754 #endif
755
756 /** @brief varargs-aware *printf-like output through a callback with tagged arguments.
757 *
758 * This is essentially vsprintf() except the output is generated
759 * character-by-character using the provided @p out function. This allows
760 * formatting text of unbounded length without incurring the cost of a
761 * temporary buffer.
762 *
763 * Note that the argument list @p ap are tagged.
764 *
765 * @note This function is available only when
766 * @kconfig{CONFIG_CBPRINTF_LIBC_SUBSTS} is selected.
767 *
768 * @note The functionality of this function is significantly reduced when
769 * @kconfig{CONFIG_CBPRINTF_NANO} is selected.
770 *
771 * @param out the function used to emit each generated character.
772 *
773 * @param ctx context provided when invoking out
774 *
775 * @param format a standard ISO C format string with characters and conversion
776 * specifications.
777 *
778 * @param ap a reference to the values to be converted.
779 *
780 * @return the number of characters generated, or a negative error value
781 * returned from invoking @p out.
782 */
783 static inline
cbvprintf_tagged_args(cbprintf_cb out,void * ctx,const char * format,va_list ap)784 int cbvprintf_tagged_args(cbprintf_cb out, void *ctx,
785 const char *format, va_list ap)
786 {
787 return z_cbvprintf_impl(out, ctx, format, ap,
788 Z_CBVPRINTF_PROCESS_FLAG_TAGGED_ARGS);
789 }
790
791 /** @brief Generate the output for a previously captured format
792 * operation.
793 *
794 * @param out the function used to emit each generated character.
795 *
796 * @param ctx context provided when invoking out
797 *
798 * @param packaged the data required to generate the formatted output, as
799 * captured by cbprintf_package() or cbvprintf_package(). The alignment
800 * requirement on this data is the same as when it was initially created.
801 *
802 * @note Memory indicated by @p packaged will be modified in a non-destructive
803 * way, meaning that it could still be reused with this function again.
804 *
805 * @return the number of characters printed, or a negative error value
806 * returned from invoking @p out.
807 */
808 static inline
cbpprintf(cbprintf_cb out,void * ctx,void * packaged)809 int cbpprintf(cbprintf_cb out, void *ctx, void *packaged)
810 {
811 #if defined(CONFIG_CBPRINTF_PACKAGE_SUPPORT_TAGGED_ARGUMENTS)
812 union cbprintf_package_hdr *hdr =
813 (union cbprintf_package_hdr *)packaged;
814
815 if ((hdr->desc.pkg_flags & CBPRINTF_PACKAGE_ARGS_ARE_TAGGED)
816 == CBPRINTF_PACKAGE_ARGS_ARE_TAGGED) {
817 return cbpprintf_external(out, cbvprintf_tagged_args,
818 ctx, packaged);
819 }
820 #endif
821
822 return cbpprintf_external(out, cbvprintf, ctx, packaged);
823 }
824
825 #ifdef CONFIG_CBPRINTF_LIBC_SUBSTS
826
827 #ifdef CONFIG_PICOLIBC
828
829 #define fprintfcb(stream, ...) fprintf(stream, __VA_ARGS__)
830 #define vfprintfcb(stream, format, ap) vfprintf(stream, format, ap)
831 #define printfcb(format, ...) printf(format, __VA_ARGS__)
832 #define vprintfcb(format, ap) vprintf(format, ap)
833 #define snprintfcb(str, size, ...) snprintf(str, size, __VA_ARGS__)
834 #define vsnprintfcb(str, size, format, ap) vsnprintf(str, size, format, ap)
835
836 #else
837
838 /** @brief fprintf using Zephyrs cbprintf infrastructure.
839 *
840 * @note This function is available only when
841 * @kconfig{CONFIG_CBPRINTF_LIBC_SUBSTS} is selected.
842 *
843 * @note The functionality of this function is significantly reduced
844 * when @kconfig{CONFIG_CBPRINTF_NANO} is selected.
845 *
846 * @param stream the stream to which the output should be written.
847 *
848 * @param format a standard ISO C format string with characters and
849 * conversion specifications.
850 *
851 * @param ... arguments corresponding to the conversion specifications found
852 * within @p format.
853 *
854 * return The number of characters printed.
855 */
856 __printf_like(2, 3)
857 int fprintfcb(FILE * stream, const char *format, ...);
858
859 /** @brief vfprintf using Zephyrs cbprintf infrastructure.
860 *
861 * @note This function is available only when
862 * @kconfig{CONFIG_CBPRINTF_LIBC_SUBSTS} is selected.
863 *
864 * @note The functionality of this function is significantly reduced when
865 * @kconfig{CONFIG_CBPRINTF_NANO} is selected.
866 *
867 * @param stream the stream to which the output should be written.
868 *
869 * @param format a standard ISO C format string with characters and conversion
870 * specifications.
871 *
872 * @param ap a reference to the values to be converted.
873 *
874 * @return The number of characters printed.
875 */
876 int vfprintfcb(FILE *stream, const char *format, va_list ap);
877
878 /** @brief printf using Zephyrs cbprintf infrastructure.
879 *
880 * @note This function is available only when
881 * @kconfig{CONFIG_CBPRINTF_LIBC_SUBSTS} is selected.
882 *
883 * @note The functionality of this function is significantly reduced
884 * when @kconfig{CONFIG_CBPRINTF_NANO} is selected.
885 *
886 * @param format a standard ISO C format string with characters and
887 * conversion specifications.
888 *
889 * @param ... arguments corresponding to the conversion specifications found
890 * within @p format.
891 *
892 * @return The number of characters printed.
893 */
894 __printf_like(1, 2)
895 int printfcb(const char *format, ...);
896
897 /** @brief vprintf using Zephyrs cbprintf infrastructure.
898 *
899 * @note This function is available only when
900 * @kconfig{CONFIG_CBPRINTF_LIBC_SUBSTS} is selected.
901 *
902 * @note The functionality of this function is significantly reduced when
903 * @kconfig{CONFIG_CBPRINTF_NANO} is selected.
904 *
905 * @param format a standard ISO C format string with characters and conversion
906 * specifications.
907 *
908 * @param ap a reference to the values to be converted.
909 *
910 * @return The number of characters printed.
911 */
912 int vprintfcb(const char *format, va_list ap);
913
914 /** @brief snprintf using Zephyrs cbprintf infrastructure.
915 *
916 * @note This function is available only when
917 * @kconfig{CONFIG_CBPRINTF_LIBC_SUBSTS} is selected.
918 *
919 * @note The functionality of this function is significantly reduced
920 * when @kconfig{CONFIG_CBPRINTF_NANO} is selected.
921 *
922 * @param str where the formatted content should be written
923 *
924 * @param size maximum number of chaacters for the formatted output,
925 * including the terminating null byte.
926 *
927 * @param format a standard ISO C format string with characters and
928 * conversion specifications.
929 *
930 * @param ... arguments corresponding to the conversion specifications found
931 * within @p format.
932 *
933 * @return The number of characters that would have been written to @p
934 * str, excluding the terminating null byte. This is greater than the
935 * number actually written if @p size is too small.
936 */
937 __printf_like(3, 4)
938 int snprintfcb(char *str, size_t size, const char *format, ...);
939
940 /** @brief vsnprintf using Zephyrs cbprintf infrastructure.
941 *
942 * @note This function is available only when
943 * @kconfig{CONFIG_CBPRINTF_LIBC_SUBSTS} is selected.
944 *
945 * @note The functionality of this function is significantly reduced when
946 * @kconfig{CONFIG_CBPRINTF_NANO} is selected.
947 *
948 * @param str where the formatted content should be written
949 *
950 * @param size maximum number of chaacters for the formatted output, including
951 * the terminating null byte.
952 *
953 * @param format a standard ISO C format string with characters and conversion
954 * specifications.
955 *
956 * @param ap a reference to the values to be converted.
957 *
958 * @return The number of characters that would have been written to @p
959 * str, excluding the terminating null byte. This is greater than the
960 * number actually written if @p size is too small.
961 */
962 int vsnprintfcb(char *str, size_t size, const char *format, va_list ap);
963
964 #endif /* CONFIG_PICOLIBC */
965 #endif /* CONFIG_CBPRINTF_LIBC_SUBSTS */
966
967 /**
968 * @}
969 */
970
971 #ifdef __cplusplus
972 }
973 #endif
974
975 #endif /* ZEPHYR_INCLUDE_SYS_CBPRINTF_H_ */
976