flexran_agent_mac.c 71 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
 * 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
Cedric Roux's avatar
Cedric Roux committed
20
 */
21

22 23
/*! \file flexran_agent_mac.c
 * \brief FlexRAN agent message handler for MAC layer
24
 * \author Xenofon Foukas, Mohamed Kassem and Navid Nikaein
25 26 27 28
 * \date 2016
 * \version 0.1
 */

29 30 31 32
#include "flexran_agent_mac.h"
#include "flexran_agent_extern.h"
#include "flexran_agent_common.h"
#include "flexran_agent_mac_internal.h"
33
#include "flexran_agent_net_comm.h"
34
#include "flexran_agent_timer.h"
shahab SHARIATBAGHERI's avatar
shahab SHARIATBAGHERI committed
35
#include "flexran_agent_ran_api.h"
36

37
#include "LAYER2/MAC/mac_proto.h"
38

39 40
#include "liblfds700.h"

41
#include "common/utils/LOG/log.h"
42

43 44 45
/*Array containing the Agent-MAC interfaces*/
AGENT_MAC_xface *agent_mac_xface[NUM_MAX_ENB];

46 47 48 49 50 51
/* 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];

52
/* the slice config as kept in the underlying system */
53
Protocol__FlexSliceConfig *slice_config[MAX_NUM_SLICES];
54
/* a structure that keeps updates which will be reflected in slice_config later */
55
Protocol__FlexSliceConfig *sc_update[MAX_NUM_SLICES];
56 57 58
/* indicates whether sc_update contains new data */
int perform_slice_config_update_count = 1;
/* queue of incoming new UE<>slice association commands */
59
Protocol__FlexUeConfig *ue_slice_assoc_update[MAX_NUM_SLICES];
60
int n_ue_slice_assoc_updates = 0;
61 62
/* mutex for sc_update: do not receive new config and write it at the same time */
pthread_mutex_t sc_update_mtx = PTHREAD_MUTEX_INITIALIZER;
63

64

Cedric Roux's avatar
Cedric Roux committed
65
int flexran_agent_mac_stats_reply(mid_t mod_id,
66 67 68
          const report_config_t *report_config,
           Protocol__FlexUeStatsReport **ue_report,
           Protocol__FlexCellStatsReport **cell_report) {
69 70


71 72
  // Protocol__FlexHeader *header;
  int i, j, k;
73
  int UE_id;
74
  int cc_id = 0;
75
  int enb_id = mod_id;
76

77 78
  /* Allocate memory for list of UE reports */
  if (report_config->nr_ue > 0) {
79

Cedric Roux's avatar
Cedric Roux committed
80

81 82
          for (i = 0; i < report_config->nr_ue; i++) {

83
                UE_id = flexran_get_mac_ue_id(mod_id, i);
84

85 86
                ue_report[i]->rnti = report_config->ue_report_type[i].ue_rnti;
                ue_report[i]->has_rnti = 1;
87 88 89 90

                /* Check flag for creation of buffer status report */
                if (report_config->ue_report_type[i].ue_report_flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_BSR) {
                      //TODO should be automated
Cedric Roux's avatar
Cedric Roux committed
91
                        ue_report[i]->n_bsr = 4;
92 93 94 95 96
                        uint32_t *elem;
                        elem = (uint32_t *) malloc(sizeof(uint32_t)*ue_report[i]->n_bsr);
                        if (elem == NULL)
                               goto error;
                        for (j = 0; j < ue_report[i]->n_bsr; j++) {
Cedric Roux's avatar
Cedric Roux committed
97
                                // NN: we need to know the cc_id here, consider the first one
98
                                elem[j] = flexran_get_ue_bsr_ul_buffer_info (enb_id, i, j);
99
                        }
Cedric Roux's avatar
Cedric Roux committed
100

101
                        ue_report[i]->bsr = elem;
102
                        ue_report[i]->flags |= PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_BSR;
103
                }
Cedric Roux's avatar
Cedric Roux committed
104

105 106
                /* Check flag for creation of PHR report */
                if (report_config->ue_report_type[i].ue_report_flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_PHR) {
107
                        ue_report[i]->phr = flexran_get_ue_phr (enb_id, UE_id); // eNB_UE_list->UE_template[UE_PCCID(enb_id,UE_id)][UE_id].phr_info;
108
                        ue_report[i]->has_phr = 1;
109
                        ue_report[i]->flags |= PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_PHR;
Cedric Roux's avatar
Cedric Roux committed
110

111 112 113 114 115 116 117 118 119 120 121 122 123 124
                }

                /* Check flag for creation of RLC buffer status report */
                if (report_config->ue_report_type[i].ue_report_flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_RLC_BS) {
                        ue_report[i]->n_rlc_report = 3; // Set this to the number of LCs for this UE. This needs to be generalized for for LCs
                        Protocol__FlexRlcBsr ** rlc_reports;
                        rlc_reports = malloc(sizeof(Protocol__FlexRlcBsr *) * ue_report[i]->n_rlc_report);
                        if (rlc_reports == NULL)
                              goto error;

                        // 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__FlexRlcBsr));
125 126 127 128 129
                              if (rlc_reports[j] == NULL){
                                 for (k = 0; k < j; k++){
                                   free(rlc_reports[k]);
                                 }
                                 free(rlc_reports);
130
                                 goto error;
131
                              }
132 133 134
                              protocol__flex_rlc_bsr__init(rlc_reports[j]);
                              rlc_reports[j]->lc_id = j+1;
                              rlc_reports[j]->has_lc_id = 1;
135
                              rlc_reports[j]->tx_queue_size = flexran_get_tx_queue_size(enb_id, UE_id, j + 1);
136 137 138
                              rlc_reports[j]->has_tx_queue_size = 1;

                              //TODO:Set tx queue head of line delay in ms
139
                              rlc_reports[j]->tx_queue_hol_delay = flexran_get_hol_delay(enb_id, UE_id, j + 1);
140 141 142 143 144 145 146 147
                              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 = 0;
                              //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 = 0;
                              //TODO DONE:Set current size of the pending message in bytes
148
                              rlc_reports[j]->status_pdu_size = flexran_get_num_pdus_buffer(enb_id, UE_id, j + 1);
149 150 151 152 153 154
                              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;
155
                        ue_report[i]->flags |= PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_RLC_BS;
Cedric Roux's avatar
Cedric Roux committed
156

157 158 159 160 161
                }

                /* Check flag for creation of MAC CE buffer status report */
                if (report_config->ue_report_type[i].ue_report_flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_MAC_CE_BS) {
                        // TODO: Fill in the actual MAC CE buffer status report
162
                        ue_report[i]->pending_mac_ces = (flexran_get_MAC_CE_bitmap_TA(enb_id, UE_id, 0) | (0 << 1) | (0 << 2) | (0 << 3)) & 15;
shahab SHARIATBAGHERI's avatar
shahab SHARIATBAGHERI committed
163 164 165
                                      // Use as bitmap. Set one or more of the; /* Use as bitmap. Set one or more of the
                                       // PROTOCOL__FLEX_CE_TYPE__FLPCET_ values
                                       // found in stats_common.pb-c.h. See
Cedric Roux's avatar
Cedric Roux committed
166
                                       // flex_ce_type in FlexRAN specification
167
                        ue_report[i]->has_pending_mac_ces = 1;
168
                        ue_report[i]->flags |= PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_MAC_CE_BS;
169 170 171 172 173 174 175 176 177 178 179 180 181 182
                }

                /* Check flag for creation of DL CQI report */
                if (report_config->ue_report_type[i].ue_report_flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_DL_CQI) {
                        // TODO: Fill in the actual DL CQI report for the UE based on its configuration
                        Protocol__FlexDlCqiReport * dl_report;
                        dl_report = malloc(sizeof(Protocol__FlexDlCqiReport));
                        if (dl_report == NULL)
                          goto error;
                        protocol__flex_dl_cqi_report__init(dl_report);

                        dl_report->sfn_sn = flexran_get_sfn_sf(enb_id);
                        dl_report->has_sfn_sn = 1;
                        //Set the number of DL CQI reports for this UE. One for each CC
183
                        dl_report->n_csi_report = flexran_get_active_CC(enb_id, UE_id);
184 185 186 187
                        dl_report->n_csi_report = 1 ;
                        //Create the actual CSI reports.
                        Protocol__FlexDlCsi **csi_reports;
                        csi_reports = malloc(sizeof(Protocol__FlexDlCsi *)*dl_report->n_csi_report);
188 189
                        if (csi_reports == NULL) {
                          free(dl_report);
Cedric Roux's avatar
Cedric Roux committed
190
                          goto error;
191
                        }
192 193 194 195 196 197 198 199 200 201
                        for (j = 0; j < dl_report->n_csi_report; j++) {

                              csi_reports[j] = malloc(sizeof(Protocol__FlexDlCsi));
                              if (csi_reports[j] == NULL)
                                goto error;
                              protocol__flex_dl_csi__init(csi_reports[j]);
                              //The servCellIndex for this report
                              csi_reports[j]->serv_cell_index = j;
                              csi_reports[j]->has_serv_cell_index = 1;
                              //The rank indicator value for this cc
202
                              csi_reports[j]->ri = flexran_get_current_RI(enb_id, UE_id, j);
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
                              csi_reports[j]->has_ri = 1;
                              //TODO: the type of CSI report based on the configuration of the UE
                              //For now we only support 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 FlexRAN specifications
                              csi_reports[j]->type =  PROTOCOL__FLEX_CSI_TYPE__FLCSIT_P10;
                              csi_reports[j]->has_type = 1;
                              csi_reports[j]->report_case = PROTOCOL__FLEX_DL_CSI__REPORT_P10CSI;

                              if(csi_reports[j]->report_case == PROTOCOL__FLEX_DL_CSI__REPORT_P10CSI){

                                    Protocol__FlexCsiP10 *csi10;
                                    csi10 = malloc(sizeof(Protocol__FlexCsiP10));
                                    if (csi10 == NULL)
                                    goto error;
                                    protocol__flex_csi_p10__init(csi10);
                                    //TODO: set the wideband value
                                    // NN: this is also depends on cc_id
221
                                    csi10->wb_cqi = flexran_get_ue_wcqi (enb_id, UE_id); //eNB_UE_list->eNB_UE_stats[UE_PCCID(enb_id,UE_id)][UE_id].dl_cqi;
222 223 224 225 226 227 228 229 230 231 232 233 234
                                    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__FLEX_DL_CSI__REPORT_P11CSI){


                                    Protocol__FlexCsiP11 *csi11;
                                    csi11 = malloc(sizeof(Protocol__FlexCsiP11));
                                    if (csi11 == NULL)
                                    goto error;
                                    protocol__flex_csi_p11__init(csi11);
Cedric Roux's avatar
Cedric Roux committed
235 236

                                    csi11->wb_cqi = malloc(sizeof(csi11->wb_cqi));
Xenofon Foukas's avatar
Xenofon Foukas committed
237
				    csi11->n_wb_cqi = 1;
238
				    csi11->wb_cqi[0] = flexran_get_ue_wcqi (enb_id, UE_id);
Cedric Roux's avatar
Cedric Roux committed
239 240
                                    // According To spec 36.213

241 242
                                    if (flexran_get_antenna_ports(enb_id, j) == 2 && csi_reports[j]->ri == 1) {
                                        // TODO PMI
243
                                        csi11->wb_pmi = flexran_get_ue_wpmi(enb_id, UE_id, 0);
244 245
                                        csi11->has_wb_pmi = 1;

Cedric Roux's avatar
Cedric Roux committed
246
                                       }
247 248 249

                                      else if (flexran_get_antenna_ports(enb_id, j) == 2 && csi_reports[j]->ri == 2){
                                        // TODO PMI
250
                                        csi11->wb_pmi = flexran_get_ue_wpmi(enb_id, UE_id, 0);
251 252 253 254 255 256
                                        csi11->has_wb_pmi = 1;

                                      }

                                      else if (flexran_get_antenna_ports(enb_id, j) == 4 && csi_reports[j]->ri == 2){
                                        // TODO PMI
257
                                        csi11->wb_pmi = flexran_get_ue_wpmi(enb_id, UE_id, 0);
258 259 260 261 262
                                        csi11->has_wb_pmi = 1;


                                      }

Cedric Roux's avatar
Cedric Roux committed
263
                                      csi11->has_wb_pmi = 0;
264 265 266 267 268

                                      csi_reports[j]->p11csi = csi11;

                               }

Cedric Roux's avatar
Cedric Roux committed
269 270


271 272 273 274 275 276 277 278 279


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

                                    Protocol__FlexCsiP20 *csi20;
                                    csi20 = malloc(sizeof(Protocol__FlexCsiP20));
                                    if (csi20 == NULL)
                                    goto error;
                                    protocol__flex_csi_p20__init(csi20);
Cedric Roux's avatar
Cedric Roux committed
280

281
                                    csi20->wb_cqi = flexran_get_ue_wcqi (enb_id, UE_id);
282
                                    csi20->has_wb_cqi = 1;
283

Cedric Roux's avatar
Cedric Roux committed
284

285 286
                                    csi20->bandwidth_part_index = 1 ;//TODO
                                    csi20->has_bandwidth_part_index = 1;
287

288
                                    csi20->sb_index = 1 ;//TODO
Cedric Roux's avatar
Cedric Roux committed
289
                                    csi20->has_sb_index = 1 ;
290 291


292
                                    csi_reports[j]->p20csi = csi20;
293

294

295
                              }
296

297
                              else if(csi_reports[j]->report_case == PROTOCOL__FLEX_DL_CSI__REPORT_P21CSI){
298

299 300 301 302 303
                                  // Protocol__FlexCsiP21 *csi21;
                                  // csi21 = malloc(sizeof(Protocol__FlexCsiP21));
                                  // if (csi21 == NULL)
                                  // goto error;
                                  // protocol__flex_csi_p21__init(csi21);
Cedric Roux's avatar
Cedric Roux committed
304

305
                                  // csi21->wb_cqi = flexran_get_ue_wcqi (enb_id, UE_id);
Cedric Roux's avatar
Cedric Roux committed
306 307


308 309
                                  // csi21->wb_pmi = flexran_get_ue_pmi(enb_id); //TDO inside
                                  // csi21->has_wb_pmi = 1;
310

Cedric Roux's avatar
Cedric Roux committed
311 312
                                  // csi21->sb_cqi = 1; // TODO

313
                                  // csi21->bandwidth_part_index = 1 ; //TDO inside
Cedric Roux's avatar
Cedric Roux committed
314
                                  // csi21->has_bandwidth_part_index = 1 ;
315

316
                                  // csi21->sb_index = 1 ;//TODO
Cedric Roux's avatar
Cedric Roux committed
317
                                  // csi21->has_sb_index = 1 ;
318

319

320
                                  // csi_reports[j]->p20csi = csi21;
321

322
                              }
323

324
                              else if(csi_reports[j]->report_case == PROTOCOL__FLEX_DL_CSI__REPORT_A12CSI){
325

326

327 328 329 330 331
                                  // Protocol__FlexCsiA12 *csi12;
                                  // csi12 = malloc(sizeof(Protocol__FlexCsiA12));
                                  // if (csi12 == NULL)
                                  // goto error;
                                  // protocol__flex_csi_a12__init(csi12);
Cedric Roux's avatar
Cedric Roux committed
332

333
                                  // csi12->wb_cqi = flexran_get_ue_wcqi (enb_id, UE_id);
Cedric Roux's avatar
Cedric Roux committed
334 335

                                  // csi12->sb_pmi = 1 ; //TODO inside
336

337 338
                                  // TODO continou
                              }
339

340
                              else if(csi_reports[j]->report_case == PROTOCOL__FLEX_DL_CSI__REPORT_A22CSI){
341

342 343 344 345 346
                                    // Protocol__FlexCsiA22 *csi22;
                                    // csi22 = malloc(sizeof(Protocol__FlexCsiA22));
                                    // if (csi22 == NULL)
                                    // goto error;
                                    // protocol__flex_csi_a22__init(csi22);
347

348
                                    // csi22->wb_cqi = flexran_get_ue_wcqi (enb_id, UE_id);
Cedric Roux's avatar
Cedric Roux committed
349 350 351

                                    // csi22->sb_cqi = 1 ; //TODO inside

352
                                    // csi22->wb_pmi = flexran_get_ue_wcqi (enb_id, UE_id);
353
                                    // csi22->has_wb_pmi = 1;
Cedric Roux's avatar
Cedric Roux committed
354 355

                                    // csi22->sb_pmi = 1 ; //TODO inside
356
                                    // csi22->has_wb_pmi = 1;
357

358
                                    // csi22->sb_list = flexran_get_ue_wcqi (enb_id, UE_id);
359

360 361 362 363 364 365 366 367 368 369 370

                                }

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

                                    // Protocol__FlexCsiA20 *csi20;
                                    // csi20 = malloc(sizeof(Protocol__FlexCsiA20));
                                    // if (csi20 == NULL)
                                    // goto error;
                                    // protocol__flex_csi_a20__init(csi20);

371
                                    // csi20->wb_cqi = flexran_get_ue_wcqi (enb_id, UE_id);
372 373
                                    // csi20->has_wb_cqi = 1;

Cedric Roux's avatar
Cedric Roux committed
374
                                    // csi20>sb_cqi = 1 ; //TODO inside
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
                                    // csi20>has_sb_cqi = 1 ;

                                    // csi20->sb_list = 1; // TODO inside


                                }

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

                                }

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

                                }

                        }
                     //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;
