oaisim_functions.c 80.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
 * the OAI Public License, Version 1.0  (the "License"); you may not use this file
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */
21

22 23
/*! \file oaisim_functions.c
* \brief function primitives of oaisim
24
* \author Navid Nikaein
25 26 27 28 29 30 31
* \date 2013-2015
* \version 1.0
* \company Eurecom
* \email: openair_tech@eurecom.fr
* \note
* \warning
*/
32 33


34 35 36 37
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
38
#include <execinfo.h>
39
#include <time.h>
Lionel Gauthier's avatar
Lionel Gauthier committed
40
#include <mcheck.h>
41
#include <sys/timerfd.h>
42

43
#include "assertions.h"
44 45 46 47
#include "oaisim_functions.h"

#include "PHY/extern.h"
#include "LAYER2/MAC/extern.h"
48 49
#ifdef OPENAIR2
#include "LAYER2/MAC/proto.h"
50
#endif
51 52 53
#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
54
#include "RRC/L2_INTERFACE/openair_rrc_L2_interface.h"
55
#include "PHY_INTERFACE/extern.h"
56
//#include "ARCH/CBMIMO1/DEVICE_DRIVER/extern.h"
57
#include "SCHED/extern.h"
Cedric Roux's avatar
Cedric Roux committed
58
#include "SIMULATION/ETH_TRANSPORT/proto.h"
59 60 61
#include "UTIL/OCG/OCG_extern.h"
#include "UTIL/LOG/vcd_signal_dumper.h"
#include "UTIL/OPT/opt.h"
62
#include "UTIL/OTG/otg_config.h"
winckel's avatar
winckel committed
63
#include "UTIL/OTG/otg_tx.h"
64
#if ENABLE_RAL
65 66
#include "lteRALenb.h"
#include "lteRALue.h"
67
#endif
68

Cedric Roux's avatar
Cedric Roux committed
69
#include "cor_SF_sim.h"
70
#include "enb_config.h"
Cedric Roux's avatar
Cedric Roux committed
71

72 73 74 75
#if defined(ENABLE_ITTI)
# include "intertask_interface.h"
#endif

76 77 78 79
#if defined(ENABLE_USE_MME)
# include "s1ap_eNB.h"
#endif

80 81 82
#include "../../ARCH/COMMON/common_lib.h"
#include "../../ARCH/ETHERNET/USERSPACE/LIB/if_defs.h"

83
#ifdef SMBV
84
extern uint8_t config_smbv;
85 86 87 88 89 90 91 92 93
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

Cedric Roux's avatar
Cedric Roux committed
94
#ifndef min
95
#define min(a,b) ((a)<(b)?(a):(b))
Cedric Roux's avatar
Cedric Roux committed
96
#endif
97

98 99 100 101
int           otg_times             = 0;
int           if_times              = 0;
int           for_times             = 0;

102
uint16_t           Nid_cell              = 0; //needed by init_lte_vars
103
int           nb_antennas_rx_ue        = 1; // //
104
uint8_t            target_dl_mcs         = 16; // max mcs used by MAC scheduler
105
uint8_t            rate_adaptation_flag  = 0;
106
uint8_t        set_snr         = 0;
107
uint8_t            set_sinr              = 0;
108
double             snr_dB=0, sinr_dB=0;
109 110 111 112
uint8_t            set_seed              = 0;
uint8_t            cooperation_flag;          // for cooperative communication
uint8_t            abstraction_flag      = 0;
uint8_t            ethernet_flag         = 0;
113
double        snr_step              = 1.0;
114
uint8_t            ue_connection_test    = 0;
115
double        forgetting_factor     = 0.0;
116 117 118
uint8_t            beta_ACK              = 0;
uint8_t            beta_RI               = 0;
uint8_t            beta_CQI              = 2;
119
uint8_t            target_ul_mcs         = 16;
Raymond Knopp's avatar
 
Raymond Knopp committed
120
LTE_DL_FRAME_PARMS *frame_parms[MAX_NUM_CCs];
121 122
int           map1,map2;
double      **ShaF                  = NULL;
123
// pointers signal buffers (s = transmit, r,r0 = receive)
124
double      **s_re, **s_im, **r_re, **r_im, **r_re0, **r_im0;
lfarizav's avatar
lfarizav committed
125
double      **s_re_f, **s_im_f, **r_re_f, **r_im_f, **r_re0_f, **r_im0_f;
126 127
node_list*     ue_node_list          = NULL;
node_list*     enb_node_list         = NULL;
128
int           omg_period            = 10000;
129
int           pdcp_period           = 0;
130
int           cba_backoff           = 30;
131 132
// time calibration for soft realtime mode
struct timespec time_spec;
133 134 135 136 137
unsigned long   time_last           = 0;
unsigned long   time_now            = 0;
int             td                  = 0;
int             td_avg              = 0;
int             sleep_time_us       = 0;
138

Florian Kaltenberger's avatar
Florian Kaltenberger committed
139 140
int phy_test = 0;

141 142 143 144
#ifdef OPENAIR2
// omv related info
//pid_t omv_pid;
char full_name[200];
145
extern int pfd[2]; // fd for omv : fixme: this could be a local var
146 147 148 149 150 151 152 153 154 155 156 157 158
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

159
Packet_OTG_List_t *otg_pdcp_buffer = NULL;
160 161 162

extern node_desc_t *enb_data[NUMBER_OF_eNB_MAX];
extern node_desc_t *ue_data[NUMBER_OF_UE_MAX];
Raymond Knopp's avatar
 
Raymond Knopp committed
163 164
extern channel_desc_t *eNB2UE[NUMBER_OF_eNB_MAX][NUMBER_OF_UE_MAX][MAX_NUM_CCs];
extern channel_desc_t *UE2eNB[NUMBER_OF_UE_MAX][NUMBER_OF_eNB_MAX][MAX_NUM_CCs];
165 166

extern mapping small_scale_names[];
Cedric Roux's avatar
Cedric Roux committed
167
#if defined(Rel10) || defined(Rel14)
Lionel Gauthier's avatar
Lionel Gauthier committed
168 169 170
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
171

172 173
extern time_stats_t dl_chan_stats;
extern time_stats_t ul_chan_stats;
174

175 176
extern int xforms;

177

178 179 180
extern uint32_t          downlink_frequency[MAX_NUM_CCs][4];
extern int32_t           uplink_frequency_offset[MAX_NUM_CCs][4];

181
 eth_params_t *eth_params;
182 183

void init_eNB(eNB_func_t node_function[], eNB_timing_t node_timing[],int nb_inst,eth_params_t *,int,int);
184 185
void stop_eNB(int nb_inst);

186
static const Enb_properties_array_t *enb_properties;
187

