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