enb_agent_mac.c 52.6 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_mac.c
 * \brief enb agent message handler for MAC layer
 * \author Navid Nikaein and Xenofon Foukas
33 34 35 36 37
 * \date 2016
 * \version 0.1
 */

#include "enb_agent_mac.h"
38 39 40
#include "enb_agent_extern.h"
#include "enb_agent_common.h"
#include "enb_agent_mac_internal.h"
41

42
#include "LAYER2/MAC/proto.h"
43
#include "LAYER2/MAC/enb_agent_mac_proto.h"
44
#include "LAYER2/MAC/eNB_agent_scheduler_dlsch_ue_remote.h"
45

46 47
#include "liblfds700.h"

48 49
#include "log.h"

50 51 52 53

/*Flags showing if a mac agent has already been registered*/
unsigned int mac_agent_registered[NUM_MAX_ENB];

54 55 56
/*Array containing the Agent-MAC interfaces*/
AGENT_MAC_xface *agent_mac_xface[NUM_MAX_ENB];

57 58 59 60 61 62 63
/* Ringbuffer related structs used for maintaining the dl mac config messages */
//message_queue_t *dl_mac_config_queue;
struct lfds700_misc_prng_state ps[NUM_MAX_ENB];
struct lfds700_ringbuffer_element *dl_mac_config_array[NUM_MAX_ENB];
struct lfds700_ringbuffer_state ringbuffer_state[NUM_MAX_ENB];


64
int enb_agent_mac_handle_stats(mid_t mod_id, const void *params, Protocol__ProgranMessage **msg){
65

66 67
  // TODO: Must deal with sanitization of input
  // TODO: Must check if RNTIs and cell ids of the request actually exist
68
  // TODO: Must resolve conflicts among stats requests
69

70
  int i;
71 72 73
  void *buffer;
  int size;
  err_code_t err_code;
74
  xid_t xid;
75
  uint32_t usec_interval, sec_interval;
76 77

  //TODO: We do not deal with multiple CCs at the moment and eNB id is 0
78
  int cc_id = 0;
79
  int enb_id = mod_id;
80

81 82
  //eNB_MAC_INST *eNB = &eNB_mac_inst[enb_id];
  //UE_list_t *eNB_UE_list=  &eNB->UE_list;
83

84 85 86 87 88
  report_config_t report_config;

  uint32_t ue_flags = 0;
  uint32_t c_flags = 0;

89 90 91
  Protocol__ProgranMessage *input = (Protocol__ProgranMessage *)params;

  Protocol__PrpStatsRequest *stats_req = input->stats_request_msg;
92
  xid = (stats_req->header)->xid;
93 94 95 96 97

  // Check the type of request that is made
  switch(stats_req->body_case) {
  case PROTOCOL__PRP_STATS_REQUEST__BODY_COMPLETE_STATS_REQUEST: ;
    Protocol__PrpCompleteStatsRequest *comp_req = stats_req->complete_stats_request;
98 99 100
    if (comp_req->report_frequency == PROTOCOL__PRP_STATS_REPORT_FREQ__PRSRF_OFF) {
      /*Disable both periodic and continuous updates*/
      enb_agent_disable_cont_mac_stats_update(mod_id);
101
      enb_agent_destroy_timer_by_task_id(xid);
102 103
      *msg = NULL;
      return 0;
104
    } else { //One-off, periodical or continuous reporting
105 106 107 108
      //Set the proper flags
      ue_flags = comp_req->ue_report_flags;
      c_flags = comp_req->cell_report_flags;
      //Create a list of all eNB RNTIs and cells
109

110
      //Set the number of UEs and create list with their RNTIs stats configs
111
      report_config.nr_ue = get_num_ues(mod_id); //eNB_UE_list->num_UEs
112 113 114 115 116 117 118
      report_config.ue_report_type = (ue_report_type_t *) malloc(sizeof(ue_report_type_t) * report_config.nr_ue);
      if (report_config.ue_report_type == NULL) {
	// TODO: Add appropriate error code
	err_code = -100;
	goto error;
      }
      for (i = 0; i < report_config.nr_ue; i++) {
119
	report_config.ue_report_type[i].ue_rnti = get_ue_crnti(enb_id, i); //eNB_UE_list->eNB_UE_stats[UE_PCCID(enb_id,i)][i].crnti;
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
	report_config.ue_report_type[i].ue_report_flags = ue_flags;
      }
      //Set the number of CCs and create a list with the cell stats configs
      report_config.nr_cc = MAX_NUM_CCs;
      report_config.cc_report_type = (cc_report_type_t *) malloc(sizeof(cc_report_type_t) * report_config.nr_cc);
      if (report_config.cc_report_type == NULL) {
	// TODO: Add appropriate error code
	err_code = -100;
	goto error;
      }
      for (i = 0; i < report_config.nr_cc; i++) {
	//TODO: Must fill in the proper cell ids
	report_config.cc_report_type[i].cc_id = i;
	report_config.cc_report_type[i].cc_report_flags = c_flags;
      }
135 136 137 138 139 140 141
      /* Check if request was periodical */
      if (comp_req->report_frequency == PROTOCOL__PRP_STATS_REPORT_FREQ__PRSRF_PERIODICAL) {
	/* Create a one off progran message as an argument for the periodical task */
	Protocol__ProgranMessage *timer_msg;
	stats_request_config_t request_config;
	request_config.report_type = PROTOCOL__PRP_STATS_TYPE__PRST_COMPLETE_STATS;
	request_config.report_frequency = PROTOCOL__PRP_STATS_REPORT_FREQ__PRSRF_ONCE;
142
	request_config.period = 0;
143 144 145 146 147 148 149 150 151 152 153 154
	/* Need to make sure that the ue flags are saved (Bug) */
	if (report_config.nr_ue == 0) {
	  report_config.nr_ue = 1;
	  report_config.ue_report_type = (ue_report_type_t *) malloc(sizeof(ue_report_type_t));
	   if (report_config.ue_report_type == NULL) {
	     // TODO: Add appropriate error code
	     err_code = -100;
	     goto error;
	   }
	   report_config.ue_report_type[0].ue_rnti = 0; // Dummy value
	   report_config.ue_report_type[0].ue_report_flags = ue_flags;
	}
155 156 157 158 159 160 161
	request_config.config = &report_config;
	enb_agent_mac_stats_request(enb_id, xid, &request_config, &timer_msg);
	/* Create a timer */
	long timer_id = 0;
	enb_agent_timer_args_t *timer_args;
	timer_args = malloc(sizeof(enb_agent_timer_args_t));
	memset (timer_args, 0, sizeof(enb_agent_timer_args_t));
162
	timer_args->mod_id = enb_id;
163 164 165 166 167 168 169 170 171
	timer_args->msg = timer_msg;
	/*Convert subframes to usec time*/
	usec_interval = 1000*comp_req->sf;
	sec_interval = 0;
	/*add seconds if required*/
	if (usec_interval >= 1000*1000) {
	  sec_interval = usec_interval/(1000*1000);
	  usec_interval = usec_interval%(1000*1000);
	}
172
	enb_agent_create_timer(sec_interval, usec_interval, ENB_AGENT_DEFAULT, enb_id, ENB_AGENT_TIMER_TYPE_PERIODIC, xid, enb_agent_handle_timed_task,(void*) timer_args, &timer_id);
173 174 175 176 177 178 179
      } else if (comp_req->report_frequency == PROTOCOL__PRP_STATS_REPORT_FREQ__PRSRF_CONTINUOUS) {
	/*If request was for continuous updates, disable the previous configuration and
	  set up a new one*/
	enb_agent_disable_cont_mac_stats_update(mod_id);
	stats_request_config_t request_config;
	request_config.report_type = PROTOCOL__PRP_STATS_TYPE__PRST_COMPLETE_STATS;
	request_config.report_frequency = PROTOCOL__PRP_STATS_REPORT_FREQ__PRSRF_ONCE;
180
	request_config.period = 0;
181 182 183 184 185 186 187 188 189 190 191 192
	/* Need to make sure that the ue flags are saved (Bug) */
	if (report_config.nr_ue == 0) {
	  report_config.nr_ue = 1;
	  report_config.ue_report_type = (ue_report_type_t *) malloc(sizeof(ue_report_type_t));
	  if (report_config.ue_report_type == NULL) {
	    // TODO: Add appropriate error code
	    err_code = -100;
	    goto error;
	  }
	  report_config.ue_report_type[0].ue_rnti = 0; // Dummy value
	  report_config.ue_report_type[0].ue_report_flags = ue_flags;
	}
193 194
	request_config.config = &report_config;
	enb_agent_enable_cont_mac_stats_update(enb_id, xid, &request_config);
195
      }
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
    }
    break;
  case PROTOCOL__PRP_STATS_REQUEST__BODY_CELL_STATS_REQUEST:;
    Protocol__PrpCellStatsRequest *cell_req = stats_req->cell_stats_request;
    // UE report config will be blank
    report_config.nr_ue = 0;
    report_config.ue_report_type = NULL;
    report_config.nr_cc = cell_req->n_cell;
    report_config.cc_report_type = (cc_report_type_t *) malloc(sizeof(cc_report_type_t) * report_config.nr_cc);
    if (report_config.cc_report_type == NULL) {
      // TODO: Add appropriate error code
      err_code = -100;
      goto error;
    }
    for (i = 0; i < report_config.nr_cc; i++) {
	//TODO: Must fill in the proper cell ids
      report_config.cc_report_type[i].cc_id = cell_req->cell[i];
      report_config.cc_report_type[i].cc_report_flags = cell_req->flags;
    }
    break;
  case PROTOCOL__PRP_STATS_REQUEST__BODY_UE_STATS_REQUEST:;
    Protocol__PrpUeStatsRequest *ue_req = stats_req->ue_stats_request;
    // Cell report config will be blank
    report_config.nr_cc = 0;
    report_config.cc_report_type = NULL;
    report_config.nr_ue = ue_req->n_rnti;
    report_config.ue_report_type = (ue_report_type_t *) malloc(sizeof(ue_report_type_t) * report_config.nr_ue);
    if (report_config.ue_report_type == NULL) {
      // TODO: Add appropriate error code
      err_code = -100;
      goto error;
    }
    for (i = 0; i < report_config.nr_ue; i++) {
      report_config.ue_report_type[i].ue_rnti = ue_req->rnti[i];
      report_config.ue_report_type[i].ue_report_flags = ue_req->flags;
    }
    break;
  default:
    //TODO: Add appropriate error code
    err_code = -100;
    goto error;
  }
238

239
  if (enb_agent_mac_stats_reply(enb_id, xid, &report_config, msg) < 0 ){
240 241 242 243
    err_code = PROTOCOL__PROGRAN_ERR__MSG_BUILD;
    goto error;
  }

244 245 246
  free(report_config.ue_report_type);
  free(report_config.cc_report_type);

247 248 249
  return 0;

 error :
250
  LOG_E(ENB_AGENT, "errno %d occured\n", err_code);
251 252 253
  return err_code;
}

