oaisim_functions.c 60.4 KB
Newer Older
1
2
3
4
5
/*
 * 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
Cedric Roux's avatar
Cedric Roux committed
6
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 * 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>
gauthier's avatar
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"
gauthier's avatar
gauthier committed
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
gauthier's avatar
gauthier committed
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

94

gauthier's avatar
gauthier committed
95
96
97
98
int           otg_times             = 0;
int           if_times              = 0;
int           for_times             = 0;

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

Florian Kaltenberger's avatar
Florian Kaltenberger committed
135
136
int phy_test = 0;

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

gauthier's avatar
gauthier committed
155
Packet_OTG_List_t *otg_pdcp_buffer = NULL;
156

157
extern node_desc_t *enb_data[NUMBER_OF_RU_MAX];
158
extern node_desc_t *ue_data[NUMBER_OF_UE_MAX];
159
160
extern channel_desc_t *RU2UE[NUMBER_OF_RU_MAX][NUMBER_OF_UE_MAX][MAX_NUM_CCs];
extern channel_desc_t *UE2RU[NUMBER_OF_UE_MAX][NUMBER_OF_RU_MAX][MAX_NUM_CCs];
161
162

extern mapping small_scale_names[];
Cedric Roux's avatar
Cedric Roux committed
163
#if defined(Rel10) || defined(Rel14)
gauthier's avatar
gauthier committed
164
165
166
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
167

168
169
extern time_stats_t dl_chan_stats;
extern time_stats_t ul_chan_stats;
170

171
172
extern int xforms;

173

174
175
176
extern uint32_t          downlink_frequency[MAX_NUM_CCs][4];
extern int32_t           uplink_frequency_offset[MAX_NUM_CCs][4];

177
int oaisim_flag=1;
178

179

180
181
void get_simulation_options(int argc, char *argv[])
{
winckel's avatar
winckel committed
182
  int                           option;
winckel's avatar
winckel committed
183

winckel's avatar
winckel committed
184
  enum long_option_e {
gauthier's avatar
gauthier committed
185
    LONG_OPTION_START = 0x100, /* Start after regular single char options */
winckel's avatar
winckel committed
186

gauthier's avatar
gauthier committed
187
    LONG_OPTION_ENB_CONF,
winckel's avatar
winckel committed
188

gauthier's avatar
gauthier committed
189
190
    LONG_OPTION_PDNC_PERIOD,
    LONG_OPTION_OMG_PERIOD,
191
    LONG_OPTION_OEH_ENABLED,
winckel's avatar
winckel committed
192

gauthier's avatar
gauthier committed
193
194
195
196
    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
197

gauthier's avatar
gauthier committed
198
199
200
    LONG_OPTION_ENB_MIHF_REMOTE_PORT,
    LONG_OPTION_ENB_MIHF_IP_ADDRESS,
    LONG_OPTION_ENB_MIHF_ID,
winckel's avatar
winckel committed
201

gauthier's avatar
gauthier committed
202
203
204
205
    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
206

gauthier's avatar
gauthier committed
207
208
209
    LONG_OPTION_UE_MIHF_REMOTE_PORT,
    LONG_OPTION_UE_MIHF_IP_ADDRESS,
    LONG_OPTION_UE_MIHF_ID,
gauthier's avatar
gauthier committed
210
211

    LONG_OPTION_MALLOC_TRACE_ENABLED,
212
213

    LONG_OPTION_CBA_BACKOFF_TIMER,
214

Florian Kaltenberger's avatar
Florian Kaltenberger committed
215
    LONG_OPTION_PHYTEST,
216
    LONG_OPTION_XFORMS,
Cedric Roux's avatar
Cedric Roux committed
217
218
219
220

#if T_TRACER
    LONG_OPTION_T_PORT,
    LONG_OPTION_T_NOWAIT,
221
    LONG_OPTION_T_DONT_FORK,
Cedric Roux's avatar
Cedric Roux committed
222
#endif
winckel's avatar
winckel committed
223
  };
winckel's avatar
winckel committed
224

