lte-ue.c 63.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
 * the OAI Public License, Version 1.0  (the "License"); you may not use this file
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

/*! \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>
48
#include <sys/sysinfo.h>
49 50 51 52 53 54

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

#include "PHY/defs.h"
knopp's avatar
 
knopp committed
55
#ifdef OPENAIR2
56 57
#include "LAYER2/MAC/defs.h"
#include "RRC/LITE/extern.h"
knopp's avatar
 
knopp committed
58
#endif
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
#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

#include "../../ARCH/COMMON/common_lib.h"

#include "PHY/extern.h"
#include "SCHED/extern.h"
#include "LAYER2/MAC/extern.h"
#include "LAYER2/MAC/proto.h"

#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"

Bilel's avatar
Bilel committed
78 79
#include "T.h"

80 81 82 83 84 85 86 87 88
#define FRAME_PERIOD    100000000ULL
#define DAQ_PERIOD      66667ULL

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

knopp's avatar
knopp committed
89
void init_UE_threads(int nb_inst);
90
void *UE_thread(void *arg);
91
void init_UE(int nb_inst,int,int);
92 93 94 95 96

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

97

98 99
extern openair0_config_t openair0_cfg[MAX_CARDS];
extern uint32_t          downlink_frequency[MAX_NUM_CCs][4];
100
extern int32_t           uplink_frequency_offset[MAX_NUM_CCs][4];
101 102
extern int oai_exit;

103 104
int32_t **rxdata;
int32_t **txdata;
105

106 107
//extern unsigned int tx_forward_nsamps;
//extern int tx_delay;
108 109 110 111 112

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

113 114
extern int oaisim_flag;

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
extern void exit_fun(const char* s);

#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;

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
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},
153
  {22, 3510    * MHz, 3590    * MHz, 3410    * MHz, 3490    * MHz, FDD},
154 155 156 157 158 159 160 161 162 163 164 165 166 167
  {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},
};

168 169


170 171 172 173
pthread_t                       main_ue_thread;
pthread_attr_t                  attr_UE_thread;
struct sched_param              sched_param_UE_thread;

knopp's avatar
knopp committed
174
void phy_init_lte_ue_transport(PHY_VARS_UE *ue,int absraction_flag);
175

knopp's avatar
knopp committed
176 177 178
PHY_VARS_UE* init_ue_vars(LTE_DL_FRAME_PARMS *frame_parms,
			  uint8_t UE_id,
			  uint8_t abstraction_flag)
179 180 181 182

{

  int i,j;
knopp's avatar
knopp committed
183 184 185 186 187 188 189 190 191
  PHY_VARS_UE* ue;

  if (frame_parms!=(PHY_VARS_UE *)NULL) { // if we want to give initial frame parms, allocate the PHY_VARS_UE structure and put them in
    ue = (PHY_VARS_UE *)malloc(sizeof(PHY_VARS_UE));
    memset(ue,0,sizeof(PHY_VARS_UE));
    memcpy(&(ue->frame_parms), frame_parms, sizeof(LTE_DL_FRAME_PARMS));
  }					
  else ue = PHY_vars_UE_g[UE_id][0];

192 193 194

  ue->Mod_id      = UE_id;
  ue->mac_enabled = 1;
knopp's avatar
knopp committed
195 196 197 198 199 200
  // initialize all signal buffers
  init_lte_ue_signal(ue,1,abstraction_flag);
  // intialize transport
  init_lte_ue_transport(ue,abstraction_flag);

  return(ue);
201 202
}

knopp's avatar
knopp committed
203

204 205 206
char uecap_xer[1024];

void init_UE(int nb_inst,int eMBMS_active, int uecap_xer_in) {
207 208

  int error_code;
knopp's avatar
knopp committed
209 210
  int inst;
  PHY_VARS_UE *UE;
knopp's avatar
knopp committed
211
  int ret;
knopp's avatar
knopp committed
212

213 214 215 216 217 218 219 220
  LOG_I(PHY,"UE %d: Calling Layer 2 for initialization\n",inst);
    
  if (mac_xface==NULL) mac_xface = malloc(sizeof(MAC_xface));  
  l2_init_ue(eMBMS_active,(uecap_xer_in==1)?uecap_xer:NULL,
	     0,// cba_group_active
	     0); // HO flag
  mac_xface->macphy_exit = &exit_fun;

knopp's avatar
knopp committed
221
  for (inst=0;inst<nb_inst;inst++) {
222

knopp's avatar
knopp committed
223 224 225 226
    LOG_I(PHY,"Initializing memory for UE instance %d\n",inst);
    PHY_vars_UE_g[inst][0] = init_ue_vars(NULL,inst,0);

    LOG_I(PHY,"Intializing UE Threads for instance %d ...\n",inst);
knopp's avatar
knopp committed
227 228
    init_UE_threads(inst);
    UE = PHY_vars_UE_g[inst][0];
knopp's avatar
knopp committed
229

230 231 232 233 234
    if (oaisim_flag == 0) {
      ret = openair0_device_load(&(UE->rfdevice), &openair0_cfg[0]);
      if (ret !=0){
	exit_fun("Error loading device library");
      }
Rohit Gupta's avatar
Rohit Gupta committed
235
    }
236
    UE->rfdevice.host_type = RAU_HOST;
knopp's avatar
knopp committed
237
    //    UE->rfdevice.type      = NONE_DEV;
knopp's avatar
knopp committed
238 239 240 241 242 243 244 245 246
    error_code = pthread_create(&UE->proc.pthread_ue, &UE->proc.attr_ue, UE_thread, NULL);
    
    if (error_code!= 0) {
      LOG_D(HW,"[lte-softmodem.c] Could not allocate UE_thread, error %d\n",error_code);
      return;
    } else {
      LOG_D(HW, "[lte-softmodem.c] Allocate UE_thread successful\n" );
      pthread_setname_np( UE->proc.pthread_ue, "main UE" );
    }
247 248 249 250 251 252 253 254 255 256 257 258 259
  }

  printf("UE threads created\n");
#ifdef USE_MME
  
  while (start_UE == 0) {
    sleep(1);
  }
  
#endif
  
}

260 261
/*!
 * \brief This is the UE synchronize thread.
262
 * It performs band scanning and synchonization.
263 264 265
 * \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.
 */
