UE_transport_IQ.c 18 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * 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
 */

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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
/*! \file UE_transport_IQ.c
 * \brief UE transport IQ sampels 
 * \author  Katerina Trilyraki, Navid Nikaein, Raymond Knopp
 * \date 2015
 * \version 0.1
 * \company Eurecom
 * \maintainer:  navid.nikaein@eurecom.fr
 * \note
 * \warning very experimental 
 */

#include <unistd.h>
#include <time.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>

#include "common_lib.h"
#include "PHY/defs.h"
#include "rrh_gw.h"
#include "rrh_gw_externs.h"

#define START_CMD 1
#define RRH_UE_PORT 51000
#define RRH_UE_DEST_IP "127.0.0.1"
#define PRINTF_PERIOD 3750

/******************************************************************************
 **                               FUNCTION PROTOTYPES                        **
 ******************************************************************************/
void *rrh_proc_UE_thread(void *);
void *rrh_UE_thread(void *);
void *rrh_UE_rx_thread(void *);
void *rrh_UE_tx_thread(void *);



openair0_timestamp	timestamp_UE_tx[4]= {0,0,0,0},timestamp_UE_rx[4]= {0,0,0,0};
openair0_timestamp 	nrt_UE_counter[4]= {0,0,0,0};

pthread_t		main_rrh_UE_thread,main_rrh_proc_UE_thread;
pthread_attr_t 		attr, attr_proc;
struct sched_param 	sched_param_rrh, sched_param_rrh_proc;
pthread_cond_t 		sync_UE_cond[4];
pthread_mutex_t 	sync_UE_mutex[4];

int32_t 		overflow_rx_buffer_UE[4]= {0,0,0,0};
int32_t 		nsamps_UE[4]= {0,0,0,0};
int32_t 		UE_tx_started=0,UE_rx_started=0;
int32_t 		RT_flag_UE=0, NRT_flag_UE=1;
int32_t 		counter_UE_rx[4]= {0,0,0,0};
int32_t 		counter_UE_tx[4]= {0,0,0,0};
int32_t			**tx_buffer_UE, **rx_buffer_UE;

int 			sync_UE_rx[4]= {-1,-1,-1,-1};
void 			*rrh_UE_thread_status;


void *rx_ue[2]; // FIXME hard coded array size; indexed by lte_frame_parms.nb_antennas_rx
void *tx_ue[2]; // FIXME hard coded array size; indexed by lte_frame_parms.nb_antennas_tx

85 86

void config_UE_mod( rrh_module_t *dev_ue, uint8_t RT_flag,uint8_t NRT_flag) {
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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

  int 	  i;
  int 	  error_code_UE, error_code_proc_UE;
  
  RT_flag_UE=RT_flag;
  NRT_flag_UE=NRT_flag;
  
  pthread_attr_init(&attr);
  sched_param_rrh.sched_priority = sched_get_priority_max(SCHED_FIFO);
  pthread_attr_init(&attr_proc);
  sched_param_rrh_proc.sched_priority = sched_get_priority_max(SCHED_FIFO-1);
  
  pthread_attr_setschedparam(&attr,&sched_param_rrh);
  pthread_attr_setschedpolicy(&attr,SCHED_FIFO);
  pthread_attr_setschedparam(&attr_proc,&sched_param_rrh_proc);
  pthread_attr_setschedpolicy(&attr_proc,SCHED_FIFO-1);
  
  
  for (i=0; i<4; i++) {
    pthread_mutex_init(&sync_UE_mutex[i],NULL);
    pthread_cond_init(&sync_UE_cond[i],NULL);
  }
  
  error_code_UE = pthread_create(&main_rrh_UE_thread, &attr, rrh_UE_thread, (void *)dev_ue);
  error_code_proc_UE = pthread_create(&main_rrh_proc_UE_thread, &attr_proc, rrh_proc_UE_thread, (void *)dev_ue);
  
  if (error_code_UE) {
    printf("Error while creating UE thread\n");
    exit(-1);
  }
  
  if (error_code_proc_UE) {
    printf("Error while creating UE proc thread\n");
    exit(-1);
  }
  
}


