oaisim.c 42.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <string.h>
#include <math.h>
#include <unistd.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <cblas.h>
#include <execinfo.h>

#include "SIMULATION/RF/defs.h"
#include "PHY/types.h"
#include "PHY/defs.h"
#include "PHY/LTE_TRANSPORT/proto.h"
#include "PHY/vars.h"
#include "MAC_INTERFACE/vars.h"

Cedric Roux's avatar
Cedric Roux committed
18 19
#include "SIMULATION/ETH_TRANSPORT/proto.h"

20 21 22
//#ifdef OPENAIR2
#include "LAYER2/MAC/defs.h"
#include "LAYER2/MAC/vars.h"
23
#include "pdcp.h"
24 25 26 27 28
#ifndef CELLULAR
#include "RRC/LITE/vars.h"
#endif
#include "PHY_INTERFACE/vars.h"
//#endif
29
#include "RRC/NAS/nas_config.h"
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

#include "ARCH/CBMIMO1/DEVICE_DRIVER/vars.h"

#ifdef IFFT_FPGA
//#include "PHY/LTE_REFSIG/mod_table.h"
#endif //IFFT_FPGA
#include "SCHED/defs.h"
#include "SCHED/vars.h"

#ifdef XFORMS
#include "PHY/TOOLS/lte_phy_scope.h"
#endif

#ifdef SMBV
#include "PHY/TOOLS/smbv.h"
char smbv_fname[] = "smbv_config_file.smbv";
unsigned short smbv_nframes = 4; // how many frames to configure 1,..,4
unsigned short config_frames[4] = {2,9,11,13};
// unsigned short config_frames[4] = {1};
49
unsigned char smbv_frame_cnt = 0;
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
u8 config_smbv = 0;
char smbv_ip[16];
#endif

#include "oaisim_functions.h"

#include "oaisim.h"
#include "oaisim_config.h"
#include "UTIL/OCG/OCG_extern.h"
#include "cor_SF_sim.h"
#include "UTIL/OMG/omg_constants.h"
#include "UTIL/FIFO/pad_list.h"

//#ifdef PROC
#include "../PROC/interface.h"
#include "../PROC/channel_sim_proc.h"
#include "../PROC/Tsync.h"
#include "../PROC/Process.h"
//#endif

#include "UTIL/LOG/vcd_signal_dumper.h"
71
#include "UTIL/OTG/otg_kpi.h"
72
#include "assertions.h"
73

winckel's avatar
winckel committed
74 75
#include "enb_app.h"

76 77
#if defined(ENABLE_ITTI)
# include "intertask_interface_init.h"
78
# include "timer.h"
79 80 81 82
# if defined(ENABLE_USE_MME)
#   include "s1ap_eNB.h"
#   include "sctp_eNB_task.h"
# endif
83
#endif
84 85 86 87 88 89 90 91 92

#define RF

//#define DEBUG_SIM

#define MCS_COUNT 24//added for PHY abstraction
#define N_TRIALS 1

/*
93 94 95 96
 DCI0_5MHz_TDD0_t          UL_alloc_pdu;
 DCI1A_5MHz_TDD_1_6_t      CCCH_alloc_pdu;
 DCI2_5MHz_2A_L10PRB_TDD_t DLSCH_alloc_pdu1;
 DCI2_5MHz_2A_M10PRB_TDD_t DLSCH_alloc_pdu2;
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
 */

#define UL_RB_ALLOC computeRIV(lte_frame_parms->N_RB_UL,0,24)
#define CCCH_RB_ALLOC computeRIV(lte_frame_parms->N_RB_UL,0,3)
#define RA_RB_ALLOC computeRIV(lte_frame_parms->N_RB_UL,0,3)
#define DLSCH_RB_ALLOC 0x1fff

#define DECOR_DIST 100
#define SF_VAR 10

//constant for OAISIM soft realtime calibration
#define SF_DEVIATION_OFFSET_NS 100000 //= 0.1ms : should be as a number of UE
#define SLEEP_STEP_US       100 //  = 0.01ms could be adaptive, should be as a number of UE
#define K 2                  // averaging coefficient 
#define TARGET_SF_TIME_NS 1000000       // 1ms = 1000000 ns
//#ifdef OPENAIR2
//u16 NODE_ID[1];
//u8 NB_INST = 2;
//#endif //OPENAIR2
116
u32 frame = 0;
117
//int eMBMS_active = 0;
118 119 120 121
char stats_buffer[16384];
channel_desc_t *eNB2UE[NUMBER_OF_eNB_MAX][NUMBER_OF_UE_MAX];
channel_desc_t *UE2eNB[NUMBER_OF_UE_MAX][NUMBER_OF_eNB_MAX];
//Added for PHY abstraction
122
node_desc_t *enb_data[NUMBER_OF_eNB_MAX];
123 124 125 126 127 128 129
node_desc_t *ue_data[NUMBER_OF_UE_MAX];
// Added for PHY abstraction
extern Node_list ue_node_list;
extern Node_list enb_node_list;
extern int pdcp_period, omg_period;

extern double **s_re, **s_im, **r_re, **r_im, **r_re0, **r_im0;
130
int map1, map2;
131
extern double **ShaF;
132
double snr_dB, sinr_dB, snr_direction; //,sinr_direction;
133
extern double snr_step;
134
extern u8 set_sinr; //,set_snr=0;
135 136
extern u8 ue_connection_test;
extern u8 set_seed;
137
u8 cooperation_flag; // for cooperative communication
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
extern u8 target_dl_mcs;
extern u8 target_ul_mcs;
u8 rate_adaptation_flag;
extern u8 abstraction_flag, ethernet_flag;
extern u16 Nid_cell;
extern struct timespec time_spec;
extern unsigned long time_last, time_now;
extern int td, td_avg, sleep_time_us;
extern LTE_DL_FRAME_PARMS *frame_parms;
//double sinr_bler_map[MCS_COUNT][2][16];
//double sinr_bler_map_up[MCS_COUNT][2][16];
//extern double SINRpost_eff[301];
//extern int mcsPost; 
//extern int  nrbPost; 
//extern int frbPost;
//extern void kpi_gen();

#ifdef XFORMS
int otg_enabled;
#endif

// this should reflect the channel models in openair1/SIMULATION/TOOLS/defs.h
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
mapping small_scale_names[] = {
  {"custom", custom},
  {"SCM_A", SCM_A},
  {"SCM_B", SCM_B},
  {"SCM_C", SCM_C},
  {"SCM_D", SCM_D},
  {"EPA", EPA},
  {"EVA", EVA},
  {"ETU", ETU},
  {"MBSFN", MBSFN},
  {"Rayleigh8", Rayleigh8},
  {"Rayleigh1", Rayleigh1},
  {"Rayleigh1_800", Rayleigh1_800},
  {"Rayleigh1_corr", Rayleigh1_corr},
  {"Rayleigh1_anticorr", Rayleigh1_anticorr},
  {"Rice8", Rice8},
  {"Rice1", Rice1},
  {"Rice1_corr", Rice1_corr},
  {"Rice1_anticorr", Rice1_anticorr},
  {"AWGN", AWGN},
  {NULL, -1}
};
182 183 184 185

//static void *sigh(void *arg);
void terminate(void);

