/*
 * otsu_tctm_msg_def.h
 *
 *  Created on: Nov 20, 2022
 *      Author: Invcuser52
 */

#ifndef INC_OTSU_TCTM_MSG_DEF_H_
#define INC_OTSU_TCTM_MSG_DEF_H_


#include "stdint.h"



typedef enum otsu_mem_type_def
{
  otsu_ftm_dwnld_mem =0,
  otsu_storage_mem,
  otsu_execution_mem,
  otsu_previous_sft_mem,
  otsu_current_sft_bckup_mem,
  otsu_sft_integ_test,
  otsu_mem_none
} e_otsu_mem_type_def;



typedef enum e_boot_cotrol_def
{
   gs_cntrl = 1,
   auto_cntrl = 2
 }e_boot_ctrl_def;


 typedef enum e_boot_schedule_def
 {
    boot_postponed = 1,
	boot_now = 2
  }e_boot_schdl_def;


  // otsu_tc_memory_layout
typedef struct otsu_memory_layout_tc_encode_def
{
    e_otsu_mem_type_def otsu_mem_typ_sel;
    uint32_t set_memory_address;
    uint32_t set_memory_size;
}s_otsu_memory_layout_tc_encode_def;

typedef struct postponed_time_set_def
{
	uint8_t hours;
	uint8_t min;
	uint8_t sec;
	uint8_t unused;
}s_time_set_def;


typedef struct postponed_date_set_def
{
	uint8_t day;
	uint8_t month;
	uint16_t year;
}s_date_set_def;

typedef union u_postponed_time_set_def
{
	s_time_set_def bit;
	uint64_t all;
}u_postponed_time_set_def;


// otsu_tc_firmware_update_config
typedef struct otsu_firmware_update_config_tc_encode_def
{
  uint16_t sft_version;                 	// 2byte
  uint16_t shutdwn_process_timeout; 		// 2byte
  s_time_set_def schboot_utc_time; 		    // 4byte
  s_date_set_def schboot_utc_date; 		    // 4byte
  s_time_set_def gs_cont_utc_time;          // 4ybte
  s_date_set_def gs_cont_utc_date;          // 4byte
  uint8_t boot_operation_ctrl;  			// 1byte
  uint8_t gs_cont_fail_time_checksum;   					    // 1byte
  uint16_t gs_cont_failure_timeout;                          // 2bytes
}s_otsu_fw_update_cfg_tc_encode_def;





/*
 * @brief OTSU message IDS
 */

typedef enum otsu_fw_error_manager
{
	otsu_no_error,
	sct_sdram_err,
	sct_nor_copy_err,
	sct_read_from_nor_err,
	sdram_fmc_cpy_err,
	fw_nor_c_copy_err,
	fw_nor_b_copy_err,
	mem_cpy_err
}e_otsu_err_flag;

typedef enum otsu_integ_test_def
{
  e_integ_tst_none =0,
  e_fw_val_pass,
  e_fw_val_fail,
  e_fallback_1_fail,
  e_fallback_2_fail,
  e_fallback_3_fail,
  e_fallback_4_fail,
  e_sband_failure_on,
  e_sband_failure_off,
  e_ftds_ack_enable,
  e_ftds_ack_disable,
  e_integ_tst_max
} e_otsu_integ_test;



typedef struct otsu_tc_encode_def_
{
  s_otsu_fw_update_cfg_tc_encode_def fw_boot_cfg;
  s_otsu_memory_layout_tc_encode_def mem_lay_config;
  e_otsu_integ_test integ_test_code;
}s_otsu_tc_encode_def;



typedef struct otsu_tctm_info
{
	uint8_t *tctm_ptr;
	uint16_t tctm_size;
}
s_otsu_tctm_info;



typedef struct otsu_tc_msg_parse_def
{
  uint8_t otsu_msg_id			:4;
  uint8_t gs_cntrl_autonomous	:2;
  uint8_t reboot_postpond_flag	:2;
}s_otsu_tc_msg_parse_def;

/*
 * @brief Firmware configuration params
 */
typedef struct firmware_update_config_parms_def
{
  uint8_t otsu_msg_id			:4;
  uint8_t gs_cntrl_autonomous	:2;
  uint8_t reboot_postpond_flag	:2;
  uint8_t reserved1;
  uint16_t sft_ver;
  s_time_set_def utc_time_to_schboot;
  s_date_set_def utc_date_to_schboot;
  s_time_set_def gs_cont_sync_timeout_time;
  s_date_set_def gs_cont_sync_timeout_date;
  uint16_t shutdwn_timeout;
}s_firmware_update_config_parms_deffff;


