flexran_agent_handler.c 28.2 KB
Newer Older
1 2 3 4 5
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
6
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */ 
21

22 23
/*! \file flexran_agent_handler.c
 * \brief FlexRAN agent tx and rx message handler 
24 25
 * \author Xenofon Foukas and Navid Nikaein and shahab SHARIAT BAGHERI
 * \date 2017
26 27 28
 * \version 0.1
 */

29
#include "flexran_agent_defs.h"
30 31
#include "flexran_agent_common.h"
#include "flexran_agent_mac.h"
32
#include "flexran_agent_rrc.h"
shahab's avatar
shahab committed
33
#include "flexran_agent_pdcp.h"
34
#include "flexran_agent_timer.h"
shahab SHARIATBAGHERI's avatar
shahab SHARIATBAGHERI committed
35
#include "flexran_agent_ran_api.h"
36
#include "common/utils/LOG/log.h"
37 38 39

#include "assertions.h"

40 41 42
flexran_agent_message_decoded_callback agent_messages_callback[][3] = {
  {flexran_agent_hello, 0, 0}, /*PROTOCOL__FLEXRAN_MESSAGE__MSG_HELLO_MSG*/
  {flexran_agent_echo_reply, 0, 0}, /*PROTOCOL__FLEXRAN_MESSAGE__MSG_ECHO_REQUEST_MSG*/
43
  {0, 0, 0}, /*PROTOCOL__FLEXRAN_MESSAGE__MSG_ECHO_REPLY_MSG*/ //Must add handler when receiving echo reply
44
  {flexran_agent_handle_stats, 0, 0}, /*PROTOCOL__FLEXRAN_MESSAGE__MSG_STATS_REQUEST_MSG*/
45 46 47
  {0, 0, 0}, /*PROTOCOL__FLEXRAN_MESSAGE__MSG_STATS_REPLY_MSG*/
  {0, 0, 0}, /*PROTOCOK__FLEXRAN_MESSAGE__MSG_SF_TRIGGER_MSG*/
  {0, 0, 0}, /*PROTOCOL__FLEXRAN_MESSAGE__MSG_UL_SR_INFO_MSG*/
48
  {flexran_agent_enb_config_reply, 0, 0}, /*PROTOCOL__FLEXRAN_MESSAGE__MSG_ENB_CONFIG_REQUEST_MSG*/
49
  {flexran_agent_handle_enb_config_reply, 0, 0}, /*PROTOCOL__FLEXRAN_MESSAGE__MSG_ENB_CONFIG_REPLY_MSG*/
50
  {flexran_agent_ue_config_reply, 0, 0}, /*PROTOCOL__FLEXRAN_MESSAGE__MSG_UE_CONFIG_REQUEST_MSG*/
51
  {flexran_agent_handle_ue_config_reply, 0, 0}, /*PROTOCOL__FLEXRAN_MESSAGE__MSG_UE_CONFIG_REPLY_MSG*/
52
  {flexran_agent_lc_config_reply, 0, 0}, /*PROTOCOL__FLEXRAN_MESSAGE__MSG_LC_CONFIG_REQUEST_MSG*/
53
  {0, 0, 0}, /*PROTOCOL__FLEXRAN_MESSAGE__MSG_LC_CONFIG_REPLY_MSG*/
54
  {flexran_agent_mac_handle_dl_mac_config, 0, 0}, /*PROTOCOL__FLEXRAN_MESSAGE__MSG_DL_MAC_CONFIG_MSG*/
55
  {0, 0, 0}, /*PROTOCOL__FLEXRAN_MESSAGE__MSG_UE_STATE_CHANGE_MSG*/
56 57
  {flexran_agent_control_delegation, 0, 0}, /*PROTOCOL__FLEXRAN_MESSAGE__MSG_CONTROL_DELEGATION_MSG*/
  {flexran_agent_reconfiguration, 0, 0}, /*PROTOCOL__FLEXRAN_MESSAGE__MSG_AGENT_RECONFIGURATION_MSG*/
58
  {flexran_agent_rrc_reconfiguration, 0, 0}, /*PROTOCOL__FLEXRAN_MESSAGE__MSG_RRC_TRIGGERING_MSG*/
59 60 61
  {0, 0, 0}, /*PROTOCOL__FLEXRAN_MESSAGE__MSG_UL_MAC_CONFIG_MSG*/
  {0, 0, 0}, /*PROTOCOL__FLEXRAN_MESSAGE__MSG_DISCONNECT_MSG*/
  {flexran_agent_rrc_trigger_handover, 0, 0}, /*PROTOCOL__FLEXRAN_MESSAGE__MSG_HO_COMMAND_MSG*/
62 63
};