winckel's avatar
winckel committed
225
  static struct option long_options[] = {
226
    {"enb-conf",               required_argument, 0, LONG_OPTION_ENB_CONF},
winckel's avatar
winckel committed
227

228
229
230
    {"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
231

232
233
234
235
    {"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
236

237
238
239
    {"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
240

241
242
243
244
    {"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
245

246
247
248
    {"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},
249

250
    {"malloc-trace-enabled",   no_argument,       0, LONG_OPTION_MALLOC_TRACE_ENABLED},
251

252
    {"cba-backoff",            required_argument, 0, LONG_OPTION_CBA_BACKOFF_TIMER},
253

Florian Kaltenberger's avatar
Florian Kaltenberger committed
254
    {"phy-test", no_argument, NULL, LONG_OPTION_PHYTEST},
255
    {"xforms",                 no_argument,       0, LONG_OPTION_XFORMS},
Florian Kaltenberger's avatar
Florian Kaltenberger committed
256

Cedric Roux's avatar
Cedric Roux committed
257
258
259
#if T_TRACER
    {"T_port",                 required_argument, 0, LONG_OPTION_T_PORT},
    {"T_nowait",               no_argument,       0, LONG_OPTION_T_NOWAIT},
260
    {"T_dont_fork",            no_argument,       0, LONG_OPTION_T_DONT_FORK},
Cedric Roux's avatar
Cedric Roux committed
261
262
#endif

263
    {NULL, 0, NULL, 0}
winckel's avatar
winckel committed
264
  };
winckel's avatar
winckel committed
265

266
  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) {
267
    switch (option) {
Florian Kaltenberger's avatar
Florian Kaltenberger committed
268
269
270
271
    case LONG_OPTION_PHYTEST:
      phy_test = 1;
      break;

272
273
    case LONG_OPTION_ENB_CONF:
      if (optarg) {
274
275
276
        free(RC.config_file_name); // prevent memory leak if option is used multiple times
        RC.config_file_name = strdup(optarg);
        printf("eNB configuration file is %s\n", RC.config_file_name);
277
      }
winckel's avatar
winckel committed
278

279
      break;
winckel's avatar
winckel committed
280

281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
    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;
gauthier's avatar
gauthier committed
310

311
#if ENABLE_RAL
winckel's avatar
winckel committed
312

313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
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
    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;
424
#endif
425

426
427
428
429
    case LONG_OPTION_XFORMS:
      xforms=1;
      break;

Cedric Roux's avatar
Cedric Roux committed
430
431
432
433
434
435
436
437
438
439
440
441
442
#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;
    }
443
444
445
446
447
448

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

451
452
    case 'a':
      abstraction_flag = 1;
453
454
      printf("FATAL: -a flag not functional for the moment.\nWe are working on fixing the abstraction mode.\n");
      exit(1);
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
      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
532
#ifdef PROC
533
534
535
      Process_Flag=1;
      node_id = wgt+atoi(optarg);
      port+=atoi(optarg);
winckel's avatar
winckel committed
536
#endif
537
538
539
540
541
542
543
544
545
546
547
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
      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
597
        exit(-1);
598
      }
gauthier's avatar
gauthier committed
599

600
601
602
603
      break;

    case 'O':
      if (optarg) {
604
605
        free(RC.config_file_name); // prevent memory leak if option is used multiple times
        RC.config_file_name = strdup(optarg);
606
607
608
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
      }

      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':
711
#ifdef SMBV
712
713
714
715
716
      config_smbv = 1;

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

717
#endif
718
      break;
winckel's avatar
winckel committed
719

720
    case 'x':
721
      /*
722
723
      oai_emulation.info.transmission_mode[0] = atoi (optarg);

724
      if ((oai_emulation.info.transmission_mode[0] != 1) && (oai_emulation.info.transmission_mode[0] != 2) &&
725
	  (oai_emulation.info.transmission_mode[0] != 3) && (oai_emulation.info.transmission_mode[0] != 4) &&
726
          (oai_emulation.info.transmission_mode[0] != 5) && (oai_emulation.info.transmission_mode[0] != 6)) && (oai_emulation.info.transmission_mode[0] !=7)) {
727
728
729
        printf("Unsupported transmission mode %d\n",oai_emulation.info.transmission_mode[0]);
        exit(-1);
      }
730
731
732
      */
      printf("Option -x deprecated. Please set transmission mode in eNB config file\n");
      exit(-1);
winckel's avatar
winckel committed
733

734
735
736
      break;

    case 'X':
winckel's avatar
winckel committed
737
#ifdef PROC
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
      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
753
#endif
754
      break;
winckel's avatar
winckel committed
755

756
    case 'y':
757
      nb_antennas_rx_ue=atoi(optarg);
winckel's avatar
winckel committed
758

759
      if (nb_antennas_rx_ue>4) {
760
761
762
        printf("Cannot have more than 4 antennas\n");
        exit(-1);
      }
winckel's avatar
winckel committed
763

764
      break;
winckel's avatar
winckel committed
765

766
767
768
    case 'Y':
      oai_emulation.info.g_log_verbosity_option = strdup(optarg);
      break;
winckel's avatar
winckel committed
769

770
771
772
773
774
775
776
777
778
779
780
781
782
    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
783
  }
784

785
786
787
788
   if ( load_configmodule(argc,argv) == NULL) {
    exit_fun("[SOFTMODEM] Error, configuration module init failed\n");
  } 
 
789
  if (RC.config_file_name != NULL) {
790
    /* Read eNB configuration file */
791
    RCConfig();
792
793
794
795
796
797
    printf("returned with %d eNBs, %d rus\n",RC.nb_inst,RC.nb_RU);
    oai_emulation.info.nb_enb_local = RC.nb_inst;
    oai_emulation.info.nb_ru_local = RC.nb_RU;
   
    /*    
    for (int j=0; j<enb_properties->nb_ru; j++) {
798
      
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
      //	local_remote_radio = BBU_REMOTE_RADIO_HEAD;
      (eth_params+j)->local_if_name             = enb_properties->ru_config[j]->ru_if_name;
      (eth_params+j)->my_addr                   = enb_properties->ru_config[j]->local_address;
      (eth_params+j)->my_port                   = enb_properties->ru_config[j]->local_port;
      (eth_params+j)->remote_addr               = enb_properties->ru_config[j]->remote_address;
      (eth_params+j)->remote_port               = enb_properties->ru_config[j]->remote_port;
      
      if (enb_properties->ru_config[j]->raw == 1) {
	(eth_params+j)->transp_preference       = ETH_RAW_MODE;
	ru_if_in[j] = REMOTE_IF5;
	node_function[j] = NGFI_RRU_IF5;
      } else if (enb_properties->ru_config[j]->rawif4p5 == 1) {
	(eth_params+j)->transp_preference       = ETH_RAW_IF4p5_MODE;
	ru_if_in[j] = REMOTE_IF4p5;    
	node_function[j] = NGFI_RRU_IF4p5; 
      } else if (enb_properties->ru_config[j]->udpif4p5 == 1) {
	(eth_params+j)->transp_preference       = ETH_UDP_IF4p5_MODE;
	ru_if_in[j] = REMOTE_IF4p5;    
	node_function[j] = NGFI_RRU_IF4p5;         
      } else if (enb_properties->ru_config[j]->rawif5_mobipass == 1) {
	(eth_params+j)->transp_preference       = ETH_RAW_IF5_MOBIPASS;             
	ru_if_in[j] = REMOTE_IF5;      
	LOG_E(EMU,"Don't use 8-bit IF5 format with oaisim, please change in configuration file\n");
      } else {
	(eth_params+j)->transp_preference       = ETH_UDP_MODE;	 
	ru_if_in[j] = REMOTE_IF5;            
	node_function[j] = NGFI_RRU_IF5;
826
      }
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
      node_timing[j] = synch_to_ext_device;

      if (enb_properties->number > 0) {
	//Update some simulation parameters 
	oai_emulation.info.frame_type[0]           = enb_properties->properties[0]->frame_type[0];
	oai_emulation.info.tdd_config[0]           = enb_properties->properties[0]->tdd_config[0];
	oai_emulation.info.tdd_config_S[0]         = enb_properties->properties[0]->tdd_config_s[0];
	oai_emulation.info.extended_prefix_flag[0] = enb_properties->properties[0]->prefix_type[0];
	
	oai_emulation.info.node_function[0]        = enb_properties->properties[0]->cc_node_function[0];
	oai_emulation.info.node_timing[0]          = enb_properties->properties[0]->cc_node_timing[0];
	downlink_frequency[0][0]                   = enb_properties->properties[0]->downlink_frequency[0];
	uplink_frequency_offset[0][0]              = enb_properties->properties[0]->uplink_frequency_offset[0];
	oai_emulation.info.N_RB_DL[0]              = enb_properties->properties[0]->N_RB_DL[0];
	LOG_E(EMU,"Please use only RRU with oaisim, remove eNB descriptors in configuration file\n");
	exit(-1);
      }
      }*/
  }
  else {
    printf("Please provide a configuration file\n");
    exit(-1);
849
  }
850
851
}

852
853
void check_and_adjust_params(void)
{
854

gauthier's avatar
gauthier committed
855
  int32_t ret;
856
857
  int i,j;

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

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

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

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

886
  if (ret < 0)
887
    LOG_W(EMU,"[INIT] Netlink not available, careful ...\n");
888
  /*
889
  if (ethernet_flag == 1) {
890
891
    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;
892

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

    while (emu_tx_status != SYNCED_TRANSPORT) {
      LOG_I (EMU, " Waiting for EMU Transport to be synced\n");
      emu_transport_sync ();    //emulation_tx_rx();
    }
916
  } // ethernet flag
917
  */
918
  //
919
920
  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;
921
  NB_RN_INST = oai_emulation.info.nb_rn_local + oai_emulation.info.nb_rn_remote;
922
  NB_RU = oai_emulation.info.nb_ru_local + oai_emulation.info.nb_ru_remote;
923

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

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

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

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

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

949
950
}

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

956
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
    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" );
989
990
  }
}
991
#endif
992

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

  if(set_seed) {

998
999
    randominit (oai_emulation.info.seed);
    set_taus_seed (oai_emulation.info.seed);
1000
1001

  } else {
1002
1003
    randominit (0);
    set_taus_seed (0);
1004
1005
1006
  }
}

