/**
 * @file ft_sender.c
 *
 * @brief This file contains functions that provides demonstration of
 *        usage of state machine APIs
 *
 * The functions provided in the file provides demonstration on
 * usage of state machine APIs
 */

#include <stdio.h>

#include "download_def.h"
#include "file_transfer.h"
#include "fsm.h"
#include "ft_timer.h"
#include "ftci_decode.h"
#include "ftds_decode.h"
#include "ftsr.h"
#include "suspend_resume.h"
#include "ft_receiver.h"
#include "ftfci_decode.h"



uint8_t rx_response_ping_cnt;
uint8_t ftds_rx_check_flag_set;


void ft_receiver_ping_rest(void)
{
	rx_response_ping_cnt =0;
}


static void receiver_side_response_periodic_check(void)
{
	rx_response_ping_cnt++;
	if(rx_response_ping_cnt >= (dwnld_rqst.rx_node_conn_fail_timeout/FTDP_WAITING_TIME_INTERVAL))
	{
		printf("Sender not responsive for past %d sec !!! Terminate transmission\n",dwnld_rqst.rx_node_conn_fail_timeout);
		ft_transfer_request(T_Rxnode,0);
	}
}

/*
 * this function only called during download, so expected state can only be suspend
 * can not be executed during suspend
 */
void sequence_loss_manager()
{
	uint8_t timer_flg;

	e_sus_res_state sus_res_sts;
	sus_res_sts = ft_get_suspend_resume_sts();
	if(sus_res_sts != state_suspend && s_sus_res_info.request_clear_flag != 1)
	{
		timer_flg = ft_get_flag();
		if(timer_flg == 1)
		{
			if(dwnld_rqst.seq_loss_detect_enable == SEQ_LOSS_DETECTION_OFF)
			{
				printf("FTM - sender connection fail ");
        			ft_reset_timer();
                                receiver_side_response_periodic_check();

			}
			else if(dwnld_rqst.data_loss.total_lost_segment_count != 0 || dwnld_rqst.receive_busy ==1)
			{
				set_resend_flag(&dwnld_rqst);
				dwnld_rqst.data_loss.head_change_request = 1;
				ft_fsm_change_state(FT_Report_Sequence_loss);
				ft_reset_timer();
				receiver_side_response_periodic_check();
			}
		}
	}
}



/**
 *@brief State Machine :Function that handles data loss detection and recovery
 *
 */
tc_tm_id_e_ft ft_statemachine_download(void)
{
	eSystemState currentState;
	tc_tm_id_e_ft tc_id = FT_TC_TM_MAX_ID;

	sequence_loss_manager();
	ft_sus_res_handler();
	sus_res_clear_pending_rqst();
	currentState = ft_fsm_read_state();
	switch (currentState)
	{

		case FT_Init:
			//init_file_transfer();
			break;

		case FT_Idle_State:
			break;

		case FT_Receive_Sequence:
//			ftds_decode();
			break;

		case FT_Report_Sequence_loss:
			prepare_packet_loss_notify_frame(&dwnld_rqst, ftsr_missing_pckts);
			break;

		case FT_Report_Head_Change:
			tc_id = prepare_head_change_request_frame(&dwnld_rqst);
			break;

		case FT_Suspended:
		break;

		case FT_Resumed:
			break;

		default:
			break;
	}

	ft_decode_ftfci_frame();
	return tc_id;
}
/**
 *@brief State Machine :Function that use to store session id 
 *
 */
void ft_rx_session_id_store(uint8 sid)
{
	dwnld_rqst.session_id = sid;
}
/**
 *@brief State Machine :Function that use to read stored session id
 *
 */
uint8_t ft_rx_session_id_read(void)
{
  return dwnld_rqst.session_id;
}
