/**
 * @file ft_sender.h
 *
 * @brief This file contains function prototypes of TCTM message type defines
 *
 * @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_OTSU_TCTM_MSG_DEF_H_
#define INC_OTSU_TCTM_MSG_DEF_H_


#include "stdint.h"


/*
 * @brief OTSU memory types
 */
typedef enum otsu_mem_type_def
{
  otsu_ftm_dwnld_mem =0,
  otsu_storage_mem,
  otsu_execution_mem,
  otsu_previous_sft_mem,
  otsu_factory_sft_mem,
  otsu_sft_integ_test,
  otsu_mem_none
} e_otsu_mem_type_def;


  /*
   * @brief Reboot control option
   */
  typedef enum e_boot_cotrol_options_def
  {
     BOOT_CONTROL_UNDEFINED 				= 0,
     BOOT_CONTROL_NOW      				    = 1,
     BOOT_CONTROL_POSTPONED 				= 2,
     BOOT_CONTROL_SCHEDULED  				= 3,
	 BOOT_CONTROL_FAIL_VERSION_MISMATCH	    = 4,
	 BOOT_CONTROL_FAIL_DWNLD_FW_CORRUPTD	= 5,
	 BOOT_CONTROL_FW_UPDATE_SUCCESS         = 6,
	 BOOT_CONTROL_FW_WAIT_GS_CONTACT        = 7,
	 BOOT_CONTROL_FW_VALIDATION_PASS        = 8,
	 BOOT_CONTROL_RESTORED_PREVIOUS_FW      = 9,
	 BOOT_CONTROL_FW_DWNLD                  = 10,
	 BOOT_CONTROL_NVM_FW_INTEGRITY_FAIL		= 11
   }e_boot_cotrol_options;


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


/*
 * @brief  Time def
 */
typedef struct s_time_set_type
{
	uint8_t hours;
	uint8_t min;
	uint8_t sec;
	uint8_t unused;
}s_time_set_def;


/*
 * @brief  Date def
 */
typedef struct s_date_set_type
{
	uint8_t day;
	uint8_t month;
	uint16_t year;
}s_date_set_def;




/*
 * @brief otsu_tc_firmware_update_config   size 24 bytes  >> 192 bits to be saved in NVM
 */
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_tc_msg_id
{
  otsu_tc_none 					  = 0,
  otsu_tc_software_vers 		  = 1,
  otsu_tc_firmware_status 		  = 2,
  otsu_tc_memory_layout 		  = 3,
  otsu_tc_firmware_update_config  = 4,
  otsu_tc_shutdwon_timeout_config = 5,
  otsu_tc_sft_integ_test	      = 6,
  otsu_tc_get_fw_status 		  = 7,
  otsu_tc_get_boot_config	      = 8,
  otsu_tc_set_gscont_waitime	  = 9,
  otsu_tc_get_gscont_waitime      = 10,
  otsu_tc_get_blt1                = 11,
  otsu_tc_get_blt2                = 12,
  otsu_tc_nvm_software_vers 	  = 13,

} e_otsu_tc_msg_id;


/*
 * @brief OTSU integertaion test commands
 */
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;


/*
 * @brief Telecommand message frame details
 */
typedef struct otsu_tc_encode_def_
{
  e_otsu_tc_msg_id otsu_msg_id;
  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;


/*
 * Frame encoding buffer pointer and length
 */
typedef struct otsu_tctm_info
{
	uint8_t *tctm_ptr;
	uint16_t tctm_size;
}
s_otsu_tctm_info;




/*
 * @brief Integration test frame
 */
typedef struct otsu_tc_integ_test_def
{
  uint8_t integ_test_cmd;
}s_otsu_tc_integ_test_def;

/*
 * @brief Firmware configuration params
 */
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;


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





typedef enum
{
	otsu_notify_flash_copy_fail,
	otsu_notify_ready_for_boot,
	otsu_notify_ack,
	otsu_notify_nack,
	otsu_notify_boot_pass,
	otsu_notify_boot_fail,
	otsu_notify_boot_initiated,
}otsu_niotification_response_code_def;
/*
 *  @brief 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;


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




/*
 * @brief FRAME: OTSU TM Info Frame - OTFS
 *
 */
typedef struct otsu_firmware_sts_def
{
	u_otfs_ctrl ctrl; 		/* control field 	*/
	uint8_t otsu_status;    /* current otsu state */
	uint16_t reserved;  /* unused */
}otsu_firmware_sts;



#endif /* INC_OTSU_TCTM_MSG_DEF_H_ */