266 267
static void *UE_thread_synch(void *arg)
{
268 269 270
  static int UE_thread_synch_retval;
  int i, hw_slot_offset;
  PHY_VARS_UE *UE = (PHY_VARS_UE*) arg;
271 272
  int current_band = 0;
  int current_offset = 0;
273
  sync_mode_t sync_mode = pbch;
274
  int CC_id = UE->CC_id;
275 276
  int ind;
  int found;
277
  int freq_offset=0;
278 279 280 281 282

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

283 284 285 286 287 288 289 290 291 292
  printf("waiting for sync (UE_thread)\n");
  pthread_mutex_lock(&sync_mutex);
  printf("Locked sync_mutex, waiting (UE_thread)\n");

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

  pthread_mutex_unlock(&sync_mutex);
  printf("unlocked sync_mutex, waiting (UE_thread)\n");

laurent's avatar
laurent committed
293
#ifndef DEADLINE_SCHEDULER
294 295 296 297 298 299 300 301 302
  int policy, s, j;
  struct sched_param sparam;
  char cpu_affinity[1024];
  cpu_set_t cpuset;

  /* Set affinity mask to include CPUs 1 to MAX_CPUS */
  /* CPU 0 is reserved for UHD threads */
  CPU_ZERO(&cpuset);

303
#ifdef CPU_AFFINITY
Rohit Gupta's avatar
Rohit Gupta committed
304 305
  if (get_nprocs() >2)
    {
306 307 308 309 310 311 312 313 314
      for (j = 1; j < get_nprocs(); j++)
	CPU_SET(j, &cpuset);

      s = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
      if (s != 0)
	{
	  perror( "pthread_setaffinity_np");
	  exit_fun("Error setting processor affinity");
	}
Rohit Gupta's avatar
Rohit Gupta committed
315
    }
316
#endif
Rohit Gupta's avatar
Rohit Gupta committed
317

318 319 320 321
  /* Check the actual affinity mask assigned to the thread */

  s = pthread_getaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
  if (s != 0)
322 323 324 325
    {
      perror( "pthread_getaffinity_np");
      exit_fun("Error getting processor affinity ");
    }
326 327
  memset(cpu_affinity, 0 , sizeof(cpu_affinity));
  for (j = 0; j < CPU_SETSIZE; j++)
328 329 330 331 332 333
    if (CPU_ISSET(j, &cpuset))
      {  
	char temp[1024];
	sprintf(temp, " CPU_%d ", j);    
	strcat(cpu_affinity, temp);
      }
334 335 336 337 338 339 340

  memset(&sparam, 0 , sizeof (sparam));
  sparam.sched_priority = sched_get_priority_max(SCHED_FIFO)-1;
  policy = SCHED_FIFO ; 
  
  s = pthread_setschedparam(pthread_self(), policy, &sparam);
  if (s != 0)
341 342 343 344
    {
      perror("pthread_setschedparam : ");
      exit_fun("Error setting thread priority");
    }
345 346
  s = pthread_getschedparam(pthread_self(), &policy, &sparam);
  if (s != 0)
347 348 349
    {
      perror("pthread_getschedparam : ");
      exit_fun("Error getting thread priority");
350

351
    }
352 353

  LOG_I( HW, "[SCHED][UE] Started UE synch thread on CPU %d TID %ld , sched_policy = %s, priority = %d, CPU Affinity = %s \n", (int)sched_getcpu(), gettid(),
354 355 356 357 358
	 (policy == SCHED_FIFO)  ? "SCHED_FIFO" :
	 (policy == SCHED_RR)    ? "SCHED_RR" :
	 (policy == SCHED_OTHER) ? "SCHED_OTHER" :
	 "???",
	 (int) sparam.sched_priority, cpu_affinity);
359 360 361 362

#endif


363

364

365
  printf("starting UE synch thread (IC %d)\n",UE->proc.instance_cnt_synch);
366 367
  ind = 0;
  found = 0;
368 369


370 371
  if (UE->UE_scan == 0) {
    do  {
372
      current_band = eutra_bands[ind].band;
knopp's avatar
 
knopp committed
373
      printf( "Scanning band %d, dl_min %"PRIu32", ul_min %"PRIu32"\n", current_band, eutra_bands[ind].dl_min,eutra_bands[ind].ul_min);
374

375
      if ((eutra_bands[ind].dl_min <= UE->frame_parms.dl_CarrierFreq) && (eutra_bands[ind].dl_max >= UE->frame_parms.dl_CarrierFreq)) {
376 377
	for (i=0; i<4; i++)
	  uplink_frequency_offset[CC_id][i] = eutra_bands[ind].ul_min - eutra_bands[ind].dl_min;
378 379 380 381

        found = 1;
        break;
      }
382

383
      ind++;
384 385
    } while (ind < sizeof(eutra_bands) / sizeof(eutra_bands[0]));
  
386 387
    if (found == 0) {
      exit_fun("Can't find EUTRA band for frequency");
388
      return &UE_thread_synch_retval;
389
    }
390

391 392


393

394 395


396
    LOG_I( PHY, "[SCHED][UE] Check absolute frequency DL %"PRIu32", UL %"PRIu32" (oai_exit %d, rx_num_channels %d)\n", UE->frame_parms.dl_CarrierFreq, UE->frame_parms.ul_CarrierFreq,oai_exit, openair0_cfg[0].rx_num_channels);
397

398
    for (i=0;i<openair0_cfg[UE->rf_map.card].rx_num_channels;i++) {
399 400
      openair0_cfg[UE->rf_map.card].rx_freq[UE->rf_map.chain+i] = UE->frame_parms.dl_CarrierFreq;
      openair0_cfg[UE->rf_map.card].tx_freq[UE->rf_map.chain+i] = UE->frame_parms.ul_CarrierFreq;
401 402 403
      openair0_cfg[UE->rf_map.card].autocal[UE->rf_map.chain+i] = 1;
      if (uplink_frequency_offset[CC_id][i] != 0) // 
	openair0_cfg[UE->rf_map.card].duplex_mode = duplex_mode_FDD;
404
      else //FDD
405
	openair0_cfg[UE->rf_map.card].duplex_mode = duplex_mode_TDD;
406 407
    }

408
    sync_mode = pbch;
409

410
  } else if  (UE->UE_scan == 1) {
411
    current_band=0;
412

413 414 415 416 417 418 419
    for (i=0; i<openair0_cfg[UE->rf_map.card].rx_num_channels; i++) {
      downlink_frequency[UE->rf_map.card][UE->rf_map.chain+i] = bands_to_scan.band_info[CC_id].dl_min;
      uplink_frequency_offset[UE->rf_map.card][UE->rf_map.chain+i] = bands_to_scan.band_info[CC_id].ul_min-bands_to_scan.band_info[CC_id].dl_min;
      
      openair0_cfg[UE->rf_map.card].rx_freq[UE->rf_map.chain+i] = downlink_frequency[CC_id][i];
      openair0_cfg[UE->rf_map.card].tx_freq[UE->rf_map.chain+i] = downlink_frequency[CC_id][i]+uplink_frequency_offset[CC_id][i];
      openair0_cfg[UE->rf_map.card].rx_gain[UE->rf_map.chain+i] = UE->rx_total_gain_dB;
420 421
    }
  }
422

knopp's avatar
knopp committed
423

424 425 426 427 428 429
  pthread_mutex_lock(&sync_mutex);
  printf("Locked sync_mutex, waiting (UE_sync_thread)\n");

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

430 431
  pthread_mutex_unlock(&sync_mutex);
  printf("Started device, unlocked sync_mutex (UE_sync_thread)\n");
432

knopp's avatar
knopp committed
433 434 435 436 437
  if (UE->rfdevice.trx_start_func(&UE->rfdevice) != 0 ) { 
    LOG_E(HW,"Could not start the device\n");
    oai_exit=1;
  }

438
  while (oai_exit==0) {
439

440
    if (pthread_mutex_lock(&UE->proc.mutex_synch) != 0) {
441
      LOG_E( PHY, "[SCHED][UE] error locking mutex for UE initial synch thread\n" );
442
      exit_fun("noting to add");
443 444
      return &UE_thread_synch_retval;
    }
445
    
446

447
    while (UE->proc.instance_cnt_synch < 0) {
448
      // the thread waits here most of the time
449
      pthread_cond_wait( &UE->proc.cond_synch, &UE->proc.mutex_synch );
450
    }
451

452
    if (pthread_mutex_unlock(&UE->proc.mutex_synch) != 0) {
453 454 455 456
      LOG_E( PHY, "[SCHED][eNB] error unlocking mutex for UE Initial Synch thread\n" );
      exit_fun("nothing to add");
      return &UE_thread_synch_retval;
    }
457

458
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_UE_THREAD_SYNCH, 1 );
459 460

    switch (sync_mode) {
461
    case pss:
462 463
      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);
464 465 466 467 468 469 470
      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;
      }

