[Epic-ID: ODUHIGH-][Task-ID: ODUHIGH-]WG8 alignment | Cell conf
[o-du/l2.git] / src / du_app / du_cell_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
19 /* This file contains message handling functionality for DU cell management */
20 #include "common_def.h"
21 #include "lrg.h"
22 #include "legtp.h"
23 #include "lrg.x"
24 #include "lkw.x"
25 #include "rgr.h"
26 #include "rgr.x"
27 #include "du_app_mac_inf.h"
28 #include "du_app_rlc_inf.h"
29 #include "du_cfg.h"
30 #include "du_mgr.h"
31 #include "du_utils.h"
32 #include "du_cell_mgr.h"
33 #include "PCCH-Config.h"
34 #include "PagingUE-Identity.h"
35 #include "PagingRecord.h"
36 #include "PagingRecordList.h"
37 #include "PagingRrc.h"
38 #include "PCCH-MessageType.h"
39 #include "PCCH-Message.h"
40 #include "odu_common_codec.h"
41
42 #ifdef O1_ENABLE
43
44 #include "AlarmInterface.h"
45 #include "CmInterface.h"
46
47 #endif
48
49 DuMacCellDeleteReq packMacCellDeleteReqOpts[] =
50 {
51    packDuMacCellDeleteReq,       /* Loose coupling */
52    MacProcCellDeleteReq,         /* TIght coupling */
53    packDuMacCellDeleteReq        /* Light weight-loose coupling */
54 };
55
56 DuMacDlPcchInd packMacDlPcchIndOpts[] =
57 {
58    packDuMacDlPcchInd,       /* Loose coupling */
59    MacProcDlPcchInd,         /* TIght coupling */
60    packDuMacDlPcchInd        /* Light weight-loose coupling */
61 };
62
63 /*******************************************************************
64  *
65  * @brief Processes cells to be activated
66  *
67  * @details
68  *
69  *    Function : duProcCellsToBeActivated
70  *
71  *    Functionality:
72  *      - Processes cells to be activated list received in F1SetupRsp
73  *
74  * @params[in] void
75  * @return ROK     - success
76  *         RFAILED - failure
77  *
78  * ****************************************************************/
79 uint8_t duProcCellsToBeActivated(uint8_t *plmnStr, uint16_t nci, uint16_t nRPci)
80 {
81    uint8_t ret = ROK;
82    DuCellCb *cellCb = NULLP;
83    uint8_t cfgIdx, tmpPlmn[4];
84
85    for(cfgIdx=0; cfgIdx<duCb.numCfgCells; cfgIdx++)
86    {
87       memset(tmpPlmn, 0, 4);
88       buildPlmnId(duCb.cfgCellLst[cfgIdx]->cellInfo.nrEcgi.plmn, tmpPlmn);
89       if(duCb.cfgCellLst[cfgIdx]->cellInfo.nrEcgi.cellId == nci &&
90             (strcmp((const char*)tmpPlmn, (const char*)plmnStr) == 0))
91       {
92          cellCb = duCb.cfgCellLst[cfgIdx];
93          break;
94       }
95       else
96       {
97          DU_LOG("\nERROR  -->  DU APP : No Cell found for NCI %d", nci);
98          return RFAILED;
99       }
100    }
101
102    cellCb->cellStatus = ACTIVATION_IN_PROGRESS; 
103    cellCb->cellInfo.nrPci = nRPci;
104
105    if((cmHashListInit(&(cellCb->pagingInfoMap), MAX_SFN, sizeof(DuPagInfoList),\
106                 FALSE, CM_HASH_KEYTYPE_CONID, DU_APP_MEM_REGION, DU_POOL)) != ROK)
107    {
108       DU_LOG("ERROR  --> DU APP: Failed to Initialize the Paging Hash Map");
109       return RFAILED;             
110    }
111    duCb.actvCellLst[cellCb->cellId -1] = cellCb;
112    duCb.numActvCells++;
113
114    if(duBuildAndSendMacCellCfg(cellCb->cellId) != ROK)
115    {
116       DU_LOG("\nERROR  -->  DU APP : macCellCfg build and send failed");
117       /* Delete cell from actvCellList */
118       duCb.actvCellLst[cellCb->cellId -1] = NULLP;
119       --(duCb.numActvCells);
120       ret = RFAILED;
121    }
122    return ret;
123 }
124
125 /*******************************************************************
126  *
127  * @brief Handles DU F1Setup Rsp received in F1AP
128  *
129  * @details
130  *
131  *    Function : duProcF1SetupRsp
132  *
133  *    Functionality:
134  *      - Handles DU F1Setup Rsp received in F1AP
135  *
136  * @params[in] Pointer to F1SetupRsp 
137  * @return void
138  *
139  ******************************************************************/
140 void duProcF1SetupRsp()
141 {
142    DU_LOG("\nINFO   -->  DU_APP : F1 Setup Response received");
143    duCb.f1Status = TRUE; //Set F1 status as true
144 }
145
146 /*******************************************************************
147 *
148 * @brief Returns cellCb based on cell ID
149 *
150 * @details
151 *
152 *    Function : duGetCellCb
153 *
154 *    Functionality: Returns DU APP CellCb based on cell ID
155 *
156 * @params[in] F1AP_PDU_t ASN decoded F1AP message
157 * @return ROK     - success
158 *         RFAILED - failure
159 *
160 * ****************************************************************/
161 uint8_t duGetCellCb(uint16_t cellId, DuCellCb **cellCb)
162 {
163    uint8_t cellIdx = 0;
164
165    for(cellIdx=0; cellIdx < MAX_NUM_CELL; cellIdx++)
166    {
167       if(duCb.actvCellLst[cellIdx] && (duCb.actvCellLst[cellIdx]->cellId == cellId))
168       {
169          *cellCb = duCb.actvCellLst[cellIdx];
170               break;
171       }
172    }
173
174    if(!*cellCb)
175    {
176       DU_LOG("\nERROR  -->  DU APP : Cell Id %d not found in DU APP", cellId);
177       return RFAILED;
178    }
179
180    return ROK;
181 }
182
183 /******************************************************************************
184 * @brief Check Paging Record for a Particular Paging Frame
185 *
186 * @details
187 *
188 *   Function : checkPagingRecord
189 *
190 *   Functionality:
191 *           Check Paging Record for a Particular Paging Frame
192
193 *  @params[in] DuCellCb *cellCb
194 *
195 *  @return ROK/RFAILURE (uint8_t return)
196
197
198 ******************************************************************************/
199 uint8_t checkPagingRecord(DuCellCb *cellCb)
200 {
201    uint16_t pf = 0;
202    DuPagInfoList *pagInfoLLFromPF = NULLP;
203    DuPagUeList *pagInfo = NULLP;
204
205    /*DUAPP may send PagingReq to MAC for future SFN so that Schedular can 
206     * schedule Paging on the exact occurence of the Slot.*/
207    pf = (cellCb->currSlotInfo.sfn + PAGING_SCHED_DELTA) % MAX_SFN;
208
209    pagInfoLLFromPF = findPagingInfoFromMap(pf, &(cellCb->pagingInfoMap));
210    if(pagInfoLLFromPF == NULLP)
211    {
212       /*No Page is present for pf thus exiting*/
213       return ROK;
214    }
215   
216    do
217    {
218       pagInfo = handlePageInfoLL(pf, NULLD, &(pagInfoLLFromPF->pagInfoList), TRAVERSE_ALL);
219       if(pagInfo != NULLP)
220       {
221          if(BuildAndSendDlPcchIndToMac(cellCb->cellId, pf, pagInfo->i_s, &(pagInfo->pagUeList)) != ROK)
222          {
223             DU_LOG("\nERROR  -->  DU APP: Issue in Building Page RRC PDU i_s:%d",pagInfo->i_s);
224             return RFAILED; 
225          }
226          handlePageInfoLL(pf, pagInfo->i_s, &(pagInfoLLFromPF->pagInfoList), DELETE);
227       }
228       if(pagInfoLLFromPF->pagInfoList.first == NULLP)
229       {
230          break;
231       }
232    }while(pagInfo != NULLP);
233    
234    cmHashListDelete(&(cellCb->pagingInfoMap), (PTR)pagInfoLLFromPF);
235    DU_FREE(pagInfoLLFromPF, sizeof(DuPagInfoList));
236    return ROK;
237 }
238
239 /******************************************************************
240  *
241  * @brief Send pcch indication to MAC
242  *
243  * @details
244  *
245  *    Function : sendDlPcchIndToMac
246  *
247  *    Functionality: Send pcch indication to MAC
248  *
249  * @Params[in] DlPcchInd *pcchInd
250  * @return ROK     - success
251  *         RFAILED - failure
252  *
253  * ****************************************************************/
254
255 uint8_t sendDlPcchIndToMac(DlPcchInd *pcchInd)
256 {
257    uint8_t ret = ROK;
258    Pst pst;
259
260    if(pcchInd)
261    {
262       /* Fill Pst */
263       FILL_PST_DUAPP_TO_MAC(pst, EVENT_MAC_DL_PCCH_IND);
264       
265       ret = (*packMacDlPcchIndOpts[pst.selector])(&pst, pcchInd);
266       if(ret == RFAILED)
267       {
268          DU_LOG("\nERROR  -->  DU APP : sendDlPcchIndToMac(): Failed to DL PCCH indication to MAC");
269       }
270    }
271    else
272    {
273       DU_LOG("\nERROR  -->  DU APP: sendDlPcchIndToMac(): Received pcchInd is NULLP");
274       ret = RFAILED;
275    }
276    return ret;
277 }
278
279
280 /*****************************************************************
281 * @brief Handles slot indication from MAC
282 *
283 * @details
284 *
285 *   Function : duHandleSlotInd
286 *
287 *   Functionality:
288 *       Handles  slot indication from MAC
289
290 *  @params[in] Post structure pointer
291 *              SlotTimingInfo *slotIndInfo
292 *  @return ROK     - success
293 *          RFAILED - failure
294
295
296 *****************************************************************/
297 uint8_t duHandleSlotInd(Pst *pst, SlotTimingInfo *slotIndInfo)
298 {
299    uint8_t cellIdx = 0, ret = ROK;
300    DuCellCb *duCellCb = NULLP;
301
302    if(slotIndInfo)
303    {
304       GET_CELL_IDX(slotIndInfo->cellId, cellIdx);
305       duCellCb = duCb.actvCellLst[cellIdx];
306
307       if(duCellCb)
308       {
309          duCellCb->currSlotInfo.sfn = slotIndInfo->sfn;
310          duCellCb->currSlotInfo.slot = slotIndInfo->slot;
311          checkPagingRecord(duCellCb);
312       }
313       else
314       {
315          DU_LOG("\nERROR  -->  DU APP : CellId[%d] doesnot exist", slotIndInfo->cellId);
316          ret = RFAILED;
317       }
318       DU_FREE_SHRABL_BUF(pst->region, pst->pool, slotIndInfo, sizeof(SlotTimingInfo));
319    }
320    else
321    {
322       DU_LOG("\nERROR  -->  DU APP : Recevied null pointer from MAC");
323       ret = RFAILED;
324    }
325    return(ret);
326 }
327 /*******************************************************************
328  *
329  * @brief Handles cell up indication from MAC
330  *
331  * @details
332  *
333  *    Function : duHandleCellUpInd
334  *
335  *    Functionality:
336  *      Handles cell up indication from MAC
337  *
338  * @params[in] Post structure pointer
339  *             cell Up info
340  * @return ROK     - success
341  *         RFAILED - failure
342  *
343  * ****************************************************************/
344 uint8_t duHandleCellUpInd(Pst *pst, OduCellId *cellId)
345 {
346    DuCellCb *cellCb = NULLP; 
347
348    if(cellId->cellId <=0 || cellId->cellId > MAX_NUM_CELL)
349    {
350       DU_LOG("\nERROR  -->  DU APP : Invalid Cell Id %d in duHandleCellUpInd()", cellId->cellId);
351       return RFAILED;
352    }
353
354    if(duGetCellCb(cellId->cellId, &cellCb) != ROK)
355       return RFAILED;
356
357    if((cellCb != NULL) && (cellCb->cellStatus == ACTIVATION_IN_PROGRESS))
358    {
359       DU_LOG("\nINFO   -->  DU APP : 5G-NR Cell %d is UP", cellId->cellId);
360       cellCb->cellStatus = ACTIVATED;
361       gCellStatus = CELL_UP;
362
363       if(duCfgParam.tempSliceCfg.numOfRrmPolicy)
364          BuildAndSendSliceConfigReq();
365 #ifdef O1_ENABLE
366       DU_LOG("\nINFO   -->  DU APP : Raise cell UP alarm for cell id=%d", cellId->cellId);
367       raiseCellAlrm(CELL_UP_ALARM_ID, cellId->cellId);
368       setCellOpState(cellId->cellId, ENABLED, ACTIVE);
369 #endif
370        duCfgParam.macCellCfg.cellCfg.opState = OP_ENABLED;
371        duCfgParam.macCellCfg.cellCfg.cellState = CELL_ACTIVE;
372    }
373
374    if((pst->selector == ODU_SELECTOR_LWLC) || (pst->selector == ODU_SELECTOR_TC))
375       DU_FREE_SHRABL_BUF(pst->region, pst->pool, cellId, sizeof(OduCellId));
376    return ROK;
377 }
378
379 /*******************************************************************
380  *
381  * @brief Handle Cell delete response from MAC
382  *
383  * @details
384  *
385  *    Function : DuProcMacCellDeleteRsp
386  *
387  *    Functionality: Handle Cell delete response from MAC
388  *
389  * @params[in] Pointer to MacCellDeleteRsp and Pst
390  * @return ROK     - success
391  *         RFAILED - failure
392  *
393  * ****************************************************************/
394
395 uint8_t DuProcMacCellDeleteRsp(Pst *pst, MacCellDeleteRsp *deleteRsp)
396 {
397    uint8_t ret = ROK;
398    uint16_t cellIdx=0, pfIdx=0;
399    DuPagInfoList *pagInfoLLFromPF=NULLP;
400
401    if(deleteRsp)
402    {
403       if(deleteRsp->result == SUCCESSFUL_RSP)
404       {
405          GET_CELL_IDX(deleteRsp->cellId, cellIdx);
406          DU_LOG("\nINFO   -->  DU APP : MAC CELL Delete Response : SUCCESS [CELL IDX : %d]", deleteRsp->cellId);
407          if(duCb.actvCellLst[cellIdx] && (duCb.actvCellLst[cellIdx]->cellId == deleteRsp->cellId))
408          {
409             for(pfIdx =0; pfIdx < MAX_SFN; pfIdx++)
410             {
411                pagInfoLLFromPF = findPagingInfoFromMap(pfIdx, &(duCb.actvCellLst[cellIdx]->pagingInfoMap));
412                if(pagInfoLLFromPF)
413                {   
414                   cmHashListDelete(&(duCb.actvCellLst[cellIdx]->pagingInfoMap), (PTR)pagInfoLLFromPF);
415                   DU_FREE(pagInfoLLFromPF, sizeof(DuPagInfoList));
416                }
417             }
418
419             memset(duCb.actvCellLst[cellIdx], 0, sizeof(DuCellCb));
420             gCellStatus = CELL_DOWN;
421
422 #ifdef O1_ENABLE
423             DU_LOG("\nINFO   -->  DU APP : Raise cell down alarm for cell id=%d", deleteRsp->cellId);
424             raiseCellAlrm(CELL_DOWN_ALARM_ID, deleteRsp->cellId);
425             setCellOpState(deleteRsp->cellId, DISABLED, INACTIVE);
426 #endif
427
428             duCb.numActvCells--;
429             duCb.numCfgCells--;
430             DU_FREE(duCb.actvCellLst[cellIdx], sizeof(DuCellCb));
431
432          }
433          else
434          {
435             DU_LOG("\nERROR  -->  DU APP : DuProcMacCellDeleteRsp(): CellId [%d] doesnot exist", deleteRsp->cellId);
436             ret = RFAILED;
437          }
438       }
439       else
440       {
441          DU_LOG("\nERROR  -->  DU APP : DuProcMacCellDeleteRsp(): MAC CELL Delete Response : FAILED\
442          [CELL IDX : %d]", deleteRsp->cellId);
443          ret = RFAILED;
444       }
445       DU_FREE_SHRABL_BUF(pst->region, pst->pool, deleteRsp, sizeof(MacCellDeleteRsp));
446    }
447    else
448    {
449       DU_LOG("\nERROR  -->  DU APP : DuProcMacCellDeleteRsp(): Received MAC cell delete response is NULL");
450       ret = RFAILED;
451    }
452    return ret;
453 }
454
455 /*******************************************************************
456  *
457  * @brief Sending Cell Delete Req To Mac
458  *
459  * @details
460  *
461  *    Function : sendCellDeleteReqToMac
462  *
463  *    Functionality:
464  *     sending Cell Delete Req To Mac
465  *
466  *  @params[in]    uint16_t cellId
467  *  @return ROK     - success
468  *          RFAILED - failure
469  *
470  *
471  *****************************************************************/
472
473 uint8_t sendCellDeleteReqToMac(uint16_t cellId)
474 {
475    Pst pst;
476    uint8_t ret=ROK;
477    MacCellDeleteReq *cellDelete = NULLP;
478    
479    DU_ALLOC_SHRABL_BUF(cellDelete, sizeof(MacCellDeleteReq));
480    if(cellDelete)
481    {
482       cellDelete->cellId = cellId;
483       FILL_PST_DUAPP_TO_MAC(pst, EVENT_MAC_CELL_DELETE_REQ);
484
485       DU_LOG("\nINFO   -->  DU APP : Sending Cell Delete Request to MAC");  
486       /* Processing one Cell at a time to MAC */
487       ret = (*packMacCellDeleteReqOpts[pst.selector])(&pst, cellDelete);
488       if(ret == RFAILED)
489       {
490          DU_LOG("\nERROR  -->  DU APP : sendCellDeleteReqToMac(): Failed to send Cell delete Req to MAC");
491          DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, cellDelete, sizeof(MacCellDeleteReq));
492       }
493    }
494    else
495    {
496       DU_LOG("\nERROR  -->   DU APP : sendCellDeleteReqToMac(): Failed to allocate memory"); 
497       ret = RFAILED;
498    }
499    return ret;
500 }
501
502 /*******************************************************************
503  *
504  * @brief DU preocess Cell Delete Req to MAC 
505  *
506  * @details
507  *
508  *    Function : duSendCellDeletReq 
509  *
510  *    Functionality: DU process Cell Delete Req to MAC 
511  *
512  * @params[in] uint16_t cellId
513  * @return ROK     - success
514  *         RFAILED - failure
515  *
516  * ****************************************************************/
517
518 uint8_t duSendCellDeletReq(uint16_t cellId)
519 {
520    uint16_t cellIdx = 0;
521    DU_LOG("\nINFO   -->  DU APP : Processing Cell Delete Request ");
522    GET_CELL_IDX(cellId, cellIdx);
523
524    if(duCb.actvCellLst[cellIdx] == NULLP)
525    {
526       DU_LOG("\nERROR  -->  DU APP : duSendCellDeletReq(): CellId[%d] is not found", cellId);
527       return RFAILED;
528    }
529    
530    if(duCb.actvCellLst[cellIdx]->cellId != cellId)
531    {
532       DU_LOG("\nERROR  -->  DU APP : duSendCellDeletReq(): CellId[%d] is not found", cellId);
533       return RFAILED;
534
535    }  
536    
537    if(duCb.actvCellLst[cellIdx]->cellStatus != DELETION_IN_PROGRESS)
538    {
539       DU_LOG("\nERROR  -->  DU APP : duSendCellDeletReq(): CellStatus[%d] of cellId[%d] is not correct.\
540       Expected CellStatus is DELETION_IN_PROGRESS",duCb.actvCellLst[cellIdx]->cellStatus, cellId);
541       return RFAILED;  
542    }
543
544    if(duBuildAndSendMacCellStop(cellId) == RFAILED)
545    {
546       DU_LOG("\nERROR  -->  DU APP : duSendCellDeletReq(): Failed to build and send cell stop request to MAC for\
547             cellId[%d]",cellId);
548       return RFAILED;
549    }
550
551    return ROK;
552 }
553
554 /*******************************************************************
555  * @brief Free PCCH PDU structure
556  *
557  * @details
558  *
559  *    Function : freePcchPdu
560  *
561  *    Functionality:  Free PCCH PDU structure
562  *
563  * @params[in] PCCH_Message_t *pcchMsg 
564  *
565  * @return void
566  *
567  * ****************************************************************/
568 void freePcchPdu(PCCH_Message_t *pcchMsg)
569 {
570    PagingRrc_t *pagingMsg = NULLP;
571    uint8_t recordIdx = 0;
572
573    if(pcchMsg != NULLP)
574    {
575       if(pcchMsg->message.choice.c1  != NULLP)
576       {
577          pagingMsg = pcchMsg->message.choice.c1->choice.paging;
578          if(pagingMsg != NULLP)   
579          {
580             if(pagingMsg->pagingRecordList)
581             {
582                if(pagingMsg->pagingRecordList->list.array != NULLP)
583                {
584                   for(recordIdx = 0; recordIdx <  pagingMsg->pagingRecordList->list.count; recordIdx++)
585                   {
586                      if(pagingMsg->pagingRecordList->list.array[recordIdx] != NULLP)
587                      {
588                         DU_FREE(pagingMsg->pagingRecordList->list.array[recordIdx]->ue_Identity.choice.ng_5G_S_TMSI.buf,\
589                                  pagingMsg->pagingRecordList->list.array[recordIdx]->ue_Identity.choice.ng_5G_S_TMSI.size);
590                         DU_FREE(pagingMsg->pagingRecordList->list.array[recordIdx], sizeof(PagingRecord_t));
591                      }
592                   }
593                   DU_FREE(pagingMsg->pagingRecordList->list.array, pagingMsg->pagingRecordList->list.size);
594                }
595                DU_FREE(pagingMsg->pagingRecordList, sizeof(PagingRecordList_t));
596             }
597             DU_FREE(pcchMsg->message.choice.c1->choice.paging, sizeof(PagingRrc_t));
598          }
599          DU_FREE(pcchMsg->message.choice.c1 , sizeof(PCCH_MessageType_t)); 
600       }
601       DU_FREE(pcchMsg , sizeof(PCCH_Message_t));
602    }
603 }
604
605 /*******************************************************************
606  * @brief Builds the Pcch RRC PDU and forwards it to MAC
607  *
608  * @details
609  *
610  *    Function : BuildAndSendDlPcchIndToMac
611  *
612  *    Functionality: Builds the Pcch RRC PDU[As per Spec 38.331, Annexure A]
613  *                   and forwards it to MAC as Buffer along with PF and i_s
614  *
615  * @params[in] uint16_t cellId, uint16_t pf, uint8_t i_s,CmLListCp *pageUeLL
616  *
617  * @return ROK     - success
618  *         RFAILED - failure
619  *
620  * ****************************************************************/
621 uint8_t BuildAndSendDlPcchIndToMac(uint16_t cellId, uint16_t pf, uint8_t i_s, CmLListCp *pageUeLL)
622 {
623    CmLList        *node = NULLP, *next = NULLP;
624    DuPagUeRecord  *ueRecord = NULLP;
625    PCCH_Message_t *pcchMsg = NULLP;
626    asn_enc_rval_t encRetVal;
627    PagingRrc_t    *pagingMsg = NULLP;
628    DlPcchInd     *macPcchInd = NULLP;
629    uint8_t        recordIdx = 0, ret = RFAILED;
630    
631    /*As per 38.473 Sec 9.3.1.39,5G-S-TMSI :48 Bits >>  Bytes and 0 UnusedBits */
632    uint8_t         totalByteInTmsi = 6, unusedBitsInTmsi = 0;
633
634    if(pageUeLL == NULLP || pageUeLL->count == 0)
635    {
636       DU_LOG("\nERROR  -->  DU APP: UE Page Record LL is empty");
637       return RFAILED;
638    }
639
640    while(true)
641    {
642       memset(&encRetVal, 0, sizeof(asn_enc_rval_t));
643       DU_ALLOC(pcchMsg , sizeof(PCCH_Message_t));
644       if(pcchMsg == NULLP)
645       {
646          DU_LOG("\nERROR  -->  DU APP: BuildAndSendDlPcchIndToMac(): (pccchMsg) Memory Alloction failed!");
647          break;
648       }
649       pcchMsg->message.present = PCCH_MessageType_PR_c1;
650       DU_ALLOC(pcchMsg->message.choice.c1 , sizeof(PCCH_MessageType_t));
651       if(pcchMsg->message.choice.c1 == NULLP)
652       {
653          DU_LOG("\nERROR  -->  DU APP: BuildAndSendDlPcchIndToMac(); (C1) Memory Alloction failed!");
654          break;
655       }
656       pcchMsg->message.choice.c1->present = PCCH_MessageType__c1_PR_paging;
657       DU_ALLOC(pcchMsg->message.choice.c1->choice.paging, sizeof(PagingRrc_t));
658
659       pagingMsg = pcchMsg->message.choice.c1->choice.paging;
660       if(pagingMsg == NULLP)
661       {
662          DU_LOG("\nERROR  -->  DU APP: BuildAndSendDlPcchIndToMac(); (Paging) Memory Alloction failed!");
663          break;
664       }
665       DU_ALLOC(pagingMsg->pagingRecordList, sizeof(PagingRecordList_t));
666       if(pagingMsg->pagingRecordList == NULLP)
667       {
668          DU_LOG("\nERROR  -->  DU APP: BuildAndSendDlPcchIndToMac(); (Paging Record List) Memory Alloction failed!");
669          break;
670       }
671
672       pagingMsg->pagingRecordList->list.count = pageUeLL->count;
673
674        /*As per Spec 38.331, maxNrofPageRec : 32 [Maximum number of page records]*/
675       if(pageUeLL->count > MAX_PAGING_UE_RECORDS)
676       {
677          pagingMsg->pagingRecordList->list.count = MAX_PAGING_UE_RECORDS;
678       }
679
680       pagingMsg->pagingRecordList->list.size = pageUeLL->count * (sizeof(PagingRecord_t *));
681       DU_ALLOC(pagingMsg->pagingRecordList->list.array, pagingMsg->pagingRecordList->list.size);
682       if(pagingMsg->pagingRecordList->list.array == NULLP)
683       {
684          DU_LOG("\nERROR  -->  DU APP: BuildAndSendDlPcchIndToMac(); (Array) Memory Alloction failed!");
685          break;
686       }
687       for(recordIdx = 0; recordIdx < pageUeLL->count; recordIdx++)
688       {
689          DU_ALLOC(pagingMsg->pagingRecordList->list.array[recordIdx], sizeof(PagingRecord_t));
690          if(pagingMsg->pagingRecordList->list.array[recordIdx] == NULLP)
691          {
692             DU_LOG("\nERROR  -->  DU APP: BuildAndSendDlPcchIndToMac(); (Record) Memory Alloction failed!");
693             break;
694          }
695       }
696       recordIdx = 0;
697       node = pageUeLL->first;
698       while(node && (recordIdx < MAX_PAGING_UE_RECORDS))
699       {
700          next = node->next;
701          ueRecord = (DuPagUeRecord *)node->node;
702          if(ueRecord)
703          {
704             /*TODO : When Connected Mode Paging will be supported then I_RNTI will be introduced*/
705             pagingMsg->pagingRecordList->list.array[recordIdx]->ue_Identity.present = PagingUE_Identity_PR_ng_5G_S_TMSI;
706             pagingMsg->pagingRecordList->list.array[recordIdx]->ue_Identity.choice.ng_5G_S_TMSI.size = totalByteInTmsi*sizeof(uint8_t);
707             DU_ALLOC(pagingMsg->pagingRecordList->list.array[recordIdx]->ue_Identity.choice.ng_5G_S_TMSI.buf,\
708                   pagingMsg->pagingRecordList->list.array[recordIdx]->ue_Identity.choice.ng_5G_S_TMSI.size);
709             if(pagingMsg->pagingRecordList->list.array[recordIdx]->ue_Identity.choice.ng_5G_S_TMSI.buf == NULLP)
710             {
711                DU_LOG("\nERROR  -->  DU APP: BuildAndSendDlPcchIndToMac(); (5gsTmsi buffer) Memory Allocation failed!");
712                break;
713             }
714             fillBitString(&pagingMsg->pagingRecordList->list.array[recordIdx]->ue_Identity.choice.ng_5G_S_TMSI,\
715                   unusedBitsInTmsi, totalByteInTmsi, ueRecord->sTmsi);
716             recordIdx++;
717          }
718
719          if(duDelNodeFromLList(pageUeLL, node) == ROK)
720              DU_FREE(ueRecord, sizeof(DuPagUeRecord));
721          node = next;
722       }
723       if(node != NULLP && (recordIdx < MAX_PAGING_UE_RECORDS))
724       {
725          /*This leg is hit because Whole UE REcord List was not traversed as
726           * some issue happened thus exiting the main while*/
727          break;
728       }
729       xer_fprint(stdout, &asn_DEF_PCCH_Message, pcchMsg);
730       memset(encBuf, 0, ENC_BUF_MAX_LEN);
731       encBufSize = 0;
732        /* Encode the PCCH RRC PDU as APER */
733       encRetVal = uper_encode(&asn_DEF_PCCH_Message, 0, pcchMsg, PrepFinalEncBuf,\
734             encBuf);
735
736       if(encRetVal.encoded == ENCODE_FAIL)
737       {
738          DU_LOG("\nERROR  -->  F1AP : Could not encode Paging structure (at %s)\n",\
739                encRetVal.failed_type ? encRetVal.failed_type->name : "unknown");
740          break;
741       }
742       else
743       {
744          DU_LOG("\nDEBUG  -->  F1AP : Created APER encoded buffer for RRC PDU for Pcch indication \n");
745          
746          DU_ALLOC_SHRABL_BUF(macPcchInd, sizeof(DlPcchInd));
747          if(macPcchInd == NULLP)
748          {
749             DU_LOG("\nERROR  -->  DU APP: BuildAndSendDlPcchIndToMac(); (macPcchInd) Memory Alloction failed!");
750             break;
751          }
752          
753          macPcchInd->cellId = cellId;
754          macPcchInd->pf = pf;
755          macPcchInd->i_s = i_s;
756          macPcchInd->pduLen = encBufSize;
757          DU_ALLOC_SHRABL_BUF(macPcchInd->pcchPdu, macPcchInd->pduLen);
758          if(macPcchInd->pcchPdu == NULLP)
759          {
760             DU_LOG("\nERROR  -->  DU APP: BuildAndSendDlPcchIndToMac(); (PcchPDU) Memory Alloction failed!");
761             break;
762          }
763          memcpy(macPcchInd->pcchPdu, encBuf, macPcchInd->pduLen);
764          ret = sendDlPcchIndToMac(macPcchInd);
765          if(ret != ROK)
766          {
767             DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, macPcchInd->pcchPdu, macPcchInd->pduLen);
768             DU_FREE_SHRABL_BUF(DU_APP_MEM_REGION, DU_POOL, macPcchInd, sizeof(DlPcchInd));
769             break;
770          }
771       }
772       ret = ROK;
773       break;
774    }
775    freePcchPdu(pcchMsg);
776    return ret;
777 }
778
779 /*******************************************************************
780  * @brief Keeping the record of Paging having a particular SFN and index associated 
781  *
782  * @details
783  *
784  *    Function : insertPagingRecord 
785  *
786  *    Functionality:  Insert record of Paging assoc with particular SFN and index associated 
787  *
788  * @params[in] DuCellCb* cellCb, uint16_t iterations
789  *             DuPagingMsg  rcvdF1apPagingParam
790  *
791  * @return ROK     - success
792  *         RFAILED - failure
793  *
794  * ****************************************************************/
795 uint8_t insertPagingRecord(DuCellCb* cellCb, DuPagingMsg *rcvdF1apPagingParam, uint16_t iterations)
796 {
797    uint16_t maxIterations = 0; 
798    DuPagInfoList *pagInfoLLFromPF = NULLP;
799    DuPagUeList  *pageUeLL = NULLP;
800    DuPagUeRecord *ueRecord = NULLP;
801
802 #if 0
803    printPageList(&(cellCb->pagingInfoMap));
804 #endif
805
806    /*MAX Iteration : A UE can be paged at every T frames thus MAX determines
807     *how many Paging Frame(s) can be considered for Paging this UE*/
808    maxIterations = MAX_SFN/rcvdF1apPagingParam->T;
809    if(iterations >= maxIterations)
810    {
811       DU_LOG("\nERROR  --> DU APP: MAX Iterations reached for UEID:%d, thus Paging is dropped!", rcvdF1apPagingParam->pagUeId);
812       return RFAILED;
813    }
814
815    /*[Step1]: Extracting the PF from the HasMap between PF and PagingInfoList*/
816    pagInfoLLFromPF = findPagingInfoFromMap(rcvdF1apPagingParam->pagingFrame, &(cellCb->pagingInfoMap));
817    if(pagInfoLLFromPF != NULLP)
818    {
819       /*[Step2]: Extracting the PageInfo List for the Paging Indices(i_s)*/
820       pageUeLL = handlePageInfoLL(rcvdF1apPagingParam->pagingFrame, rcvdF1apPagingParam->i_s, \
821             &(pagInfoLLFromPF->pagInfoList), CREATE);
822
823       if(pageUeLL != NULLP)
824       {
825          /*[Step3]: Check whether MAX UE Record against this PF and i_s has reached(Spec 38.331, Annexure A)*/
826          if(pageUeLL->pagUeList.count < MAX_PAGING_UE_RECORDS)
827          {
828             /*[Step4]: Insert the Paging Record to the end of the UE record List*/
829             ueRecord = handlePageUeLL(rcvdF1apPagingParam->pagUeId, rcvdF1apPagingParam->sTmsi,\
830                   &(pageUeLL->pagUeList), CREATE);
831
832             if(ueRecord == NULLP)
833             {
834                DU_LOG("\nERROR  --> DU APP: Unable to create UE Record in PagingList");
835                return RFAILED;
836             }
837             DU_LOG("\nDEBUG  --> DU APP: UE Record created successfully in PagingList");
838             return ROK;
839          }
840          else
841          {
842             /*Since MAX Page record has reached for this PF thus calculating and
843              *moving this UE to next Paging Cycle*/
844             DU_LOG("\nINFO   --> DU APP: Max Page Record reached for PagingFrame:%d",rcvdF1apPagingParam->pagingFrame);
845             rcvdF1apPagingParam->pagingFrame = ((rcvdF1apPagingParam->pagingFrame + rcvdF1apPagingParam->T) % MAX_SFN);
846             iterations++;
847
848             return insertPagingRecord(cellCb, rcvdF1apPagingParam, iterations);
849          }
850       }
851    }
852    /*Reaching here means that PF has no entry in the PageLists,
853     *Thus creating, Updating and inseritng the Paging Map, List and UE record.*/
854
855    DU_ALLOC(pagInfoLLFromPF, sizeof(DuPagInfoList));
856
857    if(pagInfoLLFromPF == NULLP)
858    {
859       DU_LOG("\nERROR  --> DU APP: PageInfo Map allocation failed.");
860       return RFAILED;
861    }
862    pagInfoLLFromPF->pf = rcvdF1apPagingParam->pagingFrame;
863    pageUeLL = handlePageInfoLL(rcvdF1apPagingParam->pagingFrame, rcvdF1apPagingParam->i_s, &(pagInfoLLFromPF->pagInfoList), CREATE);
864    ueRecord = handlePageUeLL(rcvdF1apPagingParam->pagUeId, rcvdF1apPagingParam->sTmsi, &(pageUeLL->pagUeList), CREATE);
865    if(cmHashListInsert(&(cellCb->pagingInfoMap), (PTR)pagInfoLLFromPF, (uint8_t *)&(pagInfoLLFromPF->pf), sizeof(uint16_t)) == RFAILED)
866    {
867       DU_LOG("\nERROR  --> DU APP: Hash Map Insertion Failed for PF:%d.",rcvdF1apPagingParam->pagingFrame);
868    }
869
870 #if 0
871    printPageList(&(cellCb->pagingInfoMap));
872 #endif
873
874    return ROK;
875
876
877 }
878
879 /*******************************************************************
880  * @brief Calculate and fill paging information of a UE belongs to a particular cell
881  *
882  * @details
883  *
884  *    Function : calcAndFillPagingInfoInCellCb
885  *
886  *    Functionality: Calculate PO and i_s and 
887  *                   fill paging information of a UE in DuCellCb
888  *
889  * @params[in] DuCellCb* cellCb, uint8_t pagUeId, 
890  *             DuPagingMsg  rcvdF1apPagingParam
891  *
892  * @return ROK     - success
893  *         RFAILED - failure
894  *
895  * ****************************************************************/
896 uint8_t calcAndFillPagingInfoInCellCb(DuCellCb* cellCb, DuPagingMsg *rcvdF1apPagingParam)
897 {
898    uint8_t ns = 0;
899    uint16_t T=0, N=0, pagingFrame = 0, n = 0;
900    uint16_t currentSfn = 0, sfn = 0, newSfn = 0;
901    PcchCfg   duPcchCfg;
902
903    if(cellCb)
904    {
905       /* calculate paging frame and paging offset */
906       duPcchCfg = duCfgParam.sib1Params.srvCellCfgCommSib.dlCfg.pcchCfg;
907       rcvdF1apPagingParam->pagingFrameOffset = duPcchCfg.pageFrameOffset;
908       ns = duPcchCfg.ns;      
909
910       /*
911        * Fill the Value of T (DRX cycle of the UE)
912        * T = min(UE Specific DRX value allocated by upper layers, default DRX
913        * broadcast in System Information) */
914       if((rcvdF1apPagingParam->pagingDrxPres) && (duPcchCfg.dfltPagingCycle > rcvdF1apPagingParam->pagingDrx))
915       {
916          T = rcvdF1apPagingParam->pagingDrx;
917       } 
918       else
919       {
920          T = duPcchCfg.dfltPagingCycle;
921       }
922       rcvdF1apPagingParam->T = T;
923
924        /* N= number of total paging frames in T */
925
926       switch(duPcchCfg.nAndPagingFrmOffsetType)
927       {
928          case PCCH_Config__nAndPagingFrameOffset_PR_oneT:
929             N = T;
930             break;
931          case PCCH_Config__nAndPagingFrameOffset_PR_halfT:
932             N = T/2;
933             break;
934          case PCCH_Config__nAndPagingFrameOffset_PR_quarterT:
935             N = T/4;
936             break;
937          case PCCH_Config__nAndPagingFrameOffset_PR_oneEighthT:
938             N = T/8;
939             break;
940          case PCCH_Config__nAndPagingFrameOffset_PR_oneSixteenthT:
941             N = T/16;
942             break;
943          default:
944             N = T;
945             break;
946       }
947
948        /* calculate the Value of pagingFrame */
949       /*Refer: 38.304 Sec 7.1: (SFN + PF_offset) mod T = (T div N)*(UE_ID mod N)*/
950       //RHS of above formula
951       pagingFrame = (T / N) * ((rcvdF1apPagingParam->pagUeId) % N);
952
953       //LHS of above formula
954       if(pagingFrame)
955       {
956          pagingFrame = (pagingFrame - rcvdF1apPagingParam->pagingFrameOffset)%T;
957       }
958       else /*Paging Frame = 0 so thus PF will be calculated on Paging Cycle*/
959       {
960          pagingFrame = (T - rcvdF1apPagingParam->pagingFrameOffset)%T;
961       }
962
963       /*Paging Frame(SFN for Paging) has to be determined from current SFN. */
964       /*For eg: If T =128, PF(Calculated above) = 20; SFN can be 20,148,276,...
965        * If currSFN is running as 200 then (newSFN % T) == (T/N)*(UE_ID%N)
966        *Thus SFN at which paging has to be sent needs to be delayed and newSFN = 276*/
967
968       currentSfn = cellCb->currSlotInfo.sfn;
969
970       /*Multiplication Factor(x) to find the next best SFN to process paging*/
971
972       /*Below calculation will determine in which nth cycle of T (DRX cycle),new PF
973        * may fall(Delayed one)*/
974       if(currentSfn > pagingFrame)
975       {
976          n  = ((currentSfn - pagingFrame) / T) + 1;
977       }
978       else
979       {
980          n  = ((pagingFrame - currentSfn) / T) + 1;
981       }
982
983       newSfn = pagingFrame + T * n;
984
985       /*When pagingFrame is future from currSFN then pagingFrame will be used 
986        * else pagingFrame is delayed thus newSFN will be used.*/
987       if(currentSfn <= pagingFrame)
988       {
989          if(pagingFrame > currentSfn + PAGING_SCHED_DELTA)
990          {
991             sfn = pagingFrame;
992          }
993          else
994          {
995             sfn = newSfn;
996          }
997       }
998       else
999       {
1000          
1001          if(newSfn > currentSfn + PAGING_SCHED_DELTA)
1002          {
1003             sfn = newSfn;
1004          }
1005          else /*If newSFN is near to currSFN then delay it more by T*/
1006          {
1007             newSfn = newSfn + T;
1008             sfn = newSfn;
1009          }
1010       }
1011       rcvdF1apPagingParam->pagingFrame =  (sfn % MAX_SFN);
1012       rcvdF1apPagingParam->i_s = ((uint32_t)(floor(rcvdF1apPagingParam->pagUeId / N)) % ns);
1013
1014       memcpy(&cellCb->tmpPagingInfoOfUe, rcvdF1apPagingParam, sizeof(DuPagingMsg));
1015    }
1016    else
1017    {
1018       DU_LOG("\nINFO  --> DU APP : calcAndFillPagingInfoInCellCb(): Received null pointer");
1019       return RFAILED;
1020    }
1021    return ROK;
1022 }
1023
1024 /*******************************************************************
1025  * @brief Paging Processing on a particular cell 
1026  *
1027  * @details
1028  *
1029  *    Function : processPagingMsg
1030  *
1031  *    Functionality: Process Paging on a particular cell 
1032  *
1033  * @params[in] uint16_t cellId
1034  *             DuPagingMsg  rcvdF1apPagingParam
1035  *
1036  * @return ROK     - success
1037  *         RFAILED - failure
1038  *
1039  * ****************************************************************/
1040 uint8_t processPagingMsg(uint16_t cellId, DuPagingMsg *rcvdF1apPagingParam)
1041 {
1042    uint16_t cellIdx = 0, iteration = 0;
1043
1044    GET_CELL_IDX(cellId, cellIdx);
1045
1046    if(duCb.actvCellLst[cellIdx] == NULLP || duCb.actvCellLst[cellIdx]->cellId != cellId)
1047    {
1048       DU_LOG("\nERROR  -->  DU APP : processPagingMsg(): CellId[%d] is not found", cellId);
1049       return RFAILED;
1050    }
1051    
1052    if(calcAndFillPagingInfoInCellCb(duCb.actvCellLst[cellIdx], rcvdF1apPagingParam) != ROK)
1053    {
1054       DU_LOG("\nERROR  --> DU APP : CellCb:%d not present to fill UE Paging Information",cellId);
1055       return RFAILED;
1056    }
1057    if(insertPagingRecord(duCb.actvCellLst[cellIdx], rcvdF1apPagingParam, iteration) != ROK)
1058    {
1059       DU_LOG("\nERROR  --> DU APP : Insertion Failed ofUE Paging Information");
1060       return RFAILED;
1061    }
1062    return ROK;
1063
1064 }
1065 /**********************************************************************
1066   End of file
1067  **********************************************************************/