lte-ue.c 64.7 KB
Newer Older
1
/*******************************************************************************
2
    OpenAirInterface
3 4 5 6 7 8 9 10 11 12 13 14 15 16
    Copyright(c) 1999 - 2014 Eurecom

    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.


    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.

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

   Contact Information
   OpenAirInterface Admin: openair_admin@eurecom.fr
   OpenAirInterface Tech : openair_tech@eurecom.fr
24
   OpenAirInterface Dev  : openair4g-devel@lists.eurecom.fr
25

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
   Address      : Eurecom, Campus SophiaTech, 450 Route des Chappes, CS 50193 - 06904 Biot Sophia Antipolis cedex, FRANCE

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

/*! \file lte-ue.c
 * \brief threads and support functions for real-time LTE UE target
 * \author R. Knopp, F. Kaltenberger, Navid Nikaein
 * \date 2015
 * \version 0.1
 * \company Eurecom
 * \email: knopp@eurecom.fr,florian.kaltenberger@eurecom.fr, navid.nikaein@eurecom.fr
 * \note
 * \warning
 */
#define _GNU_SOURCE
#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>
#include <linux/sched.h>
#include <signal.h>
#include <execinfo.h>
#include <getopt.h>
#include <syscall.h>

#include "rt_wrapper.h"
#include "assertions.h"
#include "PHY/types.h"

#include "PHY/defs.h"
62
#ifdef OPENAIR2
63 64
#include "LAYER2/MAC/defs.h"
#include "RRC/LITE/extern.h"
65
#endif
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
#include "PHY_INTERFACE/extern.h"

#undef MALLOC //there are two conflicting definitions, so we better make sure we don't use it at all
//#undef FRAME_LENGTH_COMPLEX_SAMPLES //there are two conflicting definitions, so we better make sure we don't use it at all

#ifdef EXMIMO
#include "openair0_lib.h"
#else
#include "../../ARCH/COMMON/common_lib.h"
#endif

#include "PHY/extern.h"
#include "MAC_INTERFACE/extern.h"
//#include "SCHED/defs.h"
#include "SCHED/extern.h"
81
#ifdef OPENAIR2
82 83
#include "LAYER2/MAC/extern.h"
#include "LAYER2/MAC/proto.h"
84
#endif
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

#include "UTIL/LOG/log_extern.h"
#include "UTIL/OTG/otg_tx.h"
#include "UTIL/OTG/otg_externs.h"
#include "UTIL/MATH/oml.h"
#include "UTIL/LOG/vcd_signal_dumper.h"
#include "UTIL/OPT/opt.h"

#define FRAME_PERIOD    100000000ULL
#define DAQ_PERIOD      66667ULL

typedef enum {
  pss=0,
  pbch=1,
  si=2
} sync_mode_t;

int init_dlsch_threads(void);
void cleanup_dlsch_threads(void);
int32_t init_rx_pdsch_thread(void);
void cleanup_rx_pdsch_thread(void);

extern pthread_cond_t sync_cond;
extern pthread_mutex_t sync_mutex;
extern int sync_var;

extern openair0_config_t openair0_cfg[MAX_CARDS];
extern uint32_t          downlink_frequency[MAX_NUM_CCs][4];
113
extern int32_t           uplink_frequency_offset[MAX_NUM_CCs][4];
114 115 116 117 118 119 120 121
extern openair0_rf_map rf_map[MAX_NUM_CCs];

extern openair0_device openair0;
extern int oai_exit;

extern int32_t **rxdata;
extern int32_t **txdata;

122 123 124
extern char * input_file;
extern int input_buffer_l;

125 126
//extern unsigned int tx_forward_nsamps;
//extern int tx_delay;
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

extern int rx_input_level_dBm;
extern uint8_t exit_missed_slots;
extern uint64_t num_missed_slots; // counter for the number of missed slots

extern void exit_fun(const char* s);

#ifdef EXMIMO

extern unsigned int             rxg_max[4];
extern unsigned int             rxg_med[4];
extern unsigned int             rxg_byp[4];
extern unsigned int             nf_max[4];
extern unsigned int             nf_med[4];
extern unsigned int             nf_byp[4];
extern rx_gain_t                rx_gain_mode[MAX_NUM_CCs][4];

extern double tx_gain[MAX_NUM_CCs][4];
extern double rx_gain[MAX_NUM_CCs][4];
#endif
#define KHz (1000UL)
#define MHz (1000 * KHz)

typedef struct eutra_band_s {
  int16_t band;
  uint32_t ul_min;
  uint32_t ul_max;
  uint32_t dl_min;
  uint32_t dl_max;
  lte_frame_type_t frame_type;
} eutra_band_t;

typedef struct band_info_s {
  int nbands;
  eutra_band_t band_info[100];
} band_info_t;

band_info_t bands_to_scan;

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
static const eutra_band_t eutra_bands[] = {
  { 1, 1920    * MHz, 1980    * MHz, 2110    * MHz, 2170    * MHz, FDD},
  { 2, 1850    * MHz, 1910    * MHz, 1930    * MHz, 1990    * MHz, FDD},
  { 3, 1710    * MHz, 1785    * MHz, 1805    * MHz, 1880    * MHz, FDD},
  { 4, 1710    * MHz, 1755    * MHz, 2110    * MHz, 2155    * MHz, FDD},
  { 5,  824    * MHz,  849    * MHz,  869    * MHz,  894    * MHz, FDD},
  { 6,  830    * MHz,  840    * MHz,  875    * MHz,  885    * MHz, FDD},
  { 7, 2500    * MHz, 2570    * MHz, 2620    * MHz, 2690    * MHz, FDD},
  { 8,  880    * MHz,  915    * MHz,  925    * MHz,  960    * MHz, FDD},
  { 9, 1749900 * KHz, 1784900 * KHz, 1844900 * KHz, 1879900 * KHz, FDD},
  {10, 1710    * MHz, 1770    * MHz, 2110    * MHz, 2170    * MHz, FDD},
  {11, 1427900 * KHz, 1452900 * KHz, 1475900 * KHz, 1500900 * KHz, FDD},
  {12,  698    * MHz,  716    * MHz,  728    * MHz,  746    * MHz, FDD},
  {13,  777    * MHz,  787    * MHz,  746    * MHz,  756    * MHz, FDD},
  {14,  788    * MHz,  798    * MHz,  758    * MHz,  768    * MHz, FDD},
  {17,  704    * MHz,  716    * MHz,  734    * MHz,  746    * MHz, FDD},
  {20,  832    * MHz,  862    * MHz,  791    * MHz,  821    * MHz, FDD},
183
  {22, 3510    * MHz, 3590    * MHz, 3410    * MHz, 3490    * MHz, FDD},
184 185 186 187 188 189 190 191 192 193 194 195 196 197
  {33, 1900    * MHz, 1920    * MHz, 1900    * MHz, 1920    * MHz, TDD},
  {34, 2010    * MHz, 2025    * MHz, 2010    * MHz, 2025    * MHz, TDD},
  {35, 1850    * MHz, 1910    * MHz, 1850    * MHz, 1910    * MHz, TDD},
  {36, 1930    * MHz, 1990    * MHz, 1930    * MHz, 1990    * MHz, TDD},
  {37, 1910    * MHz, 1930    * MHz, 1910    * MHz, 1930    * MHz, TDD},
  {38, 2570    * MHz, 2620    * MHz, 2570    * MHz, 2630    * MHz, TDD},
  {39, 1880    * MHz, 1920    * MHz, 1880    * MHz, 1920    * MHz, TDD},
  {40, 2300    * MHz, 2400    * MHz, 2300    * MHz, 2400    * MHz, TDD},
  {41, 2496    * MHz, 2690    * MHz, 2496    * MHz, 2690    * MHz, TDD},
  {42, 3400    * MHz, 3600    * MHz, 3400    * MHz, 3600    * MHz, TDD},
  {43, 3600    * MHz, 3800    * MHz, 3600    * MHz, 3800    * MHz, TDD},
  {44, 703    * MHz, 803    * MHz, 703    * MHz, 803    * MHz, TDD},
};