186 187 188
void help(void) {
  printf (
      "Usage: oaisim -h -a -F -C tdd_config -V -R N_RB_DL -e -x transmission_mode -m target_dl_mcs -r(ate_adaptation) -n n_frames -s snr_dB -k ricean_factor -t max_delay -f forgetting factor -A channel_model -z cooperation_flag -u nb_local_ue -U UE mobility -b nb_local_enb -B eNB_mobility -M ethernet_flag -p nb_master -g multicast_group -l log_level -c ocg_enable -T traffic model -D multicast network device\n");
189 190 191

  printf ("-h provides this help message!\n");
  printf ("-a Activates PHY abstraction mode\n");
192 193
  printf (
      "-A set the multipath channel simulation,  options are: SCM_A, SCM_B, SCM_C, SCM_D, EPA, EVA, ETU, Rayleigh8, Rayleigh1, Rayleigh1_corr,Rayleigh1_anticorr, Rice8,, Rice1, AWGN \n");
194 195
  printf ("-b Set the number of local eNB\n");
  printf ("-B Set the mobility model for eNB, options are: STATIC, RWP, RWALK, \n");
196 197
  printf (
      "-c [1,2,3,4] Activate the config generator (OCG) to process the scenario descriptor, or give the scenario manually: -c template_1.xml \n");
198 199
  printf ("-C [0-6] Sets TDD configuration\n");
  printf ("-e Activates extended prefix mode\n");
200
  printf ("-E Random number generator seed\n");
201 202 203 204
  printf ("-f Set the forgetting factor for time-variation\n");
  printf ("-F Activates FDD transmission (TDD is default)\n");
  printf ("-g Set multicast group ID (0,1,2,3) - valid if M is set\n");
  printf ("-G Enable background traffic \n");
205
  printf ("-H Enable handover operation (default disabled) \n");
206 207 208 209 210 211 212 213 214 215 216 217 218
  printf ("-I Enable CLI interface (to connect use telnet localhost 1352)\n");
  printf ("-k Set the Ricean factor (linear)\n");
  printf ("-l Set the global log level (8:trace, 7:debug, 6:info, 4:warn, 3:error) \n");
  printf ("-L [0-1] 0 to disable new link adaptation, 1 to enable new link adapatation\n");
  printf ("-m Gives a fixed DL mcs\n");
  printf ("-M Set the machine ID for Ethernet-based emulation\n");
  printf ("-n Set the number of frames for the simulation\n");
  printf ("-O [mme ipv4 address] Enable MME mode\n");
  printf ("-p Set the total number of machine in emulation - valid if M is set\n");
  printf ("-P [trace type] Enable protocol analyzer. Possible values for OPT:\n");
  printf ("    - wireshark: Enable tracing of layers above PHY using an UDP socket\n");
  printf ("    - pcap:      Enable tracing of layers above PHY to a pcap file\n");
  printf ("    - tshark:    Not implemented yet\n");
219 220
  printf (
      "-Q Activate and set the MBMS service: 0 : not used (default eMBMS disabled), 1: eMBMS and RRC Connection enabled, 2: eMBMS relaying and RRC Connection enabled, 3: eMBMS enabled, RRC Connection disabled, 4: eMBMS relaying enabled, RRC Connection disabled\n");
221 222 223 224 225 226 227 228 229 230
  printf ("-R [6,15,25,50,75,100] Sets N_RB_DL\n");
  printf ("-r Activates rate adaptation (DL for now)\n");
  printf ("-s snr_dB set a fixed (average) SNR, this deactivates the openair channel model generator (OCM)\n");
  printf ("-S snir_dB set a fixed (average) SNIR, this deactivates the openair channel model generator (OCM)\n");
  printf ("-t Set the delay spread (microseconds)\n");
  printf ("-T activate the traffic generator: cbr, mcbr, bcbr, mscbr \n");
  printf ("-u Set the number of local UE\n");
  printf ("-U Set the mobility model for UE, options are: STATIC, RWP, RWALK \n");
  printf ("-V Enable VCD dump, file = openair_vcd_dump.vcd\n");
  printf ("-w number of CBA groups, if not specified or zero, CBA is inactive\n");
231 232
  printf (
      "-W IP address to connect to SMBV and configure SMBV from config file. Requires compilation with SMBV=1, -W0 uses default IP 192.168.12.201\n");
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
  printf ("-x Set the transmission mode (1,2,5,6 supported for now)\n");
  printf ("-Y Set the global log verbosity (none, low, medium, high, full) \n");
  printf ("-z Set the cooperation flag (0 for no cooperation, 1 for delay diversity and 2 for distributed alamouti\n");
  printf ("-Z Reserved\n");
}

pthread_t log_thread;

void log_thread_init() {
  //create log_list
  //log_list_init(&log_list);
#ifndef LOG_NO_THREAD

  log_shutdown = 0;

248
  if ((pthread_mutex_init (&log_lock, NULL) != 0) || (pthread_cond_init (&log_notify, NULL) != 0)) {
249 250 251
    return;
  }

252 253
  if (pthread_create (&log_thread, NULL, log_thread_function, (void*) NULL) != 0) {
    log_thread_finalize ();
254 255 256 257 258 259 260 261 262 263 264 265
    return;
  }
#endif

}

//Call it after the last LOG call
int log_thread_finalize() {
  int err = 0;

#ifndef LOG_NO_THREAD

266
  if (pthread_mutex_lock (&log_lock) != 0) {
267 268 269 270 271 272
    return -1;
  }

  log_shutdown = 1;

  /* Wake up LOG thread */
273
  if ((pthread_cond_broadcast (&log_notify) != 0) || (pthread_mutex_unlock (&log_lock) != 0)) {
274 275
    err = -1;
  }
276
  if (pthread_join (log_thread, NULL) != 0) {
277 278
    err = -1;
  }
279
  if (pthread_mutex_unlock (&log_lock) != 0) {
280 281 282
    err = -1;
  }

283
  if (!err) {
284
    //log_list_free(&log_list);
285 286 287
    pthread_mutex_lock (&log_lock);
    pthread_mutex_destroy (&log_lock);
    pthread_cond_destroy (&log_notify);
288
  }
Cedric Roux's avatar
Cedric Roux committed
289 290
#endif

291 292 293 294
  return err;
}

