lte-softmodem.c 97.9 KB
Newer Older
1
/*******************************************************************************
2 3
    OpenAirInterface 
    Copyright(c) 1999 - 2014 Eurecom
4

5 6 7 8
    OpenAirInterface is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
9 10


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

16 17 18 19
    You should have received a copy of the GNU General Public License
    along with OpenAirInterface.The full GNU General Public License is 
    included in this distribution in the file called "COPYING". If not, 
    see <http://www.gnu.org/licenses/>.
20

21 22 23 24 25
   Contact Information
   OpenAirInterface Admin: openair_admin@eurecom.fr
   OpenAirInterface Tech : openair_tech@eurecom.fr
   OpenAirInterface Dev  : openair4g-devel@eurecom.fr
  
ghaddab's avatar
ghaddab committed
26
   Address      : Eurecom, Campus SophiaTech, 450 Route des Chappes, CS 50193 - 06904 Biot Sophia Antipolis cedex, FRANCE
27

Raymond Knopp's avatar
 
Raymond Knopp committed
28
*******************************************************************************/
29 30

/*! \file lte-softmodem.c
Raymond Knopp's avatar
 
Raymond Knopp committed
31
 * \brief main program to control HW and scheduling
32
 * \author R. Knopp, F. Kaltenberger, Navid Nikaein
Raymond Knopp's avatar
 
Raymond Knopp committed
33 34 35
 * \date 2012
 * \version 0.1
 * \company Eurecom
36
 * \email: knopp@eurecom.fr,florian.kaltenberger@eurecom.fr, navid.nikaein@eurecom.fr
Raymond Knopp's avatar
 
Raymond Knopp committed
37 38 39
 * \note
 * \warning
 */
Raymond Knopp's avatar
 
Raymond Knopp committed
40
#define _GNU_SOURCE
41 42 43 44 45 46 47 48 49 50
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sched.h>
51
#include <linux/sched.h>
52 53 54
#include <signal.h>
#include <execinfo.h>
#include <getopt.h>
55
#include <syscall.h>
56 57

#include "rt_wrapper.h"
58 59
#undef MALLOC //there are two conflicting definitions, so we better make sure we don't use it at all

60
#ifndef EXMIMO
Raymond Knopp's avatar
 
Raymond Knopp committed
61
static int hw_subframe;
Raymond Knopp's avatar
 
Raymond Knopp committed
62
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
63

64
#include "assertions.h"
65 66 67 68 69 70

#ifdef EMOS
#include <gps.h>
#endif

#include "PHY/types.h"
71

72
#include "PHY/defs.h"
73
#undef MALLOC //there are two conflicting definitions, so we better make sure we don't use it at all
Raymond Knopp's avatar
 
Raymond Knopp committed
74
//#undef FRAME_LENGTH_COMPLEX_SAMPLES //there are two conflicting definitions, so we better make sure we don't use it at all
75

76
#ifdef EXMIMO
77
#include "openair0_lib.h"
Raymond Knopp's avatar
 
Raymond Knopp committed
78 79 80 81
#else
#include "../../ARCH/COMMON/common_lib.h"
#endif

Raymond Knopp's avatar
 
Raymond Knopp committed
82
//#undef FRAME_LENGTH_COMPLEX_SAMPLES //there are two conflicting definitions, so we better make sure we don't use it at all
83 84 85

#include "PHY/vars.h"
#include "MAC_INTERFACE/vars.h"
86
//#include "SCHED/defs.h"
87 88 89 90 91 92 93 94 95 96 97 98
#include "SCHED/vars.h"
#include "LAYER2/MAC/vars.h"

#include "../../SIMU/USER/init_lte.h"

#ifdef EMOS
#include "SCHED/phy_procedures_emos.h"
#endif

#ifdef OPENAIR2
#include "LAYER2/MAC/defs.h"
#include "LAYER2/MAC/vars.h"
99
#include "LAYER2/MAC/proto.h"
100 101 102 103 104 105 106 107 108
#include "RRC/LITE/vars.h"
#include "PHY_INTERFACE/vars.h"
#endif

#ifdef SMBV
#include "PHY/TOOLS/smbv.h"
unsigned short config_frames[4] = {2,9,11,13};
#endif
#include "UTIL/LOG/log_extern.h"
Navid Nikaein's avatar
Navid Nikaein committed
109
#include "UTIL/OTG/otg_tx.h"
110
#include "UTIL/OTG/otg_externs.h"
111 112
#include "UTIL/MATH/oml.h"
#include "UTIL/LOG/vcd_signal_dumper.h"
113
#include "UTIL/OPT/opt.h"
114
#include "enb_config.h"
Navid Nikaein's avatar
Navid Nikaein committed
115
//#include "PHY/TOOLS/time_meas.h"
116

Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
117 118 119 120
#ifndef OPENAIR2
#include "UTIL/OTG/otg_vars.h"
#endif

121 122
#if defined(ENABLE_ITTI)
# include "intertask_interface_init.h"
123
# include "create_tasks.h"
124 125
# if defined(ENABLE_USE_MME)
#   include "s1ap_eNB.h"
126 127 128
#ifdef NAS_NETLINK
#   include "SIMULATION/ETH_TRANSPORT/proto.h"
#endif
129
# endif
130 131
#endif

132 133 134
#ifdef XFORMS
#include "PHY/TOOLS/lte_phy_scope.h"
#include "stats.h"
135 136 137 138 139
#endif

#define FRAME_PERIOD    100000000ULL
#define DAQ_PERIOD      66667ULL

Florian Kaltenberger's avatar
Florian Kaltenberger committed
140
//#define DEBUG_THREADS 1
Raymond Knopp's avatar
 
Raymond Knopp committed
141

Raymond Knopp's avatar
 
Raymond Knopp committed
142 143
//#define USRP_DEBUG 1

144 145 146 147 148 149 150 151
struct timing_info_t {
  //unsigned int frame, hw_slot, last_slot, next_slot;
  RTIME time_min, time_max, time_avg, time_last, time_now;
  //unsigned int mbox0, mbox1, mbox2, mbox_target;
  unsigned int n_samples;
} timing_info;


Raymond Knopp's avatar
 
Raymond Knopp committed
152

153
openair0_config_t openair0_cfg[MAX_CARDS];
Raymond Knopp's avatar
 
Raymond Knopp committed
154

Raymond Knopp's avatar
 
Raymond Knopp committed
155 156
int32_t **rxdata;
int32_t **txdata;
157 158
int setup_ue_buffers(PHY_VARS_UE **phy_vars_ue, openair0_config_t *openair0_cfg, openair0_rf_map rf_map[MAX_NUM_CCs]);
int setup_eNB_buffers(PHY_VARS_eNB **phy_vars_eNB, openair0_config_t *openair0_cfg, openair0_rf_map rf_map[MAX_NUM_CCs]);
159

160
void fill_ue_band_info(void);
161
#ifdef XFORMS
162 163 164 165
// current status is that every UE has a DL scope for a SINGLE eNB (eNB_id=0)
// at eNB 0, an UL scope for every UE 
FD_lte_phy_scope_ue  *form_ue[NUMBER_OF_UE_MAX];
FD_lte_phy_scope_enb *form_enb[NUMBER_OF_UE_MAX];
166
FD_stats_form                  *form_stats=NULL,*form_stats_l2=NULL;
167
char title[255];
168
unsigned char                   scope_enb_num_ue = 1;
169 170 171
#endif //XFORMS

#ifdef RTAI
172

173 174
static long                      main_eNB_thread;
static long                      main_ue_thread;
175

176
#else
Raymond Knopp's avatar
 
Raymond Knopp committed
177 178
pthread_t                       main_eNB_thread;
pthread_t                       main_ue_thread;
179

180
pthread_attr_t                  attr_dlsch_threads;
181 182
pthread_attr_t                  attr_UE_thread;

183
#ifndef LOWLATENCY
184
struct sched_param              sched_param_dlsch;
185
#endif 
186
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
187

Raymond Knopp's avatar
 
Raymond Knopp committed
188 189
pthread_cond_t sync_cond;
pthread_mutex_t sync_mutex;
190 191
int sync_var=-1;

192

193 194


195
struct sched_param              sched_param_UE_thread;
196 197 198 199

pthread_attr_t                  attr_eNB_proc_tx[MAX_NUM_CCs][10];
pthread_attr_t                  attr_eNB_proc_rx[MAX_NUM_CCs][10];
#ifndef LOWLATENCY
Raymond Knopp's avatar
 
Raymond Knopp committed
200 201
struct sched_param              sched_param_eNB_proc_tx[MAX_NUM_CCs][10];
struct sched_param              sched_param_eNB_proc_rx[MAX_NUM_CCs][10];
202
#endif
203
#ifdef XFORMS
Raymond Knopp's avatar
 
Raymond Knopp committed
204
static pthread_t                forms_thread; //xforms
205 206 207 208
#endif
#ifdef EMOS
static pthread_t                thread3; //emos
#endif
209

Raymond Knopp's avatar
 
Raymond Knopp committed
210
openair0_device openair0;
211

Raymond Knopp's avatar
 
Raymond Knopp committed
212

213
/*
Raymond Knopp's avatar
 
Raymond Knopp committed
214 215 216
  static int instance_cnt=-1; //0 means worker is busy, -1 means its free
  int instance_cnt_ptr_kern,*instance_cnt_ptr_user;
  int pci_interface_ptr_kern;
217 218 219 220
*/
//extern unsigned int bigphys_top;
//extern unsigned int mem_base;

221
int                             card = 0;
Raymond Knopp's avatar
 
Raymond Knopp committed
222

223

224
#if defined(ENABLE_ITTI)
225 226
static volatile int             start_eNB = 0;
static volatile int             start_UE = 0;
227
#endif
228 229 230 231 232 233
volatile int                    oai_exit = 0;

//static int                      time_offset[4] = {-138,-138,-138,-138};
//static int                      time_offset[4] = {-145,-145,-145,-145};
static int                      time_offset[4] = {0,0,0,0};

Raymond Knopp's avatar
 
Raymond Knopp committed
234

235
static char                     UE_flag=0;
236 237
//static uint8_t                  eNB_id=0,UE_id=0;

238

239 240
uint32_t                 downlink_frequency[MAX_NUM_CCs][4];
int32_t                  uplink_frequency_offset[MAX_NUM_CCs][4]; 
Lionel Gauthier's avatar
Lionel Gauthier committed
241

242
openair0_rf_map rf_map[MAX_NUM_CCs];
243

244
static char                    *conf_config_file_name = NULL;
245
#if defined(ENABLE_ITTI)
246
static char                    *itti_dump_file = NULL;
Raymond Knopp's avatar
 
Raymond Knopp committed
247 248
#endif

249 250 251 252
int UE_scan = 1;
runmode_t mode = normal_txrx;


Raymond Knopp's avatar
 
Raymond Knopp committed
253
#ifdef EXMIMO
254 255
double tx_gain[MAX_NUM_CCs][4] = {{20,20,0,0},{20,20,0,0}};
double rx_gain[MAX_NUM_CCs][4] = {{20,20,0,0},{20,20,0,0}};
256
// these are for EXMIMO2 target only
257
/*
Raymond Knopp's avatar
 
Raymond Knopp committed
258 259 260
  static unsigned int             rxg_max[4] =    {133,133,133,133};
  static unsigned int             rxg_med[4] =    {127,127,127,127};
  static unsigned int             rxg_byp[4] =    {120,120,120,120};
261 262
*/
// these are for EXMIMO2 card 39
263 264 265 266 267 268 269
unsigned int             rxg_max[4] =    {128,128,128,126};
unsigned int             rxg_med[4] =    {122,123,123,120};
unsigned int             rxg_byp[4] =    {116,117,116,116};
unsigned int             nf_max[4] =    {7,9,16,12};
unsigned int             nf_med[4] =    {12,13,22,17};
unsigned int             nf_byp[4] =    {15,20,29,23};
rx_gain_t                rx_gain_mode[MAX_NUM_CCs][4] = {{max_gain,max_gain,max_gain,max_gain},{max_gain,max_gain,max_gain,max_gain}};
Lionel Gauthier's avatar
Lionel Gauthier committed
270
#else
kortke's avatar
kortke committed
271 272
double tx_gain[MAX_NUM_CCs][4] = {{20,0,0,0}};
double rx_gain[MAX_NUM_CCs][4] = {{110,0,0,0}};
Lionel Gauthier's avatar
Lionel Gauthier committed
273
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
274

Raymond Knopp's avatar
 
Raymond Knopp committed
275 276
double sample_rate=30.72e6;
double bw = 14e6;
277

278
static int                      tx_max_power[MAX_NUM_CCs]; /* =  {0,0}*/;
279

280
#ifndef EXMIMO
Raymond Knopp's avatar
 
Raymond Knopp committed
281 282 283
char ref[128] = "internal";
char channels[128] = "0";

Raymond Knopp's avatar
 
Raymond Knopp committed
284 285
unsigned int samples_per_frame = 307200;
unsigned int tx_forward_nsamps;
Raymond Knopp's avatar
 
Raymond Knopp committed
286 287 288
int tx_delay;

#endif
289 290

int                      rx_input_level_dBm;
291
static int                      online_log_messages=0;
292
#ifdef XFORMS
293
extern int                      otg_enabled;
294
static char                     do_forms=0;
295
#else
296
int                             otg_enabled;
297
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
298
//int                             number_of_cards =   1;
Raymond Knopp's avatar
 
Raymond Knopp committed
299
#ifdef EXMIMO
300 301
static int                      mbox_bounds[20] =   {8,16,24,30,38,46,54,60,68,76,84,90,98,106,114,120,128,136,144, 0}; ///boundaries of slots in terms ob mbox counter rounded up to even numbers
//static int                      mbox_bounds[20] =   {6,14,22,28,36,44,52,58,66,74,82,88,96,104,112,118,126,134,142, 148}; ///boundaries of slots in terms ob mbox counter rounded up to even numbers
Raymond Knopp's avatar
 
Raymond Knopp committed
302
#endif
303

Raymond Knopp's avatar
 
Raymond Knopp committed
304
static LTE_DL_FRAME_PARMS      *frame_parms[MAX_NUM_CCs];
305

Raymond Knopp's avatar
 
Raymond Knopp committed
306
int multi_thread=1;
Florian Kaltenberger's avatar
Florian Kaltenberger committed
307
uint32_t target_dl_mcs = 28; //maximum allowed mcs
308
uint32_t target_ul_mcs = 10;
309 310
uint8_t exit_missed_slots=1;
uint64_t num_missed_slots=0; // counter for the number of missed slots
311

Raymond Knopp's avatar
 
Raymond Knopp committed
312

313
time_stats_t softmodem_stats_mt; // main thread
Raymond Knopp's avatar
 
Raymond Knopp committed
314
time_stats_t softmodem_stats_hw; //  hw acquisition
315 316
time_stats_t softmodem_stats_tx_sf[10]; // total tx time 
time_stats_t softmodem_stats_rx_sf[10]; // total rx time 
Navid Nikaein's avatar
Navid Nikaein committed
317 318
void reset_opp_meas(void);
void print_opp_meas(void);
319
int transmission_mode=1;
Raymond Knopp's avatar
 
Raymond Knopp committed
320

Raymond Knopp's avatar
 
Raymond Knopp committed
321

322
int16_t           glog_level         = LOG_INFO;
Lionel Gauthier's avatar
 
Lionel Gauthier committed
323
int16_t           glog_verbosity     = LOG_MED;
324
int16_t           hw_log_level       = LOG_INFO;
Lionel Gauthier's avatar
 
Lionel Gauthier committed
325
int16_t           hw_log_verbosity   = LOG_MED;
326
int16_t           phy_log_level      = LOG_INFO;
Lionel Gauthier's avatar
 
Lionel Gauthier committed
327
int16_t           phy_log_verbosity  = LOG_MED;
328
int16_t           mac_log_level      = LOG_INFO;
Lionel Gauthier's avatar
 
Lionel Gauthier committed
329
int16_t           mac_log_verbosity  = LOG_MED;
330
int16_t           rlc_log_level      = LOG_INFO;
Lionel Gauthier's avatar
 
Lionel Gauthier committed
331
int16_t           rlc_log_verbosity  = LOG_MED;
332
int16_t           pdcp_log_level     = LOG_INFO;
Lionel Gauthier's avatar
 
Lionel Gauthier committed
333
int16_t           pdcp_log_verbosity = LOG_MED;
Florian Kaltenberger's avatar
Florian Kaltenberger committed
334
int16_t           rrc_log_level      = LOG_INFO;
Lionel Gauthier's avatar
 
Lionel Gauthier committed
335 336 337 338 339 340 341
int16_t           rrc_log_verbosity  = LOG_MED;
# if defined(ENABLE_USE_MME)
int16_t           gtpu_log_level     = LOG_DEBUG;
int16_t           gtpu_log_verbosity = LOG_MED;
int16_t           udp_log_level      = LOG_DEBUG;
int16_t           udp_log_verbosity  = LOG_MED;
#endif
342 343 344 345
#if defined (ENABLE_SECURITY)
int16_t           osa_log_level      = LOG_INFO;
int16_t           osa_log_verbosity  = LOG_MED;
#endif 
346

Raymond Knopp's avatar
 
Raymond Knopp committed
347 348 349

#ifdef ETHERNET
char rrh_eNB_ip[20] = "127.0.0.1";
Raymond Knopp's avatar
Raymond Knopp committed
350
int rrh_eNB_port = 50000;
Raymond Knopp's avatar
 
Raymond Knopp committed
351
char *rrh_UE_ip = "127.0.0.1";
352
int rrh_UE_port = 51000;
Raymond Knopp's avatar
 
Raymond Knopp committed
353 354 355
#endif

char uecap_xer[1024],uecap_xer_in=0;
356 357
extern void *UE_thread(void *arg);
extern void init_UE_threads(void);
Raymond Knopp's avatar
 
Raymond Knopp committed
358

359
unsigned int build_rflocal(int txi, int txq, int rxi, int rxq)
360
{
Raymond Knopp's avatar
 
Raymond Knopp committed
361
  return (txi + (txq<<6) + (rxi<<12) + (rxq<<18));
362 363 364
}
unsigned int build_rfdc(int dcoff_i_rxfe, int dcoff_q_rxfe)
{
Raymond Knopp's avatar
 
Raymond Knopp committed
365
  return (dcoff_i_rxfe + (dcoff_q_rxfe<<8));
366 367
}

368 369 370 371 372 373 374 375 376 377 378 379
#ifdef LOWLATENCY
int sched_setattr(pid_t pid, const struct sched_attr *attr, unsigned int flags) {

  return syscall(__NR_sched_setattr, pid, attr, flags);
}


int sched_getattr(pid_t pid,struct sched_attr *attr,unsigned int size, unsigned int flags){

  return syscall(__NR_sched_getattr, pid, attr, size, flags);
}
#endif 
380
#if !defined(ENABLE_ITTI)
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
void signal_handler(int sig)
{
  void *array[10];
  size_t size;

  if (sig==SIGSEGV) {
    // get void*'s for all entries on the stack
    size = backtrace(array, 10);
    
    // print out all the frames to stderr
    fprintf(stderr, "Error: signal %d:\n", sig);
    backtrace_symbols_fd(array, size, 2);
    exit(-1);
  }
  else {
396
    printf("trying to exit gracefully...\n"); 
397
    oai_exit = 1;
398 399
  }
}
400
#endif
401 402 403

void exit_fun(const char* s)
{
404
  if (s != NULL) {
405
    printf("%s %s() Exiting OAI softmodem: %s\n",__FILE__, __FUNCTION__, s);
406 407 408 409 410
  }

  oai_exit = 1;

#if defined(ENABLE_ITTI)
Florian Kaltenberger's avatar
Florian Kaltenberger committed
411
  sleep(1); //allow lte-softmodem threads to exit first
412
  itti_terminate_tasks (TASK_UNKNOWN);
413
#endif
414 415 416 417 418 419

  //rt_sleep_ns(FRAME_PERIOD);

  //exit (-1);
}

Raymond Knopp's avatar
 
Raymond Knopp committed
420 421 422
static int latency_target_fd = -1;
static int32_t latency_target_value = 0;
/* Latency trick - taken from cyclictest.c 
Raymond Knopp's avatar
 
Raymond Knopp committed
423 424 425 426 427 428 429 430 431
 * if the file /dev/cpu_dma_latency exists,
 * open it and write a zero into it. This will tell
 * the power management system not to transition to
 * a high cstate (in fact, the system acts like idle=poll)
 * When the fd to /dev/cpu_dma_latency is closed, the behavior
 * goes back to the system default.
 *
 * Documentation/power/pm_qos_interface.txt
 */
Raymond Knopp's avatar
 
Raymond Knopp committed
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
static void set_latency_target(void)
{
  struct stat s;
  int ret;
  if (stat("/dev/cpu_dma_latency", &s) == 0) {
    latency_target_fd = open("/dev/cpu_dma_latency", O_RDWR);
    if (latency_target_fd == -1)
      return;
    ret = write(latency_target_fd, &latency_target_value, 4);
    if (ret == 0) {
      printf("# error setting cpu_dma_latency to %d!: %s\n", latency_target_value, strerror(errno));
      close(latency_target_fd);
      return;
    }
    printf("# /dev/cpu_dma_latency set to %dus\n", latency_target_value);
  }
}
Raymond Knopp's avatar
 
Raymond Knopp committed
449
 
450
#ifdef XFORMS
451 452 453 454

void reset_stats(FL_OBJECT *button, long arg) {
  int i,j,k;
  PHY_VARS_eNB *phy_vars_eNB = PHY_vars_eNB_g[0][0];
455 456 457 458 459 460 461 462 463 464 465 466
  for (i=0;i<NUMBER_OF_UE_MAX;i++) {
    for (k=0;k<8;k++) {//harq_processes
      for (j=0;j<phy_vars_eNB->dlsch_eNB[i][0]->Mdlharq;j++) {
	phy_vars_eNB->eNB_UE_stats[i].dlsch_NAK[k][j]=0;
	phy_vars_eNB->eNB_UE_stats[i].dlsch_ACK[k][j]=0;
	phy_vars_eNB->eNB_UE_stats[i].dlsch_trials[k][j]=0;
      }
      phy_vars_eNB->eNB_UE_stats[i].dlsch_l2_errors[k]=0;
      phy_vars_eNB->eNB_UE_stats[i].ulsch_errors[k]=0;
      phy_vars_eNB->eNB_UE_stats[i].ulsch_consecutive_errors=0;
      for (j=0;j<phy_vars_eNB->ulsch_eNB[i]->Mdlharq;j++) {
	phy_vars_eNB->eNB_UE_stats[i].ulsch_decoding_attempts[k][j]=0;
Raymond Knopp's avatar
 
Raymond Knopp committed
467 468 469
	phy_vars_eNB->eNB_UE_stats[i].ulsch_decoding_attempts_last[k][j]=0;
	phy_vars_eNB->eNB_UE_stats[i].ulsch_round_errors[k][j]=0;
	phy_vars_eNB->eNB_UE_stats[i].ulsch_round_fer[k][j]=0;
470
      }
471
    }
472 473 474
    phy_vars_eNB->eNB_UE_stats[i].dlsch_sliding_cnt=0;
    phy_vars_eNB->eNB_UE_stats[i].dlsch_NAK_round0=0;
    phy_vars_eNB->eNB_UE_stats[i].dlsch_mcs_offset=0;
475 476 477
  }
}

