/**
 * @file ft_sender.h
 *
 * @brief This file contains function prototypes of OTSU function
 *
 * @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 OTSU_OTSU_H_
#define OTSU_OTSU_H_

//#define BOOTLOADER_ON

#include "common_otsu.h"
#include "otsu_tctm_man.h"


/******************************************************************************/
/*                Definition of exported macros                 			  */
/******************************************************************************/

#define HEADER_INTG_TEST 2
#define BTL_WRITE 		 1
#define INTG_TEST 		 0
//#define FTM_TEST_69_BOARD

/******************************************************************************/
/*         Definition of exported types (typedef, enum, struct, union)        */
/******************************************************************************/

/*
 * @brief Structure for new firmware validation status
 */
typedef enum otsu_new_fw_validate_sts_def
{
	new_fw_validate_none =0,				///< New Firmware Validation not started
	new_fw_validate_pass,					///< New Firmware Validation Pass
	new_fw_validate_fail					///< New Firmware Validation Fail
}otsu_new_fw_validate_sts;


/*
 * Structure for firmware download and manage
 */
typedef struct fw_flash_handle
{
	uint32_t storage_address;				///< Firmware downloaded address
	uint32_t flash_address_inc;				///< Firmware stored flash address
	uint32_t binary_size;    				///< storage size required */
	uint8_t *input_buffer_ptr;				///< FTDS frame buffer pointer
	uint32_t input_buffer_index_cntr;		///< FTDS frame index counter
	uint8_t  module_id;      				///< module id */
	uint8_t flash_write_option;				///< Flash driver write options
	uint8_t flash_sts;						///< Flash operation status
	uint32_t bin_checksum;					///< Firmware checksum
	uint8_t bin_copy_trial;					///< Firmware copy complete status
	uint16_t sw_version_bin;				///< FImeare vresion
}s_fw_flash_handle;



/*
 * @brief OTSU state enumeration
 */
typedef enum otsu_fw_update_manager
{
	e_otsu_idle,
	e_otsu_new_fw_validate_init,
	e_otsu_new_fw_validate_inprogress,
	e_otsu_new_fw_validated,
	e_otsu_new_fw_fail_overwrite_locC,
	e_otsu_new_fw_pass_overwrite_locB,
	e_otsu_dwld_prepare,
	e_otsu_dwld_copy,
	e_otsu_dwld_copy_error,
	e_otsu_dwld_complete,
	e_otsu_dwld_crc_error,
	e_otsu_dwld_flash_copy_fail,
	e_otsu_dwld_bin_header_error,
	e_otsu_gs_ack_wait_for_boot,
	e_otsu_auto_boot_enable,
	e_otsu_postponed_boot,
	e_otsu_postponed_rtc_config,
	e_otsu_postponed_rtc_ruinning,
	e_otsu_shutdown_publish,
	e_otsu_shutdown_processsing,
	e_otsu_shutdown_timeout_expired,
	e_otsu_boot_activated,
	e_otsu_integ_test_fallback_corrupt,
	e_otsu_boot_canceled,
	e_otsu_vfb_prepare_loc_c,
	e_otsu_vfb_prepare_loc_d,
	e_otsu_fallback_3_prepare_loc_c,
	e_otsu_ftm_saveconext_norcopy_progress,
	e_otsu_ftm_saveconext_sdramcopy_progress,
	e_otsu_sch_boot_alarm,
	e_otsu_sch_sband_sync_fail_alarm,
	e_otsu_fallback_corrupt_locA,
	e_otsu_fallback_corrupt_locB,
	e_otsu_fallback_corrupt_locC,
	e_otsu_fallback_corrupt_locD,
	e_otsu_read_version,
	e_otsu_gscont_waittime_running,
	e_otsu_gscont_waittime_over,
	e_otsu_gscont_waittime_started,
	e_otsu_waitfor_obc_shutdown,
	e_otsu_new_fw_pass_overwrite_locB_fail,
	e_otsu_new_fw_pass_overwrite_locB_norcopy_fail,
	e_otsu_ftm_saveconext_norcopy_fail,
	e_otsu_ftm_saveconext_norcopy_pass,
	e_otsu_new_fw_fail,
	e_otsu_vfb_prepare_loc_c_pass,
	e_otsu_vfb_prepare_loc_c_fail,
	e_otsu_vfb_prepare_loc_d_pass,
	e_otsu_vfb_prepare_loc_d_fail,
	e_otsu_gscontact_comms_request_sent,
	e_otsu_dwnd_cancel_fw_exists,
	e_otsu_boot_canceled_sw_version_mismatch,
	e_otsu_boot_canceled_dwld_sw_integrity_fail,
	e_otsu_boot_canceled_nvm_intgrity_fail,
}e_otsu_fw_update_man;


#if 0


/*
 * @brief OTSU state enumeration
 */