#ifdef OPENAIR2
295 296 297
int omv_write(int pfd, Node_list enb_node_list, Node_list ue_node_list, Data_Flow_Unit omv_data) {
  int i, j;
  omv_data.end = 0;
298
  //omv_data.total_num_nodes = NB_UE_INST + NB_eNB_INST;
299
  for (i = 0; i < NB_eNB_INST; i++) {
300
    if (enb_node_list != NULL) {
301 302
      omv_data.geo[i].x = (enb_node_list->node->X_pos < 0.0) ? 0.0 : enb_node_list->node->X_pos;
      omv_data.geo[i].y = (enb_node_list->node->Y_pos < 0.0) ? 0.0 : enb_node_list->node->Y_pos;
303 304 305 306
      omv_data.geo[i].z = 1.0;
      omv_data.geo[i].mobility_type = oai_emulation.info.omg_model_enb;
      omv_data.geo[i].node_type = 0; //eNB
      enb_node_list = enb_node_list->next;
307 308 309 310 311 312 313 314 315 316
      omv_data.geo[i].Neighbors = 0;
      for (j = NB_eNB_INST; j < NB_UE_INST + NB_eNB_INST; j++) {
        if (is_UE_active (i, j - NB_eNB_INST) == 1) {
          omv_data.geo[i].Neighbor[omv_data.geo[i].Neighbors] = j;
          omv_data.geo[i].Neighbors++;
          LOG_D(
              OMG,
              "[eNB %d][UE %d] is_UE_active(i,j) %d geo (x%d, y%d) num neighbors %d\n", i, j-NB_eNB_INST, is_UE_active(i,j-NB_eNB_INST), omv_data.geo[i].x, omv_data.geo[i].y, omv_data.geo[i].Neighbors);
        }
      }
317 318
    }
  }
319
  for (i = NB_eNB_INST; i < NB_UE_INST + NB_eNB_INST; i++) {
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
    if (ue_node_list != NULL) {
      omv_data.geo[i].x = (ue_node_list->node->X_pos < 0.0) ? 0.0 : ue_node_list->node->X_pos;
      omv_data.geo[i].y = (ue_node_list->node->Y_pos < 0.0) ? 0.0 : ue_node_list->node->Y_pos;
      omv_data.geo[i].z = 1.0;
      omv_data.geo[i].mobility_type = oai_emulation.info.omg_model_ue;
      omv_data.geo[i].node_type = 1; //UE
      //trial
      omv_data.geo[i].state = 1;
      omv_data.geo[i].rnti = 88;
      omv_data.geo[i].connected_eNB = 0;
      omv_data.geo[i].RSRP = 66;
      omv_data.geo[i].RSRQ = 55;
      omv_data.geo[i].Pathloss = 44;
      omv_data.geo[i].RSSI[0] = 33;
      omv_data.geo[i].RSSI[1] = 22;
335 336 337 338
      if ((sizeof(omv_data.geo[0].RSSI) / sizeof(omv_data.geo[0].RSSI[0])) > 2)
      {
        omv_data.geo[i].RSSI[2] = 11;
      }
339

340
      ue_node_list = ue_node_list->next;
341 342 343 344 345 346 347 348 349
      omv_data.geo[i].Neighbors = 0;
      for (j = 0; j < NB_eNB_INST; j++) {
        if (is_UE_active (j, i - NB_eNB_INST) == 1) {
          omv_data.geo[i].Neighbor[omv_data.geo[i].Neighbors] = j;
          omv_data.geo[i].Neighbors++;
          LOG_D(
              OMG,
              "[UE %d][eNB %d] is_UE_active  %d geo (x%d, y%d) num neighbors %d\n", i-NB_eNB_INST, j, is_UE_active(j,i-NB_eNB_INST), omv_data.geo[i].x, omv_data.geo[i].y, omv_data.geo[i].Neighbors);
        }
350 351 352
      }
    }
  }
353 354 355
  LOG_E(OMG, "pfd %d \n", pfd);
  if (write (pfd, &omv_data, sizeof(struct Data_Flow_Unit)) == -1)
    perror ("write omv failed");
356 357 358
  return 1;
}

359 360 361 362
void omv_end(int pfd, Data_Flow_Unit omv_data) {
  omv_data.end = 1;
  if (write (pfd, &omv_data, sizeof(struct Data_Flow_Unit)) == -1)
    perror ("write omv failed");
363 364 365
}
#endif 

366
#ifdef OPENAIR2
367 368
int pfd[2]; // fd for omv : fixme: this could be a local var
#endif
369

370 371 372 373 374 375 376
#ifdef OPENAIR2
static Data_Flow_Unit omv_data;
#endif //ALU
static s32 UE_id = 0, eNB_id = 0;
#ifdef Rel10
static s32 RN_id=0;
#endif
377