/*! \fn void *rrh_proc_UE_thread((void *)dev_ue)
* \brief this function
* \param[in]
* \param[out]
* \return
* \note
* @ingroup  _oai
*/
void *rrh_proc_UE_thread(void * arg) {
  
  int				antenna_index,i;
  openair0_timestamp 		truncated_timestamp, truncated_timestamp_final, last_hw_counter=0;
  struct			timespec time_req, time_rem;
  int16_t       		*txp,*rxp;
  unsigned int                  samples_per_frame=0;
  rrh_module_t 	*dev=(rrh_module_t *)arg;
  
143
  samples_per_frame= dev->eth_dev.openair0_cfg->samples_per_frame;
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
  AssertFatal(samples_per_frame <=0, "invalide samples_per_frame !%u\n",samples_per_frame);

  time_req.tv_sec = 0;
  time_req.tv_nsec = 1000;
  
  while (rrh_exit==0) {
    //wait until some data has been copied
    for (antenna_index=0; antenna_index<4; antenna_index++) {
      if (sync_UE_rx[antenna_index]==0) {
	if (!UE_tx_started) {
	  UE_tx_started=1;  //Set this flag to 1 to indicate that a UE started retrieving data
	  if (RT_flag_UE==1) {
	    last_hw_counter=hw_counter;
	  }
	} else {
	  if (RT_flag_UE==1) {
	    if (hw_counter > last_hw_counter+1) {
	      printf("L1");
	      //              goto end_copy_UE;
	    } else {
	      while (hw_counter < last_hw_counter+1)
		nanosleep(&time_req,&time_rem);
	    }
	  }
	}
	
	truncated_timestamp = timestamp_UE_tx[antenna_index]%(samples_per_frame);
	truncated_timestamp_final =  (timestamp_UE_tx[antenna_index]+nsamps_UE[antenna_index])%samples_per_frame;
	
	if ((truncated_timestamp + nsamps_UE[antenna_index]) > samples_per_frame) {
	  if ((timestamp_eNB_rx[antenna_index]%samples_per_frame < nsamps_UE[antenna_index]) && (eNB_rx_started==1)) {
	    overflow_rx_buffer_eNB[antenna_index]++;
	    printf("eNB Overflow[%d] : %d, timestamp : %d\n",antenna_index,overflow_rx_buffer_eNB[antenna_index],(int)truncated_timestamp);
	    
	    if (NRT_flag_UE==1) {
	      while ((timestamp_eNB_rx[antenna_index]%samples_per_frame) < nsamps_UE[antenna_index])
		nanosleep(&time_req,&time_rem);
	    }
	  }
	  
	  rxp = (int16_t*)&rx_buffer_eNB[antenna_index][truncated_timestamp];
	  txp = (int16_t*)&tx_buffer_UE[antenna_index][truncated_timestamp];
	  
	  for (i=0; i<(samples_per_frame<<1)-(truncated_timestamp<<1); i++) {
	    rxp[i] = txp[i]>>6;
	  }
	  
	  rxp = (int16_t*)&rx_buffer_eNB[antenna_index][0];
	  txp = (int16_t*)&tx_buffer_UE[antenna_index][0];
	  
	  for (i=0; i<nsamps_eNB[antenna_index]-(samples_per_frame)+(truncated_timestamp); i++) {
	    rxp[i] = txp[i]>>6;
	  }
	  
	} else {
	  if (((truncated_timestamp < (timestamp_eNB_rx[antenna_index]%samples_per_frame)) && 
	       (truncated_timestamp_final >  (timestamp_eNB_rx[antenna_index]%samples_per_frame))) && 
	      (eNB_rx_started==1)) {
	    overflow_rx_buffer_eNB[antenna_index]++;
	    printf("eNB Overflow[%d] : %d, timestamp : %d\n",antenna_index,overflow_rx_buffer_eNB[antenna_index],(int)truncated_timestamp);
	    
	    if (NRT_flag_UE==1) {
	      while (truncated_timestamp_final >  timestamp_eNB_rx[antenna_index]%samples_per_frame)
		nanosleep(&time_req,&time_rem);
	    }
	  }
	  
	  rxp = (int16_t*)&rx_buffer_eNB[antenna_index][truncated_timestamp];
	  txp = (int16_t*)&tx_buffer_UE[antenna_index][truncated_timestamp];
	  
	  for (i=0; i<(nsamps_eNB[antenna_index]); i++) {
	    rxp[i] =txp[i]>>6;
	  }
		  
	}
	//end_copy_UE :
	last_hw_counter=hw_counter;
	pthread_mutex_lock(&sync_UE_mutex[antenna_index]);
	sync_UE_rx[antenna_index]--;
	pthread_mutex_unlock(&sync_UE_mutex[antenna_index]);
	
      }
    }

  }
  
  return(0);
}

