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