|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#ifndef ZEPHYR_INCLUDE_SYS_CBPRINTF_INTERNAL_H_ |
| 8 | +#define ZEPHYR_INCLUDE_SYS_CBPRINTF_INTERNAL_H_ |
| 9 | + |
| 10 | +#include <stdarg.h> |
| 11 | +#include <stddef.h> |
| 12 | +#include <stdint.h> |
| 13 | +#include <toolchain.h> |
| 14 | +#include <sys/util.h> |
| 15 | + |
| 16 | +#ifdef __cplusplus |
| 17 | +extern "C" { |
| 18 | +#endif |
| 19 | + |
| 20 | +/** @brief Return 1 if argument is a pointer to char or wchar_t |
| 21 | + * |
| 22 | + * @param x argument. |
| 23 | + * |
| 24 | + * @return 1 if char * or wchar_t *, 0 otherwise. |
| 25 | + */ |
| 26 | +#define Z_CBPRINTF_IS_PCHAR(x) _Generic((x), \ |
| 27 | + char *: 1, \ |
| 28 | + const char *: 1, \ |
| 29 | + volatile char *: 1, \ |
| 30 | + const volatile char *: 1, \ |
| 31 | + wchar_t *: 1, \ |
| 32 | + const wchar_t *: 1, \ |
| 33 | + volatile wchar_t *: 1, \ |
| 34 | + const volatile wchar_t *: 1, \ |
| 35 | + default: 0) |
| 36 | + |
| 37 | + |
| 38 | +/** @brief Calculate number of char * or wchar_t * arguments in the arguments. |
| 39 | + * |
| 40 | + * @param fmt string. |
| 41 | + * |
| 42 | + * @param ... string arguments. |
| 43 | + * |
| 44 | + * @return number of arguments which are char * or wchar_t *. |
| 45 | + */ |
| 46 | +#define Z_CBPRINTF_HAS_PCHAR_ARGS_(fmt, ...) \ |
| 47 | + (FAST_FOR_EACH(Z_CBPRINTF_IS_PCHAR, (+), __VA_ARGS__)) |
| 48 | + |
| 49 | +/** |
| 50 | + * @brief Check if formatted string must be packaged in runtime. |
| 51 | + * |
| 52 | + * @param skip number of char/wchar_t pointers in the argument list which are |
| 53 | + * accepted for static packaging. |
| 54 | + * |
| 55 | + * @param ... String with arguments (fmt, ...). |
| 56 | + * |
| 57 | + * @retval 1 if string must be packaged at runtime. |
| 58 | + * @retval 0 if string can be statically packaged. |
| 59 | + */ |
| 60 | +#define Z_CBPRINTF_MUST_RUNTIME_PACKAGE(skip, ...) \ |
| 61 | + COND_CODE_0(NUM_VA_ARGS_LESS_1(__VA_ARGS__), \ |
| 62 | + (0), \ |
| 63 | + ((Z_CBPRINTF_HAS_PCHAR_ARGS_(__VA_ARGS__) - skip) > 0 ?\ |
| 64 | + 1 : 0)) |
| 65 | + |
| 66 | +/** @brief Get storage size for given argument. |
| 67 | + * |
| 68 | + * Floats are promoted to double so they use size of double, others int storage |
| 69 | + * or it's own storage size if it is bigger than int. Strings are stored in |
| 70 | + * the package with 1 byte header indicating if string is stored as pointer or |
| 71 | + * by value. |
| 72 | + * |
| 73 | + * @param x argument. |
| 74 | + * |
| 75 | + * @return Number of bytes used for storing the argument. |
| 76 | + */ |
| 77 | +#define Z_CBPRINTF_ARG_SIZE(v) \ |
| 78 | + _Generic((v), void *:sizeof(void *), \ |
| 79 | + float: sizeof(double), \ |
| 80 | + default: (MAX(sizeof(int), sizeof((v)+0))) + \ |
| 81 | + _Generic((v) + 0, char *: 1, default: 0)) |
| 82 | + |
| 83 | +/** @brief Get storage size in words. |
| 84 | + * |
| 85 | + * @param x argument. |
| 86 | + * |
| 87 | + * @return number of words needed for storage. |
| 88 | + */ |
| 89 | +#define Z_CBPRINTF_ARG_WSIZE(x) (Z_CBPRINTF_ARG_SIZE(x) / sizeof(int)) |
| 90 | + |
| 91 | +/** @brief Macro for packaging single argument. |
| 92 | + * |
| 93 | + * Macro handles special cases: |
| 94 | + * - promotions of arguments smaller than int |
| 95 | + * - promotion of float |
| 96 | + * - char * packaging which is prefixed with null byte. |
| 97 | + * - special treatment of void * which would generate warning on arithmetic |
| 98 | + * operation ((void *)ptr + 0). |
| 99 | + */ |
| 100 | +#define Z_CBPRINTF_PACK(__buf, x, _arg_wsize) do {\ |
| 101 | + uint8_t *_buf = __buf; \ |
| 102 | + *_buf = 0; \ |
| 103 | + _buf += _Generic((x), \ |
| 104 | + void *: 0, \ |
| 105 | + default: _Generic((x)+0, \ |
| 106 | + char *:1, \ |
| 107 | + volatile char *: 1, \ |
| 108 | + default: 0)); \ |
| 109 | + double _d = _Generic((x), \ |
| 110 | + float: (x), \ |
| 111 | + default: 0.0); \ |
| 112 | + (void)_d; \ |
| 113 | + int _i = _Generic((x), \ |
| 114 | + short: (x), \ |
| 115 | + default: 0); \ |
| 116 | + (void)_i; \ |
| 117 | + void *_v = _Generic((x), \ |
| 118 | + void *: (x), \ |
| 119 | + default: NULL); \ |
| 120 | + (void)_v; \ |
| 121 | + __auto_type _a = _Generic((x), \ |
| 122 | + void *: NULL, \ |
| 123 | + default: (x) + 0); \ |
| 124 | + z_cbprintf_wcpy(_buf, \ |
| 125 | + (void *)_Generic((x), \ |
| 126 | + float: &_d, \ |
| 127 | + short: &_i, \ |
| 128 | + void *: &_v, \ |
| 129 | + default: &_a), \ |
| 130 | + _arg_wsize ); \ |
| 131 | +} while (0) |
| 132 | + |
| 133 | +/** @brief Safely package arguments to a buffer. |
| 134 | + * |
| 135 | + * Argument is put into the buffer if capable buffer is provided. Length is |
| 136 | + * incremented even if data is not packaged. |
| 137 | + * |
| 138 | + * @param _buf buffer. |
| 139 | + * |
| 140 | + * @param _idx index. Index is postincremented. |
| 141 | + * |
| 142 | + * @param _max maximum index (buffer capacity). |
| 143 | + * |
| 144 | + * @param _arg argument. |
| 145 | + */ |
| 146 | +#define Z_CBPRINTF_PACK_ARG(_buf, _idx, _max, _arg) \ |
| 147 | + do { \ |
| 148 | + uint32_t _arg_size = Z_CBPRINTF_ARG_SIZE(_arg); \ |
| 149 | + uint32_t _arg_wsize = _arg_size / sizeof(uint32_t); \ |
| 150 | + if (_buf && _idx < _max) { \ |
| 151 | + Z_CBPRINTF_PACK(&_buf[_idx], _arg, _arg_wsize); \ |
| 152 | + } \ |
| 153 | + _idx += _arg_size; \ |
| 154 | + } while (0) |
| 155 | + |
| 156 | +/** @brief Package single argument. |
| 157 | + * |
| 158 | + * Macro is called in a loop for each argument in the string. |
| 159 | + * |
| 160 | + * @param arg argument. |
| 161 | + */ |
| 162 | +#define Z_CBPRINTF_LOOP_PACK_ARG(arg) \ |
| 163 | + do { \ |
| 164 | + Z_CBPRINTF_PACK_ARG(__package_buf, __package_len, \ |
| 165 | + __package_max, arg); \ |
| 166 | + } while (0) |
| 167 | + |
| 168 | +/** @brief Statically package a formatted string with arguments. |
| 169 | + * |
| 170 | + * @param buf buffer. If null then only length is calculated. |
| 171 | + * |
| 172 | + * @param len buffer capacity on input, package size on output. |
| 173 | + * |
| 174 | + * @param fmt_as_ptr Flag indicating that format string is stored as void |
| 175 | + * pointer. |
| 176 | + * |
| 177 | + * @param ... String with variable list of arguments. |
| 178 | + */ |
| 179 | +#define Z_CBPRINTF_STATIC_PACKAGE(buf, len, fmt_as_ptr, ... /* fmt, ... */) \ |
| 180 | + do { \ |
| 181 | + uint8_t *__package_buf = buf; \ |
| 182 | + size_t __package_max = (buf != NULL) ? len : SIZE_MAX; \ |
| 183 | + size_t __package_len = 0; \ |
| 184 | + FAST_FOR_EACH(Z_CBPRINTF_LOOP_PACK_ARG, (;), \ |
| 185 | + IF_ENABLED(fmt_as_ptr, ((uint8_t *)))__VA_ARGS__); \ |
| 186 | + len = __package_len; \ |
| 187 | + } while (0) |
| 188 | + |
| 189 | +#define Z_CBPRINTF_FMT_SIZE(fmt_as_ptr) (sizeof(void *) + (fmt_as_ptr ? 0 : 1)) |
| 190 | + |
| 191 | +/** @brief Calculate package size. 0 is retuned if only null pointer is given.*/ |
| 192 | +#define Z_CBPRINTF_STATIC_PACKAGE_SIZE(fmt_as_ptr, ...) \ |
| 193 | + COND_CODE_0(NUM_VA_ARGS_LESS_1(__VA_ARGS__), \ |
| 194 | + (FAST_GET_ARG_N(1, __VA_ARGS__) == NULL ? \ |
| 195 | + 0 : Z_CBPRINTF_FMT_SIZE(fmt_as_ptr)), \ |
| 196 | + (Z_CBPRINTF_FMT_SIZE(fmt_as_ptr) + \ |
| 197 | + FAST_FOR_EACH(Z_CBPRINTF_ARG_SIZE, \ |
| 198 | + (+), FAST_GET_ARGS_LESS_N(1,__VA_ARGS__)))) |
| 199 | + |
| 200 | +static inline void z_cbprintf_wcpy(void *dst, void *src, uint32_t wlen) |
| 201 | +{ |
| 202 | + uint32_t *dst32 = dst; |
| 203 | + uint32_t *src32 = src; |
| 204 | + |
| 205 | + for (uint32_t i = 0; i < wlen; i++) { |
| 206 | + dst32[i] = src32[i]; |
| 207 | + } |
| 208 | +} |
| 209 | + |
| 210 | +#ifdef __cplusplus |
| 211 | +} |
| 212 | +#endif |
| 213 | + |
| 214 | + |
| 215 | +#endif /* ZEPHYR_INCLUDE_SYS_CBPRINTF_INTERNAL_H_ */ |
0 commit comments