395
                    ue_report[i]->flags |= PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_DL_CQI;
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
                }

                /* Check flag for creation of paging buffer status report */
                if (report_config->ue_report_type[i].ue_report_flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_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__FlexPagingBufferReport *paging_report;
                            paging_report = malloc(sizeof(Protocol__FlexPagingBufferReport));
                            if (paging_report == NULL)
                              goto error;
                            protocol__flex_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__FlexPagingInfo **p_info;
                            p_info = malloc(sizeof(Protocol__FlexPagingInfo *) * paging_report->n_paging_info);
412 413
                            if (p_info == NULL){
                              free(paging_report);
414
                              goto error;
415
                            }
416 417 418 419

                            for (j = 0; j < paging_report->n_paging_info; j++) {

                                    p_info[j] = malloc(sizeof(Protocol__FlexPagingInfo));
420 421
                                    if(p_info[j] == NULL){
                                      free(paging_report);
422
                                      goto error;
423
                                    }
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
                                    protocol__flex_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;
444
                            ue_report[i]->flags |= PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_PBS;
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
                }

                  /* Check flag for creation of UL CQI report */
                if (report_config->ue_report_type[i].ue_report_flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_UL_CQI) {

                      //Fill in the full UL CQI report of the UE
                      Protocol__FlexUlCqiReport *full_ul_report;
                      full_ul_report = malloc(sizeof(Protocol__FlexUlCqiReport));
                      if(full_ul_report == NULL)
                        goto error;
                      protocol__flex_ul_cqi_report__init(full_ul_report);
                      //TODO:Set the SFN and SF of the generated report
                      full_ul_report->sfn_sn = flexran_get_sfn_sf(enb_id);
                      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__FlexUlCqi **ul_report;
                      ul_report = malloc(sizeof(Protocol__FlexUlCqi *) * full_ul_report->n_cqi_meas);
464 465
                      if(ul_report == NULL) {
                        free(full_ul_report);
466
                        goto error;
467
                      }
468 469 470 471
                      //Fill each UL report of the UE for each of the configured report types
                      for(j = 0; j < full_ul_report->n_cqi_meas; j++) {

                              ul_report[j] = malloc(sizeof(Protocol__FlexUlCqi));
472 473 474 475
                              if(ul_report[j] == NULL) {
                                free(full_ul_report);
                                goto error;
                              }
476 477 478 479 480 481 482 483 484 485
                              protocol__flex_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 flex_ul_cqi_type in FlexRAN specification for more details
                              ul_report[j]->type = PROTOCOL__FLEX_UL_CQI_TYPE__FLUCT_SRS;
                              ul_report[j]->has_type = 1;
                              //TODO:Set the number of SINR measurements based on the report type
                              //See struct flex_ul_cqi in FlexRAN specification for more details
                              ul_report[j]->n_sinr = 0;
                              uint32_t *sinr_meas;
                              sinr_meas = (uint32_t *) malloc(sizeof(uint32_t) * ul_report[j]->n_sinr);
486 487
                              if (sinr_meas == NULL) {
                                free(full_ul_report);
488
                                goto error;
489
                              }
490 491 492 493 494 495 496 497
                              //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;
Cedric Roux's avatar
Cedric Roux committed
498

499 500 501 502 503 504 505 506 507 508 509 510 511
                              //Set the list of UL reports of this UE to the full UL report
                              full_ul_report->cqi_meas = ul_report;

                              full_ul_report->n_pucch_dbm = MAX_NUM_CCs;
                              full_ul_report->pucch_dbm = malloc(sizeof(Protocol__FlexPucchDbm *) * full_ul_report->n_pucch_dbm);

                              for (j = 0; j < MAX_NUM_CCs; j++) {

                                      full_ul_report->pucch_dbm[j] = malloc(sizeof(Protocol__FlexPucchDbm));
                                      protocol__flex_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;

512 513
                                      if(flexran_get_p0_pucch_dbm(enb_id, UE_id, j) != -1){
                                        full_ul_report->pucch_dbm[j]->p0_pucch_dbm = flexran_get_p0_pucch_dbm(enb_id, UE_id, j);
514 515 516 517 518 519 520 521
                                        full_ul_report->pucch_dbm[j]->has_p0_pucch_dbm = 1;
                                      }
                              }


                          }
                        //  Add full UL CQI report to the UE report
                        ue_report[i]->ul_cqi_report = full_ul_report;
522
                        ue_report[i]->flags |= PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_UL_CQI;
Cedric Roux's avatar
Cedric Roux committed
523 524

                     }
525 526 527 528 529 530 531
                      if (report_config->ue_report_type[i].ue_report_flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_MAC_STATS) {

                            Protocol__FlexMacStats *macstats;
                            macstats = malloc(sizeof(Protocol__FlexMacStats));
                            if (macstats == NULL)
                              goto error;
                            protocol__flex_mac_stats__init(macstats);
Cedric Roux's avatar
Cedric Roux committed
532

533

534
                            macstats->total_bytes_sdus_dl = flexran_get_total_size_dl_mac_sdus(mod_id, UE_id, cc_id);
535 536
                            macstats->has_total_bytes_sdus_dl = 1;

537
                            macstats->total_bytes_sdus_ul = flexran_get_total_size_ul_mac_sdus(mod_id, UE_id, cc_id);
538 539
                            macstats->has_total_bytes_sdus_ul = 1;

540
                            macstats->tbs_dl = flexran_get_TBS_dl(mod_id, UE_id, cc_id);
541 542
                            macstats->has_tbs_dl = 1;

543
                            macstats->tbs_ul = flexran_get_TBS_ul(mod_id, UE_id, cc_id);
544 545
                            macstats->has_tbs_ul = 1;

546
                            macstats->prb_retx_dl = flexran_get_num_prb_retx_dl_per_ue(mod_id, UE_id, cc_id);
547 548
                            macstats->has_prb_retx_dl = 1;

549
                            macstats->prb_retx_ul = flexran_get_num_prb_retx_ul_per_ue(mod_id, UE_id, cc_id);
550 551
                            macstats->has_prb_retx_ul = 1;

552
                            macstats->prb_dl = flexran_get_num_prb_dl_tx_per_ue(mod_id, UE_id, cc_id);
553 554
                            macstats->has_prb_dl = 1;

555
                            macstats->prb_ul = flexran_get_num_prb_ul_rx_per_ue(mod_id, UE_id, cc_id);
556 557
                            macstats->has_prb_ul = 1;

558
                            macstats->mcs1_dl = flexran_get_mcs1_dl(mod_id, UE_id, cc_id);
559 560
                            macstats->has_mcs1_dl = 1;

561
                            macstats->mcs2_dl = flexran_get_mcs2_dl(mod_id, UE_id, cc_id);
562 563
                            macstats->has_mcs2_dl = 1;

564
                            macstats->mcs1_ul = flexran_get_mcs1_ul(mod_id, UE_id, cc_id);
565 566
                            macstats->has_mcs1_ul = 1;

567
                            macstats->mcs2_ul = flexran_get_mcs2_ul(mod_id, UE_id, cc_id);
568 569
                            macstats->has_mcs2_ul = 1;

570
                            macstats->total_prb_dl = flexran_get_total_prb_dl_tx_per_ue(mod_id, UE_id, cc_id);
571 572
                            macstats->has_total_prb_dl = 1;

573
                            macstats->total_prb_ul = flexran_get_total_prb_ul_rx_per_ue(mod_id, UE_id, cc_id);
574 575
                            macstats->has_total_prb_ul = 1;

576
                            macstats->total_pdu_dl = flexran_get_total_num_pdu_dl(mod_id, UE_id, cc_id);
577 578
                            macstats->has_total_pdu_dl = 1;

579
                            macstats->total_pdu_ul = flexran_get_total_num_pdu_ul(mod_id, UE_id, cc_id);
580 581
                            macstats->has_total_pdu_ul = 1;

582
                            macstats->total_tbs_dl = flexran_get_total_TBS_dl(mod_id, UE_id, cc_id);
583 584
                            macstats->has_total_tbs_dl = 1;

585
                            macstats->total_tbs_ul = flexran_get_total_TBS_ul(mod_id, UE_id, cc_id);
586 587
                            macstats->has_total_tbs_ul = 1;

588
                            macstats->harq_round = flexran_get_harq_round(mod_id, cc_id, UE_id);
589 590
                            macstats->has_harq_round = 1;

Cedric Roux's avatar
Cedric Roux committed
591
                            Protocol__FlexMacSdusDl ** mac_sdus;
592
                            mac_sdus = malloc(sizeof(Protocol__FlexMacSdusDl) * flexran_get_num_mac_sdu_tx(mod_id, UE_id, cc_id));
593 594 595
                            if (mac_sdus == NULL)
                                goto error;

596
                            macstats->n_mac_sdus_dl = flexran_get_num_mac_sdu_tx(mod_id, UE_id, cc_id);
Cedric Roux's avatar
Cedric Roux committed
597

598 599
                            for (j = 0; j < macstats->n_mac_sdus_dl; j++){

Cedric Roux's avatar
Cedric Roux committed
600

601 602 603
                                mac_sdus[j] = malloc(sizeof(Protocol__FlexMacSdusDl));
                                protocol__flex_mac_sdus_dl__init(mac_sdus[j]);

604
                                mac_sdus[j]->lcid = flexran_get_mac_sdu_lcid_index(mod_id, UE_id, cc_id, j);
605 606
                                mac_sdus[j]->has_lcid = 1;

607
                                mac_sdus[j]->sdu_length = flexran_get_mac_sdu_size(mod_id, UE_id, cc_id, mac_sdus[j]->lcid);
608 609 610 611 612 613
                                mac_sdus[j]->has_sdu_length = 1;


                            }


Cedric Roux's avatar
Cedric Roux committed
614 615 616 617
                            macstats->mac_sdus_dl = mac_sdus;


                        ue_report[i]->mac_stats = macstats;
618
                        ue_report[i]->flags |= PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_MAC_STATS;
Cedric Roux's avatar
Cedric Roux committed
619 620 621 622
               }



623

Cedric Roux's avatar
Cedric Roux committed
624
             }
