oaisim_functions.c 49 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*******************************************************************************

  Eurecom OpenAirInterface
  Copyright(c) 1999 - 2014 Eurecom

  This program is free software; you can redistribute it and/or modify it
  under the terms and conditions of the GNU General Public License,
  version 2, as published by the Free Software Foundation.

  This program is distributed in the hope it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  more details.

  You should have received a copy of the GNU General Public License along with
  this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.

  The full GNU General Public License is included in this distribution in
  the file called "COPYING".

  Contact Information
  Openair Admin: openair_admin@eurecom.fr
  Openair Tech : openair_tech@eurecom.fr
  Forums       : http://forums.eurecom.fsr/openairinterface
  Address      : Eurecom, 2229, route des crêtes, 06560 Valbonne Sophia Antipolis, France

*******************************************************************************/


31 32 33 34
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
35
#include <execinfo.h>
36 37 38
#include <time.h>

#include <sys/timerfd.h>
39

40
#include "assertions.h"
41 42 43 44 45 46 47 48
#include "oaisim_functions.h"

#include "PHY/extern.h"
#include "MAC_INTERFACE/extern.h"
#include "LAYER2/MAC/extern.h"
#include "LAYER2/PDCP_v10.1.0/pdcp.h"
#include "LAYER2/PDCP_v10.1.0/pdcp_primitives.h"
#include "RRC/LITE/extern.h"
Cedric Roux's avatar
Cedric Roux committed
49
#include "RRC/L2_INTERFACE/openair_rrc_L2_interface.h"
50 51 52
#include "PHY_INTERFACE/extern.h"
#include "ARCH/CBMIMO1/DEVICE_DRIVER/extern.h"
#include "SCHED/extern.h"
Cedric Roux's avatar
Cedric Roux committed
53
#include "SIMULATION/ETH_TRANSPORT/proto.h"
54 55 56
#include "UTIL/OCG/OCG_extern.h"
#include "UTIL/LOG/vcd_signal_dumper.h"
#include "UTIL/OPT/opt.h"
57
#include "UTIL/OTG/otg_config.h"
winckel's avatar
winckel committed
58
#include "UTIL/OTG/otg_tx.h"
59 60
#include "lteRALenb.h"
#include "lteRALue.h"
61

Cedric Roux's avatar
Cedric Roux committed
62
#include "cor_SF_sim.h"
63
#include "enb_config.h"
Cedric Roux's avatar
Cedric Roux committed
64

65 66 67 68
#if defined(ENABLE_ITTI)
# include "intertask_interface.h"
#endif

69 70 71 72
#if defined(ENABLE_USE_MME)
# include "s1ap_eNB.h"
#endif

73
#ifdef SMBV
74
extern uint8_t config_smbv;
75 76 77 78 79 80 81 82 83
extern char smbv_ip[16];
#endif

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

84 85 86 87 88
int           otg_times             = 0;
int           if_times              = 0;
int           for_times             = 0;

static char  *conf_config_file_name = NULL;
89
uint16_t           Nid_cell              = 0; //needed by init_lte_vars
90
int           nb_antennas_rx        = 2; // //
91 92 93
uint8_t            target_dl_mcs         = 0; // not set
uint8_t            rate_adaptation_flag  = 0;
uint8_t            set_sinr              = 0;
94
double        snr_dB, sinr_dB;
95 96 97 98
uint8_t            set_seed              = 0;
uint8_t            cooperation_flag;          // for cooperative communication
uint8_t            abstraction_flag      = 0;
uint8_t            ethernet_flag         = 0;
99
double        snr_step              = 1.0;
100
uint8_t            ue_connection_test    = 0;
101
double        forgetting_factor     = 0.0;
102 103 104 105
uint8_t            beta_ACK              = 0;
uint8_t            beta_RI               = 0;
uint8_t            beta_CQI              = 2;
uint8_t            target_ul_mcs         = 4;
106 107 108
LTE_DL_FRAME_PARMS *frame_parms     = NULL;
int           map1,map2;
double      **ShaF                  = NULL;
109
// pointers signal buffers (s = transmit, r,r0 = receive)
110 111 112 113 114
double      **s_re, **s_im, **r_re, **r_im, **r_re0, **r_im0;
Node_list     ue_node_list          = NULL;
Node_list     enb_node_list         = NULL;
int           pdcp_period           = 0;
int           omg_period            = 0;
115

116

117 118
// time calibration for soft realtime mode
struct timespec time_spec;
119 120 121 122 123
unsigned long   time_last           = 0;
unsigned long   time_now            = 0;
int             td                  = 0;
int             td_avg              = 0;
int             sleep_time_us       = 0;
124 125 126 127 128

#ifdef OPENAIR2
// omv related info
//pid_t omv_pid;
char full_name[200];
129
extern int pfd[2]; // fd for omv : fixme: this could be a local var
130 131 132 133 134 135 136 137 138 139 140 141 142
char fdstr[10];
char frames[10];
char num_enb[10];
char num_ue[10];
//area_x, area_y and area_z for omv
char x_area[20];
char y_area[20];
char z_area[20];
char nb_antenna[20];
char frame_type[10];
char tdd_config[10];
#endif

143
Packet_OTG_List_t *otg_pdcp_buffer = NULL;
144 145 146 147 148 149 150

extern node_desc_t *enb_data[NUMBER_OF_eNB_MAX];
extern node_desc_t *ue_data[NUMBER_OF_UE_MAX];
extern channel_desc_t *eNB2UE[NUMBER_OF_eNB_MAX][NUMBER_OF_UE_MAX];
extern channel_desc_t *UE2eNB[NUMBER_OF_UE_MAX][NUMBER_OF_eNB_MAX];

extern mapping small_scale_names[];
Lionel Gauthier's avatar
Lionel Gauthier committed
151 152 153 154
#if defined(Rel10)
extern pdcp_mbms_t pdcp_mbms_array_ue[NUMBER_OF_UE_MAX][maxServiceCount][maxSessionPerPMCH];
extern pdcp_mbms_t pdcp_mbms_array_eNB[NUMBER_OF_eNB_MAX][maxServiceCount][maxSessionPerPMCH];
#endif
155

156 157
extern time_stats_t dl_chan_stats;
extern time_stats_t ul_chan_stats;
158

