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