471
      if (current_band==bands_to_scan.nbands) {
472 473
        current_band=0;
        oai_exit=1;
474
      }
475

476 477 478
      for (i=0; i<openair0_cfg[UE->rf_map.card].rx_num_channels; i++) {
	downlink_frequency[UE->rf_map.card][UE->rf_map.chain+i] = bands_to_scan.band_info[current_band].dl_min+current_offset;
	uplink_frequency_offset[UE->rf_map.card][UE->rf_map.chain+i] = bands_to_scan.band_info[current_band].ul_min-bands_to_scan.band_info[0].dl_min + current_offset;
479

480 481 482 483 484
	openair0_cfg[UE->rf_map.card].rx_freq[UE->rf_map.chain+i] = downlink_frequency[CC_id][i];
	openair0_cfg[UE->rf_map.card].tx_freq[UE->rf_map.chain+i] = downlink_frequency[CC_id][i]+uplink_frequency_offset[CC_id][i];
	openair0_cfg[UE->rf_map.card].rx_gain[UE->rf_map.chain+i] = UE->rx_total_gain_dB;
	if (UE->UE_scan_carrier) {
	  openair0_cfg[UE->rf_map.card].autocal[UE->rf_map.chain+i] = 1;
485
	}
486
	
487
      }
488

489 490
      break;
 
