/**
 * @file #include "message_frame_def.h"
 *
 * @brief This file contains function prototypes and data definitions that
 * helps to form frame FTCI, FTSR and FTDS
 *
 * @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 SRC_MESSAGE_FRAME_DEF_H_
#define SRC_MESSAGE_FRAME_DEF_H_
#include "data_type.h"
#include "tc_tm_sample.h"

/******************************************************************************/
/*                Definition of exported macros                 			  */
/******************************************************************************/

#define FTCI_MSG_ID_UP 0x10
#define FTSR_MSG_ID_UP 0x20
#define FTDP_MSG_ID_UP 0x30
#define FTCI_MSG_ID_DL 0x40
#define FTSR_MSG_ID_DL 0x50
#define FTDP_MSG_ID_DL 0x60



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



/*
 * @brief File transfer Control field 1
 *
 */
typedef struct ftci_ctrl1
{
	uint8_t file_tx_session_id: 3; 		/*bit 0-2 >> file transfer session id */
	uint8_t resume_context_mode: 1; 	/*bit 3 >>  */
	uint8_t seq_loss_detection_mode: 1; /*bit 4 >>  */
	uint8_t file_id: 3; 				/*bit 5-7 >> file id */}

u_ftci_ctrl1;

/*
 * @brief File transfer Control field 2
 *
 */
typedef struct ftci_ctrl2
{
	uint8_t transfer_mode_sel: 1; 		/*bit 0   >> file transfer mode */
	uint8_t file_format: 3; 			/*bit 1-3 >> file transfer mode */
	uint8_t unit: 2; 					/*bit 4-5 >> Unit of File size or Memory size  */
	uint8_t size_field_length: 2; 		/*bit 6-7 >> No of bytes used to hold the total file size or data transfer size  */

	//uint8_t all;
}

u_ftci_ctrl2;

/*
 * @brief   File Transfer Status Response Frame Control field
 *
 */
typedef struct ftsr_ctrl_bits
{
	uint8_t file_tx_session_id: 3; 		/*bit 0-2 >> file transfer session id */
	uint8_t response_code: 5; 			/*bit 3-8 >> response code */

}

s_ftsr_ctrl_bits;

/*
 * @brief   File Transfer Status Response Frame Control field
 *
 */
typedef union ftsr_ctrl
{
	s_ftsr_ctrl_bits bit; 				/*bit 0-2 >> file transfer session id */
	uint8_t all;
}

u_ftsr_ctrl;

/*
 * @brief   File Transfer Data Packet Frame Control field
 *
 */
typedef struct ftds_ctrl_bits
{
	uint8_t file_tx_session_id: 3; 			/*bit 0-2 >> file transfer session id */
	uint8_t seg_type: 2; 					/*bit 3-4 >> segment type selection */
	uint8_t seq_num_field_size_sel: 2; 		/*bit 5-6 >> No of byte used to hold the SEQUENCE NUM field  */
	uint8_t data_length_field_size_sel: 1; 	/*bit 7   >> No of byte used to hold the data length field of the current segment */
}

s_ftds_ctrl_bits;

typedef union ftds_ctrl
{
	s_ftds_ctrl_bits bit;
	uint8_t all;
}

u_ftds_ctrl;




/*
 * @brief   File Transfer Status Response Frame Control field
 *
 */
typedef struct ftfci_ctrl_bits
{
	uint8_t file_tx_session_id: 3; 		/*bit 0-2 >> file transfer session id */
	uint8_t response_code: 5; 			/*bit 3-7 >> response code  */
}

s_ftfci_ctrl_bits;

/*
 * @brief   File Transfer Status Response Frame Control field
 *
 */
typedef union ftfci_ctrl
{
	s_ftfci_ctrl_bits bit; 				/*bit 0-2 >> file transfer session id */
	uint8_t all;
}

u_ftfci_ctrl;





/*
 * @brief   size representation
 *
 */
