1 | #include "globals.h"
|
---|
2 | #include "oscam-time.h"
|
---|
3 |
|
---|
4 | static enum clock_type clock_type = CLOCK_TYPE_UNKNOWN;
|
---|
5 |
|
---|
6 | int64_t comp_timeb(struct timeb *tpa, struct timeb *tpb)
|
---|
7 | {
|
---|
8 | return (uint64_t)(((uint64_t)(tpa->time - tpb->time) * 1000ull) + (tpa->millitm - tpb->millitm));
|
---|
9 | }
|
---|
10 |
|
---|
11 | /* Checks if year is a leap year. If so, 1 is returned, else 0. */
|
---|
12 | static int8_t is_leap(unsigned int y)
|
---|
13 | {
|
---|
14 | return (y % 4) == 0 && ((y % 100) != 0 || (y % 400) == 0);
|
---|
15 | }
|
---|
16 |
|
---|
17 | /* Drop-in replacement for timegm function as some plattforms strip the function from their libc.. */
|
---|
18 | time_t cs_timegm(struct tm *tm)
|
---|
19 | {
|
---|
20 | time_t result = 0;
|
---|
21 | int32_t i;
|
---|
22 | if(tm->tm_mon > 12 || tm->tm_mon < 0 || tm->tm_mday > 31 || tm->tm_min > 60 || tm->tm_sec > 60 || tm->tm_hour > 24)
|
---|
23 | { return 0; }
|
---|
24 | for(i = 70; i < tm->tm_year; ++i)
|
---|
25 | {
|
---|
26 | result += is_leap(i + 1900) ? 366 : 365;
|
---|
27 | }
|
---|
28 | for(i = 0; i < tm->tm_mon; ++i)
|
---|
29 | {
|
---|
30 | if(i == 0 || i == 2 || i == 4 || i == 6 || i == 7 || i == 9 || i == 11) { result += 31; }
|
---|
31 | else if(i == 3 || i == 5 || i == 8 || i == 10) { result += 30; }
|
---|
32 | else if(is_leap(tm->tm_year + 1900)) { result += 29; }
|
---|
33 | else { result += 28; }
|
---|
34 | }
|
---|
35 | result += tm->tm_mday - 1;
|
---|
36 | result *= 24;
|
---|
37 | result += tm->tm_hour;
|
---|
38 | result *= 60;
|
---|
39 | result += tm->tm_min;
|
---|
40 | result *= 60;
|
---|
41 | result += tm->tm_sec;
|
---|
42 | return result;
|
---|
43 | }
|
---|
44 |
|
---|
45 | /* Drop-in replacement for gmtime_r as some plattforms strip the function from their libc. */
|
---|
46 | struct tm *cs_gmtime_r(const time_t *timep, struct tm *r)
|
---|
47 | {
|
---|
48 | static const int16_t daysPerMonth[13] = { 0,
|
---|
49 | 31,
|
---|
50 | 31 + 28,
|
---|
51 | 31 + 28 + 31,
|
---|
52 | 31 + 28 + 31 + 30,
|
---|
53 | 31 + 28 + 31 + 30 + 31,
|
---|
54 | 31 + 28 + 31 + 30 + 31 + 30,
|
---|
55 | 31 + 28 + 31 + 30 + 31 + 30 + 31,
|
---|
56 | 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
|
---|
57 | 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
|
---|
58 | 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
|
---|
59 | 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
|
---|
60 | 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31
|
---|
61 | };
|
---|
62 | time_t i;
|
---|
63 | time_t work = * timep % 86400;
|
---|
64 | r->tm_sec = work % 60;
|
---|
65 | work /= 60;
|
---|
66 | r->tm_min = work % 60;
|
---|
67 | r->tm_hour = work / 60;
|
---|
68 | work = * timep / 86400;
|
---|
69 | r->tm_wday = (4 + work) % 7;
|
---|
70 | for(i = 1970; ; ++i)
|
---|
71 | {
|
---|
72 | time_t k = is_leap(i) ? 366 : 365;
|
---|
73 | if(work >= k)
|
---|
74 | { work -= k; }
|
---|
75 | else
|
---|
76 | { break; }
|
---|
77 | }
|
---|
78 | r->tm_year = i - 1900;
|
---|
79 | r->tm_yday = work;
|
---|
80 | r->tm_mday = 1;
|
---|
81 | if(is_leap(i) && work > 58)
|
---|
82 | {
|
---|
83 | if(work == 59)
|
---|
84 | { r->tm_mday = 2; } /* 29.2. */
|
---|
85 | work -= 1;
|
---|
86 | }
|
---|
87 | for(i = 11; i && daysPerMonth[i] > work; --i)
|
---|
88 | { ; }
|
---|
89 | r->tm_mon = i;
|
---|
90 | r->tm_mday += work - daysPerMonth[i];
|
---|
91 | return r;
|
---|
92 | }
|
---|
93 |
|
---|
94 | /* Drop-in replacement for ctime_r as some plattforms strip the function from their libc. */
|
---|
95 | char *cs_ctime_r(const time_t *timep, char *buf)
|
---|
96 | {
|
---|
97 | struct tm t;
|
---|
98 | localtime_r(timep, &t);
|
---|
99 | strftime(buf, 26, "%c\n", &t);
|
---|
100 | return buf;
|
---|
101 | }
|
---|
102 |
|
---|
103 | void cs_ftime(struct timeb *tp)
|
---|
104 | {
|
---|
105 | struct timeval tv;
|
---|
106 | gettimeofday(&tv, NULL);
|
---|
107 | tp->time = tv.tv_sec;
|
---|
108 | tp->millitm = tv.tv_usec / 1000;
|
---|
109 | }
|
---|
110 |
|
---|
111 | void cs_sleepms(uint32_t msec)
|
---|
112 | {
|
---|
113 | //does not interfere with signals like sleep and usleep do
|
---|
114 | struct timespec req_ts;
|
---|
115 | req_ts.tv_sec = msec / 1000;
|
---|
116 | req_ts.tv_nsec = (msec % 1000) * 1000000L;
|
---|
117 | int32_t olderrno = errno; // Some OS (especially MacOSX) seem to set errno to ETIMEDOUT when sleeping
|
---|
118 | while (1)
|
---|
119 | {
|
---|
120 | /* Sleep for the time specified in req_ts. If interrupted by a
|
---|
121 | signal, place the remaining time left to sleep back into req_ts. */
|
---|
122 | int rval = nanosleep (&req_ts, &req_ts);
|
---|
123 | if (rval == 0)
|
---|
124 | return; // Completed the entire sleep time; all done.
|
---|
125 | else if (errno == EINTR)
|
---|
126 | continue; // Interrupted by a signal. Try again.
|
---|
127 | else
|
---|
128 | return; // Some other error; bail out.
|
---|
129 | }
|
---|
130 | errno = olderrno;
|
---|
131 | }
|
---|
132 |
|
---|
133 | void cs_sleepus(uint32_t usec)
|
---|
134 | {
|
---|
135 | //does not interfere with signals like sleep and usleep do
|
---|
136 | struct timespec req_ts;
|
---|
137 | req_ts.tv_sec = usec / 1000000;
|
---|
138 | req_ts.tv_nsec = (usec % 1000000) * 1000L;
|
---|
139 | int32_t olderrno = errno; // Some OS (especially MacOSX) seem to set errno to ETIMEDOUT when sleeping
|
---|
140 | while (1)
|
---|
141 | {
|
---|
142 | /* Sleep for the time specified in req_ts. If interrupted by a
|
---|
143 | signal, place the remaining time left to sleep back into req_ts. */
|
---|
144 | int rval = nanosleep (&req_ts, &req_ts);
|
---|
145 | if (rval == 0)
|
---|
146 | return; // Completed the entire sleep time; all done.
|
---|
147 | else if (errno == EINTR)
|
---|
148 | continue; // Interrupted by a signal. Try again.
|
---|
149 | else
|
---|
150 | return; // Some other error; bail out.
|
---|
151 | }
|
---|
152 | errno = olderrno;
|
---|
153 | }
|
---|
154 |
|
---|
155 | void add_ms_to_timespec(struct timespec *timeout, int32_t msec)
|
---|
156 | {
|
---|
157 | struct timeval now;
|
---|
158 | gettimeofday(&now, NULL);
|
---|
159 | int32_t nano_secs = ((now.tv_usec * 1000) + ((msec % 1000) * 1000 * 1000));
|
---|
160 | timeout->tv_sec = now.tv_sec + (msec / 1000) + (nano_secs / 1000000000);
|
---|
161 | timeout->tv_nsec = nano_secs % 1000000000;
|
---|
162 | }
|
---|
163 |
|
---|
164 | void add_ms_to_timeb(struct timeb *tb, int32_t ms)
|
---|
165 | {
|
---|
166 | tb->time += ms / 1000;
|
---|
167 | tb->millitm += ms % 1000;
|
---|
168 | if(tb->millitm >= 1000)
|
---|
169 | {
|
---|
170 | tb->millitm %= 1000;
|
---|
171 | tb->time++;
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | int64_t add_ms_to_timeb_diff(struct timeb *tb, int32_t ms)
|
---|
176 | {
|
---|
177 | struct timeb tb_now;
|
---|
178 | add_ms_to_timeb(tb, ms);
|
---|
179 | cs_ftime(&tb_now);
|
---|
180 | return comp_timeb(tb, &tb_now);
|
---|
181 | }
|
---|
182 |
|
---|
183 | #if defined(__UCLIBC__)
|
---|
184 | # define __UCLIBC_VER (__UCLIBC_MAJOR__ * 10000 + __UCLIBC_MINOR__ * 100 + __UCLIBC_SUBLEVEL__)
|
---|
185 | #else
|
---|
186 | # define __UCLIBC_VER 999999
|
---|
187 | #endif
|
---|
188 |
|
---|
189 | #if defined(__GLIBC__)
|
---|
190 | # define __GLIBCVER (__GLIBC__ * 100 + __GLIBC_MINOR__)
|
---|
191 | #else
|
---|
192 | # define __GLIBCVER 9999
|
---|
193 | #endif
|
---|
194 |
|
---|
195 | // Assume we have HAVE_pthread_condattr_setclock if CLOCK_MONOTONIC is defined
|
---|
196 | #if defined(CLOCKFIX) && defined(CLOCK_MONOTONIC)
|
---|
197 | #define HAVE_pthread_condattr_setclock 1
|
---|
198 | #endif
|
---|
199 |
|
---|
200 | #if defined(HAVE_pthread_condattr_setclock)
|
---|
201 | // UCLIBC 0.9.31 does not have pthread_condattr_setclock
|
---|
202 | # if __UCLIBC_VER < 932
|
---|
203 | # undef HAVE_pthread_condattr_setclock
|
---|
204 | # endif
|
---|
205 | // glibc 2.3.6 in ppc old toolchain do not have pthread_condattr_setclock
|
---|
206 | # if __GLIBCVER < 204
|
---|
207 | # undef HAVE_pthread_condattr_setclock
|
---|
208 | # endif
|
---|
209 | // android's libc not have pthread_condattr_setclock
|
---|
210 | # if __BIONIC__
|
---|
211 | # undef HAVE_pthread_condattr_setclock
|
---|
212 | # endif
|
---|
213 | #endif
|
---|
214 |
|
---|
215 | void __cs_pthread_cond_init(pthread_cond_t *cond)
|
---|
216 | {
|
---|
217 | pthread_condattr_t attr;
|
---|
218 | pthread_condattr_init(&attr); // init condattr with defaults
|
---|
219 | #if 0
|
---|
220 | #if defined(HAVE_pthread_condattr_setclock)
|
---|
221 | enum clock_type ctype = cs_getclocktype();
|
---|
222 | pthread_condattr_setclock(&attr, (ctype == CLOCK_TYPE_MONOTONIC) ? CLOCK_MONOTONIC : CLOCK_REALTIME);
|
---|
223 | #endif
|
---|
224 | #endif
|
---|
225 | pthread_cond_init(cond, &attr); // init thread with right clock assigned
|
---|
226 | }
|
---|
227 |
|
---|
228 | void sleepms_on_cond(pthread_mutex_t *mutex, pthread_cond_t *cond, uint32_t msec)
|
---|
229 | {
|
---|
230 | struct timespec ts;
|
---|
231 | add_ms_to_timespec(&ts, msec);
|
---|
232 | pthread_mutex_lock(mutex);
|
---|
233 | pthread_cond_timedwait(cond, mutex, &ts); // sleep on sleep_cond
|
---|
234 | pthread_mutex_unlock(mutex);
|
---|
235 | }
|
---|
236 |
|
---|
237 | void cs_pthread_cond_init(pthread_mutex_t *mutex, pthread_cond_t *cond)
|
---|
238 | {
|
---|
239 | pthread_mutex_init(mutex, NULL);
|
---|
240 | __cs_pthread_cond_init(cond);
|
---|
241 | }
|
---|
242 |
|
---|
243 | enum clock_type cs_getclocktype(void) {
|
---|
244 | if (clock_type == CLOCK_TYPE_UNKNOWN) {
|
---|
245 | struct timespec ts;
|
---|
246 | cs_gettime(&ts); // init clock type
|
---|
247 | }
|
---|
248 | return clock_type;
|
---|
249 | }
|
---|
250 |
|
---|
251 | time_t cs_walltime(struct timeb *tp)
|
---|
252 | {
|
---|
253 | // we dont need to fetch time again and calculate if oscam is already using realtimeclock!
|
---|
254 | if (clock_type != CLOCK_TYPE_MONOTONIC)
|
---|
255 | return tp->time;
|
---|
256 |
|
---|
257 | struct timespec ts;
|
---|
258 | struct timeval tv;
|
---|
259 |
|
---|
260 | cs_gettime(&ts);
|
---|
261 | gettimeofday(&tv, NULL);
|
---|
262 | int64_t skew = tv.tv_sec - ts.tv_sec;
|
---|
263 | return(tp->time + skew);
|
---|
264 | }
|
---|
265 |
|
---|
266 | /* Return real time clock value calculated based on cs_gettime(). Use this instead of time() */
|
---|
267 | time_t cs_time(void)
|
---|
268 | {
|
---|
269 | struct timeb tb;
|
---|
270 | cs_ftime(&tb);
|
---|
271 | return cs_walltime(&tb);
|
---|
272 | }
|
---|
273 |
|
---|
274 | #ifdef __MACH__
|
---|
275 | #include <mach/clock.h>
|
---|
276 | #include <mach/mach.h>
|
---|
277 | #endif
|
---|
278 |
|
---|
279 | void cs_gettime(struct timespec *ts)
|
---|
280 | {
|
---|
281 | struct timeval tv;
|
---|
282 | gettimeofday(&tv, NULL);
|
---|
283 | ts->tv_sec = tv.tv_sec;
|
---|
284 | ts->tv_nsec = tv.tv_usec * 1000;
|
---|
285 | clock_type = CLOCK_TYPE_REALTIME;
|
---|
286 | return;
|
---|
287 | #if 0
|
---|
288 | #if !defined(CLOCKFIX) || (!defined(CLOCK_MONOTONIC) && !defined(__MACH__))
|
---|
289 | struct timeval tv;
|
---|
290 | gettimeofday(&tv, NULL);
|
---|
291 | ts->tv_sec = tv.tv_sec;
|
---|
292 | ts->tv_nsec = tv.tv_usec * 1000;
|
---|
293 | clock_type = CLOCK_TYPE_REALTIME;
|
---|
294 | return;
|
---|
295 | #elif defined (__MACH__)
|
---|
296 | // OS X does not have clock_gettime, use clock_get_time
|
---|
297 | clock_serv_t cclock;
|
---|
298 | mach_timespec_t mts;
|
---|
299 | host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
|
---|
300 | clock_get_time(cclock, &mts);
|
---|
301 | mach_port_deallocate(mach_task_self(), cclock);
|
---|
302 | ts->tv_sec = mts.tv_sec;
|
---|
303 | ts->tv_nsec = mts.tv_nsec;
|
---|
304 | clock_type = CLOCK_TYPE_REALTIME;
|
---|
305 | #else
|
---|
306 | if (clock_type == CLOCK_TYPE_REALTIME) { // monotonic returned error
|
---|
307 | clock_gettime(CLOCK_REALTIME, ts);
|
---|
308 | return;
|
---|
309 | }
|
---|
310 | int32_t ret = clock_gettime(CLOCK_MONOTONIC, ts);
|
---|
311 | clock_type = CLOCK_TYPE_MONOTONIC;
|
---|
312 | if ((ret < 0 && errno == EINVAL)){ // Error fetching time from this source (Shouldn't happen on modern Linux)
|
---|
313 | clock_gettime(CLOCK_REALTIME, ts);
|
---|
314 | clock_type = CLOCK_TYPE_REALTIME;
|
---|
315 | }
|
---|
316 | #endif
|
---|
317 | #endif
|
---|
318 | }
|
---|