main-ocp.c 50.2 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 76 77
#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;
uint16_t sf_ahead=4;
int otg_enabled;
laurent's avatar
laurent committed
78
uint64_t  downlink_frequency[MAX_NUM_CCs][4];
laurent's avatar
laurent committed
79 80
int32_t   uplink_frequency_offset[MAX_NUM_CCs][4];
int split73;
81
int usrp_tx_thread = 0;
laurent's avatar
laurent committed
82 83
char * split73_config;
int split73;
84 85 86 87 88 89 90 91 92
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) {
}
int proto_agent_start(mod_id_t mod_id, const cudu_params_t *p){
        return 0;
}
void proto_agent_stop(mod_id_t mod_id){
}
laurent's avatar
laurent committed
93 94 95 96 97 98 99 100 101 102

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
103 104 105 106 107 108 109 110 111 112 113 114 115
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
116

117 118
// Fixme: there are many mistakes in the datamodel and in redondant variables
// TDD is also mode complex
119
void setAllfromTS(uint64_t TS, L1_rxtx_proc_t *proc) {
120 121 122 123 124 125 126 127 128 129 130 131 132 133
  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;
134
      proc->subframe_tx=  (TStx  / fp->samples_per_tti)%10;
135 136 137 138 139 140
    }
  }

  return;
}

laurent's avatar
laurent committed
141
void init_RU_proc(RU_t *ru) {
laurent's avatar
laurent committed
142
  pthread_t t;
laurent's avatar
laurent committed
143 144 145 146 147 148 149 150 151

  switch(split73) {
  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:
laurent's avatar
laurent committed
152
    threadCreate(&t,  ru_thread, (void *)ru, "MainRu", -1, OAI_PRIORITY_RT_MAX);
laurent's avatar
laurent committed
153
  }
laurent's avatar
laurent committed
154 155
}

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

laurent's avatar
laurent committed
161
  for (int i=0; i<NUMBER_OF_UE_MAX; i++) {
laurent's avatar
laurent committed
162 163
    LOG_D(PHY,"Allocating Transport Channel Buffers for DLSCH, UE %d\n",i);

laurent's avatar
laurent committed
164 165 166 167 168
    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
169 170 171
    }

    LOG_D(PHY,"Allocating Transport Channel Buffer for ULSCH, UE %d\n",i);
laurent's avatar
laurent committed
172 173
    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
174 175 176 177 178 179
    // 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
180 181
  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
182 183 184 185 186 187 188 189
  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
190
  for(int i=0; i<NUMBER_OF_UE_MAX; i++)
laurent's avatar
laurent committed
191 192 193 194 195 196 197 198 199 200
    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;
}

void init_eNB_afterRU(void) {
laurent's avatar
laurent committed
201 202 203
  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
204 205
      phy_init_lte_eNB(eNB,0,0);
      eNB->frame_parms.nb_antennas_rx       = 0;
laurent's avatar
laurent committed
206
      eNB->frame_parms.nb_antennas_tx       = 0;
laurent's avatar
laurent committed
207 208 209 210 211 212
      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
213
      for (int ru_id=0,aa=0; ru_id<eNB->num_RU; ru_id++) {
laurent's avatar
laurent committed
214
        eNB->frame_parms.nb_antennas_rx    += eNB->RU_list[ru_id]->nb_rx;
laurent's avatar
laurent committed
215
        eNB->frame_parms.nb_antennas_tx    += eNB->RU_list[ru_id]->nb_tx;
laurent's avatar
laurent committed
216 217 218 219 220 221 222
        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
223
        for (int i=0; i<eNB->RU_list[ru_id]->nb_rx; aa++,i++) {
laurent's avatar
laurent committed
224
          LOG_I(PHY,"Attaching RU %d antenna %d to eNB antenna %d\n",eNB->RU_list[ru_id]->idx,i,aa);
225
          eNB->prach_vars.rxsigF[0][aa]    =  eNB->RU_list[ru_id]->prach_rxsigF[0][i];
laurent's avatar
laurent committed
226 227 228 229 230 231 232 233

          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];

          eNB->common_vars.rxdataF[aa]     =  eNB->RU_list[ru_id]->common.rxdataF[i];
        }
      }

