[Epic-ID: ODUHIGH-406][Task-ID: ODUHIGH-450] PAGING memory fixes
[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_mgr.h"
33 #include "du_utils.h"
34 #include "du_f1ap_msg_hdl.h"
35 #include "du_ue_mgr.h"
36
37 #ifdef O1_ENABLE
38 #include "AlarmInterface.h"
39 #include "CmInterface.h"
40 #endif
41
42 DuMacDlCcchInd packMacDlCcchIndOpts[] =
43 {
44    packMacDlCcchInd,           /* Loose coupling */
45    MacProcDlCcchInd,           /* TIght coupling */
46    packMacDlCcchInd            /* Light weight-loose coupling */
47 };
48
49 DuMacUeCreateReq packMacUeCreateReqOpts[] =
50 {
51    packDuMacUeCreateReq,       /* Loose coupling */
52    MacProcUeCreateReq,         /* TIght coupling */
53    packDuMacUeCreateReq        /* Light weight-loose coupling */
54 };
55
56 DuRlcUeCreateReq packRlcUeCreateReqOpts[] =
57 {
58    packDuRlcUeCreateReq,       /* Loose coupling */
59    RlcProcUeCreateReq,         /* TIght coupling */
60    packDuRlcUeCreateReq        /* Light weight-loose coupling */
61 };
62
63 DuDlRrcMsgToRlcFunc duSendDlRrcMsgToRlcOpts[] =
64 {
65    packDlRrcMsgToRlc,          /* Loose coupling */ 
66    RlcProcDlRrcMsgTransfer,    /* Tight coupling */
67    packDlRrcMsgToRlc           /* Light weight-loose coupling */
68 };
69
70 DuRlcUeReconfigReq packRlcUeReconfigReqOpts[] =
71 {
72    packDuRlcUeReconfigReq,     /* Loose coupling */
73    RlcProcUeReconfigReq,       /* TIght coupling */
74    packDuRlcUeReconfigReq      /* Light weight-loose coupling */
75 };
76
77 DuMacUeReconfigReq packMacUeReconfigReqOpts[] =
78 {
79    packDuMacUeReconfigReq,     /* Loose coupling */
80    MacProcUeReconfigReq,       /* TIght coupling */
81    packDuMacUeReconfigReq      /* Light weight-loose coupling */
82 };
83
84 DuMacRachRsrcReq packMacRachRsrcReqOpts[] = 
85 {
86    packDuMacRachRsrcReq,      /* Loose coupling */
87    MacProcRachRsrcReq,        /* Tight coupling */
88    packDuMacRachRsrcReq       /* Light weight-loose coupling */
89 };
90
91 DuRlcDlUserDataToRlcFunc duSendRlcDlUserDataToRlcOpts[] =
92 {
93    packRlcDlUserDataToRlc,     /* Loose coupling */ 
94    RlcProcDlUserDataTransfer,  /* Tight coupling */
95    packRlcDlUserDataToRlc      /* Light weight-loose coupling */
96 };
97
98 DuMacUeDeleteReq packMacUeDeleteReqOpts[] =
99 {
100    packDuMacUeDeleteReq,       /* Loose coupling */
101    MacProcUeDeleteReq,         /* TIght coupling */
102    packDuMacUeDeleteReq        /* Light weight-loose coupling */
103 };
104
105 DuRlcUeDeleteReq packRlcUeDeleteReqOpts[] =
106 {
107    packDuRlcUeDeleteReq,       /* Loose coupling */
108    RlcProcUeDeleteReq,         /* TIght coupling */
109    packDuRlcUeDeleteReq        /* Light weight-loose coupling */
110 };
111
112 /******************************************************************
113  *
114  * @brief Function to return Drb LcId
115  *
116  * @details
117  *
118  *    Function : getDrbLcId
119  *
120  *    Functionality: Function to return Drb LcId
121  *
122  * @params[in] drbBitMap
123  *
124  * Returns: lcId - SUCCESS
125  *         RFAILED - FAILURE
126  *****************************************************************/
127
128 uint8_t getDrbLcId(uint32_t *drbBitMap)
129 {
130    uint8_t bitMask = 1, bitPos = 0;
131    bitPos = MIN_DRB_LCID;
132
133    while(bitPos <= MAX_DRB_LCID)
134    {
135       if((*drbBitMap & (bitMask << bitPos)) == 0)
136       {
137          *drbBitMap = ((bitMask << bitPos)| *drbBitMap);
138          return bitPos;
139       }
140       else
141       {
142          bitPos++;
143       }
144    }
145    DU_LOG("\nERROR   -->  DU_APP: Max LC Reached in getDrbLcId()");
146    return RFAILED;
147 }
148
149 /*******************************************************************
150  *
151  * @brief Function to fillDlUserDataInfo
152  *
153  * @details
154  *
155  *    Function : fillDlUserDataInfo
156  *
157  *    Functionality:
158  *      Function to fillDlUserDataInfo
159  *
160  * @params[in] teId,
161  *             dlDataMsgInfo
162  *
163  * @return ROK     - success
164  *         RFAILED - failure
165  *
166  * ****************************************************************/
167
168 uint8_t fillDlUserDataInfo(uint32_t teId, RlcDlUserDataInfo *dlDataMsgInfo)
169 {
170    uint8_t teIdx = 0;
171
172    /*Traversing the duCb to find the CellId, UeId, LCID corresponding to TEID*/
173    for(teIdx = 0; teIdx < duCb.numTeId; teIdx++)
174    {
175       if(duCb.upTnlCfg[teIdx] && (duCb.upTnlCfg[teIdx]->tnlCfg1 != NULLP))
176       {
177          if(duCb.upTnlCfg[teIdx]->tnlCfg1->teId == teId)
178          {
179             dlDataMsgInfo->cellId = duCb.upTnlCfg[teIdx]->cellId;
180             dlDataMsgInfo->ueId = duCb.upTnlCfg[teIdx]->ueId;
181             dlDataMsgInfo->rbId = duCb.upTnlCfg[teIdx]->drbId;
182             return ROK;
183          }
184       }
185    }
186    return RFAILED;
187 }
188
189  /*******************************************************************
190  *
191  * @brief Build and Send DL Data Message transfer to RLC
192  *
193  * @details
194  *
195  *    Function : duBuildAndSendDlUserDataToRlc
196  *
197  *    Functionality:
198  *      Build and Send DL Data Message transfer to RLC
199  *
200  * @params[in] Cell ID
201  *             UE Index
202  *             Logical Channgel ID
203  *             RRC Message
204  *             RRC Message Length
205  * @return ROK     - success
206  *         RFAILED - failure
207  *
208  * ****************************************************************/
209
210 uint8_t duBuildAndSendDlUserDataToRlc(uint16_t msgLen, EgtpMsg *egtpMsg)
211 {
212    uint8_t ret = RFAILED;
213    Pst     pst;
214    RlcDlUserDataInfo *dlDataMsgInfo = NULLP;
215
216    DU_ALLOC_SHRABL_BUF(dlDataMsgInfo, sizeof(RlcDlUserDataInfo));
217    if(!dlDataMsgInfo)
218    {
219       DU_LOG("\nERROR  -->  DU_APP : Memory allocation failed for dlDataMsgInfo in duHdlEgtpDlData()");
220       return RFAILED;
221    }
222    memset(dlDataMsgInfo, 0, sizeof(RlcDlUserDataInfo));
223    dlDataMsgInfo->dlMsg = egtpMsg->msg;
224    dlDataMsgInfo->msgLen = msgLen;
225
226    /* Filling DL DATA Msg Info */
227    if(fillDlUserDataInfo(egtpMsg->msgHdr.teId, dlDataMsgInfo) == ROK)
228    {
229       /* Filling post structure and sending msg */ 
230       FILL_PST_DUAPP_TO_RLC(pst, RLC_DL_INST, EVENT_DL_USER_DATA_TRANS_TO_RLC);
231       DU_LOG("\nDEBUG  -->  DU_APP : Sending User Data Msg to RLC [TEID, nPDU]:[%d, %d]\n",\
232                        egtpMsg->msgHdr.teId, egtpMsg->msgHdr.nPdu.val);
233       ret = (*duSendRlcDlUserDataToRlcOpts[pst.selector])(&pst, dlDataMsgInfo);
234    }
235    if(ret != ROK)
236    {
237       DU_LOG("\nERROR  -->  DU_APP : Failed to send User Data to RLC in duHdlEgtpDlData()");
238       ODU_PUT_MSG_BUF(dlDataMsgInfo->dlMsg);
239       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, dlDataMsgInfo, sizeof(RlcDlUserDataInfo));
240    }
241    return ret;
242 }
243
244 /*******************************************************************
245  *
246  * @brief Handles EGTP data from CU 
247  *
248  * @details
249  *
250  *    Function : duHdlEgtpData
251  *
252  *    Functionality: 
253  *      Processes EGTP header and sends data to RLC
254  *
255  * @params[in]  Pointer to EGTP Message 
256  * @return ROK     - success
257  *         RFAILED - failure
258  *
259  * ****************************************************************/
260 uint8_t duHdlEgtpDlData(EgtpMsg  *egtpMsg)
261 {
262    uint16_t msgLen = 0;
263
264 #ifdef CALL_FLOW_DEBUG_LOG
265     DU_LOG("\nCall Flow: ENTEGTP -> ENTDUAPP : EVENT_HDL_RECV_DL_DATA\n");
266 #endif
267
268    DU_LOG("\nDEBUG  --> DU_APP : Processing DL data in duHdlEgtpDlData()");
269    
270    if(!egtpMsg->msg)
271    {
272       DU_LOG("\nERROR  -->  DU_APP : Recevied Dl Data is NULLP in duHdlEgtpDlData()");
273       return RFAILED;
274    }
275    ODU_GET_MSG_LEN(egtpMsg->msg, (MsgLen *)&msgLen);
276    if(duBuildAndSendDlUserDataToRlc(msgLen, egtpMsg) != ROK)
277    {
278       DU_LOG("\nERROR  -->  DU_APP : Failed to build DL USer Data in duHdlEgtpDlData()");
279       return RFAILED;
280    }
281    return ROK;
282 }
283
284 /******************************************************************
285  *
286  * @brief Builds and Sends DL CCCH Ind to MAC
287  *
288  * @details
289  *
290  *    Function : duBuildAndSendDlCcchInd 
291  *
292  *    Functionality: Builds and sends DL CCCH Ind Msg to MAC
293  *
294  * @params[in] dlCcchMsg - uint8_t*
295  * @return ROK     - success
296  *         RFAILED - failure
297  *
298  * ****************************************************************/
299 uint8_t duBuildAndSendDlCcchInd(uint16_t *cellId, uint16_t *crnti, \
300       DlCcchMsgType msgType, uint16_t dlCcchMsgSize, uint8_t *dlCcchMsg)
301 {
302    uint8_t  ret                  = ROK;
303    uint16_t idx2;
304    DlCcchIndInfo *dlCcchIndInfo = NULLP;
305    Pst pst;
306
307    DU_LOG("\nDEBUG   -->  DU APP : Building and Sending DL CCCH Ind to MAC");
308
309    DU_ALLOC_SHRABL_BUF(dlCcchIndInfo, sizeof(DlCcchIndInfo));
310
311    if(!dlCcchIndInfo)
312    {
313       DU_LOG("\nERROR  -->  DU APP : Memory alloc failed while building DL CCCH Ind");
314       return RFAILED;
315    }
316
317    dlCcchIndInfo->cellId = *cellId;
318    dlCcchIndInfo->crnti = *crnti;
319    dlCcchIndInfo->msgType = msgType;
320    dlCcchIndInfo->dlCcchMsgLen = dlCcchMsgSize;
321
322    DU_ALLOC_SHRABL_BUF(dlCcchIndInfo->dlCcchMsg, dlCcchIndInfo->dlCcchMsgLen);
323    if(!dlCcchIndInfo->dlCcchMsg)
324    {
325       DU_LOG("\nERROR  -->  DU APP : Memory alloc failed while building DL CCCH Ind");
326       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, dlCcchIndInfo, sizeof(DlCcchIndInfo));
327       return RFAILED;
328    }
329    for(idx2 = 0; idx2 < dlCcchIndInfo->dlCcchMsgLen; idx2++)
330    {
331       dlCcchIndInfo->dlCcchMsg[idx2] = dlCcchMsg[idx2];
332    }
333    DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, dlCcchMsg, dlCcchMsgSize);
334
335    /* Fill Pst */
336    FILL_PST_DUAPP_TO_MAC(pst, EVENT_MAC_DL_CCCH_IND);
337    ret = (*packMacDlCcchIndOpts[pst.selector])(&pst, dlCcchIndInfo);
338    if(ret != ROK)
339    {
340       DU_LOG("\nERROR  -->  DU_APP : Failure in sending DL CCCH to MAC");
341       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, dlCcchIndInfo->dlCcchMsg,\
342             dlCcchIndInfo->dlCcchMsgLen);
343       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, dlCcchIndInfo, \
344             sizeof(DlCcchIndInfo));
345       ret = RFAILED; 
346    }
347
348    return ret;
349
350 }
351
352 /*******************************************************************
353  *
354  * @brief Build and Send DL RRC Message transfer to RLC
355  *
356  * @details
357  *
358  *    Function : duBuildAndSendDlRrcMsgToRlc
359  *
360  *    Functionality:
361  *      Build and Send DL RRC Message transfer to RLC
362  *
363  * @params[in] Cell ID
364  *             UE Index
365  *             Logical Channgel ID
366  *             RRC Message
367  *             RRC Message Length
368  * @return ROK     - success
369  *         RFAILED - failure
370  *
371  * ****************************************************************/
372 uint8_t duBuildAndSendDlRrcMsgToRlc(uint16_t cellId, RlcUeCfg ueCfg, F1DlRrcMsg *f1DlRrcMsg)
373 {
374    Pst     pst;
375    uint8_t ret;
376    uint8_t lcIdx;
377    RlcDlRrcMsgInfo  *dlRrcMsgInfo = NULLP;
378
379    if(!f1DlRrcMsg)
380    {
381       DU_LOG("\nERROR  -->  DU APP : Received Dl RRC Msg is NULL at duBuildAndSendDlRrcMsgToRlc()");
382       return RFAILED;
383    }
384
385    DU_ALLOC_SHRABL_BUF(dlRrcMsgInfo, sizeof(RlcDlRrcMsgInfo));
386    if(!dlRrcMsgInfo)
387    {
388       DU_LOG("\nERROR  -->  DU APP : Memory allocation failed for dlRrcMsgInfo in \
389          duBuildAndSendDlRrcMsgToRlc");
390       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, f1DlRrcMsg->rrcMsgPdu, f1DlRrcMsg->rrcMsgSize);
391       return RFAILED;
392    }
393    
394    /* Filling up the RRC msg info */
395    dlRrcMsgInfo->cellId = cellId;
396    dlRrcMsgInfo->ueId = ueCfg.ueId;
397    for(lcIdx = 0; lcIdx <= MAX_NUM_LC; lcIdx++)
398    {
399       if(ueCfg.rlcLcCfg[lcIdx].lcId == f1DlRrcMsg->srbId)
400       {
401          dlRrcMsgInfo->rbType = ueCfg.rlcLcCfg[lcIdx].rbType;
402          dlRrcMsgInfo->rbId   = ueCfg.rlcLcCfg[lcIdx].rbId;
403          dlRrcMsgInfo->lcType = ueCfg.rlcLcCfg[lcIdx].lcType;
404          dlRrcMsgInfo->lcId   = ueCfg.rlcLcCfg[lcIdx].lcId;
405          break;
406       }
407    }
408    dlRrcMsgInfo->execDup = f1DlRrcMsg->execDup;
409    dlRrcMsgInfo->deliveryStaRpt = f1DlRrcMsg->deliveryStatRpt;
410    dlRrcMsgInfo->msgLen = f1DlRrcMsg->rrcMsgSize;
411    dlRrcMsgInfo->rrcMsg = f1DlRrcMsg->rrcMsgPdu;
412
413    /* Filling post structure and sending msg */ 
414    FILL_PST_DUAPP_TO_RLC(pst, RLC_DL_INST, EVENT_DL_RRC_MSG_TRANS_TO_RLC);
415    DU_LOG("\nDEBUG   -->  DU_APP: Sending Dl RRC Msg to RLC \n");
416    ret = (*duSendDlRrcMsgToRlcOpts[pst.selector])(&pst, dlRrcMsgInfo);
417    if(ret != ROK)
418    {
419       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, f1DlRrcMsg->rrcMsgPdu, f1DlRrcMsg->rrcMsgSize);
420       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, dlRrcMsgInfo, sizeof(RlcDlRrcMsgInfo));
421       return RFAILED;
422    }
423
424    return ROK;
425
426
427
428
429 /******************************************************************
430  *
431  * @brief Process DL RRC Msg recevied from F1AP
432  *
433  * @details
434  *
435  *    Function : duProcDlRrcMsg
436  *
437  *    Functionality: Process DL RRC Msg recevied from F1AP
438  *
439  * @params[in] dlCcchMsg - uint8_t*
440  * @return ROK     - success
441  *         RFAILED - failure
442  *
443  * ****************************************************************/
444
445 uint8_t duProcDlRrcMsg(F1DlRrcMsg *dlRrcMsg)
446 {
447    uint8_t ueIdx, ret;
448    uint16_t crnti, cellId, cellIdx;
449    bool ueCcchCtxtFound = false;
450    bool ueFound = false;
451
452    ret = ROK;
453
454    if(dlRrcMsg->srbId == SRB0_LCID) //RRC connection setup
455    {
456       for(ueIdx=0; ueIdx<duCb.numUe; ueIdx++)
457       {
458          if(dlRrcMsg->gnbDuUeF1apId == duCb.ueCcchCtxt[ueIdx].gnbDuUeF1apId)
459          {
460             ueCcchCtxtFound = true;
461             crnti  = duCb.ueCcchCtxt[ueIdx].crnti;
462             cellId = duCb.ueCcchCtxt[ueIdx].cellId;
463             break;
464          }
465       }
466    }
467    if(ueCcchCtxtFound)
468    {
469       ret = duBuildAndSendDlCcchInd(&cellId, &crnti, RRC_SETUP, dlRrcMsg->rrcMsgSize, dlRrcMsg->rrcMsgPdu);
470       if(ret == RFAILED)
471       {
472          DU_LOG("\nERROR  -->  DU APP : Failed to build DlCcch Ind at procDlRrcMsgTrans()");
473       }
474       else
475       {
476          if(duCb.actvCellLst[cellId-1] == NULLP)
477          {
478             DU_LOG("\nERROR  -->  DU APP : cellId [%d] does not exist", cellId);
479             ret = RFAILED;
480          }
481          if(duCb.actvCellLst[cellId-1]->numActvUes < MAX_NUM_UE)
482          {
483             ret = duCreateUeCb(&duCb.ueCcchCtxt[ueIdx], dlRrcMsg->gnbCuUeF1apId);
484             if(ret == RFAILED)
485             {
486                DU_LOG("\nERROR  -->  DU APP : Failed to createUeCb for cellId [%d] at procDlRrcMsgTrans()", \
487                      duCb.ueCcchCtxt[ueIdx].cellId);
488             }
489          }
490          else
491          {
492             DU_LOG("\nERROR   -->  DU_APP: Max Active UEs has reached at procDlRrcMsgTrans()");
493             ret = RFAILED;
494          }
495       }
496    }
497    else
498    {
499       for(cellIdx = 0; cellIdx < MAX_NUM_CELL; cellIdx++)
500       {
501          for(ueIdx = 0 ; ueIdx < MAX_NUM_UE; ueIdx++)
502          {
503             if(duCb.actvCellLst[cellIdx] && (dlRrcMsg->gnbCuUeF1apId == duCb.actvCellLst[cellIdx]->ueCb[ueIdx].gnbCuUeF1apId)
504                   && (dlRrcMsg->gnbDuUeF1apId == duCb.actvCellLst[cellIdx]->ueCb[ueIdx].gnbDuUeF1apId))
505             {
506                ueFound = true;
507                ret = duBuildAndSendDlRrcMsgToRlc(duCb.actvCellLst[cellIdx]->cellId, \
508                      duCb.actvCellLst[cellIdx]->ueCb[ueIdx].rlcUeCfg, dlRrcMsg);
509                break; 
510             }
511          }
512          if(ueFound)
513             break;
514       }
515       if(!ueFound)
516       {
517          DU_LOG("\nERROR   -->  DU_APP: UE ID [%d] not found", dlRrcMsg->gnbDuUeF1apId);
518          ret = RFAILED;
519       }
520    }
521    return ret;
522 }
523
524 /******************************************************************
525  *
526  * @brief Processes UL CCCH Ind recvd from MAC
527  *
528  * @details
529  *
530  *    Function : duProcUlCcchInd
531  *
532  *    Functionality: Processes UL CCCH Ind recvd from MAC
533  *
534  * @params[in] UlCcchIndInfo *ulCcchIndInfo
535  * @return ROK     - success
536  *         RFAILED - failure
537  *
538  * ****************************************************************/
539 uint8_t duProcUlCcchInd(UlCcchIndInfo *ulCcchIndInfo)
540 {
541
542    uint8_t ret = ROK;
543    int32_t gnbDuUeF1apId = 0;
544
545    if(ulCcchIndInfo->crnti)
546    {
547       GET_UE_ID(ulCcchIndInfo->crnti, gnbDuUeF1apId);
548    }
549    else
550    {
551       DU_LOG("\nERROR  -->  DU_APP : Received invalid CRNTI [%d] ", ulCcchIndInfo->crnti);
552       return RFAILED;
553    }
554    
555    /* Store Ue mapping */
556    duCb.ueCcchCtxt[duCb.numUe].gnbDuUeF1apId = (uint32_t)gnbDuUeF1apId;
557    duCb.ueCcchCtxt[duCb.numUe].crnti         = ulCcchIndInfo->crnti;
558    duCb.ueCcchCtxt[duCb.numUe].cellId        = ulCcchIndInfo->cellId;
559
560    duCb.numUe++;
561
562    ret = (BuildAndSendInitialRrcMsgTransfer(gnbDuUeF1apId, ulCcchIndInfo->crnti, ulCcchIndInfo->ulCcchMsgLen,
563             ulCcchIndInfo->ulCcchMsg));
564    if(ret != ROK)
565    {
566       DU_LOG("\nERROR  -->  DU_APP : BuildAndSendInitialRrcMsgTransfer failed");
567    }
568
569    DU_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, ulCcchIndInfo->ulCcchMsg, ulCcchIndInfo->ulCcchMsgLen);
570    DU_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, ulCcchIndInfo, sizeof(UlCcchIndInfo));
571
572    return ret;
573
574 }
575
576 /******************************************************************
577  *
578  * @brief Fills Default UL LC Cfg
579  *
580  * @details
581  *
582  *    Function : fillDefaultUlLcCfg
583  *
584  *    Functionality: Fills Default UL LC Cfg
585  *
586  * @params[in]  UlLcCfg *ulLcCfg 
587  * @return void
588  *****************************************************************/
589 void fillDefaultUlLcCfg(UlLcCfg *ulLcCfg)
590 {
591    ulLcCfg->priority = LC_PRIORITY_1;
592    ulLcCfg->lcGroup =  0;
593    ulLcCfg->schReqId = 0;
594    ulLcCfg->pbr = PBR_KBPS_INFINITY;
595    ulLcCfg->bsd = BSD_MS_1000;
596 }
597
598 /******************************************************************
599  *
600  * @brief Fills Initial DL Bandwidth Part
601  *
602  * @details
603  *
604  *    Function : fillDefaultInitDlBwp
605  *
606  *    Functionality: Fills Initial DL Bandwidth Part
607  *
608  * @params[in]  InitialDlBwp *initDlBwp
609  * @return ROK - success
610  *         RFAILED - failure
611  *
612  *****************************************************************/
613 uint8_t fillDefaultInitDlBwp(InitialDlBwp *initDlBwp)
614 {
615    uint8_t idx = 0;
616    uint8_t freqDomainResource[FREQ_DOM_RSRC_SIZE] = {0};
617    uint8_t coreset0EndPrb, coreset1StartPrb, coreset1NumPrb;
618
619    if(initDlBwp)
620    {
621       /* Filling PDCCH Config */
622       initDlBwp->pdcchPresent = TRUE;
623       if(initDlBwp->pdcchPresent)
624       {
625          initDlBwp->pdcchCfg.numCRsetToAddMod = PDCCH_CTRL_RSRC_SET_ONE_ID;
626          memset(initDlBwp->pdcchCfg.cRSetToAddModList, 0, MAX_NUM_CRSET);
627          if(initDlBwp->pdcchCfg.numCRsetToAddMod <= MAX_NUM_CRSET)
628          {
629             initDlBwp->pdcchCfg.cRSetToAddModList[idx].cRSetId = PDCCH_CTRL_RSRC_SET_ONE_ID;
630             memset(initDlBwp->pdcchCfg.cRSetToAddModList[idx].freqDomainRsrc, 0, FREQ_DOM_RSRC_SIZE); 
631             coreset0EndPrb = CORESET0_END_PRB;
632             coreset1StartPrb = coreset0EndPrb +6;
633             coreset1NumPrb = CORESET1_NUM_PRB;
634             /* calculate the PRBs */
635             fillCoresetFeqDomAllocMap(((coreset1StartPrb)/6), (coreset1NumPrb/6), freqDomainResource);
636             memcpy(initDlBwp->pdcchCfg.cRSetToAddModList[idx].freqDomainRsrc, freqDomainResource, FREQ_DOM_RSRC_SIZE);
637
638             initDlBwp->pdcchCfg.cRSetToAddModList[idx].duration = PDCCH_CTRL_RSRC_SET_ONE_DURATION;
639             initDlBwp->pdcchCfg.cRSetToAddModList[idx].cceRegMappingType = CCE_REG_MAPPINGTYPE_PR_NONINTERLEAVED;
640             initDlBwp->pdcchCfg.cRSetToAddModList[idx].precoderGranularity = ALL_CONTIGUOUS_RBS;
641             initDlBwp->pdcchCfg.cRSetToAddModList[idx].dmrsScramblingId = SCRAMBLING_ID;
642          }
643          initDlBwp->pdcchCfg.numCRsetToRel = 0;
644
645          /* Filling Serach Space */
646          initDlBwp->pdcchCfg.numSearchSpcToAddMod = PDCCH_CTRL_RSRC_SET_ONE_ID;
647          memset(initDlBwp->pdcchCfg.searchSpcToAddModList, 0, MAX_NUM_CRSET);
648          if(initDlBwp->pdcchCfg.numSearchSpcToAddMod <= MAX_NUM_CRSET)
649          {
650             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].searchSpaceId = PDCCH_SRCH_SPC_TWO_ID;
651             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].cRSetId = PDCCH_CTRL_RSRC_SET_ONE_ID;
652             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].mSlotPeriodicityAndOffset = SLOTPERIODICITYANDOFFSET_PR_SL1;
653             memset(initDlBwp->pdcchCfg.searchSpcToAddModList[idx].mSymbolsWithinSlot, 0, MONITORING_SYMB_WITHIN_SLOT_SIZE);
654             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].mSymbolsWithinSlot[idx] = PDCCH_SYMBOL_WITHIN_SLOT;
655             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].numCandidatesAggLevel1 = AGGREGATIONLEVEL_N8; 
656             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].numCandidatesAggLevel2 = AGGREGATIONLEVEL_N8; 
657             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].numCandidatesAggLevel4 = AGGREGATIONLEVEL_N4; 
658             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].numCandidatesAggLevel8 = AGGREGATIONLEVEL_N2; 
659             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].numCandidatesAggLevel16 = AGGREGATIONLEVEL_N1;
660             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].searchSpaceType = SEARCHSPACETYPE_PR_UE_SPECIFIC;
661             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].ueSpecificDciFormat = PDCCH_SRCH_SPC_TWO_UE_SPEC_DCI_FORMAT;
662          }
663          initDlBwp->pdcchCfg.numSearchSpcToRel = 0;
664       }
665
666       /* Filling PDSCH Config */
667       initDlBwp->pdschPresent = TRUE;
668       if(initDlBwp->pdschPresent)
669       {
670          initDlBwp->pdschCfg.dmrsDlCfgForPdschMapTypeA.addPos = ADDITIONALPOSITION_POS0;
671          initDlBwp->pdschCfg.resourceAllocType = RESOURCEALLOCATION_TYPE1;
672
673          initDlBwp->pdschCfg.numTimeDomRsrcAlloc = NUM_TIME_DOM_RSRC_ALLOC;
674
675          idx = 0; 
676          initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].mappingType = MAPPING_TYPEA;
677          initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].startSymbol = PDSCH_START_SYMBOL; 
678          initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].symbolLength = PDSCH_LENGTH_SYMBOL;
679          initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].startSymbolAndLength = calcSliv(PDSCH_START_SYMBOL, PDSCH_LENGTH_SYMBOL);
680
681          idx++;
682          DU_ALLOC_SHRABL_BUF(initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].k0, sizeof(uint8_t));
683          if(initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].k0 == NULLP)
684          {
685             DU_LOG("\nERROR  -->  DUAPP : Failed to allocate memory to K0 in fillDefaultInitDlBwp");
686             return RFAILED;
687          }
688          if(initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].k0)
689             *(initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].k0) = 1;
690          initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].mappingType = MAPPING_TYPEA;
691          initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].startSymbol = PDSCH_START_SYMBOL; 
692          initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].symbolLength = PDSCH_LENGTH_SYMBOL;
693          initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].startSymbolAndLength = calcSliv(PDSCH_START_SYMBOL, PDSCH_LENGTH_SYMBOL);
694
695          initDlBwp->pdschCfg.rbgSize = RBG_SIZE_CONFIG1;
696          initDlBwp->pdschCfg.numCodeWordsSchByDci = CODEWORDS_SCHED_BY_DCI_N1;
697          initDlBwp->pdschCfg.bundlingType = TYPE_STATIC_BUNDLING;
698          initDlBwp->pdschCfg.bundlingInfo.StaticBundling.size = 0;
699       }
700    }
701
702    return ROK;
703 }
704
705 /******************************************************************
706  *
707  * @brief Fills Initial UL Bandwidth Part
708  *
709  * @details
710  *
711  *    Function : fillDefaultInitUlBwp
712  *
713  *    Functionality: Fills Initial UL Bandwidth Part
714  *
715  * @params[in]  InitialUlBwp *initUlBwp
716  * @return void
717  *
718  *****************************************************************/
719 void fillDefaultInitUlBwp(InitialUlBwp *initUlBwp)
720 {
721    uint8_t idx;
722    if(initUlBwp)
723    {
724       initUlBwp->pucchPresent = FALSE;
725
726       /*Filling PUSCH Config */
727       initUlBwp->puschPresent = TRUE;
728       if(initUlBwp->puschPresent)
729       {
730          initUlBwp->puschCfg.dataScramblingId = SCRAMBLING_ID;
731          initUlBwp->puschCfg.dmrsUlCfgForPuschMapTypeA.addPos = ADDITIONALPOSITION_POS0; 
732          initUlBwp->puschCfg.dmrsUlCfgForPuschMapTypeA.transPrecodDisabled. \
733             scramblingId0 = SCRAMBLING_ID; 
734          initUlBwp->puschCfg.resourceAllocType = RESOURCEALLOCATION_TYPE1;
735          initUlBwp->puschCfg.numTimeDomRsrcAlloc = 2;
736          idx = 0;
737          if(initUlBwp->puschCfg.numTimeDomRsrcAlloc <= MAX_NUM_UL_ALLOC)
738          {
739             initUlBwp->puschCfg.timeDomRsrcAllocList[idx].k2 = PUSCH_K2_CFG1;
740             initUlBwp->puschCfg.timeDomRsrcAllocList[idx].mappingType =\
741                MAPPING_TYPEA;
742             initUlBwp->puschCfg.timeDomRsrcAllocList[idx].startSymbol = PUSCH_START_SYMBOL;
743             initUlBwp->puschCfg.timeDomRsrcAllocList[idx].symbolLength = PUSCH_LENGTH_SYMBOL;
744             initUlBwp->puschCfg.timeDomRsrcAllocList[idx].startSymbolAndLength =\
745                calcSliv(PUSCH_START_SYMBOL, PUSCH_LENGTH_SYMBOL);
746
747        idx++;
748        initUlBwp->puschCfg.timeDomRsrcAllocList[idx].k2 = PUSCH_K2_CFG2;
749        initUlBwp->puschCfg.timeDomRsrcAllocList[idx].mappingType = MAPPING_TYPEA;
750        initUlBwp->puschCfg.timeDomRsrcAllocList[idx].startSymbol = PUSCH_START_SYMBOL;
751        initUlBwp->puschCfg.timeDomRsrcAllocList[idx].symbolLength = PUSCH_LENGTH_SYMBOL;
752        initUlBwp->puschCfg.timeDomRsrcAllocList[idx].startSymbolAndLength =\
753           calcSliv(PUSCH_START_SYMBOL, PUSCH_LENGTH_SYMBOL);
754          }
755          initUlBwp->puschCfg.transformPrecoder = TRANSFORM_PRECODER_DISABLED;
756       }
757    }
758    else
759    {
760       DU_LOG("\nERROR  -->  DU APP : Memory is NULL of InitalUlBwp");
761    }
762
763 }
764 /******************************************************************
765  *
766  * @brief Fills SpCell Group Info
767  *
768  * @details
769  *
770  *    Function : fillDefaultSpCellGrpInfo
771  *
772  *    Functionality: Fills Sp Cell Group Info
773  *
774  * @params[in]  MacUeCfg *macUeCfg
775  * @return ROK - Success
776  *         RFAILED - Failure
777  *
778  *****************************************************************/
779 uint8_t fillDefaultSpCellGrpInfo(MacUeCfg *macUeCfg)
780 {
781    SpCellCfg *spCell = NULL;
782
783    if(macUeCfg)
784       spCell = &macUeCfg->spCellCfg;
785
786    if(spCell)
787    {
788       macUeCfg->spCellCfgPres = true;
789
790       spCell->servCellIdx = SERV_CELL_IDX;
791       /* Filling Initial Dl Bwp */
792       if((fillDefaultInitDlBwp(&spCell->servCellCfg.initDlBwp)) != ROK)
793       {
794          DU_LOG("\nERROR  -->  DUAPP : Failed in fillDefaultInitDlBwp");
795          return RFAILED;
796       }
797
798       spCell->servCellCfg.numDlBwpToAdd    = 0; 
799       spCell->servCellCfg.firstActvDlBwpId = ACTIVE_DL_BWP_ID;
800       spCell->servCellCfg.defaultDlBwpId   = ACTIVE_DL_BWP_ID;
801       spCell->servCellCfg.bwpInactivityTmr = NULLP;
802       spCell->servCellCfg.pdschServCellCfg.maxMimoLayers = NULLP;
803       spCell->servCellCfg.pdschServCellCfg.maxCodeBlkGrpPerTb = NULLP;
804       spCell->servCellCfg.pdschServCellCfg.codeBlkGrpFlushInd = NULLP;
805       spCell->servCellCfg.pdschServCellCfg.xOverhead = NULLP;
806       spCell->servCellCfg.pdschServCellCfg.numHarqProcForPdsch = NUM_HARQ_PROC_FOR_PDSCH_N_16;
807
808       /* Filling Initial UL Bwp*/
809       fillDefaultInitUlBwp(&spCell->servCellCfg.initUlBwp);
810       spCell->servCellCfg.numUlBwpToAdd     = 0; 
811       spCell->servCellCfg.firstActvUlBwpId  = ACTIVE_DL_BWP_ID; 
812    }
813    else
814    {
815       DU_LOG("\nERROR  -->  DU APP : Memory is NULL for SpCellGrp");
816       return RFAILED;
817    }
818    return ROK;
819 }
820
821 /******************************************************************
822  *
823  * @brief Fills Physical Cell Group Info
824  *
825  * @details
826  *
827  *    Function : fillDefaultPhyCellGrpInfo
828  *
829  *    Functionality: Fills Physical Cell Group Info
830  *
831  * @params[in]  MacUeCfg *macUeCfg
832  * @return void
833  *
834  *****************************************************************/
835 void fillDefaultPhyCellGrpInfo(MacUeCfg *macUeCfg)
836 {
837    PhyCellGrpCfg *cellGrp = NULL;
838
839    if(macUeCfg)
840       cellGrp = &macUeCfg->phyCellGrpCfg;
841
842    if(cellGrp)
843    {
844       macUeCfg->phyCellGrpCfgPres = true;
845       cellGrp->pdschHarqAckCodebook = PDSCH_HARQ_ACK_CODEBOOK_DYNAMIC;
846       cellGrp->pNrFr1 = P_NR_FR1;
847    }
848    else
849    {
850       DU_LOG("\nERROR  -->  DU APP : Memory is NULL for Physical Cell Group");
851    }
852 }
853
854 /******************************************************************
855  *
856  * @brief Fills Mac Cell Group Info
857  *
858  * @details
859  *
860  *    Function : fillDefaultMacCellGrpInfo
861  *
862  *    Functionality: Fills Mac Cell Group Info
863  *
864  * @params[in]  MacUeCfg *macUeCfg
865  * @return void
866  *
867  *****************************************************************/
868 void fillDefaultMacCellGrpInfo(MacUeCfg *macUeCfg)
869 {
870    uint8_t idx;
871    MacCellGrpCfg *cellGrp = NULL;
872
873    if(macUeCfg)
874       cellGrp = &macUeCfg->macCellGrpCfg;
875
876    if(cellGrp)
877    {
878       macUeCfg->macCellGrpCfgPres = true;
879
880       /* Filling Scheduling Request Config */
881       cellGrp->schReqCfg.addModListCount = 1;
882       if(cellGrp->schReqCfg.addModListCount <= MAX_NUM_SR_CFG_PER_CELL_GRP)
883       {
884          for(idx = 0; idx < cellGrp->schReqCfg.addModListCount; idx++)
885          {
886             cellGrp->schReqCfg.addModList[idx].schedReqId    = SCH_REQ_ID;
887             cellGrp->schReqCfg.addModList[idx].srProhibitTmr = SR_PROHIBIT_MS_32;
888             cellGrp->schReqCfg.addModList[idx].srTransMax    = SR_TRANS_MAX_N_16;
889          }
890       }
891       cellGrp->schReqCfg.relListCount = 0;
892
893       /* Filling Tag config */
894       cellGrp->tagCfg.addModListCount = 1;
895       if(cellGrp->tagCfg.addModListCount <= MAC_NUM_TAGS)
896       {
897          for(idx = 0; idx < cellGrp->tagCfg.addModListCount; idx++)
898          {
899             cellGrp->tagCfg.addModList[idx].tagId = TAG_ID;
900             cellGrp->tagCfg.addModList[idx].timeAlignTimer = TIME_ALIGNMENT_TIMER_INFINITY;
901          }
902       }
903       cellGrp->tagCfg.relListCount = 0;
904
905       /* Filling BSR config */
906       cellGrp->bsrTmrCfg.periodicTimer = BSR_PERIODIC_TIMER_SF_10;
907       cellGrp->bsrTmrCfg.retxTimer = BSR_RETX_TIMER_SF_320;
908       cellGrp->bsrTmrCfg.srDelayTimer = BSR_SR_DELAY_TMR_2560;
909
910       /* Filling PHR config */
911       cellGrp->phrCfgSetupPres = true;
912       cellGrp->phrCfg.periodicTimer = PHR_PERIODIC_TIMER_INFINITY;
913       cellGrp->phrCfg.prohibitTimer = PHR_PROHIBIT_TIMER_SF_0;
914       cellGrp->phrCfg.txPowerFactor = PHR_TX_PWR_FACTOR_CHANGE_INFINITY;
915       cellGrp->phrCfg.multiplePHR   = false;
916       cellGrp->phrCfg.dummy         = false;
917       cellGrp->phrCfg.phrType2OtherCell = false;
918       cellGrp->phrCfg.phrOtherCG = PHR_MODE_OTHER_CG_REAL;  
919
920    }
921    else
922    {
923       DU_LOG("\nERROR  -->  DU APP : Memory is NULL for Master Cell Group");
924    }
925 }
926
927 /*******************************************************************
928  *
929  * @brief Fills default modulation info for a UE
930  *
931  * @details
932  *
933  *    Function : fillDefaultModulation
934  *
935  *    Functionality: Fills default modulation info for a UE
936  *
937  * @params[in] Pointer to MAC UE configuration
938  * @return ROK     - success
939  *         RFAILED - failure
940  *
941  * ****************************************************************/
942 void fillDefaultModulation(MacUeCfg *ueCfg)
943 {
944    ueCfg->dlModInfo.modOrder = MOD_ORDER_QPSK;
945    ueCfg->dlModInfo.mcsIndex = DEFAULT_MCS;
946    ueCfg->dlModInfo.mcsTable = MCS_TABLE_QAM64; /* Spec 38.214-Table 5.1.3.1-1 */
947
948    ueCfg->ulModInfo.modOrder = MOD_ORDER_QPSK;
949    ueCfg->ulModInfo.mcsIndex = DEFAULT_MCS;
950    ueCfg->ulModInfo.mcsTable = MCS_TABLE_QAM64; /* Spec 38.214-Table 5.1.3.1-1 */
951 }
952
953 /******************************************************************
954  *
955  * @brief Function to fill Mac Lc Cfg for SRB1
956  *
957  * @details
958  *
959  *    Function : fillMacSrb1LcCfg
960  *
961  *    Functionality: Function to fill Mac Lc cfg for SRB1
962  *
963  * @params[in]  LcCfg *lcCfg, LcCfg *ueSetReqDb
964  * @return void
965  *****************************************************************/
966
967 void fillMacSrb1LcCfg(LcCfg *macLcCfg)
968 {
969    macLcCfg->lcId   = SRB1_LCID;
970    macLcCfg->configType = CONFIG_ADD;
971    macLcCfg->drbQos = NULLP;
972    macLcCfg->snssai = NULLP;
973    macLcCfg->ulLcCfgPres = true;
974    fillDefaultUlLcCfg(&macLcCfg->ulLcCfg);
975 }
976
977 /******************************************************************
978  *
979  * @brief Function to fill the Lc cfg from ueSetupReqDb
980  *
981  * @details
982  *
983  *    Function : fillMacLcCfgToAddMod
984  *
985  *    Functionality: Function to fill the Lc cfg from ueSetupReqDb
986  *
987  * @params[in]  LcCfg *lcCfg, LcCfg *ueSetReqDb
988  * @return ROK/RFAILED
989  *
990  *****************************************************************/
991
992 uint8_t fillMacLcCfgToAddMod(LcCfg *macLcCfgToSend, LcCfg *ueLcCfgDb, LcCfg *oldLcCfg, Bool toUpdate)
993 {
994    if(!toUpdate)
995    {
996       if(macLcCfgToSend)
997       {
998          macLcCfgToSend->lcId = ueLcCfgDb->lcId;
999          macLcCfgToSend->configType = ueLcCfgDb->configType;
1000
1001          if(ueLcCfgDb->drbQos)
1002             macLcCfgToSend->drbQos = ueLcCfgDb->drbQos;
1003          else if(oldLcCfg)
1004             macLcCfgToSend->drbQos = oldLcCfg->drbQos;
1005          else
1006             macLcCfgToSend->drbQos = NULL;
1007
1008          if(ueLcCfgDb->snssai)
1009             macLcCfgToSend->snssai = ueLcCfgDb->snssai;
1010          else if(oldLcCfg)
1011             macLcCfgToSend->snssai = oldLcCfg->snssai;
1012          else
1013             macLcCfgToSend->snssai = NULL;
1014
1015          macLcCfgToSend->ulLcCfgPres = ueLcCfgDb->ulLcCfgPres;
1016          memcpy(&macLcCfgToSend->ulLcCfg, &ueLcCfgDb->ulLcCfg, sizeof(UlLcCfg));
1017          memcpy(&macLcCfgToSend->dlLcCfg, &ueLcCfgDb->dlLcCfg, sizeof(DlLcCfg));
1018       }
1019    }
1020    else
1021    {
1022       oldLcCfg->lcId = ueLcCfgDb->lcId;
1023       oldLcCfg->configType = ueLcCfgDb->configType;
1024
1025       if(ueLcCfgDb->drbQos)
1026       {
1027          if(oldLcCfg->drbQos)
1028             DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, oldLcCfg->drbQos, sizeof(DrbQosInfo));
1029
1030          DU_ALLOC_SHRABL_BUF(oldLcCfg->drbQos, sizeof(DrbQosInfo));
1031          if(oldLcCfg->drbQos == NULL)
1032          {
1033             DU_LOG("\nERROR  -->  DU APP : Memory Alloc Failed at fillMacLcCfgToAddMod()");
1034             return RFAILED;
1035          }
1036          memcpy(oldLcCfg->drbQos, ueLcCfgDb->drbQos, sizeof(DrbQosInfo));
1037       }
1038
1039       if(ueLcCfgDb->snssai)
1040       {
1041          if(oldLcCfg->snssai)
1042             DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, oldLcCfg->snssai, sizeof(Snssai));
1043
1044          DU_ALLOC_SHRABL_BUF(oldLcCfg->snssai, sizeof(Snssai));
1045          if(oldLcCfg->snssai == NULL)
1046          {
1047             DU_LOG("\nERROR  -->  DU APP : Memory Alloc Failed at fillMacLcCfgToAddMod()");
1048             DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, oldLcCfg->drbQos, sizeof(DrbQosInfo));
1049             return RFAILED;
1050          }
1051          memcpy(oldLcCfg->snssai, ueLcCfgDb->snssai, sizeof(Snssai));
1052       }
1053
1054       oldLcCfg->ulLcCfgPres = ueLcCfgDb->ulLcCfgPres;
1055       memcpy(&oldLcCfg->ulLcCfg, &ueLcCfgDb->ulLcCfg, sizeof(UlLcCfg));
1056       memcpy(&oldLcCfg->dlLcCfg, &ueLcCfgDb->dlLcCfg, sizeof(DlLcCfg));
1057    }
1058    return ROK;
1059 }
1060
1061 /******************************************************************
1062  *
1063  * @brief Function to copy the Bit rate from ueSetupReqDb
1064  *
1065  * @details
1066  *
1067  *    Function : fillAmbr
1068  *
1069  *    Functionality: Function to copy bit Rate from ueSetupReqDb
1070  *
1071  * @params[in]  AmbrCfg **macAmbr, AmbrCfg  *ueDbAmbr
1072  * @return ROK/RFAILED
1073  *
1074  *****************************************************************/
1075
1076 uint8_t fillAmbr(AmbrCfg **macAmbrCfgToSend, AmbrCfg *ueDbAmbr, AmbrCfg **oldMacAmbrCfg, Bool toUpdate)
1077 {
1078    if(!toUpdate)
1079    {
1080       if(ueDbAmbr)
1081       {
1082          *macAmbrCfgToSend = ueDbAmbr;
1083       }
1084       else if(oldMacAmbrCfg)
1085          *macAmbrCfgToSend = *oldMacAmbrCfg;       
1086    }
1087    else
1088    {
1089       if(ueDbAmbr)
1090       {
1091          if(oldMacAmbrCfg)
1092          {
1093             if(*oldMacAmbrCfg)
1094             {
1095                DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, *oldMacAmbrCfg, sizeof(AmbrCfg));
1096             }
1097             DU_ALLOC_SHRABL_BUF(*oldMacAmbrCfg, sizeof(AmbrCfg));
1098             if(*oldMacAmbrCfg == NULLP)
1099             {
1100                DU_LOG("\nERROR  -->  DU APP : Memory Alloc Failed at fillAmbr()");
1101                return RFAILED;
1102             }
1103             memset(*oldMacAmbrCfg, 0, sizeof(AmbrCfg));
1104             (*oldMacAmbrCfg)->ulBr = ueDbAmbr->ulBr;
1105          }
1106       }
1107    }
1108
1109    return ROK;
1110 }
1111
1112 /******************************************************************
1113  *
1114  * @brief Builds and Send UE ReConfig Request to MAC
1115  *
1116  * @details
1117  *
1118  *    Function : sendUeReCfgReqToMac
1119  *
1120  *    Functionality: Builds and Send UE ReConfig Request to MAC
1121  *
1122  * @Params[in]  MacUeCfg pointer
1123  * @return ROK     - success
1124  *         RFAILED - failure
1125  *
1126  * ****************************************************************/
1127
1128 uint8_t sendUeReCfgReqToMac(MacUeCfg *macUeCfg)
1129 {
1130    uint8_t ret = ROK;
1131    Pst pst;
1132    
1133    /* Fill Pst */
1134    FILL_PST_DUAPP_TO_MAC(pst, EVENT_MAC_UE_RECONFIG_REQ);
1135    
1136    if(macUeCfg)
1137    {
1138       /* Processing one Ue at a time to MAC */
1139       DU_LOG("\nDEBUG   -->  DU_APP: Sending Ue Reconfig Request to MAC");
1140       ret = (*packMacUeReconfigReqOpts[pst.selector])(&pst, macUeCfg);
1141       if(ret == RFAILED)
1142       {
1143          DU_LOG("\nERROR  -->  DU APP : Failed to send Reconfig Request to MAC at sendUeReCfgReqToMac()");
1144          DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, macUeCfg, sizeof(MacUeCfg));
1145       }
1146    }
1147    else
1148    {
1149       DU_LOG("\nERROR  -->  DU_APP: Received macUeCfg is NULLP at sendUeReCfgReqToMac()");
1150       ret = RFAILED;
1151    }
1152    return ret;
1153 }
1154
1155
1156
1157 /******************************************************************
1158  *
1159  * @brief Fills MacUeCfg structure
1160  *
1161  * @details
1162  *
1163  *    Function : fillMacUeCfg
1164  *
1165  *    Functionality: Fills MacUeCfg
1166  *
1167  * @params[in]  cellId, ueId, crnti, 
1168  *              DuUeCfg  pointer,
1169  *              MacUeCfg pointer
1170  * @return ROK/RFAILED
1171  *
1172  *****************************************************************/
1173 uint8_t fillMacUeCfg(uint16_t cellId, uint8_t gnbDuUef1apId, uint16_t crnti, DuUeCfg *ueCfgDb, MacUeCfg *macUeCfg)
1174 {
1175    uint8_t ret = ROK, dbIdx = 0, lcIdx = 0, cellIdx = 0;
1176    bool lcIdFound = false;
1177    MacUeCfg *duMacDb = NULLP;
1178
1179    macUeCfg->cellId = cellId;
1180    macUeCfg->ueId = gnbDuUef1apId;
1181    macUeCfg->crnti = crnti;
1182
1183    if(!ueCfgDb)
1184    {
1185       fillDefaultMacCellGrpInfo(macUeCfg);
1186       fillDefaultPhyCellGrpInfo(macUeCfg);
1187
1188       if((fillDefaultSpCellGrpInfo(macUeCfg)) != ROK)
1189       {
1190          DU_LOG("\nERROR  --> DUAPP : Failed in fillDefaultSpCellGrpInfo");
1191          return RFAILED;
1192       }
1193
1194       macUeCfg->ambrCfg = NULLP;
1195       fillDefaultModulation(macUeCfg);
1196       fillMacSrb1LcCfg(&macUeCfg->lcCfgList[0]);
1197       macUeCfg->numLcs++;
1198    }
1199    else
1200    {
1201       if(ueCfgDb->dataTransmissionAction == STOP_TRANSMISSION)
1202       {
1203          macUeCfg->transmissionAction = ueCfgDb->dataTransmissionAction; 
1204          return ROK;
1205       }
1206
1207       GET_CELL_IDX(cellId, cellIdx);
1208       if(duCb.actvCellLst[cellIdx])
1209          duMacDb = &duCb.actvCellLst[cellIdx]->ueCb[macUeCfg->ueId-1].macUeCfg;
1210       else
1211       {
1212          DU_LOG("\nERROR  -->  DU APP : Cell Id [%d] does not exist", cellId);
1213          return RFAILED;
1214       }
1215       duMacDb->macUeCfgState = UE_CFG_INPROGRESS;
1216       
1217       if(ueCfgDb->cellGrpCfg)
1218       {
1219          ret = procUeReCfgCellInfo(macUeCfg, duMacDb, ueCfgDb->cellGrpCfg);
1220          if(ret == ROK)
1221          {
1222             if(macUeCfg->spCellCfgPres == true)
1223             {
1224                if(macUeCfg->spCellCfg.servCellCfg.initDlBwp.pdschPresent)
1225                {
1226                   fillStartSymbolAndLen(macUeCfg->spCellCfg.servCellCfg.initDlBwp.pdschCfg.numTimeDomRsrcAlloc,\
1227                         &macUeCfg->spCellCfg.servCellCfg.initDlBwp.pdschCfg, NULL);
1228                }
1229                if(macUeCfg->spCellCfg.servCellCfg.initUlBwp.puschPresent)
1230                {
1231                   fillStartSymbolAndLen(macUeCfg->spCellCfg.servCellCfg.initUlBwp.puschCfg.numTimeDomRsrcAlloc,\
1232                         NULL, &macUeCfg->spCellCfg.servCellCfg.initUlBwp.puschCfg);
1233                }
1234             }
1235
1236             if(duMacDb)
1237                ret = fillAmbr(&macUeCfg->ambrCfg, ueCfgDb->ambrCfg , &duMacDb->ambrCfg, FALSE);
1238             else
1239                ret = fillAmbr(&macUeCfg->ambrCfg, ueCfgDb->ambrCfg , NULL, FALSE);
1240
1241             duFillModulationDetails(macUeCfg, duMacDb, ueCfgDb->ueNrCapability);
1242          }
1243       }
1244       else
1245       {
1246          fillDefaultMacCellGrpInfo(macUeCfg);
1247          fillDefaultPhyCellGrpInfo(macUeCfg);
1248          fillDefaultSpCellGrpInfo(macUeCfg);
1249          fillDefaultModulation(macUeCfg);
1250       }
1251
1252       /* Filling LC Context */
1253       for(dbIdx = 0; (dbIdx < ueCfgDb->numMacLcs && ret == ROK); dbIdx++)
1254       {
1255          if(!ueCfgDb->macLcCfg[dbIdx].ulLcCfgPres)
1256          {
1257             /* Filling default UL LC config in MAC if not present */
1258             ueCfgDb->macLcCfg[dbIdx].ulLcCfgPres = true;
1259             fillDefaultUlLcCfg(&ueCfgDb->macLcCfg[dbIdx].ulLcCfg);
1260          }
1261
1262          if(duMacDb)
1263          {
1264             for(lcIdx = 0; lcIdx < duMacDb->numLcs; lcIdx++)
1265             {
1266                if(ueCfgDb->macLcCfg[dbIdx].lcId == duMacDb->lcCfgList[lcIdx].lcId)
1267                {
1268                   lcIdFound = true;
1269                   if((ueCfgDb->macLcCfg[dbIdx].configType == CONFIG_UNKNOWN) ||
1270                         (ueCfgDb->macLcCfg[dbIdx].configType == CONFIG_MOD))
1271                   {
1272                      ueCfgDb->macLcCfg[dbIdx].configType = CONFIG_MOD;
1273                      ret = fillMacLcCfgToAddMod(&macUeCfg->lcCfgList[dbIdx], &ueCfgDb->macLcCfg[dbIdx],\
1274                            &duMacDb->lcCfgList[lcIdx], FALSE);
1275                   }
1276                }
1277                else
1278                   lcIdFound = false;
1279             }
1280          }
1281
1282          if(!lcIdFound)
1283          {
1284             /* ADD/DEL CONFIG */
1285             ret = fillMacLcCfgToAddMod(&macUeCfg->lcCfgList[dbIdx], &ueCfgDb->macLcCfg[dbIdx], NULL, FALSE);
1286          }
1287          if(ret == ROK)
1288          {
1289             macUeCfg->numLcs++;
1290          }
1291          else
1292          {
1293             DU_LOG("\nERROR  -->  DU APP : Failed to add Lc at Idx %d in fillMacUeCfg()", dbIdx); 
1294             break;
1295          }
1296       }/*End of Outer FOR loop */
1297       memcpy(&ueCfgDb->copyOfmacUeCfg, macUeCfg, sizeof(MacUeCfg));
1298    }
1299    return ret;
1300 }
1301
1302 /******************************************************************
1303  *
1304  * @brief Fills Rlc AM Information
1305  *
1306  * @details
1307  *
1308  *    Function : fillDefaultAmInfo
1309  *
1310  *    Functionality: Fills Rlc AM Information
1311  *
1312  * @params[in]  AmBearerCfg *amCfg
1313  * @return void
1314  *
1315  *****************************************************************/
1316 void fillDefaultAmInfo(AmBearerCfg *amCfg)
1317 {
1318    /* DL AM */
1319    amCfg->dlAmCfg.snLenDl     = AM_SIZE_12;
1320    amCfg->dlAmCfg.pollRetxTmr = T_POLL_RETRANSMIT_VAL;
1321    amCfg->dlAmCfg.pollPdu     = POLL_PDU_VAL;
1322    amCfg->dlAmCfg.pollByte    = POLL_BYTE_VAL;
1323    amCfg->dlAmCfg.maxRetxTh   = MAX_RETX_THRESHOLD_VAL;   
1324  
1325    /* UL AM */
1326    amCfg->ulAmCfg.snLenUl     = AM_SIZE_12;
1327    amCfg->ulAmCfg.reAssemTmr  = T_REASSEMBLY_VAL; 
1328    amCfg->ulAmCfg.statProhTmr = T_STATUS_PROHIBHIT_VAL;
1329 }
1330
1331 /******************************************************************
1332  *
1333  * @brief Fills RLC UM Bi Directional Information
1334  *
1335  * @details
1336  *
1337  *    Function : fillDefaultUmBiInfo
1338  *
1339  *    Functionality: Fills RLC UM Bi Directional Information
1340  *
1341  * @params[in]  UmBiDirBearerCfg *umBiDirCfg
1342  * @return void
1343  *
1344  *****************************************************************/
1345 void fillDefaultUmBiInfo(UmBiDirBearerCfg *umBiDirCfg)
1346 {
1347    /* UL UM BI DIR INFO */
1348    umBiDirCfg->ulUmCfg.snLenUlUm = UM_SIZE_12;
1349    umBiDirCfg->ulUmCfg.reAssemTmr = T_REASSEMBLY_VAL;
1350
1351    /* DL UM BI DIR INFO */
1352    umBiDirCfg->dlUmCfg.snLenDlUm  = UM_SIZE_12;
1353 }
1354
1355 /******************************************************************
1356  *
1357  * @brief Fills RLC UM Uni Directional UL Information
1358  *
1359  * @details
1360  *
1361  *    Function : fillDefaultUmUlInfo
1362  *
1363  *    Functionality: Fills RLC UM Uni Directional Info
1364  *
1365  * @params[in]  UmUniDirUlBearerCfg *UmUlCfg
1366  * @return void
1367  *
1368  *****************************************************************/
1369 void fillDefaultUmUlInfo(UmUniDirUlBearerCfg *UmUlCfg)
1370 {
1371    UmUlCfg->ulUmCfg.snLenUlUm = UM_SIZE_12;
1372    UmUlCfg->ulUmCfg.reAssemTmr = T_REASSEMBLY_VAL;
1373 }
1374
1375 /******************************************************************
1376  *
1377  * @brief Fills RLC UM Uni Directional DL Information
1378  *
1379  * @details
1380  *
1381  *    Function : fillDefaultUmDlInfo
1382  *
1383  *    Functionality: Fills RLC UM Uni Directional DL Info
1384  *
1385  * @params[in]  UmUniDirDlBearerCfg *UmDlCfg
1386  * @return void
1387  *
1388  *****************************************************************/
1389 void fillDefaultUmDlInfo(UmUniDirDlBearerCfg *UmDlCfg)
1390 {
1391    UmDlCfg->dlUmCfg.snLenDlUm  = UM_SIZE_12;
1392 }
1393
1394 /******************************************************************
1395  *
1396  * @brief Builds Rlc Mode Default Configuration
1397  *
1398  * @details
1399  *
1400  *    Function : fillDefaultRlcModeCfg
1401  *
1402  *    Functionality: Builds Rlc Mode Default Configuration
1403  *
1404  * @params[in]  rlcMode, RlcBearerCfg *lcCfg
1405  * @return ROK/RFAILED
1406  *
1407  *****************************************************************/
1408
1409 uint8_t fillDefaultRlcModeCfg(uint8_t rlcMode, RlcBearerCfg *lcCfg)
1410 {
1411    if(lcCfg)
1412    {
1413       switch(rlcMode)
1414       {
1415          case RLC_AM :
1416             {
1417                if(!lcCfg->u.amCfg)
1418                {
1419                   DU_ALLOC_SHRABL_BUF(lcCfg->u.amCfg, sizeof(AmBearerCfg));
1420                   if(lcCfg->u.amCfg)
1421                      fillDefaultAmInfo(lcCfg->u.amCfg);
1422                   else
1423                   {
1424                      DU_LOG("\n ERROR  -->  DU APP : Memory Alloc failed at AmCfg at fillDefaultRlcModeCfg()");
1425                      return RFAILED;
1426                   }
1427                }
1428                break;
1429             }
1430          case RLC_UM_BI_DIRECTIONAL :
1431             {
1432                if(!lcCfg->u.umBiDirCfg)
1433                {
1434                   DU_ALLOC_SHRABL_BUF(lcCfg->u.umBiDirCfg, sizeof(UmBiDirBearerCfg));
1435                   if(lcCfg->u.umBiDirCfg)
1436                      fillDefaultUmBiInfo(lcCfg->u.umBiDirCfg);
1437                   else
1438                   {
1439                      DU_LOG("\n ERROR  -->  DU APP : Memory Alloc failed at UmBiDirCfg at fillDefaultRlcModeCfg()");
1440                      return RFAILED;
1441                   }
1442                }
1443                break;
1444             }
1445          case RLC_UM_UNI_DIRECTIONAL_UL :
1446             {
1447                if(!lcCfg->u.umUniDirUlCfg)
1448                {
1449                   DU_ALLOC_SHRABL_BUF(lcCfg->u.umUniDirUlCfg, sizeof(UmUniDirUlBearerCfg));
1450                   if(lcCfg->u.umUniDirUlCfg)
1451                      fillDefaultUmUlInfo(lcCfg->u.umUniDirUlCfg);
1452                   else
1453                   {
1454                      DU_LOG("\n ERROR  -->  DU APP : Memory Alloc failed at UmUniDirUlCfg at fillDefaultRlcModeCfg()");
1455                      return RFAILED;
1456                   }
1457                }
1458                break;
1459             }
1460          case RLC_UM_UNI_DIRECTIONAL_DL :
1461             {
1462                if(!lcCfg->u.umUniDirDlCfg)
1463                {
1464                   DU_ALLOC_SHRABL_BUF(lcCfg->u.umUniDirDlCfg, sizeof(UmUniDirDlBearerCfg));
1465                   if(lcCfg->u.umUniDirDlCfg)
1466                      fillDefaultUmDlInfo(lcCfg->u.umUniDirDlCfg);
1467                   else
1468                   {
1469                      DU_LOG("\n ERROR  -->  DU APP : Memory Alloc failed at UmUniDirDlCfg at fillDefaultRlcModeCfg()");
1470                      return RFAILED;
1471                   }
1472                }
1473                break;
1474             }
1475          default:
1476             DU_LOG("\nERROR  -->  DUAPP: Invalid rlcMode %d at extractRlcCfgToAddMod()", rlcMode);
1477             return RFAILED;
1478       }
1479
1480    }
1481    else
1482    {
1483       DU_LOG("\nERROR  -->  DUAPP: Received LC Config is NULL");
1484       return RFAILED;
1485    }
1486    return ROK;
1487 }
1488
1489 /*******************************************************************
1490  *
1491  * @brief Function to fill Rlc Lc Cfg for SRB1
1492  *
1493  * @details
1494  *
1495  *    Function : fillRlcSrb1LcCfg
1496  *
1497  *    Functionality: 
1498  *     Function to fill Rlc Lc Cfg for SRB1
1499  * 
1500  *  @params[in]     Pointer to RlcBearerCfg
1501  *  @return ROK/RFAILED
1502  * 
1503  *****************************************************************/
1504
1505 uint8_t fillRlcSrb1LcCfg(RlcBearerCfg *rlcLcCfg)
1506 {
1507    uint8_t ret = ROK;
1508
1509    rlcLcCfg->rbId   = SRB1_LCID;
1510    rlcLcCfg->rbType = RB_TYPE_SRB;
1511    rlcLcCfg->lcId   = SRB1_LCID;
1512    rlcLcCfg->lcType = LCH_DCCH;
1513    rlcLcCfg->rlcMode = RLC_AM;
1514    rlcLcCfg->configType = CONFIG_ADD;
1515    ret = fillDefaultRlcModeCfg(rlcLcCfg->rlcMode, rlcLcCfg);
1516    rlcLcCfg->isLcAddModRspSent = true;
1517    return ret;
1518 }
1519
1520 /*******************************************************************
1521  *
1522  * @brief Processes UE ReConfig Req to RLC UL
1523  *
1524  * @details
1525  *
1526  *    Function : sendUeReCfgReqToRlc
1527  *
1528  *    Functionality: 
1529  *     Processes UE Reconfig Req to RLC UL
1530  * 
1531  *  @params[in]     Pointer to RlcUeCfg
1532  *  @return ROK     - success
1533  *          RFAILED - failure
1534  * 
1535  *****************************************************************/
1536
1537 uint8_t sendUeReCfgReqToRlc(RlcUeCfg *rlcUeCfg)
1538 {
1539    uint8_t ret;
1540    Pst pst;
1541    
1542    FILL_PST_DUAPP_TO_RLC(pst, RLC_UL_INST, EVENT_RLC_UE_RECONFIG_REQ);
1543    if(rlcUeCfg)
1544    {
1545       /* Processing one Ue at a time to RLC */
1546       DU_LOG("\nDEBUG   -->  DU_APP: Sending Ue Reconfig Request to RLC UL");
1547       ret = (*packRlcUeReconfigReqOpts[pst.selector])(&pst, rlcUeCfg);
1548       if(ret == RFAILED)
1549       {
1550          DU_LOG("\nERROR  -->  DU_APP : Failed to send Ue Reconfig Req to RLC at sendUeReCfgReqToRlc()");
1551          DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, rlcUeCfg, sizeof(RlcUeCfg));
1552       }
1553    }
1554    else
1555    {
1556       DU_LOG("\nERROR  -->   DU_APP: Received RlcUeCfg is NULL at sendUeReCfgReqToRlc()");
1557       ret = RFAILED;
1558    }
1559    return ret;
1560 }
1561
1562 /******************************************************************
1563  *
1564  * @brief Fills Snssai information
1565  *
1566  * @details
1567  *
1568  *    Function : fillSnssaiInfo
1569  *
1570  *    Functionality: Fills Snssai information
1571  *
1572  *  @params[in]    LcCfg *snssaiTobeSend, LcCfg *snssaiDb, LcCfg *oldSnssai,
1573  *  Bool toUpdateg
1574  *  @return ROK     - success
1575  *          RFAILED - failure
1576  * 
1577  *****************************************************************/
1578 uint8_t fillSnssaiInfo(Snssai *snssaiTobeSend, Snssai *snssaiDb, Snssai **oldSnssai, Bool toUpdate)
1579 {
1580    if(!toUpdate)
1581    {
1582       if(snssaiDb)
1583          snssaiTobeSend = snssaiDb;
1584       else if(oldSnssai)
1585          snssaiTobeSend = *oldSnssai;
1586       else
1587          snssaiTobeSend = NULL;
1588    }
1589    else
1590    {
1591       if(snssaiDb)
1592       {
1593          if(*oldSnssai)
1594             DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, *oldSnssai, sizeof(Snssai));
1595
1596          DU_ALLOC_SHRABL_BUF(*oldSnssai, sizeof(Snssai));
1597          if(*oldSnssai == NULL)
1598          {
1599             DU_LOG("\nERROR  -->  DU APP : Memory Alloc Failed at fillSnssaiInfo()");
1600             return RFAILED;
1601          }
1602          memcpy(*oldSnssai, snssaiDb, sizeof(Snssai));
1603       }
1604    }
1605    return ROK;
1606 }
1607 /******************************************************************
1608  *
1609  * @brief Fills RlcBearerCfg structure
1610  *
1611  * @details
1612  *
1613  *    Function : fillRlcUeCfg
1614  *
1615  *    Functionality: Fills Rlc Bearer Cfg
1616  *
1617  *
1618  *****************************************************************/
1619 uint8_t fillRlcUeCfg(uint16_t cellId, uint8_t duUeF1apId, DuUeCfg *ueCfgDb, RlcUeCfg *rlcUeCfg)
1620 {
1621    uint8_t ret, dbIdx, lcIdx, cellIdx;
1622    bool lcIdFound = false;
1623    RlcUeCfg *duRlcDb = NULLP;
1624
1625    ret = ROK;
1626    rlcUeCfg->cellId       = cellId;
1627    rlcUeCfg->ueId         = duUeF1apId;
1628
1629    if(!ueCfgDb)
1630    {
1631       /* Initial RB being Added */ 
1632       ret = fillRlcSrb1LcCfg(&rlcUeCfg->rlcLcCfg[0]);
1633       if(ret == ROK)
1634          rlcUeCfg->numLcs++;
1635       else
1636          memset(rlcUeCfg, 0, sizeof(RlcUeCfg));
1637    }
1638    else
1639    {
1640       /* Fetch RlcDb from DuUeCb */ 
1641       GET_CELL_IDX(cellId, cellIdx);
1642       duRlcDb = &duCb.actvCellLst[cellIdx]->ueCb[rlcUeCfg->ueId-1].rlcUeCfg;
1643       duRlcDb->rlcUeCfgState = UE_CFG_INPROGRESS;
1644
1645       for(dbIdx = 0; (dbIdx < ueCfgDb->numRlcLcs && ret == ROK); dbIdx++)
1646       {
1647          ret = fillDefaultRlcModeCfg(ueCfgDb->rlcLcCfg[dbIdx].rlcMode, &ueCfgDb->rlcLcCfg[dbIdx]);
1648          if(ret == RFAILED)
1649          {
1650             DU_LOG("\n ERROR  -->  DU APP : Failed to fill Rlc Mode at fillRlcUeCfg()");
1651             memset(rlcUeCfg, 0, sizeof(RlcUeCfg));
1652             return ret;
1653          }
1654
1655          if(duRlcDb)
1656          {
1657             /* Fill Rlc Ue Cfg List for ADD/MOD/DEL */
1658             for(lcIdx = 0; lcIdx < duRlcDb->numLcs; lcIdx++)
1659             { 
1660                if(ueCfgDb->rlcLcCfg[dbIdx].lcId == duRlcDb->rlcLcCfg[lcIdx].lcId)
1661                {
1662                   lcIdFound = true;
1663                   if((ueCfgDb->rlcLcCfg[dbIdx].configType == CONFIG_UNKNOWN)||
1664                         (ueCfgDb->rlcLcCfg[dbIdx].configType == CONFIG_MOD))
1665                   {
1666                      /* MOD */ 
1667                      ueCfgDb->rlcLcCfg[dbIdx].configType = CONFIG_MOD; /* update Db for MOD type */
1668                      memcpy(&rlcUeCfg->rlcLcCfg[dbIdx], &ueCfgDb->rlcLcCfg[dbIdx], sizeof(RlcBearerCfg));
1669                      fillSnssaiInfo(rlcUeCfg->rlcLcCfg[dbIdx].snssai, ueCfgDb->rlcLcCfg[dbIdx].snssai,\
1670                            &duRlcDb->rlcLcCfg[lcIdx].snssai,false);
1671                   }
1672                }
1673                else
1674                   lcIdFound = false;
1675             }
1676          }
1677
1678          if(!lcIdFound)
1679          {
1680             /* ADD/ DEL Config Type */
1681             memcpy(&rlcUeCfg->rlcLcCfg[dbIdx], &ueCfgDb->rlcLcCfg[dbIdx], sizeof(RlcBearerCfg));
1682             fillSnssaiInfo(rlcUeCfg->rlcLcCfg[dbIdx].snssai, ueCfgDb->rlcLcCfg[dbIdx].snssai, NULL, false);
1683          }
1684          rlcUeCfg->numLcs++;
1685       }
1686    }
1687    return ret;
1688 }
1689
1690 /******************************************************************
1691  *
1692  * @brief creates UE context
1693  *
1694  * @details
1695  *
1696  *    Function : duCreateUeCb
1697  *
1698  *    Functionality: Creates UE Conetxt
1699  *
1700  * @params[in] UeCcchCtxt Pointer
1701  *             UeIdx Pointer
1702  *
1703  * @return ROK     - success
1704  *         RFAILED - failure
1705  * ****************************************************************/
1706 uint8_t duCreateUeCb(UeCcchCtxt *ueCcchCtxt, uint32_t gnbCuUeF1apId)
1707 {
1708    uint8_t cellIdx = 0;
1709    uint8_t ret     = ROK;
1710    uint8_t ueId = 0, ueIdx = 0;
1711
1712    for(cellIdx = 0; cellIdx < MAX_NUM_CELL; cellIdx++)
1713    {
1714       if(duCb.actvCellLst[cellIdx] && (ueCcchCtxt->cellId == duCb.actvCellLst[cellIdx]->cellId))
1715       {
1716          GET_UE_ID(ueCcchCtxt->crnti, ueId);
1717          DU_LOG("\nDEBUG   -->  DU_APP: Filling UeCb for ueId [%d]", ueId);
1718
1719          ueIdx = ueId-1;
1720          duCb.actvCellLst[cellIdx]->ueCb[ueIdx].f1UeDb        = NULLP;
1721          duCb.actvCellLst[cellIdx]->ueCb[ueIdx].crnti         = ueCcchCtxt->crnti;
1722          duCb.actvCellLst[cellIdx]->ueCb[ueIdx].gnbDuUeF1apId = ueCcchCtxt->gnbDuUeF1apId;
1723          duCb.actvCellLst[cellIdx]->ueCb[ueIdx].gnbCuUeF1apId = gnbCuUeF1apId;
1724          duCb.actvCellLst[cellIdx]->ueCb[ueIdx].drbBitMap     = NULLP;
1725          duCb.actvCellLst[cellIdx]->ueCb[ueIdx].ueState       = UE_ACTIVE;
1726
1727          /* Filling Mac Ue Config */ 
1728          memset(&duCb.actvCellLst[cellIdx]->ueCb[ueIdx].macUeCfg, 0, sizeof(MacUeCfg));
1729          ret = duBuildAndSendUeCreateReqToMac(ueCcchCtxt->cellId, ueCcchCtxt->gnbDuUeF1apId, ueCcchCtxt->crnti, NULL, 
1730                &duCb.actvCellLst[cellIdx]->ueCb[ueIdx].macUeCfg);
1731          if(ret == RFAILED)
1732             DU_LOG("\nERROR  -->  DU APP : Failed to send UE create request to MAC");
1733
1734          /* Filling Rlc Ue Config */
1735          memset(&duCb.actvCellLst[cellIdx]->ueCb[ueIdx].rlcUeCfg, 0, sizeof(RlcUeCfg));
1736          ret = duBuildAndSendUeCreateReqToRlc(ueCcchCtxt->cellId, ueCcchCtxt->gnbDuUeF1apId, NULL,
1737                &duCb.actvCellLst[cellIdx]->ueCb[ueIdx].rlcUeCfg);
1738          if(ret == RFAILED)
1739             DU_LOG("\nERROR  -->  DU APP : Failed to send UE create request to RLC");
1740
1741          duCb.actvCellLst[cellIdx]->numActvUes++;
1742          memset(ueCcchCtxt, 0, sizeof(UeCcchCtxt));
1743          duCb.numUe--;
1744       }
1745    }
1746    return ret;
1747 }
1748
1749 /******************************************************************
1750  *
1751  * @brief Builds and Send UE Create Request to MAC
1752  *
1753  * @details
1754  *
1755  *    Function : duBuildAndSendUeCreateReqToMac
1756  *
1757  *    Functionality: Builds and Send UE Create Request to MAC
1758  *
1759  * @Params[in]  cellId,
1760  *              ueId,
1761  *              crnti,
1762  *              UE config extracted from F1AP msg
1763  *              MAC UE config struct to be filled
1764  * @return ROK     - success
1765  *         RFAILED - failure
1766  *
1767  * ****************************************************************/
1768
1769 uint8_t duBuildAndSendUeCreateReqToMac(uint16_t cellId, uint8_t gnbDuUeF1apId, uint16_t crnti, DuUeCfg *ueCfgDb, MacUeCfg *duMacUeCfg)
1770 {
1771    uint8_t  ret = ROK;
1772    MacUeCfg *macUeCfg = NULLP;
1773    Pst       pst;
1774    memset(&pst, 0, sizeof(Pst));
1775
1776
1777    ret = fillMacUeCfg(cellId, gnbDuUeF1apId, crnti, ueCfgDb, duMacUeCfg);
1778    if(ret == RFAILED)
1779    {
1780       DU_LOG("\nERROR  -->  DU APP : Failed to fill MacUeCfg at duBuildAndSendUeCreateReqToMac()");
1781       return RFAILED;
1782    }
1783
1784    /* Fill Pst */
1785    FILL_PST_DUAPP_TO_MAC(pst, EVENT_MAC_UE_CREATE_REQ);
1786
1787    /* Copying ueCb to a sharable buffer */
1788    DU_ALLOC_SHRABL_BUF(macUeCfg, sizeof(MacUeCfg));
1789    if(macUeCfg)
1790    {
1791       memset(macUeCfg, 0, sizeof(MacUeCfg));
1792       memcpy(macUeCfg, duMacUeCfg, sizeof(MacUeCfg));
1793       DU_LOG("\nDEBUG   -->  DU_APP: Sending UE create request to MAC");
1794
1795       /* Processing one Ue at a time to MAC */
1796       ret = (*packMacUeCreateReqOpts[pst.selector])(&pst, macUeCfg);
1797       if(ret == RFAILED)
1798       {
1799          DU_LOG("\nERROR  -->  DU_APP : Failure in sending Ue Create Req to MAC at duBuildAndSendUeCreateReqToMac()");
1800          DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, macUeCfg, sizeof(MacUeCfg));
1801       }
1802    }
1803    else
1804    {
1805       DU_LOG("\n ERROR  -->  DU APP : Memory alloc failed at duBuildAndSendUeCreateReqToMac()");
1806       ret = RFAILED;
1807    }
1808    return ret;
1809 }
1810
1811 /*******************************************************************
1812  *
1813  * @brief Build and send RACH Resource request to MAC
1814  *
1815  * @details
1816  *
1817  *    Function : duBuildAndSendRachRsrcReqToMac
1818  *    Functionality:
1819  *        Build and send RACH Resource request to MAC
1820  *
1821  * @params[in] Cell Id
1822  *             UE Id
1823  * @return ROK     - success
1824  *         RFAILED - failure
1825  *
1826  * ****************************************************************/
1827 uint8_t duBuildAndSendRachRsrcReqToMac(uint16_t cellId, uint16_t ueId)
1828 {
1829    uint16_t cellIdx = 0, ssbIdx = 0;
1830    Pst pst;
1831    MacRachRsrcReq *rachRsrcReq = NULLP;
1832
1833    GET_CELL_IDX(cellId, cellIdx);
1834    if(duCb.actvCellLst[cellIdx] == NULLP)
1835    {
1836       DU_LOG("\nERROR  -->  DU APP : Cell Id [%d] not found in duBuildAndSendRachRsrcReqToMac()", cellId);
1837       return RFAILED;
1838    }
1839
1840    DU_ALLOC_SHRABL_BUF(rachRsrcReq, sizeof(MacRachRsrcReq));
1841    if(!rachRsrcReq)
1842    {
1843       DU_LOG("\nERROR  -->  DU APP : Failed to allocate memory for RACH Resource Request in \
1844             duBuildAndSendRachRsrcReqToMac()");
1845       return RFAILED;
1846    }
1847
1848    rachRsrcReq->cellId = cellId;
1849    rachRsrcReq->ueId = ueId;
1850    rachRsrcReq->numSsb = duCfgParam.macCellCfg.prachCfg.ssbPerRach;
1851    for(ssbIdx = 0; ssbIdx < rachRsrcReq->numSsb; ssbIdx++)
1852    {
1853       rachRsrcReq->ssbIdx[ssbIdx] = ssbIdx;
1854    }
1855
1856    /* Fill Pst */
1857    FILL_PST_DUAPP_TO_MAC(pst, EVENT_MAC_RACH_RESOURCE_REQ);
1858    
1859    if(((*packMacRachRsrcReqOpts[pst.selector])(&pst, rachRsrcReq)) != ROK)
1860    {
1861       DU_LOG("\nERROR  -->  DU_APP : Failure in sending RACH Resource Request to MAC at \
1862             duBuildAndSendRachRsrcReqToMac()");
1863       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, rachRsrcReq, sizeof(MacRachRsrcReq));
1864       return RFAILED;
1865    }
1866
1867    return ROK;
1868 }
1869
1870 /*******************************************************************
1871  *
1872  * @brief Process RACH resource response from MAC
1873  *
1874  * @details
1875  *
1876  *    Function : DuProcMacRachRsrcRsp
1877  *    Functionality:
1878  *        Process RACH resource response from MAC
1879  *
1880  * @params[in] Post structure
1881  *             RACH resource response
1882  * @return ROK     - success
1883  *         RFAILED - failure
1884  *
1885  * ****************************************************************/
1886 uint8_t DuProcMacRachRsrcRsp(Pst *pst, MacRachRsrcRsp *rachRsrcRsp)
1887 {
1888    uint8_t  ret = RFAILED;
1889    uint16_t cellIdx = 0;
1890    DuCellCb *cellCb = NULLP;
1891    DuUeCb   *ueCb = NULLP;
1892
1893    DU_LOG("\nINFO  -->  DU APP : Received RACH Resource Response from MAC. Cell ID [%d] UE ID [%d]",
1894          rachRsrcRsp->cellId, rachRsrcRsp->ueId);
1895
1896    if(rachRsrcRsp->result == MAC_DU_APP_RSP_OK)
1897    {
1898       DU_LOG("\nINFO : DU APP : RACH Resource Response from MAC : Result [SUCCESS]");
1899
1900       /* Fetch Cell Cb */
1901       GET_CELL_IDX(rachRsrcRsp->cellId, cellIdx);
1902       if(duCb.actvCellLst[cellIdx] && (duCb.actvCellLst[cellIdx]->cellId == rachRsrcRsp->cellId))
1903       {
1904          cellCb = duCb.actvCellLst[cellIdx];
1905
1906          /* Fetch UE CB */
1907          if(cellCb->ueCb[rachRsrcRsp->ueId-1].gnbDuUeF1apId == rachRsrcRsp->ueId)
1908          {
1909             ueCb = &cellCb->ueCb[rachRsrcRsp->ueId-1];
1910
1911             /* Store CRNTI alloted to this UE by MAC */
1912             ueCb->crnti = rachRsrcRsp->newCrnti;
1913
1914             /* Store the assigned CF-RA resources */
1915             memcpy(&ueCb->cfraResource, &rachRsrcRsp->cfraResource, sizeof(MacCfraResource));
1916
1917             /* RACH resources allocated to UE is sent to CU in UE Context Setup Response
1918              * along with the result of UE Context setup requested by CU */
1919             if((ret = BuildAndSendUeCtxtRsp(rachRsrcRsp->cellId, rachRsrcRsp->ueId)) != ROK)
1920                DU_LOG("\nERROR  ->  DU APP : Failure in BuildAndSendUeCtxtRsp()");
1921          }
1922          else
1923             DU_LOG("\nERROR  -->  DU APP : UE ID [%d] not found in DuProcMacRachRsrcRsp", rachRsrcRsp->ueId);
1924       }
1925       else
1926          DU_LOG("\nERROR  -->  DU APP : Cell ID [%d] not found in DuProcMacRachRsrcRsp", rachRsrcRsp->cellId);
1927    }
1928    else
1929       DU_LOG("\nINFO : DU APP : RACH Resource Response from MAC : Result [FAILURE]");
1930
1931    DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, rachRsrcRsp, sizeof(MacRachRsrcRsp));
1932    return ret;
1933 }
1934
1935 /*******************************************************************
1936  *
1937  * @brief To update DuUeCb Mac Cfg
1938  *
1939  * @details
1940  *
1941  *    Function : duUpdateMacCfg
1942  *    Functionality:  update DuUeCb MAC Cfg
1943  *
1944  * @params[in] DuUeCb Pointer
1945  *             F1UeContextSetupDb pointer 
1946  * @return ROK     - success
1947  *         RFAILED - failure
1948  *
1949  * ****************************************************************/
1950 uint8_t duUpdateMacCfg(MacUeCfg *macUeCfg, F1UeContextSetupDb *f1UeDb) 
1951 {
1952    uint8_t ret, lcIdx, dbIdx, numLcs, lcDelIdx, cellIdx;
1953    MacUeCfg *oldMacUeCfg;
1954    ret = ROK;
1955    
1956    GET_CELL_IDX(macUeCfg->cellId, cellIdx);
1957    if(duCb.actvCellLst[cellIdx] == NULLP)
1958    {
1959       DU_LOG("\nERROR  --> DU APP: CellId[%d] not found", macUeCfg->cellId);
1960       return RFAILED;
1961    }
1962    oldMacUeCfg = &duCb.actvCellLst[cellIdx]->ueCb[macUeCfg->ueId-1].macUeCfg;
1963
1964    /*Filling Cell Group Cfg*/
1965    ret =  procUeReCfgCellInfo(macUeCfg, &f1UeDb->duUeCfg.copyOfmacUeCfg, f1UeDb->duUeCfg.cellGrpCfg);
1966    if(ret == ROK)
1967    {
1968       if(macUeCfg->spCellCfg.servCellCfg.initDlBwp.pdschPresent)
1969       {
1970          fillStartSymbolAndLen(macUeCfg->spCellCfg.servCellCfg.initDlBwp.pdschCfg.numTimeDomRsrcAlloc,\
1971                &macUeCfg->spCellCfg.servCellCfg.initDlBwp.pdschCfg, NULL);
1972       }
1973       if(macUeCfg->spCellCfg.servCellCfg.initUlBwp.puschPresent)
1974       {
1975          fillStartSymbolAndLen(macUeCfg->spCellCfg.servCellCfg.initUlBwp.puschCfg.numTimeDomRsrcAlloc,\
1976                NULL, &macUeCfg->spCellCfg.servCellCfg.initUlBwp.puschCfg);
1977       }
1978       ret = fillAmbr(NULL, f1UeDb->duUeCfg.ambrCfg, &oldMacUeCfg->ambrCfg, true);
1979       duFillModulationDetails(macUeCfg, oldMacUeCfg, f1UeDb->duUeCfg.ueNrCapability);
1980    }
1981
1982    /* Filling LC Context */
1983    for(dbIdx = 0; (dbIdx < f1UeDb->duUeCfg.numMacLcs && ret == ROK); dbIdx++)
1984    {
1985       numLcs = macUeCfg->numLcs;
1986       for(lcIdx = 0; lcIdx < numLcs; lcIdx++)
1987       {
1988          if(f1UeDb->duUeCfg.macLcCfg[dbIdx].lcId ==  macUeCfg->lcCfgList[lcIdx].lcId)
1989          {
1990             if(f1UeDb->duUeCfg.macLcCfg[dbIdx].configType == CONFIG_MOD)
1991             {
1992                ret = fillMacLcCfgToAddMod(NULL, &f1UeDb->duUeCfg.macLcCfg[dbIdx], &macUeCfg->lcCfgList[lcIdx], true);
1993             }
1994             else if(f1UeDb->duUeCfg.macLcCfg[dbIdx].configType == CONFIG_DEL)
1995             {
1996                /* Free memory at matched  lcIdx index */
1997                freeMacLcCfg(&macUeCfg->lcCfgList[lcIdx]);
1998                macUeCfg->numLcs--;
1999                for(lcDelIdx = lcIdx; lcDelIdx < macUeCfg->numLcs; lcDelIdx++)
2000                {
2001                   /* moving all elements one index ahead */
2002                   ret = fillMacLcCfgToAddMod(NULL,  &macUeCfg->lcCfgList[lcDelIdx+1], &macUeCfg->lcCfgList[lcDelIdx], true);
2003                   freeMacLcCfg(&macUeCfg->lcCfgList[lcDelIdx+1]);
2004                   if(ret == RFAILED)
2005                   {
2006                      DU_LOG("\nERROR  -->  DU APP : Failed to delete LC at Idx %d in duUpdateMacCfg()", lcDelIdx);
2007                      break;
2008                   }
2009                }
2010             }
2011          }
2012       } 
2013       if(f1UeDb->duUeCfg.macLcCfg[dbIdx].configType == CONFIG_ADD)
2014       {
2015          ret = fillMacLcCfgToAddMod(NULL, &f1UeDb->duUeCfg.macLcCfg[dbIdx], &macUeCfg->lcCfgList[numLcs], true);
2016          if(ret == RFAILED)
2017          {
2018             DU_LOG("\nERROR  -->  DU APP : Failed to add LC at Idx %d in duUpdateMacCfg()", numLcs);
2019             break;
2020          }
2021          macUeCfg->numLcs++;
2022       }
2023                 
2024    }
2025    return ret;
2026 }
2027
2028 /******************************************************************
2029  *
2030  * @brief Function to fill the RLC Lc cfg from ueSetupReqDb
2031  *
2032  * @details
2033  *
2034  *    Function : fillRlcCfgToAddMod
2035  *
2036  *    Functionality: Function to fill the RLC Lc cfg from ueSetupReqDb
2037  *
2038  *
2039  *****************************************************************/
2040
2041 uint8_t fillRlcCfgToAddMod(RlcBearerCfg *lcCfg, RlcBearerCfg *f1UeDbLcCfg)
2042 {
2043    lcCfg->configType = f1UeDbLcCfg->configType;
2044    lcCfg->rbId       = f1UeDbLcCfg->rbId;
2045    lcCfg->rbType     = f1UeDbLcCfg->rbType;
2046    lcCfg->lcId       = f1UeDbLcCfg->lcId;
2047    lcCfg->lcType     = f1UeDbLcCfg->lcType;
2048    lcCfg->rlcMode    = f1UeDbLcCfg->rlcMode;
2049    
2050    switch(lcCfg->rlcMode)
2051    {
2052       case RLC_AM :
2053          {
2054             if(!lcCfg->u.amCfg)
2055             {
2056                DU_ALLOC_SHRABL_BUF(lcCfg->u.amCfg, sizeof(AmBearerCfg));
2057                if(!lcCfg->u.amCfg)
2058                   return RFAILED;
2059             }
2060             /* DL AM */
2061             lcCfg->u.amCfg->dlAmCfg.snLenDl     = f1UeDbLcCfg->u.amCfg->dlAmCfg.snLenDl;    
2062             lcCfg->u.amCfg->dlAmCfg.pollRetxTmr = f1UeDbLcCfg->u.amCfg->dlAmCfg.pollRetxTmr;
2063             lcCfg->u.amCfg->dlAmCfg.pollPdu     = f1UeDbLcCfg->u.amCfg->dlAmCfg.pollPdu;
2064             lcCfg->u.amCfg->dlAmCfg.pollByte    = f1UeDbLcCfg->u.amCfg->dlAmCfg.pollByte;   
2065             lcCfg->u.amCfg->dlAmCfg.maxRetxTh   = f1UeDbLcCfg->u.amCfg->dlAmCfg.maxRetxTh;   
2066
2067             /* UL AM */
2068             lcCfg->u.amCfg->ulAmCfg.snLenUl     = f1UeDbLcCfg->u.amCfg->ulAmCfg.snLenUl;
2069             lcCfg->u.amCfg->ulAmCfg.reAssemTmr  = f1UeDbLcCfg->u.amCfg->ulAmCfg.reAssemTmr; 
2070             lcCfg->u.amCfg->ulAmCfg.statProhTmr = f1UeDbLcCfg->u.amCfg->ulAmCfg.statProhTmr;
2071             break;
2072          }
2073       case RLC_UM_BI_DIRECTIONAL :
2074          {
2075             if(!lcCfg->u.umBiDirCfg)
2076             {
2077                DU_ALLOC_SHRABL_BUF(lcCfg->u.umBiDirCfg, sizeof(UmBiDirBearerCfg));
2078                if(!lcCfg->u.umBiDirCfg)
2079                   return RFAILED;
2080             }
2081             /* UL UM BI DIR INFO */
2082             lcCfg->u.umBiDirCfg->ulUmCfg.snLenUlUm  = f1UeDbLcCfg->u.umBiDirCfg->ulUmCfg.snLenUlUm;  
2083             lcCfg->u.umBiDirCfg->ulUmCfg.reAssemTmr = f1UeDbLcCfg->u.umBiDirCfg->ulUmCfg.reAssemTmr;
2084             /* DL UM BI DIR INFO */
2085             lcCfg->u.umBiDirCfg->dlUmCfg.snLenDlUm  = f1UeDbLcCfg->u.umBiDirCfg->dlUmCfg.snLenDlUm;
2086             break;
2087          }
2088       case RLC_UM_UNI_DIRECTIONAL_UL :
2089          {
2090             if(!lcCfg->u.umUniDirUlCfg)
2091             {
2092                DU_ALLOC_SHRABL_BUF(lcCfg->u.umUniDirUlCfg, sizeof(UmUniDirUlBearerCfg));
2093                if(!lcCfg->u.umUniDirUlCfg)
2094                   return RFAILED;
2095             }
2096             lcCfg->u.umUniDirUlCfg->ulUmCfg.snLenUlUm  = f1UeDbLcCfg->u.umUniDirUlCfg->ulUmCfg.snLenUlUm;  
2097             lcCfg->u.umUniDirUlCfg->ulUmCfg.reAssemTmr = f1UeDbLcCfg->u.umUniDirUlCfg->ulUmCfg.reAssemTmr;
2098             break;
2099
2100          }
2101       case RLC_UM_UNI_DIRECTIONAL_DL :
2102          {
2103             if(!lcCfg->u.umUniDirDlCfg)
2104             {
2105                DU_ALLOC_SHRABL_BUF(lcCfg->u.umUniDirDlCfg, sizeof(UmUniDirDlBearerCfg));
2106                if(!lcCfg->u.umUniDirDlCfg)
2107                   return RFAILED;
2108             }
2109             lcCfg->u.umUniDirDlCfg->dlUmCfg.snLenDlUm  = f1UeDbLcCfg->u.umUniDirDlCfg->dlUmCfg.snLenDlUm;
2110             break;
2111          }
2112       default:
2113          DU_LOG("\nERROR  -->  DU_APP: Invalid Rlc Mode %d at fillRlcCfgToAddMod()", lcCfg->rlcMode);
2114          return RFAILED;
2115    }
2116    return ROK;
2117 }
2118
2119 /*******************************************************************
2120  *
2121  * @brief To update DuUeCb Rlc Lc Cfg
2122  *
2123  * @details
2124  *
2125  *    Function : duUpdateRlcLcCfg
2126  *    Functionality:  update DuUeCb Rlc Lc Cfg
2127  *
2128  * @params[in] DuUeCb Pointer
2129  *             F1UeContextSetupDb pointer 
2130  * @return ROK     - success
2131  *         RFAILED - failure
2132  *
2133  * ****************************************************************/
2134
2135 uint8_t duUpdateRlcLcCfg(RlcUeCfg *rlcUeCfg, F1UeContextSetupDb *f1UeDb)
2136 {
2137    uint8_t ret, lcIdx, dbIdx, numLcs, lcDelIdx;
2138
2139    ret = ROK;
2140    for(dbIdx = 0; (dbIdx < f1UeDb->duUeCfg.numRlcLcs && ret ==ROK); dbIdx++)
2141    {
2142       numLcs = rlcUeCfg->numLcs;
2143       for(lcIdx = 0; lcIdx < numLcs; lcIdx++)
2144       {
2145          if(f1UeDb->duUeCfg.rlcLcCfg[dbIdx].lcId == rlcUeCfg->rlcLcCfg[lcIdx].lcId)
2146          {
2147             if(f1UeDb->duUeCfg.rlcLcCfg[dbIdx].configType == CONFIG_MOD)
2148             {
2149                ret = fillRlcCfgToAddMod(&rlcUeCfg->rlcLcCfg[lcIdx], &f1UeDb->duUeCfg.rlcLcCfg[dbIdx]);
2150                if(ret == RFAILED)
2151                {
2152                   DU_LOG("\nERROR  -->  DU APP : Failed to modify LC at Idx %d in duUpdateRlcCfg()", lcDelIdx);
2153                   break;
2154                }
2155                fillSnssaiInfo(NULL, f1UeDb->duUeCfg.rlcLcCfg[dbIdx].snssai, &rlcUeCfg->rlcLcCfg[lcIdx].snssai, true);
2156             }
2157             else if(f1UeDb->duUeCfg.rlcLcCfg[dbIdx].configType == CONFIG_DEL)
2158             {
2159                /* Free memory at matched lcIdx index */
2160                freeRlcLcCfg(&rlcUeCfg->rlcLcCfg[lcIdx]);
2161                rlcUeCfg->numLcs--;
2162                for(lcDelIdx = lcIdx; lcDelIdx < rlcUeCfg->numLcs; lcDelIdx++)
2163                {
2164                   ret = fillRlcCfgToAddMod(&rlcUeCfg->rlcLcCfg[lcDelIdx], &rlcUeCfg->rlcLcCfg[lcDelIdx+1]);
2165                   fillSnssaiInfo(NULL, rlcUeCfg->rlcLcCfg[lcDelIdx+1].snssai, &rlcUeCfg->rlcLcCfg[lcDelIdx].snssai,\
2166                   true);
2167                   freeRlcLcCfg(&rlcUeCfg->rlcLcCfg[lcDelIdx+1]);
2168                   if(ret == RFAILED)
2169                   {
2170                      DU_LOG("\nERROR  -->  DU APP : Failed to delete LC at Idx %d in duUpdateRlcCfg()", lcDelIdx);
2171                      break;
2172                   }
2173                }
2174             }
2175          }
2176       }
2177       if(f1UeDb->duUeCfg.rlcLcCfg[dbIdx].configType == CONFIG_ADD)
2178       {
2179          ret = fillRlcCfgToAddMod(&rlcUeCfg->rlcLcCfg[lcIdx], &f1UeDb->duUeCfg.rlcLcCfg[dbIdx]);
2180          if(ret == ROK)
2181          {    
2182             fillSnssaiInfo(NULL, f1UeDb->duUeCfg.rlcLcCfg[dbIdx].snssai, &rlcUeCfg->rlcLcCfg[ rlcUeCfg->numLcs].snssai,\
2183                   true);
2184             rlcUeCfg->numLcs++;
2185          }
2186       }
2187    }
2188    return ret;
2189 }
2190
2191 /*******************************************************************
2192  *
2193  * @brief Function to fill Tunnel Config to Add/Mod
2194  * 
2195  *
2196  * @details
2197  *
2198  *    Function : fillTnlCfgToAddMod
2199  *
2200  *    Functionality: Function to fill tunnel Config to Add/Mod
2201  *
2202  * @params[in] Pointer to tnlCfgDb,
2203  *             pointer to f1TnlCfg
2204  * @return ROK     - success
2205  *         RFAILED - failure
2206  *
2207  * ****************************************************************/
2208 uint8_t fillTnlCfgToAddMod(UpTnlCfg **ueCbTnlCfg, UpTnlCfg *f1TnlCfg)
2209 {
2210    if(*ueCbTnlCfg)
2211    {
2212       DU_FREE((*ueCbTnlCfg)->tnlCfg1, sizeof(GtpTnlCfg));
2213       DU_FREE(*ueCbTnlCfg, sizeof(UpTnlCfg));
2214    }
2215
2216    if(*ueCbTnlCfg == NULLP)
2217    {
2218       /* copying to DuCb Tnl Cfg */
2219       DU_ALLOC(*ueCbTnlCfg, sizeof(UpTnlCfg));
2220       if(*ueCbTnlCfg == NULLP)
2221       {
2222          DU_LOG("\nERROR  -->  DU_APP : fillTnlCfgToAddMod: Memory Alloc failed for drbId[%d]", f1TnlCfg->drbId);
2223          return RFAILED;
2224       }
2225    }
2226    memset(*ueCbTnlCfg, 0, sizeof(UpTnlCfg));
2227    (*ueCbTnlCfg)->configType = f1TnlCfg->configType;
2228    (*ueCbTnlCfg)->cellId    = f1TnlCfg->cellId;
2229    (*ueCbTnlCfg)->ueId      = f1TnlCfg->ueId;
2230    (*ueCbTnlCfg)->drbId     = f1TnlCfg->drbId;
2231    if(f1TnlCfg->tnlCfg1)
2232    {
2233       if((*ueCbTnlCfg)->tnlCfg1 == NULLP)
2234       {
2235          DU_ALLOC((*ueCbTnlCfg)->tnlCfg1, sizeof(GtpTnlCfg));
2236          if((*ueCbTnlCfg)->tnlCfg1 == NULLP)
2237          {
2238             DU_LOG("\nERROR  -->  DU_APP : fillTnlCfgToAddMod: Memory Alloc failed for tnlCfg1 for drbId[%d]", f1TnlCfg->drbId);
2239             return RFAILED;
2240          }
2241       }
2242       memset((*ueCbTnlCfg)->tnlCfg1, 0, sizeof(GtpTnlCfg));
2243       (*ueCbTnlCfg)->tnlCfg1->teId = f1TnlCfg->tnlCfg1->teId;
2244       (*ueCbTnlCfg)->tnlCfg1->ulTnlAddress = f1TnlCfg->tnlCfg1->ulTnlAddress;
2245       (*ueCbTnlCfg)->tnlCfg1->dlTnlAddress = f1TnlCfg->tnlCfg1->dlTnlAddress;
2246    }
2247    return ROK;
2248 }
2249
2250 /*******************************************************************
2251  *
2252  * @brief Processing the tunnel Request to EGTP
2253  *        
2254  * @details
2255  *
2256  *    Function : duProcEgtpTunnelCfg
2257  *
2258  *    Functionality: Processing the tunnel Request to EGTP
2259  *
2260  * @params[in] UptnlCfg *duTnlCfg, UpTnlCfg *f1TnlCfg 
2261  * @return ROK     - success
2262  *         RFAILED - failure
2263  *
2264  * ****************************************************************/
2265
2266 uint8_t duProcEgtpTunnelCfg(uint8_t ueCbIdx, UpTnlCfg *duTnlCfg, UpTnlCfg *f1TnlCfg)
2267 {
2268    uint8_t ret = RFAILED, delIdx;
2269
2270    if(f1TnlCfg->tnlCfg1 == NULLP)
2271    {
2272       DU_LOG("\nERROR  -->  DU_APP : Tunnel config not found");
2273       return ret;
2274    }
2275
2276    if(f1TnlCfg->configType == CONFIG_ADD)
2277    {
2278       if(duSendEgtpTnlMgmtReq(EGTP_TNL_MGMT_ADD, NULLP, f1TnlCfg->tnlCfg1) == ROK)
2279       {
2280          if(fillTnlCfgToAddMod(&duCb.upTnlCfg[duCb.numTeId], f1TnlCfg) == ROK)
2281          {
2282             duCb.numTeId++;
2283             ret = ROK;
2284          }
2285       }      
2286    }
2287    else if(f1TnlCfg->configType == CONFIG_MOD)
2288    {
2289       if(duSendEgtpTnlMgmtReq(EGTP_TNL_MGMT_MOD, duTnlCfg->tnlCfg1->teId, f1TnlCfg->tnlCfg1) == ROK)
2290       {
2291          if(fillTnlCfgToAddMod(&duTnlCfg, f1TnlCfg) == ROK)
2292          {
2293             ret = ROK;
2294          }
2295       }   
2296    }
2297    else if(f1TnlCfg->configType == CONFIG_DEL)
2298    {
2299       if(duSendEgtpTnlMgmtReq(EGTP_TNL_MGMT_DEL, duTnlCfg->tnlCfg1->teId, f1TnlCfg->tnlCfg1) == ROK)
2300       {    
2301          /* Free memory at drbIdx */
2302          duCb.numTeId--;
2303          for(delIdx = ueCbIdx; delIdx < duCb.numTeId; delIdx++)
2304          {
2305             /* moving all elements one index ahead */
2306             ret = fillTnlCfgToAddMod(&duCb.upTnlCfg[delIdx], duCb.upTnlCfg[delIdx+1]);
2307             if(ret != ROK)
2308             {
2309                return ret;
2310             }
2311          }
2312          if(duCb.upTnlCfg[delIdx])
2313          {
2314             DU_FREE(duCb.upTnlCfg[delIdx]->tnlCfg1, sizeof(GtpTnlCfg));
2315             DU_FREE(duCb.upTnlCfg[delIdx], sizeof(UpTnlCfg));
2316          }
2317       }   
2318    }
2319    return ret;
2320 }
2321
2322 /***********************************************************************
2323  *
2324  * @brief Function to fill Tunnel Config
2325  *        and sends tunnel Req to EGTP
2326  * 
2327  *
2328  * @details
2329  *
2330  *    Function : duUpdateTunnelCfgDb
2331  *
2332  *    Functionality: Function to fill tunnel Config
2333  *                   and sends tunnel Cfg Req to EGTP
2334  *
2335  * @params[in] ueId, cellId, DuUeCfg 
2336  * @return ROK     - success
2337  *         RFAILED - failure
2338  *
2339  * ****************************************************************/
2340
2341 uint8_t duUpdateTunnelCfgDb(uint8_t ueId, uint8_t cellId, DuUeCfg *duUeCfg)
2342 {
2343    uint8_t ret = ROK, drbIdx, teIdx;
2344    bool drbFound = false;
2345
2346    /*If Add/Mod tunnels request for that DRB is successful in EGTP */
2347    /*then update drbId and tunnel Info in duCb */
2348    for(drbIdx=0; drbIdx < duUeCfg->numDrb; drbIdx++)
2349    {
2350       duUeCfg->upTnlInfo[drbIdx].cellId = cellId;
2351       duUeCfg->upTnlInfo[drbIdx].ueId = ueId;
2352       for(teIdx = 0; teIdx < duCb.numTeId; teIdx++)
2353       {
2354          if((duCb.upTnlCfg[teIdx]->ueId == duUeCfg->upTnlInfo[drbIdx].ueId) && \
2355             (duCb.upTnlCfg[teIdx]->drbId == duUeCfg->upTnlInfo[drbIdx].drbId))
2356          {
2357             drbFound = true; /* existing DRB */
2358             if(duProcEgtpTunnelCfg(teIdx, duCb.upTnlCfg[teIdx], &duUeCfg->upTnlInfo[drbIdx]) != ROK)
2359             {
2360                DU_LOG("\nERROR  -> DU_APP : duUpdateTunnelCfgDb: Failed to modify tunnel req for Drb id[%d]",
2361                      duUeCfg->upTnlInfo[drbIdx].drbId);
2362                ret = RFAILED;
2363             }
2364             break;
2365          }
2366          else
2367             drbFound = false;
2368       }
2369
2370       if(!drbFound && ret == ROK)/* new DRB to Add */
2371       {
2372          if(duProcEgtpTunnelCfg(NULLP, NULLP, &duUeCfg->upTnlInfo[drbIdx]) != ROK)
2373          {
2374             DU_LOG("\nERROR  -> DU_APP : duUpdateTunnelCfgDb: Failed to add tunnel req for Drb id[%d]",
2375                   duUeCfg->upTnlInfo[drbIdx].drbId);
2376             ret = RFAILED;
2377             break;
2378          }
2379       }
2380       else
2381          break;
2382    }
2383    return ret;
2384 }
2385
2386 /*******************************************************************
2387  *
2388  * @brief @brief To update DuUeCb Mac and Rlc Ue Cfg
2389  * 
2390  *
2391  * @details
2392  *
2393  *    Function : duUpdateDuUeCbCfg
2394  *
2395  *    Functionality: update DuUeCb Mac and Rlc Ue Cfg
2396  *
2397  * @params[in] ueId, cellIdx 
2398  * @return ROK     - success
2399  *         RFAILED - failure
2400  *
2401  * ****************************************************************/
2402
2403 uint8_t duUpdateDuUeCbCfg(uint8_t ueId, uint8_t cellId)
2404 {
2405    uint8_t ret = ROK, cellIdx = 0, crnti=0;
2406    DuUeCb *ueCb = NULLP;
2407
2408    GET_CELL_IDX(cellId, cellIdx);
2409    ueCb = &duCb.actvCellLst[cellIdx]->ueCb[ueId-1];
2410
2411    /*Filling RLC Ue Cfg */
2412    ueCb->rlcUeCfg.cellId = cellId;
2413    ueCb->rlcUeCfg.ueId   = ueId;
2414    ret = duUpdateRlcLcCfg(&ueCb->rlcUeCfg, ueCb->f1UeDb);
2415    if(ret == ROK)
2416    {
2417       /*Filling MAC Ue Cfg */
2418       GET_CRNTI(crnti, ueId);
2419       ueCb->macUeCfg.cellId = cellId;
2420       ueCb->macUeCfg.ueId  = ueId;
2421       ueCb->macUeCfg.crnti  = crnti;
2422       ret = duUpdateMacCfg(&ueCb->macUeCfg, ueCb->f1UeDb);
2423       if(ret == RFAILED)
2424          DU_LOG("\nERROR  -->  DU APP : Failed while updating MAC LC Config at duUpdateDuUeCbCfg()");
2425       else
2426       {
2427          if(duUpdateTunnelCfgDb(ueId, cellId, &ueCb->f1UeDb->duUeCfg) != ROK)
2428          {
2429             DU_LOG("\nERROR  -->  DU_APP : Failed to establish tunnel in duUpdateDuUeCbCfg()");
2430             return RFAILED;
2431          }
2432       }
2433    }
2434    else
2435       DU_LOG("\nERROR  -->  DU APP : Failed while updating RLC LC Config at duUpdateDuUeCbCfg()");
2436    return ret;
2437 }
2438
2439 /*******************************************************************
2440  *
2441  * @brief Handle UE config response from MAC
2442  *
2443  * @details
2444  *
2445  *    Function : DuProcMacUeCfgRsp
2446  *
2447  *    Functionality: Handle UE Config response from MAC
2448  *
2449  * @params[in] Pointer to MacUeCfgRsp and Pst 
2450  * @return ROK     - success
2451  *         RFAILED - failure
2452  *
2453  * ****************************************************************/
2454 uint8_t DuProcMacUeCfgRsp(Pst *pst, MacUeCfgRsp *cfgRsp)
2455 {
2456    uint8_t ret = ROK;
2457    uint16_t cellIdx;
2458
2459    if(cfgRsp)
2460    {
2461       GET_CELL_IDX(cfgRsp->cellId, cellIdx);
2462       if(cfgRsp->result == MAC_DU_APP_RSP_OK)
2463       {
2464          if(pst->event == EVENT_MAC_UE_CREATE_RSP)
2465          {
2466             DU_LOG("\nINFO   -->  DU APP : MAC UE Create Response : SUCCESS [DU UE F1AP ID : %d]", cfgRsp->ueId);
2467
2468             if(duCb.actvCellLst[cellIdx] && (duCb.actvCellLst[cellIdx]->ueCb[cfgRsp->ueId -1].gnbDuUeF1apId == cfgRsp->ueId))
2469             {
2470                duCb.actvCellLst[cellIdx]->ueCb[cfgRsp->ueId -1].macUeCfg.macUeCfgState = UE_CREATE_COMPLETE;
2471
2472               if((duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].ueState == UE_HANDIN_IN_PROGRESS) && 
2473                     (duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].macUeCfg.macUeCfgState == UE_CREATE_COMPLETE) &&
2474                      (duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].rlcUeCfg.rlcUeCfgState == UE_CREATE_COMPLETE))
2475                {
2476                   if((ret = duUpdateDuUeCbCfg(cfgRsp->ueId, cfgRsp->cellId)) == ROK)
2477                   {
2478                      /* If UE is in handover, RACH resource needs to be requested
2479                       * from MAC for CFRA */
2480                      if((duBuildAndSendRachRsrcReqToMac(cfgRsp->cellId, cfgRsp->ueId)) != ROK)
2481                      {
2482                         DU_LOG("\nERROR  --> DU APP : Failed to send RACH Resource Request to MAC");
2483                         DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(MacUeCfgRsp));
2484                         return RFAILED;
2485                      }
2486                   }
2487                   else
2488                   {
2489                      DU_LOG("\nERROR  ->  DU APP : Failure in updating DU UE CB");
2490                      DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(MacUeCfgRsp));
2491                      return RFAILED;
2492                   }
2493                }
2494             }
2495          }
2496          else if(pst->event == EVENT_MAC_UE_RECONFIG_RSP)
2497          {
2498             DU_LOG("\nINFO   -->  DU APP : MAC UE Reconfig Response : SUCCESS [DU UE F1AP ID : %d]", cfgRsp->ueId);
2499             if(duCb.actvCellLst[cellIdx] && 
2500                   (duCb.actvCellLst[cellIdx]->ueCb[cfgRsp->ueId -1].gnbDuUeF1apId == cfgRsp->ueId))
2501             {
2502                duCb.actvCellLst[cellIdx]->ueCb[cfgRsp->ueId -1].macUeCfg.macUeCfgState = UE_RECFG_COMPLETE;
2503                if((duCb.actvCellLst[cellIdx]->ueCb[cfgRsp->ueId -1].macUeCfg.macUeCfgState == UE_RECFG_COMPLETE) &&
2504                      (duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].rlcUeCfg.rlcUeCfgState == UE_RECFG_COMPLETE))
2505                {
2506                   if((ret = duUpdateDuUeCbCfg(cfgRsp->ueId, cfgRsp->cellId)) == ROK)
2507                   {  
2508                      if((BuildAndSendUeCtxtRsp(cfgRsp->cellId, cfgRsp->ueId)) != ROK)
2509                      {
2510                         DU_LOG("\nERROR  ->  DU APP : Failure in BuildAndSendUeCtxtRsp()");
2511                         DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(MacUeCfgRsp));
2512                         return RFAILED;
2513                      }
2514                   }
2515                   else
2516                   {
2517                      DU_LOG("\nERROR  ->  DU APP : Failure in updating DU UE CB");
2518                      DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(MacUeCfgRsp));
2519                      return RFAILED;
2520                   }
2521                }
2522             }
2523          }
2524       }
2525       else
2526       {
2527          DU_LOG("\nERROR  -->  DU APP : MAC UE CFG Response for EVENT[%d]: FAILURE [DU UE F1AP ID : %d]", pst->event, cfgRsp->ueId);
2528          if(pst->event == EVENT_MAC_UE_RECONFIG_RSP)
2529          {
2530             //TODO: Send the failure case in Ue Context Setup Response
2531          }
2532          ret = RFAILED;
2533       }
2534       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(MacUeCfgRsp));
2535    }
2536    else
2537    {
2538       DU_LOG("\nERROR  -->  DU APP : Received MAC Ue Config Response is NULL at DuProcMacUeCfgRsp()");
2539       ret = RFAILED;
2540    }
2541    return ret;
2542 }
2543
2544
2545 /*******************************************************************
2546  *
2547  * @brief Processes UE create Req to RLC UL
2548  *
2549  * @details
2550  *
2551  *    Function : duBuildAndSendUeCreateReqToRlc
2552  *
2553  *    Functionality: 
2554  *     Processes UE create Req to RLC UL
2555  * 
2556  *  @params[in]  cellId,
2557  *               ueId,
2558  *               Pointer to RlcUeCfg
2559  *  @return ROK     - success
2560  *          RFAILED - failure
2561  * 
2562  *****************************************************************/
2563
2564 uint8_t duBuildAndSendUeCreateReqToRlc(uint16_t cellId, uint8_t gnbDuUeF1apId, DuUeCfg *ueCfgDb, RlcUeCfg *duRlcUeCfg)
2565 {
2566    uint8_t  ret = ROK;
2567    RlcUeCfg *rlcUeCfg = NULLP;
2568    Pst       pst;
2569   
2570    ret = fillRlcUeCfg(cellId, gnbDuUeF1apId, ueCfgDb, duRlcUeCfg);
2571    if(ret == RFAILED)
2572    {
2573       DU_LOG("\nERROR  -->  DU APP : Failed to fill Rlc Ue Cfg at duBuildAndSendUeCreateReqToRlc()");
2574       return ret;
2575    }
2576
2577    FILL_PST_DUAPP_TO_RLC(pst, RLC_UL_INST, EVENT_RLC_UE_CREATE_REQ);
2578    /* Copying ueCfg to a sharable buffer */
2579    DU_ALLOC_SHRABL_BUF(rlcUeCfg, sizeof(RlcUeCfg));
2580    if(rlcUeCfg)
2581    {
2582       memset(rlcUeCfg, 0, sizeof(RlcUeCfg));
2583       memcpy(rlcUeCfg, duRlcUeCfg, sizeof(RlcUeCfg));
2584       /* Processing one Ue at a time to RLC */
2585       DU_LOG("\nDEBUG   -->  DU_APP: Sending UE create request to RLC UL");
2586       ret = (*packRlcUeCreateReqOpts[pst.selector])(&pst, rlcUeCfg);
2587       if(ret == RFAILED)
2588       {
2589          DU_LOG("\nERROR  -->  DU_APP : Failure in sending Ue Create Req to RLC");
2590          DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, rlcUeCfg, sizeof(RlcUeCfg));
2591          ret = RFAILED;
2592       }
2593    }
2594    else
2595    {
2596       DU_LOG("\n ERROR  -->  DU APP : Memory alloc failed at duBuildAndSendUeCreateReqToRlc()");
2597       ret = RFAILED;
2598    }
2599    return ret;
2600 }
2601
2602 /*******************************************************************
2603  *
2604  * @brief Processes UE create Rsp received from RLC UL
2605  *
2606  * @details
2607  *
2608  
2609  *    Function : DuProcRlcUeCfgRsp
2610  *
2611  *    Functionality: 
2612  *     Processes UE create Rsp received from RLC UL
2613  * 
2614  *  @params[in]  Post structure
2615  *               Pointer to RlcCfgCfm
2616  *  @return ROK     - success
2617  *          RFAILED - failure
2618  * 
2619  *****************************************************************/
2620 uint8_t DuProcRlcUeCfgRsp(Pst *pst, RlcUeCfgRsp *cfgRsp)
2621 {
2622    uint8_t ret = ROK;
2623
2624    if(cfgRsp)
2625    {
2626       if(cfgRsp->result == RLC_DU_APP_RSP_OK)
2627       {
2628          if(pst->event == EVENT_RLC_UE_CREATE_RSP)
2629          {
2630             DU_LOG("\nINFO   -->  DU_APP: RLC UE Create Response : SUCCESS [UE IDX:%d]", cfgRsp->ueId);
2631             duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].rlcUeCfg.rlcUeCfgState = UE_CREATE_COMPLETE;
2632
2633             if((duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].ueState == UE_HANDIN_IN_PROGRESS) &&
2634                   (duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].macUeCfg.macUeCfgState == UE_CREATE_COMPLETE) &&
2635                   (duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].rlcUeCfg.rlcUeCfgState == UE_CREATE_COMPLETE))
2636             {
2637                if((ret = duUpdateDuUeCbCfg(cfgRsp->ueId, cfgRsp->cellId)) == ROK)
2638                {
2639                   /* If UE is in handover, RACH resource needs to be requested
2640                    * from MAC for CFRA */
2641                   if((duBuildAndSendRachRsrcReqToMac(cfgRsp->cellId, cfgRsp->ueId)) != ROK)
2642                   {
2643                      DU_LOG("\nERROR  --> DU APP : Failed to send RACH Resource Request to MAC");
2644                      DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(MacUeCfgRsp));
2645                      DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(RlcUeCfgRsp));
2646                      return RFAILED;
2647                   }
2648                }
2649                else
2650                {
2651                   DU_LOG("\nERROR  -->  DU APP : Failure in updating DU UE CB");
2652                   DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(RlcUeCfgRsp));
2653                   return RFAILED;
2654                }
2655             }
2656          }
2657          else if(pst->event == EVENT_RLC_UE_RECONFIG_RSP)
2658          {
2659             DU_LOG("\nINFO   -->  DU_APP: RLC UE Reconfig Response : SUCCESS [UE IDX:%d]", cfgRsp->ueId);
2660
2661             duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].rlcUeCfg.rlcUeCfgState = UE_RECFG_COMPLETE;
2662             if((duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].macUeCfg.macUeCfgState == UE_RECFG_COMPLETE) &&
2663                   (duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].rlcUeCfg.rlcUeCfgState == UE_RECFG_COMPLETE))
2664             {
2665                if((ret = duUpdateDuUeCbCfg(cfgRsp->ueId, cfgRsp->cellId)) == ROK)
2666                {
2667                   if((BuildAndSendUeCtxtRsp(cfgRsp->cellId, cfgRsp->ueId)) != ROK)
2668                   {
2669                      DU_LOG("\nERROR  -->  DU APP : Failure in BuildAndSendUeCtxtRsp");
2670                      DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(RlcUeCfgRsp));
2671                      return RFAILED;
2672                   }
2673                }
2674                else
2675                {
2676                   DU_LOG("\nERROR  -->  DU APP : Failure in updating DU UE CB");
2677                   DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(RlcUeCfgRsp));
2678                   return RFAILED;
2679                }
2680             }
2681          }
2682       }
2683       else
2684       {
2685          DU_LOG("\nERROR  -->  DU_APP: RLC UE CFG Response for EVENT[%d] : FAILED [UE IDX : %d, REASON :%d]",\
2686                pst->event, cfgRsp->ueId, cfgRsp->reason);
2687          if((pst->event == EVENT_RLC_UE_RECONFIG_RSP))
2688          {
2689             //TODO: update failure case in ue Context setup Response
2690          }
2691          ret = RFAILED;
2692       }
2693       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(RlcUeCfgRsp));
2694    }
2695    else
2696    {
2697       DU_LOG("\nERROR  -->  DU_APP: Received RLC Ue Config Response is NULL at DuProcRlcUeCfgRsp()");
2698       ret = RFAILED;
2699    }
2700    return ret;
2701 }
2702
2703 /*******************************************************************
2704  *
2705  * @brief Builds and Send Ue Reconfig Req to RLC
2706  *
2707  * @details
2708  *
2709  *    Function : duBuildAndSendUeReCfgReqToRLC
2710  *
2711  *    Functionality: Builds and Send Ue Reconfig Req to RLC
2712  *
2713  * @params[in] cellId, crnti
2714  *             DuUeCfg *ueCfgDb
2715  *             RlcUeCfg *rlcUeCfg
2716  * @return ROK     - success
2717  *         RFAILED - failure
2718  *
2719  * ****************************************************************/
2720
2721 uint8_t duBuildAndSendUeReCfgReqToRlc(uint8_t cellId, uint8_t gnbDuUeF1apId, uint8_t crnti, DuUeCfg *ueCfgDb)
2722 {
2723    uint8_t ret = ROK;
2724    RlcUeCfg *rlcUeCfg = NULLP;
2725
2726    DU_ALLOC_SHRABL_BUF(rlcUeCfg, sizeof(RlcUeCfg));
2727    if(rlcUeCfg)
2728    {
2729       memset(rlcUeCfg, 0, sizeof(RlcUeCfg));
2730       ret = fillRlcUeCfg(cellId, gnbDuUeF1apId, ueCfgDb, rlcUeCfg);
2731       if(ret == RFAILED)
2732          DU_LOG("\nERROR  -->  DU APP : Failed at duBuildAndSendUeReCfgReqToRlc()");
2733       else
2734          ret = sendUeReCfgReqToRlc(rlcUeCfg);
2735    }
2736    else
2737    {
2738       DU_LOG("\nERROR  -->  DU APP : Memory Alloc failed at duBuildAndSendUeReCfgReqToRlc()");
2739       ret = RFAILED;
2740    }
2741    return ret;
2742 }
2743
2744 /*******************************************************************
2745  *
2746  * @brief Builds and Send Ue Reconfig Req to MAC
2747  *
2748  * @details
2749  *
2750  *    Function : duBuildAndSendUeReCfgReqToMac
2751  *
2752  *    Functionality: Builds and Send Ue Reconfig Req to MAC
2753  *
2754  * @params[in] CellGroupConfigRrc_t *macCellGrpCfg
2755  *             DuUeCfg *ueCfgDb
2756  *             MacUeCfg    *macUeCfg
2757  * @return ROK     - success
2758  *         RFAILED - failure
2759  *
2760  * ****************************************************************/
2761
2762 uint8_t duBuildAndSendUeReCfgReqToMac(uint8_t cellId, uint8_t duUeF1apId, uint8_t crnti, DuUeCfg *ueCfgDb)
2763 {
2764    uint8_t ret = ROK;
2765    MacUeCfg *macUeCfg = NULLP;
2766
2767    DU_ALLOC_SHRABL_BUF(macUeCfg, sizeof(MacUeCfg));
2768    if(macUeCfg)
2769    {
2770       memset(macUeCfg, 0, sizeof(MacUeCfg));
2771       ret = fillMacUeCfg(cellId, duUeF1apId, crnti, ueCfgDb, macUeCfg);
2772       if(ret == RFAILED)
2773          DU_LOG("\nERROR  -->  DU APP : Failed to fill Mac Ue Cfg at duBuildAndSendUeReCfgReqToMac()");
2774       else
2775          ret = sendUeReCfgReqToMac(macUeCfg);
2776    }
2777    else
2778    {
2779       DU_LOG("\nERROR  -->  DU APP : Memory alloc failed for macUeCfg at duBuildAndSendUeReCfgReqToMac()");
2780       ret = RFAILED;
2781    }
2782    return ret;
2783 }
2784
2785 /*******************************************************************
2786  *
2787  * @brief Build and Send Ue context setup request
2788  *
2789  * @details
2790  *
2791  
2792  *    Function : duBuildAndSendUeContextSetupReq
2793  *
2794  *    Functionality: 
2795  *     Build and Send Ue context setup request
2796  * 
2797  *  @params[in]  cellId, crnti, DuUeCfg pointer
2798  *  @return ROK     - success
2799  *          RFAILED - failure
2800  * 
2801  *****************************************************************/
2802
2803 uint8_t duBuildAndSendUeContextSetupReq(uint16_t cellId, DuUeCb *ueCb)
2804 {
2805    uint8_t ret = ROK;
2806    uint16_t crnti; 
2807    DuUeCfg *duUeCfg = NULLP;
2808
2809    DU_LOG("\nDEBUG   -->  DU_APP: Processing Ue Context Setup Request for cellId [%d]", cellId);
2810
2811    if(!ueCb)
2812    {
2813       DU_LOG("\nERROR  -->  DU APP : UE Cb is NULL");
2814       return RFAILED;
2815    }
2816
2817    crnti = ueCb->crnti;
2818    duUeCfg = &ueCb->f1UeDb->duUeCfg;
2819
2820    /* If UE is being handed-in to this DU, UE context setup request will create
2821     * new UE context at MAC/SCH and RLC.
2822     * If UE is in active state, UE contex setup request will lead to
2823     * reconfiguration of UE at MAC/SCH and RLC
2824     */
2825    if(ueCb->ueState == UE_HANDIN_IN_PROGRESS)
2826    {
2827       /* Filling MAC UE Config */
2828       memset(&ueCb->macUeCfg, 0, sizeof(MacUeCfg));
2829
2830       /* Since UE attach has not yet happened, crnti is unknow. Hence passing 0 */
2831       ret = duBuildAndSendUeCreateReqToMac(cellId, ueCb->gnbDuUeF1apId, 0, duUeCfg, &ueCb->macUeCfg);
2832       if(ret == RFAILED)
2833          DU_LOG("\nERROR  -->  DU APP : Failed to send UE create request to MAC");
2834
2835       ret = duBuildAndSendUeCreateReqToRlc(cellId, ueCb->gnbDuUeF1apId, duUeCfg, &ueCb->rlcUeCfg);
2836       if(ret == RFAILED)
2837          DU_LOG("\nERROR  --> DU APP : Failed to send UE create request to RLC");
2838
2839    }
2840    else
2841    {
2842       /* Filling RLC UE Reconfig */ 
2843       ret = duBuildAndSendUeReCfgReqToRlc(cellId, ueCb->gnbDuUeF1apId, crnti, duUeCfg);
2844       if(ret == RFAILED)
2845          DU_LOG("\nERROR  -->  DU APP : Failed to build ctxt setup req for RLC at duBuildAndSendUeContextSetupReq()");
2846
2847       /* Filling MAC UE Reconfig */
2848       ret = duBuildAndSendUeReCfgReqToMac(cellId, ueCb->gnbDuUeF1apId, crnti, duUeCfg);
2849       if(ret == RFAILED)
2850          DU_LOG("\nERROR  -->  DU APP : Failed at build ctxt setup req for MAC at duBuildAndSendUeContextSetupReq()");
2851    }
2852
2853    return ret;
2854 }
2855
2856 /*******************************************************************
2857  *
2858  * @brief Processes DL Rsp received from RLC DL
2859  *
2860  * @details
2861  *
2862  
2863  *    Function : DuProcRlcDlRrcMsgRsp
2864  *
2865  *    Functionality: 
2866  *     Processes UE Rsp received from RLC DL
2867  * 
2868  *  @params[in]  Post structure
2869  *               Pointer to RlcCfgCfm
2870  *  @return ROK     - success
2871  *          RFAILED - failure
2872  * 
2873  *****************************************************************/
2874 uint8_t DuProcRlcDlRrcMsgRsp(Pst *pst, RlcDlRrcMsgRsp *dlRrcMsg)
2875 {
2876    uint8_t ret = ROK, ueId = 0;
2877    uint16_t cellId, crnti;
2878    DuUeCb *ueCb = NULLP;
2879    DlMsgState state;
2880
2881    state = dlRrcMsg->state;
2882    cellId = dlRrcMsg->cellId;
2883    crnti = dlRrcMsg->crnti;
2884    DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, dlRrcMsg, sizeof(RlcDlRrcMsgRsp));
2885
2886    if(state == TRANSMISSION_COMPLETE)
2887    {
2888       GET_UE_ID(crnti, ueId);
2889       ueCb = &duCb.actvCellLst[cellId -1]->ueCb[ueId -1];
2890
2891       if(ueCb->f1UeDb && ueCb->f1UeDb->dlRrcMsgPres)
2892       {
2893          if(ueCb->f1UeDb->actionType == UE_CTXT_SETUP)
2894          {
2895             ret = duBuildAndSendUeContextSetupReq(cellId, ueCb);
2896             if(ret == RFAILED)
2897                DU_LOG("\nERROR  -->  DU APP : Failed to process UE Context Setup Request in DuProcRlcDlRrcMsgRsp()");
2898          }
2899          
2900          if(ueCb->f1UeDb->actionType == UE_CTXT_MOD)
2901          {
2902             ret = duBuildAndSendUeContextModReq(cellId, ueCb->gnbDuUeF1apId, crnti, &ueCb->f1UeDb->duUeCfg);
2903             if(ret == RFAILED)
2904                DU_LOG("\nERROR  -->  DU APP : Failed to process UE Context Mod Request in DuProcRlcDlRrcMsgRsp()");
2905          }
2906
2907          if(ueCb->f1UeDb->actionType == UE_CTXT_RELEASE && ueCb->ueState == UE_ACTIVE)
2908          {
2909             ret = duBuildAndSendUeDeleteReq(cellId, crnti);
2910             if(ret == RFAILED)
2911             {
2912                DU_LOG("\nERROR  -->  DU APP : Failed to process UE Context Release Request in DuProcRlcDlRrcMsgRsp()");
2913             }
2914          }
2915       }
2916    }
2917    else
2918       DU_LOG("\nERROR  -->  DU APP : Failed to transmit DL RRC Msg");
2919
2920    return ret;
2921 }
2922 /*******************************************************************
2923  *
2924  * @brief Process UE context setup request from CU
2925  *
2926  * @details
2927  *
2928  *    Function : duProcUeContextSetupRequest 
2929  *
2930  *    Functionality: Process UE context setup request from CU
2931  *
2932  * @params[in] F1AP message
2933  * @return ROK     - success
2934  *         RFAILED - failure
2935  *
2936  * ****************************************************************/
2937
2938 uint8_t duProcUeContextSetupRequest(DuUeCb *ueCb)
2939 {
2940    uint8_t ret, cellId;
2941
2942    ret = ROK;
2943    if(ueCb)
2944    {
2945       cellId = duCb.actvCellLst[ueCb->f1UeDb->cellIdx]->cellId;
2946
2947       /* Send DL RRC msg for security Mode */
2948       if(ueCb->f1UeDb->dlRrcMsg)
2949       {
2950          if(ueCb->f1UeDb->dlRrcMsg->rrcMsgPdu)
2951          {
2952             /* Sending DL RRC Message to RLC */
2953             ret = duBuildAndSendDlRrcMsgToRlc(cellId, ueCb->rlcUeCfg, ueCb->f1UeDb->dlRrcMsg);
2954             if(ret == RFAILED)
2955             {
2956                DU_LOG("\nERROR  -->  DU APP : Failed to send DL RRC msg in duProcUeContextSetupRequest()");
2957                DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, ueCb->f1UeDb->dlRrcMsg->rrcMsgPdu,\
2958                      ueCb->f1UeDb->dlRrcMsg->rrcMsgSize);
2959                DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, ueCb->f1UeDb->dlRrcMsg, sizeof(F1DlRrcMsg));
2960             }
2961          }
2962       }
2963       else if(ueCb->f1UeDb->actionType == UE_CTXT_SETUP)
2964       {
2965          ret = duBuildAndSendUeContextSetupReq(cellId, ueCb);
2966          if(ret == RFAILED)
2967          {
2968             DU_LOG("\nERROR  -->  DU APP : Failed to build ue context setup Req in duProcUeContextSetupRequest()");
2969          }
2970       }
2971    }
2972    else
2973    {
2974       //TODO: To send the failure cause in UeContextSetupRsp
2975       DU_LOG("ERROR  -->  DU APP : Failed to process UE CNTXT SETUP REQ at duProcUeContextSetupRequest()");
2976    }
2977    return ret;
2978 }
2979
2980 /*******************************************************************
2981  *
2982  * @brief Build and Send Ue context mod request
2983  *
2984  * @details
2985  *
2986  *
2987  *    Function : duBuildAndSendUeContextModReq
2988  *
2989  *    Functionality: 
2990  *     Build and Send Ue context mod request
2991  * 
2992  *  @params[in]  cellId, crnti, DuUeCfg pointer
2993  *  @return ROK     - success
2994  *          RFAILED - failure
2995  * 
2996  *****************************************************************/
2997
2998 uint8_t duBuildAndSendUeContextModReq(uint16_t cellId, uint8_t gnbDuUeF1apId, uint16_t crnti, DuUeCfg *duUeCfg)
2999 {
3000    uint8_t ret = ROK;
3001
3002    DU_LOG("\nDEBUG   -->  DU_APP: Processing Ue Context Mod Request for cellId [%d]", cellId);
3003    /* Filling RLC Ue Reconfig */ 
3004    ret = duBuildAndSendUeReCfgReqToRlc(cellId, gnbDuUeF1apId, crnti, duUeCfg);
3005    if(ret == RFAILED)
3006       DU_LOG("\nERROR  -->  DU APP : Failed to build ctxt setup req for RLC at duBuildAndSendUeContextModReq()");
3007    
3008    /* Filling MAC Ue Reconfig */
3009    ret = duBuildAndSendUeReCfgReqToMac(cellId, gnbDuUeF1apId, crnti, duUeCfg);
3010    if(ret == RFAILED)
3011       DU_LOG("\nERROR  -->  DU APP : Failed at build ctxt setup req for MAC at duBuildAndSendUeContextModReq()");
3012
3013    return ret;
3014 }
3015 /*******************************************************************
3016  *
3017  * @brief Processing Ue context mod request
3018  *
3019  * @details
3020  *
3021  *
3022  *    Function : duProcUeContextModReq 
3023  *
3024  *    Functionality:
3025  *        Processing  Ue context mod request
3026  *
3027  *  @params[in] DuUeCb *ueCb 
3028  *  @return ROK     - success
3029  *          RFAILED - failure
3030  *
3031  *****************************************************************/
3032
3033 uint8_t duProcUeContextModReq(DuUeCb *ueCb)
3034 {
3035    uint8_t ret, cellId;
3036
3037    ret = ROK;
3038    if(ueCb)
3039    {
3040       cellId = duCb.actvCellLst[ueCb->f1UeDb->cellIdx]->cellId;
3041       /* Send DL RRC msg for security Mode */
3042       if(ueCb->f1UeDb->dlRrcMsg)
3043       {
3044          if(ueCb->f1UeDb->dlRrcMsg->rrcMsgPdu)
3045          {
3046             /* Sending DL RRC Message to RLC */
3047             ret = duBuildAndSendDlRrcMsgToRlc(cellId, ueCb->rlcUeCfg, ueCb->f1UeDb->dlRrcMsg);
3048             if(ret == RFAILED)
3049             {
3050                DU_LOG("\nERROR  -->  DU APP : Failed to send DL RRC msg in duProcUeContextModReq()");
3051                DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, ueCb->f1UeDb->dlRrcMsg->rrcMsgPdu,\
3052                      ueCb->f1UeDb->dlRrcMsg->rrcMsgSize);
3053                DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, ueCb->f1UeDb->dlRrcMsg, sizeof(F1DlRrcMsg));
3054             }
3055          }
3056       }
3057       else if(ueCb->f1UeDb->actionType == UE_CTXT_MOD)
3058       {
3059          ret = duBuildAndSendUeContextModReq(cellId, ueCb->gnbDuUeF1apId, ueCb->crnti, &ueCb->f1UeDb->duUeCfg);
3060          if(ret == RFAILED)
3061          {
3062             DU_LOG("\nERROR  -->  DU APP : Failed to build ue context setup Req in duProcUeContextModReq()");
3063             return RFAILED;
3064          }
3065       }
3066       else if((ueCb->f1UeDb->actionType == UE_CTXT_CFG_QUERY) || (ueCb->f1UeDb->actionType == UE_CTXT_RRC_RECFG_COMPLETE))
3067       {
3068          if((BuildAndSendUeContextModRsp(ueCb) != ROK))
3069          {
3070             DU_LOG("\nERROR  -->  DU APP : Failed to build UE Context modification response");
3071             return RFAILED;
3072          }
3073       }
3074    }
3075    else
3076    {
3077       //TODO: To send the failure cause in UeContextModRsp
3078       
3079       DU_LOG("ERROR  -->  DU APP : Failed to process UE CNTXT MOD REQ at duProcUeContextModReq()");
3080       return RFAILED;
3081    }
3082    return ROK;
3083 }
3084
3085 /*******************************************************************
3086 *
3087 * @brief Function to delete Pdsch ServCellCfg
3088 *
3089 * @details
3090 *
3091 *    Function : deletePdschServCellCfg
3092 *
3093 *    Functionality: Function to delete Pdsch ServCellCfg
3094 *
3095 * @params[in] PdschServCellCfg *pdschServCellCfg
3096 * @return void
3097 *
3098 * ****************************************************************/
3099
3100 void deletePdschServCellCfg(PdschServCellCfg *pdschServCellCfg)
3101 {
3102    DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL,pdschServCellCfg->maxMimoLayers, sizeof(uint8_t));
3103    DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL,pdschServCellCfg->maxCodeBlkGrpPerTb, sizeof(MaxCodeBlkGrpPerTB));
3104    DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL,pdschServCellCfg->codeBlkGrpFlushInd, sizeof(bool));
3105    DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL,pdschServCellCfg->xOverhead, sizeof(PdschXOverhead));
3106 }
3107
3108 /*******************************************************************
3109  *
3110 * @brief delete MacUeCfg from duCb
3111 *
3112 * @details
3113 *
3114 *    Function : deleteMacUeCfg 
3115 *
3116 *    Functionality: delete MacUeCfg from duCb
3117 *
3118 * @params[in] Pointer to MacUeCfg 
3119 * @return ROK     - success
3120 *         RFAILED - failure
3121 *
3122 *******************************************************************/
3123
3124 void deleteMacUeCfg(MacUeCfg *ueCfg)
3125 {
3126    uint8_t lcCfgIdx=0;
3127    
3128    DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL,ueCfg->ambrCfg, sizeof(AmbrCfg));
3129    if(ueCfg->spCellCfgPres)
3130    {
3131       freeUeReCfgCellGrpInfo(ueCfg);
3132    }
3133    for(lcCfgIdx=0; lcCfgIdx< ueCfg->numLcs; lcCfgIdx++)
3134    {
3135       freeMacLcCfg(&ueCfg->lcCfgList[lcCfgIdx]);
3136    }
3137    memset(ueCfg, 0, sizeof(MacUeCfg));
3138 }
3139
3140 /*******************************************************************
3141 *
3142 * @brief delete UE Configuration of a particular UE
3143 *
3144 * @details
3145 *
3146 *    Function : deleteUeCfg 
3147 *
3148 *    Functionality: delete UE Configuration of a particular UE 
3149 *
3150 * @params[in] uint16_t cellIdx, uint8_t ueId
3151 * @return ROK     - success
3152 *         RFAILED - failure
3153 *
3154 * ****************************************************************/
3155 uint8_t  deleteUeCfg(uint16_t cellId, uint8_t ueId)
3156 {
3157    uint8_t tnlIdx = 0;
3158    uint16_t cellIdx = 0;
3159    DuUeCb *ueCb = NULLP;
3160    
3161    GET_CELL_IDX(cellId, cellIdx);
3162    if(duCb.actvCellLst[cellIdx] != NULLP)
3163    {
3164       if((duCb.actvCellLst[cellIdx]->ueCb[ueId-1].macUeCfg.macUeCfgState == UE_DELETE_COMPLETE)\
3165             &&(duCb.actvCellLst[cellIdx]->ueCb[ueId-1].rlcUeCfg.rlcUeCfgState == UE_DELETE_COMPLETE))
3166       {
3167          ueCb = &duCb.actvCellLst[cellIdx]->ueCb[ueId-1];
3168          deleteMacUeCfg(&ueCb->macUeCfg);
3169          deleteRlcUeCfg(&ueCb->rlcUeCfg);
3170          if(ueCb->f1UeDb !=NULLP)
3171          {
3172             freeF1UeDb(ueCb->f1UeDb);
3173          }
3174          for(tnlIdx = 0; tnlIdx < duCb.numTeId; )
3175          {
3176             if(duCb.upTnlCfg[tnlIdx]->ueId == ueId)
3177             {
3178                duCb.upTnlCfg[tnlIdx]->configType = CONFIG_DEL;
3179                duProcEgtpTunnelCfg(tnlIdx, duCb.upTnlCfg[tnlIdx], duCb.upTnlCfg[tnlIdx]);
3180             }
3181             else
3182                tnlIdx++;
3183          }
3184          unsetBitInUeBitMap(cellId, ueId-1);
3185          duCb.actvCellLst[cellIdx]->numActvUes--;
3186          memset(ueCb, 0, sizeof(DuUeCb));
3187       }
3188       else
3189       {
3190          return RFAILED;
3191       }
3192    }
3193    else
3194    {
3195       DU_LOG("\nERROR  --> DU APP : deleteUeCfg(): CellIdx[%d] is not found", cellIdx);
3196       return RFAILED;
3197    }
3198    return ROK;
3199 }
3200
3201
3202 /*******************************************************************
3203 *
3204 * @brief Handle UE delete response from MAC
3205 *
3206 * @details
3207 *
3208 *    Function : DuProcMacUeDeleteRsp
3209 *
3210 *    Functionality: Handle UE delete response from MAC
3211 *
3212 * @params[in] Pointer to MacUeDeleteRsp and Pst
3213 * @return ROK     - success
3214 *         RFAILED - failure
3215 *
3216 * ****************************************************************/
3217
3218 uint8_t DuProcMacUeDeleteRsp(Pst *pst, MacUeDeleteRsp *deleteRsp)
3219 {
3220    uint8_t  ret =ROK,ueId=0;
3221    uint16_t cellIdx=0;
3222    uint32_t gnbCuUeF1apId =0 , gnbDuUeF1apId =0;
3223    
3224    if(deleteRsp)
3225    {
3226       if(deleteRsp->result == SUCCESS)
3227       {
3228          DU_LOG("\nINFO   -->  DU APP : MAC UE Delete Response : SUCCESS [UE IDX : %d]", deleteRsp->ueId);
3229          GET_CELL_IDX(deleteRsp->cellId, cellIdx);
3230          if(duCb.actvCellLst[cellIdx])
3231          {
3232             duCb.actvCellLst[cellIdx]->ueCb[deleteRsp->ueId -1].macUeCfg.macUeCfgState = UE_DELETE_COMPLETE;
3233             ueId = deleteRsp->ueId;
3234             gnbCuUeF1apId = duCb.actvCellLst[cellIdx]->ueCb[ueId-1].gnbCuUeF1apId;
3235             gnbDuUeF1apId = duCb.actvCellLst[cellIdx]->ueCb[ueId-1].gnbDuUeF1apId;
3236             if(deleteUeCfg(deleteRsp->cellId, ueId) == ROK)
3237             {
3238                ret = BuildAndSendUeContextReleaseComplete(deleteRsp->cellId, gnbCuUeF1apId, gnbDuUeF1apId);
3239                if(ret != ROK)
3240                {
3241                   DU_LOG("\nERROR  -->  DU APP : DuProcMacUeDeleteRsp(): failed to send UE context release complete");
3242                }
3243             }
3244
3245          }
3246       }
3247       else
3248       {
3249          DU_LOG("\nERROR  -->  DU APP : DuProcMacUeDeleteRsp(): MAC UE Delete Response : FAILURE [UE IDX : %d]",\
3250          deleteRsp->ueId);
3251          ret =  RFAILED;
3252       }
3253       DU_FREE_SHRABL_BUF(pst->region, pst->pool, deleteRsp, sizeof(MacUeDeleteRsp));
3254    }
3255    else
3256    {
3257       DU_LOG("\nERROR  -->  DU APP : DuProcMacUeDeleteRsp(): MAC UE Delete Response is null");
3258       ret = RFAILED;
3259    }
3260    return ret;
3261 }
3262
3263 /*******************************************************************
3264  *
3265  * @brief Processes UE Delete Rsp received from RLC 
3266  *
3267  * @details
3268  *
3269  *    Function : DuProcRlcUeDeleteRsp
3270  *
3271  *    Functionality:
3272  *     Processes UE Delete Rsp received from RLC 
3273  *
3274  *  @params[in]  Post structure
3275  *               Pointer to RlcUeDeleteRsp
3276  *  @return ROK     - success
3277  *          RFAILED - failure
3278  *
3279  * *****************************************************************/
3280
3281 uint8_t DuProcRlcUeDeleteRsp(Pst *pst, RlcUeDeleteRsp *delRsp)
3282 {
3283    uint8_t  ueId = 0, ret = ROK;
3284    uint16_t cellIdx = 0,crnti=0;
3285
3286    if(delRsp)
3287    {
3288       ueId = delRsp->ueId;
3289       GET_CELL_IDX(delRsp->cellId, cellIdx);
3290
3291       if(delRsp->result == SUCCESSFUL)
3292       {
3293          DU_LOG("\nINFO   -->  DU_APP: RLC UE Delete Response : SUCCESS [UE IDX:%d]", ueId);
3294          if(duCb.actvCellLst[cellIdx]!=NULLP)
3295          {
3296             duCb.actvCellLst[cellIdx]->ueCb[ueId-1].rlcUeCfg.rlcUeCfgState = UE_DELETE_COMPLETE;
3297             GET_CRNTI(crnti, ueId);
3298             if(sendUeDeleteReqToMac(delRsp->cellId, ueId, crnti) == RFAILED)
3299             {
3300                DU_LOG("\nERROR  -->  DU APP : duBuildAndSendUeDeleteReq(): Failed to build UE  delete req for MAC ");
3301                return RFAILED;
3302             }
3303
3304          }
3305       }
3306       else
3307       {
3308          DU_LOG("\nERROR   -->  DU_APP: RLC UE Delete Response : FAILED [UE IDX:%d]", ueId);
3309          ret = RFAILED;
3310       }
3311       DU_FREE_SHRABL_BUF(pst->region, pst->pool, delRsp, sizeof(RlcUeDeleteRsp));
3312
3313    }
3314    return ret;
3315 }
3316
3317 /*******************************************************************
3318  *
3319  * @brief Sending UE Delete Req To Mac
3320 *
3321 * @details
3322 *
3323 *    Function : sendUeDeleteReqToMac
3324 *
3325 *    Functionality:
3326 *     sending UE Delete Req To Mac
3327 *
3328 *  @params[in]    cellId, ueId, crnti 
3329 *  @return ROK     - success
3330 *          RFAILED - failure
3331 *
3332 *****************************************************************/
3333
3334 uint8_t sendUeDeleteReqToMac(uint16_t cellId, uint8_t ueId, uint16_t crnti)
3335 {
3336    Pst pst;
3337    uint8_t ret=ROK;
3338    MacUeDelete *ueDelete = NULLP;
3339
3340    DU_ALLOC_SHRABL_BUF(ueDelete, sizeof(MacUeDelete));
3341    if(ueDelete)
3342    {
3343       ueDelete->cellId = cellId;
3344       ueDelete->ueId   = ueId;
3345       ueDelete->crnti  = crnti;
3346       FILL_PST_DUAPP_TO_MAC(pst, EVENT_MAC_UE_DELETE_REQ);
3347
3348       DU_LOG("\nDEBUG  -->  DU_APP: Sending UE delete Request to MAC ");
3349       ret = (*packMacUeDeleteReqOpts[pst.selector])(&pst, ueDelete);
3350       if(ret == RFAILED)
3351       {
3352          DU_LOG("\nERROR  -->  DU_APP: sendUeDeleteReqToMac(): Failed to send UE delete Req to MAC");
3353          DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, ueDelete, sizeof(MacUeDelete));
3354       }
3355    }
3356    else
3357    {
3358       DU_LOG("\nERROR  -->   DU_APP: sendUeDeleteReqToMac(): Failed to allocate memory"); 
3359       ret = RFAILED;
3360    }
3361    return ret;
3362 }
3363
3364 /*******************************************************************
3365  *
3366  * @brief Sending UE Delete Req To Rlc
3367  *
3368  * @details
3369  *
3370  *    Function : sendUeDeleteReqToRlc
3371  *
3372  *    Functionality:
3373  *     Sending UE Delete Req To Rlc
3374  *
3375  *  @params[in]  cellId, ueId 
3376  *  @return ROK     - success
3377  *          RFAILED - failure
3378  *
3379  *****************************************************************/
3380
3381 uint8_t sendUeDeleteReqToRlc(uint16_t cellId, uint8_t ueId)
3382 {
3383    uint8_t ret;
3384    Pst pst;
3385    RlcUeDelete *ueDelete;
3386
3387    DU_ALLOC_SHRABL_BUF(ueDelete, sizeof(RlcUeDelete));
3388    if(ueDelete !=NULLP)
3389    {
3390       ueDelete->cellId = cellId;
3391       ueDelete->ueId = ueId;
3392       FILL_PST_DUAPP_TO_RLC(pst, RLC_UL_INST, EVENT_RLC_UE_DELETE_REQ);
3393
3394       ret = (*packRlcUeDeleteReqOpts[pst.selector])(&pst, ueDelete);
3395       if(ret == RFAILED)
3396       {
3397          DU_LOG("\nERROR  -->  DU_APP : sendUeDeleteReqToRlc():Failed to send UE Delete  Req to RLC"); 
3398          DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, ueDelete, sizeof(RlcUeDelete));
3399       }
3400    }
3401    else
3402    {
3403       DU_LOG("\nERROR  -->   DU_APP: sendUeDeleteReqToRlc():Memory allocation failed");
3404       ret = RFAILED;
3405    }
3406    return ret;
3407 }
3408
3409 /*******************************************************************
3410  *
3411  * @brief DU processes UE delete req from CU and sends to MAC and RLC 
3412  *
3413  * @details
3414  *
3415  *    Function : duBuildAndSendUeDeleteReq
3416  *
3417  *    Functionality: DU processes UE delete req from CU and sends to MAC 
3418  *                   and RLC 
3419  *
3420  * @params[in] cellId, crnti 
3421  * @return ROK     - success
3422  *         RFAILED - failure
3423  *
3424  * ****************************************************************/
3425
3426 uint8_t duBuildAndSendUeDeleteReq(uint16_t cellId, uint16_t crnti)
3427 {
3428    uint8_t  ueId =0;
3429    uint16_t cellIdx = 0;
3430
3431    DU_LOG("\nDEBUG  -->  DU_APP: Processing UE Delete Request ");
3432    GET_CELL_IDX(cellId, cellIdx);
3433    GET_UE_ID(crnti, ueId);
3434
3435    if(duCb.actvCellLst[cellIdx] != NULLP)
3436    {
3437       if(crnti != duCb.actvCellLst[cellIdx]->ueCb[ueId - 1].crnti)
3438       {
3439          DU_LOG("\nERROR  -->  DU APP : duBuildAndSendUeDeleteReq(): CRNTI [%d] not found", crnti);
3440          return RFAILED;
3441       }
3442
3443       duCb.actvCellLst[cellIdx]->ueCb[ueId - 1].ueState = UE_DELETION_IN_PROGRESS; 
3444       if(sendUeDeleteReqToRlc(cellId, ueId) == RFAILED)
3445       {
3446          DU_LOG("\nERROR  -->  DU APP : DuProcMacUeDeleteRsp():Failed to build UE  delete req for RLC ");
3447          return RFAILED;
3448       }
3449    }
3450    else
3451    {
3452       DU_LOG("\nERROR  -->  DU APP : duBuildAndSendUeDeleteReq(): Cell Id is not found");
3453       return RFAILED;
3454    }
3455
3456    return ROK;
3457 }
3458
3459 /*******************************************************************
3460  *
3461  * @brief delete RlcUeCfg from duCb
3462  *
3463  * @details
3464  *
3465  *    Function : deleteRlcUeCfg
3466  *
3467  *    Functionality:
3468  *       delete  RlcUeCfg from duCb
3469  *
3470  *  @params[in] RlcUeCfg *ueCfg 
3471  *               
3472  *  @return ROK     - success
3473  *          RFAILED - failure
3474  *
3475  *****************************************************************/
3476
3477 void deleteRlcUeCfg(RlcUeCfg *ueCfg)
3478 {
3479    uint8_t lcIdx = 0;
3480    RlcBearerCfg *lcCfg= NULLP;
3481    if(ueCfg)
3482    {
3483       for(lcIdx =0 ; lcIdx < ueCfg->numLcs ; lcIdx++)
3484       {
3485          lcCfg = &ueCfg->rlcLcCfg[lcIdx];
3486          switch(lcCfg->rlcMode)
3487          {
3488             case RLC_AM :
3489                {
3490                   DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, lcCfg->u.amCfg, sizeof(AmBearerCfg));
3491                   break;
3492                }
3493             case RLC_UM_BI_DIRECTIONAL :
3494                {
3495                   DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, lcCfg->u.umBiDirCfg, sizeof(UmBiDirBearerCfg));
3496                   break;
3497                }
3498             case RLC_UM_UNI_DIRECTIONAL_UL :
3499                {
3500                   DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, lcCfg->u.umUniDirUlCfg, sizeof(UmUniDirUlBearerCfg));
3501                   break;
3502                }
3503             case RLC_UM_UNI_DIRECTIONAL_DL :
3504                {
3505                   DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, lcCfg->u.umUniDirDlCfg, sizeof(UmUniDirDlBearerCfg));
3506                   break;
3507                }
3508          }
3509          DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, lcCfg->snssai, sizeof(Snssai));
3510       }
3511       memset(ueCfg, 0, sizeof(RlcUeCfg));
3512    }
3513 }
3514
3515 /*******************************************************************
3516 *
3517 * @brief Du process Ue Context Release Command
3518 *
3519 * @details
3520 *
3521 *    Function : duProcUeContextReleaseCommand 
3522 *
3523 *    Functionality: Du process Ue Context Release Command 
3524 *
3525 * @params[in] DuUeCb *duUeCb 
3526 * @return ROK     - success
3527 *         RFAILED - failure
3528 *
3529 * ****************************************************************/
3530 uint8_t duProcUeContextReleaseCommand(uint16_t cellId, DuUeCb *duUeCb)
3531 {
3532    uint8_t ret =ROK, ueId=0;
3533    uint16_t crnti = 0;
3534
3535    if(duUeCb != NULLP)
3536    {
3537       crnti = duUeCb->crnti;
3538       GET_UE_ID(crnti, ueId);
3539       
3540       if(duUeCb->f1UeDb)
3541       {
3542          /* Send DL RRC msg for RRC release */
3543          if(duUeCb->f1UeDb->dlRrcMsg)
3544          {
3545             if(duUeCb->f1UeDb->dlRrcMsg->rrcMsgPdu != NULLP)
3546             {
3547                ret = duBuildAndSendDlRrcMsgToRlc(cellId, duCb.actvCellLst[duUeCb->f1UeDb->cellIdx]->ueCb[ueId-1].rlcUeCfg,\
3548                      duUeCb->f1UeDb->dlRrcMsg);
3549                if(ret == RFAILED)
3550                {
3551                   DU_LOG("\nERROR  -->  DU APP : duProcUeContextReleaseCommand() : Failed to send DL RRC msg");
3552                   DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, duUeCb->f1UeDb->dlRrcMsg->rrcMsgPdu,\
3553                         duUeCb->f1UeDb->dlRrcMsg->rrcMsgSize);
3554                   DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, duUeCb->f1UeDb->dlRrcMsg, sizeof(F1DlRrcMsg));
3555                }
3556             }
3557          }
3558       }
3559       else
3560       {
3561          ret = duBuildAndSendUeDeleteReq(cellId,crnti);
3562          if(ret == RFAILED)
3563          {
3564             DU_LOG("\nERROR  -->  DU APP : duProcUeContextReleaseCommand(): Failed to build and send Ue Delete request");
3565          }
3566       }
3567    }
3568    return ret;
3569 }
3570
3571 /**********************************************************************
3572   End of file
3573 ***********************************************************************/