typedef struct size_rep
{
	uint8_t size_in_byte: 8; 			/*sequence number in 1 byte length */
	uint16_t size_in_2byte: 16; 		/*sequence number in 2 byte length */
	uint32_t size_in_3byte: 24; 		/*sequence number in 3 byte length */
	uint32_t size_in_4byte: 32; 		/*sequence number in 4 byte length */
	//uint32_t  size;
}

u_size_rep;

/*
 * @brief   size representation
 *
 */
typedef enum response_code
{
	    ftsr_receiver_ready = 0,
//		ftsr_disk_full = 1,
		ftsr_rx_api_not_found = 1,
		ftsr_suspend = 2,
		ftsr_resume = 3,
		ftsr_stop_transfer = 4,
		ftsr_missing_pckts = 5,
		ftsr_crc_fail = 6,
		ftsr_file_not_found = 7,
		ftsr_head_change = 8,
		ftsr_cancelled_by_suspend_timeoout =9,
		ftsr_suspend_savecontext = 10,
		ftsr_resume_savecontext = 11,
		ftsr_ack = 12,
		ftsr_nak= 13,
		ftsr_ping = 14,
		ftsr_download_complete = 15,
		ftsr_terminate_rxnode,
		ftsr_invalid_session_id,
        ftsr_dwnld_ptr_null,
		ftsr_ftm_busy,
		ftsr_invalid_cfg_input,
		ftsr_rx_system_error
}

e_ftsr_response_code;



/*
 * @brief   FTFCI response code
 *
 */
typedef enum ftfci_response_code
{
	ftfci_na = 0,
	ftfci_cancelled_by_app = 1,
	ftfci_cancelled_by_seqloss = 2,
	ftfci_suspend = 3,
	ftfci_resume = 4,
	ftfci_suspend_savecontext = 6,
	ftfci_resume_savecontext = 7,
	ftfci_cancelled_by_susp_timeoout = 8,
	ftfci_cancelled_by_txnode = 9,
	ftfci_ack,
	ftfci_nak,
	ftfci_ping
}

e_ftfci_response_code;


typedef struct string_info
{
	uint8_t * str_ptr;
	uint16_t size;
}

s_string_info;

/*
 * @brief  FRAME1:  File Transfer Control Info Frame - FTCI
 *
 */
typedef struct file_tx_ctrl_info
{
	u_ftci_ctrl1 ctrl_1; 		/*control field 1 */
	u_ftci_ctrl2 ctrl_2; 		/*control field 2 */
	uint16_t mtu_size; 			/*MTU size information	 */
	uint32_t checksum; 			/*checksum	 */
	uint16_t rx_con_fail_timeout;  /* receiver node timeout time
	                                * for connection failure in seconds */
	uint16_t reserved;
}

s_file_tx_ctrl_info;


/*
 * @brief  FRAME2: File Transfer Status Response Frame - FTSR
 *
 */
typedef struct file_tx_sts_resp
{
	u_ftsr_ctrl ctrl; 				/*control field */
	uint8_t segment_loss_count; 	/* total number of missed packet */
	uint16_t suspend_wait_timeout; 	/* timeout value for receiver side suspend request */
	//u_packet_loss_ctrl_bits data_loss_ctrl; /* control field for data loss info */
	//uint8_t	   		missing_packtT;                /*Total no of missing data segments	 */
	//uint8_t 	    *seg_id_ptr;				    /*missing segments id	 */
}

s_file_tx_sts_resp;

/*
 * @brief  FRAME3: File Transfer Data Packet Frame - FTDS
 *
 */
typedef struct file_tx_data_pckt
{
	u_ftds_ctrl ctrl; 		/*control field */
	//uint8_t 	   *data_ptr;					    /*other info >> seq num, data length, data	 */
}

s_file_tx_data_pckt;




/*
 * @brief  FRAME4: File Transfer Framework Control Info Frame - FTCI
 *
 */
typedef struct file_tx_framework_ctrl_info
{
	u_ftfci_ctrl ctrl; 				/*control field */
	uint16_t suspend_wait_timeout; 	/* timeout value for receiver side suspend request */
}

s_file_tx_framework_ctrl_info;


#endif /*SRC_MESSAGE_FRAME_DEF_H_ */
