lte-softmodem.c 97.5 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 126
# if defined(ENABLE_USE_MME)
#   include "s1ap_eNB.h"
# endif
127 128
#endif

129 130 131
#ifdef XFORMS
#include "PHY/TOOLS/lte_phy_scope.h"
#include "stats.h"
132 133 134 135 136
#endif

#define FRAME_PERIOD    100000000ULL
#define DAQ_PERIOD      66667ULL

Raymond Knopp's avatar
 
Raymond Knopp committed
137
#define DEBUG_THREADS 1
Raymond Knopp's avatar
 
Raymond Knopp committed
138

Raymond Knopp's avatar
 
Raymond Knopp committed
139 140
//#define USRP_DEBUG 1

141 142 143 144 145 146 147 148
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
149

150
openair0_config_t openair0_cfg[MAX_CARDS];
Raymond Knopp's avatar
 
Raymond Knopp committed
151

Raymond Knopp's avatar
 
Raymond Knopp committed
152 153
int32_t **rxdata;
int32_t **txdata;
154 155
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]);
156

157
void fill_ue_band_info(void);
158
#ifdef XFORMS
159 160 161 162
// 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];
163
FD_stats_form                  *form_stats=NULL,*form_stats_l2=NULL;
164
char title[255];
165
unsigned char                   scope_enb_num_ue = 1;
166 167 168
#endif //XFORMS

#ifdef RTAI
169

170 171
static long                      main_eNB_thread;
static long                      main_ue_thread;
172

173
#else
Raymond Knopp's avatar
 
Raymond Knopp committed
174 175
pthread_t                       main_eNB_thread;
pthread_t                       main_ue_thread;
176

177
pthread_attr_t                  attr_dlsch_threads;
178 179
pthread_attr_t                  attr_UE_thread;

180
#ifndef LOWLATENCY
181
struct sched_param              sched_param_dlsch;
182
#endif 
183
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
184

Raymond Knopp's avatar
 
Raymond Knopp committed
185 186
pthread_cond_t sync_cond;
pthread_mutex_t sync_mutex;
187 188
int sync_var=-1;

189

190 191


192
struct sched_param              sched_param_UE_thread;
193 194 195 196

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
197 198
struct sched_param              sched_param_eNB_proc_tx[MAX_NUM_CCs][10];
struct sched_param              sched_param_eNB_proc_rx[MAX_NUM_CCs][10];
199
#endif
200
#ifdef XFORMS
Raymond Knopp's avatar
 
Raymond Knopp committed
201
static pthread_t                forms_thread; //xforms
202 203 204 205
#endif
#ifdef EMOS
static pthread_t                thread3; //emos
#endif
206

Raymond Knopp's avatar
 
Raymond Knopp committed
207
openair0_device openair0;
208

Raymond Knopp's avatar
 
Raymond Knopp committed
209

210
/*
Raymond Knopp's avatar
 
Raymond Knopp committed
211 212 213
  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;
214 215 216 217
*/
//extern unsigned int bigphys_top;
//extern unsigned int mem_base;

218
int                             card = 0;
Raymond Knopp's avatar
 
Raymond Knopp committed
219

220

221
#if defined(ENABLE_ITTI)
222 223
static volatile int             start_eNB = 0;
static volatile int             start_UE = 0;
224
#endif
225 226 227 228 229 230
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
231

232
static char                     UE_flag=0;
233 234
//static uint8_t                  eNB_id=0,UE_id=0;

235

236 237
uint32_t                 downlink_frequency[MAX_NUM_CCs][4];
int32_t                  uplink_frequency_offset[MAX_NUM_CCs][4]; 
Lionel Gauthier's avatar
Lionel Gauthier committed
238

239
openair0_rf_map rf_map[MAX_NUM_CCs];
240

241
static char                    *conf_config_file_name = NULL;
242
#if defined(ENABLE_ITTI)
243
static char                    *itti_dump_file = NULL;
Raymond Knopp's avatar
 
Raymond Knopp committed
244 245
#endif

246 247 248 249
int UE_scan = 1;
runmode_t mode = normal_txrx;


Raymond Knopp's avatar
 
Raymond Knopp committed
250
#ifdef EXMIMO
251 252
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}};
253
// these are for EXMIMO2 target only
254
/*
Raymond Knopp's avatar
 
Raymond Knopp committed
255 256 257
  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};
258 259
*/
// these are for EXMIMO2 card 39
260 261 262 263 264 265 266
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
267
#else
kortke's avatar
kortke committed
268 269
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
270
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
271

Raymond Knopp's avatar
 
Raymond Knopp committed
272 273
double sample_rate=30.72e6;
double bw = 14e6;
274

275
static int                      tx_max_power[MAX_NUM_CCs]; /* =  {0,0}*/;
276

277
#ifndef EXMIMO
Raymond Knopp's avatar
 
Raymond Knopp committed
278 279 280
char ref[128] = "internal";
char channels[128] = "0";

Raymond Knopp's avatar
 
Raymond Knopp committed
281 282
unsigned int samples_per_frame = 307200;
unsigned int tx_forward_nsamps;
Raymond Knopp's avatar
 
Raymond Knopp committed
283 284 285
int tx_delay;

#endif
286 287

int                      rx_input_level_dBm;
288
static int                      online_log_messages=0;
289
#ifdef XFORMS
290
extern int                      otg_enabled;
291
static char                     do_forms=0;
292
#else
293
int                             otg_enabled;
294
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
295
//int                             number_of_cards =   1;
Raymond Knopp's avatar
 
Raymond Knopp committed
296
#ifdef EXMIMO
297 298
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
299
#endif
300

Raymond Knopp's avatar
 
Raymond Knopp committed
301
static LTE_DL_FRAME_PARMS      *frame_parms[MAX_NUM_CCs];
302

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

Raymond Knopp's avatar
 
Raymond Knopp committed
309

310
time_stats_t softmodem_stats_mt; // main thread
Raymond Knopp's avatar
 
Raymond Knopp committed
311
time_stats_t softmodem_stats_hw; //  hw acquisition
312 313
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
314 315
void reset_opp_meas(void);
void print_opp_meas(void);
316
int transmission_mode=1;
Raymond Knopp's avatar
 
Raymond Knopp committed
317

Raymond Knopp's avatar
 
Raymond Knopp committed
318

319
int16_t           glog_level         = LOG_INFO;
Lionel Gauthier's avatar
 
Lionel Gauthier committed
320
int16_t           glog_verbosity     = LOG_MED;
321
int16_t           hw_log_level       = LOG_INFO;
Lionel Gauthier's avatar
 
Lionel Gauthier committed
322
int16_t           hw_log_verbosity   = LOG_MED;
323
int16_t           phy_log_level      = LOG_INFO;
Lionel Gauthier's avatar
 
Lionel Gauthier committed
324
int16_t           phy_log_verbosity  = LOG_MED;
325
int16_t           mac_log_level      = LOG_INFO;
Lionel Gauthier's avatar
 
Lionel Gauthier committed
326
int16_t           mac_log_verbosity  = LOG_MED;
327
int16_t           rlc_log_level      = LOG_INFO;
Lionel Gauthier's avatar
 
Lionel Gauthier committed
328
int16_t           rlc_log_verbosity  = LOG_MED;
329
int16_t           pdcp_log_level     = LOG_INFO;
Lionel Gauthier's avatar
 
Lionel Gauthier committed
330
int16_t           pdcp_log_verbosity = LOG_MED;
331
int16_t           rrc_log_level      = LOG_INFO;
Lionel Gauthier's avatar
 
Lionel Gauthier committed
332 333 334 335 336 337 338
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
339 340 341 342
#if defined (ENABLE_SECURITY)
int16_t           osa_log_level      = LOG_INFO;
int16_t           osa_log_verbosity  = LOG_MED;
#endif 
343

Raymond Knopp's avatar
 
Raymond Knopp committed
344 345 346

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

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

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

365 366 367 368 369 370 371 372 373 374 375 376
#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 
377
#if !defined(ENABLE_ITTI)
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
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 {
393
    printf("trying to exit gracefully...\n"); 
394
    oai_exit = 1;
395 396
  }
}
397
#endif
398 399 400

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

  oai_exit = 1;

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

  //rt_sleep_ns(FRAME_PERIOD);

  //exit (-1);
}

Raymond Knopp's avatar
 
Raymond Knopp committed
417 418 419
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
420 421 422 423 424 425 426 427 428
 * 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
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
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
446
 
447
#ifdef XFORMS
448 449 450 451

void reset_stats(FL_OBJECT *button, long arg) {
  int i,j,k;
  PHY_VARS_eNB *phy_vars_eNB = PHY_vars_eNB_g[0][0];
452 453 454 455 456 457 458 459 460 461 462 463
  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
464 465 466
	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;
467
      }
468
    }
469 470 471
    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;
472 473 474
  }
}

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

Raymond Knopp's avatar
 
Raymond Knopp committed
484 485
  sched_param.sched_priority = sched_get_priority_min(SCHED_FIFO)+1; 
  sched_setscheduler(0, SCHED_FIFO,&sched_param);
486

Raymond Knopp's avatar
 
Raymond Knopp committed
487
  printf("Scope thread has priority %d\n",sched_param.sched_priority);
488
    
489 490
# ifdef ENABLE_XFORMS_WRITE_STATS
  if (UE_flag==1) 
Raymond Knopp's avatar
 
Raymond Knopp committed
491
    UE_stats  = fopen("UE_stats.txt", "w");
492
  else 
Raymond Knopp's avatar
 
