Moving all common header file into common_def.h file
[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 "lkw.x"
23 #include "legtp.h"
24 #include "du_cfg.h"
25 #include "du_ue_mgr.h"
26 #include<ProtocolIE-Field.h>
27 #include "F1AP-PDU.h"
28
29 #ifdef EGTP_TEST
30 U32 sduId = 0;
31 #endif
32
33 DuMacDlCcchInd packMacDlCcchIndOpts[] =
34 {
35    packMacDlCcchInd,   /* Loose coupling */
36    MacHdlDlCcchInd,    /* TIght coupling */
37    packMacDlCcchInd    /* Light weight-loose coupling */
38 };
39
40 DuMacUeCreateReq packMacUeCreateReqOpts[] =
41 {
42    packDuMacUeCreateReq,       /* Loose coupling */
43    MacHdlUeCreateReq,          /* TIght coupling */
44    packDuMacUeCreateReq,       /* Light weight-loose coupling */
45 };
46
47 /******************************************************************
48  *
49  * @brief Send UE configuration to RLC
50  *
51  * @details
52  *
53  *    Function : duSendUeCreateReqToRlc
54  *
55  *    Functionality: Send UeCreateReqToRlc
56  *
57  * @return ROK     - success
58  *         RFAILED - failure
59  *
60  * ****************************************************************/
61 S16 duSendUeCreateReqToRlc()
62 {
63    U8  idx;
64    Pst pst;
65    CkwCfgInfo *ueCfg;
66    
67    DU_SET_ZERO(&ueCfg, sizeof(ueCfg));
68    DU_SET_ZERO(&pst, sizeof(Pst));
69
70    DU_ALLOC(ueCfg, sizeof(CkwCfgInfo));
71
72 #ifdef EGTP_TEST
73    ueCfg->ueId = UE_ID;
74 #endif
75    ueCfg->cellId = NR_CELL_ID;
76    ueCfg->numEnt = 1;
77    
78    for(idx = 0; idx < ueCfg->numEnt; idx++)
79    {
80 #ifdef EGTP_TEST
81       ueCfg->entCfg[idx].rbId           = RB_ID;
82       ueCfg->entCfg[idx].rbType         = CM_LTE_DRB;
83       ueCfg->entCfg[idx].lCh[0].lChId   = LC_ID;
84       ueCfg->entCfg[idx].lCh[0].type    = CM_LTE_LCH_DTCH;
85 #endif
86       ueCfg->entCfg[idx].entMode        = CM_LTE_MODE_UM;
87       ueCfg->entCfg[idx].dir            = CKW_CFG_DIR_BOTH;
88       switch(ueCfg->entCfg[idx].entMode)
89       {
90          case CM_LTE_MODE_TM:
91          {
92             break;
93          }
94
95          case CM_LTE_MODE_UM:
96          {
97             ueCfg->entCfg[idx].m.umInfo.dl.snLen = 1;      /* For 12 bit SN */
98             ueCfg->entCfg[idx].m.umInfo.ul.snLen = 1;      /* For 12 bit SN */
99             ueCfg->entCfg[idx].m.umInfo.ul.reOrdTmr = 10;  /* in msec */
100             break;
101          }
102
103          case CM_LTE_MODE_AM:
104          {
105             break;
106          }
107          
108          default:
109             break;
110       } /* End of switch(entMode) */
111    } /* End of entity configuration for loop */
112
113    /* Fill Pst */
114    pst.selector  = ODU_SELECTOR_LWLC;
115    pst.srcEnt    = ENTDUAPP;
116    pst.dstEnt    = ENTKW;
117    pst.dstInst   = RLC_UL_INST;
118    pst.dstProcId = DU_PROC;
119    pst.srcProcId = DU_PROC;
120    pst.region    = duCb.init.region;
121
122    /* Sending to RLC */
123    packUeCreateReq(&pst, ueCfg);
124
125    RETVALUE(ROK); 
126 } /* End of duSendUeCreateReqToRlc */
127
128 /*******************************************************************
129  *
130  * @brief Handles EGTP data from CU 
131  *
132  * @details
133  *
134  *    Function : duHdlEgtpData
135  *
136  *    Functionality: 
137  *      Processes EGTP header and sends data to RLC
138  *
139  * @params[in]  Pointer to EGTP Message 
140  * @return ROK     - success
141  *         RFAILED - failure
142  *
143  * ****************************************************************/
144 S16 duHdlEgtpDlData(EgtpMsg  *egtpMsg)
145 {
146    /* TODO : Extract RbId/UeID/CellID/SduId from database
147       using tunnel id in egtp header */
148    
149    DU_LOG("\nDU_APP : Processing DL data");
150    
151    Pst pst;
152    KwuDatReqInfo datReqInfo;
153
154 #ifdef EGTP_TEST
155    datReqInfo.rlcId.rbId = RB_ID;
156    datReqInfo.rlcId.rbType = CM_LTE_DRB;
157    datReqInfo.rlcId.ueId = UE_ID;
158    datReqInfo.rlcId.cellId = NR_CELL_ID;
159    
160    datReqInfo.sduId = ++sduId;
161    datReqInfo.lcType = CM_LTE_LCH_DTCH;
162 #endif
163    /* Filling pst and Sending to RLC DL */
164    pst.selector  = ODU_SELECTOR_LWLC;
165    pst.srcEnt    = ENTDUAPP;
166    pst.dstEnt    = ENTKW;
167    pst.dstInst   = RLC_DL_INST;
168    pst.dstProcId = DU_PROC;
169    pst.srcProcId = DU_PROC;
170    pst.region    = duCb.init.region;
171
172    cmPkKwuDatReq(&pst, &datReqInfo, egtpMsg->msg);
173    return ROK;
174 }
175
176 /*******************************************************************
177  *
178  * @brief Handles UL data and send to CU
179  *
180  * @details
181  *
182  *    Function : duHdlRlcUlData
183  *
184  *    Functionality: 
185  *     Processes UL Data from RLC and sends to CU
186  * 
187  *  @params[in]  Pointer to EGTP Message 
188  *  @return ROK     - success
189  *          RFAILED - failure
190  * 
191  *****************************************************************/
192
193 PUBLIC S16 duHdlRlcUlData(Pst *pst, KwuDatIndInfo* datInd, Buffer *mBuf)
194 {
195    DU_LOG("\nDU_APP : Received UL Data at DU_APP");
196  
197    /* Send UL data to CU via EGTP */
198    duSendEgtpDatInd(mBuf);
199    SPutMsg(mBuf);
200
201    return ROK;
202 }
203
204 /******************************************************************
205 *
206 * @brief Builds and Sends DL CCCH Ind to MAC
207 *
208 * @details
209 *
210 *    Function : duBuildAndSendDlCcchInd 
211 *
212 *    Functionality: Builds and sends DL CCCH Ind Msg to MAC
213 *
214 * @params[in] dlCcchMsg - uint8_t*
215 * @return ROK     - success
216 *         RFAILED - failure
217 *
218 * ****************************************************************/
219 uint8_t duBuildAndSendDlCcchInd(uint16_t cellId, uint16_t crnti, \
220   DlCcchMsgType msgType, uint8_t *dlCcchMsg, uint16_t dlCcchMsgSize)
221 {
222         uint8_t ret                  = ROK;
223         uint16_t idx2;
224    DlCcchIndInfo *dlCcchIndInfo = NULLP;
225    Pst pst;
226    
227         memset(&pst, 0, sizeof(Pst));
228    DU_LOG("\nDU APP : Building and Sending DL CCCH Ind to MAC");
229
230         DU_ALLOC_SHRABL_BUF(dlCcchIndInfo, sizeof(DlCcchIndInfo));
231
232    if(!dlCcchIndInfo)
233    {
234                 DU_LOG("\nDU APP : Memory alloc failed while building DL CCCH Ind");
235                 return RFAILED;
236         }
237
238         dlCcchIndInfo->cellId = cellId;
239         dlCcchIndInfo->crnti = crnti;
240         dlCcchIndInfo->msgType = msgType;
241         dlCcchIndInfo->dlCcchMsgLen = dlCcchMsgSize;
242
243         DU_ALLOC_SHRABL_BUF(dlCcchIndInfo->dlCcchMsg, dlCcchIndInfo->dlCcchMsgLen);
244         if(!dlCcchIndInfo->dlCcchMsg)
245    {
246                 DU_LOG("\nDU APP : Memory alloc failed while building DL CCCH Ind");
247                 DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, dlCcchIndInfo, sizeof(DlCcchIndInfo));
248                 return RFAILED;
249         }
250         for(idx2 = 0; idx2 < dlCcchIndInfo->dlCcchMsgLen; idx2++)
251         {
252            dlCcchIndInfo->dlCcchMsg[idx2] = dlCcchMsg[idx2];
253         }
254         DU_FREE(dlCcchMsg, dlCcchMsgSize);
255
256         /* Fill Pst */
257         pst.selector  = ODU_SELECTOR_LWLC;
258         pst.srcEnt    = ENTDUAPP;
259         pst.dstEnt    = ENTRG;
260         pst.dstInst   = 0;
261         pst.srcInst   = 0;
262         pst.dstProcId = DU_PROC;
263         pst.srcProcId = DU_PROC;
264         pst.region    = DU_APP_MEM_REGION;
265         pst.pool      = DU_POOL;
266         pst.event     = EVENT_MAC_DL_CCCH_IND;
267
268    ret = (*packMacDlCcchIndOpts[pst.selector])(&pst, dlCcchIndInfo);
269         if(ret != ROK)
270         {
271       DU_LOG("\nDU_APP : Failure in sending DL CCCH to MAC");
272                 DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, dlCcchIndInfo->dlCcchMsg,\
273                   dlCcchIndInfo->dlCcchMsgLen);
274                 DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, dlCcchIndInfo, \
275                   sizeof(DlCcchIndInfo));
276       ret = RFAILED; 
277         }
278
279         return ret;
280
281 }
282
283 /******************************************************************
284 *
285 * @brief Processes DL RRC Message Transfer  sent by CU
286 *
287 * @details
288 *
289 *    Function : procDlRrcMsgTrans 
290 *
291 *    Functionality: Processes DL RRC Message Transfer sent by CU
292 *
293 * @params[in] F1AP_PDU_t ASN decoded F1AP message
294 * @return ROK     - success
295 *         RFAILED - failure
296 *
297 * ****************************************************************/
298 uint8_t procDlRrcMsgTrans(F1AP_PDU_t *f1apMsg)
299 {
300         DLRRCMessageTransfer_t *dlRrcMsg = NULLP;
301         uint8_t                *dlCcchMsg = NULLP;
302         uint8_t                idx, ret, srbId;
303         uint16_t               idx2, crnti, cellId, dlCcchMsgSize;
304         uint32_t               gnbCuUeF1apId, gnbDuUeF1apId;
305
306
307         DU_LOG("\nDU_APP : DL RRC message transfer Recevied");
308         dlRrcMsg = &f1apMsg->choice.initiatingMessage->value.choice.DLRRCMessageTransfer;
309
310         ret = ROK;
311
312         for(idx=0; idx<dlRrcMsg->protocolIEs.list.count; idx++)
313         {
314                 switch(dlRrcMsg->protocolIEs.list.array[idx]->id)
315                 {
316                         case ProtocolIE_ID_id_gNB_CU_UE_F1AP_ID:
317                                 {
318                                         gnbCuUeF1apId = dlRrcMsg->protocolIEs.list.array[idx]->value.choice.GNB_CU_UE_F1AP_ID;
319                                         break;
320                                 }
321                         case ProtocolIE_ID_id_gNB_DU_UE_F1AP_ID:
322                                 {
323                                         gnbDuUeF1apId = dlRrcMsg->protocolIEs.list.array[idx]->value.choice.GNB_DU_UE_F1AP_ID;
324                                         break;
325                                 }
326                         case ProtocolIE_ID_id_SRBID:
327                                 {
328                                         srbId = dlRrcMsg->protocolIEs.list.array[idx]->value.choice.SRBID;
329                                         break;
330                                 }
331         case ProtocolIE_ID_id_ExecuteDuplication:
332                                         break;
333
334                         case ProtocolIE_ID_id_RRCContainer:
335                                 {
336                                    if(dlRrcMsg->protocolIEs.list.array[idx]->value.choice.RRCContainer.size > 0)
337                                         {
338                                       dlCcchMsgSize = dlRrcMsg->protocolIEs.list.array[idx]->value.choice.RRCContainer.size;
339                                       DU_ALLOC(dlCcchMsg, dlCcchMsgSize);
340                                            for(idx2 = 0; idx2 < dlCcchMsgSize; idx2++)
341                                            {
342                                               dlCcchMsg[idx2] = \
343                                                  dlRrcMsg->protocolIEs.list.array[idx]->value.choice.RRCContainer.buf[idx2];
344                                            }
345                                         }
346                                         else
347                                         {
348                                       DU_LOG("\nDU_APP : RRC Container Size is invalid:%ld",\
349                                                  dlRrcMsg->protocolIEs.list.array[idx]->value.choice.RRCContainer.size);
350                                         }
351                                         break;
352                                 }
353
354                         default:
355                                 DU_LOG("\nDU_APP : Invalid IE received in DL RRC Msg Transfer:%ld",
356                                                 dlRrcMsg->protocolIEs.list.array[idx]->id);
357                 }
358         }
359    
360    for(idx=0; idx<duCb.numUe; idx++)
361         {
362       if(gnbDuUeF1apId == duCb.ueCcchCtxt[idx].gnbDuUeF1apId)
363                 {
364                    crnti  = duCb.ueCcchCtxt[idx].crnti;
365                         cellId = duCb.ueCcchCtxt[idx].cellId;
366                         break;
367                 }
368         }
369         if(srbId == SRB_ID_1) //RRC connection setup
370         {
371                 ret = duBuildAndSendDlCcchInd(cellId, crnti, RRC_SETUP, dlCcchMsg, dlCcchMsgSize);
372       if(ret)
373       {
374          DU_LOG("\nDU_APP: Falied at duBuildAndSendDlCcchInd()");
375       }
376       else
377       {
378                    if(duCb.actvCellLst[cellId-1]->numActvUes < DU_MAX_UE)
379                         {
380              ret = duCreateUeCb(&duCb.ueCcchCtxt[idx], gnbCuUeF1apId);
381                                  if(ret)
382                                  {
383                 DU_LOG("\nDU_APP: Failed at duCreateUeCb for cellId [%d]", duCb.ueCcchCtxt[idx].cellId);
384                                          ret = RFAILED;
385                                  }
386                         }
387                         else
388                         {
389             DU_LOG("\nDU_APP: Max Active UEs has reached");
390                            ret = RFAILED;
391                         }
392       }
393    }            
394         return ret;
395 }
396
397 /******************************************************************
398  *
399  * @brief Generates GNB DU Ue F1AP ID
400  *
401  * @details
402  *
403  *    Function : genGnbDuUeF1apId
404  *
405  *    Functionality: Generates GNB DU Ue F1AP ID
406  *
407  * @params[in] void
408  * @return gnbDuF1apId
409  *
410  * ****************************************************************/
411 uint32_t genGnbDuUeF1apId()
412 {
413         static uint32_t gnbDuUeF1apId = 0;
414
415         return ++gnbDuUeF1apId;
416 }
417
418 /******************************************************************
419  *
420  * @brief Processes UL CCCH Ind recvd from MAC
421  *
422  * @details
423  *
424  *    Function : duProcUlCcchInd
425  *
426  *    Functionality: Processes UL CCCH Ind recvd from MAC
427  *
428  * @params[in] UlCcchIndInfo *ulCcchIndInfo
429  * @return ROK     - success
430  *         RFAILED - failure
431  *
432  * ****************************************************************/
433 uint8_t duProcUlCcchInd(UlCcchIndInfo *ulCcchIndInfo)
434 {
435
436    uint8_t ret = ROK;
437         uint32_t gnbDuUeF1apId = 0;
438
439         gnbDuUeF1apId = genGnbDuUeF1apId();
440
441         /* Store Ue mapping */
442         duCb.ueCcchCtxt[duCb.numUe].gnbDuUeF1apId = gnbDuUeF1apId;
443         duCb.ueCcchCtxt[duCb.numUe].crnti         = ulCcchIndInfo->crnti;
444         duCb.ueCcchCtxt[duCb.numUe].cellId        = ulCcchIndInfo->cellId;
445
446         duCb.numUe++;
447
448    ret = (BuildAndSendInitialRrcMsgTransfer(gnbDuUeF1apId, ulCcchIndInfo->crnti,
449                                 ulCcchIndInfo->ulCcchMsg));
450         if(ret != ROK)
451         {
452       DU_LOG("\nDU_APP : BuildAndSendInitialRrcMsgTransfer failed");
453         }
454
455    DU_FREE_SHRABL_BUF(MAC_MEM_REGION, RG_POOL, ulCcchIndInfo->ulCcchMsg, strlen((const char*)ulCcchIndInfo->ulCcchMsg));
456    DU_FREE_SHRABL_BUF(MAC_MEM_REGION, RG_POOL, ulCcchIndInfo, sizeof(UlCcchIndInfo));
457
458         return ret;
459
460 }
461
462 /******************************************************************
463  *
464  * @brief Fills Initial DL Bandwidth Part
465  *
466  * @details
467  *
468  *    Function : fillInitDlBwp
469  *
470  *    Functionality: Fills Initial DL Bandwidth Part
471  *
472  *
473  *****************************************************************/
474 void fillInitDlBwp(InitialDlBwp *initDlBwp)
475 {
476    uint8_t idx = 0;
477
478    if(initDlBwp)
479    {
480       /* Filling PDCCH Config */
481            initDlBwp->pdcchPresent = TRUE;
482                 if(initDlBwp->pdcchPresent)
483                 {
484          initDlBwp->pdcchCfg.numCRsetToAddMod = PDCCH_CTRL_RSRC_SET_ONE_ID;
485          memset(initDlBwp->pdcchCfg.cRSetToAddModList, 0, MAX_NUM_CRSET);
486          if(initDlBwp->pdcchCfg.numCRsetToAddMod <= MAX_NUM_CRSET)
487          {
488             initDlBwp->pdcchCfg.cRSetToAddModList[idx].cRSetId = \
489              PDCCH_CTRL_RSRC_SET_ONE_ID;
490             memset(initDlBwp->pdcchCfg.cRSetToAddModList[idx].freqDomainRsrc, 0,\
491              FREQ_DOM_RSRC_SIZE); 
492             initDlBwp->pdcchCfg.cRSetToAddModList[idx].freqDomainRsrc[idx] =\
493              PDCCH_FREQ_DOM_RSRC;
494             initDlBwp->pdcchCfg.cRSetToAddModList[idx].duration = \
495              PDCCH_CTRL_RSRC_SET_ONE_DURATION;
496             initDlBwp->pdcchCfg.cRSetToAddModList[idx].cceRegMappingType = \
497              CCE_REG_MAPPINGTYPE_PR_NONINTERLEAVED;
498             initDlBwp->pdcchCfg.cRSetToAddModList[idx].precoderGranularity = \
499              ALL_CONTIGUOUS_RBS;
500             initDlBwp->pdcchCfg.cRSetToAddModList[idx].dmrsScramblingId = \
501              SCRAMBLING_ID;
502          }
503          initDlBwp->pdcchCfg.numCRsetToRel = 0;
504          /* Filling Serach Space */
505          initDlBwp->pdcchCfg.numSearchSpcToAddMod = PDCCH_CTRL_RSRC_SET_ONE_ID;
506          memset(initDlBwp->pdcchCfg.searchSpcToAddModList, 0, MAX_NUM_CRSET);
507          if(initDlBwp->pdcchCfg.numSearchSpcToAddMod <= MAX_NUM_CRSET)
508          {
509             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].searchSpaceId =\
510              PDCCH_SRCH_SPC_TWO_ID;
511             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].cRSetId = \
512              PDCCH_CTRL_RSRC_SET_ONE_ID;
513             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].\
514              mSlotPeriodicityAndOffset = SLOTPERIODICITYANDOFFSET_PR_SL1;
515             memset(initDlBwp->pdcchCfg.searchSpcToAddModList[idx].mSymbolsWithinSlot, 0,\
516               MONITORING_SYMB_WITHIN_SLOT_SIZE);
517             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].mSymbolsWithinSlot[idx] =\
518               PDCCH_SYMBOL_WITHIN_SLOT;
519             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].numCandidatesAggLevel1 =\
520              AGGREGATIONLEVEL_N8; 
521             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].numCandidatesAggLevel2 =\
522              AGGREGATIONLEVEL_N8; 
523             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].numCandidatesAggLevel4 =\
524              AGGREGATIONLEVEL_N4; 
525             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].numCandidatesAggLevel8 =\
526              AGGREGATIONLEVEL_N2; 
527             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].numCandidatesAggLevel16 =\
528              AGGREGATIONLEVEL_N1;
529             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].searchSpaceType = \
530              SEARCHSPACETYPE_PR_UE_SPECIFIC;
531             initDlBwp->pdcchCfg.searchSpcToAddModList[idx].ueSpecificDciFormat =\
532              PDCCH_SRCH_SPC_TWO_UE_SPEC_DCI_FORMAT;
533
534             initDlBwp->pdcchCfg.numSearchSpcToRel = 0;
535
536          }
537                 }
538       /* Filling PDSCH Config */
539            initDlBwp->pdschPresent = TRUE;
540                 if(initDlBwp->pdschPresent)
541                 {
542          initDlBwp->pdschCfg.dmrsDlCfgForPdschMapTypeA.addPos = ADDITIONALPOSITION_POS0;
543          initDlBwp->pdschCfg.resourceAllocType = RESOURCEALLOCATION_TYPE1;
544          initDlBwp->pdschCfg.numTimeDomRsrcAlloc = 1;
545          initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].mappingType = \
546           MAPPING_TYPEA;
547          initDlBwp->pdschCfg.timeDomRsrcAllociList[idx].startSymbolAndLength = \
548           PDSCH_START_SYMBOL_LEN;
549          initDlBwp->pdschCfg.rbgSize = RBG_SIZE_CONFIG1;
550          initDlBwp->pdschCfg.numCodeWordsSchByDci = CODEWORDS_SCHED_BY_DCI_N1;
551          initDlBwp->pdschCfg.bundlingType = TYPE_STATIC_BUNDLING;
552                 }
553    }
554
555 }
556
557 /******************************************************************
558  *
559  * @brief Fills Initial UL Bandwidth Part
560  *
561  * @details
562  *
563  *    Function : fillInitUlBwp
564  *
565  *    Functionality: Fills Initial UL Bandwidth Part
566  *
567  *
568  *****************************************************************/
569 void fillInitUlBwp(InitialUlBwp *initUlBwp)
570 {
571    uint8_t idx;
572    if(initUlBwp)
573    {
574       initUlBwp->pucchPresent = FALSE;
575
576       /*Filling PUSCH Config */
577       initUlBwp->puschPresent = TRUE;
578                 if(initUlBwp->puschPresent)
579                 {
580          initUlBwp->puschCfg.dmrsUlCfgForPuschMapTypeA.addPos = ADDITIONALPOSITION_POS0; 
581          initUlBwp->puschCfg.dmrsUlCfgForPuschMapTypeA.transPrecodDisabled. \
582            scramblingId0 = SCRAMBLING_ID; 
583          initUlBwp->puschCfg.resourceAllocType = RESOURCEALLOCATION_TYPE1;
584          initUlBwp->puschCfg.numTimeDomRsrcAlloc = 1;
585          idx = 0;
586          if(initUlBwp->puschCfg.numTimeDomRsrcAlloc <= MAX_NUM_UL_ALLOC)
587          {
588             initUlBwp->puschCfg.timeDomRsrcAllocList[idx].k2 = PUSCH_K2;
589             initUlBwp->puschCfg.timeDomRsrcAllocList[idx].mappingType =\
590               MAPPING_TYPEA;
591             initUlBwp->puschCfg.timeDomRsrcAllocList[idx].startSymbolAndLength =\
592               PUSCH_START_SYMBOL_LEN;
593          }
594          initUlBwp->puschCfg.transformPrecoder = TRANSFORM_PRECODER_DISABLED;
595                 }
596    }
597    else
598    {
599       DU_LOG("\n DUAPP: Memory is NULL of InitalUlBwp");
600    }
601
602 }
603 /******************************************************************
604  *
605  * @brief Fills SpCell Group Info
606  *
607  * @details
608  *
609  *    Function : fillSpCellGrpInfo
610  *
611  *    Functionality: Fills Sp Cell Group Info
612  *
613  *
614  *****************************************************************/
615 void fillSpCellGrpInfo(SpCellCfg *spCell)
616 {
617    if(spCell)
618    {
619       spCell->servCellIdx = SERV_CELL_IDX;
620       /* Filling Initial Dl Bwp */
621       fillInitDlBwp(&spCell->servCellCfg.initDlBwp);
622
623       spCell->servCellCfg.numDlBwpToAdd    = 0; 
624       spCell->servCellCfg.firstActvDlBwpId = ACTIVE_DL_BWP_ID;
625       spCell->servCellCfg.defaultDlBwpId   = ACTIVE_DL_BWP_ID;
626       spCell->servCellCfg.bwpInactivityTmr = NULLP;
627       spCell->servCellCfg.pdschServCellCfg.maxMimoLayers = NULLP;
628       spCell->servCellCfg.pdschServCellCfg.maxCodeBlkGrpPerTb = NULLP;
629       spCell->servCellCfg.pdschServCellCfg.codeBlkGrpFlushInd = NULLP;
630       spCell->servCellCfg.pdschServCellCfg.xOverhead = NULLP;
631       spCell->servCellCfg.pdschServCellCfg.numHarqProcForPdsch =\
632            NUM_HARQ_PROC_FOR_PDSCH_N_16;
633       /* Filling Initial UL Bwp*/
634       fillInitUlBwp(&spCell->servCellCfg.initUlBwp);
635       spCell->servCellCfg.numUlBwpToAdd     = 0; 
636       spCell->servCellCfg.firstActvUlBwpId  = ACTIVE_DL_BWP_ID; 
637    }
638    else
639    {
640       DU_LOG("\n DU_APP: Memory is NULL for SpCellGrp");
641    }
642 }
643
644 /******************************************************************
645  *
646  * @brief Fills Physical Cell Group Info
647  *
648  * @details
649  *
650  *    Function : fillPhyCellGrpInfo
651  *
652  *    Functionality: Fills Physical Cell Group Info
653  *
654  *
655  *****************************************************************/
656 void fillPhyCellGrpInfo(PhyCellGrpCfg *cellGrp)
657 {
658    if(cellGrp)
659    {
660       cellGrp->pdschHarqAckCodebook = PDSCH_HARQ_ACK_CODEBOOK_DYNAMIC;
661       cellGrp->pNrFr1 = P_NR_FR1;
662    }
663    else
664    {
665       DU_LOG("\nDUAPP: Memory is NULL for Physical Cell Group");
666    }
667 }
668
669 /******************************************************************
670  *
671  * @brief Fills Mac Cell Group Info
672  *
673  * @details
674  *
675  *    Function : fillMacCellGrpInfo
676  *
677  *    Functionality: Fills Mac Cell Group Info
678  *
679  *
680  *****************************************************************/
681 void fillMacCellGrpInfo(MacCellGrpCfg *cellGrp)
682 {
683    uint8_t idx;
684
685    if(cellGrp)
686    {
687       /* Filling Scheduling Request Config */
688       cellGrp->schReqCfg.addModListCount = 1;
689       if(cellGrp->schReqCfg.addModListCount <= MAX_NUM_SR_CFG_PER_CELL_GRP)
690       {
691          for(idx = 0; idx < cellGrp->schReqCfg.addModListCount; idx++)
692          {
693             cellGrp->schReqCfg.addModList[idx].schedReqId    = SCH_REQ_ID;
694             cellGrp->schReqCfg.addModList[idx].srProhibitTmr = SR_PROHIBIT_MS_32;
695             cellGrp->schReqCfg.addModList[idx].srTransMax    = SR_TRANS_MAX_N_16;
696          }
697       }
698       cellGrp->schReqCfg.relListCount = 0;
699
700       /* Filling Tag config */
701       cellGrp->tagCfg.addModListCount = 1;
702       if(cellGrp->tagCfg.addModListCount <= MAC_NUM_TAGS)
703       {
704          for(idx = 0; idx < cellGrp->tagCfg.addModListCount; idx++)
705          {
706             cellGrp->tagCfg.addModList[idx].tagId = TAG_ID;
707             cellGrp->tagCfg.addModList[idx].timeAlignTimer = TIME_ALIGNMENT_TIMER_INFINITY;
708          }
709       }
710       cellGrp->tagCfg.relListCount = 0;
711       
712       /* Filling PHR config */
713       cellGrp->phrCfgSetupPres = true;
714       cellGrp->phrCfg.periodicTimer = PHR_PERIODIC_TIMER_INFINITY;
715       cellGrp->phrCfg.prohibitTimer = PHR_PROHIBIT_TIMER_SF_0;
716       cellGrp->phrCfg.txPowerFactor = PHR_TX_PWR_FACTOR_CHANGE_INFINITY;
717       cellGrp->phrCfg.multiplePHR   = false;
718       cellGrp->phrCfg.dummy         = false;
719       cellGrp->phrCfg.phrType2OtherCell = false;
720       cellGrp->phrCfg.phrOtherCG = PHR_MODE_OTHER_CG_REAL;  
721
722    }
723    else
724    {
725       DU_LOG("\nDUAPP: Memory is NULL for Master Cell Group");
726    }
727 }
728
729 /******************************************************************
730  *
731  * @brief Fills Logical Channel Config List
732  *
733  * @details
734  *
735  *    Function : fillLcCfgList
736  *
737  *    Functionality: Fills Logical channel Config List
738  *
739  *
740  *****************************************************************/
741 void fillLcCfgList(LcCfg *lcCfgInfo)
742 {
743    if(lcCfgInfo)
744    {
745       lcCfgInfo->lcId = SRB_ID_1;
746       lcCfgInfo->drbQos = NULLP;
747       lcCfgInfo->snssai = NULLP;
748       lcCfgInfo->ulLcCfg = NULLP;
749       lcCfgInfo->dlLcCfg.lcp = LC_PRIORITY_1;
750       
751 #if 0
752       /* TODO: To be filled on receving UE CONTEXT SETUP from CU */
753       /* Filling Qos characteristics */
754       lcCfgInfo->drbQos.fiveQiType = QoS_Characteristics_PR_non_Dynamic_5QI;
755       lcCfgInfo->drbQos.u.nonDyn5Qi.fiveQi = 0;
756       lcCfgInfo->drbQos.u.nonDyn5Qi.priorLevel = 0;
757       lcCfgInfo->drbQos.u.nonDyn5Qi.avgWindow = 0;
758       lcCfgInfo->drbQos.u.nonDyn5Qi.maxDataBurstVol = 0;
759
760       /* Filling NgRAN */
761       lcCfgInfo->drbQos.ngRanRetPri.priorityLevel = PriorityLevel_highest;
762       lcCfgInfo->drbQos.ngRanRetPri.preEmptionCap = \
763        Pre_emptionCapability_may_trigger_pre_emption;
764       lcCfgInfo->drbQos.ngRanRetPri.preEmptionVul = \
765        Pre_emptionVulnerability_not_pre_emptable;
766
767       /* Filling Grb Qos */
768       lcCfgInfo->drbQos.grbQosInfo.maxFlowBitRateDl  = 0;
769       lcCfgInfo->drbQos.grbQosInfo.maxFlowBitRateUl  = 0;
770       lcCfgInfo->drbQos.grbQosInfo.guarFlowBitRateDl = 0;
771       lcCfgInfo->drbQos.grbQosInfo.guarFlowBitRateUl = 0;
772       
773       /* Filling S-NSSAI */
774       /* TODO :To be filled when UE Context Setup Request is sent from CU */
775       /* Filling UL Logical Channel Config */
776       lcCfgInfo->ulLcCfg.priority = 0;
777       lcCfgInfo->ulLcCfg.lcGroup  = 0;
778       lcCfgInfo->ulLcCfg.schReqId = 0;
779       lcCfgInfo->ulLcCfg.pbr = 0;
780       lcCfgInfo->ulLcCfg.bsd = 0;
781         
782       /* Filling DL Logical Channel Config */
783       lcCfgInfo->dlLcCfg.lcp = 0;
784 #endif
785    }
786    else
787    {
788       DU_LOG("\n Memory is null for LcCfgList");
789    }
790 }
791
792 /******************************************************************
793  *
794  * @brief Fills MacUeCfg structure
795  *
796  * @details
797  *
798  *    Function : fillMacUeCfg
799  *
800  *    Functionality: Fills MacUeCfg
801  *
802  *
803  *****************************************************************/
804 void fillMacUeCfg(MacUeCfg *ueCfg)
805 {
806    uint8_t idx;
807
808    /* Filling MacCellGroup Config */ 
809    fillMacCellGrpInfo(&ueCfg->macCellGrpCfg);
810    /* Filling PhyCellGroup Config */ 
811    fillPhyCellGrpInfo(&ueCfg->phyCellGrpCfg);
812    /* Filling SpCellGroup Config */ 
813    fillSpCellGrpInfo(&ueCfg->spCellCfg);
814    /* Filling AMBR for UL and DL */ 
815         ueCfg->maxAggrBitRate = NULLP;
816    /* Filling LC Context */
817    ueCfg->numLcs = SRB_ID_1;
818    if(ueCfg->numLcs < MAX_NUM_LOGICAL_CHANNELS)
819    {
820       for(idx = 0; idx < ueCfg->numLcs; idx++)
821       {   
822          fillLcCfgList(&ueCfg->lcCfgList[idx]);
823       }
824    }
825
826 }
827
828 /******************************************************************
829  *
830  * @brief creates UE context
831  *
832  * @details
833  *
834  *    Function : duCreateUeCb
835  *
836  *    Functionality: Creates UE Conetxt
837  *
838  * @params[in] UeCcchCtxt Pointer
839  *             UeIdx Pointer
840  *
841  * @return ROK     - success
842  *         RFAILED - failure
843  * ****************************************************************/
844 uint8_t duCreateUeCb(UeCcchCtxt *ueCcchCtxt, uint32_t gnbCuUeF1apId)
845 {
846         uint8_t cellIdx = 0;
847         uint8_t ret     = ROK;
848         uint8_t ueIdx;
849
850    for(cellIdx = 0; cellIdx < DU_MAX_CELLS; cellIdx++)
851         {
852            if(ueCcchCtxt->cellId == duCb.actvCellLst[cellIdx]->cellId)
853                 {
854                    ueIdx = ueCcchCtxt->crnti % DU_UE_START_CRNTI;
855          DU_LOG("\nDU_APP: Filling UeCb for ueIdx [%d]", ueIdx);
856                    duCb.actvCellLst[cellIdx]->ueCb[ueIdx].gnbDuUeF1apId = ueCcchCtxt->gnbDuUeF1apId;
857                    duCb.actvCellLst[cellIdx]->ueCb[ueIdx].gnbCuUeF1apId = gnbCuUeF1apId;
858          duCb.actvCellLst[cellIdx]->ueCb[ueIdx].ueState       = UE_ACTIVE;
859          
860                         /* Filling Mac Ue Config */ 
861          memset(&duCb.actvCellLst[cellIdx]->ueCb[ueIdx].macUeCfg, 0, sizeof(MacUeCfg));
862          duCb.actvCellLst[cellIdx]->ueCb[ueIdx].macUeCfg.cellIdx       = ueCcchCtxt->cellId;
863          duCb.actvCellLst[cellIdx]->ueCb[ueIdx].macUeCfg.ueIdx         = ueIdx;
864          duCb.actvCellLst[cellIdx]->ueCb[ueIdx].macUeCfg.crnti         = ueCcchCtxt->crnti;
865          fillMacUeCfg(&duCb.actvCellLst[cellIdx]->ueCb[ueIdx].macUeCfg);
866                    duCb.actvCellLst[cellIdx]->numActvUes++;
867                         memset(ueCcchCtxt, 0, sizeof(UeCcchCtxt));
868
869                         /* Send Ue Create Request to MAC */
870                         ret = duBuildAndSendUeCreateReqToMac(duCb.actvCellLst[cellIdx]->ueCb[ueIdx].macUeCfg.cellIdx, ueIdx);
871                         if(ret)
872                            DU_LOG("\nDU_APP: Failed to send UE create request to MAC");
873                 }
874    }
875
876         return ret;
877 }
878
879 /******************************************************************
880  *
881  * @brief Builds and Send UE Create Request to MAC
882  *
883  * @details
884  *
885  *    Function : duBuildAndSendUeCreateReqToMac
886  *
887  *    Functionality: Builds and Send UE Create Request to MAC
888  *
889  * @Params[in]  cellId,
890  *              ueIdx
891  * @return ROK     - success
892  *         RFAILED - failure
893  *
894  * ****************************************************************/
895
896 uint8_t duBuildAndSendUeCreateReqToMac(uint16_t cellId, uint8_t ueIdx)
897 {
898    uint8_t ret = ROK;
899    MacUeCfg *macUeCfg = NULLP;
900    Pst pst;
901
902    memset(&pst, 0, sizeof(Pst));
903
904    /* Fill Pst */
905    pst.selector  = ODU_SELECTOR_LWLC;
906    pst.srcEnt    = ENTDUAPP;
907    pst.srcInst   = DU_INST;
908    pst.dstEnt    = ENTRG;
909    pst.dstInst   = 0;
910    pst.dstProcId = DU_PROC;
911    pst.srcProcId = DU_PROC;
912    pst.region    = DU_APP_MEM_REGION;
913    pst.pool      = DU_POOL;
914    pst.event     = EVENT_MAC_UE_CREATE_REQ;
915
916    /* Copying ueCb to a sharable buffer */
917    DU_ALLOC_SHRABL_BUF(macUeCfg, sizeof(MacUeCfg));
918         if(macUeCfg)
919         {
920                 memset(macUeCfg, 0, sizeof(MacUeCfg));
921       memcpy(macUeCfg, &duCb.actvCellLst[cellId - 1]->ueCb[ueIdx].macUeCfg, sizeof(MacUeCfg));
922       /* Processing one Ue at a time to MAC */
923                 ret = (*packMacUeCreateReqOpts[pst.selector])(&pst, macUeCfg);
924                 if(ret)
925                 {
926          DU_LOG("\nDU_APP : Failure in sending Ue Create Req to MAC");
927                    DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, macUeCfg, sizeof(MacUeCfg));
928                         ret = RFAILED;
929                 }
930         }
931         else
932         {
933            DU_LOG("\n DU_APP: Memory alloc failed at duBuildAndSendUeCreateReq()");
934                 ret = RFAILED;
935         }
936         return ret;
937 }
938
939 /**********************************************************************
940          End of file
941 ***********************************************************************/