diff --git a/arch/cpu/native/rtimer-arch.c b/arch/cpu/native/rtimer-arch.c index 2dab7efb6..ad081c9f5 100644 --- a/arch/cpu/native/rtimer-arch.c +++ b/arch/cpu/native/rtimer-arch.c @@ -79,11 +79,11 @@ rtimer_arch_schedule(rtimer_clock_t t) c = t - (unsigned short)clock_time(); - val.it_value.tv_sec = c / 1000; - val.it_value.tv_usec = (c % 1000) * 1000; + val.it_value.tv_sec = c / CLOCK_SECOND; + val.it_value.tv_usec = (c % CLOCK_SECOND) * CLOCK_SECOND; - PRINTF("rtimer_arch_schedule time %u %u in %d.%d seconds\n", t, c, c / 1000, - (c % 1000) * 1000); + PRINTF("rtimer_arch_schedule time %u %u in %d.%d seconds\n", t, c, val.it_value.tv_sec, + val.it_value.tv_usec); val.it_interval.tv_sec = val.it_interval.tv_usec = 0; setitimer(ITIMER_REAL, &val, NULL); diff --git a/arch/platform/native/Makefile.native b/arch/platform/native/Makefile.native index 4ed8e318b..2d1ee4fbc 100644 --- a/arch/platform/native/Makefile.native +++ b/arch/platform/native/Makefile.native @@ -19,6 +19,10 @@ else CONTIKI_TARGET_SOURCEFILES += tun6-net.c endif +ifeq ($(HOST_OS),Linux) +TARGET_LIBFILES += -lrt +endif + CONTIKI_SOURCEFILES += $(CONTIKI_TARGET_SOURCEFILES) .SUFFIXES: diff --git a/arch/platform/native/clock.c b/arch/platform/native/clock.c index 67d9ee317..3b94f3d4f 100644 --- a/arch/platform/native/clock.c +++ b/arch/platform/native/clock.c @@ -42,24 +42,49 @@ #include /*---------------------------------------------------------------------------*/ -clock_time_t -clock_time(void) +typedef struct clock_timespec_s { + time_t tv_sec; + long tv_nsec; +} clock_timespec_t; +/*---------------------------------------------------------------------------*/ +static void +get_time(clock_timespec_t *spec) { +#if defined(__linux__) || (defined(__MACH__) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200) + struct timespec ts; + + clock_gettime(CLOCK_MONOTONIC, &ts); + + spec->tv_sec = ts.tv_sec; + spec->tv_nsec = ts.tv_nsec; +#else struct timeval tv; gettimeofday(&tv, NULL); - return tv.tv_sec * 1000 + tv.tv_usec / 1000; + spec->tv_sec = tv.tv_sec; + spec->tv_nsec = tv.tv_usec * 1000; +#endif +} +/*---------------------------------------------------------------------------*/ +clock_time_t +clock_time(void) +{ + clock_timespec_t ts; + + get_time(&ts); + + return ts.tv_sec * CLOCK_SECOND + ts.tv_nsec / (1000000000 / CLOCK_SECOND); } /*---------------------------------------------------------------------------*/ unsigned long clock_seconds(void) { - struct timeval tv; + clock_timespec_t ts; - gettimeofday(&tv, NULL); + get_time(&ts); - return tv.tv_sec; + return ts.tv_sec; } /*---------------------------------------------------------------------------*/ void