/**
 * @file notify_apps.h
 *
 * @brief This file contains function prototypes and data definitions that
 * helps to handle app notification
 *
 * @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_NOTIFY_APPS_H_
#define _INC_NOTIFY_APPS_H_

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


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



/*
 *  @brief enumeration for notification type
 */
typedef enum ft_status
{
	transfer_ignore_notification,               ///< No notification
	transfer_upload_ready,					    ///< Ready for Upload
	transfer_download_ready,                    ///< Ready for download
	transfer_otsu,                              ///< Transmission for 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 auto context save mode
	transfer_suspend_savecontext_accepeted, 	///< d.	File transfer suspend save context accepted
	transfer_resumed_restorecontext, 			///< e.	File transfer resumed save context  mode
	transfer_resume_savecontext_accepeted,		///< e.	File transfer resumed save context accepted
	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,                   ///< Upload rejected
	transfer_suspend_tout_not_accepted,         ///< Suspend timeout request failed
	transfer_rseume_tout_not_accepted,          ///< Resume timeout mode failed
	transfer_suspended_savecontext_etended_sts  ///< Status to start NOR copy
}ft_status_type;


/*
 * @brief file/ data transfer download details
 */
typedef struct _ft_download_info
{
	uint8_t tx_mode;   		        			// transmission mode >> FILE or DATA
	uint8_t tx_file_id;   		        	    // 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;							// Checksum
	uint8_t *bulkdata_ptr;						// bulk data buffer pointer
}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;                     // File name
	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;
	ptr_typ 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);


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


/*!
 * @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);




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

/**
 * @brief This function used to handle the positive acknowledgment received for suspend or resume request
 *
 * @param[in]  n_fp : choose sender or receiver app
 *
 * @return none
 * @retval void : none
 */
void ft_acceptance_received(notify_complete_sts_fp n_fp);

/**
 * @brief This function used to handle the negative acknowledgment received for suspend or resume request
 *
 * @param[in]  n_fp : choose sender or receiver app
 *
 * @return none
 * @retval void : none
 */
void ft_acceptance_nack(notify_complete_sts_fp n_fp);

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

/**
 * @brief This function used to restart the transmission without FTDS interval timer ON
 *
 * @param[in]  void : none
 *
 * @return none
 * @retval void : none
 */
void restart_transmission_no_timer_set(void);

/**
 * @brief This function used to check that notification callback has been registered for the requested app id
 *
 *
 * @param[in]  input_app_id : status notification
 *
 * @return status of callback registration for sender app
 * @retval 1 indicates app available, 0 is not available
 */
uint8_t is_sender_app_resgistered(uint16_t input_app_id);

/**
 * @brief This function used to update the notification structure with status information
 * that will be passed to application notification callbacks for receiver side.
 *
 *
 * @param[in]  status : status notification
 *
 * @return status of notification published to app
 * @retval 1 indicates success, 0 is error
 */
uint8_t prepare_sts_and_notify_receiver_app(ft_status_type status);

/**
 * @brief This function used to update the notification structure with status information
 * that will be passed to application notification callbacks for sender side.
 *
 *
 * @param[in]  status : status notification
 *
 * @return status of notification published to app
 * @retval 1 indicates success, 0 is error
 */
uint8_t prepare_sts_and_notify_sender_app(ft_status_type status);

#endif /* _INC_NOTIFY_APPS_H_ */