188
int oaisim_flag=1;
189 190
void get_simulation_options(int argc, char *argv[])
{
winckel's avatar
winckel committed
191
  int                           option;
lfarizav's avatar
lfarizav committed
192 193
  int CC_id;
  int k;
194
  char  *conf_config_file_name = NULL;
winckel's avatar
winckel committed
195
  enum long_option_e {
196
    LONG_OPTION_START = 0x100, /* Start after regular single char options */
winckel's avatar
winckel committed
197

198
    LONG_OPTION_ENB_CONF,
winckel's avatar
winckel committed
199

200 201
    LONG_OPTION_PDNC_PERIOD,
    LONG_OPTION_OMG_PERIOD,
202
    LONG_OPTION_OEH_ENABLED,
winckel's avatar
winckel committed
203

204 205 206 207
    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
208

209 210 211
    LONG_OPTION_ENB_MIHF_REMOTE_PORT,
    LONG_OPTION_ENB_MIHF_IP_ADDRESS,
    LONG_OPTION_ENB_MIHF_ID,
winckel's avatar
winckel committed
212

213 214 215 216
    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
217

218 219 220
    LONG_OPTION_UE_MIHF_REMOTE_PORT,
    LONG_OPTION_UE_MIHF_IP_ADDRESS,
    LONG_OPTION_UE_MIHF_ID,
Lionel Gauthier's avatar
Lionel Gauthier committed
221 222

    LONG_OPTION_MALLOC_TRACE_ENABLED,
223 224

    LONG_OPTION_CBA_BACKOFF_TIMER,
225

Florian Kaltenberger's avatar
Florian Kaltenberger committed
226
    LONG_OPTION_PHYTEST,
227
    LONG_OPTION_XFORMS,
Cedric Roux's avatar
Cedric Roux committed
228 229 230 231

#if T_TRACER
    LONG_OPTION_T_PORT,
    LONG_OPTION_T_NOWAIT,
232
    LONG_OPTION_T_DONT_FORK,
Cedric Roux's avatar
Cedric Roux committed
233
#endif
winckel's avatar
winckel committed
234
  };
winckel's avatar
winckel committed
235

winckel's avatar
winckel committed
236
  static struct option long_options[] = {
237
    {"enb-conf",               required_argument, 0, LONG_OPTION_ENB_CONF},
winckel's avatar
winckel committed
238

239 240 241
    {"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
242

243 244 245 246
    {"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
247

248 249 250
    {"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
251

252 253 254 255
    {"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
256

257 258 259
    {"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},
260

261
    {"malloc-trace-enabled",   no_argument,       0, LONG_OPTION_MALLOC_TRACE_ENABLED},
262

263
    {"cba-backoff",            required_argument, 0, LONG_OPTION_CBA_BACKOFF_TIMER},
264

Florian Kaltenberger's avatar
Florian Kaltenberger committed
265
    {"phy-test", no_argument, NULL, LONG_OPTION_PHYTEST},
266
    {"xforms",                 no_argument,       0, LONG_OPTION_XFORMS},
Florian Kaltenberger's avatar
Florian Kaltenberger committed
267

Cedric Roux's avatar
Cedric Roux committed
268 269 270
#if T_TRACER
    {"T_port",                 required_argument, 0, LONG_OPTION_T_PORT},
    {"T_nowait",               no_argument,       0, LONG_OPTION_T_NOWAIT},
271
    {"T_dont_fork",            no_argument,       0, LONG_OPTION_T_DONT_FORK},
Cedric Roux's avatar
Cedric Roux committed
272 273
#endif

274
    {NULL, 0, NULL, 0}
winckel's avatar
winckel committed
275
  };
winckel's avatar
winckel committed
276

277
  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) {
278
    switch (option) {
Florian Kaltenberger's avatar
Florian Kaltenberger committed
279 280 281 282
    case LONG_OPTION_PHYTEST:
      phy_test = 1;
      break;

283 284 285 286 287 288
    case LONG_OPTION_ENB_CONF:
      if (optarg) {
        free(conf_config_file_name); // prevent memory leak if option is used multiple times
        conf_config_file_name = strdup(optarg);
        printf("eNB configuration file is %s\n", conf_config_file_name);
      }
winckel's avatar
winckel committed
289

290
      break;
winckel's avatar
winckel committed
291

292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
    case LONG_OPTION_PDNC_PERIOD:
      if (optarg) {
        pdcp_period = atoi(optarg);
        printf("PDCP period is %d\n", pdcp_period);
      }

      break;

    case LONG_OPTION_OMG_PERIOD:
      if (optarg) {
        omg_period = atoi(optarg);
        printf("OMG period is %d\n", omg_period);
      }

      break;

    case LONG_OPTION_OEH_ENABLED:
      oai_emulation.info.oeh_enabled = 1;
      break;

    case LONG_OPTION_MALLOC_TRACE_ENABLED:
      mtrace();
      break;

    case LONG_OPTION_CBA_BACKOFF_TIMER:
      oai_emulation.info.cba_backoff=atoi(optarg);
      cba_backoff=atoi(optarg);
      printf("setting CBA backoff to %d\n", cba_backoff);
      break;
Lionel Gauthier's avatar
Lionel Gauthier committed
321

322
#if ENABLE_RAL
winckel's avatar
winckel committed
323

324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 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 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
    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;

    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;

    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;

    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;

    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;

    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;

    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;

    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;

    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;

    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;

    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;

    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;

    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;

    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;
435
#endif
436

437 438 439 440
    case LONG_OPTION_XFORMS:
      xforms=1;
      break;

Cedric Roux's avatar
Cedric Roux committed
441 442 443 444 445 446 447 448 449 450 451 452 453
#if T_TRACER
    case LONG_OPTION_T_PORT: {
      extern int T_port;
      if (optarg == NULL) abort();  /* should not happen */
      T_port = atoi(optarg);
      break;
    }

    case LONG_OPTION_T_NOWAIT: {
      extern int T_wait;
      T_wait = 0;
      break;
    }
454 455 456 457 458 459

    case LONG_OPTION_T_DONT_FORK: {
      extern int T_dont_fork;
      T_dont_fork = 1;
      break;
    }
Cedric Roux's avatar
Cedric Roux committed
460 461
#endif

462 463
    case 'a':
      abstraction_flag = 1;
464 465
      printf("FATAL: -a flag not functional for the moment.\nWe are working on fixing the abstraction mode.\n");
      exit(1);
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 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
      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);
      strncpy(oai_emulation.info.local_server, optarg, sizeof(oai_emulation.info.local_server));
      oai_emulation.info.local_server[sizeof(oai_emulation.info.local_server) - 1] = 0; // terminate string
      oai_emulation.info.ocg_enabled=1;
      break;

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

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

    case 'e':
      oai_emulation.info.extended_prefix_flag[0] = 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] = 0;
      oai_emulation.info.frame_type_name[0] = "FDD";
      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
543
#ifdef PROC
544 545 546
      Process_Flag=1;
      node_id = wgt+atoi(optarg);
      port+=atoi(optarg);
winckel's avatar
winckel committed
547
#endif
548 549 550 551 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 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
      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");
      exit(-1);
      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);
      printf("Max target downlink MCS used by MAC scheduler is set to %d\n", target_dl_mcs);
      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);
      oai_emulation.emulation_config.emulation_time_ms= oai_emulation.info.n_frames * 10; // 10 ms frame
      //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);
winckel's avatar
winckel committed
608
        exit(-1);
609
      }
610

611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 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 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
      break;

    case 'O':
      if (optarg) {
        free(conf_config_file_name); // prevent memory leak if option is used multiple times
        conf_config_file_name = strdup(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;
      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;
        opt_enabled = 0;
      }

      oai_emulation.info.opt_mode = opt_type;
      break;

    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;

    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[0] = atoi (optarg);

      if ((oai_emulation.info.N_RB_DL[0] != 6) && (oai_emulation.info.N_RB_DL[0] != 15) && (oai_emulation.info.N_RB_DL[0] != 25)
          && (oai_emulation.info.N_RB_DL[0] != 50) && (oai_emulation.info.N_RB_DL[0] != 75) && (oai_emulation.info.N_RB_DL[0] != 100)) {
        printf("Illegal N_RB_DL %d (should be one of 6,15,25,50,75,100)\n", oai_emulation.info.N_RB_DL[0]);
        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':
      target_ul_mcs = atoi (optarg);
      printf("Max target uplink MCS used by MAC scheduler is set to %d\n", target_ul_mcs);
      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':
722
#ifdef SMBV
723 724 725 726 727
      config_smbv = 1;

      if(atoi(optarg)!=0)
        strcpy(smbv_ip,optarg);

728
#endif
729
      break;
winckel's avatar
winckel committed
730

731
    case 'x':
732
      /*
733 734
      oai_emulation.info.transmission_mode[0] = atoi (optarg);

735
      if ((oai_emulation.info.transmission_mode[0] != 1) && (oai_emulation.info.transmission_mode[0] != 2) &&
736
	  (oai_emulation.info.transmission_mode[0] != 3) && (oai_emulation.info.transmission_mode[0] != 4) &&
737
          (oai_emulation.info.transmission_mode[0] != 5) && (oai_emulation.info.transmission_mode[0] != 6)) && (oai_emulation.info.transmission_mode[0] !=7)) {
738 739 740
        printf("Unsupported transmission mode %d\n",oai_emulation.info.transmission_mode[0]);
        exit(-1);
      }
741 742 743
      */
      printf("Option -x deprecated. Please set transmission mode in eNB config file\n");
      exit(-1);
winckel's avatar
winckel committed
744

745 746 747
      break;

    case 'X':
winckel's avatar
winckel committed
748
#ifdef PROC
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
      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
764
#endif
765
      break;
winckel's avatar
winckel committed
766

767
    case 'y':
768
      nb_antennas_rx_ue=atoi(optarg);
winckel's avatar
winckel committed
769

770
      if (nb_antennas_rx_ue>4) {
771 772 773
        printf("Cannot have more than 4 antennas\n");
        exit(-1);
      }
winckel's avatar
winckel committed
774

775
      break;
winckel's avatar
winckel committed
776

777 778 779
    case 'Y':
      oai_emulation.info.g_log_verbosity_option = strdup(optarg);
      break;
winckel's avatar
winckel committed
780

781 782 783 784 785 786 787 788 789 790 791 792 793
    case 'z':
      cooperation_flag = atoi (optarg);
      break;

    case 'Z':
      /* Sebastien ROUX: Reserved for future use (currently used in ltenow branch) */
      break;

    default:
      help ();
      exit (-1);
      break;
    }
winckel's avatar
winckel committed
794
  }
795

796 797 798
  if ((oai_emulation.info.nb_enb_local > 0) && (conf_config_file_name != NULL)) {
    /* Read eNB configuration file */
    enb_properties = enb_config_init(conf_config_file_name);
799
    printf("OAISIM reads eNB configuration file for %d eNBs\n",(int)(*enb_properties).number);
800 801 802
    AssertFatal (oai_emulation.info.nb_enb_local <= enb_properties->number,
                 "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);
803 804 805 806 807
    //printf("Read eNB configuration file. enb_properties->number_i %d\n",i);
    eth_params = (eth_params_t*)malloc(enb_properties->number * sizeof(eth_params_t));
    memset(eth_params, 0, enb_properties->number * sizeof(eth_params_t));
    /*For each eNB*/
   for (int i = 0; i < enb_properties->number; i++) {   
808
    for (int j=0; j<enb_properties->properties[i]->nb_rrh_gw; j++) {
809
      
810
      if (enb_properties->properties[i]->rrh_gw_config[j].active == 1 ) {
811
	//	local_remote_radio = BBU_REMOTE_RADIO_HEAD;
812 813 814 815 816 817
	(eth_params+i+j)->local_if_name             = enb_properties->properties[i]->rrh_gw_config[j].rrh_gw_if_name;
	(eth_params+i+j)->my_addr                   = enb_properties->properties[i]->rrh_gw_config[j].local_address;
	(eth_params+i+j)->my_port                   = enb_properties->properties[i]->rrh_gw_config[j].local_port;
	(eth_params+i+j)->remote_addr               = enb_properties->properties[i]->rrh_gw_config[j].remote_address;
	(eth_params+i+j)->remote_port               = enb_properties->properties[i]->rrh_gw_config[j].remote_port;
        printf("%d RRH associated to eNB %d and local ip %s, (i,j)(%d,%d)\n",enb_properties->properties[i]->nb_rrh_gw,enb_properties->properties[i]->rrh_gw_config[j].eNB,(eth_params+i+j)->my_addr,i,j);
818
	if (enb_properties->properties[i]->rrh_gw_config[j].raw == 1) {
819
	  (eth_params+i+j)->transp_preference       = ETH_RAW_MODE; 
820
	} else if (enb_properties->properties[i]->rrh_gw_config[j].rawif4p5 == 1) {
821
	  (eth_params+i+j)->transp_preference       = ETH_RAW_IF4p5_MODE;             
822
	} else if (enb_properties->properties[i]->rrh_gw_config[j].udpif4p5 == 1) {
823 824
	  //printf("enb_properties->properties[%d]->rrh_gw_config[%d].udpif4p5 == %d\n",i,j,enb_properties->properties[i]->rrh_gw_config[j].udpif4p5);
	  (eth_params+i+j)->transp_preference       = ETH_UDP_IF4p5_MODE;             
825
	} else if (enb_properties->properties[i]->rrh_gw_config[j].rawif5_mobipass == 1) {
826
	  (eth_params+i+j)->transp_preference       = ETH_RAW_IF5_MOBIPASS;             
827
	} else {
828
	  (eth_params+i+j)->transp_preference       = ETH_UDP_MODE;	 
829 830 831
	}
      }
    }
832
    /* Update some simulation parameters */
lfarizav's avatar
lfarizav committed
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
    for (CC_id=0;CC_id<MAX_NUM_CCs;CC_id++){
	    oai_emulation.info.frame_type[CC_id]           = enb_properties->properties[i]->frame_type[CC_id];
	    oai_emulation.info.tdd_config[CC_id]           = enb_properties->properties[i]->tdd_config[CC_id];
	    oai_emulation.info.tdd_config_S[CC_id]         = enb_properties->properties[i]->tdd_config_s[CC_id];
	    oai_emulation.info.extended_prefix_flag[CC_id] = enb_properties->properties[i]->prefix_type[CC_id];
	    oai_emulation.info.N_RB_DL[CC_id]              = enb_properties->properties[i]->N_RB_DL[CC_id];

	    oai_emulation.info.node_function[CC_id]        = enb_properties->properties[i]->cc_node_function[CC_id];
	    oai_emulation.info.node_timing[CC_id]          = enb_properties->properties[i]->cc_node_timing[CC_id];
	for (k=0;k<4;k++){
	    downlink_frequency[CC_id][0]                   = enb_properties->properties[i]->downlink_frequency[CC_id];
	    uplink_frequency_offset[CC_id][0]              = enb_properties->properties[i]->uplink_frequency_offset[CC_id];
	}
    }
   }
848
 }
849 850
  free(conf_config_file_name);
  conf_config_file_name = 0;
851
  //NB_eNB_INST=NB_eNB_INST-1;
852 853
}

854 855
void check_and_adjust_params(void)
{
856

857
  int32_t ret;
858 859
  int i,j;

860
  if (oai_emulation.info.nb_ue_local  + oai_emulation.info.nb_rn_local > NUMBER_OF_UE_MAX) {
861 862
    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);
863 864
  }

865
  if (oai_emulation.info.nb_enb_local + oai_emulation.info.nb_rn_local > NUMBER_OF_eNB_MAX) {
866 867
    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);
868
  }
869

870
  if (oai_emulation.info.nb_rn_local > NUMBER_OF_RN_MAX) {
871 872
    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);
873
  }
874

875 876 877 878 879 880 881 882 883 884 885
  // 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
886
  ret = netlink_init();
887

888
  if (ret < 0)
889
    LOG_W(EMU,"[INIT] Netlink not available, careful ...\n");
890 891

  if (ethernet_flag == 1) {
892 893
    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;
894

895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
    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 ();

    while (emu_tx_status != SYNCED_TRANSPORT) {
      LOG_I (EMU, " Waiting for EMU Transport to be synced\n");
      emu_transport_sync ();    //emulation_tx_rx();
    }
918 919
  } // ethernet flag

920
  //
921 922
  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;
923 924
  NB_RN_INST = oai_emulation.info.nb_rn_local + oai_emulation.info.nb_rn_remote;

925
#if defined(PDCP_USE_NETLINK_QUEUES) && defined(OPENAIR2)
926 927 928
  pdcp_netlink_init();
#endif

929
  if (NB_RN_INST > 0 ) {
930 931
    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);
932

933
    LOG_N(EMU,"Adjust the number of eNB inst (%d->%d) and UE inst (%d->%d)\n ",
934 935
          NB_eNB_INST, NB_eNB_INST+NB_RN_INST,
          NB_UE_INST, NB_UE_INST+NB_RN_INST);