254 255
int enb_agent_mac_stats_request(mid_t mod_id,
				xid_t xid,
256
				const stats_request_config_t *report_config,
257 258 259
				Protocol__ProgranMessage **msg) {
  Protocol__PrpHeader *header;
  int i;
260

261 262
  if (prp_create_header(xid, PROTOCOL__PRP_TYPE__PRPT_STATS_REQUEST, &header) != 0)
    goto error;
263

264 265 266 267 268 269 270
  Protocol__PrpStatsRequest *stats_request_msg;
  stats_request_msg = malloc(sizeof(Protocol__PrpStatsRequest));
  if(stats_request_msg == NULL)
    goto error;

  protocol__prp_stats_request__init(stats_request_msg);
  stats_request_msg->header = header;
271

272 273
  stats_request_msg->type = report_config->report_type;
  stats_request_msg->has_type = 1;
274

275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
  switch (report_config->report_type) {
  case PROTOCOL__PRP_STATS_TYPE__PRST_COMPLETE_STATS:
    stats_request_msg->body_case =  PROTOCOL__PRP_STATS_REQUEST__BODY_COMPLETE_STATS_REQUEST;
    Protocol__PrpCompleteStatsRequest *complete_stats;
    complete_stats = malloc(sizeof(Protocol__PrpCompleteStatsRequest));
    if(complete_stats == NULL)
      goto error;
    protocol__prp_complete_stats_request__init(complete_stats);
    complete_stats->report_frequency = report_config->report_frequency;
    complete_stats->has_report_frequency = 1;
    complete_stats->sf = report_config->period;
    complete_stats->has_sf = 1;
    complete_stats->has_cell_report_flags = 1;
    complete_stats->has_ue_report_flags = 1;
    if (report_config->config->nr_cc > 0) {
      complete_stats->cell_report_flags = report_config->config->cc_report_type[0].cc_report_flags;
    }
    if (report_config->config->nr_ue > 0) {
      complete_stats->ue_report_flags = report_config->config->ue_report_type[0].ue_report_flags;
    }
    stats_request_msg->complete_stats_request = complete_stats;
    break;
  case  PROTOCOL__PRP_STATS_TYPE__PRST_CELL_STATS:
    stats_request_msg->body_case = PROTOCOL__PRP_STATS_REQUEST__BODY_CELL_STATS_REQUEST;
     Protocol__PrpCellStatsRequest *cell_stats;
     cell_stats = malloc(sizeof(Protocol__PrpCellStatsRequest));
    if(cell_stats == NULL)
      goto error;
    protocol__prp_cell_stats_request__init(cell_stats);
    cell_stats->n_cell = report_config->config->nr_cc;
    cell_stats->has_flags = 1;
    if (cell_stats->n_cell > 0) {
      uint32_t *cells;
      cells = (uint32_t *) malloc(sizeof(uint32_t)*cell_stats->n_cell);
      for (i = 0; i < cell_stats->n_cell; i++) {
	cells[i] = report_config->config->cc_report_type[i].cc_id;
      }
      cell_stats->cell = cells;
      cell_stats->flags = report_config->config->cc_report_type[i].cc_report_flags;
    }
    stats_request_msg->cell_stats_request = cell_stats;
    break;
  case PROTOCOL__PRP_STATS_TYPE__PRST_UE_STATS:
    stats_request_msg->body_case = PROTOCOL__PRP_STATS_REQUEST__BODY_UE_STATS_REQUEST;
     Protocol__PrpUeStatsRequest *ue_stats;
     ue_stats = malloc(sizeof(Protocol__PrpUeStatsRequest));
    if(ue_stats == NULL)
      goto error;
    protocol__prp_ue_stats_request__init(ue_stats);
    ue_stats->n_rnti = report_config->config->nr_ue;
    ue_stats->has_flags = 1;
    if (ue_stats->n_rnti > 0) {
      uint32_t *ues;
      ues = (uint32_t *) malloc(sizeof(uint32_t)*ue_stats->n_rnti);
      for (i = 0; i < ue_stats->n_rnti; i++) {
	ues[i] = report_config->config->ue_report_type[i].ue_rnti;
      }
      ue_stats->rnti = ues;
      ue_stats->flags = report_config->config->ue_report_type[i].ue_report_flags;
    }
    stats_request_msg->ue_stats_request = ue_stats;
    break;
  default:
    goto error;
  }
  *msg = malloc(sizeof(Protocol__ProgranMessage));
  if(*msg == NULL)
    goto error;
  protocol__progran_message__init(*msg);
  (*msg)->msg_case = PROTOCOL__PROGRAN_MESSAGE__MSG_STATS_REQUEST_MSG;
  (*msg)->msg_dir = PROTOCOL__PROGRAN_DIRECTION__INITIATING_MESSAGE;
  (*msg)->stats_request_msg = stats_request_msg;
  return 0;
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
 error:
  // TODO: Need to make proper error handling
  if (header != NULL)
    free(header);
  if (stats_request_msg != NULL)
    free(stats_request_msg);
  if(*msg != NULL)
    free(*msg);
  //LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
  return -1;
}

int enb_agent_mac_destroy_stats_request(Protocol__ProgranMessage *msg) {
   if(msg->msg_case != PROTOCOL__PROGRAN_MESSAGE__MSG_STATS_REQUEST_MSG)
    goto error;
  free(msg->stats_request_msg->header);
  if (msg->stats_request_msg->body_case == PROTOCOL__PRP_STATS_REQUEST__BODY_CELL_STATS_REQUEST) {
    free(msg->stats_request_msg->cell_stats_request->cell);
  }
  if (msg->stats_request_msg->body_case == PROTOCOL__PRP_STATS_REQUEST__BODY_UE_STATS_REQUEST) {
    free(msg->stats_request_msg->ue_stats_request->rnti);
  }
  free(msg->stats_request_msg);
  free(msg);
  return 0;
374

375 376 377 378 379
 error:
  //LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
  return -1;
}