478
static void *scope_thread(void *arg) {
Raymond Knopp's avatar
 
Raymond Knopp committed
479
  char stats_buffer[16384];
480
# ifdef ENABLE_XFORMS_WRITE_STATS
Raymond Knopp's avatar
 
Raymond Knopp committed
481
  FILE *UE_stats, *eNB_stats;
482
# endif
Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
483
  int len = 0;
Raymond Knopp's avatar
 
Raymond Knopp committed
484
  struct sched_param sched_param;
485
  int UE_id;
486

Raymond Knopp's avatar
 
Raymond Knopp committed
487 488
  sched_param.sched_priority = sched_get_priority_min(SCHED_FIFO)+1; 
  sched_setscheduler(0, SCHED_FIFO,&sched_param);
489

Raymond Knopp's avatar
 
Raymond Knopp committed
490
  printf("Scope thread has priority %d\n",sched_param.sched_priority);
491
    
492 493
# ifdef ENABLE_XFORMS_WRITE_STATS
  if (UE_flag==1) 
Raymond Knopp's avatar
 
Raymond Knopp committed
494
    UE_stats  = fopen("UE_stats.txt", "w");
495
  else 
Raymond Knopp's avatar
 
Raymond Knopp committed
496
    eNB_stats = fopen("eNB_stats.txt", "w");
497
#endif
498
    
Raymond Knopp's avatar
 
Raymond Knopp committed
499 500
  while (!oai_exit) {
    if (UE_flag==1) {
Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
501
      len = dump_ue_stats (PHY_vars_UE_g[0][0], stats_buffer, 0, mode,rx_input_level_dBm);
Raymond Knopp's avatar
 
Raymond Knopp committed
502
      fl_set_object_label(form_stats->stats_text, stats_buffer);
503

504 505 506 507
      phy_scope_UE(form_ue[0], 
		   PHY_vars_UE_g[0][0],
		   0,
		   0,7);
508
            
Raymond Knopp's avatar
 
Raymond Knopp committed
509
    } else {
Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
510 511
#ifdef OPENAIR2
      len = dump_eNB_l2_stats (stats_buffer, 0);
512
      fl_set_object_label(form_stats_l2->stats_text, stats_buffer);
Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
513 514 515 516 517
#endif
      len = dump_eNB_stats (PHY_vars_eNB_g[0][0], stats_buffer, 0);
      if (MAX_NUM_CCs>1)
	len += dump_eNB_stats (PHY_vars_eNB_g[0][1], &stats_buffer[len], 0);
    
518 519
      fl_set_object_label(form_stats->stats_text, stats_buffer);

Raymond Knopp's avatar
 
Raymond Knopp committed
520 521
      for(UE_id=0;UE_id<scope_enb_num_ue;UE_id++) {
	phy_scope_eNB(form_enb[UE_id], 
522
		      PHY_vars_eNB_g[0][0],
Raymond Knopp's avatar
 
Raymond Knopp committed
523 524
		      UE_id);
      }
525 526
              
    }
Raymond Knopp's avatar
 
Raymond Knopp committed
527
    //printf("doing forms\n");
Florian Kaltenberger's avatar
Florian Kaltenberger committed
528 529
    //usleep(100000); // 100 ms
    sleep(1);
Raymond Knopp's avatar
 
Raymond Knopp committed
530
  }
531 532

  printf("%s",stats_buffer);
533
    
534
# ifdef ENABLE_XFORMS_WRITE_STATS
535 536 537 538 539 540 541 542 543 544 545 546 547 548
  if (UE_flag==1) {
    if (UE_stats) {
      rewind (UE_stats);
      fwrite (stats_buffer, 1, len, UE_stats);
      fclose (UE_stats);
    }
  }
  else {
    if (eNB_stats) {
      rewind (eNB_stats);
      fwrite (stats_buffer, 1, len, eNB_stats);
      fclose (eNB_stats);
    }
  }
549
# endif
550
    
Raymond Knopp's avatar
 
Raymond Knopp committed
551
  pthread_exit((void*)arg);
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
}
#endif

#ifdef EMOS
#define NO_ESTIMATES_DISK 100 //No. of estimates that are aquired before dumped to disk

void *emos_thread (void *arg)
{
  char c;
  char *fifo2file_buffer, *fifo2file_ptr;

  int fifo, counter=0, bytes;

  FILE  *dumpfile_id;
  char  dumpfile_name[1024];
  time_t starttime_tmp;
  struct tm starttime;
  
  int channel_buffer_size;
  
  time_t timer;
  struct tm *now;

  struct gps_data_t *gps_data = NULL;
  struct gps_fix_t dummy_gps_data;
577 578 579 580 581 582 583

  struct sched_param sched_param;
  
  sched_param.sched_priority = sched_get_priority_max(SCHED_FIFO)-1; 
  sched_setscheduler(0, SCHED_FIFO,&sched_param);
  
  printf("EMOS thread has priority %d\n",sched_param.sched_priority);
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
 
  timer = time(NULL);
  now = localtime(&timer);

  memset(&dummy_gps_data,1,sizeof(struct gps_fix_t));
  
  gps_data = gps_open("127.0.0.1","2947");
  if (gps_data == NULL) 
    {
      printf("[EMOS] Could not open GPS\n");
      //exit(-1);
    }
#if GPSD_API_MAJOR_VERSION>=4
  else if (gps_stream(gps_data, WATCH_ENABLE,NULL) != 0)
#else
  else if (gps_query(gps_data, "w+x") != 0)
#endif
    {
      //sprintf(tmptxt,"Error sending command to GPS, gps_data = %x", gps_data);
      printf("[EMOS] Error sending command to GPS\n");
      //exit(-1);
    }
606 607
  else 
    printf("[EMOS] Opened GPS, gps_data=%p\n");
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
  
  if (UE_flag==0)
    channel_buffer_size = sizeof(fifo_dump_emos_eNB);
  else
    channel_buffer_size = sizeof(fifo_dump_emos_UE);

  // allocate memory for NO_FRAMES_DISK channes estimations
  fifo2file_buffer = malloc(NO_ESTIMATES_DISK*channel_buffer_size);
  fifo2file_ptr = fifo2file_buffer;

  if (fifo2file_buffer == NULL)
    {
      printf("[EMOS] Cound not allocate memory for fifo2file_buffer\n");
      exit(EXIT_FAILURE);
    }

  if ((fifo = open(CHANSOUNDER_FIFO_DEV, O_RDONLY)) < 0)
    {
      fprintf(stderr, "[EMOS] Error opening the fifo\n");
      exit(EXIT_FAILURE);
    }


  time(&starttime_tmp);
  localtime_r(&starttime_tmp,&starttime);
  snprintf(dumpfile_name,1024,"/tmp/%s_data_%d%02d%02d_%02d%02d%02d.EMOS",
	   (UE_flag==0) ? "eNB" : "UE",
	   1900+starttime.tm_year, starttime.tm_mon+1, starttime.tm_mday, starttime.tm_hour, starttime.tm_min, starttime.tm_sec);

  dumpfile_id = fopen(dumpfile_name,"w");
  if (dumpfile_id == NULL)
    {
      fprintf(stderr, "[EMOS] Error opening dumpfile %s\n",dumpfile_name);
      exit(EXIT_FAILURE);
    }


  printf("[EMOS] starting dump, channel_buffer_size=%d ...\n",channel_buffer_size);
  while (!oai_exit)
    {
      bytes = rtf_read_timed(fifo, fifo2file_ptr, channel_buffer_size,100);
      if (bytes==0)
	continue;

      /*
Raymond Knopp's avatar
 
Raymond Knopp committed
653
	if (UE_flag==0)
654
	printf("eNB: count %d, frame %d, read: %d bytes from the fifo\n",counter, ((fifo_dump_emos_eNB*)fifo2file_ptr)->frame_tx,bytes);
Raymond Knopp's avatar
 
Raymond Knopp committed
655
	else
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
	printf("UE: count %d, frame %d, read: %d bytes from the fifo\n",counter, ((fifo_dump_emos_UE*)fifo2file_ptr)->frame_rx,bytes);
      */

      fifo2file_ptr += channel_buffer_size;
      counter ++;

      if (counter == NO_ESTIMATES_DISK)
        {
          //reset stuff
          fifo2file_ptr = fifo2file_buffer;
          counter = 0;

          //flush buffer to disk
	  if (UE_flag==0)
	    printf("[EMOS] eNB: count %d, frame %d, flushing buffer to disk\n",
		   counter, ((fifo_dump_emos_eNB*)fifo2file_ptr)->frame_tx);
	  else
	    printf("[EMOS] UE: count %d, frame %d, flushing buffer to disk\n",
		   counter, ((fifo_dump_emos_UE*)fifo2file_ptr)->frame_rx);


          if (fwrite(fifo2file_buffer, sizeof(char), NO_ESTIMATES_DISK*channel_buffer_size, dumpfile_id) != NO_ESTIMATES_DISK*channel_buffer_size)
            {
              fprintf(stderr, "[EMOS] Error writing to dumpfile\n");
              exit(EXIT_FAILURE);
            }
	  if (gps_data)
	    {
	      if (gps_poll(gps_data) != 0) {
		printf("[EMOS] problem polling data from gps\n");
	      }
	      else {
		printf("[EMOS] lat %g, lon %g\n",gps_data->fix.latitude,gps_data->fix.longitude);
	      }
	      if (fwrite(&(gps_data->fix), sizeof(char), sizeof(struct gps_fix_t), dumpfile_id) != sizeof(struct gps_fix_t))
		{
		  printf("[EMOS] Error writing to dumpfile, stopping recording\n");
		  exit(EXIT_FAILURE);
		}
	    }
	  else
	    {
	      printf("[EMOS] WARNING: No GPS data available, storing dummy packet\n");
	      if (fwrite(&(dummy_gps_data), sizeof(char), sizeof(struct gps_fix_t), dumpfile_id) != sizeof(struct gps_fix_t))
		{
		  printf("[EMOS] Error writing to dumpfile, stopping recording\n");
		  exit(EXIT_FAILURE);
		}
	    } 
        }
    }
  
  free(fifo2file_buffer);
  fclose(dumpfile_id);
  close(fifo);
  
  pthread_exit((void*) arg);

}
#endif

717 718


719
#if defined(ENABLE_ITTI)
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
static void wait_system_ready (char *message, volatile int *start_flag)
{
  /* Wait for eNB application initialization to be complete (eNB registration to MME) */
  {
    static char *indicator[] = {".    ", "..   ", "...  ", ".... ", ".....",
                                " ....", "  ...", "   ..", "    .", "     "};
    int i = 0;

    while ((!oai_exit) && (*start_flag == 0)) {
      LOG_N(EMU, message, indicator[i]);
      i = (i + 1) % (sizeof(indicator) / sizeof(indicator[0]));
      usleep(200000);
    }
    LOG_D(EMU,"\n");
  }
}
#endif

Raymond Knopp's avatar
 
Raymond Knopp committed
738
#if defined(ENABLE_ITTI)
739
void *l2l1_task(void *arg)
740
{
Raymond Knopp's avatar
 
Raymond Knopp committed
741 742 743 744 745
  MessageDef *message_p = NULL;
  int         result;

  itti_set_task_real_time(TASK_L2L1);
  itti_mark_task_ready(TASK_L2L1);
746

Raymond Knopp's avatar
 
Raymond Knopp committed
747 748
  if (UE_flag == 0) {
    /* Wait for the initialize message */
749
    do {
Raymond Knopp's avatar
 
Raymond Knopp committed
750 751 752 753
      if (message_p != NULL) {
	result = itti_free (ITTI_MSG_ORIGIN_ID(message_p), message_p);
	AssertFatal (result == EXIT_SUCCESS, "Failed to free memory (%d)!\n", result);
      }
754 755 756
      itti_receive_msg (TASK_L2L1, &message_p);

      switch (ITTI_MSG_ID(message_p)) {
Raymond Knopp's avatar
 
Raymond Knopp committed
757 758 759 760 761 762 763
      case INITIALIZE_MESSAGE:
	/* Start eNB thread */
	LOG_D(EMU, "L2L1 TASK received %s\n", ITTI_MSG_NAME(message_p));
	start_eNB = 1;
	break;

      case TERMINATE_MESSAGE:
Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
764
	printf("received terminate message\n");
Raymond Knopp's avatar
 
Raymond Knopp committed
765 766 767 768 769 770 771
	oai_exit=1;
	itti_exit_task ();
	break;

      default:
	LOG_E(EMU, "Received unexpected message %s\n", ITTI_MSG_NAME(message_p));
	break;
772
      }
Raymond Knopp's avatar
 
Raymond Knopp committed
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
    } while (ITTI_MSG_ID(message_p) != INITIALIZE_MESSAGE);
    result = itti_free (ITTI_MSG_ORIGIN_ID(message_p), message_p);
    AssertFatal (result == EXIT_SUCCESS, "Failed to free memory (%d)!\n", result);
  }

  do {
    // Wait for a message
    itti_receive_msg (TASK_L2L1, &message_p);

    switch (ITTI_MSG_ID(message_p)) {
    case TERMINATE_MESSAGE:
      oai_exit=1;
      itti_exit_task ();
      break;

    case ACTIVATE_MESSAGE:
      start_UE = 1;
      break;

    case DEACTIVATE_MESSAGE:
      start_UE = 0;
      break;
795

Raymond Knopp's avatar
 
Raymond Knopp committed
796 797 798 799 800 801 802 803 804 805 806 807
    case MESSAGE_TEST:
      LOG_I(EMU, "Received %s\n", ITTI_MSG_NAME(message_p));
      break;

    default:
      LOG_E(EMU, "Received unexpected message %s\n", ITTI_MSG_NAME(message_p));
      break;
    }

    result = itti_free (ITTI_MSG_ORIGIN_ID(message_p), message_p);
    AssertFatal (result == EXIT_SUCCESS, "Failed to free memory (%d)!\n", result);
  } while(1);
808

Raymond Knopp's avatar
 
Raymond Knopp committed
809
  return NULL;
810 811 812
}
#endif

Raymond Knopp's avatar
 
Raymond Knopp committed
813

Raymond Knopp's avatar
 
Raymond Knopp committed
814
void do_OFDM_mod_rt(int subframe,PHY_VARS_eNB *phy_vars_eNB) {
Raymond Knopp's avatar
 
Raymond Knopp committed
815 816 817 818

  unsigned int aa,slot_offset, slot_offset_F;
  int dummy_tx_b[7680*4] __attribute__((aligned(16)));
  int i, tx_offset;
Raymond Knopp's avatar
 
Raymond Knopp committed
819
  int slot_sizeF = (phy_vars_eNB->lte_frame_parms.ofdm_symbol_size)*
Raymond Knopp's avatar
 
Raymond Knopp committed
820
    ((phy_vars_eNB->lte_frame_parms.Ncp==1) ? 6 : 7);
Raymond Knopp's avatar
 
Raymond Knopp committed
821 822 823

  slot_offset_F = (subframe<<1)*slot_sizeF;
    
Raymond Knopp's avatar
 
Raymond Knopp committed
824 825
  slot_offset = subframe*phy_vars_eNB->lte_frame_parms.samples_per_tti;

Raymond Knopp's avatar
 
Raymond Knopp committed
826 827
  if ((subframe_select(&phy_vars_eNB->lte_frame_parms,subframe)==SF_DL)||
      ((subframe_select(&phy_vars_eNB->lte_frame_parms,subframe)==SF_S))) {
Raymond Knopp's avatar
 
Raymond Knopp committed
828
    //	  LOG_D(HW,"Frame %d: Generating slot %d\n",frame,next_slot);
Raymond Knopp's avatar
 
Raymond Knopp committed
829

Raymond Knopp's avatar
 
Raymond Knopp committed
830
    
Raymond Knopp's avatar
 
Raymond Knopp committed
831 832 833 834 835 836 837 838 839 840
    for (aa=0; aa<phy_vars_eNB->lte_frame_parms.nb_antennas_tx; aa++) {
      if (phy_vars_eNB->lte_frame_parms.Ncp == EXTENDED){ 
	PHY_ofdm_mod(&phy_vars_eNB->lte_eNB_common_vars.txdataF[0][aa][slot_offset_F],
		     dummy_tx_b,
		     phy_vars_eNB->lte_frame_parms.log2_symbol_size,
		     6,
		     phy_vars_eNB->lte_frame_parms.nb_prefix_samples,
		     phy_vars_eNB->lte_frame_parms.twiddle_ifft,
		     phy_vars_eNB->lte_frame_parms.rev,
		     CYCLIC_PREFIX);
Raymond Knopp's avatar
 
Raymond Knopp committed
841 842 843 844 845 846 847 848
	PHY_ofdm_mod(&phy_vars_eNB->lte_eNB_common_vars.txdataF[0][aa][slot_offset_F+slot_sizeF],
		     dummy_tx_b+(phy_vars_eNB->lte_frame_parms.samples_per_tti>>1),
		     phy_vars_eNB->lte_frame_parms.log2_symbol_size,
		     6,
		     phy_vars_eNB->lte_frame_parms.nb_prefix_samples,
		     phy_vars_eNB->lte_frame_parms.twiddle_ifft,
		     phy_vars_eNB->lte_frame_parms.rev,
		     CYCLIC_PREFIX);
Raymond Knopp's avatar
 
Raymond Knopp committed
849 850 851 852 853 854
      }
      else {
	normal_prefix_mod(&phy_vars_eNB->lte_eNB_common_vars.txdataF[0][aa][slot_offset_F],
			  dummy_tx_b,
			  7,
			  &(phy_vars_eNB->lte_frame_parms));
Raymond Knopp's avatar
 
Raymond Knopp committed
855 856 857 858
	normal_prefix_mod(&phy_vars_eNB->lte_eNB_common_vars.txdataF[0][aa][slot_offset_F+slot_sizeF],
			  dummy_tx_b+(phy_vars_eNB->lte_frame_parms.samples_per_tti>>1),
			  7,
			  &(phy_vars_eNB->lte_frame_parms));
Raymond Knopp's avatar
 
Raymond Knopp committed
859
      }
Raymond Knopp's avatar
 
Raymond Knopp committed
860 861

      for (i=0; i<phy_vars_eNB->lte_frame_parms.samples_per_tti; i++) {
Raymond Knopp's avatar
 
Raymond Knopp committed
862 863 864 865 866 867
	tx_offset = (int)slot_offset+time_offset[aa]+i;
	if (tx_offset<0)
	  tx_offset += LTE_NUMBER_OF_SUBFRAMES_PER_FRAME*phy_vars_eNB->lte_frame_parms.samples_per_tti;
	if (tx_offset>=(LTE_NUMBER_OF_SUBFRAMES_PER_FRAME*phy_vars_eNB->lte_frame_parms.samples_per_tti))
	  tx_offset -= LTE_NUMBER_OF_SUBFRAMES_PER_FRAME*phy_vars_eNB->lte_frame_parms.samples_per_tti;
	((short*)&phy_vars_eNB->lte_eNB_common_vars.txdata[0][aa][tx_offset])[0]=
Raymond Knopp's avatar
 
Raymond Knopp committed
868
#ifdef EXMIMO
Raymond Knopp's avatar
 
Raymond Knopp committed
869
	  ((short*)dummy_tx_b)[2*i]<<4;
Raymond Knopp's avatar
 
Raymond Knopp committed
870
#else
Lionel Gauthier's avatar
Lionel Gauthier committed
871
	  ((short*)dummy_tx_b)[2*i]<<5;
Raymond Knopp's avatar
 
Raymond Knopp committed
872
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
873
	((short*)&phy_vars_eNB->lte_eNB_common_vars.txdata[0][aa][tx_offset])[1]=
Raymond Knopp's avatar
 
Raymond Knopp committed
874
#ifdef EXMIMO
Raymond Knopp's avatar
 
Raymond Knopp committed
875
	  ((short*)dummy_tx_b)[2*i+1]<<4;
Raymond Knopp's avatar
 
Raymond Knopp committed
876
#else
Lionel Gauthier's avatar
Lionel Gauthier committed
877
	  ((short*)dummy_tx_b)[2*i+1]<<5;
Raymond Knopp's avatar
 
Raymond Knopp committed
878
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
879 880 881 882 883 884 885 886
      }
    }
  }
}