936 937
    NB_eNB_INST+=NB_RN_INST;
    NB_UE_INST+=NB_RN_INST;
938
  }
939

940
  LOG_I(EMU,"Total number of UE %d (first local %d , num local %d, remote %d, relay %d) mobility %s \n",
941 942 943
        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);
944 945

  LOG_I(EMU,"Total number of eNB %d (local %d, remote %d, relay %d) mobility %s \n",
946 947 948
        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);
949

950 951
}

952
#ifdef OPENAIR2
953 954
void init_omv(void)
{
955 956
  if (oai_emulation.info.omv_enabled == 1) {

957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
    if(pipe(pfd) == -1)
      perror("pipe error \n");

    snprintf( full_name, sizeof(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]);

    switch(fork()) {
    case -1 :
      perror("fork failed \n");
      break;

    case 0 : // child is going to be the omv, it is the reader
      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] == 0) ? "FDD" : "TDD");
      sprintf(tdd_config, "%d", oai_emulation.info.tdd_config[0]);
      // 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" );
990 991
  }
}
992
#endif
993

994 995
void init_seed(uint8_t set_seed)
{
996 997 998

  if(set_seed) {

999
    randominit (oai_emulation.info.seed);
1000
    table_nor(oai_emulation.info.seed);//Setup for the normal probability distribution function
1001
    set_taus_seed (oai_emulation.info.seed);
1002 1003

  } else {
1004
    randominit (0);
1005
    table_nor(123456789);//Setup for the normal probability distribution function. Always use a non-zero unsigned long argument. 
1006
    set_taus_seed (0);
1007 1008 1009
  }
}

1010 1011 1012 1013
openair0_timestamp current_eNB_rx_timestamp[NUMBER_OF_eNB_MAX][MAX_NUM_CCs];
openair0_timestamp current_UE_rx_timestamp[NUMBER_OF_UE_MAX][MAX_NUM_CCs];
openair0_timestamp last_eNB_rx_timestamp[NUMBER_OF_eNB_MAX][MAX_NUM_CCs];
openair0_timestamp last_UE_rx_timestamp[NUMBER_OF_UE_MAX][MAX_NUM_CCs];
1014

1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
int eNB_trx_start(openair0_device *device) {
  return(0);
}

void eNB_trx_end(openair0_device *device) {
  return;
}

int eNB_trx_stop(openair0_device *device) {
  return(0);
}
int UE_trx_start(openair0_device *device) {
  return(0);
}
Cedric Roux's avatar
Cedric Roux committed
1029 1030
void UE_trx_end(openair0_device *device) {
  return;
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
}
int UE_trx_stop(openair0_device *device) {
  return(0);
}
int eNB_trx_set_freq(openair0_device *device, openair0_config_t *openair0_cfg, int dummy) {
  return(0);
}
int eNB_trx_set_gains(openair0_device *device, openair0_config_t *openair0_cfg) {
  return(0);
}
int UE_trx_set_freq(openair0_device *device, openair0_config_t *openair0_cfg, int dummy) {
  return(0);
}
int UE_trx_set_gains(openair0_device *device, openair0_config_t *openair0_cfg) {
  return(0);
}

1048 1049
extern pthread_mutex_t subframe_mutex;
extern int subframe_eNB_mask,subframe_UE_mask;
1050
//static int first_run=0;
1051 1052
//static int count=0;
//static int sum=0;
1053 1054
int eNB_trx_read(openair0_device *device, openair0_timestamp *ptimestamp, void **buff, int nsamps, int cc)
{
1055 1056 1057 1058 1059 1060
  //if (!first_run)
  //{
  //   first_run=1;
  //} 
  //if (count==1000)
  //{
1061
     //Use ./oaisim -q option to enable the oai performance profiler.
1062 1063 1064 1065 1066 1067
     //count=0;
     //sum=0;
     //print_opp_meas_oaisim ();
     //reset_opp_meas_oaisim ();
  //}
  //count++;
1068
  int ret = nsamps;
1069
  int eNB_id = device->Mod_id;
1070
  //printf("eNB_id %d\n",eNB_id);
1071
  int CC_id  = device->CC_id;
1072
  int UE_id = 0;
1073

1074
  //printf("eNB_trx_read: eNB %d, Type %s\n",eNB_id,(PHY_vars_eNB_g[eNB_id][CC_id]->frame_parms.frame_type==FDD)?"FDD":"TDD");
1075
  int subframe;
1076 1077
  int read_samples, max_samples;
  openair0_timestamp last = last_eNB_rx_timestamp[eNB_id][CC_id];
1078 1079

  *ptimestamp = last_eNB_rx_timestamp[eNB_id][CC_id];
lfarizav's avatar
lfarizav committed
1080
 
1081
  int do_ofdm_mod = PHY_vars_UE_g[UE_id][CC_id]->do_ofdm_mod;
1082

lfarizav's avatar
lfarizav committed
1083
  uint32_t frame;
1084
  //int n_ra_prb;
1085
  //uint8_t tdd_mapindex       = PHY_vars_UE_g[0][0]->prach_resources[eNB_id]->ra_TDD_map_index;
lfarizav's avatar
lfarizav committed
1086 1087 1088 1089 1090 1091 1092 1093

  if (do_ofdm_mod)
  	LOG_D(EMU,"eNB_trx_read nsamps %d TS(%"PRId64",%"PRId64") => subframe %d\n",nsamps,
        current_eNB_rx_timestamp[eNB_id][CC_id],
        last_eNB_rx_timestamp[eNB_id][CC_id],
	(int)((*ptimestamp/(PHY_vars_eNB_g[eNB_id][CC_id]->frame_parms.ofdm_symbol_size*PHY_vars_eNB_g[eNB_id][CC_id]->frame_parms.symbols_per_tti))%10));
  else
  	LOG_D(EMU,"eNB_trx_read nsamps %d TS(%"PRId64",%"PRId64") => subframe %d\n",nsamps,
1094 1095 1096
        current_eNB_rx_timestamp[eNB_id][CC_id],
        last_eNB_rx_timestamp[eNB_id][CC_id],
	(int)((*ptimestamp/PHY_vars_eNB_g[eNB_id][CC_id]->frame_parms.samples_per_tti)%10));
1097 1098
  // if we're at a subframe boundary generate UL signals for this eNB

1099
  while (nsamps) {
1100
    //printf("nsamps %d, eNB %d\n",nsamps,PHY_vars_eNB_g[eNB_id][CC_id]->Mod_id);
1101
    while (current_eNB_rx_timestamp[eNB_id][CC_id] == last) {
1102
      //LOG_D(EMU,"eNB: current TS %"PRId64", last TS %"PRId64", sleeping\n",current_eNB_rx_timestamp[eNB_id][CC_id],last_eNB_rx_timestamp[eNB_id][CC_id]);
1103 1104 1105
      usleep(500);
    }

1106 1107 1108 1109
    read_samples = nsamps;
    max_samples = current_eNB_rx_timestamp[eNB_id][CC_id]-last;
    if (read_samples > max_samples)
      read_samples = max_samples;
1110

1111 1112
    last += read_samples;
    nsamps -= read_samples;
1113

1114 1115
    if (current_eNB_rx_timestamp[eNB_id][CC_id] == last) {
      //subframe = (subframe+9) % 10;
lfarizav's avatar
lfarizav committed
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
	if (do_ofdm_mod)
	{
      		subframe = (last/(PHY_vars_eNB_g[eNB_id][CC_id]->frame_parms.ofdm_symbol_size*PHY_vars_eNB_g[eNB_id][CC_id]->frame_parms.symbols_per_tti))%10;
		frame = (last/(10*PHY_vars_eNB_g[eNB_id][CC_id]->frame_parms.ofdm_symbol_size*PHY_vars_eNB_g[eNB_id][CC_id]->frame_parms.symbols_per_tti))%1023;
	}
	else
	{
		subframe = (last/PHY_vars_eNB_g[eNB_id][CC_id]->frame_parms.samples_per_tti)%10;
		frame = (last/(10*PHY_vars_eNB_g[eNB_id][CC_id]->frame_parms.samples_per_tti))%1023;
	}
1126
      //n_ra_prb = get_prach_prb_offset(frame_parms, PHY_vars_UE_g[0][CC_id]->prach_resources[eNB_id]->ra_TDD_map_index, PHY_vars_UE_g[0][CC_id]->proc.proc_rxtx[subframe&0x1].frame_tx);
lfarizav's avatar
lfarizav committed
1127
      //printf("[oaisim_functs]eNB_trx_read UL subframe %d, frame %d\n",subframe,frame);
1128 1129 1130
      LOG_D(PHY,"eNB_trx_read generating UL subframe %d (Ts %llu, current TS %llu)\n",
            subframe,(unsigned long long)*ptimestamp,
            (unsigned long long)current_eNB_rx_timestamp[eNB_id][CC_id]);
1131

lfarizav's avatar
lfarizav committed
1132
      //printf("is Prach generated? %d, is prach_subframe? %d, frame %d, subframe %d, mode %d\n",PHY_vars_UE_g[0][CC_id]->generate_prach,is_prach_subframe(frame_parms,frame,subframe),frame,subframe,PHY_vars_UE_g[0][CC_id]->UE_mode[eNB_id]);
lfarizav's avatar
lfarizav committed
1133 1134
      if (do_ofdm_mod)
      {
1135
	for (UE_id=0; UE_id<NB_UE_INST; UE_id++){
1136
	     if (eNB_id!=PHY_vars_UE_g[UE_id][CC_id]->common_vars.eNb_id) continue;
1137 1138
		if (is_prach_subframe(&PHY_vars_UE_g[UE_id][CC_id]->frame_parms,frame,subframe) && PHY_vars_UE_g[UE_id][CC_id]->generate_prach)
		{
1139
			//clock_t start=clock();
1140
			//printf("subframe UL PRACH: %d\n",subframe);
1141
			printf("prach is generated for UE %d, eNB %d, subframe %d\n",UE_id,eNB_id,subframe);
1142
			start_meas(&UE2eNB[UE_id][eNB_id][CC_id]->UL_PRACH_channel_freq);
lfarizav's avatar
lfarizav committed
1143
			do_UL_sig_freq_prach(UE2eNB,
1144 1145 1146 1147 1148 1149 1150 1151
				enb_data,
				ue_data,
				subframe,
				0,  // abstraction_flag
				&PHY_vars_eNB_g[eNB_id][CC_id]->frame_parms,
				0,  // frame is only used for abstraction
				eNB_id,
				CC_id);
1152
		        stop_meas(&UE2eNB[UE_id][eNB_id][CC_id]->UL_PRACH_channel_freq);
1153 1154
  			//clock_t stop=clock();
  			/*printf("do_DL_sig time_prach is %f s, AVERAGE time is %f s, count %d, sum %e\n",(float) (stop-start)/CLOCKS_PER_SEC,(float) (sum+stop-start)/(count1*CLOCKS_PER_SEC),count1,sum+stop-start);
1155
  			sum=(sum+stop-start);
1156
        		count1++;*/
1157 1158 1159
			//write_output("txprachF.m","prach_txF", PHY_vars_UE_g[0][CC_id]->prach_vars[0]->prachF,12*frame_parms->ofdm_symbol_size*frame_parms->symbols_per_tti,1,16);
			break;
		}
1160
	}
1161
	start_meas(&UE2eNB[0][eNB_id][CC_id]->UL_channel_freq);
1162
	//clock_t start=clock();
1163
	
lfarizav's avatar
lfarizav committed
1164 1165 1166
        do_UL_sig_freq(UE2eNB,
                enb_data,
                ue_data,
1167
                (subframe-1<0)?9:subframe-1,
lfarizav's avatar
lfarizav committed
1168 1169 1170 1171 1172
                0,  // abstraction_flag
                &PHY_vars_eNB_g[eNB_id][CC_id]->frame_parms,
                0,  // frame is only used for abstraction
                eNB_id,
                CC_id);
1173
	stop_meas(&UE2eNB[0][eNB_id][CC_id]->UL_channel_freq);
1174 1175 1176
        /*clock_t stop=clock();
  	printf("do_UL_sig time is %f s, AVERAGE time is %f s, count %d, sum %d, start %d, stop %d\n",(float) (stop-start)/CLOCKS_PER_SEC,(float) (sum+stop-start)/(count*CLOCKS_PER_SEC),count,sum+stop-start,start,stop);
  	sum=(sum+stop-start);*/
lfarizav's avatar
lfarizav committed
1177 1178
      }
      else
1179
      {
1180
	start_meas(&UE2eNB[0][eNB_id][CC_id]->UL_channel);
lfarizav's avatar
lfarizav committed
1181
        do_UL_sig(UE2eNB,
1182 1183 1184 1185 1186 1187 1188 1189
                enb_data,
                ue_data,
                subframe,
                0,  // abstraction_flag
                &PHY_vars_eNB_g[eNB_id][CC_id]->frame_parms,
                0,  // frame is only used for abstraction
                eNB_id,
                CC_id);
1190
	stop_meas(&UE2eNB[0][eNB_id][CC_id]->UL_channel);
1191
      }
1192 1193 1194

      last_eNB_rx_timestamp[eNB_id][CC_id] = last;
    }
1195
  }
1196
  
1197
  last_eNB_rx_timestamp[eNB_id][CC_id] = last;
1198
  //printf("end of enb_trx_read function\n");
1199
  return ret;
1200 1201
}