Raymond Knopp committed
493
    eNB_stats = fopen("eNB_stats.txt", "w");
494
#endif
495
    
Raymond Knopp's avatar
 
Raymond Knopp committed
496 497
  while (!oai_exit) {
    if (UE_flag==1) {
Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
498
      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
499
      fl_set_object_label(form_stats->stats_text, stats_buffer);
500

501 502 503 504
      phy_scope_UE(form_ue[0], 
		   PHY_vars_UE_g[0][0],
		   0,
		   0,7);
505
            
Raymond Knopp's avatar
 
Raymond Knopp committed
506
    } else {
Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
507 508
#ifdef OPENAIR2
      len = dump_eNB_l2_stats (stats_buffer, 0);
509
      fl_set_object_label(form_stats_l2->stats_text, stats_buffer);
Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
510 511 512 513 514
#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);
    
515 516
      fl_set_object_label(form_stats->stats_text, stats_buffer);

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

  printf("%s",stats_buffer);
530
    
531
# ifdef ENABLE_XFORMS_WRITE_STATS
532 533 534 535 536 537 538 539 540 541 542 543 544 545
  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);
    }
  }
546
# endif
547
    
Raymond Knopp's avatar
 
Raymond Knopp committed
548
  pthread_exit((void*)arg);
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
}
#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;
574 575 576 577 578 579 580

  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);
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
 
  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);
    }
603 604
  else 
    printf("[EMOS] Opened GPS, gps_data=%p\n");
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
  
  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
650
	if (UE_flag==0)
651
	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
652
	else
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
	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

714 715


716
#if defined(ENABLE_ITTI)
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
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
735
#if defined(ENABLE_ITTI)
736
void *l2l1_task(void *arg)
737
{
Raymond Knopp's avatar
 
Raymond Knopp committed
738 739 740 741 742
  MessageDef *message_p = NULL;
  int         result;

  itti_set_task_real_time(TASK_L2L1);
  itti_mark_task_ready(TASK_L2L1);
743

Raymond Knopp's avatar
 
Raymond Knopp committed
744 745
  if (UE_flag == 0) {
    /* Wait for the initialize message */
746
    do {
Raymond Knopp's avatar
 
Raymond Knopp committed
747 748 749 750
      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);
      }
751 752 753
      itti_receive_msg (TASK_L2L1, &message_p);

      switch (ITTI_MSG_ID(message_p)) {
Raymond Knopp's avatar
 
Raymond Knopp committed
754 755 756 757 758 759 760
      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
761
	printf("received terminate message\n");
Raymond Knopp's avatar
 
Raymond Knopp committed
762 763 764 765 766 767 768
	oai_exit=1;
	itti_exit_task ();
	break;

      default:
	LOG_E(EMU, "Received unexpected message %s\n", ITTI_MSG_NAME(message_p));
	break;
769
      }
Raymond Knopp's avatar
 
Raymond Knopp committed
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
    } 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;
792

Raymond Knopp's avatar
 
Raymond Knopp committed
793 794 795 796 797 798 799 800 801 802 803 804
    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);
805

Raymond Knopp's avatar
 
Raymond Knopp committed
806
  return NULL;
807 808 809
}
#endif

Raymond Knopp's avatar
 
Raymond Knopp committed
810

Raymond Knopp's avatar
 
Raymond Knopp committed
811
void do_OFDM_mod_rt(int subframe,PHY_VARS_eNB *phy_vars_eNB) {
Raymond Knopp's avatar
 
Raymond Knopp committed
812 813 814 815

  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
816
  int slot_sizeF = (phy_vars_eNB->lte_frame_parms.ofdm_symbol_size)*
Raymond Knopp's avatar
 
Raymond Knopp committed
817
    ((phy_vars_eNB->lte_frame_parms.Ncp==1) ? 6 : 7);
Raymond Knopp's avatar
 
Raymond Knopp committed
818 819 820

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

Raymond Knopp's avatar
 
Raymond Knopp committed
823 824
  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
825
    //	  LOG_D(HW,"Frame %d: Generating slot %d\n",frame,next_slot);
Raymond Knopp's avatar
 
Raymond Knopp committed
826

Raymond Knopp's avatar
 
Raymond Knopp committed
827
    
Raymond Knopp's avatar
 
Raymond Knopp committed
828 829 830 831 832 833 834 835 836 837
    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
838 839 840 841 842 843 844 845
	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
846 847 848 849 850 851
      }
      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
852 853 854 855
	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
856
      }
Raymond Knopp's avatar
 
Raymond Knopp committed
857 858

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


int eNB_thread_tx_status[10];
static void * eNB_thread_tx(void *param) {
Raymond Knopp's avatar
 
Raymond Knopp committed
884 885 886

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

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

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

  if (task==NULL) {
Lionel Gauthier's avatar
Lionel Gauthier committed
909 910 911
  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
912
  else {
Lionel Gauthier's avatar
Lionel Gauthier committed
913 914 915 916 917
  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
918
#else
919 920 921 922 923 924
#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
925
  /* This creates a 1ms reservation every 10ms period*/
926
  attr.sched_policy = SCHED_DEADLINE;
Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
927 928 929
  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
930 931
  
  if (sched_setattr(0, &attr, flags) < 0 ){
Lionel Gauthier's avatar
Lionel Gauthier committed
932 933 934
  perror("[SCHED] eNB tx thread: sched_setattr failed\n");
  exit(-1);
}
935
  LOG_I(HW,"[SCHED] eNB TX deadline thread %d(id %ld) started on CPU %d\n",
Lionel Gauthier's avatar
Lionel Gauthier committed
936
    proc->subframe, gettid(),sched_getcpu());
937 938
#else 
  LOG_I(HW,"[SCHED] eNB TX thread %d started on CPU %d\n",
Lionel Gauthier's avatar
Lionel Gauthier committed
939
    proc->subframe,sched_getcpu());
940 941
#endif

Raymond Knopp's avatar
 
Raymond Knopp committed
942 943 944 945 946 947 948 949 950 951 952 953
#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
954
    
Lionel Gauthier's avatar
Lionel Gauthier committed
955
  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
956
    
Lionel Gauthier's avatar
Lionel Gauthier committed
957 958
  //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
959

960 961
    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
962
      exit_fun("nothing to add");
963 964
    }
    else {
Raymond Knopp's avatar
 
Raymond Knopp committed
965
      
Lionel Gauthier's avatar
Lionel Gauthier committed
966 967 968
  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
969
	
970 971 972 973 974
	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
975
	exit_fun("nothing to add");
976 977 978 979 980
      }
    }
    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]);
981

Lionel Gauthier's avatar
Lionel Gauthier committed
982
  if (oai_exit) break;
Raymond Knopp's avatar
 
Raymond Knopp committed
983

Raymond Knopp's avatar
 
Raymond Knopp committed
984
    
Lionel Gauthier's avatar
Lionel Gauthier committed
985 986 987
  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
988

Lionel Gauthier's avatar
Lionel Gauthier committed
989
  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
990
      
Lionel Gauthier's avatar
Lionel Gauthier committed
991 992 993 994
}
  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
995
    
Lionel Gauthier's avatar
Lionel Gauthier committed
996
  do_OFDM_mod_rt(proc->subframe_tx,PHY_vars_eNB_g[0][proc->CC_id]);  
Raymond Knopp's avatar
 
Raymond Knopp committed
997
    
Lionel Gauthier's avatar
Lionel Gauthier committed
998 999 1000 1001 1002
  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
1003
      
Lionel Gauthier's avatar
Lionel Gauthier committed
1004 1005 1006 1007
  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
1008

Raymond Knopp's avatar
 
Raymond Knopp committed
1009
    
Lionel Gauthier's avatar
Lionel Gauthier committed
1010 1011 1012
  proc->frame_tx++;
  if (proc->frame_tx==1024)
    proc->frame_tx=0;
Raymond Knopp's avatar
 
Raymond Knopp committed
1013

Lionel Gauthier's avatar
Lionel Gauthier committed
1014
}    
1015
  stop_meas(&softmodem_stats_tx_sf[proc->subframe]);
Raymond Knopp's avatar
 
Raymond Knopp committed
1016 1017 1018 1019
  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
1020
  
Raymond Knopp's avatar
 
Raymond Knopp committed
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
#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
1031
  
Raymond Knopp's avatar
 
Raymond Knopp committed
1032 1033 1034 1035 1036
#ifdef DEBUG_THREADS
  printf("Exiting eNB TX thread %d\n",proc->subframe);
#endif
}

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

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

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

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

  if (task==NULL) {
Lionel Gauthier's avatar
Lionel Gauthier committed
1063 1064 1065
  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
1066
  else {
1067 1068
    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
1069
	  proc->subframe,
1070
	  task); /*,rtai_cpuid()*/
Raymond Knopp's avatar
 
Raymond Knopp committed
1071 1072
  }
#else
1073 1074 1075 1076 1077 1078
#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
1079
  /* This creates a 2ms reservation every 10ms period*/
1080
  attr.sched_policy = SCHED_DEADLINE;
Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
1081 1082 1083
  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
1084 1085 1086 1087 1088 1089
  
  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
1090
	proc->subframe, gettid(),sched_getcpu());
1091 1092
#else 
  LOG_I(HW,"[SCHED][eNB] eNB RX thread %d started on CPU %d\n",
Raymond Knopp's avatar
 
Raymond Knopp committed
1093
	proc->subframe,sched_getcpu());
1094 1095
#endif
 
Raymond Knopp's avatar
 
Raymond Knopp committed
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
#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
1110
    
ghaddab's avatar
ghaddab committed
1111
    // 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
