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