378
static void *l2l1_task(void *args_p) {
379 380 381
  // Framing variables
  s32 slot, last_slot, next_slot;

382
#ifdef Rel10
383
  relaying_type_t r_type=no_relay; // no relaying
384
#endif
385 386 387

  lte_subframe_t direction;

388
  char fname[64], vname[64];
389

390
#if defined(ENABLE_ITTI)
391
  MessageDef *message_p = NULL;
392

393
  itti_mark_task_ready (TASK_L2L1);
394

395 396 397 398 399 400 401 402 403 404
  if (NB_eNB_INST > 0) {
    /* Wait for the initialize message */
    do {
      if (message_p != NULL) {
        free (message_p);
      }
      itti_receive_msg (TASK_L2L1, &message_p);
    } while (ITTI_MSG_ID(message_p) != INITIALIZE_MESSAGE);
    free (message_p);
  }
405 406
#endif

407
  for (frame = 0; frame < oai_emulation.info.n_frames; frame++) {
408

409
#if defined(ENABLE_ITTI)
410 411
    do {
      // Checks if a message has been sent to L2L1 task
412
      itti_poll_msg (TASK_L2L1, &message_p);
413 414

      if (message_p != NULL) {
Cedric Roux's avatar
Cedric Roux committed
415
        switch (ITTI_MSG_ID(message_p)) {
416 417 418 419 420
          case TERMINATE_MESSAGE:
            itti_exit_task ();
            break;

          case MESSAGE_TEST:
winckel's avatar
winckel committed
421
            LOG_I(EMU, "Received %s\n", ITTI_MSG_NAME(message_p));
422 423 424
            break;

          default:
Cedric Roux's avatar
Cedric Roux committed
425
            LOG_E(EMU, "Received unexpected message %s\n", ITTI_MSG_NAME(message_p));
426 427
            break;
        }
428

429 430 431
        free (message_p);
      }
    } while(message_p != NULL);
432 433
#endif

434
    /*
435 436 437 438 439 440 441 442 443
     // Handling the cooperation Flag
     if (cooperation_flag == 2)
     {
     if ((PHY_vars_eNB_g[0]->eNB_UE_stats[0].mode == PUSCH) && (PHY_vars_eNB_g[0]->eNB_UE_stats[1].mode == PUSCH))
     PHY_vars_eNB_g[0]->cooperation_flag = 2;
     }
     */
    if (ue_connection_test == 1) {
      if ((frame % 20) == 0) {
444 445 446 447
        snr_dB += snr_direction;
        sinr_dB -= snr_direction;
      }
      if (snr_dB == -20) {
448
        snr_direction = snr_step;
449
      }
450 451 452 453
      else
        if (snr_dB == 20) {
          snr_direction = -snr_step;
        }
454 455
    }

456 457
    oai_emulation.info.frame = frame;
    //oai_emulation.info.time_ms += 1;
458 459
    oai_emulation.info.time_s += 0.1; // emu time in s, each frame lasts for 10 ms // JNote: TODO check the coherency of the time and frame (I corrected it to 10 (instead of 0.01)
    // if n_frames not set by the user or is greater than max num frame then set adjust the frame counter
460 461 462 463 464
    if ((oai_emulation.info.n_frames_flag == 0) || (oai_emulation.info.n_frames >= 0xffff)) {
      frame %= (oai_emulation.info.n_frames - 1);
    }

    update_omg (); // frequency is defined in the omg_global params configurable by the user
465

466
    update_omg_ocm ();
467 468 469

#ifdef OPENAIR2
    // check if pipe is still open
470 471
    if ((oai_emulation.info.omv_enabled == 1)) {
      omv_write (pfd[1], enb_node_list, ue_node_list, omv_data);
472
    }
473
#endif
474 475 476 477 478 479
#ifdef DEBUG_OMG
    if ((((int) oai_emulation.info.time_s) % 100) == 0) {
      for (UE_id = oai_emulation.info.first_ue_local; UE_id < (oai_emulation.info.first_ue_local + oai_emulation.info.nb_ue_local); UE_id++) {
        get_node_position (UE, UE_id);
      }
    }
480 481 482 483 484
#endif

    update_ocm ();

    for (slot = 0; slot < 20; slot++) {
485

486
      wait_for_slot_isr ();
487

488
#if defined(ENABLE_ITTI)
489
      itti_update_lte_time(frame, slot);
490
#endif
491

492 493 494 495
      last_slot = (slot - 1) % 20;
      if (last_slot < 0)
        last_slot += 20;
      next_slot = (slot + 1) % 20;
496

497
      oai_emulation.info.time_ms = frame * 10 + (slot >> 1);
498

499
      direction = subframe_select (frame_parms, next_slot >> 1);
500

501
#ifdef PROC
502
      if(Channel_Flag==1)
503
      Channel_Func(s_re2,s_im2,r_re2,r_im2,r_re02,r_im02,r_re0_d,r_im0_d,r_re0_u,r_im0_u,eNB2UE,UE2eNB,enb_data,ue_data,abstraction_flag,frame_parms,slot);
504 505 506 507

      if(Channel_Flag==0)
#endif
      {
508 509 510 511
#if defined(ENABLE_ITTI)
        log_set_instance_type (LOG_INSTANCE_ENB);
#endif

512 513
        if ((next_slot % 2) == 0)
          clear_eNB_transport_info (oai_emulation.info.nb_enb_local);
514

515 516 517
        for (eNB_id = oai_emulation.info.first_enb_local;
            (eNB_id < (oai_emulation.info.first_enb_local + oai_emulation.info.nb_enb_local))
                && (oai_emulation.info.cli_start_enb[eNB_id] == 1); eNB_id++) {
518 519
          //printf ("debug: Nid_cell %d\n", PHY_vars_eNB_g[eNB_id]->lte_frame_parms.Nid_cell);
          //printf ("debug: frame_type %d,tdd_config %d\n", PHY_vars_eNB_g[eNB_id]->lte_frame_parms.frame_type,PHY_vars_eNB_g[eNB_id]->lte_frame_parms.tdd_config);
520 521 522
          LOG_D(
              EMU,
              "PHY procedures eNB %d for frame %d, slot %d (subframe TX %d, RX %d) TDD %d/%d Nid_cell %d\n", eNB_id, frame, slot, next_slot >> 1, last_slot>>1, PHY_vars_eNB_g[eNB_id]->lte_frame_parms.frame_type, PHY_vars_eNB_g[eNB_id]->lte_frame_parms.tdd_config, PHY_vars_eNB_g[eNB_id]->lte_frame_parms.Nid_cell);
523

524
#ifdef OPENAIR2
525
          //Appliation: traffic gen
526
          update_otg_eNB (eNB_id, oai_emulation.info.time_ms);
527 528

          //IP/OTG to PDCP and PDCP to IP operation
529
          pdcp_run (frame, 1, 0, eNB_id); //PHY_vars_eNB_g[eNB_id]->Mod_id
530
#endif
531 532 533

          // PHY_vars_eNB_g[eNB_id]->frame = frame;
          phy_procedures_eNB_lte (last_slot, next_slot, PHY_vars_eNB_g[eNB_id], abstraction_flag, no_relay, NULL);
534 535

#ifdef PRINT_STATS
536 537 538 539 540 541 542 543 544
          if(last_slot==9 && frame%10==0)
          if(eNB_avg_thr)
          fprintf(eNB_avg_thr,"%d %d\n",PHY_vars_eNB_g[eNB_id]->frame,(PHY_vars_eNB_g[eNB_id]->total_system_throughput)/((PHY_vars_eNB_g[eNB_id]->frame+1)*10));
          if (eNB_stats[eNB_id]) {
            len = dump_eNB_stats(PHY_vars_eNB_g[eNB_id], stats_buffer, 0);
            rewind (eNB_stats[eNB_id]);
            fwrite (stats_buffer, 1, len, eNB_stats[eNB_id]);
            fflush(eNB_stats[eNB_id]);
          }
545
#ifdef OPENAIR2
546 547 548 549 550 551 552
          if (eNB_l2_stats) {
            len = dump_eNB_l2_stats (stats_buffer, 0);
            rewind (eNB_l2_stats);
            fwrite (stats_buffer, 1, len, eNB_l2_stats);
            fflush(eNB_l2_stats);
          }
#endif
553 554 555 556 557
#endif
        }
        // Call ETHERNET emulation here
        //emu_transport (frame, last_slot, next_slot, direction, oai_emulation.info.frame_type, ethernet_flag);

558 559 560 561
#if defined(ENABLE_ITTI)
        log_set_instance_type (LOG_INSTANCE_UE);
#endif

562 563 564 565
        if ((next_slot % 2) == 0)
          clear_UE_transport_info (oai_emulation.info.nb_ue_local);

        for (UE_id = oai_emulation.info.first_ue_local;
566 567 568 569 570 571 572 573 574 575 576 577
            (UE_id < (oai_emulation.info.first_ue_local + oai_emulation.info.nb_ue_local))
                && (oai_emulation.info.cli_start_ue[UE_id] == 1); UE_id++) {
          if (frame >= (UE_id * 20)) { // activate UE only after 20*UE_id frames so that different UEs turn on separately

            LOG_D(
                EMU,
                "PHY procedures UE %d for frame %d, slot %d (subframe TX %d, RX %d)\n", UE_id, frame, slot, next_slot >> 1, last_slot>>1);

            if (PHY_vars_UE_g[UE_id]->UE_mode[0] != NOT_SYNCHED) {
              if (frame > 0) {
                PHY_vars_UE_g[UE_id]->frame = frame;

578
#ifdef OPENAIR2
579 580 581 582 583
                //Application
                update_otg_UE (UE_id, oai_emulation.info.time_ms);

                //Access layer
                pdcp_run (frame, 0, UE_id, 0);
584
#endif
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615

                phy_procedures_UE_lte (last_slot, next_slot, PHY_vars_UE_g[UE_id], 0, abstraction_flag, normal_txrx,
                                       no_relay, NULL);
                ue_data[UE_id]->tx_power_dBm = PHY_vars_UE_g[UE_id]->tx_power_dBm;
              }
            }
            else {
              if (abstraction_flag == 1) {
                LOG_E(
                    EMU,
                    "sync not supported in abstraction mode (UE%d,mode%d)\n", UE_id, PHY_vars_UE_g[UE_id]->UE_mode[0]);
                exit (-1);
              }
              if ((frame > 0) && (last_slot == (LTE_SLOTS_PER_FRAME - 2))) {
                initial_sync (PHY_vars_UE_g[UE_id], normal_txrx);

                /*
                 write_output("dlchan00.m","dlch00",&(PHY_vars_UE_g[0]->lte_ue_common_vars.dl_ch_estimates[0][0][0]),(6*(PHY_vars_UE_g[0]->lte_frame_parms.ofdm_symbol_size)),1,1);
                 if (PHY_vars_UE_g[0]->lte_frame_parms.nb_antennas_rx>1)
                 write_output("dlchan01.m","dlch01",&(PHY_vars_UE_g[0]->lte_ue_common_vars.dl_ch_estimates[0][1][0]),(6*(PHY_vars_UE_g[0]->lte_frame_parms.ofdm_symbol_size)),1,1);
                 write_output("dlchan10.m","dlch10",&(PHY_vars_UE_g[0]->lte_ue_common_vars.dl_ch_estimates[0][2][0]),(6*(PHY_vars_UE_g[0]->lte_frame_parms.ofdm_symbol_size)),1,1);
                 if (PHY_vars_UE_g[0]->lte_frame_parms.nb_antennas_rx>1)
                 write_output("dlchan11.m","dlch11",&(PHY_vars_UE_g[0]->lte_ue_common_vars.dl_ch_estimates[0][3][0]),(6*(PHY_vars_UE_g[0]->lte_frame_parms.ofdm_symbol_size)),1,1);
                 write_output("rxsig.m","rxs",PHY_vars_UE_g[0]->lte_ue_common_vars.rxdata[0],PHY_vars_UE_g[0]->lte_frame_parms.samples_per_tti*10,1,1);
                 write_output("rxsigF.m","rxsF",PHY_vars_UE_g[0]->lte_ue_common_vars.rxdataF[0],2*PHY_vars_UE_g[0]->lte_frame_parms.symbols_per_tti*PHY_vars_UE_g[0]->lte_frame_parms.ofdm_symbol_size,2,1);
                 write_output("pbch_rxF_ext0.m","pbch_ext0",PHY_vars_UE_g[0]->lte_ue_pbch_vars[0]->rxdataF_ext[0],6*12*4,1,1);
                 write_output("pbch_rxF_comp0.m","pbch_comp0",PHY_vars_UE_g[0]->lte_ue_pbch_vars[0]->rxdataF_comp[0],6*12*4,1,1);
                 write_output("pbch_rxF_llr.m","pbch_llr",PHY_vars_UE_g[0]->lte_ue_pbch_vars[0]->llr,(frame_parms->Ncp==0) ? 1920 : 1728,1,4);
                 */
              }
            }
616
#ifdef PRINT_STATS
617 618 619 620 621 622 623 624 625
            if(last_slot==2 && frame%10==0)
            if (UE_stats_th[UE_id])
            fprintf(UE_stats_th[UE_id],"%d %d\n",frame, PHY_vars_UE_g[UE_id]->bitrate[0]/1000);
            if (UE_stats[UE_id]) {
              len = dump_ue_stats (PHY_vars_UE_g[UE_id], stats_buffer, 0, normal_txrx, 0);
              rewind (UE_stats[UE_id]);
              fwrite (stats_buffer, 1, len, UE_stats[UE_id]);
              fflush(UE_stats[UE_id]);
            }
626
#endif
627 628
          }
        }
629
#ifdef Rel10
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
        for (RN_id=oai_emulation.info.first_rn_local;
            RN_id<oai_emulation.info.first_rn_local+oai_emulation.info.nb_rn_local;
            RN_id++) {
          // UE id and eNB id of the RN
          UE_id= oai_emulation.info.first_ue_local+oai_emulation.info.nb_ue_local + RN_id;// NB_UE_INST + RN_id
          eNB_id= oai_emulation.info.first_enb_local+oai_emulation.info.nb_enb_local + RN_id;// NB_eNB_INST + RN_id
          // currently only works in FDD
          if (oai_emulation.info.eMBMS_active_state == 4) {
            r_type = multicast_relay;
            //LOG_I(EMU,"Activating the multicast relaying\n");
          }
          else {
            LOG_E(EMU,"Not supported eMBMS option when relaying is enabled %d\n", r_type);
            exit(-1);
          }
          PHY_vars_RN_g[RN_id]->frame = frame;
          if ( oai_emulation.info.frame_type == 0) {
            // RN == UE
            if (frame>0) {
              if (PHY_vars_UE_g[UE_id]->UE_mode[0] != NOT_SYNCHED) {
                LOG_D(EMU,"[RN %d] PHY procedures UE %d for frame %d, slot %d (subframe TX %d, RX %d)\n",
                    RN_id, UE_id, frame, slot, next_slot >> 1,last_slot>>1);
                PHY_vars_UE_g[UE_id]->frame = frame;
                phy_procedures_UE_lte (last_slot, next_slot, PHY_vars_UE_g[UE_id], 0, abstraction_flag,normal_txrx,
                    r_type, PHY_vars_RN_g[RN_id]);
              }
              else if (last_slot == (LTE_SLOTS_PER_FRAME-2)) {
                initial_sync(PHY_vars_UE_g[UE_id],normal_txrx);
              }
            }
            // RN == eNB
            LOG_D(EMU,"[RN %d] PHY procedures eNB %d for frame %d, slot %d (subframe TX %d, RX %d)\n",
                RN_id, eNB_id, frame, slot, next_slot >> 1,last_slot>>1);
            phy_procedures_eNB_lte (last_slot, next_slot, PHY_vars_eNB_g[eNB_id], abstraction_flag,
                r_type, PHY_vars_RN_g[RN_id]);
          }
          else {
            LOG_E(EMU,"TDD is not supported for multicast relaying %d\n", r_type);
            exit(-1);
          }
        }
671
#endif
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
        emu_transport (frame, last_slot, next_slot, direction, oai_emulation.info.frame_type, ethernet_flag);
        if ((direction == SF_DL) || (frame_parms->frame_type == 0)) {
          // consider only sec id 0
          /*  for (eNB_id=0;eNB_id<NB_eNB_INST;eNB_id++) {
           if (abstraction_flag == 0) {
           do_OFDM_mod(PHY_vars_eNB_g[eNB_id]->lte_eNB_common_vars.txdataF[0],
           PHY_vars_eNB_g[eNB_id]->lte_eNB_common_vars.txdata[0],
           frame,next_slot,
           frame_parms);
           }
           }*/
          for (UE_id = 0; UE_id < NB_UE_INST; UE_id++) {
            do_DL_sig (r_re0, r_im0, r_re, r_im, s_re, s_im, eNB2UE, enb_data, ue_data, next_slot, abstraction_flag,
                       frame_parms, UE_id);
          }
        }
        if ((direction == SF_UL) || (frame_parms->frame_type == 0)) { //if ((subframe<2) || (subframe>4))
          do_UL_sig (r_re0, r_im0, r_re, r_im, s_re, s_im, UE2eNB, enb_data, ue_data, next_slot, abstraction_flag,
                     frame_parms, frame);
691
          /*
692 693 694 695 696 697 698 699
           int ccc;
           fprintf(SINRpost,"SINRdb For eNB New Subframe : \n ");
           for(ccc = 0 ; ccc<301; ccc++)
           {
           fprintf(SINRpost,"_ %f ", SINRpost_eff[ccc]);
           }
           fprintf(SINRpost,"SINRdb For eNB : %f \n ", SINRpost_eff[ccc]);
           */
700
        }
701 702 703 704 705 706 707 708 709 710 711 712 713
        if ((direction == SF_S)) { //it must be a special subframe
          if (next_slot % 2 == 0) { //DL part
            /*  for (eNB_id=0;eNB_id<NB_eNB_INST;eNB_id++) {
             if (abstraction_flag == 0) {
             do_OFDM_mod(PHY_vars_eNB_g[eNB_id]->lte_eNB_common_vars.txdataF[0],
             PHY_vars_eNB_g[eNB_id]->lte_eNB_common_vars.txdata[0],
             frame,next_slot,
             frame_parms);
             }
             }*/
            for (UE_id = 0; UE_id < NB_UE_INST; UE_id++) {
              do_DL_sig (r_re0, r_im0, r_re, r_im, s_re, s_im, eNB2UE, enb_data, ue_data, next_slot, abstraction_flag,
                         frame_parms, UE_id);
714
            }
715 716 717 718 719 720
            /*
             for (aarx=0;aarx<UE2eNB[1][0]->nb_rx;aarx++)
             for (aatx=0;aatx<UE2eNB[1][0]->nb_tx;aatx++)
             for (k=0;k<UE2eNB[1][0]->channel_length;k++)
             printf("SB(%d,%d,%d)->(%f,%f)\n",k,aarx,aatx,UE2eNB[1][0]->ch[aarx+(aatx*UE2eNB[1][0]->nb_rx)][k].r,UE2eNB[1][0]->ch[aarx+(aatx*UE2eNB[1][0]->nb_rx)][k].i);
             */
721
          }
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
          else { // UL part
            do_UL_sig (r_re0, r_im0, r_re, r_im, s_re, s_im, UE2eNB, enb_data, ue_data, next_slot, abstraction_flag,
                       frame_parms, frame);

            /*        int ccc;
             fprintf(SINRpost,"SINRdb For eNB New Subframe : \n ");
             for(ccc = 0 ; ccc<301; ccc++)
             {
             fprintf(SINRpost,"_ %f ", SINRpost_eff[ccc]);
             }
             fprintf(SINRpost,"SINRdb For eNB : %f \n ", SINRpost_eff[ccc]);
             }
             */}
        }
        if ((last_slot == 1) && (frame == 0) && (abstraction_flag == 0) && (oai_emulation.info.n_frames == 1)) {
737

738
          write_output ("dlchan0.m", "dlch0", &(PHY_vars_UE_g[0]->lte_ue_common_vars.dl_ch_estimates[0][0][0]),
739
                        (6 * (PHY_vars_UE_g[0]->lte_frame_parms.ofdm_symbol_size)), 1, 1);
740
          write_output ("dlchan1.m", "dlch1", &(PHY_vars_UE_g[0]->lte_ue_common_vars.dl_ch_estimates[1][0][0]),
741
                        (6 * (PHY_vars_UE_g[0]->lte_frame_parms.ofdm_symbol_size)), 1, 1);
742
          write_output ("dlchan2.m", "dlch2", &(PHY_vars_UE_g[0]->lte_ue_common_vars.dl_ch_estimates[2][0][0]),
743
                        (6 * (PHY_vars_UE_g[0]->lte_frame_parms.ofdm_symbol_size)), 1, 1);
744 745 746 747
          write_output ("pbch_rxF_comp0.m", "pbch_comp0", PHY_vars_UE_g[0]->lte_ue_pbch_vars[0]->rxdataF_comp[0],
                        6 * 12 * 4, 1, 1);
          write_output ("pbch_rxF_llr.m", "pbch_llr", PHY_vars_UE_g[0]->lte_ue_pbch_vars[0]->llr,
                        (frame_parms->Ncp == 0) ? 1920 : 1728, 1, 4);
748 749 750 751 752 753
        }
        /*
         if ((last_slot==1) && (frame==1)) {
         write_output("dlsch_rxF_comp0.m","dlsch0_rxF_comp0",PHY_vars_UE->lte_ue_pdsch_vars[eNB_id]->rxdataF_comp[0],300*(-(PHY_vars_UE->lte_frame_parms.Ncp*2)+14),1,1);
         write_output("pdcch_rxF_comp0.m","pdcch0_rxF_comp0",PHY_vars_UE->lte_ue_pdcch_vars[eNB_id]->rxdataF_comp[0],4*300,1,1);
         }
754
         */
755

756
        if (next_slot % 2 == 0) {
757 758 759 760
          clock_gettime (CLOCK_REALTIME, &time_spec);
          time_last = time_now;
          time_now = (unsigned long) time_spec.tv_nsec;
          td = (int) (time_now - time_last);
761 762 763 764 765
          if (td > 0) {
            td_avg = (int) (((K * (long) td) + (((1 << 3) - K) * ((long) td_avg))) >> 3); // in us
            LOG_T(
                EMU,
                "sleep frame %d, time_now %ldus,time_last %ldus,average time difference %ldns, CURRENT TIME DIFF %dus, avgerage difference from the target %dus\n", frame, time_now, time_last, td_avg, td/1000, (td_avg-TARGET_SF_TIME_NS)/1000);
766
          }
767
          if (td_avg < (TARGET_SF_TIME_NS - SF_DEVIATION_OFFSET_NS)) {
768
            sleep_time_us += SLEEP_STEP_US;
769
            LOG_D(EMU, "Faster than realtime increase the avg sleep time for %d us, frame %d\n", sleep_time_us, frame);
770 771
            // LOG_D(EMU,"Faster than realtime increase the avg sleep time for %d us, frame %d, time_now %ldus,time_last %ldus,average time difference %ldns, CURRENT TIME DIFF %dus, avgerage difference from the target %dus\n",    sleep_time_us,frame, time_now,time_last,td_avg, td/1000,(td_avg-TARGET_SF_TIME_NS)/1000);
          }
772 773 774 775 776 777 778 779
          else
            if (td_avg > (TARGET_SF_TIME_NS + SF_DEVIATION_OFFSET_NS)) {
              sleep_time_us -= SLEEP_STEP_US;
              LOG_D(
                  EMU,
                  "Slower than realtime reduce the avg sleep time for %d us, frame %d, time_now\n", sleep_time_us, frame);
              //LOG_T(EMU,"Slower than realtime reduce the avg sleep time for %d us, frame %d, time_now %ldus,time_last %ldus,average time difference %ldns, CURRENT TIME DIFF %dus, avgerage difference from the target %dus\n",     sleep_time_us,frame, time_now,time_last,td_avg, td/1000,(td_avg-TARGET_SF_TIME_NS)/1000);
            }
780
        } // end if next_slot%2
781 782 783
      } // if Channel_Flag==0

    } //end of slot
784

785
    if ((frame >= 1) && (frame <= 9) && (abstraction_flag == 0)
786
#ifdef PROC
787
        &&(Channel_Flag==0)
788
#endif
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
        ) {
      write_output ("UEtxsig0.m", "txs0", PHY_vars_UE_g[0]->lte_ue_common_vars.txdata[0], FRAME_LENGTH_COMPLEX_SAMPLES,
                    1, 1);
      sprintf (fname, "eNBtxsig%d.m", frame);
      sprintf (vname, "txs%d", frame);
      write_output (fname, vname, PHY_vars_eNB_g[0]->lte_eNB_common_vars.txdata[0][0], FRAME_LENGTH_COMPLEX_SAMPLES, 1,
                    1);
      write_output (
          "eNBtxsigF0.m", "txsF0", PHY_vars_eNB_g[0]->lte_eNB_common_vars.txdataF[0][0],
          PHY_vars_eNB_g[0]->lte_frame_parms.symbols_per_tti * PHY_vars_eNB_g[0]->lte_frame_parms.ofdm_symbol_size, 1,
          1);

      write_output ("UErxsig0.m", "rxs0", PHY_vars_UE_g[0]->lte_ue_common_vars.rxdata[0], FRAME_LENGTH_COMPLEX_SAMPLES,
                    1, 1);
      write_output ("eNBrxsig0.m", "rxs0", PHY_vars_eNB_g[0]->lte_eNB_common_vars.rxdata[0][0],
                    FRAME_LENGTH_COMPLEX_SAMPLES, 1, 1);
    }
806 807 808 809

#ifdef XFORMS
    eNB_id = 0;
    for (UE_id = 0; UE_id < NB_UE_INST; UE_id++) {
810 811 812 813 814 815 816 817 818 819
      phy_scope_UE(form_ue[UE_id],
          PHY_vars_UE_g[UE_id],
          eNB_id,
          UE_id,
          7);

      phy_scope_eNB(form_enb[UE_id],
          PHY_vars_eNB_g[eNB_id],
          UE_id);

820
    }
821
#endif
822 823

    // calibrate at the end of each frame if there is some time  left
824 825 826 827
    if ((sleep_time_us > 0) && (ethernet_flag == 0)) {
      LOG_I(EMU, "[TIMING] Adjust average frame duration, sleep for %d us\n", sleep_time_us);
      usleep (sleep_time_us);
      sleep_time_us = 0; // reset the timer, could be done per n SF
828 829 830
    }
#ifdef SMBV
    if ((frame == config_frames[0]) || (frame == config_frames[1]) || (frame == config_frames[2]) || (frame == config_frames[3])) {
831 832 833 834 835 836 837 838 839 840 841 842
      smbv_frame_cnt++;
    }
#endif
  } //end of frame

#if defined(ENABLE_ITTI)
  itti_terminate_tasks(TASK_L2L1);
#endif

  return NULL;
}

