[Task-ID: ODUHIGH-455] Changes to support new L1 20.11
[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, RlcUeCfg 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    DU_ALLOC_SHRABL_BUF(dlRrcMsgInfo, sizeof(RlcDlRrcMsgInfo));
393    if(!dlRrcMsgInfo)
394    {
395       DU_LOG("\nERROR  -->  DU APP : Memory allocation failed for dlRrcMsgInfo in \
396          duBuildAndSendDlRrcMsgToRlc");
397       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, f1DlRrcMsg->rrcMsgPdu, f1DlRrcMsg->rrcMsgSize);
398       return RFAILED;
399    }
400    
401    /* Filling up the RRC msg info */
402    dlRrcMsgInfo->cellId = cellId;
403    dlRrcMsgInfo->ueId = ueCfg.ueId;
404    for(lcIdx = 0; lcIdx <= MAX_NUM_LC; lcIdx++)
405    {
406       if(ueCfg.rlcLcCfg[lcIdx].lcId == f1DlRrcMsg->srbId)
407       {
408          dlRrcMsgInfo->rbType = ueCfg.rlcLcCfg[lcIdx].rbType;
409          dlRrcMsgInfo->rbId   = ueCfg.rlcLcCfg[lcIdx].rbId;
410          dlRrcMsgInfo->lcType = ueCfg.rlcLcCfg[lcIdx].lcType;
411          dlRrcMsgInfo->lcId   = ueCfg.rlcLcCfg[lcIdx].lcId;
412          break;
413       }
414    }
415    dlRrcMsgInfo->execDup = f1DlRrcMsg->execDup;
416    dlRrcMsgInfo->deliveryStaRpt = f1DlRrcMsg->deliveryStatRpt;
417    dlRrcMsgInfo->msgLen = f1DlRrcMsg->rrcMsgSize;
418    dlRrcMsgInfo->rrcMsg = f1DlRrcMsg->rrcMsgPdu;
419
420    /* Filling post structure and sending msg */ 
421    FILL_PST_DUAPP_TO_RLC(pst, RLC_DL_INST, EVENT_DL_RRC_MSG_TRANS_TO_RLC);
422    DU_LOG("\nDEBUG   -->  DU_APP: Sending Dl RRC Msg to RLC \n");
423    ret = (*duSendDlRrcMsgToRlcOpts[pst.selector])(&pst, dlRrcMsgInfo);
424    if(ret != ROK)
425    {
426       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, f1DlRrcMsg->rrcMsgPdu, f1DlRrcMsg->rrcMsgSize);
427       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, dlRrcMsgInfo, sizeof(RlcDlRrcMsgInfo));
428       return RFAILED;
429    }
430
431    return ROK;
432
433
434
435
436 /******************************************************************
437  *
438  * @brief Process DL RRC Msg recevied from F1AP
439  *
440  * @details
441  *
442  *    Function : duProcDlRrcMsg
443  *
444  *    Functionality: Process DL RRC Msg recevied from F1AP
445  *
446  * @params[in] dlCcchMsg - uint8_t*
447  * @return ROK     - success
448  *         RFAILED - failure
449  *
450  * ****************************************************************/
451
452 uint8_t duProcDlRrcMsg(F1DlRrcMsg *dlRrcMsg)
453 {
454    uint8_t ueIdx, ret;
455    uint16_t crnti, cellId, cellIdx;
456    bool ueCcchCtxtFound = false;
457    bool ueFound = false;
458
459    ret = ROK;
460
461    if(dlRrcMsg->srbId == SRB0_LCID) //RRC connection setup
462    {
463       for(ueIdx=0; ueIdx<duCb.numUe; ueIdx++)
464       {
465          if(dlRrcMsg->gnbDuUeF1apId == duCb.ueCcchCtxt[ueIdx].gnbDuUeF1apId)
466          {
467             ueCcchCtxtFound = true;
468             crnti  = duCb.ueCcchCtxt[ueIdx].crnti;
469             cellId = duCb.ueCcchCtxt[ueIdx].cellId;
470             break;
471          }
472       }
473    }
474    if(ueCcchCtxtFound)
475    {
476       ret = duBuildAndSendDlCcchInd(&cellId, &crnti, RRC_SETUP, dlRrcMsg->rrcMsgSize, dlRrcMsg->rrcMsgPdu);
477       if(ret == RFAILED)
478       {
479          DU_LOG("\nERROR  -->  DU APP : Failed to build DlCcch Ind at procDlRrcMsgTrans()");
480       }
481       else
482       {
483          if(duCb.actvCellLst[cellId-1] == NULLP)
484          {
485             DU_LOG("\nERROR  -->  DU APP : cellId [%d] does not exist", cellId);
486             ret = RFAILED;
487          }
488          if(duCb.actvCellLst[cellId-1]->numActvUes < MAX_NUM_UE)
489          {
490             ret = duCreateUeCb(&duCb.ueCcchCtxt[ueIdx], dlRrcMsg->gnbCuUeF1apId);
491             if(ret == RFAILED)
492             {
493                DU_LOG("\nERROR  -->  DU APP : Failed to createUeCb for cellId [%d] at procDlRrcMsgTrans()", \
494                      duCb.ueCcchCtxt[ueIdx].cellId);
495             }
496          }
497          else
498          {
499             DU_LOG("\nERROR   -->  DU_APP: Max Active UEs has reached at procDlRrcMsgTrans()");
500             ret = RFAILED;
501          }
502       }
503    }
504    else
505    {
506       for(cellIdx = 0; cellIdx < MAX_NUM_CELL; cellIdx++)
507       {
508          for(ueIdx = 0 ; ueIdx < MAX_NUM_UE; ueIdx++)
509          {
510             if(duCb.actvCellLst[cellIdx] && (dlRrcMsg->gnbCuUeF1apId == duCb.actvCellLst[cellIdx]->ueCb[ueIdx].gnbCuUeF1apId)
511                   && (dlRrcMsg->gnbDuUeF1apId == duCb.actvCellLst[cellIdx]->ueCb[ueIdx].gnbDuUeF1apId))
512             {
513                ueFound = true;
514                ret = duBuildAndSendDlRrcMsgToRlc(duCb.actvCellLst[cellIdx]->cellId, \
515                      duCb.actvCellLst[cellIdx]->ueCb[ueIdx].rlcUeCfg, dlRrcMsg);
516                break; 
517             }
518          }
519          if(ueFound)
520             break;
521       }
522       if(!ueFound)
523       {
524          DU_LOG("\nERROR   -->  DU_APP: UE ID [%d] not found", dlRrcMsg->gnbDuUeF1apId);
525          ret = RFAILED;
526       }
527    }
528    return ret;
529 }
530
531 /******************************************************************
532  *
533  * @brief Processes UL CCCH Ind recvd from MAC
534  *
535  * @details
536  *
537  *    Function : duProcUlCcchInd
538  *
539  *    Functionality: Processes UL CCCH Ind recvd from MAC
540  *
541  * @params[in] UlCcchIndInfo *ulCcchIndInfo
542  * @return ROK     - success
543  *         RFAILED - failure
544  *
545  * ****************************************************************/
546 uint8_t duProcUlCcchInd(UlCcchIndInfo *ulCcchIndInfo)
547 {
548
549    uint8_t ret = ROK;
550    int32_t gnbDuUeF1apId = 0;
551
552    if(ulCcchIndInfo->crnti)
553    {
554       GET_UE_ID(ulCcchIndInfo->crnti, gnbDuUeF1apId);
555    }
556    else
557    {
558       DU_LOG("\nERROR  -->  DU_APP : Received invalid CRNTI [%d] ", ulCcchIndInfo->crnti);
559       return RFAILED;
560    }
561    
562    /* Store Ue mapping */
563    duCb.ueCcchCtxt[duCb.numUe].gnbDuUeF1apId = (uint32_t)gnbDuUeF1apId;
564    duCb.ueCcchCtxt[duCb.numUe].crnti         = ulCcchIndInfo->crnti;
565    duCb.ueCcchCtxt[duCb.numUe].cellId        = ulCcchIndInfo->cellId;
566
567    duCb.numUe++;
568
569    ret = (BuildAndSendInitialRrcMsgTransfer(gnbDuUeF1apId, ulCcchIndInfo->crnti, ulCcchIndInfo->ulCcchMsgLen,
570             ulCcchIndInfo->ulCcchMsg));
571    if(ret != ROK)
572    {
573       DU_LOG("\nERROR  -->  DU_APP : BuildAndSendInitialRrcMsgTransfer failed");
574    }
575
576    DU_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, ulCcchIndInfo->ulCcchMsg, ulCcchIndInfo->ulCcchMsgLen);
577    DU_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, ulCcchIndInfo, sizeof(UlCcchIndInfo));
578
579    return ret;
580
581 }
582
583 /******************************************************************
584  *
585  * @brief Fills Default UL LC Cfg
586  *
587  * @details
588  *
589  *    Function : fillDefaultUlLcCfg
590  *
591  *    Functionality: Fills Default UL LC Cfg
592  *
593  * @params[in]  UlLcCfg *ulLcCfg 
594  * @return void
595  *****************************************************************/
596 void fillDefaultUlLcCfg(UlLcCfg *ulLcCfg)
597 {
598    ulLcCfg->priority = LC_PRIORITY_1;
599    ulLcCfg->lcGroup =  0;
600    ulLcCfg->schReqId = 0;
601    ulLcCfg->pbr = PBR_KBPS_INFINITY;
602    ulLcCfg->bsd = BSD_MS_1000;
603 }
604
605 /******************************************************************
606  *
607  * @brief Fills Initial DL Bandwidth Part
608  *
609  * @details
610  *
611  *    Function : fillDefaultInitDlBwp
612  *
613  *    Functionality: Fills Initial DL Bandwidth Part
614  *
615  * @params[in]  InitialDlBwp *initDlBwp
616  * @return ROK - success
617  *         RFAILED - failure
618  *
619  *****************************************************************/
620 uint8_t fillDefaultInitDlBwp(InitialDlBwp *initDlBwp)
621 {
622    uint8_t idx = 0;
623    uint8_t freqDomainResource[FREQ_DOM_RSRC_SIZE] = {0};
624    uint8_t coreset0EndPrb, coreset1StartPrb, coreset1NumPrb;
625
626    if(initDlBwp)
627    {
628       /* Filling PDCCH Config */
629       initDlBwp->pdcchPresent = TRUE;
630       if(initDlBwp->pdcchPresent)
631       {
632          initDlBwp->pdcchCfg.numCRsetToAddMod = PDCCH_CTRL_RSRC_SET_ONE_ID;
633          memset(initDlBwp->pdcchCfg.cRSetToAddModList, 0, MAX_NUM_CRSET);
634          if(initDlBwp->pdcchCfg.numCRsetToAddMod <= MAX_NUM_CRSET)
635          {
636             initDlBwp->pdcchCfg.cRSetToAddModList[idx].cRSetId = PDCCH_CTRL_RSRC_SET_ONE_ID;
637             memset(initDlBwp->pdcchCfg.cRSetToAddModList[idx].freqDomainRsrc, 0, FREQ_DOM_RSRC_SIZE); 
638             coreset0EndPrb = CORESET0_END_PRB;
639             coreset1StartPrb = coreset0EndPrb +6;
640             coreset1NumPrb = CORESET1_NUM_PRB;
641             /* calculate the PRBs */
642             fillCoresetFeqDomAllocMap(((coreset1StartPrb)/6), (coreset1NumPrb/6), freqDomainResource);
643             memcpy(initDlBwp->pdcchCfg.cRSetToAddModList[idx].freqDomainRsrc, freqDomainResource, FREQ_DOM_RSRC_SIZE);
644
645             initDlBwp->pdcchCfg.cRSetToAddModList[idx].duration = PDCCH_CTRL_RSRC_SET_ONE_DURATION;
646             initDlBwp->pdcchCfg.cRSetToAddModList[idx].cceRegMappingType = CCE_REG_MAPPINGTYPE_PR_NONINTERLEAVED;
647             initDlBwp->pdcchCfg.cRSetToAddModList[idx].precoderGranularity = ALL_CONTIGUOUS_RBS;
648             initDlBwp->pdcchCfg.cRSetToAddModList[idx].dmrsScramblingId = SCRAMBLING_ID;
649          }
650          initDlBwp->pdcchCfg.numCRsetToRel = 0;
651
652          /* Filling Serach Space */
653          initDlBwp->pdcchCfg.numSearchSpcToAddMod = PDCCH_CTRL_RSRC_SET_ONE_ID;
654          memset(initDlBwp->pdcchCfg.searchSpcToAddModList, 0, MAX_NUM_CRSET);
655          if(initDlBwp->pdcchCfg.numSearchSpcToAddMod <= MAX_NUM_CRSET)
656          {
657             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].searchSpaceId = PDCCH_SRCH_SPC_TWO_ID;
658             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].cRSetId = PDCCH_CTRL_RSRC_SET_ONE_ID;
659             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].mSlotPeriodicityAndOffset = SLOTPERIODICITYANDOFFSET_PR_SL1;
660             memset(initDlBwp->pdcchCfg.searchSpcToAddModList[idx].mSymbolsWithinSlot, 0, MONITORING_SYMB_WITHIN_SLOT_SIZE);
661             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].mSymbolsWithinSlot[idx] = PDCCH_SYMBOL_WITHIN_SLOT;
662             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].numCandidatesAggLevel1 = AGGREGATIONLEVEL_N8; 
663             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].numCandidatesAggLevel2 = AGGREGATIONLEVEL_N8; 
664             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].numCandidatesAggLevel4 = AGGREGATIONLEVEL_N4; 
665             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].numCandidatesAggLevel8 = AGGREGATIONLEVEL_N2; 
666             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].numCandidatesAggLevel16 = AGGREGATIONLEVEL_N1;
667             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].searchSpaceType = SEARCHSPACETYPE_PR_UE_SPECIFIC;
668             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].ueSpecificDciFormat = PDCCH_SRCH_SPC_TWO_UE_SPEC_DCI_FORMAT;
669          }
670          initDlBwp->pdcchCfg.numSearchSpcToRel = 0;
671       }
672
673       /* Filling PDSCH Config */
674       initDlBwp->pdschPresent = TRUE;
675       if(initDlBwp->pdschPresent)
676       {
677          initDlBwp->pdschCfg.dmrsDlCfgForPdschMapTypeA.addPos = ADDITIONALPOSITION_POS0;
678          initDlBwp->pdschCfg.resourceAllocType = RESOURCEALLOCATION_TYPE1;
679
680          initDlBwp->pdschCfg.numTimeDomRsrcAlloc = NUM_TIME_DOM_RSRC_ALLOC;
681
682          idx = 0; 
683          initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].mappingType = MAPPING_TYPEA;
684          initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].startSymbol = PDSCH_START_SYMBOL; 
685          initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].symbolLength = PDSCH_LENGTH_SYMBOL;
686          initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].startSymbolAndLength = calcSliv(PDSCH_START_SYMBOL, PDSCH_LENGTH_SYMBOL);
687
688          idx++;
689          DU_ALLOC_SHRABL_BUF(initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].k0, sizeof(uint8_t));
690          if(initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].k0 == NULLP)
691          {
692             DU_LOG("\nERROR  -->  DUAPP : Failed to allocate memory to K0 in fillDefaultInitDlBwp");
693             return RFAILED;
694          }
695          if(initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].k0)
696             *(initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].k0) = 1;
697          initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].mappingType = MAPPING_TYPEA;
698          initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].startSymbol = PDSCH_START_SYMBOL; 
699          initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].symbolLength = PDSCH_LENGTH_SYMBOL;
700          initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].startSymbolAndLength = calcSliv(PDSCH_START_SYMBOL, PDSCH_LENGTH_SYMBOL);
701
702          initDlBwp->pdschCfg.rbgSize = RBG_SIZE_CONFIG1;
703          initDlBwp->pdschCfg.numCodeWordsSchByDci = CODEWORDS_SCHED_BY_DCI_N1;
704          initDlBwp->pdschCfg.bundlingType = TYPE_STATIC_BUNDLING;
705          initDlBwp->pdschCfg.bundlingInfo.StaticBundling.size = 0;
706       }
707    }
708
709    return ROK;
710 }
711
712 /******************************************************************
713  *
714  * @brief Fills Initial UL Bandwidth Part
715  *
716  * @details
717  *
718  *    Function : fillDefaultInitUlBwp
719  *
720  *    Functionality: Fills Initial UL Bandwidth Part
721  *
722  * @params[in]  InitialUlBwp *initUlBwp
723  * @return void
724  *
725  *****************************************************************/
726 void fillDefaultInitUlBwp(InitialUlBwp *initUlBwp)
727 {
728    uint8_t idx;
729    if(initUlBwp)
730    {
731       initUlBwp->pucchPresent = FALSE;
732
733       /*Filling PUSCH Config */
734       initUlBwp->puschPresent = TRUE;
735       if(initUlBwp->puschPresent)
736       {
737          initUlBwp->puschCfg.dataScramblingId = SCRAMBLING_ID;
738          initUlBwp->puschCfg.dmrsUlCfgForPuschMapTypeA.addPos = ADDITIONALPOSITION_POS0; 
739          initUlBwp->puschCfg.dmrsUlCfgForPuschMapTypeA.transPrecodDisabled. \
740             scramblingId0 = SCRAMBLING_ID; 
741          initUlBwp->puschCfg.resourceAllocType = RESOURCEALLOCATION_TYPE1;
742          initUlBwp->puschCfg.numTimeDomRsrcAlloc = 2;
743          idx = 0;
744          if(initUlBwp->puschCfg.numTimeDomRsrcAlloc <= MAX_NUM_UL_ALLOC)
745          {
746             initUlBwp->puschCfg.timeDomRsrcAllocList[idx].k2 = PUSCH_K2_CFG1;
747             initUlBwp->puschCfg.timeDomRsrcAllocList[idx].mappingType =\
748                MAPPING_TYPEA;
749             initUlBwp->puschCfg.timeDomRsrcAllocList[idx].startSymbol = PUSCH_START_SYMBOL;
750             initUlBwp->puschCfg.timeDomRsrcAllocList[idx].symbolLength = PUSCH_LENGTH_SYMBOL;
751             initUlBwp->puschCfg.timeDomRsrcAllocList[idx].startSymbolAndLength =\
752                calcSliv(PUSCH_START_SYMBOL, PUSCH_LENGTH_SYMBOL);
753
754        idx++;
755        initUlBwp->puschCfg.timeDomRsrcAllocList[idx].k2 = PUSCH_K2_CFG2;
756        initUlBwp->puschCfg.timeDomRsrcAllocList[idx].mappingType = MAPPING_TYPEA;
757        initUlBwp->puschCfg.timeDomRsrcAllocList[idx].startSymbol = PUSCH_START_SYMBOL;
758        initUlBwp->puschCfg.timeDomRsrcAllocList[idx].symbolLength = PUSCH_LENGTH_SYMBOL;
759        initUlBwp->puschCfg.timeDomRsrcAllocList[idx].startSymbolAndLength =\
760           calcSliv(PUSCH_START_SYMBOL, PUSCH_LENGTH_SYMBOL);
761          }
762          initUlBwp->puschCfg.transformPrecoder = TRANSFORM_PRECODER_DISABLED;
763       }
764    }
765    else
766    {
767       DU_LOG("\nERROR  -->  DU APP : Memory is NULL of InitalUlBwp");
768    }
769
770 }
771 /******************************************************************
772  *
773  * @brief Fills SpCell Group Info
774  *
775  * @details
776  *
777  *    Function : fillDefaultSpCellGrpInfo
778  *
779  *    Functionality: Fills Sp Cell Group Info
780  *
781  * @params[in]  MacUeCfg *macUeCfg
782  * @return ROK - Success
783  *         RFAILED - Failure
784  *
785  *****************************************************************/
786 uint8_t fillDefaultSpCellGrpInfo(MacUeCfg *macUeCfg)
787 {
788    SpCellCfg *spCell = NULL;
789
790    if(macUeCfg)
791       spCell = &macUeCfg->spCellCfg;
792
793    if(spCell)
794    {
795       macUeCfg->spCellCfgPres = true;
796
797       spCell->servCellIdx = SERV_CELL_IDX;
798       /* Filling Initial Dl Bwp */
799       if((fillDefaultInitDlBwp(&spCell->servCellCfg.initDlBwp)) != ROK)
800       {
801          DU_LOG("\nERROR  -->  DUAPP : Failed in fillDefaultInitDlBwp");
802          return RFAILED;
803       }
804
805       spCell->servCellCfg.numDlBwpToAdd    = 0; 
806       spCell->servCellCfg.firstActvDlBwpId = ACTIVE_DL_BWP_ID;
807       spCell->servCellCfg.defaultDlBwpId   = ACTIVE_DL_BWP_ID;
808       spCell->servCellCfg.bwpInactivityTmr = NULLP;
809       spCell->servCellCfg.pdschServCellCfg.maxMimoLayers = NULLP;
810       spCell->servCellCfg.pdschServCellCfg.maxCodeBlkGrpPerTb = NULLP;
811       spCell->servCellCfg.pdschServCellCfg.codeBlkGrpFlushInd = NULLP;
812       spCell->servCellCfg.pdschServCellCfg.xOverhead = NULLP;
813       spCell->servCellCfg.pdschServCellCfg.numHarqProcForPdsch = NUM_HARQ_PROC_FOR_PDSCH_N_16;
814
815       /* Filling Initial UL Bwp*/
816       fillDefaultInitUlBwp(&spCell->servCellCfg.initUlBwp);
817       spCell->servCellCfg.numUlBwpToAdd     = 0; 
818       spCell->servCellCfg.firstActvUlBwpId  = ACTIVE_DL_BWP_ID; 
819    }
820    else
821    {
822       DU_LOG("\nERROR  -->  DU APP : Memory is NULL for SpCellGrp");
823       return RFAILED;
824    }
825    return ROK;
826 }
827
828 /******************************************************************
829  *
830  * @brief Fills Physical Cell Group Info
831  *
832  * @details
833  *
834  *    Function : fillDefaultPhyCellGrpInfo
835  *
836  *    Functionality: Fills Physical Cell Group Info
837  *
838  * @params[in]  MacUeCfg *macUeCfg
839  * @return void
840  *
841  *****************************************************************/
842 void fillDefaultPhyCellGrpInfo(MacUeCfg *macUeCfg)
843 {
844    PhyCellGrpCfg *cellGrp = NULL;
845
846    if(macUeCfg)
847       cellGrp = &macUeCfg->phyCellGrpCfg;
848
849    if(cellGrp)
850    {
851       macUeCfg->phyCellGrpCfgPres = true;
852       cellGrp->pdschHarqAckCodebook = PDSCH_HARQ_ACK_CODEBOOK_DYNAMIC;
853       cellGrp->pNrFr1 = P_NR_FR1;
854    }
855    else
856    {
857       DU_LOG("\nERROR  -->  DU APP : Memory is NULL for Physical Cell Group");
858    }
859 }
860
861 /******************************************************************
862  *
863  * @brief Fills Mac Cell Group Info
864  *
865  * @details
866  *
867  *    Function : fillDefaultMacCellGrpInfo
868  *
869  *    Functionality: Fills Mac Cell Group Info
870  *
871  * @params[in]  MacUeCfg *macUeCfg
872  * @return void
873  *
874  *****************************************************************/
875 void fillDefaultMacCellGrpInfo(MacUeCfg *macUeCfg)
876 {
877    uint8_t idx;
878    MacCellGrpCfg *cellGrp = NULL;
879
880    if(macUeCfg)
881       cellGrp = &macUeCfg->macCellGrpCfg;
882
883    if(cellGrp)
884    {
885       macUeCfg->macCellGrpCfgPres = true;
886
887       /* Filling Scheduling Request Config */
888       cellGrp->schReqCfg.addModListCount = 1;
889       if(cellGrp->schReqCfg.addModListCount <= MAX_NUM_SR_CFG_PER_CELL_GRP)
890       {
891          for(idx = 0; idx < cellGrp->schReqCfg.addModListCount; idx++)
892          {
893             cellGrp->schReqCfg.addModList[idx].schedReqId    = SCH_REQ_ID;
894             cellGrp->schReqCfg.addModList[idx].srProhibitTmr = SR_PROHIBIT_MS_32;
895             cellGrp->schReqCfg.addModList[idx].srTransMax    = SR_TRANS_MAX_N_16;
896          }
897       }
898       cellGrp->schReqCfg.relListCount = 0;
899
900       /* Filling Tag config */
901       cellGrp->tagCfg.addModListCount = 1;
902       if(cellGrp->tagCfg.addModListCount <= MAC_NUM_TAGS)
903       {
904          for(idx = 0; idx < cellGrp->tagCfg.addModListCount; idx++)
905          {
906             cellGrp->tagCfg.addModList[idx].tagId = TAG_ID;
907             cellGrp->tagCfg.addModList[idx].timeAlignTimer = TIME_ALIGNMENT_TIMER_INFINITY;
908          }
909       }
910       cellGrp->tagCfg.relListCount = 0;
911
912       /* Filling BSR config */
913       cellGrp->bsrTmrCfg.periodicTimer = BSR_PERIODIC_TIMER_SF_10;
914       cellGrp->bsrTmrCfg.retxTimer = BSR_RETX_TIMER_SF_320;
915       cellGrp->bsrTmrCfg.srDelayTimer = BSR_SR_DELAY_TMR_2560;
916
917       /* Filling PHR config */
918       cellGrp->phrCfgSetupPres = true;
919       cellGrp->phrCfg.periodicTimer = PHR_PERIODIC_TIMER_INFINITY;
920       cellGrp->phrCfg.prohibitTimer = PHR_PROHIBIT_TIMER_SF_0;
921       cellGrp->phrCfg.txPowerFactor = PHR_TX_PWR_FACTOR_CHANGE_INFINITY;
922       cellGrp->phrCfg.multiplePHR   = false;
923       cellGrp->phrCfg.dummy         = false;
924       cellGrp->phrCfg.phrType2OtherCell = false;
925       cellGrp->phrCfg.phrOtherCG = PHR_MODE_OTHER_CG_REAL;  
926
927    }
928    else
929    {
930       DU_LOG("\nERROR  -->  DU APP : Memory is NULL for Master Cell Group");
931    }
932 }
933
934 /*******************************************************************
935  *
936  * @brief Fills default modulation info for a UE
937  *
938  * @details
939  *
940  *    Function : fillDefaultModulation
941  *
942  *    Functionality: Fills default modulation info for a UE
943  *
944  * @params[in] Pointer to MAC UE configuration
945  * @return ROK     - success
946  *         RFAILED - failure
947  *
948  * ****************************************************************/
949 void fillDefaultModulation(MacUeCfg *ueCfg)
950 {
951    ueCfg->dlModInfo.modOrder = MOD_ORDER_QPSK;
952    ueCfg->dlModInfo.mcsIndex = DEFAULT_MCS;
953    ueCfg->dlModInfo.mcsTable = MCS_TABLE_QAM64; /* Spec 38.214-Table 5.1.3.1-1 */
954
955    ueCfg->ulModInfo.modOrder = MOD_ORDER_QPSK;
956    ueCfg->ulModInfo.mcsIndex = DEFAULT_MCS;
957    ueCfg->ulModInfo.mcsTable = MCS_TABLE_QAM64; /* Spec 38.214-Table 5.1.3.1-1 */
958 }
959
960 /******************************************************************
961  *
962  * @brief Function to fill Mac Lc Cfg for SRB1
963  *
964  * @details
965  *
966  *    Function : fillMacSrb1LcCfg
967  *
968  *    Functionality: Function to fill Mac Lc cfg for SRB1
969  *
970  * @params[in]  LcCfg *lcCfg, LcCfg *ueSetReqDb
971  * @return void
972  *****************************************************************/
973
974 void fillMacSrb1LcCfg(LcCfg *macLcCfg)
975 {
976    macLcCfg->lcId   = SRB1_LCID;
977    macLcCfg->configType = CONFIG_ADD;
978    macLcCfg->drbQos = NULLP;
979    macLcCfg->snssai = NULLP;
980    macLcCfg->ulLcCfgPres = true;
981    fillDefaultUlLcCfg(&macLcCfg->ulLcCfg);
982 }
983
984 /******************************************************************
985  *
986  * @brief Function to fill the Lc cfg from ueSetupReqDb
987  *
988  * @details
989  *
990  *    Function : fillMacLcCfgToAddMod
991  *
992  *    Functionality: Function to fill the Lc cfg from ueSetupReqDb
993  *
994  * @params[in]  LcCfg *lcCfg, LcCfg *ueSetReqDb
995  * @return ROK/RFAILED
996  *
997  *****************************************************************/
998
999 uint8_t fillMacLcCfgToAddMod(LcCfg *macLcCfgToSend, LcCfg *ueLcCfgDb, LcCfg *oldLcCfg, Bool toUpdate)
1000 {
1001    if(!toUpdate)
1002    {
1003       if(macLcCfgToSend)
1004       {
1005          macLcCfgToSend->lcId = ueLcCfgDb->lcId;
1006          macLcCfgToSend->configType = ueLcCfgDb->configType;
1007
1008          if(ueLcCfgDb->drbQos)
1009             macLcCfgToSend->drbQos = ueLcCfgDb->drbQos;
1010          else if(oldLcCfg)
1011             macLcCfgToSend->drbQos = oldLcCfg->drbQos;
1012          else
1013             macLcCfgToSend->drbQos = NULL;
1014
1015          if(ueLcCfgDb->snssai)
1016             macLcCfgToSend->snssai = ueLcCfgDb->snssai;
1017          else if(oldLcCfg)
1018             macLcCfgToSend->snssai = oldLcCfg->snssai;
1019          else
1020             macLcCfgToSend->snssai = NULL;
1021
1022          macLcCfgToSend->ulLcCfgPres = ueLcCfgDb->ulLcCfgPres;
1023          memcpy(&macLcCfgToSend->ulLcCfg, &ueLcCfgDb->ulLcCfg, sizeof(UlLcCfg));
1024          memcpy(&macLcCfgToSend->dlLcCfg, &ueLcCfgDb->dlLcCfg, sizeof(DlLcCfg));
1025       }
1026    }
1027    else
1028    {
1029       oldLcCfg->lcId = ueLcCfgDb->lcId;
1030       oldLcCfg->configType = ueLcCfgDb->configType;
1031
1032       if(ueLcCfgDb->drbQos)
1033       {
1034          if(oldLcCfg->drbQos)
1035             DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, oldLcCfg->drbQos, sizeof(DrbQosInfo));
1036
1037          DU_ALLOC_SHRABL_BUF(oldLcCfg->drbQos, sizeof(DrbQosInfo));
1038          if(oldLcCfg->drbQos == NULL)
1039          {
1040             DU_LOG("\nERROR  -->  DU APP : Memory Alloc Failed at fillMacLcCfgToAddMod()");
1041             return RFAILED;
1042          }
1043          memcpy(oldLcCfg->drbQos, ueLcCfgDb->drbQos, sizeof(DrbQosInfo));
1044       }
1045
1046       if(ueLcCfgDb->snssai)
1047       {
1048          if(oldLcCfg->snssai)
1049             DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, oldLcCfg->snssai, sizeof(Snssai));
1050
1051          DU_ALLOC_SHRABL_BUF(oldLcCfg->snssai, sizeof(Snssai));
1052          if(oldLcCfg->snssai == NULL)
1053          {
1054             DU_LOG("\nERROR  -->  DU APP : Memory Alloc Failed at fillMacLcCfgToAddMod()");
1055             DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, oldLcCfg->drbQos, sizeof(DrbQosInfo));
1056             return RFAILED;
1057          }
1058          memcpy(oldLcCfg->snssai, ueLcCfgDb->snssai, sizeof(Snssai));
1059       }
1060
1061       oldLcCfg->ulLcCfgPres = ueLcCfgDb->ulLcCfgPres;
1062       memcpy(&oldLcCfg->ulLcCfg, &ueLcCfgDb->ulLcCfg, sizeof(UlLcCfg));
1063       memcpy(&oldLcCfg->dlLcCfg, &ueLcCfgDb->dlLcCfg, sizeof(DlLcCfg));
1064    }
1065    return ROK;
1066 }
1067
1068 /******************************************************************
1069  *
1070  * @brief Function to copy the Bit rate from ueSetupReqDb
1071  *
1072  * @details
1073  *
1074  *    Function : fillAmbr
1075  *
1076  *    Functionality: Function to copy bit Rate from ueSetupReqDb
1077  *
1078  * @params[in]  AmbrCfg **macAmbr, AmbrCfg  *ueDbAmbr
1079  * @return ROK/RFAILED
1080  *
1081  *****************************************************************/
1082
1083 uint8_t fillAmbr(AmbrCfg **macAmbrCfgToSend, AmbrCfg *ueDbAmbr, AmbrCfg **oldMacAmbrCfg, Bool toUpdate)
1084 {
1085    if(!toUpdate)
1086    {
1087       if(ueDbAmbr)
1088       {
1089          *macAmbrCfgToSend = ueDbAmbr;
1090       }
1091       else if(oldMacAmbrCfg)
1092          *macAmbrCfgToSend = *oldMacAmbrCfg;       
1093    }
1094    else
1095    {
1096       if(ueDbAmbr)
1097       {
1098          if(oldMacAmbrCfg)
1099          {
1100             if(*oldMacAmbrCfg)
1101             {
1102                DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, *oldMacAmbrCfg, sizeof(AmbrCfg));
1103             }
1104             DU_ALLOC_SHRABL_BUF(*oldMacAmbrCfg, sizeof(AmbrCfg));
1105             if(*oldMacAmbrCfg == NULLP)
1106             {
1107                DU_LOG("\nERROR  -->  DU APP : Memory Alloc Failed at fillAmbr()");
1108                return RFAILED;
1109             }
1110             memset(*oldMacAmbrCfg, 0, sizeof(AmbrCfg));
1111             (*oldMacAmbrCfg)->ulBr = ueDbAmbr->ulBr;
1112          }
1113       }
1114    }
1115
1116    return ROK;
1117 }
1118
1119 /******************************************************************
1120  *
1121  * @brief Builds and Send UE ReConfig Request to MAC
1122  *
1123  * @details
1124  *
1125  *    Function : sendUeReCfgReqToMac
1126  *
1127  *    Functionality: Builds and Send UE ReConfig Request to MAC
1128  *
1129  * @Params[in]  MacUeCfg pointer
1130  * @return ROK     - success
1131  *         RFAILED - failure
1132  *
1133  * ****************************************************************/
1134
1135 uint8_t sendUeReCfgReqToMac(MacUeCfg *macUeCfg)
1136 {
1137    uint8_t ret = ROK;
1138    Pst pst;
1139    
1140    /* Fill Pst */
1141    FILL_PST_DUAPP_TO_MAC(pst, EVENT_MAC_UE_RECONFIG_REQ);
1142    
1143    if(macUeCfg)
1144    {
1145       /* Processing one Ue at a time to MAC */
1146       DU_LOG("\nDEBUG   -->  DU_APP: Sending Ue Reconfig Request to MAC");
1147       ret = (*packMacUeReconfigReqOpts[pst.selector])(&pst, macUeCfg);
1148       if(ret == RFAILED)
1149       {
1150          DU_LOG("\nERROR  -->  DU APP : Failed to send Reconfig Request to MAC at sendUeReCfgReqToMac()");
1151          DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, macUeCfg, sizeof(MacUeCfg));
1152       }
1153    }
1154    else
1155    {
1156       DU_LOG("\nERROR  -->  DU_APP: Received macUeCfg is NULLP at sendUeReCfgReqToMac()");
1157       ret = RFAILED;
1158    }
1159    return ret;
1160 }
1161
1162
1163
1164 /******************************************************************
1165  *
1166  * @brief Fills MacUeCfg structure
1167  *
1168  * @details
1169  *
1170  *    Function : fillMacUeCfg
1171  *
1172  *    Functionality: Fills MacUeCfg
1173  *
1174  * @params[in]  cellId, ueId, crnti, 
1175  *              DuUeCfg  pointer,
1176  *              MacUeCfg pointer
1177  * @return ROK/RFAILED
1178  *
1179  *****************************************************************/
1180 uint8_t fillMacUeCfg(uint16_t cellId, uint8_t gnbDuUef1apId, uint16_t crnti, DuUeCfg *ueCfgDb, MacUeCfg *macUeCfg)
1181 {
1182    uint8_t ret = ROK, dbIdx = 0, lcIdx = 0, cellIdx = 0;
1183    bool lcIdFound = false;
1184    MacUeCfg *duMacDb = NULLP;
1185
1186    macUeCfg->cellId = cellId;
1187    macUeCfg->ueId = gnbDuUef1apId;
1188    macUeCfg->crnti = crnti;
1189
1190    if(!ueCfgDb)
1191    {
1192       fillDefaultMacCellGrpInfo(macUeCfg);
1193       fillDefaultPhyCellGrpInfo(macUeCfg);
1194
1195       if((fillDefaultSpCellGrpInfo(macUeCfg)) != ROK)
1196       {
1197          DU_LOG("\nERROR  --> DUAPP : Failed in fillDefaultSpCellGrpInfo");
1198          return RFAILED;
1199       }
1200
1201       macUeCfg->ambrCfg = NULLP;
1202       fillDefaultModulation(macUeCfg);
1203       fillMacSrb1LcCfg(&macUeCfg->lcCfgList[0]);
1204       macUeCfg->numLcs++;
1205    }
1206    else
1207    {
1208       if(ueCfgDb->dataTransmissionAction == STOP_TRANSMISSION)
1209       {
1210          macUeCfg->transmissionAction = ueCfgDb->dataTransmissionAction; 
1211          return ROK;
1212       }
1213
1214       GET_CELL_IDX(cellId, cellIdx);
1215       if(duCb.actvCellLst[cellIdx])
1216          duMacDb = &duCb.actvCellLst[cellIdx]->ueCb[macUeCfg->ueId-1].macUeCfg;
1217       else
1218       {
1219          DU_LOG("\nERROR  -->  DU APP : Cell Id [%d] does not exist", cellId);
1220          return RFAILED;
1221       }
1222       duMacDb->macUeCfgState = UE_CFG_INPROGRESS;
1223       
1224       if(ueCfgDb->cellGrpCfg)
1225       {
1226          ret = procUeReCfgCellInfo(macUeCfg, duMacDb, ueCfgDb->cellGrpCfg);
1227          if(ret == ROK)
1228          {
1229             if(macUeCfg->spCellCfgPres == true)
1230             {
1231                if(macUeCfg->spCellCfg.servCellCfg.initDlBwp.pdschPresent)
1232                {
1233                   fillStartSymbolAndLen(macUeCfg->spCellCfg.servCellCfg.initDlBwp.pdschCfg.numTimeDomRsrcAlloc,\
1234                         &macUeCfg->spCellCfg.servCellCfg.initDlBwp.pdschCfg, NULL);
1235                }
1236                if(macUeCfg->spCellCfg.servCellCfg.initUlBwp.puschPresent)
1237                {
1238                   fillStartSymbolAndLen(macUeCfg->spCellCfg.servCellCfg.initUlBwp.puschCfg.numTimeDomRsrcAlloc,\
1239                         NULL, &macUeCfg->spCellCfg.servCellCfg.initUlBwp.puschCfg);
1240                }
1241             }
1242
1243             if(duMacDb)
1244                ret = fillAmbr(&macUeCfg->ambrCfg, ueCfgDb->ambrCfg , &duMacDb->ambrCfg, FALSE);
1245             else
1246                ret = fillAmbr(&macUeCfg->ambrCfg, ueCfgDb->ambrCfg , NULL, FALSE);
1247
1248             duFillModulationDetails(macUeCfg, duMacDb, ueCfgDb->ueNrCapability);
1249          }
1250       }
1251       else
1252       {
1253          fillDefaultMacCellGrpInfo(macUeCfg);
1254          fillDefaultPhyCellGrpInfo(macUeCfg);
1255          fillDefaultSpCellGrpInfo(macUeCfg);
1256          fillDefaultModulation(macUeCfg);
1257       }
1258
1259       /* Filling LC Context */
1260       for(dbIdx = 0; (dbIdx < ueCfgDb->numMacLcs && ret == ROK); dbIdx++)
1261       {
1262          if(!ueCfgDb->macLcCfg[dbIdx].ulLcCfgPres)
1263          {
1264             /* Filling default UL LC config in MAC if not present */
1265             ueCfgDb->macLcCfg[dbIdx].ulLcCfgPres = true;
1266             fillDefaultUlLcCfg(&ueCfgDb->macLcCfg[dbIdx].ulLcCfg);
1267          }
1268
1269          if(duMacDb)
1270          {
1271             for(lcIdx = 0; lcIdx < duMacDb->numLcs; lcIdx++)
1272             {
1273                if(ueCfgDb->macLcCfg[dbIdx].lcId == duMacDb->lcCfgList[lcIdx].lcId)
1274                {
1275                   lcIdFound = true;
1276                   if((ueCfgDb->macLcCfg[dbIdx].configType == CONFIG_UNKNOWN) ||
1277                         (ueCfgDb->macLcCfg[dbIdx].configType == CONFIG_MOD))
1278                   {
1279                      ueCfgDb->macLcCfg[dbIdx].configType = CONFIG_MOD;
1280                      ret = fillMacLcCfgToAddMod(&macUeCfg->lcCfgList[dbIdx], &ueCfgDb->macLcCfg[dbIdx],\
1281                            &duMacDb->lcCfgList[lcIdx], FALSE);
1282                   }
1283                }
1284                else
1285                   lcIdFound = false;
1286             }
1287          }
1288
1289          if(!lcIdFound)
1290          {
1291             /* ADD/DEL CONFIG */
1292             ret = fillMacLcCfgToAddMod(&macUeCfg->lcCfgList[dbIdx], &ueCfgDb->macLcCfg[dbIdx], NULL, FALSE);
1293          }
1294          if(ret == ROK)
1295          {
1296             macUeCfg->numLcs++;
1297          }
1298          else
1299          {
1300             DU_LOG("\nERROR  -->  DU APP : Failed to add Lc at Idx %d in fillMacUeCfg()", dbIdx); 
1301             break;
1302          }
1303       }/*End of Outer FOR loop */
1304       memcpy(&ueCfgDb->copyOfmacUeCfg, macUeCfg, sizeof(MacUeCfg));
1305    }
1306    return ret;
1307 }
1308
1309 /******************************************************************
1310  *
1311  * @brief Fills Rlc AM Information
1312  *
1313  * @details
1314  *
1315  *    Function : fillDefaultAmInfo
1316  *
1317  *    Functionality: Fills Rlc AM Information
1318  *
1319  * @params[in]  AmBearerCfg *amCfg
1320  * @return void
1321  *
1322  *****************************************************************/
1323 void fillDefaultAmInfo(AmBearerCfg *amCfg)
1324 {
1325    /* DL AM */
1326    amCfg->dlAmCfg.snLenDl     = AM_SIZE_12;
1327    amCfg->dlAmCfg.pollRetxTmr = T_POLL_RETRANSMIT_VAL;
1328    amCfg->dlAmCfg.pollPdu     = POLL_PDU_VAL;
1329    amCfg->dlAmCfg.pollByte    = POLL_BYTE_VAL;
1330    amCfg->dlAmCfg.maxRetxTh   = MAX_RETX_THRESHOLD_VAL;   
1331  
1332    /* UL AM */
1333    amCfg->ulAmCfg.snLenUl     = AM_SIZE_12;
1334    amCfg->ulAmCfg.reAssemTmr  = T_REASSEMBLY_VAL; 
1335    amCfg->ulAmCfg.statProhTmr = T_STATUS_PROHIBHIT_VAL;
1336 }
1337
1338 /******************************************************************
1339  *
1340  * @brief Fills RLC UM Bi Directional Information
1341  *
1342  * @details
1343  *
1344  *    Function : fillDefaultUmBiInfo
1345  *
1346  *    Functionality: Fills RLC UM Bi Directional Information
1347  *
1348  * @params[in]  UmBiDirBearerCfg *umBiDirCfg
1349  * @return void
1350  *
1351  *****************************************************************/
1352 void fillDefaultUmBiInfo(UmBiDirBearerCfg *umBiDirCfg)
1353 {
1354    /* UL UM BI DIR INFO */
1355    umBiDirCfg->ulUmCfg.snLenUlUm = UM_SIZE_12;
1356    umBiDirCfg->ulUmCfg.reAssemTmr = T_REASSEMBLY_VAL;
1357
1358    /* DL UM BI DIR INFO */
1359    umBiDirCfg->dlUmCfg.snLenDlUm  = UM_SIZE_12;
1360 }
1361
1362 /******************************************************************
1363  *
1364  * @brief Fills RLC UM Uni Directional UL Information
1365  *
1366  * @details
1367  *
1368  *    Function : fillDefaultUmUlInfo
1369  *
1370  *    Functionality: Fills RLC UM Uni Directional Info
1371  *
1372  * @params[in]  UmUniDirUlBearerCfg *UmUlCfg
1373  * @return void
1374  *
1375  *****************************************************************/
1376 void fillDefaultUmUlInfo(UmUniDirUlBearerCfg *UmUlCfg)
1377 {
1378    UmUlCfg->ulUmCfg.snLenUlUm = UM_SIZE_12;
1379    UmUlCfg->ulUmCfg.reAssemTmr = T_REASSEMBLY_VAL;
1380 }
1381
1382 /******************************************************************
1383  *
1384  * @brief Fills RLC UM Uni Directional DL Information
1385  *
1386  * @details
1387  *
1388  *    Function : fillDefaultUmDlInfo
1389  *
1390  *    Functionality: Fills RLC UM Uni Directional DL Info
1391  *
1392  * @params[in]  UmUniDirDlBearerCfg *UmDlCfg
1393  * @return void
1394  *
1395  *****************************************************************/
1396 void fillDefaultUmDlInfo(UmUniDirDlBearerCfg *UmDlCfg)
1397 {
1398    UmDlCfg->dlUmCfg.snLenDlUm  = UM_SIZE_12;
1399 }
1400
1401 /******************************************************************
1402  *
1403  * @brief Builds Rlc Mode Default Configuration
1404  *
1405  * @details
1406  *
1407  *    Function : fillDefaultRlcModeCfg
1408  *
1409  *    Functionality: Builds Rlc Mode Default Configuration
1410  *
1411  * @params[in]  rlcMode, RlcBearerCfg *lcCfg
1412  * @return ROK/RFAILED
1413  *
1414  *****************************************************************/
1415
1416 uint8_t fillDefaultRlcModeCfg(uint8_t rlcMode, RlcBearerCfg *lcCfg)
1417 {
1418    if(lcCfg)
1419    {
1420       switch(rlcMode)
1421       {
1422          case RLC_AM :
1423             {
1424                if(!lcCfg->u.amCfg)
1425                {
1426                   DU_ALLOC_SHRABL_BUF(lcCfg->u.amCfg, sizeof(AmBearerCfg));
1427                   if(lcCfg->u.amCfg)
1428                      fillDefaultAmInfo(lcCfg->u.amCfg);
1429                   else
1430                   {
1431                      DU_LOG("\n ERROR  -->  DU APP : Memory Alloc failed at AmCfg at fillDefaultRlcModeCfg()");
1432                      return RFAILED;
1433                   }
1434                }
1435                break;
1436             }
1437          case RLC_UM_BI_DIRECTIONAL :
1438             {
1439                if(!lcCfg->u.umBiDirCfg)
1440                {
1441                   DU_ALLOC_SHRABL_BUF(lcCfg->u.umBiDirCfg, sizeof(UmBiDirBearerCfg));
1442                   if(lcCfg->u.umBiDirCfg)
1443                      fillDefaultUmBiInfo(lcCfg->u.umBiDirCfg);
1444                   else
1445                   {
1446                      DU_LOG("\n ERROR  -->  DU APP : Memory Alloc failed at UmBiDirCfg at fillDefaultRlcModeCfg()");
1447                      return RFAILED;
1448                   }
1449                }
1450                break;
1451             }
1452          case RLC_UM_UNI_DIRECTIONAL_UL :
1453             {
1454                if(!lcCfg->u.umUniDirUlCfg)
1455                {
1456                   DU_ALLOC_SHRABL_BUF(lcCfg->u.umUniDirUlCfg, sizeof(UmUniDirUlBearerCfg));
1457                   if(lcCfg->u.umUniDirUlCfg)
1458                      fillDefaultUmUlInfo(lcCfg->u.umUniDirUlCfg);
1459                   else
1460                   {
1461                      DU_LOG("\n ERROR  -->  DU APP : Memory Alloc failed at UmUniDirUlCfg at fillDefaultRlcModeCfg()");
1462                      return RFAILED;
1463                   }
1464                }
1465                break;
1466             }
1467          case RLC_UM_UNI_DIRECTIONAL_DL :
1468             {
1469                if(!lcCfg->u.umUniDirDlCfg)
1470                {
1471                   DU_ALLOC_SHRABL_BUF(lcCfg->u.umUniDirDlCfg, sizeof(UmUniDirDlBearerCfg));
1472                   if(lcCfg->u.umUniDirDlCfg)
1473                      fillDefaultUmDlInfo(lcCfg->u.umUniDirDlCfg);
1474                   else
1475                   {
1476                      DU_LOG("\n ERROR  -->  DU APP : Memory Alloc failed at UmUniDirDlCfg at fillDefaultRlcModeCfg()");
1477                      return RFAILED;
1478                   }
1479                }
1480                break;
1481             }
1482          default:
1483             DU_LOG("\nERROR  -->  DUAPP: Invalid rlcMode %d at extractRlcCfgToAddMod()", rlcMode);
1484             return RFAILED;
1485       }
1486
1487    }
1488    else
1489    {
1490       DU_LOG("\nERROR  -->  DUAPP: Received LC Config is NULL");
1491       return RFAILED;
1492    }
1493    return ROK;
1494 }
1495
1496 /*******************************************************************
1497  *
1498  * @brief Function to fill Rlc Lc Cfg for SRB1
1499  *
1500  * @details
1501  *
1502  *    Function : fillRlcSrb1LcCfg
1503  *
1504  *    Functionality: 
1505  *     Function to fill Rlc Lc Cfg for SRB1
1506  * 
1507  *  @params[in]     Pointer to RlcBearerCfg
1508  *  @return ROK/RFAILED
1509  * 
1510  *****************************************************************/
1511
1512 uint8_t fillRlcSrb1LcCfg(RlcBearerCfg *rlcLcCfg)
1513 {
1514    uint8_t ret = ROK;
1515
1516    rlcLcCfg->rbId   = SRB1_LCID;
1517    rlcLcCfg->rbType = RB_TYPE_SRB;
1518    rlcLcCfg->lcId   = SRB1_LCID;
1519    rlcLcCfg->lcType = LCH_DCCH;
1520    rlcLcCfg->rlcMode = RLC_AM;
1521    rlcLcCfg->configType = CONFIG_ADD;
1522    ret = fillDefaultRlcModeCfg(rlcLcCfg->rlcMode, rlcLcCfg);
1523    rlcLcCfg->isLcAddModRspSent = true;
1524    return ret;
1525 }
1526
1527 /*******************************************************************
1528  *
1529  * @brief Processes UE ReConfig Req to RLC UL
1530  *
1531  * @details
1532  *
1533  *    Function : sendUeReCfgReqToRlc
1534  *
1535  *    Functionality: 
1536  *     Processes UE Reconfig Req to RLC UL
1537  * 
1538  *  @params[in]     Pointer to RlcUeCfg
1539  *  @return ROK     - success
1540  *          RFAILED - failure
1541  * 
1542  *****************************************************************/
1543
1544 uint8_t sendUeReCfgReqToRlc(RlcUeCfg *rlcUeCfg)
1545 {
1546    uint8_t ret;
1547    Pst pst;
1548    
1549    FILL_PST_DUAPP_TO_RLC(pst, RLC_UL_INST, EVENT_RLC_UE_RECONFIG_REQ);
1550    if(rlcUeCfg)
1551    {
1552       /* Processing one Ue at a time to RLC */
1553       DU_LOG("\nDEBUG   -->  DU_APP: Sending Ue Reconfig Request to RLC UL");
1554       ret = (*packRlcUeReconfigReqOpts[pst.selector])(&pst, rlcUeCfg);
1555       if(ret == RFAILED)
1556       {
1557          DU_LOG("\nERROR  -->  DU_APP : Failed to send Ue Reconfig Req to RLC at sendUeReCfgReqToRlc()");
1558          DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, rlcUeCfg, sizeof(RlcUeCfg));
1559       }
1560    }
1561    else
1562    {
1563       DU_LOG("\nERROR  -->   DU_APP: Received RlcUeCfg is NULL at sendUeReCfgReqToRlc()");
1564       ret = RFAILED;
1565    }
1566    return ret;
1567 }
1568
1569 /******************************************************************
1570  *
1571  * @brief Fills Snssai information
1572  *
1573  * @details
1574  *
1575  *    Function : fillSnssaiInfo
1576  *
1577  *    Functionality: Fills Snssai information
1578  *
1579  *  @params[in]    LcCfg *snssaiTobeSend, LcCfg *snssaiDb, LcCfg *oldSnssai,
1580  *  Bool toUpdateg
1581  *  @return ROK     - success
1582  *          RFAILED - failure
1583  * 
1584  *****************************************************************/
1585 uint8_t fillSnssaiInfo(Snssai *snssaiTobeSend, Snssai *snssaiDb, Snssai **oldSnssai, Bool toUpdate)
1586 {
1587    if(!toUpdate)
1588    {
1589       if(snssaiDb)
1590          snssaiTobeSend = snssaiDb;
1591       else if(oldSnssai)
1592          snssaiTobeSend = *oldSnssai;
1593       else
1594          snssaiTobeSend = NULL;
1595    }
1596    else
1597    {
1598       if(snssaiDb)
1599       {
1600          if(*oldSnssai)
1601             DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, *oldSnssai, sizeof(Snssai));
1602
1603          DU_ALLOC_SHRABL_BUF(*oldSnssai, sizeof(Snssai));
1604          if(*oldSnssai == NULL)
1605          {
1606             DU_LOG("\nERROR  -->  DU APP : Memory Alloc Failed at fillSnssaiInfo()");
1607             return RFAILED;
1608          }
1609          memcpy(*oldSnssai, snssaiDb, sizeof(Snssai));
1610       }
1611    }
1612    return ROK;
1613 }
1614 /******************************************************************
1615  *
1616  * @brief Fills RlcBearerCfg structure
1617  *
1618  * @details
1619  *
1620  *    Function : fillRlcUeCfg
1621  *
1622  *    Functionality: Fills Rlc Bearer Cfg
1623  *
1624  *
1625  *****************************************************************/
1626 uint8_t fillRlcUeCfg(uint16_t cellId, uint8_t duUeF1apId, DuUeCfg *ueCfgDb, RlcUeCfg *rlcUeCfg)
1627 {
1628    uint8_t ret, dbIdx, lcIdx, cellIdx;
1629    bool lcIdFound = false;
1630    RlcUeCfg *duRlcDb = NULLP;
1631
1632    ret = ROK;
1633    rlcUeCfg->cellId       = cellId;
1634    rlcUeCfg->ueId         = duUeF1apId;
1635
1636    if(!ueCfgDb)
1637    {
1638       /* Initial RB being Added */ 
1639       ret = fillRlcSrb1LcCfg(&rlcUeCfg->rlcLcCfg[0]);
1640       if(ret == ROK)
1641          rlcUeCfg->numLcs++;
1642       else
1643          memset(rlcUeCfg, 0, sizeof(RlcUeCfg));
1644    }
1645    else
1646    {
1647       /* Fetch RlcDb from DuUeCb */ 
1648       GET_CELL_IDX(cellId, cellIdx);
1649       duRlcDb = &duCb.actvCellLst[cellIdx]->ueCb[rlcUeCfg->ueId-1].rlcUeCfg;
1650       duRlcDb->rlcUeCfgState = UE_CFG_INPROGRESS;
1651
1652       for(dbIdx = 0; (dbIdx < ueCfgDb->numRlcLcs && ret == ROK); dbIdx++)
1653       {
1654          ret = fillDefaultRlcModeCfg(ueCfgDb->rlcLcCfg[dbIdx].rlcMode, &ueCfgDb->rlcLcCfg[dbIdx]);
1655          if(ret == RFAILED)
1656          {
1657             DU_LOG("\n ERROR  -->  DU APP : Failed to fill Rlc Mode at fillRlcUeCfg()");
1658             memset(rlcUeCfg, 0, sizeof(RlcUeCfg));
1659             return ret;
1660          }
1661
1662          if(duRlcDb)
1663          {
1664             /* Fill Rlc Ue Cfg List for ADD/MOD/DEL */
1665             for(lcIdx = 0; lcIdx < duRlcDb->numLcs; lcIdx++)
1666             { 
1667                if(ueCfgDb->rlcLcCfg[dbIdx].lcId == duRlcDb->rlcLcCfg[lcIdx].lcId)
1668                {
1669                   lcIdFound = true;
1670                   if((ueCfgDb->rlcLcCfg[dbIdx].configType == CONFIG_UNKNOWN)||
1671                         (ueCfgDb->rlcLcCfg[dbIdx].configType == CONFIG_MOD))
1672                   {
1673                      /* MOD */ 
1674                      ueCfgDb->rlcLcCfg[dbIdx].configType = CONFIG_MOD; /* update Db for MOD type */
1675                      memcpy(&rlcUeCfg->rlcLcCfg[dbIdx], &ueCfgDb->rlcLcCfg[dbIdx], sizeof(RlcBearerCfg));
1676                      fillSnssaiInfo(rlcUeCfg->rlcLcCfg[dbIdx].snssai, ueCfgDb->rlcLcCfg[dbIdx].snssai,\
1677                            &duRlcDb->rlcLcCfg[lcIdx].snssai,false);
1678                   }
1679                }
1680                else
1681                   lcIdFound = false;
1682             }
1683          }
1684
1685          if(!lcIdFound)
1686          {
1687             /* ADD/ DEL Config Type */
1688             memcpy(&rlcUeCfg->rlcLcCfg[dbIdx], &ueCfgDb->rlcLcCfg[dbIdx], sizeof(RlcBearerCfg));
1689             fillSnssaiInfo(rlcUeCfg->rlcLcCfg[dbIdx].snssai, ueCfgDb->rlcLcCfg[dbIdx].snssai, NULL, false);
1690          }
1691          rlcUeCfg->numLcs++;
1692       }
1693    }
1694    return ret;
1695 }
1696
1697 /******************************************************************
1698  *
1699  * @brief creates UE context
1700  *
1701  * @details
1702  *
1703  *    Function : duCreateUeCb
1704  *
1705  *    Functionality: Creates UE Conetxt
1706  *
1707  * @params[in] UeCcchCtxt Pointer
1708  *             UeIdx Pointer
1709  *
1710  * @return ROK     - success
1711  *         RFAILED - failure
1712  * ****************************************************************/
1713 uint8_t duCreateUeCb(UeCcchCtxt *ueCcchCtxt, uint32_t gnbCuUeF1apId)
1714 {
1715    uint8_t cellIdx = 0;
1716    uint8_t ret     = ROK;
1717    uint8_t ueId = 0, ueIdx = 0;
1718
1719    for(cellIdx = 0; cellIdx < MAX_NUM_CELL; cellIdx++)
1720    {
1721       if(duCb.actvCellLst[cellIdx] && (ueCcchCtxt->cellId == duCb.actvCellLst[cellIdx]->cellId))
1722       {
1723          GET_UE_ID(ueCcchCtxt->crnti, ueId);
1724          DU_LOG("\nDEBUG   -->  DU_APP: Filling UeCb for ueId [%d]", ueId);
1725
1726          ueIdx = ueId-1;
1727          duCb.actvCellLst[cellIdx]->ueCb[ueIdx].f1UeDb        = NULLP;
1728          duCb.actvCellLst[cellIdx]->ueCb[ueIdx].crnti         = ueCcchCtxt->crnti;
1729          duCb.actvCellLst[cellIdx]->ueCb[ueIdx].gnbDuUeF1apId = ueCcchCtxt->gnbDuUeF1apId;
1730          duCb.actvCellLst[cellIdx]->ueCb[ueIdx].gnbCuUeF1apId = gnbCuUeF1apId;
1731          duCb.actvCellLst[cellIdx]->ueCb[ueIdx].drbBitMap     = NULLP;
1732          duCb.actvCellLst[cellIdx]->ueCb[ueIdx].ueState       = UE_ACTIVE;
1733
1734          /* Filling Mac Ue Config */ 
1735          memset(&duCb.actvCellLst[cellIdx]->ueCb[ueIdx].macUeCfg, 0, sizeof(MacUeCfg));
1736          ret = duBuildAndSendUeCreateReqToMac(ueCcchCtxt->cellId, ueCcchCtxt->gnbDuUeF1apId, ueCcchCtxt->crnti, NULL, 
1737                &duCb.actvCellLst[cellIdx]->ueCb[ueIdx].macUeCfg);
1738          if(ret == RFAILED)
1739             DU_LOG("\nERROR  -->  DU APP : Failed to send UE create request to MAC");
1740
1741          /* Filling Rlc Ue Config */
1742          memset(&duCb.actvCellLst[cellIdx]->ueCb[ueIdx].rlcUeCfg, 0, sizeof(RlcUeCfg));
1743          ret = duBuildAndSendUeCreateReqToRlc(ueCcchCtxt->cellId, ueCcchCtxt->gnbDuUeF1apId, NULL,
1744                &duCb.actvCellLst[cellIdx]->ueCb[ueIdx].rlcUeCfg);
1745          if(ret == RFAILED)
1746             DU_LOG("\nERROR  -->  DU APP : Failed to send UE create request to RLC");
1747
1748          duCb.actvCellLst[cellIdx]->numActvUes++;
1749          memset(ueCcchCtxt, 0, sizeof(UeCcchCtxt));
1750          duCb.numUe--;
1751       }
1752    }
1753    return ret;
1754 }
1755
1756 /******************************************************************
1757  *
1758  * @brief Builds and Send UE Create Request to MAC
1759  *
1760  * @details
1761  *
1762  *    Function : duBuildAndSendUeCreateReqToMac
1763  *
1764  *    Functionality: Builds and Send UE Create Request to MAC
1765  *
1766  * @Params[in]  cellId,
1767  *              ueId,
1768  *              crnti,
1769  *              UE config extracted from F1AP msg
1770  *              MAC UE config struct to be filled
1771  * @return ROK     - success
1772  *         RFAILED - failure
1773  *
1774  * ****************************************************************/
1775
1776 uint8_t duBuildAndSendUeCreateReqToMac(uint16_t cellId, uint8_t gnbDuUeF1apId, uint16_t crnti, DuUeCfg *ueCfgDb, MacUeCfg *duMacUeCfg)
1777 {
1778    uint8_t  ret = ROK;
1779    MacUeCfg *macUeCfg = NULLP;
1780    Pst       pst;
1781    memset(&pst, 0, sizeof(Pst));
1782
1783
1784    ret = fillMacUeCfg(cellId, gnbDuUeF1apId, crnti, ueCfgDb, duMacUeCfg);
1785    if(ret == RFAILED)
1786    {
1787       DU_LOG("\nERROR  -->  DU APP : Failed to fill MacUeCfg at duBuildAndSendUeCreateReqToMac()");
1788       return RFAILED;
1789    }
1790
1791    /* Fill Pst */
1792    FILL_PST_DUAPP_TO_MAC(pst, EVENT_MAC_UE_CREATE_REQ);
1793
1794    /* Copying ueCb to a sharable buffer */
1795    DU_ALLOC_SHRABL_BUF(macUeCfg, sizeof(MacUeCfg));
1796    if(macUeCfg)
1797    {
1798       memset(macUeCfg, 0, sizeof(MacUeCfg));
1799       memcpy(macUeCfg, duMacUeCfg, sizeof(MacUeCfg));
1800       DU_LOG("\nDEBUG   -->  DU_APP: Sending UE create request to MAC");
1801
1802       /* Processing one Ue at a time to MAC */
1803       ret = (*packMacUeCreateReqOpts[pst.selector])(&pst, macUeCfg);
1804       if(ret == RFAILED)
1805       {
1806          DU_LOG("\nERROR  -->  DU_APP : Failure in sending Ue Create Req to MAC at duBuildAndSendUeCreateReqToMac()");
1807          DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, macUeCfg, sizeof(MacUeCfg));
1808       }
1809    }
1810    else
1811    {
1812       DU_LOG("\n ERROR  -->  DU APP : Memory alloc failed at duBuildAndSendUeCreateReqToMac()");
1813       ret = RFAILED;
1814    }
1815    return ret;
1816 }
1817
1818 /*******************************************************************
1819  *
1820  * @brief Build and send RACH Resource request to MAC
1821  *
1822  * @details
1823  *
1824  *    Function : duBuildAndSendRachRsrcReqToMac
1825  *    Functionality:
1826  *        Build and send RACH Resource request to MAC
1827  *
1828  * @params[in] Cell Id
1829  *             UE Id
1830  * @return ROK     - success
1831  *         RFAILED - failure
1832  *
1833  * ****************************************************************/
1834 uint8_t duBuildAndSendRachRsrcReqToMac(uint16_t cellId, uint16_t ueId)
1835 {
1836    uint16_t cellIdx = 0, ssbIdx = 0;
1837    Pst pst;
1838    MacRachRsrcReq *rachRsrcReq = NULLP;
1839
1840    GET_CELL_IDX(cellId, cellIdx);
1841    if(duCb.actvCellLst[cellIdx] == NULLP)
1842    {
1843       DU_LOG("\nERROR  -->  DU APP : Cell Id [%d] not found in duBuildAndSendRachRsrcReqToMac()", cellId);
1844       return RFAILED;
1845    }
1846
1847    DU_ALLOC_SHRABL_BUF(rachRsrcReq, sizeof(MacRachRsrcReq));
1848    if(!rachRsrcReq)
1849    {
1850       DU_LOG("\nERROR  -->  DU APP : Failed to allocate memory for RACH Resource Request in \
1851             duBuildAndSendRachRsrcReqToMac()");
1852       return RFAILED;
1853    }
1854
1855    rachRsrcReq->cellId = cellId;
1856    rachRsrcReq->ueId = ueId;
1857    rachRsrcReq->numSsb = duCfgParam.macCellCfg.prachCfg.ssbPerRach;
1858    for(ssbIdx = 0; ssbIdx < rachRsrcReq->numSsb; ssbIdx++)
1859    {
1860       rachRsrcReq->ssbIdx[ssbIdx] = ssbIdx;
1861    }
1862
1863    /* Fill Pst */
1864    FILL_PST_DUAPP_TO_MAC(pst, EVENT_MAC_RACH_RESOURCE_REQ);
1865    
1866    if(((*packMacRachRsrcReqOpts[pst.selector])(&pst, rachRsrcReq)) != ROK)
1867    {
1868       DU_LOG("\nERROR  -->  DU_APP : Failure in sending RACH Resource Request to MAC at \
1869             duBuildAndSendRachRsrcReqToMac()");
1870       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, rachRsrcReq, sizeof(MacRachRsrcReq));
1871       return RFAILED;
1872    }
1873
1874    return ROK;
1875 }
1876
1877 /*******************************************************************
1878  *
1879  * @brief Process RACH resource response from MAC
1880  *
1881  * @details
1882  *
1883  *    Function : DuProcMacRachRsrcRsp
1884  *    Functionality:
1885  *        Process RACH resource response from MAC
1886  *
1887  * @params[in] Post structure
1888  *             RACH resource response
1889  * @return ROK     - success
1890  *         RFAILED - failure
1891  *
1892  * ****************************************************************/
1893 uint8_t DuProcMacRachRsrcRsp(Pst *pst, MacRachRsrcRsp *rachRsrcRsp)
1894 {
1895    uint8_t  ret = RFAILED;
1896    uint16_t cellIdx = 0;
1897    DuCellCb *cellCb = NULLP;
1898    DuUeCb   *ueCb = NULLP;
1899
1900    DU_LOG("\nINFO  -->  DU APP : Received RACH Resource Response from MAC. Cell ID [%d] UE ID [%d]",
1901          rachRsrcRsp->cellId, rachRsrcRsp->ueId);
1902
1903    if(rachRsrcRsp->result == MAC_DU_APP_RSP_OK)
1904    {
1905       DU_LOG("\nINFO : DU APP : RACH Resource Response from MAC : Result [SUCCESS]");
1906
1907       /* Fetch Cell Cb */
1908       GET_CELL_IDX(rachRsrcRsp->cellId, cellIdx);
1909       if(duCb.actvCellLst[cellIdx] && (duCb.actvCellLst[cellIdx]->cellId == rachRsrcRsp->cellId))
1910       {
1911          cellCb = duCb.actvCellLst[cellIdx];
1912
1913          /* Fetch UE CB */
1914          if(cellCb->ueCb[rachRsrcRsp->ueId-1].gnbDuUeF1apId == rachRsrcRsp->ueId)
1915          {
1916             ueCb = &cellCb->ueCb[rachRsrcRsp->ueId-1];
1917
1918             /* Store CRNTI alloted to this UE by MAC */
1919             ueCb->crnti = rachRsrcRsp->newCrnti;
1920
1921             /* Store the assigned CF-RA resources */
1922             memcpy(&ueCb->cfraResource, &rachRsrcRsp->cfraResource, sizeof(MacCfraResource));
1923
1924             /* RACH resources allocated to UE is sent to CU in UE Context Setup Response
1925              * along with the result of UE Context setup requested by CU */
1926             if((ret = BuildAndSendUeCtxtRsp(rachRsrcRsp->cellId, rachRsrcRsp->ueId)) != ROK)
1927                DU_LOG("\nERROR  ->  DU APP : Failure in BuildAndSendUeCtxtRsp()");
1928          }
1929          else
1930             DU_LOG("\nERROR  -->  DU APP : UE ID [%d] not found in DuProcMacRachRsrcRsp", rachRsrcRsp->ueId);
1931       }
1932       else
1933          DU_LOG("\nERROR  -->  DU APP : Cell ID [%d] not found in DuProcMacRachRsrcRsp", rachRsrcRsp->cellId);
1934    }
1935    else
1936       DU_LOG("\nINFO : DU APP : RACH Resource Response from MAC : Result [FAILURE]");
1937
1938    DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, rachRsrcRsp, sizeof(MacRachRsrcRsp));
1939    return ret;
1940 }
1941
1942 /*******************************************************************
1943  *
1944  * @brief To update DuUeCb Mac Cfg
1945  *
1946  * @details
1947  *
1948  *    Function : duUpdateMacCfg
1949  *    Functionality:  update DuUeCb MAC Cfg
1950  *
1951  * @params[in] DuUeCb Pointer
1952  *             F1UeContextSetupDb pointer 
1953  * @return ROK     - success
1954  *         RFAILED - failure
1955  *
1956  * ****************************************************************/
1957 uint8_t duUpdateMacCfg(MacUeCfg *macUeCfg, F1UeContextSetupDb *f1UeDb) 
1958 {
1959    uint8_t ret, lcIdx, dbIdx, numLcs, lcDelIdx, cellIdx;
1960    MacUeCfg *oldMacUeCfg;
1961    ret = ROK;
1962    
1963    GET_CELL_IDX(macUeCfg->cellId, cellIdx);
1964    if(duCb.actvCellLst[cellIdx] == NULLP)
1965    {
1966       DU_LOG("\nERROR  --> DU APP: CellId[%d] not found", macUeCfg->cellId);
1967       return RFAILED;
1968    }
1969    oldMacUeCfg = &duCb.actvCellLst[cellIdx]->ueCb[macUeCfg->ueId-1].macUeCfg;
1970
1971    /*Filling Cell Group Cfg*/
1972    ret =  procUeReCfgCellInfo(macUeCfg, &f1UeDb->duUeCfg.copyOfmacUeCfg, f1UeDb->duUeCfg.cellGrpCfg);
1973    if(ret == ROK)
1974    {
1975       if(macUeCfg->spCellCfg.servCellCfg.initDlBwp.pdschPresent)
1976       {
1977          fillStartSymbolAndLen(macUeCfg->spCellCfg.servCellCfg.initDlBwp.pdschCfg.numTimeDomRsrcAlloc,\
1978                &macUeCfg->spCellCfg.servCellCfg.initDlBwp.pdschCfg, NULL);
1979       }
1980       if(macUeCfg->spCellCfg.servCellCfg.initUlBwp.puschPresent)
1981       {
1982          fillStartSymbolAndLen(macUeCfg->spCellCfg.servCellCfg.initUlBwp.puschCfg.numTimeDomRsrcAlloc,\
1983                NULL, &macUeCfg->spCellCfg.servCellCfg.initUlBwp.puschCfg);
1984       }
1985       ret = fillAmbr(NULL, f1UeDb->duUeCfg.ambrCfg, &oldMacUeCfg->ambrCfg, true);
1986       duFillModulationDetails(macUeCfg, oldMacUeCfg, f1UeDb->duUeCfg.ueNrCapability);
1987    }
1988
1989    /* Filling LC Context */
1990    for(dbIdx = 0; (dbIdx < f1UeDb->duUeCfg.numMacLcs && ret == ROK); dbIdx++)
1991    {
1992       numLcs = macUeCfg->numLcs;
1993       for(lcIdx = 0; lcIdx < numLcs; lcIdx++)
1994       {
1995          if(f1UeDb->duUeCfg.macLcCfg[dbIdx].lcId ==  macUeCfg->lcCfgList[lcIdx].lcId)
1996          {
1997             if(f1UeDb->duUeCfg.macLcCfg[dbIdx].configType == CONFIG_MOD)
1998             {
1999                ret = fillMacLcCfgToAddMod(NULL, &f1UeDb->duUeCfg.macLcCfg[dbIdx], &macUeCfg->lcCfgList[lcIdx], true);
2000             }
2001             else if(f1UeDb->duUeCfg.macLcCfg[dbIdx].configType == CONFIG_DEL)
2002             {
2003                /* Free memory at matched  lcIdx index */
2004                freeMacLcCfg(&macUeCfg->lcCfgList[lcIdx]);
2005                macUeCfg->numLcs--;
2006                for(lcDelIdx = lcIdx; lcDelIdx < macUeCfg->numLcs; lcDelIdx++)
2007                {
2008                   /* moving all elements one index ahead */
2009                   ret = fillMacLcCfgToAddMod(NULL,  &macUeCfg->lcCfgList[lcDelIdx+1], &macUeCfg->lcCfgList[lcDelIdx], true);
2010                   freeMacLcCfg(&macUeCfg->lcCfgList[lcDelIdx+1]);
2011                   if(ret == RFAILED)
2012                   {
2013                      DU_LOG("\nERROR  -->  DU APP : Failed to delete LC at Idx %d in duUpdateMacCfg()", lcDelIdx);
2014                      break;
2015                   }
2016                }
2017             }
2018          }
2019       } 
2020       if(f1UeDb->duUeCfg.macLcCfg[dbIdx].configType == CONFIG_ADD)
2021       {
2022          ret = fillMacLcCfgToAddMod(NULL, &f1UeDb->duUeCfg.macLcCfg[dbIdx], &macUeCfg->lcCfgList[numLcs], true);
2023          if(ret == RFAILED)
2024          {
2025             DU_LOG("\nERROR  -->  DU APP : Failed to add LC at Idx %d in duUpdateMacCfg()", numLcs);
2026             break;
2027          }
2028          macUeCfg->numLcs++;
2029       }
2030                 
2031    }
2032    return ret;
2033 }
2034
2035 /******************************************************************
2036  *
2037  * @brief Function to fill the RLC Lc cfg from ueSetupReqDb
2038  *
2039  * @details
2040  *
2041  *    Function : fillRlcCfgToAddMod
2042  *
2043  *    Functionality: Function to fill the RLC Lc cfg from ueSetupReqDb
2044  *
2045  *
2046  *****************************************************************/
2047
2048 uint8_t fillRlcCfgToAddMod(RlcBearerCfg *lcCfg, RlcBearerCfg *f1UeDbLcCfg)
2049 {
2050    lcCfg->configType = f1UeDbLcCfg->configType;
2051    lcCfg->rbId       = f1UeDbLcCfg->rbId;
2052    lcCfg->rbType     = f1UeDbLcCfg->rbType;
2053    lcCfg->lcId       = f1UeDbLcCfg->lcId;
2054    lcCfg->lcType     = f1UeDbLcCfg->lcType;
2055    lcCfg->rlcMode    = f1UeDbLcCfg->rlcMode;
2056    
2057    switch(lcCfg->rlcMode)
2058    {
2059       case RLC_AM :
2060          {
2061             if(!lcCfg->u.amCfg)
2062             {
2063                DU_ALLOC_SHRABL_BUF(lcCfg->u.amCfg, sizeof(AmBearerCfg));
2064                if(!lcCfg->u.amCfg)
2065                   return RFAILED;
2066             }
2067             /* DL AM */
2068             lcCfg->u.amCfg->dlAmCfg.snLenDl     = f1UeDbLcCfg->u.amCfg->dlAmCfg.snLenDl;    
2069             lcCfg->u.amCfg->dlAmCfg.pollRetxTmr = f1UeDbLcCfg->u.amCfg->dlAmCfg.pollRetxTmr;
2070             lcCfg->u.amCfg->dlAmCfg.pollPdu     = f1UeDbLcCfg->u.amCfg->dlAmCfg.pollPdu;
2071             lcCfg->u.amCfg->dlAmCfg.pollByte    = f1UeDbLcCfg->u.amCfg->dlAmCfg.pollByte;   
2072             lcCfg->u.amCfg->dlAmCfg.maxRetxTh   = f1UeDbLcCfg->u.amCfg->dlAmCfg.maxRetxTh;   
2073
2074             /* UL AM */
2075             lcCfg->u.amCfg->ulAmCfg.snLenUl     = f1UeDbLcCfg->u.amCfg->ulAmCfg.snLenUl;
2076             lcCfg->u.amCfg->ulAmCfg.reAssemTmr  = f1UeDbLcCfg->u.amCfg->ulAmCfg.reAssemTmr; 
2077             lcCfg->u.amCfg->ulAmCfg.statProhTmr = f1UeDbLcCfg->u.amCfg->ulAmCfg.statProhTmr;
2078             break;
2079          }
2080       case RLC_UM_BI_DIRECTIONAL :
2081          {
2082             if(!lcCfg->u.umBiDirCfg)
2083             {
2084                DU_ALLOC_SHRABL_BUF(lcCfg->u.umBiDirCfg, sizeof(UmBiDirBearerCfg));
2085                if(!lcCfg->u.umBiDirCfg)
2086                   return RFAILED;
2087             }
2088             /* UL UM BI DIR INFO */
2089             lcCfg->u.umBiDirCfg->ulUmCfg.snLenUlUm  = f1UeDbLcCfg->u.umBiDirCfg->ulUmCfg.snLenUlUm;  
2090             lcCfg->u.umBiDirCfg->ulUmCfg.reAssemTmr = f1UeDbLcCfg->u.umBiDirCfg->ulUmCfg.reAssemTmr;
2091             /* DL UM BI DIR INFO */
2092             lcCfg->u.umBiDirCfg->dlUmCfg.snLenDlUm  = f1UeDbLcCfg->u.umBiDirCfg->dlUmCfg.snLenDlUm;
2093             break;
2094          }
2095       case RLC_UM_UNI_DIRECTIONAL_UL :
2096          {
2097             if(!lcCfg->u.umUniDirUlCfg)
2098             {
2099                DU_ALLOC_SHRABL_BUF(lcCfg->u.umUniDirUlCfg, sizeof(UmUniDirUlBearerCfg));
2100                if(!lcCfg->u.umUniDirUlCfg)
2101                   return RFAILED;
2102             }
2103             lcCfg->u.umUniDirUlCfg->ulUmCfg.snLenUlUm  = f1UeDbLcCfg->u.umUniDirUlCfg->ulUmCfg.snLenUlUm;  
2104             lcCfg->u.umUniDirUlCfg->ulUmCfg.reAssemTmr = f1UeDbLcCfg->u.umUniDirUlCfg->ulUmCfg.reAssemTmr;
2105             break;
2106
2107          }
2108       case RLC_UM_UNI_DIRECTIONAL_DL :
2109          {
2110             if(!lcCfg->u.umUniDirDlCfg)
2111             {
2112                DU_ALLOC_SHRABL_BUF(lcCfg->u.umUniDirDlCfg, sizeof(UmUniDirDlBearerCfg));
2113                if(!lcCfg->u.umUniDirDlCfg)
2114                   return RFAILED;
2115             }
2116             lcCfg->u.umUniDirDlCfg->dlUmCfg.snLenDlUm  = f1UeDbLcCfg->u.umUniDirDlCfg->dlUmCfg.snLenDlUm;
2117             break;
2118          }
2119       default:
2120          DU_LOG("\nERROR  -->  DU_APP: Invalid Rlc Mode %d at fillRlcCfgToAddMod()", lcCfg->rlcMode);
2121          return RFAILED;
2122    }
2123    return ROK;
2124 }
2125
2126 /*******************************************************************
2127  *
2128  * @brief To update DuUeCb Rlc Lc Cfg
2129  *
2130  * @details
2131  *
2132  *    Function : duUpdateRlcLcCfg
2133  *    Functionality:  update DuUeCb Rlc Lc Cfg
2134  *
2135  * @params[in] DuUeCb Pointer
2136  *             F1UeContextSetupDb pointer 
2137  * @return ROK     - success
2138  *         RFAILED - failure
2139  *
2140  * ****************************************************************/
2141
2142 uint8_t duUpdateRlcLcCfg(RlcUeCfg *rlcUeCfg, F1UeContextSetupDb *f1UeDb)
2143 {
2144    uint8_t ret, lcIdx, dbIdx, numLcs, lcDelIdx;
2145
2146    ret = ROK;
2147    for(dbIdx = 0; (dbIdx < f1UeDb->duUeCfg.numRlcLcs && ret ==ROK); dbIdx++)
2148    {
2149       numLcs = rlcUeCfg->numLcs;
2150       for(lcIdx = 0; lcIdx < numLcs; lcIdx++)
2151       {
2152          if(f1UeDb->duUeCfg.rlcLcCfg[dbIdx].lcId == rlcUeCfg->rlcLcCfg[lcIdx].lcId)
2153          {
2154             if(f1UeDb->duUeCfg.rlcLcCfg[dbIdx].configType == CONFIG_MOD)
2155             {
2156                ret = fillRlcCfgToAddMod(&rlcUeCfg->rlcLcCfg[lcIdx], &f1UeDb->duUeCfg.rlcLcCfg[dbIdx]);
2157                if(ret == RFAILED)
2158                {
2159                   DU_LOG("\nERROR  -->  DU APP : Failed to modify LC at Idx %d in duUpdateRlcCfg()", lcDelIdx);
2160                   break;
2161                }
2162                fillSnssaiInfo(NULL, f1UeDb->duUeCfg.rlcLcCfg[dbIdx].snssai, &rlcUeCfg->rlcLcCfg[lcIdx].snssai, true);
2163             }
2164             else if(f1UeDb->duUeCfg.rlcLcCfg[dbIdx].configType == CONFIG_DEL)
2165             {
2166                /* Free memory at matched lcIdx index */
2167                freeRlcLcCfg(&rlcUeCfg->rlcLcCfg[lcIdx]);
2168                rlcUeCfg->numLcs--;
2169                for(lcDelIdx = lcIdx; lcDelIdx < rlcUeCfg->numLcs; lcDelIdx++)
2170                {
2171                   ret = fillRlcCfgToAddMod(&rlcUeCfg->rlcLcCfg[lcDelIdx], &rlcUeCfg->rlcLcCfg[lcDelIdx+1]);
2172                   fillSnssaiInfo(NULL, rlcUeCfg->rlcLcCfg[lcDelIdx+1].snssai, &rlcUeCfg->rlcLcCfg[lcDelIdx].snssai,\
2173                   true);
2174                   freeRlcLcCfg(&rlcUeCfg->rlcLcCfg[lcDelIdx+1]);
2175                   if(ret == RFAILED)
2176                   {
2177                      DU_LOG("\nERROR  -->  DU APP : Failed to delete LC at Idx %d in duUpdateRlcCfg()", lcDelIdx);
2178                      break;
2179                   }
2180                }
2181             }
2182          }
2183       }
2184       if(f1UeDb->duUeCfg.rlcLcCfg[dbIdx].configType == CONFIG_ADD)
2185       {
2186          ret = fillRlcCfgToAddMod(&rlcUeCfg->rlcLcCfg[lcIdx], &f1UeDb->duUeCfg.rlcLcCfg[dbIdx]);
2187          if(ret == ROK)
2188          {    
2189             fillSnssaiInfo(NULL, f1UeDb->duUeCfg.rlcLcCfg[dbIdx].snssai, &rlcUeCfg->rlcLcCfg[ rlcUeCfg->numLcs].snssai,\
2190                   true);
2191             rlcUeCfg->numLcs++;
2192          }
2193       }
2194    }
2195    return ret;
2196 }
2197
2198 /*******************************************************************
2199  *
2200  * @brief Function to fill Tunnel Config to Add/Mod
2201  * 
2202  *
2203  * @details
2204  *
2205  *    Function : fillTnlCfgToAddMod
2206  *
2207  *    Functionality: Function to fill tunnel Config to Add/Mod
2208  *
2209  * @params[in] Pointer to tnlCfgDb,
2210  *             pointer to f1TnlCfg
2211  * @return ROK     - success
2212  *         RFAILED - failure
2213  *
2214  * ****************************************************************/
2215 uint8_t fillTnlCfgToAddMod(UpTnlCfg **ueCbTnlCfg, UpTnlCfg *f1TnlCfg)
2216 {
2217    if(*ueCbTnlCfg)
2218    {
2219       DU_FREE((*ueCbTnlCfg)->tnlCfg1, sizeof(GtpTnlCfg));
2220       DU_FREE(*ueCbTnlCfg, sizeof(UpTnlCfg));
2221    }
2222
2223    if(*ueCbTnlCfg == NULLP)
2224    {
2225       /* copying to DuCb Tnl Cfg */
2226       DU_ALLOC(*ueCbTnlCfg, sizeof(UpTnlCfg));
2227       if(*ueCbTnlCfg == NULLP)
2228       {
2229          DU_LOG("\nERROR  -->  DU_APP : fillTnlCfgToAddMod: Memory Alloc failed for drbId[%d]", f1TnlCfg->drbId);
2230          return RFAILED;
2231       }
2232    }
2233    memset(*ueCbTnlCfg, 0, sizeof(UpTnlCfg));
2234    (*ueCbTnlCfg)->configType = f1TnlCfg->configType;
2235    (*ueCbTnlCfg)->cellId    = f1TnlCfg->cellId;
2236    (*ueCbTnlCfg)->ueId      = f1TnlCfg->ueId;
2237    (*ueCbTnlCfg)->drbId     = f1TnlCfg->drbId;
2238    if(f1TnlCfg->tnlCfg1)
2239    {
2240       if((*ueCbTnlCfg)->tnlCfg1 == NULLP)
2241       {
2242          DU_ALLOC((*ueCbTnlCfg)->tnlCfg1, sizeof(GtpTnlCfg));
2243          if((*ueCbTnlCfg)->tnlCfg1 == NULLP)
2244          {
2245             DU_LOG("\nERROR  -->  DU_APP : fillTnlCfgToAddMod: Memory Alloc failed for tnlCfg1 for drbId[%d]", f1TnlCfg->drbId);
2246             return RFAILED;
2247          }
2248       }
2249       memset((*ueCbTnlCfg)->tnlCfg1, 0, sizeof(GtpTnlCfg));
2250       (*ueCbTnlCfg)->tnlCfg1->teId = f1TnlCfg->tnlCfg1->teId;
2251       (*ueCbTnlCfg)->tnlCfg1->ulTnlAddress = f1TnlCfg->tnlCfg1->ulTnlAddress;
2252       (*ueCbTnlCfg)->tnlCfg1->dlTnlAddress = f1TnlCfg->tnlCfg1->dlTnlAddress;
2253    }
2254    return ROK;
2255 }
2256
2257 /*******************************************************************
2258  *
2259  * @brief Processing the tunnel Request to EGTP
2260  *        
2261  * @details
2262  *
2263  *    Function : duProcEgtpTunnelCfg
2264  *
2265  *    Functionality: Processing the tunnel Request to EGTP
2266  *
2267  * @params[in] UptnlCfg *duTnlCfg, UpTnlCfg *f1TnlCfg 
2268  * @return ROK     - success
2269  *         RFAILED - failure
2270  *
2271  * ****************************************************************/
2272
2273 uint8_t duProcEgtpTunnelCfg(uint8_t ueCbIdx, UpTnlCfg *duTnlCfg, UpTnlCfg *f1TnlCfg)
2274 {
2275    uint8_t ret = RFAILED, delIdx;
2276
2277    if(f1TnlCfg->tnlCfg1 == NULLP)
2278    {
2279       DU_LOG("\nERROR  -->  DU_APP : Tunnel config not found");
2280       return ret;
2281    }
2282
2283    if(f1TnlCfg->configType == CONFIG_ADD)
2284    {
2285       if(duSendEgtpTnlMgmtReq(EGTP_TNL_MGMT_ADD, NULLP, f1TnlCfg->tnlCfg1) == ROK)
2286       {
2287          if(fillTnlCfgToAddMod(&duCb.upTnlCfg[duCb.numTeId], f1TnlCfg) == ROK)
2288          {
2289             duCb.numTeId++;
2290             ret = ROK;
2291          }
2292       }      
2293    }
2294    else if(f1TnlCfg->configType == CONFIG_MOD)
2295    {
2296       if(duSendEgtpTnlMgmtReq(EGTP_TNL_MGMT_MOD, duTnlCfg->tnlCfg1->teId, f1TnlCfg->tnlCfg1) == ROK)
2297       {
2298          if(fillTnlCfgToAddMod(&duTnlCfg, f1TnlCfg) == ROK)
2299          {
2300             ret = ROK;
2301          }
2302       }   
2303    }
2304    else if(f1TnlCfg->configType == CONFIG_DEL)
2305    {
2306       if(duSendEgtpTnlMgmtReq(EGTP_TNL_MGMT_DEL, duTnlCfg->tnlCfg1->teId, f1TnlCfg->tnlCfg1) == ROK)
2307       {    
2308          /* Free memory at drbIdx */
2309          duCb.numTeId--;
2310          for(delIdx = ueCbIdx; delIdx < duCb.numTeId; delIdx++)
2311          {
2312             /* moving all elements one index ahead */
2313             ret = fillTnlCfgToAddMod(&duCb.upTnlCfg[delIdx], duCb.upTnlCfg[delIdx+1]);
2314             if(ret != ROK)
2315             {
2316                return ret;
2317             }
2318          }
2319          if(duCb.upTnlCfg[delIdx])
2320          {
2321             DU_FREE(duCb.upTnlCfg[delIdx]->tnlCfg1, sizeof(GtpTnlCfg));
2322             DU_FREE(duCb.upTnlCfg[delIdx], sizeof(UpTnlCfg));
2323          }
2324       }   
2325    }
2326    return ret;
2327 }
2328
2329 /***********************************************************************
2330  *
2331  * @brief Function to fill Tunnel Config
2332  *        and sends tunnel Req to EGTP
2333  * 
2334  *
2335  * @details
2336  *
2337  *    Function : duUpdateTunnelCfgDb
2338  *
2339  *    Functionality: Function to fill tunnel Config
2340  *                   and sends tunnel Cfg Req to EGTP
2341  *
2342  * @params[in] ueId, cellId, DuUeCfg 
2343  * @return ROK     - success
2344  *         RFAILED - failure
2345  *
2346  * ****************************************************************/
2347
2348 uint8_t duUpdateTunnelCfgDb(uint8_t ueId, uint8_t cellId, DuUeCfg *duUeCfg)
2349 {
2350    uint8_t ret = ROK, drbIdx, teIdx;
2351    bool drbFound = false;
2352
2353    /*If Add/Mod tunnels request for that DRB is successful in EGTP */
2354    /*then update drbId and tunnel Info in duCb */
2355    for(drbIdx=0; drbIdx < duUeCfg->numDrb; drbIdx++)
2356    {
2357       duUeCfg->upTnlInfo[drbIdx].cellId = cellId;
2358       duUeCfg->upTnlInfo[drbIdx].ueId = ueId;
2359       for(teIdx = 0; teIdx < duCb.numTeId; teIdx++)
2360       {
2361          if((duCb.upTnlCfg[teIdx]->ueId == duUeCfg->upTnlInfo[drbIdx].ueId) && \
2362             (duCb.upTnlCfg[teIdx]->drbId == duUeCfg->upTnlInfo[drbIdx].drbId))
2363          {
2364             drbFound = true; /* existing DRB */
2365             if(duProcEgtpTunnelCfg(teIdx, duCb.upTnlCfg[teIdx], &duUeCfg->upTnlInfo[drbIdx]) != ROK)
2366             {
2367                DU_LOG("\nERROR  -> DU_APP : duUpdateTunnelCfgDb: Failed to modify tunnel req for Drb id[%d]",
2368                      duUeCfg->upTnlInfo[drbIdx].drbId);
2369                ret = RFAILED;
2370             }
2371             break;
2372          }
2373          else
2374             drbFound = false;
2375       }
2376
2377       if(!drbFound && ret == ROK)/* new DRB to Add */
2378       {
2379          if(duProcEgtpTunnelCfg(NULLP, NULLP, &duUeCfg->upTnlInfo[drbIdx]) != ROK)
2380          {
2381             DU_LOG("\nERROR  -> DU_APP : duUpdateTunnelCfgDb: Failed to add tunnel req for Drb id[%d]",
2382                   duUeCfg->upTnlInfo[drbIdx].drbId);
2383             ret = RFAILED;
2384             break;
2385          }
2386       }
2387       else
2388          break;
2389    }
2390    return ret;
2391 }
2392
2393 /*******************************************************************
2394  *
2395  * @brief @brief To update DuUeCb Mac and Rlc Ue Cfg
2396  * 
2397  *
2398  * @details
2399  *
2400  *    Function : duUpdateDuUeCbCfg
2401  *
2402  *    Functionality: update DuUeCb Mac and Rlc Ue Cfg
2403  *
2404  * @params[in] ueId, cellIdx 
2405  * @return ROK     - success
2406  *         RFAILED - failure
2407  *
2408  * ****************************************************************/
2409
2410 uint8_t duUpdateDuUeCbCfg(uint8_t ueId, uint8_t cellId)
2411 {
2412    uint8_t ret = ROK, cellIdx = 0, crnti=0;
2413    DuUeCb *ueCb = NULLP;
2414
2415    GET_CELL_IDX(cellId, cellIdx);
2416    ueCb = &duCb.actvCellLst[cellIdx]->ueCb[ueId-1];
2417
2418    /*Filling RLC Ue Cfg */
2419    ueCb->rlcUeCfg.cellId = cellId;
2420    ueCb->rlcUeCfg.ueId   = ueId;
2421    ret = duUpdateRlcLcCfg(&ueCb->rlcUeCfg, ueCb->f1UeDb);
2422    if(ret == ROK)
2423    {
2424       /*Filling MAC Ue Cfg */
2425       GET_CRNTI(crnti, ueId);
2426       ueCb->macUeCfg.cellId = cellId;
2427       ueCb->macUeCfg.ueId  = ueId;
2428       ueCb->macUeCfg.crnti  = crnti;
2429       ret = duUpdateMacCfg(&ueCb->macUeCfg, ueCb->f1UeDb);
2430       if(ret == RFAILED)
2431          DU_LOG("\nERROR  -->  DU APP : Failed while updating MAC LC Config at duUpdateDuUeCbCfg()");
2432       else
2433       {
2434          if(duUpdateTunnelCfgDb(ueId, cellId, &ueCb->f1UeDb->duUeCfg) != ROK)
2435          {
2436             DU_LOG("\nERROR  -->  DU_APP : Failed to establish tunnel in duUpdateDuUeCbCfg()");
2437             return RFAILED;
2438          }
2439       }
2440    }
2441    else
2442       DU_LOG("\nERROR  -->  DU APP : Failed while updating RLC LC Config at duUpdateDuUeCbCfg()");
2443    return ret;
2444 }
2445
2446 /*******************************************************************
2447  *
2448  * @brief Handle UE config response from MAC
2449  *
2450  * @details
2451  *
2452  *    Function : DuProcMacUeCfgRsp
2453  *
2454  *    Functionality: Handle UE Config response from MAC
2455  *
2456  * @params[in] Pointer to MacUeCfgRsp and Pst 
2457  * @return ROK     - success
2458  *         RFAILED - failure
2459  *
2460  * ****************************************************************/
2461 uint8_t DuProcMacUeCfgRsp(Pst *pst, MacUeCfgRsp *cfgRsp)
2462 {
2463    uint8_t ret = ROK;
2464    uint16_t cellIdx;
2465
2466    if(cfgRsp)
2467    {
2468       GET_CELL_IDX(cfgRsp->cellId, cellIdx);
2469       if(cfgRsp->result == MAC_DU_APP_RSP_OK)
2470       {
2471          if(pst->event == EVENT_MAC_UE_CREATE_RSP)
2472          {
2473             DU_LOG("\nINFO   -->  DU APP : MAC UE Create Response : SUCCESS [DU UE F1AP ID : %d]", cfgRsp->ueId);
2474
2475             if(duCb.actvCellLst[cellIdx] && (duCb.actvCellLst[cellIdx]->ueCb[cfgRsp->ueId -1].gnbDuUeF1apId == cfgRsp->ueId))
2476             {
2477                duCb.actvCellLst[cellIdx]->ueCb[cfgRsp->ueId -1].macUeCfg.macUeCfgState = UE_CREATE_COMPLETE;
2478
2479               if((duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].ueState == UE_HANDIN_IN_PROGRESS) && 
2480                     (duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].macUeCfg.macUeCfgState == UE_CREATE_COMPLETE) &&
2481                      (duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].rlcUeCfg.rlcUeCfgState == UE_CREATE_COMPLETE))
2482                {
2483                   if((ret = duUpdateDuUeCbCfg(cfgRsp->ueId, cfgRsp->cellId)) == ROK)
2484                   {
2485                      /* If UE is in handover, RACH resource needs to be requested
2486                       * from MAC for CFRA */
2487                      if((duBuildAndSendRachRsrcReqToMac(cfgRsp->cellId, cfgRsp->ueId)) != ROK)
2488                      {
2489                         DU_LOG("\nERROR  --> DU APP : Failed to send RACH Resource Request to MAC");
2490                         DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(MacUeCfgRsp));
2491                         return RFAILED;
2492                      }
2493                   }
2494                   else
2495                   {
2496                      DU_LOG("\nERROR  ->  DU APP : Failure in updating DU UE CB");
2497                      DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(MacUeCfgRsp));
2498                      return RFAILED;
2499                   }
2500                }
2501             }
2502          }
2503          else if(pst->event == EVENT_MAC_UE_RECONFIG_RSP)
2504          {
2505             DU_LOG("\nINFO   -->  DU APP : MAC UE Reconfig Response : SUCCESS [DU UE F1AP ID : %d]", cfgRsp->ueId);
2506             if(duCb.actvCellLst[cellIdx] && 
2507                   (duCb.actvCellLst[cellIdx]->ueCb[cfgRsp->ueId -1].gnbDuUeF1apId == cfgRsp->ueId))
2508             {
2509                duCb.actvCellLst[cellIdx]->ueCb[cfgRsp->ueId -1].macUeCfg.macUeCfgState = UE_RECFG_COMPLETE;
2510                if((duCb.actvCellLst[cellIdx]->ueCb[cfgRsp->ueId -1].macUeCfg.macUeCfgState == UE_RECFG_COMPLETE) &&
2511                      (duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].rlcUeCfg.rlcUeCfgState == UE_RECFG_COMPLETE))
2512                {
2513                   if((ret = duUpdateDuUeCbCfg(cfgRsp->ueId, cfgRsp->cellId)) == ROK)
2514                   {  
2515                      if((BuildAndSendUeCtxtRsp(cfgRsp->cellId, cfgRsp->ueId)) != ROK)
2516                      {
2517                         DU_LOG("\nERROR  ->  DU APP : Failure in BuildAndSendUeCtxtRsp()");
2518                         DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(MacUeCfgRsp));
2519                         return RFAILED;
2520                      }
2521                   }
2522                   else
2523                   {
2524                      DU_LOG("\nERROR  ->  DU APP : Failure in updating DU UE CB");
2525                      DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(MacUeCfgRsp));
2526                      return RFAILED;
2527                   }
2528                }
2529             }
2530          }
2531       }
2532       else
2533       {
2534          DU_LOG("\nERROR  -->  DU APP : MAC UE CFG Response for EVENT[%d]: FAILURE [DU UE F1AP ID : %d]", pst->event, cfgRsp->ueId);
2535          if(pst->event == EVENT_MAC_UE_RECONFIG_RSP)
2536          {
2537             //TODO: Send the failure case in Ue Context Setup Response
2538          }
2539          ret = RFAILED;
2540       }
2541       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(MacUeCfgRsp));
2542    }
2543    else
2544    {
2545       DU_LOG("\nERROR  -->  DU APP : Received MAC Ue Config Response is NULL at DuProcMacUeCfgRsp()");
2546       ret = RFAILED;
2547    }
2548    return ret;
2549 }
2550
2551
2552 /*******************************************************************
2553  *
2554  * @brief Processes UE create Req to RLC UL
2555  *
2556  * @details
2557  *
2558  *    Function : duBuildAndSendUeCreateReqToRlc
2559  *
2560  *    Functionality: 
2561  *     Processes UE create Req to RLC UL
2562  * 
2563  *  @params[in]  cellId,
2564  *               ueId,
2565  *               Pointer to RlcUeCfg
2566  *  @return ROK     - success
2567  *          RFAILED - failure
2568  * 
2569  *****************************************************************/
2570
2571 uint8_t duBuildAndSendUeCreateReqToRlc(uint16_t cellId, uint8_t gnbDuUeF1apId, DuUeCfg *ueCfgDb, RlcUeCfg *duRlcUeCfg)
2572 {
2573    uint8_t  ret = ROK;
2574    RlcUeCfg *rlcUeCfg = NULLP;
2575    Pst       pst;
2576   
2577    ret = fillRlcUeCfg(cellId, gnbDuUeF1apId, ueCfgDb, duRlcUeCfg);
2578    if(ret == RFAILED)
2579    {
2580       DU_LOG("\nERROR  -->  DU APP : Failed to fill Rlc Ue Cfg at duBuildAndSendUeCreateReqToRlc()");
2581       return ret;
2582    }
2583
2584    FILL_PST_DUAPP_TO_RLC(pst, RLC_UL_INST, EVENT_RLC_UE_CREATE_REQ);
2585    /* Copying ueCfg to a sharable buffer */
2586    DU_ALLOC_SHRABL_BUF(rlcUeCfg, sizeof(RlcUeCfg));
2587    if(rlcUeCfg)
2588    {
2589       memset(rlcUeCfg, 0, sizeof(RlcUeCfg));
2590       memcpy(rlcUeCfg, duRlcUeCfg, sizeof(RlcUeCfg));
2591       /* Processing one Ue at a time to RLC */
2592       DU_LOG("\nDEBUG   -->  DU_APP: Sending UE create request to RLC UL");
2593       ret = (*packRlcUeCreateReqOpts[pst.selector])(&pst, rlcUeCfg);
2594       if(ret == RFAILED)
2595       {
2596          DU_LOG("\nERROR  -->  DU_APP : Failure in sending Ue Create Req to RLC");
2597          DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, rlcUeCfg, sizeof(RlcUeCfg));
2598          ret = RFAILED;
2599       }
2600    }
2601    else
2602    {
2603       DU_LOG("\n ERROR  -->  DU APP : Memory alloc failed at duBuildAndSendUeCreateReqToRlc()");
2604       ret = RFAILED;
2605    }
2606    return ret;
2607 }
2608
2609 /*******************************************************************
2610  *
2611  * @brief Processes UE create Rsp received from RLC UL
2612  *
2613  * @details
2614  *
2615  
2616  *    Function : DuProcRlcUeCfgRsp
2617  *
2618  *    Functionality: 
2619  *     Processes UE create Rsp received from RLC UL
2620  * 
2621  *  @params[in]  Post structure
2622  *               Pointer to RlcCfgCfm
2623  *  @return ROK     - success
2624  *          RFAILED - failure
2625  * 
2626  *****************************************************************/
2627 uint8_t DuProcRlcUeCfgRsp(Pst *pst, RlcUeCfgRsp *cfgRsp)
2628 {
2629    uint8_t ret = ROK;
2630
2631    if(cfgRsp)
2632    {
2633       if(cfgRsp->result == RLC_DU_APP_RSP_OK)
2634       {
2635          if(pst->event == EVENT_RLC_UE_CREATE_RSP)
2636          {
2637             DU_LOG("\nINFO   -->  DU_APP: RLC UE Create Response : SUCCESS [UE IDX:%d]", cfgRsp->ueId);
2638             duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].rlcUeCfg.rlcUeCfgState = UE_CREATE_COMPLETE;
2639
2640             if((duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].ueState == UE_HANDIN_IN_PROGRESS) &&
2641                   (duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].macUeCfg.macUeCfgState == UE_CREATE_COMPLETE) &&
2642                   (duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].rlcUeCfg.rlcUeCfgState == UE_CREATE_COMPLETE))
2643             {
2644                if((ret = duUpdateDuUeCbCfg(cfgRsp->ueId, cfgRsp->cellId)) == ROK)
2645                {
2646                   /* If UE is in handover, RACH resource needs to be requested
2647                    * from MAC for CFRA */
2648                   if((duBuildAndSendRachRsrcReqToMac(cfgRsp->cellId, cfgRsp->ueId)) != ROK)
2649                   {
2650                      DU_LOG("\nERROR  --> DU APP : Failed to send RACH Resource Request to MAC");
2651                      DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(MacUeCfgRsp));
2652                      DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(RlcUeCfgRsp));
2653                      return RFAILED;
2654                   }
2655                }
2656                else
2657                {
2658                   DU_LOG("\nERROR  -->  DU APP : Failure in updating DU UE CB");
2659                   DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(RlcUeCfgRsp));
2660                   return RFAILED;
2661                }
2662             }
2663          }
2664          else if(pst->event == EVENT_RLC_UE_RECONFIG_RSP)
2665          {
2666             DU_LOG("\nINFO   -->  DU_APP: RLC UE Reconfig Response : SUCCESS [UE IDX:%d]", cfgRsp->ueId);
2667
2668             duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].rlcUeCfg.rlcUeCfgState = UE_RECFG_COMPLETE;
2669             if((duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].macUeCfg.macUeCfgState == UE_RECFG_COMPLETE) &&
2670                   (duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueId -1].rlcUeCfg.rlcUeCfgState == UE_RECFG_COMPLETE))
2671             {
2672                if((ret = duUpdateDuUeCbCfg(cfgRsp->ueId, cfgRsp->cellId)) == ROK)
2673                {
2674                   if((BuildAndSendUeCtxtRsp(cfgRsp->cellId, cfgRsp->ueId)) != ROK)
2675                   {
2676                      DU_LOG("\nERROR  -->  DU APP : Failure in BuildAndSendUeCtxtRsp");
2677                      DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(RlcUeCfgRsp));
2678                      return RFAILED;
2679                   }
2680                }
2681                else
2682                {
2683                   DU_LOG("\nERROR  -->  DU APP : Failure in updating DU UE CB");
2684                   DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(RlcUeCfgRsp));
2685                   return RFAILED;
2686                }
2687             }
2688          }
2689       }
2690       else
2691       {
2692          DU_LOG("\nERROR  -->  DU_APP: RLC UE CFG Response for EVENT[%d] : FAILED [UE IDX : %d, REASON :%d]",\
2693                pst->event, cfgRsp->ueId, cfgRsp->reason);
2694          if((pst->event == EVENT_RLC_UE_RECONFIG_RSP))
2695          {
2696             //TODO: update failure case in ue Context setup Response
2697          }
2698          ret = RFAILED;
2699       }
2700       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(RlcUeCfgRsp));
2701    }
2702    else
2703    {
2704       DU_LOG("\nERROR  -->  DU_APP: Received RLC Ue Config Response is NULL at DuProcRlcUeCfgRsp()");
2705       ret = RFAILED;
2706    }
2707    return ret;
2708 }
2709
2710 /*******************************************************************
2711  *
2712  * @brief Builds and Send Ue Reconfig Req to RLC
2713  *
2714  * @details
2715  *
2716  *    Function : duBuildAndSendUeReCfgReqToRLC
2717  *
2718  *    Functionality: Builds and Send Ue Reconfig Req to RLC
2719  *
2720  * @params[in] cellId, crnti
2721  *             DuUeCfg *ueCfgDb
2722  *             RlcUeCfg *rlcUeCfg
2723  * @return ROK     - success
2724  *         RFAILED - failure
2725  *
2726  * ****************************************************************/
2727
2728 uint8_t duBuildAndSendUeReCfgReqToRlc(uint8_t cellId, uint8_t gnbDuUeF1apId, uint8_t crnti, DuUeCfg *ueCfgDb)
2729 {
2730    uint8_t ret = ROK;
2731    RlcUeCfg *rlcUeCfg = NULLP;
2732
2733    DU_ALLOC_SHRABL_BUF(rlcUeCfg, sizeof(RlcUeCfg));
2734    if(rlcUeCfg)
2735    {
2736       memset(rlcUeCfg, 0, sizeof(RlcUeCfg));
2737       ret = fillRlcUeCfg(cellId, gnbDuUeF1apId, ueCfgDb, rlcUeCfg);
2738       if(ret == RFAILED)
2739          DU_LOG("\nERROR  -->  DU APP : Failed at duBuildAndSendUeReCfgReqToRlc()");
2740       else
2741          ret = sendUeReCfgReqToRlc(rlcUeCfg);
2742    }
2743    else
2744    {
2745       DU_LOG("\nERROR  -->  DU APP : Memory Alloc failed at duBuildAndSendUeReCfgReqToRlc()");
2746       ret = RFAILED;
2747    }
2748    return ret;
2749 }
2750
2751 /*******************************************************************
2752  *
2753  * @brief Builds and Send Ue Reconfig Req to MAC
2754  *
2755  * @details
2756  *
2757  *    Function : duBuildAndSendUeReCfgReqToMac
2758  *
2759  *    Functionality: Builds and Send Ue Reconfig Req to MAC
2760  *
2761  * @params[in] CellGroupConfigRrc_t *macCellGrpCfg
2762  *             DuUeCfg *ueCfgDb
2763  *             MacUeCfg    *macUeCfg
2764  * @return ROK     - success
2765  *         RFAILED - failure
2766  *
2767  * ****************************************************************/
2768
2769 uint8_t duBuildAndSendUeReCfgReqToMac(uint8_t cellId, uint8_t duUeF1apId, uint8_t crnti, DuUeCfg *ueCfgDb)
2770 {
2771    uint8_t ret = ROK;
2772    MacUeCfg *macUeCfg = NULLP;
2773
2774    DU_ALLOC_SHRABL_BUF(macUeCfg, sizeof(MacUeCfg));
2775    if(macUeCfg)
2776    {
2777       memset(macUeCfg, 0, sizeof(MacUeCfg));
2778       ret = fillMacUeCfg(cellId, duUeF1apId, crnti, ueCfgDb, macUeCfg);
2779       if(ret == RFAILED)
2780          DU_LOG("\nERROR  -->  DU APP : Failed to fill Mac Ue Cfg at duBuildAndSendUeReCfgReqToMac()");
2781       else
2782          ret = sendUeReCfgReqToMac(macUeCfg);
2783    }
2784    else
2785    {
2786       DU_LOG("\nERROR  -->  DU APP : Memory alloc failed for macUeCfg at duBuildAndSendUeReCfgReqToMac()");
2787       ret = RFAILED;
2788    }
2789    return ret;
2790 }
2791
2792 /*******************************************************************
2793  *
2794  * @brief Build and Send Ue context setup request
2795  *
2796  * @details
2797  *
2798  
2799  *    Function : duBuildAndSendUeContextSetupReq
2800  *
2801  *    Functionality: 
2802  *     Build and Send Ue context setup request
2803  * 
2804  *  @params[in]  cellId, crnti, DuUeCfg pointer
2805  *  @return ROK     - success
2806  *          RFAILED - failure
2807  * 
2808  *****************************************************************/
2809
2810 uint8_t duBuildAndSendUeContextSetupReq(uint16_t cellId, DuUeCb *ueCb)
2811 {
2812    uint8_t ret = ROK;
2813    uint16_t crnti; 
2814    DuUeCfg *duUeCfg = NULLP;
2815
2816    DU_LOG("\nDEBUG   -->  DU_APP: Processing Ue Context Setup Request for cellId [%d]", cellId);
2817
2818    if(!ueCb)
2819    {
2820       DU_LOG("\nERROR  -->  DU APP : UE Cb is NULL");
2821       return RFAILED;
2822    }
2823
2824    crnti = ueCb->crnti;
2825    duUeCfg = &ueCb->f1UeDb->duUeCfg;
2826
2827    /* If UE is being handed-in to this DU, UE context setup request will create
2828     * new UE context at MAC/SCH and RLC.
2829     * If UE is in active state, UE contex setup request will lead to
2830     * reconfiguration of UE at MAC/SCH and RLC
2831     */
2832    if(ueCb->ueState == UE_HANDIN_IN_PROGRESS)
2833    {
2834       /* Filling MAC UE Config */
2835       memset(&ueCb->macUeCfg, 0, sizeof(MacUeCfg));
2836
2837       /* Since UE attach has not yet happened, crnti is unknow. Hence passing 0 */
2838       ret = duBuildAndSendUeCreateReqToMac(cellId, ueCb->gnbDuUeF1apId, 0, duUeCfg, &ueCb->macUeCfg);
2839       if(ret == RFAILED)
2840          DU_LOG("\nERROR  -->  DU APP : Failed to send UE create request to MAC");
2841
2842       ret = duBuildAndSendUeCreateReqToRlc(cellId, ueCb->gnbDuUeF1apId, duUeCfg, &ueCb->rlcUeCfg);
2843       if(ret == RFAILED)
2844          DU_LOG("\nERROR  --> DU APP : Failed to send UE create request to RLC");
2845
2846    }
2847    else
2848    {
2849       /* Filling RLC UE Reconfig */ 
2850       ret = duBuildAndSendUeReCfgReqToRlc(cellId, ueCb->gnbDuUeF1apId, crnti, duUeCfg);
2851       if(ret == RFAILED)
2852          DU_LOG("\nERROR  -->  DU APP : Failed to build ctxt setup req for RLC at duBuildAndSendUeContextSetupReq()");
2853
2854       /* Filling MAC UE Reconfig */
2855       ret = duBuildAndSendUeReCfgReqToMac(cellId, ueCb->gnbDuUeF1apId, crnti, duUeCfg);
2856       if(ret == RFAILED)
2857          DU_LOG("\nERROR  -->  DU APP : Failed at build ctxt setup req for MAC at duBuildAndSendUeContextSetupReq()");
2858    }
2859
2860    return ret;
2861 }
2862
2863 /*******************************************************************
2864  *
2865  * @brief Processes DL Rsp received from RLC DL
2866  *
2867  * @details
2868  *
2869  
2870  *    Function : DuProcRlcDlRrcMsgRsp
2871  *
2872  *    Functionality: 
2873  *     Processes UE Rsp received from RLC DL
2874  * 
2875  *  @params[in]  Post structure
2876  *               Pointer to RlcCfgCfm
2877  *  @return ROK     - success
2878  *          RFAILED - failure
2879  * 
2880  *****************************************************************/
2881 uint8_t DuProcRlcDlRrcMsgRsp(Pst *pst, RlcDlRrcMsgRsp *dlRrcMsg)
2882 {
2883    uint8_t ret = ROK, ueId = 0;
2884    uint16_t cellId, crnti;
2885    DuUeCb *ueCb = NULLP;
2886    DlMsgState state;
2887
2888    state = dlRrcMsg->state;
2889    cellId = dlRrcMsg->cellId;
2890    crnti = dlRrcMsg->crnti;
2891    DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, dlRrcMsg, sizeof(RlcDlRrcMsgRsp));
2892
2893    if(state == TRANSMISSION_COMPLETE)
2894    {
2895       GET_UE_ID(crnti, ueId);
2896       ueCb = &duCb.actvCellLst[cellId -1]->ueCb[ueId -1];
2897
2898       if(ueCb->f1UeDb && ueCb->f1UeDb->dlRrcMsgPres)
2899       {
2900          if(ueCb->f1UeDb->actionType == UE_CTXT_SETUP)
2901          {
2902             ret = duBuildAndSendUeContextSetupReq(cellId, ueCb);
2903             if(ret == RFAILED)
2904                DU_LOG("\nERROR  -->  DU APP : Failed to process UE Context Setup Request in DuProcRlcDlRrcMsgRsp()");
2905          }
2906          
2907          if(ueCb->f1UeDb->actionType == UE_CTXT_MOD)
2908          {
2909             ret = duBuildAndSendUeContextModReq(cellId, ueCb->gnbDuUeF1apId, crnti, &ueCb->f1UeDb->duUeCfg);
2910             if(ret == RFAILED)
2911                DU_LOG("\nERROR  -->  DU APP : Failed to process UE Context Mod Request in DuProcRlcDlRrcMsgRsp()");
2912          }
2913
2914          if(ueCb->f1UeDb->actionType == UE_CTXT_RELEASE && ueCb->ueState == UE_ACTIVE)
2915          {
2916             ret = duBuildAndSendUeDeleteReq(cellId, crnti);
2917             if(ret == RFAILED)
2918             {
2919                DU_LOG("\nERROR  -->  DU APP : Failed to process UE Context Release Request in DuProcRlcDlRrcMsgRsp()");
2920             }
2921          }
2922       }
2923    }
2924    else
2925       DU_LOG("\nERROR  -->  DU APP : Failed to transmit DL RRC Msg");
2926
2927    return ret;
2928 }
2929 /*******************************************************************
2930  *
2931  * @brief Process UE context setup request from CU
2932  *
2933  * @details
2934  *
2935  *    Function : duProcUeContextSetupRequest 
2936  *
2937  *    Functionality: Process UE context setup request from CU
2938  *
2939  * @params[in] F1AP message
2940  * @return ROK     - success
2941  *         RFAILED - failure
2942  *
2943  * ****************************************************************/
2944
2945 uint8_t duProcUeContextSetupRequest(DuUeCb *ueCb)
2946 {
2947    uint8_t ret, cellId;
2948
2949    ret = ROK;
2950    if(ueCb)
2951    {
2952       cellId = duCb.actvCellLst[ueCb->f1UeDb->cellIdx]->cellId;
2953
2954       /* Send DL RRC msg for security Mode */
2955       if(ueCb->f1UeDb->dlRrcMsg)
2956       {
2957          if(ueCb->f1UeDb->dlRrcMsg->rrcMsgPdu)
2958          {
2959             /* Sending DL RRC Message to RLC */
2960             ret = duBuildAndSendDlRrcMsgToRlc(cellId, ueCb->rlcUeCfg, ueCb->f1UeDb->dlRrcMsg);
2961             if(ret == RFAILED)
2962             {
2963                DU_LOG("\nERROR  -->  DU APP : Failed to send DL RRC msg in duProcUeContextSetupRequest()");
2964                DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, ueCb->f1UeDb->dlRrcMsg->rrcMsgPdu,\
2965                      ueCb->f1UeDb->dlRrcMsg->rrcMsgSize);
2966                DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, ueCb->f1UeDb->dlRrcMsg, sizeof(F1DlRrcMsg));
2967             }
2968          }
2969       }
2970       else if(ueCb->f1UeDb->actionType == UE_CTXT_SETUP)
2971       {
2972          ret = duBuildAndSendUeContextSetupReq(cellId, ueCb);
2973          if(ret == RFAILED)
2974          {
2975             DU_LOG("\nERROR  -->  DU APP : Failed to build ue context setup Req in duProcUeContextSetupRequest()");
2976          }
2977       }
2978    }
2979    else
2980    {
2981       //TODO: To send the failure cause in UeContextSetupRsp
2982       DU_LOG("ERROR  -->  DU APP : Failed to process UE CNTXT SETUP REQ at duProcUeContextSetupRequest()");
2983    }
2984    return ret;
2985 }
2986
2987 /*******************************************************************
2988  *
2989  * @brief Build and Send Ue context mod request
2990  *
2991  * @details
2992  *
2993  *
2994  *    Function : duBuildAndSendUeContextModReq
2995  *
2996  *    Functionality: 
2997  *     Build and Send Ue context mod request
2998  * 
2999  *  @params[in]  cellId, crnti, DuUeCfg pointer
3000  *  @return ROK     - success
3001  *          RFAILED - failure
3002  * 
3003  *****************************************************************/
3004
3005 uint8_t duBuildAndSendUeContextModReq(uint16_t cellId, uint8_t gnbDuUeF1apId, uint16_t crnti, DuUeCfg *duUeCfg)
3006 {
3007    uint8_t ret = ROK;
3008
3009    DU_LOG("\nDEBUG   -->  DU_APP: Processing Ue Context Mod Request for cellId [%d]", cellId);
3010    /* Filling RLC Ue Reconfig */ 
3011    ret = duBuildAndSendUeReCfgReqToRlc(cellId, gnbDuUeF1apId, crnti, duUeCfg);
3012    if(ret == RFAILED)
3013       DU_LOG("\nERROR  -->  DU APP : Failed to build ctxt setup req for RLC at duBuildAndSendUeContextModReq()");
3014    
3015    /* Filling MAC Ue Reconfig */
3016    ret = duBuildAndSendUeReCfgReqToMac(cellId, gnbDuUeF1apId, crnti, duUeCfg);
3017    if(ret == RFAILED)
3018       DU_LOG("\nERROR  -->  DU APP : Failed at build ctxt setup req for MAC at duBuildAndSendUeContextModReq()");
3019
3020    return ret;
3021 }
3022 /*******************************************************************
3023  *
3024  * @brief Processing Ue context mod request
3025  *
3026  * @details
3027  *
3028  *
3029  *    Function : duProcUeContextModReq 
3030  *
3031  *    Functionality:
3032  *        Processing  Ue context mod request
3033  *
3034  *  @params[in] DuUeCb *ueCb 
3035  *  @return ROK     - success
3036  *          RFAILED - failure
3037  *
3038  *****************************************************************/
3039
3040 uint8_t duProcUeContextModReq(DuUeCb *ueCb)
3041 {
3042    uint8_t ret, cellId;
3043
3044    ret = ROK;
3045    if(ueCb)
3046    {
3047       cellId = duCb.actvCellLst[ueCb->f1UeDb->cellIdx]->cellId;
3048       /* Send DL RRC msg for security Mode */
3049       if(ueCb->f1UeDb->dlRrcMsg)
3050       {
3051          if(ueCb->f1UeDb->dlRrcMsg->rrcMsgPdu)
3052          {
3053             /* Sending DL RRC Message to RLC */
3054             ret = duBuildAndSendDlRrcMsgToRlc(cellId, ueCb->rlcUeCfg, ueCb->f1UeDb->dlRrcMsg);
3055             if(ret == RFAILED)
3056             {
3057                DU_LOG("\nERROR  -->  DU APP : Failed to send DL RRC msg in duProcUeContextModReq()");
3058                DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, ueCb->f1UeDb->dlRrcMsg->rrcMsgPdu,\
3059                      ueCb->f1UeDb->dlRrcMsg->rrcMsgSize);
3060                DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, ueCb->f1UeDb->dlRrcMsg, sizeof(F1DlRrcMsg));
3061             }
3062          }
3063       }
3064       else if(ueCb->f1UeDb->actionType == UE_CTXT_MOD)
3065       {
3066          ret = duBuildAndSendUeContextModReq(cellId, ueCb->gnbDuUeF1apId, ueCb->crnti, &ueCb->f1UeDb->duUeCfg);
3067          if(ret == RFAILED)
3068          {
3069             DU_LOG("\nERROR  -->  DU APP : Failed to build ue context setup Req in duProcUeContextModReq()");
3070             return RFAILED;
3071          }
3072       }
3073       else if((ueCb->f1UeDb->actionType == UE_CTXT_CFG_QUERY) || (ueCb->f1UeDb->actionType == UE_CTXT_RRC_RECFG_COMPLETE))
3074       {
3075          if((BuildAndSendUeContextModRsp(ueCb) != ROK))
3076          {
3077             DU_LOG("\nERROR  -->  DU APP : Failed to build UE Context modification response");
3078             return RFAILED;
3079          }
3080       }
3081    }
3082    else
3083    {
3084       //TODO: To send the failure cause in UeContextModRsp
3085       
3086       DU_LOG("ERROR  -->  DU APP : Failed to process UE CNTXT MOD REQ at duProcUeContextModReq()");
3087       return RFAILED;
3088    }
3089    return ROK;
3090 }
3091
3092 /*******************************************************************
3093 *
3094 * @brief Build and send dedicated RACH resource release request to MAC
3095 *
3096 * @details
3097 *
3098 *    Function : duBuildAndSendRachRsrcRelToMac
3099 *
3100 *    Functionality: Function to Build and send dedicated RACH resource 
3101 *    release request to MAC
3102 *
3103 * @params[in] Cell ID
3104 *             UE CB
3105 * @return ROK - Success
3106 *         RFAILED - Failure
3107 *
3108 * ****************************************************************/
3109 uint8_t duBuildAndSendRachRsrcRelToMac(uint16_t cellId, DuUeCb *ueCb)
3110 {
3111    Pst pst;
3112    MacRachRsrcRel *rachRsrcRel = NULLP;
3113
3114    DU_ALLOC_SHRABL_BUF(rachRsrcRel, sizeof(MacRachRsrcRel));
3115    if(!rachRsrcRel)
3116    {
3117       DU_LOG("\nERROR  -->  DU APP : Failed to allocate memory for RACH Resource Release in \
3118             duBuildAndSendRachRsrcRelToMac()");
3119       return RFAILED;
3120    }
3121
3122    rachRsrcRel->cellId = cellId;
3123    rachRsrcRel->ueId = ueCb->gnbDuUeF1apId;
3124    rachRsrcRel->crnti = ueCb->crnti;
3125
3126    /* Fill Pst */
3127    FILL_PST_DUAPP_TO_MAC(pst, EVENT_MAC_RACH_RESOURCE_REL);
3128
3129    if(((*packMacRachRsrcRelOpts[pst.selector])(&pst, rachRsrcRel)) != ROK)
3130    {
3131       DU_LOG("\nERROR  -->  DU_APP : Failure in sending RACH Resource Release to MAC at \
3132             duBuildAndSendRachRsrcRelToMac()");
3133       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, rachRsrcRel, sizeof(MacRachRsrcRel));
3134       return RFAILED;
3135    }
3136
3137    return ROK;
3138 }
3139
3140 /*******************************************************************
3141  *
3142 * @brief delete MacUeCfg from duCb
3143 *
3144 * @details
3145 *
3146 *    Function : deleteMacUeCfg 
3147 *
3148 *    Functionality: delete MacUeCfg from duCb
3149 *
3150 * @params[in] Pointer to MacUeCfg 
3151 * @return ROK     - success
3152 *         RFAILED - failure
3153 *
3154 *******************************************************************/
3155
3156 void deleteMacUeCfg(MacUeCfg *ueCfg)
3157 {
3158    uint8_t lcCfgIdx=0;
3159    
3160    DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL,ueCfg->ambrCfg, sizeof(AmbrCfg));
3161    if(ueCfg->spCellCfgPres)
3162    {
3163       freeUeReCfgCellGrpInfo(ueCfg);
3164    }
3165    for(lcCfgIdx=0; lcCfgIdx< ueCfg->numLcs; lcCfgIdx++)
3166    {
3167       freeMacLcCfg(&ueCfg->lcCfgList[lcCfgIdx]);
3168    }
3169    memset(ueCfg, 0, sizeof(MacUeCfg));
3170 }
3171
3172 /*******************************************************************
3173 *
3174 * @brief delete UE Configuration of a particular UE
3175 *
3176 * @details
3177 *
3178 *    Function : deleteUeCfg 
3179 *
3180 *    Functionality: delete UE Configuration of a particular UE 
3181 *
3182 * @params[in] uint16_t cellIdx, uint8_t ueId
3183 * @return ROK     - success
3184 *         RFAILED - failure
3185 *
3186 * ****************************************************************/
3187 uint8_t  deleteUeCfg(uint16_t cellId, uint8_t ueId)
3188 {
3189    uint8_t tnlIdx = 0;
3190    uint16_t cellIdx = 0;
3191    DuUeCb *ueCb = NULLP;
3192    
3193    GET_CELL_IDX(cellId, cellIdx);
3194    if(duCb.actvCellLst[cellIdx] != NULLP)
3195    {
3196       if((duCb.actvCellLst[cellIdx]->ueCb[ueId-1].macUeCfg.macUeCfgState == UE_DELETE_COMPLETE)\
3197             &&(duCb.actvCellLst[cellIdx]->ueCb[ueId-1].rlcUeCfg.rlcUeCfgState == UE_DELETE_COMPLETE))
3198       {
3199          ueCb = &duCb.actvCellLst[cellIdx]->ueCb[ueId-1];
3200          deleteMacUeCfg(&ueCb->macUeCfg);
3201          deleteRlcUeCfg(&ueCb->rlcUeCfg);
3202          if(ueCb->f1UeDb !=NULLP)
3203          {
3204             freeF1UeDb(ueCb->f1UeDb);
3205          }
3206          for(tnlIdx = 0; tnlIdx < duCb.numTeId; )
3207          {
3208             if(duCb.upTnlCfg[tnlIdx]->ueId == ueId)
3209             {
3210                duCb.upTnlCfg[tnlIdx]->configType = CONFIG_DEL;
3211                duProcEgtpTunnelCfg(tnlIdx, duCb.upTnlCfg[tnlIdx], duCb.upTnlCfg[tnlIdx]);
3212             }
3213             else
3214                tnlIdx++;
3215          }
3216          unsetBitInUeBitMap(cellId, ueId-1);
3217          duCb.actvCellLst[cellIdx]->numActvUes--;
3218          memset(ueCb, 0, sizeof(DuUeCb));
3219       }
3220       else
3221       {
3222          return RFAILED;
3223       }
3224    }
3225    else
3226    {
3227       DU_LOG("\nERROR  --> DU APP : deleteUeCfg(): CellIdx[%d] is not found", cellIdx);
3228       return RFAILED;
3229    }
3230    return ROK;
3231 }
3232
3233
3234 /*******************************************************************
3235 *
3236 * @brief Handle UE delete response from MAC
3237 *
3238 * @details
3239 *
3240 *    Function : DuProcMacUeDeleteRsp
3241 *
3242 *    Functionality: Handle UE delete response from MAC
3243 *
3244 * @params[in] Pointer to MacUeDeleteRsp and Pst
3245 * @return ROK     - success
3246 *         RFAILED - failure
3247 *
3248 * ****************************************************************/
3249
3250 uint8_t DuProcMacUeDeleteRsp(Pst *pst, MacUeDeleteRsp *deleteRsp)
3251 {
3252    uint8_t  ret =ROK,ueId=0;
3253    uint16_t cellIdx=0;
3254    uint32_t gnbCuUeF1apId =0 , gnbDuUeF1apId =0;
3255    
3256    if(deleteRsp)
3257    {
3258       if(deleteRsp->result == DEL_SUCCESSFUL)
3259       {
3260          DU_LOG("\nINFO   -->  DU APP : MAC UE Delete Response : SUCCESS [UE IDX : %d]", deleteRsp->ueId);
3261          GET_CELL_IDX(deleteRsp->cellId, cellIdx);
3262          if(duCb.actvCellLst[cellIdx])
3263          {
3264             duCb.actvCellLst[cellIdx]->ueCb[deleteRsp->ueId -1].macUeCfg.macUeCfgState = UE_DELETE_COMPLETE;
3265             ueId = deleteRsp->ueId;
3266             gnbCuUeF1apId = duCb.actvCellLst[cellIdx]->ueCb[ueId-1].gnbCuUeF1apId;
3267             gnbDuUeF1apId = duCb.actvCellLst[cellIdx]->ueCb[ueId-1].gnbDuUeF1apId;
3268             if(deleteUeCfg(deleteRsp->cellId, ueId) == ROK)
3269             {
3270                ret = BuildAndSendUeContextReleaseComplete(deleteRsp->cellId, gnbCuUeF1apId, gnbDuUeF1apId);
3271                if(ret != ROK)
3272                {
3273                   DU_LOG("\nERROR  -->  DU APP : DuProcMacUeDeleteRsp(): failed to send UE context release complete");
3274                }
3275             }
3276
3277          }
3278       }
3279       else
3280       {
3281          DU_LOG("\nERROR  -->  DU APP : DuProcMacUeDeleteRsp(): MAC UE Delete Response : FAILURE [UE IDX : %d]",\
3282          deleteRsp->ueId);
3283          ret =  RFAILED;
3284       }
3285       DU_FREE_SHRABL_BUF(pst->region, pst->pool, deleteRsp, sizeof(MacUeDeleteRsp));
3286    }
3287    else
3288    {
3289       DU_LOG("\nERROR  -->  DU APP : DuProcMacUeDeleteRsp(): MAC UE Delete Response is null");
3290       ret = RFAILED;
3291    }
3292    return ret;
3293 }
3294
3295 /*******************************************************************
3296  *
3297  * @brief Processes UE Delete Rsp received from RLC 
3298  *
3299  * @details
3300  *
3301  *    Function : DuProcRlcUeDeleteRsp
3302  *
3303  *    Functionality:
3304  *     Processes UE Delete Rsp received from RLC 
3305  *
3306  *  @params[in]  Post structure
3307  *               Pointer to RlcUeDeleteRsp
3308  *  @return ROK     - success
3309  *          RFAILED - failure
3310  *
3311  * *****************************************************************/
3312
3313 uint8_t DuProcRlcUeDeleteRsp(Pst *pst, RlcUeDeleteRsp *delRsp)
3314 {
3315    uint8_t  ueId = 0, ret = ROK;
3316    uint16_t cellIdx = 0,crnti=0;
3317
3318    if(delRsp)
3319    {
3320       ueId = delRsp->ueId;
3321       GET_CELL_IDX(delRsp->cellId, cellIdx);
3322
3323       if(delRsp->result == SUCCESSFUL)
3324       {
3325          DU_LOG("\nINFO   -->  DU_APP: RLC UE Delete Response : SUCCESS [UE IDX:%d]", ueId);
3326          if(duCb.actvCellLst[cellIdx]!=NULLP)
3327          {
3328             duCb.actvCellLst[cellIdx]->ueCb[ueId-1].rlcUeCfg.rlcUeCfgState = UE_DELETE_COMPLETE;
3329             GET_CRNTI(crnti, ueId);
3330             if(sendUeDeleteReqToMac(delRsp->cellId, ueId, crnti) == RFAILED)
3331             {
3332                DU_LOG("\nERROR  -->  DU APP : duBuildAndSendUeDeleteReq(): Failed to build UE  delete req for MAC ");
3333                return RFAILED;
3334             }
3335
3336          }
3337       }
3338       else
3339       {
3340          DU_LOG("\nERROR   -->  DU_APP: RLC UE Delete Response : FAILED [UE IDX:%d]", ueId);
3341          ret = RFAILED;
3342       }
3343       DU_FREE_SHRABL_BUF(pst->region, pst->pool, delRsp, sizeof(RlcUeDeleteRsp));
3344
3345    }
3346    return ret;
3347 }
3348
3349 /*******************************************************************
3350  *
3351  * @brief Sending UE Delete Req To Mac
3352 *
3353 * @details
3354 *
3355 *    Function : sendUeDeleteReqToMac
3356 *
3357 *    Functionality:
3358 *     sending UE Delete Req To Mac
3359 *
3360 *  @params[in]    cellId, ueId, crnti 
3361 *  @return ROK     - success
3362 *          RFAILED - failure
3363 *
3364 *****************************************************************/
3365
3366 uint8_t sendUeDeleteReqToMac(uint16_t cellId, uint8_t ueId, uint16_t crnti)
3367 {
3368    Pst pst;
3369    uint8_t ret=ROK;
3370    MacUeDelete *ueDelete = NULLP;
3371
3372    DU_ALLOC_SHRABL_BUF(ueDelete, sizeof(MacUeDelete));
3373    if(ueDelete)
3374    {
3375       ueDelete->cellId = cellId;
3376       ueDelete->ueId   = ueId;
3377       ueDelete->crnti  = crnti;
3378       FILL_PST_DUAPP_TO_MAC(pst, EVENT_MAC_UE_DELETE_REQ);
3379
3380       DU_LOG("\nDEBUG  -->  DU_APP: Sending UE delete Request to MAC ");
3381       ret = (*packMacUeDeleteReqOpts[pst.selector])(&pst, ueDelete);
3382       if(ret == RFAILED)
3383       {
3384          DU_LOG("\nERROR  -->  DU_APP: sendUeDeleteReqToMac(): Failed to send UE delete Req to MAC");
3385          DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, ueDelete, sizeof(MacUeDelete));
3386       }
3387    }
3388    else
3389    {
3390       DU_LOG("\nERROR  -->   DU_APP: sendUeDeleteReqToMac(): Failed to allocate memory"); 
3391       ret = RFAILED;
3392    }
3393    return ret;
3394 }
3395
3396 /*******************************************************************
3397  *
3398  * @brief Sending UE Delete Req To Rlc
3399  *
3400  * @details
3401  *
3402  *    Function : sendUeDeleteReqToRlc
3403  *
3404  *    Functionality:
3405  *     Sending UE Delete Req To Rlc
3406  *
3407  *  @params[in]  cellId, ueId 
3408  *  @return ROK     - success
3409  *          RFAILED - failure
3410  *
3411  *****************************************************************/
3412
3413 uint8_t sendUeDeleteReqToRlc(uint16_t cellId, uint8_t ueId)
3414 {
3415    uint8_t ret;
3416    Pst pst;
3417    RlcUeDelete *ueDelete;
3418
3419    DU_ALLOC_SHRABL_BUF(ueDelete, sizeof(RlcUeDelete));
3420    if(ueDelete !=NULLP)
3421    {
3422       ueDelete->cellId = cellId;
3423       ueDelete->ueId = ueId;
3424       FILL_PST_DUAPP_TO_RLC(pst, RLC_UL_INST, EVENT_RLC_UE_DELETE_REQ);
3425
3426       ret = (*packRlcUeDeleteReqOpts[pst.selector])(&pst, ueDelete);
3427       if(ret == RFAILED)
3428       {
3429          DU_LOG("\nERROR  -->  DU_APP : sendUeDeleteReqToRlc():Failed to send UE Delete  Req to RLC"); 
3430          DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, ueDelete, sizeof(RlcUeDelete));
3431       }
3432    }
3433    else
3434    {
3435       DU_LOG("\nERROR  -->   DU_APP: sendUeDeleteReqToRlc():Memory allocation failed");
3436       ret = RFAILED;
3437    }
3438    return ret;
3439 }
3440
3441 /*******************************************************************
3442  *
3443  * @brief DU processes UE delete req from CU and sends to MAC and RLC 
3444  *
3445  * @details
3446  *
3447  *    Function : duBuildAndSendUeDeleteReq
3448  *
3449  *    Functionality: DU processes UE delete req from CU and sends to MAC 
3450  *                   and RLC 
3451  *
3452  * @params[in] cellId, crnti 
3453  * @return ROK     - success
3454  *         RFAILED - failure
3455  *
3456  * ****************************************************************/
3457
3458 uint8_t duBuildAndSendUeDeleteReq(uint16_t cellId, uint16_t crnti)
3459 {
3460    uint8_t  ueId =0;
3461    uint16_t cellIdx = 0;
3462
3463    DU_LOG("\nDEBUG  -->  DU_APP: Processing UE Delete Request ");
3464    GET_CELL_IDX(cellId, cellIdx);
3465    GET_UE_ID(crnti, ueId);
3466
3467    if(duCb.actvCellLst[cellIdx] != NULLP)
3468    {
3469       if(crnti != duCb.actvCellLst[cellIdx]->ueCb[ueId - 1].crnti)
3470       {
3471          DU_LOG("\nERROR  -->  DU APP : duBuildAndSendUeDeleteReq(): CRNTI [%d] not found", crnti);
3472          return RFAILED;
3473       }
3474
3475       duCb.actvCellLst[cellIdx]->ueCb[ueId - 1].ueState = UE_DELETION_IN_PROGRESS; 
3476       if(sendUeDeleteReqToRlc(cellId, ueId) == RFAILED)
3477       {
3478          DU_LOG("\nERROR  -->  DU APP : DuProcMacUeDeleteRsp():Failed to build UE  delete req for RLC ");
3479          return RFAILED;
3480       }
3481    }
3482    else
3483    {
3484       DU_LOG("\nERROR  -->  DU APP : duBuildAndSendUeDeleteReq(): Cell Id is not found");
3485       return RFAILED;
3486    }
3487
3488    return ROK;
3489 }
3490
3491 /*******************************************************************
3492  *
3493  * @brief delete RlcUeCfg from duCb
3494  *
3495  * @details
3496  *
3497  *    Function : deleteRlcUeCfg
3498  *
3499  *    Functionality:
3500  *       delete  RlcUeCfg from duCb
3501  *
3502  *  @params[in] RlcUeCfg *ueCfg 
3503  *               
3504  *  @return ROK     - success
3505  *          RFAILED - failure
3506  *
3507  *****************************************************************/
3508
3509 void deleteRlcUeCfg(RlcUeCfg *ueCfg)
3510 {
3511    uint8_t lcIdx = 0;
3512    RlcBearerCfg *lcCfg= NULLP;
3513    if(ueCfg)
3514    {
3515       for(lcIdx =0 ; lcIdx < ueCfg->numLcs ; lcIdx++)
3516       {
3517          lcCfg = &ueCfg->rlcLcCfg[lcIdx];
3518          switch(lcCfg->rlcMode)
3519          {
3520             case RLC_AM :
3521                {
3522                   DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, lcCfg->u.amCfg, sizeof(AmBearerCfg));
3523                   break;
3524                }
3525             case RLC_UM_BI_DIRECTIONAL :
3526                {
3527                   DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, lcCfg->u.umBiDirCfg, sizeof(UmBiDirBearerCfg));
3528                   break;
3529                }
3530             case RLC_UM_UNI_DIRECTIONAL_UL :
3531                {
3532                   DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, lcCfg->u.umUniDirUlCfg, sizeof(UmUniDirUlBearerCfg));
3533                   break;
3534                }
3535             case RLC_UM_UNI_DIRECTIONAL_DL :
3536                {
3537                   DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, lcCfg->u.umUniDirDlCfg, sizeof(UmUniDirDlBearerCfg));
3538                   break;
3539                }
3540          }
3541          DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, lcCfg->snssai, sizeof(Snssai));
3542       }
3543       memset(ueCfg, 0, sizeof(RlcUeCfg));
3544    }
3545 }
3546
3547 /*******************************************************************
3548 *
3549 * @brief Du process Ue Context Release Command
3550 *
3551 * @details
3552 *
3553 *    Function : duProcUeContextReleaseCommand 
3554 *
3555 *    Functionality: Du process Ue Context Release Command 
3556 *
3557 * @params[in] DuUeCb *duUeCb 
3558 * @return ROK     - success
3559 *         RFAILED - failure
3560 *
3561 * ****************************************************************/
3562 uint8_t duProcUeContextReleaseCommand(uint16_t cellId, DuUeCb *duUeCb)
3563 {
3564    uint8_t ret =ROK, ueId=0;
3565    uint16_t crnti = 0;
3566
3567    if(duUeCb != NULLP)
3568    {
3569       crnti = duUeCb->crnti;
3570       GET_UE_ID(crnti, ueId);
3571       
3572       if(duUeCb->f1UeDb)
3573       {
3574          /* Send DL RRC msg for RRC release */
3575          if(duUeCb->f1UeDb->dlRrcMsg)
3576          {
3577             if(duUeCb->f1UeDb->dlRrcMsg->rrcMsgPdu != NULLP)
3578             {
3579                ret = duBuildAndSendDlRrcMsgToRlc(cellId, duCb.actvCellLst[duUeCb->f1UeDb->cellIdx]->ueCb[ueId-1].rlcUeCfg,\
3580                      duUeCb->f1UeDb->dlRrcMsg);
3581                if(ret == RFAILED)
3582                {
3583                   DU_LOG("\nERROR  -->  DU APP : duProcUeContextReleaseCommand() : Failed to send DL RRC msg");
3584                   DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, duUeCb->f1UeDb->dlRrcMsg->rrcMsgPdu,\
3585                         duUeCb->f1UeDb->dlRrcMsg->rrcMsgSize);
3586                   DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, duUeCb->f1UeDb->dlRrcMsg, sizeof(F1DlRrcMsg));
3587                }
3588             }
3589          }
3590       }
3591       else
3592       {
3593          ret = duBuildAndSendUeDeleteReq(cellId,crnti);
3594          if(ret == RFAILED)
3595          {
3596             DU_LOG("\nERROR  -->  DU APP : duProcUeContextReleaseCommand(): Failed to build and send Ue Delete request");
3597          }
3598       }
3599    }
3600    return ret;
3601 }
3602
3603 /**********************************************************************
3604   End of file
3605 ***********************************************************************/