380 381
int enb_agent_mac_stats_reply(mid_t mod_id,
			      xid_t xid,
382
			      const report_config_t *report_config,
383 384 385 386
			      Protocol__ProgranMessage **msg) {
  Protocol__PrpHeader *header;
  int i, j, k;
  int cc_id = 0;
387 388 389
  int enb_id = mod_id;
  //eNB_MAC_INST *eNB = &eNB_mac_inst[enb_id];
  //UE_list_t *eNB_UE_list=  &eNB->UE_list;
390 391


392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
  if (prp_create_header(xid, PROTOCOL__PRP_TYPE__PRPT_STATS_REPLY, &header) != 0)
    goto error;

  Protocol__PrpStatsReply *stats_reply_msg;
  stats_reply_msg = malloc(sizeof(Protocol__PrpStatsReply));
  if (stats_reply_msg == NULL)
    goto error;
  protocol__prp_stats_reply__init(stats_reply_msg);
  stats_reply_msg->header = header;

  stats_reply_msg->n_ue_report = report_config->nr_ue;
  stats_reply_msg->n_cell_report = report_config->nr_cc;

  Protocol__PrpUeStatsReport **ue_report;
  Protocol__PrpCellStatsReport **cell_report;

408

409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
  /* Allocate memory for list of UE reports */
  if (report_config->nr_ue > 0) {
    ue_report = malloc(sizeof(Protocol__PrpUeStatsReport *) * report_config->nr_ue);
    if (ue_report == NULL)
      goto error;
    for (i = 0; i < report_config->nr_ue; i++) {
      ue_report[i] = malloc(sizeof(Protocol__PrpUeStatsReport));
      protocol__prp_ue_stats_report__init(ue_report[i]);
      ue_report[i]->rnti = report_config->ue_report_type[i].ue_rnti;
      ue_report[i]->has_rnti = 1;
      ue_report[i]->flags = report_config->ue_report_type[i].ue_report_flags;
      ue_report[i]->has_flags = 1;
      /* Check the types of reports that need to be constructed based on flag values */

      /* Check flag for creation of buffer status report */
      if (report_config->ue_report_type[i].ue_report_flags & PROTOCOL__PRP_UE_STATS_TYPE__PRUST_BSR) {
	//TODO: Create a report for each LCG (4 elements). See prp_ue_stats_report of
	// progRAN specifications for more details
	ue_report[i]->n_bsr = 4;
	uint32_t *elem;
	elem = (uint32_t *) malloc(sizeof(uint32_t)*ue_report[i]->n_bsr);
	if (elem == NULL)
	  goto error;
	for (j = 0; j++; j < ue_report[i]->n_bsr) {
	  // Set the actual BSR for LCG j of the current UE
	  // NN: we need to know the cc_id here, consider the first one
435
	  elem[j] = get_ue_bsr (enb_id, i, j); //eNB_UE_list->UE_template[UE_PCCID(enb_id,i)][i].bsr_info[j];
436 437 438
	}
	ue_report[i]->bsr = elem;
      }
439

440 441 442
      /* Check flag for creation of PRH report */
      if (report_config->ue_report_type[i].ue_report_flags & PROTOCOL__PRP_UE_STATS_TYPE__PRUST_PRH) {
	// TODO: Fill in the actual power headroom value for the RNTI
443
	ue_report[i]->phr = get_ue_phr (enb_id, i); // eNB_UE_list->UE_template[UE_PCCID(enb_id,i)][i].phr_info;
444 445 446 447 448 449
	ue_report[i]->has_phr = 1;
      }

      /* Check flag for creation of RLC buffer status report */
      if (report_config->ue_report_type[i].ue_report_flags & PROTOCOL__PRP_UE_STATS_TYPE__PRUST_RLC_BS) {
	// TODO: Fill in the actual RLC buffer status reports
450
	ue_report[i]->n_rlc_report = 3; // Set this to the number of LCs for this UE
451
	Protocol__PrpRlcBsr ** rlc_reports;
452
	rlc_reports = malloc(sizeof(Protocol__PrpRlcBsr *) * ue_report[i]->n_rlc_report);
453 454
	if (rlc_reports == NULL)
	  goto error;
455

456 457 458 459 460 461 462 463
	// Fill the buffer status report for each logical channel of the UE
	// NN: see LAYER2/openair2_proc.c for rlc status
	for (j = 0; j < ue_report[i]->n_rlc_report; j++) {
	  rlc_reports[j] = malloc(sizeof(Protocol__PrpRlcBsr));
	  if (rlc_reports[j] == NULL)
	    goto error;
	  protocol__prp_rlc_bsr__init(rlc_reports[j]);
	  //TODO:Set logical channel id
464
	  rlc_reports[j]->lc_id = j+1;
465 466
	  rlc_reports[j]->has_lc_id = 1;
	  //TODO:Set tx queue size in bytes
467
	  rlc_reports[j]->tx_queue_size = get_tx_queue_size(enb_id,i,j+1);
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
	  rlc_reports[j]->has_tx_queue_size = 1;
	  //TODO:Set tx queue head of line delay in ms
	  rlc_reports[j]->tx_queue_hol_delay = 100;
	  rlc_reports[j]->has_tx_queue_hol_delay = 1;
	  //TODO:Set retransmission queue size in bytes
	  rlc_reports[j]->retransmission_queue_size = 10;
	  rlc_reports[j]->has_retransmission_queue_size = 1;
	  //TODO:Set retransmission queue head of line delay in ms
	  rlc_reports[j]->retransmission_queue_hol_delay = 100;
	  rlc_reports[j]->has_retransmission_queue_hol_delay = 1;
	  //TODO:Set current size of the pending message in bytes
	  rlc_reports[j]->status_pdu_size = 100;
	  rlc_reports[j]->has_status_pdu_size = 1;
	}
	// Add RLC buffer status reports to the full report
	if (ue_report[i]->n_rlc_report > 0)
	  ue_report[i]->rlc_report = rlc_reports;
      }

      /* Check flag for creation of MAC CE buffer status report */
      if (report_config->ue_report_type[i].ue_report_flags & PROTOCOL__PRP_UE_STATS_TYPE__PRUST_MAC_CE_BS) {
	// TODO: Fill in the actual MAC CE buffer status report
490
	ue_report[i]->pending_mac_ces = (get_MAC_CE_bitmap_TA(enb_id,i,0) | (0 << 1) | (0 << 2) | (0 << 3)) & 15; /* Use as bitmap. Set one or more of the; /* Use as bitmap. Set one or more of the
491 492 493 494 495
					       PROTOCOL__PRP_CE_TYPE__PRPCET_ values
					       found in stats_common.pb-c.h. See
					       prp_ce_type in progRAN specification */
	ue_report[i]->has_pending_mac_ces = 1;
      }
496

497 498 499 500 501 502 503 504 505
      /* Check flag for creation of DL CQI report */
      if (report_config->ue_report_type[i].ue_report_flags & PROTOCOL__PRP_UE_STATS_TYPE__PRUST_DL_CQI) {
	// TODO: Fill in the actual DL CQI report for the UE based on its configuration
	Protocol__PrpDlCqiReport * dl_report;
	dl_report = malloc(sizeof(Protocol__PrpDlCqiReport));
	if (dl_report == NULL)
	  goto error;
	protocol__prp_dl_cqi_report__init(dl_report);
	//TODO:Set the SFN and SF of the last report held in the agent.
506
	dl_report->sfn_sn = get_sfn_sf(enb_id);
507 508
	dl_report->has_sfn_sn = 1;
	//TODO:Set the number of DL CQI reports for this UE. One for each CC
509
	dl_report->n_csi_report = get_active_CC(enb_id,i);
510 511
	//TODO:Create the actual CSI reports.
	Protocol__PrpDlCsi **csi_reports;
512
	csi_reports = malloc(sizeof(Protocol__PrpDlCsi *)*dl_report->n_csi_report);
513 514 515 516 517 518 519 520
	if (csi_reports == NULL)
	  goto error;
	for (j = 0; j < dl_report->n_csi_report; j++) {
	  csi_reports[j] = malloc(sizeof(Protocol__PrpDlCsi));
	  if (csi_reports[j] == NULL)
	    goto error;
	  protocol__prp_dl_csi__init(csi_reports[j]);
	  //TODO: the servCellIndex for this report
521
	  csi_reports[j]->serv_cell_index = j;
522 523
	  csi_reports[j]->has_serv_cell_index = 1;
	  //TODO: the rank indicator value for this cc
524
	  csi_reports[j]->ri = get_current_RI(enb_id,i,j);
525 526 527 528 529
	  csi_reports[j]->has_ri = 1;
	  //TODO: the type of CSI report based on the configuration of the UE
	  //For this example we use type P10, which only needs a wideband value
	  //The full set of types can be found in stats_common.pb-c.h and
	  //in the progRAN specifications
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
    csi_reports[j]->type =  PROTOCOL__PRP_CSI_TYPE__PRCSIT_P10;
		  csi_reports[j]->has_type = 1;
		  csi_reports[j]->report_case = PROTOCOL__PRP_DL_CSI__REPORT_P10CSI;
		  if(csi_reports[j]->report_case == PROTOCOL__PRP_DL_CSI__REPORT_P10CSI){
			  Protocol__PrpCsiP10 *csi10;
			  csi10 = malloc(sizeof(Protocol__PrpCsiP10));
			  if (csi10 == NULL)
				goto error;
			  protocol__prp_csi_p10__init(csi10);
			  //TODO: set the wideband value
			  // NN: this is also depends on cc_id
			  csi10->wb_cqi = get_ue_wcqi (enb_id, i); //eNB_UE_list->eNB_UE_stats[UE_PCCID(enb_id,i)][i].dl_cqi;
			  csi10->has_wb_cqi = 1;
			  //Add the type of measurements to the csi report in the proper union type
			  csi_reports[j]->p10csi = csi10;
		  }
		  else if(csi_reports[j]->report_case == PROTOCOL__PRP_DL_CSI__REPORT_P11CSI){

		  }
		  else if(csi_reports[j]->report_case == PROTOCOL__PRP_DL_CSI__REPORT_P20CSI){

		  }
		  else if(csi_reports[j]->report_case == PROTOCOL__PRP_DL_CSI__REPORT_P21CSI){

		  }
		  else if(csi_reports[j]->report_case == PROTOCOL__PRP_DL_CSI__REPORT_A12CSI){

		  }
		  else if(csi_reports[j]->report_case == PROTOCOL__PRP_DL_CSI__REPORT_A22CSI){

		  }
		  else if(csi_reports[j]->report_case == PROTOCOL__PRP_DL_CSI__REPORT_A20CSI){

		  }
		  else if(csi_reports[j]->report_case == PROTOCOL__PRP_DL_CSI__REPORT_A30CSI){

		  }
		  else if(csi_reports[j]->report_case == PROTOCOL__PRP_DL_CSI__REPORT_A31CSI){

		  }
		}
571 572 573 574 575
	//Add the csi reports to the full DL CQI report
	dl_report->csi_report = csi_reports;
	//Add the DL CQI report to the stats report
	ue_report[i]->dl_cqi_report = dl_report;
      }
576

577 578 579 580 581 582 583 584 585 586 587 588 589
      /* Check flag for creation of paging buffer status report */
      if (report_config->ue_report_type[i].ue_report_flags & PROTOCOL__PRP_UE_STATS_TYPE__PRUST_PBS) {
	//TODO: Fill in the actual paging buffer status report. For this field to be valid, the RNTI
	//set in the report must be a P-RNTI
	Protocol__PrpPagingBufferReport *paging_report;
	paging_report = malloc(sizeof(Protocol__PrpPagingBufferReport));
	if (paging_report == NULL)
	  goto error;
	protocol__prp_paging_buffer_report__init(paging_report);
	//Set the number of pending paging messages
	paging_report->n_paging_info = 1;
	//Provide a report for each pending paging message
	Protocol__PrpPagingInfo **p_info;
590
	p_info = malloc(sizeof(Protocol__PrpPagingInfo *) * paging_report->n_paging_info);
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
	if (p_info == NULL)
	  goto error;
	for (j = 0; j < paging_report->n_paging_info; j++) {
	  p_info[j] = malloc(sizeof(Protocol__PrpPagingInfo));
	  if(p_info[j] == NULL)
	    goto error;
	  protocol__prp_paging_info__init(p_info[j]);
	  //TODO: Set paging index. This index is the same that will be used for the scheduling of the
	  //paging message by the controller
	  p_info[j]->paging_index = 10;
	  p_info[j]->has_paging_index = 1;
	  //TODO:Set the paging message size
	  p_info[j]->paging_message_size = 100;
	  p_info[j]->has_paging_message_size = 1;
	  //TODO: Set the paging subframe
	  p_info[j]->paging_subframe = 10;
	  p_info[j]->has_paging_subframe = 1;
	  //TODO: Set the carrier index for the pending paging message
	  p_info[j]->carrier_index = 0;
	  p_info[j]->has_carrier_index = 1;
	}
	//Add all paging info to the paging buffer rerport
	paging_report->paging_info = p_info;
	//Add the paging report to the UE report
	ue_report[i]->pbr = paging_report;
      }
617

618 619 620 621 622 623 624 625 626
      /* Check flag for creation of UL CQI report */
      if (report_config->ue_report_type[i].ue_report_flags & PROTOCOL__PRP_UE_STATS_TYPE__PRUST_UL_CQI) {
	//Fill in the full UL CQI report of the UE
	Protocol__PrpUlCqiReport *full_ul_report;
	full_ul_report = malloc(sizeof(Protocol__PrpUlCqiReport));
	if(full_ul_report == NULL)
	  goto error;
	protocol__prp_ul_cqi_report__init(full_ul_report);
	//TODO:Set the SFN and SF of the generated report
627
	full_ul_report->sfn_sn = get_sfn_sf(enb_id);
628 629 630 631 632 633 634 635 636
	full_ul_report->has_sfn_sn = 1;
	//TODO:Set the number of UL measurement reports based on the types of measurements
	//configured for this UE and on the servCellIndex
	full_ul_report->n_cqi_meas = 1;
	Protocol__PrpUlCqi **ul_report;
	ul_report = malloc(sizeof(Protocol__PrpUlCqi *) * full_ul_report->n_cqi_meas);
	if(ul_report == NULL)
	  goto error;
	//Fill each UL report of the UE for each of the configured report types
637
	for(j = 0; j < full_ul_report->n_cqi_meas; j++) {
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
	  ul_report[j] = malloc(sizeof(Protocol__PrpUlCqi));
	  if(ul_report[j] == NULL)
	  goto error;
	  protocol__prp_ul_cqi__init(ul_report[j]);
	  //TODO: Set the type of the UL report. As an example set it to SRS UL report
	  // See enum prp_ul_cqi_type in progRAN specification for more details
	  ul_report[j]->type = PROTOCOL__PRP_UL_CQI_TYPE__PRUCT_SRS;
	  ul_report[j]->has_type = 1;
	  //TODO:Set the number of SINR measurements based on the report type
	  //See struct prp_ul_cqi in progRAN specification for more details
	  ul_report[j]->n_sinr = 100;
	  uint32_t *sinr_meas;
	  sinr_meas = (uint32_t *) malloc(sizeof(uint32_t) * ul_report[j]->n_sinr);
	  if (sinr_meas == NULL)
	    goto error;
	  //TODO:Set the SINR measurements for the specified type
	  for (k = 0; k < ul_report[j]->n_sinr; k++) {
	    sinr_meas[k] = 10;
	  }
	  ul_report[j]->sinr = sinr_meas;
	  //TODO: Set the servCellIndex for this report
	  ul_report[j]->serv_cell_index = 0;
	  ul_report[j]->has_serv_cell_index = 1;
661
	  
662 663
	  //Set the list of UL reports of this UE to the full UL report
	  full_ul_report->cqi_meas = ul_report;
664

665 666 667 668
	  full_ul_report->n_pucch_dbm = MAX_NUM_CCs;
	  full_ul_report->pucch_dbm = malloc(sizeof(Protocol__PrpPucchDbm *) * full_ul_report->n_pucch_dbm);

	  for (j = 0; j < MAX_NUM_CCs; j++) {
669 670 671 672 673 674 675 676
	    full_ul_report->pucch_dbm[j] = malloc(sizeof(Protocol__PrpPucchDbm));
	    protocol__prp_pucch_dbm__init(full_ul_report->pucch_dbm[j]);
	    full_ul_report->pucch_dbm[j]->has_serv_cell_index = 1;
	    full_ul_report->pucch_dbm[j]->serv_cell_index = j;
	    if(get_p0_pucch_dbm(enb_id,i, j) != -1){
	      full_ul_report->pucch_dbm[j]->p0_pucch_dbm = get_p0_pucch_dbm(enb_id,i,j);
	      full_ul_report->pucch_dbm[j]->has_p0_pucch_dbm = 1;
	    }
677 678
	  }

679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
	  //Add full UL CQI report to the UE report
	  ue_report[i]->ul_cqi_report = full_ul_report;
	}
      }
    }
    /* Add list of all UE reports to the message */
    stats_reply_msg->ue_report = ue_report;
  }

  /* Allocate memory for list of cell reports */
  if (report_config->nr_cc > 0) {
    cell_report = malloc(sizeof(Protocol__PrpCellStatsReport *) * report_config->nr_cc);
    if (cell_report == NULL)
      goto error;
    // Fill in the Cell reports
    for (i = 0; i < report_config->nr_cc; i++) {
      cell_report[i] = malloc(sizeof(Protocol__PrpCellStatsReport));
696
      if(cell_report[i] == NULL)
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
	goto error;
      protocol__prp_cell_stats_report__init(cell_report[i]);
      cell_report[i]->carrier_index = report_config->cc_report_type[i].cc_id;
      cell_report[i]->has_carrier_index = 1;
      cell_report[i]->flags = report_config->cc_report_type[i].cc_report_flags;
      cell_report[i]->has_flags = 1;

      /* Check flag for creation of noise and interference report */
      if(report_config->cc_report_type[i].cc_report_flags & PROTOCOL__PRP_CELL_STATS_TYPE__PRCST_NOISE_INTERFERENCE) {
	// TODO: Fill in the actual noise and interference report for this cell
	Protocol__PrpNoiseInterferenceReport *ni_report;
	ni_report = malloc(sizeof(Protocol__PrpNoiseInterferenceReport));
	if(ni_report == NULL)
	  goto error;
	protocol__prp_noise_interference_report__init(ni_report);
	// Current frame and subframe number
713
	ni_report->sfn_sf = get_sfn_sf(enb_id);
714 715 716 717 718 719 720
	ni_report->has_sfn_sf = 1;
	// Received interference power in dbm
	ni_report->rip = 0;
	ni_report->has_rip = 1;
	// Thermal noise power in dbm
	ni_report->tnp = 0;
	ni_report->has_tnp = 1;
721 722 723

	ni_report->p0_nominal_pucch = get_p0_nominal_pucch(enb_id, 0);
	ni_report->has_p0_nominal_pucch = 1;
724 725 726 727 728 729
	cell_report[i]->noise_inter_report = ni_report;
      }
    }
    /* Add list of all cell reports to the message */
    stats_reply_msg->cell_report = cell_report;
  }
730

731 732 733 734 735 736 737 738
  *msg = malloc(sizeof(Protocol__ProgranMessage));
  if(*msg == NULL)
    goto error;
  protocol__progran_message__init(*msg);
  (*msg)->msg_case = PROTOCOL__PROGRAN_MESSAGE__MSG_STATS_REPLY_MSG;
  (*msg)->msg_dir =  PROTOCOL__PROGRAN_DIRECTION__SUCCESSFUL_OUTCOME;
  (*msg)->stats_reply_msg = stats_reply_msg;
  return 0;
739

740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
 error:
  // TODO: Need to make proper error handling
  if (header != NULL)
    free(header);
  if (stats_reply_msg != NULL)
    free(stats_reply_msg);
  if(*msg != NULL)
    free(*msg);
  //LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
  return -1;
}

