#ifndef ICD_G346_H_
#define ICD_G346_H_

#include "th_b1553_icd.h"

namespace grifo_icd = icd1553;

#if 0
class IcdInfo
{
public:
  virtual unsigned int major_version() const =0;
  virtual unsigned int minor_version() const =0;
  virtual const char* name() const =0;
  virtual const char* pn() const =0;
};

extern IcdInfo& Icd_GetInfo();


#include "idd_types.h"
#include "stddef.h"

IDD_VERSION(grifo_icd_v, "G346", 1, 0, "IDDxxxxx-48", "IRSxxxxx-48");

namespace grifo_icd
{


#include "idd_pack16.h"


typedef unsigned short icd_raw_word_t;

#define ICD_STATIC_CHECK_SIZE_WORD(type_, word_size_) IDD_STATIC_CECK_SIZE(type_, (word_size_)*2)
#define ICD_STATIC_CHECK_OFFSET_WORD(struct_, member_, offset_) IDD_STATIC_CHECK_OFFSET(struct_, member_, 2*(offset_))

//A1: Radar Settings

typedef idd_bitfield_u16_t<IDD_REVBIT16(1), 2, unsigned int> tgt_history_level_field_t;
typedef idd_bitfield_u16_t<IDD_REVBIT16(8), 7, icd_raw_word_t> symb_intensity_field_t;
typedef idd_bitfield_u16_t<IDD_REVBIT16(9), 1, bool> ground_reject_vel_high_field_t;
typedef idd_bitfield_u16_t<IDD_REVBIT16(10), 1, bool> min_dectect_ground_radial_vel_high_field_t;
typedef idd_bitfield_u16_t<IDD_REVBIT16(11), 1, bool> ale_blanking_disable_field_t;

enum altitude_block_t
{
  BTBK,
  TPBK,
  NORMAL_BK
};

typedef idd_bitfield_u16_t<IDD_REVBIT16(13), 2, altitude_block_t> altitude_block_field_t;

enum prf_lookup_selection_t
{
  LPRF,
  MPRF_LOOKUP
};

typedef idd_bitfield_u16_t<IDD_REVBIT16(14), 1, bool> prf_lookup_selection_field_t;

typedef idd_bitfield_u16_t<IDD_REVBIT16(15), 1, bool> sett_spare15_field_t;

union rdr_settings_t
{
  tgt_history_level_field_t  history_level;
  symb_intensity_field_t sym_intensity;
  ground_reject_vel_high_field_t ground_reject_vel_high;
  min_dectect_ground_radial_vel_high_field_t min_dectect_ground_radial_vel_high;
  ale_blanking_disable_field_t ale_blanking_disable;
  altitude_block_field_t altitude_block;
  prf_lookup_selection_field_t prf_lookup_selection;

  sett_spare15_field_t sett_spare15;

  icd_raw_word_t raw;
};

typedef idd_bitfield_u16_t<IDD_REVBIT16(1), 2, unsigned int> frq_agility_field_t;
typedef idd_bitfield_u16_t<IDD_REVBIT16(2), 1, bool> frq_group1_field_t;
typedef idd_bitfield_u16_t<IDD_REVBIT16(3), 1, bool> frq_group2_field_t;
typedef idd_bitfield_u16_t<IDD_REVBIT16(4), 1, bool> frq_group3_field_t;
typedef idd_bitfield_u16_t<IDD_REVBIT16(5), 1, bool> frq_group4_field_t;
typedef idd_bitfield_u16_t<IDD_REVBIT16(6), 1, bool> frq_group5_field_t;
typedef idd_bitfield_u16_t<IDD_REVBIT16(12), 6, unsigned int> frq_channel_field_t;
typedef idd_bitfield_u16_t<IDD_REVBIT16(14), 2, unsigned int> frq_waveform_field_t;
typedef idd_bitfield_u16_t<IDD_REVBIT16(15), 1, bool> lprf_threshold_field_t;

union frequency_t {
  frq_agility_field_t agility;

  frq_group1_field_t g1;
  frq_group2_field_t g2;
  frq_group3_field_t g3;
  frq_group4_field_t g4;
  frq_group5_field_t g5;

