flexran_agent_common.c 75 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
/*! \file flexran_agent_common.c
23
 * \brief common primitives for all agents 
24
 * \author Xenofon Foukas, Mohamed Kassem and Navid Nikaein
25 26 27 28
 * \date 2016
 * \version 0.1
 */

29 30
#include<stdio.h>
#include <time.h>
31

32 33 34
#include "flexran_agent_common.h"
#include "flexran_agent_common_internal.h"
#include "flexran_agent_extern.h"
35
#include "PHY/extern.h"
36 37
#include "log.h"

38
#include "SCHED/defs.h"
39 40
#include "RRC/LITE/extern.h"
#include "RRC/L2_INTERFACE/openair_rrc_L2_interface.h"
41
#include "rrc_eNB_UE_context.h"
42

43 44
void * enb[NUM_MAX_ENB];
void * enb_ue[NUM_MAX_ENB];
45
void * enb_rrc[NUM_MAX_ENB];
46

47 48 49 50
/*
 * message primitives
 */

51
int flexran_agent_serialize_message(Protocol__FlexranMessage *msg, void **buf, int *size) {
52

53
  *size = protocol__flexran_message__get_packed_size(msg);
54 55 56 57 58
  
  *buf = malloc(*size);
  if (buf == NULL)
    goto error;
  
59
  protocol__flexran_message__pack(msg, *buf);
60 61 62 63
  
  return 0;
  
 error:
64
  LOG_E(FLEXRAN_AGENT, "an error occured\n"); // change the com
65 66 67 68 69 70 71
  return -1;
}



/* We assume that the buffer size is equal to the message size.
   Should be chekced durint Tx/Rx */
72
int flexran_agent_deserialize_message(void *data, int size, Protocol__FlexranMessage **msg) {
73
  *msg = protocol__flexran_message__unpack(NULL, size, data);
74 75
  if (*msg == NULL)
    goto error;
76

77 78 79 80 81 82 83 84 85
  return 0;
  
 error:
  //LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
  return -1;
}



86
int flexran_create_header(xid_t xid, Protocol__FlexType type,  Protocol__FlexHeader **header) {
87
  
88
  *header = malloc(sizeof(Protocol__FlexHeader));
89 90 91
  if(*header == NULL)
    goto error;
  
92 93
  protocol__flex_header__init(*header);
  (*header)->version = FLEXRAN_VERSION;
94 95 96 97 98 99 100 101 102
  (*header)->has_version = 1; 
  // check if the type is set
  (*header)->type = type;
  (*header)->has_type = 1;
  (*header)->xid = xid;
  (*header)->has_xid = 1;
  return 0;

 error:
103
  LOG_E(FLEXRAN_AGENT, "%s: an error occured\n", __FUNCTION__);
104 105 106 107
  return -1;
}


108
int flexran_agent_hello(mid_t mod_id, const void *params, Protocol__FlexranMessage **msg) {
109
 
110
  Protocol__FlexHeader *header;
111 112
  /*TODO: Need to set random xid or xid from received hello message*/
  xid_t xid = 1;
113
  if (flexran_create_header(xid, PROTOCOL__FLEX_TYPE__FLPT_HELLO, &header) != 0)
114 115
    goto error;

116 117
  Protocol__FlexHello *hello_msg;
  hello_msg = malloc(sizeof(Protocol__FlexHello));
118 119
  if(hello_msg == NULL)
    goto error;
120
  protocol__flex_hello__init(hello_msg);
121 122
  hello_msg->header = header;

123
  *msg = malloc(sizeof(Protocol__FlexranMessage));
124 125 126
  if(*msg == NULL)
    goto error;
  
127 128 129
  protocol__flexran_message__init(*msg);
  (*msg)->msg_case = PROTOCOL__FLEXRAN_MESSAGE__MSG_HELLO_MSG;
  (*msg)->msg_dir = PROTOCOL__FLEXRAN_DIRECTION__SUCCESSFUL_OUTCOME;
130
  (*msg)->has_msg_dir = 1;
131 132 133 134 135 136 137 138 139 140
  (*msg)->hello_msg = hello_msg;
  return 0;
  
 error:
  if(header != NULL)
    free(header);
  if(hello_msg != NULL)
    free(hello_msg);
  if(*msg != NULL)
    free(*msg);
141
  LOG_E(FLEXRAN_AGENT, "%s: an error occured\n", __FUNCTION__);
142 143 144 145
  return -1;
}


146
int flexran_agent_destroy_hello(Protocol__FlexranMessage *msg) {
147
  
148
  if(msg->msg_case != PROTOCOL__FLEXRAN_MESSAGE__MSG_HELLO_MSG)
149 150 151 152 153 154 155 156
    goto error;
  
  free(msg->hello_msg->header);
  free(msg->hello_msg);
  free(msg);
  return 0;

 error:
157
  LOG_E(FLEXRAN_AGENT, "%s: an error occured\n", __FUNCTION__);
158 159 160 161
  return -1;
}


162
int flexran_agent_echo_request(mid_t mod_id, const void* params, Protocol__FlexranMessage **msg) {
163
  Protocol__FlexHeader *header;
164 165
  /*TODO: Need to set a random xid*/
  xid_t xid = 1;
166
  if (flexran_create_header(xid, PROTOCOL__FLEX_TYPE__FLPT_ECHO_REQUEST, &header) != 0)
167 168
    goto error;

169 170
  Protocol__FlexEchoRequest *echo_request_msg;
  echo_request_msg = malloc(sizeof(Protocol__FlexEchoRequest));
171 172
  if(echo_request_msg == NULL)
    goto error;
173
  protocol__flex_echo_request__init(echo_request_msg);
174 175
  echo_request_msg->header = header;

176
  *msg = malloc(sizeof(Protocol__FlexranMessage));
177 178
  if(*msg == NULL)
    goto error;
179 180 181
  protocol__flexran_message__init(*msg);
  (*msg)->msg_case = PROTOCOL__FLEXRAN_MESSAGE__MSG_ECHO_REQUEST_MSG;
  (*msg)->msg_dir = PROTOCOL__FLEXRAN_DIRECTION__INITIATING_MESSAGE;
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
  (*msg)->echo_request_msg = echo_request_msg;
  return 0;

 error:
  if(header != NULL)
    free(header);
  if(echo_request_msg != NULL)
    free(echo_request_msg);
  if(*msg != NULL)
    free(*msg);
  //LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
  return -1;
}


197
int flexran_agent_destroy_echo_request(Protocol__FlexranMessage *msg) {
198
  if(msg->msg_case != PROTOCOL__FLEXRAN_MESSAGE__MSG_ECHO_REQUEST_MSG)
199 200 201 202 203 204 205 206
    goto error;
  
  free(msg->echo_request_msg->header);
  free(msg->echo_request_msg);
  free(msg);
  return 0;
  
 error:
207
  LOG_E(FLEXRAN_AGENT, "%s: an error occured\n", __FUNCTION__);
208 209 210 211 212
  return -1;
}



