Ue Config Changes aligned with AAD
[o-du/l2.git] / src / 5gnrmac / mac_ue_mgr.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
19 /* This file contains UE management handling functionality for MAC */
20
21 /* header include files (.h) */
22 #include "common_def.h"
23 #include "lrg.h"
24 #include "lrg.x"
25 #include "du_app_mac_inf.h"
26 #include "mac_sch_interface.h"
27 #include "lwr_mac_upr_inf.h"
28 #include "mac.h"
29 #include "mac_utils.h"
30
31 /* function pointers for packing slot ind from mac to sch */
32 MacSchAddUeConfigReqFunc macSchAddUeConfigReqOpts[] =
33 {
34    packMacSchAddUeConfigReq,    /* packing for loosely coupled */
35    MacSchAddUeConfigReq,        /* packing for tightly coupled */
36    packMacSchAddUeConfigReq     /* packing for light weight loosely coupled */
37 };
38
39 MacDuUeCfgRspFunc macDuUeCfgRspOpts[] =
40 {
41    packDuMacUeCfgRsp,   /* packing for loosely coupled */
42    DuProcMacUeCfgRsp,   /* packing for tightly coupled */
43    packDuMacUeCfgRsp   /* packing for light weight loosly coupled */
44 };
45
46 MacSchModUeConfigReqFunc macSchModUeConfigReqOpts[] =
47 {
48    packMacSchModUeConfigReq,    /* packing for loosely coupled */
49    MacSchModUeConfigReq,        /* packing for tightly coupled */
50    packMacSchModUeConfigReq     /* packing for light weight loosely coupled */
51 };
52
53 /*******************************************************************
54  *
55  * @brief Fills mac cell group config to be sent to scheduler
56  *
57  * @details
58  *
59  *    Function : fillMacCellGroupCfg
60  *
61  *    Functionality: Fills mac cell group config to be sent to sch
62  *
63  * @params[in] macCellGrp : mac cell group config at MAC
64  *             *macCellGrpCfg : mac cell group cfg to be filled
65  * @return ROK     - success
66  *         RFAILED - failure
67  *
68  * ****************************************************************/
69 uint8_t fillMacCellGroupCfg(MacCellGrpCfg macCellGrp, SchMacCellGrpCfg  *macCellGrpCfg)
70 {
71    uint8_t           idx;
72    SchSchedReqCfg    *schedReqCfg;
73    SchTagCfg         *tagCfg;
74
75    /* Copy scheduling request config */
76    schedReqCfg = &macCellGrpCfg->schedReqCfg;
77    schedReqCfg->addModListCount = macCellGrp.schReqCfg.addModListCount;
78    if(schedReqCfg->addModListCount > MAX_NUM_SR_CFG_PER_CELL_GRP)
79    {
80       DU_LOG("\nMAC : Scheduling Request Add/Mod Count %d exceeds max limit %d", \
81             schedReqCfg->addModListCount, MAX_NUM_SR_CFG_PER_CELL_GRP);
82       return RFAILED;
83    }
84    for(idx = 0; idx < schedReqCfg->addModListCount; idx++)
85    {
86       schedReqCfg->addModList[idx].schedReqId = \
87          macCellGrp.schReqCfg.addModList[idx].schedReqId;
88       schedReqCfg->addModList[idx].srProhibitTmr = \
89          macCellGrp.schReqCfg.addModList[idx].srProhibitTmr;
90       schedReqCfg->addModList[idx].srTransMax = \
91          macCellGrp.schReqCfg.addModList[idx].srTransMax;
92    }
93    schedReqCfg->relListCount = macCellGrp.schReqCfg.relListCount;
94
95    /* copy TAG config */
96    tagCfg = &macCellGrpCfg->tagCfg;
97    tagCfg->addModListCount = macCellGrp.tagCfg.addModListCount;
98    if(tagCfg->addModListCount > MAX_NUM_TAGS)
99    {
100       DU_LOG("\nMAC : Scheduling Request Add/Mod Count %d exceeds max limit %d", \
101             tagCfg->addModListCount, MAX_NUM_TAGS);
102       return RFAILED;
103    }
104    for(idx = 0; idx < tagCfg->addModListCount; idx++)
105    {
106       tagCfg->addModList[idx].tagId = \
107          macCellGrp.tagCfg.addModList[idx].tagId;
108       tagCfg->addModList[idx].timeAlignmentTmr = \
109          macCellGrp.tagCfg.addModList[idx].timeAlignTimer;
110    }
111    tagCfg->relListCount = macCellGrp.tagCfg.relListCount;
112
113    /* Copy PHR config */
114    if(macCellGrp.phrCfgSetupPres)
115    {
116       macCellGrpCfg->phrCfg.periodicTmr = macCellGrp.phrCfg.periodicTimer;
117       macCellGrpCfg->phrCfg.prohibitTmr =  macCellGrp.phrCfg.prohibitTimer;
118       macCellGrpCfg->phrCfg.txpowerFactorChange = macCellGrp.phrCfg.txPowerFactor;
119       macCellGrpCfg->phrCfg.multiplePhr = macCellGrp.phrCfg.multiplePHR;
120       macCellGrpCfg->phrCfg.dummy = macCellGrp.phrCfg.dummy;
121       macCellGrpCfg->phrCfg.type2OtherCell = macCellGrp.phrCfg.phrType2OtherCell;
122       macCellGrpCfg->phrCfg.modeOtherCG = macCellGrp.phrCfg.phrOtherCG;
123    }
124
125    return ROK;
126 }
127
128 /*******************************************************************
129  *
130  * @brief Fills phy cell group config to be sent to scheduler
131  *
132  * @details
133  *
134  *    Function : fillPhyCellGroupCfg
135  *
136  *    Functionality: Fills phy cell group config to be sent to sch
137  *
138  * @params[in] macUeCfg : Phy cell group config at MAC
139  *             *schPhyCellGrp : phy cell group config to be filled
140  * @return ROK     - success
141  *         RFAILED - failure
142  *
143  * ****************************************************************/
144 uint8_t fillPhyCellGroupCfg(PhyCellGrpCfg macUeCfg, SchPhyCellGrpCfg *schPhyCellGrp)
145 {
146    schPhyCellGrp->pdschHarqAckCodebook = macUeCfg.pdschHarqAckCodebook;
147    schPhyCellGrp->pNrFr1 = macUeCfg.pNrFr1;
148
149    return ROK;
150 }
151
152 /*******************************************************************
153  *
154  * @brief Fills PDSCh serving cell config to send to scheduler
155  *
156  * @details
157  *
158  *    Function : fillPdschServCellCfg 
159  *
160  *    Functionality: Fills PDSCh serving cell config to send to scheduler
161  *
162  * @params[in] macPdschCfg : Pdsch serving cell config at MAC
163  *             *schPdschCfg : Pdsch serving cell config to be filled
164  * @return ROK     - success
165  *         RFAILED - failure
166  *
167  * ****************************************************************/
168 uint8_t fillPdschServCellCfg(PdschServCellCfg macPdschCfg, SchPdschServCellCfg *schPdschCfg) 
169 {
170    if(macPdschCfg.maxMimoLayers)
171    {
172       if(!schPdschCfg->maxMimoLayers)
173       {
174          MAC_ALLOC_SHRABL_BUF(schPdschCfg->maxMimoLayers, sizeof(uint8_t));
175          if(!schPdschCfg->maxMimoLayers)
176          {
177             DU_LOG("\nMAC :Memory Alloc MimoLayers Failed at fillPdschServCellCfg()");
178             return RFAILED;
179          }
180       }
181       *schPdschCfg->maxMimoLayers = *macPdschCfg.maxMimoLayers;
182    }
183    else
184    {
185       schPdschCfg->maxMimoLayers = NULLP;
186    }
187
188    schPdschCfg->numHarqProcForPdsch = \
189       macPdschCfg.numHarqProcForPdsch;
190
191    if(macPdschCfg.maxCodeBlkGrpPerTb)
192    {
193       if(!schPdschCfg->maxCodeBlkGrpPerTb)
194       {
195          MAC_ALLOC_SHRABL_BUF(schPdschCfg->maxCodeBlkGrpPerTb, sizeof(SchMaxCodeBlkGrpPerTB));
196          if(!schPdschCfg->maxCodeBlkGrpPerTb)
197          {
198             DU_LOG("\nMAC :Memory Alloc for code Block Failed at fillPdschServCellCfg()");
199             return RFAILED;
200          }
201       }
202       *schPdschCfg->maxCodeBlkGrpPerTb = *macPdschCfg.maxCodeBlkGrpPerTb;
203    }
204    else
205    {
206       schPdschCfg->maxCodeBlkGrpPerTb = NULLP;
207    }
208
209    if(macPdschCfg.codeBlkGrpFlushInd)
210    {
211       if(!schPdschCfg->codeBlkGrpFlushInd)
212       {
213          MAC_ALLOC_SHRABL_BUF(schPdschCfg->codeBlkGrpFlushInd, sizeof(bool));
214          if(!schPdschCfg->codeBlkGrpFlushInd)
215          {
216             DU_LOG("\nMAC :Memory Alloc for Flush Ind Failed at fillPdschServCellCfg()");
217             return RFAILED;
218          }
219       }
220       *schPdschCfg->codeBlkGrpFlushInd = *macPdschCfg.codeBlkGrpFlushInd;
221    }
222    else
223    {
224       schPdschCfg->codeBlkGrpFlushInd = NULLP;
225    }
226
227    if(macPdschCfg.xOverhead)
228    {
229       if(!schPdschCfg->xOverhead)
230       {
231          MAC_ALLOC_SHRABL_BUF(schPdschCfg->xOverhead, sizeof(SchPdschXOverhead));
232          if(!schPdschCfg->xOverhead)
233          {
234             DU_LOG("\nMAC :Memory Alloc for xOverHead Failed at fillPdschServCellCfg()");
235             return RFAILED;
236          }
237       }
238       *schPdschCfg->xOverhead = *macPdschCfg.xOverhead;
239    }
240    else
241    {
242       schPdschCfg->xOverhead = NULLP;
243    }
244
245    return ROK;
246 }
247
248 /*******************************************************************
249  *
250  * @brief Fills PUSCH cofig in initial UL BWP config for SCH UE Cfg
251  *
252  * @details
253  *
254  *    Function : fillInitalUlBwpPuschCfg
255  *
256  *    Functionality:
257  *       Fills PUSCH cofig in initial UL BWP config for SCH UE Cfg
258  *
259  * @params[in] macPuschCfg : Initial UL-BWP PUSCH cfg at MAC
260  *             schPuschCfg : Initial UL-BWP PUSCH cfg to be filled
261  * @return ROK     - success
262  *         RFAILED - failure
263  *
264  * ****************************************************************/
265 uint8_t fillInitalUlBwpPuschCfg(PuschCfg macPuschCfg, SchPuschCfg *schPuschCfg)
266 {
267    uint8_t   idx;
268
269    schPuschCfg->dataScramblingId = macPuschCfg.dataScramblingId;
270    schPuschCfg->dmrsUlCfgForPuschMapTypeA.addPos = \
271       macPuschCfg.dmrsUlCfgForPuschMapTypeA.addPos;
272    schPuschCfg->dmrsUlCfgForPuschMapTypeA.transPrecodDisabled.scramblingId0 = \
273       macPuschCfg.dmrsUlCfgForPuschMapTypeA.transPrecodDisabled.scramblingId0;
274    schPuschCfg->resourceAllocType = macPuschCfg.resourceAllocType;
275
276    schPuschCfg->numTimeDomRsrcAlloc = macPuschCfg.numTimeDomRsrcAlloc;
277    if(schPuschCfg->numTimeDomRsrcAlloc > MAX_NUM_UL_ALLOC)
278    {
279       DU_LOG("\nMAC : Number of Time domain resource allocation [%d] exceeds max limit [%d]",\
280             schPuschCfg->numTimeDomRsrcAlloc, MAX_NUM_UL_ALLOC);
281       return RFAILED;
282    }    
283    for(idx = 0; idx < schPuschCfg->numTimeDomRsrcAlloc; idx++)
284    {
285       schPuschCfg->timeDomRsrcAllocList[idx].k2 = \
286          macPuschCfg.timeDomRsrcAllocList[idx].k2;
287       schPuschCfg->timeDomRsrcAllocList[idx].mappingType = \
288          macPuschCfg.timeDomRsrcAllocList[idx].mappingType;
289       schPuschCfg->timeDomRsrcAllocList[idx].startSymbol = \
290          macPuschCfg.timeDomRsrcAllocList[idx].startSymbol; 
291       schPuschCfg->timeDomRsrcAllocList[idx].symbolLength = \
292          macPuschCfg.timeDomRsrcAllocList[idx].symbolLength;
293    }
294
295    schPuschCfg->transformPrecoder = macPuschCfg.transformPrecoder;
296    return ROK;
297 }
298
299 /*******************************************************************
300  *
301  * @brief Fills initail UL BWP config to send to scheduler
302  *
303  * @details
304  *
305  *    Function : fillInitialUlBwp
306  *
307  *    Functionality: Fills initail UL BWP config to send to sch
308  *
309  * @params[in] macInitUlBwp : Initial UL BWP cfg at MAC
310  *             schInitUlBwp : Initial UL BWP cfg to be filled
311  * @return ROK     - success
312  *         RFAILED - failure
313  *
314  * ****************************************************************/
315 uint8_t fillInitialUlBwp(InitialUlBwp macInitUlBwp, SchInitialUlBwp *schInitUlBwp)
316 {
317    schInitUlBwp->pucchCfgPres = macInitUlBwp.pucchPresent;
318    if(schInitUlBwp->pucchCfgPres)
319    {
320       /* TODO : Optional parameter */
321    }
322
323    schInitUlBwp->puschCfgPres = macInitUlBwp.puschPresent;
324    if(schInitUlBwp->puschCfgPres)
325    {
326       if(fillInitalUlBwpPuschCfg(macInitUlBwp.puschCfg, &schInitUlBwp->puschCfg) != ROK)
327       {
328          DU_LOG("\nMAC : fillInitalUlBwpPuschCfg() failed");
329          return RFAILED;
330       }
331    }
332    return ROK;
333 }
334
335 /*******************************************************************
336  *
337  * @brief Fill PDCCH cfg in Initial DL BWP for UE Cfg in Scheduler
338  *
339  * @details
340  *
341  *    Function : fillInitDlBwpPdcchCfg
342  *
343  *    Functionality:
344  *        Fill PDCCH cfg in Initial DL BWP for UE Cfg in Scheduler
345  *
346  * @params[in] macPdcchCfg : Inital DL BWP PDCCH cfg in MAC
347  *             schPdcchCfg : Inital DL BWP PDCCH cfg to be filled
348  * @return ROK     - success
349  *         RFAILED - failure
350  *
351  * ****************************************************************/
352 uint8_t fillInitDlBwpPdcchCfg(PdcchConfig macPdcchCfg, SchPdcchConfig *schPdcchCfg)
353 {
354    uint8_t idx;
355
356    /* Fill CORESET info */
357    schPdcchCfg->numCRsetToAddMod = macPdcchCfg.numCRsetToAddMod;
358    if(schPdcchCfg->numCRsetToAddMod > MAX_NUM_CRSET)
359    {
360       DU_LOG("\nMAC : Number of CORESET to ADD/MOD [%d] exceeds max limit [%d]",\
361             schPdcchCfg->numCRsetToAddMod, MAX_NUM_CRSET);
362       return RFAILED;
363    }
364
365    for(idx = 0; idx < schPdcchCfg->numCRsetToAddMod; idx++)
366    {
367       schPdcchCfg->cRSetToAddModList[idx].cRSetId = \
368          macPdcchCfg.cRSetToAddModList[idx].cRSetId;
369       memcpy(&schPdcchCfg->cRSetToAddModList[idx].freqDomainRsrc,\
370          &macPdcchCfg.cRSetToAddModList[idx].freqDomainRsrc, FREQ_DOM_RSRC_SIZE);
371       schPdcchCfg->cRSetToAddModList[idx].duration = \
372          macPdcchCfg.cRSetToAddModList[idx].duration;
373       schPdcchCfg->cRSetToAddModList[idx].cceRegMappingType = \
374          macPdcchCfg.cRSetToAddModList[idx].cceRegMappingType;
375       schPdcchCfg->cRSetToAddModList[idx].precoderGranularity = \
376          macPdcchCfg.cRSetToAddModList[idx].precoderGranularity;
377       schPdcchCfg->cRSetToAddModList[idx].dmrsScramblingId = \
378          macPdcchCfg.cRSetToAddModList[idx].dmrsScramblingId;
379    }
380
381    schPdcchCfg->numCRsetToRel = macPdcchCfg.numCRsetToRel;
382    if(schPdcchCfg->numCRsetToAddMod > MAX_NUM_CRSET)
383    {
384       DU_LOG("\nMAC : Number of CORESET to release [%d] exceeds max limit [%d]",\
385             schPdcchCfg->numCRsetToRel, MAX_NUM_CRSET);
386       return RFAILED;
387    }
388
389    for(idx = 0; idx < schPdcchCfg->numCRsetToRel; idx++)
390    {
391       /* TODO */
392    }
393
394    /* Fill Search space info */
395    schPdcchCfg->numSearchSpcToAddMod = macPdcchCfg.numSearchSpcToAddMod;
396    if(schPdcchCfg->numSearchSpcToAddMod > MAX_NUM_SEARCH_SPC)
397    {
398       DU_LOG("\nMAC : Number of search space to ADD/MOD [%d] exceeds max [%d]", \
399             schPdcchCfg->numSearchSpcToAddMod, MAX_NUM_SEARCH_SPC);
400       return RFAILED;
401    }
402    for(idx = 0; idx < schPdcchCfg->numSearchSpcToAddMod; idx++)
403    {
404       schPdcchCfg->searchSpcToAddModList[idx].searchSpaceId = \
405          macPdcchCfg.searchSpcToAddModList[idx].searchSpaceId;
406       schPdcchCfg->searchSpcToAddModList[idx].cRSetId = \
407          macPdcchCfg.searchSpcToAddModList[idx].cRSetId;
408       schPdcchCfg->searchSpcToAddModList[idx].mSlotPeriodicityAndOffset = \
409          macPdcchCfg.searchSpcToAddModList[idx].mSlotPeriodicityAndOffset;
410       memcpy(&schPdcchCfg->searchSpcToAddModList[idx].mSymbolsWithinSlot,
411          &macPdcchCfg.searchSpcToAddModList[idx].mSymbolsWithinSlot, \
412          MONITORING_SYMB_WITHIN_SLOT_SIZE);
413       schPdcchCfg->searchSpcToAddModList[idx].numCandidatesAggLevel1 = \
414          macPdcchCfg.searchSpcToAddModList[idx].numCandidatesAggLevel1;
415       schPdcchCfg->searchSpcToAddModList[idx].numCandidatesAggLevel2 = \
416          macPdcchCfg.searchSpcToAddModList[idx].numCandidatesAggLevel2;
417       schPdcchCfg->searchSpcToAddModList[idx].numCandidatesAggLevel4 = \
418          macPdcchCfg.searchSpcToAddModList[idx].numCandidatesAggLevel4;
419       schPdcchCfg->searchSpcToAddModList[idx].numCandidatesAggLevel8 = \
420          macPdcchCfg.searchSpcToAddModList[idx].numCandidatesAggLevel8;
421       schPdcchCfg->searchSpcToAddModList[idx].numCandidatesAggLevel16 = \
422          macPdcchCfg.searchSpcToAddModList[idx].numCandidatesAggLevel16;
423       schPdcchCfg->searchSpcToAddModList[idx].searchSpaceType = \
424          macPdcchCfg.searchSpcToAddModList[idx].searchSpaceType;
425       schPdcchCfg->searchSpcToAddModList[idx].ueSpecificDciFormat = \
426          macPdcchCfg.searchSpcToAddModList[idx].ueSpecificDciFormat;
427    }
428
429    schPdcchCfg->numSearchSpcToRel = macPdcchCfg.numSearchSpcToRel;
430    if(schPdcchCfg->numSearchSpcToRel > MAX_NUM_SEARCH_SPC)
431    {
432       DU_LOG("\nMAC : Number of search space to release [%d] exceeds max [%d]", \
433             schPdcchCfg->numSearchSpcToRel, MAX_NUM_SEARCH_SPC);
434       return RFAILED;
435    }
436    for(idx = 0; idx < schPdcchCfg->numSearchSpcToRel; idx++)
437    {
438       /* TODO */
439    }
440
441    return ROK;
442 }
443
444 /*******************************************************************
445  *
446  * @brief Fill PDSCH cfg in Initial DL BWP for UE Cfg in Scheduler
447  *
448  * @details
449  *
450  *    Function : fillInitDlBwpPdschCfg
451  *
452  *    Functionality:
453  *        Fill PDSCH cfg in Initial DL BWP for UE Cfg in Scheduler
454  *
455  * @params[in] macPdschCfg : Inital DL BWP PDSCH cfg at  MAC
456  *             schPdschCfg : Inital DL BWP PDSCH cfg to be filled
457  * @return ROK     - success
458  *         RFAILED - failure
459  *
460  * ****************************************************************/
461 uint8_t fillInitDlBwpPdschCfg(PdschConfig macPdschCfg, SchPdschConfig *schPdschCfg)
462 {
463    uint8_t   idx;
464
465    schPdschCfg->dmrsDlCfgForPdschMapTypeA.addPos = \
466        macPdschCfg.dmrsDlCfgForPdschMapTypeA.addPos;
467    schPdschCfg->resourceAllocType = macPdschCfg.resourceAllocType;
468    schPdschCfg->numTimeDomRsrcAlloc = macPdschCfg.numTimeDomRsrcAlloc;
469    if(schPdschCfg->numTimeDomRsrcAlloc > MAX_NUM_DL_ALLOC)
470    {
471       DU_LOG("\nMAC : Number of time domain resource allocation [%d], exceeds\
472             max limit [%d]", schPdschCfg->numTimeDomRsrcAlloc, MAX_NUM_DL_ALLOC);
473       return RFAILED;
474    }
475
476    for(idx = 0; idx < schPdschCfg->numTimeDomRsrcAlloc; idx++)
477    {
478       schPdschCfg->timeDomRsrcAllociList[idx].mappingType = \
479          macPdschCfg.timeDomRsrcAllociList[idx].mappingType;
480       schPdschCfg->timeDomRsrcAllociList[idx].startSymbol = \
481          macPdschCfg.timeDomRsrcAllociList[idx].startSymbol;
482       schPdschCfg->timeDomRsrcAllociList[idx].symbolLength = \
483          macPdschCfg.timeDomRsrcAllociList[idx].symbolLength;
484    }
485
486    schPdschCfg->rbgSize = macPdschCfg.rbgSize;
487    schPdschCfg->numCodeWordsSchByDci = macPdschCfg.numCodeWordsSchByDci;
488    schPdschCfg->bundlingType = macPdschCfg.bundlingType;
489    if(schPdschCfg->bundlingType == STATIC_BUNDLING_TYPE)
490    {
491       schPdschCfg->bundlingInfo.SchStaticBundling.size  = macPdschCfg.bundlingInfo.StaticBundling.size;
492    }
493    else if(schPdschCfg->bundlingType == DYNAMIC_BUNDLING_TYPE)
494    {
495       schPdschCfg->bundlingInfo.SchDynamicBundling.sizeSet1 = macPdschCfg.bundlingInfo.DynamicBundling.sizeSet1;
496       schPdschCfg->bundlingInfo.SchDynamicBundling.sizeSet2 = macPdschCfg.bundlingInfo.DynamicBundling.sizeSet2;
497    }
498    return ROK;
499 }
500
501 /*******************************************************************
502  *
503  * @brief Fill Initial DL BWP for SCH UE config
504  *
505  * @details
506  *
507  *    Function : fillInitialDlBwp
508  *
509  *    Functionality: Fill Initial DL BWP for SCH UE config
510  *
511  * @params[in] macInitDlBwp : Inital DL BWP cfg at MAC
512  *             schInitDlBwp : Inital DL BWP cfg to be filled
513  * @return ROK     - success
514  *         RFAILED - failure
515  *
516  * ****************************************************************/
517 uint8_t fillInitialDlBwp(InitialDlBwp macInitDlBwp, SchInitalDlBwp *schInitDlBwp)
518 {
519    schInitDlBwp->pdcchCfgPres = macInitDlBwp.pdcchPresent;
520    if(schInitDlBwp->pdcchCfgPres)
521    {
522       if(fillInitDlBwpPdcchCfg(macInitDlBwp.pdcchCfg, &schInitDlBwp->pdcchCfg) != ROK)
523       {
524          DU_LOG("\nMAC : fillInitDlBwpPdcchCfg() failed");
525          return RFAILED;
526       }
527    }
528
529    schInitDlBwp->pdschCfgPres = macInitDlBwp.pdschPresent;
530    if(schInitDlBwp->pdschCfgPres)
531    {
532       if(fillInitDlBwpPdschCfg(macInitDlBwp.pdschCfg,&schInitDlBwp->pdschCfg) != ROK)
533       {
534          DU_LOG("\nMAC : fillInitDlBwpPdschCfg() failed");
535          return RFAILED;
536       }
537    }
538    return ROK;
539 }
540
541 /*******************************************************************
542  *
543  * @brief Fills Sp Cell config to be sent to scheduler
544  *
545  * @details
546  *
547  *    Function : fillSpCellCfg 
548  *
549  *    Functionality: Fills Sp Cell config to be sent to scheduler
550  *
551  * @params[in] macSpCellCfg : SP cell cfg at MAC
552  *             schSpCellCfg : SP cell cfg to be filled
553  * @return ROK     - success
554  *         RFAILED - failure
555  *
556  * ****************************************************************/
557 uint8_t fillSpCellCfg(SpCellCfg macSpCellCfg, SchSpCellCfg *schSpCellCfg)
558 {
559    uint8_t   idx;
560    SchServCellCfgInfo   *servCellCfg;
561
562    schSpCellCfg->servCellIdx = macSpCellCfg.servCellIdx;
563    servCellCfg = &schSpCellCfg->servCellCfg;
564
565    /* Fill initial DL BWP */
566    if(fillInitialDlBwp(macSpCellCfg.servCellCfg.initDlBwp, \
567             &servCellCfg->initDlBwp) != ROK )
568    {
569       DU_LOG("\nMAC : fillInitialDlBwp() failed");
570       return RFAILED;
571    }
572
573    servCellCfg->numDlBwpToAdd = macSpCellCfg.servCellCfg.numDlBwpToAdd;
574    if(servCellCfg->numDlBwpToAdd > MAX_NUM_BWP)
575    {
576       DU_LOG("\nMAC : Number of DL BWP to ADD/MOD [%d] exceeds max limit [%d]",\
577             servCellCfg->numDlBwpToAdd, MAX_NUM_BWP);
578       return RFAILED;
579    }
580    for(idx = 0; idx < servCellCfg->numDlBwpToAdd; idx++)
581    {
582       /* TODO : As of now numDlBwpToAdd = 0 */
583    }
584
585    servCellCfg->firstActvDlBwpId =  macSpCellCfg.servCellCfg.firstActvDlBwpId;
586    servCellCfg->defaultDlBwpId = macSpCellCfg.servCellCfg.defaultDlBwpId;
587    servCellCfg->bwpInactivityTmr = NULL;
588    if(macSpCellCfg.servCellCfg.bwpInactivityTmr)
589    {
590       /* TODO : This is an optional parameter, not filled currently */
591    }
592
593    /* Fill PDSCH serving cell config */
594    if(fillPdschServCellCfg(macSpCellCfg.servCellCfg.pdschServCellCfg, \
595             &servCellCfg->pdschServCellCfg) != ROK)
596    {
597       DU_LOG("\nMAC : fillPdschServCellCfg() failed");
598       return RFAILED;
599    }
600
601    /* Fill Initail UL BWP */
602    if(fillInitialUlBwp(macSpCellCfg.servCellCfg.initUlBwp, \
603             &servCellCfg->initUlBwp) != ROK)
604    {
605       DU_LOG("\nMAC : fillInitialUlBwp() failed");
606       return RFAILED;
607    }
608
609    servCellCfg->numUlBwpToAdd = macSpCellCfg.servCellCfg.numUlBwpToAdd;
610    if(servCellCfg->numUlBwpToAdd > MAX_NUM_BWP)
611    {
612       DU_LOG("\nMAC : Number of UL BWP to ADD/MOD [%d] exceeds max limit [%d]",\
613             servCellCfg->numUlBwpToAdd, MAX_NUM_BWP);
614       return RFAILED;
615    }
616    for(idx = 0; idx < servCellCfg->numUlBwpToAdd; idx++)
617    {
618       /* TODO : As of now numDlBwpToAdd = 0 */
619    }
620    servCellCfg->firstActvUlBwpId =  macSpCellCfg.servCellCfg.firstActvUlBwpId;
621
622    return ROK;
623 }
624
625 /*******************************************************************
626  *
627  * @brief Sends UE configuration to Scheduler
628  *
629  * @details
630  *
631  *    Function : sendUeReqToSch
632  *
633  *    Functionality: sends UE configuration to Scheduler
634  *
635  * @params[in] Pst and Ue configuration
636  * @return ROK     - success
637  *         RFAILED - failure
638  *
639  * ****************************************************************/
640
641 uint8_t sendUeReqToSch(Pst *pst, SchUeCfg *schUeCfg)
642 {
643    Pst schPst;
644    switch(pst->event)
645    {
646       case EVENT_MAC_UE_CREATE_REQ:
647          FILL_PST_MAC_TO_SCH(schPst, EVENT_ADD_UE_CONFIG_REQ_TO_SCH);
648          return(*macSchAddUeConfigReqOpts[schPst.selector])(&schPst, schUeCfg);
649
650       case EVENT_MAC_UE_RECONFIG_REQ:
651          FILL_PST_MAC_TO_SCH(schPst, EVENT_MODIFY_UE_CONFIG_REQ_TO_SCH);
652          return(*macSchModUeConfigReqOpts[schPst.selector])(&schPst,schUeCfg);
653       default: 
654          DU_LOG("\n Invalid Pst received %d", pst->event);
655          return RFAILED;
656    }
657 }
658
659 /*******************************************************************
660  *
661  * @brief Fills Sch Drb Qos Information
662  *
663  * @details
664  *
665  *    Function : fillSchDrbQosInfo
666  *
667  *    Functionality: Fills Sch Drb Qos Information
668  *
669  * @params[in] macLcCfg : Logical channel Cfg at MAC
670  *             schLcCfg : LC cfg to fill at scheduler
671  * @return ROK     - success
672  *         RFAILED - failure
673  *
674  * ****************************************************************/
675 void fillSchDrbQosInfo(DrbQosInfo *macDrbQos, SchDrbQosInfo *schDrbQos)
676 {
677    schDrbQos->fiveQiType  = macDrbQos->fiveQiType;
678    if(schDrbQos->fiveQiType == SCH_QOS_NON_DYNAMIC)
679    {
680       schDrbQos->u.nonDyn5Qi.fiveQi = macDrbQos->u.nonDyn5Qi.fiveQi;
681       schDrbQos->u.nonDyn5Qi.avgWindow = macDrbQos->u.nonDyn5Qi.avgWindow;
682       schDrbQos->u.nonDyn5Qi.maxDataBurstVol = macDrbQos->u.nonDyn5Qi.maxDataBurstVol;
683       schDrbQos->u.nonDyn5Qi.priorLevel =  macDrbQos->u.nonDyn5Qi.priorLevel;
684    }
685    else if(schDrbQos->fiveQiType == SCH_QOS_DYNAMIC)
686    {
687       schDrbQos->u.dyn5Qi.priorLevel         = macDrbQos->u.dyn5Qi.priorLevel;
688       schDrbQos->u.dyn5Qi.packetDelayBudget  = macDrbQos->u.dyn5Qi.packetDelayBudget;
689       schDrbQos->u.dyn5Qi.packetErrRateScalar= macDrbQos->u.dyn5Qi.packetErrRateScalar;
690       schDrbQos->u.dyn5Qi.packetErrRateExp   = macDrbQos->u.dyn5Qi.packetErrRateExp;
691       schDrbQos->u.dyn5Qi.fiveQi             = macDrbQos->u.dyn5Qi.fiveQi;
692       schDrbQos->u.dyn5Qi.delayCritical      = macDrbQos->u.dyn5Qi.delayCritical;
693       schDrbQos->u.dyn5Qi.avgWindow          = macDrbQos->u.dyn5Qi.avgWindow;
694       schDrbQos->u.dyn5Qi.maxDataBurstVol    = macDrbQos->u.dyn5Qi.maxDataBurstVol;
695    }
696    schDrbQos->ngRanRetPri.priorityLevel   = macDrbQos->ngRanRetPri.priorityLevel;
697    schDrbQos->ngRanRetPri.preEmptionCap   = macDrbQos->ngRanRetPri.preEmptionCap;
698    schDrbQos->ngRanRetPri.preEmptionVul   = macDrbQos->ngRanRetPri.preEmptionVul;
699    schDrbQos->grbQosFlowInfo.maxFlowBitRateDl = macDrbQos->grbQosInfo.maxFlowBitRateDl;
700    schDrbQos->grbQosFlowInfo.maxFlowBitRateUl = macDrbQos->grbQosInfo.maxFlowBitRateUl;
701    schDrbQos->grbQosFlowInfo.guarFlowBitRateDl= macDrbQos->grbQosInfo.guarFlowBitRateDl;
702    schDrbQos->grbQosFlowInfo.guarFlowBitRateUl= macDrbQos->grbQosInfo.guarFlowBitRateUl;
703    schDrbQos->pduSessionId = macDrbQos->pduSessionId;
704    schDrbQos->ulPduSessAggMaxBitRate = macDrbQos->ulPduSessAggMaxBitRate;
705 }
706
707 /*******************************************************************
708  *
709  * @brief Fill SCH UL logical channel configuration
710  *
711  * @details
712  *
713  *    Function : fillSchUlLcCfg
714  *
715  *    Functionality: Fills Sch Ul Lc configuration
716  *
717  * @params[in] macLcCfg : Logical channel Cfg at MAC
718  *             schLcCfg : LC cfg to fill at scheduler
719  * @return void
720  *
721  * ****************************************************************/
722
723 void fillSchUlLcCfg(SchUlLcCfg *schUlLcCfg, UlLcCfg *macUlLcCfg) 
724 {
725    schUlLcCfg->priority= macUlLcCfg->priority;
726    schUlLcCfg->lcGroup = macUlLcCfg->lcGroup;
727    schUlLcCfg->schReqId= macUlLcCfg->schReqId;
728    schUlLcCfg->pbr    = macUlLcCfg->pbr;    
729    schUlLcCfg->bsd    = macUlLcCfg->bsd;    
730 }
731
732 /*******************************************************************
733  *
734  * @brief Fill logical channel configuration
735  *
736  * @details
737  *
738  *    Function : fillLogicalChannelCfg
739  *
740  *    Functionality: Fill logical channel configuration
741  *
742  * @params[in] macLcCfg : Logical channel Cfg at MAC
743  *             schLcCfg : LC cfg to fill at scheduler
744  * @return ROK     - success
745  *         RFAILED - failure
746  *
747  * ****************************************************************/
748 uint8_t fillLogicalChannelCfg(SchLcCfg *schLcCfg, LcCfg *macLcCfg)
749 {
750    uint8_t sdIdx;
751    uint8_t ret = ROK;
752    schLcCfg->lcId = macLcCfg->lcId;
753    schLcCfg->configType = macLcCfg->configType;
754    schLcCfg->dlLcCfg.lcp = macLcCfg->dlLcCfg.lcp;
755    fillSchUlLcCfg(&schLcCfg->ulLcCfg, &macLcCfg->ulLcCfg);
756
757    if(macLcCfg->drbQos)
758    {
759      if(!schLcCfg->drbQos)
760      {
761         MAC_ALLOC(schLcCfg->drbQos, sizeof(SchDrbQosInfo));
762         if(!schLcCfg->drbQos)
763         {
764            DU_LOG("\nMAC : Memory alloc failed at drbQos at fillLogicalChannelCfg()");
765            ret = RFAILED;
766         }
767      }
768      if(ret == ROK)
769      {
770         fillSchDrbQosInfo(macLcCfg->drbQos, schLcCfg->drbQos);
771      }
772      else
773         return ret;
774    }
775    else
776    {
777       schLcCfg->drbQos = NULLP;
778    }
779
780    if(ret == ROK)
781    {
782       if(macLcCfg->snssai)
783       {
784          if(!schLcCfg->snssai)
785          {
786             MAC_ALLOC(schLcCfg->snssai, sizeof(SchSnssai));
787             if(!schLcCfg->snssai)
788             {
789                DU_LOG("\nMAC : Memory alloc failed at snssai at fillLogicalChannelCfg()");
790                ret = RFAILED;
791             }
792          }
793          if(ret == ROK)
794          {
795             schLcCfg->snssai->sst = macLcCfg->snssai->sst;
796             for(sdIdx = 0; sdIdx < SD_SIZE; sdIdx++)
797             {
798               schLcCfg->snssai->sd[sdIdx] = macLcCfg->snssai->sd[sdIdx];
799             }
800          }
801          else
802          {
803             schLcCfg->snssai = NULLP;
804             /*Freeing the previously allocated buffer in case of failure */
805             if(schLcCfg->drbQos)
806             {
807                MAC_FREE(schLcCfg->drbQos, sizeof(SchDrbQosInfo));
808                schLcCfg->drbQos = NULLP;
809             }
810             return ret;
811          }
812       }
813       else
814       {
815          schLcCfg->snssai = NULLP;
816       }
817    }
818
819    return ret;
820 }
821
822 /*******************************************************************
823  *
824  * @brief Fills Logical channel Cfg List to Add/Mod/Del
825  *
826  * @details
827  *
828  *    Function : fillSchLcCfgList
829  *
830  *    Functionality: Fills Logical channel Cfg List to Add/Mod/Del
831  *
832  * @params[in] MAC UE Cb Cfg , MAC UE Configuration
833  * @return ROK     - success
834  *         RFAILED - failure
835  *
836  * ****************************************************************/
837
838 uint8_t fillSchLcCfgList(SchUeCfg *schUeCfg, MacUeCfg *ueCfg)
839 {
840    uint8_t lcIdx;
841
842    for(lcIdx = 0; lcIdx < ueCfg->numLcs; lcIdx++)
843    {
844       if(fillLogicalChannelCfg(&schUeCfg->schLcCfg[lcIdx], &ueCfg->lcCfgList[lcIdx]) != ROK)
845       {
846          DU_LOG("\nMAC : fillLogicalChannelCfg() failed for lc Idx[%d]", lcIdx);
847          return RFAILED;
848       }
849       schUeCfg->numLcs++;
850    }
851    return ROK;
852 }
853
854 /*******************************************************************
855  *
856  * @brief Fills and sends UE configuration to Scheduler
857  *
858  * @details
859  *
860  *    Function : fillSchUeCfg
861  *
862  *    Functionality: Fills and sends UE configuration to Scheduler
863  *
864  * @params[in] Ue configuration from DU APP
865  * @return ROK     - success
866  *         RFAILED - failure
867  *
868  * ****************************************************************/
869 uint8_t fillSchUeCfg(Pst *pst, SchUeCfg *schUeCfg, MacUeCfg *ueCfg)
870 {
871    uint8_t ret = ROK;
872
873    schUeCfg->cellId = ueCfg->cellId;
874    schUeCfg->crnti = ueCfg->crnti;
875
876    /* Copy MAC cell group config */
877    memset(&schUeCfg->macCellGrpCfg, 0, sizeof(SchMacCellGrpCfg));
878    if(fillMacCellGroupCfg(ueCfg->macCellGrpCfg, &schUeCfg->macCellGrpCfg) != ROK)
879    {
880       DU_LOG("\nMAC : fillMacCellGroupCfg() failed");
881       return RFAILED;
882    }
883
884    /* Copy Physical cell group config */
885    memset(&schUeCfg->phyCellGrpCfg, 0,sizeof(SchPhyCellGrpCfg));
886    if(fillPhyCellGroupCfg(ueCfg->phyCellGrpCfg, &schUeCfg->phyCellGrpCfg) != ROK)
887    {
888       DU_LOG("\nMAC : fillPhyCellGroupCfg() failed");
889       return RFAILED;
890    }
891
892    /* Copy sp cell config */
893    memset(&schUeCfg->spCellCfg, 0, sizeof(SchSpCellCfg));
894    if(fillSpCellCfg(ueCfg->spCellCfg, &schUeCfg->spCellCfg) != ROK)
895    {
896       DU_LOG("\nMAC : fillSpCellCfg() failed");
897       return RFAILED;
898    }
899
900    if(ueCfg->ambrCfg != NULLP)
901    {
902       MAC_ALLOC(schUeCfg->ambrCfg, sizeof(SchAmbrCfg));
903       if(!schUeCfg->ambrCfg)
904       {
905          DU_LOG("\nMAC : Memory allocation failed in sendReconfigReqToSch");
906          return RFAILED;
907       }
908       schUeCfg->ambrCfg->ulBr = ueCfg->ambrCfg->ulBr;
909       schUeCfg->ambrCfg->dlBr = ueCfg->ambrCfg->dlBr;
910    }
911    else
912       schUeCfg->ambrCfg = NULLP;
913
914    /* Fill sch Lc Cfg  to Add/ Mod/ Del */
915    ret  = fillSchLcCfgList(schUeCfg, ueCfg);
916    if(ret == RFAILED)
917    {
918       DU_LOG("\nMAC : Failed to copy LCs at fillSchUeCfg()");
919       return ret;
920    }
921    return ret;
922 }
923
924 /*******************************************************************
925  *
926  * @brief Update UeUlCb Lc List
927  *
928  * @details
929  *
930  *    Function : updateMacUlCb
931  *
932  *    Functionality: Update UeUlCb Lc List
933  *
934  * @params[in] delIdx, UeUlCb pointer
935  * @return void
936  *
937  * ****************************************************************/
938
939 void updateMacUlCb(uint8_t delIdx, UeUlCb *ulCb)
940 {
941    uint8_t lcIdx = 0;
942
943    for(lcIdx = delIdx; lcIdx < ulCb->numUlLc; lcIdx++)
944    {
945       /* Moving the array element to one step ahead */
946       memcpy(&ulCb->lcCb[lcIdx], &ulCb->lcCb[lcIdx+1], sizeof(UlLcCb));
947       memset(&ulCb->lcCb[lcIdx+1], 0, sizeof(UlLcCb));
948    }
949 }
950
951 /*******************************************************************
952  *
953  * @brief Update UeDlCb Lc List
954  *
955  * @details
956  *
957  *    Function : updateMacDlCb
958  *
959  *    Functionality: Update UeDlCb Lc List
960  *
961  * @params[in] delIdx, UeDlCb pointer
962  * @return void
963  *
964  * ****************************************************************/
965
966 void updateMacDlCb(uint8_t delIdx, UeDlCb *dlCb)
967 {
968    uint8_t lcIdx = 0;
969
970    for(lcIdx = delIdx; lcIdx < dlCb->numDlLc; lcIdx++)
971    {
972       /* Moving the array element to one step ahead */
973       memcpy(&dlCb->lcCb[lcIdx], &dlCb->lcCb[lcIdx+1], sizeof(DlLcCb));
974       memset(&dlCb->lcCb[lcIdx+1], 0, sizeof(DlLcCb));
975    }
976 }
977
978 /*******************************************************************
979  *
980  * @brief Fills Logical channel Cfg List to Add/Mod/Del
981  *
982  * @details
983  *
984  *    Function : fillMacLcCfgList
985  *
986  *    Functionality: Fills Logical channel Cfg List to Add/Mod/Del
987  *
988  * @params[in] MAC UE Cb Cfg , MAC UE Configuration
989  * @return ROK     - success
990  *         RFAILED - failure
991  *
992  * ****************************************************************/
993
994 uint8_t fillMacLcCfgList(MacUeCb *ueCb, MacUeCfg *ueCfg)
995 {
996    uint8_t lcIdx, ueLcIdx;
997
998    for(lcIdx = 0; lcIdx < ueCfg->numLcs; lcIdx++)
999    {
1000       if(ueCb->dlInfo.numDlLc < MAX_NUM_LC)
1001       {
1002          if(ueCfg->lcCfgList[lcIdx].configType == CONFIG_ADD)
1003          {
1004             /*Filling DL LC CB */
1005             ueCb->dlInfo.lcCb[ueCb->dlInfo.numDlLc].lcId = ueCfg->lcCfgList[lcIdx].lcId;
1006             ueCb->dlInfo.lcCb[ueCb->dlInfo.numDlLc].lcState = MAC_LC_STATE_ACTIVE;
1007             ueCb->dlInfo.numDlLc++;
1008             /*Filling UL LC CB */
1009             ueCb->ulInfo.lcCb[ueCb->ulInfo.numUlLc].lcId = ueCfg->lcCfgList[lcIdx].lcId;
1010             ueCb->ulInfo.lcCb[ueCb->ulInfo.numUlLc].lcGrpId = ueCfg->lcCfgList[lcIdx].ulLcCfg.lcGroup;
1011             ueCb->ulInfo.lcCb[ueCb->ulInfo.numUlLc].lcActive = MAC_LC_STATE_ACTIVE;
1012             ueCb->ulInfo.numUlLc++;
1013          }/*End of Add Config */
1014          else
1015          { 
1016             //searching for Lc to be Mod
1017             for(ueLcIdx = 0; ueLcIdx < ueCb->ulInfo.numUlLc; ueLcIdx++)
1018             {
1019                if(ueCb->ulInfo.lcCb[ueLcIdx].lcId == ueCfg->lcCfgList[lcIdx].lcId)
1020                {
1021                   if(ueCfg->lcCfgList[lcIdx].configType == CONFIG_MOD)
1022                   {
1023                      /*Nothing to Modify in DL LC CB */
1024                      /*Modify UL LC CB */
1025                      ueCb->ulInfo.lcCb[ueLcIdx].lcGrpId = ueCfg->lcCfgList[lcIdx].ulLcCfg.lcGroup;
1026                      DU_LOG("\nMAC: Successfully Modified LC context for lcId[%d]", ueCfg->lcCfgList[lcIdx].lcId);
1027                      break;
1028                   }
1029                   if(ueCfg->lcCfgList[lcIdx].configType == CONFIG_DEL)
1030                   {
1031                      memset(&ueCb->dlInfo.lcCb[ueLcIdx], 0, sizeof(DlLcCb));
1032                      (ueCb->dlInfo.numDlLc)--;
1033                      updateMacDlCb(ueLcIdx, &ueCb->dlInfo);
1034
1035                      memset(&ueCb->ulInfo.lcCb[ueLcIdx], 0, sizeof(UlLcCb));
1036                      (ueCb->ulInfo.numUlLc)--;
1037                      updateMacUlCb(ueLcIdx, &ueCb->ulInfo);
1038                      DU_LOG("\nMAC: Successfully Deleted LC context for lcId[%d]", ueCfg->lcCfgList[lcIdx].lcId);
1039                      break;
1040                   }
1041                }
1042             }
1043          }/*End of Mod Config */
1044       }
1045    }
1046    return ROK;
1047 }
1048
1049 /*******************************************************************
1050  *
1051  * @brief Fills MAC UE Cb Cfg
1052  *
1053  * @details
1054  *
1055  *    Function : fillMacUeCb
1056  *
1057  *    Functionality: Fills MAC UE Cb Cfg
1058  *
1059  * @params[in] MAC UE Cb Cfg , MAC UE Configuration
1060  *             cellIdx
1061  * @return ROK     - success
1062  *         RFAILED - failure
1063  *
1064  * ****************************************************************/
1065
1066 uint8_t fillMacUeCb(MacUeCb *ueCb, MacUeCfg *ueCfg, uint8_t cellIdx)
1067 {
1068    uint8_t ret = ROK;
1069
1070    ueCb->ueIdx = ueCfg->ueIdx;
1071    ueCb->crnti = ueCfg->crnti;
1072    ueCb->cellCb = macCb.macCell[cellIdx];
1073    ueCb->dlInfo.dlHarqEnt.numHarqProcs = \
1074       ueCfg->spCellCfg.servCellCfg.pdschServCellCfg.numHarqProcForPdsch; 
1075    ueCb->state = UE_STATE_ACTIVE;
1076    /*TODO: To check the bsr value during implementation */
1077    ueCb->bsrTmrCfg.periodicTimer = ueCfg->macCellGrpCfg.bsrTmrCfg.periodicTimer;
1078    ueCb->bsrTmrCfg.retxTimer     = ueCfg->macCellGrpCfg.bsrTmrCfg.retxTimer;
1079    ueCb->bsrTmrCfg.srDelayTimer  = ueCfg->macCellGrpCfg.bsrTmrCfg.srDelayTimer;
1080    ret = fillMacLcCfgList(ueCb, ueCfg);
1081    if(ret == RFAILED)
1082    {
1083       DU_LOG("\nMAC: Failed while filing MAC LC List at fillMacUeCb()");
1084    }
1085    return ret;
1086 }
1087
1088 /*******************************************************************
1089  *
1090  * @brief Function to update Mac Ra Cb
1091  *
1092  * @details
1093  *
1094  *    Function : updateMacRaCb
1095  *
1096  *    Functionality: Function to update Mac Ra Cb
1097  *
1098  * @params[in] cellIdx, Mac Ue Cb
1099  * @return ROK     - success
1100  *         RFAILED - failure
1101  *
1102  * ****************************************************************/
1103
1104 uint8_t updateMacRaCb(uint16_t cellIdx, MacUeCb *ueCb)
1105 {
1106    uint8_t ueIdx;
1107    /* Copy RA Cb */
1108    for(ueIdx = 0; ueIdx < MAX_NUM_UE; ueIdx++)
1109    {
1110       if(macCb.macCell[cellIdx]->macRaCb[ueIdx].crnti == ueCb->crnti)
1111       {
1112          ueCb->raCb = &macCb.macCell[cellIdx]->macRaCb[ueIdx];
1113          break;
1114       }
1115    }
1116    return ROK;
1117 }
1118
1119 /*******************************************************************
1120  *
1121  * @brief Function to delete Mac Ra Cb
1122  *
1123  * @details
1124  *
1125  *    Function : deleteMacRaCb
1126  *
1127  *    Functionality: Function to delete Mac Ra Cb
1128  *
1129  * @params[in] cellIdx, Mac Ue Cb
1130  * @return void
1131  *
1132  * ****************************************************************/
1133
1134 void deleteMacRaCb(uint16_t cellIdx, MacUeCb *ueCb)
1135 {
1136    uint8_t ueIdx;
1137
1138    for(ueIdx = 0; ueIdx < MAX_NUM_UE; ueIdx++)
1139    {
1140       if(macCb.macCell[cellIdx]->macRaCb[ueIdx].crnti == ueCb->crnti)
1141       {
1142          if(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4Pdu)
1143          {
1144            MAC_FREE(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4Pdu, \
1145                      macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4PduLen);
1146          }
1147          if(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4TxPdu)
1148          {
1149             MAC_FREE(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4TxPdu, \
1150                       macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4TbSize);
1151          }
1152          memset(&macCb.macCell[cellIdx]->macRaCb[ueIdx], 0, sizeof(MacRaCbInfo));
1153          break;
1154       }
1155    }
1156                   
1157 }
1158
1159 /*******************************************************************
1160  *
1161  * @brief Creates UE Cb
1162  *
1163  * @details
1164  *
1165  *    Function : createUeCb
1166  *
1167  *    Functionality: Creates UE Cb
1168  *
1169  * @params[in] MAC UE Configuration
1170  * @return ROK     - success
1171  *         RFAILED - failure
1172  *
1173  * ****************************************************************/
1174 uint8_t createUeCb(uint8_t cellIdx, MacUeCb *ueCb, MacUeCfg *ueCfg)
1175 {
1176    uint8_t ret =ROK;
1177
1178    if((ueCb->ueIdx == ueCfg->ueIdx) && (ueCb->crnti == ueCfg->crnti)\
1179       &&(ueCb->state == UE_STATE_ACTIVE))
1180    {
1181       DU_LOG("\n MAC : CRNTI %d already configured ", ueCfg->crnti);
1182       return ROKDUP;
1183    }
1184    else
1185    {
1186       memset(ueCb, 0, sizeof(MacUeCb));
1187       ret = fillMacUeCb(ueCb, ueCfg, cellIdx);
1188       if(ret != ROK)
1189       {
1190          DU_LOG("\nMAC : Failed to create Ue Cb at createUeCb()");
1191          return ret;
1192       }
1193       else
1194       {
1195          macCb.macCell[cellIdx]->numActvUe++;
1196          updateMacRaCb(cellIdx, ueCb);
1197          return ROK;
1198       }
1199
1200    }
1201
1202 }
1203
1204 /*******************************************************************
1205  *
1206  * @brief Modify UE Cb Cfg
1207  *
1208  * @details
1209  *
1210  *    Function : modifyUeCb
1211  *
1212  *    Functionality: modify UE Cb
1213  *
1214  * @params[in] MAC UE Configuration
1215  * @return ROK     - success
1216  *         RFAILED - failure
1217  *
1218  * ****************************************************************/
1219 uint8_t modifyUeCb(uint8_t cellIdx, MacUeCb *ueCb, MacUeCfg *ueCfg)
1220 {
1221    uint8_t ret = ROK;
1222
1223    if((ueCb->ueIdx == ueCfg->ueIdx) && (ueCb->crnti == ueCfg->crnti)\
1224       &&(ueCb->state == UE_STATE_ACTIVE))
1225    {
1226       DU_LOG("\n MAC : Modifying Ue config Req for CRNTI %d ", ueCfg->crnti);
1227       ret = fillMacUeCb(ueCb, ueCfg, cellIdx);
1228       if(ret != ROK)
1229       {
1230          DU_LOG("\nMAC : Failed to modify MacUeCb at modifyUeCb()");
1231          return ret;
1232       }
1233       else
1234       {
1235          deleteMacRaCb(cellIdx, ueCb);
1236          return ROK;
1237       }
1238    }
1239    return RFAILED;
1240 }
1241
1242
1243 /*******************************************************************
1244  *
1245  * @brief Creates UE Cb and fills ueCfg
1246  *
1247  * @details
1248  *
1249  *    Function : procMacUeCfgData
1250  *
1251  *    Functionality: Creates UE Cb and fills ueCfg
1252  *
1253  * @params[in] MAC UE Configuration
1254  * @return ROK     - success
1255  *         RFAILED - failure
1256  *
1257  * ****************************************************************/
1258
1259 uint8_t procMacUeCfgData(Pst *pst, MacUeCfg *ueCfg)
1260 {
1261    uint8_t ret = ROK;
1262    uint16_t  cellIdx;
1263    MacUeCb   *ueCb = NULLP;
1264
1265
1266    GET_CELL_IDX(ueCfg->cellId, cellIdx);
1267
1268    /* Validate cell id */
1269    if(macCb.macCell[cellIdx]->cellId != ueCfg->cellId)
1270    {
1271       DU_LOG("\nMAC : Cell Id %d not configured", ueCfg->cellId);
1272       return RFAILED;
1273    }
1274
1275    /* Check if max number of UE configured */
1276    if(macCb.macCell[cellIdx]->numActvUe > MAX_NUM_UE)
1277    {
1278       DU_LOG("MAC : Max number of UE [%d] already configured", MAX_NUM_UE);
1279       return RFAILED;
1280    }
1281
1282    /* Check if UE already configured */
1283    ueCb = &macCb.macCell[cellIdx]->ueCb[ueCfg->ueIdx -1];
1284    switch(pst->event)
1285    {
1286       case EVENT_UE_CONFIG_RSP_TO_MAC:
1287          ret = createUeCb(cellIdx, ueCb, ueCfg);
1288          if(ret != ROK)
1289             DU_LOG("\nMAC: AddUeConfigReq for cellIdx :%d failed in procMacUeCfgData()", cellIdx);
1290          break;
1291       case EVENT_UE_RECONFIG_RSP_TO_MAC:
1292          ret = modifyUeCb(cellIdx, ueCb, ueCfg);
1293          if(ret != ROK)
1294             DU_LOG("\nMAC: ModifyUeConfigReq for cellIdx :%d failed at procMacUeCfgData()", cellIdx);
1295          break;
1296       default:
1297          break;
1298    }
1299
1300    return ret;
1301 }
1302
1303 /*******************************************************************
1304  *
1305  * @brief Function to store the UeCfg Data 
1306  *
1307  * @details
1308  *
1309  *    Function : copyToTmpData
1310  *
1311  *    Functionality: Function to store the UeCfg Data
1312  *
1313  * @params[in] MacUeCfg pointer 
1314  * @return ROK     - success
1315  *         RFAILED - failure
1316  *
1317  * ****************************************************************/
1318
1319 uint8_t copyToTmpData(MacUeCfg *ueCfg)
1320 {
1321    uint8_t cellIdx;
1322    MacUeCfg *tmpData = NULLP;
1323
1324    MAC_ALLOC(tmpData, sizeof(MacUeCfg));
1325    if(!tmpData)
1326    {
1327       DU_LOG("\nMAC: Memory Alloc Failed at copyToTmpData()");
1328       return RFAILED;
1329    }
1330    memcpy(tmpData, ueCfg, sizeof(MacUeCfg));
1331    GET_CELL_IDX(ueCfg->cellId, cellIdx);
1332    macCb.macCell[cellIdx]->ueCfgTmpData[ueCfg->ueIdx-1] = tmpData;
1333    return ROK;
1334 }
1335
1336 /*******************************************************************
1337  *
1338  * @brief Handles UE create requst from DU APP
1339  *
1340  * @details
1341  *
1342  *    Function : MacProcUeCreateReq
1343  *
1344  *    Functionality: Handles UE create requst from DU APP
1345  *
1346  * @params[in] 
1347  * @return ROK     - success
1348  *         RFAILED - failure
1349  *
1350  * ****************************************************************/
1351 uint8_t MacProcUeCreateReq(Pst *pst, MacUeCfg *ueCfg)
1352 {
1353    uint8_t ret = ROK;
1354    SchUeCfg   schUeCfg;
1355    memset(&schUeCfg, 0, sizeof(SchUeCfg));
1356
1357    DU_LOG("\nMAC : UE Create Request for CRNTI[%d]", ueCfg->crnti);
1358
1359    if(ueCfg)
1360    {
1361       /*Storing received ueCfg in ueCfgTmpData */
1362       ret = copyToTmpData(ueCfg);
1363       if(ret == ROK)
1364       {
1365          /*Sending Cfg Req to SCH */
1366          ret = fillSchUeCfg(pst, &schUeCfg, ueCfg);
1367          if(ret != ROK)
1368             DU_LOG("\nMAC : Failed to fill Sch Ue Cfg at MacProcUeCreateReq()");
1369          else
1370          {
1371             /* Fill event and send UE create request to SCH */
1372             ret = sendUeReqToSch(pst, &schUeCfg);
1373             if(ret != ROK)
1374                DU_LOG("\nMAC : Failed to send UE Create request to SCH");
1375          }
1376       }
1377       else 
1378       {
1379          DU_LOG("\nMAC : Failed to store MAC UE CFG ");
1380       }
1381    }
1382    else
1383    {
1384       DU_LOG("\nMAC : MAC UE Create request processing failed");
1385       ret = RFAILED;
1386    }
1387    /* FREE shared memory */
1388    MAC_FREE_SHRABL_BUF(pst->region, pst->pool, ueCfg, sizeof(MacUeCfg));
1389
1390    return ret;
1391 }
1392
1393 /*******************************************************************
1394  *
1395  * @brief Fill and Send UE create response from MAC to DU APP
1396  *
1397  * @details
1398  *
1399  *    Function : MacSendUeCreateRsp
1400  *
1401  *    Functionality: Fill and Send UE create response from MAC to DUAPP
1402  *
1403  * @params[in] MAC UE create result
1404  *             SCH UE create response
1405  * @return ROK     - success
1406  *         RFAILED - failure
1407  *
1408  * ****************************************************************/
1409 uint8_t MacSendUeCreateRsp(MacRsp result, SchUeCfgRsp *schCfgRsp)
1410 {
1411    MacUeCfgRsp   *cfgRsp;
1412    Pst        rspPst;
1413
1414    MAC_ALLOC_SHRABL_BUF(cfgRsp, sizeof(MacUeCfgRsp));
1415    if(!cfgRsp)
1416    {
1417       DU_LOG("\nMAC: Memory allocation for UE config response failed");
1418       return RFAILED;
1419    }
1420
1421    /* Filling UE Config response */
1422    memset(cfgRsp, 0, sizeof(MacUeCfgRsp));
1423    cfgRsp->cellId = schCfgRsp->cellId;
1424    cfgRsp->ueIdx = schCfgRsp->ueIdx;
1425    cfgRsp->result = result;
1426
1427    /* Fill Post structure and send UE Create response*/
1428    memset(&rspPst, 0, sizeof(Pst));
1429    FILL_PST_MAC_TO_DUAPP(rspPst, EVENT_MAC_UE_CREATE_RSP);
1430    return (*macDuUeCfgRspOpts[rspPst.selector])(&rspPst, cfgRsp); 
1431 }
1432
1433 /*******************************************************************
1434  *
1435  * @brief Fill and Send UE Reconfig response from MAC to DU APP
1436  *
1437  * @details
1438  *
1439  *    Function : MacSendUeReconfigRsp
1440  *
1441  *    Functionality: Fill and Send UE Reconfig response from MAC to DUAPP
1442  *
1443  * @params[in] MAC UE create result
1444  *             SCH UE create response
1445  * @return ROK     - success
1446  *         RFAILED - failure
1447  *
1448  * ****************************************************************/
1449 uint8_t MacSendUeReconfigRsp(MacRsp result, SchUeCfgRsp *schCfgRsp)
1450 {
1451    MacUeCfgRsp   *cfgRsp;
1452    Pst        rspPst;
1453
1454    MAC_ALLOC_SHRABL_BUF(cfgRsp, sizeof(MacUeCfgRsp));
1455    if(!cfgRsp)
1456    {
1457       DU_LOG("\nMAC: Memory allocation for UE Reconfig response failed");
1458       return RFAILED;
1459    }
1460
1461    /* Filling UE Config response */
1462    memset(cfgRsp, 0, sizeof(MacUeCfgRsp));
1463    cfgRsp->cellId = schCfgRsp->cellId;
1464    cfgRsp->ueIdx = schCfgRsp->ueIdx;
1465    cfgRsp->result = result;
1466
1467    /* Fill Post structure and send UE Create response*/
1468    memset(&rspPst, 0, sizeof(Pst));
1469    FILL_PST_MAC_TO_DUAPP(rspPst, EVENT_MAC_UE_RECONFIG_RSP);
1470    return (*macDuUeCfgRspOpts[rspPst.selector])(&rspPst, cfgRsp);
1471 }
1472
1473 /*******************************************************************
1474  *
1475  * @brief  Function to return Mac Ue Cfg pointer
1476  *
1477  * @details
1478  *
1479  *    Function : getMacUeCfg
1480  *
1481  *    Functionality:
1482  *      Function to return Mac Ue Cfg pointer
1483  *
1484  * @params[in] cellIdx, ueIdx
1485  *
1486  * @return MacUeCfg pointer - success
1487  *         NULLP - failure
1488  *
1489  * ****************************************************************/
1490
1491 MacUeCfg *getMacUeCfg(uint16_t cellIdx, uint8_t ueIdx)
1492 {
1493    MacUeCfg *ueCfg = NULLP;
1494    if(macCb.macCell[cellIdx])
1495    {
1496       ueCfg = macCb.macCell[cellIdx]->ueCfgTmpData[ueIdx-1];
1497    }
1498    else
1499    {
1500       DU_LOG("\nMAC: Failed to get macCellCb in getMacUeCfg()");
1501    }
1502    return ueCfg;
1503 }
1504
1505 /*******************************************************************
1506  *
1507  * @brief  Processes UE create response from scheduler
1508  *
1509  * @details
1510  *
1511  *    Function : MacProcSchUeCfgRsp
1512  *
1513  *    Functionality:
1514  *      Processes UE create response from scheduler
1515  *      Sends UE create response to DU APP
1516  *
1517  * @params[in] Pst : Post structure
1518  *             schCfgRsp : Scheduler UE cfg response
1519  * @return ROK     - success
1520  *         RFAILED - failure
1521  *
1522  * ****************************************************************/
1523 uint8_t MacProcSchUeCfgRsp(Pst *pst, SchUeCfgRsp *schCfgRsp)
1524 {
1525    uint8_t result = MAC_DU_APP_RSP_NOK;
1526    uint8_t ret = ROK;
1527    uint16_t cellIdx;
1528    MacUeCfg *ueCfg = NULLP;
1529
1530    GET_CELL_IDX(schCfgRsp->cellId, cellIdx);
1531    ueCfg = getMacUeCfg(cellIdx, schCfgRsp->ueIdx);
1532    if(ueCfg == NULLP)
1533    {
1534       DU_LOG("\nMAC : Failed to find the Mac Ue Cfg for event [%d] in MacProcSchUeCfgRsp()", pst->event);
1535       ret = RFAILED;
1536    }
1537
1538    switch(pst->event)
1539    {
1540       case EVENT_UE_CONFIG_RSP_TO_MAC:
1541       {
1542          if(schCfgRsp->rsp != RSP_NOK)
1543          {
1544             DU_LOG("\nMAC: SCH UeConfigRsp for CRNTI[%d] is success in MacProcSchUeCfgRsp()", schCfgRsp->crnti);
1545             if(ret == ROK)
1546             {
1547                ret = procMacUeCfgData(pst, ueCfg);
1548                if(ret == ROK)
1549                {
1550                   result = MAC_DU_APP_RSP_OK;
1551                }
1552             }
1553          }
1554          else
1555          {
1556             DU_LOG("\nMAC: SCH UeConfigRsp for CRNTI[%d] is failed in MacProcSchUeCfgRsp()", schCfgRsp->crnti);
1557          }
1558          ret = MacSendUeCreateRsp(result, schCfgRsp);
1559       }
1560       break;
1561
1562       case EVENT_UE_RECONFIG_RSP_TO_MAC:
1563       {
1564          if(schCfgRsp->rsp != RSP_NOK)
1565          {
1566             DU_LOG("\nMAC: SCH UeReconfigRsp for CRNTI[%d] is success in MacProcSchUeCfgRsp()", schCfgRsp->crnti);
1567             if(ret == ROK)
1568             {
1569                ret = procMacUeCfgData(pst, ueCfg);
1570                if(ret == ROK)
1571                {
1572                   result = MAC_DU_APP_RSP_OK;
1573                }
1574             }
1575          }
1576          else
1577          {
1578             DU_LOG("\nMAC: SCH UeReconfigRsp for CRNTI[%d] is failed in MacProcSchUeCfgRsp()", schCfgRsp->crnti);
1579          }
1580          ret = MacSendUeReconfigRsp(result, schCfgRsp);
1581       }
1582       break;
1583       
1584       default:
1585       break;
1586    }
1587    MAC_FREE(ueCfg, sizeof(MacUeCfg));
1588    ueCfg = NULLP;
1589    
1590    return ret; 
1591 }
1592
1593 /*******************************************************************
1594  *
1595  * @brief Handles UE Reconfig requst from DU APP
1596  *
1597  * @details
1598  *
1599  *    Function : MacProcUeReconfigReq
1600  *
1601  *    Functionality: Handles UE Reconfig requst from DU APP
1602  *
1603  * @params[in] 
1604  * @return ROK     - success
1605  *         RFAILED - failure
1606  *
1607  * ****************************************************************/
1608 uint8_t MacProcUeReconfigReq(Pst *pst, MacUeCfg *ueCfg)
1609 {
1610    uint8_t ret = ROK;
1611    SchUeCfg   schUeCfg;
1612    memset(&schUeCfg, 0, sizeof(SchUeCfg));
1613
1614    DU_LOG("\nMAC : UE Reconfig Request for CRNTI[%d]", ueCfg->crnti);
1615
1616    if(ueCfg)
1617    {
1618       /*Storing received ueCfg in ueCfgTmpData */
1619       ret = copyToTmpData(ueCfg);
1620       if(ret == ROK)
1621       {
1622          /*Sending Cfg Req to SCH */
1623          ret = fillSchUeCfg(pst, &schUeCfg, ueCfg);
1624          if(ret != ROK)
1625             DU_LOG("\nMAC : Failed to fill sch Ue Cfg at MacProcUeReconfigReq()");
1626          else
1627          {
1628             /* Fill event and send UE create request to SCH */
1629             ret = sendUeReqToSch(pst, &schUeCfg);
1630             if(ret != ROK)
1631                DU_LOG("\nMAC : Failed to send UE Reconfig Request to SCH");
1632          }
1633       }
1634       else 
1635       {
1636          DU_LOG("\nMAC : Failed to store MAC UE Cb ");
1637       }
1638    }
1639    else
1640    {
1641       DU_LOG("\nMAC : MAC UE Create request processing failed");
1642       ret = RFAILED;
1643    }
1644    /* FREE shared memory */
1645    MAC_FREE_SHRABL_BUF(pst->region, pst->pool, ueCfg, sizeof(MacUeCfg));
1646    return ROK;
1647 }
1648
1649
1650 /**********************************************************************
1651   End of file
1652  **********************************************************************/