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
13uint32_t ntoh32(uint32_t const net)
14{
15 uint8_t data[4];
16 memcpy(&data, &net, sizeof(data));
17
18 return ((uint32_t) data[3] << 0)
19 | ((uint32_t) data[2] << 8)
20 | ((uint32_t) data[1] << 16)
21 | ((uint32_t) data[0] << 24);
22}
23
24#if __STDC_VERSION__ >= 201112L
25struct timespec timespec_diff(const struct timespec* old_ts, const struct timespec* new_ts)
26{
27 struct timespec diff;
28
29 if ((new_ts->tv_nsec - old_ts->tv_nsec) < 0)
30 {
31 diff.tv_sec = new_ts->tv_sec - old_ts->tv_sec - 1;
32 diff.tv_nsec = new_ts->tv_nsec - old_ts->tv_nsec + 1000000000;
33 } else {
34 diff.tv_sec = new_ts->tv_sec - old_ts->tv_sec;
35 diff.tv_nsec = new_ts->tv_nsec - old_ts->tv_nsec;
36 }
37
38 return diff;
39}
40
41struct timespec timespec_add(const struct timespec* ts_1, const struct timespec* ts_2)
42{
43 struct timespec res;
44
45 if (999999999 - ts_1->tv_nsec < ts_2->tv_nsec)
46 {
47 res.tv_sec = ts_1->tv_sec + ts_2->tv_sec + 1;
48 res.tv_nsec = ts_1->tv_nsec + (ts_2->tv_nsec - 1000000000) ;
49 } else {
50 res.tv_sec = ts_1->tv_sec + ts_2->tv_sec;
51 res.tv_nsec = ts_1->tv_nsec + ts_2->tv_nsec;
52 }
53
54 return res;
55}
56
57
58max_uint_t timespec_ms(const struct timespec* ts)
59{
60 return (max_uint_t)ts->tv_sec * 1000 + (max_uint_t)ts->tv_nsec/1000000;
61}
62#endif
63