diff --git a/Makefile.dep b/Makefile.dep index 94c2441f7e7a9f6ed724616c52106d9c917c7dee..8234fc8296b9be056b48b5dd41bcb99ac2a5ad54 100644 --- a/Makefile.dep +++ b/Makefile.dep @@ -261,6 +261,7 @@ ifneq (,$(filter cbor,$(USEMODULE))) endif ifneq (,$(filter vtimer,$(USEMODULE))) + USEMODULE += xtimer USEMODULE += timex endif diff --git a/sys/Makefile b/sys/Makefile index aceb6dbf542fb8d8257fcd209317ef8c28956ed5..d133ab8fc440d4be1a2828a267fcb2a6d34d7bbc 100644 --- a/sys/Makefile +++ b/sys/Makefile @@ -58,6 +58,9 @@ endif ifneq (,$(filter log_%,$(USEMODULE))) DIRS += log endif +ifneq (,$(filter vtimer,$(USEMODULE))) + DIRS += compat/vtimer +endif ifneq (,$(filter cpp11-compat,$(USEMODULE))) DIRS += cpp11-compat endif diff --git a/sys/compat/vtimer/Makefile b/sys/compat/vtimer/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..48422e909a47d7cd428d10fa73825060ccc8d8c2 --- /dev/null +++ b/sys/compat/vtimer/Makefile @@ -0,0 +1 @@ +include $(RIOTBASE)/Makefile.base diff --git a/sys/compat/vtimer/vtimer_compat.c b/sys/compat/vtimer/vtimer_compat.c new file mode 100644 index 0000000000000000000000000000000000000000..0a35908a75f0211e726f3696d759b6e4a9e6c6f3 --- /dev/null +++ b/sys/compat/vtimer/vtimer_compat.c @@ -0,0 +1,94 @@ +/* + * Copyright (C) 2015 Kaspar Schleiser <kaspar@schleiser.de> + * + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup sys_compat + * @{ + * + * @file + * @brief Implementation of the vtimer interface over xtimers + * + * @author Kaspar Schleiser <kaspar@schleiser.de> + * + * @} + */ + +#include "xtimer.h" +#include "timex.h" +#include "vtimer.h" + +#include <time.h> +#include <sys/time.h> +#include <string.h> + +int vtimer_sleep(timex_t time) +{ + xtimer_usleep64(timex_uint64(time)); + return 0; +} + +int vtimer_msg_receive_timeout(msg_t *m, timex_t timeout) +{ + uint64_t _timeout = timex_uint64(timeout); + return xtimer_msg_receive_timeout64(m, _timeout); +} + +void vtimer_now(timex_t *out) +{ + *out = timex_from_uint64(xtimer_now64()); +} + +void vtimer_get_localtime(struct tm *localt) +{ + timex_t now; + vtimer_now(&now); + + memset(localt, 0, sizeof(struct tm)); + localt->tm_sec = now.seconds % 60; + localt->tm_min = (now.seconds / 60) % 60; + localt->tm_hour = (now.seconds / 60 / 60) % 24; +} + +void vtimer_set_msg(vtimer_t *t, timex_t interval, kernel_pid_t pid, uint16_t type, void *ptr) +{ + t->msg.type = type; + t->msg.content.ptr = ptr; + + xtimer_set_msg64(&t->timer, timex_uint64(interval), &t->msg, pid); +} + +int vtimer_set_wakeup(vtimer_t *t, timex_t interval, kernel_pid_t pid) +{ + xtimer_set_wakeup64(&t->timer, timex_uint64(interval), pid); + + return 0; +} + +void vtimer_remove(vtimer_t *t) +{ + xtimer_remove(&t->timer); +} + +/* + * This is a workaround for missing support in clang on OSX, + * the alias is not needed in native. + * Compare https://github.com/RIOT-OS/RIOT/issues/2336 + */ +#ifndef BOARD_NATIVE +void _gettimeofday(void) __attribute__ ((weak, alias("vtimer_gettimeofday"))); +#endif + +void vtimer_gettimeofday(struct timeval *tp) +{ + timex_t now; + vtimer_now(&now); + + tp->tv_sec = now.seconds; + tp->tv_usec = now.microseconds; +} diff --git a/sys/include/vtimer.h b/sys/include/vtimer.h new file mode 100644 index 0000000000000000000000000000000000000000..3c5563677177c1bae91d2705fdf2e2ff41bc6bb0 --- /dev/null +++ b/sys/include/vtimer.h @@ -0,0 +1,138 @@ +/* + * Copyright (C) 2009, 2014, 2015 Kaspar Schleiser <kaspar@schleiser.de> + * Copyright (C) 2013, 2014 Freie Universität Berlin + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @defgroup sys_vtimer Virtual Timer + * @ingroup sys + * @brief Provides a high level abstraction timer module to register + * timers, get current system time, and let a thread sleep for a certain amount + * of time. It does not give any timing guarantees. + * + * @deprecated + * + * @{ + * @file + * @brief vtimer interface definitions + * @author Kaspar Schleiser <kaspar@schleiser.de> + */ + +#ifndef VTIMER_H +#define VTIMER_H + +#include "msg.h" +#include "timex.h" +#include "xtimer.h" + +#include <time.h> +#include <sys/time.h> + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief IPC message type for vtimer msg callback + */ +#define MSG_TIMER 12345 + +/** + * @brief A vtimer object. + * + * This structure is used for declaring a vtimer. This should not be used by + * programmers, use the vtimer_set_*-functions instead. + * + * \hideinitializer + * @deprecated + */ +typedef struct vtimer { + xtimer_t timer; + msg_t msg; +} vtimer_t; + +/** + * @brief Current system time + * @return Time as timex_t since system boot + * @deprecated + */ +void vtimer_now(timex_t *out); + +/** + * @brief will cause the calling thread to be suspended from excecution until the time specified by time has elapsed + * @param[in] time timex_t with time to suspend execution + * @return 0 on success, < 0 on error + * @deprecated + */ +int vtimer_sleep(timex_t time); + +/** + * @brief receive a message but return in case of timeout time is passed by without a new message + * @param[out] m pointer to a msg_t which will be filled in case of no timeout + * @param[in] timeout timex_t containing the relative time to fire the timeout + * @return < 0 on error, other value otherwise + * @deprecated + */ +int vtimer_msg_receive_timeout(msg_t *m, timex_t timeout); + +/** + * @brief Returns the current time in broken down format + * @param[out] localt Pointer to structure to receive time + * @deprecated + */ +void vtimer_get_localtime(struct tm *localt); + +/** + * @brief set a vtimer with msg event handler of the specified type + * @param[in] t pointer to preinitialised vtimer_t + * @param[in] interval vtimer timex_t interval + * @param[in] pid process id + * @param[in] type value for the msg_t type + * @param[in] ptr message value + * @deprecated + */ +void vtimer_set_msg(vtimer_t *t, timex_t interval, kernel_pid_t pid, uint16_t type, void *ptr); + +/** + * @brief set a vtimer with wakeup event + * @param[in] t pointer to preinitialised vtimer_t + * @param[in] interval the interval after which the timer shall fire + * @param[in] pid process id + * @return 0 on success, < 0 on error + */ +int vtimer_set_wakeup(vtimer_t *t, timex_t interval, kernel_pid_t pid); + +/** + * @brief Get the current time in seconds and microseconds since system start + * @param[in] tp Uptime will be stored in the timeval structure pointed to by tp + */ +void vtimer_gettimeofday(struct timeval *tp); + +/** + * @brief remove a vtimer + * @param[in] t pointer to preinitialised vtimer_t + * @deprecated + */ +void vtimer_remove(vtimer_t *t); + +/** + * @brief will cause the calling thread to be suspended from excecution until the number of microseconds has elapsed + * @param[in] us number of microseconds + * @return 0 on success, < 0 on error + */ +static inline int vtimer_usleep(uint32_t us) +{ + xtimer_usleep(us); + return 0; +} + +#ifdef __cplusplus +} +#endif + +/** @} */ +#endif /* VTIMER_H */