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