/**
 * @file comms_uhf_ftm.c
 *
 * @brief This file has the file transfer functions for S-Band firmware update
 *
 * @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 LINUX_TEMP_PORT
#include "comms_uhf_ftm.h"
#include "exo_osal_mem_cpy.h"
#include "crc_cal.h"
#include "exo_common.h"
#include "exo_tctm_ipc.h"


/* comms Firmware stored NOR flash address  */
#define COMMS_UHF_FW_STORED_ADDR 0x60C00000

/* comms Firmware stored SDRAM address  */
#define COMMS_UHF_FW_SDRAM_TEMP_ADDR 0xC0C00000

/**
* @brief Memcpy info saving structure definition
*/
typedef struct memcpy_info_save_def
{
	uint32_t src_addrs;   /*!< Source address for memcpy */
	uint32_t des_addrs;   /*!< Destination address for memcpy */ 
	uint32_t size;        /*!< Size in bytes to copy */
	uint32_t checksum;    /*!< Checksum value */
}memcpy_info_save;


memcpy_info_save PLACE_IN_EXT_RAM_MEM s_comms_uhf_ftm_handle;

/**
* @brief This function will be invoked by memcopy module once firmware
*  fully moved from SDRAM to permanent storage memory NOR flash
*
* @param sts : Unused variable
*/
//static void comms_uhf_sdram_nor_cpy_completed(uint8_t sts);


/*
 * It should be called in initialization of thread, it should not be called before the FTM module
 * initialization routine has not complete
 */
void comms_uhf_ftm_init(void)
{
	ft_register_receiver_app(TC_DST_FTM_OBC_COMMS, comms_uhf_ft_receiver_notificataion_clbk);
}


#if 0
/*
 * This function will be invoked by memcopy module once firmware fully moved from SDRAM to
 * permanent storage memory NOR flash
 */

//static void comms_uhf_sdram_nor_cpy_completed(uint8_t sts)
//{
//	uint32_t cal_checksum;
	


	// fw_flash copy from SDRAM to flash has completed
//	cal_checksum = calc_checksum_memory((uint8_t*)s_comms_uhf_ftm_handle.des_addrs,s_comms_uhf_ftm_handle.size);

//	if(cal_checksum == s_comms_uhf_ftm_handle.checksum)
//	{
		//DEBUG_CPRINT(("COMMS FW copied in NOR at Address:0x% of Size %lukb\n",s_comms_uhf_ftm_handle.des_addrs,s_comms_uhf_ftm_handle.size));

		/*
		 *  Add IPC here,
		 *  FW stored address :s_comms_uhf_ftm_handle.des_addrs
		 *  FW size : s_comms_uhf_ftm_handle.size
		 */
//	}
//	else
//	{
//		/* copy operation will be done again if first trail not done successfully */
//		DEBUG_CPRINT(("COMMS FW copied at NOR Fail\n"));

//	}

}
#endif






/*
 *
 *  Application API which will be invoked during file transmission by FT framework
 *  it handles all FTM related notification published by FTM module
 *
 */
/**
 * @brief UHF file transfer receiver notification callback
 * 
 * This callback function is invoked when a new file transfer is 
 * received via the UHF communication system. It handles processing
 * the received file.
 *
 * @param arg Pointer to the file transfer structure
 */
