main-ocp.c 50.5 KB
Newer Older
laurent's avatar
laurent committed
1
/*
laurent's avatar
laurent committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
* Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The OpenAirInterface Software Alliance licenses this file to You under
* the OAI Public License, Version 1.1  (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.openairinterface.org/?page_id=698
*
* Author and copyright: Laurent Thomas, open-cells.com
*
* 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.
*-------------------------------------------------------------------------------
* For more information about the OpenAirInterface (OAI) Software Alliance:
*      contact@openairinterface.org
*/


/*
laurent's avatar
laurent committed
26
 * This file replaces
laurent's avatar
laurent committed
27 28
 * targets/RT/USER/lte-softmodem.c
 * targets/RT/USER/rt_wrapper.c
laurent's avatar
laurent committed
29 30
 * targets/RT/USER/lte-ru.c
 * targets/RT/USER/lte-enb.c
laurent's avatar
laurent committed
31
 * targets/RT/USER/ru_control.c
laurent's avatar
laurent committed
32 33 34 35
 * openair1/SCHED/prach_procedures.c
 * The merger of OpenAir central code to this branch
 * should check if these 3 files are modified and analyze if code code has to be copied in here
 */
36 37
#define  _GNU_SOURCE
#include <pthread.h>
laurent's avatar
laurent committed
38

laurent's avatar
laurent committed
39
#include <common/utils/LOG/log.h>
laurent's avatar
laurent committed
40
#include <common/utils/system.h>
laurent's avatar
laurent committed
41
#include <common/utils/assertions.h>
laurent's avatar
laurent committed
42 43 44 45 46 47 48 49 50 51
static int DEFBANDS[] = {7};
static int DEFENBS[] = {0};
#include <common/config/config_userapi.h>
#include <targets/RT/USER/lte-softmodem.h>
#include <openair1/PHY/defs_eNB.h>
#include <openair1/PHY/phy_extern.h>
#include <nfapi/oai_integration/vendor_ext.h>
#include <openair1/SCHED/fapi_l1.h>
#include <openair1/PHY/INIT/phy_init.h>
#include <openair2/LAYER2/MAC/mac_extern.h>
laurent's avatar
laurent committed
52
#include <openair1/PHY/LTE_REFSIG/lte_refsig.h>
laurent's avatar
laurent committed
53
#include <nfapi/oai_integration/nfapi_pnf.h>
laurent's avatar
laurent committed
54
#include <executables/split_headers.h>
laurent's avatar
laurent committed
55
#include <common/utils/threadPool/thread-pool.h>
laurent's avatar
laurent committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
#include <openair2/ENB_APP/NB_IoT_interface.h>
#include <common/utils/load_module_shlib.h>
#include <targets/COMMON/create_tasks.h>
#include <openair1/PHY/TOOLS/phy_scope_interface.h>
#include <openair2/UTIL/OPT/opt.h>
#include <openair1/SIMULATION/TOOLS/sim.h>
#include <openair1/PHY/phy_vars.h>
#include <openair1/SCHED/sched_common_vars.h>
#include <openair2/LAYER2/MAC/mac_vars.h>
#include <openair2/RRC/LTE/rrc_vars.h>

pthread_cond_t nfapi_sync_cond;
pthread_mutex_t nfapi_sync_mutex;
int nfapi_sync_var=-1; //!< protected by mutex \ref nfapi_sync_mutex
pthread_cond_t sync_cond;
pthread_mutex_t sync_mutex;
int sync_var=-1; //!< protected by mutex \ref sync_mutex.
int config_sync_var=-1;
volatile int oai_exit = 0;
double cpuf;
76 77
THREAD_STRUCT thread_struct;

laurent's avatar
laurent committed
78
uint16_t sf_ahead=4;
Mahesh's avatar
Mahesh committed
79
//uint16_t slot_ahead=6;
laurent's avatar
laurent committed
80
int otg_enabled;
laurent's avatar
laurent committed
81
uint64_t  downlink_frequency[MAX_NUM_CCs][4];
laurent's avatar
laurent committed
82 83
int32_t   uplink_frequency_offset[MAX_NUM_CCs][4];
int split73;
Laurent Thomas's avatar
Laurent Thomas committed
84
char *split73_config;
laurent's avatar
laurent committed
85
int split73;
86 87 88 89
AGENT_RRC_xface *agent_rrc_xface[NUM_MAX_ENB]= {0};
AGENT_MAC_xface *agent_mac_xface[NUM_MAX_ENB]= {0};
void flexran_agent_slice_update(mid_t module_idP) {
}
Laurent Thomas's avatar
Laurent Thomas committed
90 91
int proto_agent_start(mod_id_t mod_id, const cudu_params_t *p) {
  return 0;
92
}
Laurent Thomas's avatar
Laurent Thomas committed
93
void proto_agent_stop(mod_id_t mod_id) {
94
}
laurent's avatar
laurent committed
95 96 97 98 99 100 101 102 103 104

static void *ru_thread( void *param );
void kill_RU_proc(RU_t *ru) {
}
void kill_eNB_proc(int inst) {
}
void free_transport(PHY_VARS_eNB *eNB) {
}
void reset_opp_meas(void) {
}
laurent's avatar
laurent committed
105 106 107 108 109 110 111 112 113 114 115 116
extern void  phy_free_RU(RU_t *);

void exit_function(const char *file, const char *function, const int line, const char *s) {
  if (s != NULL) {
    printf("%s:%d %s() Exiting OAI softmodem: %s\n",file,line, function, s);
  }

  close_log_mem();
  oai_exit = 1;
  sleep(1); //allow lte-softmodem threads to exit first
  exit(1);
}
laurent's avatar
laurent committed
117

118 119
// Fixme: there are many mistakes in the datamodel and in redondant variables
// TDD is also mode complex
120
void setAllfromTS(uint64_t TS, L1_rxtx_proc_t *proc) {
121 122 123 124 125 126 127 128 129 130 131 132 133 134
  for (int i=0; i < RC.nb_inst; i++) {
    for (int j=0; j<RC.nb_CC[i]; j++) {
      LTE_DL_FRAME_PARMS *fp=&RC.eNB[i][j]->frame_parms;
      uint64_t TStx=TS+(sf_ahead)*fp->samples_per_tti;
      uint64_t TSrach=TS;//-fp->samples_per_tti;
      proc->timestamp_rx=  TS;
      proc->timestamp_tx=  TStx;
      proc->subframe_rx=   (TS    / fp->samples_per_tti)%10;
      proc->subframe_prach=(TSrach    / fp->samples_per_tti)%10;
      proc->subframe_prach_br=(TSrach / fp->samples_per_tti)%10;
      proc->frame_rx=      (TS    / (fp->samples_per_tti*10))&1023;
      proc->frame_prach=   (TSrach    / (fp->samples_per_tti*10))&1023;
      proc->frame_prach_br=(TSrach    / (fp->samples_per_tti*10))&1023;
      proc->frame_tx=      (TStx  / (fp->samples_per_tti*10))&1023;
135
      proc->subframe_tx=  (TStx  / fp->samples_per_tti)%10;
136 137 138 139 140 141
    }
  }

  return;
}

laurent's avatar
laurent committed
142
void init_RU_proc(RU_t *ru) {
laurent's avatar
laurent committed
143
  pthread_t t;
laurent's avatar
laurent committed
144 145

  switch(split73) {
Laurent Thomas's avatar
Laurent Thomas committed
146 147 148 149 150 151 152 153 154 155
    case SPLIT73_CU:
      threadCreate(&t, cu_fs6, (void *)ru, "MainCu", -1, OAI_PRIORITY_RT_MAX);
      break;

    case SPLIT73_DU:
      threadCreate(&t, du_fs6, (void *)ru, "MainDuRx", -1, OAI_PRIORITY_RT_MAX);
      break;

    default:
      threadCreate(&t,  ru_thread, (void *)ru, "MainRu", -1, OAI_PRIORITY_RT_MAX);
laurent's avatar
laurent committed
156
  }
laurent's avatar
laurent committed
157 158
}

laurent's avatar
laurent committed
159
// Create per UE structures
laurent's avatar
laurent committed
160 161 162 163
void init_transport(PHY_VARS_eNB *eNB) {
  LTE_DL_FRAME_PARMS *fp = &eNB->frame_parms;
  LOG_I(PHY, "Initialise transport\n");

Laurent Thomas's avatar
Laurent Thomas committed
164
  for (int i=0; i<NUMBER_OF_DLSCH_MAX; i++) {
laurent's avatar
laurent committed
165 166
    LOG_D(PHY,"Allocating Transport Channel Buffers for DLSCH, UE %d\n",i);

laurent's avatar
laurent committed
167 168 169 170 171
    for (int j=0; j<2; j++) {
      AssertFatal( (eNB->dlsch[i][j] = new_eNB_dlsch(1,8,NSOFT,fp->N_RB_DL,0,fp)) != NULL,
                   "Can't get eNB dlsch structures for UE %d \n", i);
      eNB->dlsch[i][j]->rnti=0;
      LOG_D(PHY,"dlsch[%d][%d] => %p rnti:%d\n",i,j,eNB->dlsch[i][j], eNB->dlsch[i][j]->rnti);
laurent's avatar
laurent committed
172
    }
Laurent Thomas's avatar
Laurent Thomas committed
173
  }
laurent's avatar
laurent committed
174

Laurent Thomas's avatar
Laurent Thomas committed
175
  for (int i=0; i<NUMBER_OF_ULSCH_MAX; i++) {
laurent's avatar
laurent committed
176
    LOG_D(PHY,"Allocating Transport Channel Buffer for ULSCH, UE %d\n",i);
laurent's avatar
laurent committed
177 178
    AssertFatal((eNB->ulsch[1+i] = new_eNB_ulsch(MAX_TURBO_ITERATIONS,fp->N_RB_UL, 0)) != NULL,
                "Can't get eNB ulsch structures\n");
laurent's avatar
laurent committed
179 180 181 182 183 184
    // this is the transmission mode for the signalling channels
    // this will be overwritten with the real transmission mode by the RRC once the UE is connected
    eNB->transmission_mode[i] = fp->nb_antenna_ports_eNB==1 ? 1 : 2;
  }

  // ULSCH for RA
laurent's avatar
laurent committed
185 186
  AssertFatal( (eNB->ulsch[0] = new_eNB_ulsch(MAX_TURBO_ITERATIONS, fp->N_RB_UL, 0)) !=NULL,
               "Can't get eNB ulsch structures\n");
laurent's avatar
laurent committed
187 188 189 190 191 192 193 194
  eNB->dlsch_SI  = new_eNB_dlsch(1,8,NSOFT,fp->N_RB_DL, 0, fp);
  LOG_D(PHY,"eNB %d.%d : SI %p\n",eNB->Mod_id,eNB->CC_id,eNB->dlsch_SI);
  eNB->dlsch_ra  = new_eNB_dlsch(1,8,NSOFT,fp->N_RB_DL, 0, fp);
  LOG_D(PHY,"eNB %d.%d : RA %p\n",eNB->Mod_id,eNB->CC_id,eNB->dlsch_ra);
  eNB->dlsch_MCH = new_eNB_dlsch(1,8,NSOFT,fp->N_RB_DL, 0, fp);
  LOG_D(PHY,"eNB %d.%d : MCH %p\n",eNB->Mod_id,eNB->CC_id,eNB->dlsch_MCH);
  eNB->rx_total_gain_dB=130;

laurent's avatar
laurent committed
195
  for(int i=0; i<NUMBER_OF_UE_MAX; i++)
laurent's avatar
laurent committed
196 197 198 199 200 201 202
    eNB->mu_mimo_mode[i].dl_pow_off = 2;

  eNB->check_for_total_transmissions = 0;
  eNB->check_for_MUMIMO_transmissions = 0;
  eNB->FULL_MUMIMO_transmissions = 0;
  eNB->check_for_SUMIMO_transmissions = 0;
  fp->pucch_config_common.deltaPUCCH_Shift = 1;
Laurent Thomas's avatar
Laurent Thomas committed
203 204 205

  if (eNB->use_DTX == 0)
    fill_subframe_mask(eNB);
laurent's avatar
laurent committed
206 207 208
}