625 626


Cedric Roux's avatar
Cedric Roux committed
627 628 629


     }
630 631 632

  /* Allocate memory for list of cell reports */
  if (report_config->nr_cc > 0) {
Cedric Roux's avatar
Cedric Roux committed
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
            // Fill in the Cell reports
            for (i = 0; i < report_config->nr_cc; i++) {


                      /* Check flag for creation of noise and interference report */
                      if(report_config->cc_report_type[i].cc_report_flags & PROTOCOL__FLEX_CELL_STATS_TYPE__FLCST_NOISE_INTERFERENCE) {
                            // TODO: Fill in the actual noise and interference report for this cell
                            Protocol__FlexNoiseInterferenceReport *ni_report;
                            ni_report = malloc(sizeof(Protocol__FlexNoiseInterferenceReport));
                            if(ni_report == NULL)
                              goto error;
                            protocol__flex_noise_interference_report__init(ni_report);
                            // Current frame and subframe number
                            ni_report->sfn_sf = flexran_get_sfn_sf(enb_id);
                            ni_report->has_sfn_sf = 1;
                            //TODO:Received interference power in dbm
                            ni_report->rip = 0;
                            ni_report->has_rip = 1;
                            //TODO:Thermal noise power in dbm
                            ni_report->tnp = 0;
                            ni_report->has_tnp = 1;

                            ni_report->p0_nominal_pucch = flexran_get_p0_nominal_pucch(enb_id, 0);
                            ni_report->has_p0_nominal_pucch = 1;
                            cell_report[i]->noise_inter_report = ni_report;
660
                            cell_report[i]->flags |= PROTOCOL__FLEX_CELL_STATS_TYPE__FLCST_NOISE_INTERFERENCE;
661 662 663
                      }
            }

Cedric Roux's avatar
Cedric Roux committed
664 665 666



667
  }
668

669
  return 0;
670

671
 error:
672 673 674 675 676 677

  if (cell_report != NULL)
        free(cell_report);
  if (ue_report != NULL)
        free(ue_report);

678 679 680
  return -1;
}

