55 lines
1.3 KiB
C++
55 lines
1.3 KiB
C++
#include "fsfw/timemanager/Clock.h"
|
|
|
|
#include <linux/sysinfo.h>
|
|
#include <sys/sysinfo.h>
|
|
#include <unistd.h>
|
|
|
|
#include <ctime>
|
|
#include <fstream>
|
|
|
|
#include "fsfw/ipc/MutexGuard.h"
|
|
#include "fsfw/serviceinterface/ServiceInterface.h"
|
|
|
|
ReturnValue_t Clock::setClock(const timeval* time) {
|
|
timespec timeUnix{};
|
|
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
|
|
return returnvalue::FAILED;
|
|
}
|
|
return returnvalue::OK;
|
|
}
|
|
|
|
ReturnValue_t Clock::getClock_timeval(timeval* time) {
|
|
timespec timeUnix{};
|
|
int status = clock_gettime(CLOCK_REALTIME, &timeUnix);
|
|
if (status != 0) {
|
|
return returnvalue::FAILED;
|
|
}
|
|
time->tv_sec = timeUnix.tv_sec;
|
|
time->tv_usec = timeUnix.tv_nsec / 1000.0;
|
|
return returnvalue::OK;
|
|
}
|
|
|
|
|
|
|
|
timeval Clock::getUptime() {
|
|
timeval uptime{};
|
|
|
|
double uptimeSeconds;
|
|
std::ifstream ifile("/proc/uptime");
|
|
if (ifile.bad()) {
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
|
sif::error << "Clock::getUptime: Error getting uptime" << std::endl;
|
|
#endif
|
|
return uptime;
|
|
}
|
|
if (ifile >> uptimeSeconds) {
|
|
uptime.tv_sec = uptimeSeconds;
|
|
uptime.tv_usec = (uptimeSeconds - uptime.tv_sec) * (double)1e6;
|
|
}
|
|
return uptime;
|
|
}
|