1007
openair0_timestamp current_ru_rx_timestamp[NUMBER_OF_RU_MAX][MAX_NUM_CCs];
1008
openair0_timestamp current_UE_rx_timestamp[NUMBER_OF_UE_MAX][MAX_NUM_CCs];
1009
openair0_timestamp last_ru_rx_timestamp[NUMBER_OF_RU_MAX][MAX_NUM_CCs];
1010
1011
openair0_timestamp last_UE_rx_timestamp[NUMBER_OF_UE_MAX][MAX_NUM_CCs];

1012
int ru_trx_start(openair0_device *device) {
1013
1014
1015
  return(0);
}

1016
void ru_trx_end(openair0_device *device) {
1017
1018
1019
  return;
}

1020
int ru_trx_stop(openair0_device *device) {
1021
1022
1023
1024
1025
  return(0);
}
int UE_trx_start(openair0_device *device) {
  return(0);
}
1026
1027
void UE_trx_end(openair0_device *device) {
  return;
1028
1029
1030
1031
}
int UE_trx_stop(openair0_device *device) {
  return(0);
}
1032
int ru_trx_set_freq(openair0_device *device, openair0_config_t *openair0_cfg, int dummy) {
1033
1034
  return(0);
}
1035
int ru_trx_set_gains(openair0_device *device, openair0_config_t *openair0_cfg) {
1036
1037
1038
1039
1040
1041
1042
1043
1044
  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);
}

