From ae1d470b4b993418ab2a50f03a47b35602e3fc0f Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Sun, 14 May 2023 21:13:35 +0100 Subject: [PATCH] Create fatal component to handle fatal errors --- components/fatal/CMakeLists.txt | 5 +++++ components/fatal/fatal.c | 17 +++++++++++++++++ components/fatal/fatal.h | 21 +++++++++++++++++++++ main/CMakeLists.txt | 6 +++++- main/main.c | 3 +++ 5 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 components/fatal/CMakeLists.txt create mode 100644 components/fatal/fatal.c create mode 100644 components/fatal/fatal.h diff --git a/components/fatal/CMakeLists.txt b/components/fatal/CMakeLists.txt new file mode 100644 index 0000000..20f84eb --- /dev/null +++ b/components/fatal/CMakeLists.txt @@ -0,0 +1,5 @@ +idf_component_register( + SRCS "fatal.c" + INCLUDE_DIRS "." + REQUIRES esp_system log +) diff --git a/components/fatal/fatal.c b/components/fatal/fatal.c new file mode 100644 index 0000000..7864123 --- /dev/null +++ b/components/fatal/fatal.c @@ -0,0 +1,17 @@ +/* + * SPDX-License-Identifier: AGPL-3.0-only + * Copyright (c) Camden Dixie O'Brien + */ + +#include "fatal.h" + +#include "esp_log.h" +#include "esp_system.h" + +#define TAG "Fatal" + +void _fatal(const char *func, const char *file, unsigned line) +{ + ESP_LOGE(TAG, "%s() @ %s:%u", func, file, line); + while (1) { } +} diff --git a/components/fatal/fatal.h b/components/fatal/fatal.h new file mode 100644 index 0000000..50a33d5 --- /dev/null +++ b/components/fatal/fatal.h @@ -0,0 +1,21 @@ +/* + * SPDX-License-Identifier: AGPL-3.0-only + * Copyright (c) Camden Dixie O'Brien + * + * Fatal error module. + * + * This small module provides the FATAL() macro, intended to be used + * to signal a fatal error. This prompts a system restart. + */ + +#ifndef FATAL_H +#define FATAL_H + +/** + * Signals a fatal error. + */ +#define FATAL() _fatal(__func__, __FILE__, __LINE__) + +void _fatal(const char *func, const char *file, unsigned line); + +#endif diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 34613e8..bef49b1 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1 +1,5 @@ -idf_component_register(SRCS "main.c") +idf_component_register( + SRCS "main.c" + INCLUDE_DIRS "." + REQUIRES fatal +) diff --git a/main/main.c b/main/main.c index 4ae5922..27909ff 100644 --- a/main/main.c +++ b/main/main.c @@ -3,6 +3,9 @@ * Copyright (c) Camden Dixie O'Brien */ +#include "fatal.h" + void app_main(void) { + FATAL(); }