1202 1203 1204
int UE_trx_read(openair0_device *device, openair0_timestamp *ptimestamp, void **buff, int nsamps, int cc)
{
  int ret = nsamps;
1205 1206
  int UE_id = device->Mod_id;
  int CC_id  = device->CC_id;
1207 1208
  int subframe;
  //int frame=0;
1209
  int read_samples, max_samples;
1210 1211
  
  //printf("UE_trx_read: UE %d, Type %s\n",UE_id,(PHY_vars_UE_g[UE_id][CC_id]->frame_parms.frame_type==FDD)?"FDD":"TDD");
1212
  openair0_timestamp last = last_UE_rx_timestamp[UE_id][CC_id];
1213 1214 1215

  *ptimestamp = last_UE_rx_timestamp[UE_id][CC_id];

1216
  int do_ofdm_mod = PHY_vars_UE_g[UE_id][CC_id]->do_ofdm_mod;
lfarizav's avatar
lfarizav committed
1217

1218
  LOG_D(EMU,"UE_trx_read nsamps %d TS(%llu,%llu) antenna %d\n",nsamps,
1219
        (unsigned long long)current_UE_rx_timestamp[UE_id][CC_id],
1220 1221
        (unsigned long long)last_UE_rx_timestamp[UE_id][CC_id],
	cc);
1222

1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
  while (nsamps) {
    /* wait for all processing to be finished */
    while (1) {
      PHY_VARS_UE *UE = PHY_vars_UE_g[UE_id][0];
      int ready = 1;
      int i;
      for (i = 0; i < 2; i++)
        if (UE->proc.proc_rxtx[i].instance_cnt_rxtx >= 0) ready = 0;
      if (UE->proc.instance_cnt_synch >= 0) ready = 0;
      if (ready) break;
      usleep(500);
    }
    while (current_UE_rx_timestamp[UE_id][CC_id] == last) {
1236
      //LOG_D(EMU,"UE_trx_read : current TS %"PRId64", last TS %"PRId64", sleeping\n",current_UE_rx_timestamp[UE_id][CC_id],last_UE_rx_timestamp[UE_id][CC_id]);
1237

1238 1239
      usleep(500);
    }
1240

1241
    //    LOG_D(EMU,"UE_trx_read : current TS %d, last TS %d, sleeping\n",current_UE_rx_timestamp[UE_id][CC_id],last_UE_rx_timestamp[UE_id][CC_id]);
1242
      
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
    read_samples = nsamps;
    max_samples = current_UE_rx_timestamp[UE_id][CC_id]-last;
    if (read_samples > max_samples)
      read_samples = max_samples;

    last += read_samples;
    nsamps -= read_samples;

    if (current_UE_rx_timestamp[UE_id][CC_id] == last) {
      // we have one subframe here so generate the received signal
lfarizav's avatar
lfarizav committed
1253 1254 1255
	if (do_ofdm_mod)
	{
      		subframe = (last/(PHY_vars_UE_g[UE_id][CC_id]->frame_parms.ofdm_symbol_size*PHY_vars_UE_g[UE_id][CC_id]->frame_parms.symbols_per_tti))%10;
1256
		//frame = (last/(10*PHY_vars_UE_g[UE_id][CC_id]->frame_parms.ofdm_symbol_size*PHY_vars_UE_g[UE_id][CC_id]->frame_parms.symbols_per_tti))%1023;
lfarizav's avatar
lfarizav committed
1257 1258 1259 1260
	}
	else
	{
		subframe = (last/PHY_vars_UE_g[UE_id][CC_id]->frame_parms.samples_per_tti)%10;
1261
		//frame = (last/(10*PHY_vars_UE_g[UE_id][CC_id]->frame_parms.samples_per_tti))%1023;
lfarizav's avatar
lfarizav committed
1262
	}
lfarizav's avatar
lfarizav committed
1263
	//printf("[oaisim_functs]UE_trx_read DL subframe %d, frame %d\n",subframe,frame);
1264 1265 1266 1267 1268
      //subframe = (subframe+9) % 10;

      LOG_D(PHY,"UE_trx_read generating DL subframe %d (Ts %llu, current TS %llu)\n",
            subframe,(unsigned long long)*ptimestamp,
            (unsigned long long)current_UE_rx_timestamp[UE_id][CC_id]);
1269

lfarizav's avatar
lfarizav committed
1270
      if (do_ofdm_mod)
1271
      {
1272
	start_meas(&eNB2UE[0][UE_id][CC_id]->DL_channel_freq);
1273
        //clock_t start=clock();
1274
      	  do_DL_sig_freq(eNB2UE,
lfarizav's avatar
lfarizav committed
1275 1276 1277 1278 1279 1280 1281
                enb_data,
                ue_data,
                subframe,
                0, //abstraction_flag,
                &PHY_vars_UE_g[UE_id][CC_id]->frame_parms,
                UE_id,
                CC_id);
1282
        stop_meas(&eNB2UE[0][UE_id][CC_id]->DL_channel_freq);
1283 1284 1285 1286
        /*clock_t stop=clock();
  	printf("do_DL_sig time is %f s, AVERAGE time is %f s, count %d, sum %e\n",(float) (stop-start)/CLOCKS_PER_SEC,(float) (sum+stop-start)/(count_d*CLOCKS_PER_SEC),count_d,sum_d+stop-start);
  	sum_d=(sum_d+stop-start);
        count_d++;*/
1287 1288
	//stop_meas(&dl_chan_stats_f);
	//print_meas(&dl_chan_stats_f,"DL_Channel Stats Frequency Domain",&dl_chan_stats_f,&dl_chan_stats_f);
1289
      }
lfarizav's avatar
lfarizav committed
1290
      else
1291
      {
1292
      //clock_t start=clock();
1293
      start_meas(&eNB2UE[0][UE_id][CC_id]->DL_channel);
1294 1295 1296 1297 1298 1299 1300 1301
      do_DL_sig(eNB2UE,
                enb_data,
                ue_data,
                subframe,
                0, //abstraction_flag,
                &PHY_vars_UE_g[UE_id][CC_id]->frame_parms,
                UE_id,
                CC_id);
1302
      stop_meas(&eNB2UE[0][UE_id][CC_id]->DL_channel);
1303 1304 1305 1306
      /*clock_t stop=clock();
      printf("do_DL_sig time is %f s, AVERAGE time is %f s, count %d, sum %e\n",(float) (stop-start)/CLOCKS_PER_SEC,(float) (sum+stop-start)/(count*CLOCKS_PER_SEC),count,sum+stop-start);
      sum=(sum+stop-start);
      count++;*/
1307
      }
1308 1309 1310

      last_UE_rx_timestamp[UE_id][CC_id] = last;
    }
1311
  }
1312

1313
  last_UE_rx_timestamp[UE_id][CC_id] = last;
1314

1315
  return ret;
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325
}

int eNB_trx_write(openair0_device *device,openair0_timestamp timestamp, void **buff, int nsamps, int cc, int flags) {

  return(nsamps);
}

int UE_trx_write(openair0_device *device,openair0_timestamp timestamp, void **buff, int nsamps, int cc, int flags) {

  return(nsamps);
1326 1327
}

1328 1329 1330 1331 1332 1333 1334 1335
void init_openair0(void);

openair0_config_t openair0_cfg[MAX_CARDS];

void init_openair0() {

  int card;
  int i;
1336
  printf("init_openair0 MAX_CARDS %d\n",MAX_CARDS);
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406
  for (card=0; card<MAX_CARDS; card++) {

    openair0_cfg[card].configFilename = NULL;

    if(frame_parms[0]->N_RB_DL == 100) {
      if (frame_parms[0]->threequarter_fs) {
	openair0_cfg[card].sample_rate=23.04e6;
	openair0_cfg[card].samples_per_frame = 230400; 
	openair0_cfg[card].tx_bw = 10e6;
	openair0_cfg[card].rx_bw = 10e6;
      }
      else {
	openair0_cfg[card].sample_rate=30.72e6;
	openair0_cfg[card].samples_per_frame = 307200; 
	openair0_cfg[card].tx_bw = 10e6;
	openair0_cfg[card].rx_bw = 10e6;
      }
    } else if(frame_parms[0]->N_RB_DL == 50) {
      openair0_cfg[card].sample_rate=15.36e6;
      openair0_cfg[card].samples_per_frame = 153600;
      openair0_cfg[card].tx_bw = 5e6;
      openair0_cfg[card].rx_bw = 5e6;
    } else if (frame_parms[0]->N_RB_DL == 25) {
      openair0_cfg[card].sample_rate=7.68e6;
      openair0_cfg[card].samples_per_frame = 76800;
      openair0_cfg[card].tx_bw = 2.5e6;
      openair0_cfg[card].rx_bw = 2.5e6;
    } else if (frame_parms[0]->N_RB_DL == 6) {
      openair0_cfg[card].sample_rate=1.92e6;
      openair0_cfg[card].samples_per_frame = 19200;
      openair0_cfg[card].tx_bw = 1.5e6;
      openair0_cfg[card].rx_bw = 1.5e6;
    }

    if (frame_parms[0]->frame_type==TDD)
      openair0_cfg[card].duplex_mode = duplex_mode_TDD;
    else //FDD
      openair0_cfg[card].duplex_mode = duplex_mode_FDD;

    
    openair0_cfg[card].remote_addr    = (eth_params+card)->remote_addr;
    openair0_cfg[card].remote_port    = (eth_params+card)->remote_port;
    openair0_cfg[card].my_addr        = (eth_params+card)->my_addr;
    openair0_cfg[card].my_port        = (eth_params+card)->my_port;    
     
    
    printf("HW: Configuring card %d, nb_antennas_tx/rx %d/%d\n",card,
           PHY_vars_eNB_g[0][0]->frame_parms.nb_antennas_tx,
           PHY_vars_eNB_g[0][0]->frame_parms.nb_antennas_rx);
    openair0_cfg[card].Mod_id = 0;



    openair0_cfg[card].num_rb_dl=frame_parms[0]->N_RB_DL;
    openair0_cfg[card].tx_num_channels=min(2,PHY_vars_eNB_g[0][0]->frame_parms.nb_antennas_tx);
    openair0_cfg[card].rx_num_channels=min(2,PHY_vars_eNB_g[0][0]->frame_parms.nb_antennas_rx);

    for (i=0; i<4; i++) {

      openair0_cfg[card].rx_gain[i] = PHY_vars_eNB_g[0][0]->rx_total_gain_dB;
      
      printf("Card %d, channel %d, Setting tx_gain %f, rx_gain %f, tx_freq %f, rx_freq %f\n",
             card,i, openair0_cfg[card].tx_gain[i],
             openair0_cfg[card].rx_gain[i],
             openair0_cfg[card].tx_freq[i],
             openair0_cfg[card].rx_freq[i]);
    }
  }
}