198 199
/*!
 * \brief This is the UE synchronize thread.
200
 * It performs band scanning and synchonization.
201 202 203
 * \param arg is a pointer to a \ref PHY_VARS_UE structure.
 * \returns a pointer to an int. The storage is not on the heap and must not be freed.
 */
204 205
static void *UE_thread_synch(void *arg)
{
206 207 208
  static int UE_thread_synch_retval;
  int i, hw_slot_offset;
  PHY_VARS_UE *UE = (PHY_VARS_UE*) arg;
209 210
  int current_band = 0;
  int current_offset = 0;
211
  sync_mode_t sync_mode = pbch;
212
  int card;
213 214
  int ind;
  int found;
215
  int freq_offset=0;
216 217 218 219 220 221 222

  UE->is_synchronized = 0;
  printf("UE_thread_sync in with PHY_vars_UE %p\n",arg);
  printf("waiting for sync (UE_thread_synch) \n");

  pthread_mutex_lock(&sync_mutex);
  printf("Locked sync_mutex, waiting (UE_sync_thread)\n");
223

224 225
  while (sync_var<0)
    pthread_cond_wait(&sync_cond, &sync_mutex);
226

227 228 229
  pthread_mutex_unlock(&sync_mutex);
  printf("unlocked sync_mutex (UE_sync_thread)\n");

230
  printf("starting UE synch thread (IC %d)\n",UE->instance_cnt_synch);
231 232
  ind = 0;
  found = 0;
233 234


235 236
  if (UE->UE_scan == 0) {
    do  {
237
      current_band = eutra_bands[ind].band;
238
      printf( "Scanning band %d, dl_min %"PRIu32", ul_min %"PRIu32"\n", current_band, eutra_bands[ind].dl_min,eutra_bands[ind].ul_min);
239

240
      if ((eutra_bands[ind].dl_min <= downlink_frequency[0][0]) && (eutra_bands[ind].dl_max >= downlink_frequency[0][0])) {
241 242 243 244 245 246 247
        for (card=0; card<MAX_NUM_CCs; card++)
          for (i=0; i<4; i++)
            uplink_frequency_offset[card][i] = eutra_bands[ind].ul_min - eutra_bands[ind].dl_min;

        found = 1;
        break;
      }
248

249
      ind++;
250 251
    } while (ind < sizeof(eutra_bands) / sizeof(eutra_bands[0]));
  
252 253
    if (found == 0) {
      exit_fun("Can't find EUTRA band for frequency");
254
      return &UE_thread_synch_retval;
255
    }
256

257 258


259

260 261


262
    LOG_I( PHY, "[SCHED][UE] Check absolute frequency DL %"PRIu32", UL %"PRIu32" (oai_exit %d)\n", downlink_frequency[0][0], downlink_frequency[0][0]+uplink_frequency_offset[0][0],oai_exit );
263 264 265 266 267 268 269

    for (i=0;i<openair0_cfg[0].rx_num_channels;i++) {
      openair0_cfg[0].rx_freq[i] = downlink_frequency[0][i];
      openair0_cfg[0].tx_freq[i] = downlink_frequency[0][i]+uplink_frequency_offset[0][i];
      openair0_cfg[0].autocal[i] = 1;
    }

270
    sync_mode = pbch;
271

272
  } else if  (UE->UE_scan == 1) {
273
    current_band=0;
274

275
    for (card=0; card<MAX_CARDS; card++) {
276
      for (i=0; i<openair0_cfg[card].rx_num_channels; i++) {
277 278 279 280 281
        downlink_frequency[card][i] = bands_to_scan.band_info[0].dl_min;
        uplink_frequency_offset[card][i] = bands_to_scan.band_info[0].ul_min-bands_to_scan.band_info[0].dl_min;

        openair0_cfg[card].rx_freq[i] = downlink_frequency[card][i];
        openair0_cfg[card].tx_freq[i] = downlink_frequency[card][i]+uplink_frequency_offset[card][i];
282
#ifdef OAI_USRP
283
        openair0_cfg[card].rx_gain[i] = UE->rx_total_gain_dB;//-USRP_GAIN_OFFSET;
284

285
	
286 287 288 289 290 291 292 293 294 295 296 297 298
        switch(UE->lte_frame_parms.N_RB_DL) {
        case 6:
          openair0_cfg[card].rx_gain[i] -= 12;
          break;

        case 25:
          openair0_cfg[card].rx_gain[i] -= 6;
          break;

        case 50:
          openair0_cfg[card].rx_gain[i] -= 3;
          break;

299 300 301 302
        case 100:
          openair0_cfg[card].rx_gain[i] -= 0;
          break;

303
        default:
304
          printf( "Unknown number of RBs %d\n", UE->lte_frame_parms.N_RB_DL );
305 306
          break;
        }
307
	
308
        printf( "UE synch: setting RX gain (%d,%d) to %f\n", card, i, openair0_cfg[card].rx_gain[i] );
309 310
#endif
      }
311 312 313
    }

  }
314 315

  while (oai_exit==0) {
316

317
    if (pthread_mutex_lock(&UE->mutex_synch) != 0) {
318
      LOG_E( PHY, "[SCHED][UE] error locking mutex for UE initial synch thread\n" );
319
      exit_fun("noting to add");
320 321
      return &UE_thread_synch_retval;
    }
322
    
323

324
    while (UE->instance_cnt_synch < 0) {
325
      // the thread waits here most of the time
326 327
      pthread_cond_wait( &UE->cond_synch, &UE->mutex_synch );
    }
328

329 330 331 332 333
    if (pthread_mutex_unlock(&UE->mutex_synch) != 0) {
      LOG_E( PHY, "[SCHED][eNB] error unlocking mutex for UE Initial Synch thread\n" );
      exit_fun("nothing to add");
      return &UE_thread_synch_retval;
    }
334

335
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_UE_SYNCH, 1 );
336 337

    switch (sync_mode) {
338
    case pss:
339 340
      LOG_I(PHY,"[SCHED][UE] Scanning band %d (%d), freq %u\n",bands_to_scan.band_info[current_band].band, current_band,bands_to_scan.band_info[current_band].dl_min+current_offset);
      lte_sync_timefreq(UE,current_band,bands_to_scan.band_info[current_band].dl_min+current_offset);
341 342 343 344 345 346 347
      current_offset += 20000000; // increase by 20 MHz

      if (current_offset > bands_to_scan.band_info[current_band].dl_max-bands_to_scan.band_info[current_band].dl_min) {
        current_band++;
        current_offset=0;
      }

348
      if (current_band==bands_to_scan.nbands) {
349 350
        current_band=0;
        oai_exit=1;
351
      }
352 353 354 355 356 357 358

      for (card=0; card<MAX_CARDS; card++) {
        for (i=0; i<openair0_cfg[card].rx_num_channels; i++) {
          downlink_frequency[card][i] = bands_to_scan.band_info[current_band].dl_min+current_offset;
          uplink_frequency_offset[card][i] = bands_to_scan.band_info[current_band].ul_min-bands_to_scan.band_info[0].dl_min + current_offset;


359 360
          openair0_cfg[card].rx_freq[i] = downlink_frequency[card][i];
          openair0_cfg[card].tx_freq[i] = downlink_frequency[card][i]+uplink_frequency_offset[card][i];
361
#ifdef OAI_USRP
362 363
          openair0_cfg[card].rx_gain[i] = UE->rx_total_gain_dB;//-USRP_GAIN_OFFSET;  // 65 calibrated for USRP B210 @ 2.6 GHz
	  
364 365 366 367 368 369 370 371 372 373 374 375 376
          switch(UE->lte_frame_parms.N_RB_DL) {
          case 6:
            openair0_cfg[card].rx_gain[i] -= 12;
            break;

          case 25:
            openair0_cfg[card].rx_gain[i] -= 6;
            break;

          case 50:
            openair0_cfg[card].rx_gain[i] -= 3;
            break;

377 378 379 380
          case 100:
            openair0_cfg[card].rx_gain[i] -= 0;
            break;

381 382 383 384
          default:
            printf("Unknown number of RBs %d\n",UE->lte_frame_parms.N_RB_DL);
            break;
          }
385
	  
386 387

          printf("UE synch: setting RX gain (%d,%d) to %f\n",card,i,openair0_cfg[card].rx_gain[i]);
388
#endif
389

390
        }
391 392 393

      }

394
      if (UE->UE_scan_carrier) {
395

396 397 398 399
	for (i=0;i<openair0_cfg[0].rx_num_channels;i++)
	  openair0_cfg[0].autocal[i] = 1;

      }
400

401 402 403

      break;
 
404
    case pbch:
405

406
      
407
      if (initial_sync( UE, UE->mode ) == 0) {
408 409 410 411 412 413 414 415 416 417 418 419 420 421

        hw_slot_offset = (UE->rx_offset<<1) / UE->lte_frame_parms.samples_per_tti;
        LOG_I( HW, "Got synch: hw_slot_offset %d\n", hw_slot_offset );
	if (UE->UE_scan_carrier == 1) {

	  UE->UE_scan_carrier = 0;
	  // rerun with new cell parameters and frequency-offset
	  for (i=0;i<openair0_cfg[0].rx_num_channels;i++) {
	    openair0_cfg[0].rx_freq[i] -= UE->lte_ue_common_vars.freq_offset;
	    openair0_cfg[0].tx_freq[i] =  openair0_cfg[0].rx_freq[i]+uplink_frequency_offset[0][i];
	    downlink_frequency[0][i] = openair0_cfg[0].rx_freq[i];
	    freq_offset=0;	    
	  }

422
	  // reconfigure for potentially different bandwidth
423 424 425
	  switch(UE->lte_frame_parms.N_RB_DL) {
	  case 6:
	    openair0_cfg[0].sample_rate =1.92e6;
426 427
	    openair0_cfg[0].rx_bw          =.96e6;
	    openair0_cfg[0].tx_bw          =.96e6;
428
            openair0_cfg[0].rx_gain[0] -= 12;
429 430
	    break;
	  case 25:
431 432 433
	    openair0_cfg[0].sample_rate =7.68e6;
	    openair0_cfg[0].rx_bw          =2.5e6;
	    openair0_cfg[0].tx_bw          =2.5e6;
434
            openair0_cfg[0].rx_gain[0] -= 6;
435 436
	    break;
	  case 50:
437 438 439
	    openair0_cfg[0].sample_rate =15.36e6;
	    openair0_cfg[0].rx_bw          =5.0e6;
	    openair0_cfg[0].tx_bw          =5.0e6;
440
            openair0_cfg[0].rx_gain[0] -= 3;
441 442 443
	    break;
	  case 100:
	    openair0_cfg[0].sample_rate=30.72e6;
444 445
	    openair0_cfg[0].rx_bw=10.0e6;
	    openair0_cfg[0].tx_bw=10.0e6;
446
            openair0_cfg[0].rx_gain[0] -= 0;
447 448
	    break;
	  }
449 450 451 452 453
#ifndef EXMIMO
	  openair0.trx_set_freq_func(&openair0,&openair0_cfg[0],0);
	  openair0.trx_set_gains_func(&openair0,&openair0_cfg[0]);
	  openair0.trx_stop_func(0);	  
#else
454 455 456
	  openair0_set_frequencies(&openair0,&openair0_cfg[0],0);
	  openair0_set_gains(&openair0,&openair0_cfg[0]);
	  openair0_stop(0);
457
#endif
458
	  sleep(1);
459 460 461 462
	  init_frame_parms(&UE->lte_frame_parms,1);
	}
	else {
	  UE->is_synchronized = 1;
463 464

	 if( UE->mode == rx_dump_frame ){
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
	   FILE *fd;
	   if ((UE->frame_rx&1) == 0) {  // this guarantees SIB1 is present 
	     if (fd = fopen("rxsig_frame0.dat","w")) {
	       fwrite((void*)&UE->lte_ue_common_vars.rxdata[0][0],
		      sizeof(int32_t),
		      10*UE->lte_frame_parms.samples_per_tti,
		      fd);
	       LOG_I(PHY,"Dummping Frame ... bye bye \n");
	       fclose(fd);
	       exit(0);
	     }
	     else {
	       LOG_E(PHY,"Cannot open file for writing\n");
	       exit(0);
	     }
	   }
	   else {
	     UE->is_synchronized = 0;
	   }
484
	 }
485
	 
486

487
#ifndef EXMIMO
488 489
	  UE->slot_rx = 0;
	  UE->slot_tx = 4;
490
#else
491 492
	  UE->slot_rx = 18;
	  UE->slot_tx = 2;
493
#endif
494
	}
495 496 497
      } else {
        // initial sync failed
        // calculate new offset and try again
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
	if (UE->UE_scan_carrier == 1) {
	  if (freq_offset >= 0) {
	    freq_offset += 100;
	    freq_offset *= -1;
	  } else {
	    freq_offset *= -1;
	  }
	
	  if (abs(freq_offset) > 7500) {
	    LOG_I( PHY, "[initial_sync] No cell synchronization found, abandoning\n" );
	    mac_xface->macphy_exit("No cell synchronization found, abandoning");
	    return &UE_thread_synch_retval; // not reached
	  }
	}
	else {
	  
	}
        LOG_I( PHY, "[initial_sync] trying carrier off %d Hz, rxgain %d (DL %u, UL %u)\n", 
	       freq_offset,
517
               UE->rx_total_gain_dB,
518 519
               downlink_frequency[0][0]+freq_offset,
               downlink_frequency[0][0]+uplink_frequency_offset[0][0]+freq_offset );
520 521 522

        for (card=0; card<MAX_CARDS; card++) {
          for (i=0; i<openair0_cfg[card].rx_num_channels; i++) {
523 524
            openair0_cfg[card].rx_freq[i] = downlink_frequency[card][i]+freq_offset;
            openair0_cfg[card].tx_freq[i] = downlink_frequency[card][i]+uplink_frequency_offset[card][i]+freq_offset;
525
#ifdef OAI_USRP
526
            openair0_cfg[card].rx_gain[i] = UE->rx_total_gain_dB;//-USRP_GAIN_OFFSET;
527 528 529 530 531 532
	    
	    
#ifndef EXMIMO
	    openair0.trx_set_freq_func(&openair0,&openair0_cfg[0],0);
	    
#else
533
	    openair0_set_frequencies(&openair0,&openair0_cfg[0],0);
534
	    
535
#endif
536 537 538 539 540 541 542 543 544 545
            switch(UE->lte_frame_parms.N_RB_DL) {
            case 6:
              openair0_cfg[card].rx_gain[i] -= 12;
              break;

            case 25:
              openair0_cfg[card].rx_gain[i] -= 6;
              break;

            case 50:
546 547 548 549 550
              openair0_cfg[card].rx_gain[i] -= 0;//3;
              break;

            case 100:
              openair0_cfg[card].rx_gain[i] -= 0;
551 552 553 554 555 556
              break;

            default:
              printf("Unknown number of RBs %d\n",UE->lte_frame_parms.N_RB_DL);
              break;
            }
557
	    
558
#endif
559 560
          }
        }
561 562 563 564 565 566
	if (UE->UE_scan_carrier==1) {
	  for (i=0;i<openair0_cfg[0].rx_num_channels;i++)
	    openair0_cfg[0].autocal[i] = 1;
	  
	}
      }// initial_sync=0
567

568
      break;
569

570 571 572 573
    case si:
    default:
      break;
    }
574

575
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_UE_SYNCH, 0 );
576

