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