forked from javilonas/NCam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ncam-time.c
453 lines (411 loc) · 11.4 KB
/
ncam-time.c
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#include "globals.h"
#include "ncam-time.h"
static enum clock_type clock_type = CLOCK_TYPE_UNKNOWN;
#if defined(CLOCKFIX)
struct timeval lasttime; // holds previous time to detect systemtime adjustments due to eg transponder change on dvb receivers
#endif
int64_t comp_timeb(struct timeb *tpa, struct timeb *tpb)
{
return (int64_t)(((int64_t)(tpa->time - tpb->time) * 1000ull) + ((int64_t) tpa->millitm - (int64_t) tpb->millitm));
}
int64_t comp_timebus(struct timeb *tpa, struct timeb *tpb)
{
return (int64_t)(((int64_t)(tpa->time - tpb->time) * 1000000ull) + ((int64_t) tpa->millitm - (int64_t) tpb->millitm));
}
/* Checks if year is a leap year. If so, 1 is returned, else 0. */
static int8_t is_leap(unsigned int y)
{
return (y % 4) == 0 && ((y % 100) != 0 || (y % 400) == 0);
}
/* Drop-in replacement for timegm function as some plattforms strip the function from their libc.. */
time_t cs_timegm(struct tm *tm)
{
#if __WORDSIZE == 64
time_t result = 0;
#else
int64_t result = 0;
#endif
int32_t i;
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)
{
return 0;
}
for(i = 70; i < tm->tm_year; ++i)
{
result += is_leap(i + 1900) ? 366 : 365;
}
for(i = 0; i < tm->tm_mon; ++i)
{
if(i == 0 || i == 2 || i == 4 || i == 6 || i == 7 || i == 9 || i == 11) { result += 31; }
else if(i == 3 || i == 5 || i == 8 || i == 10) { result += 30; }
else if(is_leap(tm->tm_year + 1900)) { result += 29; }
else { result += 28; }
}
result += tm->tm_mday - 1;
result *= 24;
result += tm->tm_hour;
result *= 60;
result += tm->tm_min;
result *= 60;
result += tm->tm_sec;
#if __WORDSIZE == 64
return result;
#else
if(result > INT32_MAX)
result = INT32_MAX;
return (time_t)result;
#endif
}
/* Drop-in replacement for gmtime_r as some plattforms strip the function from their libc. */
struct tm *cs_gmtime_r(const time_t *timep, struct tm *r)
{
static const int16_t daysPerMonth[13] =
{
0,
31,
31 + 28,
31 + 28 + 31,
31 + 28 + 31 + 30,
31 + 28 + 31 + 30 + 31,
31 + 28 + 31 + 30 + 31 + 30,
31 + 28 + 31 + 30 + 31 + 30 + 31,
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31
};
time_t i;
time_t work = * timep % 86400;
r->tm_sec = work % 60;
work /= 60;
r->tm_min = work % 60;
r->tm_hour = work / 60;
work = * timep / 86400;
r->tm_wday = (4 + work) % 7;
for(i = 1970; ; ++i)
{
time_t k = is_leap(i) ? 366 : 365;
if(work >= k)
{
work -= k;
}
else
{
break;
}
}
r->tm_year = i - 1900;
r->tm_yday = work;
r->tm_mday = 1;
if(is_leap(i) && work > 58)
{
if(work == 59)
{
r->tm_mday = 2; /* 29.2. */
}
work -= 1;
}
for(i = 11; i && daysPerMonth[i] > work; --i) { ; }
r->tm_mon = i;
r->tm_mday += work - daysPerMonth[i];
return r;
}
/* Drop-in replacement for ctime_r as some plattforms strip the function from their libc. */
char *cs_ctime_r(const time_t *timep, char *buf)
{
struct tm t;
localtime_r(timep, &t);
strftime(buf, 26, "%c\n", &t);
return buf;
}
void cs_ftime(struct timeb *tp)
{
struct timeval tv;
gettimeofday(&tv, NULL);
#if defined(CLOCKFIX)
if (tv.tv_sec > lasttime.tv_sec || (tv.tv_sec == lasttime.tv_sec && tv.tv_usec >= lasttime.tv_usec)) // check for time issues!
{
lasttime = tv; // register this valid time
}
else
{
tv = lasttime;
settimeofday(&tv, NULL); // set time back to last known valid time
//fprintf(stderr, "*** WARNING: BAD TIME AFFECTING WHOLE NCAM ECM HANDLING, SYSTEMTIME SET TO LAST KNOWN VALID TIME **** \n");
}
#endif
tp->time = tv.tv_sec;
tp->millitm = tv.tv_usec / 1000;
}
void cs_ftimeus(struct timeb *tp)
{
struct timeval tv;
gettimeofday(&tv, NULL);
#if defined(CLOCKFIX)
if (tv.tv_sec > lasttime.tv_sec || (tv.tv_sec == lasttime.tv_sec && tv.tv_usec >= lasttime.tv_usec)) // check for time issues!
{
lasttime = tv; // register this valid time
}
else
{
tv = lasttime;
settimeofday(&tv, NULL); // set time back to last known valid time
//fprintf(stderr, "*** WARNING: BAD TIME AFFECTING WHOLE NCAM ECM HANDLING, SYSTEMTIME SET TO LAST KNOWN VALID TIME **** \n");
}
#endif
tp->time = tv.tv_sec;
tp->millitm = tv.tv_usec;
}
void cs_sleepms(uint32_t msec)
{
// does not interfere with signals like sleep and usleep do
struct timespec req_ts;
req_ts.tv_sec = msec / 1000;
req_ts.tv_nsec = (msec % 1000) * 1000000L;
int32_t olderrno = errno; // Some OS (especially MacOSX) seem to set errno to ETIMEDOUT when sleeping
while (1)
{
/* Sleep for the time specified in req_ts. If interrupted by a
signal, place the remaining time left to sleep back into req_ts. */
int rval = nanosleep (&req_ts, &req_ts);
if (rval == 0)
{
break; // Completed the entire sleep time; all done.
}
else if (errno == EINTR)
{
continue; // Interrupted by a signal. Try again.
}
else
{
break; // Some other error; bail out.
}
}
errno = olderrno;
}
void cs_sleepus(uint32_t usec)
{
// does not interfere with signals like sleep and usleep do
struct timespec req_ts;
req_ts.tv_sec = usec / 1000000;
req_ts.tv_nsec = (usec % 1000000) * 1000L;
int32_t olderrno = errno; // Some OS (especially MacOSX) seem to set errno to ETIMEDOUT when sleeping
while (1)
{
/* Sleep for the time specified in req_ts. If interrupted by a
signal, place the remaining time left to sleep back into req_ts. */
int rval = nanosleep (&req_ts, &req_ts);
if (rval == 0)
{
break; // Completed the entire sleep time; all done.
}
else if (errno == EINTR)
{
continue; // Interrupted by a signal. Try again.
}
else
{
break; // Some other error; bail out.
}
}
errno = olderrno;
}
void add_ms_to_timespec(struct timespec *timeout, int32_t msec)
{
struct timespec now;
int64_t nanosecs, secs;
const int64_t NANOSEC_PER_MS = 1000000;
const int64_t NANOSEC_PER_SEC = 1000000000;
cs_gettime(&now);
nanosecs = (int64_t) (msec * NANOSEC_PER_MS + now.tv_nsec);
if (nanosecs >= NANOSEC_PER_SEC)
{
secs = now.tv_sec + (nanosecs / NANOSEC_PER_SEC);
nanosecs %= NANOSEC_PER_SEC;
}
else
{
secs = now.tv_sec;
}
timeout->tv_sec = (long)secs;
timeout->tv_nsec = (long)nanosecs;
}
void add_ms_to_timeb(struct timeb *tb, int32_t ms)
{
if (ms >= 1000){
tb->time += ms / 1000;
tb->millitm += (ms % 1000);
}
else{
tb->millitm += ms;
}
if(tb->millitm >= 1000)
{
tb->millitm %= 1000;
tb->time++;
}
}
int64_t add_ms_to_timeb_diff(struct timeb *tb, int32_t ms)
{
struct timeb tb_now;
add_ms_to_timeb(tb, ms);
cs_ftime(&tb_now);
return comp_timeb(tb, &tb_now);
}
#if defined(__UCLIBC__)
#define __UCLIBC_VER (__UCLIBC_MAJOR__ * 10000 + __UCLIBC_MINOR__ * 100 + __UCLIBC_SUBLEVEL__)
#else
#define __UCLIBC_VER 999999
#endif
#if defined(__GLIBC__)
#define __GLIBCVER (__GLIBC__ * 100 + __GLIBC_MINOR__)
#else
#define __GLIBCVER 9999
#endif
// Assume we have HAVE_pthread_condattr_setclock if CLOCK_MONOTONIC is defined
#if defined(CLOCKFIX) && defined(CLOCK_MONOTONIC)
#define HAVE_pthread_condattr_setclock 1
#endif
#if defined(HAVE_pthread_condattr_setclock)
// UCLIBC 0.9.31 does not have pthread_condattr_setclock
#if __UCLIBC_VER < 932
#undef HAVE_pthread_condattr_setclock
#endif
// glibc 2.3.6 in ppc old toolchain do not have pthread_condattr_setclock
#if __GLIBCVER < 204
#undef HAVE_pthread_condattr_setclock
#endif
// android's libc not have pthread_condattr_setclock
#if __BIONIC__
#undef HAVE_pthread_condattr_setclock
#endif
#endif
void __cs_pthread_cond_init(const char *n, pthread_cond_t *cond)
{
pthread_condattr_t attr;
SAFE_CONDATTR_INIT_R(&attr, n); // init condattr with defaults
#if 0
#if defined(HAVE_pthread_condattr_setclock)
enum clock_type ctype = cs_getclocktype();
SAFE_CONDATTR_SETCLOCK_R(&attr, (ctype == CLOCK_TYPE_MONOTONIC) ? CLOCK_MONOTONIC : CLOCK_REALTIME, n);
#endif
#endif
SAFE_COND_INIT_R(cond, &attr, n); // init thread with right clock assigned
pthread_condattr_destroy(&attr);
}
void __cs_pthread_cond_init_nolog(const char *n, pthread_cond_t *cond)
{
pthread_condattr_t attr;
SAFE_CONDATTR_INIT_NOLOG_R(&attr, n); // init condattr with defaults
#if 0
#if defined(HAVE_pthread_condattr_setclock)
enum clock_type ctype = cs_getclocktype();
SAFE_CONDATTR_SETCLOCK_NOLOG_R(&attr, (ctype == CLOCK_TYPE_MONOTONIC) ? CLOCK_MONOTONIC : CLOCK_REALTIME, n);
#endif
#endif
SAFE_COND_INIT_NOLOG_R(cond, &attr, n); // init thread with right clock assigned
pthread_condattr_destroy(&attr);
}
void sleepms_on_cond(const char *n, pthread_mutex_t *mutex, pthread_cond_t *cond, uint32_t msec)
{
struct timespec ts;
add_ms_to_timespec(&ts, msec);
SAFE_MUTEX_LOCK_R(mutex, n);
SAFE_COND_TIMEDWAIT_R(cond, mutex, &ts, n); // sleep on sleep_cond
SAFE_MUTEX_UNLOCK_R(mutex, n);
}
void cs_pthread_cond_init(const char *n, pthread_mutex_t *mutex, pthread_cond_t *cond)
{
SAFE_MUTEX_INIT_R(mutex, NULL, n);
__cs_pthread_cond_init(n, cond);
}
void cs_pthread_cond_init_nolog(const char *n, pthread_mutex_t *mutex, pthread_cond_t *cond)
{
SAFE_MUTEX_INIT_NOLOG_R(mutex, NULL, n);
__cs_pthread_cond_init(n, cond);
}
enum clock_type cs_getclocktype(void) {
if (clock_type == CLOCK_TYPE_UNKNOWN) {
struct timespec ts;
cs_gettime(&ts); // init clock type
}
return clock_type;
}
time_t cs_walltime(struct timeb *tp)
{
// we dont need to fetch time again and calculate if ncam is already using realtimeclock!
if (clock_type != CLOCK_TYPE_MONOTONIC)
return tp->time;
struct timespec ts;
struct timeval tv;
cs_gettime(&ts);
gettimeofday(&tv, NULL);
int64_t skew = tv.tv_sec - ts.tv_sec;
return(tp->time + skew);
}
/* Return real time clock value calculated based on cs_gettime(). Use this instead of time() */
time_t cs_time(void)
{
struct timeb tb;
cs_ftime(&tb);
return cs_walltime(&tb);
}
#ifdef __MACH__
#include <mach/clock.h>
#include <mach/mach.h>
#endif
void cs_gettime(struct timespec *ts)
{
struct timeval tv;
gettimeofday(&tv, NULL);
#if defined(CLOCKFIX)
if (tv.tv_sec > lasttime.tv_sec || (tv.tv_sec == lasttime.tv_sec && tv.tv_usec >= lasttime.tv_usec)) // check for time issues!
{
lasttime = tv; // register this valid time
}
else
{
tv = lasttime;
settimeofday(&tv, NULL); // set time back to last known valid time
//fprintf(stderr, "*** WARNING: BAD TIME AFFECTING WHOLE NCAM ECM HANDLING, SYSTEMTIME SET TO LAST KNOWN VALID TIME **** \n");
}
#endif
ts->tv_sec = tv.tv_sec;
ts->tv_nsec = tv.tv_usec * 1000;
clock_type = CLOCK_TYPE_REALTIME;
return;
#if 0
#if !defined(CLOCKFIX) || (!defined(CLOCK_MONOTONIC) && !defined(__MACH__))
struct timeval tv;
gettimeofday(&tv, NULL);
ts->tv_sec = tv.tv_sec;
ts->tv_nsec = tv.tv_usec * 1000;
clock_type = CLOCK_TYPE_REALTIME;
return;
#elif defined (__MACH__)
// OS X does not have clock_gettime, use clock_get_time
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
ts->tv_sec = mts.tv_sec;
ts->tv_nsec = mts.tv_nsec;
clock_type = CLOCK_TYPE_REALTIME;
#else
if (clock_type == CLOCK_TYPE_REALTIME) // monotonic returned error
{
clock_gettime(CLOCK_REALTIME, ts);
return;
}
int32_t ret = clock_gettime(CLOCK_MONOTONIC, ts);
clock_type = CLOCK_TYPE_MONOTONIC;
if ((ret < 0 && errno == EINVAL)) // Error fetching time from this source (Shouldn't happen on modern Linux)
{
clock_gettime(CLOCK_REALTIME, ts);
clock_type = CLOCK_TYPE_REALTIME;
}
#endif
#endif
}