577 578


579
    if (pthread_mutex_lock(&UE->mutex_synch) != 0) {
580 581 582 583
      LOG_E( PHY, "[SCHED][UE] error locking mutex for UE synch\n" );
      exit_fun("noting to add");
      return &UE_thread_synch_retval;
    }
584

585
    // indicate readiness
586 587 588 589 590 591
    UE->instance_cnt_synch--;

    if (pthread_mutex_unlock(&UE->mutex_synch) != 0) {
      LOG_E( PHY, "[SCHED][UE] error unlocking mutex for UE synch\n" );
      exit_fun("noting to add");
      return &UE_thread_synch_retval;
592
    }
593

594
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_UE_SYNCH, 0 );
595
  }  // while !oai_exit
596

597
  return &UE_thread_synch_retval;
598 599
}

600 601 602 603 604 605
/*!
 * \brief This is the UE transmit thread.
 * This thread performs the phy_procedures_UE_TX() on every transmit slot.
 * \param arg is a pointer to a \ref PHY_VARS_UE structure.
 * \returns a pointer to an int. The storage is not on the heap and must not be freed.
 */
606 607
static void *UE_thread_tx(void *arg)
{
608
  static int UE_thread_tx_retval;
609 610 611 612 613 614 615
  int ret;

  PHY_VARS_UE *UE = (PHY_VARS_UE*)arg;

  UE->instance_cnt_tx=-1;

#ifdef RTAI
616
  RT_TASK *task = rt_task_init_schmod(nam2num("UE TX Thread"), 0, 0, 0, SCHED_FIFO, 0xF);
617

618 619 620 621
  if (task==NULL) {
    LOG_E(PHY,"[SCHED][UE] Problem starting UE TX thread!!!!\n");
    return 0;
  }
622

623 624 625 626
  LOG_D(HW,"Started UE TX thread (id %p)\n",task);
#else

#ifdef LOWLATENCY
627 628 629
  struct sched_attr attr;
  unsigned int flags = 0;

630 631 632
  attr.size = sizeof(attr);
  attr.sched_flags = 0;
  attr.sched_nice = 0;
633
  attr.sched_priority = 0;
634

635
  /* This creates a 1ms reservation every 10ms period*/
636 637 638 639
  attr.sched_policy   = SCHED_DEADLINE;
  attr.sched_runtime  = 900000;  // each tx thread requires .5ms to finish its job
  attr.sched_deadline = 1000000; // each tx thread will finish within 1ms
  attr.sched_period   = 1000000; // each tx thread has a period of 1ms from the starting point
640

641 642

  if (sched_setattr(0, &attr, flags) < 0 ) {
knopp's avatar
knopp committed
643
    perror("[SCHED] UE_thread_tx thread: sched_setattr failed\n");
644
    return &UE_thread_tx_retval;
645
  }
646

647 648 649 650 651
#else
  struct sched_param sp;
  sp.sched_priority = sched_get_priority_max(SCHED_FIFO)-1;
  pthread_setschedparam(pthread_self(),SCHED_FIFO,&sp);

652 653
#endif
#endif
654

655 656 657 658
  printf("waiting for sync (UE_thread_tx)\n");

  pthread_mutex_lock(&sync_mutex);
  printf("Locked sync_mutex, waiting (UE_thread_tx)\n");
659

660 661
  while (sync_var<0)
    pthread_cond_wait(&sync_cond, &sync_mutex);
662

663 664 665 666 667
  pthread_mutex_unlock(&sync_mutex);
  printf("unlocked sync_mutex, waiting (UE_thread_tx)\n");

  printf("Starting UE TX thread\n");

668
  // Lock memory from swapping. This is a process wide call (not constraint to this thread).
669 670 671 672 673
  mlockall(MCL_CURRENT | MCL_FUTURE);

  while (!oai_exit) {

    if (pthread_mutex_lock(&UE->mutex_tx) != 0) {
674
      LOG_E( PHY, "[SCHED][UE] error locking mutex for UE TX\n" );
675
      exit_fun("nothing to add");
676 677
      return &UE_thread_tx_retval;
    }
678

679 680 681
    while (UE->instance_cnt_tx < 0) {
      // most of the time, the thread is waiting here
      pthread_cond_wait( &UE->cond_tx, &UE->mutex_tx );
682
    }
683

684 685 686 687 688
    if (pthread_mutex_unlock(&UE->mutex_tx) != 0) {
      LOG_E( PHY, "[SCHED][UE] error unlocking mutex for UE TX\n" );
      exit_fun("nothing to add");
      return &UE_thread_tx_retval;
    }
689
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_UE_THREAD_TX, 1 );
690

691
    if ((subframe_select( &UE->lte_frame_parms, UE->slot_tx>>1 ) == SF_UL) ||
692
        (UE->lte_frame_parms.frame_type == FDD)) {
693
      phy_procedures_UE_TX( UE, 0, 0, UE->mode, no_relay );
694
    }
695

696 697 698
    if ((subframe_select( &UE->lte_frame_parms, UE->slot_tx>>1 ) == SF_S) &&
        ((UE->slot_tx&1) == 1)) {
      phy_procedures_UE_S_TX( UE, 0, 0, no_relay );
699
    }
700

701 702 703 704 705 706 707 708 709 710 711
    UE->slot_tx += 2;

    if (UE->slot_tx >= 20) {
      UE->slot_tx -= 20;
      UE->frame_tx++;
      VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_FRAME_NUMBER_TX_UE, UE->frame_tx );
    }

    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_SUBFRAME_NUMBER_TX_UE, UE->slot_tx>>1 );

    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_UE_THREAD_TX, 0 );
