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
|
|
|
}
|
|
|
|
|
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;
|
2023-03-15 12:21:50 +01:00
|
|
|
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;
|
2023-03-15 12:21:50 +01:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|