{%- import 'slaplte.jinja2' as slaplte  with context %}

{#- for standalone testing via slapos-render-config.py
    NOTE: keep in sync with ru/libinstance.jinja2.cfg   #}
{%- if _standalone is defined %}
{%-   set cell_list = {}  %}
{%-   do slaplte.load_cell(cell_list) %}
{%- endif %}

{%- set cell_count = cell_list|length %}

{%- if slapparameter_dict.get('tdd_ul_dl_config', '[Configuration 2] 5ms 2UL 6DL (default)') == '[Configuration 2] 5ms 2UL 6DL (default)' %}
{%- set tdd_config = 2 %}
{%- elif slapparameter_dict.get('tdd_ul_dl_config', '[Configuration 2] 5ms 2UL 6DL (default)') == '[Configuration 6] 5ms 5UL 3DL (maximum uplink)' %}
{%- set tdd_config = 6 %}
{%- endif %}
{% if rf_mode == 'tdd' %}
#define TDD                 1
{% else %}
#define TDD                 0
{% endif %}
{%- if   slapparameter_dict.get('bandwidth', slap_configuration['configuration.default_lte_bandwidth']) == '1.4 MHz' %}
#define N_RB_DL             6
{%- elif slapparameter_dict.get('bandwidth', slap_configuration['configuration.default_lte_bandwidth']) == '3 MHz' %}
#define N_RB_DL             15
{%- elif slapparameter_dict.get('bandwidth', slap_configuration['configuration.default_lte_bandwidth']) == '5 MHz' %}
#define N_RB_DL             25
{%- elif slapparameter_dict.get('bandwidth', slap_configuration['configuration.default_lte_bandwidth']) == '10 MHz' %}
#define N_RB_DL             50
{%- elif slapparameter_dict.get('bandwidth', slap_configuration['configuration.default_lte_bandwidth']) == '15 MHz' %}
#define N_RB_DL             75
{%- elif slapparameter_dict.get('bandwidth', slap_configuration['configuration.default_lte_bandwidth']) == '20 MHz' %}
#define N_RB_DL             100
{%- endif %}
#define N_ANTENNA_DL        {{ slapparameter_dict.get('n_antenna_dl', slap_configuration['configuration.default_n_antenna_dl']) }}
#define N_ANTENNA_UL        {{ slapparameter_dict.get('n_antenna_ul', slap_configuration['configuration.default_n_antenna_ul']) }}

{
{% if slapparameter_dict.get('log_phy_debug', False) %}
  log_options: "all.level=error,all.max_size=0,nas.level=debug,nas.max_size=1,s1ap.level=debug,s1ap.max_size=1,x2ap.level=debug,x2ap.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,s1ap.level=debug,s1ap.max_size=1,x2ap.level=debug,x2ap.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'] }}/enb.log",

{% if ru == "lopcomm" %}
  rf_driver: {
{%- if slapparameter_dict.get('disable_sdr', False) %}
      name: "dummy",
{%- else %}
      name: "sdr",
{%- endif %}
        args: "dev0=
        {%- for i, k in enumerate(cell_list) %}
        {%- set cpri_port = cell_list[k].cpri_port_number %}
        {%- if i != 0 -%}
        ,
        {%- endif -%}
          /dev/sdr{{ slapparameter_dict.get('sdr_number', 0) }}@{{ cpri_port }}
        {%- endfor -%}
        ",
      cpri_mapping: "hw",
      {%- set cpri_mult = slapparameter_dict.get('cpri_mult', 16) %}
      cpri_mult: "
        {%- for i, k in enumerate(cell_list) %}
        {%- if i != 0 -%}
        ,
        {%- endif -%}
          {{ cpri_mult }}
        {%- endfor -%}
        ",
      cpri_rx_delay: "
        {%- for i, k in enumerate(cell_list) %}
        {%- if i != 0 -%}
        ,
        {%- endif -%}
          {{ cell_list[k].get('cpri_rx_delay', 0) }}
        {%- endfor -%}
        ",
      cpri_tx_delay: "
        {%- for i, k in enumerate(cell_list) %}
        {%- if i != 0 -%}
        ,
        {%- endif -%}
          {{ cell_list[k].get('cpri_tx_delay', 0) }}
        {%- endfor -%}
        ",
      cpri_tx_dbm: "
        {%- for i, k in enumerate(cell_list) %}
        {%- if i != 0 -%}
        ,
        {%- endif -%}
          {{ cell_list[k].get('cpri_tx_dbm', 0) }}
        {%- endfor -%}
        ",
      ifname: "{{ cell_list.values() | map(attribute='_tap') | join(',') }}",
  },
  tx_gain: 0,
  rx_gain: 0,
{% elif bbu == "ors" %}
  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 %}

  mme_list: [
{% if slapparameter_dict.get('mme_list', '') %}
{%- for i, k in enumerate(slapparameter_dict['mme_list']) %}
{%- if i == 0 %}
    {
{%- else -%}
    , {
{%- endif %}
         mme_addr: "{{ slapparameter_dict['mme_list'][k]['mme_addr'] }}",
    }
{%- endfor -%}
{% else %}
    {
      mme_addr: "{{ slap_configuration['configuration.mme_addr'] }}",
    },
{% endif %}
  ],
{% if slapparameter_dict.get('mme_list', '') %}
  {% if slapparameter_dict.get('gtp_addr') %}
    gtp_addr: "{{ slapparameter_dict.get('gtp_addr') }}",
  {% else %}
    {% if slapparameter_dict.get('use_ipv4', False) %}
      gtp_addr: "{{ gtp_addr_v6 }}",
    {% else %}
      gtp_addr: "{{ gtp_addr_v4 }}",
    {% endif %}
  {% endif %}
{% else %}
  gtp_addr: "{{ slap_configuration['configuration.gtp_addr'] }}",
{% endif %}

  enb_id: {{ slapparameter_dict.get('enb_id', '0x1A2D0') }},

  cell_list: [
    {%- for i, k in enumerate(cell_list) %}
    {%- if i == 0 -%}
    {
    {%- else -%}
    , {
    {%- endif %}
      rf_port: {{ i }},
      cell_id: {{ cell_list[k].get('cell_id', slapparameter_dict.get('cell_id', '0x0' + i|string)) }},
      tac: {{ cell_list[k].get('tac', slapparameter_dict.get('tac', '0x0001')) }},
      n_id_cell: {{ cell_list[k].get('pci', slapparameter_dict.get('pci', i)) }},
      root_sequence_index: {{ cell_list[k].get('root_sequence_index', slapparameter_dict.get('root_sequence_index', 204 + i)) }},
      dl_earfcn: {{ cell_list[k].get('dl_earfcn', slapparameter_dict.get('dl_earfcn', earfcn)) }},
      {%- if slapparameter_dict.get('ncell_list', '') %}
      ncell_list: [
      {%- for i, k in enumerate(slapparameter_dict['ncell_list']) %}
      {%- if i == 0 -%}
        {
      {%- else -%}
        , {
      {%- endif %}
          n_id_cell: {{ slapparameter_dict['ncell_list'][k].get('pci', '') }},
          dl_earfcn: {{ slapparameter_dict['ncell_list'][k].get('dl_earfcn', '') }},
          cell_id: {{ slapparameter_dict['ncell_list'][k].get('cell_id', '') }},
          tac: {{ slapparameter_dict['ncell_list'][k].get('tac', 1) }},
        }
      {%- endfor -%}
      ],
      {% endif %}
      {%- set scell_list = [] %}
      {%- for j, l in enumerate(cell_list) %}
      {%-   if j != i %}
      {%-     do scell_list.append([j, l]) %}
      {%-   endif %}
      {%- endfor %}
      scell_list: [
        {%- for j, l in enumerate(scell_list) %}
        {%- if j == 0 -%}
        {
        {%- else -%}
        , {
        {%- endif %}
          cell_id: {{ cell_list[l[1]].get('cell_id', '0x0' + l[0]|string) }},
          cross_carrier_scheduling: false,
        }
        {%- endfor %}
      ],
    }
    {%- endfor %}
  ],

  cell_default: {
    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'] }}",
      reserved: {{ str(slapparameter_dict['plmn_list'][k].get('reserved', false)).lower() }},
      attach_without_pdn: {{ str(slapparameter_dict['plmn_list'][k].get('attach_without_pdn', false)).lower() }},
      }
{%- endfor -%}
{% else %}
      "00101",
{% endif %}
    ],
    n_antenna_dl: N_ANTENNA_DL,
    n_antenna_ul: N_ANTENNA_UL,
{% if bbu == "ors" %}
    manual_ref_signal_power: true,
{% endif %}
#if TDD == 1
    uldl_config: {{ tdd_config }},
    sp_config: 7,
#endif
    n_rb_dl: N_RB_DL,
    cyclic_prefix: "normal",

    phich_duration: "normal",
    phich_resource: "1",

    si_value_tag: 0,
    cell_barred: false,
    intra_freq_reselection: true,
    q_rx_lev_min: -70,
    si_window_length: 40,
    sib_sched_list: [
      {
        filename: "{{ sib23_file }}",
        si_periodicity: 16,
      },
    ],

#if N_RB_DL == 6
    si_coderate: 0.30,
#else
    si_coderate: 0.20,
#endif
    si_pdcch_format: 2,

    n_symb_cch: 0,

    pdsch_dedicated: {
#if N_ANTENNA_DL == 4
      p_a: -6,
#elif N_ANTENNA_DL == 2
      p_a: -3,
#else
      p_a: 0,
#endif
      p_b: -1,
    },

#if N_RB_DL == 6
    pdcch_format: 1,
#else
    pdcch_format: 2,
#endif

#if N_RB_DL == 6
    prach_config_index: 15,
#else
    prach_config_index: 4,
#endif
    prach_freq_offset: -1,

    pucch_dedicated: {
      n1_pucch_sr_count: 11,
      cqi_pucch_n_rb: 1,
#if TDD == 1
      tdd_ack_nack_feedback_mode: "multiplexing", /* TDD only */
#endif
    },

    pusch_dedicated: {
      beta_offset_ack_index: 9,
      beta_offset_ri_index: 6,
      beta_offset_cqi_index: 6,
    },

    pusch_hopping_offset: -1,

    pusch_msg3_mcs: 0,

#if N_RB_DL == 6
    initial_cqi: 5,
#else
    initial_cqi: 3,
#endif

    dl_256qam: true,
    ul_64qam: true,

    sr_period: 20,

    cqi_period: 40,

#if N_ANTENNA_DL >= 2
    m_ri: 8,
    transmission_mode: 3,
#endif

    srs_dedicated: {
#if N_RB_DL == 6
      srs_bandwidth_config: 7,
      srs_bandwidth: 1,
#elif N_RB_DL == 15
      srs_bandwidth_config: 6,
      srs_bandwidth: 1,
#elif N_RB_DL == 25
      srs_bandwidth_config: 3,
      srs_bandwidth: 1,
#elif N_RB_DL == 50
      srs_bandwidth_config: 2,
      srs_bandwidth: 2,
#elif N_RB_DL == 75
      srs_bandwidth_config: 2,
      srs_bandwidth: 2,
#else
      srs_bandwidth_config: 2,
      srs_bandwidth: 3,
#endif
      srs_subframe_config: 3,
      srs_period: 40,
      srs_hopping_bandwidth: 0,
    },

    mac_config: {
       ul_max_harq_tx: 5,
       dl_max_harq_tx: 5,
    },

    pusch_max_its: 6,

    dpc: true,
    dpc_pusch_snr_target: 25,
    dpc_pucch_snr_target: 25,

    cipher_algo_pref: [],
    integ_algo_pref: [2, 1],

    inactivity_timer: {{ slapparameter_dict.get('inactivity_timer', slap_configuration['configuration.default_lte_inactivity_timer']) }},

    srb_config: [
      {
        id: 1,
        maxRetxThreshold: 32,
        t_Reordering: 45,
        t_PollRetransmit: 60,
      },
      {
        id: 2 ,
        maxRetxThreshold: 32,
        t_Reordering: 45,
        t_PollRetransmit: 60,
      }
    ],

    drb_config: "{{ drb_file }}",

  meas_config_desc: {
    a1_report_type: "rsrp",
    a1_rsrp: -70,
    a1_hysteresis: 0,
    a1_time_to_trigger: 640,
    a2_report_type: "rsrp",
    a2_rsrp: -80,
    a2_hysteresis: 0,
    a2_time_to_trigger: 640,
    a3_report_type: "rsrp",
    a3_offset: {{ slapparameter_dict.get('lte_handover_a3_offset', 6) }},
    a3_hysteresis: 0,
    a3_time_to_trigger: {{ slapparameter_dict.get('lte_handover_a3_time_to_trigger', 480) }},
  },
  meas_gap_config: "gp0",
  ho_from_meas: true,
  },
}
