misc.c
Raw
1#include <cutils/misc.h>
2
3void sleep_ms(unsigned int milliseconds)
4{
5 #ifdef __unix__
6 usleep(milliseconds * 1000);
7 #endif
8 #ifdef _WIN32
9 Sleep(milliseconds);
10 #endif
11}
12
13#if __STDC_VERSION__ >= 199901L
14uint32_t ntoh32(uint32_t const net)
15{
16 uint8_t data[4];
17 memcpy(&data, &net, sizeof(data));
18
19 return ((uint32_t) data[3] << 0)
20 | ((uint32_t) data[2] << 8)
21 | ((uint32_t) data[1] << 16)
22 | ((uint32_t) data[0] << 24);
23}
24#endif
25
26#if __STDC_VERSION__ >= 201112L
27struct timespec timespec_diff(const struct timespec* old_ts, const struct timespec* new_ts)
28{
29 struct timespec diff;
30
31 if ((new_ts->tv_nsec - old_ts->tv_nsec) < 0)
32 {
33 diff.tv_sec = new_ts->tv_sec - old_ts->tv_sec - 1;
34 diff.tv_nsec = new_ts->tv_nsec - old_ts->tv_nsec + 1000000000;
35 } else {
36 diff.tv_sec = new_ts->tv_sec - old_ts->tv_sec;
37 diff.tv_nsec = new_ts->tv_nsec - old_ts->tv_nsec;
38 }
39
40 return diff;
41}
42
43struct timespec timespec_add(const struct timespec* ts_1, const struct timespec* ts_2)
44{
45 struct timespec res;
46
47 if (999999999 - ts_1->tv_nsec < ts_2->tv_nsec)
48 {
49 res.tv_sec = ts_1->tv_sec + ts_2->tv_sec + 1;
50 res.tv_nsec = ts_1->tv_nsec + (ts_2->tv_nsec - 1000000000) ;
51 } else {
52 res.tv_sec = ts_1->tv_sec + ts_2->tv_sec;
53 res.tv_nsec = ts_1->tv_nsec + ts_2->tv_nsec;
54 }
55
56 return res;
57}
58
59
60max_uint_t timespec_ms(const struct timespec* ts)
61{
62 return (max_uint_t)ts->tv_sec * 1000 + (max_uint_t)ts->tv_nsec/1000000;
63}
64#endif
65