void init_eNB_afterRU(void) {
laurent's avatar
laurent committed
209 210 211
  for (int inst=0; inst<RC.nb_inst; inst++) {
    for (int CC_id=0; CC_id<RC.nb_CC[inst]; CC_id++) {
      PHY_VARS_eNB *eNB = RC.eNB[inst][CC_id];
laurent's avatar
laurent committed
212
      eNB->frame_parms.nb_antennas_rx       = 0;
laurent's avatar
laurent committed
213
      eNB->frame_parms.nb_antennas_tx       = 0;
laurent's avatar
laurent committed
214 215 216 217 218 219
      eNB->prach_vars.rxsigF[0] = (int16_t **)malloc16(64*sizeof(int16_t *));

      for (int ce_level=0; ce_level<4; ce_level++) {
        eNB->prach_vars_br.rxsigF[ce_level] = (int16_t **)malloc16(64*sizeof(int16_t *));
      }

laurent's avatar
laurent committed
220
      for (int ru_id=0,aa=0; ru_id<eNB->num_RU; ru_id++) {
laurent's avatar
laurent committed
221
        eNB->frame_parms.nb_antennas_rx    += eNB->RU_list[ru_id]->nb_rx;
laurent's avatar
laurent committed
222
        eNB->frame_parms.nb_antennas_tx    += eNB->RU_list[ru_id]->nb_tx;
laurent's avatar
laurent committed
223 224 225 226 227 228 229
        AssertFatal(eNB->RU_list[ru_id]->common.rxdataF!=NULL,
                    "RU %d : common.rxdataF is NULL\n",
                    eNB->RU_list[ru_id]->idx);
        AssertFatal(eNB->RU_list[ru_id]->prach_rxsigF!=NULL,
                    "RU %d : prach_rxsigF is NULL\n",
                    eNB->RU_list[ru_id]->idx);

laurent's avatar
laurent committed
230
        for (int i=0; i<eNB->RU_list[ru_id]->nb_rx; aa++,i++) {
laurent's avatar
laurent committed
231
          LOG_I(PHY,"Attaching RU %d antenna %d to eNB antenna %d\n",eNB->RU_list[ru_id]->idx,i,aa);
232
          eNB->prach_vars.rxsigF[0][aa]    =  eNB->RU_list[ru_id]->prach_rxsigF[0][i];
laurent's avatar
laurent committed
233 234 235 236 237 238

          for (int ce_level=0; ce_level<4; ce_level++)
            eNB->prach_vars_br.rxsigF[ce_level][aa] = eNB->RU_list[ru_id]->prach_rxsigF_br[ce_level][i];
        }
      }

239 240 241 242 243 244
      AssertFatal( eNB->frame_parms.nb_antennas_rx > 0 && eNB->frame_parms.nb_antennas_rx < 5, "");
      AssertFatal( eNB->frame_parms.nb_antennas_tx > 0 && eNB->frame_parms.nb_antennas_rx < 5, "");
      phy_init_lte_eNB(eNB,0,0);

      // need to copy rxdataF after L1 variables are allocated
      for (int inst=0; inst<RC.nb_inst; inst++) {
Laurent Thomas's avatar
Laurent Thomas committed
245 246 247 248 249 250 251 252
        for (int CC_id=0; CC_id<RC.nb_CC[inst]; CC_id++) {
          PHY_VARS_eNB *eNB = RC.eNB[inst][CC_id];

          for (int ru_id=0,aa=0; ru_id<eNB->num_RU; ru_id++) {
            for (int i=0; i<eNB->RU_list[ru_id]->nb_rx; aa++,i++)
              eNB->common_vars.rxdataF[aa]     =  eNB->RU_list[ru_id]->common.rxdataF[i];
          }
        }
253 254
      }

laurent's avatar
laurent committed
255 256 257 258 259 260 261 262
      LOG_I(PHY,"inst %d, CC_id %d : nb_antennas_rx %d\n",inst,CC_id,eNB->frame_parms.nb_antennas_rx);
      init_transport(eNB);
      //init_precoding_weights(RC.eNB[inst][CC_id]);
    }
  }
}

void init_eNB(int single_thread_flag,int wait_for_sync) {
laurent's avatar
laurent committed
263
  AssertFatal(RC.eNB != NULL,"RC.eNB must have been allocated\n");
laurent's avatar
laurent committed
264

laurent's avatar
laurent committed
265 266
  for (int inst=0; inst<RC.nb_L1_inst; inst++) {
    AssertFatal(RC.eNB[inst] != NULL,"RC.eNB[%d] must have been allocated\n", inst);
laurent's avatar
laurent committed
267

laurent's avatar
laurent committed
268 269 270
    for (int CC_id=0; CC_id<RC.nb_L1_CC[inst]; CC_id++) {
      AssertFatal(RC.eNB[inst][CC_id] != NULL,"RC.eNB[%d][%d] must have been allocated\n", inst, CC_id);
      PHY_VARS_eNB *eNB = RC.eNB[inst][CC_id];
laurent's avatar
laurent committed
271 272 273 274 275 276 277
      eNB->abstraction_flag   = 0;
      eNB->single_thread_flag = single_thread_flag;
      AssertFatal((eNB->if_inst         = IF_Module_init(inst))!=NULL,"Cannot register interface");
      eNB->if_inst->schedule_response   = schedule_response;
      eNB->if_inst->PHY_config_req      = phy_config_request;
      memset((void *)&eNB->UL_INFO,0,sizeof(eNB->UL_INFO));
      memset((void *)&eNB->Sched_INFO,0,sizeof(eNB->Sched_INFO));
laurent's avatar
laurent committed
278
      pthread_mutex_init( &eNB->UL_INFO_mutex, NULL);
laurent's avatar
laurent committed
279 280 281 282 283
      LOG_I(PHY,"Setting indication lists\n");
      eNB->UL_INFO.rx_ind.rx_indication_body.rx_pdu_list   = eNB->rx_pdu_list;
      eNB->UL_INFO.crc_ind.crc_indication_body.crc_pdu_list = eNB->crc_pdu_list;
      eNB->UL_INFO.sr_ind.sr_indication_body.sr_pdu_list = eNB->sr_pdu_list;
      eNB->UL_INFO.harq_ind.harq_indication_body.harq_pdu_list = eNB->harq_pdu_list;
laurent's avatar
laurent committed
284 285
      eNB->UL_INFO.cqi_ind.cqi_indication_body.cqi_pdu_list = eNB->cqi_pdu_list;
      eNB->UL_INFO.cqi_ind.cqi_indication_body.cqi_raw_pdu_list = eNB->cqi_raw_pdu_list;
laurent's avatar
laurent committed
286 287 288
      eNB->prach_energy_counter = 0;
    }
  }
289 290

  SET_LOG_DEBUG(PRACH);
laurent's avatar
laurent committed
291 292 293 294 295 296 297 298
}

void stop_eNB(int nb_inst) {
  for (int inst=0; inst<nb_inst; inst++) {
    LOG_I(PHY,"Killing eNB %d processing threads\n",inst);
    kill_eNB_proc(inst);
  }
}
laurent's avatar
laurent committed
299

