fsfw/src/fsfw/timemanager/ClockCommon.cpp

156 lines
3.9 KiB
C++
Raw Normal View History

2023-01-26 11:33:40 +01:00
#include <cstdlib>
2023-01-26 13:40:44 +01:00
#include <ctime>
2023-01-26 15:30:23 +01:00
#include "boost_timegm.h"
2021-07-13 20:22:54 +02:00
#include "fsfw/ipc/MutexGuard.h"
2022-02-02 10:29:30 +01:00
#include "fsfw/timemanager/Clock.h"
2021-06-15 15:59:20 +02:00
uint16_t Clock::leapSeconds = 0;
MutexIF* Clock::timeMutex = nullptr;
bool Clock::leapSecondsSet = false;
ReturnValue_t Clock::convertUTCToTT(timeval utc, timeval* tt) {
2022-02-02 10:29:30 +01:00
uint16_t leapSeconds;
ReturnValue_t result = getLeapSeconds(&leapSeconds);
2022-08-15 20:28:16 +02:00
if (result != returnvalue::OK) {
2022-02-02 10:29:30 +01:00
return result;
}
timeval leapSeconds_timeval = {0, 0};
leapSeconds_timeval.tv_sec = leapSeconds;
2021-06-15 15:59:20 +02:00
2022-02-02 10:29:30 +01:00
// initial offset between UTC and TAI
timeval UTCtoTAI1972 = {10, 0};
2021-06-15 15:59:20 +02:00
2022-02-02 10:29:30 +01:00
timeval TAItoTT = {32, 184000};
2021-06-15 15:59:20 +02:00
2022-02-02 10:29:30 +01:00
*tt = utc + leapSeconds_timeval + UTCtoTAI1972 + TAItoTT;
2021-06-15 15:59:20 +02:00
2022-08-15 20:28:16 +02:00
return returnvalue::OK;
2021-06-15 15:59:20 +02:00
}
ReturnValue_t Clock::setLeapSeconds(const uint16_t leapSeconds_) {
2022-08-15 20:28:16 +02:00
if (checkOrCreateClockMutex() != returnvalue::OK) {
return returnvalue::FAILED;
2022-02-02 10:29:30 +01:00
}
MutexGuard helper(timeMutex);
2021-06-15 15:59:20 +02:00
2022-02-02 10:29:30 +01:00
leapSeconds = leapSeconds_;
leapSecondsSet = true;
2021-06-15 15:59:20 +02:00
2022-08-15 20:28:16 +02:00
return returnvalue::OK;
2021-06-15 15:59:20 +02:00
}
ReturnValue_t Clock::getLeapSeconds(uint16_t* leapSeconds_) {
2022-03-25 18:48:53 +01:00
if (not leapSecondsSet) {
2022-08-15 20:28:16 +02:00
return returnvalue::FAILED;
}
2022-08-15 20:28:16 +02:00
if (checkOrCreateClockMutex() != returnvalue::OK) {
return returnvalue::FAILED;
2022-02-02 10:29:30 +01:00
}
MutexGuard helper(timeMutex);
2021-06-15 15:59:20 +02:00
2022-02-02 10:29:30 +01:00
*leapSeconds_ = leapSeconds;
2021-06-15 15:59:20 +02:00
2022-08-15 20:28:16 +02:00
return returnvalue::OK;
2021-06-15 15:59:20 +02:00
}
ReturnValue_t Clock::convertTimevalToTimeOfDay(const timeval* from, TimeOfDay_t* to) {
2023-01-26 00:01:40 +01:00
struct tm time_tm;
// WINDOWS does not provide gmtime_r, but gmtime_s
2023-01-26 15:30:23 +01:00
#ifdef _MSC_VER
time_t seconds = from->tv_sec;
errno_t result = gmtime_s(&time_tm, &seconds);
2023-01-26 00:01:40 +01:00
if (result != 0) {
return returnvalue::FAILED;
}
2022-09-27 21:46:11 +02:00
#else
2023-01-26 00:01:40 +01:00
void* result = gmtime_r(&from->tv_sec, &time_tm);
if (result == nullptr) {
return returnvalue::FAILED;
}
2022-09-27 21:46:11 +02:00
#endif
2023-01-26 00:01:40 +01:00
to->year = time_tm.tm_year + 1900;
to->month = time_tm.tm_mon + 1;
to->day = time_tm.tm_mday;
to->hour = time_tm.tm_hour;
to->minute = time_tm.tm_min;
to->second = time_tm.tm_sec;
to->usecond = from->tv_usec;
2022-08-15 20:28:16 +02:00
return returnvalue::OK;
}
2023-01-26 00:01:40 +01:00
ReturnValue_t Clock::convertTimeOfDayToTimeval(const TimeOfDay_t* from, timeval* to) {
struct tm time_tm = {};
time_tm.tm_year = from->year - 1900;
time_tm.tm_mon = from->month - 1;
time_tm.tm_mday = from->day;
time_tm.tm_hour = from->hour;
time_tm.tm_min = from->minute;
time_tm.tm_sec = from->second;
time_tm.tm_isdst = 0;
2023-01-26 15:30:23 +01:00
time_t seconds = internal_timegm(&time_tm);
2023-01-26 00:01:40 +01:00
to->tv_sec = seconds;
to->tv_usec = from->usecond;
return returnvalue::OK;
}
ReturnValue_t Clock::convertTimevalToJD2000(timeval time, double* JD2000) {
*JD2000 = (time.tv_sec - 946728000. + time.tv_usec / 1000000.) / 24. / 3600.;
return returnvalue::OK;
}
2021-06-15 16:12:25 +02:00
ReturnValue_t Clock::checkOrCreateClockMutex() {
2022-02-02 10:29:30 +01:00
if (timeMutex == nullptr) {
MutexFactory* mutexFactory = MutexFactory::instance();
2022-02-02 10:29:30 +01:00
if (mutexFactory == nullptr) {
2022-08-15 20:28:16 +02:00
return returnvalue::FAILED;
2022-02-02 10:29:30 +01:00
}
timeMutex = mutexFactory->createMutex();
if (timeMutex == nullptr) {
2022-08-15 20:28:16 +02:00
return returnvalue::FAILED;
2022-02-02 10:29:30 +01:00
}
}
2022-08-15 20:28:16 +02:00
return returnvalue::OK;
2021-06-15 15:59:20 +02:00
}
2023-01-26 00:01:40 +01:00
ReturnValue_t Clock::getDateAndTime(TimeOfDay_t* time) {
timeval time_timeval;
ReturnValue_t result = getClock_timeval(&time_timeval);
if (result != returnvalue::OK) {
return result;
}
return convertTimevalToTimeOfDay(&time_timeval, time);
}
ReturnValue_t Clock::getClock_usecs(uint64_t* time) {
timeval timeVal{};
ReturnValue_t result = getClock_timeval(&timeVal);
if (result != returnvalue::OK) {
return result;
}
*time = static_cast<uint64_t>(timeVal.tv_sec) * 1e6 + timeVal.tv_usec;
return returnvalue::OK;
}
ReturnValue_t Clock::setClock(const TimeOfDay_t* time) {
timeval timeTimeval{};
ReturnValue_t result = convertTimeOfDayToTimeval(time, &timeTimeval);
if (result != returnvalue::OK) {
return result;
}
return setClock(&timeTimeval);
}
uint32_t Clock::getUptime_ms() {
timeval uptime = getUptime();
2023-01-26 11:33:40 +01:00
// TODO verify that overflow is correct
2023-01-26 00:01:40 +01:00
return uptime.tv_sec * 1e3 + uptime.tv_usec / 1e3;
}