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