typedef struct tctm_firmware_update_config_parms_def
{
  uint8_t otsu_msg_id			:4;
  uint8_t boot_type             :4;
  uint8_t reserved1;
  uint16_t sft_ver;
  s_time_set_def utc_time_to_schboot;
  s_date_set_def utc_date_to_schboot;
  uint32_t gs_contact_wait_time;
  uint32_t reserved2;
  uint16_t shutdwn_timeout;
}s_firmware_update_config_parms_def;


/*
 * @brief OTSU memory layout frame structure
 */
typedef struct otsu_memory_layout_conf_def
{
  uint8_t otsu_msg_id 		: 4;
  uint8_t otsu_mem_typ_sel 	: 4;
  uint8_t reserved1;
  uint16_t reserved2;
  uint32_t set_memory_address;
  uint32_t memory_size;
}s_otsu_memory_layout_conf_def;


/*
 *  OTFS response code
 *
 */
typedef enum otfs_response_code_def
{
	otfs_none 				=0,
	otfs_sft_version 		=1,
	otfs_ready_for_boot 	=2,
	otfs_boot_initiated 	=3,
	otfs_boot_pass 			=4,
	otfs_boot_fail 			=5,
	otfs_flash_copy_fail 	=6,
	otfs_sft_ver_exists 	=7,
	otfs_current_status		=8,
	otfs_utc_time_invalid 	=9,
	otfs_get_boot_cfg_info 	=10,
	otfs_ack				=11,
	otfs_nack				=12,
	otfs_ready_4resume_cntxt  			=13,
	otfs_get_fw_info 					=14,
	otfs_downaloded_fw_integrity_fail	=15,
	otfs_sft_version_mismatch 			=16,
	otfs_boot_postponed					=17,
	otfs_boot_scheduled					=18,
	otfs_boot_immediate					=19,
	otfs_get_gscont_waitime				=20,
	otfs_fallback_boot_initiated   		=21,
	otfs_get_blt1						=22,
	otfs_get_blt2						=23,
	otfs_nvm_sft_version				=24,
	otfs_nvm_fw_integrity_fail   		=25,
	otfs_response_max 					=0xff
}otfs_response_code;

/*
 *  File Transfer Status Response Frame Control field
 *
 */
typedef struct otfs_ctrl_bits
{
	uint8_t response_code: 5; /*bit 0-4 >> unused bits */
	uint8_t unused		 : 3; /*bit 5-7 >> unused bits */
}s_otfs_ctrl_bits;


/*
 *  File Transfer Status Response Frame Control field
 *
 */
typedef union otfs_ctrl
{
	s_otfs_ctrl_bits bit; /*bit 0-2 >> file transfer session id */
	uint8_t all;
}u_otfs_ctrl;




/*
 * @@@@@ FRAME: OTSU TM Info Frame - OTFS
 *
 */
typedef struct otsu_firmware_sts_def
{
	u_otfs_ctrl ctrl; 		/* control field 	*/
	uint8_t otsu_status;
	uint16_t software_ver;  /* software version */
}otsu_firmware_sts;

