proto_agent_common.c 22.1 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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
/*******************************************************************************
    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.

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

/*! \file proto_agent_common.c
 * \brief common primitives for all agents 
 * \author Navid Nikaein and Xenofon Foukas
 * \date 2016
 * \version 0.1
 */

#include<stdio.h>
#include <dlfcn.h>
#include <time.h>

#include "proto_agent_common.h"
#include "PHY/extern.h"
#include "log.h"

#include "RRC/LITE/extern.h"
#include "RRC/L2_INTERFACE/openair_rrc_L2_interface.h"
#include "rrc_eNB_UE_context.h"

void * enb[NUM_MAX_ENB];
void * enb_ue[NUM_MAX_ENB];
void * enb_rrc[NUM_MAX_ENB];
52

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
/*
 * message primitives
 */

int proto_agent_serialize_message(Protocol__FlexsplitMessage *msg, void **buf, int *size) {

  *size = protocol__flexsplit_message__get_packed_size(msg);
  
  *buf = malloc(*size);
  if (buf == NULL)
    goto error;
  
  protocol__flexsplit_message__pack(msg, *buf);
  
  return 0;
  
 error:
70
  LOG_E(PROTO_AGENT, "an error occured\n"); 
71 72 73 74 75 76 77 78
  return -1;
}

/* We assume that the buffer size is equal to the message size.
   Should be chekced durint Tx/Rx */