laurent's avatar
laurent committed
234 235
      AssertFatal( eNB->frame_parms.nb_antennas_rx > 0 && eNB->frame_parms.nb_antennas_rx < 4, "");
      AssertFatal( eNB->frame_parms.nb_antennas_tx > 0 && eNB->frame_parms.nb_antennas_rx < 4, "");
laurent's avatar
laurent committed
236 237 238 239 240 241 242 243
      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
244
  AssertFatal(RC.eNB != NULL,"RC.eNB must have been allocated\n");
laurent's avatar
laurent committed
245

laurent's avatar
laurent committed
246 247
  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
248

laurent's avatar
laurent committed
249 250 251
    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
252 253 254 255 256 257 258
      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
259
      pthread_mutex_init( &eNB->UL_INFO_mutex, NULL);
laurent's avatar
laurent committed
260 261 262 263 264
      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
265 266
      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
267 268 269
      eNB->prach_energy_counter = 0;
    }
  }
270 271

  SET_LOG_DEBUG(PRACH);
laurent's avatar
laurent committed
272 273 274 275 276 277 278 279
}

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
280

laurent's avatar
laurent committed
281 282 283
// this is for RU with local RF unit
void fill_rf_config(RU_t *ru, char *rf_config_file) {
  int i;
284
  LTE_DL_FRAME_PARMS *fp   = ru->frame_parms;
laurent's avatar
laurent committed
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
  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
313
      LOG_E(PHY,"Wrong input for numerology %d\n setting to 20MHz normal CP configuration",numerology);
laurent's avatar
laurent committed
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 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;
  } else AssertFatal(1==0,"Unknown N_RB_DL %d\n",fp->N_RB_DL);

  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
353 354 355 356 357
    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
358 359 360 361 362 363 364 365 366 367
  }
}

/* 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
368
  AssertFatal(ru, "ru is NULL");
369
  frame_parms = ru->frame_parms;
laurent's avatar
laurent committed
370
  LOG_I(PHY,"setup_RU_buffers: frame_parms = %p\n",frame_parms);
laurent's avatar
laurent committed
371 372

  if (frame_parms->frame_type == TDD) {
laurent's avatar
laurent committed
373
    if (frame_parms->N_RB_DL == 100) {
laurent's avatar
laurent committed
374
      ru->N_TA_offset = 624;
laurent's avatar
laurent committed
375
    } else if (frame_parms->N_RB_DL == 50) {
laurent's avatar
laurent committed
376
      ru->N_TA_offset = 624/2;
laurent's avatar
laurent committed
377 378 379
      ru->sf_extension       /= 2;
      ru->end_of_burst_delay /= 2;
    } else if (frame_parms->N_RB_DL == 25) {
laurent's avatar
laurent committed
380
      ru->N_TA_offset = 624/4;
laurent's avatar
laurent committed
381 382 383
      ru->sf_extension       /= 4;
      ru->end_of_burst_delay /= 4;
    } else {
laurent's avatar
laurent committed
384
      LOG_E(PHY,"not handled, todo\n");
laurent's avatar
laurent committed
385 386 387 388 389 390 391 392 393 394
      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
395

396
#if 0
laurent's avatar
laurent committed
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
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;
          }
        }
      }
    }
  }
}
429
#endif
laurent's avatar
laurent committed
430

431
void ocp_rx_prach(PHY_VARS_eNB *eNB,
laurent's avatar
laurent committed
432 433 434 435 436 437 438 439 440
                  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) {
441 442
  int i;
  int prach_mask=0;
laurent's avatar
laurent committed
443

444 445
  if (br_flag == 0) {
    rx_prach0(eNB,ru,proc->frame_prach, proc->subframe_prach,
laurent's avatar
laurent committed
446
              max_preamble,max_preamble_energy,max_preamble_delay,avg_preamble_energy,Nf,tdd_mapindex,0,0);
447 448 449 450 451 452 453 454 455 456 457 458 459 460
  } 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)
        if (eNB->prach_vars_br.repetition_number[i]==0) eNB->prach_vars_br.first_frame[i]=proc->frame_prach_br;

        // 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
461
                  max_preamble,max_preamble_energy,max_preamble_delay,avg_preamble_energy,Nf,tdd_mapindex,1,i);
462 463 464 465 466 467 468 469 470 471

        // 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 */
}