712

713
    if (pthread_mutex_lock(&UE->mutex_tx) != 0) {
714 715 716 717
      LOG_E( PHY, "[SCHED][UE] error locking mutex for UE TX thread\n" );
      exit_fun("nothing to add");
      return &UE_thread_tx_retval;
    }
718

719
    UE->instance_cnt_tx--;
720
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_UE_INST_CNT_TX, UE->instance_cnt_tx);
721 722 723 724 725

    if (pthread_mutex_unlock(&UE->mutex_tx) != 0) {
      LOG_E( PHY, "[SCHED][UE] error unlocking mutex for UE TX thread\n" );
      exit_fun("nothing to add");
      return &UE_thread_tx_retval;
726
    }
727

728
  }
729

730
  return &UE_thread_tx_retval;
731 732
}

733 734 735 736 737 738
/*!
 * \brief This is the UE receive thread.
 * This thread performs the phy_procedures_UE_RX() on every received slot.
 * \param arg is a pointer to a \ref PHY_VARS_UE structure.
 * \returns a pointer to an int. The storage is not on the heap and must not be freed.
 */
739

740
/*
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
#ifdef OAI_USRP
void rescale(int16_t *input,int length)
{
#if defined(__x86_64__) || defined(__i386__)
  __m128i *input128 = (__m128i *)input;
#elif defined(__arm__)
  int16x8_t *input128 = (int16x8_t *)input;
#endif
  int i;

  for (i=0; i<length>>2; i++) {
#if defined(__x86_64__) || defined(__i386__)
    input128[i] = _mm_srai_epi16(input128[i],4);
#elif defined(__arm__)
    input128[i] = vshrq_n_s16(input128[i],4);
#endif
  }
}
#endif
760
*/
761