  frq_channel_field_t channel;
  frq_waveform_field_t waveform;

  lprf_threshold_field_t lprf_threshold;

  icd_raw_word_t raw;
};

typedef idd_bitfield_u16_t<IDD_REVBIT16(15), 7, icd_raw_word_t> if_gain_field_t;

union gains_and_chgrp_t
{
  if_gain_field_t if_gain;

  icd_raw_word_t raw;
};

typedef idd_bitfield_u16_t<IDD_REVBIT16(0), 1, bool> param_enable_field_t;
typedef idd_bitfield_u16_t<IDD_REVBIT16(1), 1, bool> param_tx_field_t;
typedef idd_bitfield_u16_t<IDD_REVBIT16(9), 8, unsigned int> param_num_field_t;

union param_id_t
{
  param_enable_field_t enable;
  param_tx_field_t tx;
  param_num_field_t num;

  icd_raw_word_t raw;
};

struct msg_rdr_settings_and_parameters_t
{
  rdr_settings_t settings;

  frequency_t frequency;

  icd_raw_word_t beacon;

  gains_and_chgrp_t gains_and_chgrp;

  icd_raw_word_t ac_id;

  icd_raw_word_t date_of_mission;
  icd_raw_word_t time_of_mission;

  param_id_t param_id;
  icd_raw_word_t param_value_1;
  icd_raw_word_t param_value_2;
};

ICD_STATIC_CHECK_SIZE_WORD(msg_rdr_settings_and_parameters_t, 10);

//A2: Radar Mode and Operational Parameters

enum rdr_modes_t
{
  RWS=0,
  VS,
  ACM,
  TWS,
  MODE_SPARE_4=4,
  GM,
  SEA_LOW,
  SEA_HIGH,
  GMTI,
  BCN,
  AGR,
  TA,
  WA,
  MODE_SPARE_13,
  MODE_SPARE_14,
  RDR_DLY  //this is actualy only a tellabck, a not commandable mode
};

enum des_control_t
{
  LOCK_ON,
  LOCK_ON_DTT,
  TRANS_HPT_BUT,
  REJECT,
  LOCK_ON_STT,
  DES_TRACK_LABEL,
  SPARE_6=6,
  SAR_ON_SPOI,
  SAR_ON_CRS,
  SPARE_9,
  SINGLE_PT_REJECT,
  DES_SPARE_11,
  DES_SPARE_12,
  DES_SPARE_13,
  DES_SPARE_14,
  NOT_VALID
};

enum ibit_request_t
{
  NORMAL=0,
  INT_BIT=1
};

enum stby_selection_t
{
  STBY_OFF,
  STBY_ON
};

typedef bool freeze_selection_t;
typedef bool stop_powerup_selection_t;
typedef bool silence_selection_t;

typedef idd_bitfield_u16_t<IDD_REVBIT16(3) , 4, rdr_modes_t> rdr_mode_field_t;
typedef idd_bitfield_u16_t<IDD_REVBIT16(7) , 4, des_control_t> des_control_field_t;
typedef idd_bitfield_u16_t<IDD_REVBIT16(8) , 1, ibit_request_t> ibit_request_field_t;
typedef idd_bitfield_u16_t<IDD_REVBIT16(9) , 1, stby_selection_t> stby_field_t;
typedef idd_bitfield_u16_t<IDD_REVBIT16(10), 1, freeze_selection_t> freeze_field_t;
typedef idd_bitfield_u16_t<IDD_REVBIT16(11), 1, stop_powerup_selection_t> stop_powerup_field_t;
typedef idd_bitfield_u16_t<IDD_REVBIT16(12), 1, icd_raw_word_t> reserved12_field_t;
typedef idd_bitfield_u16_t<IDD_REVBIT16(13), 1, silence_selection_t> silence_field_t;
typedef idd_bitfield_u16_t<IDD_REVBIT16(14), 1, icd_raw_word_t> spare14_field_t;
typedef idd_bitfield_u16_t<IDD_REVBIT16(15), 1, icd_raw_word_t> spare15_field_t;


union rdr_mode_command_word_t {
  rdr_mode_field_t master_mode;
  des_control_field_t des_ctrl;
  stby_field_t stby;

