[JIRA ID: ODUHIGH-232]: RB config for MAC and SCH
[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 MacSchUeCreateReqFunc macSchUeCreateReqOpts[] =
33 {
34    packMacSchUeCreateReq,    /* packing for loosely coupled */
35    MacSchUeCreateReq,        /* packing for tightly coupled */
36    packMacSchUeCreateReq     /* 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 MacSchUeReconfigReqFunc macSchUeReconfigReqOpts[] =
47 {
48    packMacSchUeReconfigReq,    /* packing for loosely coupled */
49    MacSchUeReconfigReq,        /* packing for tightly coupled */
50    packMacSchUeReconfigReq     /* 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_UE_CREATE_REQ_TO_SCH);
648          return(*macSchUeCreateReqOpts[schPst.selector])(&schPst, schUeCfg);
649
650       case EVENT_MAC_UE_RECONFIG_REQ:
651          FILL_PST_MAC_TO_SCH(schPst, EVENT_UE_RECONFIG_REQ_TO_SCH);
652          return(*macSchUeReconfigReqOpts[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    schUeCfg->aggrMaxBitRate = NULL;
901    if(ueCfg->maxAggrBitRate != NULL)
902    {
903       MAC_ALLOC(schUeCfg->aggrMaxBitRate, sizeof(SchAggrMaxBitRate));
904       if(!schUeCfg->aggrMaxBitRate)
905       {
906          DU_LOG("\nMAC : Memory allocation failed in sendReconfigReqToSch");
907          return RFAILED;
908       }
909       schUeCfg->aggrMaxBitRate->ulBitRate = ueCfg->maxAggrBitRate->ulBits;
910       schUeCfg->aggrMaxBitRate->dlBitRate = ueCfg->maxAggrBitRate->dlBits;
911    }
912
913    /* Fill sch Lc Cfg  to Add/ Mod/ Del */
914    ret  = fillSchLcCfgList(schUeCfg, ueCfg);
915    if(ret == RFAILED)
916    {
917       DU_LOG("\nMAC : Failed to copy LCs at fillSchUeCfg()");
918       return ret;
919    }
920    return ret;
921 }
922
923 /*******************************************************************
924  *
925  * @brief Update UeUlCb Lc List
926  *
927  * @details
928  *
929  *    Function : updateMacUlCb
930  *
931  *    Functionality: Update UeUlCb Lc List
932  *
933  * @params[in] delIdx, UeUlCb pointer
934  * @return void
935  *
936  * ****************************************************************/
937
938 void updateMacUlCb(uint8_t delIdx, UeUlCb *ulCb)
939 {
940    uint8_t lcIdx = 0;
941
942    for(lcIdx = delIdx; lcIdx < ulCb->numUlLc; lcIdx++)
943    {
944       /* Moving the array element to one step ahead */
945       memcpy(&ulCb->lcCb[lcIdx], &ulCb->lcCb[lcIdx+1], sizeof(UlLcCb));
946       memset(&ulCb->lcCb[lcIdx+1], 0, sizeof(UlLcCb));
947    }
948 }
949
950 /*******************************************************************
951  *
952  * @brief Update UeDlCb Lc List
953  *
954  * @details
955  *
956  *    Function : updateMacDlCb
957  *
958  *    Functionality: Update UeDlCb Lc List
959  *
960  * @params[in] delIdx, UeDlCb pointer
961  * @return void
962  *
963  * ****************************************************************/
964
965 void updateMacDlCb(uint8_t delIdx, UeDlCb *dlCb)
966 {
967    uint8_t lcIdx = 0;
968
969    for(lcIdx = delIdx; lcIdx < dlCb->numDlLc; lcIdx++)
970    {
971       /* Moving the array element to one step ahead */
972       memcpy(&dlCb->lcCb[lcIdx], &dlCb->lcCb[lcIdx+1], sizeof(DlLcCb));
973       memset(&dlCb->lcCb[lcIdx+1], 0, sizeof(DlLcCb));
974    }
975 }
976
977 /*******************************************************************
978  *
979  * @brief Fills Logical channel Cfg List to Add/Mod/Del
980  *
981  * @details
982  *
983  *    Function : fillMacLcCfgList
984  *
985  *    Functionality: Fills Logical channel Cfg List to Add/Mod/Del
986  *
987  * @params[in] MAC UE Cb Cfg , MAC UE Configuration
988  * @return ROK     - success
989  *         RFAILED - failure
990  *
991  * ****************************************************************/
992
993 uint8_t fillMacLcCfgList(MacUeCb *ueCb, MacUeCfg *ueCfg)
994 {
995    uint8_t lcIdx, ueLcIdx;
996
997    for(lcIdx = 0; lcIdx < ueCfg->numLcs; lcIdx++)
998    {
999       if(ueCb->dlInfo.numDlLc < MAX_NUM_LC)
1000       {
1001          if(ueCfg->lcCfgList[lcIdx].configType == CONFIG_ADD)
1002          {
1003             /*Filling DL LC CB */
1004             ueCb->dlInfo.lcCb[ueCb->dlInfo.numDlLc].lcId = ueCfg->lcCfgList[lcIdx].lcId;
1005             ueCb->dlInfo.lcCb[ueCb->dlInfo.numDlLc].lcState = MAC_LC_STATE_ACTIVE;
1006             ueCb->dlInfo.numDlLc++;
1007             /*Filling UL LC CB */
1008             ueCb->ulInfo.lcCb[ueCb->ulInfo.numUlLc].lcId = ueCfg->lcCfgList[lcIdx].lcId;
1009             ueCb->ulInfo.lcCb[ueCb->ulInfo.numUlLc].lcGrpId = ueCfg->lcCfgList[lcIdx].ulLcCfg.lcGroup;
1010             ueCb->ulInfo.lcCb[ueCb->ulInfo.numUlLc].lcActive = MAC_LC_STATE_ACTIVE;
1011             ueCb->ulInfo.numUlLc++;
1012          }/*End of Add Config */
1013          else
1014          { 
1015             //searching for Lc to be Mod
1016             for(ueLcIdx = 0; ueLcIdx < ueCb->ulInfo.numUlLc; ueLcIdx++)
1017             {
1018                if(ueCb->ulInfo.lcCb[ueLcIdx].lcId == ueCfg->lcCfgList[lcIdx].lcId)
1019                {
1020                   if(ueCfg->lcCfgList[lcIdx].configType == CONFIG_MOD)
1021                   {
1022                      /*Nothing to Modify in DL LC CB */
1023                      /*Modify UL LC CB */
1024                      ueCb->ulInfo.lcCb[ueLcIdx].lcGrpId = ueCfg->lcCfgList[lcIdx].ulLcCfg.lcGroup;
1025                      DU_LOG("\nMAC: Successfully Modified LC context for lcId[%d]", ueCfg->lcCfgList[lcIdx].lcId);
1026                      break;
1027                   }
1028                   if(ueCfg->lcCfgList[lcIdx].configType == CONFIG_DEL)
1029                   {
1030                      memset(&ueCb->dlInfo.lcCb[ueLcIdx], 0, sizeof(DlLcCb));
1031                      (ueCb->dlInfo.numDlLc)--;
1032                      updateMacDlCb(ueLcIdx, &ueCb->dlInfo);
1033
1034                      memset(&ueCb->ulInfo.lcCb[ueLcIdx], 0, sizeof(UlLcCb));
1035                      (ueCb->ulInfo.numUlLc)--;
1036                      updateMacUlCb(ueLcIdx, &ueCb->ulInfo);
1037                      DU_LOG("\nMAC: Successfully Deleted LC context for lcId[%d]", ueCfg->lcCfgList[lcIdx].lcId);
1038                      break;
1039                   }
1040                }
1041             }
1042          }/*End of Mod Config */
1043       }
1044    }
1045    return ROK;
1046 }
1047
1048 /*******************************************************************
1049  *
1050  * @brief Fills MAC UE Cb Cfg
1051  *
1052  * @details
1053  *
1054  *    Function : fillMacUeCb
1055  *
1056  *    Functionality: Fills MAC UE Cb Cfg
1057  *
1058  * @params[in] MAC UE Cb Cfg , MAC UE Configuration
1059  *             cellIdx
1060  * @return ROK     - success
1061  *         RFAILED - failure
1062  *
1063  * ****************************************************************/
1064
1065 uint8_t fillMacUeCb(MacUeCb *ueCb, MacUeCfg *ueCfg, uint8_t cellIdx)
1066 {
1067    uint8_t ret = ROK;
1068
1069    ueCb->ueIdx = ueCfg->ueIdx;
1070    ueCb->crnti = ueCfg->crnti;
1071    ueCb->cellCb = macCb.macCell[cellIdx];
1072    ueCb->dlInfo.dlHarqEnt.numHarqProcs = \
1073       ueCfg->spCellCfg.servCellCfg.pdschServCellCfg.numHarqProcForPdsch; 
1074    ueCb->state = UE_STATE_ACTIVE;
1075    /*TODO: To check the bsr value during implementation */
1076    ueCb->bsrTmrCfg.periodicTimer = ueCfg->macCellGrpCfg.bsrTmrCfg.periodicTimer;
1077    ueCb->bsrTmrCfg.retxTimer     = ueCfg->macCellGrpCfg.bsrTmrCfg.retxTimer;
1078    ueCb->bsrTmrCfg.srDelayTimer  = ueCfg->macCellGrpCfg.bsrTmrCfg.srDelayTimer;
1079    ret = fillMacLcCfgList(ueCb, ueCfg);
1080    if(ret == RFAILED)
1081    {
1082       DU_LOG("\nMAC: Failed while filing MAC LC List at fillMacUeCb()");
1083    }
1084    return ret;
1085 }
1086
1087 /*******************************************************************
1088  *
1089  * @brief Function to update Mac Ra Cb
1090  *
1091  * @details
1092  *
1093  *    Function : updateMacRaCb
1094  *
1095  *    Functionality: Function to update Mac Ra Cb
1096  *
1097  * @params[in] cellIdx, Mac Ue Cb
1098  * @return ROK     - success
1099  *         RFAILED - failure
1100  *
1101  * ****************************************************************/
1102
1103 uint8_t updateMacRaCb(uint16_t cellIdx, MacUeCb *ueCb)
1104 {
1105    uint8_t ueIdx;
1106    /* Copy RA Cb */
1107    for(ueIdx = 0; ueIdx < MAX_NUM_UE; ueIdx++)
1108    {
1109       if(macCb.macCell[cellIdx]->macRaCb[ueIdx].crnti == ueCb->crnti)
1110       {
1111          ueCb->raCb = &macCb.macCell[cellIdx]->macRaCb[ueIdx];
1112          break;
1113       }
1114    }
1115    return ROK;
1116 }
1117
1118 /*******************************************************************
1119  *
1120  * @brief Function to delete Mac Ra Cb
1121  *
1122  * @details
1123  *
1124  *    Function : deleteMacRaCb
1125  *
1126  *    Functionality: Function to delete Mac Ra Cb
1127  *
1128  * @params[in] cellIdx, Mac Ue Cb
1129  * @return void
1130  *
1131  * ****************************************************************/
1132
1133 void deleteMacRaCb(uint16_t cellIdx, MacUeCb *ueCb)
1134 {
1135    uint8_t ueIdx;
1136
1137    for(ueIdx = 0; ueIdx < MAX_NUM_UE; ueIdx++)
1138    {
1139       if(macCb.macCell[cellIdx]->macRaCb[ueIdx].crnti == ueCb->crnti)
1140       {
1141          if(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4Pdu)
1142          {
1143            MAC_FREE(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4Pdu, \
1144                      macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4PduLen);
1145          }
1146          if(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4TxPdu)
1147          {
1148             MAC_FREE(macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4TxPdu, \
1149                       macCb.macCell[cellIdx]->macRaCb[ueIdx].msg4TbSize);
1150          }
1151          memset(&macCb.macCell[cellIdx]->macRaCb[ueIdx], 0, sizeof(MacRaCbInfo));
1152          break;
1153       }
1154    }
1155                   
1156 }
1157
1158 /*******************************************************************
1159  *
1160  * @brief Creates UE Cb
1161  *
1162  * @details
1163  *
1164  *    Function : createUeCb
1165  *
1166  *    Functionality: Creates UE Cb
1167  *
1168  * @params[in] MAC UE Configuration
1169  * @return ROK     - success
1170  *         RFAILED - failure
1171  *
1172  * ****************************************************************/
1173 uint8_t createUeCb(uint8_t cellIdx, MacUeCb *ueCb, MacUeCfg *ueCfg)
1174 {
1175    uint8_t ret =ROK;
1176
1177    if((ueCb->ueIdx == ueCfg->ueIdx) && (ueCb->crnti == ueCfg->crnti)\
1178       &&(ueCb->state == UE_STATE_ACTIVE))
1179    {
1180       DU_LOG("\n MAC : CRNTI %d already configured ", ueCfg->crnti);
1181       return ROKDUP;
1182    }
1183    else
1184    {
1185       memset(ueCb, 0, sizeof(MacUeCb));
1186       ret = fillMacUeCb(ueCb, ueCfg, cellIdx);
1187       if(ret != ROK)
1188       {
1189          DU_LOG("\nMAC : Failed to create Ue Cb at createUeCb()");
1190          return ret;
1191       }
1192       else
1193       {
1194          macCb.macCell[cellIdx]->numActvUe++;
1195          updateMacRaCb(cellIdx, ueCb);
1196          return ROK;
1197       }
1198
1199    }
1200
1201 }
1202
1203 /*******************************************************************
1204  *
1205  * @brief Modify UE Cb Cfg
1206  *
1207  * @details
1208  *
1209  *    Function : modifyUeCb
1210  *
1211  *    Functionality: modify UE Cb
1212  *
1213  * @params[in] MAC UE Configuration
1214  * @return ROK     - success
1215  *         RFAILED - failure
1216  *
1217  * ****************************************************************/
1218 uint8_t modifyUeCb(uint8_t cellIdx, MacUeCb *ueCb, MacUeCfg *ueCfg)
1219 {
1220    uint8_t ret = ROK;
1221
1222    if((ueCb->ueIdx == ueCfg->ueIdx) && (ueCb->crnti == ueCfg->crnti)\
1223       &&(ueCb->state == UE_STATE_ACTIVE))
1224    {
1225       DU_LOG("\n MAC : Reconfig Req received for CRNTI %d ", ueCfg->crnti);
1226       ret = fillMacUeCb(ueCb, ueCfg, cellIdx);
1227       if(ret != ROK)
1228       {
1229          DU_LOG("\nMAC : Failed to modify Ue Cb at modifyUeCb()");
1230          return ret;
1231       }
1232       else
1233       {
1234          deleteMacRaCb(cellIdx, ueCb);
1235          return ROK;
1236       }
1237    }
1238    return RFAILED;
1239 }
1240
1241
1242 /*******************************************************************
1243  *
1244  * @brief Creates UE Cb and fills ueCfg
1245  *
1246  * @details
1247  *
1248  *    Function : procMacUeCfgData
1249  *
1250  *    Functionality: Creates UE Cb and fills ueCfg
1251  *
1252  * @params[in] MAC UE Configuration
1253  * @return ROK     - success
1254  *         RFAILED - failure
1255  *
1256  * ****************************************************************/
1257
1258 uint8_t procMacUeCfgData(Pst *pst, MacUeCfg *ueCfg)
1259 {
1260    uint8_t ret = ROK;
1261    uint16_t  cellIdx;
1262    MacUeCb   *ueCb = NULLP;
1263
1264
1265    GET_CELL_IDX(ueCfg->cellId, cellIdx);
1266
1267    /* Validate cell id */
1268    if(macCb.macCell[cellIdx]->cellId != ueCfg->cellId)
1269    {
1270       DU_LOG("\nMAC : Cell Id %d not configured", ueCfg->cellId);
1271       return RFAILED;
1272    }
1273
1274    /* Check if max number of UE configured */
1275    if(macCb.macCell[cellIdx]->numActvUe > MAX_NUM_UE)
1276    {
1277       DU_LOG("MAC : Max number of UE [%d] already configured", MAX_NUM_UE);
1278       return RFAILED;
1279    }
1280
1281    /* Check if UE already configured */
1282    ueCb = &macCb.macCell[cellIdx]->ueCb[ueCfg->ueIdx -1];
1283    switch(pst->event)
1284    {
1285       case EVENT_UE_CREATE_RSP_TO_MAC:
1286          ret = createUeCb(cellIdx, ueCb, ueCfg);
1287          break;
1288       case EVENT_UE_RECONFIG_RSP_TO_MAC:
1289          ret = modifyUeCb(cellIdx, ueCb, ueCfg);
1290          break;
1291       default:
1292          break;
1293    }
1294
1295    return ret;
1296 }
1297
1298 /*******************************************************************
1299  *
1300  * @brief Function to store the UeCfg Data 
1301  *
1302  * @details
1303  *
1304  *    Function : copyToTmpData
1305  *
1306  *    Functionality: Function to store the UeCfg Data
1307  *
1308  * @params[in] MacUeCfg pointer 
1309  * @return ROK     - success
1310  *         RFAILED - failure
1311  *
1312  * ****************************************************************/
1313
1314 uint8_t copyToTmpData(MacUeCfg *ueCfg)
1315 {
1316    uint8_t cellIdx;
1317    MacUeCfg *tmpData = NULLP;
1318
1319    MAC_ALLOC(tmpData, sizeof(MacUeCfg));
1320    if(!tmpData)
1321    {
1322       DU_LOG("\nMAC: Memory Alloc Failed at copyToTmpData()");
1323       return RFAILED;
1324    }
1325    memcpy(tmpData, ueCfg, sizeof(MacUeCfg));
1326    GET_CELL_IDX(ueCfg->cellId, cellIdx);
1327    macCb.macCell[cellIdx]->ueCfgTmpData[ueCfg->ueIdx-1] = tmpData;
1328    return ROK;
1329 }
1330
1331 /*******************************************************************
1332  *
1333  * @brief Handles UE create requst from DU APP
1334  *
1335  * @details
1336  *
1337  *    Function : MacProcUeCreateReq
1338  *
1339  *    Functionality: Handles UE create requst from DU APP
1340  *
1341  * @params[in] 
1342  * @return ROK     - success
1343  *         RFAILED - failure
1344  *
1345  * ****************************************************************/
1346 uint8_t MacProcUeCreateReq(Pst *pst, MacUeCfg *ueCfg)
1347 {
1348    uint8_t ret = ROK;
1349    SchUeCfg   schUeCfg;
1350    memset(&schUeCfg, 0, sizeof(SchUeCfg));
1351
1352    DU_LOG("\nMAC : UE Create Request for CRNTI[%d]", ueCfg->crnti);
1353
1354    if(ueCfg)
1355    {
1356       /*Storing received ueCfg in ueCfgTmpData */
1357       ret = copyToTmpData(ueCfg);
1358       if(ret == ROK)
1359       {
1360          /*Sending Cfg Req to SCH */
1361          ret = fillSchUeCfg(pst, &schUeCfg, ueCfg);
1362          if(ret != ROK)
1363             DU_LOG("\nMAC : Failed to fill Sch Ue Cfg at MacProcUeCreateReq()");
1364          else
1365          {
1366             /* Fill event and send UE create request to SCH */
1367             ret = sendUeReqToSch(pst, &schUeCfg);
1368             if(ret != ROK)
1369                DU_LOG("\nMAC : Failed to send UE Create request to SCH");
1370          }
1371       }
1372       else 
1373       {
1374          DU_LOG("\nMAC : Failed to store MAC UE CFG ");
1375       }
1376    }
1377    else
1378    {
1379       DU_LOG("\nMAC : MAC UE Create request processing failed");
1380       ret = RFAILED;
1381    }
1382    /* FREE shared memory */
1383    MAC_FREE_SHRABL_BUF(pst->region, pst->pool, ueCfg, sizeof(MacUeCfg));
1384
1385    return ret;
1386 }
1387
1388 /*******************************************************************
1389  *
1390  * @brief Fill and Send UE create response from MAC to DU APP
1391  *
1392  * @details
1393  *
1394  *    Function : MacSendUeCreateRsp
1395  *
1396  *    Functionality: Fill and Send UE create response from MAC to DUAPP
1397  *
1398  * @params[in] MAC UE create result
1399  *             SCH UE create response
1400  * @return ROK     - success
1401  *         RFAILED - failure
1402  *
1403  * ****************************************************************/
1404 uint8_t MacSendUeCreateRsp(MacRsp result, SchUeCfgRsp *schCfgRsp)
1405 {
1406    MacUeCfgRsp   *cfgRsp;
1407    Pst        rspPst;
1408
1409    MAC_ALLOC_SHRABL_BUF(cfgRsp, sizeof(MacUeCfgRsp));
1410    if(!cfgRsp)
1411    {
1412       DU_LOG("\nMAC: Memory allocation for UE config response failed");
1413       return RFAILED;
1414    }
1415
1416    /* Filling UE Config response */
1417    memset(cfgRsp, 0, sizeof(MacUeCfgRsp));
1418    cfgRsp->cellId = schCfgRsp->cellId;
1419    cfgRsp->ueIdx = schCfgRsp->ueIdx;
1420    cfgRsp->result = result;
1421
1422    /* Fill Post structure and send UE Create response*/
1423    memset(&rspPst, 0, sizeof(Pst));
1424    FILL_PST_MAC_TO_DUAPP(rspPst, EVENT_MAC_UE_CREATE_RSP);
1425    return MacDuUeCfgRspOpts[rspPst.selector](&rspPst, cfgRsp); 
1426 }
1427
1428 /*******************************************************************
1429  *
1430  * @brief Fill and Send UE Reconfig response from MAC to DU APP
1431  *
1432  * @details
1433  *
1434  *    Function : MacSendUeReconfigRsp
1435  *
1436  *    Functionality: Fill and Send UE Reconfig response from MAC to DUAPP
1437  *
1438  * @params[in] MAC UE create result
1439  *             SCH UE create response
1440  * @return ROK     - success
1441  *         RFAILED - failure
1442  *
1443  * ****************************************************************/
1444 uint8_t MacSendUeReconfigRsp(MacRsp result, SchUeCfgRsp *schCfgRsp)
1445 {
1446    MacUeCfgRsp   *cfgRsp;
1447    Pst        rspPst;
1448
1449    MAC_ALLOC_SHRABL_BUF(cfgRsp, sizeof(MacUeCfgRsp));
1450    if(!cfgRsp)
1451    {
1452       DU_LOG("\nMAC: Memory allocation for UE Reconfig response failed");
1453       return RFAILED;
1454    }
1455
1456    /* Filling UE Config response */
1457    memset(cfgRsp, 0, sizeof(MacUeCfgRsp));
1458    cfgRsp->cellId = schCfgRsp->cellId;
1459    cfgRsp->ueIdx = schCfgRsp->ueIdx;
1460    cfgRsp->result = result;
1461
1462    /* Fill Post structure and send UE Create response*/
1463    memset(&rspPst, 0, sizeof(Pst));
1464    FILL_PST_MAC_TO_DUAPP(rspPst, EVENT_MAC_UE_RECONFIG_RSP);
1465    return MacDuUeCfgRspOpts[rspPst.selector](&rspPst, cfgRsp);
1466 }
1467
1468 /*******************************************************************
1469  *
1470  * @brief  Function to return Mac Ue Cfg pointer
1471  *
1472  * @details
1473  *
1474  *    Function : getMacUeCfg
1475  *
1476  *    Functionality:
1477  *      Function to return Mac Ue Cfg pointer
1478  *
1479  * @params[in] cellIdx, ueIdx
1480  *
1481  * @return MacUeCfg pointer - success
1482  *         NULLP - failure
1483  *
1484  * ****************************************************************/
1485
1486 MacUeCfg *getMacUeCfg(uint16_t cellIdx, uint8_t ueIdx)
1487 {
1488    MacUeCfg *ueCfg = NULLP;
1489    if(macCb.macCell[cellIdx])
1490    {
1491       ueCfg = macCb.macCell[cellIdx]->ueCfgTmpData[ueIdx-1];
1492    }
1493    else
1494    {
1495       DU_LOG("\nMAC: Failed to get macCellCb in getMacUeCfg()");
1496    }
1497    return ueCfg;
1498 }
1499
1500 /*******************************************************************
1501  *
1502  * @brief  Processes UE create response from scheduler
1503  *
1504  * @details
1505  *
1506  *    Function : MacProcSchUeCfgRsp
1507  *
1508  *    Functionality:
1509  *      Processes UE create response from scheduler
1510  *      Sends UE create response to DU APP
1511  *
1512  * @params[in] Pst : Post structure
1513  *             schCfgRsp : Scheduler UE cfg response
1514  * @return ROK     - success
1515  *         RFAILED - failure
1516  *
1517  * ****************************************************************/
1518 uint8_t MacProcSchUeCfgRsp(Pst *pst, SchUeCfgRsp *schCfgRsp)
1519 {
1520    uint8_t result = MAC_DU_APP_RSP_NOK;
1521    uint8_t ret = ROK;
1522    uint16_t cellIdx;
1523    MacUeCfg *ueCfg = NULLP;
1524
1525    GET_CELL_IDX(schCfgRsp->cellId, cellIdx);
1526    ueCfg = getMacUeCfg(cellIdx, schCfgRsp->ueIdx);
1527
1528    if(ueCfg)
1529    {
1530       if(schCfgRsp->rsp == RSP_NOK)
1531       {
1532          DU_LOG("\nMAC : SCH UE Config Response : FAILURE [CRNTI %d] for event %d", schCfgRsp->crnti, pst->event);
1533       }
1534       else
1535       {
1536          DU_LOG("\nMAC : SCH UE Config Response : SUCCESS [CRNTI %d]", schCfgRsp->crnti);
1537          ret = procMacUeCfgData(pst, ueCfg);
1538          if(ret == ROK)
1539          {
1540             result = MAC_DU_APP_RSP_OK;
1541          }
1542       }
1543       MAC_FREE(ueCfg, sizeof(MacUeCfg));
1544       ueCfg = NULLP;
1545    }
1546    if(pst->event == EVENT_UE_CREATE_RSP_TO_MAC)
1547    {
1548       ret = MacSendUeCreateRsp(result, schCfgRsp);
1549    }
1550    if(pst->event == EVENT_UE_RECONFIG_RSP_TO_MAC)
1551    {
1552       ret = MacSendUeReconfigRsp(result, schCfgRsp);
1553    }
1554    
1555    return ret; 
1556 }
1557
1558 /*******************************************************************
1559  *
1560  * @brief Handles UE Reconfig requst from DU APP
1561  *
1562  * @details
1563  *
1564  *    Function : MacProcUeReconfigReq
1565  *
1566  *    Functionality: Handles UE Reconfig requst from DU APP
1567  *
1568  * @params[in] 
1569  * @return ROK     - success
1570  *         RFAILED - failure
1571  *
1572  * ****************************************************************/
1573 uint8_t MacProcUeReconfigReq(Pst *pst, MacUeCfg *ueCfg)
1574 {
1575    uint8_t ret = ROK;
1576    SchUeCfg   schUeCfg;
1577    memset(&schUeCfg, 0, sizeof(SchUeCfg));
1578
1579    DU_LOG("\nMAC : UE Reconfig Request for CRNTI[%d]", ueCfg->crnti);
1580
1581    if(ueCfg)
1582    {
1583       /*Storing received ueCfg in ueCfgTmpData */
1584       ret = copyToTmpData(ueCfg);
1585       if(ret == ROK)
1586       {
1587          /*Sending Cfg Req to SCH */
1588          ret = fillSchUeCfg(pst, &schUeCfg, ueCfg);
1589          if(ret != ROK)
1590             DU_LOG("\nMAC : Failed to fill sch Ue Cfg at MacProcUeReconfigReq()");
1591          else
1592          {
1593             /* Fill event and send UE create request to SCH */
1594             ret = sendUeReqToSch(pst, &schUeCfg);
1595             if(ret != ROK)
1596                DU_LOG("\nMAC : Failed to send UE Reconfig Request to SCH");
1597          }
1598       }
1599       else 
1600       {
1601          DU_LOG("\nMAC : Failed to store MAC UE Cb ");
1602       }
1603    }
1604    else
1605    {
1606       DU_LOG("\nMAC : MAC UE Create request processing failed");
1607       ret = RFAILED;
1608    }
1609    /* FREE shared memory */
1610    MAC_FREE_SHRABL_BUF(pst->region, pst->pool, ueCfg, sizeof(MacUeCfg));
1611    return ROK;
1612 }
1613
1614
1615 /**********************************************************************
1616   End of file
1617  **********************************************************************/