762 763
static void *UE_thread_rx(void *arg)
{
764
  static int UE_thread_rx_retval;
765 766 767
  PHY_VARS_UE *UE = (PHY_VARS_UE*)arg;
  int i;
  int ret;
768

769 770 771
  UE->instance_cnt_rx=-1;

#ifdef RTAI
772
  RT_TASK *task = rt_task_init_schmod(nam2num("UE RX Thread"), 0, 0, 0, SCHED_FIFO, 0xF);
773

774 775
  if (task==NULL) {
    LOG_E(PHY,"[SCHED][UE] Problem starting UE RX thread!!!!\n");
776
    return &UE_thread_rx_retval;
777
  }
778

779 780
  LOG_D(HW,"Started UE RX thread (id %p)\n",task);
#else
781

782
#ifdef LOWLATENCY
783 784 785
  struct sched_attr attr;
  unsigned int flags = 0;

786 787 788
  attr.size = sizeof(attr);
  attr.sched_flags = 0;
  attr.sched_nice = 0;
789
  attr.sched_priority = 0;
790

791
  // This creates a .5ms reservation every 1ms period
792 793 794 795
  attr.sched_policy   = SCHED_DEADLINE;
  attr.sched_runtime  = 900000;  // each rx thread requires 1ms to finish its job
  attr.sched_deadline = 1000000; // each rx thread will finish within 1ms
  attr.sched_period   = 1000000; // each rx thread has a period of 1ms from the starting point
796 797

  if (sched_setattr(0, &attr, flags) < 0 ) {
knopp's avatar
knopp committed
798
    perror("[SCHED] UE_thread_rx : sched_setattr failed\n");
799
    return &UE_thread_rx_retval;
800 801
  }

802 803 804 805 806
#else
  struct sched_param sp;
  sp.sched_priority = sched_get_priority_max(SCHED_FIFO)-1;
  pthread_setschedparam(pthread_self(),SCHED_FIFO,&sp);

807 808
#endif
#endif
809

810
  // Lock memory from swapping. This is a process wide call (not constraint to this thread).
811
  mlockall(MCL_CURRENT | MCL_FUTURE);
812

813
  printf("waiting for sync (UE_thread_rx)\n");
814

815 816
  pthread_mutex_lock(&sync_mutex);
  printf("Locked sync_mutex, waiting (UE_thread_rx)\n");
817

818 819
  while (sync_var<0)
    pthread_cond_wait(&sync_cond, &sync_mutex);
820

821 822
  pthread_mutex_unlock(&sync_mutex);
  printf("unlocked sync_mutex, waiting (UE_thread_rx)\n");
823

824
  printf("Starting UE RX thread\n");
825 826

  while (!oai_exit) {
827
    if (pthread_mutex_lock(&UE->mutex_rx) != 0) {
828
      LOG_E( PHY, "[SCHED][UE] error locking mutex for UE RX\n" );
829
      exit_fun("nothing to add");
830
      return &UE_thread_rx_retval;
831
    }
832 833

    while (UE->instance_cnt_rx < 0) {
834 835
      // most of the time, the thread is waiting here
      pthread_cond_wait( &UE->cond_rx, &UE->mutex_rx );
836 837 838
    }

    if (pthread_mutex_unlock(&UE->mutex_rx) != 0) {
839
      LOG_E( PHY, "[SCHED][UE] error unlocking mutex for UE RX\n" );
840
      exit_fun("nothing to add");
841
      return &UE_thread_rx_retval;
842 843
    }

844
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_UE_THREAD_RX, 1 );
845
    for (i=0; i<2; i++) {
846
      if ((subframe_select( &UE->lte_frame_parms, UE->slot_rx>>1 ) == SF_DL) ||
847
          (UE->lte_frame_parms.frame_type == FDD)) {
848
	/*
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
#ifdef OAI_USRP
	// this does the adjustments of RX signal amplitude to bring into least 12 significant bits
	int slot_length = UE->lte_frame_parms.samples_per_tti>>1;
	int rx_offset = (UE->slot_rx)*slot_length + UE->rx_offset;
	int frame_length = UE->lte_frame_parms.samples_per_tti*10;
	int aa;
	if (rx_offset > frame_length)
	  rx_offset-=frame_length;


	if (rx_offset >= 0) {
	  if (rx_offset + slot_length < frame_length)
	    for (aa=0;aa<UE->lte_frame_parms.nb_antennas_rx;aa++)
	      rescale((int16_t*)&UE->lte_ue_common_vars.rxdata[aa][rx_offset&(~0x3)],
		      slot_length);
	  else {
	    int diff = rx_offset + slot_length - frame_length;
	    for (aa=0;aa<UE->lte_frame_parms.nb_antennas_rx;aa++){
	      rescale((int16_t*)&UE->lte_ue_common_vars.rxdata[aa][rx_offset&(~0x3)],
		      slot_length-diff);
	      rescale((int16_t*)&UE->lte_ue_common_vars.rxdata[aa][0],
		      diff);
	    }
	  }
	}
	else {
	    for (aa=0;aa<UE->lte_frame_parms.nb_antennas_rx;aa++){
	      rescale((int16_t*)&UE->lte_ue_common_vars.rxdata[aa][(frame_length+rx_offset)&(~0x3)],
		      -rx_offset);
	      rescale((int16_t*)&UE->lte_ue_common_vars.rxdata[aa][0],
		      slot_length+rx_offset);
	    }
	}
#endif
883
	*/
884
        phy_procedures_UE_RX( UE, 0, 0, UE->mode, no_relay, NULL );
885
      }
gauthier's avatar
gauthier committed
886

887 888
      if ((subframe_select( &UE->lte_frame_parms, UE->slot_rx>>1 ) == SF_S) &&
          ((UE->slot_rx&1) == 0)) {
889
	/*
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
#ifdef OAI_USRP
	// this does the adjustments of RX signal amplitude to bring into least 12 significant bits
	int slot_length = UE->lte_frame_parms.samples_per_tti>>1;
	int rx_offset = (UE->slot_rx)*slot_length + UE->rx_offset;
	int frame_length = UE->lte_frame_parms.samples_per_tti*10;
	if (rx_offset > frame_length)
	  rx_offset-=frame_length;
	int aa;

	if (rx_offset >= 0) {
	  if (rx_offset + slot_length < frame_length)
	    for (aa=0;aa<UE->lte_frame_parms.nb_antennas_rx;aa++)
	      rescale((int16_t*)&UE->lte_ue_common_vars.rxdata[aa][rx_offset&(~0x3)],
		      slot_length);
	  else {
	    int diff = rx_offset + slot_length - frame_length;
	    for (aa=0;aa<UE->lte_frame_parms.nb_antennas_rx;aa++){
	      rescale((int16_t*)&UE->lte_ue_common_vars.rxdata[aa][rx_offset&(~0x3)],
		      slot_length-diff);
	      rescale((int16_t*)&UE->lte_ue_common_vars.rxdata[aa][0],
		      diff);
	    }
	  }
	}
	else {
	  for (aa=0;aa<UE->lte_frame_parms.nb_antennas_rx;aa++){
	    rescale((int16_t*)&UE->lte_ue_common_vars.rxdata[aa][(frame_length+rx_offset)&(~0x3)],
		    -rx_offset);
	    rescale((int16_t*)&UE->lte_ue_common_vars.rxdata[aa][0],
		    slot_length+rx_offset);
	  }
	}
#endif
923
	*/
924
        phy_procedures_UE_RX( UE, 0, 0, UE->mode, no_relay, NULL );
925 926 927 928 929 930 931 932 933 934 935 936 937
      }

#ifdef OPENAIR2

      if (i==0) {
        ret = mac_xface->ue_scheduler(UE->Mod_id,
                                      UE->frame_tx,
                                      UE->slot_rx>>1,
                                      subframe_select(&UE->lte_frame_parms,UE->slot_tx>>1),
                                      0,
                                      0/*FIXME CC_id*/);

        if (ret == CONNECTION_LOST) {
938
          LOG_E( PHY, "[UE %"PRIu8"] Frame %"PRIu32", subframe %u RRC Connection lost, returning to PRACH\n",
939
                 UE->Mod_id, UE->frame_rx, UE->slot_tx>>1 );
940 941
          UE->UE_mode[0] = PRACH;
        } else if (ret == PHY_RESYNCH) {
942
          LOG_E( PHY, "[UE %"PRIu8"] Frame %"PRIu32", subframe %u RRC Connection lost, trying to resynch\n",
943
                 UE->Mod_id, UE->frame_rx, UE->slot_tx>>1 );
944 945
          UE->UE_mode[0] = RESYNCH;
        } else if (ret == PHY_HO_PRACH) {
946
          LOG_I( PHY, "[UE %"PRIu8"] Frame %"PRIu32", subframe %u, return to PRACH and perform a contention-free access\n",
947
                 UE->Mod_id, UE->frame_rx, UE->slot_tx>>1 );
948 949
          UE->UE_mode[0] = PRACH;
        }
950
      }
951 952 953 954

#endif
      UE->slot_rx++;

955 956
      if (UE->slot_rx == 20) {
        UE->slot_rx = 0;
957
        UE->frame_rx++;
958
        VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_FRAME_NUMBER_RX_UE, UE->frame_rx );
959 960
      }

961
      VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_SUBFRAME_NUMBER_RX_UE, UE->slot_rx>>1 );