int enb_agent_mac_destroy_stats_reply(Protocol__ProgranMessage *msg) {
  //TODO: Need to deallocate memory for the stats reply message
  if(msg->msg_case != PROTOCOL__PROGRAN_MESSAGE__MSG_STATS_REPLY_MSG)
    goto error;
  free(msg->stats_reply_msg->header);
  int i, j, k;
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
  Protocol__PrpStatsReply *reply = msg->stats_reply_msg;
  Protocol__PrpDlCqiReport *dl_report;
  Protocol__PrpUlCqiReport *ul_report;
  Protocol__PrpPagingBufferReport *paging_report;

  // Free the memory for the UE reports
  for (i = 0; i < reply->n_ue_report; i++) {
    free(reply->ue_report[i]->bsr);
    for (j = 0; j < reply->ue_report[i]->n_rlc_report; j++) {
      free(reply->ue_report[i]->rlc_report[j]);
    }
    free(reply->ue_report[i]->rlc_report);
    // If DL CQI report flag was set
    if (reply->ue_report[i]->flags & PROTOCOL__PRP_UE_STATS_TYPE__PRUST_DL_CQI) {
      dl_report = reply->ue_report[i]->dl_cqi_report;
      // Delete all CSI reports
      for (j = 0; j < dl_report->n_csi_report; j++) {
	//Must free memory based on the type of report
	switch(dl_report->csi_report[j]->report_case) {
	case PROTOCOL__PRP_DL_CSI__REPORT_P10CSI:
	  free(dl_report->csi_report[j]->p10csi);
	  break;
	case PROTOCOL__PRP_DL_CSI__REPORT_P11CSI:
	  free(dl_report->csi_report[j]->p11csi->wb_cqi);
	  free(dl_report->csi_report[j]->p11csi);
	  break;
	case PROTOCOL__PRP_DL_CSI__REPORT_P20CSI:
	  free(dl_report->csi_report[j]->p20csi);
	  break;
	case PROTOCOL__PRP_DL_CSI__REPORT_P21CSI:
	  free(dl_report->csi_report[j]->p21csi->wb_cqi);
	  free(dl_report->csi_report[j]->p21csi->sb_cqi);
	  free(dl_report->csi_report[j]->p21csi);
	  break;
	case PROTOCOL__PRP_DL_CSI__REPORT_A12CSI:
	  free(dl_report->csi_report[j]->a12csi->wb_cqi);
	  free(dl_report->csi_report[j]->a12csi->sb_pmi);
	  free(dl_report->csi_report[j]->a12csi);
	  break;
	case PROTOCOL__PRP_DL_CSI__REPORT_A22CSI:
	  free(dl_report->csi_report[j]->a22csi->wb_cqi);
	  free(dl_report->csi_report[j]->a22csi->sb_cqi);
	  free(dl_report->csi_report[j]->a22csi->sb_list);
	  free(dl_report->csi_report[j]->a22csi);
	  break;
	case PROTOCOL__PRP_DL_CSI__REPORT_A20CSI:
	  free(dl_report->csi_report[j]->a20csi->sb_list);
	  free(dl_report->csi_report[j]->a20csi);
	  break;
	case PROTOCOL__PRP_DL_CSI__REPORT_A30CSI:
	  free(dl_report->csi_report[j]->a30csi->sb_cqi);
	  free(dl_report->csi_report[j]->a30csi);
	  break;
	case PROTOCOL__PRP_DL_CSI__REPORT_A31CSI:
	  free(dl_report->csi_report[j]->a31csi->wb_cqi);
	  for (k = 0; k < dl_report->csi_report[j]->a31csi->n_sb_cqi; k++) {
	    free(dl_report->csi_report[j]->a31csi->sb_cqi[k]);
	  }
	  free(dl_report->csi_report[j]->a31csi->sb_cqi);
	  break;
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
	free(dl_report->csi_report[j]);
      }
      free(dl_report->csi_report);
      free(dl_report);
    }
    // If Paging buffer report flag was set
    if (reply->ue_report[i]->flags & PROTOCOL__PRP_UE_STATS_TYPE__PRUST_PBS) {
      paging_report = reply->ue_report[i]->pbr;
      // Delete all paging buffer reports
      for (j = 0; j < paging_report->n_paging_info; j++) {
	free(paging_report->paging_info[j]);
      }
      free(paging_report->paging_info);
      free(paging_report);
    }
    // If UL CQI report flag was set
    if (reply->ue_report[i]->flags & PROTOCOL__PRP_UE_STATS_TYPE__PRUST_UL_CQI) {
      ul_report = reply->ue_report[i]->ul_cqi_report;
      for (j = 0; j < ul_report->n_cqi_meas; j++) {
	free(ul_report->cqi_meas[j]->sinr);
	free(ul_report->cqi_meas[j]);
      }
      free(ul_report->cqi_meas);
844 845 846 847
      for (j = 0; j < ul_report->n_pucch_dbm; j++) {
	free(ul_report->pucch_dbm[j]);
      }
      free(ul_report->pucch_dbm);
848 849 850 851 852 853 854 855 856 857 858
    }
    free(reply->ue_report[i]);
  }
  free(reply->ue_report);

  // Free memory for all Cell reports
  for (i = 0; i < reply->n_cell_report; i++) {
    free(reply->cell_report[i]->noise_inter_report);
    free(reply->cell_report[i]);
  }
  free(reply->cell_report);
859

860 861 862
  free(reply);
  free(msg);
  return 0;
863

864 865 866 867
 error:
  //LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
  return -1;
}
868 869 870 871 872 873 874 875 876 877 878 879 880 881

int enb_agent_mac_sr_info(mid_t mod_id, const void *params, Protocol__ProgranMessage **msg) {
  Protocol__PrpHeader *header;
  int i;
  const int xid = *((int *)params);
  if (prp_create_header(xid, PROTOCOL__PRP_TYPE__PRPT_UL_SR_INFO, &header) != 0)
    goto error;

  Protocol__PrpUlSrInfo *ul_sr_info_msg;
  ul_sr_info_msg = malloc(sizeof(Protocol__PrpUlSrInfo));
  if (ul_sr_info_msg == NULL) {
    goto error;
  }
  protocol__prp_ul_sr_info__init(ul_sr_info_msg);
882

883 884 885 886 887 888 889 890 891 892 893 894 895 896
  ul_sr_info_msg->header = header;
  ul_sr_info_msg->has_sfn_sf = 1;
  ul_sr_info_msg->sfn_sf = get_sfn_sf(mod_id);
  /*TODO: Set the number of UEs that sent an SR */
  ul_sr_info_msg->n_rnti = 1;
  ul_sr_info_msg->rnti = (uint32_t *) malloc(ul_sr_info_msg->n_rnti * sizeof(uint32_t));

  if(ul_sr_info_msg->rnti == NULL) {
    goto error;
  }
  /*TODO:Set the rnti of the UEs that sent an SR */
  for (i = 0; i < ul_sr_info_msg->n_rnti; i++) {
    ul_sr_info_msg->rnti[i] = 1;
  }
897

898 899 900 901 902 903 904 905
  *msg = malloc(sizeof(Protocol__ProgranMessage));
  if(*msg == NULL)
    goto error;
  protocol__progran_message__init(*msg);
  (*msg)->msg_case = PROTOCOL__PROGRAN_MESSAGE__MSG_UL_SR_INFO_MSG;
  (*msg)->msg_dir =  PROTOCOL__PROGRAN_DIRECTION__INITIATING_MESSAGE;
  (*msg)->ul_sr_info_msg = ul_sr_info_msg;
  return 0;
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
 error:
  // TODO: Need to make proper error handling
  if (header != NULL)
    free(header);
  if (ul_sr_info_msg != NULL) {
    free(ul_sr_info_msg->rnti);
    free(ul_sr_info_msg);
  }
  if(*msg != NULL)
    free(*msg);
  //LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
  return -1;
}

int enb_agent_mac_destroy_sr_info(Protocol__ProgranMessage *msg) {
   if(msg->msg_case != PROTOCOL__PROGRAN_MESSAGE__MSG_UL_SR_INFO_MSG)
     goto error;

   free(msg->ul_sr_info_msg->header);
   free(msg->ul_sr_info_msg->rnti);
   free(msg->ul_sr_info_msg);
   free(msg);
   return 0;

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

936
int enb_agent_mac_sf_trigger(mid_t mod_id, const void *params, Protocol__ProgranMessage **msg) {
937 938 939
  Protocol__PrpHeader *header;
  int i,j;
  const int xid = *((int *)params);
940
  if (prp_create_header(xid, PROTOCOL__PRP_TYPE__PRPT_SF_TRIGGER, &header) != 0)
941 942
    goto error;

943 944 945
  Protocol__PrpSfTrigger *sf_trigger_msg;
  sf_trigger_msg = malloc(sizeof(Protocol__PrpSfTrigger));
  if (sf_trigger_msg == NULL) {
946 947
    goto error;
  }
948
  protocol__prp_sf_trigger__init(sf_trigger_msg);
949

950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
  frame_t frame;
  sub_frame_t subframe;

  int ahead_of_time = 1;
  
  frame = (frame_t) get_current_system_frame_num(mod_id);
  subframe = (sub_frame_t) get_current_subframe(mod_id);

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

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

  if (subframe < get_current_subframe(mod_id)) {
    frame++;
  }

968 969
  sf_trigger_msg->header = header;
  sf_trigger_msg->has_sfn_sf = 1;
970
  sf_trigger_msg->sfn_sf = get_future_sfn_sf(mod_id, 1);
971 972 973 974

  /*TODO: Fill in the number of dl HARQ related info, based on the number of currently
   *transmitting UEs
   */
975
  sf_trigger_msg->n_dl_info = get_num_ues(mod_id);
976

977 978
  Protocol__PrpDlInfo **dl_info = NULL;

979 980
  if (sf_trigger_msg->n_dl_info > 0) {
    dl_info = malloc(sizeof(Protocol__PrpDlInfo *) * sf_trigger_msg->n_dl_info);
981 982 983
    if(dl_info == NULL)
      goto error;
    //Fill the status of the current HARQ process for each UE
984
    for(i = 0; i < sf_trigger_msg->n_dl_info; i++) {
985 986 987 988 989 990 991
      dl_info[i] = malloc(sizeof(Protocol__PrpDlInfo));
      if(dl_info[i] == NULL)
	goto error;
      protocol__prp_dl_info__init(dl_info[i]);
      dl_info[i]->rnti = get_ue_crnti(mod_id, i);
      dl_info[i]->has_rnti = 1;
      /*TODO: fill in the right id of this round's HARQ process for this UE*/
992 993
      int harq_id;
      int harq_status;
994
      get_harq(mod_id, UE_PCCID(mod_id,i), i, frame, subframe, &harq_id, &harq_status);
995
      dl_info[i]->harq_process_id = harq_id;
996 997 998 999
      dl_info[i]->has_harq_process_id = 1;
      /*TODO: fill in the status of the HARQ process (2 TBs)*/
      dl_info[i]->n_harq_status = 2;
      dl_info[i]->harq_status = malloc(sizeof(uint32_t) * dl_info[i]->n_harq_status);
1000
      for (j = 0; j < dl_info[i]->n_harq_status; j++) {
1001
	// TODO: This should be different per TB
1002 1003 1004 1005
	if(harq_status == 0)
	  dl_info[i]->harq_status[j] = PROTOCOL__PRP_HARQ_STATUS__PRHS_ACK;
	else if (harq_status == 1)
	  dl_info[i]->harq_status[j] = PROTOCOL__PRP_HARQ_STATUS__PRHS_NACK;
1006 1007
      }
      /*TODO: fill in the serving cell index for this UE */
MKassem's avatar
MKassem committed
1008
      dl_info[i]->serv_cell_index = UE_PCCID(mod_id,i);
1009 1010 1011
      dl_info[i]->has_serv_cell_index = 1;
    }
  }
1012

1013
  sf_trigger_msg->dl_info = dl_info;
1014

1015
    /*TODO: Fill in the number of UL reception status related info, based on the number of currently
1016 1017
   *transmitting UEs
   */
1018
  sf_trigger_msg->n_ul_info = get_num_ues(mod_id);
1019

1020 1021
  Protocol__PrpUlInfo **ul_info = NULL;

1022 1023
  if (sf_trigger_msg->n_ul_info > 0) {
    ul_info = malloc(sizeof(Protocol__PrpUlInfo *) * sf_trigger_msg->n_ul_info);
1024 1025 1026
    if(ul_info == NULL)
      goto error;
    //Fill the reception info for each transmitting UE
1027
    for(i = 0; i < sf_trigger_msg->n_ul_info; i++) {
1028 1029 1030 1031 1032 1033 1034
      ul_info[i] = malloc(sizeof(Protocol__PrpUlInfo));
      if(ul_info[i] == NULL)
	goto error;
      protocol__prp_ul_info__init(ul_info[i]);
      ul_info[i]->rnti = get_ue_crnti(mod_id, i);
      ul_info[i]->has_rnti = 1;
      /*TODO: fill in the Tx power control command for this UE (if available)*/
MKassem's avatar
MKassem committed
1035 1036 1037 1038 1039 1040 1041 1042
      if(get_tpc(mod_id,i) != 1){
    	  ul_info[i]->tpc = get_tpc(mod_id,i);
          ul_info[i]->has_tpc = 1;
      }
      else{
    	  ul_info[i]->tpc = get_tpc(mod_id,i);
    	  ul_info[i]->has_tpc = 0;
      }
1043
      /*TODO: fill in the amount of data in bytes in the MAC SDU received in this subframe for the
1044 1045 1046
	given logical channel*/
      ul_info[i]->n_ul_reception = 11;
      ul_info[i]->ul_reception = malloc(sizeof(uint32_t) * ul_info[i]->n_ul_reception);
1047
      for (j = 0; j < ul_info[i]->n_ul_reception; j++) {
1048 1049 1050 1051 1052 1053
	ul_info[i]->ul_reception[j] = 100;
      }
      /*TODO: Fill in the reception status for each UEs data*/
      ul_info[i]->reception_status = PROTOCOL__PRP_RECEPTION_STATUS__PRRS_OK;
      ul_info[i]->has_reception_status = 1;
      /*TODO: fill in the serving cell index for this UE */
MKassem's avatar
MKassem committed
1054
      ul_info[i]->serv_cell_index = UE_PCCID(mod_id,i);
1055 1056 1057
      ul_info[i]->has_serv_cell_index = 1;
    }
  }
1058

1059 1060
  sf_trigger_msg->ul_info = ul_info;

1061 1062 1063 1064
  *msg = malloc(sizeof(Protocol__ProgranMessage));
  if(*msg == NULL)
    goto error;
  protocol__progran_message__init(*msg);
1065
  (*msg)->msg_case = PROTOCOL__PROGRAN_MESSAGE__MSG_SF_TRIGGER_MSG;
1066
  (*msg)->msg_dir =  PROTOCOL__PROGRAN_DIRECTION__INITIATING_MESSAGE;
1067
  (*msg)->sf_trigger_msg = sf_trigger_msg;
1068
  return 0;
1069

1070 1071 1072
 error:
  if (header != NULL)
    free(header);
1073 1074 1075 1076 1077 1078 1079
  if (sf_trigger_msg != NULL) {
    for (i = 0; i < sf_trigger_msg->n_dl_info; i++) {
      free(sf_trigger_msg->dl_info[i]->harq_status);
    }
    free(sf_trigger_msg->dl_info);
    for (i = 0; i < sf_trigger_msg->n_ul_info; i++) {
      free(sf_trigger_msg->ul_info[i]->reception_status);
1080
    }
1081 1082
    free(sf_trigger_msg->ul_info);
    free(sf_trigger_msg);
1083 1084 1085 1086 1087 1088 1089
  }
  if(*msg != NULL)
    free(*msg);
  //LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
  return -1;
}

1090
int enb_agent_mac_destroy_sf_trigger(Protocol__ProgranMessage *msg) {
1091
  int i;
1092
  if(msg->msg_case != PROTOCOL__PROGRAN_MESSAGE__MSG_SF_TRIGGER_MSG)
1093
    goto error;
1094

1095 1096 1097
  free(msg->sf_trigger_msg->header);
  for (i = 0; i < msg->sf_trigger_msg->n_dl_info; i++) {
    free(msg->sf_trigger_msg->dl_info[i]->harq_status);
1098
    free(msg->sf_trigger_msg->dl_info[i]);
1099
  }
1100 1101
  free(msg->sf_trigger_msg->dl_info);
  for (i = 0; i < msg->sf_trigger_msg->n_ul_info; i++) {
1102 1103
    free(msg->sf_trigger_msg->ul_info[i]->ul_reception);
    free(msg->sf_trigger_msg->ul_info[i]);
1104 1105 1106
  }
  free(msg->sf_trigger_msg->ul_info);
  free(msg->sf_trigger_msg);
1107
  free(msg);
1108

1109 1110 1111 1112 1113 1114 1115
  return 0;

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

1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
int enb_agent_mac_create_empty_dl_config(mid_t mod_id, Protocol__ProgranMessage **msg) {

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

  Protocol__PrpDlMacConfig *dl_mac_config_msg;
  dl_mac_config_msg = malloc(sizeof(Protocol__PrpDlMacConfig));
  if (dl_mac_config_msg == NULL) {
    goto error;
  }
  protocol__prp_dl_mac_config__init(dl_mac_config_msg);
1129

1130 1131 1132
  dl_mac_config_msg->header = header;
  dl_mac_config_msg->has_sfn_sf = 1;
  dl_mac_config_msg->sfn_sf = get_sfn_sf(mod_id);
1133

1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
  *msg = malloc(sizeof(Protocol__ProgranMessage));
  if(*msg == NULL)
    goto error;
  protocol__progran_message__init(*msg);
  (*msg)->msg_case = PROTOCOL__PROGRAN_MESSAGE__MSG_DL_MAC_CONFIG_MSG;
  (*msg)->msg_dir =  PROTOCOL__PROGRAN_DIRECTION__INITIATING_MESSAGE;
  (*msg)->dl_mac_config_msg = dl_mac_config_msg;

  return 0;

 error:
  return -1;
}

int enb_agent_mac_destroy_dl_config(Protocol__ProgranMessage *msg) {
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
  int i,j, k;
  if(msg->msg_case != PROTOCOL__PROGRAN_MESSAGE__MSG_DL_MAC_CONFIG_MSG)
    goto error;

  Protocol__PrpDlDci *dl_dci;

  free(msg->dl_mac_config_msg->header);
  for (i = 0; i < msg->dl_mac_config_msg->n_dl_ue_data; i++) {
    free(msg->dl_mac_config_msg->dl_ue_data[i]->ce_bitmap);
    for (j = 0; j < msg->dl_mac_config_msg->dl_ue_data[i]->n_rlc_pdu; j++) {
      for (k = 0; k <  msg->dl_mac_config_msg->dl_ue_data[i]->rlc_pdu[j]->n_rlc_pdu_tb; k++) {
	free(msg->dl_mac_config_msg->dl_ue_data[i]->rlc_pdu[j]->rlc_pdu_tb[k]);
      }
      free(msg->dl_mac_config_msg->dl_ue_data[i]->rlc_pdu[j]->rlc_pdu_tb);
      free(msg->dl_mac_config_msg->dl_ue_data[i]->rlc_pdu[j]);
    }
    free(msg->dl_mac_config_msg->dl_ue_data[i]->rlc_pdu);
    dl_dci = msg->dl_mac_config_msg->dl_ue_data[i]->dl_dci;
    free(dl_dci->tbs_size);
    free(dl_dci->mcs);
    free(dl_dci->ndi);
    free(dl_dci->rv);
    free(dl_dci);
    free(msg->dl_mac_config_msg->dl_ue_data[i]);
  }
  free(msg->dl_mac_config_msg->dl_ue_data);
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
  for (i = 0; i <  msg->dl_mac_config_msg->n_dl_rar; i++) {
    dl_dci = msg->dl_mac_config_msg->dl_rar[i]->rar_dci;
    free(dl_dci->tbs_size);
    free(dl_dci->mcs);
    free(dl_dci->ndi);
    free(dl_dci->rv);
    free(dl_dci);
    free(msg->dl_mac_config_msg->dl_rar[i]);
  }
  free(msg->dl_mac_config_msg->dl_rar);

  for (i = 0; i < msg->dl_mac_config_msg->n_dl_broadcast; i++) {
    dl_dci = msg->dl_mac_config_msg->dl_broadcast[i]->broad_dci;
    free(dl_dci->tbs_size);
    free(dl_dci->mcs);
    free(dl_dci->ndi);
    free(dl_dci->rv);
    free(dl_dci);
    free(msg->dl_mac_config_msg->dl_broadcast[i]);
  }
  free(msg->dl_mac_config_msg->dl_broadcast);

    for ( i = 0; i < msg->dl_mac_config_msg->n_ofdm_sym; i++) {
    free(msg->dl_mac_config_msg->ofdm_sym[i]);
  }
  free(msg->dl_mac_config_msg->ofdm_sym);
1202 1203
  free(msg->dl_mac_config_msg);
  free(msg);
1204 1205 1206 1207

  return 0;

 error:
1208 1209 1210
  return -1;
}

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
void enb_agent_get_pending_dl_mac_config(mid_t mod_id, Protocol__ProgranMessage **msg) {

  struct lfds700_misc_prng_state ls;
  
  LFDS700_MISC_MAKE_VALID_ON_CURRENT_LOGICAL_CORE_INITS_COMPLETED_BEFORE_NOW_ON_ANY_OTHER_LOGICAL_CORE;
  lfds700_misc_prng_init(&ls);
  
  if (lfds700_ringbuffer_read(&ringbuffer_state[mod_id], NULL, (void **) msg, &ls) == 0) {
    *msg = NULL;
  }
}

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

  struct lfds700_misc_prng_state ls;
  enum lfds700_misc_flag overwrite_occurred_flag;
  Protocol__ProgranMessage *overwritten_dl_config;
   
  LFDS700_MISC_MAKE_VALID_ON_CURRENT_LOGICAL_CORE_INITS_COMPLETED_BEFORE_NOW_ON_ANY_OTHER_LOGICAL_CORE;
  lfds700_misc_prng_init(&ls);
  
  lfds700_ringbuffer_write( &ringbuffer_state[mod_id],
			    NULL,
			    (void *) params,
			    &overwrite_occurred_flag,
			    NULL,
			    (void **)&overwritten_dl_config,
			    &ls);

  if (overwrite_occurred_flag == LFDS700_MISC_FLAG_RAISED) {
    // Delete unmanaged dl_config
    enb_agent_mac_destroy_dl_config(overwritten_dl_config);
  }
  *msg = NULL;
  return 2;

 error:
  *msg = NULL;
  return -1;
}

void enb_agent_init_mac_agent(mid_t mod_id) {
  lfds700_misc_library_init_valid_on_current_logical_core();
  lfds700_misc_prng_init(&ps[mod_id]);
  int num_elements = RINGBUFFER_SIZE + 1;
  //Allow RINGBUFFER_SIZE messages to be stored in the ringbuffer at any time
  dl_mac_config_array[mod_id] = malloc( sizeof(struct lfds700_ringbuffer_element) *  num_elements);
  lfds700_ringbuffer_init_valid_on_current_logical_core( &ringbuffer_state[mod_id], dl_mac_config_array[mod_id], num_elements, &ps[mod_id], NULL );
}

1261 1262 1263 1264 1265
/***********************************************
 * eNB agent - technology mac API implementation
 ***********************************************/

void enb_agent_send_sr_info(mid_t mod_id) {
1266 1267 1268 1269 1270 1271
  int size;
  Protocol__ProgranMessage *msg;
  void *data;
  int priority;
  err_code_t err_code;

1272 1273
  int xid = 0;

1274
  /*TODO: Must use a proper xid*/
1275
  err_code = enb_agent_mac_sr_info(mod_id, (void *) &xid, &msg);
1276 1277 1278 1279 1280 1281
  if (err_code < 0) {
    goto error;
  }

  if (msg != NULL){
    data=enb_agent_pack_message(msg, &size);
1282 1283
    /*Send sr info using the MAC channel of the eNB*/
    if (enb_agent_msg_send(mod_id, ENB_AGENT_MAC, data, size, priority)) {
1284 1285 1286
      err_code = PROTOCOL__PROGRAN_ERR__MSG_ENQUEUING;
      goto error;
    }
1287

1288
    LOG_D(ENB_AGENT,"sent message with size %d\n", size);
1289
    return;
1290 1291 1292 1293 1294
  }
 error:
  LOG_D(ENB_AGENT, "Could not send sr message\n");
}

1295
void enb_agent_send_sf_trigger(mid_t mod_id) {
1296 1297 1298 1299 1300 1301
  int size;
  Protocol__ProgranMessage *msg;
  void *data;
  int priority;
  err_code_t err_code;

1302
  int xid = 0;
1303

1304
  /*TODO: Must use a proper xid*/
1305
  err_code = enb_agent_mac_sf_trigger(mod_id, (void *) &xid, &msg);
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318
  if (err_code < 0) {
    goto error;
  }

  if (msg != NULL){
    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_MAC, data, size, priority)) {
      err_code = PROTOCOL__PROGRAN_ERR__MSG_ENQUEUING;
      goto error;
    }

    LOG_D(ENB_AGENT,"sent message with size %d\n", size);
1319 1320 1321 1322 1323 1324 1325 1326
    return;
  }
 error:
  LOG_D(ENB_AGENT, "Could not send sf trigger message\n");
}