213
int flexran_agent_echo_reply(mid_t mod_id, const void *params, Protocol__FlexranMessage **msg) {
214 215
  
  xid_t xid;
216 217
  Protocol__FlexranMessage *input = (Protocol__FlexranMessage *)params;
  Protocol__FlexEchoRequest *echo_req = input->echo_request_msg;
218 219
  xid = (echo_req->header)->xid;

220
  Protocol__FlexHeader *header;
221
  if (flexran_create_header(xid, PROTOCOL__FLEX_TYPE__FLPT_ECHO_REPLY, &header) != 0)
222 223
    goto error;

224 225
  Protocol__FlexEchoReply *echo_reply_msg;
  echo_reply_msg = malloc(sizeof(Protocol__FlexEchoReply));
226 227
  if(echo_reply_msg == NULL)
    goto error;
228
  protocol__flex_echo_reply__init(echo_reply_msg);
229 230
  echo_reply_msg->header = header;

231
  *msg = malloc(sizeof(Protocol__FlexranMessage));
232 233
  if(*msg == NULL)
    goto error;
234 235 236
  protocol__flexran_message__init(*msg);
  (*msg)->msg_case = PROTOCOL__FLEXRAN_MESSAGE__MSG_ECHO_REPLY_MSG;
  (*msg)->msg_dir = PROTOCOL__FLEXRAN_DIRECTION__SUCCESSFUL_OUTCOME;
237
  (*msg)->has_msg_dir = 1;
238 239 240 241 242 243 244 245 246 247
  (*msg)->echo_reply_msg = echo_reply_msg;
  return 0;

 error:
  if(header != NULL)
    free(header);
  if(echo_reply_msg != NULL)
    free(echo_reply_msg);
  if(*msg != NULL)
    free(*msg);
248
  LOG_E(FLEXRAN_AGENT, "%s: an error occured\n", __FUNCTION__);
249 250 251 252
  return -1;
}


253
int flexran_agent_destroy_echo_reply(Protocol__FlexranMessage *msg) {
254
  if(msg->msg_case != PROTOCOL__FLEXRAN_MESSAGE__MSG_ECHO_REPLY_MSG)
255 256 257 258 259 260 261 262
    goto error;
  
  free(msg->echo_reply_msg->header);
  free(msg->echo_reply_msg);
  free(msg);
  return 0;
  
 error:
263
  LOG_E(FLEXRAN_AGENT, "%s: an error occured\n", __FUNCTION__);
264 265
  return -1;
}
266

267

268
int flexran_agent_destroy_enb_config_reply(Protocol__FlexranMessage *msg) {
269
  if(msg->msg_case != PROTOCOL__FLEXRAN_MESSAGE__MSG_ENB_CONFIG_REPLY_MSG)
270 271 272
    goto error;
  free(msg->enb_config_reply_msg->header);
  int i, j;
273
  Protocol__FlexEnbConfigReply *reply = msg->enb_config_reply_msg;
274
  
275
  for(i = 0; i < reply->n_cell_config;i++) {
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
    free(reply->cell_config[i]->mbsfn_subframe_config_rfoffset);
    free(reply->cell_config[i]->mbsfn_subframe_config_rfperiod);
    free(reply->cell_config[i]->mbsfn_subframe_config_sfalloc);
    if (reply->cell_config[i]->si_config != NULL) {
      for(j = 0; j < reply->cell_config[i]->si_config->n_si_message;j++){
	free(reply->cell_config[i]->si_config->si_message[j]);
      }
      free(reply->cell_config[i]->si_config->si_message);
      free(reply->cell_config[i]->si_config);
    }
    free(reply->cell_config[i]);
  }
  free(reply->cell_config);
  free(reply);
  free(msg);
  
  return 0;
 error:
294
  //LOG_E(FLEXRAN_AGENT, "%s: an error occured\n", __FUNCTION__);
295
  return -1;
296 297
}

298
int flexran_agent_destroy_ue_config_reply(Protocol__FlexranMessage *msg) {
299
  if(msg->msg_case != PROTOCOL__FLEXRAN_MESSAGE__MSG_UE_CONFIG_REPLY_MSG)
300 301 302
    goto error;
  free(msg->ue_config_reply_msg->header);
  int i, j;
303
  Protocol__FlexUeConfigReply *reply = msg->ue_config_reply_msg;
304 305 306 307 308 309 310 311 312
  
  for(i = 0; i < reply->n_ue_config;i++){
    free(reply->ue_config[i]->capabilities);
    free(reply->ue_config[i]);
  }
  free(reply->ue_config);
  free(reply);
  free(msg);

313
  return 0;
314
 error:
315
  //LOG_E(FLEXRAN_AGENT, "%s: an error occured\n", __FUNCTION__);
316
  return -1;
317 318
}

319
int flexran_agent_destroy_lc_config_reply(Protocol__FlexranMessage *msg) {
320
  if(msg->msg_case != PROTOCOL__FLEXRAN_MESSAGE__MSG_LC_CONFIG_REPLY_MSG)
321 322 323 324 325 326 327 328 329 330 331 332 333 334
    goto error;

  int i, j;
  free(msg->lc_config_reply_msg->header);
  for (i = 0; i < msg->lc_config_reply_msg->n_lc_ue_config; i++) {
    for (j = 0; j < msg->lc_config_reply_msg->lc_ue_config[i]->n_lc_config; j++) {
      free(msg->lc_config_reply_msg->lc_ue_config[i]->lc_config[j]);
    }
    free(msg->lc_config_reply_msg->lc_ue_config[i]->lc_config);
    free(msg->lc_config_reply_msg->lc_ue_config[i]);
  }
  free(msg->lc_config_reply_msg->lc_ue_config);
  free(msg->lc_config_reply_msg);
  free(msg);
335
  return 0;
336
 error:
337
  //LOG_E(FLEXRAN_AGENT, "%s: an error occured\n", __FUNCTION__);
338
  return -1;
339 340
}

341
int flexran_agent_destroy_ue_state_change(Protocol__FlexranMessage *msg) {
342
  if(msg->msg_case != PROTOCOL__FLEXRAN_MESSAGE__MSG_UE_STATE_CHANGE_MSG)
343 344 345 346 347
    goto error;
  free(msg->ue_state_change_msg->header);
  //TODO: Free the contents of the UE config structure
  free(msg->ue_state_change_msg);
  free(msg);
348
  return 0;
349 350 351 352

 error:
  //LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
  return -1;
353 354
}

355
int flexran_agent_destroy_enb_config_request(Protocol__FlexranMessage *msg) {
356
  if(msg->msg_case != PROTOCOL__FLEXRAN_MESSAGE__MSG_ENB_CONFIG_REQUEST_MSG)
357 358 359 360 361 362 363
    goto error;
  free(msg->enb_config_request_msg->header);
  free(msg->enb_config_request_msg);
  free(msg);
  return 0;
  
 error:
364
  //LOG_E(FLEXRAN_AGENT, "%s: an error occured\n", __FUNCTION__);
365
  return -1;
366 367
}