typedef enum otsu_fw_update_manager
{
     


    e_otsu_idle,
	e_otsu_new_fw_validate_init,
	e_otsu_new_fw_validate_inprogress,
	e_otsu_new_fw_validated,
	e_otsu_new_fw_fail_overwrite_locC,
	e_otsu_new_fw_pass_overwrite_locB,
	e_otsu_dwld_prepare,
	e_otsu_dwld_copy,
	e_otsu_dwld_copy_error,
	e_otsu_dwld_complete,
	e_otsu_dwld_crc_error,
	e_otsu_dwld_flash_copy_fail,
	e_otsu_dwld_bin_header_error,
	e_otsu_gs_ack_wait_for_boot,
	e_otsu_auto_boot_enable,
	e_otsu_postponed_boot,
	e_otsu_postponed_rtc_config,
	e_otsu_postponed_rtc_ruinning,
	e_otsu_shutdown_publish,
	e_otsu_shutdown_processsing,
	e_otsu_shutdown_timeout_expired,
	e_otsu_boot_activated,
	e_otsu_integ_test_fallback_corrupt,
	e_otsu_boot_canceled,
	e_otsu_vfb_prepare_loc_c,
	e_otsu_vfb_prepare_loc_d,
	e_otsu_fallback_3_prepare_loc_c,
	e_otsu_ftm_saveconext_norcopy_progress,
	e_otsu_ftm_saveconext_sdramcopy_progress,
	e_otsu_sch_boot_alarm,
	e_otsu_sch_sband_sync_fail_alarm,
	e_otsu_fallback_corrupt_locA,
	e_otsu_fallback_corrupt_locB,
	e_otsu_fallback_corrupt_locC,
	e_otsu_fallback_corrupt_locD,
	e_otsu_read_version,
	e_otsu_gscont_waittime_running,
	e_otsu_gscont_waittime_over,
	e_otsu_gscont_waittime_started,
	e_otsu_gscont_waitfor_obc_shutdown,
	e_otsu_new_fw_pass_overwrite_locB_fail,
	e_otsu_new_fw_pass_overwrite_locB_norcopy_fail,
	e_otsu_ftm_saveconext_norcopy_fail,
	e_otsu_ftm_saveconext_norcopy_pass,
	e_otsu_new_fw_fail,
	e_otsu_vfb_prepare_loc_c_pass,
	e_otsu_vfb_prepare_loc_c_fail,
	e_otsu_vfb_prepare_loc_d_pass,
	e_otsu_vfb_prepare_loc_d_fail,
	e_otsu_gscontact_comms_request_sent,
	e_otsu_dwnd_cancel_fw_exists,
	e_otsu_boot_canceled_sw_version_mismatch,
	e_otsu_boot_canceled_dwld_sw_integrity_fail,
	e_otsu_boot_canceled_nvm_intgrity_fail,

    
}e_otsu_fw_update_man;




/*//////////////// Bootloader definitions ///////////////////////////////// */
typedef enum module_list_e
{
	MODULE_ONE_OS = 0,
	MODULE_TWO_OTSU,
	MDOULE_MAXIMUM
#if 0
	MODULE_THREE,
	MODULE_FOUR,
	MODULE_FIVE,
	MODULE_SIX,
	MODULE_SEVEN,
	MODULE_EIGHT,
	MODULE_NINE,
	MODULE_TEN,
	MDOULE_MAXIMUM = 50
#endif
}e_module_list;



typedef enum sbl_list
{
	bootloader_lut_one =0,  /* it should be 0 since it will be used as array inex */
	bootloader_lut_two =1,
	bootloader_lut_undefined,
	bootloader_lut_one_corrupted,
	bootloader_lut_two_corrupted,
	bootloader_lut_both_corrupted
}e_sbl_list;

typedef enum lut_selection
{
	lut_activate = 0x10,
	lut_deactivate = 0x20
}e_lut_selection;


typedef enum bank_selection
{
	bank_1 = 0x08000000,
	bank_2 = 0x08100000
}e_bank_selection;


typedef enum ffs_flag_e
{
	ffs_set 		= 0xAA,     // ffs_new_boot_avail
	ffs_notSet 		= 0xBB,  // unused
	ffs_success 	= 0x55,  // _already_boot_success
	ffs_undefined 	= 0xFF, // // ffs_no_boot_avail,

}e_ffs_flag;

// look up table control bytes   | size = 12
typedef struct lut_control
{
	uint8_t total_module;
	uint8_t    FFS_flag;
	uint8_t       single_bin_boot;
//	uint8_t       memory_chunk_release;   // for janus 1 it will be used for new firmware status update
	uint8_t       new_firmware_update_state;
	uint16_t      boot_success_state;

}s_lut_control;

// module wise information   | size = 24bytes
typedef struct lut_moduel_details
{

	uint32_t 	 firmware_header;
	uint32_t	 code_loaded_address;
	uint32_t	 data_loaded_address;
	uint8_t		 code_load_area_memtype;
	uint8_t		 data_load_area_memtype;
	//uint8_t 	 dynamic_loading;
	uint8_t		 module_load_status;
	uint8_t		 module_id;
	uint32_t	 bin_size;
	uint32_t	 packet_count;
	//uint16_t	 reserv2;

}s_lut_module_details;


typedef struct boot_lut_table_def
{
	uint32_t 	  		 btable_bank_loc_flag;
	uint8_t 	 boot_selection;  // 4 bytes
	s_lut_control   	 table_control_data;   // 12 bytes
	s_lut_module_details software_module[MDOULE_MAXIMUM];   // 24*10 = 240
	uint32_t      		 boot_table_crc;   //
}s_boot_lut_table_def;   /// total size boot table 16  + 240 = 212








#endif /* INC_OTSU_TCTM_MSG*/
