/*
 * file_transfer.h
 *
 *  Created on: 30-Sep-2022
 *      Author: Invcuser52
 */

#ifndef _INC_FILE_TRANSFER_H_
#define _INC_FILE_TRANSFER_H_


#include "fsm.h"
#include "ft_timer.h"
#include "get_input.h"
#include "segmentation.h"
#include "unistd.h"
#include "ft_config_param.h"
#include "ft_debug.h"

//#define FT_SAT
//#define FT_OBC

#define M_FILE 0
#define M_DATA 1


/* APP id for OBC OTSU, COMMS, EPS and ADCS */
#define APPID_DST_FTM_OTSU_             0x8D
#define APPID_DST_FTM_OBC_COMMS_ 		0x8F
#define APPID_DST_FTM_OBC_EPS_			0x90
#define APPID_DST_FTM_OBC_ADCS_         0x91



/* the below macro should be enabled in makefile in file transfer source folder
 *lib/file_transfer/makefile
 */
//#define FTM_SEQ_DROP_EN_FILE
//#define FTM_SEQ_DROP_EN_YES_NO
//#define FTM_CRC_CORRUPT_EN
//#define FTM_SBAN_FAIL_EN
//#define FTM_SBAN_FAIL_EN_START5_SKIP_ALL_SEQ
//#define FTM_SBAN_FAIL_EN_START5_SKIP_100_SEQ
//#define FTM_SBAN_FAIL_EN_START5_SKIP_2_SEQ
//#define FTM_SBAN_FAIL_EN_START5_NOSKIP
//#define FT_SUSRES_TEST


/* ftds frame waiting timeout time in seconds */
#define FTDP_WAITING_TIME_INTERVAL 15

#define SEQ_LOSS_DETECTION_OFF 0

#ifndef FT_OBC
#define FTDS_TICK_TIME 1000 // 1 sec = 1000 millisecnds
#else
#define FTDS_TICK_TIME 5 // millisecnds
#endif

typedef enum ft_current_process
{
	ft_upload,
	ft_download,
	ft_process_na
}e_ft_current_process;


typedef enum ft_err_cod
{
	ft_noerror,
	ft_framework_busy_error,
	ft_icd_tctm_pld_callback_error,
	ft_sender_app_not_reg_error,
	ft_input_file_open_error,
	ft_invalid_input_error,
	ft_receiver_app_not_reg_error,
	ft_receiver_side_undefined_error,
	ft_malloc_ram_error,
	ft_malloc_flash_error,
	ft_output_file_open_error,
	ft_sc_malloc_error,
	ft_rc_null_error,
	ft_rc_context_input_error,
    ft_rx_mem_ptr_null_error,
	ft_rx_invld_input_cfg,
	ft_rx_sys_error
}e_ft_error_sts;



typedef struct ft_status_update
{
	e_ft_error_sts error;
	uint8_t upload_percentile;
}ft_compl_and_error_status;


typedef enum ft_flag_def_
{
	e_ft_flag_set = 0xaa,
	e_ft_flasg_clear = 0x55
}ft_flag_def;

extern e_ft_current_process ft_process;

typedef uint8_t (*ft_payload_tx_type)(uint16 tc_tm_id, uint16 src_dct_id, uint8_t *payload_ptr, uint16_t payload_len);


typedef uint8_t (*ft_payload_obc_type)(uint64_t mem_addr, uint8_t *payload_ptr, uint16_t payload_len);

typedef uint8_t (*ft_payload_obc_busycheck_type)(void);

extern ft_payload_tx_type ft_payload_send;
extern ft_payload_obc_type ft_payload_obc_send;
extern ft_payload_obc_busycheck_type ft_payload_obc_busy_sts;



/*!
 * @brief This API manages sender and receiver operation of file transfer
 *
 */
void* ft_handler(void*);
void ft_set_ftds_delay(uint16_t set);
uint8_t ft_get_ftds_delay(void);
uint8_t ft_get_sband_link_status(void);
void ft_sus_res_debug_disable(void);




/* internal FT function */
void init_file_transfer();
e_ft_current_process get_file_transfer_state(void);
void set_file_transfer_state(e_ft_current_process set);
void ft_free_message_buffer(uint16 tc_tm_id);
void ft_payload_parser(uint16 tc_tm_id, uint8_t src_dst_id, uint8_t *payload_ptr, uint16_t payload_len);
uint8_t ft_register_pld_trasnmitter_cblk(ft_payload_tx_type fp);
uint8_t ft_register_obc_trasnmitter_cblk(ft_payload_obc_type fp);
uint8_t ft_register_obc_flashbusy_sts_cblk(ft_payload_obc_busycheck_type fp);
e_ft_error_sts ft_get_error_sts(void);
void ft_set_err_sts(e_ft_error_sts);
void ft_set_percentile_sts(uint8_t percent);
uint8_t ft_get_upload_percentage_compl(void);
void ft_send_tctm(uint16 tc_id, uint8 *tc_data, uint16 tc_data_len);

void ft_sus_res_debug_skip_all_sus_req(void);   // FTD1
void ft_sus_res_debug_enable_all_sus_req(void); // FTD2
void ft_sus_res_debug_skip_all_sus_ack(void); // FTD3
void ft_sus_res_debug_enable_all_sus_ack(void); // FTD4
void ft_sus_res_debug_skip_all_res_req(void); // FTD5
void ft_sus_res_debug_enable_all_res_req(void); // FTD6
void ft_sus_res_debug_skip_all_res_ack(void); // FTD7
void ft_sus_res_debug_enable_all_res_ack(void); // FTD8
void ft_sus_res_debug_enable(void);   // FTD9



#ifndef FT_SAT
void ft_save_tc_request(tc_tm_id_e_ft set);
tc_tm_id_e_ft ft_get_tc_request(void);
#else
void ft_save_tm_request(tc_tm_id_e_ft set);
tc_tm_id_e_ft ft_get_tm_request(void);
#endif

#endif /* _INC_FILE_TRANSFER_H_ */