472
void prach_procedures_ocp(PHY_VARS_eNB *eNB, L1_rxtx_proc_t *proc, int br_flag) {
laurent's avatar
laurent committed
473 474 475 476 477 478 479 480 481
  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++) {
482
      eNB->prach_vars.rxsigF[0][aa] = eNB->RU_list[i]->prach_rxsigF[0][ru_aa];
laurent's avatar
laurent committed
483 484 485 486 487 488 489
      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
490

laurent's avatar
laurent committed
491
  // run PRACH detection for CE-level 0 only for now when br_flag is set
492
  ocp_rx_prach(eNB,
laurent's avatar
laurent committed
493 494 495 496 497 498 499 500 501 502
               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
              );
503 504 505 506 507 508
  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
509 510 511

  if (br_flag==1) {
    int             prach_mask;
512
    prach_mask = is_prach_subframe (&eNB->frame_parms, proc->frame_prach_br, proc->subframe_prach_br);
laurent's avatar
laurent committed
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
    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
532
        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
533 534 535 536 537 538 539 540 541 542 543 544 545 546
        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
547
      } */// ce_level
laurent's avatar
laurent committed
548 549 550 551 552
  } 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,
553 554
          proc->frame_prach,
          proc->subframe_prach,
laurent's avatar
laurent committed
555 556 557 558 559 560 561 562 563
          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;
564
    eNB->UL_INFO.rach_ind.sfn_sf                                    = proc->frame_prach<<4 | proc->subframe_prach;
laurent's avatar
laurent committed
565 566 567
    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];
568
    eNB->preamble_list[0].preamble_rel8.rnti                  = 1+proc->subframe_prach;  // note: fid is implicitly 0 here
laurent's avatar
laurent committed
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
    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

593
void prach_eNB(PHY_VARS_eNB *eNB, L1_rxtx_proc_t *proc, int frame,int subframe) {
laurent's avatar
laurent committed
594
  // check if we have to detect PRACH first
laurent's avatar
laurent committed
595
  if (is_prach_subframe(&eNB->frame_parms, frame,subframe)>0) {
596 597
    prach_procedures_ocp(eNB, proc, 0);
    prach_procedures_ocp(eNB, proc, 1);
laurent's avatar
laurent committed
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
  }
}

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), "");
613
  prach_eNB(eNB,proc,proc->frame_rx,proc->subframe_rx);
laurent's avatar
laurent committed
614 615 616 617 618 619 620 621 622 623 624 625
  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;
626
  eNB->if_inst->UL_indication(&eNB->UL_INFO, proc);
laurent's avatar
laurent committed
627 628 629 630 631
  pthread_mutex_unlock(&eNB->UL_INFO_mutex);
  phy_procedures_eNB_TX(eNB, proc, 1);
  return(0);
}

632
void rx_rf(RU_t *ru, L1_rxtx_proc_t *proc) {
633
  LTE_DL_FRAME_PARMS *fp = ru->frame_parms;
laurent's avatar
laurent committed
634 635 636
  void *rxp[ru->nb_rx];
  unsigned int rxs;
  int i;
637 638
  openair0_timestamp ts=0, timestamp_rx;
  static openair0_timestamp old_ts=0;
639

laurent's avatar
laurent committed
640
  for (i=0; i<ru->nb_rx; i++)
641
    //receive in the next slot
642
    rxp[i] = (void *)&ru->common.rxdata[i][((proc->subframe_rx+1)%10)*fp->samples_per_tti];
laurent's avatar
laurent committed
643 644 645 646 647 648

  rxs = ru->rfdevice.trx_read_func(&ru->rfdevice,
                                   &ts,
                                   rxp,
                                   fp->samples_per_tti,
                                   ru->nb_rx);
649
  timestamp_rx = ts-ru->ts_offset;
laurent's avatar
laurent committed
650 651 652 653 654

  //  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);
655 656 657 658 659 660
#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
661 662
  }

663
  if (old_ts != 0 && timestamp_rx - old_ts != fp->samples_per_tti) {
664 665 666
    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
667
  }
668

669 670
  old_ts=timestamp_rx;
  setAllfromTS(timestamp_rx, proc);