int eNB_thread_tx_status[10];
static void * eNB_thread_tx(void *param) {
Raymond Knopp's avatar
 
Raymond Knopp committed
887 888 889

  //unsigned long cpuid;
  eNB_proc_t *proc = (eNB_proc_t*)param;
Raymond Knopp's avatar
 
Raymond Knopp committed
890
  //  RTIME time_in,time_out;
Raymond Knopp's avatar
 
Raymond Knopp committed
891 892
#ifdef RTAI
  RT_TASK *task;
893
  char task_name[8];
894 895 896 897
#else 
#ifdef LOWLATENCY
  struct sched_attr attr;
  unsigned int flags = 0;
Raymond Knopp's avatar
 
Raymond Knopp committed
898
#endif
899
#endif 
Raymond Knopp's avatar
 
Raymond Knopp committed
900 901

 
Raymond Knopp's avatar
 
Raymond Knopp committed
902
  /*#if defined(ENABLE_ITTI)
Florian Kaltenberger's avatar
Florian Kaltenberger committed
903
  // Wait for eNB application initialization to be complete (eNB registration to MME)
Raymond Knopp's avatar
 
Raymond Knopp committed
904
  wait_system_ready ("Waiting for eNB application to be ready %s\r", &start_eNB);
Raymond Knopp's avatar
 
Raymond Knopp committed
905
  #endif*/
Raymond Knopp's avatar
 
Raymond Knopp committed
906

Raymond Knopp's avatar
 
Raymond Knopp committed
907
#ifdef RTAI
908
  sprintf(task_name,"TXC%dS%d",proc->CC_id,proc->subframe);
Raymond Knopp's avatar
 
Raymond Knopp committed
909 910 911
  task = rt_task_init_schmod(nam2num(task_name), 0, 0, 0, SCHED_FIFO, 0xF);

  if (task==NULL) {
Lionel Gauthier's avatar
Lionel Gauthier committed
912 913 914
  LOG_E(PHY,"[SCHED][eNB] Problem starting eNB_proc_TX thread_index %d (%s)!!!!\n",proc->subframe,task_name);
  return 0;
}
Raymond Knopp's avatar
 
Raymond Knopp committed
915
  else {
Lionel Gauthier's avatar
Lionel Gauthier committed
916 917 918 919 920
  LOG_I(PHY,"[SCHED][eNB] eNB TX thread CC %d SF %d started with id %p\n",
    proc->CC_id,
    proc->subframe,
    task);
}
Raymond Knopp's avatar
 
Raymond Knopp committed
921
#else
922 923 924 925 926 927
#ifdef LOWLATENCY
  attr.size = sizeof(attr);
  attr.sched_flags = 0;
  attr.sched_nice = 0;
  attr.sched_priority = 0;
  
Navid Nikaein's avatar
Navid Nikaein committed
928
  /* This creates a 1ms reservation every 10ms period*/
929
  attr.sched_policy = SCHED_DEADLINE;
Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
930 931 932
  attr.sched_runtime  = 0.9   *  1000000;  // each tx thread requires 1ms to finish its job
  attr.sched_deadline = 1   *  1000000; // each tx thread will finish within 1ms
  attr.sched_period   = 1   * 10000000; // each tx thread has a period of 10ms from the starting point
933 934
  
  if (sched_setattr(0, &attr, flags) < 0 ){
Lionel Gauthier's avatar
Lionel Gauthier committed
935 936 937
  perror("[SCHED] eNB tx thread: sched_setattr failed\n");
  exit(-1);
}
938
  LOG_I(HW,"[SCHED] eNB TX deadline thread %d(id %ld) started on CPU %d\n",
Lionel Gauthier's avatar
Lionel Gauthier committed
939
    proc->subframe, gettid(),sched_getcpu());
940 941
#else 
  LOG_I(HW,"[SCHED] eNB TX thread %d started on CPU %d\n",
Lionel Gauthier's avatar
Lionel Gauthier committed
942
    proc->subframe,sched_getcpu());
943 944
#endif

Raymond Knopp's avatar
 
Raymond Knopp committed
945 946 947 948 949 950 951 952 953 954 955 956
#endif

  mlockall(MCL_CURRENT | MCL_FUTURE);

  //rt_set_runnable_on_cpuid(task,1);
  //cpuid = rtai_cpuid();

#ifdef HARD_RT
  rt_make_hard_real_time();
#endif

  while (!oai_exit){
Raymond Knopp's avatar
 
Raymond Knopp committed
957
    
Lionel Gauthier's avatar
Lionel Gauthier committed
958
  vcd_signal_dumper_dump_function_by_name(VCD_SIGNAL_DUMPER_FUNCTIONS_eNB_PROC_TX0+(2*proc->subframe),0);
Raymond Knopp's avatar
 
Raymond Knopp committed
959
    
Lionel Gauthier's avatar
Lionel Gauthier committed
960 961
  //LOG_I(PHY,"Locking mutex for eNB proc %d (IC %d,mutex %p)\n",proc->subframe,proc->instance_cnt,&proc->mutex);
  //    printf("Locking mutex for eNB proc %d (subframe_tx %d))\n",proc->subframe,proc->instance_cnt_tx);
Raymond Knopp's avatar
 
Raymond Knopp committed
962

963 964
    if (pthread_mutex_lock(&proc->mutex_tx) != 0) {
      LOG_E(PHY,"[SCHED][eNB] error locking mutex for eNB TX proc %d\n",proc->subframe);
Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
965
      exit_fun("nothing to add");
966 967
    }
    else {
Raymond Knopp's avatar
 
Raymond Knopp committed
968
      
Lionel Gauthier's avatar
Lionel Gauthier committed
969 970 971
  while (proc->instance_cnt_tx < 0) {
  //	LOG_I(PHY,"Waiting and unlocking mutex for eNB proc %d (IC %d,lock %d)\n",proc->subframe,proc->instance_cnt,pthread_mutex_trylock(&proc->mutex));
  //printf("Waiting and unlocking mutex for eNB proc %d (subframe_tx %d)\n",proc->subframe,proc->instance_cnt_tx);
Raymond Knopp's avatar
 
Raymond Knopp committed
972
	
973 974 975 976 977
	pthread_cond_wait(&proc->cond_tx,&proc->mutex_tx);
      }
      //      LOG_I(PHY,"Waking up and unlocking mutex for eNB proc %d instance_cnt_tx %d\n",proc->subframe,proc->instance_cnt_tx);
      if (pthread_mutex_unlock(&proc->mutex_tx) != 0) {	
	LOG_E(PHY,"[SCHED][eNB] error unlocking mutex for eNB TX proc %d\n",proc->subframe);
Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
978
	exit_fun("nothing to add");
979 980 981 982 983
      }
    }
    vcd_signal_dumper_dump_function_by_name(VCD_SIGNAL_DUMPER_FUNCTIONS_eNB_PROC_TX0+(2*proc->subframe),1);    
    vcd_signal_dumper_dump_variable_by_name(VCD_SIGNAL_DUMPER_VARIABLES_FRAME_NUMBER_TX_ENB, proc->frame_tx);
    start_meas(&softmodem_stats_tx_sf[proc->subframe]);
984

Lionel Gauthier's avatar
Lionel Gauthier committed
985
  if (oai_exit) break;
Raymond Knopp's avatar
 
Raymond Knopp committed
986

Raymond Knopp's avatar
 
Raymond Knopp committed
987
    
Lionel Gauthier's avatar
Lionel Gauthier committed
988 989 990
  if ((((PHY_vars_eNB_g[0][proc->CC_id]->lte_frame_parms.frame_type == TDD)&&
    (subframe_select(&PHY_vars_eNB_g[0][proc->CC_id]->lte_frame_parms,proc->subframe_tx)==SF_DL))||
    (PHY_vars_eNB_g[0][proc->CC_id]->lte_frame_parms.frame_type == FDD))) {
ghaddab's avatar
ghaddab committed
991

Lionel Gauthier's avatar
Lionel Gauthier committed
992
  phy_procedures_eNB_TX(proc->subframe,PHY_vars_eNB_g[0][proc->CC_id],0,no_relay,NULL);
Raymond Knopp's avatar
 
Raymond Knopp committed
993
      
Lionel Gauthier's avatar
Lionel Gauthier committed
994 995 996 997
}
  if ((subframe_select(&PHY_vars_eNB_g[0][proc->CC_id]->lte_frame_parms,proc->subframe_tx)==SF_S)) {
  phy_procedures_eNB_TX(proc->subframe,PHY_vars_eNB_g[0][proc->CC_id],0,no_relay,NULL);
}
Raymond Knopp's avatar
 
Raymond Knopp committed
998
    
Lionel Gauthier's avatar
Lionel Gauthier committed
999
  do_OFDM_mod_rt(proc->subframe_tx,PHY_vars_eNB_g[0][proc->CC_id]);  
Raymond Knopp's avatar
 
Raymond Knopp committed
1000
    
Lionel Gauthier's avatar
Lionel Gauthier committed
1001 1002 1003 1004 1005
  if (pthread_mutex_lock(&proc->mutex_tx) != 0) {
  printf("[openair][SCHED][eNB] error locking mutex for eNB TX proc %d\n",proc->subframe);
}
  else {
  proc->instance_cnt_tx--;
Raymond Knopp's avatar
 
Raymond Knopp committed
1006
      
Lionel Gauthier's avatar
Lionel Gauthier committed
1007 1008 1009 1010
  if (pthread_mutex_unlock(&proc->mutex_tx) != 0) {	
  printf("[openair][SCHED][eNB] error unlocking mutex for eNB TX proc %d\n",proc->subframe);
}
}
Raymond Knopp's avatar
 
Raymond Knopp committed
1011

Raymond Knopp's avatar
 
Raymond Knopp committed
1012
    
Lionel Gauthier's avatar
Lionel Gauthier committed
1013 1014 1015
  proc->frame_tx++;
  if (proc->frame_tx==1024)
    proc->frame_tx=0;
Raymond Knopp's avatar
 
Raymond Knopp committed
1016

Lionel Gauthier's avatar
Lionel Gauthier committed
1017
}    
1018
  stop_meas(&softmodem_stats_tx_sf[proc->subframe]);
Raymond Knopp's avatar
 
Raymond Knopp committed
1019 1020 1021 1022
  vcd_signal_dumper_dump_function_by_name(VCD_SIGNAL_DUMPER_FUNCTIONS_eNB_PROC_TX0+(2*proc->subframe),0);        
#ifdef HARD_RT
  rt_make_soft_real_time();
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
1023
  
Raymond Knopp's avatar
 
Raymond Knopp committed
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
#ifdef DEBUG_THREADS
  printf("Exiting eNB thread TX %d\n",proc->subframe);
#endif
  // clean task
#ifdef RTAI
  rt_task_delete(task);
#else
  eNB_thread_tx_status[proc->subframe]=0;
  pthread_exit(&eNB_thread_tx_status[proc->subframe]);
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
1034
  
Raymond Knopp's avatar
 
Raymond Knopp committed
1035 1036 1037 1038 1039
#ifdef DEBUG_THREADS
  printf("Exiting eNB TX thread %d\n",proc->subframe);
#endif
}

Lionel Gauthier's avatar
Lionel Gauthier committed
1040 1041
  int eNB_thread_rx_status[10];
  static void * eNB_thread_rx(void *param) {
Raymond Knopp's avatar
 
Raymond Knopp committed
1042 1043 1044 1045 1046 1047

  //unsigned long cpuid;
  eNB_proc_t *proc = (eNB_proc_t*)param;
  //  RTIME time_in,time_out;
#ifdef RTAI
  RT_TASK *task;
1048
  char task_name[8];
1049 1050 1051 1052 1053
#else
#ifdef LOWLATENCY
  struct sched_attr attr;
  unsigned int flags = 0;
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
1054 1055
#endif

Raymond Knopp's avatar
 
Raymond Knopp committed
1056
  /*#if defined(ENABLE_ITTI)
Florian Kaltenberger's avatar
Florian Kaltenberger committed
1057
  // Wait for eNB application initialization to be complete (eNB registration to MME) 
Raymond Knopp's avatar
 
Raymond Knopp committed
1058
  wait_system_ready ("Waiting for eNB application to be ready %s\r", &start_eNB);
Raymond Knopp's avatar
 
Raymond Knopp committed
1059
  #endif*/
Raymond Knopp's avatar
 
Raymond Knopp committed
1060 1061

#ifdef RTAI
1062
  sprintf(task_name,"RXC%1dS%1d",proc->CC_id,proc->subframe);
Raymond Knopp's avatar
 
Raymond Knopp committed
1063 1064 1065
  task = rt_task_init_schmod(nam2num(task_name), 0, 0, 0, SCHED_FIFO, 0xF);

  if (task==NULL) {
Lionel Gauthier's avatar
Lionel Gauthier committed
1066 1067 1068
  LOG_E(PHY,"[SCHED][eNB] Problem starting eNB_proc_RX thread_index %d (%s)!!!!\n",proc->subframe,task_name);
  return 0;
}
Raymond Knopp's avatar
 
Raymond Knopp committed
1069
  else {
1070 1071
    LOG_I(PHY,"[SCHED][eNB] eNB RX thread CC_id %d SF %d started with id %p\n", /*  on CPU %d*/
	  proc->CC_id,
Raymond Knopp's avatar
 
Raymond Knopp committed
1072
	  proc->subframe,
1073
	  task); /*,rtai_cpuid()*/
Raymond Knopp's avatar
 
Raymond Knopp committed
1074 1075
  }
#else
1076 1077 1078 1079 1080 1081
#ifdef LOWLATENCY
  attr.size = sizeof(attr);
  attr.sched_flags = 0;
  attr.sched_nice = 0;
  attr.sched_priority = 0;
  
Navid Nikaein's avatar
Navid Nikaein committed
1082
  /* This creates a 2ms reservation every 10ms period*/
1083
  attr.sched_policy = SCHED_DEADLINE;
Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
1084 1085 1086
  attr.sched_runtime  = 0.9   *  1000000;  // each rx thread must finish its job in the worst case in 2ms
  attr.sched_deadline = 1   *  1000000; // each rx thread will finish within 2ms
  attr.sched_period   = 1   * 10000000; // each rx thread has a period of 10ms from the starting point
1087 1088 1089 1090 1091 1092
  
  if (sched_setattr(0, &attr, flags) < 0 ){
    perror("[SCHED] eNB RX sched_setattr failed\n");
    exit(-1);
  }
  LOG_I(HW,"[SCHED] eNB RX deadline thread %d(id %ld) started on CPU %d\n",
Raymond Knopp's avatar
 
Raymond Knopp committed
1093
	proc->subframe, gettid(),sched_getcpu());
1094 1095
#else 
  LOG_I(HW,"[SCHED][eNB] eNB RX thread %d started on CPU %d\n",
Raymond Knopp's avatar
 
Raymond Knopp committed
1096
	proc->subframe,sched_getcpu());
1097 1098
#endif
 
Raymond Knopp's avatar
 
Raymond Knopp committed
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
#endif

  mlockall(MCL_CURRENT | MCL_FUTURE);

  //rt_set_runnable_on_cpuid(task,1);
  //cpuid = rtai_cpuid();

#ifdef HARD_RT
  rt_make_hard_real_time();
#endif

  while (!oai_exit){

    vcd_signal_dumper_dump_function_by_name(VCD_SIGNAL_DUMPER_FUNCTIONS_eNB_PROC_RX0+(2*proc->subframe),0);
Raymond Knopp's avatar
 
Raymond Knopp committed
1113
    
ghaddab's avatar
ghaddab committed
1114
    // LOG_I(PHY,"Locking mutex for eNB proc %d (IC %d,mutex %p)\n",proc->subframe,proc->instance_cnt_rx,&proc->mutex_rx);
Raymond Knopp's avatar
 
Raymond Knopp committed
1115 1116 1117 1118 1119 1120
    if (pthread_mutex_lock(&proc->mutex_rx) != 0) {
      LOG_E(PHY,"[SCHED][eNB] error locking mutex for eNB RX proc %d\n",proc->subframe);
    }
    else {
        
      while (proc->instance_cnt_rx < 0) {
ghaddab's avatar
ghaddab committed
1121
	//	LOG_I(PHY,"Waiting and unlocking mutex for eNB proc %d (IC %d,lock %d)\n",proc->subframe,proc->instance_cnt_rx,pthread_mutex_trylock(&proc->mutex_rx));
Raymond Knopp's avatar
 
Raymond Knopp committed
1122 1123

	pthread_cond_wait(&proc->cond_rx,&proc->mutex_rx);
Raymond Knopp's avatar
 
Raymond Knopp committed
1124
      }
ghaddab's avatar
ghaddab committed
1125
      //      LOG_I(PHY,"Waking up and unlocking mutex for eNB RX proc %d instance_cnt_rx %d\n",proc->subframe,proc->instance_cnt_rx);
Raymond Knopp's avatar
 
Raymond Knopp committed
1126 1127
      if (pthread_mutex_unlock(&proc->mutex_rx) != 0) {	
	LOG_E(PHY,"[SCHED][eNB] error unlocking mutex for eNB RX proc %d\n",proc->subframe);
Raymond Knopp's avatar
 
Raymond Knopp committed
1128 1129
      }
    }
Raymond Knopp's avatar
 
Raymond Knopp committed
1130
    vcd_signal_dumper_dump_function_by_name(VCD_SIGNAL_DUMPER_FUNCTIONS_eNB_PROC_RX0+(2*proc->subframe),1);    
Raymond Knopp's avatar
 
Raymond Knopp committed
1131
    vcd_signal_dumper_dump_variable_by_name(VCD_SIGNAL_DUMPER_VARIABLES_FRAME_NUMBER_RX_ENB, proc->frame_rx);
1132 1133
    start_meas(&softmodem_stats_rx_sf[proc->subframe]);
    
Raymond Knopp's avatar
 
Raymond Knopp committed
1134 1135
    if (oai_exit) break;
    
Raymond Knopp's avatar
 
Raymond Knopp committed
1136 1137 1138
    if ((((PHY_vars_eNB_g[0][proc->CC_id]->lte_frame_parms.frame_type == TDD )&&(subframe_select(&PHY_vars_eNB_g[0][proc->CC_id]->lte_frame_parms,proc->subframe_rx)==SF_UL)) ||
	 (PHY_vars_eNB_g[0][proc->CC_id]->lte_frame_parms.frame_type == FDD))){
      phy_procedures_eNB_RX(proc->subframe,PHY_vars_eNB_g[0][proc->CC_id],0,no_relay);
Raymond Knopp's avatar
 
Raymond Knopp committed
1139
    }
Raymond Knopp's avatar
 
Raymond Knopp committed
1140 1141
    if ((subframe_select(&PHY_vars_eNB_g[0][proc->CC_id]->lte_frame_parms,proc->subframe_rx)==SF_S)){
      phy_procedures_eNB_S_RX(proc->subframe,PHY_vars_eNB_g[0][proc->CC_id],0,no_relay);
Raymond Knopp's avatar
 
Raymond Knopp committed
1142 1143 1144 1145
    }
      
    if (pthread_mutex_lock(&proc->mutex_rx) != 0) {
      printf("[openair][SCHED][eNB] error locking mutex for eNB RX proc %d\n",proc->subframe);
Raymond Knopp's avatar
 
Raymond Knopp committed
1146 1147
    }
    else {
Raymond Knopp's avatar
 
Raymond Knopp committed
1148
      proc->instance_cnt_rx--;
Raymond Knopp's avatar
 
Raymond Knopp committed
1149
      
Raymond Knopp's avatar
 
Raymond Knopp committed
1150 1151
      if (pthread_mutex_unlock(&proc->mutex_rx) != 0) {	
	printf("[openair][SCHED][eNB] error unlocking mutex for eNB RX proc %d\n",proc->subframe);
Raymond Knopp's avatar
 
Raymond Knopp committed
1152 1153
      }
    }
Raymond Knopp's avatar
 
Raymond Knopp committed
1154 1155 1156 1157

    proc->frame_rx++;
    if (proc->frame_rx==1024)
      proc->frame_rx=0;
Raymond Knopp's avatar
 
Raymond Knopp committed
1158
    
Raymond Knopp's avatar
 
Raymond Knopp committed
1159
  }
1160
  stop_meas(&softmodem_stats_rx_sf[proc->subframe]);
Raymond Knopp's avatar
 
Raymond Knopp committed
1161
  vcd_signal_dumper_dump_function_by_name(VCD_SIGNAL_DUMPER_FUNCTIONS_eNB_PROC_RX0+(2*proc->subframe),0);        
Raymond Knopp's avatar
 
Raymond Knopp committed
1162
#ifdef HARD_RT
Raymond Knopp's avatar
 
Raymond Knopp committed
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
  rt_make_soft_real_time();
#endif

#ifdef DEBUG_THREADS
  printf("Exiting eNB thread RX %d\n",proc->subframe);
#endif
  // clean task
#ifdef RTAI
  rt_task_delete(task);
#else
  eNB_thread_rx_status[proc->subframe]=0;
  pthread_exit(&eNB_thread_rx_status[proc->subframe]);
Raymond Knopp's avatar
 
Raymond Knopp committed
1175 1176
#endif

Raymond Knopp's avatar
 
Raymond Knopp committed
1177 1178 1179
#ifdef DEBUG_THREADS
  printf("Exiting eNB RX thread %d\n",proc->subframe);
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
1180 1181 1182 1183
}



Raymond Knopp's avatar
 
Raymond Knopp committed
1184

1185
void init_eNB_proc(void) {
Raymond Knopp's avatar
 
Raymond Knopp committed
1186 1187

  int i;
Raymond Knopp's avatar
 
Raymond Knopp committed
1188 1189 1190 1191
  int CC_id;

  for (CC_id=0;CC_id<MAX_NUM_CCs;CC_id++) {
    for (i=0;i<10;i++) {
1192
      /*set the stack sizw */ 
Raymond Knopp's avatar
 
Raymond Knopp committed
1193
      pthread_attr_init (&attr_eNB_proc_tx[CC_id][i]);
Raymond Knopp's avatar
 
Raymond Knopp committed
1194
      if (pthread_attr_setstacksize(&attr_eNB_proc_tx[CC_id][i],PTHREAD_STACK_MIN) != 0)
1195 1196 1197
	perror("[ENB_PROC_TX] setting thread stack size failed\n");

      pthread_attr_init (&attr_eNB_proc_rx[CC_id][i]);
Raymond Knopp's avatar
 
Raymond Knopp committed
1198
      if (pthread_attr_setstacksize(&attr_eNB_proc_rx[CC_id][i],PTHREAD_STACK_MIN) != 0)
1199 1200 1201
	perror("[ENB_PROC_RX] setting thread stack size failed\n");
      /* set the kernel scheduling policy and priority */
#ifndef LOWLATENCY
Raymond Knopp's avatar
 
Raymond Knopp committed
1202 1203 1204 1205 1206 1207 1208 1209
      //attr_dlsch_threads.priority = 1;
      sched_param_eNB_proc_tx[CC_id][i].sched_priority = sched_get_priority_max(SCHED_FIFO)-1; //OPENAIR_THREAD_PRIORITY;
      pthread_attr_setschedparam  (&attr_eNB_proc_tx[CC_id][i], &sched_param_eNB_proc_tx[CC_id][i]);
      pthread_attr_setschedpolicy (&attr_eNB_proc_tx[CC_id][i], SCHED_FIFO);
      //attr_dlsch_threads.priority = 1;
      sched_param_eNB_proc_rx[CC_id][i].sched_priority = sched_get_priority_max(SCHED_FIFO)-1; //OPENAIR_THREAD_PRIORITY;
      pthread_attr_setschedparam  (&attr_eNB_proc_rx[CC_id][i], &sched_param_eNB_proc_rx[CC_id][i]);
      pthread_attr_setschedpolicy (&attr_eNB_proc_rx[CC_id][i], SCHED_FIFO);
1210
#endif       
Raymond Knopp's avatar
 
Raymond Knopp committed
1211
      
Raymond Knopp's avatar
 
Raymond Knopp committed
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
      PHY_vars_eNB_g[0][CC_id]->proc[i].instance_cnt_tx=-1;
      PHY_vars_eNB_g[0][CC_id]->proc[i].instance_cnt_rx=-1;
      PHY_vars_eNB_g[0][CC_id]->proc[i].subframe=i;
      PHY_vars_eNB_g[0][CC_id]->proc[i].CC_id = CC_id;
      pthread_mutex_init(&PHY_vars_eNB_g[0][CC_id]->proc[i].mutex_tx,NULL);
      pthread_mutex_init(&PHY_vars_eNB_g[0][CC_id]->proc[i].mutex_rx,NULL);
      pthread_cond_init(&PHY_vars_eNB_g[0][CC_id]->proc[i].cond_tx,NULL);
      pthread_cond_init(&PHY_vars_eNB_g[0][CC_id]->proc[i].cond_rx,NULL);
      pthread_create(&PHY_vars_eNB_g[0][CC_id]->proc[i].pthread_tx,NULL,eNB_thread_tx,(void*)&PHY_vars_eNB_g[0][CC_id]->proc[i]);
      pthread_create(&PHY_vars_eNB_g[0][CC_id]->proc[i].pthread_rx,NULL,eNB_thread_rx,(void*)&PHY_vars_eNB_g[0][CC_id]->proc[i]);
      PHY_vars_eNB_g[0][CC_id]->proc[i].frame_tx = 0;
      PHY_vars_eNB_g[0][CC_id]->proc[i].frame_rx = 0;
1224
#ifdef EXMIMO
Raymond Knopp's avatar
 
Raymond Knopp committed
1225 1226
      PHY_vars_eNB_g[0][CC_id]->proc[i].subframe_rx = (i+9)%10;
      PHY_vars_eNB_g[0][CC_id]->proc[i].subframe_tx = (i+1)%10;
Raymond Knopp's avatar
 
Raymond Knopp committed
1227
#else
Raymond Knopp's avatar
 
Raymond Knopp committed
1228 1229
      PHY_vars_eNB_g[0][CC_id]->proc[i].subframe_rx = i;
      PHY_vars_eNB_g[0][CC_id]->proc[i].subframe_tx = (i+2)%10;
Raymond Knopp's avatar
 
Raymond Knopp committed
1230
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
1231 1232 1233
    }
  
  
1234
#ifdef EXMIMO
Raymond Knopp's avatar
 
Raymond Knopp committed
1235 1236
    // TX processes subframe + 1, RX subframe -1
    // Note this inialization is because the first process awoken for frame 0 is number 1 and so processes 9 and 0 have to start with frame 1
Raymond Knopp's avatar
 
Raymond Knopp committed
1237
    
Raymond Knopp's avatar
 
Raymond Knopp committed
1238
    //PHY_vars_eNB_g[0][CC_id]->proc[0].frame_rx = 1023;
Raymond Knopp's avatar
 
Raymond Knopp committed
1239 1240
    PHY_vars_eNB_g[0][CC_id]->proc[9].frame_tx = 1;
    PHY_vars_eNB_g[0][CC_id]->proc[0].frame_tx = 1;
Raymond Knopp's avatar
 
Raymond Knopp committed
1241
#else
Raymond Knopp's avatar
 
Raymond Knopp committed
1242 1243
    // TX processes subframe +2, RX subframe
    // Note this inialization is because the first process awoken for frame 0 is number 1 and so processes 8,9 and 0 have to start with frame 1
Raymond Knopp's avatar
 
Raymond Knopp committed
1244
    //    PHY_vars_eNB_g[0][CC_id]->proc[7].frame_tx = 1;
Raymond Knopp's avatar
 
Raymond Knopp committed
1245 1246
    PHY_vars_eNB_g[0][CC_id]->proc[8].frame_tx = 1;
    PHY_vars_eNB_g[0][CC_id]->proc[9].frame_tx = 1;
Raymond Knopp's avatar
 
Raymond Knopp committed
1247
    //    PHY_vars_eNB_g[0][CC_id]->proc[0].frame_tx = 1;
Raymond Knopp's avatar
 
Raymond Knopp committed
1248
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
1249 1250
  }
}
Raymond Knopp's avatar
 
Raymond Knopp committed
1251