64 65 66 67
flexran_agent_message_destruction_callback message_destruction_callback[] = {
  flexran_agent_destroy_hello,
  flexran_agent_destroy_echo_request,
  flexran_agent_destroy_echo_reply,
68
  flexran_agent_destroy_stats_request,
69
  flexran_agent_destroy_stats_reply,
70 71 72 73 74 75 76 77 78 79 80 81
  flexran_agent_mac_destroy_sf_trigger,
  flexran_agent_mac_destroy_sr_info,
  flexran_agent_destroy_enb_config_request,
  flexran_agent_destroy_enb_config_reply,
  flexran_agent_destroy_ue_config_request,
  flexran_agent_destroy_ue_config_reply,
  flexran_agent_destroy_lc_config_request,
  flexran_agent_destroy_lc_config_reply,
  flexran_agent_mac_destroy_dl_config,
  flexran_agent_destroy_ue_state_change,
  flexran_agent_destroy_control_delegation,
  flexran_agent_destroy_agent_reconfiguration,
82 83
};

84 85 86 87 88 89
/* static const char *flexran_agent_direction2String[] = { */
/*   "", /\* not_set  *\/ */
/*   "originating message", /\* originating message *\/ */
/*   "successfull outcome", /\* successfull outcome *\/ */
/*   "unsuccessfull outcome", /\* unsuccessfull outcome *\/ */
/* }; */
90 91


92 93 94
Protocol__FlexranMessage* flexran_agent_handle_message (mid_t mod_id,
							uint8_t *data, 
							uint32_t size){
95
  
96 97
  Protocol__FlexranMessage *decoded_message = NULL;
  Protocol__FlexranMessage *reply_message = NULL;
98 99 100
  err_code_t err_code;
  DevAssert(data != NULL);

101
  if (flexran_agent_deserialize_message(data, size, &decoded_message) < 0) {
102
    err_code= PROTOCOL__FLEXRAN_ERR__MSG_DECODING;
103 104
    goto error; 
  }
105
  if ((decoded_message->msg_case > sizeof(agent_messages_callback) / (3 * sizeof(flexran_agent_message_decoded_callback))) || 
106 107
      (decoded_message->msg_dir > PROTOCOL__FLEXRAN_DIRECTION__UNSUCCESSFUL_OUTCOME)){
    err_code= PROTOCOL__FLEXRAN_ERR__MSG_NOT_HANDLED;
108
    goto error;
109 110
  }
    
111
  if (agent_messages_callback[decoded_message->msg_case-1][decoded_message->msg_dir-1] == NULL) {
112
    err_code= PROTOCOL__FLEXRAN_ERR__MSG_NOT_SUPPORTED;
113 114 115 116
    goto error;

  }

117
  err_code = ((*agent_messages_callback[decoded_message->msg_case-1][decoded_message->msg_dir-1])(mod_id, (void *) decoded_message, &reply_message));
118 119
  if ( err_code < 0 ){
    goto error;
120
  } else if (err_code == 0) { //If err_code > 1, we do not want to dispose the message yet
121
    protocol__flexran_message__free_unpacked(decoded_message, NULL);
122
  }
123
  return reply_message;
124 125
  
error:
126
  LOG_E(FLEXRAN_AGENT,"errno %d occured\n",err_code);
127
  return NULL;
128 129 130 131 132

}