laurent's avatar
laurent committed
671 672
}

673 674
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
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 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
  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
721
    ru->rfdevice.trx_write_func(&ru->rfdevice,
722 723 724 725 726
                                proc->timestamp_tx+ru->ts_offset-ru->openair0_cfg.tx_sample_advance-sf_extension,
                                txp,
                                siglen+sf_extension,
                                ru->nb_tx,
                                flags);
727 728
    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
729
  }
730

731
  return;
laurent's avatar
laurent committed
732 733
}

laurent's avatar
laurent committed
734
static void *ru_thread( void *param ) {
735 736
  setbuf(stdout, NULL);
  setbuf(stderr, NULL);
laurent's avatar
laurent committed
737
  RU_t *ru = (RU_t *)param;
738
  L1_rxtx_proc_t L1proc= {0};
laurent's avatar
laurent committed
739 740 741 742 743
  // 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
744 745
  if (ru->if_south == LOCAL_RF) { // configure RF parameters only
    fill_rf_config(ru,ru->rf_config_file);
746
    init_frame_parms(ru->frame_parms,1);
laurent's avatar
laurent committed
747
    phy_init_RU(ru);
748
    init_rf(ru);
laurent's avatar
laurent committed
749
  }
laurent's avatar
laurent committed
750

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

laurent's avatar
laurent committed
755
  // Start RF device if any
756 757 758
  if (ru->rfdevice.trx_start_func(&ru->rfdevice) != 0)
    LOG_E(HW,"Could not start the RF device\n");
  else LOG_I(PHY,"RU %d rf device ready\n",ru->idx);
laurent's avatar
laurent committed
759

laurent's avatar
laurent committed
760 761 762
  // 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
763
    rx_rf(ru, &L1proc);
laurent's avatar
laurent committed
764
    // do RX front-end processing (frequency-shift, dft) if needed
laurent's avatar
laurent committed
765 766
    fep_full(ru, L1proc.subframe_rx);

laurent's avatar
laurent committed
767 768 769 770 771 772 773
    // 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
774 775 776 777

      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
778
    }
laurent's avatar
laurent committed
779

laurent's avatar
laurent committed
780
    // do TX front-end processing if needed (precoding and/or IDFTs)
laurent's avatar
laurent committed
781
    feptx_prec(ru, L1proc.frame_tx, L1proc.subframe_tx);
laurent's avatar
laurent committed
782
    // do OFDM if needed
laurent's avatar
laurent committed
783
    feptx_ofdm(ru, L1proc.frame_tx, L1proc.subframe_tx);
laurent's avatar
laurent committed
784
    // do outgoing fronthaul (south) if needed
laurent's avatar
laurent committed
785
    ocp_tx_rf(ru, &L1proc);
laurent's avatar
laurent committed
786
  }
laurent's avatar
laurent committed
787

laurent's avatar
laurent committed
788
  LOG_W(PHY,"Exiting ru_thread \n");
789 790
  ru->rfdevice.trx_end_func(&ru->rfdevice);
  LOG_I(PHY,"RU %d rf device stopped\n",ru->idx);
laurent's avatar
laurent committed
791
  return NULL;
laurent's avatar
laurent committed
792 793
}

794 795 796 797 798 799 800 801 802
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;
}

803
void ocp_init_RU(RU_t *ru, char *rf_config_file, int send_dmrssync) {
laurent's avatar
laurent committed
804 805 806 807
  PHY_VARS_eNB *eNB0= (PHY_VARS_eNB *)NULL;
  int i;
  int CC_id;
  // read in configuration file)
laurent's avatar
laurent committed
808
  LOG_I(PHY,"configuring RU from file\n");
laurent's avatar
laurent committed
809
  LOG_I(PHY,"number of L1 instances %d, number of RU %d, number of CPU cores %d\n",
laurent's avatar
laurent committed
810
        RC.nb_L1_inst,RC.nb_RU,get_nprocs());
laurent's avatar
laurent committed
811 812 813

  if (RC.nb_CC != 0)
    for (i=0; i<RC.nb_L1_inst; i++)
laurent's avatar
laurent committed
814 815
      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
816 817 818 819

  LOG_D(PHY,"Process RUs RC.nb_RU:%d\n",RC.nb_RU);

    ru->rf_config_file = rf_config_file;
820
    ru->idx          = 0;
laurent's avatar
laurent committed
821
    ru->ts_offset    = 0;
laurent's avatar
laurent committed
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836

    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;
    }

    ru->cmd      = EMPTY;
    ru->south_out_cnt= 0;

    //    ru->generate_dmrs_sync = (ru->is_slave == 0) ? 1 : 0;
    if (ru->generate_dmrs_sync == 1) {
      generate_ul_ref_sigs();
837
      ru->dmrssync = (int16_t *)malloc16_clear(ru->frame_parms->ofdm_symbol_size*2*sizeof(int16_t));
laurent's avatar
laurent committed
838 839 840 841
    }

    ru->wakeup_L1_sleeptime = 2000;
    ru->wakeup_L1_sleep_cnt_max  = 3;