843
#if defined(ENABLE_ITTI)
844
static int create_tasks(uint32_t enb_nb, uint32_t ue_nb) {
845 846
# if defined(ENABLE_USE_MME)
  {
847
    if (enb_nb > 0) {
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
      if (itti_create_task(TASK_SCTP, sctp_eNB_task, NULL) < 0) {
          LOG_E(EMU, "Create task failed");
          LOG_D(EMU, "Initializing SCTP task interface: FAILED\n");
          return -1;
      }

      if (itti_create_task(TASK_S1AP, s1ap_eNB_task, NULL) < 0) {
          LOG_E(EMU, "Create task failed");
          LOG_D(EMU, "Initializing S1AP task interface: FAILED\n");
          return -1;
      }
    }
  }
# endif

# ifdef OPENAIR2
  {
865
    if (enb_nb > 0) {
866 867 868 869 870 871 872
      if (itti_create_task (TASK_RRC_ENB, rrc_enb_task, NULL) < 0) {
        LOG_E(EMU, "Create task failed");
        LOG_D(EMU, "Initializing RRC eNB task interface: FAILED\n");
        exit (-1);
      }
    }

873
    if (ue_nb > 0) {
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
      if (itti_create_task (TASK_RRC_UE, rrc_ue_task, NULL) < 0) {
        LOG_E(EMU, "Create task failed");
        LOG_D(EMU, "Initializing RRC UE task interface: FAILED\n");
        exit (-1);
      }
    }
  }
# endif

  if (itti_create_task(TASK_L2L1, l2l1_task, NULL) < 0) {
    LOG_E(EMU, "Create task failed");
    LOG_D(EMU, "Initializing L2L1 task interface: FAILED\n");
    return -1;
  }

889
  if (enb_nb > 0) {
890 891 892 893 894 895 896 897 898 899 900
    /* Last task to create, others task must be ready before its start */
    if (itti_create_task(TASK_ENB_APP, eNB_app_task, NULL) < 0) {
      LOG_E(EMU, "Create task failed");
      LOG_D(EMU, "Initializing eNB APP task interface: FAILED\n");
      return -1;
    }
  }
  return 0;
}
#endif