962
    }
963 964
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_UE_THREAD_RX, 0 );

965
    if (pthread_mutex_lock(&UE->mutex_rx) != 0) {
966 967 968
      LOG_E( PHY, "[SCHED][UE] error locking mutex for UE RX\n" );
      exit_fun("noting to add");
      return &UE_thread_rx_retval;
969 970
    }

971
    UE->instance_cnt_rx--;
972
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_UE_INST_CNT_RX, UE->instance_cnt_rx);
973 974 975 976 977 978

    if (pthread_mutex_unlock(&UE->mutex_rx) != 0) {
      LOG_E( PHY, "[SCHED][UE] error unlocking mutex for UE RX\n" );
      exit_fun("noting to add");
      return &UE_thread_rx_retval;
    }
gauthier's avatar
gauthier committed
979 980 981
  }

  // thread finished
982
  return &UE_thread_rx_retval;
983
}
984

985 986 987 988 989 990 991 992



#ifndef EXMIMO
#define RX_OFF_MAX 10
#define RX_OFF_MIN 5
#define RX_OFF_MID ((RX_OFF_MAX+RX_OFF_MIN)/2)

993 994 995 996 997 998 999 1000 1001
/*!
 * \brief This is the main UE thread.
 * This thread controls the other three UE threads:
 * - UE_thread_rx
 * - UE_thread_tx
 * - UE_thread_synch
 * \param arg unused
 * \returns a pointer to an int. The storage is not on the heap and must not be freed.
 */