laurent's avatar
laurent committed
300 301 302
// this is for RU with local RF unit
void fill_rf_config(RU_t *ru, char *rf_config_file) {
  int i;
303
  LTE_DL_FRAME_PARMS *fp   = ru->frame_parms;
laurent's avatar
laurent committed
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
  openair0_config_t *cfg   = &ru->openair0_cfg;
  //printf("////////////////numerology in config = %d\n",numerology);
  int numerology = get_softmodem_params()->numerology;

  if(fp->N_RB_DL == 100) {
    if(numerology == 0) {
      if (fp->threequarter_fs) {
        cfg->sample_rate=23.04e6;
        cfg->samples_per_frame = 230400;
        cfg->tx_bw = 10e6;
        cfg->rx_bw = 10e6;
      } else {
        cfg->sample_rate=30.72e6;
        cfg->samples_per_frame = 307200;
        cfg->tx_bw = 10e6;
        cfg->rx_bw = 10e6;
      }
    } else if(numerology == 1) {
      cfg->sample_rate=61.44e6;
      cfg->samples_per_frame = 307200;
      cfg->tx_bw = 20e6;
      cfg->rx_bw = 20e6;
    } else if(numerology == 2) {
      cfg->sample_rate=122.88e6;
      cfg->samples_per_frame = 307200;
      cfg->tx_bw = 40e6;
      cfg->rx_bw = 40e6;
    } else {
laurent's avatar
laurent committed
332
      LOG_E(PHY,"Wrong input for numerology %d\n setting to 20MHz normal CP configuration",numerology);
laurent's avatar
laurent committed
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
      cfg->sample_rate=30.72e6;
      cfg->samples_per_frame = 307200;
      cfg->tx_bw = 10e6;
      cfg->rx_bw = 10e6;
    }
  } else if(fp->N_RB_DL == 50) {
    cfg->sample_rate=15.36e6;
    cfg->samples_per_frame = 153600;
    cfg->tx_bw = 5e6;
    cfg->rx_bw = 5e6;
  } else if (fp->N_RB_DL == 25) {
    cfg->sample_rate=7.68e6;
    cfg->samples_per_frame = 76800;
    cfg->tx_bw = 2.5e6;
    cfg->rx_bw = 2.5e6;
  } else if (fp->N_RB_DL == 6) {
    cfg->sample_rate=1.92e6;
    cfg->samples_per_frame = 19200;
    cfg->tx_bw = 1.5e6;
    cfg->rx_bw = 1.5e6;
Laurent Thomas's avatar
Laurent Thomas committed
353 354
  } else
    AssertFatal(1==0,"Unknown N_RB_DL %d\n",fp->N_RB_DL);
laurent's avatar
laurent committed
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372

  if (fp->frame_type==TDD)
    cfg->duplex_mode = duplex_mode_TDD;
  else //FDD
    cfg->duplex_mode = duplex_mode_FDD;

  cfg->Mod_id = 0;
  cfg->num_rb_dl=fp->N_RB_DL;
  cfg->tx_num_channels=ru->nb_tx;
  cfg->rx_num_channels=ru->nb_rx;
  cfg->clock_source=get_softmodem_params()->clock_source;

  for (i=0; i<ru->nb_tx; i++) {
    cfg->tx_freq[i] = (double)fp->dl_CarrierFreq;
    cfg->rx_freq[i] = (double)fp->ul_CarrierFreq;
    cfg->tx_gain[i] = (double)ru->att_tx;
    cfg->rx_gain[i] = ru->max_rxgain-(double)ru->att_rx;
    cfg->configFilename = rf_config_file;
laurent's avatar
laurent committed
373 374 375 376 377
    LOG_I(PHY,"channel %d, Setting tx_gain offset %f, rx_gain offset %f, tx_freq %f, rx_freq %f\n",
          i, cfg->tx_gain[i],
          cfg->rx_gain[i],
          cfg->tx_freq[i],
          cfg->rx_freq[i]);
laurent's avatar
laurent committed
378 379 380 381 382 383 384 385 386 387
  }
}

/* this function maps the RU tx and rx buffers to the available rf chains.
   Each rf chain is is addressed by the card number and the chain on the card. The
   rf_map specifies for each antenna port, on which rf chain the mapping should start. Multiple
   antennas are mapped to successive RF chains on the same card. */
int setup_RU_buffers(RU_t *ru) {
  //uint16_t N_TA_offset = 0;
  LTE_DL_FRAME_PARMS *frame_parms;
laurent's avatar
laurent committed
388
  AssertFatal(ru, "ru is NULL");
389
  frame_parms = ru->frame_parms;
laurent's avatar
laurent committed
390
  LOG_I(PHY,"setup_RU_buffers: frame_parms = %p\n",frame_parms);
laurent's avatar
laurent committed
391 392

  if (frame_parms->frame_type == TDD) {
laurent's avatar
laurent committed
393
    if (frame_parms->N_RB_DL == 100) {
laurent's avatar
laurent committed
394
      ru->N_TA_offset = 624;
laurent's avatar
laurent committed
395
    } else if (frame_parms->N_RB_DL == 50) {
laurent's avatar
laurent committed
396
      ru->N_TA_offset = 624/2;
laurent's avatar
laurent committed
397 398 399
      ru->sf_extension       /= 2;
      ru->end_of_burst_delay /= 2;
    } else if (frame_parms->N_RB_DL == 25) {
laurent's avatar
laurent committed
400
      ru->N_TA_offset = 624/4;
laurent's avatar
laurent committed
401 402 403
      ru->sf_extension       /= 4;
      ru->end_of_burst_delay /= 4;
    } else {
laurent's avatar
laurent committed
404
      LOG_E(PHY,"not handled, todo\n");
laurent's avatar
laurent committed
405 406 407 408 409 410 411 412 413 414
      exit(1);
    }
  } else {
    ru->N_TA_offset = 0;
    ru->sf_extension = 0;
    ru->end_of_burst_delay = 0;
  }

  return(0);
}
laurent's avatar
laurent committed
415

416
#if 0
laurent's avatar
laurent committed
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
void init_precoding_weights(PHY_VARS_eNB *eNB) {
  int layer,ru_id,aa,re,ue,tb;
  LTE_DL_FRAME_PARMS *fp=&eNB->frame_parms;
  RU_t *ru;
  LTE_eNB_DLSCH_t *dlsch;

  // init precoding weigths
  for (ue=0; ue<NUMBER_OF_UE_MAX; ue++) {
    for (tb=0; tb<2; tb++) {
      dlsch = eNB->dlsch[ue][tb];

      for (layer=0; layer<4; layer++) {
        int nb_tx=0;

        for (ru_id=0; ru_id<RC.nb_RU; ru_id++) {
          ru = RC.ru[ru_id];
          nb_tx+=ru->nb_tx;
        }

        dlsch->ue_spec_bf_weights[layer] = (int32_t **)malloc16(nb_tx*sizeof(int32_t *));

        for (aa=0; aa<nb_tx; aa++) {
          dlsch->ue_spec_bf_weights[layer][aa] = (int32_t *)malloc16(fp->ofdm_symbol_size*sizeof(int32_t));

          for (re=0; re<fp->ofdm_symbol_size; re++) {
            dlsch->ue_spec_bf_weights[layer][aa][re] = 0x00007fff;
          }
        }
      }
    }
  }
}
449
#endif
laurent's avatar
laurent committed
450

451
void ocp_rx_prach(PHY_VARS_eNB *eNB,
laurent's avatar
laurent committed
452 453 454 455 456 457 458 459 460
                  L1_rxtx_proc_t *proc,
                  RU_t *ru,
                  uint16_t *max_preamble,
                  uint16_t *max_preamble_energy,
                  uint16_t *max_preamble_delay,
                  uint16_t *avg_preamble_energy,
                  uint16_t Nf,
                  uint8_t tdd_mapindex,
                  uint8_t br_flag) {
461 462
  int i;
  int prach_mask=0;
laurent's avatar
laurent committed
463

464 465
  if (br_flag == 0) {
    rx_prach0(eNB,ru,proc->frame_prach, proc->subframe_prach,
laurent's avatar
laurent committed
466
              max_preamble,max_preamble_energy,max_preamble_delay,avg_preamble_energy,Nf,tdd_mapindex,0,0);
467 468 469 470 471 472 473 474
  } else { // This is procedure for eMTC, basically handling the repetitions
    prach_mask = is_prach_subframe(&eNB->frame_parms,proc->frame_prach_br,proc->subframe_prach_br);

    for (i=0; i<4; i++) {
      if ((eNB->frame_parms.prach_emtc_config_common.prach_ConfigInfo.prach_CElevel_enable[i]==1) &&
          ((prach_mask&(1<<(i+1))) > 0)) { // check that prach CE level is active now

        // if first reception in group of repetitions store frame for later (in RA-RNTI for Msg2)
Laurent Thomas's avatar
Laurent Thomas committed
475 476
        if (eNB->prach_vars_br.repetition_number[i]==0)
          eNB->prach_vars_br.first_frame[i]=proc->frame_prach_br;
477 478 479 480 481

        // increment repetition number
        eNB->prach_vars_br.repetition_number[i]++;
        // do basic PRACH reception
        rx_prach0(eNB,ru,proc->frame_prach, proc->subframe_prach_br,
laurent's avatar
laurent committed
482
                  max_preamble,max_preamble_energy,max_preamble_delay,avg_preamble_energy,Nf,tdd_mapindex,1,i);
483 484 485 486 487 488 489 490 491 492

        // if last repetition, clear counter
        if (eNB->prach_vars_br.repetition_number[i] == eNB->frame_parms.prach_emtc_config_common.prach_ConfigInfo.prach_numRepetitionPerPreambleAttempt[i]) {
          eNB->prach_vars_br.repetition_number[i]=0;
        }
      }
    } /* for i ... */
  } /* else br_flag == 0 */
}