681
int flexran_agent_mac_destroy_stats_reply(Protocol__FlexStatsReply *reply) {
682
  int i, j, k;
683

684 685 686
  Protocol__FlexDlCqiReport *dl_report;
  Protocol__FlexUlCqiReport *ul_report;
  Protocol__FlexPagingBufferReport *paging_report;
687 688 689 690 691 692 693 694 695

  // 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
696
    if (reply->ue_report[i]->flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_DL_CQI) {
697 698 699 700 701
      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) {
702
	case PROTOCOL__FLEX_DL_CSI__REPORT_P10CSI:
703 704
	  free(dl_report->csi_report[j]->p10csi);
	  break;
705
	case PROTOCOL__FLEX_DL_CSI__REPORT_P11CSI:
706 707 708
	  free(dl_report->csi_report[j]->p11csi->wb_cqi);
	  free(dl_report->csi_report[j]->p11csi);
	  break;
709
	case PROTOCOL__FLEX_DL_CSI__REPORT_P20CSI:
710 711
	  free(dl_report->csi_report[j]->p20csi);
	  break;
712
	case PROTOCOL__FLEX_DL_CSI__REPORT_P21CSI:
713 714 715 716
	  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;
717
	case PROTOCOL__FLEX_DL_CSI__REPORT_A12CSI:
718 719 720 721
	  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;
722
	case PROTOCOL__FLEX_DL_CSI__REPORT_A22CSI:
723 724 725 726 727
	  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;
728
	case PROTOCOL__FLEX_DL_CSI__REPORT_A20CSI:
729 730 731
	  free(dl_report->csi_report[j]->a20csi->sb_list);
	  free(dl_report->csi_report[j]->a20csi);
	  break;
732
	case PROTOCOL__FLEX_DL_CSI__REPORT_A30CSI:
733 734 735
	  free(dl_report->csi_report[j]->a30csi->sb_cqi);
	  free(dl_report->csi_report[j]->a30csi);
	  break;
736
	case PROTOCOL__FLEX_DL_CSI__REPORT_A31CSI:
737 738 739 740 741 742
	  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;
743 744
	default:
	  break;
745 746
	}

747 748 749 750 751 752
	free(dl_report->csi_report[j]);
      }
      free(dl_report->csi_report);
      free(dl_report);
    }
    // If Paging buffer report flag was set
753
    if (reply->ue_report[i]->flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_PBS) {
754 755 756 757 758 759 760 761 762
      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
763
    if (reply->ue_report[i]->flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_UL_CQI) {
764 765 766 767 768 769
      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);
770 771 772 773
      for (j = 0; j < ul_report->n_pucch_dbm; j++) {
	free(ul_report->pucch_dbm[j]);
      }
      free(ul_report->pucch_dbm);
774 775 776 777 778 779 780
      free(ul_report);
    }
    if (reply->ue_report[i]->flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_MAC_STATS) {
      for (j = 0; j < reply->ue_report[i]->mac_stats->n_mac_sdus_dl; j++)
        free(reply->ue_report[i]->mac_stats->mac_sdus_dl[j]);
      free(reply->ue_report[i]->mac_stats->mac_sdus_dl);
      free(reply->ue_report[i]->mac_stats);
781 782 783 784 785
    }
  }

  // Free memory for all Cell reports
  for (i = 0; i < reply->n_cell_report; i++) {
786 787 788
    if (reply->cell_report[i]->flags & PROTOCOL__FLEX_CELL_STATS_TYPE__FLCST_NOISE_INTERFERENCE) {
      free(reply->cell_report[i]->noise_inter_report);
    }
789
  }
790

791 792
  return 0;
}
793

794
int flexran_agent_mac_sr_info(mid_t mod_id, const void *params, Protocol__FlexranMessage **msg) {
795
  Protocol__FlexHeader *header = NULL;
796 797 798
  int i;
  const int xid = *((int *)params);

799 800
  Protocol__FlexUlSrInfo *ul_sr_info_msg;
  ul_sr_info_msg = malloc(sizeof(Protocol__FlexUlSrInfo));
801 802 803
  if (ul_sr_info_msg == NULL) {
    goto error;
  }
804
  protocol__flex_ul_sr_info__init(ul_sr_info_msg);
805

806 807 808
  if (flexran_create_header(xid, PROTOCOL__FLEX_TYPE__FLPT_UL_SR_INFO, &header) != 0)
    goto error;

809 810
  ul_sr_info_msg->header = header;
  ul_sr_info_msg->has_sfn_sf = 1;
811
  ul_sr_info_msg->sfn_sf = flexran_get_sfn_sf(mod_id);
812 813 814 815 816 817 818 819 820 821 822
  /*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;
  }
823

824
  *msg = malloc(sizeof(Protocol__FlexranMessage));
825 826
  if(*msg == NULL)
    goto error;
827 828 829
  protocol__flexran_message__init(*msg);
  (*msg)->msg_case = PROTOCOL__FLEXRAN_MESSAGE__MSG_UL_SR_INFO_MSG;
  (*msg)->msg_dir =  PROTOCOL__FLEXRAN_DIRECTION__INITIATING_MESSAGE;
830 831
  (*msg)->ul_sr_info_msg = ul_sr_info_msg;
  return 0;
832

833 834 835 836 837 838 839 840 841 842 843 844 845 846
 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;
}

847
int flexran_agent_mac_destroy_sr_info(Protocol__FlexranMessage *msg) {
848
   if(msg->msg_case != PROTOCOL__FLEXRAN_MESSAGE__MSG_UL_SR_INFO_MSG)
849 850 851 852 853 854 855 856 857 858 859 860 861
     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;
}

862
int flexran_agent_mac_sf_trigger(mid_t mod_id, const void *params, Protocol__FlexranMessage **msg) {
863
  Protocol__FlexHeader *header = NULL;
864
  int i, j, UE_id;
Cedric Roux's avatar
Cedric Roux committed
865

866
  int available_harq[MAX_MOBILES_PER_ENB];
Cedric Roux's avatar
Cedric Roux committed
867

868 869
  const int xid = *((int *)params);

870

871 872
  Protocol__FlexSfTrigger *sf_trigger_msg;
  sf_trigger_msg = malloc(sizeof(Protocol__FlexSfTrigger));
873
  if (sf_trigger_msg == NULL) {
874 875
    goto error;
  }
876
  protocol__flex_sf_trigger__init(sf_trigger_msg);
877

878 879 880
  if (flexran_create_header(xid, PROTOCOL__FLEX_TYPE__FLPT_SF_TRIGGER, &header) != 0)
    goto error;

881 882 883
  frame_t frame;
  sub_frame_t subframe;

884
  for (i = 0; i < MAX_MOBILES_PER_ENB; i++) {
885 886 887 888
    available_harq[i] = -1;
  }

  int ahead_of_time = 0;
Cedric Roux's avatar
Cedric Roux committed
889

890 891
  frame = (frame_t) flexran_get_current_system_frame_num(mod_id);
  subframe = (sub_frame_t) flexran_get_current_subframe(mod_id);
892 893

  subframe = ((subframe + ahead_of_time) % 10);
Cedric Roux's avatar
Cedric Roux committed
894

895
  if (subframe < flexran_get_current_subframe(mod_id)) {
896
    frame = (frame + 1) % 1024;
897 898
  }

899 900 901
  int additional_frames = ahead_of_time / 10;
  frame = (frame + additional_frames) % 1024;

902 903
  sf_trigger_msg->header = header;
  sf_trigger_msg->has_sfn_sf = 1;
904
  sf_trigger_msg->sfn_sf = flexran_get_future_sfn_sf(mod_id, 3);
905

906 907
  sf_trigger_msg->n_dl_info = 0;

908
  for (i = 0; i < MAX_MOBILES_PER_ENB; i++) {
909
    for (j = 0; j < 8; j++) {
910
      if (RC.mac && RC.mac[mod_id] && RC.mac[mod_id]->UE_list.eNB_UE_stats[UE_PCCID(mod_id,i)][i].harq_pid == 1) {
911 912 913 914 915
	available_harq[i] = j;
	break;
      }
    }
  }
Cedric Roux's avatar
Cedric Roux committed
916

917 918 919

  //  LOG_I(FLEXRAN_AGENT, "Sending subframe trigger for frame %d and subframe %d\n", flexran_get_current_frame(mod_id), (flexran_get_current_subframe(mod_id) + 1) % 10);

920
  sf_trigger_msg->n_dl_info = flexran_get_mac_num_ues(mod_id);
921

922
  Protocol__FlexDlInfo **dl_info = NULL;
923

924
  if (sf_trigger_msg->n_dl_info > 0) {
925
    dl_info = malloc(sizeof(Protocol__FlexDlInfo *) * sf_trigger_msg->n_dl_info);
926 927
    if(dl_info == NULL)
      goto error;
928
    i = -1;
929
    //Fill the status of the current HARQ process for each UE
930 931
    for(i = 0; i < sf_trigger_msg->n_dl_info; i++) {
      if (available_harq[i] < 0)
932
	continue;
933
      dl_info[i] = malloc(sizeof(Protocol__FlexDlInfo));
934 935
      if(dl_info[i] == NULL)
	goto error;
936
      UE_id = flexran_get_mac_ue_id(mod_id, i);
937
      protocol__flex_dl_info__init(dl_info[i]);
938
      dl_info[i]->rnti = flexran_get_mac_ue_crnti(mod_id, UE_id);
939
      dl_info[i]->has_rnti = 1;
940
      /*Fill in the right id of this round's HARQ process for this UE*/
941 942
      //      uint8_t harq_id;
      //uint8_t harq_status;
943
      //      flexran_get_harq(mod_id, UE_PCCID(mod_id, UE_id), i, frame, subframe, &harq_id, &harq_status);
Cedric Roux's avatar
Cedric Roux committed
944 945


946
      dl_info[i]->harq_process_id = available_harq[UE_id];
947
      if (RC.mac && RC.mac[mod_id])
948
        RC.mac[mod_id]->UE_list.eNB_UE_stats[UE_PCCID(mod_id, UE_id)][UE_id].harq_pid = 0;
949
      dl_info[i]->has_harq_process_id = 1;
950
      /* Fill in the status of the HARQ process (2 TBs)*/
951 952
      dl_info[i]->n_harq_status = 2;
      dl_info[i]->harq_status = malloc(sizeof(uint32_t) * dl_info[i]->n_harq_status);
953
      for (j = 0; j < dl_info[i]->n_harq_status; j++) {
954
        dl_info[i]->harq_status[j] = RC.mac[mod_id]->UE_list.UE_sched_ctrl[UE_id].round[UE_PCCID(mod_id, UE_id)][j];
955
        // TODO: This should be different per TB
956 957 958 959
      }
      //      LOG_I(FLEXRAN_AGENT, "Sending subframe trigger for frame %d and subframe %d and harq %d (round %d)\n", flexran_get_current_frame(mod_id), (flexran_get_current_subframe(mod_id) + 1) % 10, dl_info[i]->harq_process_id, dl_info[i]->harq_status[0]);
      if(dl_info[i]->harq_status[0] > 0) {
	//	LOG_I(FLEXRAN_AGENT, "[Frame %d][Subframe %d]Need to make a retransmission for harq %d (round %d)\n", flexran_get_current_frame(mod_id), flexran_get_current_subframe(mod_id), dl_info[i]->harq_process_id, dl_info[i]->harq_status[0]);
960
      }
961
      /*Fill in the serving cell index for this UE */
962
      dl_info[i]->serv_cell_index = UE_PCCID(mod_id, UE_id);
963 964 965
      dl_info[i]->has_serv_cell_index = 1;
    }
  }
