|
| 1 | +/* |
| 2 | + * Copyright (c) 2015, Intel Corporation. All rights reserved. |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions are met: |
| 6 | + * |
| 7 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 8 | + * this list of conditions and the following disclaimer. |
| 9 | + * |
| 10 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer in the documentation |
| 12 | + * and/or other materials provided with the distribution. |
| 13 | + * |
| 14 | + * 3. Neither the name of the copyright holder nor the names of its contributors |
| 15 | + * may be used to endorse or promote products derived from this software without |
| 16 | + * specific prior written permission. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 22 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | + * POSSIBILITY OF SUCH DAMAGE. |
| 29 | + */ |
| 30 | + |
| 31 | +#include <stdarg.h> |
| 32 | +#include "UARTClass.h" |
| 33 | + |
| 34 | +extern "C" void printk(const char *fmt, va_list args); |
| 35 | +int __vsnprintf(char *dest, int size, const char *fmt, va_list args) |
| 36 | +{ |
| 37 | + int might_format = 0; |
| 38 | + int len = 0; |
| 39 | + char lastchar = 0; |
| 40 | + bool binary_format = false; |
| 41 | + |
| 42 | + if (!dest || !size) |
| 43 | + return 0; |
| 44 | + |
| 45 | + while (*fmt && len < size) { |
| 46 | + if (!might_format) { |
| 47 | + if (*fmt == '\n' && lastchar != '\r') { |
| 48 | + if (len < size) { |
| 49 | + lastchar = *dest++ = '\r', len++; |
| 50 | + continue; |
| 51 | + } else |
| 52 | + break; |
| 53 | + } else if (*fmt != '%') { |
| 54 | + if (len < size) |
| 55 | + lastchar = *dest++ = *fmt, len++; |
| 56 | + else |
| 57 | + break; |
| 58 | + } else |
| 59 | + might_format = 1; |
| 60 | + } else { |
| 61 | + if (*fmt == '%') { |
| 62 | + if (len < size) |
| 63 | + *dest++ = '%', len++; |
| 64 | + else |
| 65 | + break; |
| 66 | + might_format = 0; |
| 67 | + } else { |
| 68 | + switch (*fmt) { |
| 69 | + case '0': |
| 70 | + case '1': |
| 71 | + might_format |= 2; |
| 72 | + goto still_format; |
| 73 | + break; |
| 74 | + case '2': |
| 75 | + might_format |= 4; |
| 76 | + goto still_format; |
| 77 | + break; |
| 78 | + case '4': |
| 79 | + might_format |= 8; |
| 80 | + goto still_format; |
| 81 | + break; |
| 82 | + case 'b': |
| 83 | + binary_format = true; |
| 84 | + goto still_format; |
| 85 | + break; |
| 86 | + case 'd': |
| 87 | + case 'i': |
| 88 | + case 'u': |
| 89 | + if (!binary_format) { |
| 90 | + unsigned long num = |
| 91 | + va_arg(args, |
| 92 | + unsigned long); |
| 93 | + unsigned long pos = 999999999; |
| 94 | + unsigned long remainder = num; |
| 95 | + int found_largest_digit = 0; |
| 96 | + |
| 97 | + if (*fmt != 'u' |
| 98 | + && (num & (1 << 31))) { |
| 99 | + if (len < size) |
| 100 | + *dest++ = |
| 101 | + '-', |
| 102 | + len++; |
| 103 | + num = (~num) + 1; |
| 104 | + remainder = num; |
| 105 | + } |
| 106 | + while (pos >= 9) { |
| 107 | + if (found_largest_digit |
| 108 | + || remainder > |
| 109 | + pos) { |
| 110 | + found_largest_digit |
| 111 | + = 1; |
| 112 | + if (len < size) |
| 113 | + *dest++ |
| 114 | + = |
| 115 | + ( |
| 116 | + char) |
| 117 | + (( |
| 118 | + remainder |
| 119 | + / ( |
| 120 | + pos |
| 121 | + + |
| 122 | + 1)) |
| 123 | + + |
| 124 | + 48), |
| 125 | + len++; |
| 126 | + else |
| 127 | + break; |
| 128 | + } |
| 129 | + remainder %= (pos + 1); |
| 130 | + pos /= 10; |
| 131 | + } |
| 132 | + if (len < size) |
| 133 | + *dest++ = |
| 134 | + (char)( |
| 135 | + remainder |
| 136 | + + |
| 137 | + 48), |
| 138 | + len++; |
| 139 | + break; |
| 140 | + } |
| 141 | + case 'x': |
| 142 | + case 'X': |
| 143 | + case 'p': { |
| 144 | + unsigned long num = |
| 145 | + va_arg(args, unsigned long); |
| 146 | + int sz = sizeof(num) * 2; |
| 147 | + |
| 148 | + if (might_format & 8) { |
| 149 | + sz = 4; |
| 150 | + } else if (might_format & 4) { |
| 151 | + sz = 2; |
| 152 | + } else if (might_format & 2) { |
| 153 | + sz = 1; |
| 154 | + } |
| 155 | + for (; sz; sz--) { |
| 156 | + char nibble; |
| 157 | + if (!binary_format) { |
| 158 | + nibble = |
| 159 | + (num >> |
| 160 | + ((sz - |
| 161 | + 1) << |
| 162 | + 2) & 0xf); |
| 163 | + nibble += |
| 164 | + nibble > |
| 165 | + 9 ? 87 : 48; |
| 166 | + } else { |
| 167 | + nibble = |
| 168 | + (num >> |
| 169 | + ((sz - |
| 170 | + 1) << |
| 171 | + 3) & 0xff); |
| 172 | + } |
| 173 | + if (len < size) |
| 174 | + *dest++ = |
| 175 | + nibble, |
| 176 | + len++; |
| 177 | + else |
| 178 | + break; |
| 179 | + } |
| 180 | + break; |
| 181 | + } |
| 182 | + case 's': { |
| 183 | + char *s = va_arg(args, char *); |
| 184 | + while (*s) |
| 185 | + if (len < size) |
| 186 | + *dest++ = |
| 187 | + *s++, len++; |
| 188 | + else |
| 189 | + break; |
| 190 | + break; |
| 191 | + } |
| 192 | + case 'c': { |
| 193 | + char c = va_arg(args, int); |
| 194 | + if (len < size) |
| 195 | + *dest++ = c, len++; |
| 196 | + break; |
| 197 | + } |
| 198 | + default: |
| 199 | + if (len < size) |
| 200 | + *dest++ = '%', len++; |
| 201 | + if (len < size) |
| 202 | + *dest++ = *fmt, len++; |
| 203 | + break; |
| 204 | + } |
| 205 | + might_format = 0; |
| 206 | +still_format: |
| 207 | + (void)might_format; |
| 208 | + } |
| 209 | + } |
| 210 | + ++fmt; |
| 211 | + } |
| 212 | + *dest = '\0'; |
| 213 | + return len; |
| 214 | +} |
| 215 | + |
| 216 | +extern UARTClass Serial1; |
| 217 | +#define PRINTK_BUFSIZ 256 |
| 218 | +void printk(const char *fmt, va_list args) |
| 219 | +{ |
| 220 | +#ifdef CONFIGURE_DEBUG_CORELIB_ENABLED |
| 221 | + int len = 0; |
| 222 | + |
| 223 | + char tmp[PRINTK_BUFSIZ]; |
| 224 | + |
| 225 | + len = __vsnprintf(tmp, PRINTK_BUFSIZ, fmt, args); |
| 226 | + |
| 227 | + tmp[len] = '\0'; |
| 228 | + Serial1.println(tmp); |
| 229 | +#endif |
| 230 | +} |
0 commit comments