133
void * flexran_agent_pack_message(Protocol__FlexranMessage *msg, 
134
				  int * size){
135 136 137

  void * buffer;
  
138
  if (flexran_agent_serialize_message(msg, &buffer, size) < 0 ) {
139
    LOG_E(FLEXRAN_AGENT,"errno %d occured\n",PROTOCOL__FLEXRAN_ERR__MSG_ENCODING);
140 141 142 143
    goto error;
  }
  
  // free the msg --> later keep this in the data struct and just update the values
144
  //TODO call proper destroy function
145
  ((*message_destruction_callback[msg->msg_case-1])(msg));
146 147
  
  DevAssert(buffer !=NULL);
148
  
149
  LOG_D(FLEXRAN_AGENT,"Serilized the eNB-UE stats reply (size %d)\n", *size);
150
  
151
  return buffer;
152 153
  
 error : 
154 155 156
  return NULL;   
}

157
Protocol__FlexranMessage *flexran_agent_handle_timed_task(void *args) {
158
  err_code_t err_code;
159
  flexran_agent_timer_args_t *timer_args = (flexran_agent_timer_args_t *) args;
160

161
  Protocol__FlexranMessage *timed_task, *reply_message;
162
  timed_task = timer_args->msg;
163
  err_code = ((*agent_messages_callback[timed_task->msg_case-1][timed_task->msg_dir-1])(timer_args->mod_id, (void *) timed_task, &reply_message));
164 165 166 167 168
  if ( err_code < 0 ){
    goto error;
  }

  return reply_message;
169
  
170
 error:
171
  LOG_E(FLEXRAN_AGENT,"errno %d occured\n",err_code);
172
  return NULL;
173 174
}

175
Protocol__FlexranMessage* flexran_agent_process_timeout(long timer_id, void* timer_args){
176
    
177
  struct flexran_agent_timer_element_s *found = get_timer_entry(timer_id);
178 179
 
  if (found == NULL ) goto error;
180
  LOG_D(FLEXRAN_AGENT, "Found the entry (%p): timer_id is 0x%lx  0x%lx\n", found, timer_id, found->timer_id);
181 182
  
  if (timer_args == NULL)
183
    LOG_W(FLEXRAN_AGENT,"null timer args\n");
184
  
185
  return found->cb(timer_args);
186

187
 error:
188
  LOG_E(FLEXRAN_AGENT, "can't get the timer element\n");
189
  return NULL;
190
}
191

192
err_code_t flexran_agent_destroy_flexran_message(Protocol__FlexranMessage *msg) {
193 194
  return ((*message_destruction_callback[msg->msg_case-1])(msg));
}
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210


/* 
  Top Level Statistics Report

 */