winckel's avatar
winckel committed
159 160 161
void get_simulation_options(int argc, char *argv[]) {
  int                           option;
  const Enb_properties_array_t *enb_properties;
winckel's avatar
winckel committed
162

winckel's avatar
winckel committed
163
  enum long_option_e {
164
    LONG_OPTION_START = 0x100, /* Start after regular single char options */
winckel's avatar
winckel committed
165

166
    LONG_OPTION_ENB_CONF,
winckel's avatar
winckel committed
167

168 169
    LONG_OPTION_PDNC_PERIOD,
    LONG_OPTION_OMG_PERIOD,
170
    LONG_OPTION_OEH_ENABLED,
winckel's avatar
winckel committed
171

172 173 174 175
    LONG_OPTION_ENB_RAL_LISTENING_PORT,
    LONG_OPTION_ENB_RAL_IP_ADDRESS,
    LONG_OPTION_ENB_RAL_LINK_ID,
    LONG_OPTION_ENB_RAL_LINK_ADDRESS,
winckel's avatar
winckel committed
176

177 178 179
    LONG_OPTION_ENB_MIHF_REMOTE_PORT,
    LONG_OPTION_ENB_MIHF_IP_ADDRESS,
    LONG_OPTION_ENB_MIHF_ID,
winckel's avatar
winckel committed
180

181 182 183 184
    LONG_OPTION_UE_RAL_LISTENING_PORT,
    LONG_OPTION_UE_RAL_IP_ADDRESS,
    LONG_OPTION_UE_RAL_LINK_ID,
    LONG_OPTION_UE_RAL_LINK_ADDRESS,
winckel's avatar
winckel committed
185

186 187 188
    LONG_OPTION_UE_MIHF_REMOTE_PORT,
    LONG_OPTION_UE_MIHF_IP_ADDRESS,
    LONG_OPTION_UE_MIHF_ID,
winckel's avatar
winckel committed
189
  };
winckel's avatar
winckel committed
190

winckel's avatar
winckel committed
191
  static struct option long_options[] = {
192
      {"enb-conf",               required_argument, 0, LONG_OPTION_ENB_CONF},
winckel's avatar
winckel committed
193

194 195 196
      {"pdcp-period",            required_argument, 0, LONG_OPTION_PDNC_PERIOD},
      {"omg-period",             required_argument, 0, LONG_OPTION_OMG_PERIOD},
      {"oeh-enabled",            no_argument, 0, LONG_OPTION_OEH_ENABLED},
winckel's avatar
winckel committed
197

198 199 200 201
      {"enb-ral-listening-port", required_argument, 0, LONG_OPTION_ENB_RAL_LISTENING_PORT},
      {"enb-ral-ip-address",     required_argument, 0, LONG_OPTION_ENB_RAL_IP_ADDRESS},
      {"enb-ral-link-id",        required_argument, 0, LONG_OPTION_ENB_RAL_LINK_ID},
      {"enb-ral-link-address",   required_argument, 0, LONG_OPTION_ENB_RAL_LINK_ADDRESS},
winckel's avatar
winckel committed
202

203 204 205
      {"enb-mihf-remote-port",   required_argument, 0, LONG_OPTION_ENB_MIHF_REMOTE_PORT},
      {"enb-mihf-ip-address",    required_argument, 0, LONG_OPTION_ENB_MIHF_IP_ADDRESS},
      {"enb-mihf-id",            required_argument, 0, LONG_OPTION_ENB_MIHF_ID},
winckel's avatar
winckel committed
206

207 208 209 210
      {"ue-ral-listening-port",  required_argument, 0, LONG_OPTION_UE_RAL_LISTENING_PORT},
      {"ue-ral-ip-address",      required_argument, 0, LONG_OPTION_UE_RAL_IP_ADDRESS},
      {"ue-ral-link-id",         required_argument, 0, LONG_OPTION_UE_RAL_LINK_ID},
      {"ue-ral-link-address",    required_argument, 0, LONG_OPTION_UE_RAL_LINK_ADDRESS},
winckel's avatar
winckel committed
211

212 213 214
      {"ue-mihf-remote-port",    required_argument, 0, LONG_OPTION_UE_MIHF_REMOTE_PORT},
      {"ue-mihf-ip-address",     required_argument, 0, LONG_OPTION_UE_MIHF_IP_ADDRESS},
      {"ue-mihf-id",             required_argument, 0, LONG_OPTION_UE_MIHF_ID},
215

216
      {NULL, 0, NULL, 0}
winckel's avatar
winckel committed
217
  };
winckel's avatar
winckel committed
218

219
  while ((option = getopt_long (argc, argv, "aA:b:B:c:C:D:d:eE:f:FGg:hHi:IJ:j:k:K:l:L:m:M:n:N:oO:p:P:qQ:rR:s:S:t:T:u:U:vV:w:W:x:X:y:Y:z:Z:", long_options, NULL)) != -1) {
220 221 222 223 224 225 226
      switch (option) {
      case LONG_OPTION_ENB_CONF:
        if (optarg) {
            conf_config_file_name = strdup(optarg);
            printf("eNB configuration file is %s\n", conf_config_file_name);
        }
        break;
winckel's avatar
winckel committed
227

228 229 230 231 232 233
      case LONG_OPTION_PDNC_PERIOD:
        if (optarg) {
            pdcp_period = atoi(optarg);
            printf("PDCP period is %d\n", pdcp_period);
        }
        break;
winckel's avatar
winckel committed
234

235 236 237 238 239 240
      case LONG_OPTION_OMG_PERIOD:
        if (optarg) {
            omg_period = atoi(optarg);
            printf("OMG period is %d\n", omg_period);
        }
        break;
241 242 243 244
      
      case LONG_OPTION_OEH_ENABLED:
	oai_emulation.info.oeh_enabled = 1;
	break;
245
#if defined(ENABLE_RAL)
246 247 248 249 250 251
      case LONG_OPTION_ENB_RAL_LISTENING_PORT:
        if (optarg) {
            g_conf_enb_ral_listening_port = strdup(optarg);
            printf("eNB RAL listening port is %s\n", g_conf_enb_ral_listening_port);
        }
        break;
winckel's avatar
winckel committed
252

253 254 255 256 257 258
      case LONG_OPTION_ENB_RAL_IP_ADDRESS:
        if (optarg) {
            g_conf_enb_ral_ip_address = strdup(optarg);
            printf("eNB RAL IP address is %s\n", g_conf_enb_ral_ip_address);
        }
        break;
winckel's avatar
winckel committed
259

260 261 262 263 264 265
      case LONG_OPTION_ENB_RAL_LINK_ADDRESS:
        if (optarg) {
            g_conf_enb_ral_link_address = strdup(optarg);
            printf("eNB RAL link address is %s\n", g_conf_enb_ral_link_address);
        }
        break;
winckel's avatar
winckel committed
266

267 268 269 270 271 272
      case LONG_OPTION_ENB_RAL_LINK_ID:
        if (optarg) {
            g_conf_enb_ral_link_id = strdup(optarg);
            printf("eNB RAL link id is %s\n", g_conf_enb_ral_link_id);
        }
        break;
winckel's avatar
winckel committed
273

274 275 276 277 278 279
      case LONG_OPTION_ENB_MIHF_REMOTE_PORT:
        if (optarg) {
            g_conf_enb_mihf_remote_port = strdup(optarg);
            printf("eNB MIH-F remote port is %s\n", g_conf_enb_mihf_remote_port);
        }
        break;
winckel's avatar
winckel committed
280

281 282 283 284 285 286
      case LONG_OPTION_ENB_MIHF_IP_ADDRESS:
        if (optarg) {
            g_conf_enb_mihf_ip_address = strdup(optarg);
            printf("eNB MIH-F IP address is %s\n", g_conf_enb_mihf_ip_address);
        }
        break;
winckel's avatar
winckel committed
287

288 289 290 291 292 293
      case LONG_OPTION_ENB_MIHF_ID:
        if (optarg) {
            g_conf_enb_mihf_id = strdup(optarg);
            printf("eNB MIH-F id is %s\n", g_conf_enb_mihf_id);
        }
        break;
winckel's avatar
winckel committed
294

295 296 297 298 299 300
      case LONG_OPTION_UE_RAL_LISTENING_PORT:
        if (optarg) {
            g_conf_ue_ral_listening_port = strdup(optarg);
            printf("UE RAL listening port is %s\n", g_conf_ue_ral_listening_port);
        }
        break;
winckel's avatar
winckel committed
301

302 303 304 305 306 307
      case LONG_OPTION_UE_RAL_IP_ADDRESS:
        if (optarg) {
            g_conf_ue_ral_ip_address = strdup(optarg);
            printf("UE RAL IP address is %s\n", g_conf_ue_ral_ip_address);
        }
        break;
winckel's avatar
winckel committed
308

309 310 311 312 313 314
      case LONG_OPTION_UE_RAL_LINK_ID:
        if (optarg) {
            g_conf_ue_ral_link_id = strdup(optarg);
            printf("UE RAL link id is %s\n", g_conf_ue_ral_link_id);
        }
        break;
winckel's avatar
winckel committed
315

316 317 318 319 320 321
      case LONG_OPTION_UE_RAL_LINK_ADDRESS:
        if (optarg) {
            g_conf_ue_ral_link_address = strdup(optarg);
            printf("UE RAL link address is %s\n", g_conf_ue_ral_link_address);
        }
        break;
winckel's avatar
winckel committed
322

323 324 325 326 327 328
      case LONG_OPTION_UE_MIHF_REMOTE_PORT:
        if (optarg) {
            g_conf_ue_mihf_remote_port = strdup(optarg);
            printf("UE MIH-F remote port is %s\n", g_conf_ue_mihf_remote_port);
        }
        break;
winckel's avatar
winckel committed
329

330 331 332 333 334 335
      case LONG_OPTION_UE_MIHF_IP_ADDRESS:
        if (optarg) {
            g_conf_ue_mihf_ip_address = strdup(optarg);
            printf("UE MIH-F IP address is %s\n", g_conf_ue_mihf_ip_address);
        }
        break;
winckel's avatar
winckel committed
336

337 338 339 340 341 342
      case LONG_OPTION_UE_MIHF_ID:
        if (optarg) {
            g_conf_ue_mihf_id = strdup(optarg);
            printf("UE MIH-F id is %s\n", g_conf_ue_mihf_id);
        }
        break;
343
#endif
winckel's avatar
winckel committed
344

345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
      case 'a':
        abstraction_flag = 1;
        break;

      case 'A':
        //oai_emulation.info.ocm_enabled=1;
        if (optarg == NULL)
          oai_emulation.environment_system_config.fading.small_scale.selected_option="AWGN";
        else
          oai_emulation.environment_system_config.fading.small_scale.selected_option= optarg;
        //awgn_flag = 1;
        break;

      case 'b':
        oai_emulation.info.nb_enb_local = atoi (optarg);
        break;

      case 'B':
        oai_emulation.topology_config.mobility.eNB_mobility.eNB_mobility_type.selected_option = optarg;
        //oai_emulation.info.omg_model_enb = atoi (optarg);
        break;

      case 'c':
        strcpy(oai_emulation.info.local_server, optarg);
        oai_emulation.info.ocg_enabled=1;
        break;

      case 'C':
        oai_emulation.info.tdd_config = atoi (optarg);
        AssertFatal (oai_emulation.info.tdd_config <= TDD_Config__subframeAssignment_sa6, "Illegal tdd_config %d (should be 0-%d)!",
            oai_emulation.info.tdd_config, TDD_Config__subframeAssignment_sa6);
        break;

      case 'D':
        oai_emulation.info.multicast_ifname = strdup(optarg);
        break;

      case 'e':
        oai_emulation.info.extended_prefix_flag = 1;
        break;

      case 'E':
        set_seed = 1;
        oai_emulation.info.seed = atoi (optarg);
        break;

      case 'f':
        forgetting_factor = atof (optarg);
        break;

      case 'F':                   // set FDD
        printf("Setting Frame to FDD\n");
        oai_emulation.info.frame_type = 0;
        break;

      case 'g':
        oai_emulation.info.multicast_group = atoi (optarg);
        break;

      case 'G' :
        oai_emulation.info.otg_bg_traffic_enabled = 1;
        break;

      case 'h':
        help ();
        exit (1);
        break;

      case 'H':
        oai_emulation.info.handover_active=1;
        printf("Activate the handover procedure at RRC\n");
        break;

      case 'i':
winckel's avatar
winckel committed
419
#ifdef PROC
420 421 422
        Process_Flag=1;
        node_id = wgt+atoi(optarg);
        port+=atoi(optarg);
winckel's avatar
winckel committed
423
#endif
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
        break;

      case 'I':
        oai_emulation.info.cli_enabled = 1;
        break;

      case 'j' :
        // number of relay nodes: currently only applicable to eMBMS
        oai_emulation.info.nb_rn_local = atoi (optarg);
        break;

      case 'J':
        ue_connection_test=1;
        oai_emulation.info.ocm_enabled=0;
        snr_step = atof(optarg);
        break;

      case 'k':
        //ricean_factor = atof (optarg);
        printf("[SIM] Option k is no longer supported on the command line. Please specify your channel model in the xml template\n");
winckel's avatar
winckel committed
444
        exit(-1);
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
        break;

      case 'K':
        oai_emulation.info.itti_dump_file = optarg;
        break;

      case 'l':
        oai_emulation.info.g_log_level = atoi(optarg);
        break;

      case 'L':                   // set FDD
        flag_LA = atoi(optarg);
        break;

      case 'm':
        target_dl_mcs = atoi (optarg);
        break;

      case 'M':
        abstraction_flag = 1;
        ethernet_flag = 1;
        oai_emulation.info.ethernet_id = atoi (optarg);
        oai_emulation.info.master_id = oai_emulation.info.ethernet_id;
        oai_emulation.info.ethernet_flag = 1;
        break;

      case 'n':
        oai_emulation.info.n_frames = atoi (optarg);
        //n_frames = (n_frames >1024) ? 1024: n_frames; // adjust the n_frames if higher that 1024
        oai_emulation.info.n_frames_flag = 1;
        break;

      case 'N':
        Nid_cell = atoi (optarg);
        if (Nid_cell > 503) {
            printf("Illegal Nid_cell %d (should be 0 ... 503)\n", Nid_cell);
            exit(-1);
        }
        break;

      case 'O':
        conf_config_file_name = optarg;
        break;

      case 'o':
        oai_emulation.info.slot_isr = 1;
        break;

      case 'p':
        oai_emulation.info.nb_master = atoi (optarg);
        break;

      case 'P':
        oai_emulation.info.opt_enabled = 1;

        if (strcmp(optarg, "wireshark") == 0) {
            opt_type = OPT_WIRESHARK;
            printf("Enabling OPT for wireshark\n");
        } else if (strcmp(optarg, "pcap") == 0) {
            opt_type = OPT_PCAP;
            printf("Enabling OPT for pcap\n");
        } else {
            printf("Unrecognized option for OPT module. -> Disabling it\n");
            printf("Possible values are either wireshark or pcap\n");
            opt_type = OPT_NONE;
            oai_emulation.info.opt_enabled = 0;
        }
        oai_emulation.info.opt_mode = opt_type;
        break;
514 515 516 517 518
      case 'q':
        // openair performane profiler 
        oai_emulation.info.opp_enabled = 1; // this var is used for OCG
        opp_enabled = 1; // this is the global var used by oaisim 
        break;
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
      case 'Q':
        //eMBMS_active=1;
        // 0 : not used (default), 1: eMBMS and RRC enabled, 2: eMBMS relaying and RRC enabled, 3: eMBMS enabled, RRC disabled, 4: eMBMS relaying enabled, RRC disabled
        oai_emulation.info.eMBMS_active_state = atoi (optarg);
        break;

      case 'r':
        rate_adaptation_flag = 1;
        break;

      case 'R':
        oai_emulation.info.N_RB_DL = atoi (optarg);
        if ((oai_emulation.info.N_RB_DL != 6) && (oai_emulation.info.N_RB_DL != 15) && (oai_emulation.info.N_RB_DL != 25)
            && (oai_emulation.info.N_RB_DL != 50) && (oai_emulation.info.N_RB_DL != 75) && (oai_emulation.info.N_RB_DL != 100)) {
            printf("Illegal N_RB_DL %d (should be one of 6,15,25,50,75,100)\n", oai_emulation.info.N_RB_DL);
            exit (-1);
        }
        break;

      case 's':
        snr_dB = atoi (optarg);
        //      set_snr = 1;
        oai_emulation.info.ocm_enabled=0;
        break;

      case 'S':
        sinr_dB = atoi (optarg);
        set_sinr = 1;
        oai_emulation.info.ocm_enabled=0;
        break;

      case 't':
551
        target_ul_mcs = atoi (optarg);
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
        break;

      case 'T':
        oai_emulation.info.otg_enabled = 1;
        oai_emulation.info.otg_traffic = optarg;
        break;

      case 'u':
        oai_emulation.info.nb_ue_local = atoi (optarg);
        break;

      case 'U':
        oai_emulation.topology_config.mobility.UE_mobility.UE_mobility_type.selected_option = optarg;
        break;

      case 'v':
        oai_emulation.info.omv_enabled = 1;
        break;

      case 'V':
        ouput_vcd = 1;
        oai_emulation.info.vcd_enabled = 1;
        oai_emulation.info.vcd_file = optarg;
        break;

      case 'w':
        oai_emulation.info.cba_group_active = atoi (optarg);
        break;

      case 'W':
582
#ifdef SMBV
583 584 585
        config_smbv = 1;
        if(atoi(optarg)!=0)
          strcpy(smbv_ip,optarg);
586
#endif
587
        break;
winckel's avatar
winckel committed
588

589 590 591 592 593 594 595
      case 'x':
        oai_emulation.info.transmission_mode = atoi (optarg);
        if ((oai_emulation.info.transmission_mode != 1) &&  (oai_emulation.info.transmission_mode != 2) && (oai_emulation.info.transmission_mode != 5) && (oai_emulation.info.transmission_mode != 6)) {
            printf("Unsupported transmission mode %d\n",oai_emulation.info.transmission_mode);
            exit(-1);
        }
        break;
winckel's avatar
winckel committed
596

597
      case 'X':
winckel's avatar
winckel committed
598
#ifdef PROC
599 600 601 602 603 604 605 606 607 608
        temp=atoi(optarg);
        if(temp==0){
            port=CHANNEL_PORT; Channel_Flag=1; Process_Flag=0; wgt=0;
        }
        else if(temp==1){
            port=eNB_PORT; wgt=0;
        }
        else {
            port=UE_PORT; wgt=MAX_eNB;
        }
winckel's avatar
winckel committed
609
#endif
610
        break;
winckel's avatar
winckel committed
611

612 613 614 615 616 617 618
      case 'y':
        nb_antennas_rx=atoi(optarg);
        if (nb_antennas_rx>4) {
            printf("Cannot have more than 4 antennas\n");
            exit(-1);
        }
        break;
winckel's avatar
winckel committed
619

620 621 622
      case 'Y':
        oai_emulation.info.g_log_verbosity_option = strdup(optarg);
        break;
winckel's avatar
winckel committed
623

624 625 626
      case 'z':
        cooperation_flag = atoi (optarg);
        break;
winckel's avatar
winckel committed
627

628 629 630
      case 'Z':
        /* Sebastien ROUX: Reserved for future use (currently used in ltenow branch) */
        break;
winckel's avatar
winckel committed
631

632 633 634 635 636
      default:
        help ();
        exit (-1);
        break;
      }
winckel's avatar
winckel committed
637
  }
638 639

  if ((oai_emulation.info.nb_enb_local > 0) && (conf_config_file_name != NULL))
640
    {
641 642 643 644
      /* Read eNB configuration file */
      enb_properties = enb_config_init(conf_config_file_name);

      AssertFatal (oai_emulation.info.nb_enb_local <= enb_properties->number,
645 646
          "Number of eNB is greater than eNB defined in configuration file %s (%d/%d)!",
          conf_config_file_name, oai_emulation.info.nb_enb_local, enb_properties->number);
647 648 649

      /* Update some simulation parameters */
      oai_emulation.info.frame_type =           enb_properties->properties[0]->frame_type;
650 651
      oai_emulation.info.tdd_config =           enb_properties->properties[0]->tdd_config;
      oai_emulation.info.tdd_config_S =         enb_properties->properties[0]->tdd_config_s;
652
      oai_emulation.info.extended_prefix_flag = enb_properties->properties[0]->prefix_type;
653
    }
654 655
}