1252
void kill_eNB_proc(void) {
Raymond Knopp's avatar
 
Raymond Knopp committed
1253 1254

  int i;
1255
  void *status_tx,*status_rx;
Raymond Knopp's avatar
 
Raymond Knopp committed
1256
  int CC_id;
Raymond Knopp's avatar
 
Raymond Knopp committed
1257

Raymond Knopp's avatar
 
Raymond Knopp committed
1258 1259 1260
  for (CC_id=0;CC_id<MAX_NUM_CCs;CC_id++) 
    for (i=0;i<10;i++) {
      
Raymond Knopp's avatar
 
Raymond Knopp committed
1261
#ifdef DEBUG_THREADS
Raymond Knopp's avatar
 
Raymond Knopp committed
1262
      printf("Killing TX CC_id %d thread %d\n",CC_id,i);
Raymond Knopp's avatar
 
Raymond Knopp committed
1263
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
1264 1265
      PHY_vars_eNB_g[0][CC_id]->proc[i].instance_cnt_tx=0; 
      pthread_cond_signal(&PHY_vars_eNB_g[0][CC_id]->proc[i].cond_tx);
Raymond Knopp's avatar
 
Raymond Knopp committed
1266
#ifdef DEBUG_THREADS
1267
      printf("Joining eNB TX CC_id %d thread %d...\n",CC_id,i);
Raymond Knopp's avatar
 
Raymond Knopp committed
1268
#endif
1269
      pthread_join(PHY_vars_eNB_g[0][CC_id]->proc[i].pthread_tx,&status_tx);
Raymond Knopp's avatar
 
Raymond Knopp committed
1270
#ifdef DEBUG_THREADS
1271
      if (status_tx) printf("status %p...\n",status_tx);
Raymond Knopp's avatar
 
Raymond Knopp committed
1272 1273
#endif
#ifdef DEBUG_THREADS
Raymond Knopp's avatar
 
Raymond Knopp committed
1274
      printf("Killing RX CC_id %d thread %d\n",CC_id,i);
Raymond Knopp's avatar
 
Raymond Knopp committed
1275
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
1276 1277
      PHY_vars_eNB_g[0][CC_id]->proc[i].instance_cnt_rx=0; 
      pthread_cond_signal(&PHY_vars_eNB_g[0][CC_id]->proc[i].cond_rx);
Raymond Knopp's avatar
 
Raymond Knopp committed
1278
#ifdef DEBUG_THREADS
1279
      printf("Joining eNB RX CC_id %d thread %d...\n",CC_id,i);
Raymond Knopp's avatar
 
Raymond Knopp committed
1280
#endif
1281
      pthread_join(PHY_vars_eNB_g[0][CC_id]->proc[i].pthread_rx,&status_rx);
Raymond Knopp's avatar
 
Raymond Knopp committed
1282
#ifdef DEBUG_THREADS 
1283
      if (status_rx) printf("status %p...\n",status_rx);
Raymond Knopp's avatar
 
Raymond Knopp committed
1284
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
1285 1286 1287 1288 1289
      pthread_mutex_destroy(&PHY_vars_eNB_g[0][CC_id]->proc[i].mutex_tx);
      pthread_mutex_destroy(&PHY_vars_eNB_g[0][CC_id]->proc[i].mutex_rx);
      pthread_cond_destroy(&PHY_vars_eNB_g[0][CC_id]->proc[i].cond_tx);
      pthread_cond_destroy(&PHY_vars_eNB_g[0][CC_id]->proc[i].cond_rx);
    }
Raymond Knopp's avatar
 
Raymond Knopp committed
1290 1291 1292
}


Raymond Knopp's avatar
 
Raymond Knopp committed
1293 1294


Raymond Knopp's avatar
 
Raymond Knopp committed
1295 1296
  
/* This is the main eNB thread. */
Raymond Knopp's avatar
 
Raymond Knopp committed
1297 1298
int eNB_thread_status;

Raymond Knopp's avatar
 
Raymond Knopp committed
1299

Raymond Knopp's avatar
 
Raymond Knopp committed
1300

1301 1302 1303 1304
static void *eNB_thread(void *arg)
{
#ifdef RTAI
  RT_TASK *task;
1305 1306 1307 1308
#else 
#ifdef LOWLATENCY
  struct sched_attr attr;
  unsigned int flags = 0;
1309
//  unsigned long mask = 1; /* processor 0 */
1310
#endif
1311
#endif
1312

1313 1314
#ifdef EXMIMO
  unsigned char slot=0;
1315 1316
#else
  unsigned char slot=1;
Raymond Knopp's avatar
 
Raymond Knopp committed
1317
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
1318
  int frame=0;
Raymond Knopp's avatar
 
Raymond Knopp committed
1319
  int CC_id;
Raymond Knopp's avatar
 
Raymond Knopp committed
1320

1321
  RTIME time_diff;
Raymond Knopp's avatar
 
Raymond Knopp committed
1322

Raymond Knopp's avatar
 
Raymond Knopp committed
1323
  int sf;
1324
#ifdef EXMIMO
Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
1325
  RTIME time_in;
Raymond Knopp's avatar
 
Raymond Knopp committed
1326
  volatile unsigned int *DAQ_MBOX = openair0_daq_cnt();
Raymond Knopp's avatar
 
Raymond Knopp committed
1327 1328
  int mbox_target=0,mbox_current=0;
  int hw_slot,delay_cnt;
Raymond Knopp's avatar
 
Raymond Knopp committed
1329 1330
  int diff;
  int ret;
1331
  int first_run=1;
Raymond Knopp's avatar
 
Raymond Knopp committed
1332
#else
1333 1334 1335 1336 1337
  unsigned int rx_pos = 0;
  unsigned int tx_pos;
  int spp;
  int tx_launched=0;
 
Raymond Knopp's avatar
 
Raymond Knopp committed
1338
  //  int tx_offset;
Raymond Knopp's avatar
 
Raymond Knopp committed
1339 1340
  void *rxp[2],*txp[2];
  int i;
Raymond Knopp's avatar
 
Raymond Knopp committed
1341

1342 1343
  openair0_timestamp timestamp;

1344
  //  int trace_cnt=0;
Raymond Knopp's avatar
 
Raymond Knopp committed
1345
  hw_subframe = 0;
1346 1347
  spp = openair0_cfg[0].samples_per_packet;
  tx_pos = spp*tx_delay;
Raymond Knopp's avatar
 
Raymond Knopp committed
1348
#endif
1349

Raymond Knopp's avatar
 
Raymond Knopp committed
1350 1351
  struct timespec trx_time0,trx_time1,trx_time2;

1352
  /*
Raymond Knopp's avatar
 
Raymond Knopp committed
1353 1354 1355 1356
    #if defined(ENABLE_ITTI)
    // Wait for eNB application initialization to be complete (eNB registration to MME) 
    wait_system_ready ("Waiting for eNB application to be ready %s\r", &start_eNB);
    #endif
1357
  */
1358

1359
#ifdef RTAI
1360
  task = rt_task_init_schmod(nam2num("eNBmain"), 0, 0, 0, SCHED_FIFO, 0xF);
1361 1362
#else 
#ifdef LOWLATENCY
Raymond Knopp's avatar
 
Raymond Knopp committed
1363 1364 1365 1366
  attr.size = sizeof(attr);
  attr.sched_flags = 0;
  attr.sched_nice = 0;
  attr.sched_priority = 0;
1367
   
Raymond Knopp's avatar
 
Raymond Knopp committed
1368 1369
  /* This creates a .5 ms  reservation */
  attr.sched_policy = SCHED_DEADLINE;
Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
1370
  attr.sched_runtime  = 0.1 * 1000000;
Raymond Knopp's avatar
 
Raymond Knopp committed
1371
  attr.sched_deadline = 0.5 * 1000000;
Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
1372
  attr.sched_period   = 1.0   * 1000000;
Lionel Gauthier's avatar
Lionel Gauthier committed
1373

1374
   
Raymond Knopp's avatar
 
Raymond Knopp committed
1375 1376 1377 1378
  /* pin the eNB main thread to CPU0*/
  /* if (pthread_setaffinity_np(pthread_self(), sizeof(mask),&mask) <0) {
     perror("[MAIN_ENB_THREAD] pthread_setaffinity_np failed\n");
     }*/
1379
   
Raymond Knopp's avatar
 
Raymond Knopp committed
1380 1381 1382 1383 1384 1385 1386
  if (sched_setattr(0, &attr, flags) < 0 ){
    perror("[SCHED] main eNB thread: sched_setattr failed\n");
    exit_fun("Nothing to add");
  } else {
    LOG_I(HW,"[SCHED][eNB] eNB main deadline thread %ld started on CPU %d\n",
	  gettid(),sched_getcpu());
  }
1387
#endif
1388 1389 1390 1391
#endif

  if (!oai_exit) {
#ifdef RTAI
1392
    printf("[SCHED][eNB] Started eNB main thread (id %p)\n",task);
Raymond Knopp's avatar
 
Raymond Knopp committed
1393
#else
1394
    printf("[SCHED][eNB] Started eNB main thread on CPU %d\n",
Raymond Knopp's avatar
 
Raymond Knopp committed
1395
	   sched_getcpu());
1396 1397 1398
#endif

#ifdef HARD_RT
1399
    rt_make_hard_real_time();
1400 1401
#endif

Raymond Knopp's avatar
 
Raymond Knopp committed
1402
    printf("eNB_thread: mlockall in ...\n");
1403
    mlockall(MCL_CURRENT | MCL_FUTURE);
Raymond Knopp's avatar
 
Raymond Knopp committed
1404
    printf("eNB_thread: mlockall out ...\n");
1405

1406 1407 1408 1409
    timing_info.time_min = 100000000ULL;
    timing_info.time_max = 0;
    timing_info.time_avg = 0;
    timing_info.n_samples = 0;
1410

1411
    printf("waiting for sync (eNB_thread)\n");
Raymond Knopp's avatar
 
Raymond Knopp committed
1412
    pthread_mutex_lock(&sync_mutex);
1413 1414
    while (sync_var<0)
      pthread_cond_wait(&sync_cond, &sync_mutex);
Raymond Knopp's avatar
 
Raymond Knopp committed
1415
    pthread_mutex_unlock(&sync_mutex);
Raymond Knopp's avatar
 
Raymond Knopp committed
1416

Raymond Knopp's avatar
 
Raymond Knopp committed
1417
    while (!oai_exit) {
1418
      start_meas(&softmodem_stats_mt);
1419
#ifdef EXMIMO
Raymond Knopp's avatar
 
Raymond Knopp committed
1420 1421
      hw_slot = (((((volatile unsigned int *)DAQ_MBOX)[0]+1)%150)<<1)/15;
      //        LOG_D(HW,"eNB frame %d, time %llu: slot %d, hw_slot %d (mbox %d)\n",frame,rt_get_time_ns(),slot,hw_slot,((unsigned int *)DAQ_MBOX)[0]);
Raymond Knopp's avatar
 
Raymond Knopp committed
1422 1423
      vcd_signal_dumper_dump_variable_by_name(VCD_SIGNAL_DUMPER_VARIABLES_HW_SUBFRAME, hw_slot>>1);
      vcd_signal_dumper_dump_variable_by_name(VCD_SIGNAL_DUMPER_VARIABLES_HW_FRAME, frame);
Raymond Knopp's avatar
 
Raymond Knopp committed
1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435
      //this is the mbox counter where we should be
      mbox_target = mbox_bounds[slot];
      //this is the mbox counter where we are
      mbox_current = ((volatile unsigned int *)DAQ_MBOX)[0];
      //this is the time we need to sleep in order to synchronize with the hw (in multiples of DAQ_PERIOD)
      if ((mbox_current>=135) && (mbox_target<15)) //handle the frame wrap-arround
	diff = 150-mbox_current+mbox_target;
      else if ((mbox_current<15) && (mbox_target>=135))
	diff = -150+mbox_target-mbox_current;
      else
	diff = mbox_target - mbox_current;
      
1436 1437 1438 1439 1440
      //when we start the aquisition we want to start with slot 0, so we rather wait for the hardware than to advance the slot number (a positive diff will cause the programm to go into the second if clause rather than the first)
      if (first_run==1) {
	first_run=0;
	if (diff<0)
	  diff = diff +150;
1441
	LOG_I(HW,"eNB Frame %d, time %llu: slot %d, hw_slot %d, diff %d\n",frame, rt_get_time_ns(), slot, hw_slot, diff);
1442 1443
      } 

Raymond Knopp's avatar
 
Raymond Knopp committed
1444 1445
      if (((slot%2==0) && (diff < (-14))) || ((slot%2==1) && (diff < (-7)))) {
	// at the eNB, even slots have double as much time since most of the processing is done here and almost nothing in odd slots
1446
	LOG_D(HW,"eNB Frame %d, time %llu: missed slot, proceeding with next one (slot %d, hw_slot %d, mbox_current %d, mbox_target %d, diff %d)\n",frame, rt_get_time_ns(), slot, hw_slot, mbox_current, mbox_target, diff);
Raymond Knopp's avatar
 
Raymond Knopp committed
1447
	slot++;
1448 1449 1450 1451 1452 1453 1454
	if (exit_missed_slots==1){
	  stop_meas(&softmodem_stats_mt);
	  exit_fun("[HW][eNB] missed slot");
	}else{
	  num_missed_slots++;
	  LOG_W(HW,"[eNB] just missed slot (total missed slots %ld)\n", num_missed_slots);
	}
Raymond Knopp's avatar
 
Raymond Knopp committed
1455 1456 1457 1458
      }
      if (diff>8)
	LOG_D(HW,"eNB Frame %d, time %llu: skipped slot, waiting for hw to catch up (slot %d, hw_slot %d, mbox_current %d, mbox_target %d, diff %d)\n",frame, rt_get_time_ns(), slot, hw_slot, mbox_current, mbox_target, diff);
      
Raymond Knopp's avatar
 
Raymond Knopp committed
1459
      vcd_signal_dumper_dump_variable_by_name(VCD_SIGNAL_DUMPER_VARIABLES_DAQ_MBOX, *DAQ_MBOX);
Raymond Knopp's avatar
 
Raymond Knopp committed
1460 1461 1462 1463 1464 1465 1466 1467 1468 1469
      vcd_signal_dumper_dump_variable_by_name(VCD_SIGNAL_DUMPER_VARIABLES_DIFF, diff);
      
      delay_cnt = 0;
      while ((diff>0) && (!oai_exit)) {
	time_in = rt_get_time_ns();
	//LOG_D(HW,"eNB Frame %d delaycnt %d : hw_slot %d (%d), slot %d, (slot+1)*15=%d, diff %d, time %llu\n",frame,delay_cnt,hw_slot,((unsigned int *)DAQ_MBOX)[0],slot,(((slot+1)*15)>>1),diff,time_in);
	//LOG_D(HW,"eNB Frame %d, time %llu: sleeping for %llu (slot %d, hw_slot %d, diff %d, mbox %d, delay_cnt %d)\n", frame, time_in, diff*DAQ_PERIOD,slot,hw_slot,diff,((volatile unsigned int *)DAQ_MBOX)[0],delay_cnt);
	vcd_signal_dumper_dump_function_by_name(VCD_SIGNAL_DUMPER_FUNCTIONS_RT_SLEEP,1);
	ret = rt_sleep_ns(diff*DAQ_PERIOD);
	vcd_signal_dumper_dump_function_by_name(VCD_SIGNAL_DUMPER_FUNCTIONS_RT_SLEEP,0);
Raymond Knopp's avatar
 
Raymond Knopp committed
1470
	vcd_signal_dumper_dump_variable_by_name(VCD_SIGNAL_DUMPER_VARIABLES_DAQ_MBOX, *DAQ_MBOX);
Raymond Knopp's avatar
 
Raymond Knopp committed
1471 1472 1473 1474 1475 1476
	if (ret)
	  LOG_D(HW,"eNB Frame %d, time %llu: rt_sleep_ns returned %d\n",frame, time_in);
	hw_slot = (((((volatile unsigned int *)DAQ_MBOX)[0]+1)%150)<<1)/15;
	//LOG_D(HW,"eNB Frame %d : hw_slot %d, time %llu\n",frame,hw_slot,rt_get_time_ns());
	delay_cnt++;
	if (delay_cnt == 10) {
1477
	  stop_meas(&softmodem_stats_mt);
Raymond Knopp's avatar
 
Raymond Knopp committed
1478 1479 1480 1481 1482
	  LOG_D(HW,"eNB Frame %d: HW stopped ... \n",frame);
	  exit_fun("[HW][eNB] HW stopped");
	}
	mbox_current = ((volatile unsigned int *)DAQ_MBOX)[0];
	if ((mbox_current>=135) && (mbox_target<15)) //handle the frame wrap-arround
Raymond Knopp's avatar
 
Raymond Knopp committed
1483
	  diff = 150-mbox_current+mbox_target;
Raymond Knopp's avatar
 
Raymond Knopp committed
1484
	else if ((mbox_current<15) && (mbox_target>=135))
Raymond Knopp's avatar
 
Raymond Knopp committed
1485
	  diff = -150+mbox_target-mbox_current;
Raymond Knopp's avatar
 
Raymond Knopp committed
1486
	else
Raymond Knopp's avatar
 
Raymond Knopp committed
1487
	  diff = mbox_target - mbox_current;
Raymond Knopp's avatar
 
Raymond Knopp committed
1488
      }
Raymond Knopp's avatar
 
Raymond Knopp committed
1489

1490
#else  // EXMIMO
Raymond Knopp's avatar
 
Raymond Knopp committed
1491 1492
      vcd_signal_dumper_dump_variable_by_name(VCD_SIGNAL_DUMPER_VARIABLES_HW_SUBFRAME, hw_subframe);
      vcd_signal_dumper_dump_variable_by_name(VCD_SIGNAL_DUMPER_VARIABLES_HW_FRAME, frame);
1493 1494
      tx_launched = 0;
      while (rx_pos < ((1+hw_subframe)*PHY_vars_eNB_g[0][0]->lte_frame_parms.samples_per_tti)) {
Raymond Knopp's avatar
 
Raymond Knopp committed
1495

1496
	//	openair0_timestamp time0,time1;
Raymond Knopp's avatar
 
Raymond Knopp committed
1497
	unsigned int rxs;
Raymond Knopp's avatar
 
Raymond Knopp committed
1498
#ifndef USRP_DEBUG
Raymond Knopp's avatar
 
Raymond Knopp committed
1499
	vcd_signal_dumper_dump_function_by_name(VCD_SIGNAL_DUMPER_FUNCTIONS_TRX_READ,1);
1500 1501
	vcd_signal_dumper_dump_variable_by_name(VCD_SIGNAL_DUMPER_VARIABLES_TXCNT,tx_pos);
	vcd_signal_dumper_dump_variable_by_name(VCD_SIGNAL_DUMPER_VARIABLES_RXCNT,rx_pos);
1502

Raymond Knopp's avatar
 
Raymond Knopp committed
1503

Raymond Knopp's avatar
 
Raymond Knopp committed
1504
	clock_gettime(CLOCK_MONOTONIC,&trx_time0);
Raymond Knopp's avatar
 
Raymond Knopp committed
1505
	for (i=0;i<PHY_vars_eNB_g[0][0]->lte_frame_parms.nb_antennas_rx;i++)
1506
	  rxp[i] = (void*)&rxdata[i][rx_pos];
1507
	start_meas(&softmodem_stats_hw);
1508 1509
	//	printf("rxp[0] %p\n",rxp[0]);

Raymond Knopp's avatar
 
Raymond Knopp committed
1510 1511
	rxs = openair0.trx_read_func(&openair0, 
				     &timestamp, 
Raymond Knopp's avatar
 
Raymond Knopp committed
1512
				     rxp, 
1513
				     spp,
Raymond Knopp's avatar
 
Raymond Knopp committed
1514
				     PHY_vars_eNB_g[0][0]->lte_frame_parms.nb_antennas_rx);
1515
	stop_meas(&softmodem_stats_hw);
Raymond Knopp's avatar
 
Raymond Knopp committed
1516
	clock_gettime(CLOCK_MONOTONIC,&trx_time1);
ghaddab's avatar
ghaddab committed
1517

1518
	if (rxs != spp)
Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
1519
	  exit_fun("problem receiving samples");
Raymond Knopp's avatar
 
Raymond Knopp committed
1520
 
Raymond Knopp's avatar
 
Raymond Knopp committed
1521

Raymond Knopp's avatar
 
Raymond Knopp committed
1522 1523
	vcd_signal_dumper_dump_function_by_name(VCD_SIGNAL_DUMPER_FUNCTIONS_TRX_READ,0);

Raymond Knopp's avatar
 
Raymond Knopp committed
1524
	// Transmit TX buffer based on timestamp from RX
Raymond Knopp's avatar
 
Raymond Knopp committed
1525
	vcd_signal_dumper_dump_function_by_name(VCD_SIGNAL_DUMPER_FUNCTIONS_TRX_WRITE,1);
Raymond Knopp's avatar
 
Raymond Knopp committed
1526 1527
	
	for (i=0;i<PHY_vars_eNB_g[0][0]->lte_frame_parms.nb_antennas_tx;i++)
1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
	  txp[i] = (void*)&txdata[i][tx_pos];
	if (frame > 50) {
	  openair0.trx_write_func(&openair0, 
				  (timestamp+(tx_delay*spp)-tx_forward_nsamps), 
				  txp,
				  spp, 
				  PHY_vars_eNB_g[0][0]->lte_frame_parms.nb_antennas_tx,
				  1);
	}

	vcd_signal_dumper_dump_variable_by_name(VCD_SIGNAL_DUMPER_VARIABLES_TRX_TS,timestamp&0xffffffff);
	vcd_signal_dumper_dump_variable_by_name(VCD_SIGNAL_DUMPER_VARIABLES_TRX_TST,(timestamp+(tx_delay*spp)-tx_forward_nsamps)&0xffffffff);


1542
	stop_meas(&softmodem_stats_mt);
Raymond Knopp's avatar
 
Raymond Knopp committed
1543 1544
	clock_gettime(CLOCK_MONOTONIC,&trx_time2);

Raymond Knopp's avatar
 
Raymond Knopp committed
1545
	vcd_signal_dumper_dump_function_by_name(VCD_SIGNAL_DUMPER_FUNCTIONS_TRX_WRITE,0);
Raymond Knopp's avatar
 
Raymond Knopp committed
1546
	/*
Lionel Gauthier's avatar
Lionel Gauthier committed
1547
	if (trace_cnt++<10) 
Raymond Knopp's avatar
 
Raymond Knopp committed
1548
	  printf("TRX: t1 %llu (trx_read), t2 %llu (trx_write)\n",(long long unsigned int)(trx_time1.tv_nsec  - trx_time0.tv_nsec), (long long unsigned int)(trx_time2.tv_nsec - trx_time1.tv_nsec));
Raymond Knopp's avatar
 
Raymond Knopp committed
1549
	*/
Raymond Knopp's avatar
 
Raymond Knopp committed
1550 1551 1552
#else
	rt_sleep_ns(1000000);
#endif
1553 1554 1555
	if ((tx_launched == 0) && 
	    (rx_pos >=(((2*hw_subframe)+1)*PHY_vars_eNB_g[0][0]->lte_frame_parms.samples_per_tti>>1))) {
	  tx_launched = 1;
Raymond Knopp's avatar
 
Raymond Knopp committed
1556 1557 1558 1559 1560
	  for (CC_id=0;CC_id<MAX_NUM_CCs;CC_id++) {
	    if (pthread_mutex_lock(&PHY_vars_eNB_g[0][CC_id]->proc[hw_subframe].mutex_tx) != 0) {
	      LOG_E(PHY,"[eNB] ERROR pthread_mutex_lock for eNB TX thread %d (IC %d)\n",hw_subframe,PHY_vars_eNB_g[0][CC_id]->proc[hw_subframe].instance_cnt_tx);   
	    }
	    else {
Raymond Knopp's avatar
 
Raymond Knopp committed
1561
	      //	      LOG_I(PHY,"[eNB] Waking up eNB process %d (IC %d,rx_cnt %d)\n",hw_subframe,PHY_vars_eNB_g[0][CC_id]->proc[hw_subframe].instance_cnt_tx,rx_cnt); 
Raymond Knopp's avatar
 
Raymond Knopp committed
1562 1563 1564 1565 1566 1567 1568 1569
	      PHY_vars_eNB_g[0][CC_id]->proc[hw_subframe].instance_cnt_tx++;
	      pthread_mutex_unlock(&PHY_vars_eNB_g[0][CC_id]->proc[hw_subframe].mutex_tx);
	      if (PHY_vars_eNB_g[0][CC_id]->proc[hw_subframe].instance_cnt_tx == 0) {
		if (pthread_cond_signal(&PHY_vars_eNB_g[0][CC_id]->proc[hw_subframe].cond_tx) != 0) {
		  LOG_E(PHY,"[eNB] ERROR pthread_cond_signal for eNB TX thread %d\n",hw_subframe);
		}
	      }
	      else {
1570
		LOG_W(PHY,"[eNB] Frame %d, eNB TX thread %d busy!! (rx_cnt %d)\n",PHY_vars_eNB_g[0][CC_id]->proc[hw_subframe].frame_tx,hw_subframe,rx_pos);
Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
1571
		exit_fun("nothing to add");
Raymond Knopp's avatar
 
Raymond Knopp committed
1572 1573 1574 1575
	      }
	    }
	  }
	}
1576 1577
	rx_pos += spp;
	tx_pos += spp;
Raymond Knopp's avatar
 
Raymond Knopp committed
1578

1579 1580 1581

	if(tx_pos >= samples_per_frame)
	  tx_pos -= samples_per_frame;
Raymond Knopp's avatar
 
Raymond Knopp committed
1582 1583
      }

1584 1585
      if (rx_pos >= samples_per_frame)
	rx_pos -= samples_per_frame; 
Raymond Knopp's avatar
 
Raymond Knopp committed
1586
      
Raymond Knopp's avatar
 
Raymond Knopp committed
1587 1588 1589

#endif // USRP
     
Raymond Knopp's avatar
 
Raymond Knopp committed
1590
      if (oai_exit) break;
1591

Raymond Knopp's avatar
 
Raymond Knopp committed
1592

Raymond Knopp's avatar
 
Raymond Knopp committed
1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613
      
      timing_info.time_last = timing_info.time_now;
      timing_info.time_now = rt_get_time_ns();
      
      if (timing_info.n_samples>0) {
	time_diff = timing_info.time_now - timing_info.time_last;
	if (time_diff < timing_info.time_min)
	  timing_info.time_min = time_diff;
	if (time_diff > timing_info.time_max)
	  timing_info.time_max = time_diff;
	timing_info.time_avg += time_diff;
      }
      
      timing_info.n_samples++;
      /*
	if ((timing_info.n_samples%2000)==0) {
	LOG_D(HW,"frame %d (%d), slot %d, hw_slot %d: diff=%llu, min=%llu, max=%llu, avg=%llu (n_samples %d)\n",
	frame, PHY_vars_eNB_g[0]->frame, slot, hw_slot,time_diff,
	timing_info.time_min,timing_info.time_max,timing_info.time_avg/timing_info.n_samples,timing_info.n_samples);
	timing_info.n_samples = 0;
	timing_info.time_avg = 0;
Raymond Knopp's avatar
 
Raymond Knopp committed
1614
	}
Raymond Knopp's avatar
 
Raymond Knopp committed
1615 1616
      */
      //}
1617
    
Raymond Knopp's avatar
 
Raymond Knopp committed
1618
      if ((slot&1) == 1) {
1619
#ifdef EXMIMO
Raymond Knopp's avatar
 
Raymond Knopp committed
1620 1621 1622 1623 1624
	sf = ((slot>>1)+1)%10;
#else
	sf = hw_subframe;
#endif
	//		    LOG_I(PHY,"[eNB] Multithread slot %d (IC %d)\n",slot,PHY_vars_eNB_g[0][CC_id]->proc[sf].instance_cnt);
Raymond Knopp's avatar
 
Raymond Knopp committed
1625
	
Raymond Knopp's avatar
 
Raymond Knopp committed
1626
	for (CC_id=0;CC_id<MAX_NUM_CCs;CC_id++) {
1627
#ifdef EXMIMO 
Raymond Knopp's avatar
 
Raymond Knopp committed
1628 1629
	  if (pthread_mutex_lock(&PHY_vars_eNB_g[0][CC_id]->proc[sf].mutex_tx) != 0) {
	    LOG_E(PHY,"[eNB] ERROR pthread_mutex_lock for eNB TX thread %d (IC %d)\n",sf,PHY_vars_eNB_g[0][CC_id]->proc[sf].instance_cnt_tx);   
Raymond Knopp's avatar
 
Raymond Knopp committed
1630
	  }
Raymond Knopp's avatar
 
Raymond Knopp committed
1631 1632 1633 1634 1635 1636 1637
	  else {
	    //		      LOG_I(PHY,"[eNB] Waking up eNB process %d (IC %d)\n",sf,PHY_vars_eNB_g[0][CC_id]->proc[sf].instance_cnt); 
	    PHY_vars_eNB_g[0][CC_id]->proc[sf].instance_cnt_tx++;
	    pthread_mutex_unlock(&PHY_vars_eNB_g[0][CC_id]->proc[sf].mutex_tx);
	    if (PHY_vars_eNB_g[0][CC_id]->proc[sf].instance_cnt_tx == 0) {
	      if (pthread_cond_signal(&PHY_vars_eNB_g[0][CC_id]->proc[sf].cond_tx) != 0) {
		LOG_E(PHY,"[eNB] ERROR pthread_cond_signal for eNB TX thread %d\n",sf);
1638
	      }
Raymond Knopp's avatar
 
Raymond Knopp committed
1639 1640 1641
	    }
	    else {
	      LOG_W(PHY,"[eNB] Frame %d, eNB TX thread %d busy!!\n",PHY_vars_eNB_g[0][CC_id]->proc[sf].frame_tx,sf);
Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
1642
	      exit_fun("nothing to add");
Raymond Knopp's avatar
 
Raymond Knopp committed
1643
	    }
Raymond Knopp's avatar
 
Raymond Knopp committed
1644
	  }
Raymond Knopp's avatar
 
Raymond Knopp committed
1645 1646 1647 1648 1649
#endif	    
	  if (pthread_mutex_lock(&PHY_vars_eNB_g[0][CC_id]->proc[sf].mutex_rx) != 0) {
	    LOG_E(PHY,"[eNB] ERROR pthread_mutex_lock for eNB RX thread %d (IC %d)\n",sf,PHY_vars_eNB_g[0][CC_id]->proc[sf].instance_cnt_rx);   
	  }
	  else {
ghaddab's avatar
ghaddab committed
1650
	    //		      LOG_I(PHY,"[eNB] Waking up eNB process %d (IC %d) CC_id %d rx_cnt %d\n",sf,PHY_vars_eNB_g[0][CC_id]->proc[sf].instance_cnt_rx,CC_id,rx_cnt); 
Raymond Knopp's avatar
 
Raymond Knopp committed
1651 1652 1653 1654 1655
	    PHY_vars_eNB_g[0][CC_id]->proc[sf].instance_cnt_rx++;
	    pthread_mutex_unlock(&PHY_vars_eNB_g[0][CC_id]->proc[sf].mutex_rx);
	    if (PHY_vars_eNB_g[0][CC_id]->proc[sf].instance_cnt_rx == 0) {
	      if (pthread_cond_signal(&PHY_vars_eNB_g[0][CC_id]->proc[sf].cond_rx) != 0) {
		LOG_E(PHY,"[eNB] ERROR pthread_cond_signal for eNB RX thread %d\n",sf);
Raymond Knopp's avatar
 
Raymond Knopp committed
1656
	      }
ghaddab's avatar
ghaddab committed
1657
              //else
Raymond Knopp's avatar
 
Raymond Knopp committed
1658
	      // LOG_I(PHY,"[eNB] pthread_cond_signal for eNB RX thread %d instance_cnt_rx %d\n",sf,PHY_vars_eNB_g[0][CC_id]->proc[sf].instance_cnt_rx);
Raymond Knopp's avatar
 
Raymond Knopp committed
1659 1660
	    }
	    else {
ghaddab's avatar
ghaddab committed
1661
	      LOG_W(PHY,"[eNB] Frame %d, eNB RX thread %d busy!! instance_cnt %d CC_id %d\n",PHY_vars_eNB_g[0][CC_id]->proc[sf].frame_rx,sf,PHY_vars_eNB_g[0][CC_id]->proc[sf].instance_cnt_rx,CC_id);
Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
1662
	      exit_fun("nothing to add");
Raymond Knopp's avatar
 
Raymond Knopp committed
1663
	    }
Raymond Knopp's avatar
 
Raymond Knopp committed
1664
	  }
Raymond Knopp's avatar
 
Raymond Knopp committed
1665
	    
Raymond Knopp's avatar
 
Raymond Knopp committed
1666 1667
	}
      }
Raymond Knopp's avatar
 
Raymond Knopp committed
1668 1669
	  
      
Raymond Knopp's avatar
 
Raymond Knopp committed
1670 1671 1672 1673 1674 1675 1676
#ifndef RTAI
      //pthread_mutex_lock(&tti_mutex);
#endif




Raymond Knopp's avatar
 
Raymond Knopp committed
1677
#ifdef EXMIMO
Raymond Knopp's avatar
 
Raymond Knopp committed
1678
      slot++;
Raymond Knopp's avatar
 
Raymond Knopp committed
1679 1680 1681 1682
      if (slot == 20) {
	frame++;
	slot = 0;
      }
Raymond Knopp's avatar
 
Raymond Knopp committed
1683
#else
Raymond Knopp's avatar
 
Raymond Knopp committed
1684 1685 1686
      hw_subframe++;
      slot+=2;
      if(hw_subframe==10) {
Raymond Knopp's avatar
 
Raymond Knopp committed
1687
	hw_subframe = 0;
Raymond Knopp's avatar
 
Raymond Knopp committed
1688
	frame++;
Raymond Knopp's avatar
 
Raymond Knopp committed
1689
	slot = 1;
Raymond Knopp's avatar
 
Raymond Knopp committed
1690
      }
Raymond Knopp's avatar
 
Raymond Knopp committed
1691
#endif     
Raymond Knopp's avatar
 
Raymond Knopp committed
1692 1693


1694
#if defined(ENABLE_ITTI)
Raymond Knopp's avatar
 
Raymond Knopp committed
1695
      itti_update_lte_time(frame, slot);
1696
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
1697 1698 1699 1700 1701 1702
    }
  }
#ifdef DEBUG_THREADS
  printf("eNB_thread: finished, ran %d times.\n",frame);
#endif
  
1703
#ifdef HARD_RT
Raymond Knopp's avatar
 
Raymond Knopp committed
1704
  rt_make_soft_real_time();
1705 1706
#endif

Raymond Knopp's avatar
 
Raymond Knopp committed
1707 1708 1709 1710 1711

#ifdef DEBUG_THREADS
  printf("Exiting eNB_thread ...");
#endif
  // clean task
1712
#ifdef RTAI
Raymond Knopp's avatar
 
Raymond Knopp committed
1713 1714 1715 1716
  rt_task_delete(task);
#else
  eNB_thread_status = 0;
  pthread_exit(&eNB_thread_status);
1717
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
1718 1719 1720 1721 1722
#ifdef DEBUG_THREADS
  printf("eNB_thread deleted. returning\n");
#endif
  return 0;
}
1723