  silence_field_t silence;

  icd_raw_word_t raw;
};


enum rws_submode_t
{
  RWS_NAM,
  RWS_ASM
};

typedef idd_bitfield_u16_t<IDD_REVBIT16(0), 1, rws_submode_t> rws_submode_field_t;

typedef bool spot_selection_t;

typedef idd_bitfield_u16_t<IDD_REVBIT16(1), 1, spot_selection_t> spot_selection_field_t;

enum acm_submode_t
{
  BORESIGHT,
  NARROW,
  HUD,
  WIDE,
  VERTICAL,
  SLEWABLE,
  ACM_SPARE_6,
  ACM_SPARE_7
};

typedef idd_bitfield_u16_t<IDD_REVBIT16(4), 3, acm_submode_t> acm_submode_field_t;

enum gm_submode_t
{
  GM_RBM,
  GM_DBS
};

typedef idd_bitfield_u16_t<IDD_REVBIT16(5), 1, gm_submode_t> gm_submode_field_t;

typedef idd_bitfield_u16_t<IDD_REVBIT16(6), 1, bool> expand_field_t;

typedef idd_bitfield_u16_t<IDD_REVBIT16(9), 3, bool> p1_spare7_field_t;

//typedef idd_bitfield_u16_t<IDD_REVBIT16(9), 2, icd_raw_word_t> old_range_scale_field_t;

enum bars_num_t
{
  BARS_1=0,
  BARS_2,
  BARS_SPARE,
  BARS_4
};

typedef idd_bitfield_u16_t<IDD_REVBIT16(11), 2, bars_num_t> bars_num_field_t;

enum scan_width_t
{
  W60,
  W30,
  W25,
  W15
};

typedef idd_bitfield_u16_t<IDD_REVBIT16(13), 2, scan_width_t> scan_width_field_t;

enum velocity_scale_t {
  KTS2400,
  KTS1200
};
typedef idd_bitfield_u16_t<IDD_REVBIT16(14), 1, velocity_scale_t> velocity_scale_field_t;

typedef idd_bitfield_u16_t<IDD_REVBIT16(15), 1, bool> p1_spare15_field_t;

union rdr_fun_and_param1_t {
  rws_submode_field_t rws_submode;
  spot_selection_field_t spot;
  acm_submode_field_t acm_submode;
  gm_submode_field_t gm_submode;
  expand_field_t expand;
  p1_spare7_field_t spare7;
  //old_range_scale_field_t old_range_scale;
  bars_num_field_t bars_num;
  scan_width_field_t scan_width;
  velocity_scale_field_t velocity_scale;
  p1_spare15_field_t spare15;

  icd_raw_word_t raw;
};


enum range_scale_t
{
  NM160,
  NM80,
  NM60,
  NM40,
  NM20,
  NM10,

  NM_NV_6,
  NM_NV_7,
  NM_NV_8,
  NM_NV_9,
  NM_NV_10,
  NM_NV_11,
  NM_NV_12,
  NM_NV_13,
  NM_NV_14,
  NM_NV_15
};


IDD_INT_TRAIT_DECLARATION(range_scale_trait_t, range_scale_t, NM160, NM_NV_15);

typedef idd_bitfield_u16_t<IDD_REVBIT16(3), 4, range_scale_t> range_scale_field_t;

enum zoom_t
{
  ZOOM_NOT_ATIVE,
  ZOOM_IN,
  ZOOM_OUT
};

typedef idd_bitfield_u16_t<IDD_REVBIT16(5), 2, zoom_t> zoom_field_t;

typedef idd_bitfield_u16_t<IDD_REVBIT16(15), 1, icd_raw_word_t> spare6_15_field_t;

union rdr_fun_and_param2_t {
  range_scale_field_t range_scale;
  zoom_field_t zoom;
  spare6_15_field_t spare;

