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