1112 1113 1114 1115 1116 1117
    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
1118
	//	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
1119 1120

	pthread_cond_wait(&proc->cond_rx,&proc->mutex_rx);
Raymond Knopp's avatar
 
Raymond Knopp committed
1121
      }
ghaddab's avatar
ghaddab committed
1122
      //      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
1123 1124
      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
1125 1126
      }
    }
Raymond Knopp's avatar
 
Raymond Knopp committed
1127
    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
1128
    vcd_signal_dumper_dump_variable_by_name(VCD_SIGNAL_DUMPER_VARIABLES_FRAME_NUMBER_RX_ENB, proc->frame_rx);
1129 1130
    start_meas(&softmodem_stats_rx_sf[proc->subframe]);
    
Raymond Knopp's avatar
 
Raymond Knopp committed
1131 1132
    if (oai_exit) break;
    
Raymond Knopp's avatar
 
Raymond Knopp committed
1133 1134 1135
    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
1136
    }
Raymond Knopp's avatar
 
Raymond Knopp committed
1137 1138
    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
1139 1140 1141 1142
    }
      
    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
1143 1144
    }
    else {
Raymond Knopp's avatar
 
Raymond Knopp committed
1145
      proc->instance_cnt_rx--;
Raymond Knopp's avatar
 
Raymond Knopp committed
1146
      
Raymond Knopp's avatar
 
Raymond Knopp committed
1147 1148
      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
1149 1150
      }
    }
Raymond Knopp's avatar
 
Raymond Knopp committed
1151 1152 1153 1154

    proc->frame_rx++;
    if (proc->frame_rx==1024)
      proc->frame_rx=0;
Raymond Knopp's avatar
 
Raymond Knopp committed
1155
    
Raymond Knopp's avatar
 
Raymond Knopp committed
1156
  }
1157
  stop_meas(&softmodem_stats_rx_sf[proc->subframe]);
Raymond Knopp's avatar
 
Raymond Knopp committed
1158
  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
1159
#ifdef HARD_RT
Raymond Knopp's avatar
 
Raymond Knopp committed
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
  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
1172 1173
#endif

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



Raymond Knopp's avatar
 
Raymond Knopp committed
1181

1182
void init_eNB_proc(void) {
Raymond Knopp's avatar
 
Raymond Knopp committed
1183 1184

  int i;
Raymond Knopp's avatar
 
Raymond Knopp committed
1185 1186 1187 1188
  int CC_id;

  for (CC_id=0;CC_id<MAX_NUM_CCs;CC_id++) {
    for (i=0;i<10;i++) {
1189
      /*set the stack sizw */ 
Raymond Knopp's avatar
 
Raymond Knopp committed
1190
      pthread_attr_init (&attr_eNB_proc_tx[CC_id][i]);
Raymond Knopp's avatar
 
Raymond Knopp committed
1191
      if (pthread_attr_setstacksize(&attr_eNB_proc_tx[CC_id][i],PTHREAD_STACK_MIN) != 0)
1192 1193 1194
	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
1195
      if (pthread_attr_setstacksize(&attr_eNB_proc_rx[CC_id][i],PTHREAD_STACK_MIN) != 0)
1196 1197 1198
	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
1199 1200 1201 1202 1203 1204 1205 1206
      //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);
1207
#endif       
Raymond Knopp's avatar
 
Raymond Knopp committed
1208
      
Raymond Knopp's avatar
 
Raymond Knopp committed
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
      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;
1221
#ifdef EXMIMO
Raymond Knopp's avatar
 
Raymond Knopp committed
1222 1223
      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
1224
#else
Raymond Knopp's avatar
 
Raymond Knopp committed
1225 1226
      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
1227
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
1228 1229 1230
    }
  
  
1231
#ifdef EXMIMO
Raymond Knopp's avatar
 
Raymond Knopp committed
1232 1233
    // 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
1234
    
Raymond Knopp's avatar
 
Raymond Knopp committed
1235
    //PHY_vars_eNB_g[0][CC_id]->proc[0].frame_rx = 1023;
Raymond Knopp's avatar
 
Raymond Knopp committed
1236 1237
    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
1238
#else
Raymond Knopp's avatar
 
Raymond Knopp committed
1239 1240
    // 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
1241
    //    PHY_vars_eNB_g[0][CC_id]->proc[7].frame_tx = 1;
Raymond Knopp's avatar
 
Raymond Knopp committed
1242 1243
    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
1244
    //    PHY_vars_eNB_g[0][CC_id]->proc[0].frame_tx = 1;
Raymond Knopp's avatar
 
Raymond Knopp committed
1245
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
1246 1247
  }
}
Raymond Knopp's avatar
 
Raymond Knopp committed
1248

1249
void kill_eNB_proc(void) {
Raymond Knopp's avatar
 
Raymond Knopp committed
1250 1251

  int i;
1252
  void *status_tx,*status_rx;
Raymond Knopp's avatar
 
Raymond Knopp committed
1253
  int CC_id;
Raymond Knopp's avatar
 
Raymond Knopp committed
1254

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


Raymond Knopp's avatar
 
Raymond Knopp committed
1290 1291


Raymond Knopp's avatar
 
Raymond Knopp committed
1292 1293
  
/* This is the main eNB thread. */
Raymond Knopp's avatar
 
Raymond Knopp committed
1294 1295
int eNB_thread_status;

Raymond Knopp's avatar
 
Raymond Knopp committed
1296

Raymond Knopp's avatar
 
Raymond Knopp committed
1297

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

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

1318
  RTIME time_diff;
Raymond Knopp's avatar
 
Raymond Knopp committed
1319

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

1339 1340
  openair0_timestamp timestamp;

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

Raymond Knopp's avatar
 
Raymond Knopp committed
1347 1348
  struct timespec trx_time0,trx_time1,trx_time2;

1349
  /*
Raymond Knopp's avatar
 
Raymond Knopp committed
1350 1351 1352 1353
    #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
1354
  */
1355

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

1371
   
Raymond Knopp's avatar
 
Raymond Knopp committed
1372 1373 1374 1375
  /* 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");
     }*/
1376
   
Raymond Knopp's avatar
 
Raymond Knopp committed
1377 1378 1379 1380 1381 1382 1383
  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());
  }
1384
#endif
1385 1386 1387 1388
#endif

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

#ifdef HARD_RT
1396
    rt_make_hard_real_time();
1397 1398
#endif

Raymond Knopp's avatar
 
Raymond Knopp committed
1399
    printf("eNB_thread: mlockall in ...\n");
1400
    mlockall(MCL_CURRENT | MCL_FUTURE);
Raymond Knopp's avatar
 
Raymond Knopp committed
1401
    printf("eNB_thread: mlockall out ...\n");
1402

1403 1404 1405 1406
    timing_info.time_min = 100000000ULL;
    timing_info.time_max = 0;
    timing_info.time_avg = 0;
    timing_info.n_samples = 0;
1407

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

Raymond Knopp's avatar
 
Raymond Knopp committed
1414
    while (!oai_exit) {
1415
      start_meas(&softmodem_stats_mt);
1416
#ifdef EXMIMO
Raymond Knopp's avatar
 
Raymond Knopp committed
1417 1418
      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
1419 1420
      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
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432
      //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;
      
1433 1434 1435 1436 1437
      //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;
1438
	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);
1439 1440
      } 

Raymond Knopp's avatar
 
Raymond Knopp committed
1441 1442
      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
1443
	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
1444
	slot++;
1445 1446 1447 1448 1449 1450 1451
	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
1452 1453 1454 1455
      }
      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
1456
      vcd_signal_dumper_dump_variable_by_name(VCD_SIGNAL_DUMPER_VARIABLES_DAQ_MBOX, *DAQ_MBOX);
Raymond Knopp's avatar
 
Raymond Knopp committed
1457 1458 1459 1460 1461 1462 1463 1464 1465 1466
      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
1467
	vcd_signal_dumper_dump_variable_by_name(VCD_SIGNAL_DUMPER_VARIABLES_DAQ_MBOX, *DAQ_MBOX);
Raymond Knopp's avatar
 
Raymond Knopp committed
1468 1469 1470 1471 1472 1473
	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) {
1474
	  stop_meas(&softmodem_stats_mt);
Raymond Knopp's avatar
 
Raymond Knopp committed
1475 1476 1477 1478 1479
	  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
1480
	  diff = 150-mbox_current+mbox_target;
Raymond Knopp's avatar
 
Raymond Knopp committed
1481
	else if ((mbox_current<15) && (mbox_target>=135))
Raymond Knopp's avatar
 
Raymond Knopp committed
1482
	  diff = -150+mbox_target-mbox_current;
Raymond Knopp's avatar
 
Raymond Knopp committed
1483
	else
Raymond Knopp's avatar
 
Raymond Knopp committed
1484
	  diff = mbox_target - mbox_current;
Raymond Knopp's avatar
 
Raymond Knopp committed
1485
      }
Raymond Knopp's avatar
 
Raymond Knopp committed
1486

1487
#else  // EXMIMO
Raymond Knopp's avatar
 
Raymond Knopp committed
1488 1489
      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);
1490 1491
      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
1492

1493
	//	openair0_timestamp time0,time1;
Raymond Knopp's avatar
 
Raymond Knopp committed
1494
	unsigned int rxs;
Raymond Knopp's avatar
 
Raymond Knopp committed
1495
#ifndef USRP_DEBUG
Raymond Knopp's avatar
 
Raymond Knopp committed
1496
	vcd_signal_dumper_dump_function_by_name(VCD_SIGNAL_DUMPER_FUNCTIONS_TRX_READ,1);