656
void check_and_adjust_params(void) {
657

658
  int32_t ret;
659 660
  int i,j;

661
  if (oai_emulation.info.nb_ue_local  + oai_emulation.info.nb_rn_local > NUMBER_OF_UE_MAX) {
662 663
      LOG_E(EMU,"Enter fewer than %d UEs/RNs for the moment or change the NUMBER_OF_UE_MAX\n", NUMBER_OF_UE_MAX);
      exit(EXIT_FAILURE);
664 665
  }

666
  if (oai_emulation.info.nb_enb_local + oai_emulation.info.nb_rn_local > NUMBER_OF_eNB_MAX) {
667 668
      LOG_E(EMU,"Enter fewer than %d eNBs/RNs for the moment or change the NUMBER_OF_UE_MAX\n", NUMBER_OF_eNB_MAX);
      exit(EXIT_FAILURE);
669
  }
670

671
  if (oai_emulation.info.nb_rn_local > NUMBER_OF_RN_MAX) {
672 673
      LOG_E(EMU,"Enter fewer than %d RNs for the moment or change the NUMBER_OF_RN_MAX\n", NUMBER_OF_RN_MAX);
      exit(EXIT_FAILURE);
674
  }
675 676 677 678 679 680 681 682 683 684 685
  // fix ethernet and abstraction with RRC_CELLULAR Flag
#ifdef RRC_CELLULAR
  abstraction_flag = 1;
  ethernet_flag = 1;
#endif

  if (set_sinr == 0)
    sinr_dB = snr_dB - 20;

  // setup netdevice interface (netlink socket)
  LOG_I(EMU,"[INIT] Starting NAS netlink interface\n");
Cedric Roux's avatar
Cedric Roux committed
686
  ret = netlink_init();
687
  if (ret < 0)
688
    LOG_W(EMU,"[INIT] Netlink not available, careful ...\n");
689 690

  if (ethernet_flag == 1) {
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
      oai_emulation.info.master[oai_emulation.info.master_id].nb_ue = oai_emulation.info.nb_ue_local + oai_emulation.info.nb_rn_local;
      oai_emulation.info.master[oai_emulation.info.master_id].nb_enb = oai_emulation.info.nb_enb_local + oai_emulation.info.nb_rn_local;
      if (oai_emulation.info.nb_rn_local>0)
        LOG_N(EMU,"Ethernet emulation is not yet tested with the relay nodes\n");
      if (!oai_emulation.info.master_id)
        oai_emulation.info.is_primary_master = 1;
      j = 1;
      for (i = 0; i < oai_emulation.info.nb_master; i++) {
          if (i != oai_emulation.info.master_id)
            oai_emulation.info.master_list = oai_emulation.info.master_list + j;
          LOG_I (EMU, "Index of master id i=%d  MASTER_LIST %d\n", i, oai_emulation.info.master_list);
          j *= 2;
      }
      LOG_I (EMU, " Total number of master %d my master id %d\n", oai_emulation.info.nb_master, oai_emulation.info.master_id);
      init_bypass ();
706

707 708 709 710
      while (emu_tx_status != SYNCED_TRANSPORT) {
          LOG_I (EMU, " Waiting for EMU Transport to be synced\n");
          emu_transport_sync ();    //emulation_tx_rx();
      }
711 712
  } // ethernet flag

713
  //
714 715
  NB_UE_INST = oai_emulation.info.nb_ue_local + oai_emulation.info.nb_ue_remote;
  NB_eNB_INST = oai_emulation.info.nb_enb_local + oai_emulation.info.nb_enb_remote;
716 717
  NB_RN_INST = oai_emulation.info.nb_rn_local + oai_emulation.info.nb_rn_remote;

718
#if defined(ENABLE_PDCP_NETLINK_FIFO) && defined(OPENAIR2)
719 720 721
  pdcp_netlink_init();
#endif

722
  if (NB_RN_INST > 0 ) {
723
      LOG_N(EMU,"Total number of RN %d (local %d, remote %d) mobility (the same as eNB) %s  \n", NB_RN_INST,oai_emulation.info.nb_rn_local,oai_emulation.info.nb_rn_remote, oai_emulation.topology_config.mobility.eNB_mobility.eNB_mobility_type.selected_option);
724

725 726 727 728 729
      LOG_N(EMU,"Adjust the number of eNB inst (%d->%d) and UE inst (%d->%d)\n ",
          NB_eNB_INST, NB_eNB_INST+NB_RN_INST,
          NB_UE_INST, NB_UE_INST+NB_RN_INST);
      NB_eNB_INST+=NB_RN_INST;
      NB_UE_INST+=NB_RN_INST;
730
  }
731
  LOG_I(EMU,"Total number of UE %d (first local %d , num local %d, remote %d, relay %d) mobility %s \n",
732 733 734
      NB_UE_INST,oai_emulation.info.first_ue_local, oai_emulation.info.nb_ue_local,oai_emulation.info.nb_ue_remote,
      NB_RN_INST,
      oai_emulation.topology_config.mobility.UE_mobility.UE_mobility_type.selected_option);
735 736

  LOG_I(EMU,"Total number of eNB %d (local %d, remote %d, relay %d) mobility %s \n",
737 738 739
      NB_eNB_INST,oai_emulation.info.nb_enb_local,oai_emulation.info.nb_enb_remote,
      NB_RN_INST,
      oai_emulation.topology_config.mobility.eNB_mobility.eNB_mobility_type.selected_option);
740

741 742
}