laurent's avatar
laurent committed
842 843

    if (ru->num_eNB > 0) {
laurent's avatar
laurent committed
844 845 846 847 848
      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
849 850
    }

laurent's avatar
laurent committed
851
    eNB0 = ru->eNB_list[0];
laurent's avatar
laurent committed
852 853
    // datamodel error in regular OAI: a RU uses one single eNB carrier parameters!
    ru->frame_parms = &eNB0->frame_parms;
laurent's avatar
laurent committed
854

laurent's avatar
laurent committed
855 856 857 858
    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
859 860 861 862 863 864
    }
}

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

laurent's avatar
laurent committed
869 870
/* --------------------------------------------------------*/
/* from here function to use configuration module          */
871
static int DEFBFW[] = {0x00007fff};
872
void ocpRCconfig_RU(RU_t* ru) {
laurent's avatar
laurent committed
873 874 875 876
  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
877 878
  if ( RUParamList.numelt == 0  ) {
    LOG_W(PHY, "Calling RCconfig_RU while no ru\n");
laurent's avatar
laurent committed
879 880 881 882
    RC.nb_RU = 0;
    return;
  } // setting != NULL

883 884 885
  ru->idx = 0;
  ru->if_timing = synch_to_ext_device;
  paramdef_t *vals=RUParamList.paramarray[0];
laurent's avatar
laurent committed
886

887 888 889 890
  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
891

892 893
  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
894

895 896 897
  if (config_isparamset(vals, RU_SDR_ADDRS)) {
    ru->openair0_cfg.sdr_addrs = strdup(*(vals[RU_SDR_ADDRS].strptr));
  }
laurent's avatar
laurent committed
898

899 900 901 902 903 904 905 906 907 908 909 910
  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);
    
    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
911
    }
912
  }
laurent's avatar
laurent committed
913

914 915 916 917
  if (strcmp(*(vals[RU_LOCAL_RF_IDX].strptr), "yes") == 0) {
    if ( !(config_isparamset(vals,RU_LOCAL_IF_NAME_IDX)) ) {
        ru->if_south  = LOCAL_RF;
        ru->function  = eNodeB_3GPP;
laurent's avatar
laurent committed
918
      } else {
919 920 921 922 923 924 925
        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);
laurent's avatar
laurent committed
926 927
      }

928 929 930
      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;
laurent's avatar
laurent committed
931
      /* sf_extension is in unit of samples for 30.72MHz here, has to be scaled later */
932 933
      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
934

935 936
      for (int i=0; i<ru->num_bands; i++)
	ru->band[i] = vals[RU_BAND_LIST_IDX].iptr[i];
laurent's avatar
laurent committed
937
    } else {
938 939 940 941 942 943 944
      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);
laurent's avatar
laurent committed
945 946
    }  /* strcmp(local_rf, "yes") != 0 */

947 948 949 950
    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
951 952 953

  return;
}
laurent's avatar
laurent committed
954 955 956 957