void enb_agent_send_update_mac_stats(mid_t mod_id) {

1327
  Protocol__ProgranMessage *current_report = NULL, *msg;
1328 1329 1330 1331
  void *data;
  int size;
  err_code_t err_code;
  int priority;
1332

1333
  mac_stats_updates_context_t stats_context = mac_stats_context[mod_id];
1334
  
1335 1336 1337
  if (pthread_mutex_lock(mac_stats_context[mod_id].mutex)) {
    goto error;
  }
1338

1339 1340 1341 1342 1343 1344 1345
  if (mac_stats_context[mod_id].cont_update == 1) {
  
    /*Create a fresh report with the required flags*/
    err_code = enb_agent_mac_handle_stats(mod_id, (void *) mac_stats_context[mod_id].stats_req, &current_report);
    if (err_code < 0) {
      goto error;
    }
1346
  }
1347 1348 1349 1350
  /* /\*TODO:Check if a previous reports exists and if yes, generate a report */
  /*  *that is the diff between the old and the new report, */
  /*  *respecting the thresholds. Otherwise send the new report*\/ */
  /* if (mac_stats_context[mod_id].prev_stats_reply != NULL) { */
1351

1352
  /*   msg = enb_agent_generate_diff_mac_stats_report(current_report, mac_stats_context[mod_id].prev_stats_reply); */
1353

1354 1355 1356 1357 1358
  /*   /\*Destroy the old stats*\/ */
  /*    enb_agent_destroy_progran_message(mac_stats_context[mod_id].prev_stats_reply); */
  /* } */
  /* /\*Use the current report for future comparissons*\/ */
  /* mac_stats_context[mod_id].prev_stats_reply = current_report; */
1359 1360


1361 1362 1363
  if (pthread_mutex_unlock(mac_stats_context[mod_id].mutex)) {
    goto error;
  }
1364

1365 1366
  if (current_report != NULL){
    data=enb_agent_pack_message(current_report, &size);
1367 1368 1369 1370 1371 1372 1373 1374
    /*Send any stats updates using the MAC channel of the eNB*/
    if (enb_agent_msg_send(mod_id, ENB_AGENT_MAC, data, size, priority)) {
      err_code = PROTOCOL__PROGRAN_ERR__MSG_ENQUEUING;
      goto error;
    }

    LOG_D(ENB_AGENT,"sent message with size %d\n", size);
    return;
1375 1376
  }
 error:
1377
  LOG_D(ENB_AGENT, "Could not send sf trigger message\n");
1378 1379
}