1497 1498
	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);
1499

Raymond Knopp's avatar
 
Raymond Knopp committed
1500

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

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

1515
	if (rxs != spp)
Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
1516
	  exit_fun("problem receiving samples");
Raymond Knopp's avatar
 
Raymond Knopp committed
1517
 
Raymond Knopp's avatar
 
Raymond Knopp committed
1518

Raymond Knopp's avatar
 
Raymond Knopp committed
1519 1520
	vcd_signal_dumper_dump_function_by_name(VCD_SIGNAL_DUMPER_FUNCTIONS_TRX_READ,0);

Raymond Knopp's avatar
 
Raymond Knopp committed
1521
	// Transmit TX buffer based on timestamp from RX
Raymond Knopp's avatar
 
Raymond Knopp committed
1522
	vcd_signal_dumper_dump_function_by_name(VCD_SIGNAL_DUMPER_FUNCTIONS_TRX_WRITE,1);
Raymond Knopp's avatar
 
Raymond Knopp committed
1523 1524
	
	for (i=0;i<PHY_vars_eNB_g[0][0]->lte_frame_parms.nb_antennas_tx;i++)
1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
	  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);


1539
	stop_meas(&softmodem_stats_mt);
Raymond Knopp's avatar
 
Raymond Knopp committed
1540 1541
	clock_gettime(CLOCK_MONOTONIC,&trx_time2);

Raymond Knopp's avatar
 
Raymond Knopp committed
1542
	vcd_signal_dumper_dump_function_by_name(VCD_SIGNAL_DUMPER_FUNCTIONS_TRX_WRITE,0);
Raymond Knopp's avatar
 
Raymond Knopp committed
1543
	/*
Lionel Gauthier's avatar
Lionel Gauthier committed
1544
	if (trace_cnt++<10) 
Raymond Knopp's avatar
 
Raymond Knopp committed
1545
	  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
1546
	*/
Raymond Knopp's avatar
 
Raymond Knopp committed
1547 1548 1549
#else
	rt_sleep_ns(1000000);
#endif
1550 1551 1552
	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
1553 1554 1555 1556 1557
	  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
1558
	      //	      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
1559 1560 1561 1562 1563 1564 1565 1566
	      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 {
1567
		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
1568
		exit_fun("nothing to add");
Raymond Knopp's avatar
 
Raymond Knopp committed
1569 1570 1571 1572
	      }
	    }
	  }
	}
1573 1574
	rx_pos += spp;
	tx_pos += spp;
Raymond Knopp's avatar
 
Raymond Knopp committed
1575

1576 1577 1578

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

1581 1582
      if (rx_pos >= samples_per_frame)
	rx_pos -= samples_per_frame; 
Raymond Knopp's avatar
 
Raymond Knopp committed
1583
      
Raymond Knopp's avatar
 
Raymond Knopp committed
1584 1585 1586

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

Raymond Knopp's avatar
 
Raymond Knopp committed
1589

Raymond Knopp's avatar
 
Raymond Knopp committed
1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610
      
      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
1611
	}
Raymond Knopp's avatar
 
Raymond Knopp committed
1612 1613
      */
      //}
1614
    
Raymond Knopp's avatar
 
Raymond Knopp committed
1615
      if ((slot&1) == 1) {
1616
#ifdef EXMIMO
Raymond Knopp's avatar
 
Raymond Knopp committed
1617 1618 1619 1620 1621
	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
1622
	
Raymond Knopp's avatar
 
Raymond Knopp committed
1623
	for (CC_id=0;CC_id<MAX_NUM_CCs;CC_id++) {
1624
#ifdef EXMIMO 
Raymond Knopp's avatar
 
Raymond Knopp committed
1625 1626
	  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
1627
	  }
Raymond Knopp's avatar
 
Raymond Knopp committed
1628 1629 1630 1631 1632 1633 1634
	  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);
1635
	      }
Raymond Knopp's avatar
 
Raymond Knopp committed
1636 1637 1638
	    }
	    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
1639
	      exit_fun("nothing to add");
Raymond Knopp's avatar
 
Raymond Knopp committed
1640
	    }
Raymond Knopp's avatar
 
Raymond Knopp committed
1641
	  }
Raymond Knopp's avatar
 
Raymond Knopp committed
1642 1643 1644 1645 1646
#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
1647
	    //		      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
1648 1649 1650 1651 1652
	    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
1653
	      }
ghaddab's avatar
ghaddab committed
1654
              //else
Raymond Knopp's avatar
 
Raymond Knopp committed
1655
	      // 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
1656 1657
	    }
	    else {
ghaddab's avatar
ghaddab committed
1658
	      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
1659
	      exit_fun("nothing to add");
Raymond Knopp's avatar
 
Raymond Knopp committed
1660
	    }
Raymond Knopp's avatar
 
Raymond Knopp committed
1661
	  }
Raymond Knopp's avatar
 
Raymond Knopp committed
1662
	    
Raymond Knopp's avatar
 
Raymond Knopp committed
1663 1664
	}
      }
Raymond Knopp's avatar
 
Raymond Knopp committed
1665 1666
	  
      
Raymond Knopp's avatar
 
Raymond Knopp committed
1667 1668 1669 1670 1671 1672 1673
#ifndef RTAI
      //pthread_mutex_lock(&tti_mutex);
#endif




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


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

Raymond Knopp's avatar
 
Raymond Knopp committed
1704 1705 1706 1707 1708

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

1721

Raymond Knopp's avatar
 
Raymond Knopp committed
1722

Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
1723

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



1741 1742 1743 1744 1745 1746 1747 1748 1749 1750

  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,
1751 1752
    LONG_OPTION_NO_L2_CONNECT,
    LONG_OPTION_RXGAIN
1753 1754 1755 1756 1757 1758 1759 1760 1761
  };
  
  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},
1762
    {"ue_rxgain",   required_argument,  NULL, LONG_OPTION_RXGAIN},
1763 1764 1765 1766 1767 1768 1769 1770
    {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
1771
      
1772 1773 1774 1775 1776
    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
1777
      
1778 1779 1780 1781
    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
1782
      break;
1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796
      
    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;
1797 1798 1799 1800
    case LONG_OPTION_RXGAIN:
      for (i=0;i<4;i++)
	rx_gain[0][i] = atof(optarg);
      break;
1801 1802 1803
    case 'M':
#ifdef ETHERNET
      strcpy(rrh_eNB_ip,optarg);
1804
#endif
1805 1806 1807 1808 1809 1810 1811 1812
      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]);
1813
      }
1814 1815
      UE_scan=0;
      break;
Raymond Knopp's avatar
 
Raymond Knopp committed
1816
      
1817 1818 1819 1820
    case 'd':
#ifdef XFORMS
      do_forms=1;
      printf("Running with XFORMS!\n");
Raymond Knopp's avatar
 
Raymond Knopp committed
1821
#endif
1822 1823 1824 1825 1826
      break;
      
    case 'K':
#if defined(ENABLE_ITTI)
      itti_dump_file = strdup(optarg);
1827
#else
1828
      printf("-K option is disabled when ENABLE_ITTI is not defined\n");
Raymond Knopp's avatar
 
Raymond Knopp committed
1829
#endif
1830
      break;
1831
      
1832 1833 1834
    case 'O':
      conf_config_file_name = optarg;
      break;
1835
      
1836 1837 1838
    case 'U':
      UE_flag = 1;
      break;
1839 1840 1841 1842 1843 1844 1845
    
    case 'm':
      target_dl_mcs = atoi (optarg);
      break;
    case 't':
      target_ul_mcs = atoi (optarg);
      break;
Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
1846
#ifdef OPENAIR2
1847
    case 'P':
Raymond Knopp's avatar
 
Raymond Knopp committed
1848
#ifdef OPENAIR2
1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862
      /* 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
1863
#endif
1864
      break;  
Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
1865
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
1866 1867 1868
    case 'V':
      ouput_vcd = 1;
      break;
Raymond Knopp's avatar
 
Raymond Knopp committed
1869
    case  'q':
Raymond Knopp's avatar
 
Raymond Knopp committed
1870 1871 1872 1873 1874 1875
      opp_enabled = 1;
      break;
    case  'R' :
      online_log_messages =1;
      break;
    case 'r':
Raymond Knopp's avatar
 
Raymond Knopp committed
1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897
      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
1898
      }
1899
      break;
Raymond Knopp's avatar
 
Raymond Knopp committed
1900 1901
    case 's':
#ifdef USRP
Raymond Knopp's avatar
 
Raymond Knopp committed
1902 1903 1904

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

    case 'F':
1925
#ifdef EXMIMO
1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942
      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);
1943
#endif
1944 1945
      break;
      
1946 1947 1948
    case 'G':
      glog_verbosity=atoi(optarg);// value from 0, 0x5, 0x15, 0x35, 0x75
      break;
1949 1950 1951 1952 1953 1954 1955
    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
1956 1957
    default:
      break;
1958 1959
    }
  }
Raymond Knopp's avatar
 
Raymond Knopp committed
1960
  
Raymond Knopp's avatar
 
Raymond Knopp committed
1961 1962
  if (UE_flag == 0)
    AssertFatal(conf_config_file_name != NULL,"Please provide a configuration file\n");
Raymond Knopp's avatar
 
Raymond Knopp committed
1963 1964
  
    
Raymond Knopp's avatar
 
Raymond Knopp committed
1965
  if ((UE_flag == 0) && (conf_config_file_name != NULL)) {
1966
    int i,j;
Raymond Knopp's avatar
 
Raymond Knopp committed
1967 1968 1969 1970 1971 1972 1973 1974 1975
    
    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);
1976

Raymond Knopp's avatar
 
Raymond Knopp committed
1977
    /* Update some simulation parameters */
