text
[RRG-proxmark3.git] / common / util_posix.c
blob4bc28f80fb36bfcab0cf8fe406ed442336a0dc1f
1 //-----------------------------------------------------------------------------
2 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // See LICENSE.txt for the text of the license.
15 //-----------------------------------------------------------------------------
16 // utilities requiring Posix library functions
17 //-----------------------------------------------------------------------------
19 // ensure availability even with -std=c99; must be included before
20 #if !defined(_WIN32)
22 #define _POSIX_C_SOURCE 200112L // need localtime_r()
23 #else
24 #include <windows.h>
25 #endif
27 #include "util_posix.h"
28 #include <stdint.h>
29 #include <time.h>
32 // Timer functions
33 #if !defined (_WIN32)
34 #include <errno.h>
36 static void nsleep(uint64_t n) {
37 struct timespec timeout;
38 timeout.tv_sec = n / 1000000000;
39 timeout.tv_nsec = n % 1000000000;
40 while (nanosleep(&timeout, &timeout) && errno == EINTR);
43 void msleep(uint32_t n) {
44 nsleep(1000000 * (uint64_t)n);
46 #endif // _WIN32
48 #ifdef __APPLE__
50 #ifndef CLOCK_MONOTONIC
51 #define CLOCK_MONOTONIC (1)
52 #endif
53 #ifndef CLOCK_REALTIME
54 #define CLOCK_REALTIME (2)
55 #endif
57 #include <sys/time.h>
58 #include <mach/clock.h>
59 #include <mach/mach.h>
60 #include <mach/mach_time.h>
62 /* clock_gettime is not implemented on OSX prior to 10.12 */
63 int _civet_clock_gettime(int clk_id, struct timespec *t);
65 int _civet_clock_gettime(int clk_id, struct timespec *t) {
66 memset(t, 0, sizeof(*t));
67 if (clk_id == CLOCK_REALTIME) {
68 struct timeval now;
69 int rv = gettimeofday(&now, NULL);
70 if (rv) {
71 return rv;
73 t->tv_sec = now.tv_sec;
74 t->tv_nsec = now.tv_usec * 1000;
75 return 0;
77 } else if (clk_id == CLOCK_MONOTONIC) {
78 static uint64_t clock_start_time = 0;
79 static mach_timebase_info_data_t timebase_info = {0, 0};
81 uint64_t now = mach_absolute_time();
83 if (clock_start_time == 0) {
85 mach_timebase_info(&timebase_info);
86 clock_start_time = now;
89 now = (uint64_t)((double)(now - clock_start_time)
90 * (double)timebase_info.numer
91 / (double)timebase_info.denom);
93 t->tv_sec = now / 1000000000;
94 t->tv_nsec = now % 1000000000;
95 return 0;
97 return -1; // EINVAL - Clock ID is unknown
100 /* if clock_gettime is declared, then __CLOCK_AVAILABILITY will be defined */
101 #ifdef __CLOCK_AVAILABILITY
102 /* If we compiled with Mac OSX 10.12 or later, then clock_gettime will be declared
103 * but it may be NULL at runtime. So we need to check before using it. */
104 int _civet_safe_clock_gettime(int clk_id, struct timespec *t);
106 int _civet_safe_clock_gettime(int clk_id, struct timespec *t) {
107 if (clock_gettime) {
108 return clock_gettime(clk_id, t);
110 return _civet_clock_gettime(clk_id, t);
112 #define clock_gettime _civet_safe_clock_gettime
113 #else
114 #define clock_gettime _civet_clock_gettime
115 #endif
117 #endif
119 // a milliseconds timer for performance measurement
120 uint64_t msclock(void) {
121 #if defined(_WIN32)
122 #include <sys/types.h>
124 // WORKAROUND FOR MinGW (some versions - use if normal code does not compile)
125 // It has no _ftime_s and needs explicit inclusion of timeb.h
126 #include <sys/timeb.h>
127 struct _timeb t;
128 _ftime(&t);
129 return 1000 * (uint64_t)t.time + t.millitm;
131 // NORMAL CODE (use _ftime_s)
132 //struct _timeb t;
133 //if (_ftime_s(&t)) {
134 // return 0;
135 //} else {
136 // return 1000 * t.time + t.millitm;
138 #else
139 struct timespec t;
140 clock_gettime(CLOCK_MONOTONIC, &t);
141 return (1000 * (uint64_t)t.tv_sec + t.tv_nsec / 1000000);
142 #endif
145 // a micro seconds timer for performance measurement
146 uint64_t usclock(void) {
147 #if defined(_WIN32)
148 #include <sys/types.h>
150 // WORKAROUND FOR MinGW (some versions - use if normal code does not compile)
151 // It has no _ftime_s and needs explicit inclusion of timeb.h
152 #include <sys/timeb.h>
153 struct _timeb t;
154 _ftime(&t);
155 return 1000 * (uint64_t)t.time + t.millitm;
157 // NORMAL CODE (use _ftime_s)
158 //struct _timeb t;
159 //if (_ftime_s(&t)) {
160 // return 0;
161 //} else {
162 // return 1000 * t.time + t.millitm;
164 #else
165 struct timespec t;
166 clock_gettime(CLOCK_MONOTONIC, &t);
167 //return (1000 * (uint64_t)t.tv_sec + t.tv_nsec / 1000);
168 return (1000 * (uint64_t)t.tv_sec + (t.tv_nsec / 1000));
169 #endif