1407 1408
void init_devices(void){

1409 1410
  module_id_t UE_id;
  uint8_t  eNB_id;
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423
  uint8_t CC_id;

  for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
    for (eNB_id=0;eNB_id<NB_eNB_INST;eNB_id++) {
      PHY_vars_eNB_g[eNB_id][CC_id]->rfdevice.Mod_id             = eNB_id;
      PHY_vars_eNB_g[eNB_id][CC_id]->rfdevice.CC_id              = CC_id;
      PHY_vars_eNB_g[eNB_id][CC_id]->rfdevice.trx_start_func     = eNB_trx_start;
      PHY_vars_eNB_g[eNB_id][CC_id]->rfdevice.trx_read_func      = eNB_trx_read;
      PHY_vars_eNB_g[eNB_id][CC_id]->rfdevice.trx_write_func     = eNB_trx_write;
      PHY_vars_eNB_g[eNB_id][CC_id]->rfdevice.trx_end_func       = eNB_trx_end;
      PHY_vars_eNB_g[eNB_id][CC_id]->rfdevice.trx_stop_func      = eNB_trx_stop;
      PHY_vars_eNB_g[eNB_id][CC_id]->rfdevice.trx_set_freq_func  = eNB_trx_set_freq;
      PHY_vars_eNB_g[eNB_id][CC_id]->rfdevice.trx_set_gains_func = eNB_trx_set_gains;
1424
      if (PHY_vars_UE_g[0][CC_id]->do_ofdm_mod)
lfarizav's avatar
lfarizav committed
1425 1426
      	current_eNB_rx_timestamp[eNB_id][CC_id] = PHY_vars_eNB_g[eNB_id][CC_id]->frame_parms.ofdm_symbol_size*PHY_vars_eNB_g[eNB_id][CC_id]->frame_parms.symbols_per_tti;
      else
1427
      	current_eNB_rx_timestamp[eNB_id][CC_id] = PHY_vars_eNB_g[eNB_id][CC_id]->frame_parms.samples_per_tti;
1428
      last_eNB_rx_timestamp[eNB_id][CC_id] = 0;
1429
    }
1430 1431 1432 1433 1434 1435

    for (eNB_id=0;eNB_id<NB_eNB_INST;eNB_id++) {
      PHY_vars_eNB_g[eNB_id][CC_id]->ifdevice.Mod_id             = eNB_id;
      PHY_vars_eNB_g[eNB_id][CC_id]->ifdevice.CC_id              = CC_id;
    }

1436
    for (UE_id=0;UE_id<NB_UE_INST;UE_id++) {
1437 1438 1439 1440 1441 1442 1443 1444 1445
      PHY_vars_UE_g[UE_id][CC_id]->rfdevice.Mod_id               = UE_id;
      PHY_vars_UE_g[UE_id][CC_id]->rfdevice.CC_id                = CC_id;
      PHY_vars_UE_g[UE_id][CC_id]->rfdevice.trx_start_func       = UE_trx_start;
      PHY_vars_UE_g[UE_id][CC_id]->rfdevice.trx_read_func        = UE_trx_read;
      PHY_vars_UE_g[UE_id][CC_id]->rfdevice.trx_write_func       = UE_trx_write;
      PHY_vars_UE_g[UE_id][CC_id]->rfdevice.trx_end_func         = UE_trx_end;
      PHY_vars_UE_g[UE_id][CC_id]->rfdevice.trx_stop_func        = UE_trx_stop;
      PHY_vars_UE_g[UE_id][CC_id]->rfdevice.trx_set_freq_func    = UE_trx_set_freq;
      PHY_vars_UE_g[UE_id][CC_id]->rfdevice.trx_set_gains_func   = UE_trx_set_gains;
1446
      if (PHY_vars_UE_g[UE_id][CC_id]->do_ofdm_mod)
lfarizav's avatar
lfarizav committed
1447 1448 1449
      	current_UE_rx_timestamp[UE_id][CC_id] = PHY_vars_UE_g[UE_id][CC_id]->frame_parms.ofdm_symbol_size*PHY_vars_UE_g[UE_id][CC_id]->frame_parms.symbols_per_tti;
      else
	current_UE_rx_timestamp[UE_id][CC_id] = PHY_vars_UE_g[UE_id][CC_id]->frame_parms.samples_per_tti;
1450 1451
      last_UE_rx_timestamp[UE_id][CC_id] = 0;

1452 1453 1454 1455
    }
  }
}

1456 1457
void init_openair1(void)
{
1458 1459
  module_id_t UE_id;
  uint8_t  eNB_id = 0;
1460 1461
  //uint8_t  RN_id = 0;
  uint8_t CC_id = 0;
1462
#if ENABLE_RAL
1463 1464
  int list_index;
#endif
1465

1466
  // change the nb_connected_eNB
1467
  /*mac_xface = malloc(sizeof(MAC_xface));
1468 1469 1470 1471 1472
  memset(mac_xface, 0, sizeof(MAC_xface));
  PHY_vars_eNB_g = (PHY_VARS_eNB***)malloc(NB_eNB_INST*sizeof(PHY_VARS_eNB**));
    for (eNB_id=0; eNB_id<NB_eNB_INST; eNB_id++) {
      PHY_vars_eNB_g[eNB_id] = (PHY_VARS_eNB**) malloc(MAX_NUM_CCs*sizeof(PHY_VARS_eNB*));
      for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
1473
        LOG_I(PHY,"init lte parms: Nid_cell %d, Frame type %s, N_RB_DL %d, nushift %d\n",enb_properties->properties[eNB_id]->Nid_cell[CC_id],(oai_emulation.info.frame_type[CC_id]== FDD)?"FDD":"TDD",oai_emulation.info.N_RB_DL[CC_id],((enb_properties->properties[eNB_id]->Nid_cell[CC_id])%6));
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
    	frame_parms[CC_id] = calloc(1, sizeof(LTE_DL_FRAME_PARMS));
    	(frame_parms[CC_id])->frame_type         = oai_emulation.info.frame_type[CC_id];
    	(frame_parms[CC_id])->tdd_config         = oai_emulation.info.tdd_config_S[CC_id];
    	(frame_parms[CC_id])->tdd_config_S       = oai_emulation.info.tdd_config_S[CC_id];
    	(frame_parms[CC_id])->N_RB_DL            = oai_emulation.info.N_RB_DL[CC_id];
   	(frame_parms[CC_id])->N_RB_UL            = (frame_parms[CC_id])->N_RB_DL;
    	(frame_parms[CC_id])->phich_config_common.phich_resource = oneSixth;
    	(frame_parms[CC_id])->phich_config_common.phich_duration = normal;
    	(frame_parms[CC_id])->Ncp                = oai_emulation.info.extended_prefix_flag[CC_id];
    	(frame_parms[CC_id])->Ncp_UL             = oai_emulation.info.extended_prefix_flag[CC_id]; 
    	(frame_parms[CC_id])->Nid_cell           = enb_properties->properties[eNB_id]->Nid_cell[CC_id];
    	(frame_parms[CC_id])->nushift            = ((enb_properties->properties[eNB_id]->Nid_cell[CC_id])%6);
    	(frame_parms[CC_id])->nb_antennas_tx     = enb_properties->properties[eNB_id]->nb_antennas_tx[CC_id];
    	(frame_parms[CC_id])->nb_antennas_rx     = enb_properties->properties[eNB_id]->nb_antennas_rx[CC_id];
    	(frame_parms[CC_id])->nb_antenna_ports_eNB = enb_properties->properties[eNB_id]->nb_antenna_ports[CC_id];
    	(frame_parms[CC_id])->mode1_flag           = (frame_parms[CC_id])->nb_antenna_ports_eNB==1 ? 1 : 0;

    	init_frame_parms(frame_parms[CC_id],1);

    	(frame_parms[CC_id])->pusch_config_common.ul_ReferenceSignalsPUSCH.cyclicShift = 0;//n_DMRS1 set to 0
    	(frame_parms[CC_id])->pusch_config_common.ul_ReferenceSignalsPUSCH.groupHoppingEnabled = 1;
    	(frame_parms[CC_id])->pusch_config_common.ul_ReferenceSignalsPUSCH.sequenceHoppingEnabled = 0;
    	(frame_parms[CC_id])->pusch_config_common.ul_ReferenceSignalsPUSCH.groupAssignmentPUSCH = 0;
    	init_ul_hopping(frame_parms[CC_id]);
        phy_init_lte_top(frame_parms[CC_id]);
        PHY_vars_eNB_g[eNB_id][CC_id] = init_lte_eNB(frame_parms[CC_id],eNB_id,enb_properties->properties[eNB_id]->Nid_cell[CC_id],eNodeB_3GPP,abstraction_flag);
        PHY_vars_eNB_g[eNB_id][CC_id]->Mod_id=eNB_id;
        PHY_vars_eNB_g[eNB_id][CC_id]->CC_id=CC_id;
      }
1503
  }
1504 1505 1506 1507 1508 1509
  PHY_vars_UE_g = (PHY_VARS_UE***)malloc(NB_UE_INST*sizeof(PHY_VARS_UE**));
  for (UE_id=0; UE_id<NB_UE_INST; UE_id++) {
    PHY_vars_UE_g[UE_id] = (PHY_VARS_UE**) malloc(MAX_NUM_CCs*sizeof(PHY_VARS_UE*));
    for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
      (frame_parms[CC_id])->nb_antennas_tx     = 1;
      (frame_parms[CC_id])->nb_antennas_rx     = nb_antennas_rx_ue;
1510
      (frame_parms[CC_id])->frame_type         = oai_emulation.info.frame_type[CC_id];
1511 1512 1513 1514 1515 1516 1517 1518 1519 1520
      PHY_vars_UE_g[UE_id][CC_id] = init_lte_UE(frame_parms[CC_id], UE_id,abstraction_flag);
      PHY_vars_UE_g[UE_id][CC_id]->Mod_id=UE_id;
      PHY_vars_UE_g[UE_id][CC_id]->CC_id=CC_id;
    }
  }
  if (NB_RN_INST > 0) {
    PHY_vars_RN_g = malloc(NB_RN_INST*sizeof(PHY_VARS_RN*));
    for (RN_id=0; RN_id<NB_RN_INST; RN_id++) {
      PHY_vars_RN_g[RN_id] = init_lte_RN(*frame_parms,RN_id,oai_emulation.info.eMBMS_active_state);
    }
1521 1522
  }*/
    init_lte_vars (&frame_parms[CC_id],
1523 1524
		   oai_emulation.info.frame_type[CC_id],
		   oai_emulation.info.tdd_config[CC_id],
1525 1526 1527
		   oai_emulation.info.tdd_config_S[CC_id],
		   oai_emulation.info.extended_prefix_flag[CC_id],
                   oai_emulation.info.N_RB_DL[CC_id], 
1528
		   enb_properties->properties[eNB_id]->Nid_cell[CC_id], 
1529
		   cooperation_flag, 
1530
		   enb_properties->properties[eNB_id]->nb_antenna_ports[CC_id], 
1531
		   abstraction_flag,
1532 1533
		   enb_properties->properties[eNB_id]->nb_antennas_rx[CC_id],
		   enb_properties->properties[eNB_id]->nb_antennas_tx[CC_id],
1534
		   nb_antennas_rx_ue,
1535
		   oai_emulation.info.eMBMS_active_state);
1536

1537
  for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
lfarizav's avatar
lfarizav committed
1538
   for (eNB_id=0;eNB_id<enb_properties->number;eNB_id++) {
1539
    // This is for IF4p5 RRU, gets done by RRC configuration of eNB
1540 1541
    PHY_vars_eNB_g[eNB_id][CC_id]->frame_parms.prach_config_common.prach_ConfigInfo.prach_ConfigIndex = enb_properties->properties[eNB_id]->prach_config_index[CC_id];
    PHY_vars_eNB_g[eNB_id][CC_id]->frame_parms.prach_config_common.prach_ConfigInfo.prach_FreqOffset  = enb_properties->properties[eNB_id]->prach_freq_offset[CC_id];
1542
   }
Raymond Knopp's avatar
 
Raymond Knopp committed
1543
  }
