]> git.zerfleddert.de Git - proxmark3-svn/blame - client/util_posix.c
Merge branch 'master' into fix_iclass_sim
[proxmark3-svn] / client / util_posix.c
CommitLineData
ec9c7112 1//-----------------------------------------------------------------------------
2// Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
3//
4// This code is licensed to you under the terms of the GNU GPL, version 2 or,
5// at your option, any later version. See the LICENSE.txt file for the text of
6// the license.
7//-----------------------------------------------------------------------------
8// utilities requiring Posix library functions
9//-----------------------------------------------------------------------------
10
11#if !defined(_WIN32)
12#define _POSIX_C_SOURCE 199309L // need nanosleep()
13#else
14#include <windows.h>
15#endif
16
17#include "util_posix.h"
18#include <stdint.h>
19#include <time.h>
20
21
22// Timer functions
23#if !defined (_WIN32)
24#include <errno.h>
25
26static void nsleep(uint64_t n) {
087c8bf3 27 struct timespec timeout;
28 timeout.tv_sec = n/1000000000;
29 timeout.tv_nsec = n%1000000000;
30 while (nanosleep(&timeout, &timeout) && errno == EINTR);
ec9c7112 31}
32
33void msleep(uint32_t n) {
bf824347 34 nsleep(1000000 * (uint64_t)n);
ec9c7112 35}
36#endif // _WIN32
37
d47c8c70 38#ifdef __APPLE__
087c8bf3 39
ec087218 40#ifndef CLOCK_MONOTONIC
087c8bf3 41 #define CLOCK_MONOTONIC (1)
ec087218 42#endif
43#ifndef CLOCK_REALTIME
087c8bf3 44 #define CLOCK_REALTIME (2)
ec087218 45#endif
087c8bf3 46
47 #include <sys/time.h>
48 #include <mach/clock.h>
49 #include <mach/mach.h>
50 #include <mach/mach_time.h>
51
52 /* clock_gettime is not implemented on OSX prior to 10.12 */
53 int _civet_clock_gettime(int clk_id, struct timespec *t);
54
55 int _civet_clock_gettime(int clk_id, struct timespec *t)
56 {
57 memset(t, 0, sizeof(*t));
58 if (clk_id == CLOCK_REALTIME) {
59 struct timeval now;
60 int rv = gettimeofday(&now, NULL);
61 if (rv) {
62 return rv;
63 }
64 t->tv_sec = now.tv_sec;
65 t->tv_nsec = now.tv_usec * 1000;
66 return 0;
67
68 } else if (clk_id == CLOCK_MONOTONIC) {
69 static uint64_t clock_start_time = 0;
d47c8c70 70 static mach_timebase_info_data_t timebase_info = {0, 0};
087c8bf3 71
72 uint64_t now = mach_absolute_time();
73
74 if (clock_start_time == 0) {
d47c8c70 75 mach_timebase_info(&timebase_info);
087c8bf3 76 clock_start_time = now;
77 }
78
79 now = (uint64_t)((double)(now - clock_start_time)
d47c8c70 80 * (double)timebase_info.numer
81 / (double)timebase_info.denom);
087c8bf3 82
83 t->tv_sec = now / 1000000000;
84 t->tv_nsec = now % 1000000000;
85 return 0;
86 }
87 return -1; // EINVAL - Clock ID is unknown
88 }
89
90 /* if clock_gettime is declared, then __CLOCK_AVAILABILITY will be defined */
91 #ifdef __CLOCK_AVAILABILITY
92 /* If we compiled with Mac OSX 10.12 or later, then clock_gettime will be declared
93 * but it may be NULL at runtime. So we need to check before using it. */
94 int _civet_safe_clock_gettime(int clk_id, struct timespec *t);
95
96 int _civet_safe_clock_gettime(int clk_id, struct timespec *t) {
97 if( clock_gettime ) {
98 return clock_gettime(clk_id, t);
99 }
100 return _civet_clock_gettime(clk_id, t);
101 }
102 #define clock_gettime _civet_safe_clock_gettime
103 #else
104 #define clock_gettime _civet_clock_gettime
105 #endif
106
107#endif
108
109
ec9c7112 110// a milliseconds timer for performance measurement
111uint64_t msclock() {
112#if defined(_WIN32)
087c8bf3 113 #include <sys/types.h>
114
115 // WORKAROUND FOR MinGW (some versions - use if normal code does not compile)
116 // It has no _ftime_s and needs explicit inclusion of timeb.h
117 #include <sys/timeb.h>
118 struct _timeb t;
119 _ftime(&t);
44964fd1 120 return 1000 * (uint64_t)t.time + t.millitm;
087c8bf3 121
122// NORMAL CODE (use _ftime_s)
ec9c7112 123 //struct _timeb t;
087c8bf3 124 //if (_ftime_s(&t)) {
ec9c7112 125 // return 0;
126 //} else {
127 // return 1000 * t.time + t.millitm;
128 //}
129#else
130 struct timespec t;
131 clock_gettime(CLOCK_MONOTONIC, &t);
132 return (t.tv_sec * 1000 + t.tv_nsec / 1000000);
133#endif
134}
135
Impressum, Datenschutz