1380
int enb_agent_register_mac_xface(mid_t mod_id, AGENT_MAC_xface *xface) {
1381
  if (mac_agent_registered[mod_id]) {
1382 1383 1384 1385
    LOG_E(MAC, "MAC agent for eNB %d is already registered\n", mod_id);
    return -1;
  }

1386
  //xface->agent_ctxt = &shared_ctxt[mod_id];
1387
  xface->enb_agent_send_sr_info = enb_agent_send_sr_info;
1388
  xface->enb_agent_send_sf_trigger = enb_agent_send_sf_trigger;
1389
  xface->enb_agent_send_update_mac_stats = enb_agent_send_update_mac_stats;
1390 1391
  //xface->enb_agent_schedule_ue_spec = schedule_ue_spec_default;
  xface->enb_agent_schedule_ue_spec = schedule_ue_spec_remote;
1392
  xface->enb_agent_get_pending_dl_mac_config = enb_agent_get_pending_dl_mac_config;
1393
  xface->enb_agent_notify_ue_state_change = enb_agent_ue_state_change;
1394

1395
  mac_agent_registered[mod_id] = 1;
1396
  agent_mac_xface[mod_id] = xface;
1397 1398

  return 0;
1399 1400 1401
}

int enb_agent_unregister_mac_xface(mid_t mod_id, AGENT_MAC_xface *xface) {
1402

1403
  //xface->agent_ctxt = NULL;
1404
  xface->enb_agent_send_sr_info = NULL;
1405
  xface->enb_agent_send_sf_trigger = NULL;
1406
  xface->enb_agent_send_update_mac_stats = NULL;
1407
  xface->enb_agent_schedule_ue_spec = NULL;
1408 1409
  xface->enb_agent_get_pending_dl_mac_config = NULL;
  xface->enb_agent_notify_ue_state_change = NULL;
1410

1411 1412
  mac_agent_registered[mod_id] = 0;
  agent_mac_xface[mod_id] = NULL;
1413

1414 1415 1416 1417 1418 1419 1420 1421 1422
  return 0;
}


