fsfw/osal/FreeRTOS/Clock.cpp

205 lines
5.2 KiB
C++
Raw Normal View History

2020-12-14 11:17:22 +01:00
#include "Timekeeper.h"
2020-08-13 20:53:35 +02:00
#include "../../timemanager/Clock.h"
#include "../../globalfunctions/timevalOperations.h"
2018-07-13 15:56:37 +02:00
2020-05-25 15:06:54 +02:00
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
2018-07-13 15:56:37 +02:00
2020-05-29 17:49:39 +02:00
#include <stdlib.h>
#include <time.h>
2018-07-13 15:56:37 +02:00
//TODO sanitize input?
//TODO much of this code can be reused for tick-only systems
uint16_t Clock::leapSeconds = 0;
2020-05-29 17:49:39 +02:00
MutexIF* Clock::timeMutex = nullptr;
2018-07-13 15:56:37 +02:00
uint32_t Clock::getTicksPerSecond(void) {
2021-03-03 17:13:37 +01:00
return 1000;
2018-07-13 15:56:37 +02:00
}
ReturnValue_t Clock::setClock(const TimeOfDay_t* time) {
2021-03-03 17:13:37 +01:00
timeval time_timeval;
2018-07-13 15:56:37 +02:00
2021-03-03 17:13:37 +01:00
ReturnValue_t result = convertTimeOfDayToTimeval(time, &time_timeval);
if (result != HasReturnvaluesIF::RETURN_OK){
return result;
}
2018-07-13 15:56:37 +02:00
2021-03-03 17:13:37 +01:00
return setClock(&time_timeval);
2018-07-13 15:56:37 +02:00
}
ReturnValue_t Clock::setClock(const timeval* time) {
2021-03-03 17:13:37 +01:00
timeval uptime = getUptime();
2018-07-13 15:56:37 +02:00
2021-03-03 17:13:37 +01:00
timeval offset = *time - uptime;
2018-07-13 15:56:37 +02:00
2021-03-03 17:13:37 +01:00
Timekeeper::instance()->setOffset(offset);
2018-07-13 15:56:37 +02:00
2021-03-03 17:13:37 +01:00
return HasReturnvaluesIF::RETURN_OK;
2018-07-13 15:56:37 +02:00
}
ReturnValue_t Clock::getClock_timeval(timeval* time) {
2021-03-03 17:13:37 +01:00
timeval uptime = getUptime();
2018-07-13 15:56:37 +02:00
2021-03-03 17:13:37 +01:00
timeval offset = Timekeeper::instance()->getOffset();
2018-07-13 15:56:37 +02:00
2021-03-03 17:13:37 +01:00
*time = offset + uptime;
2018-07-13 15:56:37 +02:00
2021-03-03 17:13:37 +01:00
return HasReturnvaluesIF::RETURN_OK;
2018-07-13 15:56:37 +02:00
}
ReturnValue_t Clock::getUptime(timeval* uptime) {
2021-03-03 17:13:37 +01:00
*uptime = getUptime();
2018-07-13 15:56:37 +02:00
2021-03-03 17:13:37 +01:00
return HasReturnvaluesIF::RETURN_OK;
2018-07-13 15:56:37 +02:00
}
timeval Clock::getUptime() {
2021-03-03 17:13:37 +01:00
TickType_t ticksSinceStart = xTaskGetTickCount();
return Timekeeper::ticksToTimeval(ticksSinceStart);
2018-07-13 15:56:37 +02:00
}
ReturnValue_t Clock::getUptime(uint32_t* uptimeMs) {
2021-03-03 17:13:37 +01:00
timeval uptime = getUptime();
*uptimeMs = uptime.tv_sec * 1000 + uptime.tv_usec / 1000;
return HasReturnvaluesIF::RETURN_OK;
2018-07-13 15:56:37 +02:00
}
2020-12-14 11:17:22 +01:00
//uint32_t Clock::getUptimeSeconds() {
// timeval uptime = getUptime();
// return uptime.tv_sec;
//}
2018-07-13 15:56:37 +02:00
ReturnValue_t Clock::getClock_usecs(uint64_t* time) {
2021-03-03 17:13:37 +01:00
timeval time_timeval;
ReturnValue_t result = getClock_timeval(&time_timeval);
if (result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
*time = time_timeval.tv_sec * 1000000 + time_timeval.tv_usec;
return HasReturnvaluesIF::RETURN_OK;
2018-07-13 15:56:37 +02:00
}
ReturnValue_t Clock::getDateAndTime(TimeOfDay_t* time) {
2021-03-03 17:13:37 +01:00
timeval time_timeval;
ReturnValue_t result = getClock_timeval(&time_timeval);
if (result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
struct tm time_tm;
2018-07-13 15:56:37 +02:00
2021-03-03 17:13:37 +01:00
gmtime_r(&time_timeval.tv_sec,&time_tm);
2018-07-13 15:56:37 +02:00
2021-03-03 17:13:37 +01:00
time->year = time_tm.tm_year + 1900;
time->month = time_tm.tm_mon + 1;
time->day = time_tm.tm_mday;
2018-07-13 15:56:37 +02:00
2021-03-03 17:13:37 +01:00
time->hour = time_tm.tm_hour;
time->minute = time_tm.tm_min;
time->second = time_tm.tm_sec;
2018-07-13 15:56:37 +02:00
2021-03-03 17:13:37 +01:00
time->usecond = time_timeval.tv_usec;
2018-07-13 15:56:37 +02:00
2021-03-03 17:13:37 +01:00
return HasReturnvaluesIF::RETURN_OK;
2018-07-13 15:56:37 +02:00
}
ReturnValue_t Clock::convertTimeOfDayToTimeval(const TimeOfDay_t* from,
2021-03-03 17:13:37 +01:00
timeval* to) {
2021-04-08 22:49:36 +02:00
struct tm time_tm = {};
2018-07-13 15:56:37 +02:00
2021-03-03 17:13:37 +01:00
time_tm.tm_year = from->year - 1900;
time_tm.tm_mon = from->month - 1;
time_tm.tm_mday = from->day;
2018-07-13 15:56:37 +02:00
2021-03-03 17:13:37 +01:00
time_tm.tm_hour = from->hour;
time_tm.tm_min = from->minute;
time_tm.tm_sec = from->second;
2018-07-13 15:56:37 +02:00
2021-03-03 17:13:37 +01:00
time_t seconds = mktime(&time_tm);
2018-07-13 15:56:37 +02:00
2021-03-03 17:13:37 +01:00
to->tv_sec = seconds;
to->tv_usec = from->usecond;
//Fails in 2038..
return HasReturnvaluesIF::RETURN_OK;
2018-07-13 15:56:37 +02:00
}
ReturnValue_t Clock::convertTimevalToJD2000(timeval time, double* JD2000) {
2021-03-03 17:13:37 +01:00
*JD2000 = (time.tv_sec - 946728000. + time.tv_usec / 1000000.) / 24.
/ 3600.;
return HasReturnvaluesIF::RETURN_OK;
2018-07-13 15:56:37 +02:00
}
ReturnValue_t Clock::convertUTCToTT(timeval utc, timeval* tt) {
2021-03-03 17:13:37 +01:00
//SHOULDDO: works not for dates in the past (might have less leap seconds)
if (timeMutex == nullptr) {
return HasReturnvaluesIF::RETURN_FAILED;
}
2018-07-13 15:56:37 +02:00
2021-03-03 17:13:37 +01:00
uint16_t leapSeconds;
ReturnValue_t result = getLeapSeconds(&leapSeconds);
if (result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
timeval leapSeconds_timeval = { 0, 0 };
leapSeconds_timeval.tv_sec = leapSeconds;
2018-07-13 15:56:37 +02:00
2021-03-03 17:13:37 +01:00
//initial offset between UTC and TAI
timeval UTCtoTAI1972 = { 10, 0 };
2018-07-13 15:56:37 +02:00
2021-03-03 17:13:37 +01:00
timeval TAItoTT = { 32, 184000 };
2018-07-13 15:56:37 +02:00
2021-03-03 17:13:37 +01:00
*tt = utc + leapSeconds_timeval + UTCtoTAI1972 + TAItoTT;
2018-07-13 15:56:37 +02:00
2021-03-03 17:13:37 +01:00
return HasReturnvaluesIF::RETURN_OK;
2018-07-13 15:56:37 +02:00
}
ReturnValue_t Clock::setLeapSeconds(const uint16_t leapSeconds_) {
2021-03-03 17:13:37 +01:00
if (checkOrCreateClockMutex() != HasReturnvaluesIF::RETURN_OK) {
return HasReturnvaluesIF::RETURN_FAILED;
}
ReturnValue_t result = timeMutex->lockMutex(MutexIF::TimeoutType::BLOCKING);
if (result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
leapSeconds = leapSeconds_;
result = timeMutex->unlockMutex();
return result;
2018-07-13 15:56:37 +02:00
}
ReturnValue_t Clock::getLeapSeconds(uint16_t* leapSeconds_) {
2021-03-03 17:13:37 +01:00
if (timeMutex == NULL) {
return HasReturnvaluesIF::RETURN_FAILED;
}
ReturnValue_t result = timeMutex->lockMutex(MutexIF::TimeoutType::BLOCKING);
if (result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
*leapSeconds_ = leapSeconds;
result = timeMutex->unlockMutex();
return result;
2018-07-13 15:56:37 +02:00
}
ReturnValue_t Clock::checkOrCreateClockMutex() {
2021-03-03 17:13:37 +01:00
if (timeMutex == NULL) {
MutexFactory* mutexFactory = MutexFactory::instance();
if (mutexFactory == NULL) {
return HasReturnvaluesIF::RETURN_FAILED;
}
timeMutex = mutexFactory->createMutex();
if (timeMutex == NULL) {
return HasReturnvaluesIF::RETURN_FAILED;
}
}
return HasReturnvaluesIF::RETURN_OK;
2018-07-13 15:56:37 +02:00
}