491
    case pbch:
492

Rohit Gupta's avatar
Rohit Gupta committed
493
      LOG_I(PHY,"[UE thread Synch] Running Initial Synch (mode %d)\n",UE->mode);
494
      if (initial_sync( UE, UE->mode ) == 0) {
495

496 497


498
        hw_slot_offset = (UE->rx_offset<<1) / UE->frame_parms.samples_per_tti;
499
        LOG_I( HW, "Got synch: hw_slot_offset %d, carrier off %d Hz, rxgain %d (DL %u, UL %u), UE_scan_carrier %d\n",
500 501 502 503 504 505
	       hw_slot_offset,
	       freq_offset,
	       UE->rx_total_gain_dB,
	       UE->frame_parms.dl_CarrierFreq+freq_offset,
	       UE->frame_parms.ul_CarrierFreq+freq_offset,
	       UE->UE_scan_carrier );
506

507 508 509 510
	if (UE->UE_scan_carrier == 1) {

	  UE->UE_scan_carrier = 0;
	  // rerun with new cell parameters and frequency-offset
511 512
	  for (i=0;i<openair0_cfg[UE->rf_map.card].rx_num_channels;i++) {
	    openair0_cfg[UE->rf_map.card].rx_gain[UE->rf_map.chain+i] = UE->rx_total_gain_dB;//-USRP_GAIN_OFFSET;
513 514
	    if (freq_offset >= 0)
	      {
515
	        openair0_cfg[UE->rf_map.card].rx_freq[UE->rf_map.chain+i] += UE->common_vars.freq_offset;
516
	      }
517
	    else
518
	      {
519
	        openair0_cfg[UE->rf_map.card].rx_freq[UE->rf_map.chain+i] -= UE->common_vars.freq_offset;
520
	      }
521
	    openair0_cfg[UE->rf_map.card].tx_freq[UE->rf_map.chain+i] =  openair0_cfg[UE->rf_map.card].rx_freq[UE->rf_map.chain+i]+uplink_frequency_offset[CC_id][i];
522
	    UE->frame_parms.dl_CarrierFreq = openair0_cfg[CC_id].rx_freq[i];
523 524 525
	    freq_offset=0;	    
	  }

526
	  // reconfigure for potentially different bandwidth
527
	  switch(UE->frame_parms.N_RB_DL) {
528
	  case 6:
529 530 531
	    openair0_cfg[UE->rf_map.card].sample_rate =1.92e6;
	    openair0_cfg[UE->rf_map.card].rx_bw          =.96e6;
	    openair0_cfg[UE->rf_map.card].tx_bw          =.96e6;
532
	    //            openair0_cfg[0].rx_gain[0] -= 12;
533 534
	    break;
	  case 25:
535 536 537
	    openair0_cfg[UE->rf_map.card].sample_rate =7.68e6;
	    openair0_cfg[UE->rf_map.card].rx_bw          =2.5e6;
	    openair0_cfg[UE->rf_map.card].tx_bw          =2.5e6;
538
	    //            openair0_cfg[0].rx_gain[0] -= 6;
539 540
	    break;
	  case 50:
541 542 543
	    openair0_cfg[UE->rf_map.card].sample_rate =15.36e6;
	    openair0_cfg[UE->rf_map.card].rx_bw          =5.0e6;
	    openair0_cfg[UE->rf_map.card].tx_bw          =5.0e6;
544
	    //            openair0_cfg[0].rx_gain[0] -= 3;
545 546
	    break;
	  case 100:
547 548 549
	    openair0_cfg[UE->rf_map.card].sample_rate=30.72e6;
	    openair0_cfg[UE->rf_map.card].rx_bw=10.0e6;
	    openair0_cfg[UE->rf_map.card].tx_bw=10.0e6;
550
	    //            openair0_cfg[0].rx_gain[0] -= 0;
551 552
	    break;
	  }
553
	
Rohit Gupta's avatar
Rohit Gupta committed
554
	  UE->rfdevice.trx_set_freq_func(&UE->rfdevice,&openair0_cfg[0],0);
knopp's avatar
knopp committed
555
	  //UE->rfdevice.trx_set_gains_func(&openair0,&openair0_cfg[0]);
556 557
	  UE->rfdevice.trx_stop_func(&UE->rfdevice);	  
	  sleep(1);
558
	  init_frame_parms(&UE->frame_parms,1);
559 560 561 562
	  if (UE->rfdevice.trx_start_func(&UE->rfdevice) != 0 ) { 
	    LOG_E(HW,"Could not start the device\n");
	    oai_exit=1;
	  }
563 564 565
	}
	else {
	  UE->is_synchronized = 1;
566

567 568
	  if( UE->mode == rx_dump_frame ){
	    FILE *fd;
569
	    if ((UE->proc.proc_rxtx[0].frame_rx&1) == 0) {  // this guarantees SIB1 is present 
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
	      if ((fd = fopen("rxsig_frame0.dat","w")) != NULL) {
		fwrite((void*)&UE->common_vars.rxdata[0][0],
		       sizeof(int32_t),
		       10*UE->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;
	    }
	  }
588
	}
589 590 591
      } else {
        // initial sync failed
        // calculate new offset and try again
592 593 594 595 596 597 598 599 600 601
	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" );
602
	    FILE *fd;
603
	    if ((fd = fopen("rxsig_frame0.dat","w"))!=NULL) {
604
	      fwrite((void*)&UE->common_vars.rxdata[0][0],
605
		     sizeof(int32_t),
606
		     10*UE->frame_parms.samples_per_tti,
607 608 609 610 611
		     fd);
	      LOG_I(PHY,"Dummping Frame ... bye bye \n");
	      fclose(fd);
	      exit(0);
	    }
612 613 614 615 616 617 618 619 620
	    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,
621
               UE->rx_total_gain_dB,
622 623
               UE->frame_parms.dl_CarrierFreq+freq_offset,
               UE->frame_parms.ul_CarrierFreq+freq_offset );
624

625
	for (i=0; i<openair0_cfg[UE->rf_map.card].rx_num_channels; i++) {
626 627
	  openair0_cfg[UE->rf_map.card].rx_freq[UE->rf_map.chain+i] = UE->frame_parms.dl_CarrierFreq+freq_offset;
	  openair0_cfg[UE->rf_map.card].tx_freq[UE->rf_map.chain+i] = UE->frame_parms.ul_CarrierFreq+freq_offset;
628 629 630 631 632
	  
	  openair0_cfg[UE->rf_map.card].rx_gain[UE->rf_map.chain+i] = UE->rx_total_gain_dB;//-USRP_GAIN_OFFSET;
	  
	  if (UE->UE_scan_carrier==1) {
	    openair0_cfg[UE->rf_map.card].autocal[UE->rf_map.chain+i] = 1;
633
	  }
634
	}
635

636 637
	UE->rfdevice.trx_set_freq_func(&UE->rfdevice,&openair0_cfg[0],0);
	    
638
      }// initial_sync=0
639

640
      break;
641

642 643 644 645
    case si:
    default:
      break;
    }
