[ Jira id - ODUHIGH-593 ] Pack and unpack function nomenclature correction
[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 "du_tmr.h"
22 #include "lrg.x"
23 #include "ckw.h"
24 #include "ckw.x"
25 #include "kwu.h"
26 #include "kwu.x"
27 #include "lkw.h"
28 #include "lkw.x"
29 #include "legtp.h"
30 #include "du_app_mac_inf.h"
31 #include "du_app_rlc_inf.h"
32 #include "du_e2ap_mgr.h"
33 #include "du_cfg.h"
34 #include "du_mgr.h"
35 #include "du_utils.h"
36 #include "du_f1ap_msg_hdl.h"
37 #include "du_ue_mgr.h"
38
39 #ifdef O1_ENABLE
40 #include "AlarmInterface.h"
41 #include "CmInterface.h"
42 #endif
43
44 extern DuCb duCb;
45 DuRlcUeReestablishReq packRlcUeReestablishReqOpts[] =
46 {
47    packDuRlcUeReestablishReq,       /* Loose coupling */
48    RlcProcUeReestablishReq,         /* TIght coupling */
49    packDuRlcUeReestablishReq        /* Light weight-loose coupling */
50 };
51
52 DuMacDlCcchInd packMacDlCcchIndOpts[] =
53 {
54    packMacDlCcchInd,           /* Loose coupling */
55    MacProcDlCcchInd,           /* TIght coupling */
56    packMacDlCcchInd            /* Light weight-loose coupling */
57 };
58
59 DuMacUeCreateReq packMacUeCreateReqOpts[] =
60 {
61    packDuMacUeCreateReq,       /* Loose coupling */
62    MacProcUeCreateReq,         /* TIght coupling */
63    packDuMacUeCreateReq        /* Light weight-loose coupling */
64 };
65
66 DuRlcUeCreateReq packRlcUeCreateReqOpts[] =
67 {
68    packDuRlcUeCreateReq,       /* Loose coupling */
69    RlcProcUeCreateReq,         /* TIght coupling */
70    packDuRlcUeCreateReq        /* Light weight-loose coupling */
71 };
72
73 DuDlRrcMsgToRlcFunc duSendDlRrcMsgToRlcOpts[] =
74 {
75    packDlRrcMsgToRlc,          /* Loose coupling */ 
76    RlcProcDlRrcMsgTransfer,    /* Tight coupling */
77    packDlRrcMsgToRlc           /* Light weight-loose coupling */
78 };
79
80 DuRlcUeReconfigReq packRlcUeReconfigReqOpts[] =
81 {
82    packDuRlcUeReconfigReq,     /* Loose coupling */
83    RlcProcUeReconfigReq,       /* TIght coupling */
84    packDuRlcUeReconfigReq      /* Light weight-loose coupling */
85 };
86
87 DuMacUeReconfigReq packMacUeReconfigReqOpts[] =
88 {
89    packDuMacUeReconfigReq,     /* Loose coupling */
90    MacProcUeReconfigReq,       /* TIght coupling */
91    packDuMacUeReconfigReq      /* Light weight-loose coupling */
92 };
93
94 DuMacRachRsrcReq packMacRachRsrcReqOpts[] = 
95 {
96    packDuMacRachRsrcReq,      /* Loose coupling */
97    MacProcRachRsrcReq,        /* Tight coupling */
98    packDuMacRachRsrcReq       /* Light weight-loose coupling */
99 };
100
101 DuRlcDlUserDataToRlcFunc duSendRlcDlUserDataToRlcOpts[] =
102 {
103    packRlcDlUserDataToRlc,     /* Loose coupling */ 
104    RlcProcDlUserDataTransfer,  /* Tight coupling */
105    packRlcDlUserDataToRlc      /* Light weight-loose coupling */
106 };
107
108 DuMacRachRsrcRel packMacRachRsrcRelOpts[] = 
109 {
110    packDuMacRachRsrcRel,      /* Loose coupling */
111    MacProcRachRsrcRel,        /* Tight coupling */
112    packDuMacRachRsrcRel       /* Light weight-loose coupling */
113 };
114
115 DuMacUeDeleteReq packMacUeDeleteReqOpts[] =
116 {
117    packDuMacUeDeleteReq,       /* Loose coupling */
118    MacProcUeDeleteReq,         /* TIght coupling */
119    packDuMacUeDeleteReq        /* Light weight-loose coupling */
120 };
121
122 DuRlcUeDeleteReq packRlcUeDeleteReqOpts[] =
123 {
124    packDuRlcUeDeleteReq,       /* Loose coupling */
125    RlcProcUeDeleteReq,         /* TIght coupling */
126    packDuRlcUeDeleteReq        /* Light weight-loose coupling */
127 };
128
129 DuMacUeResetReq packMacUeResetReqOpts[] =
130 {
131    packDuMacUeResetReq,       /* Loose coupling */
132    MacProcUeResetReq,         /* TIght coupling */
133    packDuMacUeResetReq        /* Light weight-loose coupling */
134 };
135
136 /*******************************************************************
137  *
138  * @brief Processes UE's max retransmission information received from RLC 
139  *
140  * @details
141  *
142  *    Function : DuProcRlcMaxRetransInd
143  *
144  *    Functionality:
145  *     Processes max retransmission reached information received from RLC 
146  *
147  *  @params[in]  Post structure
148  *               Pointer to RlcMaxRetransInfo
149  *  @return ROK     - success
150  *          RFAILED - failure
151  *
152  * *****************************************************************/
153
154 uint8_t DuProcRlcMaxRetransInd(Pst *pst, RlcMaxRetransInfo *maxRetransInfo)
155 {
156    uint8_t  ueId = 0, ret = RFAILED;
157    uint16_t cellIdx = 0,crnti=0;
158
159    if(maxRetransInfo)
160    {
161       GET_CELL_IDX(maxRetransInfo->cellId, cellIdx);
162
163       if(duCb.actvCellLst[cellIdx]!=NULLP)
164       {
165          ueId = maxRetransInfo->ueId;
166          GET_CRNTI(crnti, ueId);
167          if(duCb.actvCellLst[cellIdx]->ueCb[ueId-1].crnti ==  crnti)
168          {
169             /*TODO: complete the processing of max retransmission */
170             ret = ROK;
171          }
172          else
173             DU_LOG("\nERROR  -->  DU APP : DuProcRlcMaxRetransInd(): CRNTI [%d] not found", crnti);
174       }
175       else
176          DU_LOG("\nERROR  -->  DU APP : DuProcRlcMaxRetransInd(): Cell Id[%d] is not found", maxRetransInfo->cellId);
177       
178       DU_FREE_SHRABL_BUF(pst->region, pst->pool, maxRetransInfo, sizeof(RlcMaxRetransInfo));
179
180    }
181    return ret;
182 }
183
184 /******************************************************************
185  *
186  * @brief Function to return Drb LcId
187  *
188  * @details
189  *
190  *    Function : getDrbLcId
191  *
192  *    Functionality: Function to return Drb LcId
193  *
194  * @params[in] drbBitMap
195  *
196  * Returns: lcId - SUCCESS
197  *         RFAILED - FAILURE
198  *****************************************************************/
199
200 uint8_t getDrbLcId(uint32_t *drbBitMap)
201 {
202    uint8_t bitMask = 1, bitPos = 0;
203    bitPos = MIN_DRB_LCID;
204
205    while(bitPos <= MAX_DRB_LCID)
206    {
207       if((*drbBitMap & (bitMask << bitPos)) == 0)
208       {
209          *drbBitMap = ((bitMask << bitPos)| *drbBitMap);
210          return bitPos;
211       }
212       else
213       {
214          bitPos++;
215       }
216    }
217    DU_LOG("\nERROR   -->  DU_APP: Max LC Reached in getDrbLcId()");
218    return RFAILED;
219 }
220
221 /*******************************************************************
222  *
223  * @brief Function to fillDlUserDataInfo
224  *
225  * @details
226  *
227  *    Function : fillDlUserDataInfo
228  *
229  *    Functionality:
230  *      Function to fillDlUserDataInfo
231  *
232  * @params[in] teId,
233  *             dlDataMsgInfo
234  *
235  * @return ROK     - success
236  *         RFAILED - failure
237  *
238  * ****************************************************************/
239
240 uint8_t fillDlUserDataInfo(uint32_t teId, RlcDlUserDataInfo *dlDataMsgInfo)
241 {
242    uint8_t teIdx = 0;
243
244    /*Traversing the duCb to find the CellId, UeId, LCID corresponding to TEID*/
245    for(teIdx = 0; teIdx < duCb.numTeId; teIdx++)
246    {
247       if(duCb.upTnlCfg[teIdx] && (duCb.upTnlCfg[teIdx]->tnlCfg1 != NULLP))
248       {
249          if(duCb.upTnlCfg[teIdx]->tnlCfg1->teId == teId)
250          {
251             dlDataMsgInfo->cellId = duCb.upTnlCfg[teIdx]->cellId;
252             dlDataMsgInfo->ueId = duCb.upTnlCfg[teIdx]->ueId;
253             dlDataMsgInfo->rbId = duCb.upTnlCfg[teIdx]->drbId;
254             return ROK;
255          }
256       }
257    }
258    return RFAILED;
259 }
260
261  /*******************************************************************
262  *
263  * @brief Build and Send DL Data Message transfer to RLC
264  *
265  * @details
266  *
267  *    Function : duBuildAndSendDlUserDataToRlc
268  *
269  *    Functionality:
270  *      Build and Send DL Data Message transfer to RLC
271  *
272  * @params[in] Cell ID
273  *             UE Index
274  *             Logical Channgel ID
275  *             RRC Message
276  *             RRC Message Length
277  * @return ROK     - success
278  *         RFAILED - failure
279  *
280  * ****************************************************************/
281
282 uint8_t duBuildAndSendDlUserDataToRlc(uint16_t msgLen, EgtpMsg *egtpMsg)
283 {
284    uint8_t ret = RFAILED;
285    Pst     pst;
286    RlcDlUserDataInfo *dlDataMsgInfo = NULLP;
287
288    DU_ALLOC_SHRABL_BUF(dlDataMsgInfo, sizeof(RlcDlUserDataInfo));
289    if(!dlDataMsgInfo)
290    {
291       DU_LOG("\nERROR  -->  DU_APP : Memory allocation failed for dlDataMsgInfo in duHdlEgtpDlData()");
292       return RFAILED;
293    }
294    memset(dlDataMsgInfo, 0, sizeof(RlcDlUserDataInfo));
295    dlDataMsgInfo->dlMsg = egtpMsg->msg;
296    dlDataMsgInfo->msgLen = msgLen;
297
298    /* Filling DL DATA Msg Info */
299    if(fillDlUserDataInfo(egtpMsg->msgHdr.teId, dlDataMsgInfo) == ROK)
300    {
301       /* Filling post structure and sending msg */ 
302       FILL_PST_DUAPP_TO_RLC(pst, RLC_DL_INST, EVENT_DL_USER_DATA_TRANS_TO_RLC);
303       DU_LOG("\nDEBUG  -->  DU_APP : Sending User Data Msg to RLC [TEID, nPDU]:[%d, %d]\n",\
304                        egtpMsg->msgHdr.teId, egtpMsg->msgHdr.nPdu.val);
305       ret = (*duSendRlcDlUserDataToRlcOpts[pst.selector])(&pst, dlDataMsgInfo);
306    }
307    if(ret != ROK)
308    {
309       DU_LOG("\nERROR  -->  DU_APP : Failed to send User Data to RLC in duHdlEgtpDlData()");
310       ODU_PUT_MSG_BUF(dlDataMsgInfo->dlMsg);
311       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, dlDataMsgInfo, sizeof(RlcDlUserDataInfo));
312    }
313    return ret;
314 }
315
316 /*******************************************************************
317  *
318  * @brief Handles EGTP data from CU 
319  *
320  * @details
321  *
322  *    Function : duHdlEgtpData
323  *
324  *    Functionality: 
325  *      Processes EGTP header and sends data to RLC
326  *
327  * @params[in]  Pointer to EGTP Message 
328  * @return ROK     - success
329  *         RFAILED - failure
330  *
331  * ****************************************************************/
332 uint8_t duHdlEgtpDlData(EgtpMsg  *egtpMsg)
333 {
334    uint16_t msgLen = 0;
335
336 #ifdef CALL_FLOW_DEBUG_LOG
337     DU_LOG("\nCall Flow: ENTEGTP -> ENTDUAPP : EVENT_HDL_RECV_DL_DATA\n");
338 #endif
339
340    DU_LOG("\nDEBUG  -->  DU_APP : Processing DL data in duHdlEgtpDlData()");
341    
342    if(!egtpMsg->msg)
343    {
344       DU_LOG("\nERROR  -->  DU_APP : Recevied Dl Data is NULLP in duHdlEgtpDlData()");
345       return RFAILED;
346    }
347    ODU_GET_MSG_LEN(egtpMsg->msg, (MsgLen *)&msgLen);
348    if(duBuildAndSendDlUserDataToRlc(msgLen, egtpMsg) != ROK)
349    {
350       DU_LOG("\nERROR  -->  DU_APP : Failed to build DL USer Data in duHdlEgtpDlData()");
351       return RFAILED;
352    }
353    return ROK;
354 }
355
356 /******************************************************************
357  *
358  * @brief Builds and Sends DL CCCH Ind to MAC
359  *
360  * @details
361  *
362  *    Function : duBuildAndSendDlCcchInd 
363  *
364  *    Functionality: Builds and sends DL CCCH Ind Msg to MAC
365  *
366  * @params[in] dlCcchMsg - uint8_t*
367  * @return ROK     - success
368  *         RFAILED - failure
369  *
370  * ****************************************************************/
371 uint8_t duBuildAndSendDlCcchInd(uint16_t *cellId, uint16_t *crnti, \
372       DlCcchMsgType msgType, uint16_t dlCcchMsgSize, uint8_t *dlCcchMsg)
373 {
374    uint8_t  ret                  = ROK;
375    uint16_t idx2;
376    DlCcchIndInfo *dlCcchIndInfo = NULLP;
377    Pst pst;
378
379    DU_LOG("\nDEBUG   -->  DU APP : Building and Sending DL CCCH Ind to MAC");
380
381    DU_ALLOC_SHRABL_BUF(dlCcchIndInfo, sizeof(DlCcchIndInfo));
382
383    if(!dlCcchIndInfo)
384    {
385       DU_LOG("\nERROR  -->  DU APP : Memory alloc failed while building DL CCCH Ind");
386       return RFAILED;
387    }
388
389    dlCcchIndInfo->cellId = *cellId;
390    dlCcchIndInfo->crnti = *crnti;
391    dlCcchIndInfo->msgType = msgType;
392    dlCcchIndInfo->dlCcchMsgLen = dlCcchMsgSize;
393
394    DU_ALLOC_SHRABL_BUF(dlCcchIndInfo->dlCcchMsg, dlCcchIndInfo->dlCcchMsgLen);
395    if(!dlCcchIndInfo->dlCcchMsg)
396    {
397       DU_LOG("\nERROR  -->  DU APP : Memory alloc failed while building DL CCCH Ind");
398       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, dlCcchIndInfo, sizeof(DlCcchIndInfo));
399       return RFAILED;
400    }
401    for(idx2 = 0; idx2 < dlCcchIndInfo->dlCcchMsgLen; idx2++)
402    {
403       dlCcchIndInfo->dlCcchMsg[idx2] = dlCcchMsg[idx2];
404    }
405    DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, dlCcchMsg, dlCcchMsgSize);
406
407    /* Fill Pst */
408    FILL_PST_DUAPP_TO_MAC(pst, EVENT_MAC_DL_CCCH_IND);
409    ret = (*packMacDlCcchIndOpts[pst.selector])(&pst, dlCcchIndInfo);
410    if(ret != ROK)
411    {
412       DU_LOG("\nERROR  -->  DU_APP : Failure in sending DL CCCH to MAC");
413       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, dlCcchIndInfo->dlCcchMsg,\
414             dlCcchIndInfo->dlCcchMsgLen);
415       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, dlCcchIndInfo, \
416             sizeof(DlCcchIndInfo));
417       ret = RFAILED; 
418    }
419
420    return ret;
421
422 }
423
424 /*******************************************************************
425  *
426  * @brief Build and Send DL RRC Message transfer to RLC
427  *
428  * @details
429  *
430  *    Function : duBuildAndSendDlRrcMsgToRlc
431  *
432  *    Functionality:
433  *      Build and Send DL RRC Message transfer to RLC
434  *
435  * @params[in] Cell ID
436  *             UE Index
437  *             Logical Channgel ID
438  *             RRC Message
439  *             RRC Message Length
440  * @return ROK     - success
441  *         RFAILED - failure
442  *
443  * ****************************************************************/
444 uint8_t duBuildAndSendDlRrcMsgToRlc(uint16_t cellId, DuRlcUeCfg ueCfg, F1DlRrcMsg *f1DlRrcMsg)
445 {
446    Pst     pst;
447    uint8_t ret;
448    uint8_t lcIdx;
449    RlcDlRrcMsgInfo  *dlRrcMsgInfo = NULLP;
450
451    if(!f1DlRrcMsg)
452    {
453       DU_LOG("\nERROR  -->  DU APP : Received Dl RRC Msg is NULL at duBuildAndSendDlRrcMsgToRlc()");
454       return RFAILED;
455    }
456
457    /*As per Spec ORAN WG8 AAD, lcId for DL RRC range from 1...3*/
458    if((f1DlRrcMsg->srbId < SRB1_LCID) || (f1DlRrcMsg->srbId > SRB3_LCID))
459    {
460       DU_LOG("\nERROR  -->  DU APP : Received SRBID for this Dl RRC Msg is not valid");
461       return RFAILED;
462    }
463
464    DU_ALLOC_SHRABL_BUF(dlRrcMsgInfo, sizeof(RlcDlRrcMsgInfo));
465   
466    if(!dlRrcMsgInfo)
467    {
468       DU_LOG("\nERROR  -->  DU APP : Memory allocation failed for dlRrcMsgInfo in \
469             duBuildAndSendDlRrcMsgToRlc");
470       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, f1DlRrcMsg->rrcMsgPdu, f1DlRrcMsg->rrcMsgSize);
471       return RFAILED;
472    }
473
474    /* Filling up the RRC msg info */
475    dlRrcMsgInfo->cellId = cellId;
476    dlRrcMsgInfo->ueId = ueCfg.ueId;
477    for(lcIdx = 0; lcIdx <= MAX_NUM_LC; lcIdx++)
478    {
479       if(ueCfg.rlcLcCfg[lcIdx].rlcBearerCfg.lcId == f1DlRrcMsg->srbId)
480       {
481          dlRrcMsgInfo->lcId   = f1DlRrcMsg->srbId;
482          break;
483       }
484    }
485    if(lcIdx == (MAX_NUM_LC + 1))
486    {
487       DU_LOG("\nERROR  -->  DU APP : (duBuildAndSendDlRrcMsgToRlc) SRB for this DL_RRC msg is not configured.");
488       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, f1DlRrcMsg->rrcMsgPdu, f1DlRrcMsg->rrcMsgSize);
489       return RFAILED;
490    }
491
492    dlRrcMsgInfo->execDup = f1DlRrcMsg->execDup;
493    dlRrcMsgInfo->deliveryStaRpt = f1DlRrcMsg->deliveryStatRpt;
494    dlRrcMsgInfo->msgLen = f1DlRrcMsg->rrcMsgSize;
495    dlRrcMsgInfo->rrcMsg = f1DlRrcMsg->rrcMsgPdu;
496
497    /* Filling post structure and sending msg */ 
498    FILL_PST_DUAPP_TO_RLC(pst, RLC_DL_INST, EVENT_DL_RRC_MSG_TRANS_TO_RLC);
499    DU_LOG("\nDEBUG  -->  DU_APP: Sending Dl RRC Msg to RLC \n");
500    ret = (*duSendDlRrcMsgToRlcOpts[pst.selector])(&pst, dlRrcMsgInfo);
501    if(ret != ROK)
502    {
503       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, f1DlRrcMsg->rrcMsgPdu, f1DlRrcMsg->rrcMsgSize);
504       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, dlRrcMsgInfo, sizeof(RlcDlRrcMsgInfo));
505       return RFAILED;
506    }
507
508    return ROK;
509
510
511
512
513 /******************************************************************
514  *
515  * @brief Process DL RRC Msg recevied from F1AP
516  *
517  * @details
518  *
519  *    Function : duProcDlRrcMsg
520  *
521  *    Functionality: Process DL RRC Msg recevied from F1AP
522  *
523  * @params[in] dlCcchMsg - uint8_t*
524  * @return ROK     - success
525  *         RFAILED - failure
526  *
527  * ****************************************************************/
528
529 uint8_t duProcDlRrcMsg(F1DlRrcMsg *dlRrcMsg)
530 {
531    uint8_t ueIdx, ret;
532    uint16_t crnti, cellId, cellIdx;
533    bool ueCcchCtxtFound = false;
534    bool ueFound = false;
535
536    ret = ROK;
537
538    if(dlRrcMsg->srbId == SRB0_LCID) //RRC connection setup
539    {
540       for(ueIdx=0; ueIdx<duCb.numUe; ueIdx++)
541       {
542          if(dlRrcMsg->gnbDuUeF1apId == duCb.ueCcchCtxt[ueIdx].gnbDuUeF1apId)
543          {
544             ueCcchCtxtFound = true;
545             crnti  = duCb.ueCcchCtxt[ueIdx].crnti;
546             cellId = duCb.ueCcchCtxt[ueIdx].cellId;
547             break;
548          }
549       }
550    }
551    if(ueCcchCtxtFound)
552    {
553       ret = duBuildAndSendDlCcchInd(&cellId, &crnti, RRC_SETUP, dlRrcMsg->rrcMsgSize, dlRrcMsg->rrcMsgPdu);
554       if(ret == RFAILED)
555       {
556          DU_LOG("\nERROR  -->  DU APP : Failed to build DlCcch Ind at procDlRrcMsgTrans()");
557       }
558       else
559       {
560          if(duCb.actvCellLst[cellId-1] == NULLP)
561          {
562             DU_LOG("\nERROR  -->  DU APP : cellId [%d] does not exist", cellId);
563             ret = RFAILED;
564          }
565          if(duCb.actvCellLst[cellId-1]->numActvUes < MAX_NUM_UE)
566          {
567             ret = duCreateUeCb(&duCb.ueCcchCtxt[ueIdx], dlRrcMsg->gnbCuUeF1apId);
568             if(ret == RFAILED)
569             {
570                DU_LOG("\nERROR  -->  DU APP : Failed to createUeCb for cellId [%d] at procDlRrcMsgTrans()", \
571                      duCb.ueCcchCtxt[ueIdx].cellId);
572             }
573          }
574          else
575          {
576             DU_LOG("\nERROR   -->  DU_APP: Max Active UEs has reached at procDlRrcMsgTrans()");
577             ret = RFAILED;
578          }
579       }
580    }
581    else
582    {
583       for(cellIdx = 0; cellIdx < MAX_NUM_CELL; cellIdx++)
584       {
585          for(ueIdx = 0 ; ueIdx < MAX_NUM_UE; ueIdx++)
586          {
587             if(duCb.actvCellLst[cellIdx] && (dlRrcMsg->gnbCuUeF1apId == duCb.actvCellLst[cellIdx]->ueCb[ueIdx].gnbCuUeF1apId)
588                   && (dlRrcMsg->gnbDuUeF1apId == duCb.actvCellLst[cellIdx]->ueCb[ueIdx].gnbDuUeF1apId))
589             {
590                ueFound = true;
591                ret = duBuildAndSendDlRrcMsgToRlc(duCb.actvCellLst[cellIdx]->cellId, \
592                      duCb.actvCellLst[cellIdx]->ueCb[ueIdx].duRlcUeCfg, dlRrcMsg);
593                if(ret == RFAILED)
594                {
595                   DU_LOG("\nERROR   -->  DU_APP: duBuildAndSendDlRrcMsgToRlc() Failed for UE ID:%d", dlRrcMsg->gnbDuUeF1apId);
596                   return RFAILED;
597                }
598                break; 
599             }
600          }
601          if(ueFound)
602             break;
603       }
604       if(!ueFound)
605       {
606          DU_LOG("\nERROR   -->  DU_APP: UE ID [%d] not found", dlRrcMsg->gnbDuUeF1apId);
607          ret = RFAILED;
608       }
609    }
610    return ret;
611 }
612
613 /******************************************************************
614  *
615  * @brief Processes UL CCCH Ind recvd from MAC
616  *
617  * @details
618  *
619  *    Function : duProcUlCcchInd
620  *
621  *    Functionality: Processes UL CCCH Ind recvd from MAC
622  *
623  * @params[in] UlCcchIndInfo *ulCcchIndInfo
624  * @return ROK     - success
625  *         RFAILED - failure
626  *
627  * ****************************************************************/
628 uint8_t duProcUlCcchInd(UlCcchIndInfo *ulCcchIndInfo)
629 {
630
631    uint8_t ret = ROK;
632    int32_t gnbDuUeF1apId = 0;
633
634    if(ulCcchIndInfo->crnti)
635    {
636       GET_UE_ID(ulCcchIndInfo->crnti, gnbDuUeF1apId);
637    }
638    else
639    {
640       DU_LOG("\nERROR  -->  DU_APP : Received invalid CRNTI [%d] ", ulCcchIndInfo->crnti);
641       return RFAILED;
642    }
643    
644    /* Store Ue mapping */
645    duCb.ueCcchCtxt[duCb.numUe].gnbDuUeF1apId = (uint32_t)gnbDuUeF1apId;
646    duCb.ueCcchCtxt[duCb.numUe].crnti         = ulCcchIndInfo->crnti;
647    duCb.ueCcchCtxt[duCb.numUe].cellId        = ulCcchIndInfo->cellId;
648
649    duCb.numUe++;
650
651    ret = (BuildAndSendInitialRrcMsgTransfer(gnbDuUeF1apId, ulCcchIndInfo->crnti, ulCcchIndInfo->ulCcchMsgLen,
652             ulCcchIndInfo->ulCcchMsg));
653    if(ret != ROK)
654    {
655       DU_LOG("\nERROR  -->  DU_APP : BuildAndSendInitialRrcMsgTransfer failed");
656    }
657
658    DU_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, ulCcchIndInfo->ulCcchMsg, ulCcchIndInfo->ulCcchMsgLen);
659    DU_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, ulCcchIndInfo, sizeof(UlCcchIndInfo));
660
661    return ret;
662
663 }
664
665 /******************************************************************
666  *
667  * @brief Fills Default UL LC Cfg
668  *
669  * @details
670  *
671  *    Function : fillDefaultUlLcCfg
672  *
673  *    Functionality: Fills Default UL LC Cfg
674  *
675  * @params[in]  UlLcCfg *ulLcCfg 
676  * @return void
677  *****************************************************************/
678 void fillDefaultUlLcCfg(UlLcCfg *ulLcCfg)
679 {
680    ulLcCfg->priority = LC_PRIORITY_1;
681    ulLcCfg->lcGroup =  0;
682    ulLcCfg->schReqId = 0;
683    ulLcCfg->pbr = PBR_KBPS_INFINITY;
684    ulLcCfg->bsd = BSD_MS_1000;
685 }
686
687 /******************************************************************
688  *
689  * @brief Fills Initial DL Bandwidth Part
690  *
691  * @details
692  *
693  *    Function : fillDefaultInitDlBwp
694  *
695  *    Functionality: Fills Initial DL Bandwidth Part
696  *
697  * @params[in]  InitialDlBwp *initDlBwp
698  * @return ROK - success
699  *         RFAILED - failure
700  *
701  *****************************************************************/
702 uint8_t fillDefaultInitDlBwp(InitialDlBwp *initDlBwp)
703 {
704    uint8_t idx = 0;
705    uint8_t freqDomainResource[FREQ_DOM_RSRC_SIZE] = {0};
706    uint8_t coreset0EndPrb, coreset1StartPrb, coreset1NumPrb;
707
708    if(initDlBwp)
709    {
710       /* Filling PDCCH Config */
711       initDlBwp->pdcchPresent = TRUE;
712       if(initDlBwp->pdcchPresent)
713       {
714          initDlBwp->pdcchCfg.numCRsetToAddMod = PDCCH_CTRL_RSRC_SET_ONE_ID;
715          memset(initDlBwp->pdcchCfg.cRSetToAddModList, 0, MAX_NUM_CRSET);
716          if(initDlBwp->pdcchCfg.numCRsetToAddMod <= MAX_NUM_CRSET)
717          {
718             initDlBwp->pdcchCfg.cRSetToAddModList[idx].cRSetId = PDCCH_CTRL_RSRC_SET_ONE_ID;
719             memset(initDlBwp->pdcchCfg.cRSetToAddModList[idx].freqDomainRsrc, 0, FREQ_DOM_RSRC_SIZE); 
720             coreset0EndPrb = CORESET0_END_PRB;
721             coreset1StartPrb = coreset0EndPrb +6;
722             coreset1NumPrb = CORESET1_NUM_PRB;
723             /* calculate the PRBs */
724             fillCoresetFeqDomAllocMap(((coreset1StartPrb)/6), (coreset1NumPrb/6), freqDomainResource);
725             memcpy(initDlBwp->pdcchCfg.cRSetToAddModList[idx].freqDomainRsrc, freqDomainResource, FREQ_DOM_RSRC_SIZE);
726
727             initDlBwp->pdcchCfg.cRSetToAddModList[idx].duration = PDCCH_CTRL_RSRC_SET_ONE_DURATION;
728             initDlBwp->pdcchCfg.cRSetToAddModList[idx].cceRegMappingType = CCE_REG_MAPPINGTYPE_PR_NONINTERLEAVED;
729             initDlBwp->pdcchCfg.cRSetToAddModList[idx].precoderGranularity = ALL_CONTIGUOUS_RBS;
730             initDlBwp->pdcchCfg.cRSetToAddModList[idx].dmrsScramblingId = SCRAMBLING_ID;
731          }
732          initDlBwp->pdcchCfg.numCRsetToRel = 0;
733
734          /* Filling Serach Space */
735          initDlBwp->pdcchCfg.numSearchSpcToAddMod = PDCCH_CTRL_RSRC_SET_ONE_ID;
736          memset(initDlBwp->pdcchCfg.searchSpcToAddModList, 0, MAX_NUM_CRSET);
737          if(initDlBwp->pdcchCfg.numSearchSpcToAddMod <= MAX_NUM_CRSET)
738          {
739             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].searchSpaceId = PDCCH_SRCH_SPC_TWO_ID;
740             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].cRSetId = PDCCH_CTRL_RSRC_SET_ONE_ID;
741             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].mSlotPeriodicityAndOffset.mSlotPeriodicity\
742                                                                    = SLOTPERIODICITY_PR_SL1;
743             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].mSlotPeriodicityAndOffset.mSlotOffset = 0;
744             memset(initDlBwp->pdcchCfg.searchSpcToAddModList[idx].mSymbolsWithinSlot, 0, MONITORING_SYMB_WITHIN_SLOT_SIZE);
745             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].mSymbolsWithinSlot[idx] = PDCCH_SYMBOL_WITHIN_SLOT;
746             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].numCandidatesAggLevel1 = AGGREGATIONLEVEL_N8; 
747             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].numCandidatesAggLevel2 = AGGREGATIONLEVEL_N8; 
748             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].numCandidatesAggLevel4 = AGGREGATIONLEVEL_N4; 
749             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].numCandidatesAggLevel8 = AGGREGATIONLEVEL_N2; 
750             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].numCandidatesAggLevel16 = AGGREGATIONLEVEL_N1;
751             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].searchSpaceType = SEARCHSPACETYPE_PR_UE_SPECIFIC;
752             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].ueSpecificDciFormat = PDCCH_SRCH_SPC_TWO_UE_SPEC_DCI_FORMAT;
753          }
754          initDlBwp->pdcchCfg.numSearchSpcToRel = 0;
755       }
756
757       /* Filling PDSCH Config */
758       initDlBwp->pdschPresent = TRUE;
759       if(initDlBwp->pdschPresent)
760       {
761          initDlBwp->pdschCfg.dmrsDlCfgForPdschMapTypeA.addPos = ADDITIONALPOSITION_POS0;
762          initDlBwp->pdschCfg.resourceAllocType = RESOURCEALLOCATION_TYPE1;
763
764          initDlBwp->pdschCfg.numTimeDomRsrcAlloc = NUM_TIME_DOM_RSRC_ALLOC;
765
766          idx = 0; 
767          initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].mappingType = MAPPING_TYPEA;
768          initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].startSymbol = PDSCH_START_SYMBOL; 
769          initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].symbolLength = PDSCH_LENGTH_SYMBOL;
770          initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].startSymbolAndLength = calcSliv(PDSCH_START_SYMBOL, PDSCH_LENGTH_SYMBOL);
771
772          idx++;
773          DU_ALLOC_SHRABL_BUF(initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].k0, sizeof(uint8_t));
774          if(initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].k0 == NULLP)
775          {
776             DU_LOG("\nERROR  -->  DUAPP : Failed to allocate memory to K0 in fillDefaultInitDlBwp");
777             return RFAILED;
778          }
779          if(initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].k0)
780          {
781             *(initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].k0) = 1;
782          }
783          initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].mappingType = MAPPING_TYPEA;
784          initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].startSymbol = PDSCH_START_SYMBOL; 
785          initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].symbolLength = PDSCH_LENGTH_SYMBOL;
786          initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].startSymbolAndLength = calcSliv(PDSCH_START_SYMBOL, PDSCH_LENGTH_SYMBOL);
787
788          initDlBwp->pdschCfg.rbgSize = RBG_SIZE_CONFIG1;
789          initDlBwp->pdschCfg.numCodeWordsSchByDci = CODEWORDS_SCHED_BY_DCI_N1;
790          initDlBwp->pdschCfg.bundlingType = TYPE_STATIC_BUNDLING;
791          initDlBwp->pdschCfg.bundlingInfo.StaticBundling.size = 0;
792       }
793    }
794
795    return ROK;
796 }
797
798 /******************************************************************
799  *
800  * @brief Fills Initial UL Bandwidth Part
801  *
802  * @details
803  *
804  *    Function : fillDefaultInitUlBwp
805  *
806  *    Functionality: Fills Initial UL Bandwidth Part
807  *
808  * @params[in]  InitialUlBwp *initUlBwp
809  * @return void
810  *
811  *****************************************************************/
812 void fillDefaultInitUlBwp(InitialUlBwp *initUlBwp)
813 {
814    uint8_t idx;
815    if(initUlBwp)
816    {
817       initUlBwp->pucchPresent = FALSE;
818
819       /*Filling PUSCH Config */
820       initUlBwp->puschPresent = TRUE;
821       if(initUlBwp->puschPresent)
822       {
823          initUlBwp->puschCfg.dataScramblingId = SCRAMBLING_ID;
824          initUlBwp->puschCfg.dmrsUlCfgForPuschMapTypeA.addPos = ADDITIONALPOSITION_POS0; 
825          initUlBwp->puschCfg.dmrsUlCfgForPuschMapTypeA.transPrecodDisabled. \
826             scramblingId0 = SCRAMBLING_ID; 
827          initUlBwp->puschCfg.resourceAllocType = RESOURCEALLOCATION_TYPE1;
828          initUlBwp->puschCfg.numTimeDomRsrcAlloc = 2;
829          idx = 0;
830          if(initUlBwp->puschCfg.numTimeDomRsrcAlloc <= MAX_NUM_UL_ALLOC)
831          {
832             initUlBwp->puschCfg.timeDomRsrcAllocList[idx].k2 = PUSCH_K2_CFG1;
833             initUlBwp->puschCfg.timeDomRsrcAllocList[idx].mappingType =\
834                                                                        MAPPING_TYPEA;
835             initUlBwp->puschCfg.timeDomRsrcAllocList[idx].startSymbol = PUSCH_START_SYMBOL;
836             initUlBwp->puschCfg.timeDomRsrcAllocList[idx].symbolLength = PUSCH_LENGTH_SYMBOL;
837             initUlBwp->puschCfg.timeDomRsrcAllocList[idx].startSymbolAndLength =\
838                                                                                 calcSliv(PUSCH_START_SYMBOL, PUSCH_LENGTH_SYMBOL);
839
840             idx++;
841             initUlBwp->puschCfg.timeDomRsrcAllocList[idx].k2 = PUSCH_K2_CFG2;
842             initUlBwp->puschCfg.timeDomRsrcAllocList[idx].mappingType = MAPPING_TYPEA;
843             initUlBwp->puschCfg.timeDomRsrcAllocList[idx].startSymbol = PUSCH_START_SYMBOL;
844             initUlBwp->puschCfg.timeDomRsrcAllocList[idx].symbolLength = PUSCH_LENGTH_SYMBOL;
845             initUlBwp->puschCfg.timeDomRsrcAllocList[idx].startSymbolAndLength =\
846                                                                                 calcSliv(PUSCH_START_SYMBOL, PUSCH_LENGTH_SYMBOL);
847          }
848          initUlBwp->puschCfg.transformPrecoder = TRANSFORM_PRECODER_DISABLED;
849       }
850    }
851    else
852    {
853       DU_LOG("\nERROR  -->  DU APP : Memory is NULL of InitalUlBwp");
854    }
855
856 }
857 /******************************************************************
858  *
859  * @brief Fills SpCell Group Info
860  *
861  * @details
862  *
863  *    Function : fillDefaultSpCellGrpInfo
864  *
865  *    Functionality: Fills Sp Cell Group Info
866  *
867  * @params[in]  DuMacUeCfg *macUeCfg
868  * @return ROK - Success
869  *         RFAILED - Failure
870  *
871  *****************************************************************/
872 uint8_t fillDefaultSpCellGrpInfo(DuMacUeCfg *macUeCfg)
873 {
874    SpCellRecfg *spCell = NULL;
875
876    if(macUeCfg)
877       spCell = &macUeCfg->spCellCfg;
878
879    if(spCell)
880    {
881       macUeCfg->spCellCfgPres = true;
882
883       spCell->servCellIdx = SERV_CELL_IDX;
884       /* Filling Initial Dl Bwp */
885       if((fillDefaultInitDlBwp(&spCell->servCellCfg.initDlBwp)) != ROK)
886       {
887          DU_LOG("\nERROR  -->  DUAPP : Failed in fillDefaultInitDlBwp");
888          return RFAILED;
889       }
890
891       spCell->servCellCfg.numDlBwpToAddOrMod    = 0; 
892       spCell->servCellCfg.numDlBwpToRel    = 0; 
893       spCell->servCellCfg.firstActvDlBwpId = ACTIVE_DL_BWP_ID;
894       spCell->servCellCfg.defaultDlBwpId   = ACTIVE_DL_BWP_ID;
895       spCell->servCellCfg.bwpInactivityTmr = NULLP;
896       spCell->servCellCfg.pdschServCellCfg.maxMimoLayers = NULLP;
897       spCell->servCellCfg.pdschServCellCfg.maxCodeBlkGrpPerTb = NULLP;
898       spCell->servCellCfg.pdschServCellCfg.codeBlkGrpFlushInd = NULLP;
899       spCell->servCellCfg.pdschServCellCfg.xOverhead = NULLP;
900       spCell->servCellCfg.pdschServCellCfg.numHarqProcForPdsch = NUM_HARQ_PROC_FOR_PDSCH_N_16;
901
902       /* Filling Initial UL Bwp*/
903       fillDefaultInitUlBwp(&spCell->servCellCfg.initUlBwp);
904       spCell->servCellCfg.numUlBwpToAddOrMod  = 0; 
905       spCell->servCellCfg.numUlBwpToRel       = 0; 
906       spCell->servCellCfg.firstActvUlBwpId    = ACTIVE_DL_BWP_ID; 
907    }
908    else
909    {
910       DU_LOG("\nERROR  -->  DU APP : Memory is NULL for SpCellGrp");
911       return RFAILED;
912    }
913    return ROK;
914 }
915
916 /******************************************************************
917  *
918  * @brief Fills Physical Cell Group Info
919  *
920  * @details
921  *
922  *    Function : fillDefaultPhyCellGrpInfo
923  *
924  *    Functionality: Fills Physical Cell Group Info
925  *
926  * @params[in]  DuMacUeCfg *macUeCfg
927  * @return void
928  *
929  *****************************************************************/
930 void fillDefaultPhyCellGrpInfo(DuMacUeCfg *macUeCfg)
931 {
932    PhyCellGrpCfg *cellGrp = NULL;
933
934    if(macUeCfg)
935       cellGrp = &macUeCfg->phyCellGrpCfg;
936
937    if(cellGrp)
938    {
939       macUeCfg->phyCellGrpCfgPres = true;
940       cellGrp->pdschHarqAckCodebook = PDSCH_HARQ_ACK_CODEBOOK_DYNAMIC;
941       cellGrp->pNrFr1 = P_NR_FR1;
942    }
943    else
944    {
945       DU_LOG("\nERROR  -->  DU APP : Memory is NULL for Physical Cell Group");
946    }
947 }
948
949 /******************************************************************
950  *
951  * @brief Fills Mac Cell Group Info
952  *
953  * @details
954  *
955  *    Function : fillDefaultMacCellGrpInfo
956  *
957  *    Functionality: Fills Mac Cell Group Info
958  *
959  * @params[in]  DuMacUeCfg *macUeCfg
960  * @return void
961  *
962  *****************************************************************/
963 void fillDefaultMacCellGrpInfo(DuMacUeCfg *macUeCfg)
964 {
965    uint8_t idx;
966    MacCellGrpCfg *cellGrp = NULL;
967    
968    if(macUeCfg)
969       cellGrp = &macUeCfg->macCellGrpCfg;
970
971    if(cellGrp)
972    {
973       macUeCfg->macCellGrpCfgPres = true;
974
975       /* Filling Scheduling Request Config */
976       cellGrp->schReqCfg.addModListCount = 1;
977       if(cellGrp->schReqCfg.addModListCount <= MAX_NUM_SR_CFG_PER_CELL_GRP)
978       {
979          for(idx = 0; idx < cellGrp->schReqCfg.addModListCount; idx++)
980          {
981             cellGrp->schReqCfg.addModList[idx].schedReqId    = SCH_REQ_ID;
982             cellGrp->schReqCfg.addModList[idx].srProhibitTmr = SR_PROHIBIT_MS_32;
983             cellGrp->schReqCfg.addModList[idx].srTransMax    = SR_TRANS_MAX_N_16;
984          }
985       }
986       cellGrp->schReqCfg.relListCount = 0;
987
988       /* Filling Tag config */
989       cellGrp->tagCfg.addModListCount = 1;
990       if(cellGrp->tagCfg.addModListCount <= MAC_NUM_TAGS)
991       {
992          for(idx = 0; idx < cellGrp->tagCfg.addModListCount; idx++)
993          {
994             cellGrp->tagCfg.addModList[idx].tagId = TAG_ID;
995             cellGrp->tagCfg.addModList[idx].timeAlignTimer = TIME_ALIGNMENT_TIMER_INFINITY;
996          }
997       }
998       cellGrp->tagCfg.relListCount = 0;
999
1000       /* Filling BSR config */
1001       cellGrp->bsrTmrCfg.periodicTimer = BSR_PERIODIC_TIMER_SF_10;
1002       cellGrp->bsrTmrCfg.retxTimer = BSR_RETX_TIMER_SF_320;
1003       cellGrp->bsrTmrCfg.srDelayTimer = BSR_SR_DELAY_TMR_2560;
1004
1005       /* Filling PHR config */
1006       cellGrp->phrCfgSetupPres = true;
1007       cellGrp->phrCfg.periodicTimer = PHR_PERIODIC_TIMER_INFINITY;
1008       cellGrp->phrCfg.prohibitTimer = PHR_PROHIBIT_TIMER_SF_0;
1009       cellGrp->phrCfg.txPowerFactor = PHR_TX_PWR_FACTOR_CHANGE_INFINITY;
1010       cellGrp->phrCfg.multiplePHR   = false;
1011       cellGrp->phrCfg.dummy         = false;
1012       cellGrp->phrCfg.phrType2OtherCell = false;
1013       cellGrp->phrCfg.phrOtherCG = PHR_MODE_OTHER_CG_REAL;  
1014
1015       /* Filling Drx Config */
1016 #ifdef NR_DRX
1017       cellGrp->drxCfg.drxOnDurationTimer.onDurationTimerValInMs = DRX_ONDURATION_TIMER_VALUE_PRESENT_IN_MS;
1018       if(!cellGrp->drxCfg.drxOnDurationTimer.onDurationTimerValInMs)
1019          cellGrp->drxCfg.drxOnDurationTimer.onDurationtimerValue.subMilliSeconds = \
1020                                                                                    DRX_ONDURATION_TIMER_VALUE_IN_SUBMS;
1021       else
1022          cellGrp->drxCfg.drxOnDurationTimer.onDurationtimerValue.milliSeconds = \
1023                                                                                 DRX_ONDURATION_TIMER_VALUE_IN_MS;
1024       cellGrp->drxCfg.drxInactivityTimer = DRX_INACTIVITY_TIMER;
1025       cellGrp->drxCfg.drxHarqRttTimerDl = DRX_HARQ_RTT_TIMER_DL;
1026       cellGrp->drxCfg.drxHarqRttTimerUl = DRX_HARQ_RTT_TIMER_UL;
1027       cellGrp->drxCfg.drxRetransmissionTimerDl = DRX_RETRANSMISSION_TIMER_DL;
1028       cellGrp->drxCfg.drxRetransmissionTimerUl = DRX_RETRANSMISSION_TIMER_UL;
1029       cellGrp->drxCfg.drxLongCycleStartOffset.drxLongCycleStartOffsetVal = DRX_LONG_CYCLE_START_OFFSET_VAL;
1030       cellGrp->drxCfg.drxLongCycleStartOffset.drxLongCycleStartOffsetChoice = DRX_LONG_CYCLE_START_OFFSET_CHOICE;
1031       cellGrp->drxCfg.shortDrxPres = DRX_SHORT_CYCLE_PRESENT;
1032       if(cellGrp->drxCfg.shortDrxPres)
1033       {
1034          cellGrp->drxCfg.shortDrx.drxShortCycle = DRX_SHORT_CYCLE;
1035          cellGrp->drxCfg.shortDrx.drxShortCycleTimer = DRX_SHORT_CYCLE_TIMER;
1036       }
1037       cellGrp->drxCfg.drxSlotOffset = DRX_SLOT_OFFSET;
1038 #endif
1039
1040    }
1041    else
1042    {
1043       DU_LOG("\nERROR  -->  DU APP : Memory is NULL for Master Cell Group");
1044    }
1045 }
1046
1047 /*******************************************************************
1048  *
1049  * @brief Fills default modulation info for a UE
1050  *
1051  * @details
1052  *
1053  *    Function : fillDefaultModulation
1054  *
1055  *    Functionality: Fills default modulation info for a UE
1056  *
1057  * @params[in] Pointer to MAC UE configuration
1058  * @return ROK     - success
1059  *         RFAILED - failure
1060  *
1061  * ****************************************************************/
1062 void fillDefaultModulation(DuMacUeCfg *ueCfg)
1063 {
1064    ueCfg->dlModInfo.modOrder = MOD_ORDER_QPSK;
1065    ueCfg->dlModInfo.mcsIndex = DEFAULT_MCS;
1066    ueCfg->dlModInfo.mcsTable = MCS_TABLE_QAM64; /* Spec 38.214-Table 5.1.3.1-1 */
1067
1068    ueCfg->ulModInfo.modOrder = MOD_ORDER_QPSK;
1069    ueCfg->ulModInfo.mcsIndex = DEFAULT_MCS;
1070    ueCfg->ulModInfo.mcsTable = MCS_TABLE_QAM64; /* Spec 38.214-Table 5.1.3.1-1 */
1071 }
1072
1073 /******************************************************************
1074  *
1075  * @brief Function to fill Mac Lc Cfg for SRB1
1076  *
1077  * @details
1078  *
1079  *    Function : fillMacSrb1LcCfg
1080  *
1081  *    Functionality: Function to fill Mac Lc cfg for SRB1
1082  *
1083  * @params[in]  LcCfg *lcCfg, LcCfg *ueSetReqDb
1084  * @return void
1085  *****************************************************************/
1086
1087 void fillMacSrb1LcCfg(DuLcCfg *macLcCfg)
1088 {
1089    macLcCfg->lcConfig.lcId   = SRB1_LCID;
1090    macLcCfg->configType = CONFIG_ADD;
1091    macLcCfg->lcConfig.drbQos = NULLP;
1092    macLcCfg->lcConfig.snssai = NULLP;
1093    macLcCfg->lcConfig.ulLcCfgPres = true;
1094    fillDefaultUlLcCfg(&macLcCfg->lcConfig.ulLcCfg);
1095 }
1096
1097 /******************************************************************
1098  *
1099  * @brief Function to fill the Lc cfg from ueSetupReqDb
1100  *
1101  * @details
1102  *
1103  *    Function : fillMacLcCfgToAddMod
1104  *
1105  *    Functionality: Function to fill the Lc cfg from ueSetupReqDb
1106  *
1107  * @params[in]  LcCfg *lcCfg, LcCfg *ueSetReqDb
1108  * @return ROK/RFAILED
1109  *
1110  *****************************************************************/
1111
1112 uint8_t fillMacLcCfgToAddMod(DuLcCfg *macLcCfgToSend, DuLcCfg *ueLcCfgDb, DuLcCfg *oldLcCfg, Bool toUpdate)
1113 {
1114    if(!toUpdate)
1115    {
1116       if(macLcCfgToSend)
1117       {
1118          macLcCfgToSend->lcConfig.lcId = ueLcCfgDb->lcConfig.lcId;
1119          macLcCfgToSend->configType = ueLcCfgDb->configType;
1120
1121          if(ueLcCfgDb->lcConfig.drbQos)
1122             macLcCfgToSend->lcConfig.drbQos = ueLcCfgDb->lcConfig.drbQos;
1123          else if(oldLcCfg)
1124             macLcCfgToSend->lcConfig.drbQos = oldLcCfg->lcConfig.drbQos;
1125          else
1126             macLcCfgToSend->lcConfig.drbQos = NULL;
1127
1128          if(ueLcCfgDb->lcConfig.snssai)
1129             macLcCfgToSend->lcConfig.snssai = ueLcCfgDb->lcConfig.snssai;
1130          else if(oldLcCfg)
1131             macLcCfgToSend->lcConfig.snssai = oldLcCfg->lcConfig.snssai;
1132          else
1133             macLcCfgToSend->lcConfig.snssai = NULL;
1134
1135          macLcCfgToSend->lcConfig.ulLcCfgPres = ueLcCfgDb->lcConfig.ulLcCfgPres;
1136          memcpy(&macLcCfgToSend->lcConfig.ulLcCfg, &ueLcCfgDb->lcConfig.ulLcCfg, sizeof(UlLcCfg));
1137          memcpy(&macLcCfgToSend->lcConfig.dlLcCfg, &ueLcCfgDb->lcConfig.dlLcCfg, sizeof(DlLcCfg));
1138       }
1139    }
1140    else
1141    {
1142       oldLcCfg->lcConfig.lcId = ueLcCfgDb->lcConfig.lcId;
1143       oldLcCfg->configType = ueLcCfgDb->configType;
1144
1145       if(ueLcCfgDb->lcConfig.drbQos)
1146       {
1147          if(oldLcCfg->lcConfig.drbQos)
1148             DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, oldLcCfg->lcConfig.drbQos, sizeof(DrbQosInfo));
1149
1150          DU_ALLOC_SHRABL_BUF(oldLcCfg->lcConfig.drbQos, sizeof(DrbQosInfo));
1151          if(oldLcCfg->lcConfig.drbQos == NULL)
1152          {
1153             DU_LOG("\nERROR  -->  DU APP : Memory Alloc Failed at fillMacLcCfgToAddMod()");
1154             return RFAILED;
1155          }
1156          memcpy(oldLcCfg->lcConfig.drbQos, ueLcCfgDb->lcConfig.drbQos, sizeof(DrbQosInfo));
1157       }
1158
1159       if(ueLcCfgDb->lcConfig.snssai)
1160       {
1161          if(oldLcCfg->lcConfig.snssai)
1162             DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, oldLcCfg->lcConfig.snssai, sizeof(Snssai));
1163
1164          DU_ALLOC_SHRABL_BUF(oldLcCfg->lcConfig.snssai, sizeof(Snssai));
1165          if(oldLcCfg->lcConfig.snssai == NULL)
1166          {
1167             DU_LOG("\nERROR  -->  DU APP : Memory Alloc Failed at fillMacLcCfgToAddMod()");
1168             DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, oldLcCfg->lcConfig.drbQos, sizeof(DrbQosInfo));
1169             return RFAILED;
1170          }
1171          memcpy(oldLcCfg->lcConfig.snssai, ueLcCfgDb->lcConfig.snssai, sizeof(Snssai));
1172       }
1173
1174       oldLcCfg->lcConfig.ulLcCfgPres = ueLcCfgDb->lcConfig.ulLcCfgPres;
1175       memcpy(&oldLcCfg->lcConfig.ulLcCfg, &ueLcCfgDb->lcConfig.ulLcCfg, sizeof(UlLcCfg));
1176       memcpy(&oldLcCfg->lcConfig.dlLcCfg, &ueLcCfgDb->lcConfig.dlLcCfg, sizeof(DlLcCfg));
1177    }
1178    return ROK;
1179 }
1180
1181 /******************************************************************
1182  *
1183  * @brief Function to copy the Bit rate from ueSetupReqDb
1184  *
1185  * @details
1186  *
1187  *    Function : fillAmbr
1188  *
1189  *    Functionality: Function to copy bit Rate from ueSetupReqDb
1190  *
1191  * @params[in]  AmbrCfg **macAmbr, AmbrCfg  *ueDbAmbr
1192  * @return ROK/RFAILED
1193  *
1194  *****************************************************************/
1195
1196 uint8_t fillAmbr(AmbrCfg **macAmbrCfgToSend, AmbrCfg *ueDbAmbr, AmbrCfg **oldMacAmbrCfg, Bool toUpdate)
1197 {
1198    if(!toUpdate)
1199    {
1200       if(ueDbAmbr)
1201       {
1202          *macAmbrCfgToSend = ueDbAmbr;
1203       }
1204       else if(oldMacAmbrCfg)
1205          *macAmbrCfgToSend = *oldMacAmbrCfg;       
1206    }
1207    else
1208    {
1209       if(ueDbAmbr)
1210       {
1211          if(oldMacAmbrCfg)
1212          {
1213             if(*oldMacAmbrCfg)
1214             {
1215                DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, *oldMacAmbrCfg, sizeof(AmbrCfg));
1216             }
1217             DU_ALLOC_SHRABL_BUF(*oldMacAmbrCfg, sizeof(AmbrCfg));
1218             if(*oldMacAmbrCfg == NULLP)
1219             {
1220                DU_LOG("\nERROR  -->  DU APP : Memory Alloc Failed at fillAmbr()");
1221                return RFAILED;
1222             }
1223             memset(*oldMacAmbrCfg, 0, sizeof(AmbrCfg));
1224             (*oldMacAmbrCfg)->ulBr = ueDbAmbr->ulBr;
1225          }
1226       }
1227    }
1228
1229    return ROK;
1230 }
1231
1232 /******************************************************************
1233  *
1234  * @brief Builds and Send UE ReConfig Request to MAC
1235  *
1236  * @details
1237  *
1238  *    Function : sendUeRecfgReqToMac
1239  *
1240  *    Functionality: Builds and Send UE ReConfig Request to MAC
1241  *
1242  * @Params[in]  MacUeRecfg pointer
1243  * @return ROK     - success
1244  *         RFAILED - failure
1245  *
1246  * ****************************************************************/
1247
1248 uint8_t sendUeRecfgReqToMac(MacUeRecfg *macUeRecfg)
1249 {
1250    uint8_t ret = ROK;
1251    Pst pst;
1252
1253    /* Fill Pst */
1254    FILL_PST_DUAPP_TO_MAC(pst, EVENT_MAC_UE_RECONFIG_REQ);
1255
1256    if(macUeRecfg)
1257    {
1258       /* Processing one Ue at a time to MAC */
1259       DU_LOG("\nDEBUG   -->  DU_APP: Sending Ue Reconfig Request to MAC");
1260       ret = (*packMacUeReconfigReqOpts[pst.selector])(&pst, macUeRecfg);
1261       if(ret == RFAILED)
1262       {
1263          DU_LOG("\nERROR  -->  DU APP : Failed to send Reconfig Request to MAC at sendUeRecfgReqToMac()");
1264          DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, macUeRecfg, sizeof(MacUeRecfg));
1265       }
1266    }
1267    else
1268    {
1269       DU_LOG("\nERROR  -->  DU_APP: Received macUeRecfg is NULLP at sendUeRecfgReqToMac()");
1270       ret = RFAILED;
1271    }
1272    return ret;
1273 }
1274
1275
1276
1277 /******************************************************************
1278  *
1279  * @brief update DuMacUeCfg structure
1280  *
1281  * @details
1282  *
1283  *    Function : updateDuMacUeCfg
1284  *
1285  *    Functionality: Fills DuMacUeCfg
1286  *
1287  * @params[in]  cellId, ueId, crnti, 
1288  *              DuUeCfg  pointer,
1289  *              DuMacUeCfg pointer
1290  * @return ROK/RFAILED
1291  *
1292  *****************************************************************/
1293 uint8_t updateDuMacUeCfg(uint16_t cellId, uint8_t gnbDuUef1apId, uint16_t crnti, DuUeCfg *ueCfgDb, DuMacUeCfg *duMacUeCfg)
1294 {
1295    uint8_t ret = ROK, dbIdx = 0, lcIdx = 0, cellIdx = 0;
1296    bool lcIdFound = false;
1297    DuMacUeCfg *duMacDb = NULLP;
1298
1299    duMacUeCfg->cellId = cellId;
1300    duMacUeCfg->ueId = gnbDuUef1apId;
1301    duMacUeCfg->crnti = crnti;
1302
1303    if(!ueCfgDb)
1304    {
1305       fillDefaultMacCellGrpInfo(duMacUeCfg);
1306       fillDefaultPhyCellGrpInfo(duMacUeCfg);
1307
1308       if((fillDefaultSpCellGrpInfo(duMacUeCfg)) != ROK)
1309       {
1310          DU_LOG("\nERROR  --> DUAPP : Failed in fillDefaultSpCellGrpInfo");
1311          return RFAILED;
1312       }
1313
1314       duMacUeCfg->ambrCfg = NULLP;
1315       fillDefaultModulation(duMacUeCfg);
1316       fillMacSrb1LcCfg(&duMacUeCfg->lcCfgList[0]);
1317       duMacUeCfg->numLcs++;
1318    }
1319    else
1320    {
1321       if(ueCfgDb->dataTransmissionAction == STOP_TRANSMISSION)
1322       {
1323          duMacUeCfg->transmissionAction = ueCfgDb->dataTransmissionAction; 
1324          return ROK;
1325       }
1326
1327       GET_CELL_IDX(cellId, cellIdx);
1328       if(duCb.actvCellLst[cellIdx])
1329          duMacDb = &duCb.actvCellLst[cellIdx]->ueCb[duMacUeCfg->ueId-1].duMacUeCfg;
1330       else
1331       {
1332          DU_LOG("\nERROR  -->  DU APP : Cell Id [%d] does not exist", cellId);
1333          return RFAILED;
1334       }
1335       duMacDb->macUeCfgState = UE_CFG_INPROGRESS;
1336       
1337       if(ueCfgDb->cellGrpCfg)
1338       {
1339          ret = procUeRecfgCellInfo(duMacUeCfg, duMacDb, ueCfgDb->cellGrpCfg);
1340          if(ret == ROK)
1341          {
1342             if(duMacUeCfg->spCellCfgPres == true)
1343             {
1344                if(duMacUeCfg->spCellCfg.servCellCfg.initDlBwp.pdschPresent)
1345                {
1346                   fillStartSymbolAndLen(duMacUeCfg->spCellCfg.servCellCfg.initDlBwp.pdschCfg.numTimeDomRsrcAlloc,\
1347                         &duMacUeCfg->spCellCfg.servCellCfg.initDlBwp.pdschCfg, NULL);
1348                }
1349                if(duMacUeCfg->spCellCfg.servCellCfg.initUlBwp.puschPresent)
1350                {
1351                   fillStartSymbolAndLen(duMacUeCfg->spCellCfg.servCellCfg.initUlBwp.puschCfg.numTimeDomRsrcAlloc,\
1352                         NULL, &duMacUeCfg->spCellCfg.servCellCfg.initUlBwp.puschCfg);
1353                }
1354             }
1355
1356             if(duMacDb)
1357                ret = fillAmbr(&duMacUeCfg->ambrCfg, ueCfgDb->ambrCfg , &duMacDb->ambrCfg, FALSE);
1358             else
1359                ret = fillAmbr(&duMacUeCfg->ambrCfg, ueCfgDb->ambrCfg , NULL, FALSE);
1360
1361             duFillModulationDetails(duMacUeCfg, duMacDb, ueCfgDb->ueNrCapability);
1362          }
1363       }
1364       else
1365       {
1366          fillDefaultMacCellGrpInfo(duMacUeCfg);
1367          fillDefaultPhyCellGrpInfo(duMacUeCfg);
1368          fillDefaultSpCellGrpInfo(duMacUeCfg);
1369          fillDefaultModulation(duMacUeCfg);
1370          duMacUeCfg->ambrCfg = NULLP;
1371       }
1372
1373 #ifdef NR_DRX
1374       
1375       duMacUeCfg->drxConfigIndicatorRelease = ueCfgDb->drxConfigIndicatorRelease;
1376
1377       if(ueCfgDb->drxCyclePres)
1378       {
1379          duMacUeCfg->macCellGrpCfg.drxCfg.drxLongCycleStartOffset.drxLongCycleStartOffsetChoice = ueCfgDb->drxCycle.drxLongCycleLength;
1380          if(ueCfgDb->drxCycle.shortDrxCyclePres)
1381          {
1382             duMacUeCfg->macCellGrpCfg.drxCfg.shortDrxPres = true;
1383             duMacUeCfg->macCellGrpCfg.drxCfg.shortDrx.drxShortCycle = ueCfgDb->drxCycle.shortDrxCycle.drxShortCycle;
1384             duMacUeCfg->macCellGrpCfg.drxCfg.shortDrx.drxShortCycleTimer = ueCfgDb->drxCycle.shortDrxCycle.drxShortCycleTimer;
1385          }
1386          else
1387          {
1388             duMacUeCfg->macCellGrpCfg.drxCfg.shortDrxPres = false;
1389          }
1390       }
1391 #endif
1392
1393       /* Filling LC Context */
1394       for(dbIdx = 0; (dbIdx < ueCfgDb->numMacLcs && ret == ROK); dbIdx++)
1395       {
1396          if(!ueCfgDb->macLcCfg[dbIdx].lcConfig.ulLcCfgPres)
1397          {
1398             /* Filling default UL LC config in MAC if not present */
1399             ueCfgDb->macLcCfg[dbIdx].lcConfig.ulLcCfgPres = true;
1400             fillDefaultUlLcCfg(&ueCfgDb->macLcCfg[dbIdx].lcConfig.ulLcCfg);
1401          }
1402
1403          if(duMacDb)
1404          {
1405             for(lcIdx = 0; lcIdx < duMacDb->numLcs; lcIdx++)
1406             {
1407                if(ueCfgDb->macLcCfg[dbIdx].lcConfig.lcId == duMacDb->lcCfgList[lcIdx].lcConfig.lcId)
1408                {
1409                   lcIdFound = true;
1410                   if((ueCfgDb->macLcCfg[dbIdx].configType == CONFIG_UNKNOWN) ||
1411                         (ueCfgDb->macLcCfg[dbIdx].configType == CONFIG_MOD))
1412                   {
1413                      ueCfgDb->macLcCfg[dbIdx].configType = CONFIG_MOD;
1414                      ret = fillMacLcCfgToAddMod(&duMacUeCfg->lcCfgList[dbIdx], &ueCfgDb->macLcCfg[dbIdx],\
1415                            &duMacDb->lcCfgList[lcIdx], FALSE);
1416                   }
1417                }
1418                else
1419                   lcIdFound = false;
1420             }
1421          }
1422
1423          if(!lcIdFound)
1424          {
1425             /* ADD/DEL CONFIG */
1426             ret = fillMacLcCfgToAddMod(&duMacUeCfg->lcCfgList[dbIdx], &ueCfgDb->macLcCfg[dbIdx], NULL, FALSE);
1427          }
1428          if(ret == ROK)
1429          {
1430             duMacUeCfg->numLcs++;
1431          }
1432          else
1433          {
1434             DU_LOG("\nERROR  -->  DU APP : Failed to add Lc at Idx %d in updateDuMacUeCfg()", dbIdx); 
1435             break;
1436          }
1437       }/*End of Outer FOR loop */
1438       memcpy(&ueCfgDb->copyOfmacUeCfg, duMacUeCfg, sizeof(DuMacUeCfg));
1439    }
1440    return ret;
1441 }
1442
1443 /******************************************************************
1444  *
1445  * @brief Fills Rlc AM Information
1446  *
1447  * @details
1448  *
1449  *    Function : fillDefaultAmInfo
1450  *
1451  *    Functionality: Fills Rlc AM Information
1452  *
1453  * @params[in]  AmBearerCfg *amCfg
1454  * @return void
1455  *
1456  *****************************************************************/
1457 void fillDefaultAmInfo(AmBearerCfg *amCfg)
1458 {
1459    /* DL AM */
1460    amCfg->dlAmCfg.snLenDl     = AM_SIZE_12;
1461    amCfg->dlAmCfg.pollRetxTmr = T_POLL_RETRANSMIT_VAL;
1462    amCfg->dlAmCfg.pollPdu     = POLL_PDU_VAL;
1463    amCfg->dlAmCfg.pollByte    = POLL_BYTE_VAL;
1464    amCfg->dlAmCfg.maxRetxTh   = MAX_RETX_THRESHOLD_VAL;   
1465  
1466    /* UL AM */
1467    amCfg->ulAmCfg.snLenUl     = AM_SIZE_12;
1468    amCfg->ulAmCfg.reAssemTmr  = T_REASSEMBLY_VAL; 
1469    amCfg->ulAmCfg.statProhTmr = T_STATUS_PROHIBHIT_VAL;
1470 }
1471
1472 /******************************************************************
1473  *
1474  * @brief Fills RLC UM Bi Directional Information
1475  *
1476  * @details
1477  *
1478  *    Function : fillDefaultUmBiInfo
1479  *
1480  *    Functionality: Fills RLC UM Bi Directional Information
1481  *
1482  * @params[in]  UmBiDirBearerCfg *umBiDirCfg
1483  * @return void
1484  *
1485  *****************************************************************/
1486 void fillDefaultUmBiInfo(UmBiDirBearerCfg *umBiDirCfg)
1487 {
1488    /* UL UM BI DIR INFO */
1489    umBiDirCfg->ulUmCfg.snLenUlUm = UM_SIZE_12;
1490    umBiDirCfg->ulUmCfg.reAssemTmr = T_REASSEMBLY_VAL;
1491
1492    /* DL UM BI DIR INFO */
1493    umBiDirCfg->dlUmCfg.snLenDlUm  = UM_SIZE_12;
1494 }
1495
1496 /******************************************************************
1497  *
1498  * @brief Fills RLC UM Uni Directional UL Information
1499  *
1500  * @details
1501  *
1502  *    Function : fillDefaultUmUlInfo
1503  *
1504  *    Functionality: Fills RLC UM Uni Directional Info
1505  *
1506  * @params[in]  UmUniDirUlBearerCfg *UmUlCfg
1507  * @return void
1508  *
1509  *****************************************************************/
1510 void fillDefaultUmUlInfo(UmUniDirUlBearerCfg *UmUlCfg)
1511 {
1512    UmUlCfg->ulUmCfg.snLenUlUm = UM_SIZE_12;
1513    UmUlCfg->ulUmCfg.reAssemTmr = T_REASSEMBLY_VAL;
1514 }
1515
1516 /******************************************************************
1517  *
1518  * @brief Fills RLC UM Uni Directional DL Information
1519  *
1520  * @details
1521  *
1522  *    Function : fillDefaultUmDlInfo
1523  *
1524  *    Functionality: Fills RLC UM Uni Directional DL Info
1525  *
1526  * @params[in]  UmUniDirDlBearerCfg *UmDlCfg
1527  * @return void
1528  *
1529  *****************************************************************/
1530 void fillDefaultUmDlInfo(UmUniDirDlBearerCfg *UmDlCfg)
1531 {
1532    UmDlCfg->dlUmCfg.snLenDlUm  = UM_SIZE_12;
1533 }
1534
1535 /******************************************************************
1536  *
1537  * @brief Builds Rlc Mode Default Configuration
1538  *
1539  * @details
1540  *
1541  *    Function : fillDefaultRlcModeCfg
1542  *
1543  *    Functionality: Builds Rlc Mode Default Configuration
1544  *
1545  * @params[in]  rlcMode, RlcBearerCfg *lcCfg
1546  * @return ROK/RFAILED
1547  *
1548  *****************************************************************/
1549
1550 uint8_t fillDefaultRlcModeCfg(uint8_t rlcMode, RlcBearerCfg *lcCfg)
1551 {
1552    if(lcCfg)
1553    {
1554       switch(rlcMode)
1555       {
1556          case RLC_AM :
1557             {
1558                if(!lcCfg->u.amCfg)
1559                {
1560                   DU_ALLOC_SHRABL_BUF(lcCfg->u.amCfg, sizeof(AmBearerCfg));
1561                   if(lcCfg->u.amCfg)
1562                      fillDefaultAmInfo(lcCfg->u.amCfg);
1563                   else
1564                   {
1565                      DU_LOG("\n ERROR  -->  DU APP : Memory Alloc failed at AmCfg at fillDefaultRlcModeCfg()");
1566                      return RFAILED;
1567                   }
1568                }
1569                break;
1570             }
1571          case RLC_UM_BI_DIRECTIONAL :
1572             {
1573                if(!lcCfg->u.umBiDirCfg)
1574                {
1575                   DU_ALLOC_SHRABL_BUF(lcCfg->u.umBiDirCfg, sizeof(UmBiDirBearerCfg));
1576                   if(lcCfg->u.umBiDirCfg)
1577                      fillDefaultUmBiInfo(lcCfg->u.umBiDirCfg);
1578                   else
1579                   {
1580                      DU_LOG("\n ERROR  -->  DU APP : Memory Alloc failed at UmBiDirCfg at fillDefaultRlcModeCfg()");
1581                      return RFAILED;
1582                   }
1583                }
1584                break;
1585             }
1586          case RLC_UM_UNI_DIRECTIONAL_UL :
1587             {
1588                if(!lcCfg->u.umUniDirUlCfg)
1589                {
1590                   DU_ALLOC_SHRABL_BUF(lcCfg->u.umUniDirUlCfg, sizeof(UmUniDirUlBearerCfg));
1591                   if(lcCfg->u.umUniDirUlCfg)
1592                      fillDefaultUmUlInfo(lcCfg->u.umUniDirUlCfg);
1593                   else
1594                   {
1595                      DU_LOG("\n ERROR  -->  DU APP : Memory Alloc failed at UmUniDirUlCfg at fillDefaultRlcModeCfg()");
1596                      return RFAILED;
1597                   }
1598                }
1599                break;
1600             }
1601          case RLC_UM_UNI_DIRECTIONAL_DL :
1602             {
1603                if(!lcCfg->u.umUniDirDlCfg)
1604                {
1605                   DU_ALLOC_SHRABL_BUF(lcCfg->u.umUniDirDlCfg, sizeof(UmUniDirDlBearerCfg));
1606                   if(lcCfg->u.umUniDirDlCfg)
1607                      fillDefaultUmDlInfo(lcCfg->u.umUniDirDlCfg);
1608                   else
1609                   {
1610                      DU_LOG("\n ERROR  -->  DU APP : Memory Alloc failed at UmUniDirDlCfg at fillDefaultRlcModeCfg()");
1611                      return RFAILED;
1612                   }
1613                }
1614                break;
1615             }
1616          default:
1617             DU_LOG("\nERROR  -->  DUAPP: Invalid rlcMode %d at extractRlcCfgToAddMod()", rlcMode);
1618             return RFAILED;
1619       }
1620
1621    }
1622    else
1623    {
1624       DU_LOG("\nERROR  -->  DUAPP: Received LC Config is NULL");
1625       return RFAILED;
1626    }
1627    return ROK;
1628 }
1629
1630 /*******************************************************************
1631  *
1632  * @brief Function to fill Rlc Lc Cfg for SRB1
1633  *
1634  * @details
1635  *
1636  *    Function : fillRlcSrb1LcCfg
1637  *
1638  *    Functionality: 
1639  *     Function to fill Rlc Lc Cfg for SRB1
1640  * 
1641  *  @params[in]     Pointer to RlcBearerCfg
1642  *  @return ROK/RFAILED
1643  * 
1644  *****************************************************************/
1645
1646 uint8_t fillRlcSrb1LcCfg(DuRlcBearerCfg *rlcLcCfg)
1647 {
1648    uint8_t ret = ROK;
1649
1650    rlcLcCfg->rlcBearerCfg.rbId   = SRB1_LCID;
1651    rlcLcCfg->rlcBearerCfg.rbType = RB_TYPE_SRB;
1652    rlcLcCfg->rlcBearerCfg.lcId   = SRB1_LCID;
1653    rlcLcCfg->rlcBearerCfg.lcType = LCH_DCCH;
1654    rlcLcCfg->rlcBearerCfg.rlcMode = RLC_AM;
1655    rlcLcCfg->configType = CONFIG_ADD;
1656    ret = fillDefaultRlcModeCfg(rlcLcCfg->rlcBearerCfg.rlcMode, &rlcLcCfg->rlcBearerCfg);
1657    rlcLcCfg->rlcBearerCfg.isLcAddModRspSent = true;
1658    return ret;
1659 }
1660
1661 /*******************************************************************
1662  *
1663  * @brief Processes UE ReConfig Req to RLC UL
1664  *
1665  * @details
1666  *
1667  *    Function : sendUeRecfgReqToRlc
1668  *
1669  *    Functionality: 
1670  *     Processes UE Reconfig Req to RLC UL
1671  * 
1672  *  @params[in]     Pointer to RlcUeRecfg
1673  *  @return ROK     - success
1674  *          RFAILED - failure
1675  * 
1676  *****************************************************************/
1677
1678 uint8_t sendUeRecfgReqToRlc(RlcUeRecfg *rlcUeRecfg)
1679 {
1680    uint8_t ret;
1681    Pst pst;
1682    
1683    FILL_PST_DUAPP_TO_RLC(pst, RLC_UL_INST, EVENT_RLC_UE_RECONFIG_REQ);
1684    if(rlcUeRecfg)
1685    {
1686       /* Processing one Ue at a time to RLC */
1687       DU_LOG("\nDEBUG   -->  DU_APP: Sending Ue Reconfig Request to RLC UL");
1688       ret = (*packRlcUeReconfigReqOpts[pst.selector])(&pst, rlcUeRecfg);
1689       if(ret == RFAILED)
1690       {
1691          DU_LOG("\nERROR  -->  DU_APP : Failed to send Ue Reconfig Req to RLC at sendUeRecfgReqToRlc()");
1692          DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, rlcUeRecfg, sizeof(RlcUeRecfg));
1693       }
1694    }
1695    else
1696    {
1697       DU_LOG("\nERROR  -->   DU_APP: Received RlcUeRecfg is NULL at sendUeRecfgReqToRlc()");
1698       ret = RFAILED;
1699    }
1700    return ret;
1701 }
1702
1703 /******************************************************************
1704  *
1705  * @brief Fills Snssai information
1706  *
1707  * @details
1708  *
1709  *    Function : fillSnssaiInfo
1710  *
1711  *    Functionality: Fills Snssai information
1712  *
1713  *  @params[in]    LcCfg *snssaiTobeSend, LcCfg *snssaiDb, LcCfg *oldSnssai,
1714  *  Bool toUpdateg
1715  *  @return ROK     - success
1716  *          RFAILED - failure
1717  * 
1718  *****************************************************************/
1719 uint8_t fillSnssaiInfo(Snssai *snssaiTobeSend, Snssai *snssaiDb, Snssai **oldSnssai, Bool toUpdate)
1720 {
1721    if(!toUpdate)
1722    {
1723       if(snssaiDb)
1724          snssaiTobeSend = snssaiDb;
1725       else if(oldSnssai)
1726          snssaiTobeSend = *oldSnssai;
1727       else
1728          snssaiTobeSend = NULL;
1729    }
1730    else
1731    {
1732       if(snssaiDb)
1733       {
1734          if(*oldSnssai)
1735             DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, *oldSnssai, sizeof(Snssai));
1736
1737          DU_ALLOC_SHRABL_BUF(*oldSnssai, sizeof(Snssai));
1738          if(*oldSnssai == NULL)
1739          {
1740             DU_LOG("\nERROR  -->  DU APP : Memory Alloc Failed at fillSnssaiInfo()");
1741             return RFAILED;
1742          }
1743          memcpy(*oldSnssai, snssaiDb, sizeof(Snssai));
1744       }
1745    }
1746    return ROK;
1747 }
1748 /******************************************************************
1749  *
1750  * @brief Fills RlcBearerCfg structure
1751  *
1752  * @details
1753  *
1754  *    Function : updateRlcUeCfg
1755  *
1756  *    Functionality: Fills Rlc Bearer Cfg
1757  *
1758  *
1759  *****************************************************************/
1760 uint8_t updateRlcUeCfg(uint16_t cellId, uint8_t duUeF1apId, DuUeCfg *ueCfgDb, DuRlcUeCfg *rlcUeCfg)
1761 {
1762    uint8_t ret, dbIdx, lcIdx, cellIdx;
1763    bool lcIdFound = false;
1764    DuRlcUeCfg *duRlcDb = NULLP;
1765
1766    ret = ROK;
1767    rlcUeCfg->cellId       = cellId;
1768    rlcUeCfg->ueId         = duUeF1apId;
1769
1770    if(!ueCfgDb)
1771    {
1772       /* Initial RB being Added */ 
1773       ret = fillRlcSrb1LcCfg(&rlcUeCfg->rlcLcCfg[0]);
1774       if(ret == ROK)
1775          rlcUeCfg->numLcs++;
1776       else
1777          memset(rlcUeCfg, 0, sizeof(DuRlcUeCfg));
1778    }
1779    else
1780    {
1781       /* Fetch RlcDb from DuUeCb */ 
1782       GET_CELL_IDX(cellId, cellIdx);
1783       duRlcDb = &duCb.actvCellLst[cellIdx]->ueCb[rlcUeCfg->ueId-1].duRlcUeCfg;
1784       duRlcDb->rlcUeCfgState = UE_CFG_INPROGRESS;
1785
1786       for(dbIdx = 0; (dbIdx < ueCfgDb->numRlcLcs && ret == ROK); dbIdx++)
1787       {
1788          ret = fillDefaultRlcModeCfg(ueCfgDb->rlcLcCfg[dbIdx].rlcBearerCfg.rlcMode, &ueCfgDb->rlcLcCfg[dbIdx].rlcBearerCfg);
1789          if(ret == RFAILED)
1790          {
1791             DU_LOG("\n ERROR  -->  DU APP : Failed to fill Rlc Mode at fillRlcUeCfg()");
1792             memset(rlcUeCfg, 0, sizeof(DuRlcUeCfg));
1793             return ret;
1794          }
1795
1796          if(duRlcDb)
1797          {
1798             /* Fill Rlc Ue Cfg List for ADD/MOD/DEL */
1799             for(lcIdx = 0; lcIdx < duRlcDb->numLcs; lcIdx++)
1800             { 
1801                if(ueCfgDb->rlcLcCfg[dbIdx].rlcBearerCfg.lcId == duRlcDb->rlcLcCfg[lcIdx].rlcBearerCfg.lcId)
1802                {
1803                   lcIdFound = true;
1804                   if((ueCfgDb->rlcLcCfg[dbIdx].configType == CONFIG_UNKNOWN)||
1805                         (ueCfgDb->rlcLcCfg[dbIdx].configType == CONFIG_MOD))
1806                   {
1807                      /* MOD */ 
1808                      ueCfgDb->rlcLcCfg[dbIdx].configType = CONFIG_MOD; /* update Db for MOD type */
1809                      memcpy(&rlcUeCfg->rlcLcCfg[dbIdx], &ueCfgDb->rlcLcCfg[dbIdx], sizeof(DuRlcBearerCfg));
1810                      fillSnssaiInfo(rlcUeCfg->rlcLcCfg[dbIdx].rlcBearerCfg.snssai, ueCfgDb->rlcLcCfg[dbIdx].rlcBearerCfg.snssai,\
1811                            &duRlcDb->rlcLcCfg[lcIdx].rlcBearerCfg.snssai,false);
1812                   }
1813                }
1814                else
1815                   lcIdFound = false;
1816             }
1817          }
1818
1819          if(!lcIdFound)
1820          {
1821             /* ADD/ DEL Config Type */
1822             memcpy(&rlcUeCfg->rlcLcCfg[dbIdx], &ueCfgDb->rlcLcCfg[dbIdx], sizeof(DuRlcBearerCfg));
1823             fillSnssaiInfo(rlcUeCfg->rlcLcCfg[dbIdx].rlcBearerCfg.snssai, ueCfgDb->rlcLcCfg[dbIdx].rlcBearerCfg.snssai, NULL, false);
1824          }
1825          rlcUeCfg->numLcs++;
1826       }
1827    }
1828    return ret;
1829 }
1830
1831 /******************************************************************
1832  *
1833  * @brief creates UE context
1834  *
1835  * @details
1836  *
1837  *    Function : duCreateUeCb
1838  *
1839  *    Functionality: Creates UE Conetxt
1840  *
1841  * @params[in] UeCcchCtxt Pointer
1842  *             UeIdx Pointer
1843  *
1844  * @return ROK     - success
1845  *         RFAILED - failure
1846  * ****************************************************************/
1847 uint8_t duCreateUeCb(UeCcchCtxt *ueCcchCtxt, uint32_t gnbCuUeF1apId)
1848 {
1849    uint8_t cellIdx = 0;
1850    uint8_t ret     = ROK;
1851    uint8_t ueId = 0, ueIdx = 0;
1852
1853    for(cellIdx = 0; cellIdx < MAX_NUM_CELL; cellIdx++)
1854    {
1855       if(duCb.actvCellLst[cellIdx] && (ueCcchCtxt->cellId == duCb.actvCellLst[cellIdx]->cellId))
1856       {
1857          GET_UE_ID(ueCcchCtxt->crnti, ueId);
1858          DU_LOG("\nDEBUG   -->  DU_APP: Filling UeCb for ueId [%d]", ueId);
1859
1860          ueIdx = ueId-1;
1861          duCb.actvCellLst[cellIdx]->ueCb[ueIdx].f1UeDb        = NULLP;
1862          duCb.actvCellLst[cellIdx]->ueCb[ueIdx].crnti         = ueCcchCtxt->crnti;
1863          duCb.actvCellLst[cellIdx]->ueCb[ueIdx].gnbDuUeF1apId = ueCcchCtxt->gnbDuUeF1apId;
1864          duCb.actvCellLst[cellIdx]->ueCb[ueIdx].gnbCuUeF1apId = gnbCuUeF1apId;
1865          duCb.actvCellLst[cellIdx]->ueCb[ueIdx].drbBitMap     = NULLP;
1866          duCb.actvCellLst[cellIdx]->ueCb[ueIdx].ueState       = UE_ACTIVE;
1867
1868          /* Filling Mac Ue Config */ 
1869          memset(&duCb.actvCellLst[cellIdx]->ueCb[ueIdx].duMacUeCfg, 0, sizeof(DuMacUeCfg));
1870          ret = duBuildAndSendUeCreateReqToMac(ueCcchCtxt->cellId, ueCcchCtxt->gnbDuUeF1apId, ueCcchCtxt->crnti, NULL, 
1871                &duCb.actvCellLst[cellIdx]->ueCb[ueIdx].duMacUeCfg);
1872          if(ret == RFAILED)
1873             DU_LOG("\nERROR  -->  DU APP : Failed to send UE create request to MAC");
1874
1875          /* Filling Rlc Ue Config */
1876          memset(&duCb.actvCellLst[cellIdx]->ueCb[ueIdx].duRlcUeCfg, 0, sizeof(DuRlcUeCfg));
1877          ret = duBuildAndSendUeCreateReqToRlc(ueCcchCtxt->cellId, ueCcchCtxt->gnbDuUeF1apId, NULL,
1878                &duCb.actvCellLst[cellIdx]->ueCb[ueIdx].duRlcUeCfg);
1879          if(ret == RFAILED)
1880             DU_LOG("\nERROR  -->  DU APP : Failed to send UE create request to RLC");
1881
1882          duCb.actvCellLst[cellIdx]->numActvUes++;
1883          memset(ueCcchCtxt, 0, sizeof(UeCcchCtxt));
1884          duCb.numUe--;
1885       }
1886    }
1887    return ret;
1888 }
1889
1890 /**********************************************************************************
1891  *
1892  * @brief Fills Ue Cfg from DU DB to MacUeCreateReq
1893  *
1894  * @details
1895  *
1896  *    Function : fillMacUeCfg
1897  *
1898  *    Functionality: fills ue Cfg to be sent to MAC
1899  *
1900  * @params[in] DuMacUeCfg Pointer
1901  *             MacUeCreateReq Pointer
1902  *
1903  * @return ROK     - success
1904  *         RFAILED - failure
1905  * *******************************************************************************/
1906 void fillMacUeCfg(DuMacUeCfg *duMacUeCfg, MacUeCreateReq *macUeCfg)
1907 {
1908    uint8_t lcIdx = 0;
1909
1910    macUeCfg->cellId = duMacUeCfg->cellId;
1911    macUeCfg->ueId = duMacUeCfg->ueId;
1912    macUeCfg->crnti = duMacUeCfg->crnti;
1913    macUeCfg->macCellGrpCfgPres = duMacUeCfg->macCellGrpCfgPres;
1914    if(macUeCfg->macCellGrpCfgPres)
1915    {
1916       memcpy(&macUeCfg->macCellGrpCfg, &duMacUeCfg->macCellGrpCfg, sizeof(MacCellGrpCfg));
1917    }
1918    macUeCfg->phyCellGrpCfgPres = duMacUeCfg->phyCellGrpCfgPres;
1919    if(macUeCfg->phyCellGrpCfgPres)
1920    {
1921       memcpy(&macUeCfg->phyCellGrpCfg, &duMacUeCfg->phyCellGrpCfg, sizeof(PhyCellGrpCfg));
1922    }
1923    macUeCfg->spCellCfgPres = duMacUeCfg->spCellCfgPres;
1924    if(macUeCfg->spCellCfgPres)
1925    {
1926       macUeCfg->spCellCfg.servCellIdx = duMacUeCfg->spCellCfg.servCellIdx;
1927       macUeCfg->spCellCfg.servCellCfg.initDlBwp = duMacUeCfg->spCellCfg.servCellCfg.initDlBwp;
1928       macUeCfg->spCellCfg.servCellCfg.numDlBwpToAdd = duMacUeCfg->spCellCfg.servCellCfg.numDlBwpToAddOrMod;
1929       if(macUeCfg->spCellCfg.servCellCfg.numDlBwpToAdd > 0)
1930       {
1931          memcpy(macUeCfg->spCellCfg.servCellCfg.dlBwpToAddList, duMacUeCfg->spCellCfg.servCellCfg.dlBwpToAddOrModList, \
1932                    (sizeof(DlBwpInfo) * MAX_NUM_BWP));
1933       }
1934       macUeCfg->spCellCfg.servCellCfg.firstActvDlBwpId = duMacUeCfg->spCellCfg.servCellCfg.firstActvDlBwpId;
1935       macUeCfg->spCellCfg.servCellCfg.defaultDlBwpId = duMacUeCfg->spCellCfg.servCellCfg.defaultDlBwpId;
1936       macUeCfg->spCellCfg.servCellCfg.bwpInactivityTmr = duMacUeCfg->spCellCfg.servCellCfg.bwpInactivityTmr;
1937       memcpy(&macUeCfg->spCellCfg.servCellCfg.pdschServCellCfg, &duMacUeCfg->spCellCfg.servCellCfg.pdschServCellCfg, sizeof(PdschServCellCfg));
1938       memcpy(&macUeCfg->spCellCfg.servCellCfg.initUlBwp, &duMacUeCfg->spCellCfg.servCellCfg.initUlBwp, sizeof(InitialUlBwp));
1939       macUeCfg->spCellCfg.servCellCfg.numUlBwpToAdd = duMacUeCfg->spCellCfg.servCellCfg.numUlBwpToAddOrMod;
1940       if(macUeCfg->spCellCfg.servCellCfg.numUlBwpToAdd > 0)
1941       {
1942          memcpy(macUeCfg->spCellCfg.servCellCfg.ulBwpToAddList, duMacUeCfg->spCellCfg.servCellCfg.ulBwpToAddOrModList,\
1943                  (sizeof(UlBwpInfo) * MAX_NUM_BWP));
1944       }
1945       macUeCfg->spCellCfg.servCellCfg.firstActvUlBwpId =  duMacUeCfg->spCellCfg.servCellCfg.firstActvUlBwpId;
1946    }
1947    if(duMacUeCfg->ambrCfg != NULLP)
1948    {
1949       memcpy(macUeCfg->ambrCfg, duMacUeCfg->ambrCfg, sizeof(AmbrCfg));
1950    }
1951    memcpy(&macUeCfg->dlModInfo, &duMacUeCfg->dlModInfo, sizeof(ModulationInfo));
1952    memcpy(&macUeCfg->ulModInfo, &duMacUeCfg->ulModInfo, sizeof(ModulationInfo));
1953    if(duMacUeCfg->numLcs > 0)
1954    {
1955       macUeCfg->numLcs = 0;
1956       for(lcIdx = 0; lcIdx < duMacUeCfg->numLcs; lcIdx++)
1957       {
1958          if(duMacUeCfg->lcCfgList[lcIdx].configType == CONFIG_ADD)
1959          {
1960             //VS: To thoroughly check
1961             memcpy(&macUeCfg->lcCfgList[macUeCfg->numLcs], &duMacUeCfg->lcCfgList[lcIdx].lcConfig, sizeof(LcCfg));
1962             macUeCfg->numLcs++;
1963          }
1964       }
1965    }
1966 }
1967
1968 /**********************************************************************************
1969  *
1970  * @brief Fills Ue Cfg from DU DB to RLCUeCfg
1971  *
1972  * @details
1973  *
1974  *    Function : fillRlcUeCfg
1975  *
1976  *    Functionality: fills ue Cfg to be sent to RLC
1977  *
1978  * @params[in] DuRlcUeCfg Pointer
1979  *             RlcUeCreate Pointer
1980  *
1981  * @return ROK     - success
1982  *         RFAILED - failure
1983  * *******************************************************************************/
1984 void fillRlcUeCfg(DuRlcUeCfg *duRlcUeCfg, RlcUeCreate *rlcUeCfg)
1985 {
1986    uint8_t lcIdx = 0;
1987
1988    rlcUeCfg->cellId = duRlcUeCfg->cellId;
1989    rlcUeCfg->ueId   = duRlcUeCfg->ueId;
1990    
1991    rlcUeCfg->numLcsToAdd = 0;
1992    for(lcIdx = 0; lcIdx < duRlcUeCfg->numLcs; lcIdx++)
1993    {
1994       if(duRlcUeCfg->rlcLcCfg[lcIdx].configType == CONFIG_ADD)
1995       {
1996          memcpy(&rlcUeCfg->rlcLcCfgAdd[rlcUeCfg->numLcsToAdd], &duRlcUeCfg->rlcLcCfg[lcIdx].rlcBearerCfg, sizeof(RlcBearerCfg));
1997          rlcUeCfg->numLcsToAdd++;
1998       }
1999    }
2000    return;
2001 }
2002
2003 /******************************************************************
2004  *
2005  * @brief Builds and Send UE Create Request to MAC
2006  *
2007  * @details
2008  *
2009  *    Function : duBuildAndSendUeCreateReqToMac
2010  *
2011  *    Functionality: Builds and Send UE Create Request to MAC
2012  *
2013  * @Params[in]  cellId,
2014  *              ueId,
2015  *              crnti,
2016  *              UE config extracted from F1AP msg
2017  *              MAC UE config struct to be filled
2018  * @return ROK     - success
2019  *         RFAILED - failure
2020  *
2021  * ****************************************************************/
2022
2023 uint8_t duBuildAndSendUeCreateReqToMac(uint16_t cellId, uint8_t gnbDuUeF1apId, uint16_t crnti, DuUeCfg *ueCfgDb, DuMacUeCfg *duMacUeCfg)
2024 {
2025    uint8_t  ret = ROK;
2026    MacUeCreateReq *macUeCfg = NULLP;
2027    Pst       pst;
2028    memset(&pst, 0, sizeof(Pst));
2029
2030
2031    ret = updateDuMacUeCfg(cellId, gnbDuUeF1apId, crnti, ueCfgDb, duMacUeCfg);
2032    if(ret == RFAILED)
2033    {
2034       DU_LOG("\nERROR  -->  DU APP : Failed to fill MacUeCfg at duBuildAndSendUeCreateReqToMac()");
2035       return RFAILED;
2036    }
2037
2038    /* Fill Pst */
2039    FILL_PST_DUAPP_TO_MAC(pst, EVENT_MAC_UE_CREATE_REQ);
2040
2041    /* Copying ueCb to a sharable buffer */
2042    DU_ALLOC_SHRABL_BUF(macUeCfg, sizeof(MacUeCreateReq));
2043    if(macUeCfg)
2044    {
2045       memset(macUeCfg, 0, sizeof(MacUeCreateReq));
2046       fillMacUeCfg(duMacUeCfg, macUeCfg); 
2047       DU_LOG("\nDEBUG   -->  DU_APP: Sending UE create request to MAC");
2048
2049       /* Processing one Ue at a time to MAC */
2050       ret = (*packMacUeCreateReqOpts[pst.selector])(&pst, macUeCfg);
2051       if(ret == RFAILED)
2052       {
2053          DU_LOG("\nERROR  -->  DU_APP : Failure in sending Ue Create Req to MAC at duBuildAndSendUeCreateReqToMac()");
2054          DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, macUeCfg, sizeof(MacUeCreateReq));
2055       }
2056    }
2057    else
2058    {
2059       DU_LOG("\n ERROR  -->  DU APP : Memory alloc failed at duBuildAndSendUeCreateReqToMac()");
2060       ret = RFAILED;
2061    }
2062    return ret;
2063 }
2064
2065 /*******************************************************************
2066  *
2067  * @brief Build and send RACH Resource request to MAC
2068  *
2069  * @details
2070  *
2071  *    Function : duBuildAndSendRachRsrcReqToMac
2072  *    Functionality:
2073  *        Build and send RACH Resource request to MAC
2074  *
2075  * @params[in] Cell Id
2076  *             UE Id
2077  * @return ROK     - success
2078  *         RFAILED - failure
2079  *
2080  * ****************************************************************/
2081 uint8_t duBuildAndSendRachRsrcReqToMac(uint16_t cellId, uint16_t ueId)
2082 {
2083    uint16_t cellIdx = 0, ssbIdx = 0;
2084    Pst pst;
2085    MacRachRsrcReq *rachRsrcReq = NULLP;
2086
2087    GET_CELL_IDX(cellId, cellIdx);
2088    if(duCb.actvCellLst[cellIdx] == NULLP)
2089    {
2090       DU_LOG("\nERROR  -->  DU APP : Cell Id [%d] not found in duBuildAndSendRachRsrcReqToMac()", cellId);
2091       return RFAILED;
2092    }
2093
2094    DU_ALLOC_SHRABL_BUF(rachRsrcReq, sizeof(MacRachRsrcReq));
2095    if(!rachRsrcReq)
2096    {
2097       DU_LOG("\nERROR  -->  DU APP : Failed to allocate memory for RACH Resource Request in \
2098             duBuildAndSendRachRsrcReqToMac()");
2099       return RFAILED;
2100    }
2101
2102    rachRsrcReq->cellId = cellId;
2103    rachRsrcReq->ueId = ueId;
2104    rachRsrcReq->numSsb = duCfgParam.macCellCfg.prachCfg.ssbPerRach;
2105    for(ssbIdx = 0; ssbIdx < rachRsrcReq->numSsb; ssbIdx++)
2106    {
2107       rachRsrcReq->ssbIdx[ssbIdx] = ssbIdx;
2108    }
2109
2110    /* Fill Pst */
2111    FILL_PST_DUAPP_TO_MAC(pst, EVENT_MAC_RACH_RESOURCE_REQ);
2112    
2113    if(((*packMacRachRsrcReqOpts[pst.selector])(&pst, rachRsrcReq)) != ROK)
2114    {
2115       DU_LOG("\nERROR  -->  DU_APP : Failure in sending RACH Resource Request to MAC at \
2116             duBuildAndSendRachRsrcReqToMac()");
2117       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, rachRsrcReq, sizeof(MacRachRsrcReq));
2118       return RFAILED;
2119    }
2120
2121    return ROK;
2122 }
2123
2124 /*******************************************************************
2125  *
2126  * @brief Process RACH resource response from MAC
2127  *
2128  * @details
2129  *
2130  *    Function : DuProcMacRachRsrcRsp
2131  *    Functionality:
2132  *        Process RACH resource response from MAC
2133  *
2134  * @params[in] Post structure
2135  *             RACH resource response
2136  * @return ROK     - success
2137  *         RFAILED - failure
2138  *
2139  * ****************************************************************/
2140 uint8_t DuProcMacRachRsrcRsp(Pst *pst, MacRachRsrcRsp *rachRsrcRsp)
2141 {
2142    uint8_t  ret = RFAILED;
2143    uint16_t cellIdx = 0;
2144    DuCellCb *cellCb = NULLP;
2145    DuUeCb   *ueCb = NULLP;
2146
2147    DU_LOG("\nINFO  -->  DU APP : Received RACH Resource Response from MAC. Cell ID [%d] UE ID [%d]",
2148          rachRsrcRsp->cellId, rachRsrcRsp->ueId);
2149
2150    if(rachRsrcRsp->result == MAC_DU_APP_RSP_OK)
2151    {
2152       DU_LOG("\nINFO : DU APP : RACH Resource Response from MAC : Result [SUCCESS]");
2153
2154       /* Fetch Cell Cb */
2155       GET_CELL_IDX(rachRsrcRsp->cellId, cellIdx);
2156       if(duCb.actvCellLst[cellIdx] && (duCb.actvCellLst[cellIdx]->cellId == rachRsrcRsp->cellId))
2157       {
2158          cellCb = duCb.actvCellLst[cellIdx];
2159
2160          /* Fetch UE CB */
2161          if(cellCb->ueCb[rachRsrcRsp->ueId-1].gnbDuUeF1apId == rachRsrcRsp->ueId)
2162          {
2163             ueCb = &cellCb->ueCb[rachRsrcRsp->ueId-1];
2164
2165             /* Store CRNTI alloted to this UE by MAC */
2166             ueCb->crnti = rachRsrcRsp->newCrnti;
2167
2168             /* Store the assigned CF-RA resources */
2169             memcpy(&ueCb->cfraResource, &rachRsrcRsp->cfraResource, sizeof(MacCfraResource));
2170
2171             /* RACH resources allocated to UE is sent to CU in UE Context Setup Response
2172              * along with the result of UE Context setup requested by CU */
2173             if((ret = BuildAndSendUeCtxtRsp(rachRsrcRsp->cellId, rachRsrcRsp->ueId)) != ROK)
2174                DU_LOG("\nERROR  ->  DU APP : Failure in BuildAndSendUeCtxtRsp()");
2175          }
2176          else
2177             DU_LOG("\nERROR  -->  DU APP : UE ID [%d] not found in DuProcMacRachRsrcRsp", rachRsrcRsp->ueId);
2178       }
2179       else
2180          DU_LOG("\nERROR  -->  DU APP : Cell ID [%d] not found in DuProcMacRachRsrcRsp", rachRsrcRsp->cellId);
2181    }
2182    else
2183       DU_LOG("\nINFO : DU APP : RACH Resource Response from MAC : Result [FAILURE]");
2184
2185    DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, rachRsrcRsp, sizeof(MacRachRsrcRsp));
2186    return ret;
2187 }
2188
2189 /*******************************************************************
2190  *
2191  * @brief  fill k0 value in duCb
2192  *
2193  * @details
2194  *
2195  *    Function : fillK0Values
2196  *    Functionality:  update k0 value in duCb 
2197  *
2198  * @params[in] PdschConfig *cuPdschCfg, PdschConfig *storePdschCfg 
2199  * @return ROK     - success
2200  *         RFAILED - failure
2201  *
2202  * ****************************************************************/
2203
2204 uint8_t fillK0Values(PdschConfig *cuPdschCfg, PdschConfig *storePdschCfg)
2205 {
2206    uint8_t numTimeDomRsrcAlloc, rsrcIdx;
2207
2208    if(cuPdschCfg)
2209    {
2210       if(storePdschCfg->numTimeDomRsrcAlloc)
2211       {
2212          numTimeDomRsrcAlloc = cuPdschCfg->numTimeDomRsrcAlloc;
2213          for(rsrcIdx =0 ; rsrcIdx<numTimeDomRsrcAlloc; rsrcIdx++)
2214          {
2215             if(cuPdschCfg->timeDomRsrcAllociList[rsrcIdx].k0)
2216             {
2217                if(storePdschCfg->timeDomRsrcAllociList[rsrcIdx].k0)
2218                {
2219                   *(storePdschCfg->timeDomRsrcAllociList[rsrcIdx].k0) = *(cuPdschCfg->timeDomRsrcAllociList[rsrcIdx].k0);
2220                   if(storePdschCfg->timeDomRsrcAllociList[rsrcIdx].k0 != cuPdschCfg->timeDomRsrcAllociList[rsrcIdx].k0)
2221                      DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cuPdschCfg->timeDomRsrcAllociList[rsrcIdx].k0, sizeof(uint8_t));
2222                }
2223                else
2224                {
2225                   (storePdschCfg->timeDomRsrcAllociList[rsrcIdx].k0) = (cuPdschCfg->timeDomRsrcAllociList[rsrcIdx].k0);
2226                }
2227             }
2228          }
2229       }
2230    }
2231    return ROK;
2232 }
2233
2234 /*******************************************************************
2235  *
2236  * @brief To update DuUeCb Mac Cfg
2237  *
2238  * @details
2239  *
2240  *    Function : duUpdateMacCfg
2241  *    Functionality:  update DuUeCb MAC Cfg
2242  *
2243  * @params[in] DuUeCb Pointer
2244  *             F1UeContextSetupDb pointer 
2245  * @return ROK     - success
2246  *         RFAILED - failure
2247  *
2248  * ****************************************************************/
2249 uint8_t duUpdateMacCfg(DuMacUeCfg *macUeCfg, F1UeContextSetupDb *f1UeDb) 
2250 {
2251    uint8_t ret, lcIdx, dbIdx, numLcs, lcDelIdx, cellIdx;
2252    DuMacUeCfg *oldMacUeCfg;
2253    ret = ROK;
2254
2255    GET_CELL_IDX(macUeCfg->cellId, cellIdx);
2256    if(duCb.actvCellLst[cellIdx] == NULLP)
2257    {
2258       DU_LOG("\nERROR  --> DU APP: CellId[%d] not found", macUeCfg->cellId);
2259       return RFAILED;
2260    }
2261    oldMacUeCfg = &duCb.actvCellLst[cellIdx]->ueCb[macUeCfg->ueId-1].duMacUeCfg;
2262
2263    /*Filling Cell Group Cfg*/
2264    ret =  procUeRecfgCellInfo(macUeCfg, &f1UeDb->duUeCfg.copyOfmacUeCfg, f1UeDb->duUeCfg.cellGrpCfg);
2265 #ifdef NR_DRX
2266    memcpy(&macUeCfg->macCellGrpCfg.drxCfg, &f1UeDb->duUeCfg.copyOfmacUeCfg.macCellGrpCfg.drxCfg, sizeof(DrxCfg));
2267 #endif
2268    if(ret == ROK)
2269    {
2270       if(macUeCfg->spCellCfg.servCellCfg.initDlBwp.pdschPresent)
2271       {
2272          /* update k0 values */
2273          fillK0Values(&f1UeDb->duUeCfg.copyOfmacUeCfg.spCellCfg.servCellCfg.initDlBwp.pdschCfg, &macUeCfg->spCellCfg.servCellCfg.initDlBwp.pdschCfg); 
2274          fillStartSymbolAndLen(macUeCfg->spCellCfg.servCellCfg.initDlBwp.pdschCfg.numTimeDomRsrcAlloc,\
2275                &macUeCfg->spCellCfg.servCellCfg.initDlBwp.pdschCfg, NULL);
2276       }
2277       if(macUeCfg->spCellCfg.servCellCfg.initUlBwp.puschPresent)
2278       {
2279          fillStartSymbolAndLen(macUeCfg->spCellCfg.servCellCfg.initUlBwp.puschCfg.numTimeDomRsrcAlloc,\
2280                NULL, &macUeCfg->spCellCfg.servCellCfg.initUlBwp.puschCfg);
2281       }
2282       ret = fillAmbr(NULL, f1UeDb->duUeCfg.ambrCfg, &oldMacUeCfg->ambrCfg, true);
2283       duFillModulationDetails(macUeCfg, oldMacUeCfg, f1UeDb->duUeCfg.ueNrCapability);
2284    }
2285
2286    /* Filling LC Context */
2287    for(dbIdx = 0; (dbIdx < f1UeDb->duUeCfg.numMacLcs && ret == ROK); dbIdx++)
2288    {
2289       numLcs = macUeCfg->numLcs;
2290       for(lcIdx = 0; lcIdx < numLcs; lcIdx++)
2291       {
2292          if(f1UeDb->duUeCfg.macLcCfg[dbIdx].lcConfig.lcId ==  macUeCfg->lcCfgList[lcIdx].lcConfig.lcId)
2293          {
2294             if(f1UeDb->duUeCfg.macLcCfg[dbIdx].configType == CONFIG_MOD)
2295             {
2296                ret = fillMacLcCfgToAddMod(NULL, &f1UeDb->duUeCfg.macLcCfg[dbIdx], &macUeCfg->lcCfgList[lcIdx], true);
2297             }
2298             else if(f1UeDb->duUeCfg.macLcCfg[dbIdx].configType == CONFIG_DEL)
2299             {
2300                /* Free memory at matched  lcIdx index */
2301                freeMacLcCfg(&macUeCfg->lcCfgList[lcIdx].lcConfig);
2302                macUeCfg->numLcs--;
2303                for(lcDelIdx = lcIdx; lcDelIdx < macUeCfg->numLcs; lcDelIdx++)
2304                {
2305                   /* moving all elements one index ahead */
2306                   ret = fillMacLcCfgToAddMod(NULL,  &macUeCfg->lcCfgList[lcDelIdx+1], &macUeCfg->lcCfgList[lcDelIdx], true);
2307                   freeMacLcCfg(&macUeCfg->lcCfgList[lcDelIdx+1].lcConfig);
2308                   if(ret == RFAILED)
2309                   {
2310                      DU_LOG("\nERROR  -->  DU APP : Failed to delete LC at Idx %d in duUpdateMacCfg()", lcDelIdx);
2311                      break;
2312                   }
2313                }
2314             }
2315          }
2316       } 
2317       if(f1UeDb->duUeCfg.macLcCfg[dbIdx].configType == CONFIG_ADD)
2318       {
2319          ret = fillMacLcCfgToAddMod(NULL, &f1UeDb->duUeCfg.macLcCfg[dbIdx], &macUeCfg->lcCfgList[numLcs], true);
2320          if(ret == RFAILED)
2321          {
2322             DU_LOG("\nERROR  -->  DU APP : Failed to add LC at Idx %d in duUpdateMacCfg()", numLcs);
2323             break;
2324          }
2325          macUeCfg->numLcs++;
2326       }
2327
2328    }
2329    return ret;
2330 }
2331
2332 /******************************************************************
2333  *
2334  * @brief Function to fill the RLC Lc cfg from ueSetupReqDb
2335  *
2336  * @details
2337  *
2338  *    Function : fillRlcCfgToAddMod
2339  *
2340  *    Functionality: Function to fill the RLC Lc cfg from ueSetupReqDb
2341  *
2342  *
2343  *****************************************************************/
2344
2345 uint8_t fillRlcCfgToAddMod(DuRlcBearerCfg *lcCfg, DuRlcBearerCfg *f1UeDbLcCfg)
2346 {
2347    lcCfg->configType = f1UeDbLcCfg->configType;
2348    lcCfg->rlcBearerCfg.rbId       = f1UeDbLcCfg->rlcBearerCfg.rbId;
2349    lcCfg->rlcBearerCfg.rbType     = f1UeDbLcCfg->rlcBearerCfg.rbType;
2350    lcCfg->rlcBearerCfg.lcId       = f1UeDbLcCfg->rlcBearerCfg.lcId;
2351    lcCfg->rlcBearerCfg.lcType     = f1UeDbLcCfg->rlcBearerCfg.lcType;
2352    lcCfg->rlcBearerCfg.rlcMode    = f1UeDbLcCfg->rlcBearerCfg.rlcMode;
2353    
2354    switch(lcCfg->rlcBearerCfg.rlcMode)
2355    {
2356       case RLC_AM :
2357          {
2358             if(!lcCfg->rlcBearerCfg.u.amCfg)
2359             {
2360                DU_ALLOC_SHRABL_BUF(lcCfg->rlcBearerCfg.u.amCfg, sizeof(AmBearerCfg));
2361                if(!lcCfg->rlcBearerCfg.u.amCfg)
2362                   return RFAILED;
2363             }
2364             /* DL AM */
2365             lcCfg->rlcBearerCfg.u.amCfg->dlAmCfg.snLenDl     = f1UeDbLcCfg->rlcBearerCfg.u.amCfg->dlAmCfg.snLenDl;    
2366             lcCfg->rlcBearerCfg.u.amCfg->dlAmCfg.pollRetxTmr = f1UeDbLcCfg->rlcBearerCfg.u.amCfg->dlAmCfg.pollRetxTmr;
2367             lcCfg->rlcBearerCfg.u.amCfg->dlAmCfg.pollPdu     = f1UeDbLcCfg->rlcBearerCfg.u.amCfg->dlAmCfg.pollPdu;
2368             lcCfg->rlcBearerCfg.u.amCfg->dlAmCfg.pollByte    = f1UeDbLcCfg->rlcBearerCfg.u.amCfg->dlAmCfg.pollByte;   
2369             lcCfg->rlcBearerCfg.u.amCfg->dlAmCfg.maxRetxTh   = f1UeDbLcCfg->rlcBearerCfg.u.amCfg->dlAmCfg.maxRetxTh;   
2370
2371             /* UL AM */
2372             lcCfg->rlcBearerCfg.u.amCfg->ulAmCfg.snLenUl     = f1UeDbLcCfg->rlcBearerCfg.u.amCfg->ulAmCfg.snLenUl;
2373             lcCfg->rlcBearerCfg.u.amCfg->ulAmCfg.reAssemTmr  = f1UeDbLcCfg->rlcBearerCfg.u.amCfg->ulAmCfg.reAssemTmr; 
2374             lcCfg->rlcBearerCfg.u.amCfg->ulAmCfg.statProhTmr = f1UeDbLcCfg->rlcBearerCfg.u.amCfg->ulAmCfg.statProhTmr;
2375             break;
2376          }
2377       case RLC_UM_BI_DIRECTIONAL :
2378          {
2379             if(!lcCfg->rlcBearerCfg.u.umBiDirCfg)
2380             {
2381                DU_ALLOC_SHRABL_BUF(lcCfg->rlcBearerCfg.u.umBiDirCfg, sizeof(UmBiDirBearerCfg));
2382                if(!lcCfg->rlcBearerCfg.u.umBiDirCfg)
2383                   return RFAILED;
2384             }
2385             /* UL UM BI DIR INFO */
2386             lcCfg->rlcBearerCfg.u.umBiDirCfg->ulUmCfg.snLenUlUm  = f1UeDbLcCfg->rlcBearerCfg.u.umBiDirCfg->ulUmCfg.snLenUlUm;  
2387             lcCfg->rlcBearerCfg.u.umBiDirCfg->ulUmCfg.reAssemTmr = f1UeDbLcCfg->rlcBearerCfg.u.umBiDirCfg->ulUmCfg.reAssemTmr;
2388             /* DL UM BI DIR INFO */
2389             lcCfg->rlcBearerCfg.u.umBiDirCfg->dlUmCfg.snLenDlUm  = f1UeDbLcCfg->rlcBearerCfg.u.umBiDirCfg->dlUmCfg.snLenDlUm;
2390             break;
2391          }
2392       case RLC_UM_UNI_DIRECTIONAL_UL :
2393          {
2394             if(!lcCfg->rlcBearerCfg.u.umUniDirUlCfg)
2395             {
2396                DU_ALLOC_SHRABL_BUF(lcCfg->rlcBearerCfg.u.umUniDirUlCfg, sizeof(UmUniDirUlBearerCfg));
2397                if(!lcCfg->rlcBearerCfg.u.umUniDirUlCfg)
2398                   return RFAILED;
2399             }
2400             lcCfg->rlcBearerCfg.u.umUniDirUlCfg->ulUmCfg.snLenUlUm  = f1UeDbLcCfg->rlcBearerCfg.u.umUniDirUlCfg->ulUmCfg.snLenUlUm;  
2401             lcCfg->rlcBearerCfg.u.umUniDirUlCfg->ulUmCfg.reAssemTmr = f1UeDbLcCfg->rlcBearerCfg.u.umUniDirUlCfg->ulUmCfg.reAssemTmr;
2402             break;
2403
2404          }
2405       case RLC_UM_UNI_DIRECTIONAL_DL :
2406          {
2407             if(!lcCfg->rlcBearerCfg.u.umUniDirDlCfg)
2408             {
2409                DU_ALLOC_SHRABL_BUF(lcCfg->rlcBearerCfg.u.umUniDirDlCfg, sizeof(UmUniDirDlBearerCfg));
2410                if(!lcCfg->rlcBearerCfg.u.umUniDirDlCfg)
2411                   return RFAILED;
2412             }
2413             lcCfg->rlcBearerCfg.u.umUniDirDlCfg->dlUmCfg.snLenDlUm  = f1UeDbLcCfg->rlcBearerCfg.u.umUniDirDlCfg->dlUmCfg.snLenDlUm;
2414             break;
2415          }
2416       default:
2417          DU_LOG("\nERROR  -->  DU_APP: Invalid Rlc Mode %d at fillRlcCfgToAddMod()", lcCfg->rlcBearerCfg.rlcMode);
2418          return RFAILED;
2419    }
2420    return ROK;
2421 }
2422
2423 /*******************************************************************
2424  *
2425  * @brief To update DuUeCb Rlc Lc Cfg
2426  *
2427  * @details
2428  *
2429  *    Function : duUpdateRlcLcCfg
2430  *    Functionality:  update DuUeCb Rlc Lc Cfg
2431  *
2432  * @params[in] DuUeCb Pointer
2433  *             F1UeContextSetupDb pointer 
2434  * @return ROK     - success
2435  *         RFAILED - failure
2436  *
2437  * ****************************************************************/
2438
2439 uint8_t duUpdateRlcLcCfg(DuRlcUeCfg *rlcUeCfg, F1UeContextSetupDb *f1UeDb)
2440 {
2441    uint8_t ret, lcIdx, dbIdx, numLcs, lcDelIdx;
2442
2443    ret = ROK;
2444    for(dbIdx = 0; (dbIdx < f1UeDb->duUeCfg.numRlcLcs && ret ==ROK); dbIdx++)
2445    {
2446       numLcs = rlcUeCfg->numLcs;
2447       for(lcIdx = 0; lcIdx < numLcs; lcIdx++)
2448       {
2449          if(f1UeDb->duUeCfg.rlcLcCfg[dbIdx].rlcBearerCfg.lcId == rlcUeCfg->rlcLcCfg[lcIdx].rlcBearerCfg.lcId)
2450          {
2451             if(f1UeDb->duUeCfg.rlcLcCfg[dbIdx].configType == CONFIG_MOD)
2452             {
2453                ret = fillRlcCfgToAddMod(&rlcUeCfg->rlcLcCfg[lcIdx], &f1UeDb->duUeCfg.rlcLcCfg[dbIdx]);
2454                if(ret == RFAILED)
2455                {
2456                   DU_LOG("\nERROR  -->  DU APP : Failed to modify LC at Idx %d in duUpdateRlcCfg()", lcDelIdx);
2457                   break;
2458                }
2459                fillSnssaiInfo(NULL, f1UeDb->duUeCfg.rlcLcCfg[dbIdx].rlcBearerCfg.snssai,\
2460                                   &rlcUeCfg->rlcLcCfg[lcIdx].rlcBearerCfg.snssai, true);
2461             }
2462             else if(f1UeDb->duUeCfg.rlcLcCfg[dbIdx].configType == CONFIG_DEL)
2463             {
2464                /* Free memory at matched lcIdx index */
2465                freeRlcLcCfg(&rlcUeCfg->rlcLcCfg[lcIdx].rlcBearerCfg);
2466                rlcUeCfg->numLcs--;
2467                for(lcDelIdx = lcIdx; lcDelIdx < rlcUeCfg->numLcs; lcDelIdx++)
2468                {
2469                   ret = fillRlcCfgToAddMod(&rlcUeCfg->rlcLcCfg[lcDelIdx], &rlcUeCfg->rlcLcCfg[lcDelIdx+1]);
2470                   fillSnssaiInfo(NULL, rlcUeCfg->rlcLcCfg[lcDelIdx+1].rlcBearerCfg.snssai,\
2471                                    &rlcUeCfg->rlcLcCfg[lcDelIdx].rlcBearerCfg.snssai, true);
2472                   freeRlcLcCfg(&rlcUeCfg->rlcLcCfg[lcDelIdx+1].rlcBearerCfg);
2473                   if(ret == RFAILED)
2474                   {
2475                      DU_LOG("\nERROR  -->  DU APP : Failed to delete LC at Idx %d in duUpdateRlcCfg()", lcDelIdx);
2476                      break;
2477                   }
2478                }
2479             }
2480          }
2481       }
2482       if(f1UeDb->duUeCfg.rlcLcCfg[dbIdx].configType == CONFIG_ADD)
2483       {
2484          ret = fillRlcCfgToAddMod(&rlcUeCfg->rlcLcCfg[lcIdx], &f1UeDb->duUeCfg.rlcLcCfg[dbIdx]);
2485          if(ret == ROK)
2486          {    
2487             fillSnssaiInfo(NULL, f1UeDb->duUeCfg.rlcLcCfg[dbIdx].rlcBearerCfg.snssai,\
2488                               &rlcUeCfg->rlcLcCfg[rlcUeCfg->numLcs].rlcBearerCfg.snssai, true);
2489             rlcUeCfg->numLcs++;
2490          }
2491       }
2492    }
2493    return ret;
2494 }
2495
2496 /*******************************************************************
2497  *
2498  * @brief Function to fill Tunnel Config to Add/Mod
2499  * 
2500  *
2501  * @details
2502  *
2503  *    Function : fillTnlCfgToAddMod
2504  *
2505  *    Functionality: Function to fill tunnel Config to Add/Mod
2506  *
2507  * @params[in] Pointer to tnlCfgDb,
2508  *             pointer to f1TnlCfg
2509  * @return ROK     - success
2510  *         RFAILED - failure
2511  *
2512  * ****************************************************************/
2513 uint8_t fillTnlCfgToAddMod(UpTnlCfg **ueCbTnlCfg, UpTnlCfg *f1TnlCfg)
2514 {
2515    if(*ueCbTnlCfg)
2516    {
2517       DU_FREE((*ueCbTnlCfg)->tnlCfg1, sizeof(GtpTnlCfg));
2518       DU_FREE(*ueCbTnlCfg, sizeof(UpTnlCfg));
2519    }
2520
2521    if(*ueCbTnlCfg == NULLP)
2522    {
2523       /* copying to DuCb Tnl Cfg */
2524       DU_ALLOC(*ueCbTnlCfg, sizeof(UpTnlCfg));
2525       if(*ueCbTnlCfg == NULLP)
2526       {
2527          DU_LOG("\nERROR  -->  DU_APP : fillTnlCfgToAddMod: Memory Alloc failed for drbId[%d]", f1TnlCfg->drbId);
2528          return RFAILED;
2529       }
2530    }
2531    memset(*ueCbTnlCfg, 0, sizeof(UpTnlCfg));
2532    (*ueCbTnlCfg)->configType = f1TnlCfg->configType;
2533    (*ueCbTnlCfg)->cellId    = f1TnlCfg->cellId;
2534    (*ueCbTnlCfg)->ueId      = f1TnlCfg->ueId;
2535    (*ueCbTnlCfg)->drbId     = f1TnlCfg->drbId;
2536    if(f1TnlCfg->tnlCfg1)
2537    {
2538       if((*ueCbTnlCfg)->tnlCfg1 == NULLP)
2539       {
2540          DU_ALLOC((*ueCbTnlCfg)->tnlCfg1, sizeof(GtpTnlCfg));
2541          if((*ueCbTnlCfg)->tnlCfg1 == NULLP)
2542          {
2543             DU_LOG("\nERROR  -->  DU_APP : fillTnlCfgToAddMod: Memory Alloc failed for tnlCfg1 for drbId[%d]", f1TnlCfg->drbId);
2544             return RFAILED;
2545          }
2546       }
2547       memset((*ueCbTnlCfg)->tnlCfg1, 0, sizeof(GtpTnlCfg));
2548       (*ueCbTnlCfg)->tnlCfg1->teId = f1TnlCfg->tnlCfg1->teId;
2549       (*ueCbTnlCfg)->tnlCfg1->ulTnlAddress = f1TnlCfg->tnlCfg1->ulTnlAddress;
2550       (*ueCbTnlCfg)->tnlCfg1->dlTnlAddress = f1TnlCfg->tnlCfg1->dlTnlAddress;
2551    }
2552    return ROK;
2553 }
2554
2555 /*******************************************************************
2556  *
2557  * @brief Processing the tunnel Request to EGTP
2558  *        
2559  * @details
2560  *
2561  *    Function : duProcEgtpTunnelCfg
2562  *
2563  *    Functionality: Processing the tunnel Request to EGTP
2564  *
2565  * @params[in] UptnlCfg *duTnlCfg, UpTnlCfg *f1TnlCfg 
2566  * @return ROK     - success
2567  *         RFAILED - failure
2568  *
2569  * ****************************************************************/
2570
2571 uint8_t duProcEgtpTunnelCfg(uint8_t ueCbIdx, UpTnlCfg *duTnlCfg, UpTnlCfg *f1TnlCfg)
2572 {
2573    uint8_t ret = RFAILED, delIdx;
2574
2575    if(f1TnlCfg->tnlCfg1 == NULLP)
2576    {
2577       DU_LOG("\nERROR  -->  DU_APP : Tunnel config not found");
2578       return ret;
2579    }
2580
2581    if(f1TnlCfg->configType == CONFIG_ADD)
2582    {
2583       if(duSendEgtpTnlMgmtReq(EGTP_TNL_MGMT_ADD, NULLP, f1TnlCfg->tnlCfg1) == ROK)
2584       {
2585          if(fillTnlCfgToAddMod(&duCb.upTnlCfg[duCb.numTeId], f1TnlCfg) == ROK)
2586          {
2587             duCb.numTeId++;
2588             ret = ROK;
2589          }
2590       }      
2591    }
2592    else if(f1TnlCfg->configType == CONFIG_MOD)
2593    {
2594       if(duSendEgtpTnlMgmtReq(EGTP_TNL_MGMT_MOD, duTnlCfg->tnlCfg1->teId, f1TnlCfg->tnlCfg1) == ROK)
2595       {
2596          if(fillTnlCfgToAddMod(&duTnlCfg, f1TnlCfg) == ROK)
2597          {
2598             ret = ROK;
2599          }
2600       }   
2601    }
2602    else if(f1TnlCfg->configType == CONFIG_DEL)
2603    {
2604       if(duSendEgtpTnlMgmtReq(EGTP_TNL_MGMT_DEL, duTnlCfg->tnlCfg1->teId, f1TnlCfg->tnlCfg1) == ROK)
2605       {    
2606          /* Free memory at drbIdx */
2607          duCb.numTeId--;
2608          for(delIdx = ueCbIdx; delIdx < duCb.numTeId; delIdx++)
2609          {
2610             /* moving all elements one index ahead */
2611             ret = fillTnlCfgToAddMod(&duCb.upTnlCfg[delIdx], duCb.upTnlCfg[delIdx+1]);
2612             if(ret != ROK)
2613             {
2614                return ret;
2615             }
2616          }
2617          if(duCb.upTnlCfg[delIdx])
2618          {
2619             DU_FREE(duCb.upTnlCfg[delIdx]->tnlCfg1, sizeof(GtpTnlCfg));
2620             DU_FREE(duCb.upTnlCfg[delIdx], sizeof(UpTnlCfg));
2621          }
2622       }   
2623    }
2624    return ret;
2625 }
2626
2627 /***********************************************************************
2628  *
2629  * @brief Function to fill Tunnel Config
2630  *        and sends tunnel Req to EGTP
2631  * 
2632  *
2633  * @details
2634  *
2635  *    Function : duUpdateTunnelCfgDb
2636  *
2637  *    Functionality: Function to fill tunnel Config
2638  *                   and sends tunnel Cfg Req to EGTP
2639  *
2640  * @params[in] ueId, cellId, DuUeCfg 
2641  * @return ROK     - success
2642  *         RFAILED - failure
2643  *
2644  * ****************************************************************/
2645
2646 uint8_t duUpdateTunnelCfgDb(uint8_t ueId, uint8_t cellId, DuUeCfg *duUeCfg)
2647 {
2648    uint8_t ret = ROK, drbIdx, teIdx;
2649    bool drbFound = false;
2650
2651    /*If Add/Mod tunnels request for that DRB is successful in EGTP */
2652    /*then update drbId and tunnel Info in duCb */
2653    for(drbIdx=0; drbIdx < duUeCfg->numDrb; drbIdx++)
2654    {
2655       duUeCfg->upTnlInfo[drbIdx].cellId = cellId;
2656       duUeCfg->upTnlInfo[drbIdx].ueId = ueId;
2657       for(teIdx = 0; teIdx < duCb.numTeId; teIdx++)
2658       {
2659          if((duCb.upTnlCfg[teIdx]->ueId == duUeCfg->upTnlInfo[drbIdx].ueId) && \
2660             (duCb.upTnlCfg[teIdx]->drbId == duUeCfg->upTnlInfo[drbIdx].drbId))
2661          {
2662             drbFound = true; /* existing DRB */
2663             if(duProcEgtpTunnelCfg(teIdx, duCb.upTnlCfg[teIdx], &duUeCfg->upTnlInfo[drbIdx]) != ROK)
2664             {
2665                DU_LOG("\nERROR  -> DU_APP : duUpdateTunnelCfgDb: Failed to modify tunnel req for Drb id[%d]",
2666                      duUeCfg->upTnlInfo[drbIdx].drbId);
2667                ret = RFAILED;
2668             }
2669             break;
2670          }
2671          else
2672             drbFound = false;
2673       }
2674
2675       if(!drbFound && ret == ROK)/* new DRB to Add */
2676       {
2677          if(duProcEgtpTunnelCfg(NULLP, NULLP, &duUeCfg->upTnlInfo[drbIdx]) != ROK)
2678          {
2679             DU_LOG("\nERROR  -> DU_APP : duUpdateTunnelCfgDb: Failed to add tunnel req for Drb id[%d]",
2680                   duUeCfg->upTnlInfo[drbIdx].drbId);
2681             ret = RFAILED;
2682             break;
2683          }
2684       }
2685    }
2686    return ret;
2687 }
2688
2689 /*******************************************************************
2690  *
2691  * @brief @brief To update DuUeCb Mac and Rlc Ue Cfg
2692  * 
2693  *
2694  * @details
2695  *
2696  *    Function : duUpdateDuUeCbCfg
2697  *
2698  *    Functionality: update DuUeCb Mac and Rlc Ue Cfg
2699  *
2700  * @params[in] ueId, cellIdx 
2701  * @return ROK     - success
2702  *         RFAILED - failure
2703  *
2704  * ****************************************************************/
2705
2706 uint8_t duUpdateDuUeCbCfg(uint8_t ueId, uint8_t cellId)
2707 {
2708    uint8_t ret = ROK;
2709    uint16_t cellIdx = 0, crnti=0;
2710    DuUeCb *ueCb = NULLP;
2711
2712    GET_CELL_IDX(cellId, cellIdx);
2713    ueCb = &duCb.actvCellLst[cellIdx]->ueCb[ueId-1];
2714
2715    /*Filling RLC Ue Cfg */
2716    ueCb->duRlcUeCfg.cellId = cellId;
2717    ueCb->duRlcUeCfg.ueId   = ueId;
2718    ret = duUpdateRlcLcCfg(&ueCb->duRlcUeCfg, ueCb->f1UeDb);
2719    if(ret == ROK)
2720    {
2721       /*Filling MAC Ue Cfg */
2722       GET_CRNTI(crnti, ueId);
2723       ueCb->duMacUeCfg.cellId = cellId;
2724       ueCb->duMacUeCfg.ueId  = ueId;
2725       ueCb->duMacUeCfg.crnti  = crnti;
2726       ret = duUpdateMacCfg(&ueCb->duMacUeCfg, ueCb->f1UeDb);
2727       if(ret == RFAILED)
2728          DU_LOG("\nERROR  -->  DU APP : Failed while updating MAC LC Config at duUpdateDuUeCbCfg()");
2729       else
2730       {
2731          if(duUpdateTunnelCfgDb(ueId, cellId, &ueCb->f1UeDb->duUeCfg) != ROK)
2732          {
2733             DU_LOG("\nERROR  -->  DU_APP : Failed to establish tunnel in duUpdateDuUeCbCfg()");
2734             return RFAILED;
2735          }
2736       }
2737    }
2738    else
2739       DU_LOG("\nERROR  -->  DU APP : Failed while updating RLC LC Config at duUpdateDuUeCbCfg()");
2740    return ret;
2741 }
2742
2743 /*******************************************************************
2744  *
2745  * @brief Handle UE create response from MAC
2746  *
2747  * @details
2748  *
2749  *    Function : DuProcMacUeCreateRsp
2750  *
2751  *    Functionality: Handle UE Create response from MAC
2752  *
2753  * @params[in] Pointer to MacUeCreateRsp and Pst 
2754  * @return ROK     - success
2755  *         RFAILED - failure
2756  *
2757  * ****************************************************************/
2758 uint8_t DuProcMacUeCreateRsp(Pst *pst, MacUeCreateRsp *cfgRsp)
2759 {
2760    uint8_t ret = ROK;
2761    uint16_t cellIdx;
2762
2763    if(cfgRsp)
2764    {
2765       GET_CELL_IDX(cfgRsp->cellId, cellIdx);
2766       if(cfgRsp->result == MAC_DU_APP_RSP_OK)
2767       {
2768          if(pst->event == EVENT_MAC_UE_CREATE_RSP)
2769          {
2770             DU_LOG("\nINFO   -->  DU APP : MAC UE Create Response : SUCCESS [DU UE F1AP ID : %d]", cfgRsp->ueId);
2771
2772             if(duCb.actvCellLst[cellIdx] && (duCb.actvCellLst[cellIdx]->ueCb[cfgRsp->ueId -1].gnbDuUeF1apId == cfgRsp->ueId))
2773             {
2774                duCb.actvCellLst[cellIdx]->ueCb[cfgRsp->ueId -1].duMacUeCfg.macUeCfgState = UE_CREATE_COMPLETE;
2775
2776               if((duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].ueState == UE_HANDIN_IN_PROGRESS) && 
2777                     (duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].duMacUeCfg.macUeCfgState == UE_CREATE_COMPLETE) &&
2778                      (duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].duRlcUeCfg.rlcUeCfgState == UE_CREATE_COMPLETE))
2779                {
2780                   if((ret = duUpdateDuUeCbCfg(cfgRsp->ueId, cfgRsp->cellId)) == ROK)
2781                   {
2782                      /* If UE is in handover, RACH resource needs to be requested
2783                       * from MAC for CFRA */
2784                      if((duBuildAndSendRachRsrcReqToMac(cfgRsp->cellId, cfgRsp->ueId)) != ROK)
2785                      {
2786                         DU_LOG("\nERROR  --> DU APP : Failed to send RACH Resource Request to MAC");
2787                         DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(MacUeCreateRsp));
2788                         return RFAILED;
2789                      }
2790                   }
2791                   else
2792                   {
2793                      DU_LOG("\nERROR  ->  DU APP : Failure in updating DU UE CB");
2794                      DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(MacUeCreateRsp));
2795                      return RFAILED;
2796                   }
2797                }
2798             }
2799          }
2800       }
2801       else
2802       {
2803          DU_LOG("\nERROR  -->  DU APP : MAC UE CFG Response for EVENT[%d]: FAILURE [DU UE F1AP ID : %d]", pst->event, cfgRsp->ueId);
2804          ret = RFAILED;
2805       }
2806       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(MacUeCreateRsp));
2807    }
2808    else
2809    {
2810       DU_LOG("\nERROR  -->  DU APP : Received MAC Ue Config Response is NULL at DuProcMacUeCreateRsp()");
2811       ret = RFAILED;
2812    }
2813    return ret;
2814 }
2815
2816 /*******************************************************************
2817  *
2818  * @brief Handle UE Reconfig response from MAC
2819  *
2820  * @details
2821  *
2822  *    Function : DuProcMacUeRecfgRsp
2823  *
2824  *    Functionality: Handle UE ReConfig response from MAC
2825  *
2826  * @params[in] Pointer to MacUeRecfgRsp and Pst 
2827  * @return ROK     - success
2828  *         RFAILED - failure
2829  *
2830  * ****************************************************************/
2831 uint8_t DuProcMacUeRecfgRsp(Pst *pst, MacUeRecfgRsp *reCfgRsp)
2832 {
2833    uint8_t ret = ROK;
2834    uint16_t cellIdx;
2835
2836    if(reCfgRsp)
2837    {
2838       GET_CELL_IDX(reCfgRsp->cellId, cellIdx);
2839       if(reCfgRsp->result == MAC_DU_APP_RSP_OK)
2840       {
2841          if(pst->event == EVENT_MAC_UE_RECONFIG_RSP)
2842          {
2843             DU_LOG("\nINFO   -->  DU APP : MAC UE Reconfig Response : SUCCESS [DU UE F1AP ID : %d]", reCfgRsp->ueId);
2844             if(duCb.actvCellLst[cellIdx] && 
2845                   (duCb.actvCellLst[cellIdx]->ueCb[reCfgRsp->ueId -1].gnbDuUeF1apId == reCfgRsp->ueId))
2846             {
2847                duCb.actvCellLst[cellIdx]->ueCb[reCfgRsp->ueId -1].duMacUeCfg.macUeCfgState = UE_RECFG_COMPLETE;
2848                if((duCb.actvCellLst[cellIdx]->ueCb[reCfgRsp->ueId -1].duMacUeCfg.macUeCfgState == UE_RECFG_COMPLETE) &&
2849                      (duCb.actvCellLst[reCfgRsp->cellId -1]->ueCb[reCfgRsp->ueId -1].duRlcUeCfg.rlcUeCfgState == UE_RECFG_COMPLETE))
2850                {
2851                   if((ret = duUpdateDuUeCbCfg(reCfgRsp->ueId, reCfgRsp->cellId)) == ROK)
2852                   {  
2853                      if((BuildAndSendUeCtxtRsp(reCfgRsp->cellId, reCfgRsp->ueId)) != ROK)
2854                      {
2855                         DU_LOG("\nERROR  ->  DU APP : Failure in BuildAndSendUeCtxtRsp()");
2856                         DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, reCfgRsp, sizeof(MacUeRecfgRsp));
2857                         return RFAILED;
2858                      }
2859                   }
2860                   else
2861                   {
2862                      DU_LOG("\nERROR  ->  DU APP : Failure in updating DU UE CB");
2863                      DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, reCfgRsp, sizeof(MacUeRecfgRsp));
2864                      return RFAILED;
2865                   }
2866                }
2867             }
2868          }
2869       }
2870       else
2871       {
2872          DU_LOG("\nERROR  -->  DU APP : MAC UE RECFG Response for EVENT[%d]: FAILURE [DU UE F1AP ID : %d]", pst->event, reCfgRsp->ueId);
2873          if(pst->event == EVENT_MAC_UE_RECONFIG_RSP)
2874          {
2875             //TODO: Send the failure case in Ue Context Setup Response
2876          }
2877          ret = RFAILED;
2878       }
2879       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, reCfgRsp, sizeof(MacUeRecfgRsp));
2880    }
2881    else
2882    {
2883       DU_LOG("\nERROR  -->  DU APP : Received MAC Ue ReConfig Response is NULL at DuProcMacUeRecfgRsp()");
2884       ret = RFAILED;
2885    }
2886    return ret;
2887 }
2888
2889 /*******************************************************************
2890  *
2891  * @brief Processes UE create Req to RLC UL
2892  *
2893  * @details
2894  *
2895  *    Function : duBuildAndSendUeCreateReqToRlc
2896  *
2897  *    Functionality: 
2898  *     Processes UE create Req to RLC UL
2899  * 
2900  *  @params[in]  cellId,
2901  *               ueId,
2902  *               Pointer to RlcUeCreate
2903  *  @return ROK     - success
2904  *          RFAILED - failure
2905  * 
2906  *****************************************************************/
2907
2908 uint8_t duBuildAndSendUeCreateReqToRlc(uint16_t cellId, uint8_t gnbDuUeF1apId, DuUeCfg *ueCfgDb, DuRlcUeCfg *duRlcUeCfg)
2909 {
2910    uint8_t  ret = ROK;
2911    RlcUeCreate *rlcUeCfg = NULLP;
2912    Pst       pst;
2913   
2914    ret = updateRlcUeCfg(cellId, gnbDuUeF1apId, ueCfgDb, duRlcUeCfg);
2915    if(ret == RFAILED)
2916    {
2917       DU_LOG("\nERROR  -->  DU APP : Failed to fill Rlc Ue Cfg at duBuildAndSendUeCreateReqToRlc()");
2918       return ret;
2919    }
2920
2921    FILL_PST_DUAPP_TO_RLC(pst, RLC_UL_INST, EVENT_RLC_UE_CREATE_REQ);
2922    /* Copying ueCfg to a sharable buffer */
2923    DU_ALLOC_SHRABL_BUF(rlcUeCfg, sizeof(RlcUeCreate));
2924    if(rlcUeCfg)
2925    {
2926       memset(rlcUeCfg, 0, sizeof(RlcUeCreate));
2927       fillRlcUeCfg(duRlcUeCfg, rlcUeCfg);
2928
2929       /* Processing one Ue at a time to RLC */
2930       DU_LOG("\nDEBUG   -->  DU_APP: Sending UE create request to RLC UL");
2931       ret = (*packRlcUeCreateReqOpts[pst.selector])(&pst, rlcUeCfg);
2932       if(ret == RFAILED)
2933       {
2934          DU_LOG("\nERROR  -->  DU_APP : Failure in sending Ue Create Req to RLC");
2935          DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, rlcUeCfg, sizeof(RlcUeCreate));
2936          ret = RFAILED;
2937       }
2938    }
2939    else
2940    {
2941       DU_LOG("\n ERROR  -->  DU APP : Memory alloc failed at duBuildAndSendUeCreateReqToRlc()");
2942       ret = RFAILED;
2943    }
2944    return ret;
2945 }
2946
2947 /*******************************************************************
2948  *
2949  * @brief Processes UE create Rsp received from RLC UL
2950  *
2951  * @details
2952  *
2953  
2954  *    Function : DuProcRlcUeCreateRsp
2955  *
2956  *    Functionality: 
2957  *     Processes UE create Rsp received from RLC UL
2958  * 
2959  *  @params[in]  Post structure
2960  *               Pointer to RlcCfgCfm
2961  *  @return ROK     - success
2962  *          RFAILED - failure
2963  * 
2964  *****************************************************************/
2965 uint8_t DuProcRlcUeCreateRsp(Pst *pst, RlcUeCreateRsp *cfgRsp)
2966 {
2967    uint8_t ret = ROK;
2968
2969    if(cfgRsp)
2970    {
2971       if(cfgRsp->result == RLC_DU_APP_RSP_OK)
2972       {
2973          if(pst->event == EVENT_RLC_UE_CREATE_RSP)
2974          {
2975             DU_LOG("\nINFO   -->  DU_APP: RLC UE Create Response : SUCCESS [UE IDX:%d]", cfgRsp->ueId);
2976             duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].duRlcUeCfg.rlcUeCfgState = UE_CREATE_COMPLETE;
2977
2978             if((duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].ueState == UE_HANDIN_IN_PROGRESS) &&
2979                   (duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].duMacUeCfg.macUeCfgState == UE_CREATE_COMPLETE) &&
2980                   (duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].duRlcUeCfg.rlcUeCfgState == UE_CREATE_COMPLETE))
2981             {
2982                if((ret = duUpdateDuUeCbCfg(cfgRsp->ueId, cfgRsp->cellId)) == ROK)
2983                {
2984                   /* If UE is in handover, RACH resource needs to be requested
2985                    * from MAC for CFRA */
2986                   if((duBuildAndSendRachRsrcReqToMac(cfgRsp->cellId, cfgRsp->ueId)) != ROK)
2987                   {
2988                      DU_LOG("\nERROR  --> DU APP : Failed to send RACH Resource Request to MAC");
2989                      DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(RlcUeCreateRsp));
2990                      return RFAILED;
2991                   }
2992                }
2993                else
2994                {
2995                   DU_LOG("\nERROR  -->  DU APP : Failure in updating DU UE CB");
2996                   DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(RlcUeCreateRsp));
2997                   return RFAILED;
2998                }
2999             }
3000          }
3001       }
3002       else
3003       {
3004          DU_LOG("\nERROR  -->  DU_APP: RLC UE CREATE Response for EVENT[%d] : FAILED [UE IDX : %d, REASON :%d]",\
3005                pst->event, cfgRsp->ueId, cfgRsp->reason);
3006          ret = RFAILED;
3007       }
3008       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(RlcUeCreateRsp));
3009    }
3010    else
3011    {
3012       DU_LOG("\nERROR  -->  DU_APP: Received RLC Ue Create Response is NULL at DuProcRlcUeCreateRsp()");
3013       ret = RFAILED;
3014    }
3015    return ret;
3016 }
3017
3018 /*******************************************************************
3019  *
3020  * @brief Processes UE reconfig Rsp received from RLC UL
3021  *
3022  * @details
3023  *
3024  
3025  *    Function : DuProcRlcUeReconfigRsp
3026  *
3027  *    Functionality: 
3028  *     Processes UE reconfig Rsp received from RLC UL
3029  * 
3030  *  @params[in]  Post structure
3031  *               Pointer to RlcCfgCfm
3032  *  @return ROK     - success
3033  *          RFAILED - failure
3034  * 
3035  *****************************************************************/
3036 uint8_t DuProcRlcUeReconfigRsp(Pst *pst, RlcUeReconfigRsp *cfgRsp)
3037 {
3038    uint8_t ret = ROK;
3039
3040    if(cfgRsp)
3041    {
3042       if(cfgRsp->result == RLC_DU_APP_RSP_OK)
3043       {
3044          if(pst->event == EVENT_RLC_UE_RECONFIG_RSP)
3045          {
3046             DU_LOG("\nINFO   -->  DU_APP: RLC UE Reconfig Response : SUCCESS [UE IDX:%d]", cfgRsp->ueId);
3047
3048             duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].duRlcUeCfg.rlcUeCfgState = UE_RECFG_COMPLETE;
3049             if((duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].duMacUeCfg.macUeCfgState == UE_RECFG_COMPLETE) &&
3050                   (duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].duRlcUeCfg.rlcUeCfgState == UE_RECFG_COMPLETE))
3051             {
3052                if((ret = duUpdateDuUeCbCfg(cfgRsp->ueId, cfgRsp->cellId)) == ROK)
3053                {
3054                   if((BuildAndSendUeCtxtRsp(cfgRsp->cellId, cfgRsp->ueId)) != ROK)
3055                   {
3056                      DU_LOG("\nERROR  -->  DU APP : Failure in BuildAndSendUeCtxtRsp");
3057                      DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(RlcUeReconfigRsp));
3058                      return RFAILED;
3059                   }
3060                }
3061                else
3062                {
3063                   DU_LOG("\nERROR  -->  DU APP : Failure in updating DU UE CB");
3064                   DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(RlcUeReconfigRsp));
3065                   return RFAILED;
3066                }
3067             }
3068          }
3069       }
3070       else
3071       {
3072          DU_LOG("\nERROR  -->  DU_APP: RLC UE RE-CFG Response for EVENT[%d] : FAILED [UE IDX : %d, REASON :%d]",\
3073                pst->event, cfgRsp->ueId, cfgRsp->reason);
3074          if((pst->event == EVENT_RLC_UE_RECONFIG_RSP))
3075          {
3076             //TODO: update failure case in ue Context setup Response
3077          }
3078          ret = RFAILED;
3079       }
3080       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(RlcUeReconfigRsp));
3081    }
3082    else
3083    {
3084       DU_LOG("\nERROR  -->  DU_APP: Received RLC Ue ReConfig Response is NULL at DuProcRlcUeReconfigRsp()");
3085       ret = RFAILED;
3086    }
3087    return ret;
3088 }
3089 /**********************************************************************************
3090  *
3091  * @brief Fills Ue ReCfg from DU DB to RlcUeRecfg
3092  *
3093  * @details
3094  *
3095  *    Function : fillRlcUeRecfg
3096  *
3097  *    Functionality: fills ue ReCfg to be sent to RLC
3098  *
3099  * @params[in] DuRlcUeCfg Pointer
3100  *             RlcUeRecfg Pointer
3101  *
3102  * @return void 
3103  * *******************************************************************************/
3104 void fillRlcUeRecfg(DuRlcUeCfg *duRlcUeCfg, RlcUeRecfg *rlcUeRecfg)
3105 {
3106    uint8_t lcIdx = 0;
3107
3108    rlcUeRecfg->cellId = duRlcUeCfg->cellId;
3109    rlcUeRecfg->ueId   = duRlcUeCfg->ueId;
3110
3111
3112    if(duRlcUeCfg->numLcs > 0)
3113    {
3114       rlcUeRecfg->numLcsToAdd = 0;
3115       rlcUeRecfg->numLcsToMod = 0;
3116       rlcUeRecfg->numLcsToRel = 0;
3117
3118       for(lcIdx = 0; lcIdx < duRlcUeCfg->numLcs; lcIdx++)
3119       {
3120          if(duRlcUeCfg->rlcLcCfg[lcIdx].configType == CONFIG_ADD)
3121          {
3122             memcpy(&rlcUeRecfg->rlcLcCfgAdd[rlcUeRecfg->numLcsToAdd], &duRlcUeCfg->rlcLcCfg[lcIdx].rlcBearerCfg, sizeof(RlcBearerCfg));
3123             rlcUeRecfg->numLcsToAdd++;
3124          }
3125          if(duRlcUeCfg->rlcLcCfg[lcIdx].configType == CONFIG_MOD)
3126          {
3127             memcpy(&rlcUeRecfg->rlcLcCfgMod[rlcUeRecfg->numLcsToMod], &duRlcUeCfg->rlcLcCfg[lcIdx].rlcBearerCfg, sizeof(RlcBearerCfg));
3128             rlcUeRecfg->numLcsToMod++;
3129          }
3130          if(duRlcUeCfg->rlcLcCfg[lcIdx].configType == CONFIG_DEL)
3131          {
3132             memcpy(&rlcUeRecfg->rlcLcCfgRel[rlcUeRecfg->numLcsToRel], &duRlcUeCfg->rlcLcCfg[lcIdx].rlcBearerCfg, sizeof(RlcBearerCfg));
3133             rlcUeRecfg->numLcsToRel++;
3134          }
3135       }
3136    }
3137    return;
3138 }
3139
3140 /*******************************************************************
3141  *
3142  * @brief Builds and Send Ue Reconfig Req to RLC
3143  *
3144  * @details
3145  *
3146  *    Function : duBuildAndSendUeRecfgReqToRLC
3147  *
3148  *    Functionality: Builds and Send Ue Reconfig Req to RLC
3149  *
3150  * @params[in] uint16_t cellId,
3151  *             uint6_t crnti #AS per 38.473 V15.3.0, Section 9.3.1.32 crnti
3152  *             value range is b/w 0..65535#  
3153  *             DuUeCfg *ueCfgDb
3154  *             RlcUeCreate *rlcUeCfg
3155  * @return ROK     - success
3156  *         RFAILED - failure
3157  *
3158  * ****************************************************************/
3159
3160 uint8_t duBuildAndSendUeRecfgReqToRlc(uint16_t cellId, uint8_t gnbDuUeF1apId, uint16_t crnti, DuUeCfg *ueCfgDb)
3161 {
3162    uint8_t ret = ROK;
3163    DuRlcUeCfg *duRlcUeCfg = NULLP;
3164    RlcUeRecfg *rlcUeRecfg = NULLP;
3165
3166    DU_ALLOC(duRlcUeCfg, sizeof(DuRlcUeCfg));
3167    DU_ALLOC_SHRABL_BUF(rlcUeRecfg, sizeof(RlcUeRecfg));
3168    if(duRlcUeCfg && rlcUeRecfg)
3169    {
3170       memset(duRlcUeCfg, 0, sizeof(DuRlcUeCfg));
3171       memset(rlcUeRecfg, 0, sizeof(RlcUeRecfg));
3172
3173       ret = updateRlcUeCfg(cellId, gnbDuUeF1apId, ueCfgDb, duRlcUeCfg);
3174
3175       if(ret == RFAILED)
3176          DU_LOG("\nERROR  -->  DU APP : Failed at duBuildAndSendUeRecfgReqToRlc()");
3177       else
3178       {
3179          fillRlcUeRecfg(duRlcUeCfg, rlcUeRecfg);
3180          ret = sendUeRecfgReqToRlc(rlcUeRecfg);
3181       }
3182    }
3183    else
3184    {
3185       DU_LOG("\nERROR  -->  DU APP : Memory Alloc failed at duBuildAndSendUeRecfgReqToRlc()");
3186       ret = RFAILED;
3187    }
3188    DU_FREE(duRlcUeCfg, sizeof(DuRlcUeCfg));
3189    return ret;
3190 }
3191
3192 /**********************************************************************************
3193  *
3194  * @brief Fills Ue ReCfg from DU DB to MacUeRecfg
3195  *
3196  * @details
3197  *
3198  *    Function : fillMacUeRecfg
3199  *
3200  *    Functionality: fills ue ReCfg to be sent to MAC
3201  *
3202  * @params[in] DuMacUeCfg Pointer
3203  *             MacUeRecfg Pointer
3204  *
3205  * @return void 
3206  * *******************************************************************************/
3207 void fillMacUeRecfg(DuMacUeCfg *duMacUeCfg, MacUeRecfg *macUeRecfg)
3208 {
3209    uint8_t lcIdx = 0;
3210
3211    macUeRecfg->cellId = duMacUeCfg->cellId;
3212    macUeRecfg->ueId = duMacUeCfg->ueId;
3213    macUeRecfg->crnti = duMacUeCfg->crnti;
3214    macUeRecfg->macCellGrpRecfgPres = duMacUeCfg->macCellGrpCfgPres;
3215    if(macUeRecfg->macCellGrpRecfgPres)
3216    {
3217       memcpy(&macUeRecfg->macCellGrpRecfg, &duMacUeCfg->macCellGrpCfg, sizeof(MacCellGrpCfg));
3218    }
3219    macUeRecfg->phyCellGrpRecfgPres = duMacUeCfg->phyCellGrpCfgPres;
3220    if(macUeRecfg->phyCellGrpRecfgPres)
3221    {
3222       memcpy(&macUeRecfg->phyCellGrpRecfg, &duMacUeCfg->phyCellGrpCfg, sizeof(PhyCellGrpCfg));
3223    }
3224    macUeRecfg->spCellRecfgPres = duMacUeCfg->spCellCfgPres;
3225    if(macUeRecfg->spCellRecfgPres)
3226    {
3227       memcpy(&macUeRecfg->spCellRecfg, &duMacUeCfg->spCellCfg, sizeof(SpCellRecfg));
3228    }
3229    if(duMacUeCfg->ambrCfg != NULLP)
3230    {
3231       DU_ALLOC_SHRABL_BUF(macUeRecfg->ambrRecfg, sizeof(AmbrCfg));
3232       memcpy(macUeRecfg->ambrRecfg, duMacUeCfg->ambrCfg, sizeof(AmbrCfg));
3233    }
3234    memcpy(&macUeRecfg->dlModInfo, &duMacUeCfg->dlModInfo, sizeof(ModulationInfo));
3235    memcpy(&macUeRecfg->ulModInfo, &duMacUeCfg->ulModInfo, sizeof(ModulationInfo));
3236    if(duMacUeCfg->numLcs > 0)
3237    {
3238       macUeRecfg->numLcsToAdd = 0;
3239       macUeRecfg->numLcsToDel = 0;
3240       macUeRecfg->numLcsToMod = 0;
3241
3242       for(lcIdx = 0; lcIdx < duMacUeCfg->numLcs; lcIdx++)
3243       {
3244          if(duMacUeCfg->lcCfgList[lcIdx].configType == CONFIG_ADD)
3245          {
3246             /*VS: To thoroughly check*/
3247             memcpy(&macUeRecfg->lcCfgAdd[macUeRecfg->numLcsToAdd], &duMacUeCfg->lcCfgList[lcIdx].lcConfig, sizeof(LcCfg));
3248             macUeRecfg->numLcsToAdd++;
3249          }
3250          if(duMacUeCfg->lcCfgList[lcIdx].configType == CONFIG_DEL)
3251          {
3252             /*VS: To thoroughly check*/
3253             macUeRecfg->lcIdToDel[macUeRecfg->numLcsToDel] = duMacUeCfg->lcCfgList[lcIdx].lcConfig.lcId;
3254             macUeRecfg->numLcsToDel++;
3255          }
3256          if(duMacUeCfg->lcCfgList[lcIdx].configType == CONFIG_MOD)
3257          {
3258             /*VS: To thoroughly check*/
3259             memcpy(&macUeRecfg->lcCfgMod[macUeRecfg->numLcsToMod], &duMacUeCfg->lcCfgList[lcIdx].lcConfig, sizeof(LcCfg));
3260             macUeRecfg->numLcsToMod++;
3261          }
3262       }
3263    }
3264    macUeRecfg->transmissionAction = duMacUeCfg->transmissionAction;
3265 #ifdef NR_DRX
3266    macUeRecfg->drxConfigIndicatorRelease = duMacUeCfg->drxConfigIndicatorRelease;
3267 #endif
3268 }
3269
3270 /*******************************************************************
3271  *
3272  * @brief Builds and Send Ue Reconfig Req to MAC
3273  *
3274  * @details
3275  *
3276  *    Function : duBuildAndSendUeRecfgReqToMac
3277  *
3278  *    Functionality: Builds and Send Ue Reconfig Req to MAC
3279  *
3280  * @params[in] uint16_t cellId,
3281  *             uint6_t crnti #AS per 38.473 V15.3.0, Section 9.3.1.32 crnti
3282  *             value range is b/w 0..65535#  
3283  *             CellGroupConfigRrc_t *macCellGrpCfg
3284  *             DuUeCfg *ueCfgDb
3285  * @return ROK     - success
3286  *         RFAILED - failure
3287  *
3288  * ****************************************************************/
3289
3290 uint8_t duBuildAndSendUeRecfgReqToMac(uint16_t cellId, uint8_t duUeF1apId, uint16_t crnti, DuUeCfg *ueCfgDb)
3291 {
3292    uint8_t ret = ROK;
3293    DuMacUeCfg *duMacUeCfg = NULLP;
3294    MacUeRecfg *macUeRecfg = NULLP;
3295
3296    DU_ALLOC(duMacUeCfg, sizeof(DuMacUeCfg));
3297    DU_ALLOC_SHRABL_BUF(macUeRecfg, sizeof(MacUeRecfg));
3298    if(macUeRecfg && duMacUeCfg)
3299    {
3300       memset(duMacUeCfg, 0, sizeof(DuMacUeCfg));
3301       memset(macUeRecfg, 0, sizeof(MacUeRecfg));
3302
3303       ret = updateDuMacUeCfg(cellId, duUeF1apId, crnti, ueCfgDb, duMacUeCfg);
3304       if(ret == RFAILED)
3305          DU_LOG("\nERROR  -->  DU APP : Failed to fill Mac Ue Cfg at duBuildAndSendUeRecfgReqToMac()");
3306       else
3307       {
3308          fillMacUeRecfg(duMacUeCfg, macUeRecfg);
3309          ret = sendUeRecfgReqToMac(macUeRecfg);
3310       }
3311    }
3312    else
3313    {
3314       DU_LOG("\nERROR  -->  DU APP : Memory alloc failed for macUeCfg at duBuildAndSendUeRecfgReqToMac()");
3315       ret = RFAILED;
3316    }
3317    DU_FREE(duMacUeCfg, sizeof(DuMacUeCfg));
3318
3319    return ret;
3320 }
3321
3322 /*******************************************************************
3323  *
3324  * @brief Build and Send Ue context setup request
3325  *
3326  * @details
3327  *
3328  
3329  *    Function : duBuildAndSendUeContextSetupReq
3330  *
3331  *    Functionality: 
3332  *     Build and Send Ue context setup request
3333  * 
3334  *  @params[in]  cellId, crnti, DuUeCfg pointer
3335  *  @return ROK     - success
3336  *          RFAILED - failure
3337  * 
3338  *****************************************************************/
3339
3340 uint8_t duBuildAndSendUeContextSetupReq(uint16_t cellId, DuUeCb *ueCb)
3341 {
3342    uint8_t ret = ROK;
3343    uint16_t crnti; 
3344    DuUeCfg *duUeCfg = NULLP;
3345
3346    DU_LOG("\nDEBUG   -->  DU_APP: Processing Ue Context Setup Request for cellId [%d]", cellId);
3347
3348    if(!ueCb)
3349    {
3350       DU_LOG("\nERROR  -->  DU APP : UE Cb is NULL");
3351       return RFAILED;
3352    }
3353
3354    crnti = ueCb->crnti;
3355    duUeCfg = &ueCb->f1UeDb->duUeCfg;
3356
3357    /* If UE is being handed-in to this DU, UE context setup request will create
3358     * new UE context at MAC/SCH and RLC.
3359     * If UE is in active state, UE contex setup request will lead to
3360     * reconfiguration of UE at MAC/SCH and RLC
3361     */
3362    if(ueCb->ueState == UE_HANDIN_IN_PROGRESS)
3363    {
3364       /* Filling MAC UE Config */
3365       memset(&ueCb->duMacUeCfg, 0, sizeof(DuMacUeCfg));
3366
3367       /* Since UE attach has not yet happened, crnti is unknow. Hence passing 0 */
3368       ret = duBuildAndSendUeCreateReqToMac(cellId, ueCb->gnbDuUeF1apId, 0, duUeCfg, &ueCb->duMacUeCfg);
3369       if(ret == RFAILED)
3370          DU_LOG("\nERROR  -->  DU APP : Failed to send UE create request to MAC");
3371
3372       ret = duBuildAndSendUeCreateReqToRlc(cellId, ueCb->gnbDuUeF1apId, duUeCfg, &ueCb->duRlcUeCfg);
3373       if(ret == RFAILED)
3374          DU_LOG("\nERROR  --> DU APP : Failed to send UE create request to RLC");
3375
3376    }
3377    else
3378    {
3379       /* Filling RLC UE Reconfig */ 
3380       ret = duBuildAndSendUeRecfgReqToRlc(cellId, ueCb->gnbDuUeF1apId, crnti, duUeCfg);
3381       if(ret == RFAILED)
3382          DU_LOG("\nERROR  -->  DU APP : Failed to build ctxt setup req for RLC at duBuildAndSendUeContextSetupReq()");
3383
3384       /* Filling MAC UE Reconfig */
3385       ret = duBuildAndSendUeRecfgReqToMac(cellId, ueCb->gnbDuUeF1apId, crnti, duUeCfg);
3386       if(ret == RFAILED)
3387          DU_LOG("\nERROR  -->  DU APP : Failed at build ctxt setup req for MAC at duBuildAndSendUeContextSetupReq()");
3388    }
3389
3390    return ret;
3391 }
3392
3393 /*******************************************************************
3394  *
3395  * @brief Processes DL Rsp received from RLC DL
3396  *
3397  * @details
3398  *
3399  
3400  *    Function : DuProcRlcDlRrcMsgRsp
3401  *
3402  *    Functionality: 
3403  *     Processes UE Rsp received from RLC DL
3404  * 
3405  *  @params[in]  Post structure
3406  *               Pointer to RlcCfgCfm
3407  *  @return ROK     - success
3408  *          RFAILED - failure
3409  * 
3410  *****************************************************************/
3411 uint8_t DuProcRlcDlRrcMsgRsp(Pst *pst, RlcDlRrcMsgRsp *dlRrcMsg)
3412 {
3413    uint8_t ret = ROK, ueId = 0;
3414    uint16_t cellId, crnti;
3415    DuUeCb *ueCb = NULLP;
3416    DlMsgState state;
3417
3418    state = dlRrcMsg->state;
3419    cellId = dlRrcMsg->cellId;
3420    crnti = dlRrcMsg->crnti;
3421    DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, dlRrcMsg, sizeof(RlcDlRrcMsgRsp));
3422
3423    if(state == TRANSMISSION_COMPLETE)
3424    {
3425       GET_UE_ID(crnti, ueId);
3426       ueCb = &duCb.actvCellLst[cellId -1]->ueCb[ueId -1];
3427
3428       if(ueCb->f1UeDb && ueCb->f1UeDb->dlRrcMsgPres)
3429       {
3430          if(ueCb->f1UeDb->actionType == UE_CTXT_SETUP)
3431          {
3432             ret = duBuildAndSendUeContextSetupReq(cellId, ueCb);
3433             if(ret == RFAILED)
3434                DU_LOG("\nERROR  -->  DU APP : Failed to process UE Context Setup Request in DuProcRlcDlRrcMsgRsp()");
3435          }
3436          
3437          if(ueCb->f1UeDb->actionType == UE_CTXT_MOD)
3438          {
3439             ret = duBuildAndSendUeContextModReq(cellId, ueCb->gnbDuUeF1apId, crnti, &ueCb->f1UeDb->duUeCfg);
3440             if(ret == RFAILED)
3441                DU_LOG("\nERROR  -->  DU APP : Failed to process UE Context Mod Request in DuProcRlcDlRrcMsgRsp()");
3442          }
3443
3444          if(ueCb->f1UeDb->actionType == UE_CTXT_RELEASE && ueCb->ueState == UE_ACTIVE)
3445          {
3446             ret = duBuildAndSendUeDeleteReq(cellId, crnti);
3447             if(ret == RFAILED)
3448             {
3449                DU_LOG("\nERROR  -->  DU APP : Failed to process UE Context Release Request in DuProcRlcDlRrcMsgRsp()");
3450             }
3451          }
3452       }
3453    }
3454    else
3455       DU_LOG("\nERROR  -->  DU APP : Failed to transmit DL RRC Msg");
3456
3457    return ret;
3458 }
3459 /*******************************************************************
3460  *
3461  * @brief Process UE context setup request from CU
3462  *
3463  * @details
3464  *
3465  *    Function : duProcUeContextSetupRequest 
3466  *
3467  *    Functionality: Process UE context setup request from CU
3468  *
3469  * @params[in] F1AP message
3470  * @return ROK     - success
3471  *         RFAILED - failure
3472  *
3473  * ****************************************************************/
3474
3475 uint8_t duProcUeContextSetupRequest(DuUeCb *ueCb)
3476 {
3477    uint8_t ret, cellId;
3478
3479    ret = ROK;
3480    if(ueCb)
3481    {
3482       cellId = duCb.actvCellLst[ueCb->f1UeDb->cellIdx]->cellId;
3483
3484       /* Send DL RRC msg for security Mode */
3485       if(ueCb->f1UeDb->dlRrcMsg)
3486       {
3487          if(ueCb->f1UeDb->dlRrcMsg->rrcMsgPdu)
3488          {
3489             /* Sending DL RRC Message to RLC */
3490             ret = duBuildAndSendDlRrcMsgToRlc(cellId, ueCb->duRlcUeCfg, ueCb->f1UeDb->dlRrcMsg);
3491             if(ret == RFAILED)
3492             {
3493                DU_LOG("\nERROR  -->  DU APP : Failed to send DL RRC msg in duProcUeContextSetupRequest()");
3494                DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, ueCb->f1UeDb->dlRrcMsg->rrcMsgPdu,\
3495                      ueCb->f1UeDb->dlRrcMsg->rrcMsgSize);
3496                DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, ueCb->f1UeDb->dlRrcMsg, sizeof(F1DlRrcMsg));
3497             }
3498          }
3499       }
3500       else if(ueCb->f1UeDb->actionType == UE_CTXT_SETUP)
3501       {
3502          ret = duBuildAndSendUeContextSetupReq(cellId, ueCb);
3503          if(ret == RFAILED)
3504          {
3505             DU_LOG("\nERROR  -->  DU APP : Failed to build ue context setup Req in duProcUeContextSetupRequest()");
3506          }
3507       }
3508    }
3509    else
3510    {
3511       //TODO: To send the failure cause in UeContextSetupRsp
3512       DU_LOG("ERROR  -->  DU APP : Failed to process UE CNTXT SETUP REQ at duProcUeContextSetupRequest()");
3513    }
3514    return ret;
3515 }
3516
3517 /*******************************************************************
3518  *
3519  * @brief Build and Send Ue context mod request
3520  *
3521  * @details
3522  *
3523  *
3524  *    Function : duBuildAndSendUeContextModReq
3525  *
3526  *    Functionality: 
3527  *     Build and Send Ue context mod request
3528  * 
3529  *  @params[in]  cellId, crnti, DuUeCfg pointer
3530  *  @return ROK     - success
3531  *          RFAILED - failure
3532  * 
3533  *****************************************************************/
3534
3535 uint8_t duBuildAndSendUeContextModReq(uint16_t cellId, uint8_t gnbDuUeF1apId, uint16_t crnti, DuUeCfg *duUeCfg)
3536 {
3537    uint8_t ret = ROK;
3538
3539    DU_LOG("\nDEBUG   -->  DU_APP: Processing Ue Context Mod Request for cellId [%d]", cellId);
3540    /* Filling RLC Ue Reconfig */ 
3541    ret = duBuildAndSendUeRecfgReqToRlc(cellId, gnbDuUeF1apId, crnti, duUeCfg);
3542    if(ret == RFAILED)
3543       DU_LOG("\nERROR  -->  DU APP : Failed to build ctxt setup req for RLC at duBuildAndSendUeContextModReq()");
3544    
3545    /* Filling MAC Ue Reconfig */
3546    ret = duBuildAndSendUeRecfgReqToMac(cellId, gnbDuUeF1apId, crnti, duUeCfg);
3547    if(ret == RFAILED)
3548       DU_LOG("\nERROR  -->  DU APP : Failed at build ctxt setup req for MAC at duBuildAndSendUeContextModReq()");
3549
3550    return ret;
3551 }
3552 /*******************************************************************
3553  *
3554  * @brief Processing Ue context mod request
3555  *
3556  * @details
3557  *
3558  *
3559  *    Function : duProcUeContextModReq 
3560  *
3561  *    Functionality:
3562  *        Processing  Ue context mod request
3563  *
3564  *  @params[in] DuUeCb *ueCb 
3565  *  @return ROK     - success
3566  *          RFAILED - failure
3567  *
3568  *****************************************************************/
3569
3570 uint8_t duProcUeContextModReq(DuUeCb *ueCb)
3571 {
3572    uint8_t ret, cellId;
3573
3574    ret = ROK;
3575    if(ueCb)
3576    {
3577       cellId = duCb.actvCellLst[ueCb->f1UeDb->cellIdx]->cellId;
3578       /* Send DL RRC msg for security Mode */
3579       if(ueCb->f1UeDb->dlRrcMsg)
3580       {
3581          if(ueCb->f1UeDb->dlRrcMsg->rrcMsgPdu)
3582          {
3583             /* Sending DL RRC Message to RLC */
3584             ret = duBuildAndSendDlRrcMsgToRlc(cellId, ueCb->duRlcUeCfg, ueCb->f1UeDb->dlRrcMsg);
3585             if(ret == RFAILED)
3586             {
3587                DU_LOG("\nERROR  -->  DU APP : Failed to send DL RRC msg in duProcUeContextModReq()");
3588                DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, ueCb->f1UeDb->dlRrcMsg->rrcMsgPdu,\
3589                      ueCb->f1UeDb->dlRrcMsg->rrcMsgSize);
3590                DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, ueCb->f1UeDb->dlRrcMsg, sizeof(F1DlRrcMsg));
3591             }
3592          }
3593       }
3594       else if(ueCb->f1UeDb->actionType == UE_CTXT_MOD)
3595       {
3596          ret = duBuildAndSendUeContextModReq(cellId, ueCb->gnbDuUeF1apId, ueCb->crnti, &ueCb->f1UeDb->duUeCfg);
3597          if(ret == RFAILED)
3598          {
3599             DU_LOG("\nERROR  -->  DU APP : Failed to build ue context setup Req in duProcUeContextModReq()");
3600             return RFAILED;
3601          }
3602       }
3603       else if((ueCb->f1UeDb->actionType == UE_CTXT_CFG_QUERY) || (ueCb->f1UeDb->actionType == UE_CTXT_RRC_RECFG_COMPLETE))
3604       {
3605          if((BuildAndSendUeContextModRsp(ueCb) != ROK))
3606          {
3607             DU_LOG("\nERROR  -->  DU APP : Failed to build UE Context modification response");
3608             return RFAILED;
3609          }
3610       }
3611    }
3612    else
3613    {
3614       //TODO: To send the failure cause in UeContextModRsp
3615       
3616       DU_LOG("ERROR  -->  DU APP : Failed to process UE CNTXT MOD REQ at duProcUeContextModReq()");
3617       return RFAILED;
3618    }
3619    return ROK;
3620 }
3621
3622 /*******************************************************************
3623 *
3624 * @brief Build and send dedicated RACH resource release request to MAC
3625 *
3626 * @details
3627 *
3628 *    Function : duBuildAndSendRachRsrcRelToMac
3629 *
3630 *    Functionality: Function to Build and send dedicated RACH resource 
3631 *    release request to MAC
3632 *
3633 * @params[in] Cell ID
3634 *             UE CB
3635 * @return ROK - Success
3636 *         RFAILED - Failure
3637 *
3638 * ****************************************************************/
3639 uint8_t duBuildAndSendRachRsrcRelToMac(uint16_t cellId, DuUeCb *ueCb)
3640 {
3641    Pst pst;
3642    MacRachRsrcRel *rachRsrcRel = NULLP;
3643
3644    DU_ALLOC_SHRABL_BUF(rachRsrcRel, sizeof(MacRachRsrcRel));
3645    if(!rachRsrcRel)
3646    {
3647       DU_LOG("\nERROR  -->  DU APP : Failed to allocate memory for RACH Resource Release in \
3648             duBuildAndSendRachRsrcRelToMac()");
3649       return RFAILED;
3650    }
3651
3652    rachRsrcRel->cellId = cellId;
3653    rachRsrcRel->ueId = ueCb->gnbDuUeF1apId;
3654    rachRsrcRel->crnti = ueCb->crnti;
3655
3656    /* Fill Pst */
3657    FILL_PST_DUAPP_TO_MAC(pst, EVENT_MAC_RACH_RESOURCE_REL);
3658
3659    if(((*packMacRachRsrcRelOpts[pst.selector])(&pst, rachRsrcRel)) != ROK)
3660    {
3661       DU_LOG("\nERROR  -->  DU_APP : Failure in sending RACH Resource Release to MAC at \
3662             duBuildAndSendRachRsrcRelToMac()");
3663       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, rachRsrcRel, sizeof(MacRachRsrcRel));
3664       return RFAILED;
3665    }
3666
3667    return ROK;
3668 }
3669
3670 /*******************************************************************
3671  *
3672 * @brief delete DuMacUeCfg from duCb
3673 *
3674 * @details
3675 *
3676 *    Function : deleteMacUeCfg 
3677 *
3678 *    Functionality: delete DuMacUeCfg from duCb
3679 *
3680 * @params[in] Pointer to DuMacUeCfg
3681 * @return ROK     - success
3682 *         RFAILED - failure
3683 *
3684 *******************************************************************/
3685
3686 void deleteMacUeCfg(DuMacUeCfg *ueCfg)
3687 {
3688    uint8_t lcCfgIdx=0;
3689    
3690    if(ueCfg->spCellCfgPres)
3691    {
3692       freeUeRecfgCellGrpInfo(ueCfg);
3693    }
3694    if(ueCfg->ambrCfg)
3695    {
3696       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL,ueCfg->ambrCfg, sizeof(AmbrCfg));
3697    }
3698    for(lcCfgIdx=0; lcCfgIdx< ueCfg->numLcs; lcCfgIdx++)
3699    {
3700       freeMacLcCfg(&ueCfg->lcCfgList[lcCfgIdx].lcConfig);
3701    }
3702    memset(ueCfg, 0, sizeof(DuMacUeCfg));
3703 }
3704
3705 /*******************************************************************
3706 *
3707 * @brief delete UE Configuration of a particular UE
3708 *
3709 * @details
3710 *
3711 *    Function : deleteUeCfg 
3712 *
3713 *    Functionality: delete UE Configuration of a particular UE 
3714 *
3715 * @params[in] uint16_t cellIdx, uint8_t ueId
3716 * @return ROK     - success
3717 *         RFAILED - failure
3718 *
3719 * ****************************************************************/
3720 uint8_t  deleteUeCfg(uint16_t cellId, uint8_t ueId)
3721 {
3722    uint8_t tnlIdx = 0;
3723    uint16_t cellIdx = 0;
3724    DuUeCb *ueCb = NULLP;
3725    
3726    GET_CELL_IDX(cellId, cellIdx);
3727    if(duCb.actvCellLst[cellIdx] != NULLP)
3728    {
3729       if((duCb.actvCellLst[cellIdx]->ueCb[ueId-1].duMacUeCfg.macUeCfgState == UE_DELETE_COMPLETE)\
3730             &&(duCb.actvCellLst[cellIdx]->ueCb[ueId-1].duRlcUeCfg.rlcUeCfgState == UE_DELETE_COMPLETE))
3731       {
3732          ueCb = &duCb.actvCellLst[cellIdx]->ueCb[ueId-1];
3733          deleteMacUeCfg(&ueCb->duMacUeCfg);
3734          deleteRlcUeCfg(&ueCb->duRlcUeCfg);
3735          if(ueCb->f1UeDb !=NULLP)
3736          {
3737             freeF1UeDb(ueCb->f1UeDb);
3738          }
3739          for(tnlIdx = 0; tnlIdx < duCb.numTeId; )
3740          {
3741             if(duCb.upTnlCfg[tnlIdx]->ueId == ueId)
3742             {
3743                duCb.upTnlCfg[tnlIdx]->configType = CONFIG_DEL;
3744                duProcEgtpTunnelCfg(tnlIdx, duCb.upTnlCfg[tnlIdx], duCb.upTnlCfg[tnlIdx]);
3745             }
3746             else
3747                tnlIdx++;
3748          }
3749          unsetBitInUeBitMap(cellId, ueId-1);
3750          duCb.actvCellLst[cellIdx]->numActvUes--;
3751          memset(ueCb, 0, sizeof(DuUeCb));
3752       }
3753       else
3754       {
3755          return RFAILED;
3756       }
3757    }
3758    else
3759    {
3760       DU_LOG("\nERROR  --> DU APP : deleteUeCfg(): CellIdx[%d] is not found", cellIdx);
3761       return RFAILED;
3762    }
3763    return ROK;
3764 }
3765
3766
3767 /*******************************************************************
3768 *
3769 * @brief Handle UE delete response from MAC
3770 *
3771 * @details
3772 *
3773 *    Function : DuProcMacUeDeleteRsp
3774 *
3775 *    Functionality: Handle UE delete response from MAC
3776 *
3777 * @params[in] Pointer to MacUeDeleteRsp and Pst
3778 * @return ROK     - success
3779 *         RFAILED - failure
3780 *
3781 * ****************************************************************/
3782
3783 uint8_t DuProcMacUeDeleteRsp(Pst *pst, MacUeDeleteRsp *deleteRsp)
3784 {
3785    uint8_t  ret =ROK,ueId=0;
3786    uint16_t cellIdx=0;
3787    uint32_t gnbCuUeF1apId =0 , gnbDuUeF1apId =0;
3788    
3789    if(deleteRsp)
3790    {
3791       if(deleteRsp->status == SUCCESSFUL)
3792       {
3793          DU_LOG("\nINFO   -->  DU APP : MAC UE Delete Response : SUCCESS [UE IDX : %d]", deleteRsp->ueId);
3794          GET_CELL_IDX(deleteRsp->cellId, cellIdx);
3795          if(duCb.actvCellLst[cellIdx])
3796          {
3797             duCb.actvCellLst[cellIdx]->ueCb[deleteRsp->ueId -1].duMacUeCfg.macUeCfgState = UE_DELETE_COMPLETE;
3798             ueId = deleteRsp->ueId;
3799             gnbCuUeF1apId = duCb.actvCellLst[cellIdx]->ueCb[ueId-1].gnbCuUeF1apId;
3800             gnbDuUeF1apId = duCb.actvCellLst[cellIdx]->ueCb[ueId-1].gnbDuUeF1apId;
3801             if(deleteUeCfg(deleteRsp->cellId, ueId) == ROK)
3802             {
3803                ret = BuildAndSendUeContextReleaseComplete(deleteRsp->cellId, gnbCuUeF1apId, gnbDuUeF1apId);
3804                if(ret != ROK)
3805                {
3806                   DU_LOG("\nERROR  -->  DU APP : DuProcMacUeDeleteRsp(): failed to send UE context release complete");
3807                }
3808             }
3809
3810          }
3811       }
3812       else
3813       {
3814          DU_LOG("\nERROR  -->  DU APP : DuProcMacUeDeleteRsp(): MAC UE Delete Response : FAILURE [UE IDX : %d]",\
3815          deleteRsp->ueId);
3816          ret =  RFAILED;
3817       }
3818       DU_FREE_SHRABL_BUF(pst->region, pst->pool, deleteRsp, sizeof(MacUeDeleteRsp));
3819    }
3820    else
3821    {
3822       DU_LOG("\nERROR  -->  DU APP : DuProcMacUeDeleteRsp(): MAC UE Delete Response is null");
3823       ret = RFAILED;
3824    }
3825    return ret;
3826 }
3827
3828 /*******************************************************************
3829  *
3830  * @brief Processes UE Delete Rsp received from RLC 
3831  *
3832  * @details
3833  *
3834  *    Function : DuProcRlcUeDeleteRsp
3835  *
3836  *    Functionality:
3837  *     Processes UE Delete Rsp received from RLC 
3838  *
3839  *  @params[in]  Post structure
3840  *               Pointer to RlcUeDeleteRsp
3841  *  @return ROK     - success
3842  *          RFAILED - failure
3843  *
3844  * *****************************************************************/
3845
3846 uint8_t DuProcRlcUeDeleteRsp(Pst *pst, RlcUeDeleteRsp *delRsp)
3847 {
3848    uint8_t  ueId = 0, ret = ROK;
3849    uint16_t cellIdx = 0,crnti=0;
3850
3851    if(delRsp)
3852    {
3853       ueId = delRsp->ueId;
3854       GET_CELL_IDX(delRsp->cellId, cellIdx);
3855
3856       if(delRsp->status == SUCCESSFUL)
3857       {
3858          DU_LOG("\nINFO   -->  DU_APP: RLC UE Delete Response : SUCCESS [UE IDX:%d]", ueId);
3859          if(duCb.actvCellLst[cellIdx]!=NULLP)
3860          {
3861             duCb.actvCellLst[cellIdx]->ueCb[ueId-1].duRlcUeCfg.rlcUeCfgState = UE_DELETE_COMPLETE;
3862             GET_CRNTI(crnti, ueId);
3863             if(sendUeDeleteReqToMac(delRsp->cellId, ueId, crnti) == RFAILED)
3864             {
3865                DU_LOG("\nERROR  -->  DU APP : duBuildAndSendUeDeleteReq(): Failed to build UE  delete req for MAC ");
3866                return RFAILED;
3867             }
3868
3869          }
3870       }
3871       else
3872       {
3873          DU_LOG("\nERROR   -->  DU_APP: RLC UE Delete Response : FAILED [UE IDX:%d]", ueId);
3874          ret = RFAILED;
3875       }
3876       DU_FREE_SHRABL_BUF(pst->region, pst->pool, delRsp, sizeof(RlcUeDeleteRsp));
3877
3878    }
3879    return ret;
3880 }
3881
3882 /*******************************************************************
3883  *
3884  * @brief Sending UE Delete Req To Mac
3885 *
3886 * @details
3887 *
3888 *    Function : sendUeDeleteReqToMac
3889 *
3890 *    Functionality:
3891 *     sending UE Delete Req To Mac
3892 *
3893 *  @params[in]    cellId, ueId, crnti 
3894 *  @return ROK     - success
3895 *          RFAILED - failure
3896 *
3897 *****************************************************************/
3898
3899 uint8_t sendUeDeleteReqToMac(uint16_t cellId, uint8_t ueId, uint16_t crnti)
3900 {
3901    Pst pst;
3902    uint8_t ret=ROK;
3903    MacUeDelete *ueDelete = NULLP;
3904
3905    DU_ALLOC_SHRABL_BUF(ueDelete, sizeof(MacUeDelete));
3906    if(ueDelete)
3907    {
3908       ueDelete->cellId = cellId;
3909       ueDelete->ueId   = ueId;
3910       ueDelete->crnti  = crnti;
3911       FILL_PST_DUAPP_TO_MAC(pst, EVENT_MAC_UE_DELETE_REQ);
3912
3913       DU_LOG("\nDEBUG  -->  DU_APP: Sending UE delete Request to MAC ");
3914       ret = (*packMacUeDeleteReqOpts[pst.selector])(&pst, ueDelete);
3915       if(ret == RFAILED)
3916       {
3917          DU_LOG("\nERROR  -->  DU_APP: sendUeDeleteReqToMac(): Failed to send UE delete Req to MAC");
3918          DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, ueDelete, sizeof(MacUeDelete));
3919       }
3920    }
3921    else
3922    {
3923       DU_LOG("\nERROR  -->   DU_APP: sendUeDeleteReqToMac(): Failed to allocate memory"); 
3924       ret = RFAILED;
3925    }
3926    return ret;
3927 }
3928
3929 /*******************************************************************
3930  *
3931  * @brief Sending UE Delete Req To Rlc
3932  *
3933  * @details
3934  *
3935  *    Function : sendUeDeleteReqToRlc
3936  *
3937  *    Functionality:
3938  *     Sending UE Delete Req To Rlc
3939  *
3940  *  @params[in]  cellId, ueId 
3941  *  @return ROK     - success
3942  *          RFAILED - failure
3943  *
3944  *****************************************************************/
3945
3946 uint8_t sendUeDeleteReqToRlc(uint16_t cellId, uint8_t ueId)
3947 {
3948    uint8_t ret;
3949    Pst pst;
3950    RlcUeDelete *ueDelete;
3951
3952    DU_ALLOC_SHRABL_BUF(ueDelete, sizeof(RlcUeDelete));
3953    if(ueDelete !=NULLP)
3954    {
3955       ueDelete->cellId = cellId;
3956       ueDelete->ueId = ueId;
3957       FILL_PST_DUAPP_TO_RLC(pst, RLC_UL_INST, EVENT_RLC_UE_DELETE_REQ);
3958
3959       ret = (*packRlcUeDeleteReqOpts[pst.selector])(&pst, ueDelete);
3960       if(ret == RFAILED)
3961       {
3962          DU_LOG("\nERROR  -->  DU_APP : sendUeDeleteReqToRlc():Failed to send UE Delete  Req to RLC"); 
3963          DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, ueDelete, sizeof(RlcUeDelete));
3964       }
3965    }
3966    else
3967    {
3968       DU_LOG("\nERROR  -->   DU_APP: sendUeDeleteReqToRlc():Memory allocation failed");
3969       ret = RFAILED;
3970    }
3971    return ret;
3972 }
3973
3974 /*******************************************************************
3975  *
3976  * @brief DU processes UE delete req from CU and sends to MAC and RLC 
3977  *
3978  * @details
3979  *
3980  *    Function : duBuildAndSendUeDeleteReq
3981  *
3982  *    Functionality: DU processes UE delete req from CU and sends to MAC 
3983  *                   and RLC 
3984  *
3985  * @params[in] cellId, crnti 
3986  * @return ROK     - success
3987  *         RFAILED - failure
3988  *
3989  * ****************************************************************/
3990
3991 uint8_t duBuildAndSendUeDeleteReq(uint16_t cellId, uint16_t crnti)
3992 {
3993    uint8_t  ueId =0;
3994    uint16_t cellIdx = 0;
3995
3996    DU_LOG("\nDEBUG  -->  DU_APP: Processing UE Delete Request ");
3997    GET_CELL_IDX(cellId, cellIdx);
3998    GET_UE_ID(crnti, ueId);
3999
4000    if(duCb.actvCellLst[cellIdx] != NULLP)
4001    {
4002       if(crnti != duCb.actvCellLst[cellIdx]->ueCb[ueId - 1].crnti)
4003       {
4004          DU_LOG("\nERROR  -->  DU APP : duBuildAndSendUeDeleteReq(): CRNTI [%d] not found", crnti);
4005          return RFAILED;
4006       }
4007
4008       duCb.actvCellLst[cellIdx]->ueCb[ueId - 1].ueState = UE_DELETION_IN_PROGRESS; 
4009       if(sendUeDeleteReqToRlc(cellId, ueId) == RFAILED)
4010       {
4011          DU_LOG("\nERROR  -->  DU APP : DuProcMacUeDeleteRsp():Failed to build UE  delete req for RLC ");
4012          return RFAILED;
4013       }
4014    }
4015    else
4016    {
4017       DU_LOG("\nERROR  -->  DU APP : duBuildAndSendUeDeleteReq(): Cell Id is not found");
4018       return RFAILED;
4019    }
4020
4021    return ROK;
4022 }
4023
4024 /*******************************************************************
4025  *
4026  * @brief delete RlcUeCreate from duCb
4027  *
4028  * @details
4029  *
4030  *    Function : deleteRlcUeCfg
4031  *
4032  *    Functionality:
4033  *       delete  RlcUeCreate from duCb
4034  *
4035  *  @params[in] RlcUeCreate *ueCfg 
4036  *               
4037  *  @return ROK     - success
4038  *          RFAILED - failure
4039  *
4040  *****************************************************************/
4041
4042 void deleteRlcUeCfg(DuRlcUeCfg *ueCfg)
4043 {
4044    uint8_t lcIdx = 0;
4045    RlcBearerCfg *lcCfg= NULLP;
4046    if(ueCfg)
4047    {
4048       for(lcIdx =0 ; lcIdx < ueCfg->numLcs ; lcIdx++)
4049       {
4050          lcCfg = &ueCfg->rlcLcCfg[lcIdx].rlcBearerCfg;
4051          switch(lcCfg->rlcMode)
4052          {
4053             case RLC_AM :
4054                {
4055                   DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, lcCfg->u.amCfg, sizeof(AmBearerCfg));
4056                   break;
4057                }
4058             case RLC_UM_BI_DIRECTIONAL :
4059                {
4060                   DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, lcCfg->u.umBiDirCfg, sizeof(UmBiDirBearerCfg));
4061                   break;
4062                }
4063             case RLC_UM_UNI_DIRECTIONAL_UL :
4064                {
4065                   DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, lcCfg->u.umUniDirUlCfg, sizeof(UmUniDirUlBearerCfg));
4066                   break;
4067                }
4068             case RLC_UM_UNI_DIRECTIONAL_DL :
4069                {
4070                   DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, lcCfg->u.umUniDirDlCfg, sizeof(UmUniDirDlBearerCfg));
4071                   break;
4072                }
4073          }
4074          DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, lcCfg->snssai, sizeof(Snssai));
4075       }
4076       memset(ueCfg, 0, sizeof(DuRlcUeCfg));
4077    }
4078 }
4079
4080 /*******************************************************************
4081 *
4082 * @brief Du process Ue Context Release Command
4083 *
4084 * @details
4085 *
4086 *    Function : duProcUeContextReleaseCommand 
4087 *
4088 *    Functionality: Du process Ue Context Release Command 
4089 *
4090 * @params[in] DuUeCb *duUeCb 
4091 * @return ROK     - success
4092 *         RFAILED - failure
4093 *
4094 * ****************************************************************/
4095 uint8_t duProcUeContextReleaseCommand(uint16_t cellId, DuUeCb *duUeCb)
4096 {
4097    uint8_t ret =ROK, ueId=0;
4098    uint16_t crnti = 0;
4099
4100    if(duUeCb != NULLP)
4101    {
4102       crnti = duUeCb->crnti;
4103       GET_UE_ID(crnti, ueId);
4104       
4105       if(duUeCb->f1UeDb)
4106       {
4107          /* Send DL RRC msg for RRC release */
4108          if(duUeCb->f1UeDb->dlRrcMsg)
4109          {
4110             if(duUeCb->f1UeDb->dlRrcMsg->rrcMsgPdu != NULLP)
4111             {
4112                ret = duBuildAndSendDlRrcMsgToRlc(cellId, duCb.actvCellLst[duUeCb->f1UeDb->cellIdx]->ueCb[ueId-1].duRlcUeCfg,\
4113                      duUeCb->f1UeDb->dlRrcMsg);
4114                if(ret == RFAILED)
4115                {
4116                   DU_LOG("\nERROR  -->  DU APP : duProcUeContextReleaseCommand() : Failed to send DL RRC msg");
4117                   DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, duUeCb->f1UeDb->dlRrcMsg->rrcMsgPdu,\
4118                         duUeCb->f1UeDb->dlRrcMsg->rrcMsgSize);
4119                   DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, duUeCb->f1UeDb->dlRrcMsg, sizeof(F1DlRrcMsg));
4120                }
4121             }
4122          }
4123       }
4124       else
4125       {
4126          ret = duBuildAndSendUeDeleteReq(cellId,crnti);
4127          if(ret == RFAILED)
4128          {
4129             DU_LOG("\nERROR  -->  DU APP : duProcUeContextReleaseCommand(): Failed to build and send Ue Delete request");
4130          }
4131       }
4132    }
4133    return ret;
4134 }
4135
4136 /*******************************************************************
4137  *
4138  * @brief Sending UE Reset Req To Mac
4139 *
4140 * @details
4141 *
4142 *    Function : sendUeResetReqToMac
4143 *
4144 *    Functionality:
4145 *     sending UE Reset Req To Mac
4146 *
4147 *  @params[in]    cellId, ueId, crnti 
4148 *  @return ROK     - success
4149 *          RFAILED - failure
4150 *
4151 *****************************************************************/
4152
4153 uint8_t sendUeResetReqToMac(uint16_t cellId, uint8_t ueId)
4154 {
4155    Pst pst;
4156    uint8_t ret=ROK;
4157    MacUeResetReq *ueReset = NULLP;
4158
4159    DU_ALLOC_SHRABL_BUF(ueReset, sizeof(MacUeResetReq));
4160    if(ueReset)
4161    {
4162       ueReset->cellId = cellId;
4163       ueReset->ueId   = ueId;
4164       FILL_PST_DUAPP_TO_MAC(pst, EVENT_MAC_UE_RESET_REQ);
4165
4166       DU_LOG("\nDEBUG  -->  DU_APP: Sending UE Reset Request to MAC ");
4167       ret = (*packMacUeResetReqOpts[pst.selector])(&pst, ueReset);
4168       if(ret == RFAILED)
4169       {
4170          DU_LOG("\nERROR  -->  DU_APP: sendUeResetReqToMac(): Failed to send UE Reset Req to MAC");
4171          DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, ueReset, sizeof(MacUeResetReq));
4172       }
4173    }
4174    else
4175    {
4176       DU_LOG("\nERROR  -->   DU_APP: sendUeResetReqToMac(): Failed to allocate memory"); 
4177       ret = RFAILED;
4178    }
4179    return ret;
4180 }
4181
4182 /*******************************************************************
4183  *
4184  * @brief DU processes UE reset req  and send it to MAC
4185  *
4186  * @details
4187  *
4188  *    Function : duBuildAndSendUeResetReq
4189  *
4190  *    Functionality: DU processes UE reset req and send to MAC
4191  *                   
4192  *
4193  * @params[in] cellId, crnti 
4194  * @return ROK     - success
4195  *         RFAILED - failure
4196  *
4197  * ****************************************************************/
4198
4199 uint8_t duBuildAndSendUeResetReq(uint16_t cellId, uint16_t crnti)
4200 {
4201    uint8_t  ueId =0;
4202    uint16_t cellIdx = 0;
4203
4204    DU_LOG("\nDEBUG  -->  DU_APP : Building UE reset request");
4205    GET_CELL_IDX(cellId, cellIdx);
4206    GET_UE_ID(crnti, ueId);
4207
4208    if(duCb.actvCellLst[cellIdx] != NULLP)
4209    {
4210       if(crnti != duCb.actvCellLst[cellIdx]->ueCb[ueId - 1].crnti)
4211       {
4212          DU_LOG("\nERROR  -->  DU APP : duBuildAndSendUeResetReq(): CRNTI [%d] not found", crnti);
4213          return RFAILED;
4214       }
4215
4216       duCb.actvCellLst[cellIdx]->ueCb[ueId - 1].ueState = UE_RESET_IN_PROGRESS; 
4217       if(sendUeResetReqToMac(cellId, ueId) == RFAILED)
4218       {
4219          DU_LOG("\nERROR  -->  DU APP : DuProcMacUeResetRsp(): Failed to build UE reset req for MAC ");
4220          return RFAILED;
4221       }
4222    }
4223    else
4224    {
4225       DU_LOG("\nERROR  -->  DU APP : duBuildAndSendUeResetReq(): Cell Id %d not found", cellId);
4226       return RFAILED;
4227    }
4228
4229    return ROK;
4230 }
4231
4232 /*******************************************************************
4233 *
4234 * @brief Handle UE reset response from MAC
4235 *
4236 * @details
4237 *
4238 *    Function : DuProcMacUeResetRsp
4239 *
4240 *    Functionality: Handle UE reset response from MAC
4241 *
4242 * @params[in] Pointer to MacUeResetRsp and Pst
4243 * @return ROK     - success
4244 *         RFAILED - failure
4245 *
4246 * ****************************************************************/
4247
4248 uint8_t DuProcMacUeResetRsp(Pst *pst, MacUeResetRsp *resetRsp)
4249 {
4250    uint8_t  ret =ROK;
4251    uint16_t cellIdx=0;
4252    
4253    if(resetRsp)
4254    {
4255       if(resetRsp->status == SUCCESSFUL)
4256       {
4257          DU_LOG("\nINFO   -->  DU APP : MAC UE Reset Response : SUCCESS [UE IDX : %d]", resetRsp->ueId);
4258          GET_CELL_IDX(resetRsp->cellId, cellIdx);
4259          if(duCb.actvCellLst[cellIdx])
4260          {
4261             duCb.actvCellLst[cellIdx]->ueCb[resetRsp->ueId -1].duMacUeCfg.macUeCfgState = UE_RESET_COMPLETE;
4262             /*TODO - Complete the processing after receiving successfully reset rsp*/
4263          }
4264       }
4265       else
4266       {
4267          DU_LOG("\nERROR  -->  DU APP : DuProcMacUeResetRsp(): MAC UE Reset Response : FAILURE [UE IDX : %d]",resetRsp->ueId);
4268          ret =  RFAILED;
4269       }
4270       DU_FREE_SHRABL_BUF(pst->region, pst->pool, resetRsp, sizeof(MacUeResetRsp));
4271    }
4272    else
4273    {
4274       DU_LOG("\nERROR  -->  DU APP : DuProcMacUeResetRsp(): MAC UE Reset Response is null");
4275       ret = RFAILED;
4276    }
4277    return ret;
4278 }
4279
4280 /*******************************************************************
4281 *
4282 * @brief Handle UE sync status indication from MAC
4283 *
4284 * @details
4285 *
4286 *    Function : DuProcMacUeSyncStatusInd
4287 *
4288 *    Functionality: Handle UE sync status indication from MAC
4289 *
4290 * @params[in] Pointer to MacUeSyncStatusInd and Pst
4291 * @return ROK     - success
4292 *         RFAILED - failure
4293 *
4294 * ****************************************************************/
4295
4296 uint8_t DuProcMacUeSyncStatusInd(Pst *pst, MacUeSyncStatusInd *ueSyncStatusInd)
4297 {
4298    uint8_t  ret =RFAILED;
4299    uint16_t cellIdx=0, crnti = 0;
4300    char *status;
4301
4302    if(ueSyncStatusInd)
4303    {
4304       GET_CELL_IDX(ueSyncStatusInd->cellId, cellIdx);
4305       if(duCb.actvCellLst[cellIdx])
4306       {
4307          GET_CRNTI(crnti, ueSyncStatusInd->ueId);
4308          if(duCb.actvCellLst[cellIdx]->ueCb[ueSyncStatusInd->ueId-1].crnti == crnti)
4309          {
4310             switch(ueSyncStatusInd->status)
4311             {
4312                case IN_SYNC:
4313                   status = "IN_SYNC";
4314                   break;
4315
4316                case OUT_OF_SYNC:
4317                   status = "OUT_OF_SYNC";
4318                   break;
4319
4320                case OUT_OF_SUNC_MAX_RETRIES:
4321                   status = "OUT_OF_SUNC_MAX_RETRIES";
4322                   break;
4323
4324                default:
4325                   status = "INVALID";
4326                   break;
4327                   
4328             }
4329             DU_LOG("\nINFO  -->   DU APP : MAC UE sync status for received UeId %d is %s", ueSyncStatusInd->ueId,status);
4330          }
4331          else
4332          {
4333             DU_LOG("\nERROR  -->  DU APP : DuProcMacUeSyncStatusInd(): MAC UE sync status indication : Ue Id [%d] not found",ueSyncStatusInd->cellId);
4334          }
4335       }
4336       else
4337       {
4338          DU_LOG("\nERROR  -->  DU APP : DuProcMacUeSyncStatusInd(): MAC UE sync status indication : Cell Id [%d] not found",ueSyncStatusInd->cellId);
4339       }
4340       DU_FREE_SHRABL_BUF(pst->region, pst->pool, ueSyncStatusInd, sizeof(MacUeSyncStatusInd));
4341    }
4342    else
4343    {
4344       DU_LOG("\nERROR  -->  DU APP : DuProcMacUeSyncStatusInd(): MAC UE sync status indication is null");
4345    }
4346    return ret;
4347 }
4348
4349 /*******************************************************************
4350  *
4351  * @brief Sending UE Reestablishment Req To Rlc
4352  *
4353  * @details
4354  *
4355  *    Function : sendUeReestablishReqToRlc
4356  *
4357  *    Functionality:
4358  *     Sending UE Reestablishment Req To Rlc
4359  *
4360  *  @params[in]  cellId, ueId 
4361  *  @return ROK     - success
4362  *          RFAILED - failure
4363  *
4364  *****************************************************************/
4365
4366 uint8_t sendUeReestablishReqToRlc(uint16_t cellId, uint8_t ueId, uint8_t numLcToReestablish, uint8_t *lcId)
4367 {
4368    uint8_t ret=ROK,idx=0;
4369    Pst pst;
4370    RlcUeReestablishReq *ueReestablish;
4371
4372    DU_ALLOC_SHRABL_BUF(ueReestablish, sizeof(RlcUeReestablishReq));
4373    if(ueReestablish !=NULLP)
4374    {
4375       ueReestablish->cellId = cellId;
4376       ueReestablish->ueId = ueId;
4377       ueReestablish->numLcsToReestablish = numLcToReestablish;
4378
4379       for(idx = 0;idx<numLcToReestablish; idx++)
4380       {
4381          ueReestablish->lcToReestablish[idx]= lcId[idx];
4382       }
4383       FILL_PST_DUAPP_TO_RLC(pst, RLC_UL_INST, EVENT_RLC_UE_REESTABLISH_REQ);
4384
4385       ret = (*packRlcUeReestablishReqOpts[pst.selector])(&pst, ueReestablish);
4386       if(ret == RFAILED)
4387       {
4388          DU_LOG("\nERROR  -->  DU_APP : sendUeReestablishReqToRlc():Failed to send UE Reestablishment  Req to RLC"); 
4389          DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, ueReestablish, sizeof(RlcUeReestablishReq));
4390       }
4391    }
4392    else
4393    {
4394       DU_LOG("\nERROR  -->   DU_APP: sendUeReestablishReqToRlc():Memory allocation failed");
4395       ret = RFAILED;
4396    }
4397    return ret;
4398 }
4399
4400 /*******************************************************************
4401  *
4402  * @brief DU processes UE reestablishment req and sends to MAC and RLC 
4403  *
4404  * @details
4405  *
4406  *    Function : duBuildAndSendUeReestablishReq
4407  *
4408  *    Functionality: DU processes UE reestablishment req and sends to MAC 
4409  *                   and RLC 
4410  *
4411  * @params[in] cellId, crnti, numLcToReestablish, ListOflcid 
4412  * @return ROK     - success
4413  *         RFAILED - failure
4414  *
4415  * ****************************************************************/
4416
4417 uint8_t duBuildAndSendUeReestablishReq(uint16_t cellId, uint16_t crnti, uint8_t numLcToReestablish, uint8_t *lcId)
4418 {
4419    uint8_t  ueId =0;
4420    uint16_t cellIdx = 0;
4421
4422    DU_LOG("\nDEBUG  -->  DU_APP: Building UE Reestablishment Request ");
4423    GET_CELL_IDX(cellId, cellIdx);
4424    GET_UE_ID(crnti, ueId);
4425
4426    if(duCb.actvCellLst[cellIdx] != NULLP)
4427    {
4428       if(crnti != duCb.actvCellLst[cellIdx]->ueCb[ueId - 1].crnti)
4429       {
4430          DU_LOG("\nERROR  -->  DU APP : duBuildAndSendUeReestablishReq(): CRNTI [%d] not found", crnti);
4431          return RFAILED;
4432       }
4433
4434       if(sendUeReestablishReqToRlc(cellId, ueId, numLcToReestablish, lcId) == RFAILED)
4435       {
4436          DU_LOG("\nERROR  -->  DU APP : duBuildAndSendUeReestablishReq(): Failed to send UE reestablishment req for RLC ");
4437          return RFAILED;
4438       }
4439    }
4440    else
4441    {
4442       DU_LOG("\nERROR  -->  DU APP : duBuildAndSendUeReestablishReq(): Cell Id %d is not found", cellId);
4443       return RFAILED;
4444    }
4445
4446    return ROK;
4447 }
4448
4449 /*******************************************************************
4450  *
4451  * @brief Processes UE Reestablishment Rsp received from RLC 
4452  *
4453  * @details
4454  *
4455  *    Function : DuProcRlcUeReestablishRsp
4456  *
4457  *    Functionality:
4458  *     Processes UE Reestablishment Rsp received from RLC 
4459  *
4460  *  @params[in]  Post structure
4461  *               Pointer to RlcUeReestablishRsp
4462  *  @return ROK     - success
4463  *          RFAILED - failure
4464  *
4465  * *****************************************************************/
4466
4467 uint8_t DuProcRlcUeReestablishRsp(Pst *pst, RlcUeReestablishRsp *ueReestablishRsp)
4468 {
4469    uint8_t  ueId = 0, ret = RFAILED;
4470    uint16_t cellIdx = 0,crnti=0;
4471
4472    if(ueReestablishRsp)
4473    {
4474       ueId = ueReestablishRsp->ueId;
4475       GET_CELL_IDX(ueReestablishRsp->cellId, cellIdx);
4476
4477       if(ueReestablishRsp->status == SUCCESSFUL)
4478       {
4479          if(duCb.actvCellLst[cellIdx]!=NULLP)
4480          {
4481             GET_CRNTI(crnti, ueId);
4482             if(duCb.actvCellLst[cellIdx]->ueCb[ueId-1].crnti ==  crnti)
4483             {
4484                /*TODO: complete the processing of UE Reestablishment Response */
4485                DU_LOG("\nINFO   -->  DU_APP: RLC UE Reestablishment Response : SUCCESS [UE IDX:%d]", ueId);
4486                ret = ROK;
4487             }
4488             else
4489                DU_LOG("\nERROR  -->  DU APP : duBuildAndSendUeReestablishRsp(): CRNTI [%d] not found", crnti);
4490          }
4491          else
4492             DU_LOG("\nERROR  -->  DU APP : duBuildAndSendUeReestablishRsp(): Cell Id[%d] is not found", ueReestablishRsp->cellId);
4493             
4494       }
4495       else
4496       {
4497          DU_LOG("\nERROR   -->  DU_APP: RLC UE Reestablishment Response : FAILED [UE IDX:%d]", ueId);
4498       }
4499       DU_FREE_SHRABL_BUF(pst->region, pst->pool, ueReestablishRsp, sizeof(RlcUeReestablishRsp));
4500
4501    }
4502    return ret;
4503 }
4504 /**********************************************************************
4505   End of file
4506 ***********************************************************************/