enb_agent_common.c 57.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*******************************************************************************
    OpenAirInterface
    Copyright(c) 1999 - 2014 Eurecom

    OpenAirInterface is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.


    OpenAirInterface is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

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

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

  Address      : Eurecom, Compus SophiaTech 450, route des chappes, 06451 Biot, France.

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

30 31 32
/*! \file enb_agent_common.c
 * \brief common primitives for all agents 
 * \author Navid Nikaein and Xenofon Foukas
33 34 35 36
 * \date 2016
 * \version 0.1
 */

37 38 39
#include<stdio.h>
#include <dlfcn.h>
#include <time.h>
40 41

#include "enb_agent_common.h"
42
#include "enb_agent_extern.h"
MKassem's avatar
MKassem committed
43
#include "PHY/extern.h"
44 45 46
#include "log.h"


47 48
void * enb[NUM_MAX_ENB];
void * enb_ue[NUM_MAX_ENB];
MKassem's avatar
MKassem committed
49
void * enb_rrc[NUM_MAX_ENB];
50
void * enb_ue_rrc[NUM_MAX_ENB];
51 52 53 54
/*
 * message primitives
 */

55 56 57 58 59 60 61 62 63 64 65 66 67
int enb_agent_serialize_message(Protocol__ProgranMessage *msg, void **buf, int *size) {

  *size = protocol__progran_message__get_packed_size(msg);
  
  *buf = malloc(*size);
  if (buf == NULL)
    goto error;
  
  protocol__progran_message__pack(msg, *buf);
  
  return 0;
  
 error:
68
  LOG_E(ENB_AGENT, "an error occured\n"); // change the com
69 70 71 72 73 74 75 76 77 78 79
  return -1;
}



/* We assume that the buffer size is equal to the message size.
   Should be chekced durint Tx/Rx */
int enb_agent_deserialize_message(void *data, int size, Protocol__ProgranMessage **msg) {
  *msg = protocol__progran_message__unpack(NULL, size, data);
  if (*msg == NULL)
    goto error;
80

81 82 83 84 85 86 87 88 89
  return 0;
  
 error:
  //LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
  return -1;
}



90
int prp_create_header(xid_t xid, Protocol__PrpType type,  Protocol__PrpHeader **header) {
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
  
  *header = malloc(sizeof(Protocol__PrpHeader));
  if(*header == NULL)
    goto error;
  
  protocol__prp_header__init(*header);
  (*header)->version = PROGRAN_VERSION;
  (*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:
  LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
  return -1;
}


112
int enb_agent_hello(mid_t mod_id, const void *params, Protocol__ProgranMessage **msg) {
113 114
 
  Protocol__PrpHeader *header;
115 116
  /*TODO: Need to set random xid or xid from received hello message*/
  xid_t xid = 1;
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
  if (prp_create_header(xid, PROTOCOL__PRP_TYPE__PRPT_HELLO, &header) != 0)
    goto error;

  Protocol__PrpHello *hello_msg;
  hello_msg = malloc(sizeof(Protocol__PrpHello));
  if(hello_msg == NULL)
    goto error;
  protocol__prp_hello__init(hello_msg);
  hello_msg->header = header;

  *msg = malloc(sizeof(Protocol__ProgranMessage));
  if(*msg == NULL)
    goto error;
  
  protocol__progran_message__init(*msg);
  (*msg)->msg_case = PROTOCOL__PROGRAN_MESSAGE__MSG_HELLO_MSG;
133
  (*msg)->msg_dir = PROTOCOL__PROGRAN_DIRECTION__SUCCESSFUL_OUTCOME;
134
  (*msg)->has_msg_dir = 1;
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
  (*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);
  LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
  return -1;
}


150
int enb_agent_destroy_hello(Protocol__ProgranMessage *msg) {
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
  
  if(msg->msg_case != PROTOCOL__PROGRAN_MESSAGE__MSG_HELLO_MSG)
    goto error;
  
  free(msg->hello_msg->header);
  free(msg->hello_msg);
  free(msg);
  return 0;

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


166
int enb_agent_echo_request(mid_t mod_id, const void* params, Protocol__ProgranMessage **msg) {
167
  Protocol__PrpHeader *header;
168 169
  /*TODO: Need to set a random xid*/
  xid_t xid = 1;
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
  if (prp_create_header(xid, PROTOCOL__PRP_TYPE__PRPT_ECHO_REQUEST, &header) != 0)
    goto error;

  Protocol__PrpEchoRequest *echo_request_msg;
  echo_request_msg = malloc(sizeof(Protocol__PrpEchoRequest));
  if(echo_request_msg == NULL)
    goto error;
  protocol__prp_echo_request__init(echo_request_msg);
  echo_request_msg->header = header;

  *msg = malloc(sizeof(Protocol__ProgranMessage));
  if(*msg == NULL)
    goto error;
  protocol__progran_message__init(*msg);
  (*msg)->msg_case = PROTOCOL__PROGRAN_MESSAGE__MSG_ECHO_REQUEST_MSG;
  (*msg)->msg_dir = PROTOCOL__PROGRAN_DIRECTION__INITIATING_MESSAGE;
  (*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;
}


int enb_agent_destroy_echo_request(Protocol__ProgranMessage *msg) {
  if(msg->msg_case != PROTOCOL__PROGRAN_MESSAGE__MSG_ECHO_REQUEST_MSG)
    goto error;
  
  free(msg->echo_request_msg->header);
  free(msg->echo_request_msg);
  free(msg);
  return 0;
  
 error:
  LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
  return -1;
}



217 218 219 220 221 222 223
int enb_agent_echo_reply(mid_t mod_id, const void *params, Protocol__ProgranMessage **msg) {
  
  xid_t xid;
  Protocol__ProgranMessage *input = (Protocol__ProgranMessage *)params;
  Protocol__PrpEchoRequest *echo_req = input->echo_request_msg;
  xid = (echo_req->header)->xid;

224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
  Protocol__PrpHeader *header;
  if (prp_create_header(xid, PROTOCOL__PRP_TYPE__PRPT_ECHO_REPLY, &header) != 0)
    goto error;

  Protocol__PrpEchoReply *echo_reply_msg;
  echo_reply_msg = malloc(sizeof(Protocol__PrpEchoReply));
  if(echo_reply_msg == NULL)
    goto error;
  protocol__prp_echo_reply__init(echo_reply_msg);
  echo_reply_msg->header = header;

  *msg = malloc(sizeof(Protocol__ProgranMessage));
  if(*msg == NULL)
    goto error;
  protocol__progran_message__init(*msg);
  (*msg)->msg_case = PROTOCOL__PROGRAN_MESSAGE__MSG_ECHO_REPLY_MSG;
  (*msg)->msg_dir = PROTOCOL__PROGRAN_DIRECTION__SUCCESSFUL_OUTCOME;
241
  (*msg)->has_msg_dir = 1;
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
  (*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);
  LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
  return -1;
}


int enb_agent_destroy_echo_reply(Protocol__ProgranMessage *msg) {
  if(msg->msg_case != PROTOCOL__PROGRAN_MESSAGE__MSG_ECHO_REPLY_MSG)
    goto error;
  
  free(msg->echo_reply_msg->header);
  free(msg->echo_reply_msg);
  free(msg);
  return 0;
  
 error:
  LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
  return -1;
}
270

271 272

int enb_agent_destroy_enb_config_reply(Protocol__ProgranMessage *msg) {
MKassem's avatar
MKassem committed
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
	if(msg->msg_case != PROTOCOL__PROGRAN_MESSAGE__MSG_ENB_CONFIG_REPLY_MSG)
		goto error;
	free(msg->enb_config_reply_msg->header);
	int i, j;
	Protocol__PrpEnbConfigReply *reply = msg->enb_config_reply_msg;

	for(i = 0; i < reply->n_cell_config;i++){
		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);
		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:
	//LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
	return -1;
298 299 300
}

int enb_agent_destroy_ue_config_reply(Protocol__ProgranMessage *msg) {
301 302 303 304 305 306 307 308 309 310 311 312 313 314
  if(msg->msg_case != PROTOCOL__PROGRAN_MESSAGE__MSG_UE_CONFIG_REPLY_MSG)
    goto error;
  free(msg->ue_config_reply_msg->header);
  int i, j;
  Protocol__PrpUeConfigReply *reply = msg->ue_config_reply_msg;
  
  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);

315
  return 0;
316 317 318
 error:
  //LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
  return -1;
319 320 321
}

int enb_agent_destroy_lc_config_reply(Protocol__ProgranMessage *msg) {
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
  if(msg->msg_case != PROTOCOL__PROGRAN_MESSAGE__MSG_LC_CONFIG_REPLY_MSG)
    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);
337
  return 0;
338 339 340
 error:
  //LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
  return -1;
341 342
}

343
int enb_agent_ue_state_change(mid_t mod_id, uint32_t rnti, uint8_t state_change) {
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
  int size;
  Protocol__ProgranMessage *msg;
  Protocol__PrpHeader *header;
  void *data;
  int priority;
  err_code_t err_code;

  int xid = 0;

  if (prp_create_header(xid, PROTOCOL__PRP_TYPE__PRPT_UE_STATE_CHANGE, &header) != 0)
    goto error;

  Protocol__PrpUeStateChange *ue_state_change_msg;
  ue_state_change_msg = malloc(sizeof(Protocol__PrpUeStateChange));
  if(ue_state_change_msg == NULL) {
    goto error;
  }
  protocol__prp_ue_state_change__init(ue_state_change_msg);
  ue_state_change_msg->has_type = 1;
  ue_state_change_msg->type = state_change;

  Protocol__PrpUeConfig *config;
  config = malloc(sizeof(Protocol__PrpUeConfig));
  if (config == NULL) {
    goto error;
  }
  protocol__prp_ue_config__init(config);
  if (state_change == PROTOCOL__PRP_UE_STATE_CHANGE_TYPE__PRUESC_DEACTIVATED) {
    // Simply set the rnti of the UE
    config->has_rnti = 1;
    config->rnti = rnti;
  } else if (state_change == PROTOCOL__PRP_UE_STATE_CHANGE_TYPE__PRUESC_UPDATED
	     || state_change == PROTOCOL__PRP_UE_STATE_CHANGE_TYPE__PRUESC_ACTIVATED) {
    // TODO: Set the whole UE configuration message
    
  } else if (state_change == PROTOCOL__PRP_UE_STATE_CHANGE_TYPE__PRUESC_MOVED) {
    // TODO: Not supported for now. Leave blank
  }

  ue_state_change_msg->config = config;
  msg = malloc(sizeof(Protocol__ProgranMessage));
  if (msg == NULL) {
    goto error;
  }
  protocol__progran_message__init(msg);
  msg->msg_case = PROTOCOL__PROGRAN_MESSAGE__MSG_UE_STATE_CHANGE_MSG;
  msg->msg_dir = PROTOCOL__PROGRAN_DIRECTION__INITIATING_MESSAGE;
  msg->ue_state_change_msg = ue_state_change_msg;
  
  data = enb_agent_pack_message(msg, &size);
  /*Send sr info using the MAC channel of the eNB*/
  if (enb_agent_msg_send(mod_id, ENB_AGENT_DEFAULT, data, size, priority)) {
    err_code = PROTOCOL__PROGRAN_ERR__MSG_ENQUEUING;
    goto error;
  }

  LOG_D(ENB_AGENT,"sent message with size %d\n", size);
  return;
 error:
  LOG_D(ENB_AGENT, "Could not send UE state message\n");
404 405 406
}

int enb_agent_destroy_ue_state_change(Protocol__ProgranMessage *msg) {
407 408 409 410 411 412
  if(msg->msg_case != PROTOCOL__PROGRAN_MESSAGE__MSG_UE_STATE_CHANGE_MSG)
    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);
413
  return 0;
414 415 416 417

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

420
int enb_agent_destroy_enb_config_request(Protocol__ProgranMessage *msg) {
421 422 423 424 425 426 427 428 429 430
  if(msg->msg_case != PROTOCOL__PROGRAN_MESSAGE__MSG_ENB_CONFIG_REQUEST_MSG)
    goto error;
  free(msg->enb_config_request_msg->header);
  free(msg->enb_config_request_msg);
  free(msg);
  return 0;
  
 error:
  //LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
  return -1;
431 432 433 434 435 436 437 438 439 440 441 442
}

int enb_agent_destroy_ue_config_request(Protocol__ProgranMessage *msg) {
  /* TODO: Deallocate memory for a dynamically allocated UE config message */
  return 0;
}

int enb_agent_destroy_lc_config_request(Protocol__ProgranMessage *msg) {
  /* TODO: Deallocate memory for a dynamically allocated LC config message */
  return 0;
}

443 444 445 446 447 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 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
// 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;
}