743
#ifdef OPENAIR2
744
void init_omv(void) {
745 746
  if (oai_emulation.info.omv_enabled == 1) {

747 748
      if(pipe(pfd) == -1)
        perror("pipe error \n");
749

750 751
      sprintf(full_name, "%s/UTIL/OMV/OMV",getenv("OPENAIR2_DIR"));
      LOG_I(EMU,"Stating the OMV path %s pfd[0] %d pfd[1] %d \n", full_name, pfd[0],pfd[1]);
752

753 754 755 756
      switch(fork()) {
      case -1 :
        perror("fork failed \n");
        break;
757
      case 0 : // child is going to be the omv, it is the reader
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
        if(close(pfd[1]) == -1 ) // we close the write desc.
          perror("close on write\n" );
        sprintf(fdstr, "%d", pfd[0] );
        sprintf(num_enb, "%d", NB_eNB_INST);
        sprintf(num_ue, "%d", NB_UE_INST);
        sprintf(x_area, "%f", oai_emulation.topology_config.area.x_m );
        sprintf(y_area, "%f", oai_emulation.topology_config.area.y_m );
        sprintf(z_area, "%f", 200.0 );
        sprintf(frames, "%d", oai_emulation.info.n_frames);
        sprintf(nb_antenna, "%d", 4);
        sprintf(frame_type, "%s", (oai_emulation.info.frame_type == 0) ? "FDD" : "TDD");
        sprintf(tdd_config, "%d", oai_emulation.info.tdd_config);
        // execl is used to launch the visualisor
        execl(full_name,"OMV", fdstr, frames, num_enb, num_ue, x_area, y_area, z_area, nb_antenna, frame_type, tdd_config,NULL );
        perror( "error in execl the OMV" );
      }
      //parent
      if(close( pfd[0] ) == -1 ) // we close the write desc.
        perror("close on read\n" );
777 778
  }
}
779
#endif
780