966

967
  sf_trigger_msg->dl_info = dl_info;
968

969 970
  /* Fill in the number of UL reception status related info, based on the number of currently
   * transmitting UEs
971
   */
972
  sf_trigger_msg->n_ul_info = flexran_get_mac_num_ues(mod_id);
973

974
  Protocol__FlexUlInfo **ul_info = NULL;
975

976
  if (sf_trigger_msg->n_ul_info > 0) {
977
    ul_info = malloc(sizeof(Protocol__FlexUlInfo *) * sf_trigger_msg->n_ul_info);
978 979 980
    if(ul_info == NULL)
      goto error;
    //Fill the reception info for each transmitting UE
981
    for(i = 0; i < sf_trigger_msg->n_ul_info; i++) {
982
      ul_info[i] = malloc(sizeof(Protocol__FlexUlInfo));
983 984
      if(ul_info[i] == NULL)
	goto error;
985
      protocol__flex_ul_info__init(ul_info[i]);
986

987
      UE_id = flexran_get_mac_ue_id(mod_id, i);
988

989
      ul_info[i]->rnti = flexran_get_mac_ue_crnti(mod_id, UE_id);
990
      ul_info[i]->has_rnti = 1;
Robert Schmidt's avatar
Robert Schmidt committed
991 992
      /* Fill in the Tx power control command for this UE (if available),
       * primary carrier */
993
      if(flexran_get_tpc(mod_id, UE_id, 0) != 1){
Robert Schmidt's avatar
Robert Schmidt committed
994
          /* assume primary carrier */
995
          ul_info[i]->tpc = flexran_get_tpc(mod_id, UE_id, 0);
MKassem's avatar
MKassem committed
996 997 998
          ul_info[i]->has_tpc = 1;
      }
      else{
Robert Schmidt's avatar
Robert Schmidt committed
999
          /* assume primary carrier */
1000
          ul_info[i]->tpc = flexran_get_tpc(mod_id, UE_id, 0);
MKassem's avatar
MKassem committed
1001 1002
    	  ul_info[i]->has_tpc = 0;
      }
1003
      /*TODO: fill in the amount of data in bytes in the MAC SDU received in this subframe for the
1004
	given logical channel*/
1005
      ul_info[i]->n_ul_reception = 0;
1006
      ul_info[i]->ul_reception = malloc(sizeof(uint32_t) * ul_info[i]->n_ul_reception);
1007
      for (j = 0; j < ul_info[i]->n_ul_reception; j++) {
1008 1009 1010
	ul_info[i]->ul_reception[j] = 100;
      }
      /*TODO: Fill in the reception status for each UEs data*/
1011
      ul_info[i]->reception_status = PROTOCOL__FLEX_RECEPTION_STATUS__FLRS_OK;
1012
      ul_info[i]->has_reception_status = 1;
1013
      /*Fill in the serving cell index for this UE */
1014
      ul_info[i]->serv_cell_index = UE_PCCID(mod_id, UE_id);
1015 1016 1017
      ul_info[i]->has_serv_cell_index = 1;
    }
  }
1018

1019 1020
  sf_trigger_msg->ul_info = ul_info;

1021
  *msg = malloc(sizeof(Protocol__FlexranMessage));
1022 1023
  if(*msg == NULL)
    goto error;
1024 1025 1026
  protocol__flexran_message__init(*msg);
  (*msg)->msg_case = PROTOCOL__FLEXRAN_MESSAGE__MSG_SF_TRIGGER_MSG;
  (*msg)->msg_dir =  PROTOCOL__FLEXRAN_DIRECTION__INITIATING_MESSAGE;
1027
  (*msg)->sf_trigger_msg = sf_trigger_msg;
1028
  return 0;
1029

1030 1031 1032
 error:
  if (header != NULL)
    free(header);
1033 1034 1035
  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);
1036
      free(sf_trigger_msg->dl_info[i]);
1037
    }
1038 1039 1040
    for (i = 0; i < sf_trigger_msg->n_ul_info; i++) {
      free(sf_trigger_msg->ul_info[i]);
    }
Cedric Roux's avatar
Cedric Roux committed
1041
    free(sf_trigger_msg->dl_info);
1042 1043
    free(sf_trigger_msg->ul_info);
    free(sf_trigger_msg);
1044 1045 1046 1047 1048 1049 1050
  }
  if(*msg != NULL)
    free(*msg);
  //LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
  return -1;
}

1051
int flexran_agent_mac_destroy_sf_trigger(Protocol__FlexranMessage *msg) {
1052
  int i;
1053
  if(msg->msg_case != PROTOCOL__FLEXRAN_MESSAGE__MSG_SF_TRIGGER_MSG)
1054
    goto error;
1055

1056 1057 1058
  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);
1059
    free(msg->sf_trigger_msg->dl_info[i]);
1060
  }
1061 1062
  free(msg->sf_trigger_msg->dl_info);
  for (i = 0; i < msg->sf_trigger_msg->n_ul_info; i++) {
1063 1064
    free(msg->sf_trigger_msg->ul_info[i]->ul_reception);
    free(msg->sf_trigger_msg->ul_info[i]);
1065 1066 1067
  }
  free(msg->sf_trigger_msg->ul_info);
  free(msg->sf_trigger_msg);
1068
  free(msg);
1069

1070 1071 1072 1073 1074 1075 1076
  return 0;

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

1077
int flexran_agent_mac_create_empty_dl_config(mid_t mod_id, Protocol__FlexranMessage **msg) {
1078 1079

  int xid = 0;
1080
  Protocol__FlexHeader *header = NULL;
1081
  if (flexran_create_header(xid, PROTOCOL__FLEX_TYPE__FLPT_DL_MAC_CONFIG, &header) != 0)
1082 1083
    goto error;

1084 1085
  Protocol__FlexDlMacConfig *dl_mac_config_msg;
  dl_mac_config_msg = malloc(sizeof(Protocol__FlexDlMacConfig));
1086
  if (dl_mac_config_msg == NULL) {
1087
    free(header);
1088 1089
    goto error;
  }
1090
  protocol__flex_dl_mac_config__init(dl_mac_config_msg);
1091

1092 1093
  dl_mac_config_msg->header = header;
  dl_mac_config_msg->has_sfn_sf = 1;
1094
  dl_mac_config_msg->sfn_sf = flexran_get_sfn_sf(mod_id);
1095

1096
  *msg = malloc(sizeof(Protocol__FlexranMessage));
1097
  if(*msg == NULL) {
1098
    free(dl_mac_config_msg);
1099
    free(header);
1100
    goto error;
1101
  }
1102 1103 1104
  protocol__flexran_message__init(*msg);
  (*msg)->msg_case = PROTOCOL__FLEXRAN_MESSAGE__MSG_DL_MAC_CONFIG_MSG;
  (*msg)->msg_dir =  PROTOCOL__FLEXRAN_DIRECTION__INITIATING_MESSAGE;
1105 1106 1107 1108 1109 1110 1111 1112
  (*msg)->dl_mac_config_msg = dl_mac_config_msg;

  return 0;

 error:
  return -1;
}

1113
int flexran_agent_mac_destroy_dl_config(Protocol__FlexranMessage *msg) {
1114
  int i,j, k;
1115
  if(msg->msg_case != PROTOCOL__FLEXRAN_MESSAGE__MSG_DL_MAC_CONFIG_MSG)
1116 1117
    goto error;

1118
  Protocol__FlexDlDci *dl_dci;
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139

  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);
1140

1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
  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);
1167 1168
  free(msg->dl_mac_config_msg);
  free(msg);
1169 1170 1171 1172

  return 0;

 error:
1173 1174 1175
  return -1;
}

shahab SHARIATBAGHERI's avatar
shahab SHARIATBAGHERI committed
1176 1177 1178
int flexran_agent_mac_create_empty_ul_config(mid_t mod_id, Protocol__FlexranMessage **msg) {

  int xid = 0;
1179
  Protocol__FlexHeader *header = NULL;
shahab SHARIATBAGHERI's avatar
shahab SHARIATBAGHERI committed
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
  if (flexran_create_header(xid, PROTOCOL__FLEX_TYPE__FLPT_UL_MAC_CONFIG, &header) != 0)
    goto error;

  Protocol__FlexUlMacConfig *ul_mac_config_msg;
  ul_mac_config_msg = malloc(sizeof(Protocol__FlexUlMacConfig));
  if (ul_mac_config_msg == NULL) {
    goto error;
  }
  protocol__flex_ul_mac_config__init(ul_mac_config_msg);

  ul_mac_config_msg->header = header;
  ul_mac_config_msg->has_sfn_sf = 1;
  ul_mac_config_msg->sfn_sf = flexran_get_sfn_sf(mod_id);

  *msg = malloc(sizeof(Protocol__FlexranMessage));
  if(*msg == NULL)
    goto error;
  protocol__flexran_message__init(*msg);
  (*msg)->msg_case = PROTOCOL__FLEXRAN_MESSAGE__MSG_UL_MAC_CONFIG_MSG;
  (*msg)->msg_dir =  PROTOCOL__FLEXRAN_DIRECTION__INITIATING_MESSAGE;
  (*msg)->ul_mac_config_msg = ul_mac_config_msg;

  return 0;

 error:
  return -1;
}