int enb_agent_control_delegation(mid_t mod_id, const void *params, Protocol__ProgranMessage **msg) {

  Protocol__ProgranMessage *input = (Protocol__ProgranMessage *)params;
  Protocol__PrpControlDelegation *control_delegation_msg = input->control_delegation_msg;

  uint32_t delegation_type = control_delegation_msg->delegation_type;

  void *lib;
  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];
  snprintf(lib_name, sizeof(lib_name), "/delegation_lib_%d.so", control_delegation_msg->header->xid);
  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);
  lib = dlopen(target, RTLD_NOW);
  if (lib == NULL) {
    goto error;
  }

  i = 0;
  //Check functions that need to be delegated
  
  //DL UE scheduler delegation
  if (delegation_type & PROTOCOL__PRP_CONTROL_DELEGATION_TYPE__PRCDT_MAC_DL_UE_SCHEDULER) {  
    void *loaded_scheduler = dlsym(lib, control_delegation_msg->name[i]);
    i++;
    if (loaded_scheduler) {
      if (mac_agent_registered[mod_id]) {
	agent_mac_xface[mod_id]->enb_agent_schedule_ue_spec = loaded_scheduler;
	LOG_D(ENB_APP,"Delegated control for DL UE scheduler successfully\n");
      }
    }
  }
  long time_elapsed_nanos = timer_end(vartime);
  LOG_I(ENB_AGENT, "DID IT IN %lld\n", time_elapsed_nanos);
  *msg = NULL;
  return 0;

 error:
  return -1;
}

int enb_agent_destroy_control_delegation(Protocol__ProgranMessage *msg) {
  /*TODO: Dealocate memory for a dynamically allocated control delegation message*/
}

513 514 515 516 517 518 519 520 521 522
/*
 * get generic info from RAN
 */

void set_enb_vars(mid_t mod_id, ran_name_t ran){

  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;
MKassem's avatar
MKassem committed
523
    enb_rrc[mod_id] = (void *)&eNB_rrc_inst[mod_id];
524
    enb_ue_rrc[mod_id] = (void *)&UE_rrc_inst[mod_id];
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
    break;
  default :
    goto error;
  }

  return; 

 error:
  LOG_E(ENB_AGENT, "unknown RAN name %d\n", ran);
}