1724

Raymond Knopp's avatar
 
Raymond Knopp committed
1725

Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
1726

1727 1728 1729 1730
static void get_options (int argc, char **argv) {
  int c;
  //  char                          line[1000];
  //  int                           l;
1731
  int k,i;//,j,k;
1732
#ifdef USRP
1733
  int clock_src;
1734
#endif
1735 1736 1737 1738 1739
  int CC_id;
#ifdef EXMIMO
  char rxg_fname[256], line[1000];
  FILE *rxg_fd;
  int l;
1740
#endif
1741 1742 1743



1744 1745 1746 1747 1748 1749 1750 1751 1752 1753

  const Enb_properties_array_t *enb_properties;
  
  enum long_option_e {
    LONG_OPTION_START = 0x100, /* Start after regular single char options */
    LONG_OPTION_ULSCH_MAX_CONSECUTIVE_ERRORS,
    LONG_OPTION_CALIB_UE_RX,
    LONG_OPTION_CALIB_UE_RX_MED,
    LONG_OPTION_CALIB_UE_RX_BYP,
    LONG_OPTION_DEBUG_UE_PRACH,
1754 1755
    LONG_OPTION_NO_L2_CONNECT,
    LONG_OPTION_RXGAIN
1756 1757 1758 1759 1760 1761 1762 1763 1764
  };
  
  static const struct option long_options[] = {
    {"ulsch-max-errors",required_argument,  NULL, LONG_OPTION_ULSCH_MAX_CONSECUTIVE_ERRORS},
    {"calib-ue-rx",     required_argument,  NULL, LONG_OPTION_CALIB_UE_RX},
    {"calib-ue-rx-med", required_argument,  NULL, LONG_OPTION_CALIB_UE_RX_MED},
    {"calib-ue-rx-byp", required_argument,  NULL, LONG_OPTION_CALIB_UE_RX_BYP},
    {"debug-ue-prach",  no_argument,        NULL, LONG_OPTION_DEBUG_UE_PRACH},
    {"no-L2-connect",   no_argument,        NULL, LONG_OPTION_NO_L2_CONNECT},
1765
    {"ue_rxgain",   required_argument,  NULL, LONG_OPTION_RXGAIN},
1766 1767 1768 1769 1770 1771 1772 1773
    {NULL, 0, NULL, 0}};
  
  while ((c = getopt_long (argc, argv, "C:dK:g:F:G:qO:m:SUVRM:r:P:s:t:x:",long_options,NULL)) != -1) {
    switch (c) {
    case LONG_OPTION_ULSCH_MAX_CONSECUTIVE_ERRORS:
      ULSCH_max_consecutive_errors = atoi(optarg);
      printf("Set ULSCH_max_consecutive_errors = %d\n",ULSCH_max_consecutive_errors);
      break;
Raymond Knopp's avatar
 
Raymond Knopp committed
1774
      
1775 1776 1777 1778 1779
    case LONG_OPTION_CALIB_UE_RX:
      mode = rx_calib_ue;
      rx_input_level_dBm = atoi(optarg);
      printf("Running with UE calibration on (LNA max), input level %d dBm\n",rx_input_level_dBm);
      break;
Raymond Knopp's avatar
 
Raymond Knopp committed
1780
      
1781 1782 1783 1784
    case LONG_OPTION_CALIB_UE_RX_MED:
      mode = rx_calib_ue_med;
      rx_input_level_dBm = atoi(optarg);
      printf("Running with UE calibration on (LNA med), input level %d dBm\n",rx_input_level_dBm);
Raymond Knopp's avatar
 
Raymond Knopp committed
1785
      break;
1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799
      
    case LONG_OPTION_CALIB_UE_RX_BYP:
      mode = rx_calib_ue_byp;
      rx_input_level_dBm = atoi(optarg);
      printf("Running with UE calibration on (LNA byp), input level %d dBm\n",rx_input_level_dBm);
      break;
      
    case LONG_OPTION_DEBUG_UE_PRACH:
      mode = debug_prach;
      break;
      
    case LONG_OPTION_NO_L2_CONNECT:
      mode = no_L2_connect;
      break;
1800 1801 1802 1803
    case LONG_OPTION_RXGAIN:
      for (i=0;i<4;i++)
	rx_gain[0][i] = atof(optarg);
      break;
1804 1805 1806
    case 'M':
#ifdef ETHERNET
      strcpy(rrh_eNB_ip,optarg);
1807
#endif
1808 1809 1810 1811 1812 1813 1814 1815
      break;
    case 'C':
      for (CC_id=0;CC_id<MAX_NUM_CCs;CC_id++) {
	downlink_frequency[CC_id][0] = atof(optarg); // Use float to avoid issue with frequency over 2^31.
	downlink_frequency[CC_id][1] = downlink_frequency[CC_id][0];
	downlink_frequency[CC_id][2] = downlink_frequency[CC_id][0];
	downlink_frequency[CC_id][3] = downlink_frequency[CC_id][0];
	printf("Downlink for CC_id %d frequency set to %u\n", CC_id, downlink_frequency[CC_id][0]);
1816
      }
1817 1818
      UE_scan=0;
      break;
Raymond Knopp's avatar
 
Raymond Knopp committed
1819
      
1820 1821 1822 1823
    case 'd':
#ifdef XFORMS
      do_forms=1;
      printf("Running with XFORMS!\n");
Raymond Knopp's avatar
 
Raymond Knopp committed
1824
#endif
1825 1826 1827 1828 1829
      break;
      
    case 'K':
#if defined(ENABLE_ITTI)
      itti_dump_file = strdup(optarg);
1830
#else
1831
      printf("-K option is disabled when ENABLE_ITTI is not defined\n");
Raymond Knopp's avatar
 
Raymond Knopp committed
1832
#endif
1833
      break;
1834
      
1835 1836 1837
    case 'O':
      conf_config_file_name = optarg;
      break;
1838
      
1839 1840 1841
    case 'U':
      UE_flag = 1;
      break;
1842 1843 1844 1845 1846 1847 1848
    
    case 'm':
      target_dl_mcs = atoi (optarg);
      break;
    case 't':
      target_ul_mcs = atoi (optarg);
      break;
Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
1849
#ifdef OPENAIR2
1850
    case 'P':
Raymond Knopp's avatar
 
Raymond Knopp committed
1851
#ifdef OPENAIR2
1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865
      /* enable openair packet tracer (OPT)*/
      if ((strcmp(optarg, "wireshark") == 0) || 
	  (strcmp(optarg, "WIRESHARK") == 0)) {
	opt_type = OPT_WIRESHARK;
	printf("Enabling OPT for wireshark\n");
      } else if ((strcmp(optarg, "pcap") == 0) ||
		 (strcmp(optarg, "PCAP") == 0)){
	opt_type = OPT_PCAP;
	printf("Enabling OPT for pcap\n");
      } else {
	opt_type = OPT_NONE;
	printf("Unrecognized option for OPT module\n");
	printf("Possible values are either wireshark or pcap\n");
      }
Raymond Knopp's avatar
 
Raymond Knopp committed
1866
#endif
1867
      break;  
Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
1868
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
1869 1870 1871
    case 'V':
      ouput_vcd = 1;
      break;
Raymond Knopp's avatar
 
Raymond Knopp committed
1872
    case  'q':
Raymond Knopp's avatar
 
Raymond Knopp committed
1873 1874 1875 1876 1877 1878
      opp_enabled = 1;
      break;
    case  'R' :
      online_log_messages =1;
      break;
    case 'r':
Raymond Knopp's avatar
 
Raymond Knopp committed
1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900
      for (CC_id=0;CC_id<MAX_NUM_CCs;CC_id++) {
	switch(atoi(optarg)) {
	case 6:
	  frame_parms[CC_id]->N_RB_DL=6;
	  frame_parms[CC_id]->N_RB_UL=6;
	  break;
	case 25:
	  frame_parms[CC_id]->N_RB_DL=25;
	  frame_parms[CC_id]->N_RB_UL=25;
	  break;
	case 50:
	  frame_parms[CC_id]->N_RB_DL=50;
	  frame_parms[CC_id]->N_RB_UL=50;
	  break;
	case 100:
	  frame_parms[CC_id]->N_RB_DL=100;
	  frame_parms[CC_id]->N_RB_UL=100;
	  break;
	default:
	  printf("Unknown N_RB_DL %d, switching to 25\n",atoi(optarg));
	  break;
	}
Raymond Knopp's avatar
 
Raymond Knopp committed
1901
      }
1902
      break;
Raymond Knopp's avatar
 
Raymond Knopp committed
1903 1904
    case 's':
#ifdef USRP
Raymond Knopp's avatar
 
Raymond Knopp committed
1905 1906 1907

      clock_src = atoi(optarg);
      if (clock_src == 0) {
1908
	//	char ref[128] = "internal";
Raymond Knopp's avatar
 
Raymond Knopp committed
1909 1910 1911
	//strncpy(uhd_ref, ref, strlen(ref)+1);
      }
      else if (clock_src == 1) {
1912
	//char ref[128] = "external";
Raymond Knopp's avatar
 
Raymond Knopp committed
1913 1914
	//strncpy(uhd_ref, ref, strlen(ref)+1);
      }
Raymond Knopp's avatar
 
Raymond Knopp committed
1915 1916 1917 1918
#else
      printf("Note: -s not defined for ExpressMIMO2\n");
#endif
      break;
1919 1920 1921 1922
    case 'S':
      exit_missed_slots=0;
      printf("Skip exit for missed slots\n");
      break;
1923
    case 'g':
Raymond Knopp's avatar
 
Raymond Knopp committed
1924
      glog_level=atoi(optarg); // value between 1 - 9
1925
      break;
1926 1927

    case 'F':
1928
#ifdef EXMIMO
1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945
      sprintf(rxg_fname,"%srxg.lime",optarg);
      rxg_fd = fopen(rxg_fname,"r");
      if (rxg_fd) {
	printf("Loading RX Gain parameters from %s\n",rxg_fname);
	l=0;
	while (fgets(line, sizeof(line), rxg_fd)) {
	  if ((strlen(line)==0) || (*line == '#')) continue; //ignore empty or comment lines
	  else {
	    if (l==0) sscanf(line,"%d %d %d %d",&rxg_max[0],&rxg_max[1],&rxg_max[2],&rxg_max[3]);
	    if (l==1) sscanf(line,"%d %d %d %d",&rxg_med[0],&rxg_med[1],&rxg_med[2],&rxg_med[3]);
	    if (l==2) sscanf(line,"%d %d %d %d",&rxg_byp[0],&rxg_byp[1],&rxg_byp[2],&rxg_byp[3]);
	    l++;
	  }
	}
      }
      else 
	printf("%s not found, running with defaults\n",rxg_fname);
1946
#endif
1947 1948
      break;
      
1949 1950 1951
    case 'G':
      glog_verbosity=atoi(optarg);// value from 0, 0x5, 0x15, 0x35, 0x75
      break;
1952 1953 1954 1955 1956 1957 1958
    case 'x':
      transmission_mode = atoi(optarg);
      if (transmission_mode > 2) {
	printf("Transmission mode > 2 (%d) not supported for the moment\n",transmission_mode);
	exit(-1);
      }
      break;
Raymond Knopp's avatar
 
Raymond Knopp committed
1959 1960
    default:
      break;
1961 1962
    }
  }
Raymond Knopp's avatar
 
Raymond Knopp committed
1963
  
Raymond Knopp's avatar
 
Raymond Knopp committed
1964 1965
  if (UE_flag == 0)
    AssertFatal(conf_config_file_name != NULL,"Please provide a configuration file\n");
Raymond Knopp's avatar
 
Raymond Knopp committed
1966 1967
  
    
Raymond Knopp's avatar
 
Raymond Knopp committed
1968
  if ((UE_flag == 0) && (conf_config_file_name != NULL)) {
1969
    int i,j;
Raymond Knopp's avatar
 
Raymond Knopp committed
1970 1971 1972 1973 1974 1975 1976 1977 1978
    
    NB_eNB_INST = 1;
    
    /* Read eNB configuration file */
    enb_properties = enb_config_init(conf_config_file_name);
    
    AssertFatal (NB_eNB_INST <= enb_properties->number,
		 "Number of eNB is greater than eNB defined in configuration file %s (%d/%d)!",
		 conf_config_file_name, NB_eNB_INST, enb_properties->number);
1979

Raymond Knopp's avatar
 
Raymond Knopp committed
1980
    /* Update some simulation parameters */
1981
    for (i=0; i < enb_properties->number; i++) {
1982 1983 1984 1985
      AssertFatal (MAX_NUM_CCs == enb_properties->properties[i]->nb_cc,
		   "lte-softmodem compiled with MAX_NUM_CCs=%d, but only %d CCs configured for eNB %d!",
		   MAX_NUM_CCs, enb_properties->properties[i]->nb_cc, i);
      
Raymond Knopp's avatar
 
Raymond Knopp committed
1986 1987 1988 1989 1990 1991
      for (CC_id=0;CC_id<MAX_NUM_CCs;CC_id++) {
	frame_parms[CC_id]->frame_type =       enb_properties->properties[i]->frame_type[CC_id];
	frame_parms[CC_id]->tdd_config =       enb_properties->properties[i]->tdd_config[CC_id];
	frame_parms[CC_id]->tdd_config_S =     enb_properties->properties[i]->tdd_config_s[CC_id];
	frame_parms[CC_id]->Ncp =              enb_properties->properties[i]->prefix_type[CC_id];
	
Raymond Knopp's avatar
 
Raymond Knopp committed
1992
	//for (j=0; j < enb_properties->properties[i]->nb_cc; j++ ){
1993 1994
	frame_parms[CC_id]->Nid_cell            =  enb_properties->properties[i]->Nid_cell[CC_id];
	frame_parms[CC_id]->N_RB_DL             =  enb_properties->properties[i]->N_RB_DL[CC_id];
Raymond Knopp's avatar
 
Raymond Knopp committed
1995
	frame_parms[CC_id]->N_RB_UL             =  enb_properties->properties[i]->N_RB_DL[CC_id];
1996 1997 1998
	frame_parms[CC_id]->nb_antennas_tx      =  enb_properties->properties[i]->nb_antennas_tx[CC_id];
	frame_parms[CC_id]->nb_antennas_tx_eNB  =  enb_properties->properties[i]->nb_antennas_tx[CC_id];
	frame_parms[CC_id]->nb_antennas_rx      =  enb_properties->properties[i]->nb_antennas_rx[CC_id];
1999
	//} // j
Raymond Knopp's avatar
 
Raymond Knopp committed
2000 2001
      }

2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021
      
#ifdef OPENAIR2
      
      init_all_otg(0);
      g_otg->seed = 0;
      init_seeds(g_otg->seed);
      for (k=0; k<enb_properties->properties[i]->num_otg_elements; k++){
	j=enb_properties->properties[i]->otg_ue_id[k]; // ue_id
	g_otg->application_idx[i][j] = 1;
	//g_otg->packet_gen_type=SUBSTRACT_STRING;
	g_otg->background[i][j][0] =enb_properties->properties[i]->otg_bg_traffic[k];
	g_otg->application_type[i][j][0] =enb_properties->properties[i]->otg_app_type[k];// BCBR; //MCBR, BCBR
      
	printf("[OTG] configuring traffic type %d for  eNB %d UE %d (Background traffic is %s)\n",
	       g_otg->application_type[i][j][0], i, j,(g_otg->background[i][j][0]==1)?"Enabled":"Disabled");
      }
      init_predef_traffic(enb_properties->properties[i]->num_otg_elements, 1);
  
#endif
      
2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035
      glog_level                     = enb_properties->properties[i]->glog_level;
      glog_verbosity                 = enb_properties->properties[i]->glog_verbosity;
      hw_log_level                   = enb_properties->properties[i]->hw_log_level;
      hw_log_verbosity               = enb_properties->properties[i]->hw_log_verbosity ;
      phy_log_level                  = enb_properties->properties[i]->phy_log_level;
      phy_log_verbosity              = enb_properties->properties[i]->phy_log_verbosity;
      mac_log_level                  = enb_properties->properties[i]->mac_log_level;
      mac_log_verbosity              = enb_properties->properties[i]->mac_log_verbosity;
      rlc_log_level                  = enb_properties->properties[i]->rlc_log_level;
      rlc_log_verbosity              = enb_properties->properties[i]->rlc_log_verbosity;
      pdcp_log_level                 = enb_properties->properties[i]->pdcp_log_level;
      pdcp_log_verbosity             = enb_properties->properties[i]->pdcp_log_verbosity;
      rrc_log_level                  = enb_properties->properties[i]->rrc_log_level;
      rrc_log_verbosity              = enb_properties->properties[i]->rrc_log_verbosity;
Lionel Gauthier's avatar
 
Lionel Gauthier committed
2036 2037 2038 2039 2040 2041
# if defined(ENABLE_USE_MME)
      gtpu_log_level                 = enb_properties->properties[i]->gtpu_log_level;
      gtpu_log_verbosity             = enb_properties->properties[i]->gtpu_log_verbosity;
      udp_log_level                  = enb_properties->properties[i]->udp_log_level;
      udp_log_verbosity              = enb_properties->properties[i]->udp_log_verbosity;
#endif
2042 2043 2044 2045
#if defined (ENABLE_SECURITY)
      osa_log_level                  = enb_properties->properties[i]->osa_log_level;
      osa_log_verbosity              = enb_properties->properties[i]->osa_log_verbosity;
#endif 
Florian Kaltenberger's avatar
Florian Kaltenberger committed
2046

Raymond Knopp's avatar
 
Raymond Knopp committed
2047
      // adjust the log
2048 2049
      for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
	for (k = 0 ; k < 4; k++) {
2050
	  downlink_frequency[CC_id][k]      =       enb_properties->properties[i]->downlink_frequency[CC_id];
2051
	  uplink_frequency_offset[CC_id][k] =  enb_properties->properties[i]->uplink_frequency_offset[CC_id];
2052 2053
	  rx_gain[CC_id][k]                 =  (double)enb_properties->properties[i]->rx_gain[CC_id];
	  tx_gain[CC_id][k]                 =  (double)enb_properties->properties[i]->tx_gain[CC_id];
2054
	}
2055
	printf("Downlink frequency/ uplink offset of CC_id %d set to %ju/%d\n", CC_id,
2056 2057
	       enb_properties->properties[i]->downlink_frequency[CC_id],
	       enb_properties->properties[i]->uplink_frequency_offset[CC_id]);
Raymond Knopp's avatar
 
Raymond Knopp committed
2058
      } // CC_id
2059
    }// i
Raymond Knopp's avatar
 
Raymond Knopp committed
2060
  }
