/**
 * @file suspend_resume.h
 *
 * @brief This file contains function prototypes and data definitions that
 * helps to handle suspend and resume operations
 *
 * @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 _INC_SUSPEND_RESUME_H_
#define _INC_SUSPEND_RESUME_H_


#include "file_transfer.h"
#include "ftfci.h"



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

/**
 * @brief Suspend Resume request
 */
typedef enum sus_res_state
{
	state_suspend,			///< Transmission suspended
	state_suspend_ack,		///< transmission suspended ack received
	state_resume,			///< transmission resumed
	state_resume_ack, 		///< transmission resumed ack received
	no_suspend_reusume		///< NA
}e_sus_res_state;

/**
 * @ mode of suspend request
 */
typedef enum suspend_type
{
	suspend_by_timeout,			///< Timeout mode
	suspend_by_savecontext,		///< Save context mode
}e_suspend_type;


typedef enum avtive_type
{
	sus_res_master,				///< Master node ; Node which initiates suspension
	sus_res_slave,				///< Slave node ; Node which receive suspend request
	no_master_slave				///< NA
}e_avtive_type;


/**
 * @brief
 */
typedef struct suspend_resume_info
{
	eSystemState fsm_state_save;			///< SFTM state saved by Slave node during suspension
	e_sus_res_state state;					///< Suspend/resume request status
	e_suspend_type type;					///< type of suspend
	uint8_t resume_time;					///< Timeouttime for receiver side
	e_avtive_type active_sts;
	uint16_t timeout_time_save;				///< Timeout
	uint16_t request_clear_flag;
	uint8_t sus_res_rqst_retry_count;		///< Request retry trial
	uint8_t rqst_retry_flag;
	e_sus_res_state sus_res_state_copy;
	uint8_t sus_savctxt_ack_sent;
}s_suspend_resume_info;


extern s_suspend_resume_info s_sus_res_info;

/******************************************************************************/
/*               Declaration of exported function prototypes                  */
/******************************************************************************/

/**
 * @brief This function used to initialize suspend resume related params
 *
 *
 * @param[in]  void : none
 *
 * @return none
 * @retval void : none
 */
void init_sus_res(void);

/**
 * @brief This function used to process the suspend request has come from other node
 *
 *
 * @param[in]  susp_type : type of suspend request whether suspend-timeout or suspend-savecontext mode
 * @param[in]  timeoout_time : timeout value
 * @param[in]  ack_set : option to enable response frame
 *
 *
 * @return none
 * @retval void : none
 */
void ft_suspend(ft_request susp_type, uint16_t timeoout_time, uint8_t ack_set);

/**
 * @brief This function used to process the resume request has come from other node
 *
 *
 * @param[in]  resume_type : type of suspend request whether suspend-timeout or suspend-savecontext mode
 * @param[in]  ack_set : option to enable response frame
 *
 *
 * @return none
 * @retval void : none
 */
void ft_resume(uint8_t resume_type, uint8_t ack_set );

/**
 * @brief This function used to get the suspend resume current status
 *
 *
 * @param[in]  void : none
 *
 * @return state of suspend resume mode
 * @retval state_suspend : suspended
 * @retval state_resume : resumed
 * @retval no_suspend_reusume : none
 */
e_sus_res_state ft_get_suspend_resume_sts(void);


/**
 * @brief This function used to set the suspend resume current status
 *
 *
 * @param[in]  set : set suspend resume status
 *
 * @return none
 * @retval void : none
 */
void ft_set_suspend_resume_sts(e_sus_res_state set);


/**
 * @brief This function used to send acknowledgment for save context request
 *
 *
 * @param[in]  ack : positive ack or negative ack
 *
 *
 * @return none
 * @retval void : none
 */
void ft_savecontext_ack(uint8_t ack);

/**
 * @brief This function used to set the type of node whether node as master or slave
 * It will be decided based on who initiated the suspend request
 *
 *
 * @param[in]  set : set sus_res_master or sus_res_slave
 *
 *
 * @return none
 * @retval void : none
 */
void ft_set_master_slave(e_avtive_type set);

/**
 * @brief This function used to return the node type whether the node was setted as mater or slave
 *
 *
 * @param[in]  void : none
 *
 *
 * @return type of node
 * @retval sus_res_master : behave as master since it start the suspend request
 * @retval sus_res_slave : behave as slave since it start the suspend request
 *
 */
e_avtive_type ft_get_master_slave_status(void);


e_sus_res_state ft_get_state_before_suspend(void);

/**
 * @brief This handler function will handle the suspend timer expiry management
 * Suspend Resume request re try will also be handled here
 *
 *
 * @param[in]  void : none
 *
 *
 * @return none
 * @retval void : none
 */
void ft_sus_res_handler(void);

/**
 * @brief This function used to terminate transmission
 *
 *
 * @param[in]  ter_option : tell the source of terminate request
 * @param[in]  ack : add enable /disable acknowldgement request option in frame
 *
 *
 * @return none
 * @retval void : none
 */
void ft_terminate(ft_request ter_option, uint8_t ack);

/**
 * @brief This function used to terminate transmission
 *
 *
 * @param[in]  void : none
 *
 *
 * @return none
 * @retval void : none
 */
void ft_cancel_ftp(void);

/**
 * @brief  * This function will resume and reopen downloading file if suspend ack not delivered to requester node
 *
 *
 * @param[in]  void : none
 *
 *
 * @return none
 * @retval void : none
 */
void ft_check_suspend_status(void);

/**
 * @brief  * This function used to clear the counters used for sus-res request failure trails
 *
 *
 * @param[in]  void : none
 *
 *
 * @return none
 * @retval void : none
 */
void clear_sus_res_req_retry_cntr(void);

/**
 * @brief  * This function will clear sus res request if any pending request not processed in time
 *
 *
 * @param[in]  void : none
 *
 *
 * @return none
 * @retval void : none
 */
void sus_res_clear_pending_rqst(void);


/* debug functions */
#ifdef FT_SUSRES_TEST
void ft_sus_res_debug_init(void);
void ft_sus_res_debug_deinit(void);
#endif

#endif /* FILE_TRANS_REL_V1_SAMPLE_APPS_GS_SAMPLE_APP_INC_SUSPEND_RESUME_H_ */
