2014-10-21 17:38:16 +02:00
|
|
|
/*
|
2014-09-03 14:18:11 +02:00
|
|
|
* Copyright (C) 2009, 2014 Kaspar Schleiser
|
|
|
|
* Copyright (C) 2013, 2014 Freie Universität Berlin
|
2010-12-10 17:31:26 +01:00
|
|
|
*
|
2014-09-03 14:18:11 +02:00
|
|
|
* 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.
|
2014-10-21 17:38:16 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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.
|
2014-09-03 14:18:11 +02:00
|
|
|
* @{
|
2014-10-21 17:38:16 +02:00
|
|
|
* @file
|
2014-09-03 14:18:11 +02:00
|
|
|
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
2010-12-10 17:31:26 +01:00
|
|
|
*/
|
|
|
|
|
2010-12-06 16:02:40 +01:00
|
|
|
#ifndef __VTIMER_H
|
2013-06-22 05:11:53 +02:00
|
|
|
#define __VTIMER_H
|
2010-12-06 16:02:40 +01:00
|
|
|
|
2013-12-18 20:47:33 +01:00
|
|
|
#include <time.h>
|
2014-02-25 12:13:34 +01:00
|
|
|
#include <sys/time.h>
|
2013-12-18 20:47:33 +01:00
|
|
|
|
2014-07-29 09:21:11 +02:00
|
|
|
#include "priority_queue.h"
|
2013-06-14 20:33:28 +02:00
|
|
|
#include "timex.h"
|
2013-12-03 12:27:49 +01:00
|
|
|
#include "msg.h"
|
2010-12-06 16:02:40 +01:00
|
|
|
|
2014-10-10 11:51:11 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2014-11-30 22:52:27 +01:00
|
|
|
/**
|
|
|
|
* @brief IPC message type for vtimer msg callback
|
|
|
|
*/
|
2011-06-24 18:54:20 +02:00
|
|
|
#define MSG_TIMER 12345
|
|
|
|
|
2010-12-10 17:31:26 +01:00
|
|
|
/**
|
2014-12-05 20:32:39 +01:00
|
|
|
* @brief A vtimer object.
|
2010-12-10 17:31:26 +01:00
|
|
|
*
|
2014-10-21 17:38:16 +02:00
|
|
|
* This structure is used for declaring a vtimer. This should not be used by
|
|
|
|
* programmers, use the vtimer_set_*-functions instead.
|
2010-12-10 17:31:26 +01:00
|
|
|
*
|
|
|
|
* \hideinitializer
|
|
|
|
*/
|
2010-12-06 16:02:40 +01:00
|
|
|
typedef struct vtimer_t {
|
2014-12-05 20:32:39 +01:00
|
|
|
/** entry in vtimer's internal priority queue */
|
2014-07-29 09:21:11 +02:00
|
|
|
priority_queue_node_t priority_queue_entry;
|
2014-12-05 20:32:39 +01:00
|
|
|
/** the absoule point in time when the timer expires */
|
2010-12-06 16:02:40 +01:00
|
|
|
timex_t absolute;
|
2014-12-05 20:32:39 +01:00
|
|
|
/** the action to perform when timer fires */
|
2014-04-18 21:43:43 +02:00
|
|
|
void (*action)(struct vtimer_t *timer);
|
2014-12-05 20:32:39 +01:00
|
|
|
/** optional argument for vtimer_t::action */
|
2013-06-22 05:11:53 +02:00
|
|
|
void *arg;
|
2014-12-05 20:32:39 +01:00
|
|
|
/** optional process id for vtimer_t::action to act on */
|
2014-07-06 22:57:56 +02:00
|
|
|
kernel_pid_t pid;
|
2010-12-06 16:02:40 +01:00
|
|
|
} vtimer_t;
|
|
|
|
|
2010-12-10 17:31:26 +01:00
|
|
|
/**
|
|
|
|
* @brief Current system time
|
|
|
|
* @return Time as timex_t since system boot
|
|
|
|
*/
|
2013-06-22 05:11:53 +02:00
|
|
|
void vtimer_now(timex_t *out);
|
2010-12-10 17:16:18 +01:00
|
|
|
|
2014-02-25 12:13:34 +01:00
|
|
|
/**
|
|
|
|
* @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);
|
|
|
|
|
2013-12-18 20:47:33 +01:00
|
|
|
/**
|
|
|
|
* @brief Returns the current time in broken down format
|
|
|
|
* @param[out] localt Pointer to structure to receive time
|
|
|
|
*/
|
|
|
|
void vtimer_get_localtime(struct tm *localt);
|
|
|
|
|
2010-12-10 17:31:26 +01:00
|
|
|
/**
|
|
|
|
* @brief Initializes the vtimer subsystem. To be called once at system initialization. Will be initialized by auto_init.
|
|
|
|
*
|
|
|
|
* @return always 0
|
|
|
|
*/
|
2013-02-06 13:20:21 +01:00
|
|
|
int vtimer_init(void);
|
2010-12-10 17:31:26 +01:00
|
|
|
|
2010-12-10 17:16:18 +01:00
|
|
|
/**
|
|
|
|
* @brief will cause the calling thread to be suspended from excecution until the number of microseconds has elapsed
|
2013-06-22 05:11:53 +02:00
|
|
|
* @param[in] us number of microseconds
|
2010-12-10 17:16:18 +01:00
|
|
|
* @return 0 on success, < 0 on error
|
|
|
|
*/
|
|
|
|
int vtimer_usleep(uint32_t us);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
int vtimer_sleep(timex_t time);
|
2010-12-10 17:31:26 +01:00
|
|
|
|
2010-12-10 17:16:18 +01:00
|
|
|
/**
|
2014-10-23 21:28:15 +02:00
|
|
|
* @brief set a vtimer with msg event handler of type @ref MSG_TIMER
|
2010-12-10 17:16:18 +01:00
|
|
|
* @param[in] t pointer to preinitialised vtimer_t
|
|
|
|
* @param[in] interval vtimer timex_t interval
|
|
|
|
* @param[in] pid process id
|
|
|
|
* @param[in] ptr message value
|
|
|
|
* @return 0 on success, < 0 on error
|
|
|
|
*/
|
2014-07-06 22:57:56 +02:00
|
|
|
int vtimer_set_msg(vtimer_t *t, timex_t interval, kernel_pid_t pid, void *ptr);
|
2010-12-10 17:16:18 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief set a vtimer with wakeup event
|
|
|
|
* @param[in] t pointer to preinitialised vtimer_t
|
2014-12-05 20:32:39 +01:00
|
|
|
* @param[in] interval the interval after which the timer shall fire
|
2010-12-10 17:16:18 +01:00
|
|
|
* @param[in] pid process id
|
|
|
|
* @return 0 on success, < 0 on error
|
|
|
|
*/
|
2014-07-06 22:57:56 +02:00
|
|
|
int vtimer_set_wakeup(vtimer_t *t, timex_t interval, kernel_pid_t pid);
|
2010-12-10 17:16:18 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief remove a vtimer
|
|
|
|
* @param[in] t pointer to preinitialised vtimer_t
|
|
|
|
* @return 0 on success, < 0 on error
|
|
|
|
*/
|
|
|
|
int vtimer_remove(vtimer_t *t);
|
2010-12-06 16:02:40 +01:00
|
|
|
|
2013-12-03 12:27:49 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
int vtimer_msg_receive_timeout(msg_t *m, timex_t timeout);
|
|
|
|
|
2013-10-10 17:06:41 +02:00
|
|
|
#if ENABLE_DEBUG
|
|
|
|
|
2013-06-14 20:38:27 +02:00
|
|
|
/**
|
|
|
|
* @brief Prints a vtimer_t
|
|
|
|
*/
|
2013-08-15 19:13:21 +02:00
|
|
|
void vtimer_print(vtimer_t *t);
|
2013-06-14 20:38:27 +02:00
|
|
|
|
2013-03-15 17:48:13 +01:00
|
|
|
/**
|
|
|
|
* @brief Prints the vtimer shortterm queue (use for debug purposes)
|
|
|
|
*/
|
2013-06-30 01:53:53 +02:00
|
|
|
void vtimer_print_short_queue(void);
|
2013-03-15 17:48:13 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Prints the vtimer longterm queue (use for debug purposes)
|
|
|
|
*/
|
2013-06-30 01:53:53 +02:00
|
|
|
void vtimer_print_long_queue(void);
|
2013-03-15 17:48:13 +01:00
|
|
|
|
2013-10-10 17:06:41 +02:00
|
|
|
#endif
|
|
|
|
|
2014-10-10 11:51:11 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-11-27 17:54:30 +01:00
|
|
|
/** @} */
|
2010-12-06 16:02:40 +01:00
|
|
|
#endif /* __VTIMER_H */
|