1045
extern pthread_mutex_t subframe_mutex;
1046
extern int subframe_ru_mask,subframe_UE_mask;
1047

1048

1049
int ru_trx_read(openair0_device *device, openair0_timestamp *ptimestamp, void **buff, int nsamps, int cc) {
1050

1051
  int ru_id  = device->Mod_id;
1052
1053
  int CC_id  = device->CC_id;

1054
1055
1056
  int subframe;
  int sample_count=0;

1057
  *ptimestamp = last_ru_rx_timestamp[ru_id][CC_id];
1058
1059


1060
1061
1062
1063
1064
  LOG_D(EMU,"RU_trx_read nsamps %d TS(%llu,%llu) => subframe %d\n",nsamps,
        (unsigned long long)current_ru_rx_timestamp[ru_id][CC_id],
        (unsigned long long)last_ru_rx_timestamp[ru_id][CC_id],
	(int)((*ptimestamp/RC.ru[ru_id]->frame_parms.samples_per_tti)%10));
  // if we're at a subframe boundary generate UL signals for this ru
1065
1066

  while (sample_count<nsamps) {
1067
1068
    while (current_ru_rx_timestamp[ru_id][CC_id]<
	   (nsamps+last_ru_rx_timestamp[ru_id][CC_id])) {
knopp's avatar
knopp committed
1069
      LOG_D(EMU,"RU: current TS %llu, last TS %llu, sleeping\n",current_ru_rx_timestamp[ru_id][CC_id],last_ru_rx_timestamp[ru_id][CC_id]);
1070
1071
1072
1073
      usleep(500);
    }

    
1074
1075
    subframe = (last_ru_rx_timestamp[ru_id][CC_id]/RC.ru[ru_id]->frame_parms.samples_per_tti)%10;
    LOG_D(EMU,"RU_trx_read generating UL subframe %d (Ts %llu, current TS %llu)\n",
1076
	  subframe,(unsigned long long)*ptimestamp,
1077
	  (unsigned long long)current_ru_rx_timestamp[ru_id][CC_id]);
1078
    
1079
    do_UL_sig(UE2RU,
1080
1081
1082
	      enb_data,
	      ue_data,
	      subframe,
1083
	      0,  // abstraction_flag
1084
	      &RC.ru[ru_id]->frame_parms,
1085
	      0,  // frame is only used for abstraction
1086
	      ru_id,
1087
	      CC_id);
1088
  
1089
1090
    last_ru_rx_timestamp[ru_id][CC_id] += RC.ru[ru_id]->frame_parms.samples_per_tti;
    sample_count += RC.ru[ru_id]->frame_parms.samples_per_tti;
1091
  }
knopp's avatar
knopp committed
1092
1093
  

1094
  return(nsamps);
1095
1096
}

