[JIRA ID: ODUHIGH-232]: RB config for MAC and SCH
[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<ProtocolIE-Field.h>
34 #include "F1AP-PDU.h"
35 #include "du_mgr.h"
36 #include "du_f1ap_msg_hdl.h"
37 #include "du_ue_mgr.h"
38
39 #ifdef EGTP_TEST
40 U32 sduId = 0;
41 #endif
42
43 DuMacDlCcchInd packMacDlCcchIndOpts[] =
44 {
45    packMacDlCcchInd,   /* Loose coupling */
46    MacProcDlCcchInd,    /* TIght coupling */
47    packMacDlCcchInd    /* Light weight-loose coupling */
48 };
49
50 DuMacUeCreateReq packMacUeCreateReqOpts[] =
51 {
52    packDuMacUeCreateReq,       /* Loose coupling */
53    MacProcUeCreateReq,          /* TIght coupling */
54    packDuMacUeCreateReq       /* Light weight-loose coupling */
55 };
56
57 DuRlcUeCreateReq packRlcUeCreateReqOpts[] =
58 {
59    packDuRlcUeCreateReq,       /* Loose coupling */
60    RlcProcUeCreateReq,          /* TIght coupling */
61    packDuRlcUeCreateReq       /* Light weight-loose coupling */
62 };
63
64 DuDlRrcMsgToRlcFunc duSendDlRrcMsgToRlcOpts[] =
65 {
66    packDlRrcMsgToRlc,          /* Loose coupling */ 
67    RlcProcDlRrcMsgTransfer,       /* Tight coupling */
68    packDlRrcMsgToRlc           /* Light weight-loose coupling */
69 };
70
71 DuRlcUeReconfigReq packRlcUeReconfigReqOpts[] =
72 {
73    packDuRlcUeReconfigReq,       /* Loose coupling */
74    RlcProcUeReconfigReq,       /* TIght coupling */
75    packDuRlcUeReconfigReq       /* Light weight-loose coupling */
76 };
77
78 DuMacUeReconfigReq packMacUeReconfigReqOpts[] =
79 {
80    packDuMacUeReconfigReq,       /* Loose coupling */
81    MacProcUeReconfigReq,       /* TIght coupling */
82    packDuMacUeReconfigReq     /* Light weight-loose coupling */
83 };
84 /*******************************************************************
85  *
86  * @brief Handles EGTP data from CU 
87  *
88  * @details
89  *
90  *    Function : duHdlEgtpData
91  *
92  *    Functionality: 
93  *      Processes EGTP header and sends data to RLC
94  *
95  * @params[in]  Pointer to EGTP Message 
96  * @return ROK     - success
97  *         RFAILED - failure
98  *
99  * ****************************************************************/
100 uint8_t duHdlEgtpDlData(EgtpMsg  *egtpMsg)
101 {
102
103    /* TODO : Extract RbId/UeID/CellID/SduId from database
104       using tunnel id in egtp header */
105
106    DU_LOG("\nDU_APP : Processing DL data");
107 #ifdef EGTP_TEST
108    Pst pst;
109    KwuDatReqInfo datReqInfo;
110
111    datReqInfo.rlcId.rbId = RB_ID;
112    datReqInfo.rlcId.rbType = CM_LTE_DRB;
113    datReqInfo.rlcId.ueId = UE_ID;
114    datReqInfo.rlcId.cellId = NR_CELL_ID;
115
116    datReqInfo.sduId = ++sduId;
117    datReqInfo.lcType = CM_LTE_LCH_DTCH;
118
119    /* Filling pst and Sending to RLC DL */
120    pst.selector  = ODU_SELECTOR_LWLC;
121    pst.srcEnt    = ENTDUAPP;
122    pst.dstEnt    = ENTRLC;
123    pst.dstInst   = RLC_DL_INST;
124    pst.dstProcId = DU_PROC;
125    pst.srcProcId = DU_PROC;
126    pst.region    = duCb.init.region;
127
128    cmPkKwuDatReq(&pst, &datReqInfo, egtpMsg->msg);
129 #else
130    //duBuildAndSendDlRrcMsgToRlc();
131 #endif
132    return ROK;
133 }
134
135 /******************************************************************
136  *
137  * @brief Builds and Sends DL CCCH Ind to MAC
138  *
139  * @details
140  *
141  *    Function : duBuildAndSendDlCcchInd 
142  *
143  *    Functionality: Builds and sends DL CCCH Ind Msg to MAC
144  *
145  * @params[in] dlCcchMsg - uint8_t*
146  * @return ROK     - success
147  *         RFAILED - failure
148  *
149  * ****************************************************************/
150 uint8_t duBuildAndSendDlCcchInd(uint16_t *cellId, uint16_t *crnti, \
151       DlCcchMsgType msgType, uint16_t dlCcchMsgSize, uint8_t *dlCcchMsg)
152 {
153    uint8_t ret                  = ROK;
154    uint16_t idx2;
155    DlCcchIndInfo *dlCcchIndInfo = NULLP;
156    Pst pst;
157
158    DU_LOG("\nDU APP : Building and Sending DL CCCH Ind to MAC");
159
160    DU_ALLOC_SHRABL_BUF(dlCcchIndInfo, sizeof(DlCcchIndInfo));
161
162    if(!dlCcchIndInfo)
163    {
164       DU_LOG("\nDU APP : Memory alloc failed while building DL CCCH Ind");
165       return RFAILED;
166    }
167
168    dlCcchIndInfo->cellId = *cellId;
169    dlCcchIndInfo->crnti = *crnti;
170    dlCcchIndInfo->msgType = msgType;
171    dlCcchIndInfo->dlCcchMsgLen = dlCcchMsgSize;
172
173    DU_ALLOC_SHRABL_BUF(dlCcchIndInfo->dlCcchMsg, dlCcchIndInfo->dlCcchMsgLen);
174    if(!dlCcchIndInfo->dlCcchMsg)
175    {
176       DU_LOG("\nDU APP : Memory alloc failed while building DL CCCH Ind");
177       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, dlCcchIndInfo, sizeof(DlCcchIndInfo));
178       return RFAILED;
179    }
180    for(idx2 = 0; idx2 < dlCcchIndInfo->dlCcchMsgLen; idx2++)
181    {
182       dlCcchIndInfo->dlCcchMsg[idx2] = dlCcchMsg[idx2];
183    }
184    DU_FREE(dlCcchMsg, dlCcchMsgSize);
185
186    /* Fill Pst */
187    FILL_PST_DUAPP_TO_MAC(pst, EVENT_MAC_DL_CCCH_IND);
188    ret = (*packMacDlCcchIndOpts[pst.selector])(&pst, dlCcchIndInfo);
189    if(ret != ROK)
190    {
191       DU_LOG("\nDU_APP : Failure in sending DL CCCH to MAC");
192       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, dlCcchIndInfo->dlCcchMsg,\
193             dlCcchIndInfo->dlCcchMsgLen);
194       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, dlCcchIndInfo, \
195             sizeof(DlCcchIndInfo));
196       ret = RFAILED; 
197    }
198
199    return ret;
200
201 }
202
203 /*******************************************************************
204  *
205  * @brief Build and Send DL RRC Message transfer to RLC
206  *
207  * @details
208  *
209  *    Function : duBuildAndSendDlRrcMsgToRlc
210  *
211  *    Functionality:
212  *      Build and Send DL RRC Message transfer to RLC
213  *
214  * @params[in] Cell ID
215  *             UE Index
216  *             Logical Channgel ID
217  *             RRC Message
218  *             RRC Message Length
219  * @return ROK     - success
220  *         RFAILED - failure
221  *
222  * ****************************************************************/
223 uint8_t duBuildAndSendDlRrcMsgToRlc(uint16_t cellId, RlcUeCfg ueCfg, \
224    uint8_t lcId, bool execDup, bool deliveryStaReq, uint16_t rrcMsgLen, uint8_t *rrcMsg)
225 {
226    Pst  pst;
227    uint8_t ret;
228    uint8_t lcIdx;
229    RlcDlRrcMsgInfo  *dlRrcMsgInfo;
230
231    DU_ALLOC_SHRABL_BUF(dlRrcMsgInfo, sizeof(RlcDlRrcMsgInfo));
232    if(!dlRrcMsgInfo)
233    {
234       DU_LOG("\nDU APP : Memory allocation failed for dlRrcMsgInfo in \
235          duBuildAndSendDlRrcMsgToRlc");
236       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, rrcMsg, rrcMsgLen);
237       return RFAILED;
238    }
239
240    /* Filling up the RRC msg info */
241    dlRrcMsgInfo->cellId = cellId;
242    dlRrcMsgInfo->ueIdx = ueCfg.ueIdx;
243    for(lcIdx = 0; lcIdx <= MAX_NUM_LC; lcIdx++)
244    {
245       if(ueCfg.rlcLcCfg[lcIdx].lcId == lcId)
246       {
247          dlRrcMsgInfo->rbType = ueCfg.rlcLcCfg[lcIdx].rbType;
248          dlRrcMsgInfo->rbId = ueCfg.rlcLcCfg[lcIdx].rbId;
249          dlRrcMsgInfo->lcType = ueCfg.rlcLcCfg[lcIdx].lcType;
250          dlRrcMsgInfo->lcId = ueCfg.rlcLcCfg[lcIdx].lcId;
251          break;
252       }
253    }
254    dlRrcMsgInfo->execDup = execDup;
255    dlRrcMsgInfo->deliveryStaRpt = deliveryStaReq;
256    dlRrcMsgInfo->rrcMsg = rrcMsg;
257    dlRrcMsgInfo->msgLen = rrcMsgLen;
258
259    /* Filling post structure and sending msg */ 
260    FILL_PST_DUAPP_TO_RLC(pst, RLC_DL_INST, EVENT_DL_RRC_MSG_TRANS_TO_RLC);
261    ret = (*duSendDlRrcMsgToRlcOpts[pst.selector])(&pst, dlRrcMsgInfo);
262    if(ret != ROK)
263    {
264       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, rrcMsg, rrcMsgLen);
265       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, dlRrcMsgInfo, sizeof(RlcDlRrcMsgInfo));
266       return RFAILED;
267    }
268
269    return ROK;
270
271
272
273
274 /******************************************************************
275  *
276  * @brief Processes DL RRC Message Transfer  sent by CU
277  *
278  * @details
279  *
280  *    Function : procDlRrcMsgTrans 
281  *
282  *    Functionality: Processes DL RRC Message Transfer sent by CU
283  *
284  * @params[in] F1AP_PDU_t ASN decoded F1AP message
285  * @return ROK     - success
286  *         RFAILED - failure
287  *
288  * ****************************************************************/
289 uint8_t procDlRrcMsgTrans(F1AP_PDU_t *f1apMsg)
290 {
291    DLRRCMessageTransfer_t *dlRrcMsg = NULLP;
292    uint8_t                *rrcMsgPdu = NULLP;
293    uint8_t                ieIdx, ueIdx, cellIdx;
294    uint8_t                ret, srbId;
295    uint16_t               byteIdx, crnti, cellId, rrcMsgSize;
296    uint32_t               gnbCuUeF1apId, gnbDuUeF1apId;
297    bool                   execDup = false;
298    bool                   deliveryStaRpt = false;
299    bool                   ueFound = false;
300    bool                   ueCcchCtxtFound = false; 
301
302    DU_LOG("\nDU_APP : DL RRC message transfer Recevied");
303
304    dlRrcMsg = &f1apMsg->choice.initiatingMessage->value.choice.DLRRCMessageTransfer;
305
306    ret = ROK;
307
308    for(ieIdx=0; ieIdx<dlRrcMsg->protocolIEs.list.count; ieIdx++)
309    {
310       switch(dlRrcMsg->protocolIEs.list.array[ieIdx]->id)
311       {
312          case ProtocolIE_ID_id_gNB_CU_UE_F1AP_ID:
313             {
314                gnbCuUeF1apId = dlRrcMsg->protocolIEs.list.array[ieIdx]->value.choice.GNB_CU_UE_F1AP_ID;
315                break;
316             }
317          case ProtocolIE_ID_id_gNB_DU_UE_F1AP_ID:
318             {
319                gnbDuUeF1apId = dlRrcMsg->protocolIEs.list.array[ieIdx]->value.choice.GNB_DU_UE_F1AP_ID;
320                break;
321             }
322          case ProtocolIE_ID_id_SRBID:
323             {
324                srbId = dlRrcMsg->protocolIEs.list.array[ieIdx]->value.choice.SRBID;
325                break;
326             }
327          case ProtocolIE_ID_id_ExecuteDuplication:
328             {
329                execDup = true;
330                break;
331             }
332          case ProtocolIE_ID_id_RRCContainer:
333             {
334                if(dlRrcMsg->protocolIEs.list.array[ieIdx]->value.choice.RRCContainer.size > 0)
335                {
336                   rrcMsgSize = dlRrcMsg->protocolIEs.list.array[ieIdx]->value.choice.RRCContainer.size;
337                   DU_ALLOC(rrcMsgPdu, rrcMsgSize);
338                   if(!rrcMsgPdu)
339                   {
340                       DU_LOG("\nDU_APP : Memory allocation failed in procDlRrcMsgTrans"); 
341                       return RFAILED;
342                   }
343                   for(byteIdx = 0; byteIdx < rrcMsgSize; byteIdx++)
344                   {
345                      rrcMsgPdu[byteIdx] = \
346                         dlRrcMsg->protocolIEs.list.array[ieIdx]->value.choice.RRCContainer.buf[byteIdx];
347                   }
348                }
349                else
350                {
351                   DU_LOG("\nDU_APP : RRC Container Size is invalid:%ld",\
352                         dlRrcMsg->protocolIEs.list.array[ieIdx]->value.choice.RRCContainer.size);
353                   return RFAILED;
354                }
355                break;
356             }
357          case ProtocolIE_ID_id_RRCDeliveryStatusRequest:
358             {
359                deliveryStaRpt = true;
360                break;
361             }
362          default:
363             DU_LOG("\nDU_APP : Invalid IE received in DL RRC Msg Transfer:%ld",
364                   dlRrcMsg->protocolIEs.list.array[ieIdx]->id);
365       }
366    }
367
368    if(srbId == SRB1_LCID) //RRC connection setup
369    {
370       for(ueIdx=0; ueIdx < duCb.numUe; ueIdx++)
371       {
372          if(gnbDuUeF1apId == duCb.ueCcchCtxt[ueIdx].gnbDuUeF1apId)
373          {
374             ueCcchCtxtFound = true;
375             crnti  = duCb.ueCcchCtxt[ueIdx].crnti;
376             cellId = duCb.ueCcchCtxt[ueIdx].cellId;
377             break;
378          }
379       }
380    }
381    if(ueCcchCtxtFound)
382    {
383       ret = duBuildAndSendDlCcchInd(&cellId, &crnti, RRC_SETUP, rrcMsgSize, rrcMsgPdu);
384       if(ret == RFAILED)
385       {
386          DU_LOG("\nDU_APP: Falied to build DlCcch Ind at procDlRrcMsgTrans()");
387       }
388       else
389       {
390          if(duCb.actvCellLst[cellId-1]->numActvUes < MAX_NUM_UE)
391          {
392             ret = duCreateUeCb(&duCb.ueCcchCtxt[ueIdx], gnbCuUeF1apId);
393             if(ret == RFAILED)
394             {
395                DU_LOG("\nDU_APP: Failed to createUeCb for cellId [%d] at procDlRrcMsgTrans()", \
396                      duCb.ueCcchCtxt[ueIdx].cellId);
397                ret = RFAILED;
398             }
399          }
400          else
401          {
402             DU_LOG("\nDU_APP: Max Active UEs has reached at procDlRrcMsgTrans()");
403             ret = RFAILED;
404          }
405       }
406    }
407    else
408    {
409       for(cellIdx = 0; cellIdx < MAX_NUM_CELL; cellIdx++)
410       {
411          for(ueIdx = 0 ; ueIdx < MAX_NUM_UE; ueIdx++)
412          {
413             if((gnbCuUeF1apId == duCb.actvCellLst[cellIdx]->ueCb[ueIdx].gnbCuUeF1apId)
414                   && (gnbDuUeF1apId == duCb.actvCellLst[cellIdx]->ueCb[ueIdx].gnbDuUeF1apId))
415             {
416                ueFound = true;
417                ret = duBuildAndSendDlRrcMsgToRlc(duCb.actvCellLst[cellIdx]->cellId, \
418                      duCb.actvCellLst[cellIdx]->ueCb[ueIdx].rlcUeCfg, srbId, \
419                      execDup, deliveryStaRpt,  rrcMsgSize, rrcMsgPdu);
420                break; 
421             }
422          }
423          if(ueFound)
424             break;
425       }
426       if(!ueFound)
427          ret = RFAILED;
428    }
429    return ret;
430 }
431
432 /******************************************************************
433  *
434  * @brief Generates GNB DU Ue F1AP ID
435  *
436  * @details
437  *
438  *    Function : genGnbDuUeF1apId
439  *
440  *    Functionality: Generates GNB DU Ue F1AP ID
441  *
442  * @params[in] void
443  * @return gnbDuF1apId
444  *
445  * ****************************************************************/
446 uint32_t genGnbDuUeF1apId()
447 {
448    static uint32_t gnbDuUeF1apId = 0;
449
450    return ++gnbDuUeF1apId;
451 }
452
453 /******************************************************************
454  *
455  * @brief Processes UL CCCH Ind recvd from MAC
456  *
457  * @details
458  *
459  *    Function : duProcUlCcchInd
460  *
461  *    Functionality: Processes UL CCCH Ind recvd from MAC
462  *
463  * @params[in] UlCcchIndInfo *ulCcchIndInfo
464  * @return ROK     - success
465  *         RFAILED - failure
466  *
467  * ****************************************************************/
468 uint8_t duProcUlCcchInd(UlCcchIndInfo *ulCcchIndInfo)
469 {
470
471    uint8_t ret = ROK;
472    uint32_t gnbDuUeF1apId = 0;
473
474    gnbDuUeF1apId = genGnbDuUeF1apId();
475
476    /* Store Ue mapping */
477    duCb.ueCcchCtxt[duCb.numUe].gnbDuUeF1apId = gnbDuUeF1apId;
478    duCb.ueCcchCtxt[duCb.numUe].crnti         = ulCcchIndInfo->crnti;
479    duCb.ueCcchCtxt[duCb.numUe].cellId        = ulCcchIndInfo->cellId;
480
481    duCb.numUe++;
482
483    ret = (BuildAndSendInitialRrcMsgTransfer(gnbDuUeF1apId, ulCcchIndInfo->crnti, ulCcchIndInfo->ulCcchMsgLen,
484             ulCcchIndInfo->ulCcchMsg));
485    if(ret != ROK)
486    {
487       DU_LOG("\nDU_APP : BuildAndSendInitialRrcMsgTransfer failed");
488    }
489
490    DU_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, ulCcchIndInfo->ulCcchMsg, ulCcchIndInfo->ulCcchMsgLen);
491    DU_FREE_SHRABL_BUF(MAC_MEM_REGION, MAC_POOL, ulCcchIndInfo, sizeof(UlCcchIndInfo));
492
493    return ret;
494
495 }
496
497 /******************************************************************
498  *
499  * @brief Fills Default UL LC Cfg
500  *
501  * @details
502  *
503  *    Function : fillDefaultUlLcCfg
504  *
505  *    Functionality: Fills Default UL LC Cfg
506  *
507  * @params[in]  UlLcCfg *ulLcCfg 
508  * @return void
509  *****************************************************************/
510 void fillDefaultUlLcCfg(UlLcCfg *ulLcCfg)
511 {
512    ulLcCfg->priority = LC_PRIORITY_1;
513    ulLcCfg->lcGroup =  0;
514    ulLcCfg->schReqId = 0;
515    ulLcCfg->pbr = PBR_KBPS_INFINITY;
516    ulLcCfg->bsd = BSD_MS_1000;
517 }
518
519 /******************************************************************
520  *
521  * @brief Fills Initial DL Bandwidth Part
522  *
523  * @details
524  *
525  *    Function : fillDefaultInitDlBwp
526  *
527  *    Functionality: Fills Initial DL Bandwidth Part
528  *
529  * @params[in]  InitialDlBwp *initDlBwp
530  * @return void
531  *
532  *****************************************************************/
533 void fillDefaultInitDlBwp(InitialDlBwp *initDlBwp)
534 {
535    uint8_t idx = 0;
536    uint8_t freqDomainResource[FREQ_DOM_RSRC_SIZE] = {0};
537    uint8_t coreset0EndPrb, coreset1StartPrb, coreset1NumPrb;
538
539
540    if(initDlBwp)
541    {
542       /* Filling PDCCH Config */
543       initDlBwp->pdcchPresent = TRUE;
544       if(initDlBwp->pdcchPresent)
545       {
546          initDlBwp->pdcchCfg.numCRsetToAddMod = PDCCH_CTRL_RSRC_SET_ONE_ID;
547          memset(initDlBwp->pdcchCfg.cRSetToAddModList, 0, MAX_NUM_CRSET);
548          if(initDlBwp->pdcchCfg.numCRsetToAddMod <= MAX_NUM_CRSET)
549          {
550             initDlBwp->pdcchCfg.cRSetToAddModList[idx].cRSetId = \
551                PDCCH_CTRL_RSRC_SET_ONE_ID;
552             memset(initDlBwp->pdcchCfg.cRSetToAddModList[idx].freqDomainRsrc, 0,\
553                FREQ_DOM_RSRC_SIZE); 
554             coreset0EndPrb = CORESET0_END_PRB;
555             coreset1StartPrb = coreset0EndPrb +6;
556             coreset1NumPrb = CORESET1_NUM_PRB;
557             /* calculate the PRBs */
558             freqDomRscAllocType0(((coreset1StartPrb)/6), (coreset1NumPrb/6), freqDomainResource);
559             memcpy(initDlBwp->pdcchCfg.cRSetToAddModList[idx].freqDomainRsrc, freqDomainResource,
560                FREQ_DOM_RSRC_SIZE);
561
562             initDlBwp->pdcchCfg.cRSetToAddModList[idx].duration = \
563                PDCCH_CTRL_RSRC_SET_ONE_DURATION;
564             initDlBwp->pdcchCfg.cRSetToAddModList[idx].cceRegMappingType = \
565                CCE_REG_MAPPINGTYPE_PR_NONINTERLEAVED;
566             initDlBwp->pdcchCfg.cRSetToAddModList[idx].precoderGranularity = \
567                ALL_CONTIGUOUS_RBS;
568             initDlBwp->pdcchCfg.cRSetToAddModList[idx].dmrsScramblingId = \
569                SCRAMBLING_ID;
570          }
571          initDlBwp->pdcchCfg.numCRsetToRel = 0;
572          /* Filling Serach Space */
573          initDlBwp->pdcchCfg.numSearchSpcToAddMod = PDCCH_CTRL_RSRC_SET_ONE_ID;
574          memset(initDlBwp->pdcchCfg.searchSpcToAddModList, 0, MAX_NUM_CRSET);
575          if(initDlBwp->pdcchCfg.numSearchSpcToAddMod <= MAX_NUM_CRSET)
576          {
577             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].searchSpaceId =\
578                PDCCH_SRCH_SPC_TWO_ID;
579             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].cRSetId = \
580                PDCCH_CTRL_RSRC_SET_ONE_ID;
581             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].\
582                mSlotPeriodicityAndOffset = SLOTPERIODICITYANDOFFSET_PR_SL1;
583             memset(initDlBwp->pdcchCfg.searchSpcToAddModList[idx].mSymbolsWithinSlot, 0,\
584                MONITORING_SYMB_WITHIN_SLOT_SIZE);
585             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].mSymbolsWithinSlot[idx] =\
586                PDCCH_SYMBOL_WITHIN_SLOT;
587             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].numCandidatesAggLevel1 =\
588                AGGREGATIONLEVEL_N8; 
589             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].numCandidatesAggLevel2 =\
590                AGGREGATIONLEVEL_N8; 
591             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].numCandidatesAggLevel4 =\
592                AGGREGATIONLEVEL_N4; 
593             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].numCandidatesAggLevel8 =\
594                AGGREGATIONLEVEL_N2; 
595             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].numCandidatesAggLevel16 =\
596                AGGREGATIONLEVEL_N1;
597             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].searchSpaceType = \
598                SEARCHSPACETYPE_PR_UE_SPECIFIC;
599             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].ueSpecificDciFormat =\
600                PDCCH_SRCH_SPC_TWO_UE_SPEC_DCI_FORMAT;
601
602             initDlBwp->pdcchCfg.numSearchSpcToRel = 0;
603
604          }
605       }
606       /* Filling PDSCH Config */
607       initDlBwp->pdschPresent = TRUE;
608       if(initDlBwp->pdschPresent)
609       {
610          initDlBwp->pdschCfg.dmrsDlCfgForPdschMapTypeA.addPos = ADDITIONALPOSITION_POS0;
611          initDlBwp->pdschCfg.resourceAllocType = RESOURCEALLOCATION_TYPE1;
612          initDlBwp->pdschCfg.numTimeDomRsrcAlloc = 1;
613          initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].mappingType = \
614             MAPPING_TYPEA;
615          initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].startSymbol = PDSCH_START_SYMBOL; 
616          initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].symbolLength = PDSCH_LENGTH_SYMBOL;
617          initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].startSymbolAndLength = \
618             calcSliv(PDSCH_START_SYMBOL, PDSCH_LENGTH_SYMBOL);
619          initDlBwp->pdschCfg.rbgSize = RBG_SIZE_CONFIG1;
620          initDlBwp->pdschCfg.numCodeWordsSchByDci = CODEWORDS_SCHED_BY_DCI_N1;
621          initDlBwp->pdschCfg.bundlingType = TYPE_STATIC_BUNDLING;
622          initDlBwp->pdschCfg.bundlingInfo.StaticBundling.size = 0;
623       }
624    }
625
626 }
627
628 /******************************************************************
629  *
630  * @brief Fills Initial UL Bandwidth Part
631  *
632  * @details
633  *
634  *    Function : fillDefaultInitUlBwp
635  *
636  *    Functionality: Fills Initial UL Bandwidth Part
637  *
638  * @params[in]  InitialUlBwp *initUlBwp
639  * @return void
640  *
641  *****************************************************************/
642 void fillDefaultInitUlBwp(InitialUlBwp *initUlBwp)
643 {
644    uint8_t idx;
645    if(initUlBwp)
646    {
647       initUlBwp->pucchPresent = FALSE;
648
649       /*Filling PUSCH Config */
650       initUlBwp->puschPresent = TRUE;
651       if(initUlBwp->puschPresent)
652       {
653          initUlBwp->puschCfg.dataScramblingId = SCRAMBLING_ID;
654          initUlBwp->puschCfg.dmrsUlCfgForPuschMapTypeA.addPos = ADDITIONALPOSITION_POS0; 
655          initUlBwp->puschCfg.dmrsUlCfgForPuschMapTypeA.transPrecodDisabled. \
656             scramblingId0 = SCRAMBLING_ID; 
657          initUlBwp->puschCfg.resourceAllocType = RESOURCEALLOCATION_TYPE1;
658          initUlBwp->puschCfg.numTimeDomRsrcAlloc = 1;
659          idx = 0;
660          if(initUlBwp->puschCfg.numTimeDomRsrcAlloc <= MAX_NUM_UL_ALLOC)
661          {
662             initUlBwp->puschCfg.timeDomRsrcAllocList[idx].k2 = PUSCH_K2;
663             initUlBwp->puschCfg.timeDomRsrcAllocList[idx].mappingType =\
664                MAPPING_TYPEA;
665             initUlBwp->puschCfg.timeDomRsrcAllocList[idx].startSymbol = PUSCH_START_SYMBOL;
666             initUlBwp->puschCfg.timeDomRsrcAllocList[idx].symbolLength = PUSCH_LENGTH_SYMBOL;
667             initUlBwp->puschCfg.timeDomRsrcAllocList[idx].startSymbolAndLength =\
668                calcSliv(PUSCH_START_SYMBOL, PUSCH_LENGTH_SYMBOL);
669          }
670          initUlBwp->puschCfg.transformPrecoder = TRANSFORM_PRECODER_DISABLED;
671       }
672    }
673    else
674    {
675       DU_LOG("\n DUAPP: Memory is NULL of InitalUlBwp");
676    }
677
678 }
679 /******************************************************************
680  *
681  * @brief Fills SpCell Group Info
682  *
683  * @details
684  *
685  *    Function : fillDefaultSpCellGrpInfo
686  *
687  *    Functionality: Fills Sp Cell Group Info
688  *
689  * @params[in]  SpCellCfg *spCell
690  * @return void
691  *
692  *****************************************************************/
693 void fillDefaultSpCellGrpInfo(SpCellCfg *spCell)
694 {
695    if(spCell)
696    {
697       spCell->servCellIdx = SERV_CELL_IDX;
698       /* Filling Initial Dl Bwp */
699       fillDefaultInitDlBwp(&spCell->servCellCfg.initDlBwp);
700
701       spCell->servCellCfg.numDlBwpToAdd    = 0; 
702       spCell->servCellCfg.firstActvDlBwpId = ACTIVE_DL_BWP_ID;
703       spCell->servCellCfg.defaultDlBwpId   = ACTIVE_DL_BWP_ID;
704       spCell->servCellCfg.bwpInactivityTmr = NULLP;
705       spCell->servCellCfg.pdschServCellCfg.maxMimoLayers = NULLP;
706       spCell->servCellCfg.pdschServCellCfg.maxCodeBlkGrpPerTb = NULLP;
707       spCell->servCellCfg.pdschServCellCfg.codeBlkGrpFlushInd = NULLP;
708       spCell->servCellCfg.pdschServCellCfg.xOverhead = NULLP;
709       spCell->servCellCfg.pdschServCellCfg.numHarqProcForPdsch =\
710          NUM_HARQ_PROC_FOR_PDSCH_N_16;
711       /* Filling Initial UL Bwp*/
712       fillDefaultInitUlBwp(&spCell->servCellCfg.initUlBwp);
713       spCell->servCellCfg.numUlBwpToAdd     = 0; 
714       spCell->servCellCfg.firstActvUlBwpId  = ACTIVE_DL_BWP_ID; 
715    }
716    else
717    {
718       DU_LOG("\n DU_APP: Memory is NULL for SpCellGrp");
719    }
720 }
721
722 /******************************************************************
723  *
724  * @brief Fills Physical Cell Group Info
725  *
726  * @details
727  *
728  *    Function : fillDefaultPhyCellGrpInfo
729  *
730  *    Functionality: Fills Physical Cell Group Info
731  *
732  * @params[in]  PhyCellGrpCfg *cellGrp
733  * @return void
734  *
735  *****************************************************************/
736 void fillDefaultPhyCellGrpInfo(PhyCellGrpCfg *cellGrp)
737 {
738    if(cellGrp)
739    {
740       cellGrp->pdschHarqAckCodebook = PDSCH_HARQ_ACK_CODEBOOK_DYNAMIC;
741       cellGrp->pNrFr1 = P_NR_FR1;
742    }
743    else
744    {
745       DU_LOG("\nDUAPP: Memory is NULL for Physical Cell Group");
746    }
747 }
748
749 /******************************************************************
750  *
751  * @brief Fills Mac Cell Group Info
752  *
753  * @details
754  *
755  *    Function : fillDefaultMacCellGrpInfo
756  *
757  *    Functionality: Fills Mac Cell Group Info
758  *
759  * @params[in]  MacCellGrpCfg *cellGrp
760  * @return void
761  *
762  *****************************************************************/
763 void fillDefaultMacCellGrpInfo(MacCellGrpCfg *cellGrp)
764 {
765    uint8_t idx;
766
767    if(cellGrp)
768    {
769       /* Filling Scheduling Request Config */
770       cellGrp->schReqCfg.addModListCount = 1;
771       if(cellGrp->schReqCfg.addModListCount <= MAX_NUM_SR_CFG_PER_CELL_GRP)
772       {
773          for(idx = 0; idx < cellGrp->schReqCfg.addModListCount; idx++)
774          {
775             cellGrp->schReqCfg.addModList[idx].schedReqId    = SCH_REQ_ID;
776             cellGrp->schReqCfg.addModList[idx].srProhibitTmr = SR_PROHIBIT_MS_32;
777             cellGrp->schReqCfg.addModList[idx].srTransMax    = SR_TRANS_MAX_N_16;
778          }
779       }
780       cellGrp->schReqCfg.relListCount = 0;
781
782       /* Filling Tag config */
783       cellGrp->tagCfg.addModListCount = 1;
784       if(cellGrp->tagCfg.addModListCount <= MAC_NUM_TAGS)
785       {
786          for(idx = 0; idx < cellGrp->tagCfg.addModListCount; idx++)
787          {
788             cellGrp->tagCfg.addModList[idx].tagId = TAG_ID;
789             cellGrp->tagCfg.addModList[idx].timeAlignTimer = TIME_ALIGNMENT_TIMER_INFINITY;
790          }
791       }
792       cellGrp->tagCfg.relListCount = 0;
793
794       /* Filling BSR config */
795       cellGrp->bsrTmrCfg.periodicTimer = BSR_PERIODIC_TIMER_SF_10;
796       cellGrp->bsrTmrCfg.retxTimer = BSR_RETX_TIMER_SF_320;
797       cellGrp->bsrTmrCfg.srDelayTimer = BSR_SR_DELAY_TMR_2560;
798
799       /* Filling PHR config */
800       cellGrp->phrCfgSetupPres = true;
801       cellGrp->phrCfg.periodicTimer = PHR_PERIODIC_TIMER_INFINITY;
802       cellGrp->phrCfg.prohibitTimer = PHR_PROHIBIT_TIMER_SF_0;
803       cellGrp->phrCfg.txPowerFactor = PHR_TX_PWR_FACTOR_CHANGE_INFINITY;
804       cellGrp->phrCfg.multiplePHR   = false;
805       cellGrp->phrCfg.dummy         = false;
806       cellGrp->phrCfg.phrType2OtherCell = false;
807       cellGrp->phrCfg.phrOtherCG = PHR_MODE_OTHER_CG_REAL;  
808
809    }
810    else
811    {
812       DU_LOG("\nDUAPP: Memory is NULL for Master Cell Group");
813    }
814 }
815
816 /******************************************************************
817  *
818  * @brief Function to fill Mac Lc Cfg for SRB1
819  *
820  * @details
821  *
822  *    Function : fillMacSrb1LcCfg
823  *
824  *    Functionality: Function to fill Mac Lc cfg for SRB1
825  *
826  * @params[in]  LcCfg *lcCfg, LcCfg *ueSetReqDb
827  * @return void
828  *****************************************************************/
829
830 void fillMacSrb1LcCfg(LcCfg *macLcCfg)
831 {
832    macLcCfg->lcId   = SRB1_LCID;
833    macLcCfg->configType = CONFIG_ADD;
834    macLcCfg->drbQos = NULLP;
835    macLcCfg->snssai = NULLP;
836    macLcCfg->ulLcCfgPres = true;
837    fillDefaultUlLcCfg(&macLcCfg->ulLcCfg);
838 }
839
840 /******************************************************************
841  *
842  * @brief Function to fill the Lc cfg from ueSetupReqDb
843  *
844  * @details
845  *
846  *    Function : fillMacLcCfgToAddMod
847  *
848  *    Functionality: Function to fill the Lc cfg from ueSetupReqDb
849  *
850  * @params[in]  LcCfg *lcCfg, LcCfg *ueSetReqDb
851  * @return ROK/RFAILED
852  *
853  *****************************************************************/
854
855 uint8_t fillMacLcCfgToAddMod(LcCfg *lcCfg, LcCfg *ueSetReqDb)
856 {
857    uint8_t ret = ROK; 
858    lcCfg->lcId = ueSetReqDb->lcId;
859    lcCfg->configType = ueSetReqDb->configType;
860    /* Filling DRBQOS */
861    if(ueSetReqDb->drbQos)
862    {
863       if(!lcCfg->drbQos)
864       {
865          DU_ALLOC_SHRABL_BUF(lcCfg->drbQos, sizeof(DrbQosInfo));
866          if(!lcCfg->drbQos)
867          {
868             DU_LOG("\nDU APP: Memory Alloc failed at drQos at fillMacLcCfgToAddMod()");
869             return RFAILED;
870          }
871       }
872       if(ret == ROK)
873       {
874          memcpy(lcCfg->drbQos, ueSetReqDb->drbQos, sizeof(DrbQosInfo));
875       }
876    }
877    else
878    {
879       lcCfg->drbQos = NULLP;
880    }
881
882    if(ret == ROK)
883    {
884       if(ueSetReqDb->snssai)
885       {
886          if(!lcCfg->snssai)
887          {
888             DU_ALLOC_SHRABL_BUF(lcCfg->snssai, sizeof(Snssai));
889             if(!lcCfg->snssai)
890             {
891                DU_LOG("\nDU APP: Memory Alloc failed at snnsai at fillMacLcCfgToAddMod()");
892                ret = RFAILED;
893             }
894          }
895          if(ret == ROK)
896          {
897             /* Filling SNSSAI */
898             memcpy(lcCfg->snssai, ueSetReqDb->snssai, sizeof(Snssai));
899          }
900          else
901          {
902             lcCfg->snssai = NULLP;
903             if(lcCfg->drbQos)
904             {
905                DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, lcCfg->drbQos, sizeof(DrbQosInfo));
906                lcCfg->drbQos = NULLP;
907             }
908             return ret;
909          }
910       }
911       else
912          lcCfg->snssai = NULLP;
913    }
914    lcCfg->ulLcCfgPres = ueSetReqDb->ulLcCfgPres;
915    memcpy(&lcCfg->ulLcCfg, &ueSetReqDb->ulLcCfg, sizeof(UlLcCfg));
916    memcpy(&lcCfg->dlLcCfg, &ueSetReqDb->dlLcCfg, sizeof(DlLcCfg));
917    return ret;
918 }
919
920 /******************************************************************
921  *
922  * @brief Function to copy the Bit rate from ueSetupReqDb
923  *
924  * @details
925  *
926  *    Function : getMaxAggrBitRate
927  *
928  *    Functionality: Function to copy bit Rate from ueSetupReqDb
929  *
930  * @params[in]  MaxAggrBitRate *macBitRate, MaxAggrBitRate *ueDbBitRate
931  * @return ROK/RFAILED
932  *
933  *****************************************************************/
934
935 uint8_t getMaxAggrBitRate(MaxAggrBitRate *macBitRate, MaxAggrBitRate *ueDbBitRate)
936 {
937    if(ueDbBitRate)
938    {
939       if(!macBitRate)
940       {
941          DU_ALLOC_SHRABL_BUF(macBitRate, sizeof(MaxAggrBitRate));
942          if(!macBitRate)
943          {
944             DU_LOG("\nDUAPP: Memory Alloc Failed at getMaxAggrBitRate()");
945             return RFAILED;
946          }
947       }
948       memcpy(macBitRate, ueDbBitRate, sizeof(MaxAggrBitRate));
949    }
950    else
951    {
952       macBitRate = NULLP;
953    }
954    return ROK;
955 }
956
957 /******************************************************************
958  *
959  * @brief Builds and Send UE ReConfig Request to MAC
960  *
961  * @details
962  *
963  *    Function : sendUeReCfgReqToMac
964  *
965  *    Functionality: Builds and Send UE ReConfig Request to MAC
966  *
967  * @Params[in]  MacUeCfg pointer
968  * @return ROK     - success
969  *         RFAILED - failure
970  *
971  * ****************************************************************/
972
973 uint8_t sendUeReCfgReqToMac(MacUeCfg *macUeCfg)
974 {
975    uint8_t ret = ROK;
976    Pst pst;
977    
978    /* Fill Pst */
979    FILL_PST_DUAPP_TO_MAC(pst, EVENT_MAC_UE_RECONFIG_REQ);
980    
981    if(macUeCfg)
982    {
983       /* Processing one Ue at a time to MAC */
984       DU_LOG("\nDU_APP: Sending Reconfig Request to MAC");
985       ret = (*packMacUeReconfigReqOpts[pst.selector])(&pst, macUeCfg);
986       if(ret == RFAILED)
987       {
988          DU_LOG("\nDU_APP: Failed to send Reconfig Request to MAC at sendUeReCfgReqToMac()");
989          DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, macUeCfg, sizeof(MacUeCfg));
990       }
991    }
992    else
993    {
994       DU_LOG("\n DU_APP: Received macUeCfg is NULLP at sendUeReCfgReqToMac()");
995       ret = RFAILED;
996    }
997    return ret;
998 }
999
1000 /******************************************************************
1001  *
1002  * @brief Function to return Drb LcId
1003  *
1004  * @details
1005  *
1006  *    Function : getDrbLcId
1007  *
1008  *    Functionality: Function to return Drb LcId
1009  *
1010  * Returns: lcId - SUCCESS
1011  *         RFAILED - FAILURE
1012  *****************************************************************/
1013
1014 uint8_t getDrbLcId(uint32_t *drbBitMap)
1015 {
1016    uint8_t bitMask = 1, bitPos = 0;
1017    bitPos = MIN_DRB_LCID;
1018
1019    while(bitPos <= MAX_DRB_LCID)
1020    {
1021       if((*drbBitMap & (bitMask << bitPos)) == 0)
1022       {
1023          *drbBitMap = ((bitMask << bitPos)| *drbBitMap);
1024          return bitPos;
1025       }
1026       else
1027       {
1028          bitPos++;
1029       }
1030    }
1031    DU_LOG("\nDU_APP: Max LC Reached in getDrbLcId()");
1032    return RFAILED;
1033 }
1034
1035 /******************************************************************
1036  *
1037  * @brief Fills MacUeCfg structure
1038  *
1039  * @details
1040  *
1041  *    Function : fillMacUeCfg
1042  *
1043  *    Functionality: Fills MacUeCfg
1044  *
1045  * @params[in]  cellId, ueIdx, crnti, 
1046  *              UeContextSetupDb pointer,
1047  *              MacUeCfg pointer
1048  * @return ROK/RFAILED
1049  *
1050  *****************************************************************/
1051 uint8_t fillMacUeCfg(uint16_t cellId, uint8_t ueIdx, uint16_t crnti, \
1052    UeContextSetupDb *ueCfgDb, MacUeCfg *macUeCfg)
1053 {
1054    uint8_t ret, dbIdx, lcIdx, cellIdx;
1055    bool lcIdFound = false;
1056    MacUeCfg *duMacDb = NULLP;
1057
1058    ret =ROK;
1059
1060    if(!ueCfgDb)
1061    {
1062       macUeCfg->cellId       = cellId;
1063       macUeCfg->ueIdx        = ueIdx;
1064       macUeCfg->crnti        = crnti;
1065       fillDefaultMacCellGrpInfo(&macUeCfg->macCellGrpCfg);
1066       fillDefaultPhyCellGrpInfo(&macUeCfg->phyCellGrpCfg);
1067       fillDefaultSpCellGrpInfo(&macUeCfg->spCellCfg);
1068       macUeCfg->maxAggrBitRate = NULLP;
1069       fillMacSrb1LcCfg(&macUeCfg->lcCfgList[0]);
1070       macUeCfg->numLcs++;
1071    }
1072    else
1073    {
1074       /* Fetching MacDb from DuUeCb */
1075       GET_CELL_IDX(cellId, cellIdx);
1076       duMacDb = &duCb.actvCellLst[cellIdx]->ueCb[ueIdx-1].macUeCfg;
1077       /* Fetching MaUeCfg List for ADD/MOD/DEL */
1078       macUeCfg->cellId       = cellId;
1079       macUeCfg->ueIdx        = ueIdx;
1080       macUeCfg->crnti        = crnti;
1081       ret = procUeReCfgCellInfo(macUeCfg, ueCfgDb->cellGrpCfg);
1082       if(ret == ROK)
1083       {
1084          ret = getMaxAggrBitRate(macUeCfg->maxAggrBitRate, ueCfgDb->duUeCfg.maxAggrBitRate);
1085       }
1086
1087       /* Filling LC Context */
1088       for(dbIdx = 0; (dbIdx < ueCfgDb->duUeCfg.numMacLcs && ret == ROK); dbIdx++)
1089       {
1090          if(!ueCfgDb->duUeCfg.macLcCfg[dbIdx].ulLcCfgPres)
1091          {
1092             /* Filling default UL LC config in MAC if not present */
1093             ueCfgDb->duUeCfg.macLcCfg[dbIdx].ulLcCfgPres = true;
1094             fillDefaultUlLcCfg(&ueCfgDb->duUeCfg.macLcCfg[dbIdx].ulLcCfg);
1095          }
1096          for(lcIdx = 0; lcIdx < duMacDb->numLcs; lcIdx++)
1097          {
1098             if(ueCfgDb->duUeCfg.macLcCfg[dbIdx].lcId == duMacDb->lcCfgList[lcIdx].lcId)
1099             {
1100                lcIdFound = true;
1101                if((ueCfgDb->duUeCfg.macLcCfg[dbIdx].configType == CONFIG_UNKNOWN) ||
1102                      (ueCfgDb->duUeCfg.macLcCfg[dbIdx].configType == CONFIG_MOD))
1103                {
1104                   ueCfgDb->duUeCfg.macLcCfg[dbIdx].configType = CONFIG_MOD;
1105                   ret = fillMacLcCfgToAddMod(&macUeCfg->lcCfgList[dbIdx], &ueCfgDb->duUeCfg.macLcCfg[dbIdx]);
1106                }
1107             }
1108             else
1109                lcIdFound = false;
1110          }
1111          if(!lcIdFound)
1112          {
1113             /* ADD/DEL CONFIG */
1114             ret = fillMacLcCfgToAddMod(&macUeCfg->lcCfgList[dbIdx], &ueCfgDb->duUeCfg.macLcCfg[dbIdx]);
1115          }
1116          if(ret == ROK)
1117          {
1118             macUeCfg->numLcs++;
1119          }
1120          else
1121          {
1122             DU_LOG("\n DU_APP: Failed to add Lc at Idx %d in fillMacUeCfg()", dbIdx); 
1123             break;
1124          }
1125       }/*End of Outer FOR loop */
1126    }
1127    return ret;
1128 }
1129
1130 /******************************************************************
1131  *
1132  * @brief Fills Rlc AM Information
1133  *
1134  * @details
1135  *
1136  *    Function : fillDefaultAmInfo
1137  *
1138  *    Functionality: Fills Rlc AM Information
1139  *
1140  * @params[in]  AmBearerCfg *amCfg
1141  * @return void
1142  *
1143  *****************************************************************/
1144 void fillDefaultAmInfo(AmBearerCfg *amCfg)
1145 {
1146    /* DL AM */
1147    amCfg->dlAmCfg.snLenDl     = AM_SIZE_12;
1148    amCfg->dlAmCfg.pollRetxTmr = POLL_RETX_TMR_45MS;
1149    amCfg->dlAmCfg.pollPdu     = POLL_PDU_TMR_INFINITY;
1150    amCfg->dlAmCfg.pollByte    = POLL_BYTES_INFINITY;
1151    amCfg->dlAmCfg.maxRetxTh   = RETX_TH_8;   
1152  
1153    /* UL AM */
1154    amCfg->ulAmCfg.snLenUl     = AM_SIZE_12;
1155    amCfg->ulAmCfg.reAssemTmr  = RE_ASM_40MS; 
1156    amCfg->ulAmCfg.statProhTmr = PROH_35MS;
1157 }
1158
1159 /******************************************************************
1160  *
1161  * @brief Fills RLC UM Bi Directional Information
1162  *
1163  * @details
1164  *
1165  *    Function : fillDefaultUmBiInfo
1166  *
1167  *    Functionality: Fills RLC UM Bi Directional Information
1168  *
1169  * @params[in]  UmBiDirBearerCfg *umBiDirCfg
1170  * @return void
1171  *
1172  *****************************************************************/
1173 void fillDefaultUmBiInfo(UmBiDirBearerCfg *umBiDirCfg)
1174 {
1175    /* UL UM BI DIR INFO */
1176    umBiDirCfg->ulUmCfg.snLenUlUm = UM_SIZE_12;
1177    umBiDirCfg->ulUmCfg.reAssemTmr = RE_ASM_40MS;
1178
1179    /* DL UM BI DIR INFO */
1180    umBiDirCfg->dlUmCfg.snLenDlUm  = UM_SIZE_12;
1181 }
1182
1183 /******************************************************************
1184  *
1185  * @brief Fills RLC UM Uni Directional UL Information
1186  *
1187  * @details
1188  *
1189  *    Function : fillDefaultUmUlInfo
1190  *
1191  *    Functionality: Fills RLC UM Uni Directional Info
1192  *
1193  * @params[in]  UmUniDirUlBearerCfg *UmUlCfg
1194  * @return void
1195  *
1196  *****************************************************************/
1197 void fillDefaultUmUlInfo(UmUniDirUlBearerCfg *UmUlCfg)
1198 {
1199    UmUlCfg->ulUmCfg.snLenUlUm = UM_SIZE_12;
1200    UmUlCfg->ulUmCfg.reAssemTmr = RE_ASM_40MS;
1201 }
1202
1203 /******************************************************************
1204  *
1205  * @brief Fills RLC UM Uni Directional DL Information
1206  *
1207  * @details
1208  *
1209  *    Function : fillDefaultUmDlInfo
1210  *
1211  *    Functionality: Fills RLC UM Uni Directional DL Info
1212  *
1213  * @params[in]  UmUniDirDlBearerCfg *UmDlCfg
1214  * @return void
1215  *
1216  *****************************************************************/
1217 void fillDefaultUmDlInfo(UmUniDirDlBearerCfg *UmDlCfg)
1218 {
1219    UmDlCfg->dlUmCfg.snLenDlUm  = UM_SIZE_12;
1220 }
1221
1222 /******************************************************************
1223  *
1224  * @brief Builds Rlc Mode Default Configuration
1225  *
1226  * @details
1227  *
1228  *    Function : fillDefaultRlcModeCfg
1229  *
1230  *    Functionality: Builds Rlc Mode Default Configuration
1231  *
1232  * @params[in]  rlcMode, RlcBearerCfg *lcCfg
1233  * @return ROK/RFAILED
1234  *
1235  *****************************************************************/
1236
1237 uint8_t fillDefaultRlcModeCfg(uint8_t rlcMode, RlcBearerCfg *lcCfg)
1238 {
1239
1240    if(lcCfg)
1241    {
1242       switch(rlcMode)
1243       {
1244          case RLC_AM :
1245             {
1246                if(!lcCfg->u.amCfg)
1247                {
1248                  DU_ALLOC_SHRABL_BUF(lcCfg->u.amCfg, sizeof(AmBearerCfg));
1249                  if(lcCfg->u.amCfg)
1250                     fillDefaultAmInfo(lcCfg->u.amCfg);
1251                  else
1252                  {
1253                     DU_LOG("\n DU_APP: Memory Alloc failed at AmCfg at fillDefaultRlcModeCfg()");
1254                     return RFAILED;
1255                  }
1256                }
1257                break;
1258             }
1259          case RLC_UM_BI_DIRECTIONAL :
1260             {
1261                if(!lcCfg->u.umBiDirCfg)
1262                {
1263                  DU_ALLOC_SHRABL_BUF(lcCfg->u.umBiDirCfg, sizeof(UmBiDirBearerCfg));
1264                  if(lcCfg->u.umBiDirCfg)
1265                     fillDefaultUmBiInfo(lcCfg->u.umBiDirCfg);
1266                  else
1267                  {
1268                     DU_LOG("\n DU_APP: Memory Alloc failed at UmBiDirCfg at fillDefaultRlcModeCfg()");
1269                     return RFAILED;
1270                  }
1271                }
1272                break;
1273             }
1274          case RLC_UM_UNI_DIRECTIONAL_UL :
1275             {
1276                if(!lcCfg->u.umUniDirUlCfg)
1277                {
1278                   DU_ALLOC_SHRABL_BUF(lcCfg->u.umUniDirUlCfg, sizeof(UmUniDirUlBearerCfg));
1279                   if(lcCfg->u.umUniDirUlCfg)
1280                     fillDefaultUmUlInfo(lcCfg->u.umUniDirUlCfg);
1281                   else
1282                   {
1283                      DU_LOG("\n DU_APP: Memory Alloc failed at UmUniDirUlCfg at fillDefaultRlcModeCfg()");
1284                      return RFAILED;
1285                   }
1286                }
1287                break;
1288             }
1289          case RLC_UM_UNI_DIRECTIONAL_DL :
1290             {
1291                if(!lcCfg->u.umUniDirDlCfg)
1292                {
1293                   DU_ALLOC_SHRABL_BUF(lcCfg->u.umUniDirDlCfg, sizeof(UmUniDirDlBearerCfg));
1294                   if(lcCfg->u.umUniDirDlCfg)
1295                    fillDefaultUmDlInfo(lcCfg->u.umUniDirDlCfg);
1296                   else
1297                   {
1298                      DU_LOG("\n DU_APP: Memory Alloc failed at UmUniDirDlCfg at fillDefaultRlcModeCfg()");
1299                      return RFAILED;
1300                   }
1301                }
1302                break;
1303             }
1304          default:
1305             DU_LOG("\nDUAPP: Invalid rlcMode %d at extractRlcCfgToAddMod()", rlcMode);
1306             return RFAILED;
1307       }
1308
1309    }
1310    else
1311    {
1312       DU_LOG("\nDUAPP: Received Lc Config is NULL");
1313       return RFAILED;
1314    }
1315    return ROK;
1316 }
1317
1318 /*******************************************************************
1319  *
1320  * @brief Function to fill Rlc Lc Cfg for SRB1
1321  *
1322  * @details
1323  *
1324  *    Function : fillRlcSrb1LcCfg
1325  *
1326  *    Functionality: 
1327  *     Function to fill Rlc Lc Cfg for SRB1
1328  * 
1329  *  @params[in]     Pointer to RlcBearerCfg
1330  *  @return ROK/RFAILED
1331  * 
1332  *****************************************************************/
1333
1334 uint8_t fillRlcSrb1LcCfg(RlcBearerCfg *rlcLcCfg)
1335 {
1336    uint8_t ret = ROK;
1337
1338    rlcLcCfg->rbId   = SRB1_LCID;
1339    rlcLcCfg->rbType = RB_TYPE_SRB;
1340    rlcLcCfg->lcId   = SRB1_LCID;
1341    rlcLcCfg->lcType = LCH_DCCH;
1342    rlcLcCfg->rlcMode = RLC_AM;
1343    rlcLcCfg->configType = CONFIG_ADD;
1344    ret = fillDefaultRlcModeCfg(rlcLcCfg->rlcMode, rlcLcCfg);
1345    return ret;
1346 }
1347
1348 /*******************************************************************
1349  *
1350  * @brief Processes UE ReConfig Req to RLC UL
1351  *
1352  * @details
1353  *
1354  *    Function : sendUeReCfgReqToRlc
1355  *
1356  *    Functionality: 
1357  *     Processes UE Reconfig Req to RLC UL
1358  * 
1359  *  @params[in]     Pointer to RlcUeCfg
1360  *  @return ROK     - success
1361  *          RFAILED - failure
1362  * 
1363  *****************************************************************/
1364
1365 uint8_t sendUeReCfgReqToRlc(RlcUeCfg *rlcUeCfg)
1366 {
1367    uint8_t ret;
1368    Pst pst;
1369    
1370    FILL_PST_DUAPP_TO_RLC(pst, RLC_UL_INST, EVENT_RLC_UE_RECONFIG_REQ);
1371    if(rlcUeCfg)
1372    {
1373       /* Processing one Ue at a time to RLC */
1374       DU_LOG("\nDU_APP: Sending Reconfig Request to RLC UL");
1375       ret = (*packRlcUeReconfigReqOpts[pst.selector])(&pst, rlcUeCfg);
1376       if(ret == RFAILED)
1377       {
1378          DU_LOG("\nDU_APP : Failed to send Ue Reconfig Req to RLC at sendUeReCfgReqToRlc()");
1379          DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, rlcUeCfg, sizeof(RlcUeCfg));
1380       }
1381    }
1382    else
1383    {
1384       DU_LOG("\n DU_APP: Received RlcUeCfg is NULL at sendUeReCfgReqToRlc()");
1385       ret = RFAILED;
1386    }
1387    return ret;
1388 }
1389
1390 /******************************************************************
1391  *
1392  * @brief Fills RlcBearerCfg structure
1393  *
1394  * @details
1395  *
1396  *    Function : fillRlcUeCfg
1397  *
1398  *    Functionality: Fills Rlc Bearer Cfg
1399  *
1400  *
1401  *****************************************************************/
1402 uint8_t fillRlcUeCfg(uint16_t cellId, uint8_t ueIdx,\
1403    DuUeCfg *ueCfgDb, RlcUeCfg *rlcUeCfg)
1404 {
1405    uint8_t ret, dbIdx, lcIdx, cellIdx;
1406    bool lcIdFound = false;
1407    RlcUeCfg *duRlcDb = NULLP;
1408
1409    ret = ROK;
1410    if(!ueCfgDb)
1411    {
1412       /* Initial RB being Added */ 
1413       rlcUeCfg->cellId       = cellId;
1414       rlcUeCfg->ueIdx        = ueIdx;
1415       ret = fillRlcSrb1LcCfg(&rlcUeCfg->rlcLcCfg[0]);
1416       if(ret == ROK)
1417          rlcUeCfg->numLcs++;
1418       else
1419          memset(rlcUeCfg, 0, sizeof(RlcUeCfg));
1420    }
1421    else
1422    {
1423       /* Fetch RlcDb from DuUeCb */ 
1424       GET_CELL_IDX(cellId, cellIdx);
1425       duRlcDb = &duCb.actvCellLst[cellIdx]->ueCb[ueIdx-1].rlcUeCfg;
1426       /*Filling RlcUeCfg */
1427       rlcUeCfg->cellId       = cellId;
1428       rlcUeCfg->ueIdx        = ueIdx;
1429       for(dbIdx = 0; (dbIdx < ueCfgDb->numRlcLcs && ret == ROK); dbIdx++)
1430       {
1431          ret = fillDefaultRlcModeCfg(ueCfgDb->rlcLcCfg[dbIdx].rlcMode, &ueCfgDb->rlcLcCfg[dbIdx]);
1432          if(ret == RFAILED)
1433          {
1434             DU_LOG("\n DU_APP: Failed to fill Rlc Mode at fillRlcUeCfg()");
1435             memset(rlcUeCfg, 0, sizeof(RlcUeCfg));
1436             return ret;
1437          }
1438          /* Fill Rlc Ue Cfg List for ADD/MOD/DEL */
1439          for(lcIdx = 0; lcIdx < duRlcDb->numLcs; lcIdx++)
1440          { 
1441             if(ueCfgDb->rlcLcCfg[dbIdx].lcId == duRlcDb->rlcLcCfg[lcIdx].lcId)
1442             {
1443                lcIdFound = true;
1444                if((ueCfgDb->rlcLcCfg[dbIdx].configType == CONFIG_UNKNOWN)||
1445                      (ueCfgDb->rlcLcCfg[dbIdx].configType == CONFIG_MOD))
1446                {
1447                   /* MOD */ 
1448                   ueCfgDb->rlcLcCfg[dbIdx].configType = CONFIG_MOD; /* update Db for MOD type */
1449                   memcpy(&rlcUeCfg->rlcLcCfg[dbIdx], &ueCfgDb->rlcLcCfg[dbIdx], sizeof(RlcBearerCfg));
1450                }
1451             }
1452             else
1453                lcIdFound = false;
1454          }
1455          if(!lcIdFound)
1456          {
1457             /* ADD/ DEL Config Type */
1458             memcpy(&rlcUeCfg->rlcLcCfg[dbIdx], &ueCfgDb->rlcLcCfg[dbIdx], sizeof(RlcBearerCfg));
1459          }
1460          rlcUeCfg->numLcs++;
1461       }
1462    }
1463    return ret;
1464 }
1465
1466 /******************************************************************
1467  *
1468  * @brief creates UE context
1469  *
1470  * @details
1471  *
1472  *    Function : duCreateUeCb
1473  *
1474  *    Functionality: Creates UE Conetxt
1475  *
1476  * @params[in] UeCcchCtxt Pointer
1477  *             UeIdx Pointer
1478  *
1479  * @return ROK     - success
1480  *         RFAILED - failure
1481  * ****************************************************************/
1482 uint8_t duCreateUeCb(UeCcchCtxt *ueCcchCtxt, uint32_t gnbCuUeF1apId)
1483 {
1484    uint8_t cellIdx = 0;
1485    uint8_t ret     = ROK;
1486    uint8_t ueIdx;
1487
1488    for(cellIdx = 0; cellIdx < MAX_NUM_CELL; cellIdx++)
1489    {
1490       if(ueCcchCtxt->cellId == duCb.actvCellLst[cellIdx]->cellId)
1491       {
1492          GET_UE_IDX(ueCcchCtxt->crnti, ueIdx);
1493          DU_LOG("\nDU_APP: Filling UeCb for ueIdx [%d]", ueIdx);
1494
1495          duCb.actvCellLst[cellIdx]->ueCb[ueIdx-1].crnti = ueCcchCtxt->crnti;
1496          duCb.actvCellLst[cellIdx]->ueCb[ueIdx-1].gnbDuUeF1apId = ueCcchCtxt->gnbDuUeF1apId;
1497          duCb.actvCellLst[cellIdx]->ueCb[ueIdx-1].gnbCuUeF1apId = gnbCuUeF1apId;
1498          duCb.actvCellLst[cellIdx]->ueCb[ueIdx-1].drbBitMap     = NULLP;
1499          duCb.actvCellLst[cellIdx]->ueCb[ueIdx-1].ueState       = UE_ACTIVE;
1500          duCb.actvCellLst[cellIdx]->ueCb[ueIdx-1].f1UeDb        = NULLP;
1501
1502          /* Filling Mac Ue Config */ 
1503          memset(&duCb.actvCellLst[cellIdx]->ueCb[ueIdx-1].macUeCfg, 0, sizeof(MacUeCfg));
1504          ret = duBuildAndSendUeCreateReqToMac(ueCcchCtxt->cellId, ueIdx, ueCcchCtxt->crnti,\
1505                 &duCb.actvCellLst[cellIdx]->ueCb[ueIdx-1].macUeCfg);
1506          if(ret == RFAILED)
1507             DU_LOG("\nDU_APP: Failed to send UE create request to MAC");
1508          
1509          /* Filling Rlc Ue Config */
1510          memset(&duCb.actvCellLst[cellIdx]->ueCb[ueIdx-1].rlcUeCfg, 0, sizeof(RlcUeCfg));
1511          ret = duBuildAndSendUeCreateReqToRlc(ueCcchCtxt->cellId, ueIdx, \
1512                 &duCb.actvCellLst[cellIdx]->ueCb[ueIdx-1].rlcUeCfg);
1513          if(ret == RFAILED)
1514             DU_LOG("\nDU_APP: Failed to send UE create request to RLC");
1515
1516          duCb.actvCellLst[cellIdx]->numActvUes++;
1517          memset(ueCcchCtxt, 0, sizeof(UeCcchCtxt));
1518       }
1519    }
1520    return ret;
1521 }
1522
1523 /******************************************************************
1524  *
1525  * @brief Builds and Send UE Create Request to MAC
1526  *
1527  * @details
1528  *
1529  *    Function : duBuildAndSendUeCreateReqToMac
1530  *
1531  *    Functionality: Builds and Send UE Create Request to MAC
1532  *
1533  * @Params[in]  cellId,
1534  *              ueIdx
1535  * @return ROK     - success
1536  *         RFAILED - failure
1537  *
1538  * ****************************************************************/
1539
1540 uint8_t duBuildAndSendUeCreateReqToMac(uint16_t cellId, uint8_t ueIdx,\
1541    uint16_t crnti, MacUeCfg *duMacUeCfg)
1542 {
1543    uint8_t ret = ROK;
1544    Pst pst;
1545    MacUeCfg *macUeCfg = NULLP;
1546
1547    ret = fillMacUeCfg(cellId, ueIdx, crnti, NULL, duMacUeCfg);
1548    if(ret == RFAILED)
1549    {
1550       DU_LOG("\nDU APP: Failed to fill MacUeCfg at duBuildAndSendUeCreateReqToMac()");
1551       return RFAILED;
1552    }
1553    /* Fill Pst */
1554    FILL_PST_DUAPP_TO_MAC(pst, EVENT_MAC_UE_CREATE_REQ);
1555
1556    /* Copying ueCb to a sharable buffer */
1557    DU_ALLOC_SHRABL_BUF(macUeCfg, sizeof(MacUeCfg));
1558    if(macUeCfg)
1559    {
1560       memset(macUeCfg, 0, sizeof(MacUeCfg));
1561       memcpy(macUeCfg, &duCb.actvCellLst[cellId - 1]->ueCb[ueIdx -1].macUeCfg, sizeof(MacUeCfg));
1562       DU_LOG("\nDU_APP: Sending UE create request to MAC");
1563
1564       /* Processing one Ue at a time to MAC */
1565       ret = (*packMacUeCreateReqOpts[pst.selector])(&pst, macUeCfg);
1566       if(ret == RFAILED)
1567       {
1568          DU_LOG("\nDU_APP : Failure in sending Ue Create Req to MAC at duBuildAndSendUeCreateReqToMac()");
1569          DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, macUeCfg, sizeof(MacUeCfg));
1570       }
1571    }
1572    else
1573    {
1574       DU_LOG("\n DU_APP: Memory alloc failed at duBuildAndSendUeCreateReqToMac()");
1575       ret = RFAILED;
1576    }
1577    return ret;
1578 }
1579
1580 /*******************************************************************
1581  *
1582  * @brief To update DuUeCb Mac Cfg
1583  *
1584  * @details
1585  *
1586  *    Function : duUpdateMacCfg
1587  *    Functionality:  update DuUeCb MAC Cfg
1588  *
1589  * @params[in] DuUeCb Pointer
1590  *             UeContextSetupDb pointer 
1591  * @return ROK     - success
1592  *         RFAILED - failure
1593  *
1594  * ****************************************************************/
1595 uint8_t duUpdateMacCfg(MacUeCfg *macUeCfg, UeContextSetupDb *f1UeDb) 
1596 {
1597    uint8_t ret, lcIdx, dbIdx, numLcs, lcDelIdx;
1598    ret = ROK;
1599
1600    /*Filling Cell Group Cfg*/
1601    ret =  procUeReCfgCellInfo(macUeCfg, f1UeDb->cellGrpCfg);
1602    if(ret == ROK)
1603    {
1604       ret = getMaxAggrBitRate(macUeCfg->maxAggrBitRate, f1UeDb->duUeCfg.maxAggrBitRate);
1605    }
1606    /* Filling LC Context */
1607    for(dbIdx = 0; (dbIdx < f1UeDb->duUeCfg.numMacLcs && ret == ROK); dbIdx++)
1608    {
1609       numLcs = macUeCfg->numLcs;
1610       for(lcIdx = 0; lcIdx < numLcs; lcIdx++)
1611       {
1612          if(f1UeDb->duUeCfg.macLcCfg[dbIdx].lcId ==  macUeCfg->lcCfgList[lcIdx].lcId)
1613          {
1614             if(f1UeDb->duUeCfg.macLcCfg[dbIdx].configType == CONFIG_MOD)
1615             {
1616                ret = fillMacLcCfgToAddMod(&macUeCfg->lcCfgList[lcIdx],&f1UeDb->duUeCfg.macLcCfg[dbIdx]);
1617             }
1618             else if(f1UeDb->duUeCfg.macLcCfg[dbIdx].configType == CONFIG_DEL)
1619             {
1620                /* Free memory at matched  lcIdx index */
1621                freeMacLcCfg(&macUeCfg->lcCfgList[lcIdx]);
1622                macUeCfg->numLcs--;
1623                for(lcDelIdx = lcIdx; lcDelIdx < macUeCfg->numLcs; lcDelIdx++)
1624                {
1625                   /* moving all elements one index ahead */
1626                   ret = fillMacLcCfgToAddMod(&macUeCfg->lcCfgList[lcDelIdx], &macUeCfg->lcCfgList[lcDelIdx+1]);
1627                   freeMacLcCfg(&macUeCfg->lcCfgList[lcDelIdx+1]);
1628                   if(ret == RFAILED)
1629                   {
1630                      DU_LOG("\nDU_APP: Failed to delete LC at Idx %d in duUpdateMacCfg()", lcDelIdx);
1631                      break;
1632                   }
1633                }
1634             }
1635          }
1636       } 
1637       if(f1UeDb->duUeCfg.macLcCfg[dbIdx].configType == CONFIG_ADD)
1638       {
1639          ret = fillMacLcCfgToAddMod(&macUeCfg->lcCfgList[numLcs], &f1UeDb->duUeCfg.macLcCfg[dbIdx]);
1640          if(ret == RFAILED)
1641          {
1642             DU_LOG("\nDU_APP: Failed to add LC at Idx %d in duUpdateMacCfg()", numLcs);
1643             break;
1644          }
1645          macUeCfg->numLcs++;
1646       }
1647                 
1648    }
1649    return ret;
1650 }
1651
1652 /******************************************************************
1653  *
1654  * @brief Function to fill the RLC Lc cfg from ueSetupReqDb
1655  *
1656  * @details
1657  *
1658  *    Function : fillRlcCfgToAddMod
1659  *
1660  *    Functionality: Function to fill the RLC Lc cfg from ueSetupReqDb
1661  *
1662  *
1663  *****************************************************************/
1664
1665 uint8_t fillRlcCfgToAddMod(RlcBearerCfg *lcCfg, RlcBearerCfg *f1UeDbLcCfg)
1666 {
1667    lcCfg->configType = f1UeDbLcCfg->configType;
1668    lcCfg->rbId       = f1UeDbLcCfg->rbId;
1669    lcCfg->rbType     = f1UeDbLcCfg->rbType;
1670    lcCfg->lcId       = f1UeDbLcCfg->lcId;
1671    lcCfg->lcType     = f1UeDbLcCfg->lcType;
1672    lcCfg->rlcMode    = f1UeDbLcCfg->rlcMode;
1673    switch(lcCfg->rlcMode)
1674    {
1675       case RLC_AM :
1676       {
1677          if(!lcCfg->u.amCfg)
1678          {
1679             DU_ALLOC_SHRABL_BUF(lcCfg->u.amCfg, sizeof(AmBearerCfg));
1680             if(!lcCfg->u.amCfg)
1681                return RFAILED;
1682          }
1683          /* DL AM */
1684          lcCfg->u.amCfg->dlAmCfg.snLenDl     = f1UeDbLcCfg->u.amCfg->dlAmCfg.snLenDl;    
1685          lcCfg->u.amCfg->dlAmCfg.pollRetxTmr = f1UeDbLcCfg->u.amCfg->dlAmCfg.pollRetxTmr;
1686          lcCfg->u.amCfg->dlAmCfg.pollPdu     = f1UeDbLcCfg->u.amCfg->dlAmCfg.pollPdu;
1687          lcCfg->u.amCfg->dlAmCfg.pollByte    = f1UeDbLcCfg->u.amCfg->dlAmCfg.pollByte;   
1688          lcCfg->u.amCfg->dlAmCfg.maxRetxTh   = f1UeDbLcCfg->u.amCfg->dlAmCfg.maxRetxTh;   
1689  
1690          /* UL AM */
1691          lcCfg->u.amCfg->ulAmCfg.snLenUl     = f1UeDbLcCfg->u.amCfg->ulAmCfg.snLenUl;
1692          lcCfg->u.amCfg->ulAmCfg.reAssemTmr  = f1UeDbLcCfg->u.amCfg->ulAmCfg.reAssemTmr; 
1693          lcCfg->u.amCfg->ulAmCfg.statProhTmr = f1UeDbLcCfg->u.amCfg->ulAmCfg.statProhTmr;
1694          break;
1695       }
1696       case RLC_UM_BI_DIRECTIONAL :
1697       {
1698          if(!lcCfg->u.umBiDirCfg)
1699          {
1700             DU_ALLOC_SHRABL_BUF(lcCfg->u.umBiDirCfg, sizeof(UmBiDirBearerCfg));
1701             if(!lcCfg->u.umBiDirCfg)
1702                 return RFAILED;
1703          }
1704          /* UL UM BI DIR INFO */
1705          lcCfg->u.umBiDirCfg->ulUmCfg.snLenUlUm  = f1UeDbLcCfg->u.umBiDirCfg->ulUmCfg.snLenUlUm;  
1706          lcCfg->u.umBiDirCfg->ulUmCfg.reAssemTmr = f1UeDbLcCfg->u.umBiDirCfg->ulUmCfg.reAssemTmr;
1707          /* DL UM BI DIR INFO */
1708          lcCfg->u.umBiDirCfg->dlUmCfg.snLenDlUm  = f1UeDbLcCfg->u.umBiDirCfg->dlUmCfg.snLenDlUm;
1709          break;
1710       }
1711       case RLC_UM_UNI_DIRECTIONAL_UL :
1712       {
1713          if(!lcCfg->u.umUniDirUlCfg)
1714          {
1715             DU_ALLOC_SHRABL_BUF(lcCfg->u.umUniDirUlCfg, sizeof(UmUniDirUlBearerCfg));
1716             if(!lcCfg->u.umUniDirUlCfg)
1717                return RFAILED;
1718          }
1719          lcCfg->u.umUniDirUlCfg->ulUmCfg.snLenUlUm  = f1UeDbLcCfg->u.umUniDirUlCfg->ulUmCfg.snLenUlUm;  
1720          lcCfg->u.umUniDirUlCfg->ulUmCfg.reAssemTmr = f1UeDbLcCfg->u.umUniDirUlCfg->ulUmCfg.reAssemTmr;
1721          break;
1722
1723       }
1724       case RLC_UM_UNI_DIRECTIONAL_DL :
1725       {
1726          if(!lcCfg->u.umUniDirDlCfg)
1727          {
1728             DU_ALLOC_SHRABL_BUF(lcCfg->u.umUniDirDlCfg, sizeof(UmUniDirDlBearerCfg));
1729             if(!lcCfg->u.umUniDirDlCfg)
1730                return RFAILED;
1731          }
1732          lcCfg->u.umUniDirDlCfg->dlUmCfg.snLenDlUm  = f1UeDbLcCfg->u.umUniDirDlCfg->dlUmCfg.snLenDlUm;
1733          break;
1734       }
1735       default:
1736          DU_LOG("\nDU_APP: Invalid Rlc Mode %d at fillRlcCfgToAddMod()", lcCfg->rlcMode);
1737          return RFAILED;
1738    }
1739    return ROK;
1740 }
1741
1742 /*******************************************************************
1743  *
1744  * @brief To update DuUeCb Rlc Lc Cfg
1745  *
1746  * @details
1747  *
1748  *    Function : duUpdateRlcLcCfg
1749  *    Functionality:  update DuUeCb Rlc Lc Cfg
1750  *
1751  * @params[in] DuUeCb Pointer
1752  *             UeContextSetupDb pointer 
1753  * @return ROK     - success
1754  *         RFAILED - failure
1755  *
1756  * ****************************************************************/
1757
1758 uint8_t duUpdateRlcLcCfg(RlcUeCfg *rlcUeCfg, UeContextSetupDb *f1UeDb)
1759 {
1760    uint8_t ret, lcIdx, dbIdx, numLcs, lcDelIdx;
1761
1762    ret = ROK;
1763    for(dbIdx = 0; (dbIdx < f1UeDb->duUeCfg.numRlcLcs && ret ==ROK); dbIdx++)
1764    {
1765       numLcs = rlcUeCfg->numLcs;
1766       for(lcIdx = 0; lcIdx < numLcs; lcIdx++)
1767       {
1768          if(f1UeDb->duUeCfg.rlcLcCfg[dbIdx].lcId == rlcUeCfg->rlcLcCfg[lcIdx].lcId)
1769          {
1770             if(f1UeDb->duUeCfg.rlcLcCfg[dbIdx].configType == CONFIG_MOD)
1771             {
1772                ret = fillRlcCfgToAddMod(&rlcUeCfg->rlcLcCfg[lcIdx], &f1UeDb->duUeCfg.rlcLcCfg[dbIdx]);
1773             }
1774             else if(f1UeDb->duUeCfg.rlcLcCfg[dbIdx].configType == CONFIG_DEL)
1775             {
1776                /* Free memory at matched lcIdx index */
1777                freeRlcLcCfg(&rlcUeCfg->rlcLcCfg[lcIdx]);
1778                rlcUeCfg->numLcs--;
1779                for(lcDelIdx = lcIdx; lcDelIdx < rlcUeCfg->numLcs; lcDelIdx++)
1780                {
1781                   ret = fillRlcCfgToAddMod(&rlcUeCfg->rlcLcCfg[lcDelIdx], &rlcUeCfg->rlcLcCfg[lcDelIdx+1]);
1782                   freeRlcLcCfg(&rlcUeCfg->rlcLcCfg[lcDelIdx+1]);
1783                   if(ret == RFAILED)
1784                   {
1785                      DU_LOG("\nDU_APP: Failed to delete LC at Idx %d in duUpdateRlcCfg()", lcDelIdx);
1786                      break;
1787                   }
1788                }
1789             }
1790          }
1791       }
1792       if(f1UeDb->duUeCfg.rlcLcCfg[dbIdx].configType == CONFIG_ADD)
1793       {
1794          ret = fillRlcCfgToAddMod(&rlcUeCfg->rlcLcCfg[lcIdx], &f1UeDb->duUeCfg.rlcLcCfg[dbIdx]);
1795          if(ret == ROK)
1796             rlcUeCfg->numLcs++;
1797       }
1798    }
1799    return ret;
1800 }
1801
1802
1803 /*******************************************************************
1804  *
1805  * @brief @brief To update DuUeCb Mac and Rlc Ue Cfg
1806  * 
1807  *
1808  * @details
1809  *
1810  *    Function : duUpdateDuUeCbCfg
1811  *
1812  *    Functionality: update DuUeCb Mac and Rlc Ue Cfg
1813  *
1814  * @params[in] ueIdx, cellIdx 
1815  * @return ROK     - success
1816  *         RFAILED - failure
1817  *
1818  * ****************************************************************/
1819
1820 uint8_t duUpdateDuUeCbCfg(uint8_t ueIdx, uint8_t cellId)
1821 {
1822    uint8_t ret = ROK, cellIdx = 0, crnti=0;
1823    DuUeCb *ueCb = NULLP;
1824
1825    GET_CELL_IDX(cellId, cellIdx);
1826    
1827    if((duCb.actvCellLst[cellIdx]->ueCb[ueIdx-1].macUeCfg. \
1828          macUeCfgState == UE_RECFG_COMPLETE) &&
1829       (duCb.actvCellLst[cellIdx]->ueCb[ueIdx-1].rlcUeCfg. \
1830          rlcUeCfgState == UE_RECFG_COMPLETE))
1831    {
1832       ueCb = &duCb.actvCellLst[cellIdx]->ueCb[ueIdx-1];
1833
1834       /*Filling RLC Ue Cfg */
1835       ueCb->rlcUeCfg.cellId = cellId;
1836       ueCb->rlcUeCfg.ueIdx  = ueIdx;
1837       ret = duUpdateRlcLcCfg(&ueCb->rlcUeCfg, ueCb->f1UeDb);
1838       if(ret == ROK)
1839       {
1840          /*Filling MAC Ue Cfg */
1841          GET_CRNTI(crnti, ueIdx);
1842          ueCb->macUeCfg.cellId = cellId;
1843          ueCb->macUeCfg.ueIdx  = ueIdx;
1844          ueCb->macUeCfg.crnti  = crnti;
1845          ret = duUpdateMacCfg(&ueCb->macUeCfg, ueCb->f1UeDb);
1846          if(ret == RFAILED)
1847             DU_LOG("\nDU APP: Failed while updating MAC LC Config at duUpdateDuUeCbCfg()");
1848       }
1849       else
1850          DU_LOG("\nDU APP: Failed while updating RLC LC Config at duUpdateDuUeCbCfg()");
1851    }
1852    else
1853       ret = RFAILED;
1854    return ret;
1855 }
1856
1857 /*******************************************************************
1858  *
1859  * @brief Handle UE config response from MAC
1860  *
1861  * @details
1862  *
1863  *    Function : DuProcMacUeCfgRsp
1864  *
1865  *    Functionality: Handle UE Config response from MAC
1866  *
1867  * @params[in] Pointer to MacUeCfgRsp and Pst 
1868  * @return ROK     - success
1869  *         RFAILED - failure
1870  *
1871  * ****************************************************************/
1872 uint8_t DuProcMacUeCfgRsp(Pst *pst, MacUeCfgRsp *cfgRsp)
1873 {
1874    uint8_t ret = ROK;
1875
1876    if(cfgRsp)
1877    {
1878       if(cfgRsp->result == MAC_DU_APP_RSP_OK)
1879       {
1880          if(pst->event == EVENT_MAC_UE_CREATE_RSP)
1881          {
1882             DU_LOG("\nDU APP : MAC UE Create Response : SUCCESS [UE IDX : %d]", cfgRsp->ueIdx);
1883             duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueIdx -1].\
1884                macUeCfg.macUeCfgState = UE_CREATE_COMPLETE;
1885          }
1886          else if(pst->event == EVENT_MAC_UE_RECONFIG_RSP)
1887          {
1888             DU_LOG("\nDU APP : MAC UE Reconfig Response : SUCCESS [UE IDX : %d]", cfgRsp->ueIdx);
1889             duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueIdx -1].\
1890                macUeCfg.macUeCfgState = UE_RECFG_COMPLETE;
1891             if((ret = duUpdateDuUeCbCfg(cfgRsp->ueIdx, cfgRsp->cellId)) == ROK)
1892                BuildAndSendUeCtxtRsp(cfgRsp->ueIdx, cfgRsp->cellId);
1893          }
1894       }
1895       else
1896       {
1897          DU_LOG("\nDU APP : MAC UE CFG Response for EVENT[%d]: FAILURE [UE IDX : %d]", pst->event, cfgRsp->ueIdx);
1898          if(pst->event == EVENT_MAC_UE_RECONFIG_RSP)
1899          {
1900             //TODO: Send the failure case in Ue Context Setup Response
1901          }
1902          ret = RFAILED;
1903       }
1904       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(MacUeCfgRsp));
1905    }
1906    else
1907    {
1908       DU_LOG("\nDU APP : Received MAC Ue Config Response is NULL at DuProcMacUeCfgRsp()");
1909       ret = RFAILED;
1910    }
1911    return ret;
1912 }
1913
1914
1915 /*******************************************************************
1916  *
1917  * @brief Processes UE create Req to RLC UL
1918  *
1919  * @details
1920  *
1921  *    Function : duBuildAndSendUeCreateReqToRlc
1922  *
1923  *    Functionality: 
1924  *     Processes UE create Req to RLC UL
1925  * 
1926  *  @params[in]  cellId,
1927  *               ueIdx,
1928  *               Pointer to RlcUeCfg
1929  *  @return ROK     - success
1930  *          RFAILED - failure
1931  * 
1932  *****************************************************************/
1933
1934 uint8_t duBuildAndSendUeCreateReqToRlc(uint16_t cellId, uint8_t ueIdx,\
1935    RlcUeCfg *duRlcUeCfg)
1936 {
1937    uint8_t ret = ROK;
1938    RlcUeCfg *rlcUeCfg = NULLP;
1939    Pst pst;
1940   
1941    ret = fillRlcUeCfg(cellId, ueIdx, NULL, duRlcUeCfg);
1942    if(ret == RFAILED)
1943    {
1944       DU_LOG("\nDU_APP: Failed to fill Rlc Ue Cfg at duBuildAndSendUeCreateReqToRlc()");
1945       return ret;
1946    }
1947
1948    FILL_PST_DUAPP_TO_RLC(pst, RLC_UL_INST, EVENT_RLC_UE_CREATE_REQ);
1949    /* Copying ueCfg to a sharable buffer */
1950    DU_ALLOC_SHRABL_BUF(rlcUeCfg, sizeof(RlcUeCfg));
1951    if(rlcUeCfg)
1952    {
1953       memset(rlcUeCfg, 0, sizeof(RlcUeCfg));
1954       memcpy(rlcUeCfg, duRlcUeCfg, sizeof(RlcUeCfg));
1955       /* Processing one Ue at a time to RLC */
1956       DU_LOG("\nDU_APP: Sending UE create request to RLC UL");
1957       ret = (*packRlcUeCreateReqOpts[pst.selector])(&pst, rlcUeCfg);
1958       if(ret == RFAILED)
1959       {
1960          DU_LOG("\nDU_APP : Failure in sending Ue Create Req to RLC");
1961          DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, rlcUeCfg, sizeof(RlcUeCfg));
1962          ret = RFAILED;
1963       }
1964    }
1965    else
1966    {
1967       DU_LOG("\n DU_APP: Memory alloc failed at duBuildAndSendUeCreateReqToRlc()");
1968       ret = RFAILED;
1969    }
1970    return ret;
1971 }
1972
1973 /*******************************************************************
1974  *
1975  * @brief Processes UE create Rsp received from RLC UL
1976  *
1977  * @details
1978  *
1979  
1980  *    Function : DuProcRlcUeCfgRsp
1981  *
1982  *    Functionality: 
1983  *     Processes UE create Rsp received from RLC UL
1984  * 
1985  *  @params[in]  Post structure
1986  *               Pointer to RlcCfgCfm
1987  *  @return ROK     - success
1988  *          RFAILED - failure
1989  * 
1990  *****************************************************************/
1991 uint8_t DuProcRlcUeCfgRsp(Pst *pst, RlcUeCfgRsp *cfgRsp)
1992 {
1993    uint8_t ret = ROK;
1994
1995    if(cfgRsp)
1996    {
1997       if(cfgRsp->result == RLC_DU_APP_RSP_OK)
1998       {
1999          if(pst->event == EVENT_RLC_UE_CREATE_RSP)
2000          {
2001             DU_LOG("\nDU_APP: RLC UE Create Response : SUCCESS [UE IDX:%d]", cfgRsp->ueIdx);
2002             duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueIdx -1].\
2003                rlcUeCfg.rlcUeCfgState = UE_CREATE_COMPLETE;
2004          }
2005          else if(pst->event == EVENT_RLC_UE_RECONFIG_RSP)
2006          {
2007             DU_LOG("\nDU_APP: RLC UE Reconfig Response : SUCCESS [UE IDX:%d]", cfgRsp->ueIdx);
2008             duCb.actvCellLst[cfgRsp->cellId -1]->ueCb[cfgRsp->ueIdx -1].\
2009                rlcUeCfg.rlcUeCfgState = UE_RECFG_COMPLETE;
2010             if((ret = duUpdateDuUeCbCfg(cfgRsp->ueIdx, cfgRsp->cellId)) == ROK)
2011                BuildAndSendUeCtxtRsp(cfgRsp->ueIdx, cfgRsp->cellId);
2012               
2013          }
2014       }
2015       else
2016       {
2017          DU_LOG("\nDU_APP: RLC UE CFG Response for EVENT[%d] : FAILED [UE IDX : %d, REASON :%d]",\
2018             pst->event, cfgRsp->ueIdx, cfgRsp->reason);
2019          if((pst->event == EVENT_RLC_UE_RECONFIG_RSP))
2020          {
2021             //TODO: update failure case in ue Context setup Response
2022          }
2023          ret = RFAILED;
2024       }
2025       DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cfgRsp, sizeof(RlcUeCfgRsp));
2026    }
2027    else
2028    {
2029       DU_LOG("\nDU_APP: Received RLC Ue Config Response is NULL at DuProcRlcUeCfgRsp()");
2030       ret = RFAILED;
2031    }
2032    return ret;
2033 }
2034
2035 /*******************************************************************
2036  *
2037  * @brief Builds and Send Ue Reconfig Req to RLC
2038  *
2039  * @details
2040  *
2041  *    Function : duBuildAndSendUeReCfgReqToRLC
2042  *
2043  *    Functionality: Builds and Send Ue Reconfig Req to RLC
2044  *
2045  * @params[in] cellId, crnti
2046  *             DuUeCfg *ueCfgDb
2047  *             RlcUeCfg *rlcUeCfg
2048  * @return ROK     - success
2049  *         RFAILED - failure
2050  *
2051  * ****************************************************************/
2052
2053 uint8_t duBuildAndSendUeReCfgReqToRlc(uint8_t cellId, uint8_t crnti, DuUeCfg *ueCfgDb)
2054 {
2055    uint8_t ret = ROK, ueIdx = 0;
2056    RlcUeCfg *rlcUeCfg = NULLP;
2057
2058    GET_UE_IDX(crnti, ueIdx);
2059    DU_ALLOC_SHRABL_BUF(rlcUeCfg, sizeof(RlcUeCfg));
2060    if(rlcUeCfg)
2061    {
2062       memset(rlcUeCfg, 0, sizeof(RlcUeCfg));
2063       ret = fillRlcUeCfg(cellId, ueIdx, ueCfgDb, rlcUeCfg);
2064       if(ret == RFAILED)
2065          DU_LOG("\nDU APP: Failed at duBuildAndSendUeReCfgReqToRlc()");
2066       else
2067          ret = sendUeReCfgReqToRlc(rlcUeCfg);
2068    }
2069    else
2070    {
2071       DU_LOG("\nDU APP: Memory Alloc failed at duBuildAndSendUeReCfgReqToRlc()");
2072       ret = RFAILED;
2073    }
2074    return ret;
2075 }
2076
2077 /*******************************************************************
2078  *
2079  * @brief Builds and Send Ue Reconfig Req to MAC
2080  *
2081  * @details
2082  *
2083  *    Function : duBuildAndSendUeReCfgReqToMac
2084  *
2085  *    Functionality: Builds and Send Ue Reconfig Req to MAC
2086  *
2087  * @params[in] CellGroupConfigRrc_t *macCellGrpCfg
2088  *             DuUeCfg *ueCfgDb
2089  *             MacUeCfg    *macUeCfg
2090  * @return ROK     - success
2091  *         RFAILED - failure
2092  *
2093  * ****************************************************************/
2094
2095 uint8_t duBuildAndSendUeReCfgReqToMac(uint8_t cellId, uint8_t crnti, UeContextSetupDb *ueCfgDb)
2096 {
2097    uint8_t ret, ueIdx = 0;
2098    MacUeCfg *macUeCfg = NULLP;
2099
2100    ret = ROK;
2101    GET_UE_IDX(crnti, ueIdx);
2102
2103    DU_ALLOC_SHRABL_BUF(macUeCfg, sizeof(MacUeCfg));
2104    if(macUeCfg)
2105    {
2106       memset(macUeCfg, 0, sizeof(MacUeCfg));
2107       ret = fillMacUeCfg(cellId, ueIdx, crnti, ueCfgDb, macUeCfg);
2108       if(ret == RFAILED)
2109          DU_LOG("\nDU_APP: Failed to fill Mac Ue Cfg at duBuildAndSendUeReCfgReqToMac()");
2110       else
2111          ret = sendUeReCfgReqToMac(macUeCfg);
2112    }
2113    else
2114    {
2115       DU_LOG("\nDU_APP: Memory alloc failed for macUeCfg at duBuildAndSendUeReCfgReqToMac()");
2116       ret = RFAILED;
2117    }
2118    return ret;
2119 }
2120
2121 /*******************************************************************
2122  *
2123  * @brief Process UE context setup request from CU
2124  *
2125  * @details
2126  *
2127  *    Function : duProcUeContextSetupRequest
2128  *
2129  *    Functionality: Process UE context setup request from CU
2130  *
2131  * @params[in] F1AP message
2132  * @return ROK     - success
2133  *         RFAILED - failure
2134  *
2135  * ****************************************************************/
2136
2137 uint8_t duProcUeContextSetupRequest(DuUeCb *ueCb)
2138 {
2139    uint8_t ret, cellId;
2140    
2141    ret = ROK;
2142    if(ueCb)
2143    {
2144       cellId = duCb.actvCellLst[ueCb->f1UeDb->cellIdx]->cellId;
2145
2146       if(ueCb->f1UeDb->rrcMsg)
2147       {
2148          /* Sending DL RRC Message to RLC */
2149          ret = duBuildAndSendDlRrcMsgToRlc(cellId, ueCb->rlcUeCfg, SRB1_LCID, false, \
2150             ueCb->f1UeDb->deliveryStaReq,  ueCb->f1UeDb->rrcMsgLen, ueCb->f1UeDb->rrcMsg);
2151          if(ret == RFAILED)
2152          {
2153             DU_LOG("\nDU APP : Failed to send DL RRC msg in duProcUeContextSetupRequest()");
2154             DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, ueCb->f1UeDb->rrcMsg, ueCb->f1UeDb->rrcMsgLen);
2155          }
2156       }
2157
2158       if(ret == ROK)
2159       {
2160          DU_LOG("\nDU_APP: Processing Ue Context Setup Request for cellId [%d]", cellId);
2161          /* Filling RLC Ue Reconfig */ 
2162          ret = duBuildAndSendUeReCfgReqToRlc(cellId, ueCb->crnti, &ueCb->f1UeDb->duUeCfg);
2163          if(ret == RFAILED)
2164             DU_LOG("\nDU_APP: Failed to build ctxt setup req for RLC at duBuildAndSendUeReCfgReqToRlc()");
2165          
2166          /* Filling MAC Ue Reconfig */
2167          ret = duBuildAndSendUeReCfgReqToMac(cellId, ueCb->crnti, ueCb->f1UeDb);
2168          if(ret == RFAILED)
2169             DU_LOG("\nDU_APP: Failed at build ctxt setup req for MAC at duBuildAndSendUeReCfgReqToMac()");
2170       }
2171       
2172    }
2173    else
2174    {
2175       //TODO: To send the failure cause in UeContextSetupRsp
2176       DU_LOG("DU_APP: Failed to process UE CNTXT SETUP REQ at duProcUeContextSetupRequest()");
2177    }
2178    return ret;
2179 }
2180
2181 /**********************************************************************
2182   End of file
2183  ***********************************************************************/