shahab SHARIATBAGHERI's avatar
shahab SHARIATBAGHERI committed
1209
int flexran_agent_mac_destroy_ul_config(Protocol__FlexranMessage *msg) {
shahab SHARIAT BAGHERI's avatar
shahab SHARIAT BAGHERI committed
1210
  int i; //,j, k;
shahab SHARIATBAGHERI's avatar
shahab SHARIATBAGHERI committed
1211 1212 1213
  if(msg->msg_case != PROTOCOL__FLEXRAN_MESSAGE__MSG_UL_MAC_CONFIG_MSG)
    goto error;

shahab SHARIAT BAGHERI's avatar
shahab SHARIAT BAGHERI committed
1214
  // Protocol__FlexUlDci *ul_dci;
shahab SHARIATBAGHERI's avatar
shahab SHARIATBAGHERI committed
1215 1216 1217 1218

  free(msg->ul_mac_config_msg->header);
  for (i = 0; i < msg->ul_mac_config_msg->n_ul_ue_data; i++) {
    // TODO  uplink rlc ...
Cedric Roux's avatar
Cedric Roux committed
1219
    // free(msg->ul_mac_config_msg->dl_ue_data[i]->ce_bitmap);
shahab SHARIATBAGHERI's avatar
shahab SHARIATBAGHERI committed
1220 1221 1222 1223 1224 1225 1226 1227
  //   for (j = 0; j < msg->ul_mac_config_msg->ul_ue_data[i]->n_rlc_pdu; j++) {
  //     for (k = 0; k <  msg->ul_mac_config_msg->ul_ue_data[i]->rlc_pdu[j]->n_rlc_pdu_tb; k++) {
  // free(msg->ul_mac_config_msg->dl_ue_data[i]->rlc_pdu[j]->rlc_pdu_tb[k]);
  //     }
  //     free(msg->ul_mac_config_msg->ul_ue_data[i]->rlc_pdu[j]->rlc_pdu_tb);
  //     free(msg->ul_mac_config_msg->ul_ue_data[i]->rlc_pdu[j]);
  //   }
    // free(msg->ul_mac_config_msg->ul_ue_data[i]->rlc_pdu);
shahab SHARIAT BAGHERI's avatar
shahab SHARIAT BAGHERI committed
1228
    // ul_dci = msg->ul_mac_config_msg->ul_ue_data[i]->ul_dci;
shahab SHARIATBAGHERI's avatar
shahab SHARIATBAGHERI committed
1229 1230 1231 1232 1233
    // free(dl_dci->tbs_size);
    // free(ul_dci->mcs);
    // free(ul_dci->ndi);
    // free(ul_dci->rv);
    // free(ul_dci);
shahab SHARIAT BAGHERI's avatar
shahab SHARIAT BAGHERI committed
1234
    // free(msg->ul_mac_config_msg->ul_ue_data[i]);
shahab SHARIATBAGHERI's avatar
shahab SHARIATBAGHERI committed
1235 1236
  }
  free(msg->ul_mac_config_msg->ul_ue_data);
Cedric Roux's avatar
Cedric Roux committed
1237

shahab SHARIATBAGHERI's avatar
shahab SHARIATBAGHERI committed
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
  free(msg->ul_mac_config_msg);
  free(msg);

  return 0;

 error:
  return -1;
}


1248
void flexran_agent_get_pending_dl_mac_config(mid_t mod_id, Protocol__FlexranMessage **msg) {
1249 1250

  struct lfds700_misc_prng_state ls;
Cedric Roux's avatar
Cedric Roux committed
1251

1252 1253
  LFDS700_MISC_MAKE_VALID_ON_CURRENT_LOGICAL_CORE_INITS_COMPLETED_BEFORE_NOW_ON_ANY_OTHER_LOGICAL_CORE;
  lfds700_misc_prng_init(&ls);
Cedric Roux's avatar
Cedric Roux committed
1254

1255 1256 1257 1258 1259
  if (lfds700_ringbuffer_read(&ringbuffer_state[mod_id], NULL, (void **) msg, &ls) == 0) {
    *msg = NULL;
  }
}

1260
int flexran_agent_mac_handle_dl_mac_config(mid_t mod_id, const void *params, Protocol__FlexranMessage **msg) {
1261 1262 1263

  struct lfds700_misc_prng_state ls;
  enum lfds700_misc_flag overwrite_occurred_flag;
1264
  Protocol__FlexranMessage *overwritten_dl_config;
Cedric Roux's avatar
Cedric Roux committed
1265

1266 1267
  LFDS700_MISC_MAKE_VALID_ON_CURRENT_LOGICAL_CORE_INITS_COMPLETED_BEFORE_NOW_ON_ANY_OTHER_LOGICAL_CORE;
  lfds700_misc_prng_init(&ls);
Cedric Roux's avatar
Cedric Roux committed
1268

1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
  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
1279
    flexran_agent_mac_destroy_dl_config(overwritten_dl_config);
1280 1281 1282 1283
  }
  *msg = NULL;
  return 2;

1284 1285 1286
  // error:
  //*msg = NULL;
  //return -1;
1287 1288
}

1289
void flexran_agent_init_mac_agent(mid_t mod_id) {
1290
  int i, j;
1291 1292 1293 1294
  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
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
  /* lfds700_ringbuffer_init_valid_on_current_logical_core()'s second argument
   * must be aligned to LFDS700_PAL_ATOMIC_ISOLATION_IN_BYTES. From the
   * documentation: "Heap allocated variables however will by no means be
   * correctly aligned and an aligned malloc must be used." Therefore, we use
   * posix_memalign */
  i = posix_memalign((void **)&dl_mac_config_array[mod_id],
                    LFDS700_PAL_ATOMIC_ISOLATION_IN_BYTES,
                    sizeof(struct lfds700_ringbuffer_element) *  num_elements);
  AssertFatal(i == 0, "posix_memalign(): could not allocate aligned memory for lfds library\n");
  lfds700_ringbuffer_init_valid_on_current_logical_core(&ringbuffer_state[mod_id],
      dl_mac_config_array[mod_id], num_elements, &ps[mod_id], NULL);
1306
  for (i = 0; i < MAX_MOBILES_PER_ENB; i++) {
1307
    for (j = 0; j < 8; j++) {
1308 1309
      if (RC.mac && RC.mac[mod_id])
        RC.mac[mod_id]->UE_list.eNB_UE_stats[UE_PCCID(mod_id,i)][i].harq_pid = 0;
1310 1311
    }
  }
1312 1313
}

1314
/***********************************************
1315
 * FlexRAN agent - technology mac API implementation
1316 1317
 ***********************************************/

1318
void flexran_agent_send_sr_info(mid_t mod_id) {
1319
  int size;
1320
  Protocol__FlexranMessage *msg = NULL;
1321
  void *data;
1322
  int priority = 0;
1323 1324
  err_code_t err_code;

1325 1326
  int xid = 0;

1327
  /*TODO: Must use a proper xid*/
1328
  err_code = flexran_agent_mac_sr_info(mod_id, (void *) &xid, &msg);
1329 1330 1331 1332 1333
  if (err_code < 0) {
    goto error;
  }

  if (msg != NULL){
1334
    data=flexran_agent_pack_message(msg, &size);
1335
    /*Send sr info using the MAC channel of the eNB*/
1336
    if (flexran_agent_msg_send(mod_id, FLEXRAN_AGENT_MAC, data, size, priority)) {
1337
      err_code = PROTOCOL__FLEXRAN_ERR__MSG_ENQUEUING;
1338 1339
      goto error;
    }
1340

1341
    LOG_D(FLEXRAN_AGENT,"sent message with size %d\n", size);
1342
    return;
1343 1344
  }
 error:
1345
  LOG_D(FLEXRAN_AGENT, "Could not send sr message\n");
1346 1347
}

1348
void flexran_agent_send_sf_trigger(mid_t mod_id) {
1349
  int size;
1350
  Protocol__FlexranMessage *msg = NULL;
1351
  void *data;
1352
  int priority = 0;
1353 1354
  err_code_t err_code;

1355
  int xid = 0;
1356

1357
  /*TODO: Must use a proper xid*/
1358
  err_code = flexran_agent_mac_sf_trigger(mod_id, (void *) &xid, &msg);
1359 1360 1361 1362 1363
  if (err_code < 0) {
    goto error;
  }

  if (msg != NULL){
1364
    data=flexran_agent_pack_message(msg, &size);
1365
    /*Send sr info using the MAC channel of the eNB*/
1366
    if (flexran_agent_msg_send(mod_id, FLEXRAN_AGENT_MAC, data, size, priority)) {
1367
      err_code = PROTOCOL__FLEXRAN_ERR__MSG_ENQUEUING;
1368 1369 1370
      goto error;
    }

1371
    LOG_D(FLEXRAN_AGENT,"sent message with size %d\n", size);
1372 1373 1374
    return;
  }
 error:
1375
  LOG_D(FLEXRAN_AGENT, "Could not send sf trigger message\n");
1376 1377
}

1378 1379


1380 1381 1382
int flexran_agent_register_mac_xface(mid_t mod_id)
{
  if (agent_mac_xface[mod_id]) {
1383 1384 1385
    LOG_E(MAC, "MAC agent for eNB %d is already registered\n", mod_id);
    return -1;
  }
1386 1387 1388 1389 1390
  AGENT_MAC_xface *xface = malloc(sizeof(AGENT_MAC_xface));
  if (!xface) {
    LOG_E(FLEXRAN_AGENT, "could not allocate memory for MAC agent xface %d\n", mod_id);
    return -1;
  }
1391

1392
  //xface->agent_ctxt = &shared_ctxt[mod_id];
1393 1394
  xface->flexran_agent_send_sr_info = flexran_agent_send_sr_info;
  xface->flexran_agent_send_sf_trigger = flexran_agent_send_sf_trigger;
1395
  //xface->flexran_agent_send_update_mac_stats = flexran_agent_send_update_mac_stats;
1396
  xface->flexran_agent_get_pending_dl_mac_config = flexran_agent_get_pending_dl_mac_config;
Cedric Roux's avatar
Cedric Roux committed
1397

1398
  xface->dl_scheduler_loaded_lib = NULL;
shahab SHARIATBAGHERI's avatar
shahab SHARIATBAGHERI committed
1399
  xface->ul_scheduler_loaded_lib = NULL;
1400
  agent_mac_xface[mod_id] = xface;
1401 1402

  return 0;
1403 1404
}

