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