1978
    for (i=0; i < enb_properties->number; i++) {
1979 1980 1981 1982
      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
1983 1984 1985 1986 1987 1988
      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
1989
	//for (j=0; j < enb_properties->properties[i]->nb_cc; j++ ){
1990 1991
	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
1992
	frame_parms[CC_id]->N_RB_UL             =  enb_properties->properties[i]->N_RB_DL[CC_id];
1993 1994 1995
	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];
1996
	//} // j
Raymond Knopp's avatar
 
Raymond Knopp committed
1997 1998
      }

1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
      
#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
      
2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032
      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
2033 2034 2035 2036 2037 2038
# 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
2039 2040 2041 2042
#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 
2043
    
Raymond Knopp's avatar
 
Raymond Knopp committed
2044
      // adjust the log
2045 2046
      for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
	for (k = 0 ; k < 4; k++) {
2047
	  downlink_frequency[CC_id][k]      =       enb_properties->properties[i]->downlink_frequency[CC_id];
2048
	  uplink_frequency_offset[CC_id][k] =  enb_properties->properties[i]->uplink_frequency_offset[CC_id];
2049 2050
	  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];
2051
	}
2052
	printf("Downlink frequency/ uplink offset of CC_id %d set to %ju/%d\n", CC_id,
2053 2054
	       enb_properties->properties[i]->downlink_frequency[CC_id],
	       enb_properties->properties[i]->uplink_frequency_offset[CC_id]);
Raymond Knopp's avatar
 
Raymond Knopp committed
2055
      } // CC_id
2056
    }// i
Raymond Knopp's avatar
 
Raymond Knopp committed
2057
  }
Raymond Knopp's avatar
 
Raymond Knopp committed
2058 2059 2060 2061 2062 2063 2064
  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
2065
}
2066

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

#ifdef ENABLE_TCXO
Raymond Knopp's avatar
 
Raymond Knopp committed
2087
  unsigned int tcxo = 114;
2088 2089
#endif

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

2097 2098 2099 2100
  PHY_VARS_UE *UE[MAX_NUM_CCs];
  int UE_id = 0;

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

2103
  memset(tx_max_power,0,sizeof(int)*MAX_NUM_CCs);
Raymond Knopp's avatar
 
Raymond Knopp committed
2104
  set_latency_target();
2105

2106

2107

Raymond Knopp's avatar
 
Raymond Knopp committed
2108 2109 2110
  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
2111
    frame_parms[CC_id]->frame_type         = TDD; /* TDD */
Raymond Knopp's avatar
 
Raymond Knopp committed
2112 2113 2114 2115
    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
2116
    frame_parms[CC_id]->Ncp                 = NORMAL;
Raymond Knopp's avatar
 
Raymond Knopp committed
2117 2118
    frame_parms[CC_id]->Ncp_UL              = NORMAL;
    frame_parms[CC_id]->Nid_cell            = Nid_cell;
Raymond Knopp's avatar
 
Raymond Knopp committed
2119
    frame_parms[CC_id]->num_MBSFN_config    = 0;
Raymond Knopp's avatar
 
Raymond Knopp committed
2120 2121 2122
    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
2123
  }
2124

2125 2126 2127
  // initialize the log (see log.h for details)
  logInit();
  set_glog(glog_level, glog_verbosity);
winckel's avatar
winckel committed
2128

Raymond Knopp's avatar
 
Raymond Knopp committed
2129 2130
  //randominit (0);
  set_taus_seed (0);
winckel's avatar
winckel committed
2131

2132 2133
  

2134 2135
  get_options (argc, argv); //Command-line options
  
Raymond Knopp's avatar
 
Raymond Knopp committed
2136 2137 2138 2139

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

2141
      set_comp_log(HW,      LOG_DEBUG,  LOG_HIGH, 1);
2142
#ifdef OPENAIR2
2143
      set_comp_log(PHY,     LOG_DEBUG,   LOG_HIGH, 1);
2144
#else
Raymond Knopp's avatar
 
Raymond Knopp committed
2145
      set_comp_log(PHY,     LOG_INFO,   LOG_HIGH, 1);
2146
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
2147 2148 2149 2150 2151
      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);
2152
#if defined(ENABLE_ITTI)
Raymond Knopp's avatar
 
Raymond Knopp committed
2153
      set_comp_log(EMU,     LOG_INFO,   LOG_MED, 1);
2154
# if defined(ENABLE_USE_MME)
Raymond Knopp's avatar
 
Raymond Knopp committed
2155
      set_comp_log(NAS,     LOG_INFO,   LOG_HIGH, 1);
2156 2157
# endif
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
2158 2159 2160 2161
    }
  else
    {
      printf("configuring for eNB\n");
2162

2163
      set_comp_log(HW,      hw_log_level, hw_log_verbosity, 1);
2164
#ifdef OPENAIR2
2165
      set_comp_log(PHY,     phy_log_level,   phy_log_verbosity, 1);
2166
#else
Raymond Knopp's avatar
 
Raymond Knopp committed
2167
      set_comp_log(PHY,     LOG_INFO,   LOG_HIGH, 1);
2168
#endif
2169 2170 2171 2172
      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);
2173
#if defined(ENABLE_ITTI)
Raymond Knopp's avatar
 
Raymond Knopp committed
2174
      set_comp_log(EMU,     LOG_INFO,   LOG_MED, 1);
2175
# if defined(ENABLE_USE_MME)
Lionel Gauthier's avatar
 
Lionel Gauthier committed
2176 2177
      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
2178 2179
      set_comp_log(S1AP,    LOG_DEBUG,   LOG_HIGH, 1);
      set_comp_log(SCTP,    LOG_INFO,   LOG_HIGH, 1);
2180
# endif
2181
#if defined(ENABLE_SECURITY)
2182
      set_comp_log(OSA,    osa_log_level,   osa_log_verbosity, 1);
2183
#endif
2184
#endif
2185 2186 2187 2188
#ifdef LOCALIZATION
      set_comp_log(LOCALIZE, LOG_DEBUG, LOG_LOW, 1);
      set_component_filelog(LOCALIZE);
#endif 
Raymond Knopp's avatar
 
Raymond Knopp committed
2189
      set_comp_log(ENB_APP, LOG_INFO, LOG_HIGH, 1);
2190
      set_comp_log(OTG,     LOG_INFO,   LOG_HIGH, 1);
Raymond Knopp's avatar
 
Raymond Knopp committed
2191 2192 2193
      if (online_log_messages == 1) { 
	set_component_filelog(RRC);
	set_component_filelog(PDCP);
Raymond Knopp's avatar
 
Raymond Knopp committed
2194 2195
      }
    }
2196

Raymond Knopp's avatar
 
Raymond Knopp committed
2197 2198 2199 2200 2201 2202
  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");
  }
2203 2204 2205
  
  if (opp_enabled ==1)
    reset_opp_meas();
Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
2206
#ifdef OPENAIR2
2207 2208 2209 2210 2211 2212 2213 2214 2215
  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
2216
#endif
2217
  
2218
#if defined(ENABLE_ITTI)
Raymond Knopp's avatar
 
Raymond Knopp committed
2219 2220 2221 2222 2223 2224
  if (UE_flag == 1) {
    log_set_instance_type (LOG_INSTANCE_UE);
  }
  else {
    log_set_instance_type (LOG_INSTANCE_ENB);
  }
2225

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

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

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

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

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

Raymond Knopp's avatar
 
Raymond Knopp committed
2249
      }
Raymond Knopp's avatar
 
Raymond Knopp committed
2250 2251 2252
    else
      { //UE_flag==1
	frame_parms[CC_id]->nb_antennas_tx     = 1;
2253
	frame_parms[CC_id]->nb_antennas_rx     = 1;
2254
	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
2255 2256 2257 2258 2259 2260 2261 2262 2263
      }
    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;
2264 2265
    init_ul_hopping(frame_parms[CC_id]);
    init_frame_parms(frame_parms[CC_id],1);
2266
    phy_init_top(frame_parms[CC_id]);
Raymond Knopp's avatar
 
Raymond Knopp committed
2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278
  }

  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
2279
    // N_ZC = (prach_fmt <4)?839:139;
Raymond Knopp's avatar
 
Raymond Knopp committed
2280
  }
Raymond Knopp's avatar
 
Raymond Knopp committed
2281 2282

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

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

      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
2294
#ifndef OPENAIR2
Raymond Knopp's avatar
 
Raymond Knopp committed
2295
      for (i=0;i<NUMBER_OF_CONNECTED_eNB_MAX;i++) {
2296 2297 2298
	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
2299
	
2300 2301 2302
	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
2303
      }
2304
#endif
2305 2306 2307 2308 2309 2310 2311 2312


      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
2313
      
2314
      UE[CC_id]->lte_ue_pdcch_vars[0]->crnti = 0x1234;
2315
#ifndef OPENAIR2
2316
      UE[CC_id]->lte_ue_pdcch_vars[0]->crnti = 0x1235;
2317
#endif
2318
    
2319
#ifdef EXMIMO
Raymond Knopp's avatar
 
Raymond Knopp committed
2320
      for (i=0;i<4;i++) {
2321 2322 2323
	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
2324
      }
2325

2326 2327 2328 2329
      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
2330 2331
	for (i=0;i<4;i++)
	  rx_gain_mode[CC_id][i] = max_gain;
2332
	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
