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