769f4e9a19bf4e92fd888db1d4b1f315e54d3096
[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, cellIdx = 0, crnti=0;
2704    DuUeCb *ueCb = NULLP;
2705
2706    GET_CELL_IDX(cellId, cellIdx);
2707    ueCb = &duCb.actvCellLst[cellIdx]->ueCb[ueId-1];
2708
2709    /*Filling RLC Ue Cfg */
2710    ueCb->duRlcUeCfg.cellId = cellId;
2711    ueCb->duRlcUeCfg.ueId   = ueId;
2712    ret = duUpdateRlcLcCfg(&ueCb->duRlcUeCfg, ueCb->f1UeDb);
2713    if(ret == ROK)
2714    {
2715       /*Filling MAC Ue Cfg */
2716       GET_CRNTI(crnti, ueId);
2717       ueCb->duMacUeCfg.cellId = cellId;
2718       ueCb->duMacUeCfg.ueId  = ueId;
2719       ueCb->duMacUeCfg.crnti  = crnti;
2720       ret = duUpdateMacCfg(&ueCb->duMacUeCfg, ueCb->f1UeDb);
2721       if(ret == RFAILED)
2722          DU_LOG("\nERROR  -->  DU APP : Failed while updating MAC LC Config at duUpdateDuUeCbCfg()");
2723       else
2724       {
2725          if(duUpdateTunnelCfgDb(ueId, cellId, &ueCb->f1UeDb->duUeCfg) != ROK)
2726          {
2727             DU_LOG("\nERROR  -->  DU_APP : Failed to establish tunnel in duUpdateDuUeCbCfg()");
2728             return RFAILED;
2729          }
2730       }
2731    }
2732    else
2733       DU_LOG("\nERROR  -->  DU APP : Failed while updating RLC LC Config at duUpdateDuUeCbCfg()");
2734    return ret;
2735 }
2736
2737 /*******************************************************************
2738  *
2739  * @brief Handle UE create response from MAC
2740  *
2741  * @details
2742  *
2743  *    Function : DuProcMacUeCreateRsp
2744  *
2745  *    Functionality: Handle UE Create response from MAC
2746  *
2747  * @params[in] Pointer to MacUeCreateRsp and Pst 
2748  * @return ROK     - success
2749  *         RFAILED - failure
2750  *
2751  * ****************************************************************/
2752 uint8_t DuProcMacUeCreateRsp(Pst *pst, MacUeCreateRsp *cfgRsp)
2753 {
2754    uint8_t ret = ROK;
2755    uint16_t cellIdx;
2756
2757    if(cfgRsp)
2758    {
2759       GET_CELL_IDX(cfgRsp->cellId, cellIdx);
2760       if(cfgRsp->result == MAC_DU_APP_RSP_OK)
2761       {
2762          if(pst->event == EVENT_MAC_UE_CREATE_RSP)
2763          {
2764             DU_LOG("\nINFO   -->  DU APP : MAC UE Create Response : SUCCESS [DU UE F1AP ID : %d]", cfgRsp->ueId);
2765
2766             if(duCb.actvCellLst[cellIdx] && (duCb.actvCellLst[cellIdx]->ueCb[cfgRsp->ueId -1].gnbDuUeF1apId == cfgRsp->ueId))
2767             {
2768                duCb.actvCellLst[cellIdx]->ueCb[cfgRsp->ueId -1].duMacUeCfg.macUeCfgState = UE_CREATE_COMPLETE;
2769
2770               if((duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].ueState == UE_HANDIN_IN_PROGRESS) && 
2771                     (duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].duMacUeCfg.macUeCfgState == UE_CREATE_COMPLETE) &&
2772                      (duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].duRlcUeCfg.rlcUeCfgState == UE_CREATE_COMPLETE))
2773                {
2774                   if((ret = duUpdateDuUeCbCfg(cfgRsp->ueId, cfgRsp->cellId)) == ROK)
2775                   {
2776                      /* If UE is in handover, RACH resource needs to be requested
2777                       * from MAC for CFRA */
2778                      if((duBuildAndSendRachRsrcReqToMac(cfgRsp->cellId, cfgRsp->ueId)) != ROK)
2779                      {
2780                         DU_LOG("\nERROR  --> DU APP : Failed to send RACH Resource Request to MAC");
2781                         DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(MacUeCreateRsp));
2782                         return RFAILED;
2783                      }
2784                   }
2785                   else
2786                   {
2787                      DU_LOG("\nERROR  ->  DU APP : Failure in updating DU UE CB");
2788                      DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(MacUeCreateRsp));
2789                      return RFAILED;
2790                   }
2791                }
2792             }
2793          }
2794       }
2795       else
2796       {
2797          DU_LOG("\nERROR  -->  DU APP : MAC UE CFG Response for EVENT[%d]: FAILURE [DU UE F1AP ID : %d]", pst->event, cfgRsp->ueId);
2798          ret = RFAILED;
2799       }
2800       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(MacUeCreateRsp));
2801    }
2802    else
2803    {
2804       DU_LOG("\nERROR  -->  DU APP : Received MAC Ue Config Response is NULL at DuProcMacUeCreateRsp()");
2805       ret = RFAILED;
2806    }
2807    return ret;
2808 }
2809
2810 /*******************************************************************
2811  *
2812  * @brief Handle UE Reconfig response from MAC
2813  *
2814  * @details
2815  *
2816  *    Function : DuProcMacUeRecfgRsp
2817  *
2818  *    Functionality: Handle UE ReConfig response from MAC
2819  *
2820  * @params[in] Pointer to MacUeRecfgRsp and Pst 
2821  * @return ROK     - success
2822  *         RFAILED - failure
2823  *
2824  * ****************************************************************/
2825 uint8_t DuProcMacUeRecfgRsp(Pst *pst, MacUeRecfgRsp *reCfgRsp)
2826 {
2827    uint8_t ret = ROK;
2828    uint16_t cellIdx;
2829
2830    if(reCfgRsp)
2831    {
2832       GET_CELL_IDX(reCfgRsp->cellId, cellIdx);
2833       if(reCfgRsp->result == MAC_DU_APP_RSP_OK)
2834       {
2835          if(pst->event == EVENT_MAC_UE_RECONFIG_RSP)
2836          {
2837             DU_LOG("\nINFO   -->  DU APP : MAC UE Reconfig Response : SUCCESS [DU UE F1AP ID : %d]", reCfgRsp->ueId);
2838             if(duCb.actvCellLst[cellIdx] && 
2839                   (duCb.actvCellLst[cellIdx]->ueCb[reCfgRsp->ueId -1].gnbDuUeF1apId == reCfgRsp->ueId))
2840             {
2841                duCb.actvCellLst[cellIdx]->ueCb[reCfgRsp->ueId -1].duMacUeCfg.macUeCfgState = UE_RECFG_COMPLETE;
2842                if((duCb.actvCellLst[cellIdx]->ueCb[reCfgRsp->ueId -1].duMacUeCfg.macUeCfgState == UE_RECFG_COMPLETE) &&
2843                      (duCb.actvCellLst[reCfgRsp->cellId -1]->ueCb[reCfgRsp->ueId -1].duRlcUeCfg.rlcUeCfgState == UE_RECFG_COMPLETE))
2844                {
2845                   if((ret = duUpdateDuUeCbCfg(reCfgRsp->ueId, reCfgRsp->cellId)) == ROK)
2846                   {  
2847                      if((BuildAndSendUeCtxtRsp(reCfgRsp->cellId, reCfgRsp->ueId)) != ROK)
2848                      {
2849                         DU_LOG("\nERROR  ->  DU APP : Failure in BuildAndSendUeCtxtRsp()");
2850                         DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, reCfgRsp, sizeof(MacUeRecfgRsp));
2851                         return RFAILED;
2852                      }
2853                   }
2854                   else
2855                   {
2856                      DU_LOG("\nERROR  ->  DU APP : Failure in updating DU UE CB");
2857                      DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, reCfgRsp, sizeof(MacUeRecfgRsp));
2858                      return RFAILED;
2859                   }
2860                }
2861             }
2862          }
2863       }
2864       else
2865       {
2866          DU_LOG("\nERROR  -->  DU APP : MAC UE RECFG Response for EVENT[%d]: FAILURE [DU UE F1AP ID : %d]", pst->event, reCfgRsp->ueId);
2867          if(pst->event == EVENT_MAC_UE_RECONFIG_RSP)
2868          {
2869             //TODO: Send the failure case in Ue Context Setup Response
2870          }
2871          ret = RFAILED;
2872       }
2873       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, reCfgRsp, sizeof(MacUeRecfgRsp));
2874    }
2875    else
2876    {
2877       DU_LOG("\nERROR  -->  DU APP : Received MAC Ue ReConfig Response is NULL at DuProcMacUeRecfgRsp()");
2878       ret = RFAILED;
2879    }
2880    return ret;
2881 }
2882
2883 /*******************************************************************
2884  *
2885  * @brief Processes UE create Req to RLC UL
2886  *
2887  * @details
2888  *
2889  *    Function : duBuildAndSendUeCreateReqToRlc
2890  *
2891  *    Functionality: 
2892  *     Processes UE create Req to RLC UL
2893  * 
2894  *  @params[in]  cellId,
2895  *               ueId,
2896  *               Pointer to RlcUeCreate
2897  *  @return ROK     - success
2898  *          RFAILED - failure
2899  * 
2900  *****************************************************************/
2901
2902 uint8_t duBuildAndSendUeCreateReqToRlc(uint16_t cellId, uint8_t gnbDuUeF1apId, DuUeCfg *ueCfgDb, DuRlcUeCfg *duRlcUeCfg)
2903 {
2904    uint8_t  ret = ROK;
2905    RlcUeCreate *rlcUeCfg = NULLP;
2906    Pst       pst;
2907   
2908    ret = updateRlcUeCfg(cellId, gnbDuUeF1apId, ueCfgDb, duRlcUeCfg);
2909    if(ret == RFAILED)
2910    {
2911       DU_LOG("\nERROR  -->  DU APP : Failed to fill Rlc Ue Cfg at duBuildAndSendUeCreateReqToRlc()");
2912       return ret;
2913    }
2914
2915    FILL_PST_DUAPP_TO_RLC(pst, RLC_UL_INST, EVENT_RLC_UE_CREATE_REQ);
2916    /* Copying ueCfg to a sharable buffer */
2917    DU_ALLOC_SHRABL_BUF(rlcUeCfg, sizeof(RlcUeCreate));
2918    if(rlcUeCfg)
2919    {
2920       memset(rlcUeCfg, 0, sizeof(RlcUeCreate));
2921       fillRlcUeCfg(duRlcUeCfg, rlcUeCfg);
2922
2923       /* Processing one Ue at a time to RLC */
2924       DU_LOG("\nDEBUG   -->  DU_APP: Sending UE create request to RLC UL");
2925       ret = (*packRlcUeCreateReqOpts[pst.selector])(&pst, rlcUeCfg);
2926       if(ret == RFAILED)
2927       {
2928          DU_LOG("\nERROR  -->  DU_APP : Failure in sending Ue Create Req to RLC");
2929          DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, rlcUeCfg, sizeof(RlcUeCreate));
2930          ret = RFAILED;
2931       }
2932    }
2933    else
2934    {
2935       DU_LOG("\n ERROR  -->  DU APP : Memory alloc failed at duBuildAndSendUeCreateReqToRlc()");
2936       ret = RFAILED;
2937    }
2938    return ret;
2939 }
2940
2941 /*******************************************************************
2942  *
2943  * @brief Processes UE create Rsp received from RLC UL
2944  *
2945  * @details
2946  *
2947  
2948  *    Function : DuProcRlcUeCreateRsp
2949  *
2950  *    Functionality: 
2951  *     Processes UE create Rsp received from RLC UL
2952  * 
2953  *  @params[in]  Post structure
2954  *               Pointer to RlcCfgCfm
2955  *  @return ROK     - success
2956  *          RFAILED - failure
2957  * 
2958  *****************************************************************/
2959 uint8_t DuProcRlcUeCreateRsp(Pst *pst, RlcUeCreateRsp *cfgRsp)
2960 {
2961    uint8_t ret = ROK;
2962
2963    if(cfgRsp)
2964    {
2965       if(cfgRsp->result == RLC_DU_APP_RSP_OK)
2966       {
2967          if(pst->event == EVENT_RLC_UE_CREATE_RSP)
2968          {
2969             DU_LOG("\nINFO   -->  DU_APP: RLC UE Create Response : SUCCESS [UE IDX:%d]", cfgRsp->ueId);
2970             duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].duRlcUeCfg.rlcUeCfgState = UE_CREATE_COMPLETE;
2971
2972             if((duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].ueState == UE_HANDIN_IN_PROGRESS) &&
2973                   (duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].duMacUeCfg.macUeCfgState == UE_CREATE_COMPLETE) &&
2974                   (duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].duRlcUeCfg.rlcUeCfgState == UE_CREATE_COMPLETE))
2975             {
2976                if((ret = duUpdateDuUeCbCfg(cfgRsp->ueId, cfgRsp->cellId)) == ROK)
2977                {
2978                   /* If UE is in handover, RACH resource needs to be requested
2979                    * from MAC for CFRA */
2980                   if((duBuildAndSendRachRsrcReqToMac(cfgRsp->cellId, cfgRsp->ueId)) != ROK)
2981                   {
2982                      DU_LOG("\nERROR  --> DU APP : Failed to send RACH Resource Request to MAC");
2983                      DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(RlcUeCreateRsp));
2984                      return RFAILED;
2985                   }
2986                }
2987                else
2988                {
2989                   DU_LOG("\nERROR  -->  DU APP : Failure in updating DU UE CB");
2990                   DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(RlcUeCreateRsp));
2991                   return RFAILED;
2992                }
2993             }
2994          }
2995       }
2996       else
2997       {
2998          DU_LOG("\nERROR  -->  DU_APP: RLC UE CREATE Response for EVENT[%d] : FAILED [UE IDX : %d, REASON :%d]",\
2999                pst->event, cfgRsp->ueId, cfgRsp->reason);
3000          ret = RFAILED;
3001       }
3002       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(RlcUeCreateRsp));
3003    }
3004    else
3005    {
3006       DU_LOG("\nERROR  -->  DU_APP: Received RLC Ue Create Response is NULL at DuProcRlcUeCreateRsp()");
3007       ret = RFAILED;
3008    }
3009    return ret;
3010 }
3011
3012 /*******************************************************************
3013  *
3014  * @brief Processes UE reconfig Rsp received from RLC UL
3015  *
3016  * @details
3017  *
3018  
3019  *    Function : DuProcRlcUeReconfigRsp
3020  *
3021  *    Functionality: 
3022  *     Processes UE reconfig Rsp received from RLC UL
3023  * 
3024  *  @params[in]  Post structure
3025  *               Pointer to RlcCfgCfm
3026  *  @return ROK     - success
3027  *          RFAILED - failure
3028  * 
3029  *****************************************************************/
3030 uint8_t DuProcRlcUeReconfigRsp(Pst *pst, RlcUeReconfigRsp *cfgRsp)
3031 {
3032    uint8_t ret = ROK;
3033
3034    if(cfgRsp)
3035    {
3036       if(cfgRsp->result == RLC_DU_APP_RSP_OK)
3037       {
3038          if(pst->event == EVENT_RLC_UE_RECONFIG_RSP)
3039          {
3040             DU_LOG("\nINFO   -->  DU_APP: RLC UE Reconfig Response : SUCCESS [UE IDX:%d]", cfgRsp->ueId);
3041
3042             duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].duRlcUeCfg.rlcUeCfgState = UE_RECFG_COMPLETE;
3043             if((duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].duMacUeCfg.macUeCfgState == UE_RECFG_COMPLETE) &&
3044                   (duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].duRlcUeCfg.rlcUeCfgState == UE_RECFG_COMPLETE))
3045             {
3046                if((ret = duUpdateDuUeCbCfg(cfgRsp->ueId, cfgRsp->cellId)) == ROK)
3047                {
3048                   if((BuildAndSendUeCtxtRsp(cfgRsp->cellId, cfgRsp->ueId)) != ROK)
3049                   {
3050                      DU_LOG("\nERROR  -->  DU APP : Failure in BuildAndSendUeCtxtRsp");
3051                      DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(RlcUeReconfigRsp));
3052                      return RFAILED;
3053                   }
3054                }
3055                else
3056                {
3057                   DU_LOG("\nERROR  -->  DU APP : Failure in updating DU UE CB");
3058                   DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(RlcUeReconfigRsp));
3059                   return RFAILED;
3060                }
3061             }
3062          }
3063       }
3064       else
3065       {
3066          DU_LOG("\nERROR  -->  DU_APP: RLC UE RE-CFG Response for EVENT[%d] : FAILED [UE IDX : %d, REASON :%d]",\
3067                pst->event, cfgRsp->ueId, cfgRsp->reason);
3068          if((pst->event == EVENT_RLC_UE_RECONFIG_RSP))
3069          {
3070             //TODO: update failure case in ue Context setup Response
3071          }
3072          ret = RFAILED;
3073       }
3074       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(RlcUeReconfigRsp));
3075    }
3076    else
3077    {
3078       DU_LOG("\nERROR  -->  DU_APP: Received RLC Ue ReConfig Response is NULL at DuProcRlcUeReconfigRsp()");
3079       ret = RFAILED;
3080    }
3081    return ret;
3082 }
3083 /**********************************************************************************
3084  *
3085  * @brief Fills Ue ReCfg from DU DB to RlcUeRecfg
3086  *
3087  * @details
3088  *
3089  *    Function : fillRlcUeRecfg
3090  *
3091  *    Functionality: fills ue ReCfg to be sent to RLC
3092  *
3093  * @params[in] DuRlcUeCfg Pointer
3094  *             RlcUeRecfg Pointer
3095  *
3096  * @return void 
3097  * *******************************************************************************/
3098 void fillRlcUeRecfg(DuRlcUeCfg *duRlcUeCfg, RlcUeRecfg *rlcUeRecfg)
3099 {
3100    uint8_t lcIdx = 0;
3101
3102    rlcUeRecfg->cellId = duRlcUeCfg->cellId;
3103    rlcUeRecfg->ueId   = duRlcUeCfg->ueId;
3104
3105
3106    if(duRlcUeCfg->numLcs > 0)
3107    {
3108       rlcUeRecfg->numLcsToAdd = 0;
3109       rlcUeRecfg->numLcsToMod = 0;
3110       rlcUeRecfg->numLcsToRel = 0;
3111
3112       for(lcIdx = 0; lcIdx < duRlcUeCfg->numLcs; lcIdx++)
3113       {
3114          if(duRlcUeCfg->rlcLcCfg[lcIdx].configType == CONFIG_ADD)
3115          {
3116             memcpy(&rlcUeRecfg->rlcLcCfgAdd[rlcUeRecfg->numLcsToAdd], &duRlcUeCfg->rlcLcCfg[lcIdx].rlcBearerCfg, sizeof(RlcBearerCfg));
3117             rlcUeRecfg->numLcsToAdd++;
3118          }
3119          if(duRlcUeCfg->rlcLcCfg[lcIdx].configType == CONFIG_MOD)
3120          {
3121             memcpy(&rlcUeRecfg->rlcLcCfgMod[rlcUeRecfg->numLcsToMod], &duRlcUeCfg->rlcLcCfg[lcIdx].rlcBearerCfg, sizeof(RlcBearerCfg));
3122             rlcUeRecfg->numLcsToMod++;
3123          }
3124          if(duRlcUeCfg->rlcLcCfg[lcIdx].configType == CONFIG_DEL)
3125          {
3126             memcpy(&rlcUeRecfg->rlcLcCfgRel[rlcUeRecfg->numLcsToRel], &duRlcUeCfg->rlcLcCfg[lcIdx].rlcBearerCfg, sizeof(RlcBearerCfg));
3127             rlcUeRecfg->numLcsToRel++;
3128          }
3129       }
3130    }
3131    return;
3132 }
3133
3134 /*******************************************************************
3135  *
3136  * @brief Builds and Send Ue Reconfig Req to RLC
3137  *
3138  * @details
3139  *
3140  *    Function : duBuildAndSendUeRecfgReqToRLC
3141  *
3142  *    Functionality: Builds and Send Ue Reconfig Req to RLC
3143  *
3144  * @params[in] cellId, crnti
3145  *             DuUeCfg *ueCfgDb
3146  *             RlcUeCreate *rlcUeCfg
3147  * @return ROK     - success
3148  *         RFAILED - failure
3149  *
3150  * ****************************************************************/
3151
3152 uint8_t duBuildAndSendUeRecfgReqToRlc(uint8_t cellId, uint8_t gnbDuUeF1apId, uint8_t crnti, DuUeCfg *ueCfgDb)
3153 {
3154    uint8_t ret = ROK;
3155    DuRlcUeCfg *duRlcUeCfg = NULLP;
3156    RlcUeRecfg *rlcUeRecfg = NULLP;
3157
3158    DU_ALLOC(duRlcUeCfg, sizeof(DuRlcUeCfg));
3159    DU_ALLOC_SHRABL_BUF(rlcUeRecfg, sizeof(RlcUeRecfg));
3160    if(duRlcUeCfg && rlcUeRecfg)
3161    {
3162       memset(duRlcUeCfg, 0, sizeof(DuRlcUeCfg));
3163       memset(rlcUeRecfg, 0, sizeof(RlcUeRecfg));
3164
3165       ret = updateRlcUeCfg(cellId, gnbDuUeF1apId, ueCfgDb, duRlcUeCfg);
3166
3167       if(ret == RFAILED)
3168          DU_LOG("\nERROR  -->  DU APP : Failed at duBuildAndSendUeRecfgReqToRlc()");
3169       else
3170       {
3171          fillRlcUeRecfg(duRlcUeCfg, rlcUeRecfg);
3172          ret = sendUeRecfgReqToRlc(rlcUeRecfg);
3173       }
3174    }
3175    else
3176    {
3177       DU_LOG("\nERROR  -->  DU APP : Memory Alloc failed at duBuildAndSendUeRecfgReqToRlc()");
3178       ret = RFAILED;
3179    }
3180    DU_FREE(duRlcUeCfg, sizeof(DuRlcUeCfg));
3181    return ret;
3182 }
3183
3184 /**********************************************************************************
3185  *
3186  * @brief Fills Ue ReCfg from DU DB to MacUeRecfg
3187  *
3188  * @details
3189  *
3190  *    Function : fillMacUeRecfg
3191  *
3192  *    Functionality: fills ue ReCfg to be sent to MAC
3193  *
3194  * @params[in] DuMacUeCfg Pointer
3195  *             MacUeRecfg Pointer
3196  *
3197  * @return void 
3198  * *******************************************************************************/
3199 void fillMacUeRecfg(DuMacUeCfg *duMacUeCfg, MacUeRecfg *macUeRecfg)
3200 {
3201    uint8_t lcIdx = 0;
3202
3203    macUeRecfg->cellId = duMacUeCfg->cellId;
3204    macUeRecfg->ueId = duMacUeCfg->ueId;
3205    macUeRecfg->crnti = duMacUeCfg->crnti;
3206    macUeRecfg->macCellGrpRecfgPres = duMacUeCfg->macCellGrpCfgPres;
3207    if(macUeRecfg->macCellGrpRecfgPres)
3208    {
3209       memcpy(&macUeRecfg->macCellGrpRecfg, &duMacUeCfg->macCellGrpCfg, sizeof(MacCellGrpCfg));
3210    }
3211    macUeRecfg->phyCellGrpRecfgPres = duMacUeCfg->phyCellGrpCfgPres;
3212    if(macUeRecfg->phyCellGrpRecfgPres)
3213    {
3214       memcpy(&macUeRecfg->phyCellGrpRecfg, &duMacUeCfg->phyCellGrpCfg, sizeof(PhyCellGrpCfg));
3215    }
3216    macUeRecfg->spCellRecfgPres = duMacUeCfg->spCellCfgPres;
3217    if(macUeRecfg->spCellRecfgPres)
3218    {
3219       memcpy(&macUeRecfg->spCellRecfg, &duMacUeCfg->spCellCfg, sizeof(SpCellRecfg));
3220    }
3221    if(duMacUeCfg->ambrCfg != NULLP)
3222    {
3223       DU_ALLOC_SHRABL_BUF(macUeRecfg->ambrRecfg, sizeof(AmbrCfg));
3224       memcpy(macUeRecfg->ambrRecfg, duMacUeCfg->ambrCfg, sizeof(AmbrCfg));
3225    }
3226    memcpy(&macUeRecfg->dlModInfo, &duMacUeCfg->dlModInfo, sizeof(ModulationInfo));
3227    memcpy(&macUeRecfg->ulModInfo, &duMacUeCfg->ulModInfo, sizeof(ModulationInfo));
3228    if(duMacUeCfg->numLcs > 0)
3229    {
3230       macUeRecfg->numLcsToAdd = 0;
3231       macUeRecfg->numLcsToDel = 0;
3232       macUeRecfg->numLcsToMod = 0;
3233
3234       for(lcIdx = 0; lcIdx < duMacUeCfg->numLcs; lcIdx++)
3235       {
3236          if(duMacUeCfg->lcCfgList[lcIdx].configType == CONFIG_ADD)
3237          {
3238             /*VS: To thoroughly check*/
3239             memcpy(&macUeRecfg->lcCfgAdd[macUeRecfg->numLcsToAdd], &duMacUeCfg->lcCfgList[lcIdx].lcConfig, sizeof(LcCfg));
3240             macUeRecfg->numLcsToAdd++;
3241          }
3242          if(duMacUeCfg->lcCfgList[lcIdx].configType == CONFIG_DEL)
3243          {
3244             /*VS: To thoroughly check*/
3245             macUeRecfg->lcIdToDel[macUeRecfg->numLcsToDel] = duMacUeCfg->lcCfgList[lcIdx].lcConfig.lcId;
3246             macUeRecfg->numLcsToDel++;
3247          }
3248          if(duMacUeCfg->lcCfgList[lcIdx].configType == CONFIG_MOD)
3249          {
3250             /*VS: To thoroughly check*/
3251             memcpy(&macUeRecfg->lcCfgMod[macUeRecfg->numLcsToMod], &duMacUeCfg->lcCfgList[lcIdx].lcConfig, sizeof(LcCfg));
3252             macUeRecfg->numLcsToMod++;
3253          }
3254       }
3255    }
3256    macUeRecfg->transmissionAction = duMacUeCfg->transmissionAction;
3257 #ifdef NR_DRX
3258    macUeRecfg->drxConfigIndicatorRelease = duMacUeCfg->drxConfigIndicatorRelease;
3259 #endif
3260 }
3261
3262 /*******************************************************************
3263  *
3264  * @brief Builds and Send Ue Reconfig Req to MAC
3265  *
3266  * @details
3267  *
3268  *    Function : duBuildAndSendUeRecfgReqToMac
3269  *
3270  *    Functionality: Builds and Send Ue Reconfig Req to MAC
3271  *
3272  * @params[in] CellGroupConfigRrc_t *macCellGrpCfg
3273  *             DuUeCfg *ueCfgDb
3274  * @return ROK     - success
3275  *         RFAILED - failure
3276  *
3277  * ****************************************************************/
3278
3279 uint8_t duBuildAndSendUeRecfgReqToMac(uint8_t cellId, uint8_t duUeF1apId, uint8_t crnti, DuUeCfg *ueCfgDb)
3280 {
3281    uint8_t ret = ROK;
3282    DuMacUeCfg *duMacUeCfg = NULLP;
3283    MacUeRecfg *macUeRecfg = NULLP;
3284
3285    DU_ALLOC(duMacUeCfg, sizeof(DuMacUeCfg));
3286    DU_ALLOC_SHRABL_BUF(macUeRecfg, sizeof(MacUeRecfg));
3287    if(macUeRecfg && duMacUeCfg)
3288    {
3289       memset(duMacUeCfg, 0, sizeof(DuMacUeCfg));
3290       memset(macUeRecfg, 0, sizeof(MacUeRecfg));
3291
3292       ret = updateDuMacUeCfg(cellId, duUeF1apId, crnti, ueCfgDb, duMacUeCfg);
3293       if(ret == RFAILED)
3294          DU_LOG("\nERROR  -->  DU APP : Failed to fill Mac Ue Cfg at duBuildAndSendUeRecfgReqToMac()");
3295       else
3296       {
3297          fillMacUeRecfg(duMacUeCfg, macUeRecfg);
3298          ret = sendUeRecfgReqToMac(macUeRecfg);
3299       }
3300    }
3301    else
3302    {
3303       DU_LOG("\nERROR  -->  DU APP : Memory alloc failed for macUeCfg at duBuildAndSendUeRecfgReqToMac()");
3304       ret = RFAILED;
3305    }
3306    DU_FREE(duMacUeCfg, sizeof(DuMacUeCfg));
3307
3308    return ret;
3309 }
3310
3311 /*******************************************************************
3312  *
3313  * @brief Build and Send Ue context setup request
3314  *
3315  * @details
3316  *
3317  
3318  *    Function : duBuildAndSendUeContextSetupReq
3319  *
3320  *    Functionality: 
3321  *     Build and Send Ue context setup request
3322  * 
3323  *  @params[in]  cellId, crnti, DuUeCfg pointer
3324  *  @return ROK     - success
3325  *          RFAILED - failure
3326  * 
3327  *****************************************************************/
3328
3329 uint8_t duBuildAndSendUeContextSetupReq(uint16_t cellId, DuUeCb *ueCb)
3330 {
3331    uint8_t ret = ROK;
3332    uint16_t crnti; 
3333    DuUeCfg *duUeCfg = NULLP;
3334
3335    DU_LOG("\nDEBUG   -->  DU_APP: Processing Ue Context Setup Request for cellId [%d]", cellId);
3336
3337    if(!ueCb)
3338    {
3339       DU_LOG("\nERROR  -->  DU APP : UE Cb is NULL");
3340       return RFAILED;
3341    }
3342
3343    crnti = ueCb->crnti;
3344    duUeCfg = &ueCb->f1UeDb->duUeCfg;
3345
3346    /* If UE is being handed-in to this DU, UE context setup request will create
3347     * new UE context at MAC/SCH and RLC.
3348     * If UE is in active state, UE contex setup request will lead to
3349     * reconfiguration of UE at MAC/SCH and RLC
3350     */
3351    if(ueCb->ueState == UE_HANDIN_IN_PROGRESS)
3352    {
3353       /* Filling MAC UE Config */
3354       memset(&ueCb->duMacUeCfg, 0, sizeof(DuMacUeCfg));
3355
3356       /* Since UE attach has not yet happened, crnti is unknow. Hence passing 0 */
3357       ret = duBuildAndSendUeCreateReqToMac(cellId, ueCb->gnbDuUeF1apId, 0, duUeCfg, &ueCb->duMacUeCfg);
3358       if(ret == RFAILED)
3359          DU_LOG("\nERROR  -->  DU APP : Failed to send UE create request to MAC");
3360
3361       ret = duBuildAndSendUeCreateReqToRlc(cellId, ueCb->gnbDuUeF1apId, duUeCfg, &ueCb->duRlcUeCfg);
3362       if(ret == RFAILED)
3363          DU_LOG("\nERROR  --> DU APP : Failed to send UE create request to RLC");
3364
3365    }
3366    else
3367    {
3368       /* Filling RLC UE Reconfig */ 
3369       ret = duBuildAndSendUeRecfgReqToRlc(cellId, ueCb->gnbDuUeF1apId, crnti, duUeCfg);
3370       if(ret == RFAILED)
3371          DU_LOG("\nERROR  -->  DU APP : Failed to build ctxt setup req for RLC at duBuildAndSendUeContextSetupReq()");
3372
3373       /* Filling MAC UE Reconfig */
3374       ret = duBuildAndSendUeRecfgReqToMac(cellId, ueCb->gnbDuUeF1apId, crnti, duUeCfg);
3375       if(ret == RFAILED)
3376          DU_LOG("\nERROR  -->  DU APP : Failed at build ctxt setup req for MAC at duBuildAndSendUeContextSetupReq()");
3377    }
3378
3379    return ret;
3380 }
3381
3382 /*******************************************************************
3383  *
3384  * @brief Processes DL Rsp received from RLC DL
3385  *
3386  * @details
3387  *
3388  
3389  *    Function : DuProcRlcDlRrcMsgRsp
3390  *
3391  *    Functionality: 
3392  *     Processes UE Rsp received from RLC DL
3393  * 
3394  *  @params[in]  Post structure
3395  *               Pointer to RlcCfgCfm
3396  *  @return ROK     - success
3397  *          RFAILED - failure
3398  * 
3399  *****************************************************************/
3400 uint8_t DuProcRlcDlRrcMsgRsp(Pst *pst, RlcDlRrcMsgRsp *dlRrcMsg)
3401 {
3402    uint8_t ret = ROK, ueId = 0;
3403    uint16_t cellId, crnti;
3404    DuUeCb *ueCb = NULLP;
3405    DlMsgState state;
3406
3407    state = dlRrcMsg->state;
3408    cellId = dlRrcMsg->cellId;
3409    crnti = dlRrcMsg->crnti;
3410    DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, dlRrcMsg, sizeof(RlcDlRrcMsgRsp));
3411
3412    if(state == TRANSMISSION_COMPLETE)
3413    {
3414       GET_UE_ID(crnti, ueId);
3415       ueCb = &duCb.actvCellLst[cellId -1]->ueCb[ueId -1];
3416
3417       if(ueCb->f1UeDb && ueCb->f1UeDb->dlRrcMsgPres)
3418       {
3419          if(ueCb->f1UeDb->actionType == UE_CTXT_SETUP)
3420          {
3421             ret = duBuildAndSendUeContextSetupReq(cellId, ueCb);
3422             if(ret == RFAILED)
3423                DU_LOG("\nERROR  -->  DU APP : Failed to process UE Context Setup Request in DuProcRlcDlRrcMsgRsp()");
3424          }
3425          
3426          if(ueCb->f1UeDb->actionType == UE_CTXT_MOD)
3427          {
3428             ret = duBuildAndSendUeContextModReq(cellId, ueCb->gnbDuUeF1apId, crnti, &ueCb->f1UeDb->duUeCfg);
3429             if(ret == RFAILED)
3430                DU_LOG("\nERROR  -->  DU APP : Failed to process UE Context Mod Request in DuProcRlcDlRrcMsgRsp()");
3431          }
3432
3433          if(ueCb->f1UeDb->actionType == UE_CTXT_RELEASE && ueCb->ueState == UE_ACTIVE)
3434          {
3435             ret = duBuildAndSendUeDeleteReq(cellId, crnti);
3436             if(ret == RFAILED)
3437             {
3438                DU_LOG("\nERROR  -->  DU APP : Failed to process UE Context Release Request in DuProcRlcDlRrcMsgRsp()");
3439             }
3440          }
3441       }
3442    }
3443    else
3444       DU_LOG("\nERROR  -->  DU APP : Failed to transmit DL RRC Msg");
3445
3446    return ret;
3447 }
3448 /*******************************************************************
3449  *
3450  * @brief Process UE context setup request from CU
3451  *
3452  * @details
3453  *
3454  *    Function : duProcUeContextSetupRequest 
3455  *
3456  *    Functionality: Process UE context setup request from CU
3457  *
3458  * @params[in] F1AP message
3459  * @return ROK     - success
3460  *         RFAILED - failure
3461  *
3462  * ****************************************************************/
3463
3464 uint8_t duProcUeContextSetupRequest(DuUeCb *ueCb)
3465 {
3466    uint8_t ret, cellId;
3467
3468    ret = ROK;
3469    if(ueCb)
3470    {
3471       cellId = duCb.actvCellLst[ueCb->f1UeDb->cellIdx]->cellId;
3472
3473       /* Send DL RRC msg for security Mode */
3474       if(ueCb->f1UeDb->dlRrcMsg)
3475       {
3476          if(ueCb->f1UeDb->dlRrcMsg->rrcMsgPdu)
3477          {
3478             /* Sending DL RRC Message to RLC */
3479             ret = duBuildAndSendDlRrcMsgToRlc(cellId, ueCb->duRlcUeCfg, ueCb->f1UeDb->dlRrcMsg);
3480             if(ret == RFAILED)
3481             {
3482                DU_LOG("\nERROR  -->  DU APP : Failed to send DL RRC msg in duProcUeContextSetupRequest()");
3483                DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, ueCb->f1UeDb->dlRrcMsg->rrcMsgPdu,\
3484                      ueCb->f1UeDb->dlRrcMsg->rrcMsgSize);
3485                DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, ueCb->f1UeDb->dlRrcMsg, sizeof(F1DlRrcMsg));
3486             }
3487          }
3488       }
3489       else if(ueCb->f1UeDb->actionType == UE_CTXT_SETUP)
3490       {
3491          ret = duBuildAndSendUeContextSetupReq(cellId, ueCb);
3492          if(ret == RFAILED)
3493          {
3494             DU_LOG("\nERROR  -->  DU APP : Failed to build ue context setup Req in duProcUeContextSetupRequest()");
3495          }
3496       }
3497    }
3498    else
3499    {
3500       //TODO: To send the failure cause in UeContextSetupRsp
3501       DU_LOG("ERROR  -->  DU APP : Failed to process UE CNTXT SETUP REQ at duProcUeContextSetupRequest()");
3502    }
3503    return ret;
3504 }
3505
3506 /*******************************************************************
3507  *
3508  * @brief Build and Send Ue context mod request
3509  *
3510  * @details
3511  *
3512  *
3513  *    Function : duBuildAndSendUeContextModReq
3514  *
3515  *    Functionality: 
3516  *     Build and Send Ue context mod request
3517  * 
3518  *  @params[in]  cellId, crnti, DuUeCfg pointer
3519  *  @return ROK     - success
3520  *          RFAILED - failure
3521  * 
3522  *****************************************************************/
3523
3524 uint8_t duBuildAndSendUeContextModReq(uint16_t cellId, uint8_t gnbDuUeF1apId, uint16_t crnti, DuUeCfg *duUeCfg)
3525 {
3526    uint8_t ret = ROK;
3527
3528    DU_LOG("\nDEBUG   -->  DU_APP: Processing Ue Context Mod Request for cellId [%d]", cellId);
3529    /* Filling RLC Ue Reconfig */ 
3530    ret = duBuildAndSendUeRecfgReqToRlc(cellId, gnbDuUeF1apId, crnti, duUeCfg);
3531    if(ret == RFAILED)
3532       DU_LOG("\nERROR  -->  DU APP : Failed to build ctxt setup req for RLC at duBuildAndSendUeContextModReq()");
3533    
3534    /* Filling MAC Ue Reconfig */
3535    ret = duBuildAndSendUeRecfgReqToMac(cellId, gnbDuUeF1apId, crnti, duUeCfg);
3536    if(ret == RFAILED)
3537       DU_LOG("\nERROR  -->  DU APP : Failed at build ctxt setup req for MAC at duBuildAndSendUeContextModReq()");
3538
3539    return ret;
3540 }
3541 /*******************************************************************
3542  *
3543  * @brief Processing Ue context mod request
3544  *
3545  * @details
3546  *
3547  *
3548  *    Function : duProcUeContextModReq 
3549  *
3550  *    Functionality:
3551  *        Processing  Ue context mod request
3552  *
3553  *  @params[in] DuUeCb *ueCb 
3554  *  @return ROK     - success
3555  *          RFAILED - failure
3556  *
3557  *****************************************************************/
3558
3559 uint8_t duProcUeContextModReq(DuUeCb *ueCb)
3560 {
3561    uint8_t ret, cellId;
3562
3563    ret = ROK;
3564    if(ueCb)
3565    {
3566       cellId = duCb.actvCellLst[ueCb->f1UeDb->cellIdx]->cellId;
3567       /* Send DL RRC msg for security Mode */
3568       if(ueCb->f1UeDb->dlRrcMsg)
3569       {
3570          if(ueCb->f1UeDb->dlRrcMsg->rrcMsgPdu)
3571          {
3572             /* Sending DL RRC Message to RLC */
3573             ret = duBuildAndSendDlRrcMsgToRlc(cellId, ueCb->duRlcUeCfg, ueCb->f1UeDb->dlRrcMsg);
3574             if(ret == RFAILED)
3575             {
3576                DU_LOG("\nERROR  -->  DU APP : Failed to send DL RRC msg in duProcUeContextModReq()");
3577                DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, ueCb->f1UeDb->dlRrcMsg->rrcMsgPdu,\
3578                      ueCb->f1UeDb->dlRrcMsg->rrcMsgSize);
3579                DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, ueCb->f1UeDb->dlRrcMsg, sizeof(F1DlRrcMsg));
3580             }
3581          }
3582       }
3583       else if(ueCb->f1UeDb->actionType == UE_CTXT_MOD)
3584       {
3585          ret = duBuildAndSendUeContextModReq(cellId, ueCb->gnbDuUeF1apId, ueCb->crnti, &ueCb->f1UeDb->duUeCfg);
3586          if(ret == RFAILED)
3587          {
3588             DU_LOG("\nERROR  -->  DU APP : Failed to build ue context setup Req in duProcUeContextModReq()");
3589             return RFAILED;
3590          }
3591       }
3592       else if((ueCb->f1UeDb->actionType == UE_CTXT_CFG_QUERY) || (ueCb->f1UeDb->actionType == UE_CTXT_RRC_RECFG_COMPLETE))
3593       {
3594          if((BuildAndSendUeContextModRsp(ueCb) != ROK))
3595          {
3596             DU_LOG("\nERROR  -->  DU APP : Failed to build UE Context modification response");
3597             return RFAILED;
3598          }
3599       }
3600    }
3601    else
3602    {
3603       //TODO: To send the failure cause in UeContextModRsp
3604       
3605       DU_LOG("ERROR  -->  DU APP : Failed to process UE CNTXT MOD REQ at duProcUeContextModReq()");
3606       return RFAILED;
3607    }
3608    return ROK;
3609 }
3610
3611 /*******************************************************************
3612 *
3613 * @brief Build and send dedicated RACH resource release request to MAC
3614 *
3615 * @details
3616 *
3617 *    Function : duBuildAndSendRachRsrcRelToMac
3618 *
3619 *    Functionality: Function to Build and send dedicated RACH resource 
3620 *    release request to MAC
3621 *
3622 * @params[in] Cell ID
3623 *             UE CB
3624 * @return ROK - Success
3625 *         RFAILED - Failure
3626 *
3627 * ****************************************************************/
3628 uint8_t duBuildAndSendRachRsrcRelToMac(uint16_t cellId, DuUeCb *ueCb)
3629 {
3630    Pst pst;
3631    MacRachRsrcRel *rachRsrcRel = NULLP;
3632
3633    DU_ALLOC_SHRABL_BUF(rachRsrcRel, sizeof(MacRachRsrcRel));
3634    if(!rachRsrcRel)
3635    {
3636       DU_LOG("\nERROR  -->  DU APP : Failed to allocate memory for RACH Resource Release in \
3637             duBuildAndSendRachRsrcRelToMac()");
3638       return RFAILED;
3639    }
3640
3641    rachRsrcRel->cellId = cellId;
3642    rachRsrcRel->ueId = ueCb->gnbDuUeF1apId;
3643    rachRsrcRel->crnti = ueCb->crnti;
3644
3645    /* Fill Pst */
3646    FILL_PST_DUAPP_TO_MAC(pst, EVENT_MAC_RACH_RESOURCE_REL);
3647
3648    if(((*packMacRachRsrcRelOpts[pst.selector])(&pst, rachRsrcRel)) != ROK)
3649    {
3650       DU_LOG("\nERROR  -->  DU_APP : Failure in sending RACH Resource Release to MAC at \
3651             duBuildAndSendRachRsrcRelToMac()");
3652       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, rachRsrcRel, sizeof(MacRachRsrcRel));
3653       return RFAILED;
3654    }
3655
3656    return ROK;
3657 }
3658
3659 /*******************************************************************
3660  *
3661 * @brief delete DuMacUeCfg from duCb
3662 *
3663 * @details
3664 *
3665 *    Function : deleteMacUeCfg 
3666 *
3667 *    Functionality: delete DuMacUeCfg from duCb
3668 *
3669 * @params[in] Pointer to DuMacUeCfg
3670 * @return ROK     - success
3671 *         RFAILED - failure
3672 *
3673 *******************************************************************/
3674
3675 void deleteMacUeCfg(DuMacUeCfg *ueCfg)
3676 {
3677    uint8_t lcCfgIdx=0;
3678    
3679    if(ueCfg->spCellCfgPres)
3680    {
3681       freeUeRecfgCellGrpInfo(ueCfg);
3682    }
3683    if(ueCfg->ambrCfg)
3684    {
3685       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL,ueCfg->ambrCfg, sizeof(AmbrCfg));
3686    }
3687    for(lcCfgIdx=0; lcCfgIdx< ueCfg->numLcs; lcCfgIdx++)
3688    {
3689       freeMacLcCfg(&ueCfg->lcCfgList[lcCfgIdx].lcConfig);
3690    }
3691    memset(ueCfg, 0, sizeof(DuMacUeCfg));
3692 }
3693
3694 /*******************************************************************
3695 *
3696 * @brief delete UE Configuration of a particular UE
3697 *
3698 * @details
3699 *
3700 *    Function : deleteUeCfg 
3701 *
3702 *    Functionality: delete UE Configuration of a particular UE 
3703 *
3704 * @params[in] uint16_t cellIdx, uint8_t ueId
3705 * @return ROK     - success
3706 *         RFAILED - failure
3707 *
3708 * ****************************************************************/
3709 uint8_t  deleteUeCfg(uint16_t cellId, uint8_t ueId)
3710 {
3711    uint8_t tnlIdx = 0;
3712    uint16_t cellIdx = 0;
3713    DuUeCb *ueCb = NULLP;
3714    
3715    GET_CELL_IDX(cellId, cellIdx);
3716    if(duCb.actvCellLst[cellIdx] != NULLP)
3717    {
3718       if((duCb.actvCellLst[cellIdx]->ueCb[ueId-1].duMacUeCfg.macUeCfgState == UE_DELETE_COMPLETE)\
3719             &&(duCb.actvCellLst[cellIdx]->ueCb[ueId-1].duRlcUeCfg.rlcUeCfgState == UE_DELETE_COMPLETE))
3720       {
3721          ueCb = &duCb.actvCellLst[cellIdx]->ueCb[ueId-1];
3722          deleteMacUeCfg(&ueCb->duMacUeCfg);
3723          deleteRlcUeCfg(&ueCb->duRlcUeCfg);
3724          if(ueCb->f1UeDb !=NULLP)
3725          {
3726             freeF1UeDb(ueCb->f1UeDb);
3727          }
3728          for(tnlIdx = 0; tnlIdx < duCb.numTeId; )
3729          {
3730             if(duCb.upTnlCfg[tnlIdx]->ueId == ueId)
3731             {
3732                duCb.upTnlCfg[tnlIdx]->configType = CONFIG_DEL;
3733                duProcEgtpTunnelCfg(tnlIdx, duCb.upTnlCfg[tnlIdx], duCb.upTnlCfg[tnlIdx]);
3734             }
3735             else
3736                tnlIdx++;
3737          }
3738          unsetBitInUeBitMap(cellId, ueId-1);
3739          duCb.actvCellLst[cellIdx]->numActvUes--;
3740          memset(ueCb, 0, sizeof(DuUeCb));
3741       }
3742       else
3743       {
3744          return RFAILED;
3745       }
3746    }
3747    else
3748    {
3749       DU_LOG("\nERROR  --> DU APP : deleteUeCfg(): CellIdx[%d] is not found", cellIdx);
3750       return RFAILED;
3751    }
3752    return ROK;
3753 }
3754
3755
3756 /*******************************************************************
3757 *
3758 * @brief Handle UE delete response from MAC
3759 *
3760 * @details
3761 *
3762 *    Function : DuProcMacUeDeleteRsp
3763 *
3764 *    Functionality: Handle UE delete response from MAC
3765 *
3766 * @params[in] Pointer to MacUeDeleteRsp and Pst
3767 * @return ROK     - success
3768 *         RFAILED - failure
3769 *
3770 * ****************************************************************/
3771
3772 uint8_t DuProcMacUeDeleteRsp(Pst *pst, MacUeDeleteRsp *deleteRsp)
3773 {
3774    uint8_t  ret =ROK,ueId=0;
3775    uint16_t cellIdx=0;
3776    uint32_t gnbCuUeF1apId =0 , gnbDuUeF1apId =0;
3777    
3778    if(deleteRsp)
3779    {
3780       if(deleteRsp->status == SUCCESSFUL)
3781       {
3782          DU_LOG("\nINFO   -->  DU APP : MAC UE Delete Response : SUCCESS [UE IDX : %d]", deleteRsp->ueId);
3783          GET_CELL_IDX(deleteRsp->cellId, cellIdx);
3784          if(duCb.actvCellLst[cellIdx])
3785          {
3786             duCb.actvCellLst[cellIdx]->ueCb[deleteRsp->ueId -1].duMacUeCfg.macUeCfgState = UE_DELETE_COMPLETE;
3787             ueId = deleteRsp->ueId;
3788             gnbCuUeF1apId = duCb.actvCellLst[cellIdx]->ueCb[ueId-1].gnbCuUeF1apId;
3789             gnbDuUeF1apId = duCb.actvCellLst[cellIdx]->ueCb[ueId-1].gnbDuUeF1apId;
3790             if(deleteUeCfg(deleteRsp->cellId, ueId) == ROK)
3791             {
3792                ret = BuildAndSendUeContextReleaseComplete(deleteRsp->cellId, gnbCuUeF1apId, gnbDuUeF1apId);
3793                if(ret != ROK)
3794                {
3795                   DU_LOG("\nERROR  -->  DU APP : DuProcMacUeDeleteRsp(): failed to send UE context release complete");
3796                }
3797             }
3798
3799          }
3800       }
3801       else
3802       {
3803          DU_LOG("\nERROR  -->  DU APP : DuProcMacUeDeleteRsp(): MAC UE Delete Response : FAILURE [UE IDX : %d]",\
3804          deleteRsp->ueId);
3805          ret =  RFAILED;
3806       }
3807       DU_FREE_SHRABL_BUF(pst->region, pst->pool, deleteRsp, sizeof(MacUeDeleteRsp));
3808    }
3809    else
3810    {
3811       DU_LOG("\nERROR  -->  DU APP : DuProcMacUeDeleteRsp(): MAC UE Delete Response is null");
3812       ret = RFAILED;
3813    }
3814    return ret;
3815 }
3816
3817 /*******************************************************************
3818  *
3819  * @brief Processes UE Delete Rsp received from RLC 
3820  *
3821  * @details
3822  *
3823  *    Function : DuProcRlcUeDeleteRsp
3824  *
3825  *    Functionality:
3826  *     Processes UE Delete Rsp received from RLC 
3827  *
3828  *  @params[in]  Post structure
3829  *               Pointer to RlcUeDeleteRsp
3830  *  @return ROK     - success
3831  *          RFAILED - failure
3832  *
3833  * *****************************************************************/
3834
3835 uint8_t DuProcRlcUeDeleteRsp(Pst *pst, RlcUeDeleteRsp *delRsp)
3836 {
3837    uint8_t  ueId = 0, ret = ROK;
3838    uint16_t cellIdx = 0,crnti=0;
3839
3840    if(delRsp)
3841    {
3842       ueId = delRsp->ueId;
3843       GET_CELL_IDX(delRsp->cellId, cellIdx);
3844
3845       if(delRsp->status == SUCCESSFUL)
3846       {
3847          DU_LOG("\nINFO   -->  DU_APP: RLC UE Delete Response : SUCCESS [UE IDX:%d]", ueId);
3848          if(duCb.actvCellLst[cellIdx]!=NULLP)
3849          {
3850             duCb.actvCellLst[cellIdx]->ueCb[ueId-1].duRlcUeCfg.rlcUeCfgState = UE_DELETE_COMPLETE;
3851             GET_CRNTI(crnti, ueId);
3852             if(sendUeDeleteReqToMac(delRsp->cellId, ueId, crnti) == RFAILED)
3853             {
3854                DU_LOG("\nERROR  -->  DU APP : duBuildAndSendUeDeleteReq(): Failed to build UE  delete req for MAC ");
3855                return RFAILED;
3856             }
3857
3858          }
3859       }
3860       else
3861       {
3862          DU_LOG("\nERROR   -->  DU_APP: RLC UE Delete Response : FAILED [UE IDX:%d]", ueId);
3863          ret = RFAILED;
3864       }
3865       DU_FREE_SHRABL_BUF(pst->region, pst->pool, delRsp, sizeof(RlcUeDeleteRsp));
3866
3867    }
3868    return ret;
3869 }
3870
3871 /*******************************************************************
3872  *
3873  * @brief Sending UE Delete Req To Mac
3874 *
3875 * @details
3876 *
3877 *    Function : sendUeDeleteReqToMac
3878 *
3879 *    Functionality:
3880 *     sending UE Delete Req To Mac
3881 *
3882 *  @params[in]    cellId, ueId, crnti 
3883 *  @return ROK     - success
3884 *          RFAILED - failure
3885 *
3886 *****************************************************************/
3887
3888 uint8_t sendUeDeleteReqToMac(uint16_t cellId, uint8_t ueId, uint16_t crnti)
3889 {
3890    Pst pst;
3891    uint8_t ret=ROK;
3892    MacUeDelete *ueDelete = NULLP;
3893
3894    DU_ALLOC_SHRABL_BUF(ueDelete, sizeof(MacUeDelete));
3895    if(ueDelete)
3896    {
3897       ueDelete->cellId = cellId;
3898       ueDelete->ueId   = ueId;
3899       ueDelete->crnti  = crnti;
3900       FILL_PST_DUAPP_TO_MAC(pst, EVENT_MAC_UE_DELETE_REQ);
3901
3902       DU_LOG("\nDEBUG  -->  DU_APP: Sending UE delete Request to MAC ");
3903       ret = (*packMacUeDeleteReqOpts[pst.selector])(&pst, ueDelete);
3904       if(ret == RFAILED)
3905       {
3906          DU_LOG("\nERROR  -->  DU_APP: sendUeDeleteReqToMac(): Failed to send UE delete Req to MAC");
3907          DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, ueDelete, sizeof(MacUeDelete));
3908       }
3909    }
3910    else
3911    {
3912       DU_LOG("\nERROR  -->   DU_APP: sendUeDeleteReqToMac(): Failed to allocate memory"); 
3913       ret = RFAILED;
3914    }
3915    return ret;
3916 }
3917
3918 /*******************************************************************
3919  *
3920  * @brief Sending UE Delete Req To Rlc
3921  *
3922  * @details
3923  *
3924  *    Function : sendUeDeleteReqToRlc
3925  *
3926  *    Functionality:
3927  *     Sending UE Delete Req To Rlc
3928  *
3929  *  @params[in]  cellId, ueId 
3930  *  @return ROK     - success
3931  *          RFAILED - failure
3932  *
3933  *****************************************************************/
3934
3935 uint8_t sendUeDeleteReqToRlc(uint16_t cellId, uint8_t ueId)
3936 {
3937    uint8_t ret;
3938    Pst pst;
3939    RlcUeDelete *ueDelete;
3940
3941    DU_ALLOC_SHRABL_BUF(ueDelete, sizeof(RlcUeDelete));
3942    if(ueDelete !=NULLP)
3943    {
3944       ueDelete->cellId = cellId;
3945       ueDelete->ueId = ueId;
3946       FILL_PST_DUAPP_TO_RLC(pst, RLC_UL_INST, EVENT_RLC_UE_DELETE_REQ);
3947
3948       ret = (*packRlcUeDeleteReqOpts[pst.selector])(&pst, ueDelete);
3949       if(ret == RFAILED)
3950       {
3951          DU_LOG("\nERROR  -->  DU_APP : sendUeDeleteReqToRlc():Failed to send UE Delete  Req to RLC"); 
3952          DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, ueDelete, sizeof(RlcUeDelete));
3953       }
3954    }
3955    else
3956    {
3957       DU_LOG("\nERROR  -->   DU_APP: sendUeDeleteReqToRlc():Memory allocation failed");
3958       ret = RFAILED;
3959    }
3960    return ret;
3961 }
3962
3963 /*******************************************************************
3964  *
3965  * @brief DU processes UE delete req from CU and sends to MAC and RLC 
3966  *
3967  * @details
3968  *
3969  *    Function : duBuildAndSendUeDeleteReq
3970  *
3971  *    Functionality: DU processes UE delete req from CU and sends to MAC 
3972  *                   and RLC 
3973  *
3974  * @params[in] cellId, crnti 
3975  * @return ROK     - success
3976  *         RFAILED - failure
3977  *
3978  * ****************************************************************/
3979
3980 uint8_t duBuildAndSendUeDeleteReq(uint16_t cellId, uint16_t crnti)
3981 {
3982    uint8_t  ueId =0;
3983    uint16_t cellIdx = 0;
3984
3985    DU_LOG("\nDEBUG  -->  DU_APP: Processing UE Delete Request ");
3986    GET_CELL_IDX(cellId, cellIdx);
3987    GET_UE_ID(crnti, ueId);
3988
3989    if(duCb.actvCellLst[cellIdx] != NULLP)
3990    {
3991       if(crnti != duCb.actvCellLst[cellIdx]->ueCb[ueId - 1].crnti)
3992       {
3993          DU_LOG("\nERROR  -->  DU APP : duBuildAndSendUeDeleteReq(): CRNTI [%d] not found", crnti);
3994          return RFAILED;
3995       }
3996
3997       duCb.actvCellLst[cellIdx]->ueCb[ueId - 1].ueState = UE_DELETION_IN_PROGRESS; 
3998       if(sendUeDeleteReqToRlc(cellId, ueId) == RFAILED)
3999       {
4000          DU_LOG("\nERROR  -->  DU APP : DuProcMacUeDeleteRsp():Failed to build UE  delete req for RLC ");
4001          return RFAILED;
4002       }
4003    }
4004    else
4005    {
4006       DU_LOG("\nERROR  -->  DU APP : duBuildAndSendUeDeleteReq(): Cell Id is not found");
4007       return RFAILED;
4008    }
4009
4010    return ROK;
4011 }
4012
4013 /*******************************************************************
4014  *
4015  * @brief delete RlcUeCreate from duCb
4016  *
4017  * @details
4018  *
4019  *    Function : deleteRlcUeCfg
4020  *
4021  *    Functionality:
4022  *       delete  RlcUeCreate from duCb
4023  *
4024  *  @params[in] RlcUeCreate *ueCfg 
4025  *               
4026  *  @return ROK     - success
4027  *          RFAILED - failure
4028  *
4029  *****************************************************************/
4030
4031 void deleteRlcUeCfg(DuRlcUeCfg *ueCfg)
4032 {
4033    uint8_t lcIdx = 0;
4034    RlcBearerCfg *lcCfg= NULLP;
4035    if(ueCfg)
4036    {
4037       for(lcIdx =0 ; lcIdx < ueCfg->numLcs ; lcIdx++)
4038       {
4039          lcCfg = &ueCfg->rlcLcCfg[lcIdx].rlcBearerCfg;
4040          switch(lcCfg->rlcMode)
4041          {
4042             case RLC_AM :
4043                {
4044                   DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, lcCfg->u.amCfg, sizeof(AmBearerCfg));
4045                   break;
4046                }
4047             case RLC_UM_BI_DIRECTIONAL :
4048                {
4049                   DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, lcCfg->u.umBiDirCfg, sizeof(UmBiDirBearerCfg));
4050                   break;
4051                }
4052             case RLC_UM_UNI_DIRECTIONAL_UL :
4053                {
4054                   DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, lcCfg->u.umUniDirUlCfg, sizeof(UmUniDirUlBearerCfg));
4055                   break;
4056                }
4057             case RLC_UM_UNI_DIRECTIONAL_DL :
4058                {
4059                   DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, lcCfg->u.umUniDirDlCfg, sizeof(UmUniDirDlBearerCfg));
4060                   break;
4061                }
4062          }
4063          DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, lcCfg->snssai, sizeof(Snssai));
4064       }
4065       memset(ueCfg, 0, sizeof(DuRlcUeCfg));
4066    }
4067 }
4068
4069 /*******************************************************************
4070 *
4071 * @brief Du process Ue Context Release Command
4072 *
4073 * @details
4074 *
4075 *    Function : duProcUeContextReleaseCommand 
4076 *
4077 *    Functionality: Du process Ue Context Release Command 
4078 *
4079 * @params[in] DuUeCb *duUeCb 
4080 * @return ROK     - success
4081 *         RFAILED - failure
4082 *
4083 * ****************************************************************/
4084 uint8_t duProcUeContextReleaseCommand(uint16_t cellId, DuUeCb *duUeCb)
4085 {
4086    uint8_t ret =ROK, ueId=0;
4087    uint16_t crnti = 0;
4088
4089    if(duUeCb != NULLP)
4090    {
4091       crnti = duUeCb->crnti;
4092       GET_UE_ID(crnti, ueId);
4093       
4094       if(duUeCb->f1UeDb)
4095       {
4096          /* Send DL RRC msg for RRC release */
4097          if(duUeCb->f1UeDb->dlRrcMsg)
4098          {
4099             if(duUeCb->f1UeDb->dlRrcMsg->rrcMsgPdu != NULLP)
4100             {
4101                ret = duBuildAndSendDlRrcMsgToRlc(cellId, duCb.actvCellLst[duUeCb->f1UeDb->cellIdx]->ueCb[ueId-1].duRlcUeCfg,\
4102                      duUeCb->f1UeDb->dlRrcMsg);
4103                if(ret == RFAILED)
4104                {
4105                   DU_LOG("\nERROR  -->  DU APP : duProcUeContextReleaseCommand() : Failed to send DL RRC msg");
4106                   DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, duUeCb->f1UeDb->dlRrcMsg->rrcMsgPdu,\
4107                         duUeCb->f1UeDb->dlRrcMsg->rrcMsgSize);
4108                   DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, duUeCb->f1UeDb->dlRrcMsg, sizeof(F1DlRrcMsg));
4109                }
4110             }
4111          }
4112       }
4113       else
4114       {
4115          ret = duBuildAndSendUeDeleteReq(cellId,crnti);
4116          if(ret == RFAILED)
4117          {
4118             DU_LOG("\nERROR  -->  DU APP : duProcUeContextReleaseCommand(): Failed to build and send Ue Delete request");
4119          }
4120       }
4121    }
4122    return ret;
4123 }
4124
4125 /*******************************************************************
4126  *
4127  * @brief Sending UE Reset Req To Mac
4128 *
4129 * @details
4130 *
4131 *    Function : sendUeResetReqToMac
4132 *
4133 *    Functionality:
4134 *     sending UE Reset Req To Mac
4135 *
4136 *  @params[in]    cellId, ueId, crnti 
4137 *  @return ROK     - success
4138 *          RFAILED - failure
4139 *
4140 *****************************************************************/
4141
4142 uint8_t sendUeResetReqToMac(uint16_t cellId, uint8_t ueId)
4143 {
4144    Pst pst;
4145    uint8_t ret=ROK;
4146    MacUeResetReq *ueReset = NULLP;
4147
4148    DU_ALLOC_SHRABL_BUF(ueReset, sizeof(MacUeResetReq));
4149    if(ueReset)
4150    {
4151       ueReset->cellId = cellId;
4152       ueReset->ueId   = ueId;
4153       FILL_PST_DUAPP_TO_MAC(pst, EVENT_MAC_UE_RESET_REQ);
4154
4155       DU_LOG("\nDEBUG  -->  DU_APP: Sending UE Reset Request to MAC ");
4156       ret = (*packMacUeResetReqOpts[pst.selector])(&pst, ueReset);
4157       if(ret == RFAILED)
4158       {
4159          DU_LOG("\nERROR  -->  DU_APP: sendUeResetReqToMac(): Failed to send UE Reset Req to MAC");
4160          DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, ueReset, sizeof(MacUeResetReq));
4161       }
4162    }
4163    else
4164    {
4165       DU_LOG("\nERROR  -->   DU_APP: sendUeResetReqToMac(): Failed to allocate memory"); 
4166       ret = RFAILED;
4167    }
4168    return ret;
4169 }
4170
4171 /*******************************************************************
4172  *
4173  * @brief DU processes UE reset req  and send it to MAC
4174  *
4175  * @details
4176  *
4177  *    Function : duBuildAndSendUeResetReq
4178  *
4179  *    Functionality: DU processes UE reset req and send to MAC
4180  *                   
4181  *
4182  * @params[in] cellId, crnti 
4183  * @return ROK     - success
4184  *         RFAILED - failure
4185  *
4186  * ****************************************************************/
4187
4188 uint8_t duBuildAndSendUeResetReq(uint16_t cellId, uint16_t crnti)
4189 {
4190    uint8_t  ueId =0;
4191    uint16_t cellIdx = 0;
4192
4193    DU_LOG("\nDEBUG  -->  DU_APP : Building UE reset request");
4194    GET_CELL_IDX(cellId, cellIdx);
4195    GET_UE_ID(crnti, ueId);
4196
4197    if(duCb.actvCellLst[cellIdx] != NULLP)
4198    {
4199       if(crnti != duCb.actvCellLst[cellIdx]->ueCb[ueId - 1].crnti)
4200       {
4201          DU_LOG("\nERROR  -->  DU APP : duBuildAndSendUeResetReq(): CRNTI [%d] not found", crnti);
4202          return RFAILED;
4203       }
4204
4205       duCb.actvCellLst[cellIdx]->ueCb[ueId - 1].ueState = UE_RESET_IN_PROGRESS; 
4206       if(sendUeResetReqToMac(cellId, ueId) == RFAILED)
4207       {
4208          DU_LOG("\nERROR  -->  DU APP : DuProcMacUeResetRsp(): Failed to build UE reset req for MAC ");
4209          return RFAILED;
4210       }
4211    }
4212    else
4213    {
4214       DU_LOG("\nERROR  -->  DU APP : duBuildAndSendUeResetReq(): Cell Id %d not found", cellId);
4215       return RFAILED;
4216    }
4217
4218    return ROK;
4219 }
4220
4221 /*******************************************************************
4222 *
4223 * @brief Handle UE reset response from MAC
4224 *
4225 * @details
4226 *
4227 *    Function : DuProcMacUeResetRsp
4228 *
4229 *    Functionality: Handle UE reset response from MAC
4230 *
4231 * @params[in] Pointer to MacUeResetRsp and Pst
4232 * @return ROK     - success
4233 *         RFAILED - failure
4234 *
4235 * ****************************************************************/
4236
4237 uint8_t DuProcMacUeResetRsp(Pst *pst, MacUeResetRsp *resetRsp)
4238 {
4239    uint8_t  ret =ROK;
4240    uint16_t cellIdx=0;
4241    
4242    if(resetRsp)
4243    {
4244       if(resetRsp->status == SUCCESSFUL)
4245       {
4246          DU_LOG("\nINFO   -->  DU APP : MAC UE Reset Response : SUCCESS [UE IDX : %d]", resetRsp->ueId);
4247          GET_CELL_IDX(resetRsp->cellId, cellIdx);
4248          if(duCb.actvCellLst[cellIdx])
4249          {
4250             duCb.actvCellLst[cellIdx]->ueCb[resetRsp->ueId -1].duMacUeCfg.macUeCfgState = UE_RESET_COMPLETE;
4251             /*TODO - Complete the processing after receiving successfully reset rsp*/
4252          }
4253       }
4254       else
4255       {
4256          DU_LOG("\nERROR  -->  DU APP : DuProcMacUeResetRsp(): MAC UE Reset Response : FAILURE [UE IDX : %d]",resetRsp->ueId);
4257          ret =  RFAILED;
4258       }
4259       DU_FREE_SHRABL_BUF(pst->region, pst->pool, resetRsp, sizeof(MacUeResetRsp));
4260    }
4261    else
4262    {
4263       DU_LOG("\nERROR  -->  DU APP : DuProcMacUeResetRsp(): MAC UE Reset Response is null");
4264       ret = RFAILED;
4265    }
4266    return ret;
4267 }
4268
4269 /*******************************************************************
4270 *
4271 * @brief Handle UE sync status indication from MAC
4272 *
4273 * @details
4274 *
4275 *    Function : DuProcMacUeSyncStatusInd
4276 *
4277 *    Functionality: Handle UE sync status indication from MAC
4278 *
4279 * @params[in] Pointer to MacUeSyncStatusInd and Pst
4280 * @return ROK     - success
4281 *         RFAILED - failure
4282 *
4283 * ****************************************************************/
4284
4285 uint8_t DuProcMacUeSyncStatusInd(Pst *pst, MacUeSyncStatusInd *ueSyncStatusInd)
4286 {
4287    uint8_t  ret =RFAILED;
4288    uint16_t cellIdx=0, crnti = 0;
4289    char *status;
4290
4291    if(ueSyncStatusInd)
4292    {
4293       GET_CELL_IDX(ueSyncStatusInd->cellId, cellIdx);
4294       if(duCb.actvCellLst[cellIdx])
4295       {
4296          GET_CRNTI(crnti, ueSyncStatusInd->ueId);
4297          if(duCb.actvCellLst[cellIdx]->ueCb[ueSyncStatusInd->ueId-1].crnti == crnti)
4298          {
4299             switch(ueSyncStatusInd->status)
4300             {
4301                case IN_SYNC:
4302                   status = "IN_SYNC";
4303                   break;
4304
4305                case OUT_OF_SYNC:
4306                   status = "OUT_OF_SYNC";
4307                   break;
4308
4309                case OUT_OF_SUNC_MAX_RETRIES:
4310                   status = "OUT_OF_SUNC_MAX_RETRIES";
4311                   break;
4312
4313                default:
4314                   status = "INVALID";
4315                   break;
4316                   
4317             }
4318             DU_LOG("\nINFO  -->   DU APP : MAC UE sync status for received UeId %d is %s", ueSyncStatusInd->ueId,status);
4319          }
4320          else
4321          {
4322             DU_LOG("\nERROR  -->  DU APP : DuProcMacUeSyncStatusInd(): MAC UE sync status indication : Ue Id [%d] not found",ueSyncStatusInd->cellId);
4323          }
4324       }
4325       else
4326       {
4327          DU_LOG("\nERROR  -->  DU APP : DuProcMacUeSyncStatusInd(): MAC UE sync status indication : Cell Id [%d] not found",ueSyncStatusInd->cellId);
4328       }
4329       DU_FREE_SHRABL_BUF(pst->region, pst->pool, ueSyncStatusInd, sizeof(MacUeSyncStatusInd));
4330    }
4331    else
4332    {
4333       DU_LOG("\nERROR  -->  DU APP : DuProcMacUeSyncStatusInd(): MAC UE sync status indication is null");
4334    }
4335    return ret;
4336 }
4337
4338 /*******************************************************************
4339  *
4340  * @brief Sending UE Reestablishment Req To Rlc
4341  *
4342  * @details
4343  *
4344  *    Function : sendUeReestablishReqToRlc
4345  *
4346  *    Functionality:
4347  *     Sending UE Reestablishment Req To Rlc
4348  *
4349  *  @params[in]  cellId, ueId 
4350  *  @return ROK     - success
4351  *          RFAILED - failure
4352  *
4353  *****************************************************************/
4354
4355 uint8_t sendUeReestablishReqToRlc(uint16_t cellId, uint8_t ueId, uint8_t numLcToReestablish, uint8_t *lcId)
4356 {
4357    uint8_t ret=ROK,idx=0;
4358    Pst pst;
4359    RlcUeReestablishReq *ueReestablish;
4360
4361    DU_ALLOC_SHRABL_BUF(ueReestablish, sizeof(RlcUeReestablishReq));
4362    if(ueReestablish !=NULLP)
4363    {
4364       ueReestablish->cellId = cellId;
4365       ueReestablish->ueId = ueId;
4366       ueReestablish->numLcsToReestablish = numLcToReestablish;
4367
4368       for(idx = 0;idx<numLcToReestablish; idx++)
4369       {
4370          ueReestablish->lcToReestablish[idx]= lcId[idx];
4371       }
4372       FILL_PST_DUAPP_TO_RLC(pst, RLC_UL_INST, EVENT_RLC_UE_REESTABLISH_REQ);
4373
4374       ret = (*packRlcUeReestablishReqOpts[pst.selector])(&pst, ueReestablish);
4375       if(ret == RFAILED)
4376       {
4377          DU_LOG("\nERROR  -->  DU_APP : sendUeReestablishReqToRlc():Failed to send UE Reestablishment  Req to RLC"); 
4378          DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, ueReestablish, sizeof(RlcUeReestablishReq));
4379       }
4380    }
4381    else
4382    {
4383       DU_LOG("\nERROR  -->   DU_APP: sendUeReestablishReqToRlc():Memory allocation failed");
4384       ret = RFAILED;
4385    }
4386    return ret;
4387 }
4388
4389 /*******************************************************************
4390  *
4391  * @brief DU processes UE reestablishment req and sends to MAC and RLC 
4392  *
4393  * @details
4394  *
4395  *    Function : duBuildAndSendUeReestablishReq
4396  *
4397  *    Functionality: DU processes UE reestablishment req and sends to MAC 
4398  *                   and RLC 
4399  *
4400  * @params[in] cellId, crnti, numLcToReestablish, ListOflcid 
4401  * @return ROK     - success
4402  *         RFAILED - failure
4403  *
4404  * ****************************************************************/
4405
4406 uint8_t duBuildAndSendUeReestablishReq(uint16_t cellId, uint16_t crnti, uint8_t numLcToReestablish, uint8_t *lcId)
4407 {
4408    uint8_t  ueId =0;
4409    uint16_t cellIdx = 0;
4410
4411    DU_LOG("\nDEBUG  -->  DU_APP: Building UE Reestablishment Request ");
4412    GET_CELL_IDX(cellId, cellIdx);
4413    GET_UE_ID(crnti, ueId);
4414
4415    if(duCb.actvCellLst[cellIdx] != NULLP)
4416    {
4417       if(crnti != duCb.actvCellLst[cellIdx]->ueCb[ueId - 1].crnti)
4418       {
4419          DU_LOG("\nERROR  -->  DU APP : duBuildAndSendUeReestablishReq(): CRNTI [%d] not found", crnti);
4420          return RFAILED;
4421       }
4422
4423       if(sendUeReestablishReqToRlc(cellId, ueId, numLcToReestablish, lcId) == RFAILED)
4424       {
4425          DU_LOG("\nERROR  -->  DU APP : duBuildAndSendUeReestablishReq(): Failed to send UE reestablishment req for RLC ");
4426          return RFAILED;
4427       }
4428    }
4429    else
4430    {
4431       DU_LOG("\nERROR  -->  DU APP : duBuildAndSendUeReestablishReq(): Cell Id %d is not found", cellId);
4432       return RFAILED;
4433    }
4434
4435    return ROK;
4436 }
4437
4438 /*******************************************************************
4439  *
4440  * @brief Processes UE Reestablishment Rsp received from RLC 
4441  *
4442  * @details
4443  *
4444  *    Function : DuProcRlcUeReestablishRsp
4445  *
4446  *    Functionality:
4447  *     Processes UE Reestablishment Rsp received from RLC 
4448  *
4449  *  @params[in]  Post structure
4450  *               Pointer to RlcUeReestablishRsp
4451  *  @return ROK     - success
4452  *          RFAILED - failure
4453  *
4454  * *****************************************************************/
4455
4456 uint8_t DuProcRlcUeReestablishRsp(Pst *pst, RlcUeReestablishRsp *ueReestablishRsp)
4457 {
4458    uint8_t  ueId = 0, ret = RFAILED;
4459    uint16_t cellIdx = 0,crnti=0;
4460
4461    if(ueReestablishRsp)
4462    {
4463       ueId = ueReestablishRsp->ueId;
4464       GET_CELL_IDX(ueReestablishRsp->cellId, cellIdx);
4465
4466       if(ueReestablishRsp->status == SUCCESSFUL)
4467       {
4468          if(duCb.actvCellLst[cellIdx]!=NULLP)
4469          {
4470             GET_CRNTI(crnti, ueId);
4471             if(duCb.actvCellLst[cellIdx]->ueCb[ueId-1].crnti ==  crnti)
4472             {
4473                /*TODO: complete the processing of UE Reestablishment Response */
4474                DU_LOG("\nINFO   -->  DU_APP: RLC UE Reestablishment Response : SUCCESS [UE IDX:%d]", ueId);
4475                ret = ROK;
4476             }
4477             else
4478                DU_LOG("\nERROR  -->  DU APP : duBuildAndSendUeReestablishRsp(): CRNTI [%d] not found", crnti);
4479          }
4480          else
4481             DU_LOG("\nERROR  -->  DU APP : duBuildAndSendUeReestablishRsp(): Cell Id[%d] is not found", ueReestablishRsp->cellId);
4482             
4483       }
4484       else
4485       {
4486          DU_LOG("\nERROR   -->  DU_APP: RLC UE Reestablishment Response : FAILED [UE IDX:%d]", ueId);
4487       }
4488       DU_FREE_SHRABL_BUF(pst->region, pst->pool, ueReestablishRsp, sizeof(RlcUeReestablishRsp));
4489
4490    }
4491    return ret;
4492 }
4493 /**********************************************************************
4494   End of file
4495 ***********************************************************************/