fsfw/src/fsfw/osal/linux/Clock.cpp

44 lines
1.1 KiB
C++
Raw Normal View History

2021-07-13 20:58:45 +02:00
#include "fsfw/timemanager/Clock.h"
#include <linux/sysinfo.h>
2022-02-02 10:29:30 +01:00
#include <sys/sysinfo.h>
#include <unistd.h>
2022-02-02 10:29:30 +01:00
#include <ctime>
2020-09-22 15:57:26 +02:00
#include <fstream>
#include "fsfw/ipc/MutexGuard.h"
2022-03-25 18:48:53 +01:00
#include "fsfw/serviceinterface/ServiceInterface.h"
ReturnValue_t Clock::setClock(const timeval* time) {
2022-07-25 10:56:19 +02:00
timespec timeUnix{};
2022-02-02 10:29:30 +01:00
timeUnix.tv_sec = time->tv_sec;
timeUnix.tv_nsec = (__syscall_slong_t)time->tv_usec * 1000;
int status = clock_settime(CLOCK_REALTIME, &timeUnix);
if (status != 0) {
// TODO errno
2022-08-16 01:08:26 +02:00
return returnvalue::FAILED;
2022-02-02 10:29:30 +01:00
}
2022-08-16 01:08:26 +02:00
return returnvalue::OK;
}
ReturnValue_t Clock::getClock_timeval(timeval* time) {
2022-07-25 10:56:19 +02:00
timespec timeUnix{};
2022-02-02 10:29:30 +01:00
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-05-29 17:47:55 +02:00
timeval Clock::getUptime() {
2023-01-26 00:01:40 +01:00
timeval uptime{0,0};
2022-02-02 10:29:30 +01:00
double uptimeSeconds;
if (std::ifstream("/proc/uptime", std::ios::in) >> uptimeSeconds) {
2023-01-26 11:33:40 +01:00
uptime.tv_sec = uptimeSeconds;
uptime.tv_usec = uptimeSeconds * (double)1e6 - (uptime.tv_sec * 1e6);
2022-02-02 10:29:30 +01:00
}
2023-01-26 00:01:40 +01:00
return uptime;
2023-01-26 11:33:40 +01:00
}