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