368
int flexran_agent_destroy_ue_config_request(Protocol__FlexranMessage *msg) {
369 370 371 372
  /* TODO: Deallocate memory for a dynamically allocated UE config message */
  return 0;
}

373
int flexran_agent_destroy_lc_config_request(Protocol__FlexranMessage *msg) {
374 375 376 377
  /* TODO: Deallocate memory for a dynamically allocated LC config message */
  return 0;
}

378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
// call this function to start a nanosecond-resolution timer
struct timespec timer_start(){
    struct timespec start_time;
    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start_time);
    return start_time;
}

// call this function to end a timer, returning nanoseconds elapsed as a long
long timer_end(struct timespec start_time){
    struct timespec end_time;
    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end_time);
    long diffInNanos = end_time.tv_nsec - start_time.tv_nsec;
    return diffInNanos;
}

393
int flexran_agent_control_delegation(mid_t mod_id, const void *params, Protocol__FlexranMessage **msg) {
394

395 396
  Protocol__FlexranMessage *input = (Protocol__FlexranMessage *)params;
  Protocol__FlexControlDelegation *control_delegation_msg = input->control_delegation_msg;
397 398 399 400 401 402 403 404 405 406

  uint32_t delegation_type = control_delegation_msg->delegation_type;

  int i;

  struct timespec vartime = timer_start();
  
  //Write the payload lib into a file in the cache and load the lib
  char lib_name[120];
  char target[512];
407
  snprintf(lib_name, sizeof(lib_name), "/%s.so", control_delegation_msg->name);
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
  strcpy(target, local_cache);
  strcat(target, lib_name);

  FILE *f;
  f = fopen(target, "wb");
  fwrite(control_delegation_msg->payload.data, control_delegation_msg->payload.len, 1, f);
  fclose(f);

  long time_elapsed_nanos = timer_end(vartime);
  *msg = NULL;
  return 0;

 error:
  return -1;
}

424
int flexran_agent_destroy_control_delegation(Protocol__FlexranMessage *msg) {
425 426 427
  /*TODO: Dealocate memory for a dynamically allocated control delegation message*/
}

428
int flexran_agent_reconfiguration(mid_t mod_id, const void *params, Protocol__FlexranMessage **msg) {
429 430
  Protocol__FlexranMessage *input = (Protocol__FlexranMessage *)params;
  Protocol__FlexAgentReconfiguration *agent_reconfiguration_msg = input->agent_reconfiguration_msg;
431 432 433 434 435 436 437

  apply_reconfiguration_policy(mod_id, agent_reconfiguration_msg->policy, strlen(agent_reconfiguration_msg->policy));

  *msg = NULL;
  return 0;
}

438
int flexran_agent_destroy_agent_reconfiguration(Protocol__FlexranMessage *msg) {
439 440 441
  /*TODO: Dealocate memory for a dynamically allocated agent reconfiguration message*/
}

442

443 444 445 446
/*
 * get generic info from RAN
 */

447
void flexran_set_enb_vars(mid_t mod_id, ran_name_t ran){
448 449 450 451 452

  switch (ran){
  case RAN_LTE_OAI :
    enb[mod_id] =  (void *)&eNB_mac_inst[mod_id];
    enb_ue[mod_id] = (void *)&eNB_mac_inst[mod_id].UE_list;
453
    enb_rrc[mod_id] = (void *)&eNB_rrc_inst[mod_id];
454 455 456 457 458 459 460 461
    break;
  default :
    goto error;
  }

  return; 

 error:
462
  LOG_E(FLEXRAN_AGENT, "unknown RAN name %d\n", ran);
463 464
}

465
int flexran_get_current_time_ms (mid_t mod_id, int subframe_flag){
466 467 468 469 470 471 472 473 474

  if (subframe_flag == 1){
    return ((eNB_MAC_INST *)enb[mod_id])->frame*10 + ((eNB_MAC_INST *)enb[mod_id])->subframe;
  }else {
    return ((eNB_MAC_INST *)enb[mod_id])->frame*10;
  }
   
}

475
unsigned int flexran_get_current_frame (mid_t mod_id) {
476 477

  #warning "SFN will not be in [0-1023] when oaisim is used"
478 479 480 481
  return ((eNB_MAC_INST *)enb[mod_id])->frame;
  
}

482 483
unsigned int flexran_get_current_system_frame_num(mid_t mod_id) {
  return (flexran_get_current_frame(mod_id) %1024);
484 485
}

486
unsigned int flexran_get_current_subframe (mid_t mod_id) {
487 488 489 490 491

  return ((eNB_MAC_INST *)enb[mod_id])->subframe;
  
}

492
uint16_t flexran_get_sfn_sf (mid_t mod_id) {
493
  
494 495 496 497
  frame_t frame;
  sub_frame_t subframe;
  uint16_t sfn_sf, frame_mask, sf_mask;
  
498 499
  frame = (frame_t) flexran_get_current_system_frame_num(mod_id);
  subframe = (sub_frame_t) flexran_get_current_subframe(mod_id);
500
  frame_mask = ((1<<12) - 1);
501
  sf_mask = ((1<<4) - 1);
502
  sfn_sf = (subframe & sf_mask) | ((frame & frame_mask) << 4);
503 504 505 506
  
  return sfn_sf;
}

507
uint16_t flexran_get_future_sfn_sf (mid_t mod_id, int ahead_of_time) {
508 509 510 511 512
  
  frame_t frame;
  sub_frame_t subframe;
  uint16_t sfn_sf, frame_mask, sf_mask;
  
513 514
  frame = (frame_t) flexran_get_current_system_frame_num(mod_id);
  subframe = (sub_frame_t) flexran_get_current_subframe(mod_id);
515 516 517 518 519 520 521

  subframe = ((subframe + ahead_of_time) % 10);

  int full_frames_ahead = ((ahead_of_time / 10) % 10);
  
  frame = frame + full_frames_ahead;

522
  if (subframe < flexran_get_current_subframe(mod_id)) {
523 524 525 526 527 528 529 530 531 532
    frame++;
  }

  frame_mask = ((1<<12) - 1);
  sf_mask = ((1<<4) - 1);
  sfn_sf = (subframe & sf_mask) | ((frame & frame_mask) << 4);
  
  return sfn_sf;
}

533
int flexran_get_num_ues (mid_t mod_id){
534 535 536 537

  return  ((UE_list_t *)enb_ue[mod_id])->num_UEs;
}

538
int flexran_get_ue_crnti (mid_t mod_id, mid_t ue_id) {
539

540
  return  UE_RNTI(mod_id, ue_id);
541 542
}

543
int flexran_get_ue_bsr (mid_t mod_id, mid_t ue_id, lcid_t lcid) {
544 545 546 547

  return ((UE_list_t *)enb_ue[mod_id])->UE_template[UE_PCCID(mod_id,ue_id)][ue_id].bsr_info[lcid];
}