1544

1545 1546
  for (UE_id=0; UE_id<NB_UE_INST; UE_id++) {
    for (eNB_id=0; eNB_id<NB_eNB_INST; eNB_id++) {
1547 1548 1549 1550
      for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
        PHY_vars_eNB_g[eNB_id][CC_id]->pusch_config_dedicated[UE_id].betaOffset_ACK_Index = beta_ACK;
        PHY_vars_eNB_g[eNB_id][CC_id]->pusch_config_dedicated[UE_id].betaOffset_RI_Index  = beta_RI;
        PHY_vars_eNB_g[eNB_id][CC_id]->pusch_config_dedicated[UE_id].betaOffset_CQI_Index = beta_CQI;
1551
        PHY_vars_eNB_g[eNB_id][CC_id]->frame_parms.pdsch_config_common.p_b = (frame_parms[CC_id]->nb_antenna_ports_eNB>1) ? 1 : 0; // rho_A = rho_B
1552
      }
1553
    }
1554 1555 1556 1557
        PHY_vars_UE_g[UE_id][0]->pusch_config_dedicated[0].betaOffset_ACK_Index = beta_ACK;
        PHY_vars_UE_g[UE_id][0]->pusch_config_dedicated[0].betaOffset_RI_Index  = beta_RI;
        PHY_vars_UE_g[UE_id][0]->pusch_config_dedicated[0].betaOffset_CQI_Index = beta_CQI;
        PHY_vars_UE_g[UE_id][0]->frame_parms.pdsch_config_common.p_b = (frame_parms[0]->nb_antenna_ports_eNB>1) ? 1 : 0; // rho_A = rho_B
1558
  }
1559 1560
  for (eNB_id=0; eNB_id<NB_eNB_INST; eNB_id++)
  	printf ("AFTER init: MAX_NUM_CCs %d, Nid_cell %d frame_type %d,tdd_config %d\n",
1561
          MAX_NUM_CCs,
1562 1563 1564
          PHY_vars_eNB_g[eNB_id][0]->frame_parms.Nid_cell,
          PHY_vars_eNB_g[eNB_id][0]->frame_parms.frame_type,
          PHY_vars_eNB_g[eNB_id][0]->frame_parms.tdd_config);
1565 1566 1567

  number_of_cards = 1;

1568 1569 1570
//  openair_daq_vars.rx_rf_mode = 1;
//  openair_daq_vars.tdd = 1;
//  openair_daq_vars.rx_gain_mode = DAQ_AGC_ON;
1571

1572
//  openair_daq_vars.dlsch_transmission_mode = oai_emulation.info.transmission_mode[0];
Cedric Roux's avatar
Cedric Roux committed
1573
//#warning "NN->FK: OAI EMU channel abstraction does not work for MCS higher than"
1574 1575 1576 1577 1578
//  openair_daq_vars.target_ue_dl_mcs = cmin(target_dl_mcs,16);
//  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;
1579

1580
  //N_TA_offset
1581 1582
  for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
    for (UE_id=0; UE_id<NB_UE_INST; UE_id++) {
1583 1584 1585 1586 1587

      PHY_vars_UE_g[UE_id][CC_id]->use_ia_receiver      = 0;
      PHY_vars_UE_g[UE_id][CC_id]->mode                 = normal_txrx;
      PHY_vars_UE_g[UE_id][CC_id]->mac_enabled          = 1;
      PHY_vars_UE_g[UE_id][CC_id]->no_timing_correction = 1;
1588

1589 1590
      if (PHY_vars_UE_g[UE_id][CC_id]->frame_parms.frame_type == TDD) {
        if (PHY_vars_UE_g[UE_id][CC_id]->frame_parms.N_RB_DL == 100)
1591
          PHY_vars_UE_g[UE_id][CC_id]->N_TA_offset = 624;
1592
        else if (PHY_vars_UE_g[UE_id][CC_id]->frame_parms.N_RB_DL == 50)
1593
          PHY_vars_UE_g[UE_id][CC_id]->N_TA_offset = 624/2;
1594
        else if (PHY_vars_UE_g[UE_id][CC_id]->frame_parms.N_RB_DL == 25)
1595 1596 1597
          PHY_vars_UE_g[UE_id][CC_id]->N_TA_offset = 624/4;
      } else {
        PHY_vars_UE_g[UE_id][CC_id]->N_TA_offset = 0;
1598 1599
      }
    }
1600 1601

    for (eNB_id=0; eNB_id<NB_eNB_INST; eNB_id++) {
1602 1603
      if (PHY_vars_eNB_g[eNB_id][CC_id]->frame_parms.frame_type == TDD) {
        if (PHY_vars_eNB_g[eNB_id][CC_id]->frame_parms.N_RB_DL == 100)
1604
          PHY_vars_eNB_g[eNB_id][CC_id]->N_TA_offset = 624;
1605
        else if (PHY_vars_eNB_g[eNB_id][CC_id]->frame_parms.N_RB_DL == 50)
1606
          PHY_vars_eNB_g[eNB_id][CC_id]->N_TA_offset = 624/2;
1607
        else if (PHY_vars_eNB_g[eNB_id][CC_id]->frame_parms.N_RB_DL == 25)
1608 1609 1610
          PHY_vars_eNB_g[eNB_id][CC_id]->N_TA_offset = 624/4;
      } else {
        PHY_vars_eNB_g[eNB_id][CC_id]->N_TA_offset = 0;
1611
      }
Raymond Knopp's avatar
Raymond Knopp committed
1612 1613
    } // eNB_id
  } // CC_id
1614

Raymond Knopp's avatar
Raymond Knopp committed
1615
  for (eNB_id=0; eNB_id<NB_eNB_INST; eNB_id++) {
Florian Kaltenberger's avatar
Florian Kaltenberger committed
1616 1617 1618 1619 1620 1621
    for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
      if (phy_test==1)
	PHY_vars_eNB_g[eNB_id][CC_id]->mac_enabled=0;
      else
	PHY_vars_eNB_g[eNB_id][CC_id]->mac_enabled=1;
    }
Raymond Knopp's avatar
Raymond Knopp committed
1622 1623
  }

1624 1625
  init_devices ();

1626
  init_eNB(oai_emulation.info.node_function,oai_emulation.info.node_timing,NB_eNB_INST,eth_params,1,0);
Florian Kaltenberger's avatar
Florian Kaltenberger committed
1627

1628
  // init_ue_status();
Raymond Knopp's avatar
Raymond Knopp committed
1629
  for (UE_id=0; UE_id<NB_UE_INST; UE_id++) {
1630
    for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
1631

hbilel's avatar
hbilel committed
1632
      PHY_vars_UE_g[UE_id][CC_id]->tx_power_max_dBm=10;
1633
      
1634
      PHY_vars_UE_g[UE_id][CC_id]->rx_total_gain_dB=100;
1635

1636
      // update UE_mode for each eNB_id not just 0
1637
      //for (eNB_id=0; eNB_id<NB_eNB_INST; eNB_id++){
lfarizav's avatar
lfarizav committed
1638
	      if (abstraction_flag == 0) {
1639 1640
		if (phy_test==0) PHY_vars_UE_g[UE_id][CC_id]->UE_mode[0] = NOT_SYNCHED;
		else PHY_vars_UE_g[UE_id][CC_id]->UE_mode[0] = PUSCH;
lfarizav's avatar
lfarizav committed
1641 1642
	      } else {
		// 0 is the index of the connected eNB
1643
		PHY_vars_UE_g[UE_id][CC_id]->UE_mode[0] = PRACH;
lfarizav's avatar
lfarizav committed
1644
	      }
1645
      //}
Florian Kaltenberger's avatar
Florian Kaltenberger committed
1646 1647 1648 1649
      if (phy_test==1)
	PHY_vars_UE_g[UE_id][CC_id]->mac_enabled=0;
      else
	PHY_vars_UE_g[UE_id][CC_id]->mac_enabled=1;
1650 1651
      //for (eNB_id=0; eNB_id<NB_eNB_INST; eNB_id++){
      	PHY_vars_UE_g[UE_id][CC_id]->pdcch_vars[0][0]->crnti = 0x1235 + UE_id;
Florian Kaltenberger's avatar
Florian Kaltenberger committed
1652

lfarizav's avatar
lfarizav committed
1653
      	for (uint8_t i=0; i<RX_NB_TH_MAX; i++) {
1654 1655
          PHY_vars_UE_g[UE_id][CC_id]->pdcch_vars[i][0]->dciFormat      = 0;
          PHY_vars_UE_g[UE_id][CC_id]->pdcch_vars[i][0]->agregationLevel      = 0xFF;
lfarizav's avatar
lfarizav committed
1656
      	}
1657
      	PHY_vars_UE_g[UE_id][CC_id]->current_dlsch_cqi[0] = 10;
1658

1659 1660
      	LOG_I(EMU, "eNB_id %d, UE %d, CC %d, mode is initialized to %d\n",PHY_vars_UE_g[UE_id][CC_id]->common_vars.eNb_id, UE_id, CC_id, PHY_vars_UE_g[UE_id][CC_id]->UE_mode[0] );
      //}
1661
#if ENABLE_RAL
Raymond Knopp's avatar
 
Raymond Knopp committed
1662
      PHY_vars_UE_g[UE_id][CC_id]->ral_thresholds_timed = hashtable_create (64, NULL, NULL);
1663

1664
      for (list_index = 0; list_index < RAL_LINK_PARAM_GEN_MAX; list_index++) {
1665
        SLIST_INIT(&PHY_vars_UE_g[UE_id][CC_id]->ral_thresholds_gen_polled[list_index]);
1666
      }
1667

1668
      for (list_index = 0; list_index < RAL_LINK_PARAM_LTE_MAX; list_index++) {
1669
        SLIST_INIT(&PHY_vars_UE_g[UE_id][CC_id]->ral_thresholds_lte_polled[list_index]);
1670
      }
1671

1672 1673
#endif

Raymond Knopp's avatar
Raymond Knopp committed
1674 1675
    } // CC_id
  } // UE_id
Cedric Roux's avatar
Cedric Roux committed
1676
  extern void init_UE(int);
Raymond Knopp's avatar
Raymond Knopp committed
1677
  init_UE(NB_UE_INST);
1678
    }
1679

1680 1681
void init_openair2(void)
{
1682
#ifdef OPENAIR2
Raymond Knopp's avatar
 
Raymond Knopp committed
1683
  int CC_id;
1684
  //int eNB_id;
Cedric Roux's avatar
Cedric Roux committed
1685
//#warning "eNB index is hard coded to zero"
1686 1687
 //for (eNB_id=0; 1/*eNB_id<NB_eNB_INST*/; eNB_id++)
 //{
1688
  for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++)
1689
    l2_init (&PHY_vars_eNB_g[0][CC_id]->frame_parms,
1690 1691 1692 1693
             oai_emulation.info.eMBMS_active_state,
             NULL,
             oai_emulation.info.cba_group_active,
             oai_emulation.info.handover_active);
