1 /*
2  * Copyright (c) 2021 Espressif Systems (Shanghai) Co., Ltd.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/logging/log.h>
9 #include "string.h"
10 #include "soc/efuse_reg.h"
11 #include "esp_log.h"
12 #include "esp_system.h"
13 #include "esp_private/system_internal.h"
14 #include "soc/cpu.h"
15 #include "soc/rtc.h"
16 #include "soc/rtc_cntl_reg.h"
17 #include "esp_rom_uart.h"
18 
19 static unsigned int lock_key;
20 
21 static const char *TAG = "system_api";
22 
23 #define SHUTDOWN_HANDLERS_NO 4
24 static shutdown_handler_t shutdown_handlers[SHUTDOWN_HANDLERS_NO];
25 
esp_register_shutdown_handler(shutdown_handler_t handler)26 esp_err_t esp_register_shutdown_handler(shutdown_handler_t handler)
27 {
28 	for (int i = 0; i < SHUTDOWN_HANDLERS_NO; i++) {
29 		if (shutdown_handlers[i] == handler) {
30 			return ESP_ERR_INVALID_STATE;
31 		} else if (shutdown_handlers[i] == NULL) {
32 			shutdown_handlers[i] = handler;
33 			return ESP_OK;
34 		}
35 	}
36 	return ESP_ERR_NO_MEM;
37 }
38 
esp_unregister_shutdown_handler(shutdown_handler_t handler)39 esp_err_t esp_unregister_shutdown_handler(shutdown_handler_t handler)
40 {
41 	for (int i = 0; i < SHUTDOWN_HANDLERS_NO; i++) {
42 		if (shutdown_handlers[i] == handler) {
43 			shutdown_handlers[i] = NULL;
44 			return ESP_OK;
45 		}
46 	}
47 	return ESP_ERR_INVALID_STATE;
48 }
49