548
int flexran_get_ue_phr (mid_t mod_id, mid_t ue_id) {
549 550 551 552

  return ((UE_list_t *)enb_ue[mod_id])->UE_template[UE_PCCID(mod_id,ue_id)][ue_id].phr_info;
}

553
int flexran_get_ue_wcqi (mid_t mod_id, mid_t ue_id) {
554 555
  return ((UE_list_t *)enb_ue[mod_id])->eNB_UE_stats[UE_PCCID(mod_id,ue_id)][ue_id].dl_cqi;
}
556 557 558 559

int flexran_get_tx_queue_size(mid_t mod_id, mid_t ue_id, logical_chan_id_t channel_id) {
	rnti_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
	uint16_t frame = (uint16_t) flexran_get_current_frame(mod_id);
560 561 562
	mac_rlc_status_resp_t rlc_status = mac_rlc_status_ind(mod_id,rnti, mod_id,frame,ENB_FLAG_YES,MBMS_FLAG_NO,channel_id,0);
	return rlc_status.bytes_in_buffer;
}
563

564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
int flexran_update_TA(mid_t mod_id, mid_t ue_id, int CC_id) {
  
  UE_list_t *UE_list=&eNB_mac_inst[mod_id].UE_list;
  UE_sched_ctrl *ue_sched_ctl = &UE_list->UE_sched_ctrl[ue_id];
  int rnti;

  rnti = flexran_get_ue_crnti(mod_id, ue_id);
  if (ue_sched_ctl->ta_timer == 0) {

    // WE SHOULD PROTECT the eNB_UE_stats with a mutex here ...                                                                         
    LTE_eNB_UE_stats		*eNB_UE_stats = mac_xface->get_eNB_UE_stats(mod_id, CC_id, rnti);
    ue_sched_ctl->ta_timer		      = 20;	// wait 20 subframes before taking TA measurement from PHY                                         
    switch (PHY_vars_eNB_g[mod_id][CC_id]->frame_parms.N_RB_DL) {
    case 6:
      ue_sched_ctl->ta_update		      = eNB_UE_stats->timing_advance_update;
      break;

    case 15:
      ue_sched_ctl->ta_update		      = eNB_UE_stats->timing_advance_update/2;
      break;

    case 25:
      ue_sched_ctl->ta_update		      = eNB_UE_stats->timing_advance_update/4;
      break;

    case 50:
      ue_sched_ctl->ta_update		      = eNB_UE_stats->timing_advance_update/8;
      break;

    case 75:
      ue_sched_ctl->ta_update		      = eNB_UE_stats->timing_advance_update/12;
      break;

    case 100:
      ue_sched_ctl->ta_update		      = eNB_UE_stats->timing_advance_update/16;
      break;
    }
    // clear the update in case PHY does not have a new measurement after timer expiry                                               
    eNB_UE_stats->timing_advance_update	      = 0;
  }
  else {
    ue_sched_ctl->ta_timer--;
    ue_sched_ctl->ta_update		      = 0;	// don't trigger a timing advance command                                                          
  }
608

609
  return ue_sched_ctl->ta_update	      = 0;
610

611
}
612

613 614 615 616
int flexran_get_MAC_CE_bitmap_TA(mid_t mod_id, mid_t ue_id,int CC_id) {
  
  UE_list_t			*UE_list      = &eNB_mac_inst[mod_id].UE_list;
  UE_sched_ctrl			*ue_sched_ctl = &UE_list->UE_sched_ctrl[ue_id];
617

618 619 620 621 622 623
  rnti_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
  LTE_eNB_UE_stats *eNB_UE_stats = mac_xface->get_eNB_UE_stats(mod_id,CC_id,rnti);
  
  if (eNB_UE_stats == NULL) {
    return 0;
  }
624

625 626 627 628 629
  if (ue_sched_ctl->ta_update == 0) {
    return 1;
  } else {
    return 0;
  }
630

631
}
632

633
int flexran_get_active_CC(mid_t mod_id, mid_t ue_id) {
634 635 636
	return ((UE_list_t *)enb_ue[mod_id])->numactiveCCs[ue_id];
}

637
int flexran_get_current_RI(mid_t mod_id, mid_t ue_id, int CC_id) {
638
	LTE_eNB_UE_stats	*eNB_UE_stats = NULL;
639

640 641
	int			 pCCid	      = UE_PCCID(mod_id,ue_id);
	rnti_t			 rnti	      = flexran_get_ue_crnti(mod_id,ue_id);
642

643
	eNB_UE_stats			      = mac_xface->get_eNB_UE_stats(mod_id,CC_id,rnti);
644 645 646 647 648
	
	if (eNB_UE_stats == NULL) {
	  return 0;
	}

649 650 651
	return eNB_UE_stats[CC_id].rank;
}

652
int flexran_get_tpc(mid_t mod_id, mid_t ue_id) {
653 654 655
	LTE_eNB_UE_stats	*eNB_UE_stats = NULL;
	int32_t			 normalized_rx_power, target_rx_power;
	int			 tpc	      = 1;
MKassem's avatar
MKassem committed
656

657 658
	int			 pCCid	      = UE_PCCID(mod_id,ue_id);
	rnti_t			 rnti	      = flexran_get_ue_crnti(mod_id,ue_id);
MKassem's avatar
MKassem committed
659

660
	eNB_UE_stats = mac_xface->get_eNB_UE_stats(mod_id, pCCid, rnti);
MKassem's avatar
MKassem committed
661 662 663

	target_rx_power = mac_xface->get_target_pusch_rx_power(mod_id,pCCid);

664 665 666 667 668 669 670 671
	if (eNB_UE_stats == NULL) {
	  normalized_rx_power = target_rx_power;
	} else if (eNB_UE_stats->UL_rssi != NULL) {
	  normalized_rx_power = eNB_UE_stats->UL_rssi[0];
	} else {
	  normalized_rx_power = target_rx_power;
	}

MKassem's avatar
MKassem committed
672
	if (normalized_rx_power>(target_rx_power+1)) {
673
		tpc = 0;	//-1
MKassem's avatar
MKassem committed
674
	} else if (normalized_rx_power<(target_rx_power-1)) {
675
		tpc = 2;	//+1
MKassem's avatar
MKassem committed
676
	} else {
677
		tpc = 1;	//0
MKassem's avatar
MKassem committed
678 679 680 681
	}
	return tpc;
}

