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