/******************************************************
 *Implementations of enb_agent_mac_internal.h functions
 ******************************************************/

err_code_t enb_agent_init_cont_mac_stats_update(mid_t mod_id) {
1423

1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
  /*Initialize the Mac stats update structure*/
  /*Initially the continuous update is set to false*/
  mac_stats_context[mod_id].cont_update = 0;
  mac_stats_context[mod_id].is_initialized = 1;
  mac_stats_context[mod_id].stats_req = NULL;
  mac_stats_context[mod_id].prev_stats_reply = NULL;
  mac_stats_context[mod_id].mutex = calloc(1, sizeof(pthread_mutex_t));
  if (mac_stats_context[mod_id].mutex == NULL)
    goto error;
  if (pthread_mutex_init(mac_stats_context[mod_id].mutex, NULL))
    goto error;;
1435

1436
  return 0;
1437

1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
 error:
  return -1;
}

err_code_t enb_agent_destroy_cont_mac_stats_update(mid_t mod_id) {
  /*Disable the continuous updates for the MAC*/
  mac_stats_context[mod_id].cont_update = 0;
  mac_stats_context[mod_id].is_initialized = 0;
  enb_agent_destroy_progran_message(mac_stats_context[mod_id].stats_req);
  enb_agent_destroy_progran_message(mac_stats_context[mod_id].prev_stats_reply);
  free(mac_stats_context[mod_id].mutex);
1449

1450 1451 1452
  mac_agent_registered[mod_id] = NULL;
  return 1;
}
1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466


