2021-07-13 20:58:45 +02:00
|
|
|
#include "fsfw/timemanager/Clock.h"
|
2018-07-12 16:29:32 +02:00
|
|
|
|
|
|
|
#include <linux/sysinfo.h>
|
2022-02-02 10:29:30 +01:00
|
|
|
#include <sys/sysinfo.h>
|
2018-07-12 16:29:32 +02:00
|
|
|
#include <unistd.h>
|
2022-02-02 10:29:30 +01:00
|
|
|
|
2022-07-25 19:36:56 +02:00
|
|
|
#include <ctime>
|
2020-09-22 15:57:26 +02:00
|
|
|
#include <fstream>
|
2018-07-12 16:29:32 +02:00
|
|
|
|
2022-03-25 18:47:31 +01:00
|
|
|
#include "fsfw/ipc/MutexGuard.h"
|
2022-03-25 18:48:53 +01:00
|
|
|
#include "fsfw/serviceinterface/ServiceInterface.h"
|
2018-07-12 16:29:32 +02:00
|
|
|
|
|
|
|
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;
|
2018-07-12 16:29:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2018-07-12 16:29:32 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|