781
void init_seed(uint8_t set_seed) {
782 783 784

  if(set_seed) {

785 786
      randominit (oai_emulation.info.seed);
      set_taus_seed (oai_emulation.info.seed);
787 788

  } else {
789 790
      randominit (0);
      set_taus_seed (0);
791 792 793
  }
}

794
void init_openair1(void) {
Lionel Gauthier's avatar
Lionel Gauthier committed
795
  module_id_t UE_id, eNB_id;
796 797 798
#if defined(ENABLE_RAL)
  int list_index;
#endif
799
  // change the nb_connected_eNB
800
  init_lte_vars (&frame_parms, oai_emulation.info.frame_type, oai_emulation.info.tdd_config, oai_emulation.info.tdd_config_S,oai_emulation.info.extended_prefix_flag,oai_emulation.info.N_RB_DL, Nid_cell, cooperation_flag, oai_emulation.info.transmission_mode, abstraction_flag,nb_antennas_rx, oai_emulation.info.eMBMS_active_state);
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817

  for (eNB_id=0; eNB_id<NB_eNB_INST;eNB_id++){
      for (UE_id=0; UE_id<NB_UE_INST;UE_id++){
          PHY_vars_eNB_g[eNB_id]->pusch_config_dedicated[UE_id].betaOffset_ACK_Index = beta_ACK;
          PHY_vars_eNB_g[eNB_id]->pusch_config_dedicated[UE_id].betaOffset_RI_Index  = beta_RI;
          PHY_vars_eNB_g[eNB_id]->pusch_config_dedicated[UE_id].betaOffset_CQI_Index = beta_CQI;
          PHY_vars_UE_g[UE_id]->pusch_config_dedicated[eNB_id].betaOffset_ACK_Index = beta_ACK;
          PHY_vars_UE_g[UE_id]->pusch_config_dedicated[eNB_id].betaOffset_RI_Index  = beta_RI;
          PHY_vars_UE_g[UE_id]->pusch_config_dedicated[eNB_id].betaOffset_CQI_Index = beta_CQI;
          ((PHY_vars_UE_g[UE_id]->lte_frame_parms).pdsch_config_common).p_b = (frame_parms->nb_antennas_tx_eNB>1) ? 1 : 0; // rho_a = rhob
          ((PHY_vars_eNB_g[eNB_id]->lte_frame_parms).pdsch_config_common).p_b = (frame_parms->nb_antennas_tx_eNB>1) ? 1 : 0; // rho_a = rhob

      }
  }

  printf ("AFTER init: Nid_cell %d\n", PHY_vars_eNB_g[0]->lte_frame_parms.Nid_cell);
  printf ("AFTER init: frame_type %d,tdd_config %d\n",
818 819
      PHY_vars_eNB_g[0]->lte_frame_parms.frame_type,
      PHY_vars_eNB_g[0]->lte_frame_parms.tdd_config);
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837

  number_of_cards = 1;

  openair_daq_vars.rx_rf_mode = 1;
  openair_daq_vars.tdd = 1;
  openair_daq_vars.rx_gain_mode = DAQ_AGC_ON;

  openair_daq_vars.dlsch_transmission_mode = oai_emulation.info.transmission_mode;

  openair_daq_vars.target_ue_dl_mcs = target_dl_mcs;
  openair_daq_vars.target_ue_ul_mcs = target_ul_mcs;
  openair_daq_vars.ue_dl_rb_alloc=0x1fff;
  openair_daq_vars.ue_ul_nb_rb=6;
  openair_daq_vars.dlsch_rate_adaptation = rate_adaptation_flag;
  openair_daq_vars.use_ia_receiver = 0;

  // init_ue_status();
  for (UE_id=0; UE_id<NB_UE_INST;UE_id++) {
838 839 840 841 842 843 844 845 846 847
      PHY_vars_UE_g[UE_id]->rx_total_gain_dB=130;
      // update UE_mode for each eNB_id not just 0
      if (abstraction_flag == 0)
        PHY_vars_UE_g[UE_id]->UE_mode[0] = NOT_SYNCHED;
      else {
          // 0 is the index of the connected eNB
          PHY_vars_UE_g[UE_id]->UE_mode[0] = PRACH;
      }
      PHY_vars_UE_g[UE_id]->lte_ue_pdcch_vars[0]->crnti = 0x1235 + UE_id;
      PHY_vars_UE_g[UE_id]->current_dlsch_cqi[0] = 10;
848

849
      LOG_I(EMU, "UE %d mode is initialized to %d\n", UE_id, PHY_vars_UE_g[UE_id]->UE_mode[0] );
850
#if defined(ENABLE_RAL)
851 852 853 854 855 856 857
      PHY_vars_UE_g[UE_id]->ral_thresholds_timed = hashtable_create (64, NULL, NULL);
      for (list_index = 0; list_index < RAL_LINK_PARAM_GEN_MAX; list_index++) {
          SLIST_INIT(&PHY_vars_UE_g[UE_id]->ral_thresholds_gen_polled[list_index]);
      }
      for (list_index = 0; list_index < RAL_LINK_PARAM_LTE_MAX; list_index++) {
          SLIST_INIT(&PHY_vars_UE_g[UE_id]->ral_thresholds_lte_polled[list_index]);
      }
858 859
#endif

860 861 862
  }
}

863
void init_openair2(void) {
864
#ifdef OPENAIR2
Lionel Gauthier's avatar
Lionel Gauthier committed
865 866
  module_id_t enb_id;
  module_id_t UE_id;
867

868
  l2_init (&PHY_vars_eNB_g[0]->lte_frame_parms,
869 870 871
      oai_emulation.info.eMBMS_active_state,
      oai_emulation.info.cba_group_active,
      oai_emulation.info.handover_active);
872

Lionel Gauthier's avatar
Lionel Gauthier committed
873 874
  for (enb_id = 0; enb_id < NB_eNB_INST; enb_id++)
    mac_xface->mrbch_phy_sync_failure (enb_id, 0, enb_id);
875 876

  if (abstraction_flag == 1) {
877 878
      for (UE_id = 0; UE_id < NB_UE_INST; UE_id++)
        mac_xface->dl_phy_sync_success (UE_id, 0, 0,1);   //UE_id%NB_eNB_INST);
879 880 881
  }

  mac_xface->macphy_exit = exit_fun;
882

883 884 885
#endif
}