2333 2334 2335 2336
      }
      else if ((mode == rx_calib_ue_med)) {
	for (i=0;i<4;i++)
	  rx_gain_mode[CC_id][i] =  med_gain;
2337
	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
2338 2339 2340 2341
      }
      else if ((mode == rx_calib_ue_byp)) {
	for (i=0;i<4;i++)
	  rx_gain_mode[CC_id][i] =  byp_gain;
2342
	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
2343
      }
2344
#else
2345
      UE[CC_id]->rx_total_gain_dB =  (int)rx_gain[CC_id][0]; 
2346
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
2347
    
2348
      UE[CC_id]->tx_power_max_dBm = tx_max_power[CC_id];
Raymond Knopp's avatar
 
Raymond Knopp committed
2349
    
2350

2351
   
2352
#ifdef EXMIMO
Raymond Knopp's avatar
 
Raymond Knopp committed
2353
      //N_TA_offset
2354 2355 2356 2357 2358 2359 2360
      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
2361 2362
      }
      else {
2363
	UE[CC_id]->N_TA_offset = 0;
Raymond Knopp's avatar
 
Raymond Knopp committed
2364
      }
2365
#else
Raymond Knopp's avatar
 
Raymond Knopp committed
2366
      //already taken care of in lte-softmodem
2367
      UE[CC_id]->N_TA_offset = 0;
2368
#endif 
Raymond Knopp's avatar
 
Raymond Knopp committed
2369
    }
Raymond Knopp's avatar
 
Raymond Knopp committed
2370 2371 2372 2373 2374
    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;
    
2375
  
Raymond Knopp's avatar
 
Raymond Knopp committed
2376 2377 2378 2379 2380
    
    //  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
2381 2382 2383
      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++) {
2384
	PHY_vars_eNB_g[0][CC_id] = init_lte_eNB(frame_parms[CC_id],0,Nid_cell,cooperation_flag,transmission_mode,abstraction_flag);
2385
	PHY_vars_eNB_g[0][CC_id]->CC_id = CC_id;
Raymond Knopp's avatar
 
Raymond Knopp committed
2386
       
2387
#ifndef OPENAIR2
Raymond Knopp's avatar
 
Raymond Knopp committed
2388 2389 2390 2391 2392 2393 2394 2395 2396
	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
2397
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
2398 2399 2400 2401
      
	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
2402

2403
#ifndef EXMIMO
Raymond Knopp's avatar
 
Raymond Knopp committed
2404

2405
	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
2406

2407
#else
2408
	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
2409 2410 2411

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

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

2417
#ifdef EXMIMO
2418
	//N_TA_offset
2419 2420 2421 2422 2423 2424 2425
	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;
2426 2427
	}
	else {
2428
	  PHY_vars_eNB_g[0][CC_id]->N_TA_offset = 0;
2429 2430 2431
	}
#else
	//already taken care of in lte-softmodem
2432
	PHY_vars_eNB_g[0][CC_id]->N_TA_offset = 0;
2433 2434
#endif 
	
Raymond Knopp's avatar
 
Raymond Knopp committed
2435
      }
2436 2437


Raymond Knopp's avatar
 
Raymond Knopp committed
2438 2439
      NB_eNB_INST=1;
      NB_INST=1;
Raymond Knopp's avatar
 
Raymond Knopp committed
2440

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

    }
2447

2448

2449

Raymond Knopp's avatar
 
Raymond Knopp committed
2450
  dump_frame_parms(frame_parms[0]);
2451

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

#ifdef ETHERNET
2491 2492
  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
2493

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

2497

2498
  for (card=0;card<MAX_CARDS;card++) {
2499
#ifndef EXMIMO
2500
    openair0_cfg[card].samples_per_packet = openair0_cfg[0].samples_per_packet;
2501 2502 2503 2504
#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
2505 2506
    openair0_cfg[card].Mod_id = 0;
#ifdef ETHERNET
ghaddab's avatar
ghaddab committed
2507
    if (UE_flag){
Raymond Knopp's avatar
 
Raymond Knopp committed
2508 2509 2510
      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
2511 2512
    }
    else
Raymond Knopp's avatar
 
Raymond Knopp committed
2513 2514 2515 2516 2517
      {
	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
2518
#endif
2519 2520 2521
    openair0_cfg[card].sample_rate = sample_rate;
    openair0_cfg[card].tx_bw = bw;
    openair0_cfg[card].rx_bw = bw;
2522 2523 2524
    // 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
2525
#ifndef EXMIMO
Raymond Knopp's avatar
 
Raymond Knopp committed
2526 2527
    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));
2528
    for (i=0;i<4;i++) {
Raymond Knopp's avatar
 
Raymond Knopp committed
2529

2530
      openair0_cfg[card].tx_gain[i] = tx_gain[0][i];
Raymond Knopp's avatar
 
Raymond Knopp committed
2531
      openair0_cfg[card].rx_gain[i] = ((UE_flag==0) ? PHY_vars_eNB_g[0][0]->rx_total_gain_eNB_dB : 
2532
                                       PHY_vars_UE_g[0][0]->rx_total_gain_dB) - USRP_GAIN_OFFSET;  // calibrated for USRP B210 @ 2.6 GHz
2533 2534
      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];
Raymond Knopp's avatar
 
Raymond Knopp committed
2535 2536 2537 2538 2539 2540
      printf("Setting tx_gain %f, rx_gain %f, tx_freq %f, rx_freq %f\n",
	     openair0_cfg[card].tx_gain[i],
	     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
2541
    }
2542
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
2543
  }
2544

2545
  printf("Initializing openair0 ...");
2546
  if (openair0_device_init(&openair0, &openair0_cfg[0]) <0) {
Raymond Knopp's avatar
 
Raymond Knopp committed
2547 2548 2549
    printf("Exiting, cannot initialize device\n");
    exit(-1);
  }
2550
  printf("Done\n");
2551 2552 2553

  mac_xface = malloc(sizeof(MAC_xface));

2554
#ifdef OPENAIR2
Raymond Knopp's avatar
 
Raymond Knopp committed
2555
  int eMBMS_active=0;
2556

Raymond Knopp's avatar
 
Raymond Knopp committed
2557
  l2_init(frame_parms[0],eMBMS_active,(uecap_xer_in==1)?uecap_xer:NULL,
Raymond Knopp's avatar
 
Raymond Knopp committed
2558 2559 2560 2561 2562 2563
	  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);
2564 2565
#endif

Raymond Knopp's avatar
 
Raymond Knopp committed
2566
  mac_xface->macphy_exit = &exit_fun;
2567

winckel's avatar
winckel committed
2568
#if defined(ENABLE_ITTI)
Raymond Knopp's avatar
 
Raymond Knopp committed
2569
  if (create_tasks(UE_flag ? 0 : 1, UE_flag ? 1 : 0) < 0) {
Florian Kaltenberger's avatar
Florian Kaltenberger committed
2570
    printf("cannot create ITTI tasks\n");
Raymond Knopp's avatar
 
Raymond Knopp committed
2571 2572 2573
    exit(-1); // need a softer mode
  }
  printf("ITTI tasks created\n");
winckel's avatar
winckel committed
2574
#endif
Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
2575 2576

#ifdef OPENAIR2
2577
  printf("Filling UE band info\n");
Raymond Knopp's avatar
 
Raymond Knopp committed
2578 2579
  if (UE_flag==1)
    fill_ue_band_info();
Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
2580
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
2581

2582
  /* #ifdef OPENAIR2
Raymond Knopp's avatar
 
Raymond Knopp committed
2583 2584 2585 2586 2587 2588
  //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
2589 2590 2591 2592 2593 2594
  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
2595 2596 2597
  }
  init_predef_traffic(UE_flag ? 1 : 0, UE_flag ? 0 : 1);
  //  }
2598
  #endif */
Raymond Knopp's avatar
 
Raymond Knopp committed
2599

2600
#ifdef EXMIMO
2601
  number_of_cards = openair0_num_detected_cards;
2602 2603 2604
#else
  number_of_cards = 1;
#endif 
2605

2606

2607

Florian Kaltenberger's avatar
Florian Kaltenberger committed
2608 2609
  for(CC_id=0;CC_id<MAX_NUM_CCs;CC_id++) {
    rf_map[CC_id].card=0;
ouni's avatar
ouni committed
2610
    rf_map[CC_id].chain=CC_id;
Florian Kaltenberger's avatar
Florian Kaltenberger committed
2611
  }
2612

Raymond Knopp's avatar
 
Raymond Knopp committed
2613 2614
  // connect the TX/RX buffers
  if (UE_flag==1) {
2615
    openair_daq_vars.timing_advance = 170;
2616
    if (setup_ue_buffers(UE,&openair0_cfg[0],rf_map)!=0) {
2617 2618 2619
      printf("Error setting up eNB buffer\n");
      exit(-1);
    }
2620 2621
    printf("Setting UE buffer to all-RX\n");
    // Set LSBs for antenna switch (ExpressMIMO)
Raymond Knopp's avatar
 
Raymond Knopp committed
2622
    for (CC_id=0;CC_id<MAX_NUM_CCs;CC_id++) {
Raymond Knopp's avatar
 
Raymond Knopp committed
2623
      for (i=0; i<frame_parms[CC_id]->samples_per_tti*10; i++)
2624
	for (aa=0; aa<frame_parms[CC_id]->nb_antennas_tx; aa++)
2625
	  UE[CC_id]->lte_ue_common_vars.txdata[aa][i] = 0x00010001;
Raymond Knopp's avatar
 
Raymond Knopp committed
2626
    }
Raymond Knopp's avatar
 
Raymond Knopp committed
2627 2628 2629
    //p_exmimo_config->framing.tdd_config = TXRXSWITCH_TESTRX;
  }
  else {
2630
    openair_daq_vars.timing_advance = 0;
2631 2632 2633 2634 2635 2636
    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
2637
    for (CC_id=0;CC_id<MAX_NUM_CCs;CC_id++) {
Raymond Knopp's avatar
 
Raymond Knopp committed
2638 2639
      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++)
2640
	  PHY_vars_eNB_g[0][CC_id]->lte_eNB_common_vars.txdata[0][aa][i] = 0x00010001;
Raymond Knopp's avatar
 
Raymond Knopp committed
2641
    }
