-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathtimerlib.h
144 lines (115 loc) · 2.99 KB
/
timerlib.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* =====================================================================================
*
* Filename: timerlib.h
*
* Description: This file is a wrapper over Timer POSIX Timer library
*
* Version: 1.0
* Created: 10/12/2020 01:47:16 PM
* Revision: none
* Compiler: gcc
*
* Author: ABHISHEK SAGAR (), [email protected]
* Organization: Juniper Networks
*
* =====================================================================================
*/
#ifndef __TIMER_WRAP__
#define __TIMER_WRAP__
#include <signal.h>
#include <time.h>
#include <unistd.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
typedef enum{
TIMER_INIT,
TIMER_DELETED,
TIMER_PAUSED,
TIMER_CANCELLED,
TIMER_RESUMED,
TIMER_RUNNING,
} TIMER_STATE_T;
typedef struct Timer_{
/* Timer config */
timer_t *posix_timer;
void *user_arg;
unsigned long exp_timer; /* in milli-sec */
unsigned long sec_exp_timer; /* in milli-sec */
uint32_t thresdhold; /* No of times to invoke the timer callback */
void (*cb)(struct Timer_ *, void *); /* Timer Callback */
bool exponential_backoff;
/* place holder value to store
* dynamic attributes of timer */
unsigned long time_remaining; /* Time left for paused timer for next expiration */
uint32_t invocation_counter;
struct itimerspec ts;
unsigned long exp_back_off_time;
TIMER_STATE_T timer_state;
} Timer_t;
/* Returns NULL in timer creation fails, else
* return a pointer to Timer object*/
Timer_t*
setup_timer(
/* Timer Callback with user data and user size*/
void (*)(Timer_t*, void *),
/* First expiration time interval in msec */
unsigned long,
/* Subsequent expiration time interval in msec */
unsigned long,
/* Max no of expirations, 0 for infinite*/
uint32_t,
/* Arg to timer callback */
void *,
/* Is timer Exp back off */
bool);
static inline void
timer_delete_user_data(Timer_t *timer){
if(timer->user_arg) {
free(timer->user_arg);
timer->user_arg = NULL;
}
}
static inline TIMER_STATE_T
timer_get_current_state(Timer_t *timer){
return timer->timer_state;
}
static inline void
timer_set_state(Timer_t *timer, TIMER_STATE_T timer_state){
timer->timer_state = timer_state;
}
void
resurrect_timer(Timer_t *timer);
void
start_timer(Timer_t *timer);
void
delete_timer(Timer_t *timer);
void
cancel_timer(Timer_t *timer);
void
pause_timer(Timer_t *timer);
void
resume_timer(Timer_t *timer);
int
execute_timer(Timer_t *timer, TIMER_STATE_T action);
/* get remaining time in msec */
unsigned long
timer_get_time_remaining_in_mill_sec(Timer_t *timer);
void
restart_timer(Timer_t *timer);
void
reschedule_timer(Timer_t *timer,
unsigned long exp_ti,
unsigned long sec_exp_ti);
void
print_timer(Timer_t *timer);
unsigned long
timespec_to_millisec(
struct timespec *time);
void
timer_fill_itimerspec(struct timespec *ts,
unsigned long msec);
bool
is_timer_running(Timer_t *timer);
#endif /* __TIMER_WRAP__ */