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