682
int flexran_get_harq(const mid_t mod_id, const uint8_t CC_id, const mid_t ue_id, const int frame, const uint8_t subframe, int *id, int *round)	{ //flag_id_status = 0 then id, else status
MKassem's avatar
MKassem committed
683 684 685 686
	/*TODO: Add int TB in function parameters to get the status of the second TB. This can be done to by editing in
	 * get_ue_active_harq_pid function in line 272 file: phy_procedures_lte_eNB.c to add
	 * DLSCH_ptr = PHY_vars_eNB_g[Mod_id][CC_id]->dlsch_eNB[(uint32_t)UE_id][1];*/

687
  uint8_t harq_pid;
688
  uint8_t harq_round;
689
  
MKassem's avatar
MKassem committed
690

691
  uint16_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
MKassem's avatar
MKassem committed
692

693
  mac_xface->get_ue_active_harq_pid(mod_id,CC_id,rnti,frame,subframe,&harq_pid,&harq_round,openair_harq_DL);
MKassem's avatar
MKassem committed
694

695
  *id = harq_pid;
696 697 698 699 700 701
  *round = harq_round;
  /* if (round > 0) { */
  /*   *status = 1; */
  /* } else { */
  /*   *status = 0; */
  /* } */
702

703 704
  /* return 0; */
  return round;
MKassem's avatar
MKassem committed
705 706
}

707
int flexran_get_p0_pucch_dbm(mid_t mod_id, mid_t ue_id, int CC_id) {
708
	LTE_eNB_UE_stats *eNB_UE_stats = NULL;
709
	uint32_t rnti = flexran_get_ue_crnti(mod_id,ue_id);
710 711

	eNB_UE_stats =  mac_xface->get_eNB_UE_stats(mod_id, CC_id, rnti);
712 713 714 715 716
	
	if (eNB_UE_stats == NULL) {
	  return -1;
	}
	
717
	if(eNB_UE_stats->Po_PUCCH_update == 1) {
718 719 720 721 722 723
		return eNB_UE_stats->Po_PUCCH_dBm;
	}
	else
		return -1;
}

724
int flexran_get_p0_nominal_pucch(mid_t mod_id, int CC_id) {
725 726 727 728
	int32_t pucch_rx_received = mac_xface->get_target_pucch_rx_power(mod_id, CC_id);
	return pucch_rx_received;
}

729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
int flexran_get_p0_pucch_status(mid_t mod_id, mid_t ue_id, int CC_id) {
        LTE_eNB_UE_stats *eNB_UE_stats = NULL;
	uint32_t rnti = flexran_get_ue_crnti(mod_id,ue_id);

	eNB_UE_stats =  mac_xface->get_eNB_UE_stats(mod_id, CC_id, rnti);
	return eNB_UE_stats->Po_PUCCH_update;
}

int flexran_update_p0_pucch(mid_t mod_id, mid_t ue_id, int CC_id) {
          LTE_eNB_UE_stats *eNB_UE_stats = NULL;
	uint32_t rnti = flexran_get_ue_crnti(mod_id,ue_id);

	eNB_UE_stats =  mac_xface->get_eNB_UE_stats(mod_id, CC_id, rnti);
	eNB_UE_stats->Po_PUCCH_update = 0;

	return 0;
}

747 748 749 750 751 752 753

/*
 * ************************************
 * Get Messages for eNB Configuration Reply
 * ************************************
 */

754
int flexran_get_hopping_offset(mid_t mod_id, int CC_id) {
755 756 757 758 759
	LTE_DL_FRAME_PARMS   *frame_parms;

	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
	return frame_parms->pusch_config_common.pusch_HoppingOffset;
}
760

761
int flexran_get_hopping_mode(mid_t mod_id, int CC_id) {
762 763 764 765 766
	LTE_DL_FRAME_PARMS   *frame_parms;

	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
	return frame_parms->pusch_config_common.hoppingMode;
}
767

768
int flexran_get_n_SB(mid_t mod_id, int CC_id) {
769 770 771 772 773
	LTE_DL_FRAME_PARMS   *frame_parms;

	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
	return frame_parms->pusch_config_common.n_SB;
}
774

775
int flexran_get_enable64QAM(mid_t mod_id, int CC_id) {
776 777 778 779 780
	LTE_DL_FRAME_PARMS   *frame_parms;

	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
	return frame_parms->pusch_config_common.enable64QAM;
}
781

782
int flexran_get_phich_duration(mid_t mod_id, int CC_id) {
783 784 785 786 787
	LTE_DL_FRAME_PARMS   *frame_parms;

	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
	return frame_parms->phich_config_common.phich_duration;
}
788

789
int flexran_get_phich_resource(mid_t mod_id, int CC_id) {
790 791 792 793 794 795 796 797 798 799 800 801 802 803
	LTE_DL_FRAME_PARMS   *frame_parms;

	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
	if(frame_parms->phich_config_common.phich_resource == oneSixth)
		return 0;
	else if(frame_parms->phich_config_common.phich_resource == half)
		return 1;
	else if(frame_parms->phich_config_common.phich_resource == one)
		return 2;
	else if(frame_parms->phich_config_common.phich_resource == two)
		return 3;

	return -1;
}
804

805
int flexran_get_n1pucch_an(mid_t mod_id, int CC_id) {
806 807 808 809 810
	LTE_DL_FRAME_PARMS   *frame_parms;

	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
	return frame_parms->pucch_config_common.n1PUCCH_AN;
}
811

812
int flexran_get_nRB_CQI(mid_t mod_id, int CC_id) {
813 814 815 816 817
	LTE_DL_FRAME_PARMS   *frame_parms;

	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
	return frame_parms->pucch_config_common.nRB_CQI;
}
818

819
int flexran_get_deltaPUCCH_Shift(mid_t mod_id, int CC_id) {
820 821 822 823 824
	LTE_DL_FRAME_PARMS   *frame_parms;

	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
	return frame_parms->pucch_config_common.deltaPUCCH_Shift;
}
825

826
int flexran_get_prach_ConfigIndex(mid_t mod_id, int CC_id) {
827 828 829 830 831
	LTE_DL_FRAME_PARMS   *frame_parms;

	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
	return frame_parms->prach_config_common.prach_ConfigInfo.prach_ConfigIndex;
}
832

833
int flexran_get_prach_FreqOffset(mid_t mod_id, int CC_id) {
834 835 836 837 838
	LTE_DL_FRAME_PARMS   *frame_parms;

	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
	return frame_parms->prach_config_common.prach_ConfigInfo.prach_FreqOffset;
}
839

840
int flexran_get_maxHARQ_Msg3Tx(mid_t mod_id, int CC_id) {
841 842 843 844 845
	LTE_DL_FRAME_PARMS   *frame_parms;

	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
	return frame_parms->maxHARQ_Msg3Tx;
}
846

847
int flexran_get_ul_cyclic_prefix_length(mid_t mod_id, int CC_id) {
848 849 850 851 852
	LTE_DL_FRAME_PARMS   *frame_parms;

	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
	return frame_parms->Ncp_UL;
}
853

854
int flexran_get_dl_cyclic_prefix_length(mid_t mod_id, int CC_id) {
855 856 857 858 859
	LTE_DL_FRAME_PARMS   *frame_parms;

	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
	return frame_parms->Ncp;
}
860

861
int flexran_get_cell_id(mid_t mod_id, int CC_id) {
862 863 864 865 866
	LTE_DL_FRAME_PARMS   *frame_parms;

	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
	return frame_parms->Nid_cell;
}
867