Raymond Knopp's avatar
 
Raymond Knopp committed
2061 2062 2063 2064 2065 2066 2067
  else if ((UE_flag == 1) && (conf_config_file_name != NULL)) {

    // Here the configuration file is the XER encoded UE capabilities
    // Read it in and store in asn1c data structures
    strcpy(uecap_xer,conf_config_file_name);
    uecap_xer_in=1;
  }
Raymond Knopp's avatar
 
Raymond Knopp committed
2068
}
2069

Raymond Knopp's avatar
 
Raymond Knopp committed
2070
int main(int argc, char **argv) {
2071
#ifdef RTAI
Raymond Knopp's avatar
 
Raymond Knopp committed
2072
  // RT_TASK *task;
Raymond Knopp's avatar
 
Raymond Knopp committed
2073
#else
Raymond Knopp's avatar
 
Raymond Knopp committed
2074
  int *eNB_thread_status_p;
Raymond Knopp's avatar
 
Raymond Knopp committed
2075
  //  int *eNB_thread_status_rx[10],*eNB_thread_status_tx[10];
2076
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
2077
  int i,aa,card;
Lionel Gauthier's avatar
Lionel Gauthier committed
2078
#if defined (XFORMS) || defined (EMOS) || defined (EXMIMO)
Raymond Knopp's avatar
 
Raymond Knopp committed
2079
  void *status;
2080
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
2081
  
Raymond Knopp's avatar
 
Raymond Knopp committed
2082
  int CC_id;
Raymond Knopp's avatar
 
Raymond Knopp committed
2083
  uint16_t Nid_cell = 0;
2084
  uint8_t  cooperation_flag=0,  abstraction_flag=0;
2085
#ifndef OPENAIR2
Raymond Knopp's avatar
 
Raymond Knopp committed
2086
  uint8_t beta_ACK=0,beta_RI=0,beta_CQI=2;
2087 2088 2089
#endif

#ifdef ENABLE_TCXO
Raymond Knopp's avatar
 
Raymond Knopp committed
2090
  unsigned int tcxo = 114;
2091 2092
#endif

Raymond Knopp's avatar
 
Raymond Knopp committed
2093
#if defined (XFORMS)
2094 2095
  int ret;
#endif
2096
#if defined (EMOS) || (! defined (RTAI))
Raymond Knopp's avatar
 
Raymond Knopp committed
2097
  int error_code;
2098
#endif
2099

2100 2101 2102 2103
  PHY_VARS_UE *UE[MAX_NUM_CCs];
  int UE_id = 0;

  mode = normal_txrx;
2104
  memset(&openair0_cfg[0],0,sizeof(openair0_config_t)*MAX_CARDS);
2105

2106
  memset(tx_max_power,0,sizeof(int)*MAX_NUM_CCs);
Raymond Knopp's avatar
 
Raymond Knopp committed
2107
  set_latency_target();
2108

2109

2110

Raymond Knopp's avatar
 
Raymond Knopp committed
2111 2112 2113
  for (CC_id=0;CC_id<MAX_NUM_CCs;CC_id++) {
    frame_parms[CC_id] = (LTE_DL_FRAME_PARMS*) malloc(sizeof(LTE_DL_FRAME_PARMS));
    /* Set some default values that may be overwritten while reading options */
Raymond Knopp's avatar
 
Raymond Knopp committed
2114
    frame_parms[CC_id]->frame_type         = TDD; /* TDD */
Raymond Knopp's avatar
 
Raymond Knopp committed
2115 2116 2117 2118
    frame_parms[CC_id]->tdd_config          = 3;
    frame_parms[CC_id]->tdd_config_S        = 0;
    frame_parms[CC_id]->N_RB_DL             = 25;
    frame_parms[CC_id]->N_RB_UL             = 25;
Raymond Knopp's avatar
 
Raymond Knopp committed
2119
    frame_parms[CC_id]->Ncp                 = NORMAL;
Raymond Knopp's avatar
 
Raymond Knopp committed
2120 2121
    frame_parms[CC_id]->Ncp_UL              = NORMAL;
    frame_parms[CC_id]->Nid_cell            = Nid_cell;
Raymond Knopp's avatar
 
Raymond Knopp committed
2122
    frame_parms[CC_id]->num_MBSFN_config    = 0;
Raymond Knopp's avatar
 
Raymond Knopp committed
2123 2124 2125
    frame_parms[CC_id]->nb_antennas_tx_eNB  = 1;
    frame_parms[CC_id]->nb_antennas_tx      = 1;
    frame_parms[CC_id]->nb_antennas_rx      = 1;
Raymond Knopp's avatar
 
Raymond Knopp committed
2126
  }
2127

2128 2129
  // initialize the log (see log.h for details)
  logInit();
Florian Kaltenberger's avatar
Florian Kaltenberger committed
2130
  
Raymond Knopp's avatar
 
Raymond Knopp committed
2131 2132
  //randominit (0);
  set_taus_seed (0);
Florian Kaltenberger's avatar
Florian Kaltenberger committed
2133
 
2134

2135
  get_options (argc, argv); //Command-line options
Florian Kaltenberger's avatar
Florian Kaltenberger committed
2136 2137
  set_glog(glog_level, glog_verbosity);

Raymond Knopp's avatar
 
Raymond Knopp committed
2138 2139 2140 2141

  if (UE_flag==1)
    {
      printf("configuring for UE\n");
2142

2143
      set_comp_log(HW,      LOG_DEBUG,  LOG_HIGH, 1);
2144
#ifdef OPENAIR2
2145
      set_comp_log(PHY,     LOG_DEBUG,   LOG_HIGH, 1);
2146
#else
Raymond Knopp's avatar
 
Raymond Knopp committed
2147
      set_comp_log(PHY,     LOG_INFO,   LOG_HIGH, 1);
2148
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
2149 2150 2151 2152 2153
      set_comp_log(MAC,     LOG_INFO,   LOG_HIGH, 1);
      set_comp_log(RLC,     LOG_INFO,   LOG_HIGH, 1);
      set_comp_log(PDCP,    LOG_INFO,   LOG_HIGH, 1);
      set_comp_log(OTG,     LOG_INFO,   LOG_HIGH, 1);
      set_comp_log(RRC,     LOG_INFO,   LOG_HIGH, 1);
2154
#if defined(ENABLE_ITTI)
Raymond Knopp's avatar
 
Raymond Knopp committed
2155
      set_comp_log(EMU,     LOG_INFO,   LOG_MED, 1);
2156
# if defined(ENABLE_USE_MME)
Raymond Knopp's avatar
 
Raymond Knopp committed
2157
      set_comp_log(NAS,     LOG_INFO,   LOG_HIGH, 1);
2158 2159
# endif
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
2160 2161 2162 2163
    }
  else
    {
      printf("configuring for eNB\n");
2164

2165
      set_comp_log(HW,      hw_log_level, hw_log_verbosity, 1);
2166
#ifdef OPENAIR2
2167
      set_comp_log(PHY,     phy_log_level,   phy_log_verbosity, 1);
2168
#else
Raymond Knopp's avatar
 
Raymond Knopp committed
2169
      set_comp_log(PHY,     LOG_INFO,   LOG_HIGH, 1);
2170
#endif
2171 2172 2173 2174
      set_comp_log(MAC,     mac_log_level,  mac_log_verbosity, 1);
      set_comp_log(RLC,     rlc_log_level,   rlc_log_verbosity, 1);
      set_comp_log(PDCP,    pdcp_log_level,  pdcp_log_verbosity, 1);
      set_comp_log(RRC,     rrc_log_level,  rrc_log_verbosity, 1);
2175
#if defined(ENABLE_ITTI)
Raymond Knopp's avatar
 
Raymond Knopp committed
2176
      set_comp_log(EMU,     LOG_INFO,   LOG_MED, 1);
2177
# if defined(ENABLE_USE_MME)
Lionel Gauthier's avatar
 
Lionel Gauthier committed
2178 2179
      set_comp_log(UDP_,    udp_log_level,   udp_log_verbosity, 1);
      set_comp_log(GTPU,    gtpu_log_level,   gtpu_log_verbosity, 1);
Raymond Knopp's avatar
 
Raymond Knopp committed
2180 2181
      set_comp_log(S1AP,    LOG_DEBUG,   LOG_HIGH, 1);
      set_comp_log(SCTP,    LOG_INFO,   LOG_HIGH, 1);
2182
# endif
2183
#if defined(ENABLE_SECURITY)
2184
      set_comp_log(OSA,    osa_log_level,   osa_log_verbosity, 1);
2185
#endif
2186
#endif
2187 2188 2189 2190
#ifdef LOCALIZATION
      set_comp_log(LOCALIZE, LOG_DEBUG, LOG_LOW, 1);
      set_component_filelog(LOCALIZE);
#endif 
Raymond Knopp's avatar
 
Raymond Knopp committed
2191
      set_comp_log(ENB_APP, LOG_INFO, LOG_HIGH, 1);
2192
      set_comp_log(OTG,     LOG_INFO,   LOG_HIGH, 1);
Raymond Knopp's avatar
 
Raymond Knopp committed
2193 2194 2195
      if (online_log_messages == 1) { 
	set_component_filelog(RRC);
	set_component_filelog(PDCP);
Raymond Knopp's avatar
 
Raymond Knopp committed
2196 2197
      }
    }
2198

Raymond Knopp's avatar
 
Raymond Knopp committed
2199 2200 2201 2202 2203 2204
  if (ouput_vcd) {
    if (UE_flag==1)
      vcd_signal_dumper_init("/tmp/openair_dump_UE.vcd");
    else
      vcd_signal_dumper_init("/tmp/openair_dump_eNB.vcd");
  }
2205 2206 2207
  
  if (opp_enabled ==1)
    reset_opp_meas();
Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
2208
#ifdef OPENAIR2
2209 2210 2211 2212 2213 2214 2215 2216 2217
  if (opt_type != OPT_NONE) {
    radio_type_t radio_type;
    if (frame_parms[0]->frame_type == FDD)
      radio_type = RADIO_TYPE_FDD;
    else 
      radio_type = RADIO_TYPE_TDD;
    if (init_opt(NULL, NULL, NULL, radio_type) == -1)
      LOG_E(OPT,"failed to run OPT \n");
  }
Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
2218
#endif
2219
  
2220
#if defined(ENABLE_ITTI)
Raymond Knopp's avatar
 
Raymond Knopp committed
2221 2222 2223 2224 2225 2226
  if (UE_flag == 1) {
    log_set_instance_type (LOG_INSTANCE_UE);
  }
  else {
    log_set_instance_type (LOG_INSTANCE_ENB);
  }
2227

Raymond Knopp's avatar
 
Raymond Knopp committed
2228
  itti_init(TASK_MAX, THREAD_MAX, MESSAGES_ID_MAX, tasks_info, messages_info, messages_definition_xml, itti_dump_file);
2229 2230
#endif

2231
#ifdef NAS_NETLINK
Raymond Knopp's avatar
 
Raymond Knopp committed
2232
  netlink_init();
2233 2234
#endif

2235
#if !defined(ENABLE_ITTI)
Raymond Knopp's avatar
 
Raymond Knopp committed
2236 2237 2238
  // to make a graceful exit when ctrl-c is pressed
  signal(SIGSEGV, signal_handler);
  signal(SIGINT, signal_handler);
2239
#endif
2240 2241

#ifndef RTAI
Raymond Knopp's avatar
 
Raymond Knopp committed
2242 2243 2244 2245
  check_clock();
#endif

  // init the parameters
Raymond Knopp's avatar
 
Raymond Knopp committed
2246 2247 2248 2249
  for (CC_id=0;CC_id<MAX_NUM_CCs;CC_id++) {
    frame_parms[CC_id]->nushift            = 0;
    if (UE_flag==0)
      {
2250

Raymond Knopp's avatar
 
Raymond Knopp committed
2251
      }
Raymond Knopp's avatar
 
Raymond Knopp committed
2252 2253 2254
    else
      { //UE_flag==1
	frame_parms[CC_id]->nb_antennas_tx     = 1;
2255
	frame_parms[CC_id]->nb_antennas_rx     = 1;
2256
	frame_parms[CC_id]->nb_antennas_tx_eNB = (transmission_mode == 1) ? 1 : 2; //initial value overwritten by initial sync later
Raymond Knopp's avatar
 
Raymond Knopp committed
2257 2258 2259 2260 2261 2262 2263 2264 2265
      }
    frame_parms[CC_id]->mode1_flag         = (transmission_mode == 1) ? 1 : 0;
    frame_parms[CC_id]->phich_config_common.phich_resource = oneSixth;
    frame_parms[CC_id]->phich_config_common.phich_duration = normal;
    // UL RS Config
    frame_parms[CC_id]->pusch_config_common.ul_ReferenceSignalsPUSCH.cyclicShift = 0;//n_DMRS1 set to 0
    frame_parms[CC_id]->pusch_config_common.ul_ReferenceSignalsPUSCH.groupHoppingEnabled = 0;
    frame_parms[CC_id]->pusch_config_common.ul_ReferenceSignalsPUSCH.sequenceHoppingEnabled = 0;
    frame_parms[CC_id]->pusch_config_common.ul_ReferenceSignalsPUSCH.groupAssignmentPUSCH = 0;
2266 2267
    init_ul_hopping(frame_parms[CC_id]);
    init_frame_parms(frame_parms[CC_id],1);
2268
    phy_init_top(frame_parms[CC_id]);
Raymond Knopp's avatar
 
Raymond Knopp committed
2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280
  }

  phy_init_lte_top(frame_parms[0]);

  for (CC_id=0;CC_id<MAX_NUM_CCs;CC_id++) {
    //init prach for openair1 test
    frame_parms[CC_id]->prach_config_common.rootSequenceIndex=22; 
    frame_parms[CC_id]->prach_config_common.prach_ConfigInfo.zeroCorrelationZoneConfig=1;
    frame_parms[CC_id]->prach_config_common.prach_ConfigInfo.prach_ConfigIndex=0; 
    frame_parms[CC_id]->prach_config_common.prach_ConfigInfo.highSpeedFlag=0;
    frame_parms[CC_id]->prach_config_common.prach_ConfigInfo.prach_FreqOffset=0;
    // prach_fmt = get_prach_fmt(frame_parms->prach_config_common.prach_ConfigInfo.prach_ConfigIndex, frame_parms->frame_type);
Raymond Knopp's avatar
 
Raymond Knopp committed
2281
    // N_ZC = (prach_fmt <4)?839:139;
Raymond Knopp's avatar
 
Raymond Knopp committed
2282
  }
Raymond Knopp's avatar
 
Raymond Knopp committed
2283 2284

  if (UE_flag==1) {
Raymond Knopp's avatar
 
Raymond Knopp committed
2285 2286 2287
    NB_UE_INST=1;
    NB_INST=1;

Raymond Knopp's avatar
 
Raymond Knopp committed
2288 2289
    PHY_vars_UE_g = malloc(sizeof(PHY_VARS_UE**));
    PHY_vars_UE_g[0] = malloc(sizeof(PHY_VARS_UE*)*MAX_NUM_CCs);
2290
  
Raymond Knopp's avatar
 
Raymond Knopp committed
2291
    for (CC_id=0;CC_id<MAX_NUM_CCs;CC_id++) {
2292 2293 2294 2295

      PHY_vars_UE_g[0][CC_id] = init_lte_UE(frame_parms[CC_id], 0,abstraction_flag,transmission_mode);
      UE[CC_id] = PHY_vars_UE_g[0][CC_id];
      printf("PHY_vars_UE_g[0][%d] = %p\n",CC_id,UE[CC_id]);
Raymond Knopp's avatar
 
Raymond Knopp committed
2296
#ifndef OPENAIR2
Raymond Knopp's avatar
 
Raymond Knopp committed
2297
      for (i=0;i<NUMBER_OF_CONNECTED_eNB_MAX;i++) {
2298 2299 2300
	UE[CC_id]->pusch_config_dedicated[i].betaOffset_ACK_Index = beta_ACK;
	UE[CC_id]->pusch_config_dedicated[i].betaOffset_RI_Index  = beta_RI;
	UE[CC_id]->pusch_config_dedicated[i].betaOffset_CQI_Index = beta_CQI;
Raymond Knopp's avatar
 
Raymond Knopp committed
2301
	
2302 2303 2304
	UE[CC_id]->scheduling_request_config[i].sr_PUCCH_ResourceIndex = 0;
	UE[CC_id]->scheduling_request_config[i].sr_ConfigIndex = 7+(0%3);
	UE[CC_id]->scheduling_request_config[i].dsr_TransMax = sr_n4;
Raymond Knopp's avatar
 
Raymond Knopp committed
2305
      }
2306
#endif
2307 2308 2309 2310 2311 2312 2313 2314


      UE[CC_id]->UE_scan = UE_scan;
      UE[CC_id]->mode    = mode;

      compute_prach_seq(&UE[CC_id]->lte_frame_parms.prach_config_common,
			UE[CC_id]->lte_frame_parms.frame_type,
			UE[CC_id]->X_u);
Raymond Knopp's avatar
 
Raymond Knopp committed
2315
      
2316
      UE[CC_id]->lte_ue_pdcch_vars[0]->crnti = 0x1234;
2317
#ifndef OPENAIR2
2318
      UE[CC_id]->lte_ue_pdcch_vars[0]->crnti = 0x1235;
2319
#endif
2320
    
2321
#ifdef EXMIMO
Raymond Knopp's avatar
 
Raymond Knopp committed
2322
      for (i=0;i<4;i++) {
2323 2324 2325
	UE[CC_id]->rx_gain_max[i] = rxg_max[i];
	UE[CC_id]->rx_gain_med[i] = rxg_med[i];
	UE[CC_id]->rx_gain_byp[i] = rxg_byp[i];
Raymond Knopp's avatar
 
Raymond Knopp committed
2326
      }
2327

2328 2329 2330 2331
      if ((UE[0]->mode == normal_txrx) || 
	  (UE[0]->mode == rx_calib_ue) || 
	  (UE[0]->mode == no_L2_connect) || 
	  (UE[0]->mode == debug_prach)) {
Raymond Knopp's avatar
 
Raymond Knopp committed
2332 2333
	for (i=0;i<4;i++)
	  rx_gain_mode[CC_id][i] = max_gain;
2334
	UE[CC_id]->rx_total_gain_dB =  UE[CC_id]->rx_gain_max[0] + (int)rx_gain[CC_id][0] - 30; //-30 because it was calibrated with a 30dB gain
Raymond Knopp's avatar
 
Raymond Knopp committed
2335 2336 2337 2338
      }
      else if ((mode == rx_calib_ue_med)) {
	for (i=0;i<4;i++)
	  rx_gain_mode[CC_id][i] =  med_gain;
2339
	UE[CC_id]->rx_total_gain_dB =  UE[CC_id]->rx_gain_med[0]  + (int)rx_gain[CC_id][0] - 30; //-30 because it was calibrated with a 30dB gain;
Raymond Knopp's avatar
 
Raymond Knopp committed
2340 2341 2342 2343
      }
      else if ((mode == rx_calib_ue_byp)) {
	for (i=0;i<4;i++)
	  rx_gain_mode[CC_id][i] =  byp_gain;
2344
	UE[CC_id]->rx_total_gain_dB =  UE[CC_id]->rx_gain_byp[0]  + (int)rx_gain[CC_id][0] - 30; //-30 because it was calibrated with a 30dB gain;
Raymond Knopp's avatar
 
Raymond Knopp committed
2345
      }
2346
#else
2347
      UE[CC_id]->rx_total_gain_dB =  (int)rx_gain[CC_id][0]; 
2348
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
2349
    
2350
      UE[CC_id]->tx_power_max_dBm = tx_max_power[CC_id];
Raymond Knopp's avatar
 
Raymond Knopp committed
2351
    
2352

2353
   
2354
#ifdef EXMIMO
Raymond Knopp's avatar
 
Raymond Knopp committed
2355
      //N_TA_offset
2356 2357 2358 2359 2360 2361 2362
      if (UE[CC_id]->lte_frame_parms.frame_type == TDD) {
	if (UE[CC_id]->lte_frame_parms.N_RB_DL == 100)
	  UE[CC_id]->N_TA_offset = 624;
	else if (UE[CC_id]->lte_frame_parms.N_RB_DL == 50)
	  UE[CC_id]->N_TA_offset = 624/2;
	else if (UE[CC_id]->lte_frame_parms.N_RB_DL == 25)
	  UE[CC_id]->N_TA_offset = 624/4;
Raymond Knopp's avatar
 
Raymond Knopp committed
2363 2364
      }
      else {
2365
	UE[CC_id]->N_TA_offset = 0;
Raymond Knopp's avatar
 
Raymond Knopp committed
2366
      }
2367
#else
Raymond Knopp's avatar
 
Raymond Knopp committed
2368
      //already taken care of in lte-softmodem
2369
      UE[CC_id]->N_TA_offset = 0;
2370
#endif 
Raymond Knopp's avatar
 
Raymond Knopp committed
2371
    }
Raymond Knopp's avatar
 
Raymond Knopp committed
2372 2373 2374 2375 2376
    openair_daq_vars.manual_timing_advance = 0;
    openair_daq_vars.rx_gain_mode = DAQ_AGC_ON;
    openair_daq_vars.auto_freq_correction = 0;
    openair_daq_vars.use_ia_receiver = 0;
    
2377
  
Raymond Knopp's avatar
 
Raymond Knopp committed
2378 2379 2380 2381 2382
    
    //  printf("tx_max_power = %d -> amp %d\n",tx_max_power,get_tx_amp(tx_max_power,tx_max_power));
  }
  else
    { //this is eNB
Raymond Knopp's avatar
 
Raymond Knopp committed
2383 2384 2385
      PHY_vars_eNB_g = malloc(sizeof(PHY_VARS_eNB**));
      PHY_vars_eNB_g[0] = malloc(sizeof(PHY_VARS_eNB*));
      for (CC_id=0;CC_id<MAX_NUM_CCs;CC_id++) {
2386
	PHY_vars_eNB_g[0][CC_id] = init_lte_eNB(frame_parms[CC_id],0,Nid_cell,cooperation_flag,transmission_mode,abstraction_flag);
2387
	PHY_vars_eNB_g[0][CC_id]->CC_id = CC_id;
Raymond Knopp's avatar
 
Raymond Knopp committed
2388
       
2389
#ifndef OPENAIR2
Raymond Knopp's avatar
 
Raymond Knopp committed
2390 2391 2392 2393 2394 2395 2396 2397 2398
	for (i=0;i<NUMBER_OF_UE_MAX;i++) {
	  PHY_vars_eNB_g[0][CC_id]->pusch_config_dedicated[i].betaOffset_ACK_Index = beta_ACK;
	  PHY_vars_eNB_g[0][CC_id]->pusch_config_dedicated[i].betaOffset_RI_Index  = beta_RI;
	  PHY_vars_eNB_g[0][CC_id]->pusch_config_dedicated[i].betaOffset_CQI_Index = beta_CQI;
	  
	  PHY_vars_eNB_g[0][CC_id]->scheduling_request_config[i].sr_PUCCH_ResourceIndex = i;
	  PHY_vars_eNB_g[0][CC_id]->scheduling_request_config[i].sr_ConfigIndex = 7+(i%3);
	  PHY_vars_eNB_g[0][CC_id]->scheduling_request_config[i].dsr_TransMax = sr_n4;
	}
Raymond Knopp's avatar
 
Raymond Knopp committed
2399
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
2400 2401 2402 2403
      
	compute_prach_seq(&PHY_vars_eNB_g[0][CC_id]->lte_frame_parms.prach_config_common,
			  PHY_vars_eNB_g[0][CC_id]->lte_frame_parms.frame_type,
			  PHY_vars_eNB_g[0][CC_id]->X_u);
Raymond Knopp's avatar
 
Raymond Knopp committed
2404

2405
#ifndef EXMIMO
Raymond Knopp's avatar
 
Raymond Knopp committed
2406

2407
	PHY_vars_eNB_g[0][CC_id]->rx_total_gain_eNB_dB = (int)rx_gain[CC_id][0];
Raymond Knopp's avatar
 
Raymond Knopp committed
2408

2409
#else
2410
	PHY_vars_eNB_g[0][CC_id]->rx_total_gain_eNB_dB =  rxg_max[0] + (int)rx_gain[CC_id][0] - 30; //was measured at rxgain=30;
Raymond Knopp's avatar
 
Raymond Knopp committed
2411 2412 2413

	printf("Setting RX total gain to %d\n",PHY_vars_eNB_g[0][CC_id]->rx_total_gain_eNB_dB);

2414 2415
	// set eNB to max gain
	for (i=0;i<4;i++)
2416 2417
	  rx_gain_mode[CC_id][i] = max_gain;
#endif
2418

2419
#ifdef EXMIMO
2420
	//N_TA_offset
2421 2422 2423 2424 2425 2426 2427
	if (PHY_vars_eNB_g[0][CC_id]->lte_frame_parms.frame_type == TDD) {
	  if (PHY_vars_eNB_g[0][CC_id]->lte_frame_parms.N_RB_DL == 100)
	    PHY_vars_eNB_g[0][CC_id]->N_TA_offset = 624;
	  else if (PHY_vars_eNB_g[0][CC_id]->lte_frame_parms.N_RB_DL == 50)
	    PHY_vars_eNB_g[0][CC_id]->N_TA_offset = 624/2;
	  else if (PHY_vars_eNB_g[0][CC_id]->lte_frame_parms.N_RB_DL == 25)
	    PHY_vars_eNB_g[0][CC_id]->N_TA_offset = 624/4;
2428 2429
	}
	else {
2430
	  PHY_vars_eNB_g[0][CC_id]->N_TA_offset = 0;
2431 2432 2433
	}
#else
	//already taken care of in lte-softmodem
2434
	PHY_vars_eNB_g[0][CC_id]->N_TA_offset = 0;
2435 2436
#endif 
	
Raymond Knopp's avatar
 
Raymond Knopp committed
2437
      }
2438 2439


Raymond Knopp's avatar
 
Raymond Knopp committed
2440 2441
      NB_eNB_INST=1;
      NB_INST=1;
Raymond Knopp's avatar
 
Raymond Knopp committed
2442

Raymond Knopp's avatar
 
Raymond Knopp committed
2443
      openair_daq_vars.ue_dl_rb_alloc=0x1fff;
2444
      openair_daq_vars.target_ue_dl_mcs=target_dl_mcs;
Raymond Knopp's avatar
 
Raymond Knopp committed
2445
      openair_daq_vars.ue_ul_nb_rb=6;
2446
      openair_daq_vars.target_ue_ul_mcs=target_ul_mcs;
Raymond Knopp's avatar
 
Raymond Knopp committed
2447 2448

    }
2449

2450

2451

Raymond Knopp's avatar
 
Raymond Knopp committed
2452
  dump_frame_parms(frame_parms[0]);
2453

Raymond Knopp's avatar
 
Raymond Knopp committed
2454
  if(frame_parms[0]->N_RB_DL == 100) {
Raymond Knopp's avatar
 
Raymond Knopp committed
2455
    sample_rate = 30.72e6;
2456
#ifndef EXMIMO
2457
    openair0_cfg[0].samples_per_packet = 2048;
Raymond Knopp's avatar
 
Raymond Knopp committed
2458 2459 2460
    samples_per_frame = 307200;
    // from usrp_time_offset
    tx_forward_nsamps = 175;
2461
    tx_delay = 8;
Raymond Knopp's avatar
 
Raymond Knopp committed
2462 2463
#endif
  }
Raymond Knopp's avatar
 
Raymond Knopp committed
2464
  else if(frame_parms[0]->N_RB_DL == 50){
2465
    sample_rate = 15.36e6;
2466
#ifndef EXMIMO
2467
    openair0_cfg[0].samples_per_packet = 2048;
Raymond Knopp's avatar
 
Raymond Knopp committed
2468 2469
    samples_per_frame = 153600;
    tx_forward_nsamps = 95;
2470
    tx_delay = 5;
Raymond Knopp's avatar
 
Raymond Knopp committed
2471 2472
#endif
  }
Raymond Knopp's avatar
 
Raymond Knopp committed
2473
  else if (frame_parms[0]->N_RB_DL == 25) {
Raymond Knopp's avatar
 
Raymond Knopp committed
2474
    sample_rate = 7.68e6;
2475
#ifndef EXMIMO
2476
    openair0_cfg[0].samples_per_packet = 1024;
Raymond Knopp's avatar
 
Raymond Knopp committed
2477 2478
    samples_per_frame = 76800;
    tx_forward_nsamps = 70;
2479
    tx_delay = 6;
Raymond Knopp's avatar
 
Raymond Knopp committed
2480 2481
#endif
  }
Raymond Knopp's avatar
 
Raymond Knopp committed
2482 2483 2484
  else if (frame_parms[0]->N_RB_DL == 6) {
    sample_rate = 1.92e6;
#ifndef EXMIMO
2485
    openair0_cfg[0].samples_per_packet = 256;
Raymond Knopp's avatar
 
Raymond Knopp committed
2486 2487
    samples_per_frame = 19200;
    tx_forward_nsamps = 40;
2488
    tx_delay = 8;
Raymond Knopp's avatar
 
Raymond Knopp committed
2489 2490
#endif
  }
Raymond Knopp's avatar
 
Raymond Knopp committed
2491 2492

#ifdef ETHERNET
2493 2494
  if (frame_parms[0]->N_RB_DL == 6) openair0_cfg[0].samples_per_packet = 256
  else openair0_cfg[0].samples_per_packet = 1536;
Raymond Knopp's avatar
 
Raymond Knopp committed
2495

2496
  printf("HW: samples_per_packet %d\n",openair0_cfg[0].samples_per_packet);
Raymond Knopp's avatar
 
Raymond Knopp committed
2497 2498
#endif

2499

2500
  for (card=0;card<MAX_CARDS;card++) {
2501
#ifndef EXMIMO
2502
    openair0_cfg[card].samples_per_packet = openair0_cfg[0].samples_per_packet;
2503 2504 2505 2506
#endif
    printf("HW: Configuring card %d, nb_antennas_tx/rx %d/%d\n",card,
	   ((UE_flag==0) ? PHY_vars_eNB_g[0][0]->lte_frame_parms.nb_antennas_tx : PHY_vars_UE_g[0][0]->lte_frame_parms.nb_antennas_tx),
	   ((UE_flag==0) ? PHY_vars_eNB_g[0][0]->lte_frame_parms.nb_antennas_rx : PHY_vars_UE_g[0][0]->lte_frame_parms.nb_antennas_rx)); 
Raymond Knopp's avatar
 
Raymond Knopp committed
2507 2508
    openair0_cfg[card].Mod_id = 0;
#ifdef ETHERNET
ghaddab's avatar
ghaddab committed
2509
    if (UE_flag){
Raymond Knopp's avatar
 
Raymond Knopp committed
2510 2511 2512
      printf("ETHERNET: Configuring UE ETH for %s:%d\n",rrh_UE_ip,rrh_UE_port);
      openair0_cfg[card].rrh_ip   = &rrh_UE_ip[0];
      openair0_cfg[card].rrh_port = rrh_UE_port;
ghaddab's avatar
ghaddab committed
2513 2514
    }
    else
Raymond Knopp's avatar
 
Raymond Knopp committed
2515 2516 2517 2518 2519
      {
	printf("ETHERNET: Configuring eNB ETH for %s:%d\n",rrh_eNB_ip,rrh_eNB_port);
	openair0_cfg[card].rrh_ip   = &rrh_eNB_ip[0];
	openair0_cfg[card].rrh_port = rrh_eNB_port;
      }
Raymond Knopp's avatar
 
Raymond Knopp committed
2520
#endif
2521 2522 2523
    openair0_cfg[card].sample_rate = sample_rate;
    openair0_cfg[card].tx_bw = bw;
    openair0_cfg[card].rx_bw = bw;
2524 2525 2526
    // in the case of the USRP, the following variables need to be initialized before the init
    // since the USRP only supports one CC (for the moment), we initialize all the cards with first CC. 
    // in the case of EXMIMO2, these values are overwirtten in the function setup_eNB/UE_buffer
2527
#ifndef EXMIMO
Raymond Knopp's avatar
 
Raymond Knopp committed
2528 2529
    openair0_cfg[card].tx_num_channels=min(2,((UE_flag==0) ? PHY_vars_eNB_g[0][0]->lte_frame_parms.nb_antennas_tx : PHY_vars_UE_g[0][0]->lte_frame_parms.nb_antennas_tx));
    openair0_cfg[card].rx_num_channels=min(2,((UE_flag==0) ? PHY_vars_eNB_g[0][0]->lte_frame_parms.nb_antennas_rx : PHY_vars_UE_g[0][0]->lte_frame_parms.nb_antennas_rx));
2530
    for (i=0;i<4;i++) {
Raymond Knopp's avatar
 
Raymond Knopp committed
2531

2532
      openair0_cfg[card].tx_gain[i] = tx_gain[0][i];
Raymond Knopp's avatar
 
Raymond Knopp committed
2533
      openair0_cfg[card].rx_gain[i] = ((UE_flag==0) ? PHY_vars_eNB_g[0][0]->rx_total_gain_eNB_dB : 
2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547
                                       PHY_vars_UE_g[0][0]->rx_total_gain_dB) - USRP_GAIN_OFFSET;  // calibrated for USRP B210 @ 2.6 GHz, 30.72 MS/s
      switch(frame_parms[0]->N_RB_DL) {
      case 6:
	openair0_cfg[card].rx_gain[i] -= 6;
	break;
      case 25:
	openair0_cfg[card].rx_gain[i] += 6;
	break;
      case 50:
	openair0_cfg[card].rx_gain[i] += 8;
	break;
      default:
	break;
      }
2548 2549
      openair0_cfg[card].tx_freq[i] = (UE_flag==0) ? downlink_frequency[0][i] : downlink_frequency[0][i]+uplink_frequency_offset[0][i];
      openair0_cfg[card].rx_freq[i] = (UE_flag==0) ? downlink_frequency[0][i] + uplink_frequency_offset[0][i] : downlink_frequency[0][i];
2550 2551
      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],
Raymond Knopp's avatar
 
Raymond Knopp committed
2552 2553 2554 2555
	     openair0_cfg[card].rx_gain[i],
	     openair0_cfg[card].tx_freq[i],
	     openair0_cfg[card].rx_freq[i]);
      
Raymond Knopp's avatar
 
Raymond Knopp committed
2556
    }