886
void init_ocm(void) {
Lionel Gauthier's avatar
Lionel Gauthier committed
887
  module_id_t UE_id, eNB_id;
888
  /* Added for PHY abstraction */
889 890
  LOG_I(OCM,"Running with frame_type %d, Nid_cell %d, N_RB_DL %d, EP %d, mode %d, target dl_mcs %d, rate adaptation %d, nframes %d, abstraction %d, channel %s\n", oai_emulation.info.frame_type, Nid_cell, oai_emulation.info.N_RB_DL, oai_emulation.info.extended_prefix_flag, oai_emulation.info.transmission_mode,target_dl_mcs,rate_adaptation_flag,oai_emulation.info.n_frames,abstraction_flag,oai_emulation.environment_system_config.fading.small_scale.selected_option);

891
  if (abstraction_flag) {
892

893
      get_beta_map();
894
#ifdef PHY_ABSTRACTION_UL
895
      get_beta_map_up();
896
#endif
897
      get_MIESM_param();
898 899

      //load_pbch_desc();
900
  }
901
  
902 903

  for (eNB_id = 0; eNB_id < NB_eNB_INST; eNB_id++) {
904 905
      enb_data[eNB_id] = (node_desc_t *)malloc(sizeof(node_desc_t));
      init_enb(enb_data[eNB_id],oai_emulation.environment_system_config.antenna.eNB_antenna);
906 907 908
  }

  for (UE_id = 0; UE_id < NB_UE_INST; UE_id++) {
909 910
      ue_data[UE_id] = (node_desc_t *)malloc(sizeof(node_desc_t));
      init_ue(ue_data[UE_id],oai_emulation.environment_system_config.antenna.UE_antenna);
911 912 913 914 915 916
  }

  if ((oai_emulation.info.ocm_enabled == 1)&& (ethernet_flag == 0 ) &&
      (oai_emulation.environment_system_config.fading.shadowing.decorrelation_distance_m>0) &&
      (oai_emulation.environment_system_config.fading.shadowing.variance_dB>0)) {

917 918 919 920
      // init SF map here!!!
      map1 =(int)oai_emulation.topology_config.area.x_m;
      map2 =(int)oai_emulation.topology_config.area.y_m;
      ShaF = init_SF(map1,map2,oai_emulation.environment_system_config.fading.shadowing.decorrelation_distance_m,oai_emulation.environment_system_config.fading.shadowing.variance_dB);
921

922 923
      // size of area to generate shadow fading map
      LOG_D(EMU,"Simulation area x=%f, y=%f\n",
924 925 926 927 928 929 930 931 932
          oai_emulation.topology_config.area.x_m,
          oai_emulation.topology_config.area.y_m);
  }

  if (abstraction_flag == 0)
    init_channel_vars (frame_parms, &s_re, &s_im, &r_re, &r_im, &r_re0, &r_im0);

  // initialize channel descriptors
  for (eNB_id = 0; eNB_id < NB_eNB_INST; eNB_id++) {
933
      for (UE_id = 0; UE_id < NB_UE_INST; UE_id++) {
934

935 936
          LOG_D(OCM,"Initializing channel (%s, %d) from eNB %d to UE %d\n", oai_emulation.environment_system_config.fading.small_scale.selected_option,
              map_str_to_int(small_scale_names,oai_emulation.environment_system_config.fading.small_scale.selected_option), eNB_id, UE_id);
937

938
          /* if (oai_emulation.info.transmission_mode == 5)
939 940 941 942 943 944 945 946 947
                    eNB2UE[eNB_id][UE_id] = new_channel_desc_scm(PHY_vars_eNB_g[eNB_id]->lte_frame_parms.nb_antennas_tx,
                                                       PHY_vars_UE_g[UE_id]->lte_frame_parms.nb_antennas_rx,
                                                       (UE_id == 0)? Rice1_corr : Rice1_anticorr,
                                                       oai_emulation.environment_system_config.system_bandwidth_MB,
                                                       forgetting_factor,
                                                       0,
                                                       0);

                else
948 949 950 951 952 953 954 955 956
           */

          eNB2UE[eNB_id][UE_id] = new_channel_desc_scm(PHY_vars_eNB_g[eNB_id]->lte_frame_parms.nb_antennas_tx,
              PHY_vars_UE_g[UE_id]->lte_frame_parms.nb_antennas_rx,
              map_str_to_int(small_scale_names,oai_emulation.environment_system_config.fading.small_scale.selected_option),
              oai_emulation.environment_system_config.system_bandwidth_MB,
              forgetting_factor,
              0,
              0);
957
          random_channel(eNB2UE[eNB_id][UE_id],abstraction_flag);
958 959 960 961 962 963 964 965 966 967 968
          LOG_D(OCM,"[SIM] Initializing channel (%s, %d) from UE %d to eNB %d\n", oai_emulation.environment_system_config.fading.small_scale.selected_option,
              map_str_to_int(small_scale_names, oai_emulation.environment_system_config.fading.small_scale.selected_option),UE_id, eNB_id);

          UE2eNB[UE_id][eNB_id] = new_channel_desc_scm(PHY_vars_UE_g[UE_id]->lte_frame_parms.nb_antennas_tx,
              PHY_vars_eNB_g[eNB_id]->lte_frame_parms.nb_antennas_rx,
              map_str_to_int(small_scale_names, oai_emulation.environment_system_config.fading.small_scale.selected_option),
              oai_emulation.environment_system_config.system_bandwidth_MB,
              forgetting_factor,
              0,
              0);

969
      random_channel(UE2eNB[UE_id][eNB_id],abstraction_flag);
970

971 972 973 974
      // to make channel reciprocal uncomment following line instead of previous. However this only works for SISO at the moment. For MIMO the channel would need to be transposed.
      //UE2eNB[UE_id][eNB_id] = eNB2UE[eNB_id][UE_id];
    }
  }
975 976
}

977
void init_otg_pdcp_buffer(void) {
Lionel Gauthier's avatar
Lionel Gauthier committed
978
  module_id_t i;
979
  otg_pdcp_buffer = malloc((NB_UE_INST + NB_eNB_INST) * sizeof(Packet_OTG_List_t));
980 981

  for (i = 0; i < NB_UE_INST + NB_eNB_INST; i++) {
982 983
      pkt_list_init(&(otg_pdcp_buffer[i]));
      //LOG_I(EMU,"HEAD of otg_pdcp_buffer[%d] is %p\n", i, pkt_list_get_head(&(otg_pdcp_buffer[i])));
984 985 986
  }
}

987
void update_omg (frame_t frameP) {
Lionel Gauthier's avatar
Lionel Gauthier committed
988
  module_id_t UE_id, eNB_id;
989 990
  int new_omg_model;

991
  if ((frameP % omg_period) == 0 ) { // call OMG every 10ms
992 993 994 995 996 997 998 999 1000 1001
      update_nodes(oai_emulation.info.time_s);
      display_node_list(enb_node_list);
      display_node_list(ue_node_list);
      if (oai_emulation.info.omg_model_ue >= MAX_NUM_MOB_TYPES){ // mix mobility model
          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++){
              new_omg_model = randomGen(STATIC,RWALK);
              LOG_D(OMG, "[UE] Node of ID %d is changing mobility generator ->%d \n", UE_id, new_omg_model);
              // reset the mobility model for a specific node
              set_new_mob_type (UE_id, UE, new_omg_model, oai_emulation.info.time_s);
          }
1002 1003
      }

1004 1005 1006 1007 1008 1009 1010
      if (oai_emulation.info.omg_model_enb >= MAX_NUM_MOB_TYPES) {      // mix mobility model
          for (eNB_id = oai_emulation.info.first_enb_local; eNB_id < (oai_emulation.info.first_enb_local + oai_emulation.info.nb_enb_local); eNB_id++) {
              new_omg_model = randomGen (STATIC, RWALK);
              LOG_D (OMG,"[eNB] Node of ID %d is changing mobility generator ->%d \n", eNB_id, new_omg_model);
              // reset the mobility model for a specific node
              set_new_mob_type (eNB_id, eNB, new_omg_model, oai_emulation.info.time_s);
          }
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
      }
  }
}

void update_omg_ocm() {
  enb_node_list = get_current_positions(oai_emulation.info.omg_model_enb, eNB, oai_emulation.info.time_s);
  ue_node_list = get_current_positions(oai_emulation.info.omg_model_ue, UE, oai_emulation.info.time_s);
}

