fsfw/src/fsfw/osal/host/Clock.cpp

81 lines
2.7 KiB
C++
Raw Normal View History

#include "fsfw/timemanager/Clock.h"
2020-09-05 20:18:52 +02:00
#include <chrono>
2021-05-12 16:47:53 +02:00
2022-03-25 18:48:53 +01:00
#include "fsfw/ipc/MutexGuard.h"
2022-02-02 10:29:30 +01:00
#include "fsfw/platform.h"
#include "fsfw/serviceinterface/ServiceInterface.h"
2021-05-12 16:47:53 +02:00
#if defined(PLATFORM_WIN)
2021-01-29 00:12:40 +01:00
#include <sysinfoapi.h>
2022-09-27 21:46:11 +02:00
#define timegm _mkgmtime
2021-05-12 16:47:53 +02:00
#elif defined(PLATFORM_UNIX)
2020-09-05 20:18:52 +02:00
#include <fstream>
#endif
using SystemClock = std::chrono::system_clock;
ReturnValue_t Clock::setClock(const timeval* time) {
2022-02-02 10:29:30 +01:00
/* I don't know why someone would need to set a clock which is probably perfectly fine on a
host system with internet access so this is not implemented for now. */
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::warning << "Clock::setClock: Not implemented for host OSAL" << std::endl;
2021-01-29 02:35:08 +01:00
#else
2022-02-02 10:29:30 +01:00
sif::printWarning("Clock::setClock: Not implemented for host OSAL\n");
#endif
2022-08-16 01:08:26 +02:00
return returnvalue::OK;
2020-09-05 20:18:52 +02:00
}
ReturnValue_t Clock::getClock_timeval(timeval* time) {
2021-05-12 17:32:40 +02:00
#if defined(PLATFORM_WIN)
2022-02-02 10:29:30 +01:00
auto now = std::chrono::system_clock::now();
auto secondsChrono = std::chrono::time_point_cast<std::chrono::seconds>(now);
auto epoch = now.time_since_epoch();
time->tv_sec = std::chrono::duration_cast<std::chrono::seconds>(epoch).count();
auto fraction = now - secondsChrono;
time->tv_usec = std::chrono::duration_cast<std::chrono::microseconds>(fraction).count();
2022-08-16 01:08:26 +02:00
return returnvalue::OK;
2021-05-12 17:32:40 +02:00
#elif defined(PLATFORM_UNIX)
2022-02-02 10:29:30 +01:00
timespec timeUnix;
int status = clock_gettime(CLOCK_REALTIME, &timeUnix);
if (status != 0) {
2022-08-16 01:08:26 +02:00
return returnvalue::FAILED;
2022-02-02 10:29:30 +01:00
}
time->tv_sec = timeUnix.tv_sec;
time->tv_usec = timeUnix.tv_nsec / 1000.0;
2022-08-16 01:08:26 +02:00
return returnvalue::OK;
2020-09-05 20:18:52 +02:00
#else
2023-01-26 00:01:40 +01:00
#warning Clock::getClock_timeval() not implemented for your platform
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::warning << "Clock::getUptime: Not implemented for found OS!" << std::endl;
2021-01-25 00:55:27 +01:00
#else
2022-02-02 10:29:30 +01:00
sif::printWarning("Clock::getUptime: Not implemented for found OS!\n");
#endif
2022-08-16 01:08:26 +02:00
return returnvalue::FAILED;
2020-09-05 20:18:52 +02:00
#endif
}
timeval Clock::getUptime() {
2022-02-02 10:29:30 +01:00
timeval timeval;
2021-05-12 17:32:40 +02:00
#if defined(PLATFORM_WIN)
2022-02-02 10:29:30 +01:00
auto uptime = std::chrono::milliseconds(GetTickCount64());
auto secondsChrono = std::chrono::duration_cast<std::chrono::seconds>(uptime);
timeval.tv_sec = secondsChrono.count();
auto fraction = uptime - secondsChrono;
timeval.tv_usec = std::chrono::duration_cast<std::chrono::microseconds>(fraction).count();
2021-05-12 17:32:40 +02:00
#elif defined(PLATFORM_UNIX)
2022-02-02 10:29:30 +01:00
double uptimeSeconds;
if (std::ifstream("/proc/uptime", std::ios::in) >> uptimeSeconds) {
// value is rounded down automatically
timeval.tv_sec = uptimeSeconds;
timeval.tv_usec = uptimeSeconds * (double)1e6 - (timeval.tv_sec * 1e6);
}
2020-09-05 20:18:52 +02:00
#else
2023-01-26 00:01:40 +01:00
#warning Clock::getUptime() not implemented for your platform
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::warning << "Clock::getUptime: Not implemented for found OS" << std::endl;
#endif
2020-09-05 20:18:52 +02:00
#endif
2022-02-02 10:29:30 +01:00
return timeval;
2020-09-05 20:18:52 +02:00
}