868
int flexran_get_srs_BandwidthConfig(mid_t mod_id, int CC_id) {
869 870 871 872 873
	LTE_DL_FRAME_PARMS   *frame_parms;

	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
	return frame_parms->soundingrs_ul_config_common.srs_BandwidthConfig;
}
874

875
int flexran_get_srs_SubframeConfig(mid_t mod_id, int CC_id) {
876 877 878 879 880
	LTE_DL_FRAME_PARMS   *frame_parms;

	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
	return frame_parms->soundingrs_ul_config_common.srs_SubframeConfig;
}
881

882
int flexran_get_srs_MaxUpPts(mid_t mod_id, int CC_id) {
883 884 885 886 887
	LTE_DL_FRAME_PARMS   *frame_parms;

	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
	return frame_parms->soundingrs_ul_config_common.srs_MaxUpPts;
}
888

889
int flexran_get_N_RB_DL(mid_t mod_id, int CC_id) {
890 891 892 893 894
	LTE_DL_FRAME_PARMS   *frame_parms;

	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
	return frame_parms->N_RB_DL;
}
895

896
int flexran_get_N_RB_UL(mid_t mod_id, int CC_id) {
897 898 899 900 901
	LTE_DL_FRAME_PARMS   *frame_parms;

	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
	return frame_parms->N_RB_UL;
}
902

903 904 905 906 907 908 909
int flexran_get_N_RBG(mid_t mod_id, int CC_id) {
  	LTE_DL_FRAME_PARMS   *frame_parms;

	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
	return frame_parms->N_RBG;
}

910
int flexran_get_subframe_assignment(mid_t mod_id, int CC_id) {
911 912 913 914 915
	LTE_DL_FRAME_PARMS   *frame_parms;

	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
	return frame_parms->tdd_config;
}
916

917
int flexran_get_special_subframe_assignment(mid_t mod_id, int CC_id) {
918 919 920 921 922
	LTE_DL_FRAME_PARMS   *frame_parms;

	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
	return frame_parms->tdd_config_S;
}
923

924
int flexran_get_ra_ResponseWindowSize(mid_t mod_id, int CC_id) {
925 926 927 928
	Enb_properties_array_t *enb_properties;
	enb_properties = enb_config_get();
	return enb_properties->properties[mod_id]->rach_raResponseWindowSize[CC_id];
}
929

930
int flexran_get_mac_ContentionResolutionTimer(mid_t mod_id, int CC_id) {
931 932 933 934
	Enb_properties_array_t *enb_properties;
	enb_properties = enb_config_get();
	return enb_properties->properties[mod_id]->rach_macContentionResolutionTimer[CC_id];
}
935

936
int flexran_get_duplex_mode(mid_t mod_id, int CC_id) {
937 938 939
	LTE_DL_FRAME_PARMS   *frame_parms;

	frame_parms = mac_xface->get_lte_frame_parms(mod_id, CC_id);
940 941 942 943
	if(frame_parms->frame_type == TDD)
		return PROTOCOL__FLEX_DUPLEX_MODE__FLDM_TDD;
	else if (frame_parms->frame_type == FDD)
		return PROTOCOL__FLEX_DUPLEX_MODE__FLDM_FDD;
944 945 946

	return -1;
}
947

948
long flexran_get_si_window_length(mid_t mod_id, int CC_id) {
949 950
	return  ((eNB_RRC_INST *)enb_rrc[mod_id])->carrier[CC_id].sib1->si_WindowLength;
}
951

952
int flexran_get_sib1_length(mid_t mod_id, int CC_id) {
953 954
	return  ((eNB_RRC_INST *)enb_rrc[mod_id])->carrier[CC_id].sizeof_SIB1;
}
955

956
int flexran_get_num_pdcch_symb(mid_t mod_id, int CC_id) {
957 958 959
  /* TODO: This should return the number of PDCCH symbols initially used by the cell CC_id */
  return 0;
  //(PHY_vars_UE_g[mod_id][CC_id]->lte_ue_pdcch_vars[mod_id]->num_pdcch_symbols);
960 961
}

962 963


964
/*
965 966 967
 * ************************************
 * Get Messages for UE Configuration Reply
 * ************************************
968 969
 */

970

971
int flexran_get_time_alignment_timer(mid_t mod_id, mid_t ue_id) {
972
	struct rrc_eNB_ue_context_s* ue_context_p = NULL;
973
	uint32_t rntiP = flexran_get_ue_crnti(mod_id,ue_id);
974 975

	ue_context_p = rrc_eNB_get_ue_context(&eNB_rrc_inst[mod_id],rntiP);
976 977 978
	if(ue_context_p != NULL) {
	  if(ue_context_p->ue_context.mac_MainConfig != NULL)
	    return ue_context_p->ue_context.mac_MainConfig->timeAlignmentTimerDedicated;
979 980
	}
	else
981
	  return -1;
982 983
}

984
int flexran_get_meas_gap_config(mid_t mod_id, mid_t ue_id) {
985
  struct rrc_eNB_ue_context_s* ue_context_p = NULL;
986
  uint32_t rntiP = flexran_get_ue_crnti(mod_id,ue_id);
987

988 989
  ue_context_p = rrc_eNB_get_ue_context(&eNB_rrc_inst[mod_id],rntiP);
  if(ue_context_p != NULL) {
990
    if(ue_context_p->ue_context.measGapConfig != NULL) {
991 992
      if(ue_context_p->ue_context.measGapConfig->present == MeasGapConfig_PR_setup) {
	if (ue_context_p->ue_context.measGapConfig->choice.setup.gapOffset.present == MeasGapConfig__setup__gapOffset_PR_gp0) {
993
	  return PROTOCOL__FLEX_MEAS_GAP_CONFIG_PATTERN__FLMGCP_GP1;
994
	} else if (ue_context_p->ue_context.measGapConfig->choice.setup.gapOffset.present == MeasGapConfig__setup__gapOffset_PR_gp1) {
995
	  return PROTOCOL__FLEX_MEAS_GAP_CONFIG_PATTERN__FLMGCP_GP2;
996
	} else {
997
	  return PROTOCOL__FLEX_MEAS_GAP_CONFIG_PATTERN__FLMGCP_OFF;
998
	}
999 1000 1001 1002
      }
    }
  }
  return -1;
1003 1004
}

1005

1006
int flexran_get_meas_gap_config_offset(mid_t mod_id, mid_t ue_id) {
1007
  struct rrc_eNB_ue_context_s* ue_context_p = NULL;
1008
  uint32_t rntiP = flexran_get_ue_crnti(mod_id,ue_id);
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
  
  ue_context_p = rrc_eNB_get_ue_context(&eNB_rrc_inst[mod_id],rntiP);
  
  if(ue_context_p != NULL) {
    if(ue_context_p->ue_context.measGapConfig != NULL){
      if(ue_context_p->ue_context.measGapConfig->present == MeasGapConfig_PR_setup) {
	if (ue_context_p->ue_context.measGapConfig->choice.setup.gapOffset.present == MeasGapConfig__setup__gapOffset_PR_gp0) {
	  return ue_context_p->ue_context.measGapConfig->choice.setup.gapOffset.choice.gp0;
	} else if (ue_context_p->ue_context.measGapConfig->choice.setup.gapOffset.present == MeasGapConfig__setup__gapOffset_PR_gp1) {
	  return ue_context_p->ue_context.measGapConfig->choice.setup.gapOffset.choice.gp0;
	} 
      }
    }
  }
  return -1;
1024 1025
}