646

647

648
    if (pthread_mutex_lock(&UE->proc.mutex_synch) != 0) {
649 650 651 652
      LOG_E( PHY, "[SCHED][UE] error locking mutex for UE synch\n" );
      exit_fun("noting to add");
      return &UE_thread_synch_retval;
    }
653

654
    // indicate readiness
655
    UE->proc.instance_cnt_synch--;
656

657
    if (pthread_mutex_unlock(&UE->proc.mutex_synch) != 0) {
658 659 660
      LOG_E( PHY, "[SCHED][UE] error unlocking mutex for UE synch\n" );
      exit_fun("noting to add");
      return &UE_thread_synch_retval;
661
    }
662

663
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_UE_THREAD_SYNCH, 0 );
664
  }  // while !oai_exit
665

666
  return &UE_thread_synch_retval;
667 668
}

669

670

671
/*!
672
 * \brief This is the UE thread for RX subframe n and TX subframe n+4.
673
 * This thread performs the phy_procedures_UE_RX() on every received slot.
674
 * then, if TX is enabled it performs TX for n+4. 
675 676 677
 * \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.
 */
678

679
static void *UE_thread_rxn_txnp4(void *arg)
680
{
knopp's avatar
knopp committed
681
  static int UE_thread_rxtx_retval;
682
  UE_rxtx_proc_t *proc = (UE_rxtx_proc_t *)arg;
683
  int ret;
684 685
  PHY_VARS_UE *UE=PHY_vars_UE_g[0][proc->CC_id];
  proc->instance_cnt_rxtx=-1;
686

687

laurent's avatar
laurent committed
688
#ifdef DEADLINE_SCHEDULER
689

690 691 692
  struct sched_attr attr;
  unsigned int flags = 0;

693 694 695
  attr.size = sizeof(attr);
  attr.sched_flags = 0;
  attr.sched_nice = 0;
696
  attr.sched_priority = 0;
697

698
  // This creates a .5ms reservation every 1ms period
699 700 701 702
  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
703 704

  if (sched_setattr(0, &attr, flags) < 0 ) {
knopp's avatar
knopp committed
705 706
    perror("[SCHED] UE_thread_rxtx : sched_setattr failed\n");
    return &UE_thread_rxtx_retval;
707 708
  }

709
#else
710 711 712 713 714 715 716 717 718
  int policy, s, j;
  struct sched_param sparam;
  char cpu_affinity[1024];
  cpu_set_t cpuset;

  /* Set affinity mask to include CPUs 1 to MAX_CPUS */
  /* CPU 0 is reserved for UHD threads */
  CPU_ZERO(&cpuset);

719
#ifdef CPU_AFFINITY
Rohit Gupta's avatar
Rohit Gupta committed
720 721
  if (get_nprocs() >2)
    {
722 723 724 725 726 727 728 729 730
      for (j = 1; j < get_nprocs(); j++)
	CPU_SET(j, &cpuset);

      s = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
      if (s != 0)
	{
	  perror( "pthread_setaffinity_np");
	  exit_fun("Error setting processor affinity");
	}
Rohit Gupta's avatar
Rohit Gupta committed
731
    }
732
#endif
Rohit Gupta's avatar
Rohit Gupta committed
733

734 735 736 737
  /* Check the actual affinity mask assigned to the thread */

  s = pthread_getaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
  if (s != 0)
738 739 740 741
    {
      perror( "pthread_getaffinity_np");
      exit_fun("Error getting processor affinity ");
    }
742 743
  memset(cpu_affinity, 0 , sizeof(cpu_affinity));
  for (j = 0; j < CPU_SETSIZE; j++)
744 745 746 747 748 749
    if (CPU_ISSET(j, &cpuset))
      {  
	char temp[1024];
	sprintf(temp, " CPU_%d ", j);    
	strcat(cpu_affinity, temp);
      }
750 751 752 753 754 755 756

  memset(&sparam, 0 , sizeof (sparam));
  sparam.sched_priority = sched_get_priority_max(SCHED_FIFO)-1;
  policy = SCHED_FIFO ; 
  
  s = pthread_setschedparam(pthread_self(), policy, &sparam);
  if (s != 0)
757 758 759 760
    {
      perror("pthread_setschedparam : ");
      exit_fun("Error setting thread priority");
    }
761 762
  s = pthread_getschedparam(pthread_self(), &policy, &sparam);
  if (s != 0)
763 764 765
    {
      perror("pthread_getschedparam : ");
      exit_fun("Error getting thread priority");
766

767
    }
768 769

  LOG_I( HW, "[SCHED][UE] Started UE RX thread on CPU %d TID %ld , sched_policy = %s, priority = %d, CPU Affinity = %s \n", (int)sched_getcpu(), gettid(),
770 771 772 773 774
	 (policy == SCHED_FIFO)  ? "SCHED_FIFO" :
	 (policy == SCHED_RR)    ? "SCHED_RR" :
	 (policy == SCHED_OTHER) ? "SCHED_OTHER" :
	 "???",
	 (int) sparam.sched_priority, cpu_affinity);
775

776

777
#endif
778

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

782
  printf("waiting for sync (UE_thread_rxn_txnp4)\n");
Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
783

784
  pthread_mutex_lock(&sync_mutex);
785
  printf("Locked sync_mutex, waiting (UE_thread_rxn_txnp4)\n");
786

787 788
  while (sync_var<0)
    pthread_cond_wait(&sync_cond, &sync_mutex);
789

790 791 792 793
#define THREAD_NAME_LEN 16
  char threadname[THREAD_NAME_LEN];
  ret = pthread_getname_np(proc->pthread_rxtx, threadname, THREAD_NAME_LEN);
  if (ret != 0)
794 795 796 797
    {
      perror("pthread_getname_np : ");
      exit_fun("Error getting thread name");
    }
798

799
  pthread_mutex_unlock(&sync_mutex);
knopp's avatar
knopp committed
800
  printf("unlocked sync_mutex, waiting (UE_thread_rxtx)\n");
801

802
  printf("Starting UE RXN_TXNP4 thread (%s)\n", threadname);
803 804

  while (!oai_exit) {
Wilson's avatar
Wilson committed
805
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_UE_LOCK_MUTEX_RXTX_FOR_COND_WAIT0+(proc->proc_id), 1 );
806
    if (pthread_mutex_lock(&proc->mutex_rxtx) != 0) {
knopp's avatar
knopp committed
807
      LOG_E( PHY, "[SCHED][UE] error locking mutex for UE RXTX\n" );
808
      exit_fun("nothing to add");
knopp's avatar
knopp committed
809
      return &UE_thread_rxtx_retval;
810
    }
811

812
    while (proc->instance_cnt_rxtx < 0) {
Wilson's avatar
Wilson committed
813
      VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_UE_WAIT_COND_RXTX0+(proc->proc_id), 1 );
814
      // most of the time, the thread is waiting here
815
      pthread_cond_wait( &proc->cond_rxtx, &proc->mutex_rxtx );
816
    }