err_code_t enb_agent_enable_cont_mac_stats_update(mid_t mod_id,
						  xid_t xid, stats_request_config_t *stats_req) {
  /*Enable the continuous updates for the MAC*/
  if (pthread_mutex_lock(mac_stats_context[mod_id].mutex)) {
    goto error;
  }

  Protocol__ProgranMessage *req_msg;

  enb_agent_mac_stats_request(mod_id, xid, stats_req, &req_msg);
  mac_stats_context[mod_id].stats_req = req_msg;
  mac_stats_context[mod_id].prev_stats_reply = NULL;
1467

1468 1469
  mac_stats_context[mod_id].cont_update = 1;
  mac_stats_context[mod_id].xid = xid;
1470

1471 1472 1473 1474
  if (pthread_mutex_unlock(mac_stats_context[mod_id].mutex)) {
    goto error;
  }
  return 0;
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
 error:
  LOG_E(ENB_AGENT, "mac_stats_context for eNB %d is not initialized\n", mod_id);
  return -1;
}

err_code_t enb_agent_disable_cont_mac_stats_update(mid_t mod_id) {
  /*Disable the continuous updates for the MAC*/
  if (pthread_mutex_lock(mac_stats_context[mod_id].mutex)) {
    goto error;
  }
  mac_stats_context[mod_id].cont_update = 0;
  mac_stats_context[mod_id].xid = 0;
  if (mac_stats_context[mod_id].stats_req != NULL) {
    enb_agent_destroy_progran_message(mac_stats_context[mod_id].stats_req);
  }
  if (mac_stats_context[mod_id].prev_stats_reply != NULL) {
    enb_agent_destroy_progran_message(mac_stats_context[mod_id].prev_stats_reply);
  }
  if (pthread_mutex_unlock(mac_stats_context[mod_id].mutex)) {
    goto error;
  }
  return 0;

 error:
  LOG_E(ENB_AGENT, "mac_stats_context for eNB %d is not initialized\n", mod_id);
  return -1;
1502

1503
}