/*! \fn void *rrh_UE_thread(void *arg)
* \brief this function
* \param[in]
* \param[out]
* \return
* \note
* @ingroup  _oai
*/
void *rrh_UE_thread(void *arg) {
  
  rrh_module_t 	*dev=(rrh_module_t *)arg;

  struct sched_param 	sched_param_UE_rx, sched_param_UE_tx;
  int16_t		i,cmd;   //,nsamps,antenna_index;
Cedric Roux's avatar
Cedric Roux committed
247
  //struct timespec 	time_req_1us;
248 249 250 251 252 253
  pthread_t 		UE_rx_thread, UE_tx_thread;
  pthread_attr_t 	attr_UE_rx, attr_UE_tx;
  int 			error_code_UE_rx, error_code_UE_tx;
  void 			*tmp;
  unsigned int          samples_per_frame=0;
  
254
  samples_per_frame= dev->eth_dev.openair0_cfg->samples_per_frame;
Cedric Roux's avatar
Cedric Roux committed
255 256
  //time_req_1us.tv_sec = 0;
  //time_req_1us.tv_nsec = 1000;
257 258 259
  
  while (rrh_exit==0) {
    
260
    cmd=dev->eth_dev.trx_start_func(&dev->eth_dev);
261 262
    
    /* allocate memory for TX/RX buffers */
263 264
    rx_buffer_UE = (int32_t**)malloc16(dev->eth_dev.openair0_cfg->rx_num_channels*sizeof(int32_t*));
    tx_buffer_UE = (int32_t**)malloc16(dev->eth_dev.openair0_cfg->tx_num_channels*sizeof(int32_t*));
265
    
266
    for (i=0; i<dev->eth_dev.openair0_cfg->rx_num_channels; i++) {
267 268 269 270 271
      tmp=(void *)malloc(sizeof(int32_t)*(samples_per_frame+4));
      memset(tmp,0,sizeof(int32_t)*(samples_per_frame+4));
      rx_buffer_UE[i]=(tmp+4*sizeof(int32_t));  
    }
    
272
    for (i=0; i<dev->eth_dev.openair0_cfg->tx_num_channels; i++) {
273 274 275 276 277
      tmp=(void *)malloc(sizeof(int32_t)*(samples_per_frame+4));
      memset(tmp,0,sizeof(int32_t)*(samples_per_frame+4));
      tx_buffer_UE[i]=(tmp+4*sizeof(int32_t));  
    }
    
278 279 280 281 282
    printf("Client %s:%d is connected (DL_RB=%d) rt=%d|%d. \n" ,   dev->eth_dev.openair0_cfg->remote_addr,
	   dev->eth_dev.openair0_cfg->remote_port,
	   dev->eth_dev.openair0_cfg->num_rb_dl,
	   dev->eth_dev.openair0_cfg->rx_num_channels,
	   dev->eth_dev.openair0_cfg->tx_num_channels);
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
    
    if (cmd==START_CMD) {
      
      pthread_attr_init(&attr_UE_rx);
      pthread_attr_init(&attr_UE_tx);
      sched_param_UE_rx.sched_priority = sched_get_priority_max(SCHED_FIFO);
      sched_param_UE_tx.sched_priority = sched_get_priority_max(SCHED_FIFO);
      pthread_attr_setschedparam(&attr_UE_rx,&sched_param_UE_rx);
      pthread_attr_setschedparam(&attr_UE_tx,&sched_param_UE_tx);
      pthread_attr_setschedpolicy(&attr_UE_rx,SCHED_FIFO);
      pthread_attr_setschedpolicy(&attr_UE_tx,SCHED_FIFO);
      
      error_code_UE_rx = pthread_create(&UE_rx_thread, &attr_UE_rx, rrh_UE_rx_thread, (void *)&dev);
      error_code_UE_tx = pthread_create(&UE_tx_thread, &attr_UE_tx, rrh_UE_tx_thread, (void *)&dev);
      
      if (error_code_UE_rx) {
	printf("Error while creating UE RX thread\n");
	exit(-1);
      }

      if (error_code_UE_tx) {
	printf("Error while creating UE TX thread\n");
	exit(-1);
      }
      
      while (rrh_exit==0)
	sleep(1);
    }
  }
  
  
  rrh_UE_thread_status = 0;
  pthread_exit(&rrh_UE_thread_status);
  
  return(0);
}