493
void prach_procedures_ocp(PHY_VARS_eNB *eNB, L1_rxtx_proc_t *proc, int br_flag) {
laurent's avatar
laurent committed
494 495 496 497 498 499 500 501 502
  uint16_t max_preamble[4],max_preamble_energy[4],max_preamble_delay[4],avg_preamble_energy[4];
  RU_t *ru;
  int aa=0;
  int ru_aa;

  for (int i=0; i<eNB->num_RU; i++) {
    ru=eNB->RU_list[i];

    for (ru_aa=0,aa=0; ru_aa<ru->nb_rx; ru_aa++,aa++) {
503
      eNB->prach_vars.rxsigF[0][aa] = eNB->RU_list[i]->prach_rxsigF[0][ru_aa];
laurent's avatar
laurent committed
504 505 506 507 508 509 510
      int ce_level;

      if (br_flag==1)
        for (ce_level=0; ce_level<4; ce_level++)
          eNB->prach_vars_br.rxsigF[ce_level][aa] = eNB->RU_list[i]->prach_rxsigF_br[ce_level][ru_aa];
    }
  }
laurent's avatar
laurent committed
511

laurent's avatar
laurent committed
512
  // run PRACH detection for CE-level 0 only for now when br_flag is set
513
  ocp_rx_prach(eNB,
laurent's avatar
laurent committed
514 515 516 517 518 519 520 521 522 523
               proc,
               eNB->RU_list[0],
               &max_preamble[0],
               &max_preamble_energy[0],
               &max_preamble_delay[0],
               &avg_preamble_energy[0],
               proc->frame_prach,
               0
               ,br_flag
              );
524 525 526 527 528 529
  LOG_D(PHY,"RACH detection index 0: max preamble: %u, energy: %u, delay: %u, avg energy: %u\n",
        max_preamble[0],
        max_preamble_energy[0],
        max_preamble_delay[0],
        avg_preamble_energy[0]
       );
laurent's avatar
laurent committed
530 531 532

  if (br_flag==1) {
    int             prach_mask;
533
    prach_mask = is_prach_subframe (&eNB->frame_parms, proc->frame_prach_br, proc->subframe_prach_br);
laurent's avatar
laurent committed
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
    eNB->UL_INFO.rach_ind_br.rach_indication_body.preamble_list = eNB->preamble_list_br;
    int             ind = 0;
    int             ce_level = 0;
    /* Save for later, it doesn't work
       for (int ind=0,ce_level=0;ce_level<4;ce_level++) {

       if ((eNB->frame_parms.prach_emtc_config_common.prach_ConfigInfo.prach_CElevel_enable[ce_level]==1)&&
       (prach_mask&(1<<(1+ce_level)) > 0) && // prach is active and CE level has finished its repetitions
       (eNB->prach_vars_br.repetition_number[ce_level]==
       eNB->frame_parms.prach_emtc_config_common.prach_ConfigInfo.prach_numRepetitionPerPreambleAttempt[ce_level])) {

    */

    if (eNB->frame_parms.prach_emtc_config_common.prach_ConfigInfo.prach_CElevel_enable[0] == 1) {
      if ((eNB->prach_energy_counter == 100) && (max_preamble_energy[0] > eNB->measurements.prach_I0 + eNB->prach_DTX_threshold_emtc[0])) {
        eNB->UL_INFO.rach_ind_br.rach_indication_body.number_of_preambles++;
        eNB->preamble_list_br[ind].preamble_rel8.timing_advance = max_preamble_delay[ind];      //
        eNB->preamble_list_br[ind].preamble_rel8.preamble = max_preamble[ind];
        // note: fid is implicitly 0 here, this is the rule for eMTC RA-RNTI from 36.321, Section 5.1.4
553
        eNB->preamble_list_br[ind].preamble_rel8.rnti = 1 + proc->subframe_prach + (60*(eNB->prach_vars_br.first_frame[ce_level] % 40));
laurent's avatar
laurent committed
554 555 556 557 558 559 560 561 562 563 564 565 566 567
        eNB->preamble_list_br[ind].instance_length = 0; //don't know exactly what this is
        eNB->preamble_list_br[ind].preamble_rel13.rach_resource_type = 1 + ce_level;    // CE Level
        LOG_I (PHY, "Filling NFAPI indication for RACH %d CELevel %d (mask %x) : TA %d, Preamble %d, rnti %x, rach_resource_type %d\n",
               ind,
               ce_level,
               prach_mask,
               eNB->preamble_list_br[ind].preamble_rel8.timing_advance,
               eNB->preamble_list_br[ind].preamble_rel8.preamble, eNB->preamble_list_br[ind].preamble_rel8.rnti, eNB->preamble_list_br[ind].preamble_rel13.rach_resource_type);
      }
    }

    /*
      ind++;
      }
laurent's avatar
laurent committed
568
      } */// ce_level
laurent's avatar
laurent committed
569 570 571 572 573
  } else if ((eNB->prach_energy_counter == 100) &&
             (max_preamble_energy[0] > eNB->measurements.prach_I0+eNB->prach_DTX_threshold)) {
    LOG_I(PHY,"[eNB %d/%d][RAPROC] Frame %d, subframe %d Initiating RA procedure with preamble %d, energy %d.%d dB, delay %d\n",
          eNB->Mod_id,
          eNB->CC_id,
574 575
          proc->frame_prach,
          proc->subframe_prach,
laurent's avatar
laurent committed
576 577 578 579 580 581 582 583 584
          max_preamble[0],
          max_preamble_energy[0]/10,
          max_preamble_energy[0]%10,
          max_preamble_delay[0]);
    pthread_mutex_lock(&eNB->UL_INFO_mutex);
    eNB->UL_INFO.rach_ind.rach_indication_body.number_of_preambles  = 1;
    eNB->UL_INFO.rach_ind.rach_indication_body.preamble_list        = &eNB->preamble_list[0];
    eNB->UL_INFO.rach_ind.rach_indication_body.tl.tag               = NFAPI_RACH_INDICATION_BODY_TAG;
    eNB->UL_INFO.rach_ind.header.message_id                         = NFAPI_RACH_INDICATION;
585
    eNB->UL_INFO.rach_ind.sfn_sf                                    = proc->frame_prach<<4 | proc->subframe_prach;
laurent's avatar
laurent committed
586 587 588
    eNB->preamble_list[0].preamble_rel8.tl.tag                = NFAPI_PREAMBLE_REL8_TAG;
    eNB->preamble_list[0].preamble_rel8.timing_advance        = max_preamble_delay[0];
    eNB->preamble_list[0].preamble_rel8.preamble              = max_preamble[0];
589
    eNB->preamble_list[0].preamble_rel8.rnti                  = 1+proc->subframe_prach;  // note: fid is implicitly 0 here
laurent's avatar
laurent committed
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
    eNB->preamble_list[0].preamble_rel13.rach_resource_type   = 0;
    eNB->preamble_list[0].instance_length                     = 0; //don't know exactly what this is

    if (NFAPI_MODE==NFAPI_MODE_PNF) {  // If NFAPI PNF then we need to send the message to the VNF
      LOG_D(PHY,"Filling NFAPI indication for RACH : SFN_SF:%d TA %d, Preamble %d, rnti %x, rach_resource_type %d\n",
            NFAPI_SFNSF2DEC(eNB->UL_INFO.rach_ind.sfn_sf),
            eNB->preamble_list[0].preamble_rel8.timing_advance,
            eNB->preamble_list[0].preamble_rel8.preamble,
            eNB->preamble_list[0].preamble_rel8.rnti,
            eNB->preamble_list[0].preamble_rel13.rach_resource_type);
      oai_nfapi_rach_ind(&eNB->UL_INFO.rach_ind);
      eNB->UL_INFO.rach_ind.rach_indication_body.number_of_preambles = 0;
    }

    pthread_mutex_unlock(&eNB->UL_INFO_mutex);
  } // max_preamble_energy > prach_I0 + 100
  else {
    eNB->measurements.prach_I0 = ((eNB->measurements.prach_I0*900)>>10) + ((avg_preamble_energy[0]*124)>>10);

    if (eNB->prach_energy_counter < 100)
      eNB->prach_energy_counter++;
  }
} // else br_flag

614
void prach_eNB(PHY_VARS_eNB *eNB, L1_rxtx_proc_t *proc, int frame,int subframe) {
laurent's avatar
laurent committed
615
  // check if we have to detect PRACH first
laurent's avatar
laurent committed
616
  if (is_prach_subframe(&eNB->frame_parms, frame,subframe)>0) {
617 618
    prach_procedures_ocp(eNB, proc, 0);
    prach_procedures_ocp(eNB, proc, 1);
laurent's avatar
laurent committed
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
  }
}

static inline int rxtx(PHY_VARS_eNB *eNB,L1_rxtx_proc_t *proc, char *thread_name) {
  AssertFatal( eNB !=NULL, "");

  if (NFAPI_MODE==NFAPI_MODE_PNF) {
    // I am a PNF and I need to let nFAPI know that we have a (sub)frame tick
    //add_subframe(&frame, &subframe, 4);
    //oai_subframe_ind(proc->frame_tx, proc->subframe_tx);
    oai_subframe_ind(proc->frame_rx, proc->subframe_rx);
  }

  AssertFatal( !(NFAPI_MODE==NFAPI_MODE_PNF &&
                 eNB->pdcch_vars[proc->subframe_tx&1].num_pdcch_symbols == 0), "");
634
  prach_eNB(eNB,proc,proc->frame_rx,proc->subframe_rx);
laurent's avatar
laurent committed
635 636 637 638 639 640 641 642 643 644 645 646
  release_UE_in_freeList(eNB->Mod_id);

  // UE-specific RX processing for subframe n
  if (NFAPI_MODE==NFAPI_MONOLITHIC || NFAPI_MODE==NFAPI_MODE_PNF) {
    phy_procedures_eNB_uespec_RX(eNB, proc);
  }

  pthread_mutex_lock(&eNB->UL_INFO_mutex);
  eNB->UL_INFO.frame     = proc->frame_rx;
  eNB->UL_INFO.subframe  = proc->subframe_rx;
  eNB->UL_INFO.module_id = eNB->Mod_id;
  eNB->UL_INFO.CC_id     = eNB->CC_id;
647
  eNB->if_inst->UL_indication(&eNB->UL_INFO, proc);
laurent's avatar
laurent committed
648 649 650 651 652
  pthread_mutex_unlock(&eNB->UL_INFO_mutex);
  phy_procedures_eNB_TX(eNB, proc, 1);
  return(0);
}

653
void rx_rf(RU_t *ru, L1_rxtx_proc_t *proc) {
654
  LTE_DL_FRAME_PARMS *fp = ru->frame_parms;
laurent's avatar
laurent committed
655 656 657
  void *rxp[ru->nb_rx];
  unsigned int rxs;
  int i;
658 659
  openair0_timestamp ts=0, timestamp_rx;
  static openair0_timestamp old_ts=0;
660

laurent's avatar
laurent committed
661
  for (i=0; i<ru->nb_rx; i++)
662
    //receive in the next slot
663
    rxp[i] = (void *)&ru->common.rxdata[i][((proc->subframe_rx+1)%10)*fp->samples_per_tti];
laurent's avatar
laurent committed
664 665 666 667 668 669

  rxs = ru->rfdevice.trx_read_func(&ru->rfdevice,
                                   &ts,
                                   rxp,
                                   fp->samples_per_tti,
                                   ru->nb_rx);
670
  timestamp_rx = ts-ru->ts_offset;
laurent's avatar
laurent committed
671 672 673 674 675

  //  AssertFatal(rxs == fp->samples_per_tti,
  //        "rx_rf: Asked for %d samples, got %d from SDR\n",fp->samples_per_tti,rxs);
  if(rxs != fp->samples_per_tti) {
    LOG_E(PHY,"rx_rf: Asked for %d samples, got %d from SDR\n",fp->samples_per_tti,rxs);
676 677 678 679 680 681
#if defined(USRP_REC_PLAY)
    exit_fun("Exiting IQ record/playback");
#else
    //exit_fun( "problem receiving samples" );
    LOG_E(PHY, "problem receiving samples");
#endif
laurent's avatar
laurent committed
682 683
  }

684
  if (old_ts != 0 && timestamp_rx - old_ts != fp->samples_per_tti) {
685 686 687
    LOG_E(HW,"impossible shift in rx stream, rx: %ld, previous rx distance: %ld, should be %d\n", timestamp_rx, proc->timestamp_rx - old_ts, fp->samples_per_tti);
    //ru->ts_offset += (proc->timestamp_rx - old_ts - fp->samples_per_tti);
    //proc->timestamp_rx = ts-ru->ts_offset;
laurent's avatar
laurent committed
688
  }
689

690 691
  old_ts=timestamp_rx;
  setAllfromTS(timestamp_rx, proc);
laurent's avatar
laurent committed
692 693
}