1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416
void flexran_agent_fill_mac_cell_config(mid_t mod_id, uint8_t cc_id,
    Protocol__FlexCellConfig *conf) {
  if (!conf->si_config) {
    conf->si_config = malloc(sizeof(Protocol__FlexSiConfig));
    if (conf->si_config)
      protocol__flex_si_config__init(conf->si_config);
  }

  if (conf->si_config) {
    conf->si_config->sfn = flexran_get_current_system_frame_num(mod_id);
    conf->si_config->has_sfn = 1;
  }
1417 1418 1419 1420

  /* get a pointer to the config which is maintained in the agent throughout
  * its lifetime */
  conf->slice_config = flexran_agent_get_slice_config(mod_id);
1421 1422
}

1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433
void flexran_agent_fill_mac_ue_config(mid_t mod_id, mid_t ue_id,
    Protocol__FlexUeConfig *ue_conf)
{
  if (ue_conf->has_rnti && ue_conf->rnti != flexran_get_mac_ue_crnti(mod_id, ue_id)) {
    LOG_E(FLEXRAN_AGENT, "ue_config existing RNTI %x does not match MAC RNTI %x\n",
          ue_conf->rnti, flexran_get_mac_ue_crnti(mod_id, ue_id));
    return;
  }
  ue_conf->rnti = flexran_get_mac_ue_crnti(mod_id, ue_id);
  ue_conf->has_rnti = 1;

1434 1435 1436 1437
  ue_conf->dl_slice_id = flexran_get_ue_dl_slice_id(mod_id, ue_id);
  ue_conf->has_dl_slice_id = 1;
  ue_conf->ul_slice_id = flexran_get_ue_ul_slice_id(mod_id, ue_id);
  ue_conf->has_ul_slice_id = 1;
1438

1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
  ue_conf->ue_aggregated_max_bitrate_ul = flexran_get_ue_aggregated_max_bitrate_ul(mod_id, ue_id);
  ue_conf->has_ue_aggregated_max_bitrate_ul = 1;

  ue_conf->ue_aggregated_max_bitrate_dl = flexran_get_ue_aggregated_max_bitrate_dl(mod_id, ue_id);
  ue_conf->has_ue_aggregated_max_bitrate_dl = 1;

  /* TODO update through RAN API */
  //config->has_pcell_carrier_index = 1;
  //config->pcell_carrier_index = UE_PCCID(mod_id, i);

  //TODO: Set carrier aggregation support (boolean)
}

1452 1453 1454
void flexran_agent_fill_mac_lc_ue_config(mid_t mod_id, mid_t ue_id,
    Protocol__FlexLcUeConfig *lc_ue_conf)
{
1455
  lc_ue_conf->rnti = flexran_get_mac_ue_crnti(mod_id, ue_id);
1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
  lc_ue_conf->has_rnti = 1;

  lc_ue_conf->n_lc_config = flexran_get_num_ue_lcs(mod_id, ue_id);
  if (lc_ue_conf->n_lc_config == 0)
    return;

  Protocol__FlexLcConfig **lc_config =
    calloc(lc_ue_conf->n_lc_config, sizeof(Protocol__FlexLcConfig *));
  if (!lc_config) {
    LOG_E(FLEXRAN_AGENT, "could not allocate memory for lc_config of UE %x\n", lc_ue_conf->rnti);
    lc_ue_conf->n_lc_config = 0;
    return; // can not allocate memory, skip rest
  }
  for (int j = 0; j < lc_ue_conf->n_lc_config; j++) {
    lc_config[j] = malloc(sizeof(Protocol__FlexLcConfig));
    if (!lc_config[j]) continue; // go over this error, try entry
    protocol__flex_lc_config__init(lc_config[j]);

    lc_config[j]->has_lcid = 1;
    lc_config[j]->lcid = j+1;

    const int lcg = flexran_get_lcg(mod_id, ue_id, j+1);
    if (lcg >= 0 && lcg <= 3) {
      lc_config[j]->has_lcg = 1;
      lc_config[j]->lcg = flexran_get_lcg(mod_id, ue_id, j+1);
    }

    lc_config[j]->has_direction = 1;
    lc_config[j]->direction = flexran_get_direction(ue_id, j+1);
    //TODO: Bearer type. One of FLQBT_* values. Currently only default bearer supported
    lc_config[j]->has_qos_bearer_type = 1;
    lc_config[j]->qos_bearer_type = PROTOCOL__FLEX_QOS_BEARER_TYPE__FLQBT_NON_GBR;

    //TODO: Set the QCI defined in TS 23.203, coded as defined in TS 36.413
    // One less than the actual QCI value. Needs to be generalized
    lc_config[j]->has_qci = 1;
    lc_config[j]->qci = 1;
    if (lc_config[j]->direction == PROTOCOL__FLEX_QOS_BEARER_TYPE__FLQBT_GBR) {
      /* TODO all of the need to be taken from API */
      //TODO: Set the max bitrate (UL)
      lc_config[j]->has_e_rab_max_bitrate_ul = 0;
      lc_config[j]->e_rab_max_bitrate_ul = 0;
      //TODO: Set the max bitrate (DL)
      lc_config[j]->has_e_rab_max_bitrate_dl = 0;
      lc_config[j]->e_rab_max_bitrate_dl = 0;
      //TODO: Set the guaranteed bitrate (UL)
      lc_config[j]->has_e_rab_guaranteed_bitrate_ul = 0;
      lc_config[j]->e_rab_guaranteed_bitrate_ul = 0;
      //TODO: Set the guaranteed bitrate (DL)
      lc_config[j]->has_e_rab_guaranteed_bitrate_dl = 0;
      lc_config[j]->e_rab_guaranteed_bitrate_dl = 0;
    }
  }
  lc_ue_conf->lc_config = lc_config;
}

1512 1513 1514 1515 1516 1517 1518
int flexran_agent_unregister_mac_xface(mid_t mod_id)
{
  if (!agent_mac_xface[mod_id]) {
    LOG_E(FLEXRAN_AGENT, "MAC agent CM for eNB %d is not registered\n", mod_id);
    return -1;
  }
  AGENT_MAC_xface *xface = agent_mac_xface[mod_id];
1519
  //xface->agent_ctxt = NULL;
1520 1521
  xface->flexran_agent_send_sr_info = NULL;
  xface->flexran_agent_send_sf_trigger = NULL;
1522
  //xface->flexran_agent_send_update_mac_stats = NULL;
1523
  xface->flexran_agent_get_pending_dl_mac_config = NULL;
1524

1525
  xface->dl_scheduler_loaded_lib = NULL;
shahab SHARIATBAGHERI's avatar
shahab SHARIATBAGHERI committed
1526
  xface->ul_scheduler_loaded_lib = NULL;
1527
  free(xface);
1528
  agent_mac_xface[mod_id] = NULL;
1529

1530 1531 1532
  return 0;
}

1533 1534 1535 1536
AGENT_MAC_xface *flexran_agent_get_mac_xface(mid_t mod_id)
{
  return agent_mac_xface[mod_id];
}
1537

1538 1539 1540 1541 1542 1543 1544 1545 1546
void flexran_create_config_structures(mid_t mod_id)
{
  int i;
  int n_dl = flexran_get_num_dl_slices(mod_id);
  int m_ul = flexran_get_num_ul_slices(mod_id);
  slice_config[mod_id] = flexran_agent_create_slice_config(n_dl, m_ul);
  sc_update[mod_id] = flexran_agent_create_slice_config(n_dl, m_ul);
  if (!slice_config[mod_id] || !sc_update[mod_id]) return;

1547 1548
  flexran_agent_read_slice_config(mod_id, slice_config[mod_id]);
  flexran_agent_read_slice_config(mod_id, sc_update[mod_id]);
1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561
  for (i = 0; i < n_dl; i++) {
    flexran_agent_read_slice_dl_config(mod_id, i, slice_config[mod_id]->dl[i]);
    flexran_agent_read_slice_dl_config(mod_id, i, sc_update[mod_id]->dl[i]);
  }
  for (i = 0; i < m_ul; i++) {
    flexran_agent_read_slice_ul_config(mod_id, i, slice_config[mod_id]->ul[i]);
    flexran_agent_read_slice_ul_config(mod_id, i, sc_update[mod_id]->ul[i]);
  }
}

void flexran_check_and_remove_slices(mid_t mod_id)
{
  Protocol__FlexDlSlice **dl = sc_update[mod_id]->dl;
Robert Schmidt's avatar
Robert Schmidt committed
1562 1563 1564
  Protocol__FlexDlSlice **dlreal = slice_config[mod_id]->dl;
  int i = 0;
  while (i < sc_update[mod_id]->n_dl) {
1565
    /* remove slices whose percentage is zero */
Robert Schmidt's avatar
Robert Schmidt committed
1566 1567 1568 1569
    if (dl[i]->percentage > 0) {
      ++i;
      continue;
    }
1570 1571 1572
    if (flexran_remove_dl_slice(mod_id, i) < 1) {
      LOG_W(FLEXRAN_AGENT, "[%d] can not remove slice index %d ID %d\n",
            mod_id, i, dl[i]->id);
Robert Schmidt's avatar
Robert Schmidt committed
1573
      ++i;
1574 1575 1576 1577
      continue;
    }
    LOG_I(FLEXRAN_AGENT, "[%d] removed slice index %d ID %d\n",
          mod_id, i, dl[i]->id);
Robert Schmidt's avatar
Robert Schmidt committed
1578 1579 1580 1581 1582 1583 1584
    if (dl[i]->n_sorting > 0) free(dl[i]->sorting);
    free(dl[i]->scheduler_name);
    if (dlreal[i]->n_sorting > 0) {
      dlreal[i]->n_sorting = 0;
      free(dlreal[i]->sorting);
    }
    free(dlreal[i]->scheduler_name);
1585
    --sc_update[mod_id]->n_dl;
Robert Schmidt's avatar
Robert Schmidt committed
1586 1587 1588 1589 1590 1591 1592 1593
    --slice_config[mod_id]->n_dl;
    const size_t last = sc_update[mod_id]->n_dl;
    /* we need to memcpy the higher slice to the position we just deleted */
    memcpy(dl[i], dl[last], sizeof(*dl[last]));
    memset(dl[last], 0, sizeof(*dl[last]));
    memcpy(dlreal[i], dlreal[last], sizeof(*dlreal[last]));
    memset(dlreal[last], 0, sizeof(*dlreal[last]));
    /* dont increase i but recheck the slice which has been copied to here */
1594 1595
  }
  Protocol__FlexUlSlice **ul = sc_update[mod_id]->ul;
Robert Schmidt's avatar
Robert Schmidt committed
1596 1597 1598 1599 1600 1601 1602
  Protocol__FlexUlSlice **ulreal = slice_config[mod_id]->ul;
  i = 0;
  while (i < sc_update[mod_id]->n_ul) {
    if (ul[i]->percentage > 0) {
      ++i;
      continue;
    }
1603 1604 1605
    if (flexran_remove_ul_slice(mod_id, i) < 1) {
      LOG_W(FLEXRAN_AGENT, "[%d] can not remove slice index %d ID %d\n",
            mod_id, i, ul[i]->id);
Robert Schmidt's avatar
Robert Schmidt committed
1606
      ++i;
1607 1608 1609 1610
      continue;
    }
    LOG_I(FLEXRAN_AGENT, "[%d] removed slice index %d ID %d\n",
          mod_id, i, ul[i]->id);
Robert Schmidt's avatar
Robert Schmidt committed
1611 1612
    free(ul[i]->scheduler_name);
    free(ulreal[i]->scheduler_name);
1613
    --sc_update[mod_id]->n_ul;
Robert Schmidt's avatar
Robert Schmidt committed
1614 1615 1616 1617 1618 1619 1620 1621
    --slice_config[mod_id]->n_ul;
    const size_t last = sc_update[mod_id]->n_ul;
    /* see DL remarks */
    memcpy(ul[i], ul[last], sizeof(*ul[last]));
    memset(ul[last], 0, sizeof(*ul[last]));
    memcpy(ulreal[i], ulreal[last], sizeof(*ulreal[last]));
    memset(ulreal[last], 0, sizeof(*ulreal[last]));
    /* dont increase i but recheck the slice which has been copied to here */
1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632
  }
}