1002 1003
void *UE_thread(void *arg)
{
1004 1005 1006
  UNUSED(arg)
  static int UE_thread_retval;
  PHY_VARS_UE *UE = PHY_vars_UE_g[0][0];
1007
  int spp = openair0_cfg[0].samples_per_packet;
1008
  int slot=1, frame=0, hw_subframe=0, rxpos=0, txpos=spp*openair0_cfg[0].tx_delay;
1009 1010 1011 1012 1013
#ifdef __AVX2__
  int dummy[2][spp] __attribute__((aligned(32)));
#else
  int dummy[2][spp] __attribute__((aligned(16)));
#endif
1014
  int dummy_dump = 0;
1015 1016
  int tx_enabled = 0;
  int start_rx_stream = 0;
1017 1018
  int rx_off_diff = 0;
  int rx_correction_timer = 0;
1019
  int first_rx = 0;
1020
  RTIME T0;
1021
  unsigned int rxs;
1022 1023 1024 1025

  openair0_timestamp timestamp;

#ifdef RTAI
1026
  RT_TASK *task = rt_task_init_schmod(nam2num("UE thread"), 0, 0, 0, SCHED_FIFO, 0xF);
1027

1028 1029 1030 1031
  if (task==NULL) {
    LOG_E(PHY,"[SCHED][UE] Problem starting UE thread!!!!\n");
    return 0;
  }
1032

1033
#else
1034

1035
#ifdef LOWLATENCY
1036 1037 1038
  struct sched_attr attr;
  unsigned int flags = 0;

1039 1040 1041
  attr.size = sizeof(attr);
  attr.sched_flags = 0;
  attr.sched_nice = 0;
1042
  attr.sched_priority = 0;//sched_get_priority_max(SCHED_DEADLINE);
1043

1044 1045
  // This creates a .5 ms  reservation
  attr.sched_policy = SCHED_DEADLINE;
1046 1047 1048
  attr.sched_runtime  = 100000;
  attr.sched_deadline = 500000;
  attr.sched_period   = 500000;
1049 1050

  if (sched_setattr(0, &attr, flags) < 0 ) {
1051 1052
    perror("[SCHED] main eNB thread: sched_setattr failed\n");
    exit_fun("Nothing to add");
1053
    return &UE_thread_retval;
1054
  }
1055 1056 1057
  LOG_I(HW,"[SCHED][eNB] eNB main deadline thread %lu started on CPU %d\n",
        (unsigned long)gettid(), sched_getcpu());

1058 1059 1060 1061
#else
  struct sched_param sp;
  sp.sched_priority = sched_get_priority_max(SCHED_FIFO);
  pthread_setschedparam(pthread_self(),SCHED_FIFO,&sp);
1062 1063 1064
#endif
#endif

1065
  // Lock memory from swapping. This is a process wide call (not constraint to this thread).
1066 1067 1068 1069 1070
  mlockall(MCL_CURRENT | MCL_FUTURE);

  printf("waiting for sync (UE_thread)\n");
  pthread_mutex_lock(&sync_mutex);
  printf("Locked sync_mutex, waiting (UE_thread)\n");
1071

1072 1073
  while (sync_var<0)
    pthread_cond_wait(&sync_cond, &sync_mutex);
1074

1075 1076 1077 1078 1079 1080 1081
  pthread_mutex_unlock(&sync_mutex);
  printf("unlocked sync_mutex, waiting (UE_thread)\n");

  printf("starting UE thread\n");

  T0 = rt_get_time_ns();
  first_rx = 1;
1082
  rxpos=0;
1083

1084
  while (!oai_exit) {
1085 1086
    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 );
1087
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_DUMMY_DUMP, dummy_dump );
1088

1089

1090
    while (rxpos < (1+hw_subframe)*UE->lte_frame_parms.samples_per_tti) {
1091
      VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_TRX_READ, 1 );
1092

1093 1094
#ifndef USRP_DEBUG

1095 1096
      DevAssert( UE->lte_frame_parms.nb_antennas_rx <= 2 );
      void* rxp[2];
1097

1098
      for (int i=0; i<UE->lte_frame_parms.nb_antennas_rx; i++)
1099
        rxp[i] = (dummy_dump==0) ? (void*)&rxdata[i][rxpos] : (void*)dummy[i];
1100 1101
      /*
      if (dummy_dump == 0)
1102 1103
      	printf("writing %d samples to %d (first_rx %d)\n",spp - ((first_rx==1) ? rx_off_diff : 0),rxpos,first_rx);
      */
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
      if (UE->mode != loop_through_memory) {
	rxs = openair0.trx_read_func(&openair0,
				     &timestamp,
				     rxp,
				     spp - ((first_rx==1) ? rx_off_diff : 0),
				     UE->lte_frame_parms.nb_antennas_rx);

	if (rxs != (spp- ((first_rx==1) ? rx_off_diff : 0))) {
	  exit_fun("problem in rx");
	  return &UE_thread_retval;
	}
1115
      }
1116

1117
      if (rx_off_diff !=0)
1118
	LOG_D(PHY,"frame %d, rx_offset %d, rx_off_diff %d\n",UE->frame_rx,UE->rx_offset,rx_off_diff);
1119

1120
      VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_TRX_READ, 0 );
1121

1122
      // Transmit TX buffer based on timestamp from RX
1123
      if ((tx_enabled==1) && (UE->mode!=loop_through_memory)) {
1124
        VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_TRX_WRITE, 1 );
1125

1126 1127
        DevAssert( UE->lte_frame_parms.nb_antennas_tx <= 2 );
        void* txp[2];
1128

1129
        for (int i=0; i<UE->lte_frame_parms.nb_antennas_tx; i++)
1130 1131 1132
          txp[i] = (void*)&txdata[i][txpos];

        openair0.trx_write_func(&openair0,
1133
                                (timestamp+spp*openair0_cfg[0].tx_delay-openair0_cfg[0].tx_forward_nsamps),
1134
                                txp,
1135
				spp - ((first_rx==1) ? rx_off_diff : 0),
1136 1137 1138
                                UE->lte_frame_parms.nb_antennas_tx,
                                1);

1139
        VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_TRX_WRITE, 0 );
1140
      }
1141 1142
      else if (UE->mode == loop_through_memory)
	rt_sleep_ns(1000000);
1143
#else
1144
      // define USRP_DEBUG is active
1145 1146
      rt_sleep_ns(1000000);
#endif
1147

1148 1149 1150
      rx_off_diff = 0;
      first_rx = 0;

1151 1152 1153 1154
      rxpos += spp;
      txpos += spp;

      if (txpos >= 10*PHY_vars_UE_g[0][0]->lte_frame_parms.samples_per_tti)
1155
        txpos -= 10*PHY_vars_UE_g[0][0]->lte_frame_parms.samples_per_tti;
1156
    }
1157

1158
    if (rxpos >= 10*PHY_vars_UE_g[0][0]->lte_frame_parms.samples_per_tti)
1159 1160
      rxpos -= 10*PHY_vars_UE_g[0][0]->lte_frame_parms.samples_per_tti;