694 695
void ocp_tx_rf(RU_t *ru, L1_rxtx_proc_t *proc) {
  LTE_DL_FRAME_PARMS *fp = ru->frame_parms;
laurent's avatar
laurent committed
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
  void *txp[ru->nb_tx];
  int i;
  lte_subframe_t SF_type     = subframe_select(fp,proc->subframe_tx%10);
  lte_subframe_t prevSF_type = subframe_select(fp,(proc->subframe_tx+9)%10);
  int sf_extension = 0;

  if ((SF_type == SF_DL) ||
      (SF_type == SF_S)) {
    int siglen=fp->samples_per_tti,flags=1;

    if (SF_type == SF_S) {
      /* end_of_burst_delay is used to stop TX only "after a while".
       * If we stop right after effective signal, with USRP B210 and
       * B200mini, we observe a high EVM on the S subframe (on the
       * PSS).
       * A value of 400 (for 30.72MHz) solves this issue. This is
       * the default.
       */
      siglen = (fp->ofdm_symbol_size + fp->nb_prefix_samples0)
               + (fp->dl_symbols_in_S_subframe - 1) * (fp->ofdm_symbol_size + fp->nb_prefix_samples)
               + ru->end_of_burst_delay;
      flags=3; // end of burst
    }

    if (fp->frame_type == TDD &&
        SF_type == SF_DL &&
        prevSF_type == SF_UL) {
      flags = 2; // start of burst
      sf_extension = ru->sf_extension;
    }

#if defined(__x86_64) || defined(__i386__)
#ifdef __AVX2__
    sf_extension = (sf_extension)&0xfffffff8;
#else
    sf_extension = (sf_extension)&0xfffffffc;
#endif
#elif defined(__arm__)
    sf_extension = (sf_extension)&0xfffffffc;
#endif

    for (i=0; i<ru->nb_tx; i++)
      txp[i] = (void *)&ru->common.txdata[i][(proc->subframe_tx*fp->samples_per_tti)-sf_extension];

    /* add fail safe for late command end */
    // prepare tx buffer pointers
742
    ru->rfdevice.trx_write_func(&ru->rfdevice,
743 744 745 746 747
                                proc->timestamp_tx+ru->ts_offset-ru->openair0_cfg.tx_sample_advance-sf_extension,
                                txp,
                                siglen+sf_extension,
                                ru->nb_tx,
                                flags);
748 749
    LOG_D(PHY,"[TXPATH] RU %d tx_rf, writing to TS %llu, frame %d, subframe %d\n",ru->idx,
          (long long unsigned int)proc->timestamp_tx,proc->frame_tx,proc->subframe_tx);
laurent's avatar
laurent committed
750
  }
751

752
  return;
laurent's avatar
laurent committed
753 754
}

laurent's avatar
laurent committed
755
static void *ru_thread( void *param ) {
756 757
  setbuf(stdout, NULL);
  setbuf(stderr, NULL);
laurent's avatar
laurent committed
758
  RU_t *ru = (RU_t *)param;
759
  L1_rxtx_proc_t L1proc= {0};
laurent's avatar
laurent committed
760 761 762 763 764
  // We pick the global thread pool from the legacy code global vars
  L1proc.threadPool=RC.eNB[0][0]->proc.L1_proc.threadPool;
  L1proc.respEncode=RC.eNB[0][0]->proc.L1_proc.respEncode;
  L1proc.respDecode=RC.eNB[0][0]->proc.L1_proc.respDecode;

laurent's avatar
laurent committed
765 766
  if (ru->if_south == LOCAL_RF) { // configure RF parameters only
    fill_rf_config(ru,ru->rf_config_file);
767
    init_frame_parms(ru->frame_parms,1);
laurent's avatar
laurent committed
768
    phy_init_RU(ru);
769
    init_rf(ru);
laurent's avatar
laurent committed
770
  }
laurent's avatar
laurent committed
771

laurent's avatar
laurent committed
772
  AssertFatal(setup_RU_buffers(ru)==0, "Exiting, cannot initialize RU Buffers\n");
laurent's avatar
laurent committed
773 774
  LOG_I(PHY, "Signaling main thread that RU %d is ready\n",ru->idx);
  wait_sync("ru_thread");
laurent's avatar
laurent committed
775

laurent's avatar
laurent committed
776
  // Start RF device if any
777 778
  if (ru->rfdevice.trx_start_func(&ru->rfdevice) != 0)
    LOG_E(HW,"Could not start the RF device\n");
Laurent Thomas's avatar
Laurent Thomas committed
779 780
  else
    LOG_I(PHY,"RU %d rf device ready\n",ru->idx);
laurent's avatar
laurent committed
781

laurent's avatar
laurent committed
782 783 784
  // This is a forever while loop, it loops over subframes which are scheduled by incoming samples from HW devices
  while (!oai_exit) {
    // synchronization on input FH interface, acquire signals/data and block
laurent's avatar
laurent committed
785
    rx_rf(ru, &L1proc);
laurent's avatar
laurent committed
786
    // do RX front-end processing (frequency-shift, dft) if needed
laurent's avatar
laurent committed
787 788
    fep_full(ru, L1proc.subframe_rx);

laurent's avatar
laurent committed
789 790 791 792 793 794 795
    // At this point, all information for subframe has been received on FH interface
    // If this proc is to provide synchronization, do so
    // Fixme: not used
    // wakeup_slaves(proc);
    for (int i=0; i<ru->num_eNB; i++) {
      char string[20];
      sprintf(string,"Incoming RU %d",ru->idx);
laurent's avatar
laurent committed
796 797 798 799

      if (rxtx(ru->eNB_list[i],&L1proc,string) < 0)
        LOG_E(PHY,"eNB %d CC_id %d failed during execution\n",
              ru->eNB_list[i]->Mod_id,ru->eNB_list[i]->CC_id);
laurent's avatar
laurent committed
800
    }
laurent's avatar
laurent committed
801

laurent's avatar
laurent committed
802
    // do TX front-end processing if needed (precoding and/or IDFTs)
laurent's avatar
laurent committed
803
    feptx_prec(ru, L1proc.frame_tx, L1proc.subframe_tx);
laurent's avatar
laurent committed
804
    // do OFDM if needed
laurent's avatar
laurent committed
805
    feptx_ofdm(ru, L1proc.frame_tx, L1proc.subframe_tx);
laurent's avatar
laurent committed
806
    // do outgoing fronthaul (south) if needed
laurent's avatar
laurent committed
807
    ocp_tx_rf(ru, &L1proc);
laurent's avatar
laurent committed
808
  }
laurent's avatar
laurent committed
809

laurent's avatar
laurent committed
810
  LOG_W(PHY,"Exiting ru_thread \n");
811 812
  ru->rfdevice.trx_end_func(&ru->rfdevice);
  LOG_I(PHY,"RU %d rf device stopped\n",ru->idx);
laurent's avatar
laurent committed
813
  return NULL;
laurent's avatar
laurent committed
814 815
}

816 817 818 819 820 821 822 823
int init_rf(RU_t *ru) {
  char name[256];
  pthread_getname_np(pthread_self(),name, 255);
  pthread_setname_np(pthread_self(),"UHD for OAI");
  int ret=openair0_device_load(&ru->rfdevice,&ru->openair0_cfg);
  pthread_setname_np(pthread_self(),name);
  return ret;
}
Laurent Thomas's avatar
Laurent Thomas committed
824

