[Epic-ID: ODUHIGH-516][Task-ID: ODUHIGH-523] Statistics Indication between DU APP...
[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_DL_TOTAL_PRB_USAGE_TMR:
41       {
42          if(((TotalPrbUsage *)cb)->periodTimer.tmrEvnt == EVENT_DL_TOTAL_PRB_USAGE_TMR)
43          {
44              DU_LOG("\nDEBUG  -->  SCH : schChkTmr: Timer Evnt [%d] already running", tmrEvnt);
45              return TRUE;
46          }
47          break;
48       }
49
50       case EVENT_UL_TOTAL_PRB_USAGE_TMR:
51       {
52          if(((TotalPrbUsage *)cb)->periodTimer.tmrEvnt == EVENT_UL_TOTAL_PRB_USAGE_TMR)
53          {
54              DU_LOG("\nDEBUG  -->  SCH : schChkTmr: Timer Evnt [%d] already running", tmrEvnt);
55              return TRUE;
56          }
57          break;
58       }
59
60       default:
61       {
62          DU_LOG("\nERROR  -->  SCH : schChkTmr: Invalid tmr Evnt [%d]", tmrEvnt);
63       }
64    }
65
66    return FALSE;
67 }
68
69 /**
70  * @brief Handler to start timer
71  *
72  * @param[in] cb        Control block depending on the type of the timer event.
73  * @param[in] tmrEvnt   Timer event to be started
74  *
75  * @return  Void
76 */
77 void schStartTmr(SchCb *gCb, PTR cb, int16_t tmrEvnt, uint8_t timerValue)
78 {
79    TotalPrbUsage *dlTotalPrbUsage;
80    TotalPrbUsage *ulTotalPrbUsage;
81    CmTmrArg arg;
82
83    arg.wait = 0;
84
85 #ifdef DEBUG_PRINT
86    DU_LOG("\nDEBUG   -->  SCH : Starting Timer Event [%d] with Wait Time [%d] ms", \
87       tmrEvnt, timerValue);
88 #endif      
89    
90    switch (tmrEvnt)
91    {
92       case EVENT_DL_TOTAL_PRB_USAGE_TMR:
93       {
94          dlTotalPrbUsage = ((TotalPrbUsage *)cb);
95          TMR_CALCUATE_WAIT(arg.wait, timerValue, gCb->schTimersInfo.tmrRes);
96
97          arg.timers = &dlTotalPrbUsage->periodTimer;
98          arg.max = MAX_TOTAL_PRB_USAGE_TMR;
99          break;
100       }
101
102       case EVENT_UL_TOTAL_PRB_USAGE_TMR:
103       {
104          ulTotalPrbUsage = ((TotalPrbUsage *)cb);
105          TMR_CALCUATE_WAIT(arg.wait, timerValue, gCb->schTimersInfo.tmrRes);
106
107          arg.timers = &ulTotalPrbUsage->periodTimer;
108          arg.max = MAX_TOTAL_PRB_USAGE_TMR;
109          break;
110       }
111       default:
112       {
113          DU_LOG("\nERROR  -->  SCH : schStartTmr: Invalid tmr Evnt [%d]", tmrEvnt);
114       }
115    }
116
117    if(arg.wait != 0)
118    {
119       arg.tqCp   = &(gCb->schTimersInfo.tmrTqCp);
120       arg.tq     = gCb->schTimersInfo.tmrTq;
121       arg.cb     = cb;
122       arg.evnt   = tmrEvnt;
123       arg.tNum   = 0;
124
125       cmPlcCbTq(&arg);
126    }
127
128    return;
129 }
130
131 /**
132  * @brief Handler to stop a timer
133  *
134  * @param[in] cb        Control block depending on the type of the timer event.
135  * @param[in] tmrType   Timer event to be started
136  *
137  * @return  Void
138 */
139 void schStopTmr(SchCb *gCb, PTR cb, uint8_t tmrType)
140 {
141    CmTmrArg   arg;
142
143    arg.timers = NULLP;
144
145 #ifdef DEBUG_PRINT
146    DU_LOG("\nDEBUG   -->  SCH : Stopping Timer Event [%d]", tmrType);
147 #endif   
148
149    switch (tmrType)
150    {
151       case EVENT_DL_TOTAL_PRB_USAGE_TMR:
152          {
153             arg.timers  = &((TotalPrbUsage *)cb)->periodTimer;
154             arg.max = MAX_TOTAL_PRB_USAGE_TMR;
155             break;
156          }
157          case EVENT_UL_TOTAL_PRB_USAGE_TMR:
158          {
159             arg.timers  = &((TotalPrbUsage *)cb)->periodTimer;
160             arg.max = MAX_TOTAL_PRB_USAGE_TMR;
161             break;
162          }
163
164       default:
165       {
166          DU_LOG("\nERROR  -->  SCH : schStopTmr: Invalid tmr Evnt[%d]", tmrType);
167          break;
168       }
169    }
170
171    if (tmrType != TMR0)
172    {
173       arg.tqCp   = &gCb->schTimersInfo.tmrTqCp;
174       arg.tq     = gCb->schTimersInfo.tmrTq;
175       arg.cb     = cb;
176       arg.evnt   = tmrType;
177       arg.wait   = 0;
178       arg.tNum   = 0;
179       cmRmvCbTq(&arg);
180    }
181
182    return;
183 }
184
185 /**
186  * @brief Handler to process Timer expiry of DL Total PRB Usage calculation 
187  *
188  * @param[in] cb        Control block depending on the type of the timer event.
189  * @param[in] tmrEvnt   Timer event to be started
190  *
191  * @return  Bool indicating whether the timer is running or not
192  *      -# ROK
193  *      -# RFAILED
194 */
195 uint8_t SchProcDlTotalPrbUsageTmrExp(TotalPrbUsage *dlTotalPrbUsage)
196 {
197    double percentageOfTotalPrbUsed = 0;
198
199    if(dlTotalPrbUsage->totalPrbAvailForTx)
200       percentageOfTotalPrbUsed = ((100.0 * dlTotalPrbUsage->numPrbUsedForTx) / dlTotalPrbUsage->totalPrbAvailForTx);
201    SchSendStatsIndToMac(dlTotalPrbUsage->schInst, SCH_DL_TOTAL_PRB_USAGE, percentageOfTotalPrbUsed);
202    
203    /* Restart Timer */
204    dlTotalPrbUsage->numPrbUsedForTx = 0;
205    dlTotalPrbUsage->totalPrbAvailForTx = 0;
206    schStartTmr(&schCb[dlTotalPrbUsage->schInst], (PTR)(dlTotalPrbUsage), EVENT_DL_TOTAL_PRB_USAGE_TMR, \
207       dlTotalPrbUsage->periodicity);
208
209    return ROK;
210 }
211
212 /**
213  * @brief Handler to check if the timer is running
214  *
215  * @param[in] cb        Control block depending on the type of the timer event.
216  * @param[in] tmrEvnt   Timer event to be started
217  *
218  * @return  Bool indicating whether the timer is running or not
219  *      -# ROK
220  *      -# RFAILED
221 */
222 uint8_t SchProcUlTotalPrbUsageTmrExp(TotalPrbUsage *ulTotalPrbUsage)
223 {
224    double percentageOfTotalPrbUsed = 0;
225
226    if(ulTotalPrbUsage->totalPrbAvailForTx)
227       percentageOfTotalPrbUsed = ((100.0 * ulTotalPrbUsage->numPrbUsedForTx) / ulTotalPrbUsage->totalPrbAvailForTx);
228    SchSendStatsIndToMac(ulTotalPrbUsage->schInst, SCH_UL_TOTAL_PRB_USAGE, percentageOfTotalPrbUsed);
229
230    /* Restart Timer */
231    ulTotalPrbUsage->numPrbUsedForTx = 0;
232    ulTotalPrbUsage->totalPrbAvailForTx = 0;
233    schStartTmr(&schCb[ulTotalPrbUsage->schInst], (PTR)(ulTotalPrbUsage), EVENT_UL_TOTAL_PRB_USAGE_TMR, \
234       ulTotalPrbUsage->periodicity);
235
236    return ROK;
237 }
238
239 /**
240  * @brief Timer Expiry handler.
241  *
242  * @details
243  *
244  *     Function : schTmrExpiry
245  *
246  *     This is a callback function used as an input parameter to cmPrcTmr()
247  *     to check expiry of any timer. In this function, we are only concerned
248  *     about tmrEvnt=Bind timer.
249  *
250  *  @param[in]  PTR   cb,  Entry for which Timer expired
251  *  @param[in]  uint8_t   tmrEvnt, the Timer Event
252  *  @return  uint8_t
253  *      -# ROK
254  **/
255 uint8_t schTmrExpiry(PTR cb, uint8_t tmrEvnt)
256 {
257 #ifdef DEBUG_PRINT
258    DU_LOG("\nDEBUG   -->  SCH : Timer Expired. Event [%d]", tmrEvnt);
259 #endif
260
261    switch (tmrEvnt)
262    {
263       case EVENT_DL_TOTAL_PRB_USAGE_TMR:
264          {
265             SchProcDlTotalPrbUsageTmrExp((TotalPrbUsage*)cb);
266             break;
267          }
268       case EVENT_UL_TOTAL_PRB_USAGE_TMR:
269          {
270             SchProcUlTotalPrbUsageTmrExp((TotalPrbUsage*)cb);
271             break;
272          }
273       default:
274          {
275             DU_LOG("\nERROR  -->  DU : duStartTmr: Invalid tmr Evnt [%d]", tmrEvnt);
276             break;
277          }
278    }
279    return ROK;
280 }
281
282 /**
283  * @brief Scheduler instance timer call back function registered with system services.
284  *
285  * @details
286  *
287  *     Function :  schActvTmr
288  *
289  *     This function is invoked for every timer activation
290  *     period expiry. Note that SS_MT_TMR flag needs to be enabled for this
291  *     as isntId is needed.As part of SRegTmr call for scheduler instance
292  *     SS_MT_TMR flag needs to be enabled and schActvTmr needs to be given as
293  *     callback function
294  *
295  *  @return  short int
296  *      -# ROK
297  **/
298 short int schActvTmr(Ent ent,Inst inst)
299 {
300    Inst schInst = (inst  - SCH_INST_START);
301
302    /* Check if any timer in the scheduler instance has expired */
303    cmPrcTmr(&schCb[schInst].schTimersInfo.tmrTqCp, schCb[schInst].schTimersInfo.tmrTq, (PFV) schTmrExpiry);
304
305    return ROK;
306
307 } /* end of schActvTmr */
308
309 /**********************************************************************
310
311          End of file
312 **********************************************************************/
313