[Epic-ID: ODUHIGH-538] XML based input configuration
[o-du/l2.git] / src / 5gnrmac / mac_cfg_hdl.c
1 /*******************************************************************************
2 ################################################################################
3 #   Copyright (c) [2017-2019] [Radisys]                                        #
4 #                                                                              #
5 #   Licensed under the Apache License, Version 2.0 (the "License");            #
6 #   you may not use this file except in compliance with the License.           #
7 #   You may obtain a copy of the License at                                    #
8 #                                                                              #
9 #       http://www.apache.org/licenses/LICENSE-2.0                             #
10 #                                                                              #
11 #   Unless required by applicable law or agreed to in writing, software        #
12 #   distributed under the License is distributed on an "AS IS" BASIS,          #
13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
14 #   See the License for the specific language governing permissions and        #
15 #   limitations under the License.                                             #
16 ################################################################################
17 *******************************************************************************/
18 /* This file stores handler for MAC and SCH configuration requests */
19 /* header include files (.h) */
20 #include "common_def.h"
21 #include "lrg.h"
22 #include "lrg.x"
23 #include "du_app_mac_inf.h"
24 #include "mac_sch_interface.h"
25 #include "lwr_mac_upr_inf.h"
26 #include "mac.h"
27 #include "rlc_mac_inf.h"
28 #include "mac_upr_inf_api.h"
29 #include "lwr_mac.h"
30 #include "lwr_mac_fsm.h"
31 #include "mac_utils.h"
32 #include "lwr_mac_phy.h"
33
34 uint8_t ssbPeriodicity[6] = {5, 10, 20, 40, 80, 160};
35
36 uint8_t MacSchCellCfgReq(Pst *pst, MacCellCfg  *macCellCfg);
37
38 packMacCellCfgConfirm packMacCellCfmOpts[] =
39 {
40    packMacCellCfgCfm,      /* packing for loosely coupled */
41    duHandleMacCellCfgCfm,      /* packing for tightly coupled */
42    packMacCellCfgCfm,    /* packing for light weight loosly coupled */
43 };
44
45 MacDuCellDeleteRspFunc macDuCellDeleteRspOpts[] =
46 {
47    packDuMacCellDeleteRsp,   /* packing for loosely coupled */
48    DuProcMacCellDeleteRsp,   /* packing for tightly coupled */
49    packDuMacCellDeleteRsp   /* packing for light weight loosly coupled */
50 };
51
52 MacDuSliceCfgRspFunc macDuSliceCfgRspOpts[] =
53 {
54    packDuMacSliceCfgRsp,   /* packing for loosely coupled */
55    DuProcMacSliceCfgRsp,   /* packing for tightly coupled */
56    packDuMacSliceCfgRsp   /* packing for light weight loosly coupled */
57 };
58
59 MacDuSliceRecfgRspFunc macDuSliceRecfgRspOpts[] =
60 {
61    packDuMacSliceRecfgRsp,   /* packing for loosely coupled */
62    DuProcMacSliceRecfgRsp,   /* packing for tightly coupled */
63    packDuMacSliceRecfgRsp    /* packing for light weight loosly coupled */
64 };
65
66 MacDuStatsRspFunc macDuStatsRspOpts[] =
67 {
68    packDuMacStatsRsp,   /* packing for loosely coupled */
69    DuProcMacStatsRsp,   /* packing for tightly coupled */
70    packDuMacStatsRsp   /* packing for light weight loosly coupled */
71 };
72
73 MacDuStatsDeleteRspFunc macDuStatsDeleteRspOpts[] =
74 {
75    packDuMacStatsDeleteRsp,   /* packing for loosely coupled */
76    DuProcMacStatsDeleteRsp,   /* packing for tightly coupled */
77    packDuMacStatsDeleteRsp   /* packing for light weight loosly coupled */
78 };
79
80 MacDuStatsModificationRspFunc macDuStatsModificationRspOpts[] =
81 {
82    packDuMacStatsModificationRsp,   /* packing for loosely coupled */
83    DuProcMacStatsModificationRsp,   /* packing for tightly coupled */
84    packDuMacStatsModificationRsp   /* packing for light weight loosly coupled */
85 };
86
87 /**
88  * @brief Layer Manager  Configuration request handler for Scheduler
89  *
90  * @details
91  *
92  *     Function : MacSchGenCfgReq
93  *
94  *     This function receives general configurations for Scheduler
95  *     from DU APP and forwards to Scheduler.
96  *
97  *  @param[in]  Pst *pst, the post structure
98  *  @param[in]  RgMngmt *cfg, the configuration parameter's structure
99  *  @return 
100  *      -# ROK
101  **/
102 uint8_t MacSchGenCfgReq(Pst *pst, RgMngmt *cfg)
103 {
104    Pst schPst;
105
106    DU_LOG("\nINFO  -->  MAC : Received Scheduler gen config at MAC");
107    memset(&schPst, 0, sizeof(Pst));
108    FILL_PST_MAC_TO_SCH(schPst, EVENT_SCH_GEN_CFG);
109    
110    return(SchMessageRouter(&schPst, (void *)cfg));
111 }
112
113 /**
114  * @brief Layer Manager Configuration response from Scheduler
115  *
116  * @details
117  *
118  *     Function : SchSendCfgCfm
119  *
120  *     This function sends general configurations response from
121  *     Scheduler to DU APP.
122  *
123  *  @param[in]  Pst *pst, the post structure
124  *  @param[in]  RgMngmt *cfm, the configuration confirm structure
125  *  @return 
126  *      -# ROK
127  **/
128 uint8_t SchSendCfgCfm(Pst *pst, RgMngmt  *cfm)
129 {
130    DU_LOG("\nDEBUG  -->  MAC : Sending Scheduler config confirm to DU APP");
131    pst->dstEnt = ENTDUAPP;
132    pst->dstInst = 0;
133    pst->srcInst = 0;
134    pst->selector = ODU_SELECTOR_LC;
135    RgMiLrgSchCfgCfm(pst, cfm);
136
137    return ROK;
138 }
139
140 /**
141  * @brief Layer Manager Configuration request handler.
142  *
143  * @details
144  *
145  *     Function : MacProcCellCfgReq
146  *
147  *     This function handles the gNB and cell configuration
148  *     request received from DU APP.
149  *     This API unapcks and forwards the config towards SCH
150  *
151  *  @param[in]  Pst           *pst
152  *  @param[in]  MacCellCfg    *macCellCfg
153  *  @return  
154  *      -# ROK
155  **/
156 uint8_t MacProcCellCfgReq(Pst *pst, MacCellCfg *macCellCfg)
157 {
158    Pst cfmPst;
159    uint16_t cellIdx, scsInKhz = 0;
160    uint8_t ret = ROK, plmnIdx = 0,sliceIdx = 0;
161    MacCellCb     *macCellCb;
162
163    memset((uint8_t *)&cfmPst, 0, sizeof(Pst));
164
165    MAC_ALLOC(macCellCb, sizeof(MacCellCb));
166    if(macCellCb == NULLP)
167    {
168       DU_LOG("\nERROR  -->  MAC : macCellCb is NULL at handling of macCellCfg\n");
169       return RFAILED;
170    }
171    memset(macCellCb, 0, sizeof(MacCellCb));
172
173    GET_CELL_IDX(macCellCfg->cellId, cellIdx);
174    macCb.macCell[cellIdx] = macCellCb;
175    macCb.macCell[cellIdx]->cellId = macCellCfg->cellId;
176    scsInKhz = convertScsEnumValToScsVal(macCellCfg->cellCfg.subCarrSpacing);
177    
178    /*Ref : 3GPP 38.211 Table 4.2-1: SCS = (2 ^ numerology * 15kHz)*/
179    macCb.macCell[cellIdx]->numerology = log2(scsInKhz/BASE_SCS);
180    macCb.macCell[cellIdx]->numOfSlots = 10 * (1 << (macCb.macCell[cellIdx]->numerology));
181    memcpy(&macCb.macCell[cellIdx]->macCellCfg, macCellCfg, sizeof(MacCellCfg));
182
183    MAC_ALLOC(macCb.macCell[cellIdx]->macCellCfg.cellCfg.sib1Cfg.sib1Pdu, \
184          macCb.macCell[cellIdx]->macCellCfg.cellCfg.sib1Cfg.sib1PduLen);
185    if(macCb.macCell[cellIdx]->macCellCfg.cellCfg.sib1Cfg.sib1Pdu == NULLP)
186    {
187       DU_LOG("\nERROR  -->  MAC : macCellCb is NULL at handling of sib1Pdu of macCellCfg\n");
188       return RFAILED;
189    }
190    memcpy(macCb.macCell[cellIdx]->macCellCfg.cellCfg.sib1Cfg.sib1Pdu, macCellCfg->cellCfg.sib1Cfg.sib1Pdu, \
191          macCb.macCell[cellIdx]->macCellCfg.cellCfg.sib1Cfg.sib1PduLen);
192
193    for(plmnIdx = 0; plmnIdx < MAX_PLMN; plmnIdx++)
194    {
195       macCb.macCell[cellIdx]->macCellCfg.cellCfg.plmnInfoList[plmnIdx].suppSliceList.numSupportedSlices = macCellCfg->cellCfg.plmnInfoList[plmnIdx].suppSliceList.numSupportedSlices;
196       MAC_ALLOC(macCb.macCell[cellIdx]->macCellCfg.cellCfg.plmnInfoList[plmnIdx].suppSliceList.snssai, macCb.macCell[cellIdx]->macCellCfg.cellCfg.plmnInfoList[plmnIdx].suppSliceList.numSupportedSlices\
197             * sizeof(Snssai*));
198       if(macCb.macCell[cellIdx]->macCellCfg.cellCfg.plmnInfoList[plmnIdx].suppSliceList.snssai == NULLP)
199       {
200          DU_LOG("\nERROR  --> MAC: Memory allocation failed at MacProcCellCfgReq");
201          return RFAILED;
202       }
203
204       if(macCb.macCell[cellIdx]->macCellCfg.cellCfg.plmnInfoList[plmnIdx].suppSliceList.snssai)
205       {
206          for(sliceIdx=0; sliceIdx<macCb.macCell[cellIdx]->macCellCfg.cellCfg.plmnInfoList[plmnIdx].suppSliceList.numSupportedSlices; sliceIdx++)
207          {
208             if(macCellCfg->cellCfg.plmnInfoList[plmnIdx].suppSliceList.snssai[sliceIdx])
209             {
210                MAC_ALLOC(macCb.macCell[cellIdx]->macCellCfg.cellCfg.plmnInfoList[plmnIdx].suppSliceList.snssai[sliceIdx], sizeof(Snssai));
211                if(!macCb.macCell[cellIdx]->macCellCfg.cellCfg.plmnInfoList[plmnIdx].suppSliceList.snssai[sliceIdx])
212                {
213                   DU_LOG("\nERROR  --> MAC: Memory allocation failed at MacProcCellCfgReq");
214                   return RFAILED;
215                }
216                memcpy(macCb.macCell[cellIdx]->macCellCfg.cellCfg.plmnInfoList[plmnIdx].suppSliceList.snssai[sliceIdx], macCellCfg->cellCfg.plmnInfoList[plmnIdx].suppSliceList.snssai[sliceIdx],\
217                      sizeof(Snssai));
218             }
219          }
220       }
221    }
222    /* Send cell cfg to scheduler */
223    ret = MacSchCellCfgReq(pst, macCellCfg);
224    if(ret != ROK)
225    {
226       MacCellCfgCfm macCellCfgCfm;
227       macCellCfgCfm.rsp = RSP_NOK;
228       macCellCfgCfm.cellId = macCellCfg->cellId;
229       /* Fill Pst */
230       FILL_PST_MAC_TO_DUAPP(cfmPst, EVENT_MAC_CELL_CONFIG_CFM);
231       cfmPst.selector  = ODU_SELECTOR_LC;
232
233       ret = (*packMacCellCfmOpts[cfmPst.selector])(&cfmPst,&macCellCfgCfm);
234    }
235    else
236    {
237       MAC_FREE_SHRABL_BUF(pst->region, pst->pool, macCellCfg ,sizeof(MacCellCfg));
238    }
239    return ret;
240 } /* end of MacProcCellCfgReq */
241
242 /**
243  * @brief Layer Manager Configuration request handler.
244  *
245  * @details
246  *
247  *     Function : MacSchCellCfgReq
248  *
249  *     This function sends cell configuration to SCH
250  *
251  *  @param[in]  Pst           *pst
252  *  @param[in]  MacCellCfg    *macCellCfg
253  *  @return  
254  *      -# ROK
255  **/
256 uint8_t MacSchCellCfgReq(Pst *pst, MacCellCfg *macCellCfg)
257 {
258    SchCellCfg schCellCfg;
259    Pst        cfgPst;
260    uint8_t    ssbMaskIdx = 0, rsrcListIdx = 0, sliceIdx=0, plmnIdx = 0;
261
262    memset(&cfgPst, 0, sizeof(Pst));
263    memset(&schCellCfg, 0, sizeof(SchCellCfg));
264    schCellCfg.cellId = macCellCfg->cellId;
265    schCellCfg.phyCellId = macCellCfg->cellCfg.phyCellId;
266
267    for(plmnIdx = 0; plmnIdx < MAX_PLMN; plmnIdx++)
268    {
269       schCellCfg.plmnInfoList[plmnIdx].plmn = macCellCfg->cellCfg.plmnInfoList[plmnIdx].plmn;
270       if(macCellCfg->cellCfg.plmnInfoList[plmnIdx].suppSliceList.snssai) 
271       {
272          schCellCfg.plmnInfoList[plmnIdx].suppSliceList.numSupportedSlices = macCellCfg->cellCfg.plmnInfoList[plmnIdx].suppSliceList.numSupportedSlices;
273          MAC_ALLOC(schCellCfg.plmnInfoList[plmnIdx].suppSliceList.snssai, schCellCfg.plmnInfoList[plmnIdx].suppSliceList.numSupportedSlices * sizeof(Snssai*));
274          if(!schCellCfg.plmnInfoList[plmnIdx].suppSliceList.snssai)
275          {
276             DU_LOG("\nERROR  --> MAC: Memory allocation failed at MacSchCellCfgReq");
277             return RFAILED;
278          }
279          for(sliceIdx=0; sliceIdx<schCellCfg.plmnInfoList[plmnIdx].suppSliceList.numSupportedSlices; sliceIdx++)
280          {
281             if(macCellCfg->cellCfg.plmnInfoList[plmnIdx].suppSliceList.snssai[sliceIdx])
282             {
283                MAC_ALLOC(schCellCfg.plmnInfoList[plmnIdx].suppSliceList.snssai[sliceIdx], sizeof(Snssai));
284                if(!schCellCfg.plmnInfoList[plmnIdx].suppSliceList.snssai[sliceIdx])
285                {
286                   DU_LOG("\nERROR  --> MAC: Memory allocation failed at MacSchCellCfgReq");
287                   return RFAILED;
288                }
289                memcpy(schCellCfg.plmnInfoList[plmnIdx].suppSliceList.snssai[sliceIdx], macCellCfg->cellCfg.plmnInfoList[plmnIdx].suppSliceList.snssai[sliceIdx],  sizeof(Snssai));
290             }
291          }
292       }
293    }
294
295    schCellCfg.dupMode = macCellCfg->cellCfg.dupType;
296    schCellCfg.dlBandwidth = macCellCfg->carrCfg.dlBw;
297    schCellCfg.ulBandwidth = macCellCfg->carrCfg.ulBw;
298
299    schCellCfg.dlCfgCommon.schFreqInfoDlSib.offsetToPointA = macCellCfg->ssbCfg.ssbOffsetPointA;
300    schCellCfg.dlCfgCommon.schFreqInfoDlSib.schSpcCarrier[0].subCarrierSpacing = macCellCfg->ssbCfg.scsCmn;
301
302    /* fill initial DL BWP */
303    schCellCfg.dlCfgCommon.schInitialDlBwp.bwp.freqAlloc.startPrb = macCellCfg->cellCfg.initialDlBwp.bwp.firstPrb;
304    schCellCfg.dlCfgCommon.schInitialDlBwp.bwp.freqAlloc.numPrb = macCellCfg->cellCfg.initialDlBwp.bwp.numPrb;
305    schCellCfg.dlCfgCommon.schInitialDlBwp.bwp.scs = macCellCfg->cellCfg.initialDlBwp.bwp.scs;
306    schCellCfg.dlCfgCommon.schInitialDlBwp.bwp.cyclicPrefix = macCellCfg->cellCfg.initialDlBwp.bwp.cyclicPrefix;
307    
308    schCellCfg.dlCfgCommon.schInitialDlBwp.pdcchCommon.commonSearchSpace.searchSpaceId =
309       macCellCfg->cellCfg.initialDlBwp.pdcchCommon.commonSearchSpace.searchSpaceId;
310    schCellCfg.dlCfgCommon.schInitialDlBwp.pdcchCommon.commonSearchSpace.coresetId =
311       macCellCfg->cellCfg.initialDlBwp.pdcchCommon.commonSearchSpace.coresetId;
312    schCellCfg.dlCfgCommon.schInitialDlBwp.pdcchCommon.commonSearchSpace.monitoringSlot =
313       macCellCfg->cellCfg.initialDlBwp.pdcchCommon.commonSearchSpace.monitoringSlot;
314    schCellCfg.dlCfgCommon.schInitialDlBwp.pdcchCommon.commonSearchSpace.duration =
315       macCellCfg->cellCfg.initialDlBwp.pdcchCommon.commonSearchSpace.duration;
316    schCellCfg.dlCfgCommon.schInitialDlBwp.pdcchCommon.commonSearchSpace.monitoringSymbol =
317       macCellCfg->cellCfg.initialDlBwp.pdcchCommon.commonSearchSpace.monitoringSymbol;
318    schCellCfg.dlCfgCommon.schInitialDlBwp.pdcchCommon.commonSearchSpace.candidate.aggLevel1 =
319       macCellCfg->cellCfg.initialDlBwp.pdcchCommon.commonSearchSpace.candidate.aggLevel1;
320    schCellCfg.dlCfgCommon.schInitialDlBwp.pdcchCommon.commonSearchSpace.candidate.aggLevel2 =
321       macCellCfg->cellCfg.initialDlBwp.pdcchCommon.commonSearchSpace.candidate.aggLevel2;
322    schCellCfg.dlCfgCommon.schInitialDlBwp.pdcchCommon.commonSearchSpace.candidate.aggLevel4 =
323       macCellCfg->cellCfg.initialDlBwp.pdcchCommon.commonSearchSpace.candidate.aggLevel4;
324    schCellCfg.dlCfgCommon.schInitialDlBwp.pdcchCommon.commonSearchSpace.candidate.aggLevel8 =
325       macCellCfg->cellCfg.initialDlBwp.pdcchCommon.commonSearchSpace.candidate.aggLevel8;
326    schCellCfg.dlCfgCommon.schInitialDlBwp.pdcchCommon.commonSearchSpace.candidate.aggLevel16 =
327       macCellCfg->cellCfg.initialDlBwp.pdcchCommon.commonSearchSpace.candidate.aggLevel16;
328    
329    schCellCfg.dlCfgCommon.schInitialDlBwp.pdschCommon.numTimeDomAlloc = macCellCfg->cellCfg.initialDlBwp.pdschCommon.numTimeDomAlloc;
330    for(rsrcListIdx = 0; rsrcListIdx<macCellCfg->cellCfg.initialDlBwp.pdschCommon.numTimeDomAlloc; rsrcListIdx++)
331    {
332       schCellCfg.dlCfgCommon.schInitialDlBwp.pdschCommon.timeDomRsrcAllocList[rsrcListIdx].k0 = 
333          macCellCfg->cellCfg.initialDlBwp.pdschCommon.timeDomRsrcAllocList[rsrcListIdx].k0;
334       schCellCfg.dlCfgCommon.schInitialDlBwp.pdschCommon.timeDomRsrcAllocList[rsrcListIdx].mappingType =
335          macCellCfg->cellCfg.initialDlBwp.pdschCommon.timeDomRsrcAllocList[rsrcListIdx].mappingType;
336       schCellCfg.dlCfgCommon.schInitialDlBwp.pdschCommon.timeDomRsrcAllocList[rsrcListIdx].startSymbol =
337          macCellCfg->cellCfg.initialDlBwp.pdschCommon.timeDomRsrcAllocList[rsrcListIdx].startSymbol;
338       schCellCfg.dlCfgCommon.schInitialDlBwp.pdschCommon.timeDomRsrcAllocList[rsrcListIdx].lengthSymbol =
339          macCellCfg->cellCfg.initialDlBwp.pdschCommon.timeDomRsrcAllocList[rsrcListIdx].lengthSymbol;
340    }
341
342    /* fill SIB1 scheduler parameters */
343    schCellCfg.dlCfgCommon.schPcchCfg.numPO  =  macCellCfg->cellCfg.sib1Cfg.pagingCfg.numPO;
344    schCellCfg.dlCfgCommon.schPcchCfg.poPresent = macCellCfg->cellCfg.sib1Cfg.pagingCfg.poPresent;
345    if(schCellCfg.dlCfgCommon.schPcchCfg.poPresent)
346    {
347       memcpy(schCellCfg.dlCfgCommon.schPcchCfg.pagingOcc, macCellCfg->cellCfg.sib1Cfg.pagingCfg.pagingOcc, MAX_PO_PER_PF);
348    }
349
350    /* fill initial UL BWP */
351    schCellCfg.ulCfgCommon.schInitialUlBwp.bwp.freqAlloc.startPrb = macCellCfg->cellCfg.initialUlBwp.bwp.firstPrb;
352    schCellCfg.ulCfgCommon.schInitialUlBwp.bwp.freqAlloc.numPrb = macCellCfg->cellCfg.initialUlBwp.bwp.numPrb;
353    schCellCfg.ulCfgCommon.schInitialUlBwp.bwp.scs = macCellCfg->cellCfg.initialUlBwp.bwp.scs;
354    schCellCfg.ulCfgCommon.schInitialUlBwp.bwp.cyclicPrefix = macCellCfg->cellCfg.initialUlBwp.bwp.cyclicPrefix;
355    /* fill RACH config params */
356    schCellCfg.ulCfgCommon.schInitialUlBwp.schRachCfg.prachCfgGeneric.prachCfgIdx   = macCellCfg->prachCfg.prachCfgIdx;
357    schCellCfg.ulCfgCommon.schInitialUlBwp.schRachCfg.prachCfgGeneric.msg1Fdm       = macCellCfg->prachCfg.msg1Fdm;
358    schCellCfg.ulCfgCommon.schInitialUlBwp.schRachCfg.prachCfgGeneric.msg1FreqStart = macCellCfg->prachCfg.msg1FreqStart;
359    schCellCfg.ulCfgCommon.schInitialUlBwp.schRachCfg.prachCfgGeneric.zeroCorrZoneCfg = macCellCfg->prachCfg.fdm[0].zeroCorrZoneCfg; 
360    schCellCfg.ulCfgCommon.schInitialUlBwp.schRachCfg.prachCfgGeneric.raRspWindow = macCellCfg->prachCfg.raRspWindow;
361
362    schCellCfg.ulCfgCommon.schInitialUlBwp.schRachCfg.totalNumRaPreamble = macCellCfg->prachCfg.totalNumRaPreamble;
363    schCellCfg.ulCfgCommon.schInitialUlBwp.schRachCfg.ssbPerRach    = macCellCfg->prachCfg.ssbPerRach;
364    schCellCfg.ulCfgCommon.schInitialUlBwp.schRachCfg.numCbPreamblePerSsb = macCellCfg->prachCfg.numCbPreamblePerSsb;
365    schCellCfg.ulCfgCommon.schInitialUlBwp.schRachCfg.raContResTmr = macCellCfg->prachCfg.raContResTmr;
366    schCellCfg.ulCfgCommon.schInitialUlBwp.schRachCfg.rsrpThreshSsb = macCellCfg->prachCfg.rsrpThreshSsb;
367    schCellCfg.ulCfgCommon.schInitialUlBwp.schRachCfg.rootSeqIdx    = macCellCfg->prachCfg.fdm[0].rootSeqIdx;
368    schCellCfg.ulCfgCommon.schInitialUlBwp.schRachCfg.numRootSeq    = macCellCfg->prachCfg.fdm[0].numRootSeq;
369    schCellCfg.ulCfgCommon.schInitialUlBwp.schRachCfg.msg1SubcSpacing = \
370                                             macCellCfg->prachCfg.prachSubcSpacing;
371
372    schCellCfg.ulCfgCommon.schInitialUlBwp.pucchCommon.pucchResourceCommon = \
373                                                                 macCellCfg->cellCfg.initialUlBwp.pucchCommon.pucchResourceCommon;
374    schCellCfg.ulCfgCommon.schInitialUlBwp.pucchCommon.pucchGroupHopping = \
375                                                                 macCellCfg->cellCfg.initialUlBwp.pucchCommon.pucchGroupHopping;
376    schCellCfg.ulCfgCommon.schInitialUlBwp.puschCommon.numTimeDomRsrcAlloc = \
377                                                                 macCellCfg->cellCfg.initialUlBwp.puschCommon.numTimeDomRsrcAlloc;
378    for(rsrcListIdx = 0; rsrcListIdx < macCellCfg->cellCfg.initialUlBwp.puschCommon.numTimeDomRsrcAlloc; rsrcListIdx++)
379    {
380       schCellCfg.ulCfgCommon.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[rsrcListIdx].k2 = 
381          macCellCfg->cellCfg.initialUlBwp.puschCommon.timeDomRsrcAllocList[rsrcListIdx].k2;
382       schCellCfg.ulCfgCommon.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[rsrcListIdx].mappingType =
383          macCellCfg->cellCfg.initialUlBwp.puschCommon.timeDomRsrcAllocList[rsrcListIdx].mappingType;
384       schCellCfg.ulCfgCommon.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[rsrcListIdx].startSymbol =
385          macCellCfg->cellCfg.initialUlBwp.puschCommon.timeDomRsrcAllocList[rsrcListIdx].startSymbol;
386       schCellCfg.ulCfgCommon.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[rsrcListIdx].symbolLength =
387          macCellCfg->cellCfg.initialUlBwp.puschCommon.timeDomRsrcAllocList[rsrcListIdx].symbolLength;
388    }
389
390 #ifdef NR_TDD
391    memcpy(&schCellCfg.tddCfg, &macCellCfg->tddCfg, sizeof(TDDCfg));
392 #endif
393
394    /* fill ssb scheduler parameters */
395    for(ssbMaskIdx=0; ssbMaskIdx<SSB_MASK_SIZE; ssbMaskIdx++)
396    {
397       schCellCfg.ssbPosInBurst[ssbMaskIdx] = macCellCfg->ssbCfg.ssbMask[ssbMaskIdx];
398    }
399    schCellCfg.ssbPeriod    = ssbPeriodicity[macCellCfg->ssbCfg.ssbPeriod];
400    schCellCfg.ssbFrequency = macCellCfg->cellCfg.ssbFreq;
401    schCellCfg.dmrsTypeAPos = macCellCfg->ssbCfg.dmrsTypeAPos;
402    schCellCfg.ssbScs       = macCellCfg->cellCfg.subCarrSpacing;
403    schCellCfg.pdcchCfgSib1.coresetZeroIndex = macCellCfg->cellCfg.sib1Cfg.pdcchCfgSib1.coresetZeroIndex;
404    schCellCfg.pdcchCfgSib1.searchSpaceZeroIndex = macCellCfg->cellCfg.sib1Cfg.pdcchCfgSib1.searchSpaceZeroIndex;
405    schCellCfg.ssbPbchPwr = macCellCfg->ssbCfg.ssbPbchPwr;
406    schCellCfg.ssbSubcOffset = macCellCfg->ssbCfg.ssbScOffset;
407    schCellCfg.sib1PduLen = macCellCfg->cellCfg.sib1Cfg.sib1PduLen;
408    
409    FILL_PST_MAC_TO_SCH(cfgPst, EVENT_SCH_CELL_CFG);
410
411    return(SchMessageRouter(&cfgPst, (void *)&schCellCfg));
412 } /* end of MacSchCellCfgReq */
413
414
415 /*******************************************************************
416  *
417  * @brief Sends Cell config confirm to DU APP
418  *
419  * @details
420  *
421  *    Function : MacSendCellCfgCfm
422  *
423  *    Functionality:
424  *      Sends Cell config confirm to DU APP
425  *
426  * @params[in] Response status
427  * @return void
428  *
429  * ****************************************************************/
430 void MacSendCellCfgCfm(uint16_t cellId, uint8_t response)
431 {
432    Pst pst;
433    uint16_t   cellIdx;
434    MacCellCfgCfm macCellCfgCfm;
435
436    memset(&pst, 0, sizeof(Pst));
437
438    GET_CELL_IDX(cellId, cellIdx);
439    macCellCfgCfm.cellId = macCb.macCell[cellIdx]->macCellCfg.cellId;
440    macCellCfgCfm.rsp = response;
441
442    /* Fill Pst */
443    FILL_PST_MAC_TO_DUAPP(pst, EVENT_MAC_CELL_CONFIG_CFM);
444    pst.selector  = ODU_SELECTOR_LC;
445
446    (*packMacCellCfmOpts[pst.selector])(&pst,&macCellCfgCfm);
447 }
448
449
450 /**
451  * @brief Layer Manager Configuration response handler.
452  *
453  * @details
454  *
455  *     Function : MacProcSchCellCfgCfm
456  *
457  *     This function processes cell configuration to SCH
458  *
459  *  @param[in]  Pst           *pst
460  *  @param[in]  SchCellCfgCfm *schCellCfgCfm
461  *  @return  int
462  *      -# ROK
463  **/
464 uint8_t MacProcSchCellCfgCfm(Pst *pst, SchCellCfgCfm *schCellCfgCfm)
465 {
466    uint16_t *cellId = NULLP;
467
468 #ifdef CALL_FLOW_DEBUG_LOG
469    DU_LOG("\nCall Flow: ENTSCH -> ENTMAC : EVENT_SCH_CELL_CFG_CFM\n");
470 #endif
471
472    if(schCellCfgCfm->rsp == RSP_OK)
473    {
474       cellId = &schCellCfgCfm->cellId;
475 #ifdef INTEL_TIMER_MODE
476       sendToLowerMac(UL_IQ_SAMPLE, 0, (void *)cellId);
477 #else
478       sendToLowerMac(CONFIG_REQUEST, 0, (void *)cellId);
479 #endif
480    }
481    else
482    {
483       MacSendCellCfgCfm(schCellCfgCfm->cellId, RSP_NOK);
484    }
485    return ROK;
486 }
487
488 /*******************************************************************
489  *
490  * @brief MAC handler for config response from PHY
491  *
492  * @details
493  *
494  *    Function : fapiMacConfigRsp
495  *
496  *    Functionality:
497  *     Processes config response from PHY and sends cell config
498  *     confirm to DU APP
499  *
500  * @params[in]
501  * @return void
502  *
503  * ****************************************************************/
504 void fapiMacConfigRsp(uint16_t cellId)
505 {
506    /* TODO : Processing of config response from PHY */
507
508    /* Send cell config cfm to DU APP */
509    MacSendCellCfgCfm(cellId, RSP_OK);
510 }
511
512 /*******************************************************************
513  *
514  * @brief Fill and Send Cell Delete response from MAC to DU APP
515  *
516  * @details
517  *
518  *    Function : MacSendCellDeleteRsp
519  *
520  *    Functionality: Fill and Send Cell Delete response from MAC to DUAPP
521  *
522  * @params[in] MAC Cell delete result
523  *             SCH Cell delete response
524  * @return ROK     - success
525  *         RFAILED - failure
526  *
527  * ****************************************************************/
528 uint8_t MacSendCellDeleteRsp(CauseOfResult  status, uint8_t cellId)
529 {
530    MacCellDeleteRsp *deleteRsp=NULLP;
531    Pst            rspPst;
532
533    MAC_ALLOC_SHRABL_BUF(deleteRsp, sizeof(MacCellDeleteRsp));
534    if(!deleteRsp)
535    {
536       DU_LOG("\nERROR  -->  MAC : MacSendCellDeleteRsp(): Memory allocation for Cell delete response failed");
537       return RFAILED;
538    }
539
540    /* Filling CELL delete response */
541    
542    memset(deleteRsp, 0, sizeof(MacCellDeleteRsp));
543    deleteRsp->cellId = cellId;
544    deleteRsp->status = status;
545
546    /* Fill Post structure and send CELL delete response*/
547    memset(&rspPst, 0, sizeof(Pst));
548    FILL_PST_MAC_TO_DUAPP(rspPst, EVENT_MAC_CELL_DELETE_RSP);
549    return (*macDuCellDeleteRspOpts[rspPst.selector])(&rspPst, deleteRsp);
550 }
551
552 /*******************************************************************
553  *
554  * @brief  Processes CELL delete response from scheduler
555  *
556  * @details
557  *
558  *    Function : MacProcSchCellDeleteRsp
559  *
560  *    Functionality:
561  *      Processes CELL delete from scheduler
562  *
563  * @params[in] Pst : Post structure
564  *             schCellDelRsp : Scheduler CELL delete respons
565  * @return ROK     - success
566  *         RFAILED - failure
567  *
568  * * ****************************************************************/
569 uint8_t MacProcSchCellDeleteRsp(Pst *pst, SchCellDeleteRsp *schCellDelRsp)
570 {
571    uint8_t  ret = ROK, sliceIdx = 0, plmnIdx = 0;
572    uint16_t cellIdx=0;
573    CauseOfResult  cause;
574
575 #ifdef CALL_FLOW_DEBUG_LOG
576    DU_LOG("\nCall Flow: ENTSCH -> ENTMAC : EVENT_CELL_DELETE_RSP_TO_MAC\n");
577 #endif  
578
579    if(schCellDelRsp)
580    {
581       if(schCellDelRsp->rsp == RSP_OK)
582       {
583          DU_LOG("\nINFO   -->  MAC : SCH CELL Delete response for cellId[%d] is successful ", \
584                schCellDelRsp->cellId);
585          GET_CELL_IDX(schCellDelRsp->cellId, cellIdx);
586          if(macCb.macCell[cellIdx])
587          {
588             if(macCb.macCell[cellIdx]->cellId == schCellDelRsp->cellId)
589             {
590                cause = SUCCESSFUL;
591                for(plmnIdx = 0; plmnIdx < MAX_PLMN; plmnIdx++)
592                {
593                   if(macCb.macCell[cellIdx]->macCellCfg.cellCfg.plmnInfoList[plmnIdx].suppSliceList.snssai)
594                   {
595                      for(sliceIdx = 0; sliceIdx<macCb.macCell[cellIdx]->macCellCfg.cellCfg.plmnInfoList[plmnIdx].suppSliceList.numSupportedSlices; sliceIdx++)
596                      {
597                         MAC_FREE(macCb.macCell[cellIdx]->macCellCfg.cellCfg.plmnInfoList[plmnIdx].suppSliceList.snssai[sliceIdx], sizeof(Snssai));
598                      }
599                      MAC_FREE(macCb.macCell[cellIdx]->macCellCfg.cellCfg.plmnInfoList[plmnIdx].suppSliceList.snssai, macCb.macCell[cellIdx]->macCellCfg.cellCfg.plmnInfoList[plmnIdx].\
600                            suppSliceList.numSupportedSlices * sizeof(Snssai*));
601                   }
602                }
603                MAC_FREE(macCb.macCell[cellIdx]->macCellCfg.cellCfg.sib1Cfg.sib1Pdu, \
604                      macCb.macCell[cellIdx]->macCellCfg.cellCfg.sib1Cfg.sib1PduLen);
605                MAC_FREE(macCb.macCell[cellIdx], sizeof(MacCellCb));
606             }
607             else
608             {
609                DU_LOG("ERROR  -->  MAC : MacProcSchCellDeleteRsp(): CellId[%d] does not exists", schCellDelRsp->cellId);
610                cause = CELLID_INVALID;
611                ret = RFAILED;
612             }
613          }
614          else
615          {
616             DU_LOG("ERROR  -->  MAC : MacProcSchCellDeleteRsp(): CellId[%d] does not exists", schCellDelRsp->cellId);
617             cause = CELLID_INVALID;
618             ret = RFAILED;
619          }
620       }
621       else
622       {
623          DU_LOG("ERROR  -->  MAC : MacProcSchCellDeleteRsp(): CellId[%d] does not exists", schCellDelRsp->cellId);
624          cause = CELLID_INVALID;
625          ret = RFAILED;
626       }
627       if(MacSendCellDeleteRsp(cause, schCellDelRsp->cellId) != ROK)
628       {
629          DU_LOG("\nERROR  -->  MAC: MacProcSchCellDeleteRsp(): Failed to send CELL delete response");
630          ret = RFAILED;
631       }
632
633    }
634    else
635    {
636       DU_LOG("\nERROR  -->  MAC: MacProcSchCellDeleteRsp(): schCellDelRsp is NULL");
637       ret = RFAILED;
638    }
639    return ret;
640 }
641
642 /*******************************************************************
643  *
644  * @brief Sends Cell delete req to Scheduler
645  *
646  * @details
647  *
648  *    Function : sendCellDelReqToSch
649  *
650  *    Functionality: sends Cell delete req to Scheduler
651  *
652  * @params[in]  SchCellDelete *schCellDel
653  * @return ROK     - success
654  *         RFAILED - failure
655  *
656  * ****************************************************************/
657
658 uint8_t sendCellDelReqToSch(SchCellDeleteReq *schCellDelReq)
659 {
660    Pst schPst;
661    FILL_PST_MAC_TO_SCH(schPst, EVENT_CELL_DELETE_REQ_TO_SCH);
662    return(SchMessageRouter(&schPst, (void *)schCellDelReq));
663 }
664
665 /*******************************************************************
666  *
667  * @brief Handles CELL Delete requst from DU APP
668  *
669  * @details
670  *
671  *    Function : MacProcCellDeleteReq
672  *
673  *    Functionality: Handles CELL Delete requst from DU APP
674  *
675  * @params[in] Pst *pst, MacCellDelete *cellDelete
676  * @return ROK     - success
677  *         RFAILED - failure
678  *
679  *
680  * ****************************************************************/
681 uint8_t MacProcCellDeleteReq(Pst *pst, MacCellDeleteReq *cellDelete)
682 {
683    uint8_t ret = ROK, cellIdx=0;
684    SchCellDeleteReq schCellDelete;
685
686    DU_LOG("\nINFO   -->  MAC : Cell Delete Request received for cellId[%d]", cellDelete->cellId);
687
688    if(cellDelete)
689    {
690       GET_CELL_IDX(cellDelete->cellId, cellIdx);
691       if(macCb.macCell[cellIdx])
692       {
693          if(macCb.macCell[cellIdx]->cellId == cellDelete->cellId)
694          {
695             memset(&schCellDelete, 0, sizeof(SchCellDeleteReq ));
696             schCellDelete.cellId =  cellDelete->cellId;
697             ret = sendCellDelReqToSch(&schCellDelete);
698             if(ret != ROK)
699             {
700                DU_LOG("\nERROR  -->  MAC : MacProcCellDeleteReq(): Failed to send UE Delete Request to SCH");
701                ret = RFAILED;
702             }
703          }
704          else
705          {
706             DU_LOG("\nERROR  -->  MAC : MacProcCellDeleteReq(): Failed to find the MacUeCb of CellId = %d",\
707             cellDelete->cellId);
708             ret = RFAILED;
709          }
710       }
711       else
712       {
713          DU_LOG("\nERROR  -->  MAC : MacProcCellDeleteReq(): Failed to find the MacUeCb of CellId = %d",\
714                cellDelete->cellId);
715          ret = RFAILED;
716       }
717
718       if(ret == RFAILED)
719       {
720           DU_LOG("\nERROR  -->  MAC : MacProcCellDeleteReq(): Sending failure response to DU");
721           if(MacSendCellDeleteRsp(CELLID_INVALID, cellDelete->cellId) != ROK)
722           {
723              DU_LOG("\nERROR  -->  MAC : MacProcCellDeleteReq(): failed to send cell delete rsp for cellID[%d]",\
724              cellDelete->cellId);
725           }
726
727       }
728       MAC_FREE_SHRABL_BUF(pst->region, pst->pool, cellDelete, sizeof(MacCellDeleteReq));
729    }
730    else
731    {
732       DU_LOG("\nERROR  -->  MAC : MacProcCellDeleteReq(): Received MacCellDelete is NULL");
733       ret = RFAILED;
734    }
735    return ret;
736 }
737
738 /**
739  * @brief free the temporary slice cfg stored in macCb.
740  *
741  * @details
742  *
743  *     Function : freeMacSliceCfgReq 
744  *
745  *   free the temporary slice cfg stored in macCb
746  *
747  *  @param[in]  
748  *  @return  int
749  *      -# ROK
750  **/
751 void freeMacSliceCfgReq(MacSliceCfgReq *cfgReq,Pst *pst)
752 {
753     uint8_t policyIdx = 0, memberListIdx=0;
754
755     if(cfgReq->numOfRrmPolicy)
756     {
757        if(cfgReq->listOfRrmPolicy)
758        {
759           for(policyIdx = 0; policyIdx<cfgReq->numOfRrmPolicy; policyIdx++)
760           {
761              if(cfgReq->listOfRrmPolicy[policyIdx])
762              {
763                 if(cfgReq->listOfRrmPolicy[policyIdx]->numOfRrmPolicyMem)
764                 {
765                    if(cfgReq->listOfRrmPolicy[policyIdx]->rRMPolicyMemberList)
766                    {
767                       for(memberListIdx = 0; memberListIdx<cfgReq->listOfRrmPolicy[policyIdx]->numOfRrmPolicyMem; memberListIdx++)
768                       {
769                          MAC_FREE_SHRABL_BUF(pst->region, pst->pool, cfgReq->listOfRrmPolicy[policyIdx]->rRMPolicyMemberList[memberListIdx], sizeof(RrmPolicyMemberList));
770                       }
771                       MAC_FREE_SHRABL_BUF(pst->region, pst->pool, cfgReq->listOfRrmPolicy[policyIdx]->rRMPolicyMemberList,\
772                       cfgReq->listOfRrmPolicy[policyIdx]->numOfRrmPolicyMem * sizeof(RrmPolicyMemberList*));
773                    }
774                 }
775                 MAC_FREE_SHRABL_BUF(pst->region, pst->pool, cfgReq->listOfRrmPolicy[policyIdx], sizeof(MacSliceRrmPolicy));
776              }
777           }
778           MAC_FREE_SHRABL_BUF(pst->region, pst->pool, cfgReq->listOfRrmPolicy, cfgReq->numOfRrmPolicy  * sizeof(MacSliceRrmPolicy*));
779        }
780     }
781
782 }
783
784 /**
785  * @brief send slice cfg response to duapp.
786  *
787  * @details
788  *
789  *     Function : MacSendSliceConfigRsp
790  *
791  *   sends  slice cfg response to duapp
792  *
793  *  @param[in] MacSliceCfgRsp macSliceCfgRsp 
794  *  @return  int
795  *      -# ROK
796  **/
797 uint8_t MacSendSliceConfigRsp(MacSliceCfgRsp *macSliceCfgRsp)
798 {
799     Pst  rspPst;
800     
801     memset(&rspPst, 0, sizeof(Pst));
802     FILL_PST_MAC_TO_DUAPP(rspPst, EVENT_MAC_SLICE_CFG_RSP);
803     return (*macDuSliceCfgRspOpts[rspPst.selector])(&rspPst, macSliceCfgRsp);
804
805 }
806
807 /**
808  * @brief Mac process the slice cfg rsp received from sch.
809  *
810  * @details
811  *
812  *     Function : MacProcSchSliceCfgRsp 
813  *
814  *     This function  process the slice cfg rsp received from sch
815  *
816  *  @param[in]  Pst           *pst
817  *  @param[in]  SchSliceCfgRsp *sliceCfgrsp
818  *  @return  int
819  *      -# ROK
820  **/
821
822 uint8_t MacProcSchSliceCfgRsp(Pst *pst, SchSliceCfgRsp *schSliceCfgRsp)
823 {
824    MacSliceCfgRsp *macSliceCfgRsp = NULLP;
825
826    if(schSliceCfgRsp)
827    {
828       MAC_ALLOC_SHRABL_BUF(macSliceCfgRsp, sizeof(MacSliceCfgRsp));
829       if(macSliceCfgRsp == NULLP)
830       {
831           DU_LOG("\nERROR  -->  MAC : Failed to allocate memory in MacProcSchSliceCfgRsp");
832           return RFAILED;
833       }
834       macSliceCfgRsp->snssai = schSliceCfgRsp->snssai;
835       if(schSliceCfgRsp->rsp == RSP_OK)
836          macSliceCfgRsp->rsp    = MAC_DU_APP_RSP_OK;
837       else
838       {
839          macSliceCfgRsp->rsp    = MAC_DU_APP_RSP_NOK;
840       }
841       macSliceCfgRsp->cause  = schSliceCfgRsp->cause;
842       MacSendSliceConfigRsp(macSliceCfgRsp);
843    }
844    return ROK;
845 }
846
847 /**
848  * @brief send slice cfg response to duapp.
849  *
850  * @details
851  *
852  *     Function : MacSendSliceReconfigRsp 
853  *
854  *   sends  slice cfg response to duapp
855  *
856  *  @param[in] MacSliceRecfgRsp macSliceRecfgRsp
857  *  @return  int
858  *      -# ROK
859  **/
860 uint8_t MacSendSliceReconfigRsp(MacSliceRecfgRsp *macSliceRecfgRsp)
861 {
862    Pst  rspPst;
863
864    memset(&rspPst, 0, sizeof(Pst));
865    FILL_PST_MAC_TO_DUAPP(rspPst, EVENT_MAC_SLICE_RECFG_RSP);
866    return (*macDuSliceRecfgRspOpts[rspPst.selector])(&rspPst, macSliceRecfgRsp);
867
868 }
869
870 /**
871  * @brief Mac process the slice cfg rsp received from sch.
872  *
873  * @details
874  *
875  *     Function : MacProcSchSliceRecfgRsp 
876  *
877  *     This function  process the slice cfg rsp received from sch
878  *
879  *  @param[in]  Pst           *pst
880  *  @param[in]  SchSliceCfgRsp *schSliceRecfgRsp
881  *  @return  int
882  *      -# ROK
883  **/
884 uint8_t MacProcSchSliceRecfgRsp(Pst *pst, SchSliceRecfgRsp *schSliceRecfgRsp)
885 {
886    MacSliceRecfgRsp *macSliceRecfgRsp = NULLP;
887
888    if(schSliceRecfgRsp)
889    {
890       MAC_ALLOC_SHRABL_BUF(macSliceRecfgRsp, sizeof(MacSliceRecfgRsp));
891       if(macSliceRecfgRsp == NULLP)
892       {
893           DU_LOG("\nERROR  -->  MAC : Failed to allocate memory in MacProcSchSliceRecfgRsp");
894           return RFAILED;
895       }
896
897       macSliceRecfgRsp->snssai = schSliceRecfgRsp->snssai;
898       if(schSliceRecfgRsp->rsp == RSP_OK)
899          macSliceRecfgRsp->rsp    = MAC_DU_APP_RSP_OK;
900       else
901       {
902          macSliceRecfgRsp->rsp    = MAC_DU_APP_RSP_NOK;
903       }
904       macSliceRecfgRsp->cause  = schSliceRecfgRsp->cause;
905       MacSendSliceReconfigRsp(macSliceRecfgRsp);
906    }
907    return ROK;
908 }
909
910 /**
911  * @brief Mac process the downlink pcch indication received from DUAPP
912  *
913  * @details
914  *
915  *     Function : MacProcDlPcchInd 
916  *
917  *     This function process the downlink pcch indication received from DUAPP
918  *
919  *  @param[in]  Pst           *pst
920  *  @param[in]  DlPcchInd    *pcchInd 
921  *  @return  int
922  *      -# ROK
923  **/
924 uint8_t MacProcDlPcchInd(Pst *pst, DlPcchInd *pcchInd)
925 {
926    uint8_t ret = RFAILED;
927    uint16_t cellIdx = 0;
928    Pst       schPst;
929    SchPageInd *schPageInd = NULLP;
930
931    if(pcchInd)
932    {
933       DU_LOG("\nINFO   -->  MAC : Received DL PCCH IND from DU_APP for cellId[%d]", pcchInd->cellId);
934       
935       GET_CELL_IDX(pcchInd->cellId, cellIdx);
936
937       if(macCb.macCell[cellIdx] == NULLP || macCb.macCell[cellIdx]->cellId != pcchInd->cellId)
938       {
939          DU_LOG("\nERROR  -->  MAC : MacProcDlPcchInd(): CellId[%d] does not exist", pcchInd->cellId);
940       }
941       else
942       {
943          if((pcchInd->pcchPdu == NULLP) || (pcchInd->pduLen <= 0))
944          {
945             DU_LOG("\nERROR  -->  MAC : MacProcDlPcchInd(): Received Pcch pdu is null");
946          }
947          else
948          {
949             MAC_ALLOC(schPageInd, sizeof(SchPageInd));
950             if(schPageInd == NULLP)
951             {
952                DU_LOG("\nERROR  -->  MAC : MacProcDlPcchInd(): Failed to allocate memory");
953             }
954             else
955             {
956                schPageInd->cellId = pcchInd->cellId;
957                schPageInd->pf = pcchInd->pf;
958                schPageInd->i_s = pcchInd->i_s;
959                schPageInd->pduLen = pcchInd->pduLen;
960                
961                MAC_ALLOC(schPageInd->pagePdu, pcchInd->pduLen);
962                if(schPageInd->pagePdu == NULLP)
963                {
964                   DU_LOG("\nERROR  -->  MAC : MacProcDlPcchInd(): Failed to allocate memory");
965                   MAC_FREE(schPageInd, sizeof(SchPageInd));
966                }
967                else
968                {
969                   memcpy(schPageInd->pagePdu, pcchInd->pcchPdu, pcchInd->pduLen);
970
971                   FILL_PST_MAC_TO_SCH(schPst, EVENT_PAGING_IND_TO_SCH);
972                   ret = SchMessageRouter(&schPst, (void *)schPageInd);
973                }
974             }
975          }
976       }
977       if((pcchInd->pcchPdu) && (pcchInd->pduLen > 0))
978       {
979          MAC_FREE_SHRABL_BUF(pst->region, pst->pool, pcchInd->pcchPdu, pcchInd->pduLen);
980       }
981       MAC_FREE_SHRABL_BUF(pst->region, pst->pool, pcchInd, sizeof(DlPcchInd));
982    }
983    else
984    {
985       DU_LOG("\nERROR  -->  MAC : MacProcDlPcchInd(): Received Null pointer");
986    }
987    return ret;
988 }
989
990 /**
991  * @brief Mac process the downlink Broadcast Req received from DUAPP
992  *
993  * @details
994  *
995  *     Function : MacProcDlBroadcastReq 
996  *
997  *     This function process the downlink Broadcast Req received from DUAPP
998  *
999  *  @param[in]  Pst           *pst
1000  *  @param[in]  DlBroadcastReq    *dlBroadcastReq 
1001  *  @return  int
1002  *      -# ROK
1003  **/
1004 uint8_t MacProcDlBroadcastReq(Pst *pst, MacDlBroadcastReq *dlBroadcastReq)
1005 {
1006    uint8_t ret = ROK, idx=0;
1007    uint16_t cellIdx = 0;
1008
1009    if(dlBroadcastReq)
1010    {
1011       DU_LOG("\nINFO   -->  MAC : Received DL braodcast req from DU_APP for cellId[%d]", dlBroadcastReq->cellId);
1012       
1013       GET_CELL_IDX(dlBroadcastReq->cellId, cellIdx);
1014
1015       if(macCb.macCell[cellIdx] == NULLP || macCb.macCell[cellIdx]->cellId != dlBroadcastReq->cellId)
1016       {
1017          ret = RFAILED;
1018          DU_LOG("\nERROR  -->  MAC : MacProcDlBroadcastReq(): CellId[%d] does not exist", dlBroadcastReq->cellId);
1019       }
1020       else
1021       {
1022          /*TODO - Complete the processing of DL Broadcast Request*/
1023       }
1024       for(idx = 0; idx<dlBroadcastReq->numSiBlock; idx++)
1025       {
1026          MAC_FREE_SHRABL_BUF(pst->region, pst->pool, dlBroadcastReq->siSchedulingInfo[idx]->siAreaID, sizeof(uint8_t));
1027          MAC_FREE_SHRABL_BUF(pst->region, pst->pool, dlBroadcastReq->siSchedulingInfo[idx], sizeof(SiSchedulingInfo));
1028       }
1029       MAC_FREE_SHRABL_BUF(pst->region, pst->pool, dlBroadcastReq, sizeof(MacDlBroadcastReq));
1030    }
1031    else
1032    {
1033       ret = RFAILED;
1034       DU_LOG("\nERROR  -->  MAC : MacProcDlBroadcastReq(): Received Null pointer");
1035    }
1036    return ret;
1037 }
1038
1039 /**
1040  * @brief Fill and send statistics response to DU APP
1041  *
1042  * @details
1043  *
1044  *     Function : MacSendStatsRspToDuApp
1045  *
1046  *     Fill and send statistics response to DU APP
1047  *
1048  *  @param[in]  Response
1049  *  @param[in]  Cause of response
1050  *  @return  int
1051  *      -# ROK
1052  **/
1053 uint8_t MacSendStatsRspToDuApp(MacStatsRsp *statsRsp)
1054 {
1055    uint8_t ret = ROK;
1056    Pst  pst;
1057    MacStatsRsp *macStatsRsp = NULLP;
1058
1059     DU_LOG("\nINFO  -->  MAC : MacSendStatsRspToDuApp: Sending Statistics Response to DU APP");
1060
1061    /* Workaround : To skip corrupted memory, allocating a pointer that will
1062     * remain unused */
1063    uint8_t *dummyPtr = NULLP;
1064    MAC_ALLOC_SHRABL_BUF(dummyPtr, sizeof(uint8_t));
1065
1066    MAC_ALLOC_SHRABL_BUF(macStatsRsp, sizeof(MacStatsRsp));
1067    if(macStatsRsp == NULLP)
1068    {
1069       DU_LOG("\nERROR  -->  MAC : Failed to allocate memory in MacProcSchStatsRsp");
1070       ret = RFAILED;
1071    }
1072    else
1073    {
1074       memcpy(macStatsRsp, statsRsp, sizeof(MacStatsRsp));
1075       memset(statsRsp, 0, sizeof(MacStatsRsp));
1076
1077       memset(&pst, 0, sizeof(Pst));
1078       FILL_PST_MAC_TO_DUAPP(pst, EVENT_MAC_STATISTICS_RSP);
1079       if(((*macDuStatsRspOpts[pst.selector])(&pst, macStatsRsp))!= ROK)
1080       {
1081          DU_LOG("\nERROR  -->  MAC : Failed to send statistics response to DU APP");
1082          MAC_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, macStatsRsp, sizeof(MacStatsRsp));
1083          ret = RFAILED;
1084       }
1085    }
1086
1087    /* Workaround : Freeing the dummy pointer */
1088    MAC_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, dummyPtr, sizeof(uint8_t));
1089    return ret;
1090 }
1091
1092 /*******************************************************************
1093  *
1094  * @brief Rejects all statistics group requested by DU APP
1095  *
1096  * @details
1097  *
1098  *    Function : MacRejectAllStats
1099  *
1100  *    Functionality: Add all statistics group received in statistics
1101  *       request from DU APP, to Reject-Stats-Group-List in statistics
1102  *       response to DU APP
1103  *
1104  * @params[in]  Statistics request from DU APP
1105  *              Cause of rejection
1106  * @return ROK     - success
1107  *         RFAILED - failure
1108  *
1109  * ****************************************************************/
1110 uint8_t MacRejectAllStats(MacStatsReq *macStatsReq, CauseOfResult cause)
1111 {
1112    uint8_t grpIdx = 0;
1113    MacStatsRsp macStatsRsp;
1114
1115    memset(&macStatsRsp, 0, sizeof(MacStatsRsp));
1116
1117    /* Copying all stats group from stats request to stats response */
1118    macStatsRsp.subscriptionId = macStatsReq->subscriptionId;
1119    for(grpIdx = 0; grpIdx < macStatsReq->numStatsGroup; grpIdx++)
1120    {
1121       macStatsRsp.statsGrpRejectedList[grpIdx].groupId = macStatsReq->statsGrpList[grpIdx].groupId;
1122       macStatsRsp.statsGrpRejectedList[grpIdx].cause = cause;
1123    }
1124    macStatsRsp.numGrpRejected = macStatsReq->numStatsGroup;
1125
1126    return MacSendStatsRspToDuApp(&macStatsRsp);
1127 }
1128
1129 /**
1130  * @brief Mac process the statistics Req received from DUAPP
1131  *
1132  * @details
1133  *
1134  *     Function : MacProcStatsReq
1135  *
1136  *     This function process the statistics request from duapp:
1137  *     [Step 1] Basic validation. If fails, all stats group in stats request are
1138  *     rejected.
1139  *     [Step 2] If basic validations passed, traverse all stats group and
1140  *     validate each measurement types in each group.
1141  *     [Step 3] If any measurement type validation fails in a group, that group 
1142  *     is not configured and it is added to stats-group-rejected-list in
1143  *     mac-stats-response message.
1144  *     [Step 4] Even if one group passes all validation, it is sent to SCH in
1145  *     statistics request. The mac-stats-response message is added to
1146  *     pending-response list. This will be sent to DU APP after stats response
1147  *     is received from SCH.
1148  *     [Step 5] If none of the groups passes all validation, mac-stats-response
1149  *     is sent to du app with all group as part of stats-group-rejected-list.
1150  *
1151  *  @param[in]  Pst      *pst
1152  *  @param[in]  StatsReq *statsReq
1153  *  @return  int
1154  *      -# ROK
1155  **/
1156 uint8_t MacProcStatsReq(Pst *pst, MacStatsReq *macStatsReq)
1157 {
1158    uint8_t       macStatsGrpIdx = 0, macStatsIdx = 0, schStatsGrpIdx = 0, schStatsIdx = 0;
1159    uint8_t       ret = RFAILED;
1160    bool          measTypeInvalid = false;
1161    Pst           schPst;
1162    MacStatsGrpInfo *macStatsGrp = NULLP;
1163    SchStatsReq     *schStatsReq = NULLP;
1164    MacStatsRsp     *macStatsRsp = NULLP;
1165
1166    DU_LOG("\nINFO   -->  MAC : Received Statistics Request from DU_APP");
1167
1168    if(macStatsReq == NULLP)
1169    {
1170       DU_LOG("\nERROR  -->  MAC : MacProcStatsReq(): Received Null pointer");
1171       return RFAILED;
1172    }
1173    
1174    /* [Step 1] Basic validation. If fails, statistics response is sent to DU APP
1175     * that rejectes all stats */
1176
1177    /* If number of statistics request for which response is still pending
1178     * towards DU APP has reached its maximum limit */
1179    if(macCb.statistics.numPendingStatsRsp >= MAX_PENDING_STATS_RSP)
1180    {
1181       DU_LOG("\nERROR  -->  MAC : MacProcStatsReq: Maximum number of statistics response is pending. \
1182          Cannot process new request."); 
1183       MacRejectAllStats(macStatsReq, RESOURCE_UNAVAILABLE);
1184       MAC_FREE_SHRABL_BUF(pst->region, pst->pool, macStatsReq, sizeof(MacStatsReq));
1185       return RFAILED;
1186    }
1187
1188    /* If memory resources are unavailable */
1189    MAC_ALLOC(schStatsReq, sizeof(SchStatsReq));
1190    if(schStatsReq == NULLP)
1191    {
1192       DU_LOG("\nERROR  -->  MAC : MacProcStatsReq: Failed to allocate memory");
1193       MacRejectAllStats(macStatsReq, RESOURCE_UNAVAILABLE);
1194       MAC_FREE_SHRABL_BUF(pst->region, pst->pool, macStatsReq, sizeof(MacStatsReq));
1195       return RFAILED;
1196    }
1197
1198    /* Add stats response to pending response list */ 
1199    macStatsRsp = &macCb.statistics.pendingStatsRsp[macCb.statistics.numPendingStatsRsp];
1200    memset(macStatsRsp, 0, sizeof(MacStatsRsp));
1201
1202    /* [Step 2] Traverse all stats group and validate each measurement types in each group */
1203    schStatsReq->subscriptionId = macStatsReq->subscriptionId;
1204    schStatsReq->numStatsGroup = 0;
1205    for(macStatsGrpIdx = 0; macStatsGrpIdx < macStatsReq->numStatsGroup; macStatsGrpIdx++)
1206    {
1207       measTypeInvalid = false;
1208       schStatsIdx = 0;
1209       macStatsGrp = &macStatsReq->statsGrpList[macStatsGrpIdx];
1210
1211       for(macStatsIdx=0; macStatsIdx < macStatsGrp->numStats; macStatsIdx++)
1212       {
1213          /* Validate each measurement type */
1214          switch(macStatsGrp->statsList[macStatsIdx])
1215          {
1216             case MAC_DL_TOTAL_PRB_USAGE:
1217                {
1218                   schStatsReq->statsGrpList[schStatsGrpIdx].statsList[schStatsIdx] = SCH_DL_TOTAL_PRB_USAGE;
1219                   break;
1220                }
1221             case MAC_UL_TOTAL_PRB_USAGE:
1222                {
1223                   schStatsReq->statsGrpList[schStatsGrpIdx].statsList[schStatsIdx] = SCH_UL_TOTAL_PRB_USAGE;
1224                   break;
1225                }
1226             default:
1227                {
1228                   DU_LOG("\nERROR  -->  MAC : MacProcStatsReq: Invalid measurement type [%d]", \
1229                      macStatsGrp->statsList[macStatsIdx]);
1230                   measTypeInvalid = true;
1231                }
1232          }
1233
1234          /* Even if one measurement type is invalid, this group is rejected */
1235          if(measTypeInvalid)
1236          {
1237             memset(&schStatsReq->statsGrpList[schStatsGrpIdx], 0, sizeof(SchStatsGrpInfo));
1238             break;
1239          }
1240          
1241          schStatsIdx++;
1242       }
1243
1244       /* If all measurement type is valid, add group info to send to SCH */
1245       if(!measTypeInvalid)
1246       {
1247          schStatsReq->statsGrpList[schStatsGrpIdx].groupId = macStatsGrp->groupId;
1248          schStatsReq->statsGrpList[schStatsGrpIdx].periodicity = macStatsGrp->periodicity;
1249          schStatsReq->statsGrpList[schStatsGrpIdx].numStats = schStatsIdx;
1250          schStatsGrpIdx++;
1251       }
1252       else
1253       {
1254          /* [Step 3] If any measurement type validation fails in a group, that group 
1255           * is not configured and it is added to stats-group-rejected-list in
1256           * mac-stats-response message */
1257          macStatsRsp->statsGrpRejectedList[macStatsRsp->numGrpRejected].groupId = macStatsGrp->groupId;
1258          macStatsRsp->statsGrpRejectedList[macStatsRsp->numGrpRejected].cause = PARAM_INVALID;
1259          macStatsRsp->numGrpRejected++;
1260       }
1261    }
1262    schStatsReq->numStatsGroup = schStatsGrpIdx;
1263
1264    macStatsRsp->subscriptionId = macStatsReq->subscriptionId;
1265
1266    if(schStatsReq->numStatsGroup)
1267    {
1268       /* [Step 4] Even if one group passes all validation, it is sent to SCH in
1269        * statistics request. The mac-stats-response message is added to
1270        * pending-response list. */     
1271       macCb.statistics.numPendingStatsRsp++;
1272
1273       FILL_PST_MAC_TO_SCH(schPst, EVENT_STATISTICS_REQ_TO_SCH);
1274       ret = SchMessageRouter(&schPst, (void *)schStatsReq);
1275    }
1276    else
1277    {
1278       /* [Step 5] If none of the groups passes all validation, mac-stats-response
1279        * is sent to du app with all group as part of stats-group-rejected-list. */
1280       DU_LOG("\nERROR  -->  MAC : MacProcStatsReq: All statistics group found invalid");
1281       MAC_FREE(schStatsReq, sizeof(SchStatsReq));
1282       ret = MacSendStatsRspToDuApp(macStatsRsp);
1283    }
1284
1285    MAC_FREE_SHRABL_BUF(pst->region, pst->pool, macStatsReq, sizeof(MacStatsReq));
1286    return ret;
1287 }
1288
1289 /**
1290  * @brief Mac process the statistics rsp received from sch.
1291  *
1292  * @details
1293  *
1294  *     Function : MacProcSchStatsRsp
1295  *
1296  *     This function  process the statistics response received from sch
1297  *
1298  *  @param[in]  Pst           *pst
1299  *  @param[in]  SchStatsRsp *schStatsRsp
1300  *  @return  int
1301  *      -# ROK
1302  **/
1303 uint8_t MacProcSchStatsRsp(Pst *pst, SchStatsRsp *schStatsRsp)
1304 {
1305    uint8_t idx = 0, accptdIdx = 0, rjctdIdx = 0;
1306    uint8_t ret = RFAILED;
1307    MacStatsRsp *macStatsRsp = NULLP;
1308
1309    if(schStatsRsp)
1310    {
1311       /* Fetch pointer to statistics response from pending list saved at MAC 
1312        * during processing statistics request from DU APP */ 
1313       for(idx = 0; idx < macCb.statistics.numPendingStatsRsp; idx++)
1314       {
1315          if(macCb.statistics.pendingStatsRsp[idx].subscriptionId == schStatsRsp->subscriptionId)
1316          {
1317             macStatsRsp = &macCb.statistics.pendingStatsRsp[idx];
1318             break;
1319          }
1320       }
1321
1322       if(macStatsRsp == NULLP)
1323       {
1324          MAC_FREE(schStatsRsp, sizeof(SchStatsRsp));
1325          return RFAILED;
1326       }
1327       
1328       /* Copy Stats-group-accpeted list received from SCH */
1329       for(accptdIdx = 0; accptdIdx<schStatsRsp->numGrpAccepted && macStatsRsp->numGrpAccepted<MAX_NUM_STATS_GRP; accptdIdx++)
1330       {
1331          macStatsRsp->statsGrpAcceptedList[macStatsRsp->numGrpAccepted++] = schStatsRsp->statsGrpAcceptedList[accptdIdx];
1332       }
1333
1334       /* List together all stats group rejected by MAC and by SCH */
1335       for(rjctdIdx = 0; rjctdIdx < schStatsRsp->numGrpRejected && macStatsRsp->numGrpRejected<MAX_NUM_STATS_GRP; rjctdIdx++)
1336       {
1337          macStatsRsp->statsGrpRejectedList[macStatsRsp->numGrpRejected].groupId = \
1338             schStatsRsp->statsGrpRejectedList[rjctdIdx].groupId;
1339          macStatsRsp->statsGrpRejectedList[macStatsRsp->numGrpRejected].cause = \
1340             schStatsRsp->statsGrpRejectedList[rjctdIdx].cause;
1341          macStatsRsp->numGrpRejected++;
1342       }
1343       
1344       /* Send statistics response to DU APP */
1345       ret = MacSendStatsRspToDuApp(macStatsRsp);
1346    }
1347    MAC_FREE(schStatsRsp, sizeof(SchStatsRsp));
1348    return ret;
1349 }
1350
1351 /**
1352  * @brief Fill and send statistics delete response to DU APP
1353  *
1354  * @details
1355  *
1356  *     Function : MacSendStatsDeleteRspToDuApp
1357  *
1358  *     Fill and send statistics delete response to DU APP
1359  *  [Step-1] In SCH delete response is received, fill MAC stats delete 
1360  *    response with info from SCH stats delete response.
1361  *  [Step-2] If a failure occurs while processing MAC stats delete request, 
1362  *    then fill MAC stats delete rsp with failure using info from MAC stats delete request.
1363  *  [Step-3] Else if numStatsGroup > 0, then send stats delete response 
1364  *    with deletion status of each stats group.
1365  *  [Step-4] If numStatsGroup = 0, send Stats delete response 
1366  *    for deletion of complete subscription.
1367  *
1368  *  @param[in]  SchStatsDeleteRsp
1369  *  @param[in]  MacStatsDeleteReq
1370  *  @return  int
1371  *      -# ROK
1372  **/
1373 uint8_t MacSendStatsDeleteRspToDuApp(SchStatsDeleteRsp *schStatsDeleteRsp,  MacStatsDeleteReq *macStatsDeleteReq)
1374 {
1375    Pst  pst;
1376    uint8_t ret = ROK, idx=0;
1377    MacStatsDeleteRsp *macStatsDeleteRsp=NULLP;
1378
1379    MAC_ALLOC_SHRABL_BUF(macStatsDeleteRsp, sizeof(MacStatsDeleteRsp));
1380    if(macStatsDeleteRsp == NULLP)
1381    {
1382       DU_LOG("\nERROR  -->  MAC : Failed to allocate memory in MacProcSchStatsDeleteRsp");
1383       ret = RFAILED;
1384    }
1385    
1386    if(schStatsDeleteRsp)
1387    {
1388       /* [Step-1] */ 
1389       macStatsDeleteRsp->subscriptionId = schStatsDeleteRsp->subscriptionId;
1390       macStatsDeleteRsp->numStatsGroupDeleted = schStatsDeleteRsp->numStatsGroupDeleted;
1391
1392       if(macStatsDeleteRsp->numStatsGroupDeleted)
1393       {
1394          /* [Step-3] */ 
1395          for(idx =0;idx<macStatsDeleteRsp->numStatsGroupDeleted; idx++)
1396          {
1397             if(schStatsDeleteRsp->statsGrpDelInfo[idx].statsGrpDelRsp == RSP_OK)
1398             {
1399                macStatsDeleteRsp->statsGrpDelInfo[idx].statsGrpDelRsp =MAC_DU_APP_RSP_OK;
1400                macStatsDeleteRsp->statsGrpDelInfo[idx].statsGrpDelCause =schStatsDeleteRsp->statsGrpDelInfo[idx].statsGrpDelCause;
1401             }
1402             else
1403             {
1404                macStatsDeleteRsp->statsGrpDelInfo[idx].statsGrpDelRsp =MAC_DU_APP_RSP_NOK;
1405                macStatsDeleteRsp->statsGrpDelInfo[idx].statsGrpDelCause =schStatsDeleteRsp->statsGrpDelInfo[idx].statsGrpDelCause;
1406             }
1407          }
1408       }
1409       else
1410       {
1411          /* [Step-4] */ 
1412          if(schStatsDeleteRsp->subsDelRsp == RSP_OK)
1413             macStatsDeleteRsp->subsDelRsp = MAC_DU_APP_RSP_OK;
1414          else
1415             macStatsDeleteRsp->subsDelRsp = MAC_DU_APP_RSP_NOK;
1416          macStatsDeleteRsp->subsDelCause = schStatsDeleteRsp->subsDelCause;
1417       }
1418    }
1419    else
1420    {
1421       /* [Step-2] */ 
1422       macStatsDeleteRsp->subscriptionId = macStatsDeleteReq->subscriptionId;
1423       macStatsDeleteRsp->numStatsGroupDeleted= macStatsDeleteReq->numStatsGroupToBeDeleted;
1424       
1425       if(macStatsDeleteRsp->numStatsGroupDeleted)
1426       {
1427          /* [Step-3] */ 
1428          for(idx =0;idx<macStatsDeleteRsp->numStatsGroupDeleted; idx++)
1429          {
1430             macStatsDeleteRsp->statsGrpDelInfo[idx].statsGrpDelRsp =MAC_DU_APP_RSP_NOK;
1431             macStatsDeleteRsp->statsGrpDelInfo[idx].statsGrpDelCause = RESOURCE_UNAVAILABLE;
1432          }
1433       }
1434       else
1435       {
1436          /* [Step-4] */ 
1437          macStatsDeleteRsp->subsDelRsp = MAC_DU_APP_RSP_NOK;
1438          macStatsDeleteRsp->subsDelCause = RESOURCE_UNAVAILABLE;
1439       }
1440    }
1441
1442    DU_LOG("\nINFO  -->  MAC : MacSendStatsDeleteRspToDuApp: Sending Delete Statistics Response to DU APP");
1443
1444    memset(&pst, 0, sizeof(Pst));
1445    FILL_PST_MAC_TO_DUAPP(pst, EVENT_MAC_STATS_DELETE_RSP);
1446    if(((*macDuStatsDeleteRspOpts[pst.selector])(&pst, macStatsDeleteRsp))!= ROK)
1447    {
1448       DU_LOG("\nERROR  -->  MAC : Failed to send statistics delete response to DU APP");
1449       MAC_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, macStatsDeleteRsp, sizeof(MacStatsDeleteRsp));
1450       ret = RFAILED;
1451    }
1452
1453    return ret;
1454 }
1455                  
1456 /**
1457  * @brief Mac process the statistics delete rsp received from sch.
1458  *
1459  * @details
1460  *
1461  *     Function : MacProcSchStatsDeleteRsp
1462  *
1463  *     This function  process the statistics delete response received from sch
1464  *
1465  *  @param[in]  Pst           *pst
1466  *  @param[in]  SchStatsDeleteRsp *schStatsDeleteRsp
1467  *  @return  int
1468  *      -# ROK
1469  **/
1470 uint8_t MacProcSchStatsDeleteRsp(Pst *pst, SchStatsDeleteRsp *schStatsDeleteRsp)
1471 {
1472    uint8_t ret = RFAILED;
1473
1474    if(schStatsDeleteRsp)
1475    {
1476       ret = MacSendStatsDeleteRspToDuApp(schStatsDeleteRsp, NULLP);
1477       MAC_FREE(schStatsDeleteRsp, sizeof(SchStatsDeleteRsp));
1478    }
1479    return ret;
1480 }
1481
1482 /**
1483  * @brief Mac process the statistics delete Req received from DUAPP
1484  *
1485  * @details
1486  *
1487  *     Function : MacProcStatsDeleteReq
1488  *
1489  *     Functionality: Process the statistics delete request from duapp
1490  *  @param[in]  Pst      *pst
1491  *  @param[in]  StatsDeleteReq *statsReq
1492  *  @return  int
1493  *      -# ROK
1494  **/
1495
1496 uint8_t MacProcStatsDeleteReq(Pst *pst, MacStatsDeleteReq *macStatsDeleteReq)
1497 {
1498    Pst       schPst;
1499    uint8_t   ret = RFAILED;
1500    SchStatsDeleteReq  *schStatsDeleteReq = NULLP;
1501  
1502    DU_LOG("\nINFO   -->  MAC : Received Statistics delete Request from DU_APP");
1503
1504    if(macStatsDeleteReq == NULLP)
1505    {
1506       DU_LOG("\nERROR  -->  MAC : MacProcStatsDeleteReq(): Received Null pointer");
1507       return RFAILED;
1508    }
1509    
1510    MAC_ALLOC(schStatsDeleteReq, sizeof(SchStatsDeleteReq));
1511    if(schStatsDeleteReq == NULLP)
1512    {
1513       DU_LOG("\nERROR  -->  MAC : MacProcStatsDeleteReq: Failed to allocate memory");
1514    }
1515    else
1516    {
1517       /* fill all the information in schStatsDeleteReq structure */
1518       schStatsDeleteReq->subscriptionId = macStatsDeleteReq->subscriptionId;
1519       schStatsDeleteReq->numStatsGroupToBeDeleted = macStatsDeleteReq->numStatsGroupToBeDeleted;
1520       memcpy(&schStatsDeleteReq->statsGrpIdToBeDelList, &macStatsDeleteReq->statsGrpIdToBeDelList, schStatsDeleteReq->numStatsGroupToBeDeleted*sizeof(uint8_t)); 
1521       FILL_PST_MAC_TO_SCH(schPst, EVENT_STATISTICS_DELETE_REQ_TO_SCH);
1522       ret = SchMessageRouter(&schPst, (void *)schStatsDeleteReq);
1523    }
1524
1525    if(ret != ROK)
1526    {
1527       ret = MacSendStatsDeleteRspToDuApp(NULLP, macStatsDeleteReq);
1528    }
1529
1530    MAC_FREE_SHRABL_BUF(pst->region, pst->pool, macStatsDeleteReq, sizeof(MacStatsDeleteReq));
1531    return ret;
1532 }
1533
1534 /**
1535  * @brief Fill and send statistics modification response to DU APP
1536  *
1537  * @details
1538  *
1539  *     Function : MacSendStatsModificationRspToDuApp 
1540  *
1541  *     Fill and send statistics modification response to DU APP
1542  *
1543  *  @param[in]  Stats modification Response
1544  *  @return  int
1545  *      -# ROK
1546  **/
1547 uint8_t MacSendStatsModificationRspToDuApp(MacStatsModificationRsp *tmpMacStatsModRsp)
1548 {
1549    Pst  pst;
1550    uint8_t ret = ROK;
1551    MacStatsModificationRsp *macStatsModificationRsp = NULLP;
1552
1553    DU_LOG("\nINFO  -->  MAC : MacSendStatsModificationRspToDuApp: Sending Statistics Modification Response to DU APP");
1554
1555
1556    MAC_ALLOC_SHRABL_BUF(macStatsModificationRsp, sizeof(MacStatsModificationRsp));
1557    if(macStatsModificationRsp == NULLP)
1558    {
1559       DU_LOG("\nERROR  -->  MAC : Failed to allocate memory in MacProcSchStatsModificationRsp");
1560       ret = RFAILED;
1561    }
1562    else
1563    {
1564       memcpy(macStatsModificationRsp, tmpMacStatsModRsp, sizeof(MacStatsModificationRsp));
1565       memset(tmpMacStatsModRsp, 0, sizeof(MacStatsModificationRsp));
1566
1567       memset(&pst, 0, sizeof(Pst));
1568       FILL_PST_MAC_TO_DUAPP(pst, EVENT_MAC_STATISTICS_MODIFY_RSP);
1569       if(((*macDuStatsModificationRspOpts[pst.selector])(&pst, macStatsModificationRsp))!= ROK)
1570       {
1571          DU_LOG("\nERROR  -->  MAC : Failed to send statistics modification response to DU APP");
1572          MAC_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, macStatsModificationRsp, sizeof(MacStatsModificationRsp));
1573          ret = RFAILED;
1574       }
1575    }
1576
1577    return ret;
1578 }
1579
1580 /**
1581  * @brief Mac process the statistics modification rsp received from sch.
1582  *
1583  * @details
1584  *
1585  *     Function : MacProcSchStatsModificationRsp
1586  *
1587  *     This function  process the statistics modification response received from sch
1588  *     [Step -1] Fetch pointer to statistics response from pending list saved at
1589  *     MAC during processing statistics request from DU APP
1590  *     [Step -2] Fill the list of accepted list
1591  *     [Step -3] Fill the list of rejected list
1592  *     [Step -4] Send statistics modification response to DU APP
1593  *
1594  *  @param[in]  Pst           *pst
1595  *  @param[in]  SchStatsModificationRsp *schStatsModificationRsp
1596  *  @return  int
1597  *      -# ROK
1598  **/
1599 uint8_t MacProcSchStatsModificationRsp(Pst *pst, SchStatsModificationRsp *schStatsModificationRsp)
1600 {
1601    uint8_t ret = RFAILED;
1602    uint8_t idx = 0, accptdIdx = 0, rjctdIdx = 0;
1603    MacStatsModificationRsp *macStatsModificationRsp = NULLP;
1604
1605    if(schStatsModificationRsp)
1606    {
1607       /* [Step -1] */
1608       for(idx = 0; idx < macCb.statistics.numPendingStatsRsp; idx++)
1609       {
1610          if(macCb.statistics.pendingStatsRsp[idx].subscriptionId == schStatsModificationRsp->subscriptionId)
1611          {
1612             macStatsModificationRsp = &macCb.statistics.pendingStatsRsp[idx];
1613             break;
1614          }
1615       }
1616
1617       if(macStatsModificationRsp == NULLP)
1618       {
1619          MAC_FREE(schStatsModificationRsp, sizeof(SchStatsModificationRsp));
1620          return RFAILED;
1621       }
1622       
1623       /* [Step -2] */
1624       for(accptdIdx = 0; accptdIdx<schStatsModificationRsp->numGrpAccepted && macStatsModificationRsp->numGrpAccepted<MAX_NUM_STATS_GRP; accptdIdx++)
1625       {
1626          macStatsModificationRsp->statsGrpAcceptedList[macStatsModificationRsp->numGrpAccepted++] = schStatsModificationRsp->statsGrpAcceptedList[accptdIdx];
1627       }
1628
1629       /* [Step -3] */
1630       for(rjctdIdx = 0; rjctdIdx < schStatsModificationRsp->numGrpRejected && macStatsModificationRsp->numGrpRejected<MAX_NUM_STATS_GRP; rjctdIdx++)
1631       {
1632          macStatsModificationRsp->statsGrpRejectedList[macStatsModificationRsp->numGrpRejected].groupId = \
1633             schStatsModificationRsp->statsGrpRejectedList[rjctdIdx].groupId;
1634          macStatsModificationRsp->statsGrpRejectedList[macStatsModificationRsp->numGrpRejected].cause = \
1635             schStatsModificationRsp->statsGrpRejectedList[rjctdIdx].cause;
1636          macStatsModificationRsp->numGrpRejected++;
1637       }
1638
1639       /* [Step -4] */
1640       ret = MacSendStatsModificationRspToDuApp(macStatsModificationRsp);
1641    }
1642    MAC_FREE(schStatsModificationRsp, sizeof(SchStatsModificationRsp));
1643    return ret;
1644 }
1645
1646 /*******************************************************************
1647  *
1648  * @brief Rejects all statistics modification group requested by DU APP
1649  *
1650  * @details
1651  *
1652  *    Function : MacRejectAllStatsModification
1653  *
1654  *    Functionality: Add all statistics modification group received in statistics
1655  *       request from DU APP, to Reject-StatsModification-Group-List in statistics
1656  *       response to DU APP
1657  *
1658  * @params[in]  Statistics request from DU APP
1659  *              Cause of rejection
1660  * @return ROK     - success
1661  *         RFAILED - failure
1662  *
1663  * ****************************************************************/
1664 uint8_t MacRejectAllStatsModification(MacStatsModificationReq *macStatsModificationReq, CauseOfResult cause)
1665 {
1666    uint8_t grpIdx = 0;
1667    MacStatsModificationRsp macStatsModificationRsp;
1668
1669    memset(&macStatsModificationRsp, 0, sizeof(MacStatsModificationRsp));
1670
1671    /* fill the subscriptionId and the rejected list in stats modification rsp */
1672    macStatsModificationRsp.subscriptionId = macStatsModificationReq->subscriptionId;
1673    for(grpIdx = 0; grpIdx < macStatsModificationReq->numStatsGroup; grpIdx++)
1674    {
1675       macStatsModificationRsp.statsGrpRejectedList[grpIdx].groupId = macStatsModificationReq->statsGrpList[grpIdx].groupId;
1676       macStatsModificationRsp.statsGrpRejectedList[grpIdx].cause = cause;
1677    }
1678    macStatsModificationRsp.numGrpRejected = macStatsModificationReq->numStatsGroup;
1679
1680    return MacSendStatsModificationRspToDuApp(&macStatsModificationRsp);
1681 }
1682
1683 /**
1684  * @brief Mac process the statistics Modification Req received from DUAPP
1685  *
1686  * @details
1687  *
1688  *     Function : MacProcStatsModificationReq
1689  *
1690  *     This function process the statistics Modification request from duapp:
1691  *     [Step 1] Basic validation. If fails, all stats group in stats request are
1692  *     rejected.
1693  *     [Step 2] If basic validations passed, traverse all stats group and
1694  *     validate each measurement types in each group.
1695  *     [Step 3] If any measurement type validation fails in a group, that group
1696  *     is not configured and it is added to stats-group-rejected-list in
1697  *     mac-stats-response message.
1698  *     [Step 4] Even if one group passes all validation, it is sent to SCH in
1699  *     statistics request. The mac-stats-response message is added to
1700  *     pending-response list. This will be sent to DU APP after stats response
1701  *     is received from SCH.
1702  *     [Step 5] If none of the groups passes all validation, mac-stats-response
1703  *     is sent to du app with all group as part of stats-group-rejected-list.
1704  *
1705  *  @param[in]  Pst      *pst
1706  *  @param[in]  StatsModificationReq *statsModificationReq
1707  *  @return  int
1708  *      -# ROK
1709  **/
1710 uint8_t MacProcStatsModificationReq(Pst *pst, MacStatsModificationReq *macStatsModificationReq)
1711 {
1712    Pst           schPst;
1713    uint8_t       ret = RFAILED;
1714    bool          measTypeInvalid = false;
1715    uint8_t       macStatsGrpIdx = 0, macStatsIdx = 0;
1716    uint8_t       schStatsGrpIdx = 0, schStatsIdx = 0;
1717    MacStatsGrpInfo          *macStatsGrp = NULLP;
1718    SchStatsModificationReq  *schStatsModificationReq = NULLP;
1719    MacStatsModificationRsp  *macStatsModificationRsp = NULLP;
1720
1721    DU_LOG("\nINFO   -->  MAC : Received Statistics Modification Request from DU_APP");
1722
1723    if(macStatsModificationReq == NULLP)
1724    {
1725       DU_LOG("\nERROR  -->  MAC : MacProcStatsModificationReq(): Received Null pointer");
1726       return RFAILED;
1727    }
1728
1729    /* [Step -1] */
1730    if(macCb.statistics.numPendingStatsRsp >= MAX_PENDING_STATS_RSP)
1731    {
1732       DU_LOG("\nERROR  -->  MAC : MacProcStatsModificationReq: Maximum number of statistics response is pending. \
1733          Cannot process new request.");
1734       MacRejectAllStatsModification(macStatsModificationReq, RESOURCE_UNAVAILABLE);
1735       MAC_FREE_SHRABL_BUF(pst->region, pst->pool, macStatsModificationReq, sizeof(MacStatsModificationReq));
1736       return RFAILED;
1737    }
1738
1739    MAC_ALLOC(schStatsModificationReq, sizeof(SchStatsModificationReq));
1740    if(schStatsModificationReq == NULLP)
1741    {
1742       DU_LOG("\nERROR  -->  MAC : MacProcStatsModificationReq: Failed to allocate memory");
1743       MacRejectAllStatsModification(macStatsModificationReq, RESOURCE_UNAVAILABLE);
1744       MAC_FREE_SHRABL_BUF(pst->region, pst->pool, macStatsModificationReq, sizeof(MacStatsModificationReq));
1745       return RFAILED;
1746    }
1747
1748    macStatsModificationRsp = &macCb.statistics.pendingStatsRsp[macCb.statistics.numPendingStatsRsp];
1749    memset(macStatsModificationRsp, 0, sizeof(MacStatsModificationRsp));
1750
1751    /* [Step 2]  */
1752    schStatsModificationReq->subscriptionId = macStatsModificationReq->subscriptionId;
1753    schStatsModificationReq->numStatsGroup = 0;
1754    for(macStatsGrpIdx = 0; macStatsGrpIdx < macStatsModificationReq->numStatsGroup; macStatsGrpIdx++)
1755    {
1756       measTypeInvalid = false;
1757       schStatsIdx = 0;
1758       macStatsGrp = &macStatsModificationReq->statsGrpList[macStatsGrpIdx];
1759
1760       for(macStatsIdx=0; macStatsIdx < macStatsGrp->numStats; macStatsIdx++)
1761       {
1762          switch(macStatsGrp->statsList[macStatsIdx])
1763          {
1764             case MAC_DL_TOTAL_PRB_USAGE:
1765                {
1766                   schStatsModificationReq->statsGrpList[schStatsGrpIdx].statsList[schStatsIdx] = SCH_DL_TOTAL_PRB_USAGE;
1767                   break;
1768                }
1769             case MAC_UL_TOTAL_PRB_USAGE:
1770                {
1771                   schStatsModificationReq->statsGrpList[schStatsGrpIdx].statsList[schStatsIdx] = SCH_UL_TOTAL_PRB_USAGE;
1772                   break;
1773                }
1774             default:
1775                {
1776                   DU_LOG("\nERROR  -->  MAC : MacProcStatsModificationReq: Invalid measurement type [%d]", \
1777                      macStatsGrp->statsList[macStatsIdx]);
1778                   measTypeInvalid = true;
1779                }
1780          }
1781
1782          if(measTypeInvalid)
1783          {
1784             memset(&schStatsModificationReq->statsGrpList[schStatsGrpIdx], 0, sizeof(SchStatsGrpInfo));
1785             break;
1786          }
1787
1788          schStatsIdx++;
1789       }
1790
1791       if(!measTypeInvalid)
1792       {
1793          schStatsModificationReq->statsGrpList[schStatsGrpIdx].groupId = macStatsGrp->groupId;
1794          schStatsModificationReq->statsGrpList[schStatsGrpIdx].periodicity = macStatsGrp->periodicity;
1795          schStatsModificationReq->statsGrpList[schStatsGrpIdx].numStats = schStatsIdx;
1796          schStatsGrpIdx++;
1797       }
1798       else
1799       {
1800          /* [Step 3] */
1801          macStatsModificationRsp->statsGrpRejectedList[macStatsModificationRsp->numGrpRejected].groupId = macStatsGrp->groupId;
1802          macStatsModificationRsp->statsGrpRejectedList[macStatsModificationRsp->numGrpRejected].cause = PARAM_INVALID;
1803          macStatsModificationRsp->numGrpRejected++;
1804       }
1805    }
1806    schStatsModificationReq->numStatsGroup = schStatsGrpIdx;
1807
1808    macStatsModificationRsp->subscriptionId = macStatsModificationReq->subscriptionId;
1809    if(schStatsModificationReq->numStatsGroup)
1810    {
1811       /* [Step 4] */
1812       macCb.statistics.numPendingStatsRsp++;
1813
1814       FILL_PST_MAC_TO_SCH(schPst, EVENT_STATISTICS_MODIFY_REQ_TO_SCH);
1815       ret = SchMessageRouter(&schPst, (void *)schStatsModificationReq);
1816    }
1817    else
1818    {
1819       /* [Step 5] */
1820       DU_LOG("\nERROR  -->  MAC : MacProcStatsModificationReq: All statistics group found invalid");
1821       MAC_FREE(schStatsModificationReq, sizeof(SchStatsModificationReq));
1822       ret = MacSendStatsModificationRspToDuApp(macStatsModificationRsp);
1823    }
1824
1825    MAC_FREE_SHRABL_BUF(pst->region, pst->pool, macStatsModificationReq, sizeof(MacStatsModificationReq));
1826    return ret;
1827 }
1828
1829 /**********************************************************************
1830   End of file
1831  **********************************************************************/