2018-01-17 08:39:42 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Intel Corporation
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
#include <kernel.h>
|
|
|
|
#include <errno.h>
|
2018-04-05 19:11:15 +02:00
|
|
|
#include <posix/time.h>
|
2019-04-23 23:43:25 +02:00
|
|
|
#include <posix/sys/time.h>
|
2020-03-24 08:10:56 +01:00
|
|
|
#include <syscall_handler.h>
|
2018-01-17 08:39:42 +01:00
|
|
|
|
2018-06-25 19:24:53 +02:00
|
|
|
/*
|
|
|
|
* `k_uptime_get` returns a timestamp based on an always increasing
|
|
|
|
* value from the system start. To support the `CLOCK_REALTIME`
|
|
|
|
* clock, this `rt_clock_base` records the time that the system was
|
|
|
|
* started. This can either be set via 'clock_settime', or could be
|
|
|
|
* set from a real time clock, if such hardware is present.
|
|
|
|
*/
|
|
|
|
static struct timespec rt_clock_base;
|
|
|
|
|
2018-01-17 08:39:42 +01:00
|
|
|
/**
|
|
|
|
* @brief Get clock time specified by clock_id.
|
|
|
|
*
|
|
|
|
* See IEEE 1003.1
|
|
|
|
*/
|
2020-03-24 08:10:56 +01:00
|
|
|
int z_impl_clock_gettime(clockid_t clock_id, struct timespec *ts)
|
2018-01-17 08:39:42 +01:00
|
|
|
{
|
2020-05-27 18:26:57 +02:00
|
|
|
uint64_t elapsed_msecs;
|
2018-06-25 19:24:53 +02:00
|
|
|
struct timespec base;
|
|
|
|
|
|
|
|
switch (clock_id) {
|
|
|
|
case CLOCK_MONOTONIC:
|
|
|
|
base.tv_sec = 0;
|
|
|
|
base.tv_nsec = 0;
|
|
|
|
break;
|
2018-01-17 08:39:42 +01:00
|
|
|
|
2018-06-25 19:24:53 +02:00
|
|
|
case CLOCK_REALTIME:
|
|
|
|
base = rt_clock_base;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2018-01-17 08:39:42 +01:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
elapsed_msecs = k_uptime_get();
|
2020-05-27 18:26:57 +02:00
|
|
|
ts->tv_sec = (int32_t) (elapsed_msecs / MSEC_PER_SEC);
|
|
|
|
ts->tv_nsec = (int32_t) ((elapsed_msecs % MSEC_PER_SEC) *
|
2018-06-02 08:47:18 +02:00
|
|
|
USEC_PER_MSEC * NSEC_PER_USEC);
|
2018-01-17 08:39:42 +01:00
|
|
|
|
2018-06-25 19:24:53 +02:00
|
|
|
ts->tv_sec += base.tv_sec;
|
|
|
|
ts->tv_nsec += base.tv_nsec;
|
2020-01-22 11:50:56 +01:00
|
|
|
if (ts->tv_nsec >= NSEC_PER_SEC) {
|
2018-06-25 19:24:53 +02:00
|
|
|
ts->tv_sec++;
|
|
|
|
ts->tv_nsec -= NSEC_PER_SEC;
|
|
|
|
}
|
|
|
|
|
2018-01-17 08:39:42 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2018-05-23 19:13:50 +02:00
|
|
|
|
2020-03-24 08:10:56 +01:00
|
|
|
#ifdef CONFIG_USERSPACE
|
|
|
|
int z_vrfy_clock_gettime(clockid_t clock_id, struct timespec *ts)
|
|
|
|
{
|
|
|
|
Z_OOPS(Z_SYSCALL_MEMORY_WRITE(ts, sizeof(*ts)));
|
|
|
|
return z_impl_clock_gettime(clock_id, ts);
|
|
|
|
}
|
|
|
|
#include <syscalls/clock_gettime_mrsh.c>
|
|
|
|
#endif
|
|
|
|
|
2018-06-26 00:43:54 +02:00
|
|
|
/**
|
|
|
|
* @brief Set the time of the specified clock.
|
|
|
|
*
|
|
|
|
* See IEEE 1003.1.
|
|
|
|
*
|
|
|
|
* Note that only the `CLOCK_REALTIME` clock can be set using this
|
|
|
|
* call.
|
|
|
|
*/
|
|
|
|
int clock_settime(clockid_t clock_id, const struct timespec *tp)
|
|
|
|
{
|
|
|
|
struct timespec base;
|
|
|
|
|
|
|
|
if (clock_id != CLOCK_REALTIME) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
uint64_t elapsed_msecs = k_uptime_get();
|
|
|
|
int64_t delta = (int64_t)NSEC_PER_SEC * tp->tv_sec + tp->tv_nsec
|
2019-05-09 16:10:58 +02:00
|
|
|
- elapsed_msecs * USEC_PER_MSEC * NSEC_PER_USEC;
|
2018-06-26 00:43:54 +02:00
|
|
|
|
|
|
|
base.tv_sec = delta / NSEC_PER_SEC;
|
|
|
|
base.tv_nsec = delta % NSEC_PER_SEC;
|
|
|
|
|
|
|
|
rt_clock_base = base;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-23 19:13:50 +02:00
|
|
|
/**
|
|
|
|
* @brief Get current real time.
|
|
|
|
*
|
|
|
|
* See IEEE 1003.1
|
|
|
|
*/
|
|
|
|
int gettimeofday(struct timeval *tv, const void *tz)
|
|
|
|
{
|
|
|
|
struct timespec ts;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
/* As per POSIX, "if tzp is not a null pointer, the behavior
|
|
|
|
* is unspecified." "tzp" is the "tz" parameter above. */
|
2020-03-18 10:33:30 +01:00
|
|
|
ARG_UNUSED(tz);
|
2018-05-23 19:13:50 +02:00
|
|
|
|
|
|
|
res = clock_gettime(CLOCK_REALTIME, &ts);
|
|
|
|
tv->tv_sec = ts.tv_sec;
|
|
|
|
tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|