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