1026
int flexran_get_ue_aggregated_max_bitrate_dl (mid_t mod_id, mid_t ue_id) {
1027 1028 1029
	return ((UE_list_t *)enb_ue[mod_id])->UE_sched_ctrl[ue_id].ue_AggregatedMaximumBitrateDL;
}

1030
int flexran_get_ue_aggregated_max_bitrate_ul (mid_t mod_id, mid_t ue_id) {
1031 1032 1033
	return ((UE_list_t *)enb_ue[mod_id])->UE_sched_ctrl[ue_id].ue_AggregatedMaximumBitrateUL;
}

1034
int flexran_get_half_duplex(mid_t ue_id) {
1035
  // TODO
1036 1037 1038 1039 1040 1041 1042
	//int halfduplex = 0;
	//int bands_to_scan = ((UE_RRC_INST *)enb_ue_rrc[ue_id])->UECap->UE_EUTRA_Capability->rf_Parameters.supportedBandListEUTRA.list.count;
	//for (int i =0; i < bands_to_scan; i++){
		//if(((UE_RRC_INST *)enb_ue_rrc[ue_id])->UECap->UE_EUTRA_Capability->rf_Parameters.supportedBandListEUTRA.list.array[i]->halfDuplex > 0)
		//	halfduplex = 1;
	//}
	//return halfduplex;
1043 1044
}

1045
int flexran_get_intra_sf_hopping(mid_t ue_id) {
1046 1047 1048
	//TODO:Get proper value
	//temp = (((UE_RRC_INST *)enb_ue_rrc[ue_id])->UECap->UE_EUTRA_Capability->featureGroupIndicators->buf);
	//return (0 & ( 1 << (31)));
1049 1050
}

1051
int flexran_get_type2_sb_1(mid_t ue_id) {
1052 1053 1054 1055
	//TODO:Get proper value
	//uint8_t temp = 0;
	//temp = (((UE_RRC_INST *)enb_ue_rrc[ue_id])->UECap->UE_EUTRA_Capability->featureGroupIndicators->buf);
	//return (temp & ( 1 << (11)));
1056 1057
}

1058
int flexran_get_ue_category(mid_t ue_id) {
1059 1060
	//TODO:Get proper value
	//return (((UE_RRC_INST *)enb_ue_rrc[ue_id])->UECap->UE_EUTRA_Capability->ue_Category);
1061 1062
}

1063
int flexran_get_res_alloc_type1(mid_t ue_id) {
1064 1065 1066 1067
	//TODO:Get proper value
	//uint8_t temp = 0;
	//temp = (((UE_RRC_INST *)enb_ue_rrc[ue_id])->UECap->UE_EUTRA_Capability->featureGroupIndicators->buf);
	//return (temp & ( 1 << (30)));
1068 1069
}

1070
int flexran_get_ue_transmission_mode(mid_t mod_id, mid_t ue_id) {
1071
	struct rrc_eNB_ue_context_s* ue_context_p = NULL;
1072
	uint32_t rntiP = flexran_get_ue_crnti(mod_id,ue_id);
1073 1074 1075

	ue_context_p = rrc_eNB_get_ue_context(&eNB_rrc_inst[mod_id],rntiP);

1076 1077 1078 1079
	if(ue_context_p != NULL) {
	  if(ue_context_p->ue_context.physicalConfigDedicated != NULL){
	    return ue_context_p->ue_context.physicalConfigDedicated->antennaInfo->choice.explicitValue.transmissionMode;
	  }
1080 1081
	}
	else
1082
	  return -1;
1083 1084
}

1085
int flexran_get_tti_bundling(mid_t mod_id, mid_t ue_id) {
1086
	struct rrc_eNB_ue_context_s* ue_context_p = NULL;
1087
	uint32_t rntiP = flexran_get_ue_crnti(mod_id,ue_id);
1088 1089

	ue_context_p = rrc_eNB_get_ue_context(&eNB_rrc_inst[mod_id],rntiP);
1090 1091 1092 1093
	if(ue_context_p != NULL) {
	  if(ue_context_p->ue_context.mac_MainConfig != NULL){
	    return ue_context_p->ue_context.mac_MainConfig->ul_SCH_Config->ttiBundling;
	  }
1094 1095
	}
	else
1096
	  return -1;
1097 1098
}

1099
int flexran_get_maxHARQ_TX(mid_t mod_id, mid_t ue_id) {
1100
  struct rrc_eNB_ue_context_s* ue_context_p = NULL;
1101
  uint32_t rntiP = flexran_get_ue_crnti(mod_id,ue_id);
1102 1103 1104 1105 1106 1107 1108 1109
  
  ue_context_p = rrc_eNB_get_ue_context(&eNB_rrc_inst[mod_id],rntiP);
  if(ue_context_p != NULL) {
    if(ue_context_p->ue_context.mac_MainConfig != NULL){
      return *ue_context_p->ue_context.mac_MainConfig->ul_SCH_Config->maxHARQ_Tx;
    }
  }
  return -1;
1110 1111
}

1112
int flexran_get_beta_offset_ack_index(mid_t mod_id, mid_t ue_id) {
1113
	struct rrc_eNB_ue_context_s* ue_context_p = NULL;
1114
	uint32_t rntiP = flexran_get_ue_crnti(mod_id,ue_id);
1115 1116

	ue_context_p = rrc_eNB_get_ue_context(&eNB_rrc_inst[mod_id],rntiP);
1117 1118 1119 1120
	if(ue_context_p != NULL) {
	  if(ue_context_p->ue_context.physicalConfigDedicated != NULL){
	    return ue_context_p->ue_context.physicalConfigDedicated->pusch_ConfigDedicated->betaOffset_ACK_Index;
	  }
1121 1122
	}
	else
1123
	  return -1;
1124 1125
}

1126
int flexran_get_beta_offset_ri_index(mid_t mod_id, mid_t ue_id) {
1127
	struct rrc_eNB_ue_context_s* ue_context_p = NULL;
1128
	uint32_t rntiP = flexran_get_ue_crnti(mod_id,ue_id);
1129 1130

	ue_context_p = rrc_eNB_get_ue_context(&eNB_rrc_inst[mod_id],rntiP);
1131 1132 1133 1134
	if(ue_context_p != NULL) {
	  if(ue_context_p->ue_context.physicalConfigDedicated != NULL){
	    return ue_context_p->ue_context.physicalConfigDedicated->pusch_ConfigDedicated->betaOffset_RI_Index;
	  }
1135 1136
	}
	else
1137
	  return -1;
1138 1139
}

