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

55 lines
1.3 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;
}
2023-06-02 11:50:09 +02:00
2020-05-29 17:47:55 +02:00
timeval Clock::getUptime() {
2022-07-25 10:56:19 +02:00
timeval uptime{};
2020-05-29 17:47:55 +02:00
2022-02-02 10:29:30 +01:00
double uptimeSeconds;
std::ifstream ifile("/proc/uptime");
if (ifile.bad()) {
2023-06-02 11:50:09 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "Clock::getUptime: Error getting uptime" << std::endl;
#endif
return uptime;
}
if (ifile >> uptimeSeconds) {
2023-06-02 12:08:06 +02:00
uptime.tv_sec = uptimeSeconds;
uptime.tv_usec = (uptimeSeconds - uptime.tv_sec) * (double)1e6;
2022-02-02 10:29:30 +01:00
}
2023-06-02 11:50:09 +02:00
return uptime;
}