  icd_raw_word_t raw;
};

struct msg_rdr_operation_command_t
{
  rdr_mode_command_word_t rdr_mode_command;
  rdr_fun_and_param1_t param1;
  rdr_fun_and_param2_t param2;
};

ICD_STATIC_CHECK_SIZE_WORD(msg_rdr_operation_command_t, 3);


//typedef icd_raw_word_t relative_timetag_t;
IDD_FIXEDPOINT_TRAIT_DECLARATION(icd_timetag_lsb, float, 0.064f, 0, 4000.0f, 0.064f, 0.064f);

typedef idd_fixedpoint<float, icd_raw_word_t, icd_timetag_lsb> relative_timetag_t;

IDD_FIXEDPOINT_TRAIT_LSB_DECLARATION(icd_velocity_lsb, float, 3.81470E-06f);

typedef idd_fixedpoint16x<float, int32_t, icd_velocity_lsb> velocity_t;

IDD_FIXEDPOINT_TRAIT_LSB_DECLARATION(icd_acceleration_lsb, float, 0.03125f);

typedef idd_fixedpoint<float, int16_t, icd_acceleration_lsb> acceleration_t;

IDD_FIXEDPOINT_TRAIT_LSB_DECLARATION(icd_semicircle_lsb, float, 180.0f/32767.0f);

typedef idd_fixedpoint<float, int16_t, icd_semicircle_lsb> semicircle_t;

typedef idd_bitfield_i16_t<IDD_REVBIT16(8), 9, int16_t> crs_motion_rate_field_t;
typedef idd_bitfield_u16_t<IDD_REVBIT16(9), 1, bool> crs_ghost_selector_field_t;
typedef idd_bitfield_u16_t<IDD_REVBIT16(9), 1, bool> crs_dtt_selector_field_t;
typedef idd_bitfield_u16_t<IDD_REVBIT16(9), 1, bool> crs_sar_selector_field_t;

typedef idd_bitfield_u16_t<IDD_REVBIT16(10), 1, bool> ant_slew_valid_field_t;
typedef idd_bitfield_u16_t<IDD_REVBIT16(11), 1, bool> crs_rate_valid_field_t;
typedef idd_bitfield_u16_t<IDD_REVBIT16(12), 1, bool> crs_slave_valid_field_t;
typedef idd_bitfield_u16_t<IDD_REVBIT16(13), 1, bool> crs_zero_valid_field_t;
typedef idd_bitfield_u16_t<IDD_REVBIT16(14), 1, bool> crs_snowplugh_valid_field_t;

union a4_validity_and_slew_t {
  ant_slew_valid_field_t ant_slew_valid;
  crs_rate_valid_field_t crs_rate_valid;
  crs_slave_valid_field_t crs_normal_slave_valid;
  crs_zero_valid_field_t crs_zero_valid_field;
  crs_snowplugh_valid_field_t crs_snowplugh_valid_field;
  icd_raw_word_t raw;
};

union crs_motion_x_t {
  crs_motion_rate_field_t rate;
  crs_ghost_selector_field_t ghost;
  crs_dtt_selector_field_t dtt;
  crs_sar_selector_field_t sar;

  icd_raw_word_t raw;
};

union crs_motion_y_t {
  crs_motion_rate_field_t rate;

  icd_raw_word_t raw;
};

struct msg_nav_data_and_cursor_t
{
  a4_validity_and_slew_t validity_and_slew;

  relative_timetag_t timetag;

  semicircle_t true_heading;
  semicircle_t magnetic_heading;

  acceleration_t x_acceleration;
  acceleration_t y_acceleration;
  acceleration_t z_acceleration;

  icd_raw_word_t true_air_speed;
  icd_raw_word_t calibrated_air_speed;

  icd_raw_word_t baro_altitude;
  icd_raw_word_t corrected_baro_altitude;
  icd_raw_word_t radio_altitude;

  icd_raw_word_t spoi_baro_altitude;

  icd_raw_word_t celarance_plane_distance;

  semicircle_t wind_direction;
  icd_raw_word_t wind_speed;

  semicircle_t az_antenna_demand;
  semicircle_t el_antenna_demand;