Wilson's avatar
Wilson committed
817
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_UE_WAIT_COND_RXTX0+(proc->proc_id), 0 );
818

819 820
    if (pthread_mutex_unlock(&proc->mutex_rxtx) != 0) {
      LOG_E( PHY, "[SCHED][UE] error unlocking mutex for UE RXn_TXnp4\n" );
821
      exit_fun("nothing to add");
knopp's avatar
knopp committed
822
      return &UE_thread_rxtx_retval;
823
    }
Wilson's avatar
Wilson committed
824
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_UE_LOCK_MUTEX_RXTX_FOR_COND_WAIT0+(proc->proc_id), 0 );
825

Wilson's avatar
Wilson committed
826 827 828 829 830
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_UE_THREAD_RXTX0+(proc->proc_id), 1 );
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_SUBFRAME_NUMBER_RX0_UE+(proc->proc_id), proc->subframe_rx );
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_SUBFRAME_NUMBER_TX0_UE+(proc->proc_id), proc->subframe_tx );
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_FRAME_NUMBER_RX0_UE+(proc->proc_id), proc->frame_rx );
    VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_FRAME_NUMBER_TX0_UE+(proc->proc_id), proc->frame_tx );
831

832 833 834 835
    lte_subframe_t sf_type = subframe_select( &UE->frame_parms, proc->subframe_rx);
    if ((sf_type == SF_DL) ||
        (UE->frame_parms.frame_type == FDD) ||
        (sf_type == SF_S)) {
836
    
837
      if (UE->frame_parms.frame_type == TDD) {
838 839 840 841 842 843
	LOG_D(PHY, "%s,TDD%d,%s: calling UE_RX\n",
	      threadname,
	      UE->frame_parms.tdd_config,
	      (sf_type==SF_DL? "SF_DL" :
	       (sf_type==SF_UL? "SF_UL" :
		(sf_type==SF_S ? "SF_S"  : "UNKNOWN_SF_TYPE"))));
844 845
      } else {
        LOG_D(PHY, "%s,%s,%s: calling UE_RX\n",
846 847 848 849 850 851
	      threadname,
	      (UE->frame_parms.frame_type==FDD? "FDD":
	       (UE->frame_parms.frame_type==TDD? "TDD":"UNKNOWN_DUPLEX_MODE")),
	      (sf_type==SF_DL? "SF_DL" :
	       (sf_type==SF_UL? "SF_UL" :
		(sf_type==SF_S ? "SF_S"  : "UNKNOWN_SF_TYPE"))));
852
      }
853 854
      phy_procedures_UE_RX( UE, proc, 0, 0, UE->mode, no_relay, NULL );
    }