825
void ocp_init_RU(RU_t *ru, char *rf_config_file, int send_dmrssync) {
laurent's avatar
laurent committed
826 827 828 829
  PHY_VARS_eNB *eNB0= (PHY_VARS_eNB *)NULL;
  int i;
  int CC_id;
  // read in configuration file)
laurent's avatar
laurent committed
830
  LOG_I(PHY,"configuring RU from file\n");
laurent's avatar
laurent committed
831
  LOG_I(PHY,"number of L1 instances %d, number of RU %d, number of CPU cores %d\n",
laurent's avatar
laurent committed
832
        RC.nb_L1_inst,RC.nb_RU,get_nprocs());
laurent's avatar
laurent committed
833 834 835

  if (RC.nb_CC != 0)
    for (i=0; i<RC.nb_L1_inst; i++)
laurent's avatar
laurent committed
836 837
      for (CC_id=0; CC_id<RC.nb_CC[i]; CC_id++)
        RC.eNB[i][CC_id]->num_RU=0;
laurent's avatar
laurent committed
838 839

  LOG_D(PHY,"Process RUs RC.nb_RU:%d\n",RC.nb_RU);
Laurent Thomas's avatar
Laurent Thomas committed
840 841 842
  ru->rf_config_file = rf_config_file;
  ru->idx          = 0;
  ru->ts_offset    = 0;
laurent's avatar
laurent committed
843

Laurent Thomas's avatar
Laurent Thomas committed
844 845 846 847 848 849 850
  if (ru->is_slave == 1) {
    ru->in_synch    = 0;
    ru->generate_dmrs_sync = 0;
  } else {
    ru->in_synch    = 1;
    ru->generate_dmrs_sync=send_dmrssync;
  }
laurent's avatar
laurent committed
851

Laurent Thomas's avatar
Laurent Thomas committed
852 853
  ru->cmd      = EMPTY;
  ru->south_out_cnt= 0;
laurent's avatar
laurent committed
854

Laurent Thomas's avatar
Laurent Thomas committed
855 856 857 858 859
  //    ru->generate_dmrs_sync = (ru->is_slave == 0) ? 1 : 0;
  if (ru->generate_dmrs_sync == 1) {
    generate_ul_ref_sigs();
    ru->dmrssync = (int16_t *)malloc16_clear(ru->frame_parms->ofdm_symbol_size*2*sizeof(int16_t));
  }
laurent's avatar
laurent committed
860

Laurent Thomas's avatar
Laurent Thomas committed
861 862
  ru->wakeup_L1_sleeptime = 2000;
  ru->wakeup_L1_sleep_cnt_max  = 3;
laurent's avatar
laurent committed
863

Laurent Thomas's avatar
Laurent Thomas committed
864 865 866 867 868 869 870
  if (ru->num_eNB > 0) {
    AssertFatal(ru->eNB_list[0], "ru->eNB_list is not initialized\n");
  } else {
    LOG_E(PHY,"Wrong data model, assigning eNB 0, carrier 0 to RU 0\n");
    ru->eNB_list[0] = RC.eNB[0][0];
    ru->num_eNB=1;
  }
laurent's avatar
laurent committed
871

Laurent Thomas's avatar
Laurent Thomas committed
872 873 874
  eNB0 = ru->eNB_list[0];
  // datamodel error in regular OAI: a RU uses one single eNB carrier parameters!
  ru->frame_parms = &eNB0->frame_parms;
laurent's avatar
laurent committed
875

Laurent Thomas's avatar
Laurent Thomas committed
876 877 878 879 880
  for (i=0; i<ru->num_eNB; i++) {
    eNB0 = ru->eNB_list[i];
    int ruIndex=eNB0->num_RU++;
    eNB0->RU_list[ruIndex] = ru;
  }
laurent's avatar
laurent committed
881 882 883 884 885
}

void stop_RU(int nb_ru) {
  for (int inst = 0; inst < nb_ru; inst++) {
    LOG_I(PHY, "Stopping RU %d processing threads\n", inst);
886
    //kill_RU_proc(RC.ru[inst]);
laurent's avatar
laurent committed
887 888
  }
}
laurent's avatar
laurent committed
889

laurent's avatar
laurent committed
890 891
/* --------------------------------------------------------*/
/* from here function to use configuration module          */
892
static int DEFBFW[] = {0x00007fff};
Laurent Thomas's avatar
Laurent Thomas committed
893
void ocpRCconfig_RU(RU_t *ru) {
laurent's avatar
laurent committed
894 895 896 897
  paramdef_t RUParams[] = RUPARAMS_DESC;
  paramlist_def_t RUParamList = {CONFIG_STRING_RU_LIST,NULL,0};
  config_getlist( &RUParamList,RUParams,sizeof(RUParams)/sizeof(paramdef_t), NULL);

laurent's avatar
laurent committed
898 899
  if ( RUParamList.numelt == 0  ) {
    LOG_W(PHY, "Calling RCconfig_RU while no ru\n");
laurent's avatar
laurent committed
900 901 902 903
    RC.nb_RU = 0;
    return;
  } // setting != NULL

904 905 906
  ru->idx = 0;
  ru->if_timing = synch_to_ext_device;
  paramdef_t *vals=RUParamList.paramarray[0];
laurent's avatar
laurent committed
907

908 909 910 911
  if (RC.nb_L1_inst >0)
    ru->num_eNB = vals[RU_ENB_LIST_IDX].numelt;
  else
    ru->num_eNB = 0;
laurent's avatar
laurent committed
912

913 914
  for (int i=0; i<ru->num_eNB; i++)
    ru->eNB_list[i] = RC.eNB[vals[RU_ENB_LIST_IDX].iptr[i]][0];
laurent's avatar
laurent committed
915

916 917 918
  if (config_isparamset(vals, RU_SDR_ADDRS)) {
    ru->openair0_cfg.sdr_addrs = strdup(*(vals[RU_SDR_ADDRS].strptr));
  }
laurent's avatar
laurent committed
919

920 921 922
  if (config_isparamset(vals, RU_SDR_CLK_SRC)) {
    char *paramVal=*(vals[RU_SDR_CLK_SRC].strptr);
    LOG_D(PHY, "RU clock source set as %s\n", paramVal);
Laurent Thomas's avatar
Laurent Thomas committed
923

924 925 926 927 928 929 930 931
    if (strcmp(paramVal, "internal") == 0) {
      ru->openair0_cfg.clock_source = internal;
    } else if (strcmp(paramVal, "external") == 0) {
      ru->openair0_cfg.clock_source = external;
    } else if (strcmp(paramVal, "gpsdo") == 0) {
      ru->openair0_cfg.clock_source = gpsdo;
    } else {
      LOG_E(PHY, "Erroneous RU clock source in the provided configuration file: '%s'\n", paramVal);
laurent's avatar
laurent committed
932
    }
933
  }
laurent's avatar
laurent committed
934

935 936
  if (strcmp(*(vals[RU_LOCAL_RF_IDX].strptr), "yes") == 0) {
    if ( !(config_isparamset(vals,RU_LOCAL_IF_NAME_IDX)) ) {
Laurent Thomas's avatar
Laurent Thomas committed
937 938
      ru->if_south  = LOCAL_RF;
      ru->function  = eNodeB_3GPP;
laurent's avatar
laurent committed
939
    } else {
Laurent Thomas's avatar
Laurent Thomas committed
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
      ru->eth_params.local_if_name = strdup(*(vals[RU_LOCAL_IF_NAME_IDX].strptr));
      ru->eth_params.my_addr       = strdup(*(vals[RU_LOCAL_ADDRESS_IDX].strptr));
      ru->eth_params.remote_addr   = strdup(*(vals[RU_REMOTE_ADDRESS_IDX].strptr));
      ru->eth_params.my_portc      = *(vals[RU_LOCAL_PORTC_IDX].uptr);
      ru->eth_params.remote_portc  = *(vals[RU_REMOTE_PORTC_IDX].uptr);
      ru->eth_params.my_portd      = *(vals[RU_LOCAL_PORTD_IDX].uptr);
      ru->eth_params.remote_portd  = *(vals[RU_REMOTE_PORTD_IDX].uptr);
    }

    ru->max_pdschReferenceSignalPower = *(vals[RU_MAX_RS_EPRE_IDX].uptr);;
    ru->max_rxgain                    = *(vals[RU_MAX_RXGAIN_IDX].uptr);
    ru->num_bands                     = vals[RU_BAND_LIST_IDX].numelt;
    /* sf_extension is in unit of samples for 30.72MHz here, has to be scaled later */
    ru->sf_extension                  = *(vals[RU_SF_EXTENSION_IDX].uptr);
    ru->end_of_burst_delay            = *(vals[RU_END_OF_BURST_DELAY_IDX].uptr);
laurent's avatar
laurent committed
955

Laurent Thomas's avatar
Laurent Thomas committed
956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
    for (int i=0; i<ru->num_bands; i++)
      ru->band[i] = vals[RU_BAND_LIST_IDX].iptr[i];
  } else {
    ru->eth_params.local_if_name    = strdup(*(vals[RU_LOCAL_IF_NAME_IDX].strptr));
    ru->eth_params.my_addr          = strdup(*(vals[RU_LOCAL_ADDRESS_IDX].strptr));
    ru->eth_params.remote_addr      = strdup(*(vals[RU_REMOTE_ADDRESS_IDX].strptr));
    ru->eth_params.my_portc         = *(vals[RU_LOCAL_PORTC_IDX].uptr);
    ru->eth_params.remote_portc     = *(vals[RU_REMOTE_PORTC_IDX].uptr);
    ru->eth_params.my_portd         = *(vals[RU_LOCAL_PORTD_IDX].uptr);
    ru->eth_params.remote_portd     = *(vals[RU_REMOTE_PORTD_IDX].uptr);
  }  /* strcmp(local_rf, "yes") != 0 */

  ru->nb_tx                             = *(vals[RU_NB_TX_IDX].uptr);
  ru->nb_rx                             = *(vals[RU_NB_RX_IDX].uptr);
  ru->att_tx                            = *(vals[RU_ATT_TX_IDX].uptr);
  ru->att_rx                            = *(vals[RU_ATT_RX_IDX].uptr);
laurent's avatar
laurent committed
972 973
  return;
}
laurent's avatar
laurent committed
974 975 976 977


static void get_options(void) {
  CONFIG_SETRTFLAG(CONFIG_NOEXITONHELP);
978
  get_common_options(SOFTMODEM_ENB_BIT);
laurent's avatar
laurent committed
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
  CONFIG_CLEARRTFLAG(CONFIG_NOEXITONHELP);

  if ( !(CONFIG_ISFLAGSET(CONFIG_ABORT)) ) {
    memset((void *)&RC,0,sizeof(RC));
    /* Read RC configuration file */
    RCConfig();
    NB_eNB_INST = RC.nb_inst;
    printf("Configuration: nb_rrc_inst %d, nb_L1_inst %d, nb_ru %d\n",NB_eNB_INST,RC.nb_L1_inst,RC.nb_RU);

    if (!IS_SOFTMODEM_NONBIOT) {
      load_NB_IoT();
      printf("               nb_nbiot_rrc_inst %d, nb_nbiot_L1_inst %d, nb_nbiot_macrlc_inst %d\n",
             RC.nb_nb_iot_rrc_inst, RC.nb_nb_iot_L1_inst, RC.nb_nb_iot_macrlc_inst);
    } else {
      printf("All Nb-IoT instances disabled\n");
      RC.nb_nb_iot_rrc_inst=RC.nb_nb_iot_L1_inst=RC.nb_nb_iot_macrlc_inst=0;
    }
  }
}