static void get_options(void) {
  CONFIG_SETRTFLAG(CONFIG_NOEXITONHELP);
958
  get_common_options(SOFTMODEM_ENB_BIT);
laurent's avatar
laurent committed
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 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
  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)
      pdcp_module_init(pdcp_initmask);

    if (NODE_IS_CU(RC.rrc[0]->node_type)) {
1034
      //pdcp_set_rlc_data_req_func((send_rlc_data_req_func_t)proto_agent_send_rlc_data_req);
laurent's avatar
laurent committed
1035 1036 1037 1038 1039
    } else {
      pdcp_set_rlc_data_req_func((send_rlc_data_req_func_t) rlc_data_req);
      pdcp_set_pdcp_data_ind_func((pdcp_data_ind_func_t) pdcp_data_ind);
    }
  } else {
1040
    //pdcp_set_pdcp_data_ind_func((pdcp_data_ind_func_t) proto_agent_send_pdcp_data_ind);
laurent's avatar
laurent committed
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
  }
}

static  void wait_nfapi_init(char *thread_name) {
  printf( "waiting for NFAPI PNF connection and population of global structure (%s)\n",thread_name);
  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);
  printf( "NFAPI: got sync (%s)\n", thread_name);
}

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;
1059
  msg = itti_alloc_new_message (from, 0, TERMINATE_MESSAGE);
laurent's avatar
laurent committed
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
  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);
1070
  //kill_RU_proc(RC.ru[enb_id]);
laurent's avatar
laurent committed
1071 1072 1073 1074 1075 1076 1077 1078 1079
  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]);
  }

1080
  //phy_free_RU(RC.ru[enb_id]);
laurent's avatar
laurent committed
1081 1082 1083 1084 1085 1086 1087 1088
  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) {
1089
  RU_t *ru = NULL; //RC.ru[enb_id];
laurent's avatar
laurent committed
1090 1091 1092 1093 1094 1095
  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);
1096

laurent's avatar
laurent committed
1097 1098 1099
  /* 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));
1100

laurent's avatar
laurent committed
1101 1102
  /* 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
1103
  init_UE_info(&RC.mac[enb_id]->UE_info);
laurent's avatar
laurent committed
1104 1105 1106 1107 1108 1109
  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 */
1110
  msg_p = itti_alloc_new_message(TASK_ENB_APP, 0, RRC_CONFIGURATION_REQ);
laurent's avatar
laurent committed
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
  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 ) {
1123
  //mtrace();
laurent's avatar
laurent committed
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
  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();
1137
  set_softmodem_sighandler();
laurent's avatar
laurent committed
1138 1139 1140 1141 1142 1143
  cpuf=get_cpu_freq_GHz();
  set_taus_seed (0);

  if (opp_enabled ==1)
    reset_opp_meas();

Laurent Thomas's avatar
Laurent Thomas committed
1144
  itti_init(TASK_MAX, tasks_info);
laurent's avatar
laurent committed
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
  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();
  }

1160 1161 1162 1163 1164 1165
  // 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
1166 1167
  /* We need to read RU configuration before FlexRAN starts so it knows what
   * splits to report. Actual RU start comes later. */
1168 1169
  if  ( NFAPI_MODE != NFAPI_MODE_VNF) {
    ocpRCconfig_RU(&ru);
laurent's avatar
laurent committed
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
    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 ) {
    char tmp[1024]={0};
    strncpy(tmp,get_softmodem_params()->split73, 1023);
    tmp[2]=0;
    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);
  }
  
  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++) {
1190
      //flexran_agent_start(i);
laurent's avatar
laurent committed
1191 1192 1193 1194 1195 1196 1197 1198
    }
    
    /* 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++) {
1199
      MessageDef *msg_p = itti_alloc_new_message (TASK_ENB_APP, 0, RRC_CONFIGURATION_REQ);
laurent's avatar
laurent committed
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 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
      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);

1271

laurent's avatar
laurent committed
1272 1273
  if (RC.nb_RU >0 && NFAPI_MODE!=NFAPI_MODE_VNF) {
    printf("Initializing RU threads\n");
1274 1275
    ocp_init_RU(&ru,
	    get_softmodem_params()->rf_config_file,
laurent's avatar
laurent committed
1276 1277
            get_softmodem_params()->send_dmrs_sync);

1278 1279 1280
      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
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290

    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");
1291
    sleep(1);
laurent's avatar
laurent committed
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320

    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();
  if(IS_SOFTMODEM_DOFORMS)
1321
    load_softscope("enb", NULL);
laurent's avatar
laurent committed
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359

  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)) {
    if(IS_SOFTMODEM_DOFORMS)
      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;
}