/*! \fn void *rrh_UE_rx_thread(void *arg)
* \brief this function
* \param[in]
* \param[out]
* \return
* \note
* @ingroup  _oai
*/
void *rrh_UE_rx_thread(void *arg) {

  rrh_module_t        *dev = (rrh_module_t *)arg;
  struct 	      timespec time0,time1,time2;
  struct 	      timespec time_req_1us, time_rem_1us;
  ssize_t	      bytes_sent;
  int 		      antenna_index, nsamps;
  int 		      trace_cnt=0;
  unsigned long long  max_rx_time=0, min_rx_time=133333, total_rx_time=0, average_rx_time=133333, s_period=0, trial=0;
  unsigned int        samples_per_frame=0;
Cedric Roux's avatar
Cedric Roux committed
339
  openair0_timestamp  last_hw_counter=0;
340 341
  
  antenna_index     = 0;
342 343
  nsamps	    = dev->eth_dev.openair0_cfg->samples_per_packet;
  samples_per_frame = dev->eth_dev.openair0_cfg->samples_per_frame;
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
  
  while (rrh_exit == 0) {
    if (!UE_rx_started) {
      UE_rx_started=1;  //Set this flag to 1 to indicate that a UE started retrieving data
      
      if (RT_flag_UE==1) {
	last_hw_counter=hw_counter;
      }
    } else {
      if (RT_flag_UE==1) {
	if (hw_counter > last_hw_counter+1) {
	  printf("L1");
	  //goto end_copy_UE;
	} else {
	  while (hw_counter < last_hw_counter+1)
	    nanosleep(&time_req_1us,&time_rem_1us);
	}
      }
    }
    
    clock_gettime(CLOCK_MONOTONIC,&time1);
    
    // send return
    
    if ((timestamp_UE_rx[antenna_index]%(samples_per_frame)+nsamps) > samples_per_frame) { // Wrap around if nsamps exceeds the buffer limit
      if (((timestamp_eNB_tx[antenna_index]%(samples_per_frame)) < ((timestamp_UE_rx[antenna_index]+nsamps)%(samples_per_frame))) && (eNB_tx_started==1)) {
	printf("UE underflow wraparound timestamp_UE_rx : %d, timestamp_eNB_tx : %d\n",
	       (int)(timestamp_UE_rx[antenna_index]%(samples_per_frame)),
	       (int)(timestamp_eNB_tx[antenna_index]%samples_per_frame));
	
	if (NRT_flag_UE==1) {
	  while ((timestamp_eNB_tx[antenna_index]%samples_per_frame) < ((timestamp_UE_rx[antenna_index]+nsamps)%(samples_per_frame)))
	    nanosleep(&time_req_1us,&time_rem_1us);
	}
      }
      
      rx_ue[antenna_index]=(void*)&rx_buffer_UE[antenna_index][timestamp_UE_rx[antenna_index]%(samples_per_frame)];
      if ((bytes_sent =dev->eth_dev.trx_write_func (dev,
						    timestamp_UE_rx[antenna_index],
						    rx_ue,
						    ( samples_per_frame- (timestamp_eNB_rx[antenna_index]%(samples_per_frame)) ),
					antenna_index,
						    0))<0)
	perror("RRH UE : sendto for RX");
      
      rx_ue[antenna_index]=(void*)&rx_buffer_UE[antenna_index][3];
      if ((bytes_sent  =dev->eth_dev.trx_write_func (dev,
						     timestamp_UE_rx[antenna_index],
						     rx_ue,
						     (nsamps - samples_per_frame + (timestamp_eNB_rx[antenna_index]%(samples_per_frame))),
						     antenna_index,
						     0))<0)
	perror("RRH UE : sendto for RX");
      
    } else {
      if (((timestamp_UE_rx[antenna_index]%samples_per_frame)< timestamp_eNB_tx[antenna_index]%samples_per_frame) && 
	  (((timestamp_UE_rx[antenna_index]+nsamps)%samples_per_frame) > (timestamp_eNB_tx[antenna_index]%samples_per_frame)) && 
	  (eNB_tx_started==1) ) {
	printf("UE underflow timestamp_UE_rx : %d, timestamp_eNB_tx : %d\n",
	       (int)(timestamp_UE_rx[antenna_index]%samples_per_frame),
	       (int)(timestamp_eNB_tx[antenna_index]%samples_per_frame));
	
	if (NRT_flag_UE==1) {
	  while (((timestamp_UE_rx[antenna_index]+nsamps)%samples_per_frame) > (timestamp_eNB_tx[antenna_index]%samples_per_frame)) {
	    nanosleep(&time_req_1us,&time_rem_1us);
	  }
	}
      }
      
      rx_ue[antenna_index]=(void*)&rx_buffer_UE[antenna_index][timestamp_UE_rx[antenna_index]%(samples_per_frame)];
      if ((bytes_sent = dev->eth_dev.trx_write_func (dev,
						     timestamp_UE_rx[antenna_index],
						     rx_ue,
						     nsamps,
						     antenna_index,
						     0))<0)
	perror("RRH UE thread: sendto for RX");
    }
    
    timestamp_UE_rx[antenna_index]+=nsamps;
    last_hw_counter=hw_counter;
    
    clock_gettime(CLOCK_MONOTONIC,&time2);
    
    if (trace_cnt++ > 10) {
      total_rx_time = (unsigned int)(time2.tv_nsec - time0.tv_nsec);
      
      if (total_rx_time < 0) total_rx_time=1000000000-total_rx_time;
      
      if ((total_rx_time > 0) && (total_rx_time < 1000000000)) {
	trial++;
	
	if (total_rx_time < min_rx_time)
	  min_rx_time = total_rx_time;
	
	if (total_rx_time > max_rx_time)
	  max_rx_time = total_rx_time;
	
	average_rx_time = (long long unsigned int)((average_rx_time*trial)+total_rx_time)/(trial+1);
      }
      
      if (s_period++ == PRINTF_PERIOD) {
	s_period=0;
Cedric Roux's avatar
Cedric Roux committed
447
	printf("Average UE RX time : %llu\tMax RX time : %llu\tMin RX time : %llu\n",average_rx_time,max_rx_time,min_rx_time);
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
	
      }
      
      //printf("RX: t1 %llu (time from last send), t2 %llu (sendto of %lu bytes) total time : %llu\n",(long long unsigned int)(time1.tv_nsec  - time0.tv_nsec), (long long unsigned int)(time2.tv_nsec - time1.tv_nsec),
      //   (nsamps<<2)+sizeof(openair0_timestamp),(long long unsigned int)(time2.tv_nsec - time0.tv_nsec));
      
    }
    
    memcpy(&time0,&time2,sizeof(struct timespec));
    
  }  //while (UE_exit==0)
  
  return(0);
  
}


