JIRA-ID:[ODUHIGH-295]- Tunnel creation in EGTP
[o-du/l2.git] / src / du_app / du_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 /* This file contains UE management handling functionality for DU APP */
19 #include "common_def.h"
20 #include "lrg.h"
21 #include "lrg.x"
22 #include "ckw.h"
23 #include "ckw.x"
24 #include "kwu.h"
25 #include "kwu.x"
26 #include "lkw.h"
27 #include "lkw.x"
28 #include "legtp.h"
29 #include "du_app_mac_inf.h"
30 #include "du_app_rlc_inf.h"
31 #include "du_cfg.h"
32 #include "du_utils.h"
33 #include "du_mgr.h"
34 #include "du_f1ap_msg_hdl.h"
35 #include "du_ue_mgr.h"
36
37 DuMacDlCcchInd packMacDlCcchIndOpts[] =
38 {
39    packMacDlCcchInd,   /* Loose coupling */
40    MacProcDlCcchInd,    /* TIght coupling */
41    packMacDlCcchInd    /* Light weight-loose coupling */
42 };
43
44 DuMacUeCreateReq packMacUeCreateReqOpts[] =
45 {
46    packDuMacUeCreateReq,       /* Loose coupling */
47    MacProcUeCreateReq,          /* TIght coupling */
48    packDuMacUeCreateReq       /* Light weight-loose coupling */
49 };
50
51 DuRlcUeCreateReq packRlcUeCreateReqOpts[] =
52 {
53    packDuRlcUeCreateReq,       /* Loose coupling */
54    RlcProcUeCreateReq,          /* TIght coupling */
55    packDuRlcUeCreateReq       /* Light weight-loose coupling */
56 };
57
58 DuDlRrcMsgToRlcFunc duSendDlRrcMsgToRlcOpts[] =
59 {
60    packDlRrcMsgToRlc,          /* Loose coupling */ 
61    RlcProcDlRrcMsgTransfer,       /* Tight coupling */
62    packDlRrcMsgToRlc           /* Light weight-loose coupling */
63 };
64
65 DuRlcUeReconfigReq packRlcUeReconfigReqOpts[] =
66 {
67    packDuRlcUeReconfigReq,       /* Loose coupling */
68    RlcProcUeReconfigReq,       /* TIght coupling */
69    packDuRlcUeReconfigReq       /* Light weight-loose coupling */
70 };
71
72 DuMacUeReconfigReq packMacUeReconfigReqOpts[] =
73 {
74    packDuMacUeReconfigReq,       /* Loose coupling */
75    MacProcUeReconfigReq,       /* TIght coupling */
76    packDuMacUeReconfigReq     /* Light weight-loose coupling */
77 };
78
79 #ifdef EGTP_TEST
80 uint32_t sduId = 0;
81 #endif
82
83 /*******************************************************************
84  *
85  * @brief Handles EGTP data from CU 
86  *
87  * @details
88  *
89  *    Function : duHdlEgtpData
90  *
91  *    Functionality: 
92  *      Processes EGTP header and sends data to RLC
93  *
94  * @params[in]  Pointer to EGTP Message 
95  * @return ROK     - success
96  *         RFAILED - failure
97  *
98  * ****************************************************************/
99 uint8_t duHdlEgtpDlData(EgtpMsg  *egtpMsg)
100 {
101
102    /* TODO : Extract RbId/UeID/CellID/SduId from database
103       using tunnel id in egtp header */
104
105    DU_LOG("\nDEBUG   -->  DU_APP : Processing DL data");
106 #ifdef EGTP_TEST
107    Pst pst;
108    uint8_t ret;
109    MsgLen copyLen;
110    RlcDlRrcMsgInfo  *dlRrcMsgInfo = NULLP;
111
112    DU_ALLOC_SHRABL_BUF(dlRrcMsgInfo, sizeof(RlcDlRrcMsgInfo));
113    if(!dlRrcMsgInfo)
114    {
115       DU_LOG("\nDU APP : Memory allocation failed for dlRrcMsgInfo in \
116          duBuildAndSendDlRrcMsgToRlc");
117       ODU_PUT_MSG_BUF(egtpMsg->msg);
118       return RFAILED;
119    }
120
121    /* Filling up the RRC msg info */
122    dlRrcMsgInfo->cellId = NR_CELL_ID;
123    dlRrcMsgInfo->ueIdx = UE_ID;
124    dlRrcMsgInfo->rbType = CM_LTE_DRB;
125    dlRrcMsgInfo->rbId   = RB_ID;
126    dlRrcMsgInfo->lcType = CM_LTE_LCH_DTCH;
127    dlRrcMsgInfo->lcId   = 4;
128    dlRrcMsgInfo->execDup = false;
129    dlRrcMsgInfo->deliveryStaRpt = false;
130    ODU_GET_MSG_LEN(egtpMsg->msg, &dlRrcMsgInfo->msgLen);
131    DU_ALLOC_SHRABL_BUF(dlRrcMsgInfo->rrcMsg, dlRrcMsgInfo->msgLen);
132    ODU_COPY_MSG_TO_FIX_BUF(egtpMsg->msg, 0, dlRrcMsgInfo->msgLen, dlRrcMsgInfo->rrcMsg, (MsgLen *)&copyLen);
133    ODU_PUT_MSG_BUF(egtpMsg->msg);
134
135    /* Filling post structure and sending msg */
136    FILL_PST_DUAPP_TO_RLC(pst, RLC_DL_INST, EVENT_DL_RRC_MSG_TRANS_TO_RLC);
137    DU_LOG("\nDU_APP: Sending Dl User Msg to RLC \n");
138    ret = (*duSendDlRrcMsgToRlcOpts[pst.selector])(&pst, dlRrcMsgInfo);
139    if(ret != ROK)
140    {
141       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, dlRrcMsgInfo, sizeof(RlcDlRrcMsgInfo));
142       return RFAILED;
143    }
144 #endif
145    return ROK;
146 }
147
148 /******************************************************************
149  *
150  * @brief Builds and Sends DL CCCH Ind to MAC
151  *
152  * @details
153  *
154  *    Function : duBuildAndSendDlCcchInd 
155  *
156  *    Functionality: Builds and sends DL CCCH Ind Msg to MAC
157  *
158  * @params[in] dlCcchMsg - uint8_t*
159  * @return ROK     - success
160  *         RFAILED - failure
161  *
162  * ****************************************************************/
163 uint8_t duBuildAndSendDlCcchInd(uint16_t *cellId, uint16_t *crnti, \
164       DlCcchMsgType msgType, uint16_t dlCcchMsgSize, uint8_t *dlCcchMsg)
165 {
166    uint8_t  ret                  = ROK;
167    uint16_t idx2;
168    DlCcchIndInfo *dlCcchIndInfo = NULLP;
169    Pst pst;
170
171    DU_LOG("\nDEBUG   -->  DU APP : Building and Sending DL CCCH Ind to MAC");
172
173    DU_ALLOC_SHRABL_BUF(dlCcchIndInfo, sizeof(DlCcchIndInfo));
174
175    if(!dlCcchIndInfo)
176    {
177       DU_LOG("\nERROR  -->  DU APP : Memory alloc failed while building DL CCCH Ind");
178       return RFAILED;
179    }
180
181    dlCcchIndInfo->cellId = *cellId;
182    dlCcchIndInfo->crnti = *crnti;
183    dlCcchIndInfo->msgType = msgType;
184    dlCcchIndInfo->dlCcchMsgLen = dlCcchMsgSize;
185
186    DU_ALLOC_SHRABL_BUF(dlCcchIndInfo->dlCcchMsg, dlCcchIndInfo->dlCcchMsgLen);
187    if(!dlCcchIndInfo->dlCcchMsg)
188    {
189       DU_LOG("\nERROR  -->  DU APP : Memory alloc failed while building DL CCCH Ind");
190       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, dlCcchIndInfo, sizeof(DlCcchIndInfo));
191       return RFAILED;
192    }
193    for(idx2 = 0; idx2 < dlCcchIndInfo->dlCcchMsgLen; idx2++)
194    {
195       dlCcchIndInfo->dlCcchMsg[idx2] = dlCcchMsg[idx2];
196    }
197    DU_FREE(dlCcchMsg, dlCcchMsgSize);
198
199    /* Fill Pst */
200    FILL_PST_DUAPP_TO_MAC(pst, EVENT_MAC_DL_CCCH_IND);
201    ret = (*packMacDlCcchIndOpts[pst.selector])(&pst, dlCcchIndInfo);
202    if(ret != ROK)
203    {
204       DU_LOG("\nERROR  -->  DU_APP : Failure in sending DL CCCH to MAC");
205       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, dlCcchIndInfo->dlCcchMsg,\
206             dlCcchIndInfo->dlCcchMsgLen);
207       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, dlCcchIndInfo, \
208             sizeof(DlCcchIndInfo));
209       ret = RFAILED; 
210    }
211
212    return ret;
213
214 }
215
216 /*******************************************************************
217  *
218  * @brief Build and Send DL RRC Message transfer to RLC
219  *
220  * @details
221  *
222  *    Function : duBuildAndSendDlRrcMsgToRlc
223  *
224  *    Functionality:
225  *      Build and Send DL RRC Message transfer to RLC
226  *
227  * @params[in] Cell ID
228  *             UE Index
229  *             Logical Channgel ID
230  *             RRC Message
231  *             RRC Message Length
232  * @return ROK     - success
233  *         RFAILED - failure
234  *
235  * ****************************************************************/
236 uint8_t duBuildAndSendDlRrcMsgToRlc(uint16_t cellId, RlcUeCfg ueCfg, F1DlRrcMsg *f1DlRrcMsg)
237 {
238    Pst     pst;
239    uint8_t ret;
240    uint8_t lcIdx;
241    RlcDlRrcMsgInfo  *dlRrcMsgInfo = NULLP;
242
243    if(!f1DlRrcMsg)
244    {
245       DU_LOG("\nERROR  -->  DU APP : Received Dl RRC Msg is NULL at duBuildAndSendDlRrcMsgToRlc()");
246       return RFAILED;
247    }
248
249    DU_ALLOC_SHRABL_BUF(dlRrcMsgInfo, sizeof(RlcDlRrcMsgInfo));
250    if(!dlRrcMsgInfo)
251    {
252       DU_LOG("\nERROR  -->  DU APP : Memory allocation failed for dlRrcMsgInfo in \
253          duBuildAndSendDlRrcMsgToRlc");
254       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, f1DlRrcMsg->rrcMsgPdu, f1DlRrcMsg->rrcMsgSize);
255       return RFAILED;
256    }
257    
258    /* Filling up the RRC msg info */
259    dlRrcMsgInfo->cellId = cellId;
260    dlRrcMsgInfo->ueIdx = ueCfg.ueIdx;
261    for(lcIdx = 0; lcIdx <= MAX_NUM_LC; lcIdx++)
262    {
263       if(ueCfg.rlcLcCfg[lcIdx].lcId == f1DlRrcMsg->srbId)
264       {
265          dlRrcMsgInfo->rbType = ueCfg.rlcLcCfg[lcIdx].rbType;
266          dlRrcMsgInfo->rbId   = ueCfg.rlcLcCfg[lcIdx].rbId;
267          dlRrcMsgInfo->lcType = ueCfg.rlcLcCfg[lcIdx].lcType;
268          dlRrcMsgInfo->lcId   = ueCfg.rlcLcCfg[lcIdx].lcId;
269          break;
270       }
271    }
272    dlRrcMsgInfo->execDup = f1DlRrcMsg->execDup;
273    dlRrcMsgInfo->deliveryStaRpt = f1DlRrcMsg->deliveryStatRpt;
274    dlRrcMsgInfo->msgLen = f1DlRrcMsg->rrcMsgSize;
275    dlRrcMsgInfo->rrcMsg = f1DlRrcMsg->rrcMsgPdu;
276
277    /* Filling post structure and sending msg */ 
278    FILL_PST_DUAPP_TO_RLC(pst, RLC_DL_INST, EVENT_DL_RRC_MSG_TRANS_TO_RLC);
279    DU_LOG("\nDEBUG   -->  DU_APP: Sending Dl RRC Msg to RLC \n");
280    ret = (*duSendDlRrcMsgToRlcOpts[pst.selector])(&pst, dlRrcMsgInfo);
281    if(ret != ROK)
282    {
283       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, f1DlRrcMsg->rrcMsgPdu, f1DlRrcMsg->rrcMsgSize);
284       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, dlRrcMsgInfo, sizeof(RlcDlRrcMsgInfo));
285       return RFAILED;
286    }
287
288    return ROK;
289
290
291
292
293 /******************************************************************
294  *
295  * @brief Process DL RRC Msg recevied from F1AP
296  *
297  * @details
298  *
299  *    Function : duProcDlRrcMsg
300  *
301  *    Functionality: Process DL RRC Msg recevied from F1AP
302  *
303  * @params[in] dlCcchMsg - uint8_t*
304  * @return ROK     - success
305  *         RFAILED - failure
306  *
307  * ****************************************************************/
308
309 uint8_t duProcDlRrcMsg(F1DlRrcMsg *dlRrcMsg)
310 {
311    uint8_t ueIdx, ret;
312    uint16_t crnti, cellId, cellIdx;
313    bool ueCcchCtxtFound = false;
314    bool ueFound = false;
315
316    ret = ROK;
317
318    if(dlRrcMsg->srbId == SRB0_LCID) //RRC connection setup
319    {
320       for(ueIdx=0; ueIdx<duCb.numUe; ueIdx++)
321       {
322          if(dlRrcMsg->gnbDuUeF1apId == duCb.ueCcchCtxt[ueIdx].gnbDuUeF1apId)
323          {
324             ueCcchCtxtFound = true;
325             crnti  = duCb.ueCcchCtxt[ueIdx].crnti;
326             cellId = duCb.ueCcchCtxt[ueIdx].cellId;
327             break;
328          }
329       }
330    }
331    if(ueCcchCtxtFound)
332    {
333       ret = duBuildAndSendDlCcchInd(&cellId, &crnti, RRC_SETUP, dlRrcMsg->rrcMsgSize, dlRrcMsg->rrcMsgPdu);
334       if(ret == RFAILED)
335       {
336          DU_LOG("\nERROR  -->  DU APP : Failed to build DlCcch Ind at procDlRrcMsgTrans()");
337       }
338       else
339       {
340          if(duCb.actvCellLst[cellId-1]->numActvUes < MAX_NUM_UE)
341          {
342             ret = duCreateUeCb(&duCb.ueCcchCtxt[ueIdx], dlRrcMsg->gnbCuUeF1apId);
343             if(ret == RFAILED)
344             {
345                DU_LOG("\nERROR  -->  DU APP : Failed to createUeCb for cellId [%d] at procDlRrcMsgTrans()", \
346                      duCb.ueCcchCtxt[ueIdx].cellId);
347             }
348          }
349          else
350          {
351             DU_LOG("\nERROR   -->  DU_APP: Max Active UEs has reached at procDlRrcMsgTrans()");
352             ret = RFAILED;
353          }
354       }
355    }
356    else
357    {
358       for(cellIdx = 0; cellIdx < MAX_NUM_CELL; cellIdx++)
359       {
360          for(ueIdx = 0 ; ueIdx < MAX_NUM_UE; ueIdx++)
361          {
362             if((dlRrcMsg->gnbCuUeF1apId == duCb.actvCellLst[cellIdx]->ueCb[ueIdx].gnbCuUeF1apId)
363                   && (dlRrcMsg->gnbDuUeF1apId == duCb.actvCellLst[cellIdx]->ueCb[ueIdx].gnbDuUeF1apId))
364             {
365                ueFound = true;
366                ret = duBuildAndSendDlRrcMsgToRlc(duCb.actvCellLst[cellIdx]->cellId, \
367                      duCb.actvCellLst[cellIdx]->ueCb[ueIdx].rlcUeCfg, dlRrcMsg);
368                break; 
369             }
370          }
371          if(ueFound)
372             break;
373       }
374       if(!ueFound)
375          ret = RFAILED;
376    }
377    return ret;
378 }
379
380 /******************************************************************
381  *
382  * @brief Generates GNB DU Ue F1AP ID
383  *
384  * @details
385  *
386  *    Function : genGnbDuUeF1apId
387  *
388  *    Functionality: Generates GNB DU Ue F1AP ID
389  *
390  * @params[in] void
391  * @return gnbDuF1apId
392  *
393  * ****************************************************************/
394 uint32_t genGnbDuUeF1apId()
395 {
396    static uint32_t gnbDuUeF1apId = 0;
397
398    return ++gnbDuUeF1apId;
399 }
400
401 /******************************************************************
402  *
403  * @brief Processes UL CCCH Ind recvd from MAC
404  *
405  * @details
406  *
407  *    Function : duProcUlCcchInd
408  *
409  *    Functionality: Processes UL CCCH Ind recvd from MAC
410  *
411  * @params[in] UlCcchIndInfo *ulCcchIndInfo
412  * @return ROK     - success
413  *         RFAILED - failure
414  *
415  * ****************************************************************/
416 uint8_t duProcUlCcchInd(UlCcchIndInfo *ulCcchIndInfo)
417 {
418
419    uint8_t ret = ROK;
420    uint32_t gnbDuUeF1apId = 0;
421
422    gnbDuUeF1apId = genGnbDuUeF1apId();
423
424    /* Store Ue mapping */
425    duCb.ueCcchCtxt[duCb.numUe].gnbDuUeF1apId = gnbDuUeF1apId;
426    duCb.ueCcchCtxt[duCb.numUe].crnti         = ulCcchIndInfo->crnti;
427    duCb.ueCcchCtxt[duCb.numUe].cellId        = ulCcchIndInfo->cellId;
428
429    duCb.numUe++;
430
431    ret = (BuildAndSendInitialRrcMsgTransfer(gnbDuUeF1apId, ulCcchIndInfo->crnti, ulCcchIndInfo->ulCcchMsgLen,
432             ulCcchIndInfo->ulCcchMsg));
433    if(ret != ROK)
434    {
435       DU_LOG("\nERROR  -->  DU_APP : BuildAndSendInitialRrcMsgTransfer failed");
436    }
437
438    DU_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, ulCcchIndInfo->ulCcchMsg, ulCcchIndInfo->ulCcchMsgLen);
439    DU_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, ulCcchIndInfo, sizeof(UlCcchIndInfo));
440
441    return ret;
442
443 }
444
445 /******************************************************************
446  *
447  * @brief Fills Default UL LC Cfg
448  *
449  * @details
450  *
451  *    Function : fillDefaultUlLcCfg
452  *
453  *    Functionality: Fills Default UL LC Cfg
454  *
455  * @params[in]  UlLcCfg *ulLcCfg 
456  * @return void
457  *****************************************************************/
458 void fillDefaultUlLcCfg(UlLcCfg *ulLcCfg)
459 {
460    ulLcCfg->priority = LC_PRIORITY_1;
461    ulLcCfg->lcGroup =  0;
462    ulLcCfg->schReqId = 0;
463    ulLcCfg->pbr = PBR_KBPS_INFINITY;
464    ulLcCfg->bsd = BSD_MS_1000;
465 }
466
467 /******************************************************************
468  *
469  * @brief Fills Initial DL Bandwidth Part
470  *
471  * @details
472  *
473  *    Function : fillDefaultInitDlBwp
474  *
475  *    Functionality: Fills Initial DL Bandwidth Part
476  *
477  * @params[in]  InitialDlBwp *initDlBwp
478  * @return void
479  *
480  *****************************************************************/
481 void fillDefaultInitDlBwp(InitialDlBwp *initDlBwp)
482 {
483    uint8_t idx = 0;
484    uint8_t freqDomainResource[FREQ_DOM_RSRC_SIZE] = {0};
485    uint8_t coreset0EndPrb, coreset1StartPrb, coreset1NumPrb;
486
487
488    if(initDlBwp)
489    {
490       /* Filling PDCCH Config */
491       initDlBwp->pdcchPresent = TRUE;
492       if(initDlBwp->pdcchPresent)
493       {
494          initDlBwp->pdcchCfg.numCRsetToAddMod = PDCCH_CTRL_RSRC_SET_ONE_ID;
495          memset(initDlBwp->pdcchCfg.cRSetToAddModList, 0, MAX_NUM_CRSET);
496          if(initDlBwp->pdcchCfg.numCRsetToAddMod <= MAX_NUM_CRSET)
497          {
498             initDlBwp->pdcchCfg.cRSetToAddModList[idx].cRSetId = \
499                PDCCH_CTRL_RSRC_SET_ONE_ID;
500             memset(initDlBwp->pdcchCfg.cRSetToAddModList[idx].freqDomainRsrc, 0,\
501                FREQ_DOM_RSRC_SIZE); 
502             coreset0EndPrb = CORESET0_END_PRB;
503             coreset1StartPrb = coreset0EndPrb +6;
504             coreset1NumPrb = CORESET1_NUM_PRB;
505             /* calculate the PRBs */
506             freqDomRscAllocType0(((coreset1StartPrb)/6), (coreset1NumPrb/6), freqDomainResource);
507             memcpy(initDlBwp->pdcchCfg.cRSetToAddModList[idx].freqDomainRsrc, freqDomainResource,
508                FREQ_DOM_RSRC_SIZE);
509
510             initDlBwp->pdcchCfg.cRSetToAddModList[idx].duration = \
511                PDCCH_CTRL_RSRC_SET_ONE_DURATION;
512             initDlBwp->pdcchCfg.cRSetToAddModList[idx].cceRegMappingType = \
513                CCE_REG_MAPPINGTYPE_PR_NONINTERLEAVED;
514             initDlBwp->pdcchCfg.cRSetToAddModList[idx].precoderGranularity = \
515                ALL_CONTIGUOUS_RBS;
516             initDlBwp->pdcchCfg.cRSetToAddModList[idx].dmrsScramblingId = \
517                SCRAMBLING_ID;
518          }
519          initDlBwp->pdcchCfg.numCRsetToRel = 0;
520          /* Filling Serach Space */
521          initDlBwp->pdcchCfg.numSearchSpcToAddMod = PDCCH_CTRL_RSRC_SET_ONE_ID;
522          memset(initDlBwp->pdcchCfg.searchSpcToAddModList, 0, MAX_NUM_CRSET);
523          if(initDlBwp->pdcchCfg.numSearchSpcToAddMod <= MAX_NUM_CRSET)
524          {
525             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].searchSpaceId =\
526                PDCCH_SRCH_SPC_TWO_ID;
527             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].cRSetId = \
528                PDCCH_CTRL_RSRC_SET_ONE_ID;
529             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].\
530                mSlotPeriodicityAndOffset = SLOTPERIODICITYANDOFFSET_PR_SL1;
531             memset(initDlBwp->pdcchCfg.searchSpcToAddModList[idx].mSymbolsWithinSlot, 0,\
532                MONITORING_SYMB_WITHIN_SLOT_SIZE);
533             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].mSymbolsWithinSlot[idx] =\
534                PDCCH_SYMBOL_WITHIN_SLOT;
535             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].numCandidatesAggLevel1 =\
536                AGGREGATIONLEVEL_N8; 
537             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].numCandidatesAggLevel2 =\
538                AGGREGATIONLEVEL_N8; 
539             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].numCandidatesAggLevel4 =\
540                AGGREGATIONLEVEL_N4; 
541             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].numCandidatesAggLevel8 =\
542                AGGREGATIONLEVEL_N2; 
543             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].numCandidatesAggLevel16 =\
544                AGGREGATIONLEVEL_N1;
545             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].searchSpaceType = \
546                SEARCHSPACETYPE_PR_UE_SPECIFIC;
547             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].ueSpecificDciFormat =\
548                PDCCH_SRCH_SPC_TWO_UE_SPEC_DCI_FORMAT;
549
550             initDlBwp->pdcchCfg.numSearchSpcToRel = 0;
551
552          }
553       }
554       /* Filling PDSCH Config */
555       initDlBwp->pdschPresent = TRUE;
556       if(initDlBwp->pdschPresent)
557       {
558          initDlBwp->pdschCfg.dmrsDlCfgForPdschMapTypeA.addPos = ADDITIONALPOSITION_POS0;
559          initDlBwp->pdschCfg.resourceAllocType = RESOURCEALLOCATION_TYPE1;
560          initDlBwp->pdschCfg.numTimeDomRsrcAlloc = 1;
561          initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].mappingType = \
562             MAPPING_TYPEA;
563          initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].startSymbol = PDSCH_START_SYMBOL; 
564          initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].symbolLength = PDSCH_LENGTH_SYMBOL;
565          initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].startSymbolAndLength = \
566             calcSliv(PDSCH_START_SYMBOL, PDSCH_LENGTH_SYMBOL);
567          initDlBwp->pdschCfg.rbgSize = RBG_SIZE_CONFIG1;
568          initDlBwp->pdschCfg.numCodeWordsSchByDci = CODEWORDS_SCHED_BY_DCI_N1;
569          initDlBwp->pdschCfg.bundlingType = TYPE_STATIC_BUNDLING;
570          initDlBwp->pdschCfg.bundlingInfo.StaticBundling.size = 0;
571       }
572    }
573
574 }
575
576 /******************************************************************
577  *
578  * @brief Fills Initial UL Bandwidth Part
579  *
580  * @details
581  *
582  *    Function : fillDefaultInitUlBwp
583  *
584  *    Functionality: Fills Initial UL Bandwidth Part
585  *
586  * @params[in]  InitialUlBwp *initUlBwp
587  * @return void
588  *
589  *****************************************************************/
590 void fillDefaultInitUlBwp(InitialUlBwp *initUlBwp)
591 {
592    uint8_t idx;
593    if(initUlBwp)
594    {
595       initUlBwp->pucchPresent = FALSE;
596
597       /*Filling PUSCH Config */
598       initUlBwp->puschPresent = TRUE;
599       if(initUlBwp->puschPresent)
600       {
601          initUlBwp->puschCfg.dataScramblingId = SCRAMBLING_ID;
602          initUlBwp->puschCfg.dmrsUlCfgForPuschMapTypeA.addPos = ADDITIONALPOSITION_POS0; 
603          initUlBwp->puschCfg.dmrsUlCfgForPuschMapTypeA.transPrecodDisabled. \
604             scramblingId0 = SCRAMBLING_ID; 
605          initUlBwp->puschCfg.resourceAllocType = RESOURCEALLOCATION_TYPE1;
606          initUlBwp->puschCfg.numTimeDomRsrcAlloc = 1;
607          idx = 0;
608          if(initUlBwp->puschCfg.numTimeDomRsrcAlloc <= MAX_NUM_UL_ALLOC)
609          {
610             initUlBwp->puschCfg.timeDomRsrcAllocList[idx].k2 = PUSCH_K2;
611             initUlBwp->puschCfg.timeDomRsrcAllocList[idx].mappingType =\
612                MAPPING_TYPEA;
613             initUlBwp->puschCfg.timeDomRsrcAllocList[idx].startSymbol = PUSCH_START_SYMBOL;
614             initUlBwp->puschCfg.timeDomRsrcAllocList[idx].symbolLength = PUSCH_LENGTH_SYMBOL;
615             initUlBwp->puschCfg.timeDomRsrcAllocList[idx].startSymbolAndLength =\
616                calcSliv(PUSCH_START_SYMBOL, PUSCH_LENGTH_SYMBOL);
617          }
618          initUlBwp->puschCfg.transformPrecoder = TRANSFORM_PRECODER_DISABLED;
619       }
620    }
621    else
622    {
623       DU_LOG("\nERROR  -->  DU APP : Memory is NULL of InitalUlBwp");
624    }
625
626 }
627 /******************************************************************
628  *
629  * @brief Fills SpCell Group Info
630  *
631  * @details
632  *
633  *    Function : fillDefaultSpCellGrpInfo
634  *
635  *    Functionality: Fills Sp Cell Group Info
636  *
637  * @params[in]  SpCellCfg *spCell
638  * @return void
639  *
640  *****************************************************************/
641 void fillDefaultSpCellGrpInfo(SpCellCfg *spCell)
642 {
643    if(spCell)
644    {
645       spCell->servCellIdx = SERV_CELL_IDX;
646       /* Filling Initial Dl Bwp */
647       fillDefaultInitDlBwp(&spCell->servCellCfg.initDlBwp);
648
649       spCell->servCellCfg.numDlBwpToAdd    = 0; 
650       spCell->servCellCfg.firstActvDlBwpId = ACTIVE_DL_BWP_ID;
651       spCell->servCellCfg.defaultDlBwpId   = ACTIVE_DL_BWP_ID;
652       spCell->servCellCfg.bwpInactivityTmr = NULLP;
653       spCell->servCellCfg.pdschServCellCfg.maxMimoLayers = NULLP;
654       spCell->servCellCfg.pdschServCellCfg.maxCodeBlkGrpPerTb = NULLP;
655       spCell->servCellCfg.pdschServCellCfg.codeBlkGrpFlushInd = NULLP;
656       spCell->servCellCfg.pdschServCellCfg.xOverhead = NULLP;
657       spCell->servCellCfg.pdschServCellCfg.numHarqProcForPdsch =\
658          NUM_HARQ_PROC_FOR_PDSCH_N_16;
659       /* Filling Initial UL Bwp*/
660       fillDefaultInitUlBwp(&spCell->servCellCfg.initUlBwp);
661       spCell->servCellCfg.numUlBwpToAdd     = 0; 
662       spCell->servCellCfg.firstActvUlBwpId  = ACTIVE_DL_BWP_ID; 
663    }
664    else
665    {
666       DU_LOG("\nERROR  -->  DU APP : Memory is NULL for SpCellGrp");
667    }
668 }
669
670 /******************************************************************
671  *
672  * @brief Fills Physical Cell Group Info
673  *
674  * @details
675  *
676  *    Function : fillDefaultPhyCellGrpInfo
677  *
678  *    Functionality: Fills Physical Cell Group Info
679  *
680  * @params[in]  PhyCellGrpCfg *cellGrp
681  * @return void
682  *
683  *****************************************************************/
684 void fillDefaultPhyCellGrpInfo(PhyCellGrpCfg *cellGrp)
685 {
686    if(cellGrp)
687    {
688       cellGrp->pdschHarqAckCodebook = PDSCH_HARQ_ACK_CODEBOOK_DYNAMIC;
689       cellGrp->pNrFr1 = P_NR_FR1;
690    }
691    else
692    {
693       DU_LOG("\nERROR  -->  DU APP : Memory is NULL for Physical Cell Group");
694    }
695 }
696
697 /******************************************************************
698  *
699  * @brief Fills Mac Cell Group Info
700  *
701  * @details
702  *
703  *    Function : fillDefaultMacCellGrpInfo
704  *
705  *    Functionality: Fills Mac Cell Group Info
706  *
707  * @params[in]  MacCellGrpCfg *cellGrp
708  * @return void
709  *
710  *****************************************************************/
711 void fillDefaultMacCellGrpInfo(MacCellGrpCfg *cellGrp)
712 {
713    uint8_t idx;
714
715    if(cellGrp)
716    {
717       /* Filling Scheduling Request Config */
718       cellGrp->schReqCfg.addModListCount = 1;
719       if(cellGrp->schReqCfg.addModListCount <= MAX_NUM_SR_CFG_PER_CELL_GRP)
720       {
721          for(idx = 0; idx < cellGrp->schReqCfg.addModListCount; idx++)
722          {
723             cellGrp->schReqCfg.addModList[idx].schedReqId    = SCH_REQ_ID;
724             cellGrp->schReqCfg.addModList[idx].srProhibitTmr = SR_PROHIBIT_MS_32;
725             cellGrp->schReqCfg.addModList[idx].srTransMax    = SR_TRANS_MAX_N_16;
726          }
727       }
728       cellGrp->schReqCfg.relListCount = 0;
729
730       /* Filling Tag config */
731       cellGrp->tagCfg.addModListCount = 1;
732       if(cellGrp->tagCfg.addModListCount <= MAC_NUM_TAGS)
733       {
734          for(idx = 0; idx < cellGrp->tagCfg.addModListCount; idx++)
735          {
736             cellGrp->tagCfg.addModList[idx].tagId = TAG_ID;
737             cellGrp->tagCfg.addModList[idx].timeAlignTimer = TIME_ALIGNMENT_TIMER_INFINITY;
738          }
739       }
740       cellGrp->tagCfg.relListCount = 0;
741
742       /* Filling BSR config */
743       cellGrp->bsrTmrCfg.periodicTimer = BSR_PERIODIC_TIMER_SF_10;
744       cellGrp->bsrTmrCfg.retxTimer = BSR_RETX_TIMER_SF_320;
745       cellGrp->bsrTmrCfg.srDelayTimer = BSR_SR_DELAY_TMR_2560;
746
747       /* Filling PHR config */
748       cellGrp->phrCfgSetupPres = true;
749       cellGrp->phrCfg.periodicTimer = PHR_PERIODIC_TIMER_INFINITY;
750       cellGrp->phrCfg.prohibitTimer = PHR_PROHIBIT_TIMER_SF_0;
751       cellGrp->phrCfg.txPowerFactor = PHR_TX_PWR_FACTOR_CHANGE_INFINITY;
752       cellGrp->phrCfg.multiplePHR   = false;
753       cellGrp->phrCfg.dummy         = false;
754       cellGrp->phrCfg.phrType2OtherCell = false;
755       cellGrp->phrCfg.phrOtherCG = PHR_MODE_OTHER_CG_REAL;  
756
757    }
758    else
759    {
760       DU_LOG("\nERROR  -->  DU APP : Memory is NULL for Master Cell Group");
761    }
762 }
763
764 /*******************************************************************
765  *
766  * @brief Fills default modulation info for a UE
767  *
768  * @details
769  *
770  *    Function : fillDefaultModulation
771  *
772  *    Functionality: Fills default modulation info for a UE
773  *
774  * @params[in] Pointer to MAC UE configuration
775  * @return ROK     - success
776  *         RFAILED - failure
777  *
778  * ****************************************************************/
779 void fillDefaultModulation(MacUeCfg *ueCfg)
780 {
781    ueCfg->dlModInfo.modOrder = MOD_ORDER_QPSK;
782    ueCfg->dlModInfo.mcsIndex = SIB1_MCS;
783    ueCfg->dlModInfo.mcsTable = MCS_TABLE_QAM64; /* Spec 38.214-Table 5.1.3.1-1 */
784
785    ueCfg->ulModInfo.modOrder = MOD_ORDER_QPSK;
786    ueCfg->ulModInfo.mcsIndex = SIB1_MCS;
787    ueCfg->ulModInfo.mcsTable = MCS_TABLE_QAM64; /* Spec 38.214-Table 5.1.3.1-1 */
788 }
789
790 /******************************************************************
791  *
792  * @brief Function to fill Mac Lc Cfg for SRB1
793  *
794  * @details
795  *
796  *    Function : fillMacSrb1LcCfg
797  *
798  *    Functionality: Function to fill Mac Lc cfg for SRB1
799  *
800  * @params[in]  LcCfg *lcCfg, LcCfg *ueSetReqDb
801  * @return void
802  *****************************************************************/
803
804 void fillMacSrb1LcCfg(LcCfg *macLcCfg)
805 {
806    macLcCfg->lcId   = SRB1_LCID;
807    macLcCfg->configType = CONFIG_ADD;
808    macLcCfg->drbQos = NULLP;
809    macLcCfg->snssai = NULLP;
810    macLcCfg->ulLcCfgPres = true;
811    fillDefaultUlLcCfg(&macLcCfg->ulLcCfg);
812 }
813
814 /******************************************************************
815  *
816  * @brief Function to fill the Lc cfg from ueSetupReqDb
817  *
818  * @details
819  *
820  *    Function : fillMacLcCfgToAddMod
821  *
822  *    Functionality: Function to fill the Lc cfg from ueSetupReqDb
823  *
824  * @params[in]  LcCfg *lcCfg, LcCfg *ueSetReqDb
825  * @return ROK/RFAILED
826  *
827  *****************************************************************/
828
829 uint8_t fillMacLcCfgToAddMod(LcCfg *lcCfg, LcCfg *ueSetReqDb)
830 {
831    uint8_t ret = ROK; 
832    lcCfg->lcId = ueSetReqDb->lcId;
833    lcCfg->configType = ueSetReqDb->configType;
834    /* Filling DRBQOS */
835    if(ueSetReqDb->drbQos)
836    {
837       if(!lcCfg->drbQos)
838       {
839          DU_ALLOC_SHRABL_BUF(lcCfg->drbQos, sizeof(DrbQosInfo));
840          if(!lcCfg->drbQos)
841          {
842             DU_LOG("\nERROR  -->  DU APP : Memory Alloc failed at drQos at fillMacLcCfgToAddMod()");
843             return RFAILED;
844          }
845       }
846       if(ret == ROK)
847       {
848          memcpy(lcCfg->drbQos, ueSetReqDb->drbQos, sizeof(DrbQosInfo));
849       }
850    }
851    else
852    {
853       lcCfg->drbQos = NULLP;
854    }
855
856    if(ret == ROK)
857    {
858       if(ueSetReqDb->snssai)
859       {
860          if(!lcCfg->snssai)
861          {
862             DU_ALLOC_SHRABL_BUF(lcCfg->snssai, sizeof(Snssai));
863             if(!lcCfg->snssai)
864             {
865                DU_LOG("\nERROR  -->  DU APP : Memory Alloc failed at snnsai at fillMacLcCfgToAddMod()");
866                ret = RFAILED;
867             }
868          }
869          if(ret == ROK)
870          {
871             /* Filling SNSSAI */
872             memcpy(lcCfg->snssai, ueSetReqDb->snssai, sizeof(Snssai));
873          }
874          else
875          {
876             lcCfg->snssai = NULLP;
877             if(lcCfg->drbQos)
878             {
879                DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, lcCfg->drbQos, sizeof(DrbQosInfo));
880                lcCfg->drbQos = NULLP;
881             }
882             return ret;
883          }
884       }
885       else
886          lcCfg->snssai = NULLP;
887    }
888    lcCfg->ulLcCfgPres = ueSetReqDb->ulLcCfgPres;
889    memcpy(&lcCfg->ulLcCfg, &ueSetReqDb->ulLcCfg, sizeof(UlLcCfg));
890    memcpy(&lcCfg->dlLcCfg, &ueSetReqDb->dlLcCfg, sizeof(DlLcCfg));
891    return ret;
892 }
893
894 /******************************************************************
895  *
896  * @brief Function to copy the Bit rate from ueSetupReqDb
897  *
898  * @details
899  *
900  *    Function : fillAmbr
901  *
902  *    Functionality: Function to copy bit Rate from ueSetupReqDb
903  *
904  * @params[in]  AmbrCfg **macAmbr, AmbrCfg  *ueDbAmbr
905  * @return ROK/RFAILED
906  *
907  *****************************************************************/
908
909 uint8_t fillAmbr(AmbrCfg **macAmbr, AmbrCfg *ueDbAmbr)
910 {
911    if(ueDbAmbr)
912    {
913       if(*macAmbr == NULLP)
914       {
915          DU_ALLOC_SHRABL_BUF(*macAmbr, sizeof(AmbrCfg));
916          if(*macAmbr == NULLP)
917          {
918             DU_LOG("\nERROR  -->  DU APP : Memory Alloc Failed at fillAmbr()");
919             return RFAILED;
920          }
921       }
922       memset(*macAmbr, 0, sizeof(AmbrCfg));
923       (*macAmbr)->ulBr = ueDbAmbr->ulBr;
924    }
925    else
926    {
927       *macAmbr = NULLP;
928    }
929    return ROK;
930 }
931
932 /******************************************************************
933  *
934  * @brief Builds and Send UE ReConfig Request to MAC
935  *
936  * @details
937  *
938  *    Function : sendUeReCfgReqToMac
939  *
940  *    Functionality: Builds and Send UE ReConfig Request to MAC
941  *
942  * @Params[in]  MacUeCfg pointer
943  * @return ROK     - success
944  *         RFAILED - failure
945  *
946  * ****************************************************************/
947
948 uint8_t sendUeReCfgReqToMac(MacUeCfg *macUeCfg)
949 {
950    uint8_t ret = ROK;
951    Pst pst;
952    
953    /* Fill Pst */
954    FILL_PST_DUAPP_TO_MAC(pst, EVENT_MAC_UE_RECONFIG_REQ);
955    
956    if(macUeCfg)
957    {
958       /* Processing one Ue at a time to MAC */
959       DU_LOG("\nDEBUG   -->  DU_APP: Sending Ue Reconfig Request to MAC");
960       ret = (*packMacUeReconfigReqOpts[pst.selector])(&pst, macUeCfg);
961       if(ret == RFAILED)
962       {
963          DU_LOG("\nERROR  -->  DU APP : Failed to send Reconfig Request to MAC at sendUeReCfgReqToMac()");
964          DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, macUeCfg, sizeof(MacUeCfg));
965       }
966    }
967    else
968    {
969       DU_LOG("\nERROR  -->  DU_APP: Received macUeCfg is NULLP at sendUeReCfgReqToMac()");
970       ret = RFAILED;
971    }
972    return ret;
973 }
974
975 /******************************************************************
976  *
977  * @brief Function to return Drb LcId
978  *
979  * @details
980  *
981  *    Function : getDrbLcId
982  *
983  *    Functionality: Function to return Drb LcId
984  *
985  * Returns: lcId - SUCCESS
986  *         RFAILED - FAILURE
987  *****************************************************************/
988
989 uint8_t getDrbLcId(uint32_t *drbBitMap)
990 {
991    uint8_t bitMask = 1, bitPos = 0;
992    bitPos = MIN_DRB_LCID;
993
994    while(bitPos <= MAX_DRB_LCID)
995    {
996       if((*drbBitMap & (bitMask << bitPos)) == 0)
997       {
998          *drbBitMap = ((bitMask << bitPos)| *drbBitMap);
999          return bitPos;
1000       }
1001       else
1002       {
1003          bitPos++;
1004       }
1005    }
1006    DU_LOG("\nERROR   -->  DU_APP: Max LC Reached in getDrbLcId()");
1007    return RFAILED;
1008 }
1009
1010 /******************************************************************
1011  *
1012  * @brief Fills MacUeCfg structure
1013  *
1014  * @details
1015  *
1016  *    Function : fillMacUeCfg
1017  *
1018  *    Functionality: Fills MacUeCfg
1019  *
1020  * @params[in]  cellId, ueIdx, crnti, 
1021  *              DuUeCfg  pointer,
1022  *              MacUeCfg pointer
1023  * @return ROK/RFAILED
1024  *
1025  *****************************************************************/
1026 uint8_t fillMacUeCfg(uint16_t cellId, uint8_t ueIdx, uint16_t crnti, \
1027    DuUeCfg *ueCfgDb, MacUeCfg *macUeCfg)
1028 {
1029    uint8_t ret, dbIdx, lcIdx, cellIdx;
1030    bool lcIdFound = false;
1031    MacUeCfg *duMacDb = NULLP;
1032
1033    ret =ROK;
1034
1035    if(!ueCfgDb)
1036    {
1037       macUeCfg->cellId       = cellId;
1038       macUeCfg->ueIdx        = ueIdx;
1039       macUeCfg->crnti        = crnti;
1040       fillDefaultMacCellGrpInfo(&macUeCfg->macCellGrpCfg);
1041       fillDefaultPhyCellGrpInfo(&macUeCfg->phyCellGrpCfg);
1042       fillDefaultSpCellGrpInfo(&macUeCfg->spCellCfg);
1043       macUeCfg->ambrCfg = NULLP;
1044       fillMacSrb1LcCfg(&macUeCfg->lcCfgList[0]);
1045       fillDefaultModulation(macUeCfg);
1046       macUeCfg->numLcs++;
1047    }
1048    else
1049    {
1050       /* Fetching MacDb from DuUeCb */
1051       GET_CELL_IDX(cellId, cellIdx);
1052       duMacDb = &duCb.actvCellLst[cellIdx]->ueCb[ueIdx-1].macUeCfg;
1053       /* Fetching MaUeCfg List for ADD/MOD/DEL */
1054       macUeCfg->cellId       = cellId;
1055       macUeCfg->ueIdx        = ueIdx;
1056       macUeCfg->crnti        = crnti;
1057       ret = procUeReCfgCellInfo(macUeCfg, ueCfgDb->cellGrpCfg);
1058       if(ret == ROK)
1059       {
1060          if(macUeCfg->spCellCfg.servCellCfg.initDlBwp.pdschPresent)
1061          {
1062             fillStartSymbolAndLen(macUeCfg->spCellCfg.servCellCfg.initDlBwp.pdschCfg.numTimeDomRsrcAlloc,\
1063                &macUeCfg->spCellCfg.servCellCfg.initDlBwp.pdschCfg, NULL);
1064          }
1065          if(macUeCfg->spCellCfg.servCellCfg.initUlBwp.puschPresent)
1066          {
1067             fillStartSymbolAndLen(macUeCfg->spCellCfg.servCellCfg.initUlBwp.puschCfg.numTimeDomRsrcAlloc,\
1068                NULL, &macUeCfg->spCellCfg.servCellCfg.initUlBwp.puschCfg);
1069          }
1070          ret = fillAmbr(&macUeCfg->ambrCfg, ueCfgDb->ambrCfg);
1071         
1072          duFillModulationDetails(macUeCfg, duMacDb, ueCfgDb->ueNrCapability);
1073       }
1074
1075       /* Filling LC Context */
1076       for(dbIdx = 0; (dbIdx < ueCfgDb->numMacLcs && ret == ROK); dbIdx++)
1077       {
1078          if(!ueCfgDb->macLcCfg[dbIdx].ulLcCfgPres)
1079          {
1080             /* Filling default UL LC config in MAC if not present */
1081             ueCfgDb->macLcCfg[dbIdx].ulLcCfgPres = true;
1082             fillDefaultUlLcCfg(&ueCfgDb->macLcCfg[dbIdx].ulLcCfg);
1083          }
1084          for(lcIdx = 0; lcIdx < duMacDb->numLcs; lcIdx++)
1085          {
1086             if(ueCfgDb->macLcCfg[dbIdx].lcId == duMacDb->lcCfgList[lcIdx].lcId)
1087             {
1088                lcIdFound = true;
1089                if((ueCfgDb->macLcCfg[dbIdx].configType == CONFIG_UNKNOWN) ||
1090                      (ueCfgDb->macLcCfg[dbIdx].configType == CONFIG_MOD))
1091                {
1092                   ueCfgDb->macLcCfg[dbIdx].configType = CONFIG_MOD;
1093                   ret = fillMacLcCfgToAddMod(&macUeCfg->lcCfgList[dbIdx], &ueCfgDb->macLcCfg[dbIdx]);
1094                }
1095             }
1096             else
1097                lcIdFound = false;
1098          }
1099          if(!lcIdFound)
1100          {
1101             /* ADD/DEL CONFIG */
1102             ret = fillMacLcCfgToAddMod(&macUeCfg->lcCfgList[dbIdx], &ueCfgDb->macLcCfg[dbIdx]);
1103          }
1104          if(ret == ROK)
1105          {
1106             macUeCfg->numLcs++;
1107          }
1108          else
1109          {
1110             DU_LOG("\nERROR  -->  DU APP : Failed to add Lc at Idx %d in fillMacUeCfg()", dbIdx); 
1111             break;
1112          }
1113       }/*End of Outer FOR loop */
1114    }
1115    return ret;
1116 }
1117
1118 /******************************************************************
1119  *
1120  * @brief Fills Rlc AM Information
1121  *
1122  * @details
1123  *
1124  *    Function : fillDefaultAmInfo
1125  *
1126  *    Functionality: Fills Rlc AM Information
1127  *
1128  * @params[in]  AmBearerCfg *amCfg
1129  * @return void
1130  *
1131  *****************************************************************/
1132 void fillDefaultAmInfo(AmBearerCfg *amCfg)
1133 {
1134    /* DL AM */
1135    amCfg->dlAmCfg.snLenDl     = AM_SIZE_12;
1136    amCfg->dlAmCfg.pollRetxTmr = T_POLL_RETRANSMIT_VAL;
1137    amCfg->dlAmCfg.pollPdu     = POLL_PDU_VAL;
1138    amCfg->dlAmCfg.pollByte    = POLL_BYTE_VAL;
1139    amCfg->dlAmCfg.maxRetxTh   = MAX_RETX_THRESHOLD_VAL;   
1140  
1141    /* UL AM */
1142    amCfg->ulAmCfg.snLenUl     = AM_SIZE_12;
1143    amCfg->ulAmCfg.reAssemTmr  = T_REASSEMBLY_VAL; 
1144    amCfg->ulAmCfg.statProhTmr = T_STATUS_PROHIBHIT_VAL;
1145 }
1146
1147 /******************************************************************
1148  *
1149  * @brief Fills RLC UM Bi Directional Information
1150  *
1151  * @details
1152  *
1153  *    Function : fillDefaultUmBiInfo
1154  *
1155  *    Functionality: Fills RLC UM Bi Directional Information
1156  *
1157  * @params[in]  UmBiDirBearerCfg *umBiDirCfg
1158  * @return void
1159  *
1160  *****************************************************************/
1161 void fillDefaultUmBiInfo(UmBiDirBearerCfg *umBiDirCfg)
1162 {
1163    /* UL UM BI DIR INFO */
1164    umBiDirCfg->ulUmCfg.snLenUlUm = UM_SIZE_12;
1165    umBiDirCfg->ulUmCfg.reAssemTmr = T_REASSEMBLY_VAL;
1166
1167    /* DL UM BI DIR INFO */
1168    umBiDirCfg->dlUmCfg.snLenDlUm  = UM_SIZE_12;
1169 }
1170
1171 /******************************************************************
1172  *
1173  * @brief Fills RLC UM Uni Directional UL Information
1174  *
1175  * @details
1176  *
1177  *    Function : fillDefaultUmUlInfo
1178  *
1179  *    Functionality: Fills RLC UM Uni Directional Info
1180  *
1181  * @params[in]  UmUniDirUlBearerCfg *UmUlCfg
1182  * @return void
1183  *
1184  *****************************************************************/
1185 void fillDefaultUmUlInfo(UmUniDirUlBearerCfg *UmUlCfg)
1186 {
1187    UmUlCfg->ulUmCfg.snLenUlUm = UM_SIZE_12;
1188    UmUlCfg->ulUmCfg.reAssemTmr = T_REASSEMBLY_VAL;
1189 }
1190
1191 /******************************************************************
1192  *
1193  * @brief Fills RLC UM Uni Directional DL Information
1194  *
1195  * @details
1196  *
1197  *    Function : fillDefaultUmDlInfo
1198  *
1199  *    Functionality: Fills RLC UM Uni Directional DL Info
1200  *
1201  * @params[in]  UmUniDirDlBearerCfg *UmDlCfg
1202  * @return void
1203  *
1204  *****************************************************************/
1205 void fillDefaultUmDlInfo(UmUniDirDlBearerCfg *UmDlCfg)
1206 {
1207    UmDlCfg->dlUmCfg.snLenDlUm  = UM_SIZE_12;
1208 }
1209
1210 /******************************************************************
1211  *
1212  * @brief Builds Rlc Mode Default Configuration
1213  *
1214  * @details
1215  *
1216  *    Function : fillDefaultRlcModeCfg
1217  *
1218  *    Functionality: Builds Rlc Mode Default Configuration
1219  *
1220  * @params[in]  rlcMode, RlcBearerCfg *lcCfg
1221  * @return ROK/RFAILED
1222  *
1223  *****************************************************************/
1224
1225 uint8_t fillDefaultRlcModeCfg(uint8_t rlcMode, RlcBearerCfg *lcCfg)
1226 {
1227
1228    if(lcCfg)
1229    {
1230       switch(rlcMode)
1231       {
1232          case RLC_AM :
1233             {
1234                if(!lcCfg->u.amCfg)
1235                {
1236                  DU_ALLOC_SHRABL_BUF(lcCfg->u.amCfg, sizeof(AmBearerCfg));
1237                  if(lcCfg->u.amCfg)
1238                     fillDefaultAmInfo(lcCfg->u.amCfg);
1239                  else
1240                  {
1241                     DU_LOG("\n ERROR  -->  DU APP : Memory Alloc failed at AmCfg at fillDefaultRlcModeCfg()");
1242                     return RFAILED;
1243                  }
1244                }
1245                break;
1246             }
1247          case RLC_UM_BI_DIRECTIONAL :
1248             {
1249                if(!lcCfg->u.umBiDirCfg)
1250                {
1251                  DU_ALLOC_SHRABL_BUF(lcCfg->u.umBiDirCfg, sizeof(UmBiDirBearerCfg));
1252                  if(lcCfg->u.umBiDirCfg)
1253                     fillDefaultUmBiInfo(lcCfg->u.umBiDirCfg);
1254                  else
1255                  {
1256                     DU_LOG("\n ERROR  -->  DU APP : Memory Alloc failed at UmBiDirCfg at fillDefaultRlcModeCfg()");
1257                     return RFAILED;
1258                  }
1259                }
1260                break;
1261             }
1262          case RLC_UM_UNI_DIRECTIONAL_UL :
1263             {
1264                if(!lcCfg->u.umUniDirUlCfg)
1265                {
1266                   DU_ALLOC_SHRABL_BUF(lcCfg->u.umUniDirUlCfg, sizeof(UmUniDirUlBearerCfg));
1267                   if(lcCfg->u.umUniDirUlCfg)
1268                     fillDefaultUmUlInfo(lcCfg->u.umUniDirUlCfg);
1269                   else
1270                   {
1271                      DU_LOG("\n ERROR  -->  DU APP : Memory Alloc failed at UmUniDirUlCfg at fillDefaultRlcModeCfg()");
1272                      return RFAILED;
1273                   }
1274                }
1275                break;
1276             }
1277          case RLC_UM_UNI_DIRECTIONAL_DL :
1278             {
1279                if(!lcCfg->u.umUniDirDlCfg)
1280                {
1281                   DU_ALLOC_SHRABL_BUF(lcCfg->u.umUniDirDlCfg, sizeof(UmUniDirDlBearerCfg));
1282                   if(lcCfg->u.umUniDirDlCfg)
1283                    fillDefaultUmDlInfo(lcCfg->u.umUniDirDlCfg);
1284                   else
1285                   {
1286                      DU_LOG("\n ERROR  -->  DU APP : Memory Alloc failed at UmUniDirDlCfg at fillDefaultRlcModeCfg()");
1287                      return RFAILED;
1288                   }
1289                }
1290                break;
1291             }
1292          default:
1293             DU_LOG("\nERROR  -->  DUAPP: Invalid rlcMode %d at extractRlcCfgToAddMod()", rlcMode);
1294             return RFAILED;
1295       }
1296
1297    }
1298    else
1299    {
1300       DU_LOG("\nERROR  -->  DUAPP: Received LC Config is NULL");
1301       return RFAILED;
1302    }
1303    return ROK;
1304 }
1305
1306 /*******************************************************************
1307  *
1308  * @brief Function to fill Rlc Lc Cfg for SRB1
1309  *
1310  * @details
1311  *
1312  *    Function : fillRlcSrb1LcCfg
1313  *
1314  *    Functionality: 
1315  *     Function to fill Rlc Lc Cfg for SRB1
1316  * 
1317  *  @params[in]     Pointer to RlcBearerCfg
1318  *  @return ROK/RFAILED
1319  * 
1320  *****************************************************************/
1321
1322 uint8_t fillRlcSrb1LcCfg(RlcBearerCfg *rlcLcCfg)
1323 {
1324    uint8_t ret = ROK;
1325
1326    rlcLcCfg->rbId   = SRB1_LCID;
1327    rlcLcCfg->rbType = RB_TYPE_SRB;
1328    rlcLcCfg->lcId   = SRB1_LCID;
1329    rlcLcCfg->lcType = LCH_DCCH;
1330    rlcLcCfg->rlcMode = RLC_AM;
1331    rlcLcCfg->configType = CONFIG_ADD;
1332    ret = fillDefaultRlcModeCfg(rlcLcCfg->rlcMode, rlcLcCfg);
1333    return ret;
1334 }
1335
1336 /*******************************************************************
1337  *
1338  * @brief Processes UE ReConfig Req to RLC UL
1339  *
1340  * @details
1341  *
1342  *    Function : sendUeReCfgReqToRlc
1343  *
1344  *    Functionality: 
1345  *     Processes UE Reconfig Req to RLC UL
1346  * 
1347  *  @params[in]     Pointer to RlcUeCfg
1348  *  @return ROK     - success
1349  *          RFAILED - failure
1350  * 
1351  *****************************************************************/
1352
1353 uint8_t sendUeReCfgReqToRlc(RlcUeCfg *rlcUeCfg)
1354 {
1355    uint8_t ret;
1356    Pst pst;
1357    
1358    FILL_PST_DUAPP_TO_RLC(pst, RLC_UL_INST, EVENT_RLC_UE_RECONFIG_REQ);
1359    if(rlcUeCfg)
1360    {
1361       /* Processing one Ue at a time to RLC */
1362       DU_LOG("\nDEBUG   -->  DU_APP: Sending Ue Reconfig Request to RLC UL");
1363       ret = (*packRlcUeReconfigReqOpts[pst.selector])(&pst, rlcUeCfg);
1364       if(ret == RFAILED)
1365       {
1366          DU_LOG("\nERROR  -->  DU_APP : Failed to send Ue Reconfig Req to RLC at sendUeReCfgReqToRlc()");
1367          DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, rlcUeCfg, sizeof(RlcUeCfg));
1368       }
1369    }
1370    else
1371    {
1372       DU_LOG("\nERROR  -->   DU_APP: Received RlcUeCfg is NULL at sendUeReCfgReqToRlc()");
1373       ret = RFAILED;
1374    }
1375    return ret;
1376 }
1377
1378 /******************************************************************
1379  *
1380  * @brief Fills RlcBearerCfg structure
1381  *
1382  * @details
1383  *
1384  *    Function : fillRlcUeCfg
1385  *
1386  *    Functionality: Fills Rlc Bearer Cfg
1387  *
1388  *
1389  *****************************************************************/
1390 uint8_t fillRlcUeCfg(uint16_t cellId, uint8_t ueIdx,\
1391    DuUeCfg *ueCfgDb, RlcUeCfg *rlcUeCfg)
1392 {
1393    uint8_t ret, dbIdx, lcIdx, cellIdx;
1394    bool lcIdFound = false;
1395    RlcUeCfg *duRlcDb = NULLP;
1396
1397    ret = ROK;
1398    if(!ueCfgDb)
1399    {
1400       /* Initial RB being Added */ 
1401       rlcUeCfg->cellId       = cellId;
1402       rlcUeCfg->ueIdx        = ueIdx;
1403       ret = fillRlcSrb1LcCfg(&rlcUeCfg->rlcLcCfg[0]);
1404       if(ret == ROK)
1405          rlcUeCfg->numLcs++;
1406       else
1407          memset(rlcUeCfg, 0, sizeof(RlcUeCfg));
1408    }
1409    else
1410    {
1411       /* Fetch RlcDb from DuUeCb */ 
1412       GET_CELL_IDX(cellId, cellIdx);
1413       duRlcDb = &duCb.actvCellLst[cellIdx]->ueCb[ueIdx-1].rlcUeCfg;
1414       /*Filling RlcUeCfg */
1415       rlcUeCfg->cellId       = cellId;
1416       rlcUeCfg->ueIdx        = ueIdx;
1417       for(dbIdx = 0; (dbIdx < ueCfgDb->numRlcLcs && ret == ROK); dbIdx++)
1418       {
1419          ret = fillDefaultRlcModeCfg(ueCfgDb->rlcLcCfg[dbIdx].rlcMode, &ueCfgDb->rlcLcCfg[dbIdx]);
1420          if(ret == RFAILED)
1421          {
1422             DU_LOG("\n ERROR  -->  DU APP : Failed to fill Rlc Mode at fillRlcUeCfg()");
1423             memset(rlcUeCfg, 0, sizeof(RlcUeCfg));
1424             return ret;
1425          }
1426          /* Fill Rlc Ue Cfg List for ADD/MOD/DEL */
1427          for(lcIdx = 0; lcIdx < duRlcDb->numLcs; lcIdx++)
1428          { 
1429             if(ueCfgDb->rlcLcCfg[dbIdx].lcId == duRlcDb->rlcLcCfg[lcIdx].lcId)
1430             {
1431                lcIdFound = true;
1432                if((ueCfgDb->rlcLcCfg[dbIdx].configType == CONFIG_UNKNOWN)||
1433                      (ueCfgDb->rlcLcCfg[dbIdx].configType == CONFIG_MOD))
1434                {
1435                   /* MOD */ 
1436                   ueCfgDb->rlcLcCfg[dbIdx].configType = CONFIG_MOD; /* update Db for MOD type */
1437                   memcpy(&rlcUeCfg->rlcLcCfg[dbIdx], &ueCfgDb->rlcLcCfg[dbIdx], sizeof(RlcBearerCfg));
1438                }
1439             }
1440             else
1441                lcIdFound = false;
1442          }
1443          if(!lcIdFound)
1444          {
1445             /* ADD/ DEL Config Type */
1446             memcpy(&rlcUeCfg->rlcLcCfg[dbIdx], &ueCfgDb->rlcLcCfg[dbIdx], sizeof(RlcBearerCfg));
1447          }
1448          rlcUeCfg->numLcs++;
1449       }
1450    }
1451    return ret;
1452 }
1453
1454 /******************************************************************
1455  *
1456  * @brief creates UE context
1457  *
1458  * @details
1459  *
1460  *    Function : duCreateUeCb
1461  *
1462  *    Functionality: Creates UE Conetxt
1463  *
1464  * @params[in] UeCcchCtxt Pointer
1465  *             UeIdx Pointer
1466  *
1467  * @return ROK     - success
1468  *         RFAILED - failure
1469  * ****************************************************************/
1470 uint8_t duCreateUeCb(UeCcchCtxt *ueCcchCtxt, uint32_t gnbCuUeF1apId)
1471 {
1472    uint8_t cellIdx = 0;
1473    uint8_t ret     = ROK;
1474    uint8_t ueIdx;
1475
1476    for(cellIdx = 0; cellIdx < MAX_NUM_CELL; cellIdx++)
1477    {
1478       if(ueCcchCtxt->cellId == duCb.actvCellLst[cellIdx]->cellId)
1479       {
1480          GET_UE_IDX(ueCcchCtxt->crnti, ueIdx);
1481          DU_LOG("\nDEBUG   -->  DU_APP: Filling UeCb for ueIdx [%d]", ueIdx);
1482
1483          duCb.actvCellLst[cellIdx]->ueCb[ueIdx-1].f1UeDb        = NULLP;
1484          duCb.actvCellLst[cellIdx]->ueCb[ueIdx-1].crnti         = ueCcchCtxt->crnti;
1485          duCb.actvCellLst[cellIdx]->ueCb[ueIdx-1].gnbDuUeF1apId = ueCcchCtxt->gnbDuUeF1apId;
1486          duCb.actvCellLst[cellIdx]->ueCb[ueIdx-1].gnbCuUeF1apId = gnbCuUeF1apId;
1487          duCb.actvCellLst[cellIdx]->ueCb[ueIdx-1].drbBitMap     = NULLP;
1488          duCb.actvCellLst[cellIdx]->ueCb[ueIdx-1].ueState       = UE_ACTIVE;
1489
1490          /* Filling Mac Ue Config */ 
1491          memset(&duCb.actvCellLst[cellIdx]->ueCb[ueIdx-1].macUeCfg, 0, sizeof(MacUeCfg));
1492          ret = duBuildAndSendUeCreateReqToMac(ueCcchCtxt->cellId, ueIdx, ueCcchCtxt->crnti,\
1493                 &duCb.actvCellLst[cellIdx]->ueCb[ueIdx-1].macUeCfg);
1494          if(ret == RFAILED)
1495             DU_LOG("\nERROR  -->  DU APP : Failed to send UE create request to MAC");
1496          
1497          /* Filling Rlc Ue Config */
1498          memset(&duCb.actvCellLst[cellIdx]->ueCb[ueIdx-1].rlcUeCfg, 0, sizeof(RlcUeCfg));
1499          ret = duBuildAndSendUeCreateReqToRlc(ueCcchCtxt->cellId, ueIdx, \
1500                 &duCb.actvCellLst[cellIdx]->ueCb[ueIdx-1].rlcUeCfg);
1501          if(ret == RFAILED)
1502             DU_LOG("\nERROR  -->  DU APP : Failed to send UE create request to RLC");
1503
1504          duCb.actvCellLst[cellIdx]->numActvUes++;
1505          memset(ueCcchCtxt, 0, sizeof(UeCcchCtxt));
1506       }
1507    }
1508    return ret;
1509 }
1510
1511 /******************************************************************
1512  *
1513  * @brief Builds and Send UE Create Request to MAC
1514  *
1515  * @details
1516  *
1517  *    Function : duBuildAndSendUeCreateReqToMac
1518  *
1519  *    Functionality: Builds and Send UE Create Request to MAC
1520  *
1521  * @Params[in]  cellId,
1522  *              ueIdx
1523  * @return ROK     - success
1524  *         RFAILED - failure
1525  *
1526  * ****************************************************************/
1527
1528 uint8_t duBuildAndSendUeCreateReqToMac(uint16_t cellId, uint8_t ueIdx,\
1529    uint16_t crnti, MacUeCfg *duMacUeCfg)
1530 {
1531    uint8_t  ret = ROK;
1532    MacUeCfg *macUeCfg = NULLP;
1533    Pst       pst;
1534    memset(&pst, 0, sizeof(Pst));
1535
1536
1537    ret = fillMacUeCfg(cellId, ueIdx, crnti, NULL, duMacUeCfg);
1538    if(ret == RFAILED)
1539    {
1540       DU_LOG("\nERROR  -->  DU APP : Failed to fill MacUeCfg at duBuildAndSendUeCreateReqToMac()");
1541       return RFAILED;
1542    }
1543    /* Fill Pst */
1544    FILL_PST_DUAPP_TO_MAC(pst, EVENT_MAC_UE_CREATE_REQ);
1545
1546    /* Copying ueCb to a sharable buffer */
1547    DU_ALLOC_SHRABL_BUF(macUeCfg, sizeof(MacUeCfg));
1548    if(macUeCfg)
1549    {
1550       memset(macUeCfg, 0, sizeof(MacUeCfg));
1551       memcpy(macUeCfg, &duCb.actvCellLst[cellId - 1]->ueCb[ueIdx -1].macUeCfg, sizeof(MacUeCfg));
1552       DU_LOG("\nDEBUG   -->  DU_APP: Sending UE create request to MAC");
1553
1554       /* Processing one Ue at a time to MAC */
1555       ret = (*packMacUeCreateReqOpts[pst.selector])(&pst, macUeCfg);
1556       if(ret == RFAILED)
1557       {
1558          DU_LOG("\nERROR  -->  DU_APP : Failure in sending Ue Create Req to MAC at duBuildAndSendUeCreateReqToMac()");
1559          DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, macUeCfg, sizeof(MacUeCfg));
1560       }
1561    }
1562    else
1563    {
1564       DU_LOG("\n ERROR  -->  DU APP : Memory alloc failed at duBuildAndSendUeCreateReqToMac()");
1565       ret = RFAILED;
1566    }
1567    return ret;
1568 }
1569
1570 /*******************************************************************
1571  *
1572  * @brief To update DuUeCb Mac Cfg
1573  *
1574  * @details
1575  *
1576  *    Function : duUpdateMacCfg
1577  *    Functionality:  update DuUeCb MAC Cfg
1578  *
1579  * @params[in] DuUeCb Pointer
1580  *             F1UeContextSetupDb pointer 
1581  * @return ROK     - success
1582  *         RFAILED - failure
1583  *
1584  * ****************************************************************/
1585 uint8_t duUpdateMacCfg(MacUeCfg *macUeCfg, F1UeContextSetupDb *f1UeDb) 
1586 {
1587    uint8_t ret, lcIdx, dbIdx, numLcs, lcDelIdx;
1588    ret = ROK;
1589
1590    /*Filling Cell Group Cfg*/
1591    ret =  procUeReCfgCellInfo(macUeCfg, f1UeDb->duUeCfg.cellGrpCfg);
1592    if(ret == ROK)
1593    {
1594       if(macUeCfg->spCellCfg.servCellCfg.initDlBwp.pdschPresent)
1595       {
1596          fillStartSymbolAndLen(macUeCfg->spCellCfg.servCellCfg.initDlBwp.pdschCfg.numTimeDomRsrcAlloc,\
1597                &macUeCfg->spCellCfg.servCellCfg.initDlBwp.pdschCfg, NULL);
1598       }
1599       if(macUeCfg->spCellCfg.servCellCfg.initUlBwp.puschPresent)
1600       {
1601          fillStartSymbolAndLen(macUeCfg->spCellCfg.servCellCfg.initUlBwp.puschCfg.numTimeDomRsrcAlloc,\
1602                NULL, &macUeCfg->spCellCfg.servCellCfg.initUlBwp.puschCfg);
1603       }
1604       ret = fillAmbr(&macUeCfg->ambrCfg, f1UeDb->duUeCfg.ambrCfg);
1605    }
1606    /* Filling LC Context */
1607    for(dbIdx = 0; (dbIdx < f1UeDb->duUeCfg.numMacLcs && ret == ROK); dbIdx++)
1608    {
1609       numLcs = macUeCfg->numLcs;
1610       for(lcIdx = 0; lcIdx < numLcs; lcIdx++)
1611       {
1612          if(f1UeDb->duUeCfg.macLcCfg[dbIdx].lcId ==  macUeCfg->lcCfgList[lcIdx].lcId)
1613          {
1614             if(f1UeDb->duUeCfg.macLcCfg[dbIdx].configType == CONFIG_MOD)
1615             {
1616                ret = fillMacLcCfgToAddMod(&macUeCfg->lcCfgList[lcIdx],&f1UeDb->duUeCfg.macLcCfg[dbIdx]);
1617             }
1618             else if(f1UeDb->duUeCfg.macLcCfg[dbIdx].configType == CONFIG_DEL)
1619             {
1620                /* Free memory at matched  lcIdx index */
1621                freeMacLcCfg(&macUeCfg->lcCfgList[lcIdx]);
1622                macUeCfg->numLcs--;
1623                for(lcDelIdx = lcIdx; lcDelIdx < macUeCfg->numLcs; lcDelIdx++)
1624                {
1625                   /* moving all elements one index ahead */
1626                   ret = fillMacLcCfgToAddMod(&macUeCfg->lcCfgList[lcDelIdx], &macUeCfg->lcCfgList[lcDelIdx+1]);
1627                   freeMacLcCfg(&macUeCfg->lcCfgList[lcDelIdx+1]);
1628                   if(ret == RFAILED)
1629                   {
1630                      DU_LOG("\nERROR  -->  DU APP : Failed to delete LC at Idx %d in duUpdateMacCfg()", lcDelIdx);
1631                      break;
1632                   }
1633                }
1634             }
1635          }
1636       } 
1637       if(f1UeDb->duUeCfg.macLcCfg[dbIdx].configType == CONFIG_ADD)
1638       {
1639          ret = fillMacLcCfgToAddMod(&macUeCfg->lcCfgList[numLcs], &f1UeDb->duUeCfg.macLcCfg[dbIdx]);
1640          if(ret == RFAILED)
1641          {
1642             DU_LOG("\nERROR  -->  DU APP : Failed to add LC at Idx %d in duUpdateMacCfg()", numLcs);
1643             break;
1644          }
1645          macUeCfg->numLcs++;
1646       }
1647                 
1648    }
1649    return ret;
1650 }
1651
1652 /******************************************************************
1653  *
1654  * @brief Function to fill the RLC Lc cfg from ueSetupReqDb
1655  *
1656  * @details
1657  *
1658  *    Function : fillRlcCfgToAddMod
1659  *
1660  *    Functionality: Function to fill the RLC Lc cfg from ueSetupReqDb
1661  *
1662  *
1663  *****************************************************************/
1664
1665 uint8_t fillRlcCfgToAddMod(RlcBearerCfg *lcCfg, RlcBearerCfg *f1UeDbLcCfg)
1666 {
1667    lcCfg->configType = f1UeDbLcCfg->configType;
1668    lcCfg->rbId       = f1UeDbLcCfg->rbId;
1669    lcCfg->rbType     = f1UeDbLcCfg->rbType;
1670    lcCfg->lcId       = f1UeDbLcCfg->lcId;
1671    lcCfg->lcType     = f1UeDbLcCfg->lcType;
1672    lcCfg->rlcMode    = f1UeDbLcCfg->rlcMode;
1673    switch(lcCfg->rlcMode)
1674    {
1675       case RLC_AM :
1676       {
1677          if(!lcCfg->u.amCfg)
1678          {
1679             DU_ALLOC_SHRABL_BUF(lcCfg->u.amCfg, sizeof(AmBearerCfg));
1680             if(!lcCfg->u.amCfg)
1681                return RFAILED;
1682          }
1683          /* DL AM */
1684          lcCfg->u.amCfg->dlAmCfg.snLenDl     = f1UeDbLcCfg->u.amCfg->dlAmCfg.snLenDl;    
1685          lcCfg->u.amCfg->dlAmCfg.pollRetxTmr = f1UeDbLcCfg->u.amCfg->dlAmCfg.pollRetxTmr;
1686          lcCfg->u.amCfg->dlAmCfg.pollPdu     = f1UeDbLcCfg->u.amCfg->dlAmCfg.pollPdu;
1687          lcCfg->u.amCfg->dlAmCfg.pollByte    = f1UeDbLcCfg->u.amCfg->dlAmCfg.pollByte;   
1688          lcCfg->u.amCfg->dlAmCfg.maxRetxTh   = f1UeDbLcCfg->u.amCfg->dlAmCfg.maxRetxTh;   
1689  
1690          /* UL AM */
1691          lcCfg->u.amCfg->ulAmCfg.snLenUl     = f1UeDbLcCfg->u.amCfg->ulAmCfg.snLenUl;
1692          lcCfg->u.amCfg->ulAmCfg.reAssemTmr  = f1UeDbLcCfg->u.amCfg->ulAmCfg.reAssemTmr; 
1693          lcCfg->u.amCfg->ulAmCfg.statProhTmr = f1UeDbLcCfg->u.amCfg->ulAmCfg.statProhTmr;
1694          break;
1695       }
1696       case RLC_UM_BI_DIRECTIONAL :
1697       {
1698          if(!lcCfg->u.umBiDirCfg)
1699          {
1700             DU_ALLOC_SHRABL_BUF(lcCfg->u.umBiDirCfg, sizeof(UmBiDirBearerCfg));
1701             if(!lcCfg->u.umBiDirCfg)
1702                 return RFAILED;
1703          }
1704          /* UL UM BI DIR INFO */
1705          lcCfg->u.umBiDirCfg->ulUmCfg.snLenUlUm  = f1UeDbLcCfg->u.umBiDirCfg->ulUmCfg.snLenUlUm;  
1706          lcCfg->u.umBiDirCfg->ulUmCfg.reAssemTmr = f1UeDbLcCfg->u.umBiDirCfg->ulUmCfg.reAssemTmr;
1707          /* DL UM BI DIR INFO */
1708          lcCfg->u.umBiDirCfg->dlUmCfg.snLenDlUm  = f1UeDbLcCfg->u.umBiDirCfg->dlUmCfg.snLenDlUm;
1709          break;
1710       }
1711       case RLC_UM_UNI_DIRECTIONAL_UL :
1712       {
1713          if(!lcCfg->u.umUniDirUlCfg)
1714          {
1715             DU_ALLOC_SHRABL_BUF(lcCfg->u.umUniDirUlCfg, sizeof(UmUniDirUlBearerCfg));
1716             if(!lcCfg->u.umUniDirUlCfg)
1717                return RFAILED;
1718          }
1719          lcCfg->u.umUniDirUlCfg->ulUmCfg.snLenUlUm  = f1UeDbLcCfg->u.umUniDirUlCfg->ulUmCfg.snLenUlUm;  
1720          lcCfg->u.umUniDirUlCfg->ulUmCfg.reAssemTmr = f1UeDbLcCfg->u.umUniDirUlCfg->ulUmCfg.reAssemTmr;
1721          break;
1722
1723       }
1724       case RLC_UM_UNI_DIRECTIONAL_DL :
1725       {
1726          if(!lcCfg->u.umUniDirDlCfg)
1727          {
1728             DU_ALLOC_SHRABL_BUF(lcCfg->u.umUniDirDlCfg, sizeof(UmUniDirDlBearerCfg));
1729             if(!lcCfg->u.umUniDirDlCfg)
1730                return RFAILED;
1731          }
1732          lcCfg->u.umUniDirDlCfg->dlUmCfg.snLenDlUm  = f1UeDbLcCfg->u.umUniDirDlCfg->dlUmCfg.snLenDlUm;
1733          break;
1734       }
1735       default:
1736          DU_LOG("\nERROR  -->  DU_APP: Invalid Rlc Mode %d at fillRlcCfgToAddMod()", lcCfg->rlcMode);
1737          return RFAILED;
1738    }
1739    return ROK;
1740 }
1741
1742 /*******************************************************************
1743  *
1744  * @brief To update DuUeCb Rlc Lc Cfg
1745  *
1746  * @details
1747  *
1748  *    Function : duUpdateRlcLcCfg
1749  *    Functionality:  update DuUeCb Rlc Lc Cfg
1750  *
1751  * @params[in] DuUeCb Pointer
1752  *             F1UeContextSetupDb pointer 
1753  * @return ROK     - success
1754  *         RFAILED - failure
1755  *
1756  * ****************************************************************/
1757
1758 uint8_t duUpdateRlcLcCfg(RlcUeCfg *rlcUeCfg, F1UeContextSetupDb *f1UeDb)
1759 {
1760    uint8_t ret, lcIdx, dbIdx, numLcs, lcDelIdx;
1761
1762    ret = ROK;
1763    for(dbIdx = 0; (dbIdx < f1UeDb->duUeCfg.numRlcLcs && ret ==ROK); dbIdx++)
1764    {
1765       numLcs = rlcUeCfg->numLcs;
1766       for(lcIdx = 0; lcIdx < numLcs; lcIdx++)
1767       {
1768          if(f1UeDb->duUeCfg.rlcLcCfg[dbIdx].lcId == rlcUeCfg->rlcLcCfg[lcIdx].lcId)
1769          {
1770             if(f1UeDb->duUeCfg.rlcLcCfg[dbIdx].configType == CONFIG_MOD)
1771             {
1772                ret = fillRlcCfgToAddMod(&rlcUeCfg->rlcLcCfg[lcIdx], &f1UeDb->duUeCfg.rlcLcCfg[dbIdx]);
1773             }
1774             else if(f1UeDb->duUeCfg.rlcLcCfg[dbIdx].configType == CONFIG_DEL)
1775             {
1776                /* Free memory at matched lcIdx index */
1777                freeRlcLcCfg(&rlcUeCfg->rlcLcCfg[lcIdx]);
1778                rlcUeCfg->numLcs--;
1779                for(lcDelIdx = lcIdx; lcDelIdx < rlcUeCfg->numLcs; lcDelIdx++)
1780                {
1781                   ret = fillRlcCfgToAddMod(&rlcUeCfg->rlcLcCfg[lcDelIdx], &rlcUeCfg->rlcLcCfg[lcDelIdx+1]);
1782                   freeRlcLcCfg(&rlcUeCfg->rlcLcCfg[lcDelIdx+1]);
1783                   if(ret == RFAILED)
1784                   {
1785                      DU_LOG("\nERROR  -->  DU APP : Failed to delete LC at Idx %d in duUpdateRlcCfg()", lcDelIdx);
1786                      break;
1787                   }
1788                }
1789             }
1790          }
1791       }
1792       if(f1UeDb->duUeCfg.rlcLcCfg[dbIdx].configType == CONFIG_ADD)
1793       {
1794          ret = fillRlcCfgToAddMod(&rlcUeCfg->rlcLcCfg[lcIdx], &f1UeDb->duUeCfg.rlcLcCfg[dbIdx]);
1795          if(ret == ROK)
1796             rlcUeCfg->numLcs++;
1797       }
1798    }
1799    return ret;
1800 }
1801
1802
1803 /*******************************************************************
1804  *
1805  * @brief Function to fill Tunnel Config to Add/Mod
1806  * 
1807  *
1808  * @details
1809  *
1810  *    Function : fillTnlCfgToAddMod
1811  *
1812  *    Functionality: Function to fill tunnel Config to Add/Mod
1813  *
1814  * @params[in] Pointer to tnlCfgDb,
1815  *             pointer to f1TnlCfg
1816  * @return ROK     - success
1817  *         RFAILED - failure
1818  *
1819  * ****************************************************************/
1820 uint8_t fillTnlCfgToAddMod(UpTnlCfg **ueCbTnlCfg, UpTnlCfg *f1TnlCfg)
1821 {
1822    if(*ueCbTnlCfg == NULLP)
1823    {
1824       /* copying to DuCb Tnl Cfg */
1825       DU_ALLOC(*ueCbTnlCfg, sizeof(UpTnlCfg));
1826       if(*ueCbTnlCfg == NULLP)
1827       {
1828          DU_LOG("\nERROR  -->  DU_APP : fillTnlCfgToAddMod: Memory Alloc failed for drbId[%d]", f1TnlCfg->drbId);
1829          return RFAILED;
1830       }
1831    }
1832    memset(*ueCbTnlCfg, 0, sizeof(UpTnlCfg));
1833    (*ueCbTnlCfg)->configType = f1TnlCfg->configType;
1834    (*ueCbTnlCfg)->cellId    = f1TnlCfg->cellId;
1835    (*ueCbTnlCfg)->ueIdx     = f1TnlCfg->ueIdx;
1836    (*ueCbTnlCfg)->drbId     = f1TnlCfg->drbId;
1837    if(f1TnlCfg->tnlCfg1)
1838    {
1839       if((*ueCbTnlCfg)->tnlCfg1 == NULLP)
1840       {
1841          DU_ALLOC((*ueCbTnlCfg)->tnlCfg1, sizeof(GtpTnlCfg));
1842          if((*ueCbTnlCfg)->tnlCfg1 == NULLP)
1843          {
1844             DU_LOG("\nERROR  -->  DU_APP : fillTnlCfgToAddMod: Memory Alloc failed for tnlCfg1 for drbId[%d]", f1TnlCfg->drbId);
1845             return RFAILED;
1846          }
1847       }
1848       memset((*ueCbTnlCfg)->tnlCfg1, 0, sizeof(GtpTnlCfg));
1849       (*ueCbTnlCfg)->tnlCfg1->teId = f1TnlCfg->tnlCfg1->teId;
1850       (*ueCbTnlCfg)->tnlCfg1->ulTnlAddress = f1TnlCfg->tnlCfg1->ulTnlAddress;
1851       (*ueCbTnlCfg)->tnlCfg1->dlTnlAddress = f1TnlCfg->tnlCfg1->dlTnlAddress;
1852    }
1853    return ROK;
1854 }
1855
1856 /*******************************************************************
1857  *
1858  * @brief Processing the tunnel Request to EGTP
1859  *        
1860  * @details
1861  *
1862  *    Function : duProcEgtpTunnelCfg
1863  *
1864  *    Functionality: Processing the tunnel Request to EGTP
1865  *
1866  * @params[in] UptnlCfg *duTnlCfg, UpTnlCfg *f1TnlCfg 
1867  * @return ROK     - success
1868  *         RFAILED - failure
1869  *
1870  * ****************************************************************/
1871
1872 uint8_t duProcEgtpTunnelCfg(uint8_t ueCbIdx, UpTnlCfg *duTnlCfg, UpTnlCfg *f1TnlCfg)
1873 {
1874    uint8_t ret = RFAILED, delIdx;
1875
1876    if(f1TnlCfg->tnlCfg1 == NULLP)
1877    {
1878       DU_LOG("\nERROR  -->  DU_APP : Tunnel config not found");
1879       return ret;
1880    }
1881
1882    if(f1TnlCfg->configType == CONFIG_ADD)
1883    {
1884       if(duSendEgtpTnlMgmtReq(EGTP_TNL_MGMT_ADD, NULLP, f1TnlCfg->tnlCfg1) == ROK)
1885       {
1886          if(fillTnlCfgToAddMod(&duCb.upTnlCfg[duCb.numDrb], f1TnlCfg) == ROK)
1887          {
1888             duCb.numDrb++;
1889             ret = ROK;
1890          }
1891       }      
1892    }
1893    else if(f1TnlCfg->configType == CONFIG_MOD)
1894    {
1895       if(duSendEgtpTnlMgmtReq(EGTP_TNL_MGMT_MOD, duTnlCfg->tnlCfg1->teId, f1TnlCfg->tnlCfg1) == ROK)
1896       {
1897          if(fillTnlCfgToAddMod(&duTnlCfg, f1TnlCfg) == ROK)
1898          {
1899             ret = ROK;
1900          }
1901       }   
1902    }
1903    else if(f1TnlCfg->configType == CONFIG_DEL)
1904    {
1905       if(duSendEgtpTnlMgmtReq(EGTP_TNL_MGMT_DEL, duTnlCfg->tnlCfg1->teId, f1TnlCfg->tnlCfg1) == ROK)
1906       {    
1907          /* Free memory at drbIdx */
1908          DU_FREE(duTnlCfg->tnlCfg1, sizeof(GtpTnlCfg));
1909          DU_FREE(duTnlCfg, sizeof(UpTnlCfg));
1910          duCb.numDrb--;
1911          for(delIdx = ueCbIdx; delIdx < duCb.numDrb; delIdx++)
1912          {
1913             /* moving all elements one index ahead */
1914             ret = fillTnlCfgToAddMod(&duCb.upTnlCfg[delIdx], duCb.upTnlCfg[delIdx+1]);
1915             if(ret != ROK)
1916             {
1917                return ret;
1918             }
1919          }
1920       }   
1921    }
1922    return ret;
1923 }
1924
1925 /***********************************************************************
1926  *
1927  * @brief Function to fill Tunnel Config
1928  *        and sends tunnel Req to EGTP
1929  * 
1930  *
1931  * @details
1932  *
1933  *    Function : duUpdateTunnelCfgDb
1934  *
1935  *    Functionality: Function to fill tunnel Config
1936  *                   and sends tunnel Cfg Req to EGTP
1937  *
1938  * @params[in] ueIdx, cellId, DuUeCfg 
1939  * @return ROK     - success
1940  *         RFAILED - failure
1941  *
1942  * ****************************************************************/
1943
1944 uint8_t duUpdateTunnelCfgDb(uint8_t ueIdx, uint8_t cellId, DuUeCfg *duUeCfg)
1945 {
1946    uint8_t ret = ROK, drbIdx, duCbDrbIdx;
1947    bool drbFound = false;
1948
1949    /*If Add/Mod tunnels request for that DRB is successful in EGTP */
1950    /*then update drbId and tunnel Info in duCb */
1951    for(drbIdx=0; drbIdx < duUeCfg->numDrb; drbIdx++)
1952    {
1953       duUeCfg->upTnlInfo[drbIdx].cellId = cellId;
1954       duUeCfg->upTnlInfo[drbIdx].ueIdx = ueIdx;
1955       for(duCbDrbIdx = 0; duCbDrbIdx < duCb.numDrb; duCbDrbIdx++)
1956       {
1957          if(duCb.upTnlCfg[duCbDrbIdx]->drbId == duUeCfg->upTnlInfo[drbIdx].drbId)
1958          {
1959             drbFound = true; /* existing DRB */
1960             if(duProcEgtpTunnelCfg(duCbDrbIdx, duCb.upTnlCfg[duCbDrbIdx], &duUeCfg->upTnlInfo[drbIdx]) != ROK)
1961             {
1962                DU_LOG("\nERROR  -> DU_APP : duUpdateTunnelCfgDb: Failed to modify tunnel req for Drb id[%d]",
1963                duUeCfg->upTnlInfo[drbIdx].drbId);
1964                ret = RFAILED;
1965             }
1966             break;
1967          }
1968          else
1969             drbFound = false;
1970       }
1971       if(!drbFound && ret == ROK)/* new DRB to Add */
1972       {
1973          if(duProcEgtpTunnelCfg(NULLP, NULLP, &duUeCfg->upTnlInfo[drbIdx]) != ROK)
1974          {
1975             DU_LOG("\nERROR  -> DU_APP : duUpdateTunnelCfgDb: Failed to add tunnel req for Drb id[%d]",
1976             duUeCfg->upTnlInfo[drbIdx].drbId);
1977             ret = RFAILED;
1978             break;
1979          }
1980       }
1981       else
1982          break;
1983    }
1984    return ret;
1985 }
1986
1987 /*******************************************************************
1988  *
1989  * @brief @brief To update DuUeCb Mac and Rlc Ue Cfg
1990  * 
1991  *
1992  * @details
1993  *
1994  *    Function : duUpdateDuUeCbCfg
1995  *
1996  *    Functionality: update DuUeCb Mac and Rlc Ue Cfg
1997  *
1998  * @params[in] ueIdx, cellIdx 
1999  * @return ROK     - success
2000  *         RFAILED - failure
2001  *
2002  * ****************************************************************/
2003
2004 uint8_t duUpdateDuUeCbCfg(uint8_t ueIdx, uint8_t cellId)
2005 {
2006    uint8_t ret = ROK, cellIdx = 0, crnti=0;
2007    DuUeCb *ueCb = NULLP;
2008
2009    GET_CELL_IDX(cellId, cellIdx);
2010    
2011    if((duCb.actvCellLst[cellIdx]->ueCb[ueIdx-1].macUeCfg. \
2012          macUeCfgState == UE_RECFG_COMPLETE) &&
2013       (duCb.actvCellLst[cellIdx]->ueCb[ueIdx-1].rlcUeCfg. \
2014          rlcUeCfgState == UE_RECFG_COMPLETE))
2015    {
2016       ueCb = &duCb.actvCellLst[cellIdx]->ueCb[ueIdx-1];
2017
2018       /*Filling RLC Ue Cfg */
2019       ueCb->rlcUeCfg.cellId = cellId;
2020       ueCb->rlcUeCfg.ueIdx  = ueIdx;
2021       ret = duUpdateRlcLcCfg(&ueCb->rlcUeCfg, ueCb->f1UeDb);
2022       if(ret == ROK)
2023       {
2024          /*Filling MAC Ue Cfg */
2025          GET_CRNTI(crnti, ueIdx);
2026          ueCb->macUeCfg.cellId = cellId;
2027          ueCb->macUeCfg.ueIdx  = ueIdx;
2028          ueCb->macUeCfg.crnti  = crnti;
2029          ret = duUpdateMacCfg(&ueCb->macUeCfg, ueCb->f1UeDb);
2030          if(ret == RFAILED)
2031             DU_LOG("\nERROR  -->  DU APP : Failed while updating MAC LC Config at duUpdateDuUeCbCfg()");
2032          else
2033          {
2034             if(duUpdateTunnelCfgDb(ueIdx, cellId, &ueCb->f1UeDb->duUeCfg) != ROK)
2035             {
2036                DU_LOG("\nERROR  -->  DU_APP : Failed to establish tunnel in duUpdateDuUeCbCfg()");
2037                return RFAILED;
2038             }
2039          }
2040       }
2041       else
2042          DU_LOG("\nERROR  -->  DU APP : Failed while updating RLC LC Config at duUpdateDuUeCbCfg()");
2043    }
2044    else
2045       ret = RFAILED;
2046    return ret;
2047 }
2048
2049 /*******************************************************************
2050  *
2051  * @brief Handle UE config response from MAC
2052  *
2053  * @details
2054  *
2055  *    Function : DuProcMacUeCfgRsp
2056  *
2057  *    Functionality: Handle UE Config response from MAC
2058  *
2059  * @params[in] Pointer to MacUeCfgRsp and Pst 
2060  * @return ROK     - success
2061  *         RFAILED - failure
2062  *
2063  * ****************************************************************/
2064 uint8_t DuProcMacUeCfgRsp(Pst *pst, MacUeCfgRsp *cfgRsp)
2065 {
2066    uint8_t ret = ROK;
2067
2068    if(cfgRsp)
2069    {
2070       if(cfgRsp->result == MAC_DU_APP_RSP_OK)
2071       {
2072          if(pst->event == EVENT_MAC_UE_CREATE_RSP)
2073          {
2074             DU_LOG("\nINFO   -->  DU APP : MAC UE Create Response : SUCCESS [UE IDX : %d]", cfgRsp->ueIdx);
2075             duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueIdx -1].\
2076                macUeCfg.macUeCfgState = UE_CREATE_COMPLETE;
2077          }
2078          else if(pst->event == EVENT_MAC_UE_RECONFIG_RSP)
2079          {
2080             DU_LOG("\nINFO   -->  DU APP : MAC UE Reconfig Response : SUCCESS [UE IDX : %d]", cfgRsp->ueIdx);
2081             duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueIdx -1].\
2082                macUeCfg.macUeCfgState = UE_RECFG_COMPLETE;
2083             if((ret = duUpdateDuUeCbCfg(cfgRsp->ueIdx, cfgRsp->cellId)) == ROK)
2084                BuildAndSendUeCtxtRsp(cfgRsp->ueIdx, cfgRsp->cellId);
2085          }
2086       }
2087       else
2088       {
2089          DU_LOG("\nERROR  -->  DU APP : MAC UE CFG Response for EVENT[%d]: FAILURE [UE IDX : %d]", pst->event, cfgRsp->ueIdx);
2090          if(pst->event == EVENT_MAC_UE_RECONFIG_RSP)
2091          {
2092             //TODO: Send the failure case in Ue Context Setup Response
2093          }
2094          ret = RFAILED;
2095       }
2096       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(MacUeCfgRsp));
2097    }
2098    else
2099    {
2100       DU_LOG("\nERROR  -->  DU APP : Received MAC Ue Config Response is NULL at DuProcMacUeCfgRsp()");
2101       ret = RFAILED;
2102    }
2103    return ret;
2104 }
2105
2106
2107 /*******************************************************************
2108  *
2109  * @brief Processes UE create Req to RLC UL
2110  *
2111  * @details
2112  *
2113  *    Function : duBuildAndSendUeCreateReqToRlc
2114  *
2115  *    Functionality: 
2116  *     Processes UE create Req to RLC UL
2117  * 
2118  *  @params[in]  cellId,
2119  *               ueIdx,
2120  *               Pointer to RlcUeCfg
2121  *  @return ROK     - success
2122  *          RFAILED - failure
2123  * 
2124  *****************************************************************/
2125
2126 uint8_t duBuildAndSendUeCreateReqToRlc(uint16_t cellId, uint8_t ueIdx,\
2127    RlcUeCfg *duRlcUeCfg)
2128 {
2129    uint8_t  ret = ROK;
2130    RlcUeCfg *rlcUeCfg = NULLP;
2131    Pst       pst;
2132   
2133    ret = fillRlcUeCfg(cellId, ueIdx, NULL, duRlcUeCfg);
2134    if(ret == RFAILED)
2135    {
2136       DU_LOG("\nERROR  -->  DU APP : Failed to fill Rlc Ue Cfg at duBuildAndSendUeCreateReqToRlc()");
2137       return ret;
2138    }
2139
2140    FILL_PST_DUAPP_TO_RLC(pst, RLC_UL_INST, EVENT_RLC_UE_CREATE_REQ);
2141    /* Copying ueCfg to a sharable buffer */
2142    DU_ALLOC_SHRABL_BUF(rlcUeCfg, sizeof(RlcUeCfg));
2143    if(rlcUeCfg)
2144    {
2145       memset(rlcUeCfg, 0, sizeof(RlcUeCfg));
2146       memcpy(rlcUeCfg, duRlcUeCfg, sizeof(RlcUeCfg));
2147       /* Processing one Ue at a time to RLC */
2148       DU_LOG("\nDEBUG   -->  DU_APP: Sending UE create request to RLC UL");
2149       ret = (*packRlcUeCreateReqOpts[pst.selector])(&pst, rlcUeCfg);
2150       if(ret == RFAILED)
2151       {
2152          DU_LOG("\nERROR  -->  DU_APP : Failure in sending Ue Create Req to RLC");
2153          DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, rlcUeCfg, sizeof(RlcUeCfg));
2154          ret = RFAILED;
2155       }
2156    }
2157    else
2158    {
2159       DU_LOG("\n ERROR  -->  DU APP : Memory alloc failed at duBuildAndSendUeCreateReqToRlc()");
2160       ret = RFAILED;
2161    }
2162    return ret;
2163 }
2164
2165 /*******************************************************************
2166  *
2167  * @brief Processes UE create Rsp received from RLC UL
2168  *
2169  * @details
2170  *
2171  
2172  *    Function : DuProcRlcUeCfgRsp
2173  *
2174  *    Functionality: 
2175  *     Processes UE create Rsp received from RLC UL
2176  * 
2177  *  @params[in]  Post structure
2178  *               Pointer to RlcCfgCfm
2179  *  @return ROK     - success
2180  *          RFAILED - failure
2181  * 
2182  *****************************************************************/
2183 uint8_t DuProcRlcUeCfgRsp(Pst *pst, RlcUeCfgRsp *cfgRsp)
2184 {
2185    uint8_t ret = ROK;
2186
2187    if(cfgRsp)
2188    {
2189       if(cfgRsp->result == RLC_DU_APP_RSP_OK)
2190       {
2191          if(pst->event == EVENT_RLC_UE_CREATE_RSP)
2192          {
2193             DU_LOG("\nINFO   -->  DU_APP: RLC UE Create Response : SUCCESS [UE IDX:%d]", cfgRsp->ueIdx);
2194             duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueIdx -1].\
2195                rlcUeCfg.rlcUeCfgState = UE_CREATE_COMPLETE;
2196          }
2197          else if(pst->event == EVENT_RLC_UE_RECONFIG_RSP)
2198          {
2199             DU_LOG("\nINFO   -->  DU_APP: RLC UE Reconfig Response : SUCCESS [UE IDX:%d]", cfgRsp->ueIdx);
2200             duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueIdx -1].\
2201                rlcUeCfg.rlcUeCfgState = UE_RECFG_COMPLETE;
2202             if((ret = duUpdateDuUeCbCfg(cfgRsp->ueIdx, cfgRsp->cellId)) == ROK)
2203                BuildAndSendUeCtxtRsp(cfgRsp->ueIdx, cfgRsp->cellId);
2204          }
2205       }
2206       else
2207       {
2208          DU_LOG("\nERROR  -->  DU_APP: RLC UE CFG Response for EVENT[%d] : FAILED [UE IDX : %d, REASON :%d]",\
2209             pst->event, cfgRsp->ueIdx, cfgRsp->reason);
2210          if((pst->event == EVENT_RLC_UE_RECONFIG_RSP))
2211          {
2212             //TODO: update failure case in ue Context setup Response
2213          }
2214          ret = RFAILED;
2215       }
2216       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(RlcUeCfgRsp));
2217    }
2218    else
2219    {
2220       DU_LOG("\nERROR  -->  DU_APP: Received RLC Ue Config Response is NULL at DuProcRlcUeCfgRsp()");
2221       ret = RFAILED;
2222    }
2223    return ret;
2224 }
2225
2226
2227
2228 /*******************************************************************
2229  *
2230  * @brief Builds and Send Ue Reconfig Req to RLC
2231  *
2232  * @details
2233  *
2234  *    Function : duBuildAndSendUeReCfgReqToRLC
2235  *
2236  *    Functionality: Builds and Send Ue Reconfig Req to RLC
2237  *
2238  * @params[in] cellId, crnti
2239  *             DuUeCfg *ueCfgDb
2240  *             RlcUeCfg *rlcUeCfg
2241  * @return ROK     - success
2242  *         RFAILED - failure
2243  *
2244  * ****************************************************************/
2245
2246 uint8_t duBuildAndSendUeReCfgReqToRlc(uint8_t cellId, uint8_t crnti, DuUeCfg *ueCfgDb)
2247 {
2248    uint8_t ret = ROK, ueIdx = 0;
2249    RlcUeCfg *rlcUeCfg = NULLP;
2250
2251    GET_UE_IDX(crnti, ueIdx);
2252    DU_ALLOC_SHRABL_BUF(rlcUeCfg, sizeof(RlcUeCfg));
2253    if(rlcUeCfg)
2254    {
2255       memset(rlcUeCfg, 0, sizeof(RlcUeCfg));
2256       ret = fillRlcUeCfg(cellId, ueIdx, ueCfgDb, rlcUeCfg);
2257       if(ret == RFAILED)
2258          DU_LOG("\nERROR  -->  DU APP : Failed at duBuildAndSendUeReCfgReqToRlc()");
2259       else
2260          ret = sendUeReCfgReqToRlc(rlcUeCfg);
2261    }
2262    else
2263    {
2264       DU_LOG("\nERROR  -->  DU APP : Memory Alloc failed at duBuildAndSendUeReCfgReqToRlc()");
2265       ret = RFAILED;
2266    }
2267    return ret;
2268 }
2269
2270 /*******************************************************************
2271  *
2272  * @brief Builds and Send Ue Reconfig Req to MAC
2273  *
2274  * @details
2275  *
2276  *    Function : duBuildAndSendUeReCfgReqToMac
2277  *
2278  *    Functionality: Builds and Send Ue Reconfig Req to MAC
2279  *
2280  * @params[in] CellGroupConfigRrc_t *macCellGrpCfg
2281  *             DuUeCfg *ueCfgDb
2282  *             MacUeCfg    *macUeCfg
2283  * @return ROK     - success
2284  *         RFAILED - failure
2285  *
2286  * ****************************************************************/
2287
2288 uint8_t duBuildAndSendUeReCfgReqToMac(uint8_t cellId, uint8_t crnti, DuUeCfg *ueCfgDb)
2289 {
2290    uint8_t ret, ueIdx = 0;
2291    MacUeCfg *macUeCfg = NULLP;
2292
2293    ret = ROK;
2294    GET_UE_IDX(crnti, ueIdx);
2295
2296    DU_ALLOC_SHRABL_BUF(macUeCfg, sizeof(MacUeCfg));
2297    if(macUeCfg)
2298    {
2299       memset(macUeCfg, 0, sizeof(MacUeCfg));
2300       ret = fillMacUeCfg(cellId, ueIdx, crnti, ueCfgDb, macUeCfg);
2301       if(ret == RFAILED)
2302          DU_LOG("\nERROR  -->  DU APP : Failed to fill Mac Ue Cfg at duBuildAndSendUeReCfgReqToMac()");
2303       else
2304          ret = sendUeReCfgReqToMac(macUeCfg);
2305    }
2306    else
2307    {
2308       DU_LOG("\nERROR  -->  DU APP : Memory alloc failed for macUeCfg at duBuildAndSendUeReCfgReqToMac()");
2309       ret = RFAILED;
2310    }
2311    return ret;
2312 }
2313
2314 /*******************************************************************
2315  *
2316  * @brief Build and Send Ue context setup request
2317  *
2318  * @details
2319  *
2320  
2321  *    Function : duBuildAndSendUeContextSetupReq
2322  *
2323  *    Functionality: 
2324  *     Build and Send Ue context setup request
2325  * 
2326  *  @params[in]  cellId, crnti, DuUeCfg pointer
2327  *  @return ROK     - success
2328  *          RFAILED - failure
2329  * 
2330  *****************************************************************/
2331
2332 uint8_t duBuildAndSendUeContextSetupReq(uint16_t cellId, uint16_t crnti, DuUeCfg *duUeCfg)
2333 {
2334    uint8_t ret = ROK;
2335
2336    DU_LOG("\nDEBUG   -->  DU_APP: Processing Ue Context Setup Request for cellId [%d]", cellId);
2337    /* Filling RLC Ue Reconfig */ 
2338    ret = duBuildAndSendUeReCfgReqToRlc(cellId, crnti, duUeCfg);
2339    if(ret == RFAILED)
2340       DU_LOG("\nERROR  -->  DU APP : Failed to build ctxt setup req for RLC at duBuildAndSendUeContextSetupReq()");
2341    
2342    /* Filling MAC Ue Reconfig */
2343    ret = duBuildAndSendUeReCfgReqToMac(cellId, crnti, duUeCfg);
2344    if(ret == RFAILED)
2345       DU_LOG("\nERROR  -->  DU APP : Failed at build ctxt setup req for MAC at duBuildAndSendUeContextSetupReq()");
2346
2347    return ret;
2348 }
2349
2350 /*******************************************************************
2351  *
2352  * @brief Processes DL Rsp received from RLC DL
2353  *
2354  * @details
2355  *
2356  
2357  *    Function : DuProcRlcDlRrcMsgRsp
2358  *
2359  *    Functionality: 
2360  *     Processes UE Rsp received from RLC DL
2361  * 
2362  *  @params[in]  Post structure
2363  *               Pointer to RlcCfgCfm
2364  *  @return ROK     - success
2365  *          RFAILED - failure
2366  * 
2367  *****************************************************************/
2368 uint8_t DuProcRlcDlRrcMsgRsp(Pst *pst, RlcDlRrcMsgRsp *dlRrcMsg)
2369 {
2370    uint8_t ret = ROK, ueIdx = 0;
2371    DuUeCb *ueCb = NULLP;
2372
2373    if(dlRrcMsg->state == TRANSMISSION_COMPLETE)
2374    {
2375       GET_UE_IDX(dlRrcMsg->crnti, ueIdx);
2376       ueCb = &duCb.actvCellLst[dlRrcMsg->cellId -1]->ueCb[ueIdx -1];
2377       if(ueCb->f1UeDb)
2378       {
2379         ret = duBuildAndSendUeContextSetupReq(dlRrcMsg->cellId, dlRrcMsg->crnti, &ueCb->f1UeDb->duUeCfg);
2380         if(ret == RFAILED)
2381            DU_LOG("\nERROR  -->  DU APP : Failed to process UE Context Setup Request in DuProcRlcDlRrcMsgRsp()");
2382       }
2383    }
2384    else
2385       DU_LOG("\nERROR  -->  DU APP : Failed to transmit DL RRC Msg");
2386
2387    DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, dlRrcMsg, sizeof(RlcDlRrcMsgRsp));
2388    return ret;
2389 }
2390 /*******************************************************************
2391  *
2392  * @brief Process UE context setup request from CU
2393  *
2394  * @details
2395  *
2396  *    Function : duProcUeContextSetupRequest
2397  *
2398  *    Functionality: Process UE context setup request from CU
2399  *
2400  * @params[in] F1AP message
2401  * @return ROK     - success
2402  *         RFAILED - failure
2403  *
2404  * ****************************************************************/
2405
2406 uint8_t duProcUeContextSetupRequest(DuUeCb *ueCb)
2407 {
2408    uint8_t ret, cellId;
2409    
2410    ret = ROK;
2411    if(ueCb)
2412    {
2413       cellId = duCb.actvCellLst[ueCb->f1UeDb->cellIdx]->cellId;
2414       /* Send DL RRC msg for security Mode */
2415       if(ueCb->f1UeDb->dlRrcMsg)
2416       {
2417          if(ueCb->f1UeDb->dlRrcMsg->rrcMsgPdu)
2418          {
2419             /* Sending DL RRC Message to RLC */
2420             ret = duBuildAndSendDlRrcMsgToRlc(cellId, ueCb->rlcUeCfg, ueCb->f1UeDb->dlRrcMsg);
2421             if(ret == RFAILED)
2422             {
2423                DU_LOG("\nERROR  -->  DU APP : Failed to send DL RRC msg in duProcUeContextSetupRequest()");
2424                DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, ueCb->f1UeDb->dlRrcMsg->rrcMsgPdu,\
2425                   ueCb->f1UeDb->dlRrcMsg->rrcMsgSize);
2426                DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, ueCb->f1UeDb->dlRrcMsg, sizeof(F1DlRrcMsg));
2427             }
2428          }
2429       }
2430       else
2431       {
2432          ret = duBuildAndSendUeContextSetupReq(cellId, ueCb->crnti, &ueCb->f1UeDb->duUeCfg);
2433          if(ret == RFAILED)
2434          {
2435             DU_LOG("\nERROR  -->  DU APP : Failed to build ue context setup Req in duProcUeContextSetupRequest()");
2436          }
2437       }
2438    }
2439    else
2440    {
2441       //TODO: To send the failure cause in UeContextSetupRsp
2442       DU_LOG("ERROR  -->  DU APP : Failed to process UE CNTXT SETUP REQ at duProcUeContextSetupRequest()");
2443    }
2444    return ret;
2445 }
2446
2447 /**********************************************************************
2448   End of file
2449  ***********************************************************************/