1694
 //}
1695
  mac_xface->macphy_exit = exit_fun;
1696

1697 1698 1699
#endif
}

1700 1701
void init_ocm(void)
{
1702 1703
  module_id_t UE_id=0, eNB_id=0;
  int CC_id=0;
1704

1705
  /* Added for PHY abstraction */
1706 1707

  char* frame_type = "unknown";
1708

1709
  int do_ofdm_mod = PHY_vars_UE_g[UE_id][CC_id]->do_ofdm_mod;
lfarizav's avatar
lfarizav committed
1710
  int nb_rb, n_samples;
1711
  nb_rb=PHY_vars_UE_g[UE_id][CC_id]->frame_parms.N_RB_DL;
lfarizav's avatar
lfarizav committed
1712 1713
  n_samples=nb_rb*12+1;

1714 1715 1716 1717
  switch (oai_emulation.info.frame_type[0]) {
  case FDD:
    frame_type = "FDD";
    break;
1718

1719
  case TDD:
gabrielC's avatar
gabrielC committed
1720
    frame_type = "TDD";
1721 1722
    break;
  }
1723

1724
  LOG_I(OCM,"Running with frame_type %d (%s), 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",
1725 1726
        oai_emulation.info.frame_type[0], frame_type, Nid_cell, oai_emulation.info.N_RB_DL[0], oai_emulation.info.extended_prefix_flag[0], oai_emulation.info.transmission_mode[0],target_dl_mcs,
        rate_adaptation_flag,oai_emulation.info.n_frames,abstraction_flag,oai_emulation.environment_system_config.fading.small_scale.selected_option);
1727

1728
  if (abstraction_flag) {
1729

1730
    get_beta_map();
1731
#ifdef PHY_ABSTRACTION_UL
1732
    get_beta_map_up();
1733
#endif
1734
    get_MIESM_param();
1735

1736
    //load_pbch_desc();
1737
  }
1738

1739 1740

  for (eNB_id = 0; eNB_id < NB_eNB_INST; eNB_id++) {
1741 1742
    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);
1743 1744 1745
  }

  for (UE_id = 0; UE_id < NB_UE_INST; UE_id++) {
1746 1747
    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);
1748 1749 1750 1751 1752 1753
  }

  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)) {

1754 1755 1756 1757
    // 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);
1758

1759 1760
    // size of area to generate shadow fading map
    LOG_D(EMU,"Simulation area x=%f, y=%f\n",
1761 1762 1763 1764
          oai_emulation.topology_config.area.x_m,
          oai_emulation.topology_config.area.y_m);
  }

1765
  if (abstraction_flag == 0){
lfarizav's avatar
lfarizav committed
1766 1767 1768 1769 1770
	if (do_ofdm_mod)
		init_channel_vars_freq (frame_parms[0], &s_re_f, &s_im_f, &r_re_f, &r_im_f, &r_re0_f, &r_im0_f);
    		
	else
		init_channel_vars (frame_parms[0], &s_re, &s_im, &r_re, &r_im, &r_re0, &r_im0);
1771
  }
1772 1773 1774

  // initialize channel descriptors
  for (eNB_id = 0; eNB_id < NB_eNB_INST; eNB_id++) {
Raymond Knopp's avatar
 
Raymond Knopp committed
1775
    for (UE_id = 0; UE_id < NB_UE_INST; UE_id++) {
1776 1777
      for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
        LOG_D(OCM,"Initializing channel (%s, %d) from eNB %d to UE %d\n", oai_emulation.environment_system_config.fading.small_scale.selected_option,
1778
              map_str_to_int(small_scale_names,oai_emulation.environment_system_config.fading.small_scale.selected_option), eNB_id, UE_id);
1779

1780

1781
        eNB2UE[eNB_id][UE_id][CC_id] =
1782 1783
	  new_channel_desc_scm(PHY_vars_eNB_g[eNB_id][CC_id]->frame_parms.nb_antennas_tx,
			       PHY_vars_UE_g[UE_id][CC_id]->frame_parms.nb_antennas_rx,
Florian Kaltenberger's avatar
Florian Kaltenberger committed
1784
			       map_str_to_int(small_scale_names,oai_emulation.environment_system_config.fading.small_scale.selected_option),
1785 1786
			       N_RB2sampling_rate(PHY_vars_eNB_g[eNB_id][CC_id]->frame_parms.N_RB_DL),
			       N_RB2channel_bandwidth(PHY_vars_eNB_g[eNB_id][CC_id]->frame_parms.N_RB_DL),
Florian Kaltenberger's avatar
Florian Kaltenberger committed
1787 1788 1789
			       forgetting_factor,
			       0,
			       0);
1790 1791
	/*if (do_ofdm_mod)
	{*/
1792 1793
		random_channel_freq(eNB2UE[eNB_id][UE_id][CC_id],abstraction_flag);//Find a(l)
		freq_channel_SSE_float(eNB2UE[eNB_id][UE_id][CC_id],nb_rb,n_samples);//Find desc->chF
1794
	/*}
lfarizav's avatar
lfarizav committed
1795 1796

	else
1797
        	random_channel(eNB2UE[eNB_id][UE_id][CC_id],abstraction_flag);*/
lfarizav's avatar
lfarizav committed
1798

1799 1800
        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);
1801

1802
        UE2eNB[UE_id][eNB_id][CC_id] =
1803 1804
	  new_channel_desc_scm(PHY_vars_UE_g[UE_id][CC_id]->frame_parms.nb_antennas_tx,
			       PHY_vars_eNB_g[eNB_id][CC_id]->frame_parms.nb_antennas_rx,
Florian Kaltenberger's avatar
Florian Kaltenberger committed
1805
			       map_str_to_int(small_scale_names, oai_emulation.environment_system_config.fading.small_scale.selected_option),
1806 1807
			       N_RB2sampling_rate(PHY_vars_eNB_g[eNB_id][CC_id]->frame_parms.N_RB_UL),
			       N_RB2channel_bandwidth(PHY_vars_eNB_g[eNB_id][CC_id]->frame_parms.N_RB_UL),
Florian Kaltenberger's avatar
Florian Kaltenberger committed
1808 1809 1810
			       forgetting_factor,
			       0,
			       0);
1811 1812
	/*if (do_ofdm_mod)
	{*/
1813 1814
		random_channel_freq(UE2eNB[UE_id][eNB_id][CC_id],abstraction_flag);//Find a(l)
		freq_channel_SSE_float(UE2eNB[UE_id][eNB_id][CC_id],nb_rb,n_samples);//Find desc->chF
1815
	/*}
lfarizav's avatar
lfarizav committed
1816
	else
1817
        	random_channel(UE2eNB[UE_id][eNB_id][CC_id],abstraction_flag);*/
1818 1819 1820

        // 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];
Raymond Knopp's avatar
 
Raymond Knopp committed
1821
      }
1822 1823
    }
  }
1824 1825
}

1826 1827
void init_otg_pdcp_buffer(void)
{
Lionel Gauthier's avatar
Lionel Gauthier committed
1828
  module_id_t i;
1829
  otg_pdcp_buffer = malloc((NB_UE_INST + NB_eNB_INST) * sizeof(Packet_OTG_List_t));
1830 1831

  for (i = 0; i < NB_UE_INST + NB_eNB_INST; i++) {
1832 1833
    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])));
1834 1835 1836
  }
}

1837 1838
void update_omg (frame_t frameP)
{
Lionel Gauthier's avatar
Lionel Gauthier committed
1839
  module_id_t UE_id, eNB_id;
1840 1841
  int new_omg_model;

1842
  if ((frameP % omg_period) == 0 ) { // call OMG every 10ms
1843 1844 1845 1846 1847 1848 1849 1850 1851 1852
    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);
1853
      }
1854
    }
1855

1856 1857 1858 1859 1860 1861
    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);
1862
      }
1863
    }
1864 1865 1866
  }
}

1867 1868 1869 1870 1871
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);
1872

1873
}
1874
static int firt_cell_association=1;
1875 1876
void update_ocm()
{
Lionel Gauthier's avatar
Lionel Gauthier committed
1877
  module_id_t UE_id, eNB_id;
Raymond Knopp's avatar
 
Raymond Knopp committed
1878
  int CC_id;
1879
  double pathloss[NB_UE_INST];
Raymond Knopp's avatar
 
Raymond Knopp committed
1880

1881
  for (eNB_id = 0; eNB_id < NB_eNB_INST; eNB_id++){
1882
    enb_data[eNB_id]->tx_power_dBm = PHY_vars_eNB_g[eNB_id][0]->frame_parms.pdsch_config_common.referenceSignalPower;
1883 1884
    pathloss[eNB_id]=-200.0;
  }
1885

1886
  for (UE_id = 0; UE_id < NB_UE_INST; UE_id++)
1887
    ue_data[UE_id]->tx_power_dBm = PHY_vars_UE_g[UE_id][0]->tx_power_dBm[0];
1888

1889 1890 1891

  /* check if the openair channel model is activated used for PHY abstraction : path loss*/
  if ((oai_emulation.info.ocm_enabled == 1)&& (ethernet_flag == 0 )) {
1892
    //LOG_D(OMG," extracting position of eNb...\n");
1893 1894
    //display_node_list(enb_node_list);
    //  display_node_list(ue_node_list);
1895 1896 1897 1898
    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)
Raymond Knopp's avatar
 
Raymond Knopp committed
1899
    extract_position(ue_node_list, ue_data, NB_UE_INST);
1900

Raymond Knopp's avatar
 
Raymond Knopp committed
1901 1902 1903
    /* if (frame % 50 == 0)
       LOG_N(OCM,"Path loss for TTI %d : \n", frame);
    */
1904 1905
   if (firt_cell_association){
    firt_cell_association=0;//TODO This static variable is just for static users. If it is necessary to consider mobile UEs, this hack should be reconsidered, or consider a mutex solution for this for
1906
    for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
1907 1908
      for (UE_id = 0; UE_id < NB_UE_INST; UE_id++) {
        for (eNB_id = 0; eNB_id < NB_eNB_INST; eNB_id++) {
1909 1910 1911 1912
          calc_path_loss (enb_data[eNB_id], ue_data[UE_id], eNB2UE[eNB_id][UE_id][CC_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][CC_id]->path_loss_dB = eNB2UE[eNB_id][UE_id][CC_id]->path_loss_dB;
          //    if (frame % 50 == 0)
hbilel's avatar
hbilel committed
1913
          LOG_D(OCM,"Path loss (CCid %d) between eNB %d at (%f,%f) and UE %d at (%f,%f) is %f, angle %f\n",
1914 1915
                CC_id,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][CC_id]->path_loss_dB, eNB2UE[eNB_id][UE_id][CC_id]->aoa);
1916 1917 1918 1919
	  if (eNB2UE[eNB_id][UE_id][CC_id]->path_loss_dB>pathloss[UE_id]){
		pathloss[UE_id] = eNB2UE[eNB_id][UE_id][CC_id]->path_loss_dB;
		PHY_vars_UE_g[UE_id][CC_id]->common_vars.eNb_id = eNB_id;
	  }
Cedric Roux's avatar
Cedric Roux committed
1920 1921 1922 1923
          //double dx, dy, distance;
          //dx = enb_data[eNB_id]->x - ue_data[UE_id]->x;
          //dy = enb_data[eNB_id]->y - ue_data[UE_id]->y;
          //distance = sqrt(dx * dx + dy * dy);
1924
          /*LOG_D(LOCALIZE, " OCM distance between eNB %d at (%f,%f) and UE %d at (%f,%f) is %f \n",
1925 1926 1927
                  eNB_id, enb_data[eNB_id]->x,enb_data[eNB_id]->y,
                  UE_id, ue_data[UE_id]->x,ue_data[UE_id]->y,
                  distance);*/
1928
        }
1929
      printf("UE %d is associated to eNB %d based on pathloss (%s domain)\n",UE_id,PHY_vars_UE_g[UE_id][CC_id]->common_vars.eNb_id,(PHY_vars_UE_g[UE_id][CC_id]->do_ofdm_mod)?"Frequency":"Time");
1930
      }
Raymond Knopp's avatar
 
Raymond Knopp committed
1931
    }
1932
   }