901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
Packet_OTG_List *otg_pdcp_buffer;

int main(int argc, char **argv) {

  s32 i;
  // pointers signal buffers (s = transmit, r,r0 = receive)
  clock_t t;

  //FILE *SINRpost;
  //char SINRpost_fname[512];
  // sprintf(SINRpost_fname,"postprocSINR.m");
  //SINRpost = fopen(SINRpost_fname,"w");
  // variables/flags which are set by user on command-line

#ifdef SMBV
  strcpy(smbv_ip,DEFAULT_SMBV_IP);
#endif

  // time calibration for soft realtime mode  

  // u8 awgn_flag = 0;
#ifdef XFORMS
  // current status is that every UE has a DL scope for a SINGLE eNB (eNB_id=0)
  // at eNB 0, an UL scope for every UE 
  FD_lte_phy_scope_ue *form_ue[NUMBER_OF_UE_MAX];
  FD_lte_phy_scope_enb *form_enb[NUMBER_OF_UE_MAX];
  char title[255];
#endif

#ifdef PRINT_STATS
  int len;
  FILE *UE_stats[NUMBER_OF_UE_MAX], *UE_stats_th[NUMBER_OF_UE_MAX], *eNB_stats[NUMBER_OF_eNB_MAX], *eNB_avg_thr, *eNB_l2_stats;
  char UE_stats_filename[255];
  char eNB_stats_filename[255];
  char UE_stats_th_filename[255];
  char eNB_stats_th_filename[255];
#endif

  //time_t t0,t1;
  //clock_t start, stop;

#ifdef PROC
  int node_id;
  int port,Process_Flag=0,wgt,Channel_Flag=0,temp;
#endif
  //double **s_re2[MAX_eNB+MAX_UE], **s_im2[MAX_eNB+MAX_UE], **r_re2[MAX_eNB+MAX_UE], **r_im2[MAX_eNB+MAX_UE], **r_re02, **r_im02;
  //double **r_re0_d[MAX_UE][MAX_eNB], **r_im0_d[MAX_UE][MAX_eNB], **r_re0_u[MAX_eNB][MAX_UE],**r_im0_u[MAX_eNB][MAX_UE];
  //default parameters
  rate_adaptation_flag = 0;
  oai_emulation.info.n_frames = 0xffff; //1024;          //10;
  oai_emulation.info.n_frames_flag = 0; //fixme
  snr_dB = 30;
  cooperation_flag = 0; // default value 0 for no cooperation, 1 for Delay diversity, 2 for Distributed Alamouti

  //Default values if not changed by the user in get_simulation_options();
  pdcp_period = 1;
  omg_period = 1;
  // start thread for log gen
  log_thread_init ();

  init_oai_emulation (); // to initialize everything !!!

  // get command-line options
  get_simulation_options (argc, argv); //Command-line options
965
  
966 967 968 969
#if defined(ENABLE_ITTI)
  itti_init(TASK_MAX, THREAD_MAX, MESSAGES_ID_MAX, tasks_info, messages_info, messages_definition_xml, oai_emulation.info.itti_dump_file);
#endif

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 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
  // Initialize VCD LOG module
  vcd_signal_dumper_init ("openair_dump.vcd");

  /*  pthread_t sigth;
   sigset_t sigblock;
   sigemptyset(&sigblock);
   sigaddset(&sigblock, SIGHUP);
   sigaddset(&sigblock, SIGINT);
   sigaddset(&sigblock, SIGTERM);
   pthread_sigmask(SIG_BLOCK, &sigblock, NULL);
   if (pthread_create(&sigth, NULL, sigh, NULL)) {
   msg("Pthread for tracing Signals is not created!\n");
   return -1;
   } else {
   msg("Pthread for tracing Signals is created!\n");
   }*/
  // configure oaisim with OCG
  oaisim_config (); // config OMG and OCG, OPT, OTG, OLG

  if (ue_connection_test == 1) {
    snr_direction = -snr_step;
    snr_dB = 20;
    sinr_dB = -20;
  }

#ifdef OPENAIR2
  init_omv ();
#endif
  //Before this call, NB_UE_INST and NB_eNB_INST are not set correctly

  check_and_adjust_params ();

#ifdef PRINT_STATS
  for (UE_id=0;UE_id<NB_UE_INST;UE_id++) {
    sprintf(UE_stats_filename,"UE_stats%d.txt",UE_id);
    UE_stats[UE_id] = fopen (UE_stats_filename, "w");
  }
  for (eNB_id=0;eNB_id<NB_eNB_INST;eNB_id++) {
    sprintf(eNB_stats_filename,"eNB_stats%d.txt",eNB_id);
    eNB_stats[eNB_id] = fopen (eNB_stats_filename, "w");
  }

  if(abstraction_flag==0) {
    for (UE_id=0;UE_id<NB_UE_INST;UE_id++) {
      sprintf(UE_stats_th_filename,"UE_stats_th%d_tx%d.txt",UE_id,oai_emulation.info.transmission_mode);
      UE_stats_th[UE_id] = fopen (UE_stats_th_filename, "w");
    }
    sprintf(eNB_stats_th_filename,"eNB_stats_th_tx%d.txt",oai_emulation.info.transmission_mode);
    eNB_avg_thr = fopen (eNB_stats_th_filename, "w");
  }
  else {
    for (UE_id=0;UE_id<NB_UE_INST;UE_id++) {
      sprintf(UE_stats_th_filename,"UE_stats_abs_th%d_tx%d.txt",UE_id,oai_emulation.info.transmission_mode);
      UE_stats_th[UE_id] = fopen (UE_stats_th_filename, "w");
    }
    sprintf(eNB_stats_th_filename,"eNB_stats_abs_th_tx%d.txt",oai_emulation.info.transmission_mode);
    eNB_avg_thr = fopen (eNB_stats_th_filename, "w");
  }
#ifdef OPENAIR2
  eNB_l2_stats = fopen ("eNB_l2_stats.txt", "w");
  LOG_I(EMU,"eNB_l2_stats=%p\n", eNB_l2_stats);
#endif 

#endif

  set_seed = oai_emulation.emulation_config.seed.value;

  init_otg_pdcp_buffer ();

  init_seed (set_seed);

  init_openair1 ();

  init_openair2 ();

  init_ocm ();

#ifdef XFORMS
  eNB_id = 0;
  for (UE_id = 0; UE_id < NB_UE_INST; UE_id++) {
    // DL scope at UEs
    fl_initialize (&argc, argv, NULL, 0, 0);
    form_ue[UE_id] = create_lte_phy_scope_ue();
    sprintf (title, "LTE DL SCOPE eNB %d to UE %d", eNB_id, UE_id);
    fl_show_form (form_ue[UE_id]->lte_phy_scope_ue, FL_PLACE_HOTSPOT, FL_FULLBORDER, title);

    // UL scope at eNB 0
    fl_initialize (&argc, argv, NULL, 0, 0);
    form_enb[UE_id] = create_lte_phy_scope_enb();
    sprintf (title, "LTE UL SCOPE UE %d to eNB %d", UE_id, eNB_id);
    fl_show_form (form_enb[UE_id]->lte_phy_scope_enb, FL_PLACE_HOTSPOT, FL_FULLBORDER, title);

    if (openair_daq_vars.use_ia_receiver == 1) {
      fl_set_button(form_ue[UE_id]->button_0,1);
      fl_set_object_label(form_ue[UE_id]->button_0, "IA Receiver ON");
      fl_set_object_color(form_ue[UE_id]->button_0, FL_GREEN, FL_GREEN);
1066
    }
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080

  }
#endif

#ifdef SMBV
  smbv_init_config(smbv_fname, smbv_nframes);
  smbv_write_config_from_frame_parms(smbv_fname, &PHY_vars_eNB_g[0]->lte_frame_parms);
#endif

  init_time ();

  init_slot_isr ();

  t = clock ();
1081 1082
  
  LOG_N(EMU, ">>>>>>>>>>>>>>>>>>>>>>>>>>> OAIEMU initialization done <<<<<<<<<<<<<<<<<<<<<<<<<<\n\n");
1083 1084 1085

#if defined(ENABLE_ITTI)
  // Handle signals until all tasks are terminated
1086
  if (create_tasks(NB_eNB_INST, NB_UE_INST) >= 0) {
1087
    itti_wait_tasks_end();
1088 1089 1090
  } else {
    exit(-1); // need a softer mode
  }
1091
#else
1092
  eNB_app_task(NULL); // do nothing for the moment
1093
  l2l1_task (NULL);
1094 1095
#endif

1096
  t = clock () - t;
1097
  LOG_I (EMU,"Duration of the simulation: %f seconds\n", ((float) t) / CLOCKS_PER_SEC);
1098 1099

  //  fclose(SINRpost);
1100
  LOG_N(EMU, ">>>>>>>>>>>>>>>>>>>>>>>>>>> OAIEMU Ending <<<<<<<<<<<<<<<<<<<<<<<<<<\n\n");
1101

1102
  free (otg_pdcp_buffer);
1103 1104 1105

#ifdef SMBV
  if (config_smbv) {
1106
    smbv_send_config (smbv_fname,smbv_ip);
1107 1108 1109 1110
  }
#endif

  //Perform KPI measurements
1111 1112
  if (oai_emulation.info.otg_enabled == 1)
    kpi_gen ();
1113 1114 1115 1116 1117 1118 1119

  // relase all rx state
  if (ethernet_flag == 1) {
    emu_transport_release ();
  }

#ifdef PROC
1120
  if (abstraction_flag == 0 && Channel_Flag==0 && Process_Flag==0)
1121
#else
1122
  if (abstraction_flag == 0)
1123
#endif
1124
      {
1125
    /*
1126 1127 1128 1129 1130 1131 1132 1133
     #ifdef IFFT_FPGA
     free(txdataF2[0]);
     free(txdataF2[1]);
     free(txdataF2);
     free(txdata[0]);
     free(txdata[1]);
     free(txdata);
     #endif
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
     */

    for (i = 0; i < 2; i++) {
      free (s_re[i]);
      free (s_im[i]);
      free (r_re[i]);
      free (r_im[i]);
    }
    free (s_re);
    free (s_im);
    free (r_re);
    free (r_im);

    lte_sync_time_free ();
  }
  //  pthread_join(sigth, NULL);

  // added for PHY abstraction
  if (oai_emulation.info.ocm_enabled == 1) {
1153 1154 1155
    for (eNB_id = 0; eNB_id < NUMBER_OF_eNB_MAX; eNB_id++)
      free (enb_data[eNB_id]);

1156
    for (UE_id = 0; UE_id < NUMBER_OF_UE_MAX; UE_id++)
1157
      free (ue_data[UE_id]);
1158 1159 1160
  } //End of PHY abstraction changes

#ifdef OPENAIR2
1161
  mac_top_cleanup ();
1162
#endif 
1163

1164
#ifdef PRINT_STATS
1165
  for (UE_id=0;UE_id<NB_UE_INST;UE_id++) {
1166 1167
    if (UE_stats[UE_id])
    fclose (UE_stats[UE_id]);
1168
    if(UE_stats_th[UE_id])
1169
    fclose (UE_stats_th[UE_id]);
1170
  }
1171 1172
  for (eNB_id=0;eNB_id<NB_eNB_INST;eNB_id++) {
    if (eNB_stats[eNB_id])
1173
    fclose (eNB_stats[eNB_id]);
1174
  }
1175
  if (eNB_avg_thr)
1176
  fclose (eNB_avg_thr);
1177
  if (eNB_l2_stats)
1178 1179
  fclose (eNB_l2_stats);

1180 1181 1182
#endif

  // stop OMG
1183
  stop_mobility_generator (oai_emulation.info.omg_model_ue); //omg_param_list.mobility_type
1184 1185
#ifdef OPENAIR2
  if (oai_emulation.info.omv_enabled == 1)
1186
    omv_end (pfd[1], omv_data);
1187
#endif
1188 1189 1190 1191 1192
  if ((oai_emulation.info.ocm_enabled == 1) && (ethernet_flag == 0) && (ShaF != NULL))
    destroyMat (ShaF, map1, map2);

  if ((oai_emulation.info.opt_enabled == 1))
    terminate_opt ();
1193 1194

  if (oai_emulation.info.cli_enabled)
1195
    cli_server_cleanup ();
1196 1197

  //bring oai if down
1198 1199 1200 1201 1202 1203
  terminate ();
  log_thread_finalize ();
  logClean ();
  vcd_signal_dumper_close ();

  return (0);
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
 static void *sigh(void *arg) {

 int signum;
 sigset_t sigcatch;
 sigemptyset(&sigcatch);
 sigaddset(&sigcatch, SIGHUP);
 sigaddset(&sigcatch, SIGINT);
 sigaddset(&sigcatch, SIGTERM);

 for (;;) {
 sigwait(&sigcatch, &signum);
 switch (signum) {
 case SIGHUP:
 case SIGINT:
 case SIGTERM:
 terminate();
 default:
 break;
 }
 }
 pthread_exit(NULL);
 }
 */
1230 1231 1232 1233

void terminate(void) {
  int i;
  char interfaceName[8];
1234 1235 1236 1237 1238
  for (i = 0; i < NUMBER_OF_eNB_MAX + NUMBER_OF_UE_MAX; i++)
    if (oai_emulation.info.oai_ifup[i] == 1) {
      sprintf (interfaceName, "oai%d", i);
      bringInterfaceUp (interfaceName, 0);
    }
1239 1240
}