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