int get_current_time_ms (mid_t mod_id, int subframe_flag){

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

546 547 548
unsigned int get_current_frame (mid_t mod_id) {

  #warning "SFN will not be in [0-1023] when oaisim is used"
549 550 551 552
  return ((eNB_MAC_INST *)enb[mod_id])->frame;
  
}

553 554 555 556 557
unsigned int get_current_system_frame_num(mid_t mod_id) {
  return (get_current_frame(mod_id) %1024);
}

unsigned int get_current_subframe (mid_t mod_id) {
558 559 560 561 562

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

563 564
uint16_t get_sfn_sf (mid_t mod_id) {
  
565 566 567 568 569 570 571 572 573
  frame_t frame;
  sub_frame_t subframe;
  uint16_t sfn_sf, frame_mask, sf_mask;
  
  frame = (frame_t) get_current_system_frame_num(mod_id);
  subframe = (sub_frame_t) get_current_subframe(mod_id);
  frame_mask = ((1<<12) - 1);
  sf_mask = ((1<<4) -1);
  sfn_sf = (subframe & sf_mask) | ((frame & frame_mask) << 4);
574 575 576 577
  
  return sfn_sf;
}

578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
int get_num_ues (mid_t mod_id){

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

int get_ue_crnti (mid_t mod_id, mid_t ue_id){

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

int get_ue_bsr (mid_t mod_id, mid_t ue_id, lcid_t lcid) {

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

int get_ue_phr (mid_t mod_id, mid_t ue_id) {

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

int get_ue_wcqi (mid_t mod_id, mid_t ue_id) {

  return ((UE_list_t *)enb_ue[mod_id])->eNB_UE_stats[UE_PCCID(mod_id,ue_id)][ue_id].dl_cqi;
}
602 603 604 605 606 607 608
int get_tx_queue_size(mid_t mod_id, mid_t ue_id, logical_chan_id_t channel_id)
{
	rnti_t rnti = get_ue_crnti(mod_id,ue_id);
	uint16_t frame = (uint16_t) get_current_frame(mod_id);
	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;
}
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633

int get_MAC_CE_bitmap_TA(mid_t mod_id, mid_t ue_id)
{
	if((((UE_list_t *)enb_ue[mod_id])->UE_sched_ctrl[ue_id].ta_update) > 0)
		return 1;
	else
		return 0;
}

int get_active_CC(mid_t mod_id, mid_t ue_id)
{
	return ((UE_list_t *)enb_ue[mod_id])->numactiveCCs[ue_id];
}

int get_current_RI(mid_t mod_id, mid_t ue_id, int CC_id)
{
	LTE_eNB_UE_stats *eNB_UE_stats = NULL;

	int pCCid = UE_PCCID(mod_id,ue_id);
	rnti_t rnti = 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[CC_id].rank;
}

MKassem's avatar
MKassem committed
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
int get_tpc(mid_t mod_id, mid_t ue_id)
{
	LTE_eNB_UE_stats *eNB_UE_stats = NULL;
	int32_t normalized_rx_power, target_rx_power;
	int tpc = 1;

	int pCCid = UE_PCCID(mod_id,ue_id);
	rnti_t rnti = get_ue_crnti(mod_id,ue_id);

	eNB_UE_stats =  mac_xface->get_eNB_UE_stats(mod_id, pCCid, rnti);

	normalized_rx_power = eNB_UE_stats->UL_rssi[0];

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

	if (normalized_rx_power>(target_rx_power+1)) {
		tpc = 0; //-1
	} else if (normalized_rx_power<(target_rx_power-1)) {
		tpc = 2; //+1
	} else {
		tpc = 1; //0
	}
	return tpc;
}

659
int 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 *status)	//flag_id_status = 0 then id, else status
MKassem's avatar
MKassem committed
660 661 662 663 664
{
	/*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];*/

665 666 667
  uint8_t harq_pid;
  uint8_t round;
  
MKassem's avatar
MKassem committed
668

669
  uint16_t rnti = get_ue_crnti(mod_id,ue_id);
MKassem's avatar
MKassem committed
670

671
  mac_xface->get_ue_active_harq_pid(mod_id,CC_id,rnti,frame,subframe,&harq_pid,&round,0);
MKassem's avatar
MKassem committed
672

673 674 675 676 677 678 679 680
  *id = harq_pid;
  if (round > 0) {
    *status = 1;
  } else {
    *status = 0;
  }

  return 0;
MKassem's avatar
MKassem committed
681 682
}

MKassem's avatar
MKassem committed
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893

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

int get_hopping_offset(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->pusch_config_common.pusch_HoppingOffset;
}
int get_hopping_mode(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->pusch_config_common.hoppingMode;
}
int get_n_SB(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->pusch_config_common.n_SB;
}
int get_enable64QAM(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->pusch_config_common.enable64QAM;
}
int get_phich_duration(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->phich_config_common.phich_duration;
}
int get_phich_resource(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);
	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;
}
int get_n1pucch_an(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->pucch_config_common.n1PUCCH_AN;
}
int get_nRB_CQI(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->pucch_config_common.nRB_CQI;
}
int get_deltaPUCCH_Shift(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->pucch_config_common.deltaPUCCH_Shift;
}
int get_prach_ConfigIndex(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->prach_config_common.prach_ConfigInfo.prach_ConfigIndex;
}
int get_prach_FreqOffset(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->prach_config_common.prach_ConfigInfo.prach_FreqOffset;
}
int get_maxHARQ_Msg3Tx(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->maxHARQ_Msg3Tx;
}
int get_ul_cyclic_prefix_length(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->Ncp_UL;
}
int get_dl_cyclic_prefix_length(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->Ncp;
}
int get_cell_id(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->Nid_cell;
}
int get_srs_BandwidthConfig(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->soundingrs_ul_config_common.srs_BandwidthConfig;
}
int get_srs_SubframeConfig(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->soundingrs_ul_config_common.srs_SubframeConfig;
}
int get_srs_MaxUpPts(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->soundingrs_ul_config_common.srs_MaxUpPts;
}
int get_N_RB_DL(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_RB_DL;
}
int get_N_RB_UL(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_RB_UL;
}
int get_subframe_assignment(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->tdd_config;
}
int get_special_subframe_assignment(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->tdd_config_S;
}
int get_ra_ResponseWindowSize(mid_t mod_id, int CC_id)
{
	Enb_properties_array_t *enb_properties;
	enb_properties = enb_config_get();
	return enb_properties->properties[mod_id]->rach_raResponseWindowSize[CC_id];
}
int get_mac_ContentionResolutionTimer(mid_t mod_id, int CC_id)
{
	Enb_properties_array_t *enb_properties;
	enb_properties = enb_config_get();
	return enb_properties->properties[mod_id]->rach_macContentionResolutionTimer[CC_id];
}
int get_duplex_mode(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);
	if(frame_parms->frame_type == 0)
		return 1;
	else if (frame_parms->frame_type == 1)
		return 0;

	return -1;
}
long get_si_window_length(mid_t mod_id, int CC_id)
{
	return  ((eNB_RRC_INST *)enb_rrc[mod_id])->carrier[CC_id].sib1->si_WindowLength;
}
int get_sib1_length(mid_t mod_id, int CC_id)
{
	return  ((eNB_RRC_INST *)enb_rrc[mod_id])->carrier[CC_id].sizeof_SIB1;
}
int get_num_pdcch_symb(mid_t mod_id, int CC_id)
{
	/*TODO: add these values to some struct in MAC
	LTE_UE_PDCCH *lte_ue_pdcch;
	lte_ue_pdcch = mac_xface->get_lte_ue_pdcch(mod_id, CC_id, mod_id);
	*/
	return (PHY_vars_UE_g[mod_id][CC_id]->lte_ue_pdcch_vars[mod_id]->num_pdcch_symbols);
}

894
/*
895 896 897
 * ************************************
 * Get Messages for UE Configuration Reply
 * ************************************
898 899
 */

900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
int get_time_alignment_timer(mid_t mod_id, mid_t ue_id)
{
	return	(((UE_RRC_INST *)enb_ue_rrc[ue_id])->mac_MainConfig[mod_id]->timeAlignmentTimerDedicated);
}

int get_meas_gap_config(mid_t mod_id, mid_t ue_id)
{
	if(((UE_RRC_INST *)enb_ue_rrc[ue_id])->measGapConfig[mod_id]->present == MeasGapConfig_PR_NOTHING)
		return 2;
	else if(((UE_RRC_INST *)enb_ue_rrc[ue_id])->measGapConfig[mod_id]->present == MeasGapConfig_PR_release)
		return 0;
	else if(((UE_RRC_INST *)enb_ue_rrc[ue_id])->measGapConfig[mod_id]->present == MeasGapConfig_PR_setup)
		return 1;

	return -1;
}

int get_meas_gap_config_offset(mid_t mod_id, mid_t ue_id)
{
	return (((UE_RRC_INST *)enb_ue_rrc[ue_id])->measGapConfig[mod_id]->choice.setup.gapOffset.present);
}

int get_ue_aggregated_max_bitrate_dl (mid_t mod_id, mid_t ue_id)
{
	return ((UE_list_t *)enb_ue[mod_id])->UE_sched_ctrl[ue_id].ue_AggregatedMaximumBitrateDL;
}

int get_ue_aggregated_max_bitrate_ul (mid_t mod_id, mid_t ue_id)
{
	return ((UE_list_t *)enb_ue[mod_id])->UE_sched_ctrl[ue_id].ue_AggregatedMaximumBitrateUL;
}

int get_half_duplex(mid_t ue_id)
{
	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;
}

int get_intra_sf_hopping(mid_t ue_id)
{
	uint8_t temp = 0;
	temp = (((UE_RRC_INST *)enb_ue_rrc[ue_id])->UECap->UE_EUTRA_Capability->featureGroupIndicators->buf);
	return (temp & ( 1 << (31)));
}

int get_type2_sb_1(mid_t ue_id)
{
	uint8_t temp = 0;
	temp = (((UE_RRC_INST *)enb_ue_rrc[ue_id])->UECap->UE_EUTRA_Capability->featureGroupIndicators->buf);
	return (temp & ( 1 << (11)));
}

int get_ue_category(mid_t ue_id)
{
	return (((UE_RRC_INST *)enb_ue_rrc[ue_id])->UECap->UE_EUTRA_Capability->ue_Category);
}

int get_res_alloc_type1(mid_t ue_id)
{
	uint8_t temp = 0;
	temp = (((UE_RRC_INST *)enb_ue_rrc[ue_id])->UECap->UE_EUTRA_Capability->featureGroupIndicators->buf);
	return (temp & ( 1 << (30)));
}

int get_ue_transmission_mode(mid_t mod_id, mid_t ue_id)
{
	return (((UE_RRC_INST *)enb_ue_rrc[ue_id])->physicalConfigDedicated[mod_id]->antennaInfo->choice.explicitValue.transmissionMode);
}

int get_tti_bundling(mid_t mod_id, mid_t ue_id)
{
	return (((UE_RRC_INST *)enb_ue_rrc[ue_id])->mac_MainConfig[mod_id]->ul_SCH_Config->ttiBundling);
}

int get_maxHARQ_TX(mid_t mod_id, mid_t ue_id)
{
	return (((UE_RRC_INST *)enb_ue_rrc[ue_id])->mac_MainConfig[mod_id]->ul_SCH_Config->maxHARQ_Tx);
}

int get_beta_offset_ack_index(mid_t mod_id, mid_t ue_id)
{
	return (((UE_RRC_INST *)enb_ue_rrc[ue_id])->physicalConfigDedicated[mod_id]->pusch_ConfigDedicated->betaOffset_ACK_Index);
}

int get_beta_offset_ri_index(mid_t mod_id, mid_t ue_id)
{
	return (((UE_RRC_INST *)enb_ue_rrc[ue_id])->physicalConfigDedicated[mod_id]->pusch_ConfigDedicated->betaOffset_RI_Index);
}

int get_beta_offset_cqi_index(mid_t mod_id, mid_t ue_id)
{
	return (((UE_RRC_INST *)enb_ue_rrc[ue_id])->physicalConfigDedicated[mod_id]->pusch_ConfigDedicated->betaOffset_CQI_Index);
}

int get_simultaneous_ack_nack_cqi(mid_t mod_id, mid_t ue_id)
{
	return (((UE_RRC_INST *)enb_ue_rrc[ue_id])->physicalConfigDedicated[mod_id]->cqi_ReportConfig->cqi_ReportPeriodic->choice.setup.simultaneousAckNackAndCQI);
}

int get_ack_nack_simultaneous_trans(mid_t mod_id,mid_t ue_id)
{
	return (((UE_RRC_INST *)enb_ue_rrc[ue_id])->sib2[mod_id]->radioResourceConfigCommon.soundingRS_UL_ConfigCommon.choice.setup.ackNackSRS_SimultaneousTransmission);
}

int get_aperiodic_cqi_rep_mode(mid_t mod_id,mid_t ue_id)
{
	return (((UE_RRC_INST *)enb_ue_rrc[ue_id])->physicalConfigDedicated[mod_id]->cqi_ReportConfig->cqi_ReportModeAperiodic);
}

int get_tdd_ack_nack_feedback(mid_t mod_id, mid_t ue_id)
{
	return (((UE_RRC_INST *)enb_ue_rrc[ue_id])->physicalConfigDedicated[mod_id]->pucch_ConfigDedicated->tdd_AckNackFeedbackMode);
}

int get_ack_nack_repetition_factor(mid_t mod_id, mid_t ue_id)
{
	return (((UE_RRC_INST *)enb_ue_rrc[ue_id])->physicalConfigDedicated[mod_id]->pucch_ConfigDedicated->ackNackRepetition.choice.setup.repetitionFactor);
}

int get_extended_bsr_size(mid_t mod_id, mid_t ue_id)
{
	//TODO: need to double check
	return (((UE_RRC_INST *)enb_ue_rrc[ue_id])->mac_MainConfig[mod_id]->ext2->mac_MainConfig_v1020->extendedBSR_Sizes_r10);
}

int get_ue_transmission_antenna(mid_t mod_id, mid_t ue_id)
{
	if(((UE_RRC_INST *)enb_ue_rrc[ue_id])->physicalConfigDedicated[mod_id]->antennaInfo->choice.explicitValue.ue_TransmitAntennaSelection.choice.setup == AntennaInfoDedicated__ue_TransmitAntennaSelection__setup_closedLoop)
		return 2;
	else if(((UE_RRC_INST *)enb_ue_rrc[ue_id])->physicalConfigDedicated[mod_id]->antennaInfo->choice.explicitValue.ue_TransmitAntennaSelection.choice.setup == AntennaInfoDedicated__ue_TransmitAntennaSelection__setup_openLoop)
		return 1;
	else
		return 0;
}

int get_lcg(mid_t ue_id, mid_t lc_id)
{
	return &UE_mac_inst[ue_id].logicalChannelConfig[lc_id]->ul_SpecificParameters->logicalChannelGroup;
}

int get_direction(mid_t ue_id, mid_t lc_id)
{
	/*TODO: fill with the value for the rest of LCID*/

	if(lc_id == CCCH | lc_id == DCCH)
		return 2;
	else if(lc_id == DTCH)
		return 1;
}


/*
 * timer primitives
 */

int enb_agent_lc_config_reply(mid_t mod_id, const void *params, Protocol__ProgranMessage **msg) {

  xid_t xid;
  Protocol__ProgranMessage *input = (Protocol__ProgranMessage *)params;
  Protocol__PrpLcConfigRequest *lc_config_request_msg = input->lc_config_request_msg;
  xid = (lc_config_request_msg->header)->xid;

  int i, j;
  Protocol__PrpHeader *header;
  if(prp_create_header(xid, PROTOCOL__PRP_TYPE__PRPT_GET_LC_CONFIG_REPLY, &header) != 0)
    goto error;

  Protocol__PrpLcConfigReply *lc_config_reply_msg;
  lc_config_reply_msg = malloc(sizeof(Protocol__PrpLcConfigReply));
  if(lc_config_reply_msg == NULL)
    goto error;
  protocol__prp_lc_config_reply__init(lc_config_reply_msg);
  lc_config_reply_msg->header = header;

  //TODO: Fill in the actual number of UEs that we are going to report LC configs about
  lc_config_reply_msg->n_lc_ue_config = get_num_ues(mod_id);

  Protocol__PrpLcUeConfig **lc_ue_config;
  if (lc_config_reply_msg->n_lc_ue_config > 0) {
    lc_ue_config = malloc(sizeof(Protocol__PrpLcUeConfig *) * lc_config_reply_msg->n_lc_ue_config);
    if (lc_ue_config == NULL) {
      goto error;
    }
    // Fill the config for each UE
    for (i = 0; i < lc_config_reply_msg->n_lc_ue_config; i++) {
      lc_ue_config[i] = malloc(sizeof(Protocol__PrpLcUeConfig));
      protocol__prp_lc_ue_config__init(lc_ue_config[i]);
      //TODO: Set the RNTI of the UE
      lc_ue_config[i]->has_rnti = 1;
      lc_ue_config[i]->rnti = get_ue_crnti(mod_id,i);
      //TODO: Set the number of LC configurations that will be reported for this UE
      lc_ue_config[i]->n_lc_config = 3;
      Protocol__PrpLcConfig **lc_config;
      if (lc_ue_config[i]->n_lc_config > 0) {
	lc_config = malloc(sizeof(Protocol__PrpLcConfig *) * lc_ue_config[i]->n_lc_config);
	if (lc_config == NULL) {
	  goto error;
	}
	for (j = 0; j < lc_ue_config[i]->n_lc_config; j++) {
	  lc_config[j] = malloc(sizeof(Protocol__PrpLcConfig));
	  protocol__prp_lc_config__init(lc_config[j]);
	  //TODO: Set the LC id
	  lc_config[j]->has_lcid = 1;
	  lc_config[j]->lcid = j+1;
	  //TODO: Set the LCG of the channel
	  lc_config[j]->has_lcg = 1;
	  lc_config[j]->lcg = get_lcg(i,j+1);
	  //TODO: Set the LC direction
	  lc_config[j]->has_direction = 1;
	  lc_config[j]->direction = get_direction(i,j+1);
	  //TODO: Bearer type. One of PRQBT_* values
	  lc_config[j]->has_qos_bearer_type = 1;
	  lc_config[j]->qos_bearer_type = PROTOCOL__PRP_QOS_BEARER_TYPE__PRQBT_NON_GBR;
	  //TODO: Set the QCI defined in TS 23.203, coded as defined in TS 36.413
	  // One less than the actual QCI value
	  lc_config[j]->has_qci = 1;
	  lc_config[j]->qci = 1;
	  if (lc_config[j]->direction == PROTOCOL__PRP_QOS_BEARER_TYPE__PRQBT_GBR) {
	    //TODO: Set the max bitrate (UL)
	    lc_config[j]->has_e_rab_max_bitrate_ul = 1;
	    lc_config[j]->e_rab_max_bitrate_ul = 1;
	    //TODO: Set the max bitrate (DL)
	    lc_config[j]->has_e_rab_max_bitrate_dl = 1;
	    lc_config[j]->e_rab_max_bitrate_dl = 1;
	    //TODO: Set the guaranteed bitrate (UL)
	    lc_config[j]->has_e_rab_guaranteed_bitrate_ul = 1;
	    lc_config[j]->e_rab_guaranteed_bitrate_ul = 1;
	    //TODO: Set the guaranteed bitrate (DL)
	    lc_config[j]->has_e_rab_guaranteed_bitrate_dl = 1;
	    lc_config[j]->e_rab_guaranteed_bitrate_dl = 1;
	  }
	}
	lc_ue_config[i]->lc_config = lc_config;
      }
    } // end for UE
    lc_config_reply_msg->lc_ue_config = lc_ue_config;
  } // lc_config_reply_msg->n_lc_ue_config > 0
  *msg = malloc(sizeof(Protocol__ProgranMessage));
  if (*msg == NULL)
    goto error;
  protocol__progran_message__init(*msg);
  (*msg)->msg_case = PROTOCOL__PROGRAN_MESSAGE__MSG_LC_CONFIG_REPLY_MSG;
  (*msg)->msg_dir = PROTOCOL__PROGRAN_DIRECTION__SUCCESSFUL_OUTCOME;
  (*msg)->lc_config_reply_msg = lc_config_reply_msg;

  return 0;

 error:
  // TODO: Need to make proper error handling
  if (header != NULL)
    free(header);
  if (lc_config_reply_msg != NULL)
    free(lc_config_reply_msg);
  if(*msg != NULL)
    free(*msg);
  //LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
  return -1;
}

/*
 * ************************************
 * UE Configuration Reply
 * ************************************
 */

int enb_agent_ue_config_reply(mid_t mod_id, const void *params, Protocol__ProgranMessage **msg) {

  xid_t xid;
  Protocol__ProgranMessage *input = (Protocol__ProgranMessage *)params;
  Protocol__PrpUeConfigRequest *ue_config_request_msg = input->ue_config_request_msg;
  xid = (ue_config_request_msg->header)->xid;

  int i;

  Protocol__PrpHeader *header;
  if(prp_create_header(xid, PROTOCOL__PRP_TYPE__PRPT_GET_UE_CONFIG_REPLY, &header) != 0)
    goto error;

  Protocol__PrpUeConfigReply *ue_config_reply_msg;
  ue_config_reply_msg = malloc(sizeof(Protocol__PrpUeConfigReply));
  if(ue_config_reply_msg == NULL)
    goto error;
  protocol__prp_ue_config_reply__init(ue_config_reply_msg);
  ue_config_reply_msg->header = header;

  //TODO: Fill in the actual number of UEs that are currently connected
  ue_config_reply_msg->n_ue_config = get_num_ues(mod_id);

  Protocol__PrpUeConfig **ue_config;
  if (ue_config_reply_msg->n_ue_config > 0) {
    ue_config = malloc(sizeof(Protocol__PrpUeConfig *) * ue_config_reply_msg->n_ue_config);
    if (ue_config == NULL) {
      goto error;
    }
    for (i = 0; i < ue_config_reply_msg->n_ue_config; i++) {
      ue_config[i] = malloc(sizeof(Protocol__PrpUeConfig *));
      protocol__prp_ue_config__init(ue_config[i]);
      //TODO: Set the RNTI of the ue with id i
      ue_config[i]->rnti = get_ue_crnti(mod_id,i);
      ue_config[i]->has_rnti = 1;
      //TODO: Set the DRX configuration (optional)
      //Not supported for now, so we do not set it

      //TODO: Set the time_alignment_timer
      ue_config[i]->time_alignment_timer = get_time_alignment_timer(mod_id,i);
      ue_config[i]->has_time_alignment_timer = 1;
      //TODO: Set the measurement gap configuration pattern
      ue_config[i]->meas_gap_config_pattern = get_meas_gap_config(mod_id,i);
      ue_config[i]->has_meas_gap_config_pattern = 1;
      //TODO: Set the measurement gap offset if applicable
      ue_config[i]->meas_gap_config_sf_offset = get_meas_gap_config_offset(mod_id,i);
      ue_config[i]->has_meas_gap_config_sf_offset = 1;
      //TODO: Set the SPS configuration (Optional)
      //Not supported for noe, so we do not set it

      //TODO: Set the SR configuration (Optional)
      //We do not set it for now

      //TODO: Set the CQI configuration (Optional)
      //We do not set it for now

      //TODO: Set the transmission mode
      ue_config[i]->transmission_mode = get_ue_transmission_mode(mod_id,i);
      ue_config[i]->has_transmission_mode = 1;

      //TODO: Set the aggregated bit-rate of the non-gbr bearer (UL)
      ue_config[i]->ue_aggregated_max_bitrate_ul = get_ue_aggregated_max_bitrate_ul(mod_id,i);
      ue_config[i]->has_ue_aggregated_max_bitrate_ul = 1;

      //TODO: Set the aggregated bit-rate of the non-gbr bearer (DL)
      ue_config[i]->ue_aggregated_max_bitrate_dl = get_ue_aggregated_max_bitrate_dl(mod_id,i);
      ue_config[i]->has_ue_aggregated_max_bitrate_dl = 1;

      //TODO: Set the UE capabilities
      Protocol__PrpUeCapabilities *capabilities;
      capabilities = malloc(sizeof(Protocol__PrpUeCapabilities));
      protocol__prp_ue_capabilities__init(capabilities);
      //TODO: Set half duplex (FDD operation)
      capabilities->has_half_duplex = 1;
      capabilities->half_duplex = get_half_duplex(i);
      //TODO: Set intra-frame hopping flag
      capabilities->has_intra_sf_hopping = 1;
      capabilities->intra_sf_hopping = get_intra_sf_hopping(i);
      //TODO: Set support for type 2 hopping with n_sb > 1
      capabilities->has_type2_sb_1 = 1;
      capabilities->type2_sb_1 = get_type2_sb_1(i);
      //TODO: Set ue category
      capabilities->has_ue_category = 1;
      capabilities->ue_category = get_ue_category(i);
      //TODO: Set UE support for resource allocation type 1
      capabilities->has_res_alloc_type1 = 1;
      capabilities->res_alloc_type1 = get_res_alloc_type1(i);
      //Set the capabilites to the message
      ue_config[i]->capabilities = capabilities;
      //TODO: Set UE transmission antenna. One of the PRUTA_* values
      ue_config[i]->has_ue_transmission_antenna = 1;
      ue_config[i]->ue_transmission_antenna = get_ue_transmission_antenna(mod_id,i);
      //TODO: Set tti bundling flag (See ts 36.321)
      ue_config[i]->has_tti_bundling = 1;
      ue_config[i]->tti_bundling = get_tti_bundling(mod_id,i);
      //TODO: Set the max HARQ retransmission for the UL
      ue_config[i]->has_max_harq_tx = 1;
      ue_config[i]->max_harq_tx = get_maxHARQ_TX(mod_id,i);
      //TODO: Fill beta_offset_ack_index (TS 36.213)
      ue_config[i]->has_beta_offset_ack_index = 1;
      ue_config[i]->beta_offset_ack_index = get_beta_offset_ack_index(mod_id,i);
      //TODO: Fill beta_offset_ri_index (TS 36.213)
      ue_config[i]->has_beta_offset_ri_index = 1;
      ue_config[i]->beta_offset_ri_index = get_beta_offset_ri_index(mod_id,i);
      //TODO: Fill beta_offset_cqi_index (TS 36.213)
      ue_config[i]->has_beta_offset_cqi_index = 1;
      ue_config[i]->beta_offset_cqi_index = get_beta_offset_cqi_index(mod_id,i);
      //TODO: Fill ack_nack_simultaneous_trans (TS 36.213)
      ue_config[i]->has_ack_nack_simultaneous_trans = 1;
      ue_config[i]->ack_nack_simultaneous_trans = get_ack_nack_simultaneous_trans(mod_id,i);
      //TODO: Fill simultaneous_ack_nack_cqi (TS 36.213)
      ue_config[i]->has_simultaneous_ack_nack_cqi = 1;
      ue_config[i]->simultaneous_ack_nack_cqi = get_simultaneous_ack_nack_cqi(mod_id,i);
      //TODO: Set PRACRM_* value regarding aperiodic cqi report mode
      ue_config[i]->has_aperiodic_cqi_rep_mode = 1;
      ue_config[i]->aperiodic_cqi_rep_mode = get_aperiodic_cqi_rep_mode(mod_id,i);
      //TODO: Set tdd_ack_nack_feedback
      ue_config[i]->has_tdd_ack_nack_feedback = 1;
      ue_config[i]->tdd_ack_nack_feedback = get_tdd_ack_nack_feedback(mod_id,i);
      //TODO: Set ack_nack_repetition factor
      ue_config[i]->has_ack_nack_repetition_factor = 1;
      ue_config[i]->ack_nack_repetition_factor = get_ack_nack_repetition_factor(mod_id,i);
      //TODO: Set extended BSR size
      ue_config[i]->has_extended_bsr_size = 1;
      ue_config[i]->extended_bsr_size = get_extended_bsr_size(mod_id,i);
      //TODO: Set carrier aggregation support (boolean)
      ue_config[i]->has_ca_support = 0;
      ue_config[i]->ca_support = 0;
      if(ue_config[i]->has_ca_support){
		  //TODO: Set cross carrier scheduling support (boolean)
		  ue_config[i]->has_cross_carrier_sched_support = 1;
		  ue_config[i]->cross_carrier_sched_support = 0;
		  //TODO: Set index of primary cell
		  ue_config[i]->has_pcell_carrier_index = 1;
		  ue_config[i]->pcell_carrier_index = 1;
		  //TODO: Set secondary cells configuration
		  // We do not set it for now. No carrier aggregation support

		  //TODO: Set deactivation timer for secondary cell
		  ue_config[i]->has_scell_deactivation_timer = 1;
		  ue_config[i]->scell_deactivation_timer = 1;
      }
    }
    ue_config_reply_msg->ue_config = ue_config;
  }
  *msg = malloc(sizeof(Protocol__ProgranMessage));
  if (*msg == NULL)
    goto error;
  protocol__progran_message__init(*msg);
  (*msg)->msg_case = PROTOCOL__PROGRAN_MESSAGE__MSG_UE_CONFIG_REPLY_MSG;
  (*msg)->msg_dir = PROTOCOL__PROGRAN_DIRECTION__SUCCESSFUL_OUTCOME;
  (*msg)->ue_config_reply_msg = ue_config_reply_msg;
  return 0;

 error:
  // TODO: Need to make proper error handling
  if (header != NULL)
    free(header);
  if (ue_config_reply_msg != NULL)
    free(ue_config_reply_msg);
  if(*msg != NULL)
    free(*msg);
  //LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
  return -1;
}


MKassem's avatar
MKassem committed
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
/*
 * ************************************
 * eNB Configuration Request and Reply
 * ************************************
 */

int enb_agent_enb_config_request(mid_t mod_id, const void* params, Protocol__ProgranMessage **msg) {

	Protocol__PrpHeader *header;
	xid_t xid = 1;
	if(prp_create_header(xid,PROTOCOL__PRP_TYPE__PRPT_GET_ENB_CONFIG_REQUEST, &header) != 0)
		goto error;

	Protocol__PrpEnbConfigRequest *enb_config_request_msg;
	enb_config_request_msg = malloc(sizeof(Protocol__PrpEnbConfigRequest));

	if(enb_config_request_msg == NULL)
		goto error;

	protocol__prp_enb_config_request__init(enb_config_request_msg);
	enb_config_request_msg->header = header;

	*msg = malloc(sizeof(Protocol__ProgranMessage));
	if(*msg == NULL)
		goto error;

	protocol__progran_message__init(*msg);
	(*msg)->msg_case = PROTOCOL__PROGRAN_MESSAGE__MSG_ENB_CONFIG_REQUEST_MSG;
	(*msg)->msg_dir = PROTOCOL__PROGRAN_DIRECTION__INITIATING_MESSAGE;
	(*msg)->enb_config_request_msg = enb_config_request_msg;
	return 0;

	error:
	  // TODO: Need to make proper error handling
	  if (header != NULL)
	    free(header);
	  if (enb_config_request_msg != NULL)
	    free(enb_config_request_msg);
	  if(*msg != NULL)
	    free(*msg);
	  //LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
	  return -1;
}

int enb_agent_enb_config_reply(mid_t mod_id, const void *params, Protocol__ProgranMessage **msg) {

	xid_t xid;
	Protocol__ProgranMessage *input = (Protocol__ProgranMessage *)params;
1385
	Protocol__PrpEnbConfigRequest *enb_config_req_msg = input->enb_config_request_msg;
MKassem's avatar
MKassem committed
1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598
	xid = (enb_config_req_msg->header)->xid;

	int i, j, k;
	int cc_id = 0;
	int enb_id = mod_id;


	Protocol__PrpHeader *header;
	if(prp_create_header(xid, PROTOCOL__PRP_TYPE__PRPT_GET_ENB_CONFIG_REPLY, &header) != 0)
		goto error;

	Protocol__PrpEnbConfigReply *enb_config_reply_msg;
	enb_config_reply_msg = malloc(sizeof(Protocol__PrpEnbConfigReply));
	if(enb_config_reply_msg == NULL)
		goto error;
	protocol__prp_enb_config_reply__init(enb_config_reply_msg);
	enb_config_reply_msg->header = header;

	enb_config_reply_msg->enb_id = mod_id;


	enb_config_reply_msg->n_cell_config = MAX_NUM_CCs;

	Protocol__PrpCellConfig **cell_conf;
	if(enb_config_reply_msg->n_cell_config > 0){
		cell_conf = malloc(sizeof(Protocol__PrpCellConfig *) * enb_config_reply_msg->n_cell_config);
		if(cell_conf == NULL)
			goto error;
		for(i = 0; i < enb_config_reply_msg->n_cell_config; i++){
			cell_conf[i] = malloc(sizeof(Protocol__PrpCellConfig));
			protocol__prp_cell_config__init(cell_conf[i]);
			//TODO: Fill in with actual value, the PCI of this cell
			cell_conf[i]->phy_cell_id = 1;
			cell_conf[i]->has_phy_cell_id = 1;
			//TODO: Fill in with actual value, the PLMN cell id of this cell
			cell_conf[i]->cell_id = get_cell_id(enb_id,i);
			cell_conf[i]->has_cell_id = 1;
			//TODO: Fill in with actual value, PUSCH resources in RBs for hopping
			cell_conf[i]->pusch_hopping_offset = get_hopping_offset(enb_id,i);
			cell_conf[i]->has_pusch_hopping_offset = 1;
			//TODO: Fill in with actual value
			cell_conf[i]->hopping_mode = get_hopping_mode(enb_id,i);
			cell_conf[i]->has_hopping_mode = 1;
			//TODO: Fill in with actual value, the number of subbands
			cell_conf[i]->n_sb = get_n_SB(enb_id,i);
			cell_conf[i]->has_n_sb = 1;
			//TODO: Fill in with actual value, The number of resource element groups used for PHICH. One of PRPR_
			cell_conf[i]->phich_resource = get_phich_resource(enb_id,i);
			cell_conf[i]->has_phich_resource = 1;
			//TODO: Fill in with actual value, one of the PRPD_ values
			cell_conf[i]->phich_duration = get_phich_duration(enb_id,i);
			cell_conf[i]->has_phich_duration = 1;
			//TODO: Fill in with actual value, See TS 36.211, section 6.9
			cell_conf[i]->init_nr_pdcch_ofdm_sym = get_num_pdcch_symb(enb_id,i);
			cell_conf[i]->has_init_nr_pdcch_ofdm_sym = 1;
			//TODO: Fill in with actual value
			Protocol__PrpSiConfig *si_config;
			si_config = malloc(sizeof(Protocol__PrpSiConfig));
			if(si_config == NULL)
				goto error;
			protocol__prp_si_config__init(si_config);
			//TODO: Fill in with actual value, Frame number to apply the SI configuration
			si_config->sfn = 1;
			si_config->has_sfn = 1;
			//TODO: Fill in with actual value, the length of SIB1 in bytes
			si_config->sib1_length = get_sib1_length(enb_id,i);
			si_config->has_sib1_length = 1;
			//TODO: Fill in with actual value, Scheduling window for all SIs in SF
			si_config->si_window_length = (uint32_t) get_si_window_length(enb_id,i);
			si_config->has_si_window_length = 1;
			//TODO: Fill in with actual value, the number of SI messages
			si_config->n_si_message=1;
			Protocol__PrpSiMessage **si_message;
			si_message = malloc(sizeof(Protocol__PrpSiMessage *) * si_config->n_si_message);
			if(si_message == NULL)
				goto error;
			for(j = 0; j < si_config->n_si_message; j++){
				si_message[j] = malloc(sizeof(Protocol__PrpSiMessage));
				if(si_message[j] == NULL)
					goto error;
				protocol__prp_si_message__init(si_message[j]);
				//TODO: Fill in with actual value, Periodicity of SI msg in radio frames
				si_message[j]->periodicity = 1;				//SIPeriod
				si_message[j]->has_periodicity = 1;
				//TODO: Fill in with actual value, rhe length of the SI message in bytes
				si_message[j]->length = 10;
				si_message[j]->has_length = 1;
			}
			if(si_config->n_si_message > 0){
				si_config->si_message = si_message;
			}
			cell_conf[i]->si_config = si_config;

			//TODO: Fill in with actual value, the DL transmission bandwidth in RBs
			cell_conf[i]->dl_bandwidth = get_N_RB_DL(enb_id,i);
			cell_conf[i]->has_dl_bandwidth = 1;
			//TODO: Fill in with actual value, the UL transmission bandwidth in RBs
			cell_conf[i]->ul_bandwidth = get_N_RB_UL(enb_id,i);
			cell_conf[i]->has_ul_bandwidth = 1;
			//TODO: Fill in with actual value, one of PRUCPL values
			cell_conf[i]->ul_cyclic_prefix_length = get_ul_cyclic_prefix_length(enb_id,i);
			cell_conf[i]->has_ul_cyclic_prefix_length = 1;
			//TODO: Fill in with actual value, one of PRUCPL values
			cell_conf[i]->dl_cyclic_prefix_length = get_dl_cyclic_prefix_length(enb_id,i);
			cell_conf[i]->has_dl_cyclic_prefix_length = 1;
			//TODO: Fill in with actual value, number of cell specific antenna ports
			cell_conf[i]->antenna_ports_count = 1;
			cell_conf[i]->has_antenna_ports_count = 1;
			//TODO: Fill in with actual value, one of PRDM values
			cell_conf[i]->duplex_mode = get_duplex_mode(enb_id,i);
			cell_conf[i]->has_duplex_mode = 1;
			//TODO: Fill in with actual value, DL/UL subframe assignment. TDD only
			cell_conf[i]->subframe_assignment = get_subframe_assignment(enb_id,i);
			cell_conf[i]->has_subframe_assignment = 1;
			//TODO: Fill in with actual value, TDD only. See TS 36.211, table 4.2.1
			cell_conf[i]->special_subframe_patterns = get_special_subframe_assignment(enb_id,i);
			cell_conf[i]->has_special_subframe_patterns = 1;
			//TODO: Fill in with actual value, The MBSFN radio frame period
			cell_conf[i]->n_mbsfn_subframe_config_rfperiod = 5;
			uint32_t *elem_rfperiod;
			elem_rfperiod = (uint32_t *) malloc(sizeof(uint32_t) *cell_conf[i]->n_mbsfn_subframe_config_rfperiod);
			if(elem_rfperiod == NULL)
				goto error;
			for(j = 0; j < cell_conf[i]->n_mbsfn_subframe_config_rfperiod; j++){
				elem_rfperiod[j] = 1;
			}
			cell_conf[i]->mbsfn_subframe_config_rfperiod = elem_rfperiod;

			//TODO: Fill in with actual value, The MBSFN radio frame offset
			cell_conf[i]->n_mbsfn_subframe_config_rfoffset = 5;
			uint32_t *elem_rfoffset;
			elem_rfoffset = (uint32_t *) malloc(sizeof(uint32_t) *cell_conf[i]->n_mbsfn_subframe_config_rfoffset);
			if(elem_rfoffset == NULL)
				goto error;
			for(j = 0; j < cell_conf[i]->n_mbsfn_subframe_config_rfoffset; j++){
				elem_rfoffset[j] = 1;
			}
			cell_conf[i]->mbsfn_subframe_config_rfoffset = elem_rfoffset;

			//TODO: Fill in with actual value, Bitmap indicating the MBSFN subframes
			cell_conf[i]->n_mbsfn_subframe_config_sfalloc = 5;
			uint32_t *elem_sfalloc;
			elem_sfalloc = (uint32_t *) malloc(sizeof(uint32_t) *cell_conf[i]->n_mbsfn_subframe_config_sfalloc);
			if(elem_sfalloc == NULL)
				goto error;
			for(j = 0; j < cell_conf[i]->n_mbsfn_subframe_config_sfalloc; j++){
				elem_sfalloc[j] = 1;
			}
			cell_conf[i]->mbsfn_subframe_config_sfalloc = elem_sfalloc;

			//TODO: Fill in with actual value, See TS 36.211, section 5.7.1
			cell_conf[i]->prach_config_index = get_prach_ConfigIndex(enb_id,i);
			cell_conf[i]->has_prach_config_index = 1;
			//TODO: Fill in with actual value, See TS 36.211, section 5.7.1
			cell_conf[i]->prach_freq_offset = get_prach_FreqOffset(enb_id,i);
			cell_conf[i]->has_prach_freq_offset = 1;
			//TODO: Fill in with actual value, Duration of RA response window in SF
			cell_conf[i]->ra_response_window_size = get_ra_ResponseWindowSize(enb_id,i);
			cell_conf[i]->has_ra_response_window_size = 1;
			//TODO: Fill in with actual value, Timer used for RA
			cell_conf[i]->mac_contention_resolution_timer = get_mac_ContentionResolutionTimer(enb_id,i);
			cell_conf[i]->has_mac_contention_resolution_timer = 1;
			//TODO: Fill in with actual value, See TS 36.321
			cell_conf[i]->max_harq_msg3tx = get_maxHARQ_Msg3Tx(enb_id,i);
			cell_conf[i]->has_max_harq_msg3tx = 1;
			//TODO: Fill in with actual value, See TS 36.213, section 10.1
			cell_conf[i]->n1pucch_an = get_n1pucch_an(enb_id,i);
			cell_conf[i]->has_n1pucch_an = 1;
			//TODO: Fill in with actual value, See TS 36.211, section 5.4
			cell_conf[i]->deltapucch_shift = get_deltaPUCCH_Shift(enb_id,i);
			cell_conf[i]->has_deltapucch_shift = 1;
			//TODO: Fill in with actual value, See TS 36.211, section 5.4
			cell_conf[i]->nrb_cqi = get_nRB_CQI(enb_id,i);
			cell_conf[i]->has_nrb_cqi = 1;
			//TODO: Fill in with actual value, See TS 36.211, table 5.5.3.3-1 and 2
			cell_conf[i]->srs_subframe_config = get_srs_SubframeConfig(enb_id,i);
			cell_conf[i]->has_srs_subframe_config = 1;
			//TODO: Fill in with actual value, See TS 36.211, section 5.5.3.2
			cell_conf[i]->srs_bw_config = get_srs_BandwidthConfig(enb_id,i);
			cell_conf[i]->has_srs_bw_config = 1;
			//TODO: Fill in with actual value, Boolean value. See TS 36.211, section 5.5.3.2. TDD only
			cell_conf[i]->srs_mac_up_pts = get_srs_MaxUpPts(enb_id,i);
			cell_conf[i]->has_srs_mac_up_pts = 1;
			//TODO: Fill in with actual value, One of the PREQ_ values
			cell_conf[i]->enable_64qam = get_enable64QAM(enb_id,i);
			cell_conf[i]->has_enable_64qam = 1;
			//TODO: Fill in with actual value, Carrier component index
			cell_conf[i]->carrier_index = i;
			cell_conf[i]->has_carrier_index = 1;
		}
		enb_config_reply_msg->cell_config=cell_conf;
	}
	*msg = malloc(sizeof(Protocol__ProgranMessage));
	if(*msg == NULL)
		goto error;
	protocol__progran_message__init(*msg);
	(*msg)->msg_case = PROTOCOL__PROGRAN_MESSAGE__MSG_ENB_CONFIG_REPLY_MSG;
	(*msg)->msg_dir = PROTOCOL__PROGRAN_DIRECTION__SUCCESSFUL_OUTCOME;
	(*msg)->enb_config_reply_msg = enb_config_reply_msg;
	return 0;

	 error:
	  // TODO: Need to make proper error handling
	 if (header != NULL)
	   free(header);
	 if (enb_config_reply_msg != NULL)
	   free(enb_config_reply_msg);
	 if(*msg != NULL)
	   free(*msg);
	 //LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
	 return -1;
}

1599 1600

//struct enb_agent_map agent_map;
1601
enb_agent_timer_instance_t timer_instance;
1602
err_code_t enb_agent_init_timer(void){
1603 1604
  
  LOG_I(ENB_AGENT, "init RB tree\n");
1605

1606
  RB_INIT(&timer_instance.enb_agent_head);
1607 1608 1609 1610 1611 1612 1613 1614 1615
 
  /*
    struct enb_agent_timer_element_s e;
  memset(&e, 0, sizeof(enb_agent_timer_element_t));
  RB_INSERT(enb_agent_map, &agent_map, &e); 
  */
 return PROTOCOL__PROGRAN_ERR__NO_ERR;
}

1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627
RB_GENERATE(enb_agent_map,enb_agent_timer_element_s, entry, enb_agent_compare_timer);

/* The timer_id might not be the best choice for the comparison */
int enb_agent_compare_timer(struct enb_agent_timer_element_s *a, struct enb_agent_timer_element_s *b){

  if (a->timer_id < b->timer_id) return -1;
  if (a->timer_id > b->timer_id) return 1;

  // equal timers
  return 0;
}

1628 1629 1630 1631 1632
err_code_t enb_agent_create_timer(uint32_t interval_sec,
				  uint32_t interval_usec,
				  agent_id_t     agent_id,
				  instance_t     instance,
				  uint32_t timer_type,
1633
				  xid_t xid,
1634 1635 1636 1637
				  enb_agent_timer_callback_t cb,
				  void*    timer_args,
				  long *timer_id){
  
1638 1639 1640
  struct enb_agent_timer_element_s *e = calloc(1, sizeof(*e));
  DevAssert(e != NULL);
  
1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658
//uint32_t timer_id;
  int ret=-1;
  
  if ((interval_sec == 0) && (interval_usec == 0 ))
    return TIMER_NULL;
  
  if (timer_type >= ENB_AGENT_TIMER_TYPE_MAX)
    return TIMER_TYPE_INVALIDE;
  
  if (timer_type  ==   ENB_AGENT_TIMER_TYPE_ONESHOT){ 
    ret = timer_setup(interval_sec, 
		      interval_usec, 
		      TASK_ENB_AGENT, 
		      instance, 
		      TIMER_ONE_SHOT,
		      timer_args,
		      timer_id);
    
1659
    e->type = TIMER_ONE_SHOT;
1660 1661 1662 1663 1664 1665 1666 1667 1668 1669
  }
  else if (timer_type  ==   ENB_AGENT_TIMER_TYPE_PERIODIC ){
    ret = timer_setup(interval_sec, 
		      interval_usec, 
		      TASK_ENB_AGENT, 
		      instance, 
		      TIMER_PERIODIC,
		      timer_args,
		      timer_id);
    
1670
    e->type = TIMER_PERIODIC;
1671 1672 1673 1674 1675 1676
  }
  
  if (ret < 0 ) {
    return TIMER_SETUP_FAILED; 
  }

1677 1678 1679 1680
  e->agent_id = agent_id;
  e->instance = instance;
  e->state = ENB_AGENT_TIMER_STATE_ACTIVE;
  e->timer_id = *timer_id;
1681 1682
  e->xid = xid;
  e->timer_args = timer_args; 
1683 1684 1685
  e->cb = cb;
  /*element should be a real pointer*/
  RB_INSERT(enb_agent_map, &timer_instance.enb_agent_head, e); 
1686
  
1687 1688 1689
  LOG_I(ENB_AGENT,"Created a new timer with id 0x%lx for agent %d, instance %d \n",
	e->timer_id, e->agent_id, e->instance);
 
1690 1691 1692 1693 1694 1695 1696 1697
  return 0; 
}

err_code_t enb_agent_destroy_timer(long timer_id){
  
  struct enb_agent_timer_element_s *e = get_timer_entry(timer_id);

  if (e != NULL ) {
1698 1699
    RB_REMOVE(enb_agent_map, &timer_instance.enb_agent_head, e);
    enb_agent_destroy_progran_message(e->timer_args->msg);
1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712
    free(e);
  }
  
  if (timer_remove(timer_id) < 0 ) 
    goto error;
  
  return 0;

 error:
  LOG_E(ENB_AGENT, "timer can't be removed\n");
  return TIMER_REMOVED_FAILED ;
}

1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733
err_code_t enb_agent_destroy_timer_by_task_id(xid_t xid) {
  struct enb_agent_timer_element_s *e = NULL;
  long timer_id;
  RB_FOREACH(e, enb_agent_map, &timer_instance.enb_agent_head) {
    if (e->xid == xid) {
      timer_id = e->timer_id;
      RB_REMOVE(enb_agent_map, &timer_instance.enb_agent_head, e);
      enb_agent_destroy_progran_message(e->timer_args->msg);
      free(e);
      if (timer_remove(timer_id) < 0 ) { 
	goto error;
      }
    }
  }
  return 0;

 error:
  LOG_E(ENB_AGENT, "timer can't be removed\n");
  return TIMER_REMOVED_FAILED ;
}

1734 1735 1736 1737
err_code_t enb_agent_destroy_timers(void){
  
  struct enb_agent_timer_element_s *e = NULL;
  
1738 1739
  RB_FOREACH(e, enb_agent_map, &timer_instance.enb_agent_head) {
    RB_REMOVE(enb_agent_map, &timer_instance.enb_agent_head, e);
1740 1741
    timer_remove(e->timer_id);
    enb_agent_destroy_progran_message(e->timer_args->msg);
1742 1743 1744 1745 1746 1747 1748
    free(e);
  }  

  return 0;

}

1749 1750 1751 1752 1753 1754 1755 1756 1757
void enb_agent_sleep_until(struct timespec *ts, int delay) {
  ts->tv_nsec += delay;
  if(ts->tv_nsec >= 1000*1000*1000){
    ts->tv_nsec -= 1000*1000*1000;
    ts->tv_sec++;
  }
  clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, ts,  NULL);
}

1758 1759 1760 1761 1762 1763 1764
/*
 int i =0;
  RB_FOREACH(e, enb_agent_map, &enb_agent_head) {
    printf("%d: %p\n", i, e); i++;
    }
*/

1765 1766

err_code_t enb_agent_stop_timer(long timer_id){
1767 1768
  
  struct enb_agent_timer_element_s *e=NULL;
1769 1770 1771 1772 1773
  struct enb_agent_timer_element_s search;
  memset(&search, 0, sizeof(struct enb_agent_timer_element_s));
  search.timer_id = timer_id;

  e = RB_FIND(enb_agent_map, &timer_instance.enb_agent_head, &search);
1774 1775 1776 1777 1778 1779 1780 1781

  if (e != NULL ) {
    e->state =  ENB_AGENT_TIMER_STATE_STOPPED;
  }
  
  timer_remove(timer_id);
  
  return 0;
1782 1783 1784 1785 1786 1787 1788
}

struct enb_agent_timer_element_s * get_timer_entry(long timer_id) {
  
  struct enb_agent_timer_element_s search;
  memset(&search, 0, sizeof(struct enb_agent_timer_element_s));
  search.timer_id = timer_id;
1789

1790
  return  RB_FIND(enb_agent_map, &timer_instance.enb_agent_head, &search); 
1791 1792
}

1793
/*
1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826
// this will change the timer_id
err_code_t enb_agent_restart_timer(uint32_t *timer_id){
  
  struct enb_agent_timer_element_s *e=NULL;
    
  RB_FOREACH(e, enb_agent_map, &enb_agent_head) {
    if (e->timer_id == timer_id)
      break;
  }

  if (e != NULL ) {
    e->state =  ENB_AGENT_TIMER_STATE_ACTIVE;
  }
  
  ret = timer_setup(e->interval_sec, 
		    e->interval_usec, 
		    e->agent_id, 
		    e->instance, 
		    e->type,
		    e->timer_args,
		    &timer_id);
    
  }

  if (ret < 0 ) {
    return PROTOCOL__PROGRAN_ERR__TIMER_SETUP_FAILED; 
  }
  
  return 0;

}

*/