void comms_uhf_ft_receiver_notificataion_clbk(ft_notification_info *ft_sts)
{



	/*
	 *
	 *  THis callback service function should not wait for long time
	 *
	 *
	 */
	uint16_t u16cnt;
	


	DEBUG_CPRINT((" FTM for COMMS module >> notification API invoked !!!!\n"));

	if(ft_sts->status == transfer_download_success)
	{
		DEBUG_CPRINT(("1. download success\n"));
		if(ft_sts->dwld_info.tx_mode == 1)
		{
			DEBUG_CPRINT(("2. Transfer mode : DATA\n"));
			DEBUG_CPRINT(("3. Bulkdata received :"));

			for (u16cnt = 0; u16cnt < ft_sts->dwld_info.size_file_or_data; u16cnt++)
			{
				DEBUG_CPRINT(("%d,",ft_sts->dwld_info.bulkdata_ptr[u16cnt]));
			}
			DEBUG_CPRINT(("\n"));
		}
		else
		{

			DEBUG_CPRINT(("2. Transfer mode : FILE\n"));
			DEBUG_CPRINT(("3. File name : "));
			for (u16cnt = 0; u16cnt < ft_sts->dwld_info.path_filename_buf_length; u16cnt++)
			{
				DEBUG_CPRINT(("%c",ft_sts->dwld_info.storage_path_and_File_name[u16cnt]));
			}
			DEBUG_CPRINT(("\n4. File size : %ldkb",ft_sts->dwld_info.size_file_or_data));
			DEBUG_CPRINT(("\nNOR copy initiated !!!\n"));

			s_comms_uhf_ftm_handle.src_addrs = (uint32_t)ft_sts->dwld_info.bulkdata_ptr;
			s_comms_uhf_ftm_handle.size = ft_sts->dwld_info.size_file_or_data;
			s_comms_uhf_ftm_handle.checksum = ft_sts->dwld_info.checksum;
			s_comms_uhf_ftm_handle.des_addrs = COMMS_UHF_FW_STORED_ADDR;
		   // obc_memcpy_wrapper((uint8_t*)s_comms_uhf_ftm_handle.des_addrs, (uint8_t*)s_comms_uhf_ftm_handle.src_addrs, s_comms_uhf_ftm_handle.size, comms_uhf_sdram_nor_cpy_completed, 0,1);
		}


	}
	else if(ft_sts->status == crc_error)
	{
		DEBUG_CPRINT(("1. download failed/ CRC error\n"));
		if(ft_sts->dwld_info.tx_mode == 1)
		{
			DEBUG_CPRINT(("2. Transfer mode : DATA\n"));
		}

	}
	else if(ft_sts->status == transfer_suspended)
	{
		DEBUG_CPRINT(("1. download suspended by sender [Timeout mode]\n"));

	}
	else if(ft_sts->status == transfer_suspended_savecontext ||
			ft_sts->status == transfer_suspended_auto_savecontext ||
			ft_sts->status == transfer_suspend_savecontext_accepeted)

		{
			 if(ft_sts->status == transfer_suspended_savecontext)
			 {
					DEBUG_CPRINT(("1. download suspended by sender [Save Context Mode]\n"));
			 }
			 else
			 {
					DEBUG_CPRINT(("1. Auto [Save Context Mode] activated\n"));

			 }


	}
	else if(ft_sts->status == transfer_resumed)
	{
		DEBUG_CPRINT(("1. downloading resumed by sender\n"));
	}
	else if(ft_sts->status == transfer_resume_accepted)
	{
		DEBUG_CPRINT(("1. downloading resumed back by sender\n"));
	}
	else if(ft_sts->status == tx_terminated_suspend_timeout_expired)
	{
		DEBUG_CPRINT(("1. download Terminated due to suspend timeout expired\n"));

	}
	else if(ft_sts->status == transfer_suspend_accepted)
	{
		DEBUG_CPRINT(("1. download Suspend - Timeout mode Accepted by Sender\n"));

	}
	else if(ft_sts->status == transfer_suspend_savecontext_accepeted)
	{

	}
	else if(ft_sts->status == transfer_resumed_restorecontext)
		{
			DEBUG_CPRINT(("1. Download Resume Savecontext mode Requested by Sender\n"));
			DEBUG_CPRINT(("\r\nResume-SaveContext mode [Bulkdata] selected \r\n"));
		}
	else if(ft_sts->status == transfer_download_ready)
	{
	    DEBUG_CPRINT(("OTSU_check BLT ****m\n"));
		DEBUG_CPRINT(("1. Download request received\n"));
		if(ft_sts->dwld_info.tx_mode == 1)
		{
			DEBUG_CPRINT(("2. Transfer mode : DATA\n"));
		}
		else
		{
			DEBUG_CPRINT(("2. Transfer mode : FILE\n"));
			DEBUG_CPRINT(("3. File size: %ld\n",ft_sts->dwld_info.size_file_or_data));
			ft_config_receiver_node_storage_address(COMMS_UHF_FW_SDRAM_TEMP_ADDR);
		}
	}
	else if(ft_sts->status == transfer_resume_savecontext_accepeted)
	{
		DEBUG_CPRINT(("1. download Resume - Savecontext mode Accepted by Sender\n"));
	}
	else if(ft_sts->status == transmission_terminated_by_rx_node)
	{
		DEBUG_CPRINT(("1. download Terminated by Receiver node\n"));
	}
	else if(ft_sts->status == transfer_otsu)
	{

	}
	else if(ft_sts->status == transfer_suspend_tout_not_accepted)
	{
		DEBUG_CPRINT(("1. suspend timeout request not completed\n"));
	}
	else if(ft_sts->status == transfer_suspend_savecontext_failed)
	{
		DEBUG_CPRINT(("1. suspend savecontxt request not completed\n"));
	}
	else if(ft_sts->status == transfer_rseume_tout_not_accepted)
	{
		DEBUG_CPRINT(("1. resume timeout request not completed\n"));
	}
	else if(ft_sts->status == transfer_resume_restorecontext_failed)
	{
		DEBUG_CPRINT(("1. resume savecontxt request not completed\n"));
	}
	else
	{
		DEBUG_CPRINT(("Invalid Notification not processed\n"));
	}

	DEBUG_CPRINT(("\n-------------/End/---------------\n\n"));


}
#endif