1140
int flexran_get_beta_offset_cqi_index(mid_t mod_id, mid_t ue_id) {
1141
	struct rrc_eNB_ue_context_s* ue_context_p = NULL;
1142
	uint32_t rntiP = flexran_get_ue_crnti(mod_id,ue_id);
1143 1144

	ue_context_p = rrc_eNB_get_ue_context(&eNB_rrc_inst[mod_id],rntiP);
1145 1146 1147 1148
	if(ue_context_p != NULL) {
	  if(ue_context_p->ue_context.physicalConfigDedicated != NULL){
	    return ue_context_p->ue_context.physicalConfigDedicated->pusch_ConfigDedicated->betaOffset_CQI_Index;
	  }
1149 1150
	}
	else
1151
	  return -1;
1152 1153
}

1154
int flexran_get_simultaneous_ack_nack_cqi(mid_t mod_id, mid_t ue_id) {
1155
  struct rrc_eNB_ue_context_s* ue_context_p = NULL;
1156
  uint32_t rntiP = flexran_get_ue_crnti(mod_id,ue_id);
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
  
  ue_context_p = rrc_eNB_get_ue_context(&eNB_rrc_inst[mod_id],rntiP);
  if(ue_context_p != NULL) {
    if(ue_context_p->ue_context.physicalConfigDedicated != NULL){
      if (ue_context_p->ue_context.physicalConfigDedicated->cqi_ReportConfig->cqi_ReportPeriodic != NULL) {
	return ue_context_p->ue_context.physicalConfigDedicated->cqi_ReportConfig->cqi_ReportPeriodic->choice.setup.simultaneousAckNackAndCQI;
      }
    }
  }
  return -1;
1167 1168
}

1169
int flexran_get_ack_nack_simultaneous_trans(mid_t mod_id,mid_t ue_id) {
1170
	return (&eNB_rrc_inst[mod_id])->carrier[0].sib2->radioResourceConfigCommon.soundingRS_UL_ConfigCommon.choice.setup.ackNackSRS_SimultaneousTransmission;
1171 1172
}

1173
int flexran_get_aperiodic_cqi_rep_mode(mid_t mod_id,mid_t ue_id) {
1174
  struct rrc_eNB_ue_context_s* ue_context_p = NULL;
1175
  uint32_t rntiP = flexran_get_ue_crnti(mod_id,ue_id);
1176 1177 1178 1179 1180 1181 1182 1183 1184
  
  ue_context_p = rrc_eNB_get_ue_context(&eNB_rrc_inst[mod_id],rntiP);
  
  if(ue_context_p != NULL) {
    if(ue_context_p->ue_context.physicalConfigDedicated != NULL){
      return *ue_context_p->ue_context.physicalConfigDedicated->cqi_ReportConfig->cqi_ReportModeAperiodic;
    }
  }
  return -1;
1185 1186
}

1187
int flexran_get_tdd_ack_nack_feedback(mid_t mod_id, mid_t ue_id) {
1188
	struct rrc_eNB_ue_context_s* ue_context_p = NULL;
1189
	uint32_t rntiP = flexran_get_ue_crnti(mod_id,ue_id);
1190 1191 1192

	ue_context_p = rrc_eNB_get_ue_context(&eNB_rrc_inst[mod_id],rntiP);

1193 1194 1195 1196
	if(ue_context_p != NULL) {
	  if(ue_context_p->ue_context.physicalConfigDedicated != NULL){
	    return ue_context_p->ue_context.physicalConfigDedicated->pucch_ConfigDedicated->tdd_AckNackFeedbackMode;
	  }
1197 1198
	}
	else
1199
	  return -1;
1200 1201
}

1202
int flexran_get_ack_nack_repetition_factor(mid_t mod_id, mid_t ue_id) {
1203
	struct rrc_eNB_ue_context_s* ue_context_p = NULL;
1204
	uint32_t rntiP = flexran_get_ue_crnti(mod_id,ue_id);
1205 1206

	ue_context_p = rrc_eNB_get_ue_context(&eNB_rrc_inst[mod_id],rntiP);
1207 1208 1209 1210
	if(ue_context_p != NULL) {
	  if(ue_context_p->ue_context.physicalConfigDedicated != NULL){
	    return ue_context_p->ue_context.physicalConfigDedicated->pucch_ConfigDedicated->ackNackRepetition.choice.setup.repetitionFactor;
	  }
1211 1212
	}
	else
1213
	  return -1;
1214 1215
}

1216
int flexran_get_extended_bsr_size(mid_t mod_id, mid_t ue_id) {
1217 1218
  //TODO: need to double check
  struct rrc_eNB_ue_context_s* ue_context_p = NULL;
1219
  uint32_t rntiP = flexran_get_ue_crnti(mod_id,ue_id);
1220

1221 1222 1223 1224 1225 1226 1227
  ue_context_p = rrc_eNB_get_ue_context(&eNB_rrc_inst[mod_id],rntiP);
  if(ue_context_p != NULL) {
    if(ue_context_p->ue_context.mac_MainConfig != NULL){
      if(ue_context_p->ue_context.mac_MainConfig->ext2 != NULL){
	long val = (*(ue_context_p->ue_context.mac_MainConfig->ext2->mac_MainConfig_v1020->extendedBSR_Sizes_r10));
	if (val > 0) {
	  return 1;
1228
	}
1229 1230 1231 1232
      }
    }
  }
  return -1;
1233 1234
}

1235
int flexran_get_ue_transmission_antenna(mid_t mod_id, mid_t ue_id) {
1236
	struct rrc_eNB_ue_context_s* ue_context_p = NULL;
1237
	uint32_t rntiP = flexran_get_ue_crnti(mod_id,ue_id);
1238 1239 1240

	ue_context_p = rrc_eNB_get_ue_context(&eNB_rrc_inst[mod_id],rntiP);

1241 1242 1243 1244 1245 1246 1247 1248 1249
	if(ue_context_p != NULL) {
	  if(ue_context_p->ue_context.physicalConfigDedicated != NULL){
	    if(ue_context_p->ue_context.physicalConfigDedicated->antennaInfo->choice.explicitValue.ue_TransmitAntennaSelection.choice.setup == AntennaInfoDedicated__ue_TransmitAntennaSelection__setup_closedLoop)
	      return 2;
	    else if(ue_context_p->ue_context.physicalConfigDedicated->antennaInfo->choice.explicitValue.ue_TransmitAntennaSelection.choice.setup == AntennaInfoDedicated__ue_TransmitAntennaSelection__setup_openLoop)
	      return 1;
	    else
	      return 0;
	  }
1250
	}
1251
	else
1252
	  return -1;
1253 1254
}

1255
int flexran_get_lcg(mid_t ue_id, mid_t lc_id) {
1256 1257 1258
  if (UE_mac_inst == NULL) {
    return -1;
  }
1259 1260 1261 1262 1263
  if(UE_mac_inst[ue_id].logicalChannelConfig[lc_id]