int proto_agent_deserialize_message(void *data, int size, Protocol__FlexsplitMessage **msg) {
  *msg = protocol__flexsplit_message__unpack(NULL, size, data);
  if (*msg == NULL)
79
    goto error;  
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

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

int fsp_create_header(xid_t xid, Protocol__FspType type,  Protocol__FspHeader **header) {
  
  *header = malloc(sizeof(Protocol__FspHeader));
  if(*header == NULL)
    goto error;
  
  protocol__fsp_header__init(*header);
95
  LOG_D(PROTO_AGENT, "Initialized the PROTOBUF message header\n");
96
  (*header)->version = FLEXSPLIT_VERSION;
97 98
  LOG_D(PROTO_AGENT, "Set the vversion to FLEXSPLIT_VERSION\n");

99 100 101 102 103 104 105 106 107 108 109 110
  (*header)->has_version = 1; 
  (*header)->type = type;
  (*header)->has_type = 1;
  (*header)->xid = xid;
  (*header)->has_xid = 1;
  return 0;

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

111 112 113 114 115
int just_print(mid_t mod_id, const void *params, Protocol__FlexsplitMessage **msg)
{
    return 1;
}

116 117 118 119 120 121 122 123 124
int proto_agent_pdcp_data_req(mid_t mod_id, const void *params, Protocol__FlexsplitMessage **msg)
{
  
  // Initialize the PDCP params
  data_req_args *args = (data_req_args *)params;
 
  // Create the protobuf header
  Protocol__FspHeader *header;
  xid_t xid = 1;
125
  LOG_D(PROTO_AGENT, "creating the data_req message\n");
126 127 128 129 130 131 132 133 134 135
  
  if (fsp_create_header(xid, PROTOCOL__FSP_TYPE__FSPT_RLC_DATA_REQ, &header) != 0)
     goto error;
  
  /* Begin constructing the messages. They are defined as follows:
  *  1) fspRlcPdu is storing the bytes of the packet
  *  2) Message fspRlcData is packing the packet + the context of the PDCP (separate message)
  *  3) Messge fspRlcDataReq is packing the header, enb_id and fspRlcData
  */
  Protocol__FspCtxt *ctxt = NULL;
136
  Protocol__FspRlcPdu *pdu = NULL;
137 138 139
  Protocol__FspRlcData *rlc_data = NULL;
  Protocol__FspRlcDataReq *data_req = NULL;
  
140

141
  ctxt = malloc(sizeof(Protocol__FspCtxt));
142
  pdu = malloc(sizeof(Protocol__FspRlcPdu));
143 144 145 146
  rlc_data = malloc(sizeof(Protocol__FspRlcData));
  data_req = malloc(sizeof(Protocol__FspRlcDataReq));
  
  protocol__fsp_ctxt__init(ctxt);
147
  protocol__fsp_rlc_pdu__init(pdu);
148 149 150 151 152 153
  protocol__fsp_rlc_data__init(rlc_data);
  protocol__fsp_rlc_data_req__init(data_req);
  
  // Copy data to the RlcPdu structure
  pdu->fsp_pdu_data.data =  malloc(args->sdu_size);
  pdu->fsp_pdu_data.len = args->sdu_size;
154
   
155
  memcpy(pdu->fsp_pdu_data.data, args->sdu_p, args->sdu_size); 
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
  pdu->has_fsp_pdu_data = 1;
  
  // Copy data to the ctxt structure
  ctxt->fsp_mod_id = args->ctxt->module_id;
  ctxt->fsp_enb_flag = args->ctxt->enb_flag;
  ctxt->fsp_instance = args->ctxt->instance;
  ctxt->fsp_rnti = args->ctxt->rnti;
  ctxt->fsp_frame = args->ctxt->frame;
  ctxt->fsp_subframe = args->ctxt->subframe;
  ctxt->fsp_enb_index = args->ctxt->eNB_index;

  ctxt->has_fsp_mod_id = 1;
  ctxt->has_fsp_enb_flag = 1;
  ctxt->has_fsp_instance = 1;
  ctxt->has_fsp_rnti = 1;
  ctxt->has_fsp_frame = 1;
  ctxt->has_fsp_subframe = 1;
  ctxt->has_fsp_enb_index = 1;

175
  rlc_data->fsp_ctxt = ctxt; 
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
  rlc_data->fsp_srb_flag = args->srb_flag;
  rlc_data->fsp_mbms_flag = args->MBMS_flag;
  rlc_data->fsp_rb_id = args->rb_id;
  rlc_data->fsp_muip = args->mui;
  rlc_data->fsp_confirm = args->confirm;
  rlc_data->fsp_sdu_buffer_size = args->sdu_size;
  rlc_data->fsp_pdu = pdu;
  
  rlc_data->has_fsp_srb_flag = 1;
  rlc_data->has_fsp_mbms_flag = 1;
  rlc_data->has_fsp_rb_id = 1;
  rlc_data->has_fsp_muip = 1;
  rlc_data->has_fsp_confirm = 1;
  rlc_data->has_fsp_sdu_buffer_size = 1;

  // Up to here, everything is a signle message that is packed inside another. The final data_req 
  // will be created later, after the setting of all variables
  
  data_req->header = header;
  data_req->enb_id = mod_id;
  data_req->has_enb_id = 1;
  data_req->pdcp_data = rlc_data;
  
  *msg = malloc(sizeof(Protocol__FlexsplitMessage));

  if(*msg == NULL)
    goto error;
  
  protocol__flexsplit_message__init(*msg);
  
  (*msg)->msg_case = PROTOCOL__FLEXSPLIT_MESSAGE__MSG_DATA_REQ_MSG;
  (*msg)->msg_dir = PROTOCOL__FLEXSPLIT_DIRECTION__INITIATING_MESSAGE; //we will be waiting for the ACK
  (*msg)->has_msg_dir = 1;
209
  (*msg)->data_req_msg = data_req;
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
  
  return 0;
  
  error:
    if(header != NULL)
      free(header);
    if(pdu!=NULL)
      free(pdu);
    if(rlc_data!=NULL)
      free(rlc_data);
    if(data_req!= NULL)
      free(data_req);
    if(*msg != NULL)
      free(*msg);
    LOG_E(PROTO_AGENT, "%s: an error occured\n", __FUNCTION__);
    return -1;
  
}

int proto_agent_destroy_pdcp_data_req(Protocol__FlexsplitMessage *msg) {
  if(msg->msg_case != PROTOCOL__FLEXSPLIT_MESSAGE__MSG_DATA_REQ_MSG)
    goto error;
  
233 234 235 236 237 238 239
  free(msg->data_req_msg->header);
  free(msg->data_req_msg->pdcp_data->fsp_pdu->fsp_pdu_data.data);
  free(msg->data_req_msg->pdcp_data->fsp_pdu);
  free(msg->data_req_msg->pdcp_data->fsp_ctxt);
  free(msg->data_req_msg->pdcp_data);
  free(msg->data_req_msg);
  free(msg);
240 241 242 243 244 245 246
  return 0;
  
  error:
    LOG_E(PROTO_AGENT, "%s: an error occured\n", __FUNCTION__);
    return -1;
}

247 248 249 250 251
int proto_agent_get_ack_result(mid_t mod_id, const void *params, Protocol__FlexsplitMessage **msg)
{
  Protocol__FspHeader *header;
  xid_t xid;
  rlc_op_status_t result = 0;
252
  LOG_D(PROTO_AGENT, "handling the data_req_ack message\n");
253 254 255
  Protocol__FlexsplitMessage *input = (Protocol__FlexsplitMessage *)params;
  Protocol__FspRlcDataReqAck *data_ack = input->data_req_ack;
  result = data_ack->result;
256
  ack_result = result;
257
  return 0;
258 259 260

}

261 262 263 264 265

int proto_agent_pdcp_data_req_ack(mid_t mod_id, const void *params, Protocol__FlexsplitMessage **msg)
{
  Protocol__FspHeader *header;
  xid_t xid;
266 267
  rlc_op_status_t result = 0;
  
268
  LOG_D(PROTO_AGENT, "creating the data_req_ack message\n");
269 270 271 272
  
  Protocol__FlexsplitMessage *input = (Protocol__FlexsplitMessage *)params;
  Protocol__FspRlcDataReq *data_req = input->data_req_msg;
  
273
  xid = data_req->header->xid;
274 275 276 277
  Protocol__FspRlcPdu *pdu = NULL;
  Protocol__FspCtxt *ctxt = NULL;
  Protocol__FspRlcData *rlc_data = NULL;
  
278
  
279
  rlc_data = data_req->pdcp_data;
280
  
281
  pdu = rlc_data->fsp_pdu;
282
  
283 284
  ctxt = rlc_data->fsp_ctxt;
  
285
  protocol_ctxt_t	 *ctxt_pP;
286 287 288 289
  srb_flag_t     	 srb_flagP = 0;
  rb_id_t        	 rb_idP = 0;
  mui_t         	 muiP = 0;
  confirm_t      	 confirmP = 0;
290 291
  sdu_size_t           	 pdcp_pdu_size = 0;
  MBMS_flag_t		 flag_MBMS = 0;
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
  mem_block_t 		 *pdcp_pdu_p = NULL;
  

  // Create a new protocol context for handling the packet
  
  ctxt_pP = malloc(sizeof(protocol_ctxt_t));
  ctxt_pP->module_id = ctxt->fsp_mod_id;
  ctxt_pP->enb_flag = ctxt->fsp_enb_flag;
  ctxt_pP->instance = ctxt->fsp_instance;
  ctxt_pP->rnti = ctxt->fsp_rnti;
  ctxt_pP->frame = ctxt->fsp_frame;
  ctxt_pP->subframe = ctxt->fsp_subframe;
  ctxt_pP->eNB_index = ctxt->fsp_enb_index;
  
  srb_flagP = rlc_data->fsp_srb_flag;
  flag_MBMS = rlc_data->fsp_mbms_flag;
  rb_idP = rlc_data->fsp_rb_id;
  muiP = rlc_data->fsp_muip;
  confirmP = rlc_data->fsp_confirm;
311
  pdcp_pdu_size = rlc_data->fsp_pdu->fsp_pdu_data.len; 
312
  pdcp_pdu_p = get_free_mem_block(pdcp_pdu_size);
313
  
314 315
  memcpy(pdcp_pdu_p->data, rlc_data->fsp_pdu->fsp_pdu_data.data, pdcp_pdu_size);

316
  
317
  result = rlc_data_req((const protocol_ctxt_t*) ctxt_pP, (const srb_flag_t) srb_flagP, (const MBMS_flag_t) flag_MBMS, (const rb_id_t) rb_idP, (const mui_t) muiP, confirmP, pdcp_pdu_size, pdcp_pdu_p);
318
 
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
  if (fsp_create_header(xid, PROTOCOL__FSP_TYPE__FSPT_RLC_DATA_REQ_ACK, &header) != 0)
    goto error;

  Protocol__FspRlcDataReqAck *ack = NULL;
  ack = malloc(sizeof(Protocol__FspRlcDataReqAck));
  protocol__fsp_rlc_data_req_ack__init(ack);
  
  ack->header = header;
  ack->result = result;
  ack->has_result = 1;
    
  *msg = malloc(sizeof(Protocol__FlexsplitMessage));
  if(*msg == NULL)
    goto error;
  
  protocol__flexsplit_message__init(*msg);
  (*msg)->msg_case = PROTOCOL__FLEXSPLIT_MESSAGE__MSG_DATA_REQ_ACK;
  (*msg)->msg_dir = PROTOCOL__FLEXSPLIT_DIRECTION__SUCCESSFUL_OUTCOME;
  (*msg)->has_msg_dir = 1;
  (*msg)->data_req_ack = ack;
  return 0;
  
  error:
342 343
    if (pdcp_pdu_p != NULL)
      free_mem_block(pdcp_pdu_p);
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
    if(header != NULL)
      free(header);
    if(ack!=NULL)
      free(ack);
    if(*msg != NULL)
      free(*msg);
    LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
    return -1;

}

int proto_agent_destroy_pdcp_data_req_ack(Protocol__FlexsplitMessage *msg) {
  if(msg->msg_case != PROTOCOL__FLEXSPLIT_MESSAGE__MSG_DATA_REQ_ACK)
    goto error;
  
359 360 361
  free(msg->data_req_ack->header);
  free(msg->data_req_ack);
  free(msg);
362 363 364 365 366 367 368
  return 0;
  
  error:
    LOG_E(PROTO_AGENT, "%s: an error occured\n", __FUNCTION__);
    return -1;
}

369 370 371 372 373

int proto_agent_destroy_pdcp_data_ind(Protocol__FlexsplitMessage *msg) {
  if(msg->msg_case != PROTOCOL__FLEXSPLIT_MESSAGE__MSG_DATA_IND_MSG)
    goto error;
  
374 375 376
  free(msg->data_req_ack->header);
  free(msg->data_req_ack);
  free(msg);
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
  return 0;
  
  error:
    LOG_E(PROTO_AGENT, "%s: an error occured\n", __FUNCTION__);
    return -1;
}

int proto_agent_pdcp_data_ind(mid_t mod_id, const void *params, Protocol__FlexsplitMessage **msg)
{
  
  // Initialize the PDCP params
  data_req_args *args = (data_req_args *)params;
 
  // Create the protobuf header
  Protocol__FspHeader *header;
  xid_t xid = 1;
393
  LOG_D(PROTO_AGENT, "creating the data_ind message\n");
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
  
  if (fsp_create_header(xid, PROTOCOL__FSP_TYPE__FSPT_PDCP_DATA_IND, &header) != 0)
     goto error;
  
  /* Begin constructing the messages. They are defined as follows:
  *  1) fspRlcPdu is storing the bytes of the packet
  *  2) Message fspRlcData is packing the packet + the context of the PDCP (separate message)
  *  3) Messge fspRlcDataReq is packing the header, enb_id and fspRlcData
  */
  
  Protocol__FspCtxt *ctxt = NULL;
  Protocol__FspRlcPdu *pdu = NULL;
  Protocol__FspRlcData *rlc_data = NULL;
  Protocol__FspPdcpDataInd *data_ind = NULL;
  

  ctxt = malloc(sizeof(Protocol__FspCtxt));
  pdu = malloc(sizeof(Protocol__FspRlcPdu));
  rlc_data = malloc(sizeof(Protocol__FspRlcData));
  data_ind = malloc(sizeof(Protocol__FspPdcpDataInd));
  
  protocol__fsp_ctxt__init(ctxt);
  protocol__fsp_rlc_pdu__init(pdu);
  protocol__fsp_rlc_data__init(rlc_data);
  protocol__fsp_pdcp_data_ind__init(data_ind);
  
  // Copy data to the RlcPdu structure
  pdu->fsp_pdu_data.data =  malloc(args->sdu_size);
  pdu->fsp_pdu_data.len = args->sdu_size;
  
424
  memcpy(pdu->fsp_pdu_data.data, args->sdu_p, args->sdu_size); 
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
  pdu->has_fsp_pdu_data = 1;
  
  // Copy data to the ctxt structure
  ctxt->fsp_mod_id = args->ctxt->module_id;
  ctxt->fsp_enb_flag = args->ctxt->enb_flag;
  ctxt->fsp_instance = args->ctxt->instance;
  ctxt->fsp_rnti = args->ctxt->rnti;
  ctxt->fsp_frame = args->ctxt->frame;
  ctxt->fsp_subframe = args->ctxt->subframe;
  ctxt->fsp_enb_index = args->ctxt->eNB_index;

  ctxt->has_fsp_mod_id = 1;
  ctxt->has_fsp_enb_flag = 1;
  ctxt->has_fsp_instance = 1;
  ctxt->has_fsp_rnti = 1;
  ctxt->has_fsp_frame = 1;
  ctxt->has_fsp_subframe = 1;
  ctxt->has_fsp_enb_index = 1;

  rlc_data->fsp_ctxt = ctxt; 
  rlc_data->fsp_srb_flag = args->srb_flag;
  rlc_data->fsp_mbms_flag = args->MBMS_flag;
  rlc_data->fsp_rb_id = args->rb_id;

  rlc_data->fsp_sdu_buffer_size = args->sdu_size;
  rlc_data->fsp_pdu = pdu;
  rlc_data->has_fsp_srb_flag = 1;
  rlc_data->has_fsp_mbms_flag = 1;
  rlc_data->has_fsp_rb_id = 1;
  rlc_data->has_fsp_sdu_buffer_size = 1;

  // Up to here, everything is a signle message that is packed inside another. The final data_req 
  // will be created later, after the setting of all variables
  
  data_ind->header = header;
  data_ind->enb_id = mod_id;
  data_ind->has_enb_id = 1;
  data_ind->rlc_data = rlc_data;
  
 
  *msg = malloc(sizeof(Protocol__FlexsplitMessage));

  if(*msg == NULL)
    goto error;
  
  protocol__flexsplit_message__init(*msg);
471
  LOG_D(PROTO_AGENT,"setting the message case to %d\n", PROTOCOL__FLEXSPLIT_MESSAGE__MSG_DATA_IND_MSG);
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502

  (*msg)->msg_case = PROTOCOL__FLEXSPLIT_MESSAGE__MSG_DATA_IND_MSG;
  (*msg)->msg_dir = PROTOCOL__FLEXSPLIT_DIRECTION__INITIATING_MESSAGE; //we will be waiting for the ACK
  (*msg)->has_msg_dir = 1;
  (*msg)->data_ind_msg = data_ind; //data_req;
  
  return 0;
  
  error:
    if(header != NULL)
      free(header);
    if(pdu!=NULL)
      free(pdu);
    if(rlc_data!=NULL)
      free(rlc_data);
    if(data_ind!= NULL)
      free(data_ind);
    if(*msg != NULL)
      free(*msg);
    LOG_E(PROTO_AGENT, "%s: an error occured\n", __FUNCTION__);
    return -1;
  
}


int proto_agent_pdcp_data_ind_ack(mid_t mod_id, const void *params, Protocol__FlexsplitMessage **msg)
{
  Protocol__FspHeader *header;
  xid_t xid;
  rlc_op_status_t result = 0;
  
503
  LOG_D(PROTO_AGENT, "creating the data_ind_ack message\n");
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 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 571 572 573 574 575 576 577
  
  Protocol__FlexsplitMessage *input = (Protocol__FlexsplitMessage *)params;
  Protocol__FspPdcpDataInd *data_ind = input->data_ind_msg;
  
  xid = data_ind->header->xid;
  Protocol__FspRlcPdu *pdu = NULL;
  Protocol__FspCtxt *ctxt = NULL;
  Protocol__FspRlcData *rlc_data = NULL;
  
  
  rlc_data = data_ind->rlc_data;
  
  pdu = rlc_data->fsp_pdu;
  
  ctxt = rlc_data->fsp_ctxt;
  
  protocol_ctxt_t	 *ctxt_pP;
  srb_flag_t     	 srb_flagP = 0;
  rb_id_t        	 rb_idP = 0;
  sdu_size_t           	 pdcp_pdu_size = 0;
  MBMS_flag_t		 flag_MBMS = 0;
  mem_block_t 		 *pdcp_pdu_p = NULL;
  

  // Create a new protocol context for handling the packet
  
  ctxt_pP = malloc(sizeof(protocol_ctxt_t));
  ctxt_pP->module_id = ctxt->fsp_mod_id;
  ctxt_pP->enb_flag = ctxt->fsp_enb_flag;
  ctxt_pP->instance = ctxt->fsp_instance;
  ctxt_pP->rnti = ctxt->fsp_rnti;
  ctxt_pP->frame = ctxt->fsp_frame;
  ctxt_pP->subframe = ctxt->fsp_subframe;
  ctxt_pP->eNB_index = ctxt->fsp_enb_index;
  
  srb_flagP = rlc_data->fsp_srb_flag;
  flag_MBMS = rlc_data->fsp_mbms_flag;
  rb_idP = rlc_data->fsp_rb_id;
  pdcp_pdu_size = rlc_data->fsp_pdu->fsp_pdu_data.len; 
  pdcp_pdu_p = get_free_mem_block(pdcp_pdu_size);
  
  memcpy(pdcp_pdu_p->data, rlc_data->fsp_pdu->fsp_pdu_data.data, pdcp_pdu_size);
  
  pdcp_data_ind((const protocol_ctxt_t*) ctxt_pP, (const srb_flag_t) srb_flagP, (const MBMS_flag_t) flag_MBMS, (const rb_id_t) rb_idP, pdcp_pdu_size, pdcp_pdu_p);

  if (fsp_create_header(xid, PROTOCOL__FSP_TYPE__FSPT_PDCP_DATA_IND_ACK, &header) != 0)
    goto error;

  Protocol__FspPdcpDataIndAck *ack = NULL;
  ack = malloc(sizeof(Protocol__FspPdcpDataIndAck));
  protocol__fsp_pdcp_data_ind_ack__init(ack);
  
  ack->header = header;
  ack->result = result;
  ack->has_result = 1;
    
  *msg = malloc(sizeof(Protocol__FlexsplitMessage));
  if(*msg == NULL)
    goto error;
  
  protocol__flexsplit_message__init(*msg);
  (*msg)->msg_case = PROTOCOL__FLEXSPLIT_MESSAGE__MSG_DATA_IND_ACK;
  (*msg)->msg_dir = PROTOCOL__FLEXSPLIT_DIRECTION__SUCCESSFUL_OUTCOME;
  (*msg)->has_msg_dir = 1;
  (*msg)->data_req_ack = ack;
  return 0;
  
  error:
    if(header != NULL)
      free(header);
    if(ack!=NULL)
      free(ack);
    if(*msg != NULL)
      free(*msg);
578 579 580
    if (pdcp_pdu_p != NULL)
      free_mem_block(pdcp_pdu_p);

581 582 583 584 585 586 587 588 589 590
    LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
    return -1;

}


int proto_agent_destroy_pdcp_data_ind_ack(Protocol__FlexsplitMessage *msg) {
  if(msg->msg_case != PROTOCOL__FLEXSPLIT_MESSAGE__MSG_DATA_IND_ACK)
    goto error;
  
591 592 593
  free(msg->data_req_ack->header);
  free(msg->data_req_ack);
  free(msg);
594 595 596 597 598 599 600
  return 0;
  
  error:
    LOG_E(PROTO_AGENT, "%s: an error occured\n", __FUNCTION__);
    return -1;
}

601 602 603 604 605 606 607 608
int proto_agent_hello(mid_t mod_id, const void *params, Protocol__FlexsplitMessage **msg) {
 
  Protocol__FspHeader *header;
  /*TODO: Need to set random xid or xid from received hello message*/
  xid_t xid = 1;
  if (fsp_create_header(xid, PROTOCOL__FSP_TYPE__FSPT_HELLO, &header) != 0)
    goto error;

609
  LOG_D(PROTO_AGENT, "creating the HELLO message\n");
610
  Protocol__FspHello *hello_msg = NULL;
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
  hello_msg = malloc(sizeof(Protocol__FspHello));
  if(hello_msg == NULL)
    goto error;
  protocol__fsp_hello__init(hello_msg);
  hello_msg->header = header;

  *msg = malloc(sizeof(Protocol__FlexsplitMessage));
  if(*msg == NULL)
    goto error;
  
  protocol__flexsplit_message__init(*msg);
  (*msg)->msg_case = PROTOCOL__FLEXSPLIT_MESSAGE__MSG_HELLO_MSG;
  (*msg)->msg_dir = PROTOCOL__FLEXSPLIT_DIRECTION__SUCCESSFUL_OUTCOME;
  (*msg)->has_msg_dir = 1;
  (*msg)->hello_msg = hello_msg;
  return 0;
  
 error:
  if(header != NULL)
    free(header);
631
  if(hello_msg!=NULL)
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
    free(hello_msg);
  if(*msg != NULL)
    free(*msg);
  LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
  return -1;
}


int proto_agent_destroy_hello(Protocol__FlexsplitMessage *msg) {
  
  if(msg->msg_case != PROTOCOL__FLEXSPLIT_MESSAGE__MSG_HELLO_MSG)
    goto error;
  
  free(msg->hello_msg->header);
  free(msg->hello_msg);
  free(msg);
  return 0;

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

655 656
int proto_agent_echo_request(mid_t mod_id, const void* params, Protocol__FlexsplitMessage **msg) {
  Protocol__FspHeader *header;
657

658 659 660
  xid_t xid = 1;
  if (fsp_create_header(xid, PROTOCOL__FSP_TYPE__FSPT_ECHO_REQUEST, &header) != 0)
    goto error;
661
  LOG_D(PROTO_AGENT, "creating the echo request message\n");
662
  
663 664 665 666
  Protocol__FspEchoRequest *echo_request_msg = NULL;
  echo_request_msg = malloc(sizeof(Protocol__FspEchoRequest));
  if(echo_request_msg == NULL)
    goto error;
667

668 669
  protocol__fsp_echo_request__init(echo_request_msg);
  echo_request_msg->header = header;
670

671 672
  *msg = malloc(sizeof(Protocol__FlexsplitMessage));
  if(*msg == NULL)
673
    goto error;
674
  protocol__flexsplit_message__init(*msg);
675

676
  LOG_D(PROTO_AGENT,"setting the message direction to %d\n", PROTOCOL__FLEXSPLIT_MESSAGE__MSG_ECHO_REQUEST_MSG);
677 678 679 680 681
  (*msg)->msg_case = PROTOCOL__FLEXSPLIT_MESSAGE__MSG_ECHO_REQUEST_MSG;
  (*msg)->msg_dir = PROTOCOL__FLEXSPLIT_DIRECTION__INITIATING_MESSAGE;
  (*msg)->has_msg_dir = 1;
  (*msg)->echo_request_msg = echo_request_msg;
  return 0;
682 683

 error:
684 685 686 687 688 689 690
  if(header != NULL)
    free(header);
  if(echo_request_msg != NULL)
    free(echo_request_msg);
  if(*msg != NULL)
    free(*msg);
  return -1;
691 692
}

693 694 695
int proto_agent_destroy_echo_request(Protocol__FlexsplitMessage *msg) {
  if(msg->msg_case != PROTOCOL__FLEXSPLIT_MESSAGE__MSG_ECHO_REQUEST_MSG)
    goto error;
696
  
697 698 699
  free(msg->echo_request_msg->header);
  free(msg->echo_request_msg);
  free(msg);
700 701
  return 0;
  
702 703 704
 error:
  LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
  return -1;
705 706
}

707
int proto_agent_echo_reply(mid_t mod_id, const void *params, Protocol__FlexsplitMessage **msg) {
708
  
709 710 711 712
  xid_t xid;
  Protocol__FlexsplitMessage *input = (Protocol__FlexsplitMessage *)params;
  Protocol__FspEchoRequest *echo_req = input->echo_request_msg;
  xid = (echo_req->header)->xid;
713

714 715
  
  
716
  LOG_D(PROTO_AGENT, "creating the echo reply message\n");
717 718
  Protocol__FspHeader *header;
  if (fsp_create_header(xid, PROTOCOL__FSP_TYPE__FSPT_ECHO_REPLY, &header) != 0)
719 720
    goto error;

721 722 723 724 725 726
  Protocol__FspEchoReply *echo_reply_msg;
  echo_reply_msg = malloc(sizeof(Protocol__FspEchoReply));
  if(echo_reply_msg == NULL)
    goto error;
  protocol__fsp_echo_reply__init(echo_reply_msg);
  echo_reply_msg->header = header;
727

728 729 730 731 732 733 734 735
  *msg = malloc(sizeof(Protocol__FlexsplitMessage));
  if(*msg == NULL)
    goto error;
  protocol__flexsplit_message__init(*msg);
  (*msg)->msg_case = PROTOCOL__FLEXSPLIT_MESSAGE__MSG_ECHO_REPLY_MSG;
  (*msg)->msg_dir = PROTOCOL__FLEXSPLIT_DIRECTION__SUCCESSFUL_OUTCOME;
  (*msg)->has_msg_dir = 1;
  (*msg)->echo_reply_msg = echo_reply_msg;
736 737 738
  return 0;

 error:
739 740 741 742 743 744 745 746
  if(header != NULL)
    free(header);
  if(echo_reply_msg != NULL)
    free(echo_reply_msg);
  if(*msg != NULL)
    free(*msg);
  LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
  return -1;
747 748
}

749 750 751
int proto_agent_destroy_echo_reply(Protocol__FlexsplitMessage *msg) {
  if(msg->msg_case != PROTOCOL__FLEXSPLIT_MESSAGE__MSG_ECHO_REPLY_MSG)
    goto error;
752
  
753 754 755
  free(msg->echo_reply_msg->header);
  free(msg->echo_reply_msg);
  free(msg);
756
  return 0;
757 758 759 760
  
 error:
  LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
  return -1;
761 762
}
/*
763 764
 * get generic info from RAN
 */
765

766
void set_enb_vars(mid_t mod_id, ran_name_t ran){
767

768 769 770 771 772 773 774 775
  switch (ran){
  case RAN_LTE_OAI :
    enb[mod_id] =  (void *)&eNB_mac_inst[mod_id];
    enb_ue[mod_id] = (void *)&eNB_mac_inst[mod_id].UE_list;
    enb_rrc[mod_id] = (void *)&eNB_rrc_inst[mod_id];
    break;
  default :
    goto error;
776 777
  }

778
  return; 
779

780 781 782
 error:
  LOG_E(PROTO_AGENT, "unknown RAN name %d\n", ran);
}