/*! \fn void *rrh_UE_tx_thread(void *arg)
* \brief this function
* \param[in]
* \param[out]
* \return
* \note
* @ingroup  _oai
*/
void *rrh_UE_tx_thread(void *arg) {
  
  struct timespec 	time0a,time0,time1,time2;
  struct timespec 	time_req_1us, time_rem_1us;
  
  
  rrh_module_t    	*dev = (rrh_module_t *)arg;
  ssize_t 		bytes_received;
  int 			antenna_index, nsamps;
  unsigned int          samples_per_frame=0;
  
  antenna_index    = 0;
485 486
  nsamps 	   = dev->eth_dev.openair0_cfg->samples_per_packet;
  samples_per_frame = dev->eth_dev.openair0_cfg->samples_per_frame;
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
  
  
  while (rrh_exit == 0) {
    
    clock_gettime(CLOCK_MONOTONIC,&time0a);
    
    bytes_received = dev->eth_dev.trx_read_func(dev,
						&timestamp_UE_tx[antenna_index],
						tx_ue,
						nsamps,
						antenna_index);
    
    clock_gettime(CLOCK_MONOTONIC,&time1);
    
    if (NRT_flag_UE==1) {
      nrt_UE_counter[antenna_index]++;
    }
Cedric Roux's avatar
Cedric Roux committed
504
    printf(" first part size: %d   second part size: %"PRId64" idx=%"PRId64"\n",
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
	   (int32_t)(((samples_per_frame)<<2)-((timestamp_UE_tx[antenna_index]%(samples_per_frame))<<2)),
	   (nsamps<<2)-((samples_per_frame)<<2)+((timestamp_UE_tx[antenna_index]%(samples_per_frame))<<2),
	   timestamp_UE_tx[antenna_index]%(samples_per_frame));
    if ((timestamp_UE_tx[antenna_index]%(samples_per_frame)+nsamps) > samples_per_frame) { // Wrap around if nsamps exceeds the buffer limit
      memcpy(&tx_buffer_UE[antenna_index][timestamp_UE_tx[antenna_index]%(samples_per_frame)],(void*)(tx_ue[antenna_index]),
	     (samples_per_frame<<2)-((timestamp_UE_tx[antenna_index]%(samples_per_frame))<<2));
      
      memcpy(&tx_buffer_UE[antenna_index][0], (void*)(tx_ue[antenna_index]+(samples_per_frame*4)-((timestamp_UE_tx[antenna_index]%(samples_per_frame))<<2)),
             (nsamps<<2)-((samples_per_frame-(timestamp_UE_tx[antenna_index]%(samples_per_frame)))<<2));
      //printf("Received UE TX samples for antenna %d, nsamps %d (%d)\n",antenna_index,nsamps,(int)(bytes_received>>2));
    } else {
      memcpy(&tx_buffer_UE[antenna_index][timestamp_UE_tx[antenna_index]%(samples_per_frame)], (void*)(tx_ue[antenna_index]),(nsamps<<2));
    }
    
    while (sync_UE_rx[antenna_index]==0)
      nanosleep(&time_req_1us,&time_rem_1us);
    
    pthread_mutex_lock(&sync_UE_mutex[antenna_index]);
    sync_UE_rx[antenna_index]++;
    
    if (!sync_UE_rx[antenna_index]) {
      counter_UE_tx[antenna_index]=(counter_UE_tx[antenna_index]+nsamps)%samples_per_frame;
      nsamps_UE[antenna_index]=nsamps;
    } else {
      printf("rrh_eNB_proc thread is busy, will exit\n");
      exit(-1);
    }
    
    pthread_mutex_unlock(&sync_UE_mutex[antenna_index]);
    
    clock_gettime(CLOCK_MONOTONIC,&time2);
    
    memcpy(&time0,&time2,sizeof(struct timespec));
    
  }
  
  return(0);
  
}