void update_ocm() {
Lionel Gauthier's avatar
Lionel Gauthier committed
1021
  module_id_t UE_id, eNB_id;
1022 1023 1024 1025 1026 1027 1028
  for (eNB_id = 0; eNB_id < NB_eNB_INST; eNB_id++)
    enb_data[eNB_id]->tx_power_dBm = PHY_vars_eNB_g[eNB_id]->lte_frame_parms.pdsch_config_common.referenceSignalPower;
  for (UE_id = 0; UE_id < NB_UE_INST; UE_id++)
    ue_data[UE_id]->tx_power_dBm = PHY_vars_UE_g[UE_id]->tx_power_dBm;

  /* check if the openair channel model is activated used for PHY abstraction : path loss*/
  if ((oai_emulation.info.ocm_enabled == 1)&& (ethernet_flag == 0 )) {
1029 1030 1031 1032 1033 1034 1035 1036
      //LOG_D(OMG," extracting position of eNb...\n");
      extract_position(enb_node_list, enb_data, NB_eNB_INST);
      //extract_position_fixed_enb(enb_data, NB_eNB_INST,frame);
      //LOG_D(OMG," extracting position of UE...\n");
      //      if (oai_emulation.info.omg_model_ue == TRACE)
      extract_position(ue_node_list, ue_data, NB_UE_INST);

      /* if (frame % 50 == 0)
1037
      LOG_N(OCM,"Path loss for TTI %d : \n", frame);
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
       */
      for (eNB_id = 0; eNB_id < NB_eNB_INST; eNB_id++) {
          for (UE_id = 0; UE_id < NB_UE_INST; UE_id++) {
              calc_path_loss (enb_data[eNB_id], ue_data[UE_id], eNB2UE[eNB_id][UE_id], oai_emulation.environment_system_config,ShaF);
              //calc_path_loss (enb_data[eNB_id], ue_data[UE_id], eNB2UE[eNB_id][UE_id], oai_emulation.environment_system_config,0);
              UE2eNB[UE_id][eNB_id]->path_loss_dB = eNB2UE[eNB_id][UE_id]->path_loss_dB;
              //    if (frame % 50 == 0)
              LOG_I(OCM,"Path loss between eNB %d at (%f,%f) and UE %d at (%f,%f) is %f, angle %f\n",
                  eNB_id,enb_data[eNB_id]->x,enb_data[eNB_id]->y,UE_id,ue_data[UE_id]->x,ue_data[UE_id]->y,
                  eNB2UE[eNB_id][UE_id]->path_loss_dB, eNB2UE[eNB_id][UE_id]->aoa);
          }
1049 1050 1051 1052
      }
  }

  else {
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
      for (eNB_id = 0; eNB_id < NB_eNB_INST; eNB_id++) {
          for (UE_id = 0; UE_id < NB_UE_INST; UE_id++) {

              //UE2eNB[UE_id][eNB_id]->path_loss_dB = -105 + snr_dB;
              if (eNB_id == (UE_id % NB_eNB_INST)) {
                  eNB2UE[eNB_id][UE_id]->path_loss_dB = -105 + snr_dB - PHY_vars_eNB_g[eNB_id]->lte_frame_parms.pdsch_config_common.referenceSignalPower;
                  UE2eNB[UE_id][eNB_id]->path_loss_dB = -105 + snr_dB - PHY_vars_eNB_g[eNB_id]->lte_frame_parms.pdsch_config_common.referenceSignalPower; //+20 to offset the difference in tx power of the UE wrt eNB
              }
              else {
                  eNB2UE[eNB_id][UE_id]->path_loss_dB = -105 + sinr_dB - PHY_vars_eNB_g[eNB_id]->lte_frame_parms.pdsch_config_common.referenceSignalPower;
                  UE2eNB[UE_id][eNB_id]->path_loss_dB = -105 + sinr_dB - PHY_vars_eNB_g[eNB_id]->lte_frame_parms.pdsch_config_common.referenceSignalPower;
              }
              LOG_I(OCM,"Path loss from eNB %d to UE %d => %f dB (eNB TX %d)\n",eNB_id,UE_id,eNB2UE[eNB_id][UE_id]->path_loss_dB,
                  PHY_vars_eNB_g[eNB_id]->lte_frame_parms.pdsch_config_common.referenceSignalPower);
              //      printf("[SIM] Path loss from UE %d to eNB %d => %f dB\n",UE_id,eNB_id,UE2eNB[UE_id][eNB_id]->path_loss_dB);
          }
1069 1070 1071 1072
      }
  }
}

1073
#ifdef OPENAIR2
Lionel Gauthier's avatar
Lionel Gauthier committed
1074
void update_otg_eNB(module_id_t enb_module_idP, unsigned int ctime) {
1075
#if defined(USER_MODE) && defined(OAI_EMU)
1076 1077
  if (oai_emulation.info.otg_enabled ==1 ) {

1078
      int dst_id, app_id;
1079
      Packet_otg_elt_t *otg_pkt;
1080 1081 1082 1083

      for (dst_id = 0; dst_id < NUMBER_OF_UE_MAX; dst_id++) {
          for_times += 1;
          // generate traffic if the ue is rrc reconfigured state
Lionel Gauthier's avatar
Lionel Gauthier committed
1084
          if (mac_get_rrc_status(enb_module_idP, ENB_FLAG_YES, dst_id) > 2 /*RRC_CONNECTED*/ ) {
1085 1086

              for (app_id=0; app_id<MAX_NUM_APPLICATION; app_id++){
1087
                  otg_pkt = malloc (sizeof(Packet_otg_elt_t));
1088 1089
                  if_times += 1;

1090
                  (otg_pkt->otg_pkt).sdu_buffer = (uint8_t*) packet_gen(enb_module_idP, dst_id + NB_eNB_INST, app_id, ctime, &((otg_pkt->otg_pkt).sdu_buffer_size));
1091 1092 1093

                  if ((otg_pkt->otg_pkt).sdu_buffer != NULL) {
                      otg_times += 1;
1094
                      (otg_pkt->otg_pkt).rb_id = DTCH-2; // app could be binded to a given DRB
Lionel Gauthier's avatar
Lionel Gauthier committed
1095
                      (otg_pkt->otg_pkt).module_id = enb_module_idP;
1096 1097
                      (otg_pkt->otg_pkt).dst_id = dst_id;
                      (otg_pkt->otg_pkt).is_ue = 0;
1098
                      (otg_pkt->otg_pkt).mode = PDCP_TRANSMISSION_MODE_DATA;
1099
                      //Adding the packet to the OTG-PDCP buffer
1100
#warning "Strange code"
Lionel Gauthier's avatar
Lionel Gauthier committed
1101
                      pkt_list_add_tail_eurecom(otg_pkt, &(otg_pdcp_buffer[enb_module_idP]));
1102 1103 1104 1105 1106 1107 1108 1109
                      LOG_I(EMU, "[eNB %d] ADD pkt to OTG buffer with size %d for dst %d on rb_id %d for app id %d \n",
                          (otg_pkt->otg_pkt).module_id, otg_pkt->otg_pkt.sdu_buffer_size, (otg_pkt->otg_pkt).dst_id,(otg_pkt->otg_pkt).rb_id, app_id);
                  } else {
                      free(otg_pkt);
                      otg_pkt=NULL;
                  }
              }
          }
1110 1111 1112
      }

#ifdef Rel10
Lionel Gauthier's avatar
Lionel Gauthier committed
1113 1114 1115
      mbms_service_id_t service_id;
      mbms_session_id_t session_id;
      rb_id_t           rb_id;
1116 1117
      // MBSM multicast traffic
      // if (frame >= 50) {// only generate when UE can receive MTCH (need to control this value)
1118
      for (service_id = 0; service_id < 2 ; service_id++) { //maxServiceCount
1119
          for (session_id = 0; session_id < 2; session_id++) { // maxSessionPerPMCH
1120
              if (pdcp_mbms_array_eNB[enb_module_idP][service_id][session_id].instanciated_instance == TRUE){ // this service/session is configured
1121

1122
                  otg_pkt = malloc (sizeof(Packet_otg_elt_t));
1123
                  // LOG_T(OTG,"multicast packet gen for (service/mch %d, session/lcid %d, rb_id %d)\n", service_id, session_id, service_id*maxSessionPerPMCH + session_id);
Lionel Gauthier's avatar
Lionel Gauthier committed
1124
                  rb_id = pdcp_mbms_array_eNB[enb_module_idP][service_id][session_id].rb_id;
1125
                  (otg_pkt->otg_pkt).sdu_buffer = (uint8_t*) packet_gen_multicast(enb_module_idP, session_id, ctime, &((otg_pkt->otg_pkt).sdu_buffer_size));
1126
                  if ((otg_pkt->otg_pkt).sdu_buffer != NULL) {
1127 1128 1129 1130
                      (otg_pkt->otg_pkt).rb_id      = rb_id;
                      (otg_pkt->otg_pkt).module_id  = enb_module_idP;
                      (otg_pkt->otg_pkt).dst_id     = session_id;
                      (otg_pkt->otg_pkt).is_ue      = FALSE;
1131
                      //Adding the packet to the OTG-PDCP buffer
1132
                      (otg_pkt->otg_pkt).mode       = PDCP_TRANSMISSION_MODE_TRANSPARENT;
Lionel Gauthier's avatar
Lionel Gauthier committed
1133
                      pkt_list_add_tail_eurecom(otg_pkt, &(otg_pdcp_buffer[enb_module_idP]));
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
                      LOG_I(EMU, "[eNB %d] ADD packet (%p) multicast to OTG buffer for dst %d on rb_id %d\n",
                          (otg_pkt->otg_pkt).module_id, otg_pkt, (otg_pkt->otg_pkt).dst_id,(otg_pkt->otg_pkt).rb_id);
                  } else {
                      //LOG_I(EMU, "OTG returns null \n");
                      free(otg_pkt);
                      otg_pkt=NULL;
                  }


                  // old version
                  /*	    // MBSM multicast traffic
1145 1146 1147 1148 1149
#ifdef Rel10
	    if (frame >= 46) {// only generate when UE can receive MTCH (need to control this value)
	      for (service_id = 0; service_id < 2 ; service_id++) { //maxServiceCount
		for (session_id = 0; session_id < 2; session_id++) { // maxSessionPerPMCH
		  //   LOG_I(OTG,"DUY:frame %d, pdcp_mbms_array[module_id][rb_id].instanciated_instance is %d\n",frame,pdcp_mbms_array[module_id][service_id*maxSessionPerPMCH + session_id].instanciated_instance);
1150
		  if ((pdcp_mbms_array[module_idP][service_id*maxSessionPerPMCH + session_id].instanciated_instance== module_idP + 1) && (eNB_flag == 1)){ // this service/session is configured
1151 1152 1153 1154 1155
		    // LOG_T(OTG,"multicast packet gen for (service/mch %d, session/lcid %d)\n", service_id, session_id);
		    // Duy add
		    LOG_I(OTG, "frame %d, multicast packet gen for (service/mch %d, session/lcid %d, rb_id %d)\n",frame, service_id, session_id,service_id*maxSessionPerPMCH + session_id);
		    // end Duy add
		    rb_id = pdcp_mbms_array[module_id][service_id*maxSessionPerPMCH + session_id].rb_id;
1156
		    otg_pkt=(uint8_t*) packet_gen_multicast(module_idP, session_id, ctime, &pkt_size);
1157
		    if (otg_pkt != NULL) {
1158
		      LOG_D(OTG,"[eNB %d] sending a multicast packet from module %d on rab id %d (src %d, dst %d) pkt size %d\n", eNB_index, module_idP, rb_id, module_idP, session_id, pkt_size);
1159 1160 1161 1162 1163 1164 1165
		      pdcp_data_req(module_id, frame, eNB_flag, rb_id, RLC_MUI_UNDEFINED, RLC_SDU_CONFIRM_NO, pkt_size, otg_pkt,PDCP_TM);
		      free(otg_pkt);
		    }
		  }
		}
	      }
	    } // end multicast traffic
1166
#endif
1167
                   */
1168

1169

1170 1171
              }
          }
1172 1173
      }
      //    } // end multicast traffic
1174
#endif
1175 1176 1177
  }
#else
  if (otg_enabled==1) {
1178 1179 1180
      ctime = frame * 100;
      for (dst_id = 0; dst_id < NUMBER_OF_UE_MAX; dst_id++) {
          if (mac_get_rrc_status(eNB_index, eNB_flag, dst_id ) > 2) {
1181
              otg_pkt = malloc (sizeof(Packet_otg_elt_t));
1182 1183
              (otg_pkt->otg_pkt).sdu_buffer = packet_gen(module_instP, dst_id, ctime, &pkt_size);
              if (otg_pkt != NULL) {
1184
                  rb_id = DTCH-2;
1185
                  (otg_pkt->otg_pkt).rb_id     = rb_id;
1186
                  (otg_pkt->otg_pkt).module_id = module_idP;
1187 1188
                  (otg_pkt->otg_pkt).is_ue     = FALSE;
                  (otg_pkt->otg_pkt).mode      = PDCP_TRANSMISSION_MODE_DATA;
1189
                  //Adding the packet to the OTG-PDCP buffer
1190
                  pkt_list_add_tail_eurecom(otg_pkt, &(otg_pdcp_buffer[module_idP]));
1191 1192 1193 1194 1195 1196 1197 1198
                  LOG_I(EMU, "[eNB %d] ADD pkt to OTG buffer for dst %d on rb_id %d\n", (otg_pkt->otg_pkt).module_id, (otg_pkt->otg_pkt).dst_id,(otg_pkt->otg_pkt).rb_id);
              } else {
                  //LOG_I(EMU, "OTG returns null \n");
                  free(otg_pkt);
                  otg_pkt=NULL;
              }
          }
      }
1199 1200 1201 1202
  }
#endif
}