855
    
856
    if (UE->mac_enabled==1) {
857

858
      ret = mac_xface->ue_scheduler(UE->Mod_id,
859 860 861 862 863 864 865
				    proc->frame_rx,
				    proc->subframe_rx,
				    proc->frame_tx,
				    proc->subframe_tx,
				    subframe_select(&UE->frame_parms,proc->subframe_tx),
				    0,
				    0/*FIXME CC_id*/);
866 867 868 869 870 871 872 873 874 875 876 877 878
      
      if (ret == CONNECTION_LOST) {
	LOG_E( PHY, "[UE %"PRIu8"] Frame %"PRIu32", subframe %u RRC Connection lost, returning to PRACH\n",
	       UE->Mod_id, proc->frame_rx, proc->subframe_tx );
	UE->UE_mode[0] = PRACH;
      } else if (ret == PHY_RESYNCH) {
	LOG_E( PHY, "[UE %"PRIu8"] Frame %"PRIu32", subframe %u RRC Connection lost, trying to resynch\n",
	       UE->Mod_id, proc->frame_rx, proc->subframe_tx );
	UE->UE_mode[0] = RESYNCH;
      } else if (ret == PHY_HO_PRACH) {
	LOG_I( PHY, "[UE %"PRIu8"] Frame %"PRIu32", subframe %u, return to PRACH and perform a contention-free access\n",
	       UE->Mod_id, proc->frame_rx, proc->subframe_tx );
	UE->UE_mode[0] = PRACH;
879 880
      }
    }
881 882

    if ((subframe_select( &UE->frame_parms, proc->subframe_tx) == SF_UL) ||
883
	(UE->frame_parms.frame_type == FDD) ) {
884 885

      if (UE->mode != loop_through_memory) {
Rohit Gupta's avatar
Rohit Gupta committed
886
	phy_procedures_UE_TX(UE,proc,0,0,UE->mode,no_relay);
887 888 889
      }
    }

890
    if ((subframe_select( &UE->frame_parms, proc->subframe_tx) == SF_S) &&
891
	(UE->frame_parms.frame_type == TDD)) {
892 893

      if (UE->mode != loop_through_memory) {
894
	phy_procedures_UE_S_TX(UE,0,0,no_relay);
895 896 897
      }
    }

Wilson's avatar
Wilson committed
898
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_UE_THREAD_RXTX0+(proc->proc_id), 0 );
899

900
    
Wilson's avatar
Wilson committed
901
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME( VCD_SIGNAL_DUMPER_FUNCTIONS_UE_LOCK_MUTEX_RXTX_FOR_CNT_DECREMENT0+