1161 1162
    if (UE->is_synchronized == 1)  {
      LOG_D( HW, "UE_thread: hw_frame %d, hw_subframe %d (time %lli)\n", frame, hw_subframe, rt_get_time_ns()-T0 );
1163

1164
      if (start_rx_stream == 1) {
1165
	LOG_D(PHY,"Locking mutex_rx (IC %d)\n",UE->instance_cnt_rx);
1166
        if (pthread_mutex_lock(&UE->mutex_rx) != 0) {
1167
          LOG_E( PHY, "[SCHED][UE] error locking mutex for UE RX thread\n" );
1168
          exit_fun("nothing to add");
1169 1170
          return &UE_thread_retval;
        }
1171

1172
        int instance_cnt_rx = ++UE->instance_cnt_rx;
1173

1174
	LOG_D(PHY,"Unlocking mutex_rx (IC %d)\n",instance_cnt_rx);
1175 1176 1177 1178 1179
        if (pthread_mutex_unlock(&UE->mutex_rx) != 0) {
          LOG_E( PHY, "[SCHED][UE] error unlocking mutex for UE RX thread\n" );
          exit_fun("nothing to add");
          return &UE_thread_retval;
        }
1180

1181 1182 1183
	VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_UE_INST_CNT_RX, instance_cnt_rx);


1184
        if (instance_cnt_rx == 0) {
1185
	  LOG_D(HW,"signalling rx thread to wake up, hw_frame %d, hw_subframe %d (time %lli)\n", frame, hw_subframe, rt_get_time_ns()-T0 );
1186 1187 1188 1189 1190
          if (pthread_cond_signal(&UE->cond_rx) != 0) {
            LOG_E( PHY, "[SCHED][UE] ERROR pthread_cond_signal for UE RX thread\n" );
            exit_fun("nothing to add");
            return &UE_thread_retval;
          }
1191
	  
1192
	  LOG_D(HW,"signalled rx thread to wake up, hw_frame %d, hw_subframe %d (time %lli)\n", frame, hw_subframe, rt_get_time_ns()-T0 );
1193
	  if (UE->mode == loop_through_memory) {
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
	     if (pthread_mutex_lock(&UE->mutex_rx) != 0) {
          	LOG_E( PHY, "[SCHED][UE] error locking mutex for UE RX thread\n" );
          	exit_fun("nothing to add");
          	return &UE_thread_retval;
             }
	    printf(" Processing subframe %d",UE->slot_rx>>1);
            if( input_file){
		if( (UE->slot_rx>>1) == 9 ){
			FILE * in_fd = fopen(input_file,"r");
      			AssertFatal(in_fd != NULL,"Please provide an input file\n");
			if(in_fd){
				fseek(in_fd,0,SEEK_END);
				int numbytes = ftell(in_fd);
				fseek(in_fd,0,SEEK_SET);
				printf("input_file:%s, numbytes(%d)\n",input_file,numbytes);
				if( input_buffer_l + sizeof(int32_t)* UE->lte_frame_parms.samples_per_tti*10 > numbytes )
					input_buffer_l = 0;
      	    			LOG_W(PHY,"Reading in from file to antenna buffer, buffer_size %d ,dessired position %d\n", UE->lte_frame_parms.samples_per_tti*10*sizeof(int32_t),input_buffer_l);
				fseek(in_fd,input_buffer_l,0);
				fread(UE->lte_ue_common_vars.rxdata[0],
            			sizeof(int32_t),
            			UE->lte_frame_parms.samples_per_tti*10,
            			in_fd);
				input_buffer_l+=ftell(in_fd);	
				fclose(in_fd);
				LOG_W(PHY,"Buffer left %d\n",numbytes - input_buffer_l);
			}
		}
	    }
	    LOG_D(PHY,"Unlocking mutex_rx (IC %d)\n",instance_cnt_rx);
            if (pthread_mutex_unlock(&UE->mutex_rx) != 0) {
                LOG_E( PHY, "[SCHED][UE] error unlocking mutex for UE RX thread\n" );
             	exit_fun("nothing to add");
           	return &UE_thread_retval;
            }
1229
	    getchar();
1230
	 }
1231

1232 1233 1234
          if (UE->mode == rx_calib_ue) {
            if (frame == 10) {
              LOG_D(PHY,
1235
                    "[SCHED][UE] Found cell with N_RB_DL %"PRIu8", PHICH CONFIG (%d,%d), Nid_cell %"PRIu16", NB_ANTENNAS_TX %"PRIu8", frequency offset "PRIi32" Hz, RSSI (digital) %hu dB, measured Gain %d dB, total_rx_gain %"PRIu32" dB, USRP rx gain %f dB\n",
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
                    UE->lte_frame_parms.N_RB_DL,
                    UE->lte_frame_parms.phich_config_common.phich_duration,
                    UE->lte_frame_parms.phich_config_common.phich_resource,
                    UE->lte_frame_parms.Nid_cell,
                    UE->lte_frame_parms.nb_antennas_tx_eNB,
                    UE->lte_ue_common_vars.freq_offset,
                    UE->PHY_measurements.rx_power_avg_dB[0],
                    UE->PHY_measurements.rx_power_avg_dB[0] - rx_input_level_dBm,
                    UE->rx_total_gain_dB,
                    openair0_cfg[0].rx_gain[0]
1246
                   );
1247 1248
              exit_fun("[HW][UE] UE in RX calibration mode, exiting");
              return &UE_thread_retval;
1249 1250
            }
          }
1251
        } else {
1252 1253 1254 1255 1256
          LOG_E( PHY, "[SCHED][UE] UE RX thread busy (IC %d)!!\n", instance_cnt_rx);
	  if (instance_cnt_rx > 1) {
	    exit_fun("instance_cnt_rx > 1");
	    return &UE_thread_retval;
	  }
1257 1258
        }

1259 1260
       
        if ((tx_enabled==1)&&(UE->mode != loop_through_memory)) {
1261 1262 1263 1264 1265 1266 1267

	  if (pthread_mutex_lock(&UE->mutex_tx) != 0) {
	    LOG_E( PHY, "[SCHED][UE] error locking mutex for UE TX thread\n" );
	    exit_fun("nothing to add");
	    return &UE_thread_retval;
	  }

1268

1269 1270 1271 1272 1273 1274 1275
          int instance_cnt_tx = ++UE->instance_cnt_tx;

          if (pthread_mutex_unlock(&UE->mutex_tx) != 0) {
            LOG_E( PHY, "[SCHED][UE] error unlocking mutex for UE TX thread\n" );
            exit_fun("nothing to add");
            return &UE_thread_retval;
          }
1276 1277
	  VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME(VCD_SIGNAL_DUMPER_VARIABLES_UE_INST_CNT_TX, instance_cnt_tx);

1278 1279 1280 1281

          if (instance_cnt_tx == 0) {
            if (pthread_cond_signal(&UE->cond_tx) != 0) {
              LOG_E( PHY, "[SCHED][UE] ERROR pthread_cond_signal for UE TX thread\n" );