1097
1098
int UE_trx_read(openair0_device *device, openair0_timestamp *ptimestamp, void **buff, int nsamps, int cc)
{
1099
1100
  int UE_id = device->Mod_id;
  int CC_id  = device->CC_id;
1101

1102
  int subframe,new_subframe;
1103
1104
1105
1106
1107
  int sample_count=0;
  int read_size;

  *ptimestamp = last_UE_rx_timestamp[UE_id][CC_id];

1108
  LOG_D(EMU,"[TXPATH]UE_trx_read nsamps %d TS %llu (%llu) antenna %d\n",nsamps,
1109
        (unsigned long long)current_UE_rx_timestamp[UE_id][CC_id],
knopp's avatar
knopp committed
1110
1111
        (unsigned long long)last_UE_rx_timestamp[UE_id][CC_id],
	cc);
1112

knopp's avatar
knopp committed
1113

1114
1115
1116
1117
1118
1119
1120
1121
  if (nsamps < PHY_vars_UE_g[UE_id][CC_id]->frame_parms.samples_per_tti)
    read_size = nsamps;
  else
    read_size = PHY_vars_UE_g[UE_id][CC_id]->frame_parms.samples_per_tti;

  while (sample_count<nsamps) {
    while (current_UE_rx_timestamp[UE_id][CC_id] < 
	   (last_UE_rx_timestamp[UE_id][CC_id]+read_size)) {
1122
      LOG_D(EMU,"[TXPATH]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]);
1123
1124
      usleep(500);
    }
1125

1126
    //    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]);
1127

1128
    // if we cross a subframe-boundary
1129
    subframe = (last_UE_rx_timestamp[UE_id][CC_id]/PHY_vars_UE_g[UE_id][CC_id]->frame_parms.samples_per_tti)%10;
1130
1131
1132
1133
1134
1135
1136
    new_subframe = ((last_UE_rx_timestamp[UE_id][CC_id]+read_size)/PHY_vars_UE_g[UE_id][CC_id]->frame_parms.samples_per_tti)%10;
    if (new_subframe!=subframe) {
      // tell top-level we are busy 
      pthread_mutex_lock(&subframe_mutex);
      subframe_UE_mask|=(1<<UE_id);
      LOG_D(EMU,"Setting UE_id %d mask to busy (%d)\n",UE_id,subframe_UE_mask);
      pthread_mutex_unlock(&subframe_mutex);
1137

1138
    }
1139
1140
1141
1142

    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]);    
1143
    do_DL_sig(RU2UE,
1144
1145
1146
	      enb_data,
	      ue_data,
	      subframe,
1147
1148
	      last_UE_rx_timestamp[UE_id][CC_id]%PHY_vars_UE_g[UE_id][CC_id]->frame_parms.samples_per_tti,
	      nsamps,
1149
1150
1151
	      0, //abstraction_flag,
	      &PHY_vars_UE_g[UE_id][CC_id]->frame_parms,
	      UE_id,
1152
	      CC_id);
knopp's avatar
knopp committed
1153

1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
    LOG_D(EMU,"[TXPATH]UE_trx_read @ TS %llu (%llu)=> frame %d, subframe %d\n",
	  (unsigned long long)current_UE_rx_timestamp[UE_id][CC_id],
	  (unsigned long long)last_UE_rx_timestamp[UE_id][CC_id],
	  ((unsigned long long)last_UE_rx_timestamp[UE_id][CC_id]/(PHY_vars_UE_g[UE_id][CC_id]->frame_parms.samples_per_tti*10))&1023,
	  subframe);

    last_UE_rx_timestamp[UE_id][CC_id] += read_size;
    sample_count += read_size;
 


knopp's avatar
knopp committed
1165

1166
  }
