Adding new commiter to ODU-High repo
[o-du/l2.git] / src / 5gnrsch / sch_tmr.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 #include "common_def.h"
20 #include "lrg.h"
21 #include "lrg.x"
22 #include "mac_sch_interface.h"
23 #include "sch.h"
24 #include "sch_tmr.h"
25
26 /**
27  * @brief Handler to check if the timer is running
28  *
29  * @param[in] cb        Control block depending on the type of the timer event.
30  * @param[in] tmrEvnt   Timer event to be started
31  *
32  * @return  Bool indicating whether the timer is running or not
33  *      -# ROK
34  *      -# RFAILED
35 */
36 bool schChkTmr(PTR cb, int16_t tmrEvnt)
37 {
38    switch (tmrEvnt)
39    {
40       case EVENT_STATISTICS_TMR:
41          {
42             if(((SchStatsGrp *)cb)->periodTimer.tmrEvnt == EVENT_STATISTICS_TMR)
43             {
44                DU_LOG("\nDEBUG  -->  SCH : schChkTmr: Timer Evnt [%d] already running", tmrEvnt);
45                return TRUE;
46             }
47             break;
48          }
49       default:
50          {
51             DU_LOG("\nERROR  -->  SCH : schChkTmr: Invalid tmr Evnt [%d]", tmrEvnt);
52          }
53    }
54    return FALSE;
55 }
56
57 /**
58  * @brief Handler to start timer
59  *
60  * @param[in] cb        Control block depending on the type of the timer event.
61  * @param[in] tmrEvnt   Timer event to be started
62  *
63  * @return  Void
64 */
65 void schStartTmr(SchCb *gCb, PTR cb, int16_t tmrEvnt, uint16_t timerValue)
66 {
67    SchStatsGrp *statsGrp = NULLP;
68    CmTmrArg arg;
69
70    arg.wait = 0;
71
72 #ifdef DEBUG_PRINT
73    DU_LOG("\nDEBUG   -->  SCH : Starting Timer Event [%d] with Wait Time [%d] ms", \
74       tmrEvnt, timerValue);
75 #endif      
76    
77    switch (tmrEvnt)
78    {
79       case EVENT_STATISTICS_TMR:
80       {
81          statsGrp = ((SchStatsGrp *)cb);
82          TMR_CALCUATE_WAIT(arg.wait, timerValue, gCb->schTimersInfo.tmrRes);
83
84          arg.timers = &statsGrp->periodTimer;
85          arg.max = MAX_NUM_TMR_PER_STATS_GRP;
86          break;
87       }
88
89       default:
90       {
91          DU_LOG("\nERROR  -->  SCH : schStartTmr: Invalid tmr Evnt [%d]", tmrEvnt);
92       }
93    }
94
95    if(arg.wait != 0)
96    {
97       arg.tqCp   = &(gCb->schTimersInfo.tmrTqCp);
98       arg.tq     = gCb->schTimersInfo.tmrTq;
99       arg.cb     = cb;
100       arg.evnt   = tmrEvnt;
101       arg.tNum   = 0;
102
103       cmPlcCbTq(&arg);
104    }
105
106    return;
107 }
108
109 /**
110  * @brief Handler to stop a timer
111  *
112  * @param[in] cb        Control block depending on the type of the timer event.
113  * @param[in] tmrType   Timer event to be started
114  *
115  * @return  Void
116 */
117 void schStopTmr(SchCb *gCb, PTR cb, uint8_t tmrType)
118 {
119    CmTmrArg   arg;
120
121    arg.timers = NULLP;
122
123 #ifdef DEBUG_PRINT
124    DU_LOG("\nDEBUG   -->  SCH : Stopping Timer Event [%d]", tmrType);
125 #endif   
126
127    switch (tmrType)
128    {
129       case EVENT_STATISTICS_TMR:
130          {
131             arg.timers  = &((SchStatsGrp *)cb)->periodTimer;
132             arg.max = MAX_NUM_TMR_PER_STATS_GRP;
133             break;
134          }
135
136       default:
137       {
138          DU_LOG("\nERROR  -->  SCH : schStopTmr: Invalid tmr Evnt[%d]", tmrType);
139          break;
140       }
141    }
142
143    if (tmrType != TMR0)
144    {
145       arg.tqCp   = &gCb->schTimersInfo.tmrTqCp;
146       arg.tq     = gCb->schTimersInfo.tmrTq;
147       arg.cb     = cb;
148       arg.evnt   = tmrType;
149       arg.wait   = 0;
150       arg.tNum   = 0;
151       cmRmvCbTq(&arg);
152    }
153
154    return;
155 }
156
157 /**
158  * @brief Handler for Statistics group timer expiry
159  *
160  * @details
161  *
162  *     Function : SchProcStatisticsGrpTmrExp
163  *
164  *     This function calculates and sends statistics of
165  *     the stats-group for which timer expired.
166  *     Once Statistics Indication is sent, timer for this
167  *     group is restarted.
168  *
169  *  @param[in]  Statistics group control block
170  *  @return  uint8_t
171  *      -# ROK
172  **/
173 uint8_t SchProcStatisticsGrpTmrExp(SchStatsGrp *cb)
174 {
175    if(schCalcAndSendGrpStats(cb) != ROK)
176    {
177       DU_LOG("\nERROR  -->  SCH : SchProcStatisticsGrpTmrExp: Fails to send group statistics");
178    }
179    schStartTmr(&schCb[cb->schInst], (PTR)(cb), EVENT_STATISTICS_TMR, cb->periodicity);
180    return ROK;
181 }
182
183 /**
184  * @brief Timer Expiry handler.
185  *
186  * @details
187  *
188  *     Function : schTmrExpiry
189  *
190  *     This is a callback function used as an input parameter to cmPrcTmr()
191  *     to check expiry of any timer. In this function, we are only concerned
192  *     about tmrEvnt=Bind timer.
193  *
194  *  @param[in]  PTR   cb,  Entry for which Timer expired
195  *  @param[in]  uint8_t   tmrEvnt, the Timer Event
196  *  @return  uint8_t
197  *      -# ROK
198  **/
199 uint8_t schTmrExpiry(PTR cb, uint8_t tmrEvnt)
200 {
201 #ifdef DEBUG_PRINT
202    DU_LOG("\nDEBUG   -->  SCH : Timer Expired. Event [%d]", tmrEvnt);
203 #endif
204
205    switch (tmrEvnt)
206    {
207       case EVENT_STATISTICS_TMR:
208          {
209 #ifdef DEBUG_PRINT
210             DU_LOG("\nDEBUG   -->  SCH : Statistics Timer Expired for Subscription Id [%ld] GroupId [%d]", \
211                   ((SchStatsGrp*)cb)->subscriptionId, ((SchStatsGrp*)cb)->groupId);
212 #endif
213             SchProcStatisticsGrpTmrExp((SchStatsGrp*)cb);
214             break;
215          }
216
217       default:
218          {
219             DU_LOG("\nERROR  -->  DU : duStartTmr: Invalid tmr Evnt [%d]", tmrEvnt);
220             break;
221          }
222    }
223    return ROK;
224 }
225
226 /**
227  * @brief Scheduler instance timer call back function registered with system services.
228  *
229  * @details
230  *
231  *     Function :  schActvTmr
232  *
233  *     This function is invoked for every timer activation
234  *     period expiry. Note that SS_MT_TMR flag needs to be enabled for this
235  *     as isntId is needed.As part of SRegTmr call for scheduler instance
236  *     SS_MT_TMR flag needs to be enabled and schActvTmr needs to be given as
237  *     callback function
238  *
239  *  @return  short int
240  *      -# ROK
241  **/
242 short int schActvTmr(Ent ent,Inst inst)
243 {
244    Inst schInst = (inst  - SCH_INST_START);
245
246    /* Check if any timer in the scheduler instance has expired */
247    cmPrcTmr(&schCb[schInst].schTimersInfo.tmrTqCp, schCb[schInst].schTimersInfo.tmrTq, (PFV) schTmrExpiry);
248
249    return ROK;
250
251 } /* end of schActvTmr */
252
253 /**********************************************************************
254
255          End of file
256 **********************************************************************/
257