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

#ifndef _INC_NOTIFY_APPS_H_
#define _INC_NOTIFY_APPS_H_

#include "stdint.h"
#include "file_transfer.h"
#include "suspend_resume.h"

/* enumeration for notification type */
typedef enum ft_status
{
	transfer_ignore_notification,
	transfer_upload_ready,
	transfer_download_ready,
	transfer_otsu,
	transfer_upload_success, 					// a.	File transfer upload success status
	transfer_download_success, 					// a.	File transfer download success status
	transfer_suspended, 						// b.	File transfer suspend status
	transfer_suspend_accepted, 				    // b.	File transfer suspend status
	transfer_resumed, 							// c.	File transfer resume status
	transfer_resume_accepted, 					// c.	File transfer resume status
	transfer_suspended_savecontext, 			// d.	File transfer suspend context save mode
	transfer_suspended_auto_savecontext, 			// d.	File transfer suspend context save mode
	transfer_suspend_savecontext_accepeted, 	// d.	File transfer suspend context save mode
	transfer_resumed_restorecontext, 			// e.	File transfer resumed context save mode
	transfer_resume_savecontext_accepeted,		// e.	File transfer resumed context save mode
	transfer_suspend_savecontext_failed, 		// f.	File transfer suspend context save failed
	transfer_resume_restorecontext_failed, 		// g.	File transfer resumed context save failed
	storage_not_available, 						// h.	Storage memory unavailable on the receiver side
	transmission_terminated_by_rx_node, 		// i.	File transfer terminated by the receiver
	transmission_terminated_by_tx_node, 		// j.	File transfer terminated by the sender
	transmission_terminated_receiver_not_responsive, 		// j.	File transfer terminated by the sender
	tranmission_cancelled_segment_loss_error,	// k.	File transfer cancelled due to continuous segment loss
	crc_error, 									// l.	File transfer failure; CRC error
	tx_terminated_suspend_timeout_expired,    	// i.   File transfer cancelled due 2 suspender timeout expire
	invalid_receiver_app_id,                  	// j.   Receiver app not registered with FT framework
	transfer_upload_rejected,
	transfer_suspend_tout_not_accepted,
	transfer_rseume_tout_not_accepted,
	transfer_suspended_savecontext_etended_sts
}ft_status_type;


/* file/ data transfer download details */
typedef struct _ft_download_info
{
	uint8_t tx_mode;   		        			// transmission mode >> FILE or DATA
	uint8 rx_file_id;
	uint8_t *storage_path_and_File_name;		// storage folder and file name ; valid if tx_mode = FILE
	uint16_t path_filename_buf_length;		    // folder and file name buffer size; valid if tx_mode = FILE
	uint64_t stored_mem_address;				// data memory address ; valid if tx_mode = DATA
	uint32_t size_file_or_data;					// size of file or data buffer
	uint32_t checksum;
	uint8_t *bulkdata_ptr;
	uint8_t retranmission_status;
}ft_download_info;


/* Context saving information details */
typedef struct _ft_contextsave_info
{
	uint8_t session_id;	                		// session id of transmission
	uint8_t file_name_size;
	uint16_t context_info_size;					// size of context information available in buffer
	uint8_t *context_info_buf_ptr;	    		// context information to be stored in app
	e_ft_current_process node_operation;			// node operation >> upload / download
	eSystemState fsm_state_save;
	e_avtive_type mast_slave_sel;
	uint8_t tx_mode;
	uint32_t bulkdata_checksum;
	uint64_t bulkdata_mem_addr;
	uint32_t bulkdata_len;
}ft_contextsave_info;


/* App notification details */
typedef struct ft_app_notificataion_info
{
	uint16_t app_id;                           // application id
	ft_status_type status;   		          // ft status type
	ft_download_info dwld_info;               // only valid if status type is transfer_success
	ft_contextsave_info context_info;         // only valid if status type is transfer_suspended_savecontext
}ft_notification_info;


typedef void (*notify_api)(ft_notification_info*);
typedef uint8_t (*notify_complete_sts_fp)(ft_status_type sts);





/*!
 * @brief This API used to register sender side APP notify callback function
 *
 */
uint8_t ft_register_sender_app(uint16_t app_id, notify_api app_notify_api);


/*!
 * @brief This API used to register receiver side APP notify callback function
 *
 */
uint8_t ft_register_receiver_app(uint16_t app_id, notify_api app_notify_api);








/* internal FT functions */
uint8_t ft_notify_sender_app(ft_notification_info* notify_info);
uint8_t ft_notify_receiver_app(ft_notification_info* notify_info);
void ft_init_notify();
void ft_acceptance_received(notify_complete_sts_fp n_fp);
void ft_acceptance_nack(notify_complete_sts_fp n_fp);
void restart_transmission(void);
void restart_transmission_no_timer_set(void);
uint8_t is_sender_app_resgistered(uint16_t input_app_id);

#endif /* _INC_NOTIFY_APPS_H_ */