void set_default_frame_parms(LTE_DL_FRAME_PARMS *frame_parms[MAX_NUM_CCs]) {
  int CC_id;

  for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
    frame_parms[CC_id] = (LTE_DL_FRAME_PARMS *) malloc(sizeof(LTE_DL_FRAME_PARMS));
    /* Set some default values that may be overwritten while reading options */
    frame_parms[CC_id]->frame_type          = FDD;
    frame_parms[CC_id]->tdd_config          = 3;
    frame_parms[CC_id]->tdd_config_S        = 0;
    frame_parms[CC_id]->N_RB_DL             = 100;
    frame_parms[CC_id]->N_RB_UL             = 100;
    frame_parms[CC_id]->Ncp                 = NORMAL;
    frame_parms[CC_id]->Ncp_UL              = NORMAL;
    frame_parms[CC_id]->Nid_cell            = 0;
    frame_parms[CC_id]->num_MBSFN_config    = 0;
    frame_parms[CC_id]->nb_antenna_ports_eNB  = 1;
    frame_parms[CC_id]->nb_antennas_tx      = 1;
    frame_parms[CC_id]->nb_antennas_rx      = 1;
    frame_parms[CC_id]->nushift             = 0;
    frame_parms[CC_id]->phich_config_common.phich_resource = oneSixth;
    frame_parms[CC_id]->phich_config_common.phich_duration = normal;
    // UL RS Config
    frame_parms[CC_id]->pusch_config_common.ul_ReferenceSignalsPUSCH.cyclicShift = 0;//n_DMRS1 set to 0
    frame_parms[CC_id]->pusch_config_common.ul_ReferenceSignalsPUSCH.groupHoppingEnabled = 0;
    frame_parms[CC_id]->pusch_config_common.ul_ReferenceSignalsPUSCH.sequenceHoppingEnabled = 0;
    frame_parms[CC_id]->pusch_config_common.ul_ReferenceSignalsPUSCH.groupAssignmentPUSCH = 0;
    frame_parms[CC_id]->prach_config_common.rootSequenceIndex=22;
    frame_parms[CC_id]->prach_config_common.prach_ConfigInfo.zeroCorrelationZoneConfig=1;
    frame_parms[CC_id]->prach_config_common.prach_ConfigInfo.prach_ConfigIndex=0;
    frame_parms[CC_id]->prach_config_common.prach_ConfigInfo.highSpeedFlag=0;
    frame_parms[CC_id]->prach_config_common.prach_ConfigInfo.prach_FreqOffset=0;
    //    downlink_frequency[CC_id][0] = 2680000000; // Use float to avoid issue with frequency over 2^31.
    //    downlink_frequency[CC_id][1] = downlink_frequency[CC_id][0];
    //    downlink_frequency[CC_id][2] = downlink_frequency[CC_id][0];
    //    downlink_frequency[CC_id][3] = downlink_frequency[CC_id][0];
    //printf("Downlink for CC_id %d frequency set to %u\n", CC_id, downlink_frequency[CC_id][0]);
    frame_parms[CC_id]->dl_CarrierFreq=downlink_frequency[CC_id][0];
  }
}

void init_pdcp(void) {
  if (!NODE_IS_DU(RC.rrc[0]->node_type)) {
    pdcp_layer_init();
    uint32_t pdcp_initmask = (IS_SOFTMODEM_NOS1) ?
                             (PDCP_USE_NETLINK_BIT | LINK_ENB_PDCP_TO_IP_DRIVER_BIT) : LINK_ENB_PDCP_TO_GTPV1U_BIT;

    if (IS_SOFTMODEM_NOS1)
      pdcp_initmask = pdcp_initmask | ENB_NAS_USE_TUN_BIT | SOFTMODEM_NOKRNMOD_BIT  ;

    pdcp_initmask = pdcp_initmask | ENB_NAS_USE_TUN_W_MBMS_BIT;

    if ( split73!=SPLIT73_DU)
1051
      pdcp_module_init(pdcp_initmask, 0);
laurent's avatar
laurent committed
1052 1053

    if (NODE_IS_CU(RC.rrc[0]->node_type)) {
1054
      //pdcp_set_rlc_data_req_func(proto_agent_send_rlc_data_req);
laurent's avatar
laurent committed
1055
    } else {
1056 1057
      pdcp_set_rlc_data_req_func(rlc_data_req);
      pdcp_set_pdcp_data_ind_func(pdcp_data_ind);
laurent's avatar
laurent committed
1058 1059
    }
  } else {
1060
    //pdcp_set_pdcp_data_ind_func(proto_agent_send_pdcp_data_ind);
laurent's avatar
laurent committed
1061 1062 1063 1064
  }
}

static  void wait_nfapi_init(char *thread_name) {
1065
  LOG_I(ENB_APP, "waiting for NFAPI PNF connection and population of global structure (%s)\n",thread_name);
laurent's avatar
laurent committed
1066 1067 1068 1069 1070 1071
  pthread_mutex_lock( &nfapi_sync_mutex );

  while (nfapi_sync_var<0)
    pthread_cond_wait( &nfapi_sync_cond, &nfapi_sync_mutex );

  pthread_mutex_unlock(&nfapi_sync_mutex);
1072
  LOG_I(ENB_APP, "NFAPI: got sync (%s)\n", thread_name);
laurent's avatar
laurent committed
1073 1074 1075 1076 1077 1078
}

void terminate_task(module_id_t mod_id, task_id_t from, task_id_t to) {
  LOG_I(ENB_APP, "sending TERMINATE_MESSAGE from task %s (%d) to task %s (%d)\n",
        itti_get_task_name(from), from, itti_get_task_name(to), to);
  MessageDef *msg;
1079
  msg = itti_alloc_new_message (from, 0, TERMINATE_MESSAGE);
laurent's avatar
laurent committed
1080 1081 1082 1083 1084 1085 1086 1087 1088
  itti_send_msg_to_task (to, ENB_MODULE_ID_TO_INSTANCE(mod_id), msg);
}

int stop_L1L2(module_id_t enb_id) {
  LOG_W(ENB_APP, "stopping lte-softmodem\n");
  /* these tasks need to pick up new configuration */
  terminate_task(enb_id, TASK_ENB_APP, TASK_RRC_ENB);
  oai_exit = 1;
  LOG_I(ENB_APP, "calling kill_RU_proc() for instance %d\n", enb_id);
1089
  //kill_RU_proc(RC.ru[enb_id]);
laurent's avatar
laurent committed
1090 1091 1092 1093 1094 1095 1096 1097 1098
  LOG_I(ENB_APP, "calling kill_eNB_proc() for instance %d\n", enb_id);
  kill_eNB_proc(enb_id);
  oai_exit = 0;

  for (int cc_id = 0; cc_id < RC.nb_CC[enb_id]; cc_id++) {
    free_transport(RC.eNB[enb_id][cc_id]);
    phy_free_lte_eNB(RC.eNB[enb_id][cc_id]);
  }

1099
  //phy_free_RU(RC.ru[enb_id]);
laurent's avatar
laurent committed
1100 1101 1102 1103 1104 1105 1106 1107
  free_lte_top();
  return 0;
}

/*
 * Restart the lte-softmodem after it has been soft-stopped with stop_L1L2()
 */
int restart_L1L2(module_id_t enb_id) {
1108
  RU_t *ru = NULL; //RC.ru[enb_id];
laurent's avatar
laurent committed
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
  MessageDef *msg_p = NULL;
  LOG_W(ENB_APP, "restarting lte-softmodem\n");
  /* block threads */
  pthread_mutex_lock(&sync_mutex);
  sync_var = -1;
  pthread_mutex_unlock(&sync_mutex);
  /* copy the changed frame parameters to the RU */
  /* TODO this should be done for all RUs associated to this eNB */
  memcpy(&ru->frame_parms, &RC.eNB[enb_id][0]->frame_parms, sizeof(LTE_DL_FRAME_PARMS));
  /* reset the list of connected UEs in the MAC, since in this process with
   * loose all UEs (have to reconnect) */
laurent's avatar
laurent committed
1120
  init_UE_info(&RC.mac[enb_id]->UE_info);
laurent's avatar
laurent committed
1121 1122 1123 1124 1125 1126
  LOG_I(ENB_APP, "attempting to create ITTI tasks\n");
  // No more rrc thread, as many race conditions are hidden behind
  rrc_enb_init();
  itti_mark_task_ready(TASK_RRC_ENB);
  /* pass a reconfiguration request which will configure everything down to
   * RC.eNB[i][j]->frame_parms, too */
1127
  msg_p = itti_alloc_new_message(TASK_ENB_APP, 0, RRC_CONFIGURATION_REQ);
laurent's avatar
laurent committed
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
  RRC_CONFIGURATION_REQ(msg_p) = RC.rrc[enb_id]->configuration;
  itti_send_msg_to_task(TASK_RRC_ENB, ENB_MODULE_ID_TO_INSTANCE(enb_id), msg_p);
  init_eNB_afterRU();
  printf("Sending sync to all threads\n");
  pthread_mutex_lock(&sync_mutex);
  sync_var=0;
  pthread_cond_broadcast(&sync_cond);
  pthread_mutex_unlock(&sync_mutex);
  return 0;
}