Raymond Knopp's avatar
 
Raymond Knopp committed
2642
  }
2643
#ifdef EXMIMO
2644
  openair0_config(&openair0_cfg[0],UE_flag);
Raymond Knopp's avatar
 
Raymond Knopp committed
2645 2646
#endif

Raymond Knopp's avatar
 
Raymond Knopp committed
2647
  /*  
Raymond Knopp's avatar
 
Raymond Knopp committed
2648 2649
      for (ant=0;ant<4;ant++)
      p_exmimo_config->rf.do_autocal[ant] = 0;
Raymond Knopp's avatar
 
Raymond Knopp committed
2650
  */
2651 2652

#ifdef EMOS
Raymond Knopp's avatar
 
Raymond Knopp committed
2653 2654 2655 2656 2657 2658 2659 2660 2661
  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);
2662 2663
#endif

Raymond Knopp's avatar
 
Raymond Knopp committed
2664
  mlockall(MCL_CURRENT | MCL_FUTURE);
2665 2666

#ifdef RTAI
Raymond Knopp's avatar
 
Raymond Knopp committed
2667
  // make main thread LXRT soft realtime
2668
  /* task = */ rt_task_init_schmod(nam2num("MAIN"), 9, 0, 0, SCHED_FIFO, 0xF);
Raymond Knopp's avatar
 
Raymond Knopp committed
2669 2670 2671 2672 2673

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

Raymond Knopp's avatar
 
Raymond Knopp committed
2676 2677
  pthread_cond_init(&sync_cond,NULL);
  pthread_mutex_init(&sync_mutex, NULL);
2678

2679
#if defined(ENABLE_ITTI)
Florian Kaltenberger's avatar
Florian Kaltenberger committed
2680 2681 2682 2683 2684
  // 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);
  }
2685
#endif
2686

Raymond Knopp's avatar
 
Raymond Knopp committed
2687 2688

  // this starts the DMA transfers
2689
#ifdef EXMIMO
Raymond Knopp's avatar
 
Raymond Knopp committed
2690
  if (UE_flag!=1)
2691 2692
    for (card=0;card<openair0_num_detected_cards;card++)
      openair0_start_rt_acquisition(card);
Raymond Knopp's avatar
 
Raymond Knopp committed
2693
#endif
2694 2695

#ifdef XFORMS
Raymond Knopp's avatar
 
Raymond Knopp committed
2696 2697
  if (do_forms==1) {
    fl_initialize (&argc, argv, NULL, 0, 0);
2698 2699

    if (UE_flag==0) {
2700
      form_stats_l2 = create_form_stats_form();
2701 2702 2703
      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
2704 2705 2706 2707 2708 2709 2710
      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
2711 2712
	}
	else {
Raymond Knopp's avatar
 
Raymond Knopp committed
2713 2714
	  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
2715
	}
2716 2717
      }
    }
Raymond Knopp's avatar
 
Raymond Knopp committed
2718
    else {
2719 2720 2721 2722 2723 2724 2725
      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
2726 2727 2728 2729 2730 2731 2732 2733 2734 2735
      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
2736
    ret = pthread_create(&forms_thread, NULL, scope_thread, NULL);
Raymond Knopp's avatar
 
Raymond Knopp committed
2737 2738
    printf("Scope thread created, ret=%d\n",ret);
  }
2739 2740 2741
#endif

#ifdef EMOS
Raymond Knopp's avatar
 
Raymond Knopp committed
2742 2743
  ret = pthread_create(&thread3, NULL, emos_thread, NULL);
  printf("EMOS thread created, ret=%d\n",ret);
2744 2745
#endif

Raymond Knopp's avatar
 
Raymond Knopp committed
2746
  rt_sleep_ns(10*FRAME_PERIOD);
2747 2748

#ifndef RTAI
Raymond Knopp's avatar
 
Raymond Knopp committed
2749
  pthread_attr_init (&attr_dlsch_threads);
2750
  pthread_attr_setstacksize(&attr_dlsch_threads,4*PTHREAD_STACK_MIN);
2751

2752
  pthread_attr_init (&attr_UE_thread);
2753
  pthread_attr_setstacksize(&attr_UE_thread,8192);//5*PTHREAD_STACK_MIN);
2754

2755
#ifndef LOWLATENCY
2756 2757
  sched_param_UE_thread.sched_priority = sched_get_priority_max(SCHED_FIFO);
  pthread_attr_setschedparam(&attr_UE_thread,&sched_param_UE_thread);
2758
  sched_param_dlsch.sched_priority = sched_get_priority_max(SCHED_FIFO); //OPENAIR_THREAD_PRIORITY;
Raymond Knopp's avatar
 
Raymond Knopp committed
2759 2760
  pthread_attr_setschedparam  (&attr_dlsch_threads, &sched_param_dlsch);
  pthread_attr_setschedpolicy (&attr_dlsch_threads, SCHED_FIFO);
2761
#endif 
Raymond Knopp's avatar
 
Raymond Knopp committed
2762

2763 2764
#endif

Raymond Knopp's avatar
 
Raymond Knopp committed
2765 2766
  // start the main thread
  if (UE_flag == 1) {
2767
    printf("Intializing UE Threads ...\n");
Raymond Knopp's avatar
 
Raymond Knopp committed
2768
    init_UE_threads();
2769 2770 2771 2772 2773
#ifdef DLSCH_THREAD
    init_rx_pdsch_thread();
    rt_sleep_ns(FRAME_PERIOD/10);
    init_dlsch_threads();
#endif
2774

2775
    sleep(1);
2776
#ifdef RTAI
2777
    main_ue_thread = rt_thread_create(UE_thread, NULL, 100000000);
2778
#else
2779
    error_code = pthread_create(&main_ue_thread, &attr_UE_thread, UE_thread, NULL);
Raymond Knopp's avatar
 
Raymond Knopp committed
2780 2781 2782 2783 2784 2785 2786
    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");
    }
2787
#endif
2788
    printf("UE threads created\n");
Raymond Knopp's avatar
 
Raymond Knopp committed
2789 2790
  }
  else {
2791

Raymond Knopp's avatar
 
Raymond Knopp committed
2792 2793
    if (multi_thread>0) {
      init_eNB_proc();
2794
      sleep(1);
Raymond Knopp's avatar
 
Raymond Knopp committed
2795 2796
      LOG_D(HW,"[lte-softmodem.c] eNB threads created\n");
    }
2797
    printf("Creating main eNB_thread \n");
Raymond Knopp's avatar
 
Raymond Knopp committed
2798
#ifdef RTAI
Raymond Knopp's avatar
 
Raymond Knopp committed
2799
    main_eNB_thread = rt_thread_create(eNB_thread, NULL, PTHREAD_STACK_MIN);
Raymond Knopp's avatar
 
Raymond Knopp committed
2800
#else
Raymond Knopp's avatar
 
Raymond Knopp committed
2801
    error_code = pthread_create(&main_eNB_thread, &attr_dlsch_threads, eNB_thread, NULL);
Raymond Knopp's avatar
 
Raymond Knopp committed
2802 2803 2804 2805 2806 2807
    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
2808
    }
Raymond Knopp's avatar
 
Raymond Knopp committed
2809 2810
#endif
  }
2811

Raymond Knopp's avatar
 
Raymond Knopp committed
2812
  // Sleep to allow all threads to setup
Raymond Knopp's avatar
 
Raymond Knopp committed
2813
  sleep(1);
Raymond Knopp's avatar
 
Raymond Knopp committed
2814

2815
#ifndef EXMIMO
Raymond Knopp's avatar
 
Raymond Knopp committed
2816
#ifndef USRP_DEBUG
Raymond Knopp's avatar
 
Raymond Knopp committed
2817
  openair0.trx_start_func(&openair0);
Raymond Knopp's avatar
 
Raymond Knopp committed
2818
  //  printf("returning from usrp start streaming: %llu\n",get_usrp_time(&openair0));
Raymond Knopp's avatar
 
Raymond Knopp committed
2819
#endif
2820 2821
#endif

2822

Raymond Knopp's avatar
 
Raymond Knopp committed
2823
  pthread_mutex_lock(&sync_mutex);
2824
  sync_var=0;
Raymond Knopp's avatar
 
Raymond Knopp committed
2825 2826
  pthread_cond_broadcast(&sync_cond);
  pthread_mutex_unlock(&sync_mutex);
2827

Raymond Knopp's avatar
 
Raymond Knopp committed
2828 2829 2830
  // wait for end of program
  printf("TYPE <CTRL-C> TO TERMINATE\n");
  //getchar();
2831 2832

#if defined(ENABLE_ITTI)
Raymond Knopp's avatar
 
Raymond Knopp committed
2833 2834
  printf("Entering ITTI signals handler\n");
  itti_wait_tasks_end();
2835
  oai_exit=1;
2836
#else
Raymond Knopp's avatar
 
