2013-03-06 10:29:49 +01:00
|
|
|
/**
|
|
|
|
* Native CPU rtc.h implementation
|
|
|
|
*
|
2013-03-13 21:56:56 +01:00
|
|
|
* The native rtc implementation uses POSIX system calls to simulate a
|
|
|
|
* real-time clock.
|
|
|
|
*
|
|
|
|
* Setting the clock will be implemented using a delta variable.
|
|
|
|
*
|
2013-03-06 10:29:49 +01:00
|
|
|
* Copyright (C) 2013 Ludwig Ortmann
|
|
|
|
*
|
2013-11-22 20:47:05 +01:00
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser General Public
|
2013-03-06 10:29:49 +01:00
|
|
|
* License. See the file LICENSE in the top level directory for more details.
|
|
|
|
*
|
|
|
|
* @author Ludwig Ortmann <ludwig.ortmann@fu-berlin.de>
|
2013-03-13 21:56:56 +01:00
|
|
|
*
|
|
|
|
* @ingroup native_cpu
|
|
|
|
* @ingroup rtc
|
|
|
|
* @file
|
2013-03-06 10:29:49 +01:00
|
|
|
*/
|
|
|
|
|
2013-03-06 01:08:15 +01:00
|
|
|
#include <time.h>
|
2013-11-20 15:25:11 +01:00
|
|
|
#include <stdlib.h>
|
2013-03-07 13:53:14 +01:00
|
|
|
#include <err.h>
|
2013-03-06 01:08:15 +01:00
|
|
|
|
|
|
|
#include "debug.h"
|
|
|
|
|
2013-09-30 15:31:40 +02:00
|
|
|
#include "rtc.h"
|
2013-09-30 15:44:22 +02:00
|
|
|
#include "cpu.h"
|
2013-03-06 01:08:15 +01:00
|
|
|
|
2013-11-07 17:23:08 +01:00
|
|
|
#include "native_internal.h"
|
|
|
|
|
2013-03-06 01:08:15 +01:00
|
|
|
static int native_rtc_enabled;
|
|
|
|
|
|
|
|
void rtc_init(void)
|
|
|
|
{
|
|
|
|
native_rtc_enabled = 0;
|
|
|
|
printf("native rtc initialized\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void rtc_enable(void)
|
|
|
|
{
|
|
|
|
DEBUG("rtc_enable\n");
|
|
|
|
native_rtc_enabled = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rtc_disable(void)
|
|
|
|
{
|
|
|
|
DEBUG("rtc_disable()\n");
|
|
|
|
native_rtc_enabled = 0;
|
|
|
|
}
|
|
|
|
|
2013-06-21 03:52:57 +02:00
|
|
|
void rtc_set_localtime(struct tm *localt)
|
2013-03-06 01:08:15 +01:00
|
|
|
{
|
|
|
|
DEBUG("rtc_set_localtime()\n");
|
2013-08-21 15:09:35 +02:00
|
|
|
|
|
|
|
(void)localt; /* not implemented atm */
|
2013-03-06 01:08:15 +01:00
|
|
|
printf("setting time not supported.");
|
|
|
|
}
|
|
|
|
|
2013-06-21 03:52:57 +02:00
|
|
|
void rtc_get_localtime(struct tm *localt)
|
2013-03-06 01:08:15 +01:00
|
|
|
{
|
|
|
|
time_t t;
|
2013-06-21 03:52:57 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (native_rtc_enabled == 1) {
|
2013-11-07 17:23:08 +01:00
|
|
|
_native_syscall_enter();
|
2013-03-06 01:08:15 +01:00
|
|
|
t = time(NULL);
|
2013-06-21 03:52:57 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (localtime_r(&t, localt) == NULL) {
|
2013-11-20 15:25:11 +01:00
|
|
|
err(EXIT_FAILURE, "rtc_get_localtime: localtime_r");
|
2013-03-06 01:08:15 +01:00
|
|
|
}
|
2013-11-07 17:23:08 +01:00
|
|
|
_native_syscall_leave();
|
2013-03-06 01:08:15 +01:00
|
|
|
}
|
|
|
|
}
|
2013-10-19 20:17:44 +02:00
|
|
|
|
|
|
|
time_t rtc_time(struct timeval *time)
|
|
|
|
{
|
|
|
|
if (native_rtc_enabled == 1) {
|
2013-11-07 17:23:08 +01:00
|
|
|
_native_syscall_enter();
|
2013-10-26 11:46:29 +02:00
|
|
|
if (gettimeofday(time, NULL) == -1) {
|
2013-11-20 15:25:11 +01:00
|
|
|
err(EXIT_FAILURE, "rtc_time: gettimeofday");
|
2013-10-19 20:17:44 +02:00
|
|
|
}
|
2013-11-07 17:23:08 +01:00
|
|
|
_native_syscall_leave();
|
2013-10-19 20:17:44 +02:00
|
|
|
}
|
|
|
|
return time->tv_sec;
|
|
|
|
}
|