void flexran_agent_slice_update(mid_t mod_id)
{
  int i;
  int changes = 0;

  if (perform_slice_config_update_count <= 0) return;
  perform_slice_config_update_count--;

1633 1634
  pthread_mutex_lock(&sc_update_mtx);

1635 1636 1637 1638
  if (!slice_config[mod_id]) {
    /* if the configuration does not exist for agent, create from eNB structure
     * and exit */
    flexran_create_config_structures(mod_id);
1639
    pthread_mutex_unlock(&sc_update_mtx);
1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653
    return;
  }

  /********* read existing config *********/
  /* simply update slice_config all the time and write new config
   * (apply_new_slice_dl_config() only updates if changes are necessary) */
  slice_config[mod_id]->n_dl = flexran_get_num_dl_slices(mod_id);
  slice_config[mod_id]->n_ul = flexran_get_num_ul_slices(mod_id);
  for (i = 0; i < slice_config[mod_id]->n_dl; i++) {
    flexran_agent_read_slice_dl_config(mod_id, i, slice_config[mod_id]->dl[i]);
  }
  for (i = 0; i < slice_config[mod_id]->n_ul; i++) {
    flexran_agent_read_slice_ul_config(mod_id, i, slice_config[mod_id]->ul[i]);
  }
1654

1655 1656 1657 1658
  /********* write new config *********/
  /* check for removal (sc_update[X]->dl[Y].percentage == 0)
   * and update sc_update & slice_config accordingly */
  flexran_check_and_remove_slices(mod_id);
1659

1660 1661
  /* create new DL and UL slices if necessary */
  for (i = slice_config[mod_id]->n_dl; i < sc_update[mod_id]->n_dl; i++) {
1662
    flexran_create_dl_slice(mod_id, sc_update[mod_id]->dl[i]->id);
1663 1664
  }
  for (i = slice_config[mod_id]->n_ul; i < sc_update[mod_id]->n_ul; i++) {
1665
    flexran_create_ul_slice(mod_id, sc_update[mod_id]->ul[i]->id);
1666
  }
Robert Schmidt's avatar
Robert Schmidt committed
1667 1668 1669
  slice_config[mod_id]->n_dl = flexran_get_num_dl_slices(mod_id);
  slice_config[mod_id]->n_ul = flexran_get_num_ul_slices(mod_id);
  changes += apply_new_slice_config(mod_id, slice_config[mod_id], sc_update[mod_id]);
1670 1671 1672 1673
  for (i = 0; i < slice_config[mod_id]->n_dl; i++) {
    changes += apply_new_slice_dl_config(mod_id,
                                         slice_config[mod_id]->dl[i],
                                         sc_update[mod_id]->dl[i]);
Robert Schmidt's avatar
Robert Schmidt committed
1674
    flexran_agent_read_slice_dl_config(mod_id, i, slice_config[mod_id]->dl[i]);
1675 1676 1677 1678 1679
  }
  for (i = 0; i < slice_config[mod_id]->n_ul; i++) {
    changes += apply_new_slice_ul_config(mod_id,
                                         slice_config[mod_id]->ul[i],
                                         sc_update[mod_id]->ul[i]);
Robert Schmidt's avatar
Robert Schmidt committed
1680
    flexran_agent_read_slice_ul_config(mod_id, i, slice_config[mod_id]->ul[i]);
1681
  }
1682
  flexran_agent_read_slice_config(mod_id, slice_config[mod_id]);
1683 1684 1685 1686 1687
  if (n_ue_slice_assoc_updates > 0) {
    changes += apply_ue_slice_assoc_update(mod_id);
  }
  if (changes > 0)
    LOG_I(FLEXRAN_AGENT, "[%d] slice configuration: applied %d changes\n", mod_id, changes);
1688

1689
  pthread_mutex_unlock(&sc_update_mtx);
1690
}
1691

1692 1693
Protocol__FlexSliceConfig *flexran_agent_get_slice_config(mid_t mod_id)
{
1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762
  if (!slice_config[mod_id]) return NULL;
  Protocol__FlexSliceConfig *config = NULL;

  pthread_mutex_lock(&sc_update_mtx);
  config = flexran_agent_create_slice_config(slice_config[mod_id]->n_dl,
                                             slice_config[mod_id]->n_ul);
  if (!config) {
    pthread_mutex_unlock(&sc_update_mtx);
    return NULL;
  }
  config->has_intraslice_share_active = 1;
  config->intraslice_share_active = slice_config[mod_id]->intraslice_share_active;
  config->has_interslice_share_active = 1;
  config->interslice_share_active = slice_config[mod_id]->interslice_share_active;
  for (int i = 0; i < slice_config[mod_id]->n_dl; ++i) {
    if (!config->dl[i]) continue;
    config->dl[i]->has_id         = 1;
    config->dl[i]->id             = slice_config[mod_id]->dl[i]->id;
    config->dl[i]->has_label      = 1;
    config->dl[i]->label          = slice_config[mod_id]->dl[i]->label;
    config->dl[i]->has_percentage = 1;
    config->dl[i]->percentage     = slice_config[mod_id]->dl[i]->percentage;
    config->dl[i]->has_isolation  = 1;
    config->dl[i]->isolation      = slice_config[mod_id]->dl[i]->isolation;
    config->dl[i]->has_priority   = 1;
    config->dl[i]->priority       = slice_config[mod_id]->dl[i]->priority;
    config->dl[i]->has_position_low  = 1;
    config->dl[i]->position_low   = slice_config[mod_id]->dl[i]->position_low;
    config->dl[i]->has_position_high = 1;
    config->dl[i]->position_high  = slice_config[mod_id]->dl[i]->position_high;
    config->dl[i]->has_maxmcs     = 1;
    config->dl[i]->maxmcs         = slice_config[mod_id]->dl[i]->maxmcs;
    config->dl[i]->n_sorting      = slice_config[mod_id]->dl[i]->n_sorting;
    config->dl[i]->sorting        = calloc(config->dl[i]->n_sorting, sizeof(Protocol__FlexDlSorting));
    if (!config->dl[i]->sorting) config->dl[i]->n_sorting = 0;
    for (int j = 0; j < config->dl[i]->n_sorting; ++j)
      config->dl[i]->sorting[j]   = slice_config[mod_id]->dl[i]->sorting[j];
    config->dl[i]->has_accounting = 1;
    config->dl[i]->accounting     = slice_config[mod_id]->dl[i]->accounting;
    config->dl[i]->scheduler_name = strdup(slice_config[mod_id]->dl[i]->scheduler_name);
  }
  for (int i = 0; i < slice_config[mod_id]->n_ul; ++i) {
    if (!config->ul[i]) continue;
    config->ul[i]->has_id         = 1;
    config->ul[i]->id             = slice_config[mod_id]->ul[i]->id;
    config->ul[i]->has_label      = 1;
    config->ul[i]->label          = slice_config[mod_id]->ul[i]->label;
    config->ul[i]->has_percentage = 1;
    config->ul[i]->percentage     = slice_config[mod_id]->ul[i]->percentage;
    config->ul[i]->has_isolation  = 1;
    config->ul[i]->isolation      = slice_config[mod_id]->ul[i]->isolation;
    config->ul[i]->has_priority   = 1;
    config->ul[i]->priority       = slice_config[mod_id]->ul[i]->priority;
    config->ul[i]->has_first_rb   = 1;
    config->ul[i]->first_rb       = slice_config[mod_id]->ul[i]->first_rb;
    config->ul[i]->has_maxmcs     = 1;
    config->ul[i]->maxmcs         = slice_config[mod_id]->ul[i]->maxmcs;
    config->ul[i]->n_sorting      = slice_config[mod_id]->ul[i]->n_sorting;
    config->ul[i]->sorting        = calloc(config->ul[i]->n_sorting, sizeof(Protocol__FlexUlSorting));
    if (!config->ul[i]->sorting) config->ul[i]->n_sorting = 0;
    for (int j = 0; j < config->ul[i]->n_sorting; ++j)
      config->ul[i]->sorting[j]   = slice_config[mod_id]->ul[i]->sorting[j];
    config->ul[i]->has_accounting = 1;
    config->ul[i]->accounting     = slice_config[mod_id]->ul[i]->accounting;
    config->ul[i]->scheduler_name = strdup(slice_config[mod_id]->ul[i]->scheduler_name);
  }

  pthread_mutex_unlock(&sc_update_mtx);
  return config;
1763
}