/**
 * @file system_init_exit.h
 *
 * @brief This file contains prototypes for system initialization and shutdown.
 * 
 *  @copyright Copyright 2023 Antaris, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SYS_INIT_EXIT_H
#define SYS_INIT_EXIT_H

#include "stdint.h"
#include "exo_io_al_sos_timer.h"
#ifndef LINUX_TEMP_PORT
#include "main.h"
#endif

#define EARLY_WDT_CATCH_TIME 9000     ///< Early watchdog timer periodicity.

#define MAX_RBT_RSN_ARR 10
#define MAX_RST_RSN_IDX 20

/**
 * @brief Reset reason storage unit
 */
typedef struct
{
	uint32_t reset_reason;
	time_t get_time;

}__attribute__ ((__packed__))rst_reason;

/**
 * @brief Reboot reason storage unit
 */
typedef struct
{
	uint32_t rbt_src;
	time_t rbt_tm;

}__attribute__ ((__packed__))reboot_reason;

/**
 *  * @brief Reboot reason storage unit
 *   */
typedef struct
{
    rst_reason reset_arr[MAX_RST_RSN_IDX];
    uint16_t rst_count;
    uint8_t arr_index;
}__attribute__ ((__packed__))reset_info;


/**
 * @brief  Possible STM32 system reset causes
 */
typedef enum reset_cause_e
{
    RESET_CAUSE_UNKNOWN = 0,
    RESET_CAUSE_LOW_POWER_RESET,
    RESET_CAUSE_WINDOW_WATCHDOG_RESET,
    RESET_CAUSE_INDEPENDENT_WATCHDOG_RESET,
    RESET_CAUSE_SOFTWARE_RESET,
    RESET_CAUSE_POWER_ON_POWER_DOWN_RESET,
    RESET_CAUSE_EXTERNAL_RESET_PIN_RESET,
    RESET_CAUSE_BROWNOUT_RESET,
} reset_cause_t;

/**
 * @brief API request to reboot.
 *
 * @param reboot_src : Reboot source id
 *
 * @return status of reboot
 * @retval 0->SUCCESS, 1->ERROR
 */
uint8_t reboot_request(uint32_t reboot_src);

/**
 * @brief API to get the reset reason.
 *
 * @return Reset reason ID
 */
reset_cause_t reset_cause_get(void);

/**
 * @brief API to get the reset reason name from the given reset cause
 *
 * @param reset_cause : reset cause id
 *
 * @return Reset reason name
 */
const char * reset_cause_get_name(reset_cause_t reset_cause);

/**
 * @brief API to save the reset reason in NVM.
 *
 * @param rst_rsn : reset reason
 */
void save_rst_reason(uint32_t rst_rsn);

/**
 * @brief API to shutdown the system
 */
void system_shutdown(void);

/**
 * @brief API to refresh watchdog timer
 */
void exo_wdt_refresh();

/**
 * @brief Watchdog early catch timer handler
 */
void wdt_early_catch_tmr(void* args);

void system_init(void);
void app_main(void);
void get_mcu_reset_reason_info(reset_info* rst_inf);

#endif