typedef enum otsu_fsm_def
{
	OTSU_INIT_STATE,
	OTSU_TCTM_SERVICE,
	OTSU_FW_DWNLD,
	OTSU_INTEG_TEST,
	OTSU_GS_SYNC_RCV,
	OTSU_GS_SYNC_TIMEOUT_SCH,
	OTSU_SCHBOOT_TIMEOUT_SCH,
	OTSU_START_SHUTDOWN_PROC,
	OTSU_OBC_HANDSHAKE_DONE,
	OTSU_INITIATE_FW_UPDATE,
	OTSU_FALLBACK_CORRUPT_TEST,
	OTSU_GS_SOFT_RESET_RQST,
	OTSU_GS_CONTACT_TIMER_CALBACK_SERVICE,
	OTSU_SCHBOOT_TIMEOUT_BYOTSU,
	OTSU_INVALID_STATE
}e_otsu_fsm;


#endif

/*
 * @brief Error status
 */
typedef enum otsu_fw_error_manager
{
	otsu_no_error,
	sct_sdram_err,
	sct_nor_copy_err,
	sct_read_from_nor_err,
	sdram_fmc_cpy_err,
	fw_nor_c_copy_err,
	fw_nor_b_copy_err,
	mem_cpy_err
}e_otsu_err_flag;


/*
 * @brief NVM read write operation
 */
typedef enum
{
	nvm_write,
	nvm_read
}nvm_rw_wr;


/*
 * @brief TCTM send enum
 */
typedef enum
{
	tc_not_send,
	tc_send
}tc_cmd;

/*
 * New firmware availability status
 */
typedef enum
{
	nw_frm_not_avail,
	nw_frm_avail,
}nw_frm_sts;


/**
 * @brief firmware update info
 */
typedef struct firmware_upd_info_def
{
	otsu_new_fw_validate_sts e_new_fw_working_sts;
	uint8_t nw_frmw_avail_sts;
	uint8_t new_fw_loaded;
	uint8_t tc_soft_reset_rqst;
}firmware_upd_info_def;


/******************************************************************************/
/*                    Declaration of exported variables                       */
/******************************************************************************/




/******************************************************************************/
/*               Declaration of global function prototypes                    */
/******************************************************************************/

/**
 * @brief This function is used to initialize the module module
 *
 * @param[in]  void : none
 *
 * @return none
 * @retval void : none
 */
void otsu_init();
void dummy_func_otsu(void);
uint8_t otsu_get_init_sts(void);

/**
 * @brief This function handles OTSU process
 *
 * @param[in]  void* : unused
 *
 * @return none
 * @retval void : none
 */
void otsu_handler_th(void *params);

/**
 * @brief This function return the current OTSU state
 *
 * @param[in]  void : none
 *
 * @return state of OTSU
 * @retval state of OTSU
 */
e_otsu_fw_update_man get_otsu_state(void);

/**
 * @brief This function used to update the OTSU state
 *
 * @param[in]  set : state to change
 *
 * @return none
 * @retval void : none
 */
void otsu_state_change(e_otsu_fw_update_man set);

/**
 * @brief This function handles the procedure to trigger system shutdown
 *
 * @param[in]  void : none
 *
 * @return none
 * @retval void : none
 */
void obc_shutdown_trigger(void);

/**
 * @brief This function return the software version of downloaded binary
 *
 * @param[in]  address_loc : software version read from address
 *
 * @return software version
 * @retval software version in 2 bytes , 1 byte tell the major version, 2nd byte tell the minor version
 */
uint16_t getVersion_downloaded_bin(uint32_t address_loc);


/**
 * @brief This function used to prepare and update the boot lookup table for the downaloded binary
 *
 * @param[in]  bin_header_address : downaloded firmware stored memory
 * @param[in]  boot_table_set_flag :  option to write boot table or not
 *
 * @return status of boot table update operation
 * @retval i indicates the update was success
 */
uint8_t prepare_boottable_setup_for_module(uint32_t bin_header_address,uint8_t boot_table_set_flag, uint8_t);

uint8_t fimrware_integrity_check(uint32_t fw_address, uint16_t sft_version);

uint16_t otsu_get_btable_size(void);

uint8_t otsu_copy_btable_data(uint8_t table_id, uint8_t * write_buf);

void otsu_initialisation(void);


void otsu_send_message_to_otsu_fsm(uint16_t msg_id, uint8_t pld_info_);

/**
 * @brief This function used to perform integrity check for firmware header section
 *
 * @param[in]  flash_address  : start address of header section to be verified
 *
 * @return status of integrity check
 * @retval R_OK1 - integrity pass
 */
 e_ret_sts header_integrity_check(uint32_t flash_address);


 e_ret_sts flash_integrity_check(uint32_t flash_address, uint32_t bin_size );



 /**
  * @brief This function used to assume new firmwware can able to
  * make GS contact
  * it can be used for testing purpose
  *
  * @param[in]  status  : status of firmware gs sync
  *
  * @return none
  * @retval void : none
  */
 void otsu_new_fw_validation_status_update(otsu_new_fw_validate_sts status);



#endif /* OTSU_OTSU_H_ */