int flexran_agent_handle_stats(mid_t mod_id, const void *params, Protocol__FlexranMessage **msg){

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

  int i;
211
  err_code_t err_code = 0;
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
  xid_t xid;
  uint32_t usec_interval, sec_interval;

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

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

  report_config_t report_config;

  uint32_t ue_flags = 0;
  uint32_t c_flags = 0;

  Protocol__FlexranMessage *input = (Protocol__FlexranMessage *)params;

  Protocol__FlexStatsRequest *stats_req = input->stats_request_msg;
  xid = (stats_req->header)->xid;

  // Check the type of request that is made
  switch(stats_req->body_case) {
  case PROTOCOL__FLEX_STATS_REQUEST__BODY_COMPLETE_STATS_REQUEST: ;
    Protocol__FlexCompleteStatsRequest *comp_req = stats_req->complete_stats_request;
    if (comp_req->report_frequency == PROTOCOL__FLEX_STATS_REPORT_FREQ__FLSRF_OFF) {
      /*Disable both periodic and continuous updates*/
      // flexran_agent_disable_cont_stats_update(mod_id);
      flexran_agent_destroy_timer_by_task_id(xid);
      *msg = NULL;
      return 0;
    } else { //One-off, periodical or continuous reporting
      //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

      //Set the number of UEs and create list with their RNTIs stats configs
248 249 250 251 252 253 254 255
      report_config.nr_ue = 0;
      if (flexran_agent_get_rrc_xface(mod_id))
        report_config.nr_ue = flexran_get_rrc_num_ues(mod_id);
      else if (flexran_agent_get_mac_xface(mod_id))
        report_config.nr_ue = flexran_get_mac_num_ues(mod_id);

      if (flexran_agent_get_rrc_xface(mod_id) && flexran_agent_get_mac_xface(mod_id)
          && flexran_get_rrc_num_ues(mod_id) != flexran_get_mac_num_ues(mod_id)) {
256 257 258 259 260
        const int nrrc = flexran_get_rrc_num_ues(mod_id);
        const int nmac = flexran_get_mac_num_ues(mod_id);
        report_config.nr_ue = nrrc < nmac ? nrrc : nmac;
        LOG_E(FLEXRAN_AGENT, "%s(): different numbers of UEs in RRC (%d) and MAC (%d), reporting for %d UEs\n",
              __func__, nrrc, nmac, report_config.nr_ue);
261
      }
262
      report_config.ue_report_type = malloc(sizeof(ue_report_type_t) * report_config.nr_ue);
263
      if (report_config.ue_report_type == NULL) {
264 265 266
        // TODO: Add appropriate error code
        err_code = -100;
        goto error;
267
      }
268 269 270 271
      if (flexran_agent_get_rrc_xface(mod_id)) {
        rnti_t rntis[report_config.nr_ue];
        flexran_get_rrc_rnti_list(mod_id, rntis, report_config.nr_ue);
        for (i = 0; i < report_config.nr_ue; i++) {
272
          report_config.ue_report_type[i].ue_rnti = rntis[i];
273 274 275 276 277
          report_config.ue_report_type[i].ue_report_flags = ue_flags;
        }
      }
      if (flexran_agent_get_mac_xface(mod_id) && !flexran_agent_get_rrc_xface(mod_id)) {
        for (i = 0; i < report_config.nr_ue; i++) {
278 279
          const int UE_id = flexran_get_mac_ue_id(mod_id, i);
          report_config.ue_report_type[i].ue_rnti = flexran_get_mac_ue_crnti(enb_id, UE_id);
280 281
          report_config.ue_report_type[i].ue_report_flags = ue_flags;
        }
282 283 284
      }
      //Set the number of CCs and create a list with the cell stats configs
      report_config.nr_cc = MAX_NUM_CCs;
285
      report_config.cc_report_type = malloc(sizeof(cc_report_type_t) * report_config.nr_cc);
286
      if (report_config.cc_report_type == NULL) {
287 288 289
        // TODO: Add appropriate error code
        err_code = -100;
        goto error;
290 291
      }
      for (i = 0; i < report_config.nr_cc; i++) {
292 293 294
        //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;
295 296 297
      }
      /* Check if request was periodical */
      if (comp_req->report_frequency == PROTOCOL__FLEX_STATS_REPORT_FREQ__FLSRF_PERIODICAL) {
298
        /* Create a one off flexran message as an argument for the periodical task */
299
        Protocol__FlexranMessage *timer_msg = NULL;
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
        stats_request_config_t request_config;
        request_config.report_type = PROTOCOL__FLEX_STATS_TYPE__FLST_COMPLETE_STATS;
        request_config.report_frequency = PROTOCOL__FLEX_STATS_REPORT_FREQ__FLSRF_ONCE;
        request_config.period = 0;
        /* 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 = 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;
        }
        request_config.config = &report_config;
317 318 319 320
        if (flexran_agent_stats_request(enb_id, xid, &request_config, &timer_msg) == -1) {
          err_code = -100;
          goto error;
        }
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
        /* Create a timer */
        long timer_id = 0;
        flexran_agent_timer_args_t *timer_args = malloc(sizeof(flexran_agent_timer_args_t));
        memset (timer_args, 0, sizeof(flexran_agent_timer_args_t));
        timer_args->mod_id = enb_id;
        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);
        }
        flexran_agent_create_timer(sec_interval, usec_interval, FLEXRAN_AGENT_DEFAULT,
            enb_id, FLEXRAN_AGENT_TIMER_TYPE_PERIODIC, xid,
            flexran_agent_handle_timed_task,(void*) timer_args, &timer_id);
338
      } else if (comp_req->report_frequency == PROTOCOL__FLEX_STATS_REPORT_FREQ__FLSRF_CONTINUOUS) {
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
        /*If request was for continuous updates, disable the previous configuration and
          set up a new one*/
        flexran_agent_disable_cont_stats_update(mod_id);
        stats_request_config_t request_config;
        request_config.report_type = PROTOCOL__FLEX_STATS_TYPE__FLST_COMPLETE_STATS;
        request_config.report_frequency = PROTOCOL__FLEX_STATS_REPORT_FREQ__FLSRF_ONCE;
        request_config.period = 0;
        /* 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 = 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;
        }
        request_config.config = &report_config;
        flexran_agent_enable_cont_stats_update(enb_id, xid, &request_config);
360 361 362 363 364 365 366 367 368
      }
    }
    break;
  case PROTOCOL__FLEX_STATS_REQUEST__BODY_CELL_STATS_REQUEST:;
    Protocol__FlexCellStatsRequest *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;
369
    report_config.cc_report_type = malloc(sizeof(cc_report_type_t) * report_config.nr_cc);
370 371 372 373 374 375
    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++) {
376
      //TODO: Must fill in the proper cell ids
377 378 379 380 381 382 383 384 385 386
      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__FLEX_STATS_REQUEST__BODY_UE_STATS_REQUEST:;
    Protocol__FlexUeStatsRequest *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;
387
    report_config.ue_report_type = malloc(sizeof(ue_report_type_t) * report_config.nr_ue);
388 389 390 391 392 393
    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++) {
394
      const int UE_id = flexran_get_mac_ue_id(mod_id, i);
395
      report_config.ue_report_type[i].ue_rnti = ue_req->rnti[UE_id];
396 397 398 399 400 401 402 403 404
      report_config.ue_report_type[i].ue_report_flags = ue_req->flags;
    }
    break;
  default:
    //TODO: Add appropriate error code
    err_code = -100;
    goto error;
  }

405 406 407 408
  if (flexran_agent_stats_reply(enb_id, xid, &report_config, msg )) {
    err_code = PROTOCOL__FLEXRAN_ERR__MSG_BUILD;
    goto error;
  }
409

410 411 412 413
  if (report_config.ue_report_type)
    free(report_config.ue_report_type);
  if (report_config.cc_report_type)
    free(report_config.cc_report_type);
414 415 416 417

  return 0;

 error :
418
  LOG_E(FLEXRAN_AGENT, "%s(): errno %d occured\n", __func__, err_code);
419 420 421 422 423 424 425 426 427
  return err_code;
}

/*
  Top level reply 
 */

int flexran_agent_stats_reply(mid_t enb_id, xid_t xid, const report_config_t *report_config, Protocol__FlexranMessage **msg){

428
  Protocol__FlexHeader *header = NULL;
429 430 431
  Protocol__FlexUeStatsReport **ue_report = NULL;
  Protocol__FlexCellStatsReport **cell_report = NULL;
  Protocol__FlexStatsReply *stats_reply_msg = NULL;
432
  err_code_t err_code = PROTOCOL__FLEXRAN_ERR__UNEXPECTED;
433
  int i,j;
434

435
  if (flexran_create_header(xid, PROTOCOL__FLEX_TYPE__FLPT_STATS_REPLY, &header) != 0) {
436
    goto error;
437
  }
438 439 440

  stats_reply_msg = malloc(sizeof(Protocol__FlexStatsReply));

441
  if (stats_reply_msg == NULL) {
442
    goto error;
443
  }
444 445 446 447 448 449 450 451 452 453

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

  // UE report

  ue_report = malloc(sizeof(Protocol__FlexUeStatsReport *) * report_config->nr_ue);
454 455 456 457

  if (ue_report == NULL) {
    goto error;
  }
458 459 460 461
  
  for (i = 0; i < report_config->nr_ue; i++) {

      ue_report[i] = malloc(sizeof(Protocol__FlexUeStatsReport));
462 463 464
      if (ue_report[i] == NULL) {
        goto error;
      }
465 466 467
      protocol__flex_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;
468
      ue_report[i]->has_flags = 1; /* actual flags are filled in the CMs below */
469 470 471 472 473 474
  
  }

  // cell rpoert 
  
  cell_report = malloc(sizeof(Protocol__FlexCellStatsReport *) * report_config->nr_cc);
475
  if (cell_report == NULL) {
476
    goto error;
477
  }
478 479 480 481
  
  for (i = 0; i < report_config->nr_cc; i++) {

      cell_report[i] = malloc(sizeof(Protocol__FlexCellStatsReport));
482
      if(cell_report[i] == NULL) {
483
          goto error;
484
      }
485 486 487 488

      protocol__flex_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;
489
      cell_report[i]->has_flags = 1; /* actual flags are filled in the CMs below */
490 491 492

  }

493 494 495 496 497 498
  /* MAC reply split */
  if (flexran_agent_get_mac_xface(enb_id)
      && flexran_agent_mac_stats_reply(enb_id, report_config,  ue_report, cell_report) < 0) {
    err_code = PROTOCOL__FLEXRAN_ERR__MSG_BUILD;
    goto error;
  }
499

500 501 502 503 504 505
  /* RRC reply split */
  if (flexran_agent_get_rrc_xface(enb_id)
      && flexran_agent_rrc_stats_reply(enb_id, report_config,  ue_report, cell_report) < 0) {
    err_code = PROTOCOL__FLEXRAN_ERR__MSG_BUILD;
    goto error;
  }
506

507 508 509 510 511 512
  /* PDCP reply split */
  if (flexran_agent_get_pdcp_xface(enb_id)
      && flexran_agent_pdcp_stats_reply(enb_id, report_config,  ue_report, cell_report) < 0) {
    err_code = PROTOCOL__FLEXRAN_ERR__MSG_BUILD;
    goto error;
  }
513 514

       
515 516 517 518
  stats_reply_msg->cell_report = cell_report;
  stats_reply_msg->ue_report = ue_report;

 *msg = malloc(sizeof(Protocol__FlexranMessage));
519
  if(*msg == NULL) {
520
    goto error;
521
  }
522 523 524 525 526 527 528 529 530
  protocol__flexran_message__init(*msg);
  (*msg)->msg_case = PROTOCOL__FLEXRAN_MESSAGE__MSG_STATS_REPLY_MSG;
  (*msg)->msg_dir =  PROTOCOL__FLEXRAN_DIRECTION__SUCCESSFUL_OUTCOME;
  (*msg)->stats_reply_msg = stats_reply_msg;

  return 0;

error :
  LOG_E(FLEXRAN_AGENT, "errno %d occured\n", err_code);
531 532

  if (header != NULL) {
533
    free(header);
534
    header = NULL;
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
  if (stats_reply_msg != NULL) {
    free(stats_reply_msg);
    stats_reply_msg = NULL;
  }

  if (ue_report != NULL) {
    for (j = 0; j < report_config->nr_ue; j++) {
      if (ue_report[j] != NULL) {
        free(ue_report[j]);
      }
    }
    free(ue_report);
    ue_report = NULL;
  }

  if (cell_report != NULL) {
    for (j = 0; j < report_config->nr_cc; j++) {
      if (cell_report[j] != NULL) {
        free(cell_report[j]);
      }
    }
    free(cell_report);
    cell_report = NULL;
  }

  return err_code;
563 564 565 566 567 568 569 570 571 572
}

/*
  Top Level Request 
 */

int flexran_agent_stats_request(mid_t mod_id,
            xid_t xid,
            const stats_request_config_t *report_config,
            Protocol__FlexranMessage **msg) {
573
  Protocol__FlexHeader *header = NULL;
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 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 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 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
  int i;

  Protocol__FlexStatsRequest *stats_request_msg;
  stats_request_msg = malloc(sizeof(Protocol__FlexStatsRequest));
  if(stats_request_msg == NULL)
    goto error;
  protocol__flex_stats_request__init(stats_request_msg);

  if (flexran_create_header(xid, PROTOCOL__FLEX_TYPE__FLPT_STATS_REQUEST, &header) != 0)
    goto error;

  stats_request_msg->header = header;

  stats_request_msg->type = report_config->report_type;
  stats_request_msg->has_type = 1;

  switch (report_config->report_type) {
  case PROTOCOL__FLEX_STATS_TYPE__FLST_COMPLETE_STATS:
    stats_request_msg->body_case =  PROTOCOL__FLEX_STATS_REQUEST__BODY_COMPLETE_STATS_REQUEST;
    Protocol__FlexCompleteStatsRequest *complete_stats;
    complete_stats = malloc(sizeof(Protocol__FlexCompleteStatsRequest));
    if(complete_stats == NULL)
      goto error;
    protocol__flex_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__FLEX_STATS_TYPE__FLST_CELL_STATS:
    stats_request_msg->body_case = PROTOCOL__FLEX_STATS_REQUEST__BODY_CELL_STATS_REQUEST;
     Protocol__FlexCellStatsRequest *cell_stats;
     cell_stats = malloc(sizeof(Protocol__FlexCellStatsRequest));
    if(cell_stats == NULL)
      goto error;
    protocol__flex_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__FLEX_STATS_TYPE__FLST_UE_STATS:
    stats_request_msg->body_case = PROTOCOL__FLEX_STATS_REQUEST__BODY_UE_STATS_REQUEST;
     Protocol__FlexUeStatsRequest *ue_stats;
     ue_stats = malloc(sizeof(Protocol__FlexUeStatsRequest));
    if(ue_stats == NULL)
      goto error;
    protocol__flex_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__FlexranMessage));
  if(*msg == NULL)
    goto error;
  protocol__flexran_message__init(*msg);
  (*msg)->msg_case = PROTOCOL__FLEXRAN_MESSAGE__MSG_STATS_REQUEST_MSG;
  (*msg)->msg_dir = PROTOCOL__FLEXRAN_DIRECTION__INITIATING_MESSAGE;
  (*msg)->stats_request_msg = stats_request_msg;
  return 0;

 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 flexran_agent_destroy_stats_request(Protocol__FlexranMessage *msg) {
   if(msg->msg_case != PROTOCOL__FLEXRAN_MESSAGE__MSG_STATS_REQUEST_MSG)
    goto error;
  free(msg->stats_request_msg->header);
  if (msg->stats_request_msg->body_case == PROTOCOL__FLEX_STATS_REQUEST__BODY_CELL_STATS_REQUEST) {
    free(msg->stats_request_msg->cell_stats_request->cell);
  }
  if (msg->stats_request_msg->body_case == PROTOCOL__FLEX_STATS_REQUEST__BODY_UE_STATS_REQUEST) {
    free(msg->stats_request_msg->ue_stats_request->rnti);
  }
  free(msg->stats_request_msg);
  free(msg);
  return 0;

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

695 696 697 698 699 700 701
int flexran_agent_destroy_stats_reply(Protocol__FlexranMessage *msg)
{
  if (msg->msg_case != PROTOCOL__FLEXRAN_MESSAGE__MSG_STATS_REPLY_MSG) {
    LOG_E(FLEXRAN_AGENT, "%s(): message is not a msg_stats_reply\n", __func__);
    return -1;
  }

702 703 704 705 706 707 708
  flexran_agent_mac_destroy_stats_reply(msg->stats_reply_msg);
  flexran_agent_rrc_destroy_stats_reply(msg->stats_reply_msg);
  flexran_agent_pdcp_destroy_stats_reply(msg->stats_reply_msg);
  for (int i = 0; i < msg->stats_reply_msg->n_cell_report; ++i)
    free(msg->stats_reply_msg->cell_report[i]);
  for (int i = 0; i < msg->stats_reply_msg->n_ue_report; ++i)
    free(msg->stats_reply_msg->ue_report[i]);
709 710
  free(msg->stats_reply_msg->cell_report);
  free(msg->stats_reply_msg->ue_report);
711
  free(msg->stats_reply_msg->header);
712 713 714 715 716
  free(msg->stats_reply_msg);
  free(msg);
  return 0;
}

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
err_code_t flexran_agent_disable_cont_stats_update(mid_t mod_id) {
  /*Disable the continuous updates for the MAC*/
  if (pthread_mutex_lock(stats_context[mod_id].mutex)) {
    goto error;
  }
  stats_context[mod_id].cont_update = 0;
  stats_context[mod_id].xid = 0;
  if (stats_context[mod_id].stats_req != NULL) {
    flexran_agent_destroy_flexran_message(stats_context[mod_id].stats_req);
  }
  if (stats_context[mod_id].prev_stats_reply != NULL) {
    flexran_agent_destroy_flexran_message(stats_context[mod_id].prev_stats_reply);
  }
  if (pthread_mutex_unlock(stats_context[mod_id].mutex)) {
    goto error;
  }
  return 0;

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

}

err_code_t flexran_agent_enable_cont_stats_update(mid_t mod_id,
                  xid_t xid, stats_request_config_t *stats_req) {
  
  if (pthread_mutex_lock(stats_context[mod_id].mutex)) {
    goto error;
  }

748
  Protocol__FlexranMessage *req_msg = NULL;
749 750 751

  flexran_agent_stats_request(mod_id, xid, stats_req, &req_msg);

752 753 754 755 756 757 758
  if (req_msg != NULL) {
    stats_context[mod_id].stats_req = req_msg;
    stats_context[mod_id].prev_stats_reply = NULL;

    stats_context[mod_id].cont_update = 1;
    stats_context[mod_id].xid = xid;
  }
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781

  if (pthread_mutex_unlock(stats_context[mod_id].mutex)) {
    goto error;
  }
  return 0;

 error:
  LOG_E(FLEXRAN_AGENT, "stats_context for eNB %d is not initialized\n", mod_id);
  return -1;
}


err_code_t flexran_agent_init_cont_stats_update(mid_t mod_id) {

  
  /*Initially the continuous update is set to false*/
  stats_context[mod_id].cont_update = 0;
  stats_context[mod_id].is_initialized = 1;
  stats_context[mod_id].stats_req = NULL;
  stats_context[mod_id].prev_stats_reply = NULL;
  stats_context[mod_id].mutex = calloc(1, sizeof(pthread_mutex_t));
  if (stats_context[mod_id].mutex == NULL)
    goto error;
782
  if (pthread_mutex_init(stats_context[mod_id].mutex, NULL) != 0)
783
    goto error;
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799

  return 0;

 error:
  return -1;
}

err_code_t flexran_agent_destroy_cont_stats_update(mid_t mod_id) {
  
  stats_context[mod_id].cont_update = 0;
  stats_context[mod_id].is_initialized = 0;
  flexran_agent_destroy_flexran_message(stats_context[mod_id].stats_req);
  flexran_agent_destroy_flexran_message(stats_context[mod_id].prev_stats_reply);
  free(stats_context[mod_id].mutex);

  return 1;
shahab SHARIATBAGHERI's avatar
shahab SHARIATBAGHERI committed
800
}