Raymond Knopp committed
2837 2838
  while (oai_exit==0)
    rt_sleep_ns(FRAME_PERIOD);
2839
#endif
2840

Raymond Knopp's avatar
 
Raymond Knopp committed
2841
  // stop threads
2842
#ifdef XFORMS
Raymond Knopp's avatar
 
Raymond Knopp committed
2843 2844 2845
  printf("waiting for XFORMS thread\n");
  if (do_forms==1)
    {
Raymond Knopp's avatar
 
Raymond Knopp committed
2846
      pthread_join(forms_thread,&status);
Raymond Knopp's avatar
 
Raymond Knopp committed
2847 2848 2849
      fl_hide_form(form_stats->stats_form);
      fl_free_form(form_stats->stats_form);
      if (UE_flag==1) {
2850 2851
	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
2852
      } else {
2853 2854
	fl_hide_form(form_stats_l2->stats_form);
	fl_free_form(form_stats_l2->stats_form);
Raymond Knopp's avatar
 
Raymond Knopp committed
2855 2856 2857 2858
	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
2859
      }
Raymond Knopp's avatar
 
Raymond Knopp committed
2860
    }
2861 2862
#endif

Raymond Knopp's avatar
 
Raymond Knopp committed
2863 2864 2865
  printf("stopping MODEM threads\n");
  // cleanup
  if (UE_flag == 1) {
2866
#ifdef EXMIMO
2867
#ifdef RTAI
Raymond Knopp's avatar
 
Raymond Knopp committed
2868
    rt_thread_join(main_ue_thread); 
2869
#else
Raymond Knopp's avatar
 
Raymond Knopp committed
2870
    pthread_join(main_ue_thread,&status); 
2871 2872
#endif
#ifdef DLSCH_THREAD
Raymond Knopp's avatar
 
Raymond Knopp committed
2873 2874
    cleanup_dlsch_threads();
    cleanup_rx_pdsch_thread();
2875
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
2876 2877 2878
#endif
  }
  else {
Raymond Knopp's avatar
 
Raymond Knopp committed
2879
#ifdef DEBUG_THREADS
Raymond Knopp's avatar
 
Raymond Knopp committed
2880
    printf("Joining eNB_thread ...");
Raymond Knopp's avatar
 
Raymond Knopp committed
2881
#endif
2882 2883 2884
#ifdef RTAI
    rt_thread_join(main_eNB_thread); 
#else
Raymond Knopp's avatar
 
Raymond Knopp committed
2885
    pthread_join(main_eNB_thread,(void**)&eNB_thread_status_p); 
Raymond Knopp's avatar
 
Raymond Knopp committed
2886
#ifdef DEBUG_THREADS
Raymond Knopp's avatar
 
Raymond Knopp committed
2887
    printf("status %d\n",*eNB_thread_status_p);
Raymond Knopp's avatar
 
Raymond Knopp committed
2888
#endif
2889
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
2890

Raymond Knopp's avatar
 
Raymond Knopp committed
2891 2892 2893
    if (multi_thread>0) {
      printf("Killing eNB processing threads\n");
      kill_eNB_proc();
Raymond Knopp's avatar
 
Raymond Knopp committed
2894

Raymond Knopp's avatar
 
Raymond Knopp committed
2895
    }
Raymond Knopp's avatar
 
Raymond Knopp committed
2896
  }
2897

2898
#ifdef OPENAIR2
Raymond Knopp's avatar
 
Raymond Knopp committed
2899
  //cleanup_pdcp_thread();
2900 2901
#endif

2902
#ifdef RTAI
Raymond Knopp's avatar
 
Raymond Knopp committed
2903
  stop_rt_timer();
2904
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
2905 2906
  pthread_cond_destroy(&sync_cond);
  pthread_mutex_destroy(&sync_mutex);
2907

2908

2909
#ifdef EXMIMO
Raymond Knopp's avatar
 
Raymond Knopp committed
2910
  printf("stopping card\n");
2911
  openair0_stop(0);
Raymond Knopp's avatar
 
Raymond Knopp committed
2912 2913
  printf("closing openair0_lib\n");
  openair0_close();
Raymond Knopp's avatar
 
Raymond Knopp committed
2914
#endif
2915 2916

#ifdef EMOS
Raymond Knopp's avatar
 
Raymond Knopp committed
2917 2918 2919
  printf("waiting for EMOS thread\n");
  pthread_cancel(thread3);
  pthread_join(thread3,&status);
2920 2921 2922
#endif

#ifdef EMOS
Raymond Knopp's avatar
 
Raymond Knopp committed
2923 2924
  error_code = rtf_destroy(CHANSOUNDER_FIFO_MINOR);
  printf("[OPENAIR][SCHED][CLEANUP] EMOS FIFO closed, error_code %d\n", error_code);
2925 2926
#endif

Raymond Knopp's avatar
 
Raymond Knopp committed
2927 2928
  if (ouput_vcd)
    vcd_signal_dumper_close();
2929

Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
2930
#ifdef OPENAIR2
2931 2932
  if (opt_type != OPT_NONE)
    terminate_opt();
Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
2933
#endif
2934

Raymond Knopp's avatar
 
Raymond Knopp committed
2935
  logClean();
2936

Raymond Knopp's avatar
 
Raymond Knopp committed
2937 2938
  return 0;
}
2939 2940


2941 2942 2943 2944 2945
/* 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]) {
2946

2947
  int i, CC_id;
2948
#ifndef EXMIMO
Raymond Knopp's avatar
 
Raymond Knopp committed
2949
  uint16_t N_TA_offset = 0;
Raymond Knopp's avatar
 
Raymond Knopp committed
2950 2951
#else
  int j;
Raymond Knopp's avatar
 
Raymond Knopp committed
2952
#endif
2953
  LTE_DL_FRAME_PARMS *frame_parms;
2954

Raymond Knopp's avatar
 
Raymond Knopp committed
2955

2956 2957
  for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
    if (phy_vars_eNB[CC_id]) {
Raymond Knopp's avatar
 
Raymond Knopp committed
2958 2959 2960
      frame_parms = &(phy_vars_eNB[CC_id]->lte_frame_parms);
      printf("setup_eNB_buffers: frame_parms = %p\n",frame_parms);
    } 
2961 2962 2963
    else {
      printf("phy_vars_eNB[%d] not initialized\n", CC_id);
      return(-1);
Raymond Knopp's avatar
 
Raymond Knopp committed
2964
    }
Raymond Knopp's avatar
 
Raymond Knopp committed
2965

2966
#ifndef EXMIMO
2967 2968
    if (frame_parms->frame_type == TDD) {
      if (frame_parms->N_RB_DL == 100)
Raymond Knopp's avatar
 
Raymond Knopp committed
2969
	N_TA_offset = 624;
2970
      else if (frame_parms->N_RB_DL == 50)
Raymond Knopp's avatar
 
Raymond Knopp committed
2971
	N_TA_offset = 624/2;
2972
      else if (frame_parms->N_RB_DL == 25)
Raymond Knopp's avatar
 
Raymond Knopp committed
2973 2974
	N_TA_offset = 624/4;
    }
Raymond Knopp's avatar
 
Raymond Knopp committed
2975
#endif
2976 2977 2978 2979 2980
   

   

  
Raymond Knopp's avatar
 
Raymond Knopp committed
2981
    // replace RX signal buffers with mmaped HW versions
2982
#ifdef EXMIMO
Raymond Knopp's avatar
 
Raymond Knopp committed
2983 2984 2985
    openair0_cfg[CC_id].tx_num_channels = 0;
    openair0_cfg[CC_id].rx_num_channels = 0;

2986 2987 2988 2989 2990 2991 2992 2993 2994 2995
    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];
2996
	openair0_cfg[rf_map[CC_id].card].rx_gain[rf_map[CC_id].chain+i] = rx_gain[CC_id][i];
2997 2998 2999
	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
3000
      for (j=0;j<16;j++) {
3001 3002
	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;
3003
      }
Raymond Knopp's avatar
 
Raymond Knopp committed
3004
    }
3005 3006 3007 3008 3009 3010 3011 3012 3013 3014
    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];
3015
	openair0_cfg[rf_map[CC_id].card].tx_gain[rf_map[CC_id].chain+i] = tx_gain[CC_id][i];
3016 3017 3018 3019
	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
3020
      for (j=0;j<16;j++) {
3021 3022
	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;
3023
      }
3024
    }
3025
#else // not EXMIMO
3026 3027 3028
    rxdata = (int32_t**)malloc16(frame_parms->nb_antennas_rx*sizeof(int32_t*));
    txdata = (int32_t**)malloc16(frame_parms->nb_antennas_tx*sizeof(int32_t*));

3029 3030
    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
3031
      rxdata[i] = (int32_t*)(16 + malloc16(16+samples_per_frame*sizeof(int32_t)));
3032 3033 3034
      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
3035
    }
3036 3037
    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
3038
      txdata[i] = (int32_t*)(16 + malloc16(16 + samples_per_frame*sizeof(int32_t)));
3039 3040
      phy_vars_eNB[CC_id]->lte_eNB_common_vars.txdata[0][i] = txdata[i];
      memset(txdata[i], 0, samples_per_frame*sizeof(int32_t));
3041
      printf("txdata[%d] @ %p\n", i, phy_vars_eNB[CC_id]->lte_eNB_common_vars.txdata[0][i]);
3042

Raymond Knopp's avatar
 
Raymond Knopp committed
3043 3044
    }
#endif
3045
  }
3046
  return(0);
Raymond Knopp's avatar
 
Raymond Knopp committed
3047
}
3048

3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067
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);
  }
}