knopp's avatar
knopp committed
1167
1168


1169
1170
1171
  return(nsamps);
}

1172
1173
extern double ru_amp[NUMBER_OF_RU_MAX];

1174
int ru_trx_write(openair0_device *device,openair0_timestamp timestamp, void **buff, int nsamps, int cc, int flags) {
1175

1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
  int ru_id = device->Mod_id;

  LTE_DL_FRAME_PARMS *frame_parms = &RC.ru[ru_id]->frame_parms;

  pthread_mutex_lock(&subframe_mutex);
  LOG_D(EMU,"[TXPATH] ru_trx_write: RU %d mask %d\n",ru_id,subframe_ru_mask);
  pthread_mutex_unlock(&subframe_mutex); 

  // compute amplitude of TX signal from first symbol in subframe
  // note: assumes that the packet is an entire subframe 

  ru_amp[ru_id] = 0;
  for (int aa=0; aa<RC.ru[ru_id]->nb_tx; aa++) {
    ru_amp[ru_id] += (double)signal_energy((int32_t*)buff[aa],frame_parms->ofdm_symbol_size)/(12*frame_parms->N_RB_DL);
  }
  ru_amp[ru_id] = sqrt(ru_amp[ru_id]);

  LOG_D(EMU,"Setting amp for RU %d to %f (%d)\n",ru_id,ru_amp[ru_id], dB_fixed((double)signal_energy((int32_t*)buff[0],frame_parms->ofdm_symbol_size)));
  // tell top-level we are done
  pthread_mutex_lock(&subframe_mutex);
  subframe_ru_mask|=(1<<ru_id);
  LOG_D(EMU,"Setting RU %d to busy\n",ru_id);
  pthread_mutex_unlock(&subframe_mutex);

1200
1201
1202
1203
1204
1205
  return(nsamps);
}

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

  return(nsamps);
1206
1207
}

1208
//void init_openair0(void);
1209

1210
//openair0_config_t openair0_cfg[MAX_CARDS];
1211

1212
/*
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
void init_openair0() {

  int card;
  int i;

  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,
1265
1266
           RC.ru[0]->nb_tx,
           RC.ru[0]->nb_rx);
1267
1268
1269
1270
1271
    openair0_cfg[card].Mod_id = 0;



    openair0_cfg[card].num_rb_dl=frame_parms[0]->N_RB_DL;
1272
1273
    openair0_cfg[card].tx_num_channels=min(2,RC.ru[0]->nb_tx);
    openair0_cfg[card].rx_num_channels=min(2,RC.ru[0]->nb_rx);
1274
1275
1276

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

1277
      openair0_cfg[card].rx_gain[i] = RC.ru[0]->rx_total_gain_dB;
1278
1279
1280