  crs_motion_x_t acq_crs_x;
  crs_motion_y_t acq_crs_y;

  icd_raw_word_t acq_crs_rng;
  icd_raw_word_t acq_crs_az;

  relative_timetag_t ppos_timetag;
  icd_raw_word_t ppos_lat_msw;
  icd_raw_word_t ppos_lat_lsw;
  icd_raw_word_t ppos_lon_msw;
  icd_raw_word_t ppos_lon_lsw;

  icd_raw_word_t spoi_lat_msw;
  icd_raw_word_t spoi_lat_lsw;
  icd_raw_word_t spoi_lon_msw;
  icd_raw_word_t spoi_lon_lsw;
};

ICD_STATIC_CHECK_SIZE_WORD(msg_nav_data_and_cursor_t, 31);

IDD_FIXEDPOINT_TRAIT_LSB_DECLARATION(attitude_rate_lsb, float, 180.0f/32767.0f);

typedef idd_fixedpoint<float, int16_t, attitude_rate_lsb> attitude_rate_t;

struct msg_inu_high_speed_t
{
  icd_raw_word_t mode_word;

  relative_timetag_t timetag;

  velocity_t x_vel;
  velocity_t y_vel;
  velocity_t z_vel;

  semicircle_t platform_azimuth;

  semicircle_t roll;
  semicircle_t pitch;

  attitude_rate_t roll_rate;
  attitude_rate_t pitch_rate;
  attitude_rate_t yaw_rate;

  icd_raw_word_t longitud_acc;
  icd_raw_word_t lateral_acc;
  icd_raw_word_t normal_acc;

  relative_timetag_t ptaz_tt;
  relative_timetag_t roll_tt;
  relative_timetag_t pitch_tt;

  icd_raw_word_t roll_ang_acc;
  icd_raw_word_t pitch_ang_acc;
  icd_raw_word_t yaw_ang_acc;
};

ICD_STATIC_CHECK_SIZE_WORD(msg_inu_high_speed_t, 23);

//Settings Tellback (B6)

typedef idd_bitfield_u16_t<IDD_REVBIT16(9), 10, uint16_t> crs_pixel_pos_field_t;

union crs_x_coord_t
{
  crs_pixel_pos_field_t pos;

  icd_raw_word_t raw;
};

union crs_y_coord_t
{
  crs_pixel_pos_field_t pos;

  icd_raw_word_t raw;
};

struct msg_settings_tellback_t
{
  icd_raw_word_t spare[13];

  semicircle_t az_scan_centre;
  semicircle_t el_scan_centre;

  crs_x_coord_t crs_x;
  crs_y_coord_t crs_y;

  icd_raw_word_t spare_end[23- (17)];
};

ICD_STATIC_CHECK_SIZE_WORD(msg_settings_tellback_t, 23);

//Mode Tellback (B7)

enum rdr_design_sts_t {
  DS_STT,
  DS_DTT,
  DS_SAM,
  DS_TWS_with_HPT,
  DS_AGR_Lock_On,
  DS_ACQUISITION_in_Progress,
  DS_spare_6,
  DS_SAR,
  DS_spare_8,
  DS_spare_9,
  DS_spare_10,
  DS_spare_11,
  DS_spare_12,
  DS_spare_13,
  DS_spare_14,
  DS_NOT_VALID
};

typedef idd_bitfield_u16_t<IDD_REVBIT16(7), 4, rdr_design_sts_t> rdr_design_sts_field_t;
//typedef idd_bitfield_u16_t<IDD_REVBIT16(8), 1, bool> ibit_status_field_t;
//typedef idd_bitfield_u16_t<IDD_REVBIT16(9), 1, bool> standby_tellback_field_t;
//typedef idd_bitfield_u16_t<IDD_REVBIT16(10), 1, bool> freeze_tellback_field_t;
typedef idd_bitfield_u16_t<IDD_REVBIT16(11), 1, bool> degraded_performance_field_t;
typedef idd_bitfield_u16_t<IDD_REVBIT16(13), 1, bool> rf_radiation_field_t;
typedef idd_bitfield_u16_t<IDD_REVBIT16(14), 1, bool> transition_status_field_t;
typedef idd_bitfield_u16_t<IDD_REVBIT16(15), 1, bool> last_acq_result_field_t;

union rdr_mode_tellback_t
{
  rdr_mode_field_t master_mode;
  rdr_design_sts_field_t design_sts;
  ibit_request_field_t ibit_sts;
  stby_field_t stby_sts;
  freeze_field_t freeze_sts;
  degraded_performance_field_t degrad_perf;
  rf_radiation_field_t rf_radiation;
  transition_status_field_t transition_status;
  last_acq_result_field_t last_acq_result;

