blob: 7c7d3d0d44d9205995195ef38b6791fb6e908d4c [file] [log] [blame]
Sylvain Munaut26bc4652020-09-14 10:19:49 +02001/*
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 Munaut7cfe0172022-01-09 16:52:46 +01008#include <stdarg.h>
Sylvain Munaut26bc4652020-09-14 10:19:49 +02009#include <stdint.h>
10#include <stdbool.h>
11
Sylvain Munaut7cfe0172022-01-09 16:52:46 +010012#include "console.h"
13#include "led.h"
14#include "mini-printf.h"
15#include "misc.h"
16
Sylvain Munaut26bc4652020-09-14 10:19:49 +020017char *
18hexstr(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 Munaut7cfe0172022-01-09 16:52:46 +010038
Sylvain Munaut7bed9032022-01-12 11:49:00 +010039uint8_t
40hexval(char c)
41{
42 if (c >= '0' && c <= '9')
43 return c - '0';
44 else if (c >= 'a' && c <= 'f')
45 return 10 + (c - 'a');
46 else if (c >= 'A' && c <= 'F')
47 return 10 + (c - 'A');
48 else
49 return 0;
50}
51
Sylvain Munaut7cfe0172022-01-09 16:52:46 +010052
53void
54_panic(const char *file, int lineno, const char *fmt, ...)
55{
56 char buf[256];
57 va_list va;
58 int l;
59
60 /* Fast hard red blinking led */
61 led_state(true);
62 led_color(255, 0, 8);
63 led_breathe(false, 0, 0);
64 led_blink(true, 75, 75);
65
66 /* Prepare buffer */
67 l = mini_snprintf(buf, 255, "PANIC @ %s:%d = ", file, lineno);
68
69 va_start(va, fmt);
70 l += mini_vsnprintf(buf+l, 255-l, fmt, va);
71 va_end(va);
72
73 buf[l] = '\n';
74 buf[l+1] = '\0';
75
76 /* Print once */
77 puts(buf);
78
79 /* Loop waiting for commands */
80 while (1) {
81 int cmd = getchar_nowait();
82
83 switch (cmd) {
84 case 'b':
85 /* Reboot */
86 reboot(2);
87 break;
88 case ' ':
89 /* Print error again */
90 puts(buf);
91 break;
92 }
93 }
94}