{%- if slapparameter_dict.get('tdd_ul_dl_config', '5ms 2UL 7DL 4/6 (default)') == '5ms 2UL 7DL 4/6 (default)' %}
{%- set tdd_config = 1 %}
{%- elif slapparameter_dict.get('tdd_ul_dl_config', '5ms 2UL 7DL 4/6 (default)') == '2.5ms 1UL 3DL 2/10' %}
{%- set tdd_config = 2 %}
{%- elif slapparameter_dict.get('tdd_ul_dl_config', '5ms 2UL 7DL 4/6 (default)') == '5ms 8UL 1DL 2/10 (maximum uplink)' %}
{%- set tdd_config = 3 %}
{%- endif %}

#define N_ANTENNA_DL         {{ slapparameter_dict.get('n_antenna_dl', slap_configuration['configuration.default_n_antenna_dl']) }}
{% if ru == "m2ru" %}
#define N_ANTENNA_UL         {{ slapparameter_dict.get('n_antenna_ul', 1) }}
{% else %}
#define N_ANTENNA_UL         {{ slapparameter_dict.get('n_antenna_ul', slap_configuration['configuration.default_n_antenna_dl']) }}
{% endif %}
{
{% if slapparameter_dict.get('log_phy_debug', False) %}
  log_options: "all.level=error,all.max_size=0,nas.level=debug,nas.max_size=1,ngap.level=debug,ngap.max_size=1,xnap.level=debug,xnap.max_size=1,rrc.level=debug,rrc.max_size=1,phy.level=debug,file.rotate=1G,file.path=/dev/null",
{% else %}
  log_options: "all.level=error,all.max_size=0,nas.level=debug,nas.max_size=1,ngap.level=debug,ngap.max_size=1,xnap.level=debug,xnap.max_size=1,rrc.level=debug,rrc.max_size=1,phy.level=info,file.rotate=1G,file.path=/dev/null",
{% endif %}
  log_filename: "{{ directory['log'] }}/gnb.log",
{% if ru == "m2ru" %}
  rf_driver: {
{%- if slapparameter_dict.get('disable_sdr', False) %}
      name: "dummy",
{%- else %}
      name: "sdr",
{%- endif %}
      args: "dev0=/dev/sdr0@0",
      cpri_mapping: "bf1",
      cpri_mult: 16,
      cpri_rx_delay: 11.0,
      cpri_tx_delay: 0,
      ifname: "cpri0",
      cpri_tx_dbm: 42.0,
  },
  tx_gain: 0,
  rx_gain: 0,
{% else %}
  rf_driver: {
{%- if slapparameter_dict.get('disable_sdr', False) %}
      name: "dummy",
{%- else %}
      name: "sdr",
{%- endif %}
      args: "dev0=/dev/sdr0",
{% if slapparameter_dict.get('gps_sync', False) %}
      sync: "gps",
{% endif %}
      rx_antenna: "tx_rx",
      tdd_tx_mod: 1,
  },
  tx_gain: {{ tx_gain }},
  rx_gain: {{ rx_gain }},
{% endif %}
  {%- if slapparameter_dict.get('websocket_password', '') %}
  com_addr: "[{{ gtp_addr_v6 }}]:{{ slap_configuration['configuration.com_ws_port'] }}",
  com_auth: {
    password: "{{ slapparameter_dict['websocket_password'] }}",
  },
  {%- else %}
  com_addr: "{{ slap_configuration['configuration.com_addr'] }}:{{ slap_configuration['configuration.com_ws_port'] }}",
  {%- endif %}
  amf_list: [
{% if slapparameter_dict.get('amf_list', '') %}
{%- for i, k in enumerate(slapparameter_dict['amf_list']) %}
{%- if i == 0 %}
    {
{%- else -%}
    , {
{%- endif %}
         amf_addr: "{{ slapparameter_dict['amf_list'][k]['amf_addr'] }}",
    }
{%- endfor -%}
{% else %}
    {
      amf_addr: "{{ slap_configuration['configuration.amf_addr'] }}",
    },
{% endif %}
  ],

{% if slapparameter_dict.get('xn_peers', '') %}
  xn_peers: [
{%- for k in slapparameter_dict['xn_peers'] -%}
    "{{ slapparameter_dict['xn_peers'][k]['xn_addr'] }}",
{%- endfor -%}
  ],
{% endif %}

{% if slapparameter_dict.get('amf_list', '') %}
{%   if slapparameter_dict.get('use_ipv4', False) %}
  gtp_addr: "{{ gtp_addr_v4 }}",
{%   else %}
  gtp_addr: "{{ gtp_addr_v6 }}",
{%   endif %}
{% else %}
  gtp_addr: "{{ slap_configuration['configuration.gtp_addr'] }}",
{% endif %}

  gnb_id_bits: {{ slapparameter_dict.get('gnb_id_bits', 28) }},
  gnb_id: {{ slapparameter_dict.get('gnb_id', '0x12345') }},
  en_dc_support: true,
  cell_list: [],
  nr_cell_list: [
  {
    rf_port: 0,
    cell_id: {{ slapparameter_dict.get('cell_id', '0x01') }},
    band: {{ nr_band }},
    dl_nr_arfcn: {{ nr_arfcn }},
    subcarrier_spacing: 30,
    ssb_pos_bitmap: "{{ slapparameter_dict.get('ssb_pos_bitmap', slap_configuration['configuration.default_nr_ssb_pos_bitmap']) }}",
{%- if slapparameter_dict.get('ncell_list', '') %}
    ncell_list: [
{%- for i, k in enumerate(slapparameter_dict['ncell_list']) %}
{%- if i == 0 -%}
      {
{%- else -%}
      , {
{%- endif %}
        rat: "nr",
        dl_nr_arfcn: {{ slapparameter_dict['ncell_list'][k].get('dl_nr_arfcn', '') }},
        ssb_nr_arfcn: {{ slapparameter_dict['ncell_list'][k].get('ssb_nr_arfcn', '') }},
        ul_nr_arfcn: {{ slapparameter_dict['ncell_list'][k].get('dl_nr_arfcn', '') }},
        n_id_cell: {{ slapparameter_dict['ncell_list'][k].get('pci', '') }},
        gnb_id_bits: {{ slapparameter_dict['ncell_list'][k].get('gnb_id_bits', '') }},
        nr_cell_id: {{ slapparameter_dict['ncell_list'][k].get('nr_cell_id', '') }},
        tac: {{ slapparameter_dict['ncell_list'][k].get('tac', 1) }},
        band: {{ slapparameter_dict['ncell_list'][k].get('nr_band', '') }},
        ssb_subcarrier_spacing: 30,
        ssb_period: 20,
        ssb_offset: 0,
        ssb_duration: 1,
      }
{%- endfor -%}
    ],
{% endif %}
  },
  ],
  nr_cell_default: {
    bandwidth: {{ slapparameter_dict.get('nr_bandwidth', slap_configuration['configuration.default_nr_bandwidth']) }},
    n_antenna_dl: N_ANTENNA_DL,
    n_antenna_ul: N_ANTENNA_UL,
{%- if bbu == "ors" %}
    manual_ref_signal_power: true,
{%- if one_watt == "True" %}
    ss_pbch_block_power: {{ (tx_gain | int)  - 54 }},
{%- else %}
    ss_pbch_block_power: {{ (tx_gain | int) - 35 }},
{%- endif -%}
{%- endif %}
{% if tdd_config == 1 %}
    tdd_ul_dl_config: {
      pattern1: {
        period: 5,
        dl_slots: 7,
        dl_symbols: 6,
        ul_slots: 2,
        ul_symbols: 4,
      },
    },
{% elif tdd_config == 2 %}
    tdd_ul_dl_config: {
      pattern1: {
        period: 2.5,
        dl_slots: 3,
        dl_symbols: 10,
        ul_slots: 1,
        ul_symbols: 2,
      },
    },
{% elif tdd_config == 3 %}
    tdd_ul_dl_config: {
      pattern1: {
         period: 5, /* in ms */
         dl_slots: 1,
         dl_symbols: 10,
         ul_slots: 8,
         ul_symbols: 2,
      },
    },
{% endif %}
    ssb_period: 20,
    n_id_cell: {{ slapparameter_dict.get('pci', 500) }},
  plmn_list: [
{%- if slapparameter_dict.get('plmn_list', '') %}
{%- for i, k in enumerate(slapparameter_dict['plmn_list']) %}
{%- if i == 0 -%}
      {
{%- else -%}
      , {
{%- endif %}
      plmn: "{{ slapparameter_dict['plmn_list'][k]['plmn'] }}",
      tac: {{ slapparameter_dict['plmn_list'][k].get('tac', 100) }},
{%- if slapparameter_dict['plmn_list'][k].get('ranac', '') %}
      ranac: {{ slapparameter_dict['plmn_list'][k]['ranac'] }},
{%- endif %}
      reserved: {{ str(slapparameter_dict['plmn_list'][k].get('reserved', false)).lower() }},
      nssai: [
{%- if slapparameter_dict.get('nssai', '') %}
{%- for j, k in enumerate(slapparameter_dict['nssai']) %}
{%- if j == 0 %}
        {
{%- else -%}
        , {
{%- endif %}
         sst: {{ slapparameter_dict['nssai'][k]['sst'] }},
{%- if slapparameter_dict['nssai'][k].get('sd', '') %}
         sd: {{ slapparameter_dict['nssai'][k]['sd'] }},
{%- endif %}
        }
{%- endfor -%}
{% else %}
        {
         sst: 1,
        },
{% endif %}
      ],
      }
{%- endfor -%}
{% else %}
      {
      plmn: "00101",
      tac: 100,
      reserved: false,
      nssai: [
{%- if slapparameter_dict.get('nssai', '') %}
{%- for j, k in enumerate(slapparameter_dict['nssai']) %}
{%- if j == 0 %}
        {
{%- else -%}
        , {
{%- endif %}
         sst: {{ slapparameter_dict['nssai'][k]['sst'] }},
{%- if slapparameter_dict['nssai'][k].get('sd', '') %}
         sd: {{ slapparameter_dict['nssai'][k]['sd'] }},
{%- endif %}
        }
{%- endfor -%}
{% else %}
        {
         sst: 1,
        },
{% endif %}
       ],
      },
{%- endif %}
    ],
    si_window_length: 40,
    cell_barred: false,
    intra_freq_reselection: true,
    q_rx_lev_min: -70,
    q_qual_min: -20,
    root_sequence_index: 1,
    sr_period: 40,
    dmrs_type_a_pos: 2,
    prach: {
      prach_config_index: 160,
      msg1_subcarrier_spacing: 30,
      msg1_fdm: 1,
{% if ru == "m2ru" %}
      msg1_frequency_start: 0,
{% else %}
      msg1_frequency_start: -1,
{% endif %}
      zero_correlation_zone_config: 15,
      preamble_received_target_power: -110,
      preamble_trans_max: 7,
      power_ramping_step: 4,
      ra_response_window: 20,
      restricted_set_config: "unrestricted_set",
      ra_contention_resolution_timer: 64,
      ssb_per_prach_occasion: 1,
      cb_preambles_per_ssb: 8,
    },
    pdcch: {
{% if ru == "m2ru" %}
      n_rb_coreset0: 48,
      n_symb_coreset0: 1,
{% endif %}
      search_space0_index: 0,
      dedicated_coreset: {
        rb_start: -1,
        l_crb: -1,
{% if ru == "m2ru" %}
        duration: 1,
{% else %}
        duration: 0,
{% endif %}
        precoder_granularity: "sameAsREG_bundle",
      },
      css: {
        n_candidates: [ 0, 0, 4, 0, 0 ],
      },
      rar_al_index: 2,
      si_al_index: 2,
{% if tdd_config == 3 %}
      uss: {
       n_candidates: [ 0, 8, 1, 0, 0 ],
       dci_0_1_and_1_1: true,
      },
{% else %}
      uss: {
        n_candidates: [ 0, 2, 1, 0, 0 ],
        dci_0_1_and_1_1: true,
      },
{% endif %}
      al_index: 1,
    },
    pdsch: {
      mapping_type: "typeA",
      dmrs_add_pos: 1,
      dmrs_type: 1,
      dmrs_max_len: 1,
{% if ru == "m2ru" %}
      k0: 0,
      k1: [ 8, 7, 7, 6, 5, 4, 12, 11 ],
{% elif tdd_config == 3 %}
      k1: [4, 11], 
{% endif %}
      mcs_table: "qam256",
      rar_mcs: 2,
      si_mcs: 6,
    },
    csi_rs: {
      nzp_csi_rs_resource: [
        {
          csi_rs_id: 0,
#if N_ANTENNA_DL == 1
          n_ports: 1,
          frequency_domain_allocation: "row2",
          bitmap: "100000000000",
          cdm_type: "no_cdm",
#elif N_ANTENNA_DL == 2
          n_ports: 2,
          frequency_domain_allocation: "other",
          bitmap: "100000",
          cdm_type: "fd_cdm2",
#elif N_ANTENNA_DL == 4
          n_ports: 4,
          frequency_domain_allocation: "row4",
          bitmap: "100",
          cdm_type: "fd_cdm2",
#elif N_ANTENNA_DL == 8
          n_ports: 8,
          frequency_domain_allocation: "other",
          bitmap: "110011",
          cdm_type: "fd_cdm2",
#else
#error unsupported number of DL antennas
#endif
          density: 1,
          first_symb: 4,
          rb_start: 0,
          l_crb: -1,
          power_control_offset: 0,
          power_control_offset_ss: 0,
          period: 80,
          offset: 1,
          qcl_info_periodic_csi_rs: 0,
        },
{% if tdd_config != 3 %}
        {
          csi_rs_id: 1,
          n_ports: 1,
          frequency_domain_allocation: "row1",
          bitmap: "0001",
          cdm_type: "no_cdm",
          density: 3,
          first_symb: 4,
          rb_start: 0,
          l_crb: -1,
          power_control_offset: 0,
          power_control_offset_ss: 0,
          period: 40,
          offset: 11,
          qcl_info_periodic_csi_rs: 0,
        },
        {
          csi_rs_id: 2,
          n_ports: 1,
          frequency_domain_allocation: "row1",
          bitmap: "0001",
          cdm_type: "no_cdm",
          density: 3,
          first_symb: 8,
          rb_start: 0,
          l_crb: -1,
          power_control_offset: 0,
          power_control_offset_ss: 0,
          period: 40,
          offset: 11,
          qcl_info_periodic_csi_rs: 0,
        },
        {
          csi_rs_id: 3,
          n_ports: 1,
          frequency_domain_allocation: "row1",
          bitmap: "0001",
          cdm_type: "no_cdm",
          density: 3,
          first_symb: 4,
          rb_start: 0,
          l_crb: -1,
          power_control_offset: 0,
          power_control_offset_ss: 0,
          period: 40,
          offset: 12,
          qcl_info_periodic_csi_rs: 0,
        },
        {
          csi_rs_id: 4,
          n_ports: 1,
          frequency_domain_allocation: "row1",
          bitmap: "0001",
          cdm_type: "no_cdm",
          density: 3,
          first_symb: 8,
          rb_start: 0,
          l_crb: -1,
          power_control_offset: 0,
          power_control_offset_ss: 0,
          period: 40,
          offset: 12,
          qcl_info_periodic_csi_rs: 0,
        },
{% endif %}
      ],
      nzp_csi_rs_resource_set: [
        {
          csi_rs_set_id: 0,
          nzp_csi_rs_resources: [ 0 ],
          repetition: false,
        },
{% if tdd_config != 3 %}
        {
          csi_rs_set_id: 1,
          nzp_csi_rs_resources: [ 1, 2, 3, 4 ],
          repetition: false,
          trs_info: true,
        },
{% endif %}
      ],
     csi_im_resource: [
        {
          csi_im_id: 0,
          pattern: 1,
          subcarrier_location: 8,
          symbol_location: 8,
          rb_start: 0,
          l_crb: -1,
          period: 80,
          offset: 1,
        },
      ],
      csi_im_resource_set: [
        {
          csi_im_set_id: 0,
          csi_im_resources: [ 0 ],
        }
      ],
      zp_csi_rs_resource: [
        {
          csi_rs_id: 0,
          frequency_domain_allocation: "row4",
          bitmap: "100",
          n_ports: 4,
          cdm_type: "fd_cdm2",
          first_symb: 8,
          density: 1,
          rb_start: 0,
          l_crb: -1,
          period: 80,
          offset: 1,
        },
      ],
      p_zp_csi_rs_resource_set: [
        {
          zp_csi_rs_resources: [ 0 ],
        },
      ],
      csi_resource_config: [
        {
          csi_rsc_config_id: 0,
          nzp_csi_rs_resource_set_list: [ 0 ],
          resource_type: "periodic",
        },
        {
          csi_rsc_config_id: 1,
          csi_im_resource_set_list: [ 0 ],
          resource_type: "periodic",
        },
{% if tdd_config != 3 %}
        {
          csi_rsc_config_id: 2,
          nzp_csi_rs_resource_set_list: [ 1 ],
          resource_type: "periodic",
        },
{% endif %}
      ],
      csi_report_config: [
        {
          resources_for_channel_measurement: 0,
          csi_im_resources_for_interference: 1,
          report_config_type: "periodic",
          period: 80,
          report_quantity: "CRI_RI_PMI_CQI",
#if N_ANTENNA_DL > 1
          codebook_config: {
            codebook_type: "type1",
            sub_type: "typeI_SinglePanel",
#if N_ANTENNA_DL == 2
#elif N_ANTENNA_DL == 4
            n1: 2,
            n2: 1,
            codebook_mode: 1,
#elif N_ANTENNA_DL == 8
            n1: 4,
            n2: 1,
            codebook_mode: 1,
#endif
          },
#endif
          cqi_table: 2,
          subband_size: "value1",
        },
      ],
    },
    pucch: {
      dpc_snr_target: 25,
      pucch_group_hopping: "neither",
      hopping_id: -1,
      p0_nominal: -90,
        pucch1: {
          n_cs: 3,
          n_occ: 3,
          freq_hopping: true,
        },
        pucch2: {
          n_symb: 2,
          n_prb: 1,
          freq_hopping: true,
          simultaneous_harq_ack_csi: false,
          max_code_rate: 0.25,
        },
    },
    pusch: {
      dpc_snr_target: 25,
      mapping_type: "typeA",
      n_symb: 14,
      dmrs_add_pos: 1,
      dmrs_type: 1,
      dmrs_max_len: 1,
      tf_precoding: false,
      mcs_table: "qam256",
      mcs_table_tp: "qam256",
      ldpc_max_its: 5,
{% if ru == "m2ru" %}
      k2: 4,
      msg3_k2: 7,
{% elif tdd_config == 3 %}
      k2: [11, 12, 4, 5, 6, 7, 7, 8], 
      msg3_k2: 7,
{% endif %}
      p0_nominal_with_grant: -84,
      msg3_mcs: 4,
      msg3_delta_power: 0,
      beta_offset_ack_index: 9,
    },
    mac_config: {
      msg3_max_harq_tx: 5,
      ul_max_harq_tx: 5,
      dl_max_harq_tx: 5,
      ul_max_consecutive_retx: 30,
      dl_max_consecutive_retx: 30,
      periodic_bsr_timer: 20,
      retx_bsr_timer: 320,
      periodic_phr_timer: 500,
      prohibit_phr_timer: 200,
      phr_tx_power_factor_change: "dB3",
      sr_prohibit_timer: 0,
      sr_trans_max: 64,
    },
    cipher_algo_pref: [],
    integ_algo_pref: [2, 1],
    inactivity_timer: {{ slapparameter_dict.get('inactivity_timer', slap_configuration['configuration.default_nr_inactivity_timer']) }},
    drb_config: "{{ drb_file }}",

  meas_config_desc: {
    a1_report_type: "rsrp",
    a1_rsrp: -60,
    a1_hysteresis: 10,
    a1_time_to_trigger: 100,
    a2_report_type: "rsrp",
    a2_rsrp: -70,
    a2_hysteresis: 0,
    a2_time_to_trigger: 100,
    a3_report_type: "rsrp",
    a3_offset: {{ slapparameter_dict.get('nr_handover_a3_offset', 6) }},
    a3_hysteresis: 0,
    a3_time_to_trigger: {{ slapparameter_dict.get('nr_handover_time_to_trigger', 100) }},
    ssb_rsrq_filter_coeff: 3,
    ssb_sinr_filter_coeff: 5
  },
  meas_gap_config: {
    pattern_id: 0
  },


  },
}