1933
  }
1934 1935

  else {
1936
    for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
1937
      for (eNB_id = 0; eNB_id < NB_eNB_INST; eNB_id++) {
1938 1939 1940 1941
        for (UE_id = 0; UE_id < NB_UE_INST; UE_id++) {

          //pathloss: -132.24 dBm/15kHz RE + target SNR - eNB TX power per RE
          if (eNB_id == (UE_id % NB_eNB_INST)) {
1942
            eNB2UE[eNB_id][UE_id][CC_id]->path_loss_dB = -132.24 + snr_dB - PHY_vars_eNB_g[eNB_id][CC_id]->frame_parms.pdsch_config_common.referenceSignalPower;
1943
            UE2eNB[UE_id][eNB_id][CC_id]->path_loss_dB = -132.24 + snr_dB - PHY_vars_eNB_g[eNB_id][CC_id]->frame_parms.pdsch_config_common.referenceSignalPower;
1944
          } else {
1945 1946
            eNB2UE[eNB_id][UE_id][CC_id]->path_loss_dB = -132.24 + sinr_dB - PHY_vars_eNB_g[eNB_id][CC_id]->frame_parms.pdsch_config_common.referenceSignalPower;
            UE2eNB[UE_id][eNB_id][CC_id]->path_loss_dB = -132.24 + sinr_dB - PHY_vars_eNB_g[eNB_id][CC_id]->frame_parms.pdsch_config_common.referenceSignalPower;
1947 1948
          }

1949
          LOG_D(OCM,"Path loss from eNB %d to UE %d (CCid %d)=> %f dB (eNB TX %d, SNR %f)\n",eNB_id,UE_id,CC_id,
1950
                eNB2UE[eNB_id][UE_id][CC_id]->path_loss_dB,
1951
                PHY_vars_eNB_g[eNB_id][CC_id]->frame_parms.pdsch_config_common.referenceSignalPower,snr_dB);
1952

1953
        }
1954
      }
Raymond Knopp's avatar
 
Raymond Knopp committed
1955
    }
1956 1957
  }
}
1958
#ifdef OPENAIR2
1959 1960
void update_otg_eNB(module_id_t enb_module_idP, unsigned int ctime)
{
1961

1962
#if defined(USER_MODE) && defined(OAI_EMU)
1963

Cedric Roux's avatar
Cedric Roux committed
1964
  //int rrc_state=0;
1965

1966 1967
  if (oai_emulation.info.otg_enabled ==1 ) {

1968 1969 1970 1971 1972 1973 1974
    int dst_id, app_id;
    Packet_otg_elt_t *otg_pkt;

    for (dst_id = 0; dst_id < NUMBER_OF_UE_MAX; dst_id++) {
      for_times += 1;

      // generate traffic if the ue is rrc reconfigured state
1975
      //if ((rrc_state=mac_eNB_get_rrc_status(enb_module_idP, dst_id)) > 2 /*RRC_CONNECTED*/ ) {
1976
      if (mac_eNB_get_rrc_status(enb_module_idP, oai_emulation.info.eNB_ue_module_id_to_rnti[enb_module_idP][dst_id]) > 2 ){
1977
	if_times += 1;
1978

1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997
        for (app_id=0; app_id<MAX_NUM_APPLICATION; app_id++) {
          otg_pkt = malloc (sizeof(Packet_otg_elt_t));

          (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));

          if ((otg_pkt->otg_pkt).sdu_buffer != NULL) {
            otg_times += 1;
            (otg_pkt->otg_pkt).rb_id = DTCH-2; // app could be binded to a given DRB
            (otg_pkt->otg_pkt).module_id = enb_module_idP;
            (otg_pkt->otg_pkt).dst_id = dst_id;
            (otg_pkt->otg_pkt).is_ue = 0;
            (otg_pkt->otg_pkt).mode = PDCP_TRANSMISSION_MODE_DATA;
            //Adding the packet to the OTG-PDCP buffer
            pkt_list_add_tail_eurecom(otg_pkt, &(otg_pdcp_buffer[enb_module_idP]));
            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;
1998
          }
1999
        }
2000
      }
2001
    }
2002

Cedric Roux's avatar
Cedric Roux committed
2003
#if defined(Rel10) || defined(Rel14)
2004 2005 2006 2007 2008
    mbms_service_id_t service_id;
    mbms_session_id_t session_id;
    rb_id_t           rb_id;

    // MBSM multicast traffic
2009
    if (ctime >= 500 ) {// only generate when UE can receive MTCH (need to control this value)
2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037
      for (service_id = 0; service_id < 2 ; service_id++) { //maxServiceCount
        for (session_id = 0; session_id < 2; session_id++) { // maxSessionPerPMCH
          if (pdcp_mbms_array_eNB[enb_module_idP][service_id][session_id].instanciated_instance == TRUE) { // this service/session is configured

            otg_pkt = malloc (sizeof(Packet_otg_elt_t));
            // 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);
            rb_id = pdcp_mbms_array_eNB[enb_module_idP][service_id][session_id].rb_id;
            (otg_pkt->otg_pkt).sdu_buffer = (uint8_t*) packet_gen_multicast(enb_module_idP, session_id, ctime, &((otg_pkt->otg_pkt).sdu_buffer_size));

            if ((otg_pkt->otg_pkt).sdu_buffer != NULL) {
              (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;
              //Adding the packet to the OTG-PDCP buffer
              (otg_pkt->otg_pkt).mode       = PDCP_TRANSMISSION_MODE_TRANSPARENT;
              pkt_list_add_tail_eurecom(otg_pkt, &(otg_pdcp_buffer[enb_module_idP]));
              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
Cedric Roux's avatar
Cedric Roux committed
2038
            #if defined(Rel10) || defined(Rel14)
2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052
            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);
            if ((pdcp_mbms_array[module_idP][service_id*maxSessionPerPMCH + session_id].instanciated_instance== module_idP + 1) && (eNB_flag == 1)){ // this service/session is configured
            // 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;
            otg_pkt=(uint8_t*) packet_gen_multicast(module_idP, session_id, ctime, &pkt_size);
            if (otg_pkt != NULL) {
            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);
            pdcp_data_req(module_id, frame, eNB_flag, rb_id, RLC_MUI_UNDEFINED, RLC_SDU_CONFIRM_NO, pkt_size, otg_pkt,PDCP_TM);
2053
            free(otg_pkt);
2054 2055 2056 2057 2058 2059 2060
            }
            }
            }
            }
            } // end multicast traffic
            #endif
             */
2061 2062


2063
          }
2064
        }
2065
      }
2066

2067
    } // end multicast traffic
2068

2069
#endif
2070
  }
2071

2072
#else
2073
#if 0 //  defined(EXMIMO) || defined(OAI_USRP)
2074
  if (otg_enabled==1) {
2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095
    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) {
        otg_pkt = malloc (sizeof(Packet_otg_elt_t));
        (otg_pkt->otg_pkt).sdu_buffer = packet_gen(module_instP, dst_id, ctime, &pkt_size);

        if (otg_pkt != NULL) {
          rb_id = DTCH-2;
          (otg_pkt->otg_pkt).rb_id     = rb_id;
          (otg_pkt->otg_pkt).module_id = module_idP;
          (otg_pkt->otg_pkt).is_ue     = FALSE;
          (otg_pkt->otg_pkt).mode      = PDCP_TRANSMISSION_MODE_DATA;
          //Adding the packet to the OTG-PDCP buffer
          pkt_list_add_tail_eurecom(otg_pkt, &(otg_pdcp_buffer[module_idP]));
          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;
        }
2096
      }
2097
    }
2098
  }
2099

2100
#endif
2101 2102 2103
#endif
}

2104 2105
void update_otg_UE(module_id_t ue_mod_idP, unsigned int ctime)
{
Cedric Roux's avatar
Cedric Roux committed
2106
#if defined(USER_MODE) && defined(OAI_EMU)
2107

2108
  int app_id;
2109
  if (oai_emulation.info.otg_enabled ==1 ) {
2110 2111 2112 2113 2114 2115 2116
    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<NB_SIG_CNX_UE; dst_id++) {
      // only consider the first attached eNB
2117
      if (mac_UE_get_rrc_status(ue_mod_idP, dst_id ) > 2 /*RRC_CONNECTED*/) {
2118 2119
        for (app_id=0; app_id<MAX_NUM_APPLICATION; app_id++) {
	  Packet_otg_elt_t *otg_pkt = malloc (sizeof(Packet_otg_elt_t));
2120

2121 2122 2123 2124 2125 2126
	  if (otg_pkt!=NULL)
	    memset(otg_pkt,0,sizeof(Packet_otg_elt_t));
	  else {
	    LOG_E(OTG,"not enough memory\n");
	    exit(-1);
	  }// Manage to add this packet to the tail of your list
2127

2128
	  (otg_pkt->otg_pkt).sdu_buffer = (uint8_t*) packet_gen(src_id, dst_id, app_id, ctime, &((otg_pkt->otg_pkt).sdu_buffer_size));
2129

2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144
	  if ((otg_pkt->otg_pkt).sdu_buffer != NULL) {
	    (otg_pkt->otg_pkt).rb_id     = DTCH-2;
	    (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
	    (otg_pkt->otg_pkt).mode      = PDCP_TRANSMISSION_MODE_DATA;
	    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;
	  }
	}
2145
      }
2146
    }
2147
  }
2148

2149 2150
#endif
}
2151
#endif
2152

2153 2154
int init_slot_isr(void)
{
2155
  if (oai_emulation.info.slot_isr) {
2156
    struct itimerspec its;
2157

2158
    int sfd;
2159

2160
    sfd = timerfd_create(CLOCK_REALTIME, 0);
2161

2162 2163 2164 2165
    if (sfd == -1) {
      LOG_E(EMU, "Failed in timerfd_create (%d:%s)\n", errno, strerror(errno));
      exit(EXIT_FAILURE);
    }
2166

2167 2168 2169 2170 2171 2172 2173 2174 2175 2176
    /* 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;

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

2178
    oai_emulation.info.slot_sfd = sfd;
2179
  }
2180

2181
  return 0;
2182 2183 2184 2185
}

void wait_for_slot_isr(void)
{
2186 2187
  uint64_t exp;
  ssize_t res;
2188

2189
  if (oai_emulation.info.slot_sfd > 0) {
2190
    res = read(oai_emulation.info.slot_sfd, &exp, sizeof(exp));
2191

2192 2193 2194 2195
    if ((res < 0) || (res != sizeof(exp))) {
      LOG_E(EMU, "Failed in read (%d:%s)\n", errno, strerror(errno));
      exit(EXIT_FAILURE);
    }
2196
  }
2197 2198
}

2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210
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);
}

2211 2212
void init_time()
{
2213
  clock_gettime (CLOCK_REALTIME, &time_spec);
2214 2215
  time_now      = (unsigned long) time_spec.tv_nsec;
  td_avg        = 0;
2216
  sleep_time_us = SLEEP_STEP_US;
2217
  td_avg        = TARGET_SF_TIME_NS;
2218 2219
}

2220
/*
2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231
int openair0_transport_load(openair0_device *device, openair0_config_t *openair0_cfg, eth_params_t * eth_params) {

	return(0);


}

int openair0_device_load(openair0_device *device, openair0_config_t *openair0_cfg) {

	return(0);
}
2232
*/