  icd_raw_word_t raw;
};

enum scan_width_tb_t {
  TB_W60,
  TB_W55,
  TB_W45,
  TB_W40,
  TB_W35,
  TB_W30,
  TB_W25,
  TB_W20,
  TB_W15,
  TB_W10,
  TB_W05,
  TB_W0,
  TB_W_NV13,
  TB_W_NV14,
  TB_W_NV15
};

typedef idd_bitfield_u16_t<IDD_REVBIT16(15), 4, scan_width_tb_t> scan_width_tellback_field_t;

union rdr_fun_and_param1_tellback_t {
  rws_submode_field_t rws_submode;
  spot_selection_field_t spot;
  acm_submode_field_t acm_submode;
  gm_submode_field_t gm_submode;
  expand_field_t expand;
  p1_spare7_field_t spare7;
  //old_range_scale_field_t old_range_scale;
  bars_num_field_t bars_num;
  scan_width_tellback_field_t scan_width;
  p1_spare15_field_t spare15;

  icd_raw_word_t raw;
};

enum velocity_scale_tb_t
{
  VS_NM160,
  VS_NM80,
  VS_NM60,
  VS_NM40,

  VS_2400KTS,
  VS_1200KTS,

  VS_NV_6,
  VS_NV_7,
  VS_NV_8,
  VS_NV_9,
  VS_NV_10,
  VS_NV_11,
  VS_NV_12,
  VS_NV_13,
  VS_NV_14,
  VS_NV_15
};


IDD_INT_TRAIT_DECLARATION(velocity_scale_tb_trait_t, velocity_scale_tb_t, VS_NM160, VS_NV_15);

typedef idd_bitfield_u16_t<IDD_REVBIT16(3), 4, velocity_scale_tb_t> velocity_scale_tb_field_t;

struct msg_rdr_status_tellback_t
{
  rdr_mode_tellback_t mode;
  rdr_fun_and_param1_tellback_t param1;
  rdr_fun_and_param2_t param2;
};

ICD_STATIC_CHECK_SIZE_WORD(msg_rdr_status_tellback_t, 3);

//B09

struct msg_b09_t
{
  relative_timetag_t tt_low;
  relative_timetag_t tt_high;
  icd_raw_word_t scan_sts;
  semicircle_t nav_az;
  semicircle_t nav_el;
  semicircle_t body_az;
  semicircle_t body_el;

  icd_raw_word_t spare[32-7];
};

ICD_STATIC_CHECK_SIZE_WORD(msg_b09_t, 32);

//B10

struct msg_b10_t
{
  icd_raw_word_t mode;
  relative_timetag_t tt_low;
  relative_timetag_t tt_high;

  icd_raw_word_t spare[32-3];
};

ICD_STATIC_CHECK_SIZE_WORD(msg_b10_t, 32);

#include "idd_unpack.h"

#define ICD_MSGLEN(s_)  (sizeof(s_)/sizeof(icd_raw_word_t))

//With MinGW offsetof() genertae a worning ot to the dummy pointer ot 0!
//#define ICD_WOFFSET(s_, f_)  ((((unsigned int)&(((s_*)0x10)->f_))-0x10)/sizeof(icd_raw_word_t))
#define ICD_WOFFSETOF(s_, f_) (IDD_OFFSETOF(s_, f_)/sizeof(icd_raw_word_t))

} //namespace grifo_icd
#endif

#endif/*ICD_G346_H_*/
