1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-17 04:52:59 +01:00

sys: introduce vtimer_compat

This commit is contained in:
Kaspar Schleiser 2015-07-29 19:17:15 +02:00
parent 2cee6b036b
commit 5e7036eed9
5 changed files with 237 additions and 0 deletions

View File

@ -261,6 +261,7 @@ ifneq (,$(filter cbor,$(USEMODULE)))
endif
ifneq (,$(filter vtimer,$(USEMODULE)))
USEMODULE += xtimer
USEMODULE += timex
endif

View File

@ -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

View File

@ -0,0 +1 @@
include $(RIOTBASE)/Makefile.base

View File

@ -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;
}

138
sys/include/vtimer.h Normal file
View File

@ -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 */