1203
void update_otg_UE(module_id_t ue_mod_idP, unsigned int ctime) {
Cedric Roux's avatar
Cedric Roux committed
1204
#if defined(USER_MODE) && defined(OAI_EMU)
1205
  if (oai_emulation.info.otg_enabled ==1 ) {
1206 1207 1208 1209 1210 1211 1212
      module_id_t dst_id, src_id; //dst_id = eNB_index
      module_id_t module_id = ue_mod_idP+NB_eNB_INST;

      src_id = module_id;

      for (dst_id=0;dst_id<NUMBER_OF_eNB_MAX;dst_id++) {
          if (mac_get_rrc_status(ue_mod_idP, 0, dst_id ) > 2 /*RRC_CONNECTED*/) {
1213
              Packet_otg_elt_t *otg_pkt = malloc (sizeof(Packet_otg_elt_t));
1214
              if (otg_pkt!=NULL)
1215
                memset(otg_pkt,0,sizeof(Packet_otg_elt_t));
1216 1217 1218 1219
              else {
                  LOG_E(OTG,"not enough memory\n");
                  exit(-1);
              }// Manage to add this packet to the tail of your list
1220
              (otg_pkt->otg_pkt).sdu_buffer = (uint8_t*) packet_gen(src_id, dst_id, 0, ctime, &((otg_pkt->otg_pkt).sdu_buffer_size));
1221 1222

              if ((otg_pkt->otg_pkt).sdu_buffer != NULL) {
1223
                  (otg_pkt->otg_pkt).rb_id     = DTCH-2;
1224 1225 1226 1227
                  (otg_pkt->otg_pkt).module_id = module_id;
                  (otg_pkt->otg_pkt).dst_id    = dst_id;
                  (otg_pkt->otg_pkt).is_ue     = 1;
                  //Adding the packet to the OTG-PDCP buffer
1228
                  (otg_pkt->otg_pkt).mode      = PDCP_TRANSMISSION_MODE_DATA;
1229 1230 1231 1232 1233 1234 1235 1236
                  pkt_list_add_tail_eurecom(otg_pkt, &(otg_pdcp_buffer[module_id]));
                  LOG_I(EMU, "[UE %d] ADD pkt to OTG buffer with size %d for dst %d on rb_id %d \n",
                      (otg_pkt->otg_pkt).module_id, otg_pkt->otg_pkt.sdu_buffer_size, (otg_pkt->otg_pkt).dst_id,(otg_pkt->otg_pkt).rb_id);
              } else {
                  free(otg_pkt);
                  otg_pkt=NULL;
              }
          }
1237 1238 1239 1240
      }
  }
#endif
}
1241
#endif
1242

1243 1244
int init_slot_isr(void)
{
1245 1246
  if (oai_emulation.info.slot_isr) {
      struct itimerspec its;
1247

1248
      int sfd;
1249

1250 1251 1252 1253 1254
      sfd = timerfd_create(CLOCK_REALTIME, 0);
      if (sfd == -1) {
          LOG_E(EMU, "Failed in timerfd_create (%d:%s)\n", errno, strerror(errno));
          exit(EXIT_FAILURE);
      }
1255

1256 1257 1258 1259 1260
      /* Start the timer */
      its.it_value.tv_sec = 0;
      its.it_value.tv_nsec = 500 * 1000;
      its.it_interval.tv_sec = its.it_value.tv_sec;
      its.it_interval.tv_nsec = its.it_value.tv_nsec;
1261

1262 1263 1264 1265
      if (timerfd_settime(sfd, TFD_TIMER_ABSTIME, &its, NULL) == -1) {
          LOG_E(EMU, "Failed in timer_settime (%d:%s)\n", errno, strerror(errno));
          exit(EXIT_FAILURE);
      }
1266

1267 1268 1269
      oai_emulation.info.slot_sfd = sfd;
  }
  return 0;
1270 1271 1272 1273
}

void wait_for_slot_isr(void)
{
1274 1275
  uint64_t exp;
  ssize_t res;
1276

1277 1278
  if (oai_emulation.info.slot_sfd > 0) {
      res = read(oai_emulation.info.slot_sfd, &exp, sizeof(exp));
1279

1280 1281 1282 1283 1284
      if ((res < 0) || (res != sizeof(exp))) {
          LOG_E(EMU, "Failed in read (%d:%s)\n", errno, strerror(errno));
          exit(EXIT_FAILURE);
      }
  }
1285 1286
}

1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
void exit_fun(const char* s)
{
  void *array[10];
  size_t size;

  size = backtrace(array, 10);
  backtrace_symbols_fd(array, size, 2);

  fprintf(stderr, "Error: %s. Exiting!\n",s);
  exit (-1);
}

void init_time() {
  clock_gettime (CLOCK_REALTIME, &time_spec);
1301 1302
  time_now      = (unsigned long) time_spec.tv_nsec;
  td_avg        = 0;
1303
  sleep_time_us = SLEEP_STEP_US;
1304
  td_avg        = TARGET_SF_TIME_NS;
1305 1306
}