Sylvain Munaut | 26bc465 | 2020-09-14 10:19:49 +0200 | [diff] [blame] | 1 | /* |
| 2 | * utils.c |
| 3 | * |
| 4 | * Copyright (C) 2019-2020 Sylvain Munaut <tnt@246tNt.com> |
| 5 | * SPDX-License-Identifier: LGPL-3.0-or-later |
| 6 | */ |
| 7 | |
Sylvain Munaut | 7cfe017 | 2022-01-09 16:52:46 +0100 | [diff] [blame] | 8 | #include <stdarg.h> |
Sylvain Munaut | 26bc465 | 2020-09-14 10:19:49 +0200 | [diff] [blame] | 9 | #include <stdint.h> |
| 10 | #include <stdbool.h> |
| 11 | |
Sylvain Munaut | 7cfe017 | 2022-01-09 16:52:46 +0100 | [diff] [blame] | 12 | #include "console.h" |
| 13 | #include "led.h" |
| 14 | #include "mini-printf.h" |
| 15 | #include "misc.h" |
| 16 | |
Sylvain Munaut | 26bc465 | 2020-09-14 10:19:49 +0200 | [diff] [blame] | 17 | char * |
| 18 | hexstr(void *d, int n, bool space) |
| 19 | { |
| 20 | static const char * const hex = "0123456789abcdef"; |
| 21 | static char buf[96]; |
| 22 | uint8_t *p = d; |
| 23 | char *s = buf; |
| 24 | char c; |
| 25 | |
| 26 | while (n--) { |
| 27 | c = *p++; |
| 28 | *s++ = hex[c >> 4]; |
| 29 | *s++ = hex[c & 0xf]; |
| 30 | if (space) |
| 31 | *s++ = ' '; |
| 32 | } |
| 33 | |
| 34 | s[space?-1:0] = '\0'; |
| 35 | |
| 36 | return buf; |
| 37 | } |
Sylvain Munaut | 7cfe017 | 2022-01-09 16:52:46 +0100 | [diff] [blame] | 38 | |
| 39 | |
| 40 | void |
| 41 | _panic(const char *file, int lineno, const char *fmt, ...) |
| 42 | { |
| 43 | char buf[256]; |
| 44 | va_list va; |
| 45 | int l; |
| 46 | |
| 47 | /* Fast hard red blinking led */ |
| 48 | led_state(true); |
| 49 | led_color(255, 0, 8); |
| 50 | led_breathe(false, 0, 0); |
| 51 | led_blink(true, 75, 75); |
| 52 | |
| 53 | /* Prepare buffer */ |
| 54 | l = mini_snprintf(buf, 255, "PANIC @ %s:%d = ", file, lineno); |
| 55 | |
| 56 | va_start(va, fmt); |
| 57 | l += mini_vsnprintf(buf+l, 255-l, fmt, va); |
| 58 | va_end(va); |
| 59 | |
| 60 | buf[l] = '\n'; |
| 61 | buf[l+1] = '\0'; |
| 62 | |
| 63 | /* Print once */ |
| 64 | puts(buf); |
| 65 | |
| 66 | /* Loop waiting for commands */ |
| 67 | while (1) { |
| 68 | int cmd = getchar_nowait(); |
| 69 | |
| 70 | switch (cmd) { |
| 71 | case 'b': |
| 72 | /* Reboot */ |
| 73 | reboot(2); |
| 74 | break; |
| 75 | case ' ': |
| 76 | /* Print error again */ |
| 77 | puts(buf); |
| 78 | break; |
| 79 | } |
| 80 | } |
| 81 | } |