diff --git a/components/esp32/system_api.c b/components/esp32/system_api.c index 93763e523..7edb8a699 100644 --- a/components/esp32/system_api.c +++ b/components/esp32/system_api.c @@ -211,14 +211,15 @@ esp_err_t esp_read_mac(uint8_t* mac, esp_mac_type_t type) esp_err_t esp_register_shutdown_handler(shutdown_handler_t handler) { - int i; - for (i = 0; i < SHUTDOWN_HANDLERS_NO; i++) { - if (shutdown_handlers[i] == NULL) { - shutdown_handlers[i] = handler; - return ESP_OK; - } - } - return ESP_FAIL; + for (int i = 0; i < SHUTDOWN_HANDLERS_NO; i++) { + if (shutdown_handlers[i] == handler) { + return ESP_ERR_INVALID_STATE; + } else if (shutdown_handlers[i] == NULL) { + shutdown_handlers[i] = handler; + return ESP_OK; + } + } + return ESP_ERR_NO_MEM; } esp_err_t esp_unregister_shutdown_handler(shutdown_handler_t handler) diff --git a/components/esp_common/include/esp_system.h b/components/esp_common/include/esp_system.h index 5de5e5a9a..f73165a1e 100644 --- a/components/esp_common/include/esp_system.h +++ b/components/esp_common/include/esp_system.h @@ -80,6 +80,11 @@ typedef void (*shutdown_handler_t)(void); * * This function allows you to register a handler that gets invoked before * the application is restarted using esp_restart function. + * @param handle function to execute on restart + * @return + * - ESP_OK on success + * - ESP_ERR_INVALID_STATE if the handler has already been registered + * - ESP_ERR_NO_MEM if no more shutdown handler slots are available */ esp_err_t esp_register_shutdown_handler(shutdown_handler_t handle);