#endif
#include "oic_time.h"
-#include <stdio.h>
+
+#include <stddef.h> // For NULL
#ifndef WITH_ARDUINO
-#include <unistd.h>
-#include <time.h>
-#include <sys/time.h>
+# if _POSIX_TIMERS > 0
+# include <time.h> // For clock_gettime()
+# else
+# include <sys/time.h> // For gettimeofday()
+# endif // _POSIX_TIMERS > 0
#endif
#define TAG "OIC_TIME"
uint64_t OICGetCurrentTime(OICTimePrecision precision)
{
uint64_t currentTime = 0;
- int err = 0;
#ifdef WITH_ARDUINO
- currentTime = (TIME_IN_MS == precision) ? (uint64_t)millis() : (uint64_t)micros();
+ currentTime = (TIME_IN_MS == precision) ? millis() : micros();
#else
- #if _POSIX_TIMERS > 0
- int32_t clockId = CLOCK_REALTIME;
- static int32_t cachedRet = 0;
+# if _POSIX_TIMERS > 0
+# if defined(CLOCK_MONOTONIC_COARSE)
+ static const clockid_t clockId = CLOCK_MONOTONIC_COARSE;
+# elif _POSIX_MONOTONIC_CLOCK >= 0
+ // Option _POSIX_MONOTONIC_CLOCK == 0 indicates that the option is
+ // available at compile time but may not be supported at run
+ // time. Check if option _POSIX_MONOTONIC_CLOCK is supported at
+ // run time.
+# if _POSIX_MONOTONIC_CLOCK == 0
+ static const clockid_t clockId =
+ sysconf(_SC_MONOTONIC_CLOCK) > 0 ? CLOCK_MONOTONIC : CLOCK_REALTIME;
+# else
+ static const clockid_t clockId = CLOCK_MONOTONIC;
+# endif // _POSIX_MONOTONIC_CLOCK == 0
+# else
+ static const clockid_t clockId = CLOCK_REALTIME;
+# endif // CLOCK_MONOTONIC_COARSE
- #if defined(CLOCK_MONOTONIC_COARSE)
- clockId = CLOCK_MONOTONIC_COARSE;
- #elif _POSIX_MONOTONIC_CLOCK >= 0
- //Option _POSIX_MONOTONIC_CLOCK == 0 indicates that the option is
- //available at compile time but may not be supported at run time.
- //Checking if option _POSIX_MONOTONIC_CLOCK is supported at run time.
- #if _POSIX_MONOTONIC_CLOCK == 0
- cachedRet = (0 == cachedRet) ? sysconf(_SC_MONOTONIC_CLOCK) : cachedRet;
- if(cachedRet > 0)
- {
- clockId = CLOCK_MONOTONIC;
- }
- #else
- clockId = CLOCK_MONOTONIC;
- #endif
- #else
- clockId = CLOCK_REALTIME;
- #endif
+ struct timespec current = { .tv_sec = 0, .tv_nsec = 0 };
+ if (clock_gettime(clockId, ¤t) == 0)
+ {
+ currentTime =
+ (TIME_IN_MS == precision)
+ ? (((uint64_t) current.tv_sec * MS_PER_SEC) + (current.tv_nsec / NS_PER_MS))
+ : (((uint64_t) current.tv_sec * US_PER_SEC) + (current.tv_nsec / NS_PER_US));
+ }
+# else
+ struct timeval current = { .tv_sec = 0, .tv_usec = 0 };
+ if (gettimeofday(¤t, NULL) == 0)
+ {
+ currentTime =
+ (TIME_IN_MS == precision)
+ ? (((uint64_t) current.tv_sec * MS_PER_SEC) + (current.tv_usec / US_PER_MS))
+ : (((uint64_t) current.tv_sec * US_PER_SEC) + (current.tv_usec));
+ }
+# endif // _POSIX_TIMERS > 0
+#endif // WITH_ARDUINO
- struct timespec current = {.tv_sec=0, .tv_nsec=0};
- if((err = clock_gettime(clockId, ¤t)) != -1)
- {
- currentTime = (TIME_IN_MS == precision) ?
- (((uint64_t)current.tv_sec * MS_PER_SEC) + (current.tv_nsec / NS_PER_MS)):
- (((uint64_t)current.tv_sec * US_PER_SEC) + (current.tv_nsec / NS_PER_US));
- }
- #else
- struct timeval current = {.tv_sec=0, .tv_usec=0};
- if((err = gettimeofday(¤t, NULL)) != -1)
- {
- currentTime = (TIME_IN_MS == precision) ?
- (((uint64_t)current.tv_sec * MS_PER_SEC) + (current.tv_usec / US_PER_MS)):
- (((uint64_t)current.tv_sec * US_PER_SEC) + (current.tv_usec));
- }
- #endif
-#endif
- return (!err) ? currentTime : 0;
+ return currentTime;
}
-