2557
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
2558
  }
2559

2560
  printf("Initializing openair0 ...");
2561
  if (openair0_device_init(&openair0, &openair0_cfg[0]) <0) {
Raymond Knopp's avatar
 
Raymond Knopp committed
2562 2563 2564
    printf("Exiting, cannot initialize device\n");
    exit(-1);
  }
2565
  printf("Done\n");
2566 2567 2568

  mac_xface = malloc(sizeof(MAC_xface));

2569
#ifdef OPENAIR2
Raymond Knopp's avatar
 
Raymond Knopp committed
2570
  int eMBMS_active=0;
2571

Raymond Knopp's avatar
 
Raymond Knopp committed
2572
  l2_init(frame_parms[0],eMBMS_active,(uecap_xer_in==1)?uecap_xer:NULL,
Raymond Knopp's avatar
 
Raymond Knopp committed
2573 2574 2575 2576 2577 2578
	  0,// cba_group_active
	  0); // HO flag
  if (UE_flag == 1)
    mac_xface->dl_phy_sync_success (0, 0, 0, 1);
  else
    mac_xface->mrbch_phy_sync_failure (0, 0, 0);
2579 2580
#endif

Raymond Knopp's avatar
 
Raymond Knopp committed
2581
  mac_xface->macphy_exit = &exit_fun;
2582

winckel's avatar
winckel committed
2583
#if defined(ENABLE_ITTI)
Raymond Knopp's avatar
 
Raymond Knopp committed
2584
  if (create_tasks(UE_flag ? 0 : 1, UE_flag ? 1 : 0) < 0) {
Florian Kaltenberger's avatar
Florian Kaltenberger committed
2585
    printf("cannot create ITTI tasks\n");
Raymond Knopp's avatar
 
Raymond Knopp committed
2586 2587 2588
    exit(-1); // need a softer mode
  }
  printf("ITTI tasks created\n");
winckel's avatar
winckel committed
2589
#endif
Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
2590 2591

#ifdef OPENAIR2
2592
  printf("Filling UE band info\n");
Raymond Knopp's avatar
 
Raymond Knopp committed
2593 2594
  if (UE_flag==1)
    fill_ue_band_info();
Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
2595
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
2596

2597
  /* #ifdef OPENAIR2
Raymond Knopp's avatar
 
Raymond Knopp committed
2598 2599 2600 2601 2602 2603
  //if (otg_enabled) {
  init_all_otg(0);
  g_otg->seed = 0;
  init_seeds(g_otg->seed);
  g_otg->num_nodes = 2;
  for (i=0; i<g_otg->num_nodes; i++){
Raymond Knopp's avatar
 
Raymond Knopp committed
2604 2605 2606 2607 2608 2609
  for (j=0; j<g_otg->num_nodes; j++){
  g_otg->application_idx[i][j] = 1;
  //g_otg->packet_gen_type=SUBSTRACT_STRING;
  g_otg->aggregation_level[i][j][0]=1;
  g_otg->application_type[i][j][0] = BCBR; //MCBR, BCBR
  }
Raymond Knopp's avatar
 
Raymond Knopp committed
2610 2611 2612
  }
  init_predef_traffic(UE_flag ? 1 : 0, UE_flag ? 0 : 1);
  //  }
2613
  #endif */
Raymond Knopp's avatar
 
Raymond Knopp committed
2614

2615
#ifdef EXMIMO
2616
  number_of_cards = openair0_num_detected_cards;
2617 2618 2619
#else
  number_of_cards = 1;
#endif 
2620

2621

2622

Florian Kaltenberger's avatar
Florian Kaltenberger committed
2623 2624
  for(CC_id=0;CC_id<MAX_NUM_CCs;CC_id++) {
    rf_map[CC_id].card=0;
Raymond Knopp's avatar
 
Raymond Knopp committed
2625
    rf_map[CC_id].chain=CC_id+1;
Florian Kaltenberger's avatar
Florian Kaltenberger committed
2626
  }
2627

Raymond Knopp's avatar
 
Raymond Knopp committed
2628 2629
  // connect the TX/RX buffers
  if (UE_flag==1) {
2630
    openair_daq_vars.timing_advance = 0;//170;
2631
    if (setup_ue_buffers(UE,&openair0_cfg[0],rf_map)!=0) {
2632 2633 2634
      printf("Error setting up eNB buffer\n");
      exit(-1);
    }
2635 2636
    printf("Setting UE buffer to all-RX\n");
    // Set LSBs for antenna switch (ExpressMIMO)
Raymond Knopp's avatar
 
Raymond Knopp committed
2637
    for (CC_id=0;CC_id<MAX_NUM_CCs;CC_id++) {
Raymond Knopp's avatar
 
Raymond Knopp committed
2638
      for (i=0; i<frame_parms[CC_id]->samples_per_tti*10; i++)
2639
	for (aa=0; aa<frame_parms[CC_id]->nb_antennas_tx; aa++)
2640
	  UE[CC_id]->lte_ue_common_vars.txdata[aa][i] = 0x00010001;
Raymond Knopp's avatar
 
Raymond Knopp committed
2641
    }
Raymond Knopp's avatar
 
Raymond Knopp committed
2642 2643 2644
    //p_exmimo_config->framing.tdd_config = TXRXSWITCH_TESTRX;
  }
  else {
2645
    openair_daq_vars.timing_advance = 0;
2646 2647 2648 2649 2650 2651
    if (setup_eNB_buffers(PHY_vars_eNB_g[0],&openair0_cfg[0],rf_map)!=0) {
      printf("Error setting up eNB buffer\n");
      exit(-1);
    }
    printf("Setting eNB buffer to all-RX\n");
    // Set LSBs for antenna switch (ExpressMIMO)
Raymond Knopp's avatar
 
Raymond Knopp committed
2652
    for (CC_id=0;CC_id<MAX_NUM_CCs;CC_id++) {
Raymond Knopp's avatar
 
Raymond Knopp committed
2653 2654
      for (i=0; i<frame_parms[CC_id]->samples_per_tti*10; i++)
	for (aa=0; aa<frame_parms[CC_id]->nb_antennas_tx; aa++)
2655
	  PHY_vars_eNB_g[0][CC_id]->lte_eNB_common_vars.txdata[0][aa][i] = 0x00010001;
Raymond Knopp's avatar
 
Raymond Knopp committed
2656
    }
Raymond Knopp's avatar
 
Raymond Knopp committed
2657
  }
2658
#ifdef EXMIMO
2659
  openair0_config(&openair0_cfg[0],UE_flag);
Raymond Knopp's avatar
 
Raymond Knopp committed
2660 2661
#endif

Raymond Knopp's avatar
 
Raymond Knopp committed
2662
  /*  
Raymond Knopp's avatar
 
Raymond Knopp committed
2663 2664
      for (ant=0;ant<4;ant++)
      p_exmimo_config->rf.do_autocal[ant] = 0;
Raymond Knopp's avatar
 
Raymond Knopp committed
2665
  */
2666 2667

#ifdef EMOS
Raymond Knopp's avatar
 
Raymond Knopp committed
2668 2669 2670 2671 2672 2673 2674 2675 2676
  error_code = rtf_create(CHANSOUNDER_FIFO_MINOR,CHANSOUNDER_FIFO_SIZE);
  if (error_code==0)
    printf("[OPENAIR][SCHED][INIT] Created EMOS FIFO %d\n",CHANSOUNDER_FIFO_MINOR);
  else if (error_code==ENODEV)
    printf("[OPENAIR][SCHED][INIT] Problem: EMOS FIFO %d is greater than or equal to RTF_NO\n",CHANSOUNDER_FIFO_MINOR);
  else if (error_code==ENOMEM)
    printf("[OPENAIR][SCHED][INIT] Problem: cannot allocate memory for EMOS FIFO %d\n",CHANSOUNDER_FIFO_MINOR);
  else 
    printf("[OPENAIR][SCHED][INIT] Problem creating EMOS FIFO %d, error_code %d\n",CHANSOUNDER_FIFO_MINOR,error_code);
2677 2678
#endif

Raymond Knopp's avatar
 
Raymond Knopp committed
2679
  mlockall(MCL_CURRENT | MCL_FUTURE);
2680 2681

#ifdef RTAI
Raymond Knopp's avatar
 
Raymond Knopp committed
2682
  // make main thread LXRT soft realtime
2683
  /* task = */ rt_task_init_schmod(nam2num("MAIN"), 9, 0, 0, SCHED_FIFO, 0xF);
Raymond Knopp's avatar
 
Raymond Knopp committed
2684 2685 2686 2687 2688

  // start realtime timer and scheduler
  //rt_set_oneshot_mode();
  rt_set_periodic_mode();
  start_rt_timer(0);
2689
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
2690

Raymond Knopp's avatar
 
Raymond Knopp committed
2691 2692
  pthread_cond_init(&sync_cond,NULL);
  pthread_mutex_init(&sync_mutex, NULL);
2693

2694
#if defined(ENABLE_ITTI)
Florian Kaltenberger's avatar
Florian Kaltenberger committed
2695 2696 2697 2698 2699
  // Wait for eNB application initialization to be complete (eNB registration to MME)
  if (UE_flag==0) {
    printf("Waiting for eNB application to be ready\n");
    wait_system_ready ("Waiting for eNB application to be ready %s\r", &start_eNB);
  }
2700
#endif
2701

Raymond Knopp's avatar
 
Raymond Knopp committed
2702 2703

  // this starts the DMA transfers
2704
#ifdef EXMIMO
Raymond Knopp's avatar
 
Raymond Knopp committed
2705
  if (UE_flag!=1)
2706 2707
    for (card=0;card<openair0_num_detected_cards;card++)
      openair0_start_rt_acquisition(card);
Raymond Knopp's avatar
 
Raymond Knopp committed
2708
#endif
2709 2710

#ifdef XFORMS
Raymond Knopp's avatar
 