int main ( int argc, char **argv ) {
1140
  //mtrace();
laurent's avatar
laurent committed
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
  int i;
  int CC_id = 0;
  int node_type = ngran_eNB;
  AssertFatal(load_configmodule(argc,argv,0), "[SOFTMODEM] Error, configuration module init failed\n");
  logInit();
  printf("Reading in command-line options\n");
  get_options ();
  AssertFatal(!CONFIG_ISFLAGSET(CONFIG_ABORT),"Getting configuration failed\n");
  EPC_MODE_ENABLED = !IS_SOFTMODEM_NOS1;
#if T_TRACER
  T_Config_Init();
#endif
  configure_linux();
1154
  set_softmodem_sighandler();
laurent's avatar
laurent committed
1155 1156 1157 1158 1159 1160
  cpuf=get_cpu_freq_GHz();
  set_taus_seed (0);

  if (opp_enabled ==1)
    reset_opp_meas();

Laurent Thomas's avatar
Laurent Thomas committed
1161
  itti_init(TASK_MAX, tasks_info);
laurent's avatar
laurent committed
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
  init_opt();
#ifndef PACKAGE_VERSION
#  define PACKAGE_VERSION "UNKNOWN-EXPERIMENTAL"
#endif
  LOG_I(HW, "Version: %s\n", PACKAGE_VERSION);

  /* Read configuration */
  if (RC.nb_inst > 0) {
    // Allocate memory from RC variable
    read_config_and_init();
  } else {
    printf("RC.nb_inst = 0, Initializing L1\n");
    RCconfig_L1();
  }

1177 1178 1179 1180 1181
  // RU thread and some L1 procedure aren't necessary in VNF or L2 FAPI simulator.
  // but RU thread deals with pre_scd and this is necessary in VNF and simulator.
  // some initialization is necessary and init_ru_vnf do this.
  RU_t ru;

laurent's avatar
laurent committed
1182 1183
  /* We need to read RU configuration before FlexRAN starts so it knows what
   * splits to report. Actual RU start comes later. */
1184 1185
  if  ( NFAPI_MODE != NFAPI_MODE_VNF) {
    ocpRCconfig_RU(&ru);
laurent's avatar
laurent committed
1186 1187 1188 1189 1190 1191
    LOG_I(PHY,
          "number of L1 instances %d, number of RU %d, number of CPU cores %d\n",
          RC.nb_L1_inst, RC.nb_RU, get_nprocs());
  }

  if ( strlen(get_softmodem_params()->split73) > 0 ) {
Laurent Thomas's avatar
Laurent Thomas committed
1192
    char tmp[1024]= {0};
laurent's avatar
laurent committed
1193 1194
    strncpy(tmp,get_softmodem_params()->split73, 1023);
    tmp[2]=0;
Laurent Thomas's avatar
Laurent Thomas committed
1195

laurent's avatar
laurent committed
1196 1197 1198 1199 1200 1201 1202
    if ( strncasecmp(tmp,"cu", 2)==0 )
      split73=SPLIT73_CU;
    else if ( strncasecmp(tmp,"du", 2)==0 )
      split73=SPLIT73_DU;
    else
      AssertFatal(false,"split73 syntax: <cu|du>:<remote ip addr>[:<ip port>] (string found: %s) \n",get_softmodem_params()->split73);
  }
Laurent Thomas's avatar
Laurent Thomas committed
1203

laurent's avatar
laurent committed
1204 1205 1206
  if (RC.nb_inst > 0) {
    /* Start the agent. If it is turned off in the configuration, it won't start */
    for (i = 0; i < RC.nb_inst; i++) {
1207
      //flexran_agent_start(i);
laurent's avatar
laurent committed
1208
    }
Laurent Thomas's avatar
Laurent Thomas committed
1209

laurent's avatar
laurent committed
1210 1211 1212 1213 1214 1215
    /* initializes PDCP and sets correct RLC Request/PDCP Indication callbacks
     * for monolithic/F1 modes */
    init_pdcp();
    AssertFatal(create_tasks(1)==0,"cannot create ITTI tasks\n");

    for (int enb_id = 0; enb_id < RC.nb_inst; enb_id++) {
1216
      MessageDef *msg_p = itti_alloc_new_message (TASK_ENB_APP, 0, RRC_CONFIGURATION_REQ);
laurent's avatar
laurent committed
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
      RRC_CONFIGURATION_REQ(msg_p) = RC.rrc[enb_id]->configuration;
      itti_send_msg_to_task (TASK_RRC_ENB, ENB_MODULE_ID_TO_INSTANCE(enb_id), msg_p);
    }

    node_type = RC.rrc[0]->node_type;
  }

  if (RC.nb_inst > 0 && NODE_IS_CU(node_type)) {
    protocol_ctxt_t ctxt;
    ctxt.module_id = 0 ;
    ctxt.instance = 0;
    ctxt.rnti = 0;
    ctxt.enb_flag = 1;
    ctxt.frame = 0;
    ctxt.subframe = 0;
    pdcp_run(&ctxt);
  }

  /* start threads if only L1 or not a CU */
  if (RC.nb_inst == 0 || !NODE_IS_CU(node_type) || NFAPI_MODE == NFAPI_MODE_PNF || NFAPI_MODE == NFAPI_MODE_VNF) {
    // init UE_PF_PO and mutex lock
    pthread_mutex_init(&ue_pf_po_mutex, NULL);
    memset (&UE_PF_PO[0][0], 0, sizeof(UE_PF_PO_t)*MAX_MOBILES_PER_ENB*MAX_NUM_CCs);
    pthread_cond_init(&sync_cond,NULL);
    pthread_mutex_init(&sync_mutex, NULL);

    if (NFAPI_MODE!=NFAPI_MONOLITHIC) {
      LOG_I(ENB_APP,"NFAPI*** - mutex and cond created - will block shortly for completion of PNF connection\n");
      pthread_cond_init(&sync_cond,NULL);
      pthread_mutex_init(&sync_mutex, NULL);
    }

    if (NFAPI_MODE==NFAPI_MODE_VNF) {// VNF
#if defined(PRE_SCD_THREAD)
      init_ru_vnf();  // ru pointer is necessary for pre_scd.
#endif
      wait_nfapi_init("main?");
    }

    LOG_I(ENB_APP,"START MAIN THREADS\n");
    // start the main threads
    number_of_cards = 1;
    printf("RC.nb_L1_inst:%d\n", RC.nb_L1_inst);

    if (RC.nb_L1_inst > 0) {
      printf("Initializing eNB threads single_thread_flag:%d wait_for_sync:%d\n",
             get_softmodem_params()->single_thread_flag,
             get_softmodem_params()->wait_for_sync);
      init_eNB(get_softmodem_params()->single_thread_flag,
               get_softmodem_params()->wait_for_sync);
    }

    for (int x=0; x < RC.nb_L1_inst; x++)
      for (int CC_id=0; CC_id<RC.nb_L1_CC[x]; CC_id++) {
        L1_rxtx_proc_t *L1proc= &RC.eNB[x][CC_id]->proc.L1_proc;
        L1proc->threadPool=(tpool_t *)malloc(sizeof(tpool_t));
        L1proc->respEncode=(notifiedFIFO_t *) malloc(sizeof(notifiedFIFO_t));
        L1proc->respDecode=(notifiedFIFO_t *) malloc(sizeof(notifiedFIFO_t));

        if ( strlen(get_softmodem_params()->threadPoolConfig) > 0 )
          initTpool(get_softmodem_params()->threadPoolConfig, L1proc->threadPool, true);
        else
          initTpool("n", L1proc->threadPool, true);

        initNotifiedFIFO(L1proc->respEncode);
        initNotifiedFIFO(L1proc->respDecode);
      }
  }

  printf("About to Init RU threads RC.nb_RU:%d\n", RC.nb_RU);

  if (RC.nb_RU >0 && NFAPI_MODE!=NFAPI_MODE_VNF) {
    printf("Initializing RU threads\n");
1290
    ocp_init_RU(&ru,
Laurent Thomas's avatar
Laurent Thomas committed
1291 1292 1293 1294 1295
                get_softmodem_params()->rf_config_file,
                get_softmodem_params()->send_dmrs_sync);
    ru.rf_map.card=0;
    ru.rf_map.chain=CC_id+(get_softmodem_params()->chain_offset);
    init_RU_proc(&ru);
laurent's avatar
laurent committed
1296 1297 1298 1299 1300 1301 1302 1303 1304
    config_sync_var=0;

    if (NFAPI_MODE==NFAPI_MODE_PNF) { // PNF
      wait_nfapi_init("main?");
    }

    LOG_I(ENB_APP,"RC.nb_RU:%d\n", RC.nb_RU);
    // once all RUs are ready intiailize the rest of the eNBs ((dependence on final RU parameters after configuration)
    printf("ALL RUs ready - init eNBs\n");
1305
    sleep(1);
laurent's avatar
laurent committed
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333

    if (NFAPI_MODE!=NFAPI_MODE_PNF && NFAPI_MODE!=NFAPI_MODE_VNF) {
      LOG_I(ENB_APP,"Not NFAPI mode - call init_eNB_afterRU()\n");
      init_eNB_afterRU();
    } else {
      LOG_I(ENB_APP,"NFAPI mode - DO NOT call init_eNB_afterRU()\n");
    }

    LOG_UI(ENB_APP,"ALL RUs ready - ALL eNBs ready\n");
    // connect the TX/RX buffers
    sleep(1); /* wait for thread activation */
    LOG_I(ENB_APP,"Sending sync to all threads\n");
    pthread_mutex_lock(&sync_mutex);
    sync_var=0;
    pthread_cond_broadcast(&sync_cond);
    pthread_mutex_unlock(&sync_mutex);
    config_check_unknown_cmdlineopt(CONFIG_CHECKALLSECTIONS);
  }

  create_tasks_mbms(1);
  // wait for end of program
  LOG_UI(ENB_APP,"TYPE <CTRL-C> TO TERMINATE\n");
  // CI -- Flushing the std outputs for the previous marker to show on the eNB / DU / CU log file
  fflush(stdout);
  fflush(stderr);

  // end of CI modifications
  //getchar();
1334
  if(IS_SOFTMODEM_DOSCOPE)
1335
    load_softscope("enb", NULL);
laurent's avatar
laurent committed
1336 1337 1338 1339 1340 1341 1342

  itti_wait_tasks_end();
  oai_exit=1;
  LOG_I(ENB_APP,"oai_exit=%d\n",oai_exit);
  // stop threads

  if (RC.nb_inst == 0 || !NODE_IS_CU(node_type)) {
1343
    if(IS_SOFTMODEM_DOSCOPE)
laurent's avatar
laurent committed
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
      end_forms();

    LOG_I(ENB_APP,"stopping MODEM threads\n");
    stop_eNB(NB_eNB_INST);
    stop_RU(RC.nb_RU);

    /* release memory used by the RU/eNB threads (incomplete), after all
     * threads have been stopped (they partially use the same memory) */
    for (int inst = 0; inst < NB_eNB_INST; inst++) {
      for (int cc_id = 0; cc_id < RC.nb_CC[inst]; cc_id++) {
        free_transport(RC.eNB[inst][cc_id]);
        phy_free_lte_eNB(RC.eNB[inst][cc_id]);
      }
    }

    free_lte_top();
    end_configmodule();
    pthread_cond_destroy(&sync_cond);
    pthread_mutex_destroy(&sync_mutex);
    pthread_cond_destroy(&nfapi_sync_cond);
    pthread_mutex_destroy(&nfapi_sync_mutex);
    pthread_mutex_destroy(&ue_pf_po_mutex);
  }

  terminate_opt();
  logClean();
  printf("Bye.\n");
  return 0;
}