1 //-----------------------------------------------------------------------------
2 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
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.
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
22 #define _POSIX_C_SOURCE 200112L // need localtime_r()
27 #include "util_posix.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
);
50 #ifndef CLOCK_MONOTONIC
51 #define CLOCK_MONOTONIC (1)
53 #ifndef CLOCK_REALTIME
54 #define CLOCK_REALTIME (2)
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
) {
69 int rv
= gettimeofday(&now
, NULL
);
73 t
->tv_sec
= now
.tv_sec
;
74 t
->tv_nsec
= now
.tv_usec
* 1000;
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;
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
) {
108 return clock_gettime(clk_id
, t
);
110 return _civet_clock_gettime(clk_id
, t
);
112 #define clock_gettime _civet_safe_clock_gettime
114 #define clock_gettime _civet_clock_gettime
119 // a milliseconds timer for performance measurement
120 uint64_t msclock(void) {
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>
129 return 1000 * (uint64_t)t
.time
+ t
.millitm
;
131 // NORMAL CODE (use _ftime_s)
133 //if (_ftime_s(&t)) {
136 // return 1000 * t.time + t.millitm;
140 clock_gettime(CLOCK_MONOTONIC
, &t
);
141 return (1000 * (uint64_t)t
.tv_sec
+ t
.tv_nsec
/ 1000000);
145 // a micro seconds timer for performance measurement
146 uint64_t usclock(void) {
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>
155 return 1000 * (uint64_t)t
.time
+ t
.millitm
;
157 // NORMAL CODE (use _ftime_s)
159 //if (_ftime_s(&t)) {
162 // return 1000 * t.time + t.millitm;
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));