Raymond Knopp committed
2711 2712
  if (do_forms==1) {
    fl_initialize (&argc, argv, NULL, 0, 0);
2713 2714

    if (UE_flag==0) {
2715
      form_stats_l2 = create_form_stats_form();
2716 2717 2718
      fl_show_form (form_stats_l2->stats_form, FL_PLACE_HOTSPOT, FL_FULLBORDER, "l2 stats");
      form_stats = create_form_stats_form();
      fl_show_form (form_stats->stats_form, FL_PLACE_HOTSPOT, FL_FULLBORDER, "stats");
Raymond Knopp's avatar
 
Raymond Knopp committed
2719 2720 2721 2722 2723 2724 2725
      for(UE_id=0;UE_id<scope_enb_num_ue;UE_id++) {
	form_enb[UE_id] = create_lte_phy_scope_enb();
	sprintf (title, "UE%d LTE UL SCOPE eNB",UE_id+1);
	fl_show_form (form_enb[UE_id]->lte_phy_scope_enb, FL_PLACE_HOTSPOT, FL_FULLBORDER, title);
	if (otg_enabled) {
	  fl_set_button(form_enb[UE_id]->button_0,1);
	  fl_set_object_label(form_enb[UE_id]->button_0,"DL Traffic ON");
Raymond Knopp's avatar
 
Raymond Knopp committed
2726 2727
	}
	else {
Raymond Knopp's avatar
 
Raymond Knopp committed
2728 2729
	  fl_set_button(form_enb[UE_id]->button_0,0);
	  fl_set_object_label(form_enb[UE_id]->button_0,"DL Traffic OFF");
Raymond Knopp's avatar
 
Raymond Knopp committed
2730
	}
2731 2732
      }
    }
Raymond Knopp's avatar
 
Raymond Knopp committed
2733
    else {
2734 2735 2736 2737 2738 2739 2740
      form_stats = create_form_stats_form();
      fl_show_form (form_stats->stats_form, FL_PLACE_HOTSPOT, FL_FULLBORDER, "stats");
      UE_id = 0;
      form_ue[UE_id] = create_lte_phy_scope_ue();
      sprintf (title, "LTE DL SCOPE UE");
      fl_show_form (form_ue[UE_id]->lte_phy_scope_ue, FL_PLACE_HOTSPOT, FL_FULLBORDER, title);

Raymond Knopp's avatar
 
Raymond Knopp committed
2741 2742 2743 2744 2745 2746 2747 2748 2749 2750
      if (openair_daq_vars.use_ia_receiver) {
	fl_set_button(form_ue[UE_id]->button_0,1);
	fl_set_object_label(form_ue[UE_id]->button_0, "IA Receiver ON");
      }
      else {
	fl_set_button(form_ue[UE_id]->button_0,0);
	fl_set_object_label(form_ue[UE_id]->button_0, "IA Receiver OFF");
      }
    }

Raymond Knopp's avatar
 
Raymond Knopp committed
2751
    ret = pthread_create(&forms_thread, NULL, scope_thread, NULL);
Raymond Knopp's avatar
 
Raymond Knopp committed
2752 2753
    printf("Scope thread created, ret=%d\n",ret);
  }
2754 2755 2756
#endif

#ifdef EMOS
Raymond Knopp's avatar
 
Raymond Knopp committed
2757 2758
  ret = pthread_create(&thread3, NULL, emos_thread, NULL);
  printf("EMOS thread created, ret=%d\n",ret);
2759 2760
#endif

Raymond Knopp's avatar
 
Raymond Knopp committed
2761
  rt_sleep_ns(10*FRAME_PERIOD);
2762 2763

#ifndef RTAI
Raymond Knopp's avatar
 
Raymond Knopp committed
2764
  pthread_attr_init (&attr_dlsch_threads);
2765
  pthread_attr_setstacksize(&attr_dlsch_threads,4*PTHREAD_STACK_MIN);
2766

2767
  pthread_attr_init (&attr_UE_thread);
2768
  pthread_attr_setstacksize(&attr_UE_thread,8192);//5*PTHREAD_STACK_MIN);
2769

2770
#ifndef LOWLATENCY
2771 2772
  sched_param_UE_thread.sched_priority = sched_get_priority_max(SCHED_FIFO);
  pthread_attr_setschedparam(&attr_UE_thread,&sched_param_UE_thread);
2773
  sched_param_dlsch.sched_priority = sched_get_priority_max(SCHED_FIFO); //OPENAIR_THREAD_PRIORITY;
Raymond Knopp's avatar
 
Raymond Knopp committed
2774 2775
  pthread_attr_setschedparam  (&attr_dlsch_threads, &sched_param_dlsch);
  pthread_attr_setschedpolicy (&attr_dlsch_threads, SCHED_FIFO);
2776
#endif 
Raymond Knopp's avatar
 
Raymond Knopp committed
2777

2778 2779
#endif

Raymond Knopp's avatar
 
Raymond Knopp committed
2780 2781
  // start the main thread
  if (UE_flag == 1) {
2782
    printf("Intializing UE Threads ...\n");
Raymond Knopp's avatar
 
Raymond Knopp committed
2783
    init_UE_threads();
2784 2785 2786 2787 2788
#ifdef DLSCH_THREAD
    init_rx_pdsch_thread();
    rt_sleep_ns(FRAME_PERIOD/10);
    init_dlsch_threads();
#endif
2789

2790
    sleep(1);
2791
#ifdef RTAI
2792
    main_ue_thread = rt_thread_create(UE_thread, NULL, 100000000);
2793
#else
2794
    error_code = pthread_create(&main_ue_thread, &attr_UE_thread, UE_thread, NULL);
Raymond Knopp's avatar
 
Raymond Knopp committed
2795 2796 2797 2798 2799 2800 2801
    if (error_code!= 0) {
      LOG_D(HW,"[lte-softmodem.c] Could not allocate UE_thread, error %d\n",error_code);
      return(error_code);
    }
    else {
      LOG_D(HW,"[lte-softmodem.c] Allocate UE_thread successful\n");
    }
2802
#endif
2803
    printf("UE threads created\n");
Raymond Knopp's avatar
 
Raymond Knopp committed
2804 2805
  }
  else {
2806

Raymond Knopp's avatar
 
Raymond Knopp committed
2807 2808
    if (multi_thread>0) {
      init_eNB_proc();
2809
      sleep(1);
Raymond Knopp's avatar
 
Raymond Knopp committed
2810 2811
      LOG_D(HW,"[lte-softmodem.c] eNB threads created\n");
    }
2812
    printf("Creating main eNB_thread \n");
Raymond Knopp's avatar
 
Raymond Knopp committed
2813
#ifdef RTAI
Raymond Knopp's avatar
 
Raymond Knopp committed
2814
    main_eNB_thread = rt_thread_create(eNB_thread, NULL, PTHREAD_STACK_MIN);
Raymond Knopp's avatar
 
Raymond Knopp committed
2815
#else
Raymond Knopp's avatar
 
Raymond Knopp committed
2816
    error_code = pthread_create(&main_eNB_thread, &attr_dlsch_threads, eNB_thread, NULL);
Raymond Knopp's avatar
 
Raymond Knopp committed
2817 2818 2819 2820 2821 2822
    if (error_code!= 0) {
      LOG_D(HW,"[lte-softmodem.c] Could not allocate eNB_thread, error %d\n",error_code);
      return(error_code);
    }
    else {
      LOG_D(HW,"[lte-softmodem.c] Allocate eNB_thread successful\n");
Raymond Knopp's avatar
 
Raymond Knopp committed
2823
    }
Raymond Knopp's avatar
 
Raymond Knopp committed
2824 2825
#endif
  }
2826

Raymond Knopp's avatar
 
Raymond Knopp committed
2827
  // Sleep to allow all threads to setup
Raymond Knopp's avatar
 
Raymond Knopp committed
2828
  sleep(1);
Raymond Knopp's avatar
 
Raymond Knopp committed
2829

2830 2831 2832 2833 2834 2835
#ifdef USE_MME
  while (start_UE == 0) {
    sleep(1);
  }
#endif

2836
#ifndef EXMIMO
Raymond Knopp's avatar
 
Raymond Knopp committed
2837
#ifndef USRP_DEBUG
Raymond Knopp's avatar
 
Raymond Knopp committed
2838
  openair0.trx_start_func(&openair0);
Raymond Knopp's avatar
 
Raymond Knopp committed
2839
  //  printf("returning from usrp start streaming: %llu\n",get_usrp_time(&openair0));
Raymond Knopp's avatar
 
Raymond Knopp committed
2840
#endif
2841 2842
#endif

2843

Raymond Knopp's avatar
 
Raymond Knopp committed
2844
  pthread_mutex_lock(&sync_mutex);
2845
  sync_var=0;
Raymond Knopp's avatar
 
Raymond Knopp committed
2846 2847
  pthread_cond_broadcast(&sync_cond);
  pthread_mutex_unlock(&sync_mutex);
2848

Raymond Knopp's avatar
 
Raymond Knopp committed
2849 2850 2851
  // wait for end of program
  printf("TYPE <CTRL-C> TO TERMINATE\n");
  //getchar();
2852 2853

#if defined(ENABLE_ITTI)
Raymond Knopp's avatar
 
Raymond Knopp committed
2854 2855
  printf("Entering ITTI signals handler\n");
  itti_wait_tasks_end();
2856
  oai_exit=1;
2857
#else
Raymond Knopp's avatar
 
Raymond Knopp committed
2858 2859
  while (oai_exit==0)
    rt_sleep_ns(FRAME_PERIOD);
2860
#endif
2861

Raymond Knopp's avatar
 
Raymond Knopp committed
2862
  // stop threads
2863
#ifdef XFORMS
Raymond Knopp's avatar
 
Raymond Knopp committed
2864 2865 2866
  printf("waiting for XFORMS thread\n");
  if (do_forms==1)
    {
Raymond Knopp's avatar
 
Raymond Knopp committed
2867
      pthread_join(forms_thread,&status);
Raymond Knopp's avatar
 
Raymond Knopp committed
2868 2869 2870
      fl_hide_form(form_stats->stats_form);
      fl_free_form(form_stats->stats_form);
      if (UE_flag==1) {
2871 2872
	fl_hide_form(form_ue[0]->lte_phy_scope_ue);
	fl_free_form(form_ue[0]->lte_phy_scope_ue);
Raymond Knopp's avatar
 
Raymond Knopp committed
2873
      } else {
2874 2875
	fl_hide_form(form_stats_l2->stats_form);
	fl_free_form(form_stats_l2->stats_form);
Raymond Knopp's avatar
 
Raymond Knopp committed
2876 2877 2878 2879
	for(UE_id=0;UE_id<scope_enb_num_ue;UE_id++) {
	  fl_hide_form(form_enb[UE_id]->lte_phy_scope_enb);
	  fl_free_form(form_enb[UE_id]->lte_phy_scope_enb);
	}
Raymond Knopp's avatar
 
Raymond Knopp committed
2880
      }
Raymond Knopp's avatar
 
Raymond Knopp committed
2881
    }
2882 2883
#endif

Raymond Knopp's avatar
 
Raymond Knopp committed
2884 2885 2886
  printf("stopping MODEM threads\n");
  // cleanup
  if (UE_flag == 1) {
2887
#ifdef EXMIMO
2888
#ifdef RTAI
Raymond Knopp's avatar
 
Raymond Knopp committed
2889
    rt_thread_join(main_ue_thread); 
2890
#else
Raymond Knopp's avatar
 
Raymond Knopp committed
2891
    pthread_join(main_ue_thread,&status); 
2892 2893
#endif
#ifdef DLSCH_THREAD
Raymond Knopp's avatar
 
Raymond Knopp committed
2894 2895
    cleanup_dlsch_threads();
    cleanup_rx_pdsch_thread();
2896
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
2897 2898 2899
#endif
  }
  else {
Raymond Knopp's avatar
 
Raymond Knopp committed
2900
#ifdef DEBUG_THREADS
Raymond Knopp's avatar
 
Raymond Knopp committed
2901
    printf("Joining eNB_thread ...");
Raymond Knopp's avatar
 
Raymond Knopp committed
2902
#endif
2903 2904 2905
#ifdef RTAI
    rt_thread_join(main_eNB_thread); 
#else
Raymond Knopp's avatar
 
Raymond Knopp committed
2906
    pthread_join(main_eNB_thread,(void**)&eNB_thread_status_p); 
Raymond Knopp's avatar
 
Raymond Knopp committed
2907
#ifdef DEBUG_THREADS
Raymond Knopp's avatar
 
Raymond Knopp committed
2908
    printf("status %d\n",*eNB_thread_status_p);
Raymond Knopp's avatar
 
Raymond Knopp committed
2909
#endif
2910
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
2911

Raymond Knopp's avatar
 
Raymond Knopp committed
2912 2913 2914
    if (multi_thread>0) {
      printf("Killing eNB processing threads\n");
      kill_eNB_proc();
Raymond Knopp's avatar
 
Raymond Knopp committed
2915

Raymond Knopp's avatar
 
Raymond Knopp committed
2916
    }
Raymond Knopp's avatar
 
Raymond Knopp committed
2917
  }
2918

2919
#ifdef OPENAIR2
Raymond Knopp's avatar
 
Raymond Knopp committed
2920
  //cleanup_pdcp_thread();
2921 2922
#endif

2923
#ifdef RTAI
Raymond Knopp's avatar
 
Raymond Knopp committed
2924
  stop_rt_timer();
2925
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
2926 2927
  pthread_cond_destroy(&sync_cond);
  pthread_mutex_destroy(&sync_mutex);
2928

2929

2930
#ifdef EXMIMO
Raymond Knopp's avatar
 
Raymond Knopp committed
2931
  printf("stopping card\n");
2932
  openair0_stop(0);
Raymond Knopp's avatar
 
Raymond Knopp committed
2933 2934
  printf("closing openair0_lib\n");
  openair0_close();
Raymond Knopp's avatar
 
Raymond Knopp committed
2935
#endif
2936 2937

#ifdef EMOS
Raymond Knopp's avatar
 
Raymond Knopp committed
2938 2939 2940
  printf("waiting for EMOS thread\n");
  pthread_cancel(thread3);
  pthread_join(thread3,&status);
2941 2942 2943
#endif

#ifdef EMOS
Raymond Knopp's avatar
 
Raymond Knopp committed
2944 2945
  error_code = rtf_destroy(CHANSOUNDER_FIFO_MINOR);
  printf("[OPENAIR][SCHED][CLEANUP] EMOS FIFO closed, error_code %d\n", error_code);
2946 2947
#endif

Raymond Knopp's avatar
 
Raymond Knopp committed
2948 2949
  if (ouput_vcd)
    vcd_signal_dumper_close();
2950

Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
2951
#ifdef OPENAIR2
2952 2953
  if (opt_type != OPT_NONE)
    terminate_opt();
Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
2954
#endif
2955

Raymond Knopp's avatar
 
Raymond Knopp committed
2956
  logClean();
2957

Raymond Knopp's avatar
 
Raymond Knopp committed
2958 2959
  return 0;
}
2960 2961


2962 2963 2964 2965 2966
/* this function maps the phy_vars_eNB tx and rx buffers to the available rf chains. 
   Each rf chain is is addressed by the card number and the chain on the card. The 
   rf_map specifies for each CC, on which rf chain the mapping should start. Multiple 
   antennas are mapped to successive RF chains on the same card. */
int setup_eNB_buffers(PHY_VARS_eNB **phy_vars_eNB, openair0_config_t *openair0_cfg, openair0_rf_map rf_map[MAX_NUM_CCs]) {
2967

2968
  int i, CC_id;
2969
#ifndef EXMIMO
Raymond Knopp's avatar
 
Raymond Knopp committed
2970
  uint16_t N_TA_offset = 0;
Raymond Knopp's avatar
 
Raymond Knopp committed
2971 2972
#else
  int j;
Raymond Knopp's avatar
 
Raymond Knopp committed
2973
#endif
2974
  LTE_DL_FRAME_PARMS *frame_parms;
2975

Raymond Knopp's avatar
 
Raymond Knopp committed
2976

2977 2978
  for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
    if (phy_vars_eNB[CC_id]) {
Raymond Knopp's avatar
 
Raymond Knopp committed
2979 2980 2981
      frame_parms = &(phy_vars_eNB[CC_id]->lte_frame_parms);
      printf("setup_eNB_buffers: frame_parms = %p\n",frame_parms);
    } 
2982 2983 2984
    else {
      printf("phy_vars_eNB[%d] not initialized\n", CC_id);
      return(-1);
Raymond Knopp's avatar
 
Raymond Knopp committed
2985
    }
Raymond Knopp's avatar
 
Raymond Knopp committed
2986

2987
#ifndef EXMIMO
2988 2989
    if (frame_parms->frame_type == TDD) {
      if (frame_parms->N_RB_DL == 100)
Raymond Knopp's avatar
 
Raymond Knopp committed
2990
	N_TA_offset = 624;
2991
      else if (frame_parms->N_RB_DL == 50)
Raymond Knopp's avatar
 
Raymond Knopp committed
2992
	N_TA_offset = 624/2;
2993
      else if (frame_parms->N_RB_DL == 25)
Raymond Knopp's avatar
 
Raymond Knopp committed
2994 2995
	N_TA_offset = 624/4;
    }
Raymond Knopp's avatar
 
Raymond Knopp committed
2996
#endif
2997 2998 2999 3000 3001
   

   

  
Raymond Knopp's avatar
 
Raymond Knopp committed
3002
    // replace RX signal buffers with mmaped HW versions
3003
#ifdef EXMIMO
Raymond Knopp's avatar
 
Raymond Knopp committed
3004 3005 3006
    openair0_cfg[CC_id].tx_num_channels = 0;
    openair0_cfg[CC_id].rx_num_channels = 0;

3007 3008 3009 3010 3011 3012 3013 3014 3015 3016
    for (i=0;i<frame_parms->nb_antennas_rx;i++) {
      printf("Mapping eNB CC_id %d, rx_ant %d, freq %u on card %d, chain %d\n",CC_id,i,downlink_frequency[CC_id][i]+uplink_frequency_offset[CC_id][i],rf_map[CC_id].card,rf_map[CC_id].chain+i);
      free(phy_vars_eNB[CC_id]->lte_eNB_common_vars.rxdata[0][i]);
      phy_vars_eNB[CC_id]->lte_eNB_common_vars.rxdata[0][i] = (int32_t*) openair0_exmimo_pci[rf_map[CC_id].card].adc_head[rf_map[CC_id].chain+i];
      if (openair0_cfg[rf_map[CC_id].card].rx_freq[rf_map[CC_id].chain+i]) {
	printf("Error with rf_map! A channel has already been allocated!\n");
	return(-1);
      }
      else {
	openair0_cfg[rf_map[CC_id].card].rx_freq[rf_map[CC_id].chain+i] = downlink_frequency[CC_id][i]+uplink_frequency_offset[CC_id][i];
3017
	openair0_cfg[rf_map[CC_id].card].rx_gain[rf_map[CC_id].chain+i] = rx_gain[CC_id][i];
3018 3019 3020
	openair0_cfg[rf_map[CC_id].card].rx_num_channels++;
      }
      printf("rxdata[%d] @ %p\n",i,phy_vars_eNB[CC_id]->lte_eNB_common_vars.rxdata[0][i]);
Raymond Knopp's avatar
 
Raymond Knopp committed
3021
      for (j=0;j<16;j++) {
3022 3023
	printf("rxbuffer %d: %x\n",j,phy_vars_eNB[CC_id]->lte_eNB_common_vars.rxdata[0][i][j]);
	phy_vars_eNB[CC_id]->lte_eNB_common_vars.rxdata[0][i][j] = 16-j;
3024
      }
Raymond Knopp's avatar
 
Raymond Knopp committed
3025
    }
3026 3027 3028 3029 3030 3031 3032 3033 3034 3035
    for (i=0;i<frame_parms->nb_antennas_tx;i++) {
      printf("Mapping eNB CC_id %d, tx_ant %d, freq %u on card %d, chain %d\n",CC_id,i,downlink_frequency[CC_id][i],rf_map[CC_id].card,rf_map[CC_id].chain+i);
      free(phy_vars_eNB[CC_id]->lte_eNB_common_vars.txdata[0][i]);
      phy_vars_eNB[CC_id]->lte_eNB_common_vars.txdata[0][i] = (int32_t*) openair0_exmimo_pci[rf_map[CC_id].card].dac_head[rf_map[CC_id].chain+i];
      if (openair0_cfg[rf_map[CC_id].card].tx_freq[rf_map[CC_id].chain+i]) {
	printf("Error with rf_map! A channel has already been allocated!\n");
	return(-1);
      }
      else {
	openair0_cfg[rf_map[CC_id].card].tx_freq[rf_map[CC_id].chain+i] = downlink_frequency[CC_id][i];
3036
	openair0_cfg[rf_map[CC_id].card].tx_gain[rf_map[CC_id].chain+i] = tx_gain[CC_id][i];
3037 3038 3039 3040
	openair0_cfg[rf_map[CC_id].card].tx_num_channels++;
      }
      
      printf("txdata[%d] @ %p\n",i,phy_vars_eNB[CC_id]->lte_eNB_common_vars.txdata[0][i]);
Raymond Knopp's avatar
 
Raymond Knopp committed
3041
      for (j=0;j<16;j++) {
3042 3043
	printf("txbuffer %d: %x\n",j,phy_vars_eNB[CC_id]->lte_eNB_common_vars.txdata[0][i][j]);
	phy_vars_eNB[CC_id]->lte_eNB_common_vars.txdata[0][i][j] = 16-j;
3044
      }
3045
    }
3046
#else // not EXMIMO
3047 3048 3049
    rxdata = (int32_t**)malloc16(frame_parms->nb_antennas_rx*sizeof(int32_t*));
    txdata = (int32_t**)malloc16(frame_parms->nb_antennas_tx*sizeof(int32_t*));

3050 3051
    for (i=0;i<frame_parms->nb_antennas_rx;i++) {
      free(phy_vars_eNB[CC_id]->lte_eNB_common_vars.rxdata[0][i]);
Raymond Knopp's avatar
 
Raymond Knopp committed
3052
      rxdata[i] = (int32_t*)(16 + malloc16(16+samples_per_frame*sizeof(int32_t)));
3053 3054 3055
      phy_vars_eNB[CC_id]->lte_eNB_common_vars.rxdata[0][i] = rxdata[i]-N_TA_offset; // N_TA offset for TDD
      memset(rxdata[i], 0, samples_per_frame*sizeof(int32_t));
      printf("rxdata[%d] @ %p (%p) (N_TA_OFFSET %d)\n", i, phy_vars_eNB[CC_id]->lte_eNB_common_vars.rxdata[0][i],rxdata[i],N_TA_offset);
Raymond Knopp's avatar
 
Raymond Knopp committed
3056
    }
3057 3058
    for (i=0;i<frame_parms->nb_antennas_tx;i++) {
      free(phy_vars_eNB[CC_id]->lte_eNB_common_vars.txdata[0][i]);
ghaddab's avatar
ghaddab committed
3059
      txdata[i] = (int32_t*)(16 + malloc16(16 + samples_per_frame*sizeof(int32_t)));
3060 3061
      phy_vars_eNB[CC_id]->lte_eNB_common_vars.txdata[0][i] = txdata[i];
      memset(txdata[i], 0, samples_per_frame*sizeof(int32_t));
3062
      printf("txdata[%d] @ %p\n", i, phy_vars_eNB[CC_id]->lte_eNB_common_vars.txdata[0][i]);
3063

Raymond Knopp's avatar
 
Raymond Knopp committed
3064 3065
    }
#endif
3066
  }
3067
  return(0);
Raymond Knopp's avatar
 
Raymond Knopp committed
3068
}
3069

3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088
void reset_opp_meas(void){
  int sfn;
  reset_meas(&softmodem_stats_mt);
  reset_meas(&softmodem_stats_hw);
  for (sfn=0; sfn < 10; sfn++) {
    reset_meas(&softmodem_stats_tx_sf[sfn]);
    reset_meas(&softmodem_stats_rx_sf[sfn]);
  }
}

void print_opp_meas(void){
  int sfn=0;
  print_meas(&softmodem_stats_mt, "Main ENB Thread", NULL, NULL);
  print_meas(&softmodem_stats_hw, "HW Acquisation", NULL, NULL);
  for (sfn=0; sfn < 10; sfn++) {
    print_meas(&softmodem_stats_tx_sf[sfn],"[eNB][total_phy_proc_tx]",NULL, NULL);
    print_meas(&softmodem_stats_rx_sf[sfn],"[eNB][total_phy_proc_rx]",NULL,NULL);
  }
}