fsfw/src/fsfw/timemanager/ClockCommon.cpp

58 lines
1.5 KiB
C++
Raw Normal View History

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
ReturnValue_t Clock::convertUTCToTT(timeval utc, timeval *tt) {
2022-02-02 10:29:30 +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;
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-02-02 10:29:30 +01:00
return HasReturnvaluesIF::RETURN_OK;
2021-06-15 15:59:20 +02:00
}
ReturnValue_t Clock::setLeapSeconds(const uint16_t leapSeconds_) {
2022-02-02 10:29:30 +01:00
if (checkOrCreateClockMutex() != HasReturnvaluesIF::RETURN_OK) {
return HasReturnvaluesIF::RETURN_FAILED;
}
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-02-02 10:29:30 +01:00
return HasReturnvaluesIF::RETURN_OK;
2021-06-15 15:59:20 +02:00
}
ReturnValue_t Clock::getLeapSeconds(uint16_t *leapSeconds_) {
2022-02-02 10:29:30 +01:00
if (timeMutex == nullptr) {
return HasReturnvaluesIF::RETURN_FAILED;
}
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-02-02 10:29:30 +01:00
return HasReturnvaluesIF::RETURN_OK;
2021-06-15 15:59:20 +02:00
}
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();
if (mutexFactory == nullptr) {
return HasReturnvaluesIF::RETURN_FAILED;
}
timeMutex = mutexFactory->createMutex();
if (timeMutex == nullptr) {
return HasReturnvaluesIF::RETURN_FAILED;
}
}
return HasReturnvaluesIF::RETURN_OK;
2021-06-15 15:59:20 +02:00
}