2020-11-03 16:06:30 +01:00
|
|
|
#ifndef FSFW_TIMEMANAGER_CLOCK_H_
|
|
|
|
#define FSFW_TIMEMANAGER_CLOCK_H_
|
2020-08-28 18:33:29 +02:00
|
|
|
|
2020-11-03 16:06:30 +01:00
|
|
|
#include "clockDefinitions.h"
|
2020-08-28 18:33:29 +02:00
|
|
|
#include "../returnvalues/HasReturnvaluesIF.h"
|
|
|
|
#include "../ipc/MutexFactory.h"
|
|
|
|
#include "../globalfunctions/timevalOperations.h"
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
2020-11-03 16:06:30 +01:00
|
|
|
// TODO: Maybe a namespace would be better/more fitting for this?
|
|
|
|
// After all, right now it is just a collection of static functions.
|
|
|
|
// The mutex and leap seconds can be put in an anonymous namespace.
|
2020-08-28 18:33:29 +02:00
|
|
|
class Clock {
|
|
|
|
public:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method returns the number of clock ticks per second.
|
|
|
|
* In RTEMS, this is typically 1000.
|
|
|
|
* @return The number of ticks.
|
|
|
|
*
|
|
|
|
* @deprecated, we should not worry about ticks, but only time
|
|
|
|
*/
|
|
|
|
static uint32_t getTicksPerSecond(void);
|
|
|
|
/**
|
|
|
|
* This system call sets the system time.
|
|
|
|
* To set the time, it uses a TimeOfDay_t struct.
|
|
|
|
* @param time The struct with the time settings to set.
|
|
|
|
* @return -@c RETURN_OK on success. Otherwise, the OS failure code
|
|
|
|
* is returned.
|
|
|
|
*/
|
|
|
|
static ReturnValue_t setClock(const TimeOfDay_t* time);
|
|
|
|
/**
|
|
|
|
* This system call sets the system time.
|
|
|
|
* To set the time, it uses a timeval struct.
|
|
|
|
* @param time The struct with the time settings to set.
|
|
|
|
* @return -@c RETURN_OK on success. Otherwise, the OS failure code is returned.
|
|
|
|
*/
|
|
|
|
static ReturnValue_t setClock(const timeval* time);
|
|
|
|
/**
|
|
|
|
* This system call returns the current system clock in timeval format.
|
|
|
|
* The timval format has the fields @c tv_sec with seconds and @c tv_usec with
|
|
|
|
* microseconds since an OS-defined epoch.
|
|
|
|
* @param time A pointer to a timeval struct where the current time is stored.
|
|
|
|
* @return @c RETURN_OK on success. Otherwise, the OS failure code is returned.
|
|
|
|
*/
|
|
|
|
static ReturnValue_t getClock_timeval(timeval* time);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the time since boot in a timeval struct
|
|
|
|
*
|
|
|
|
* @param[out] time A pointer to a timeval struct where the uptime is stored.
|
|
|
|
* @return @c RETURN_OK on success. Otherwise, the OS failure code is returned.
|
|
|
|
*
|
|
|
|
* @deprecated, I do not think this should be able to fail, use timeval getUptime()
|
|
|
|
*/
|
|
|
|
static ReturnValue_t getUptime(timeval* uptime);
|
|
|
|
|
|
|
|
static timeval getUptime();
|
|
|
|
|
2020-09-18 12:57:30 +02:00
|
|
|
static uint32_t getUptimeSeconds();
|
|
|
|
|
2020-08-28 18:33:29 +02:00
|
|
|
/**
|
|
|
|
* Get the time since boot in milliseconds
|
|
|
|
*
|
|
|
|
* This value can overflow! Still, it can be used to calculate time intervalls
|
|
|
|
* between two calls up to 49 days by always using uint32_t in the calculation
|
|
|
|
*
|
|
|
|
* @param ms uptime in ms
|
|
|
|
* @return RETURN_OK on success. Otherwise, the OS failure code is returned.
|
|
|
|
*/
|
|
|
|
static ReturnValue_t getUptime(uint32_t* uptimeMs);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the time in microseconds since an OS-defined epoch.
|
|
|
|
* The time is returned in a 64 bit unsigned integer.
|
|
|
|
* @param time A pointer to a 64 bit unisigned integer where the data is stored.
|
2020-11-03 16:06:30 +01:00
|
|
|
* @return
|
|
|
|
* - @c RETURN_OK on success.
|
|
|
|
* - Otherwise, the OS failure code is returned.
|
2020-08-28 18:33:29 +02:00
|
|
|
*/
|
|
|
|
static ReturnValue_t getClock_usecs(uint64_t* time);
|
|
|
|
/**
|
|
|
|
* Returns the time in a TimeOfDay_t struct.
|
|
|
|
* @param time A pointer to a TimeOfDay_t struct.
|
2020-11-03 16:06:30 +01:00
|
|
|
* @return
|
|
|
|
* - @c RETURN_OK on success.
|
|
|
|
* - Otherwise, the OS failure code is returned.
|
2020-08-28 18:33:29 +02:00
|
|
|
*/
|
|
|
|
static ReturnValue_t getDateAndTime(TimeOfDay_t* time);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a time of day struct to POSIX seconds.
|
|
|
|
* @param time The time of day as input
|
|
|
|
* @param timeval The corresponding seconds since the epoch.
|
2020-11-03 16:06:30 +01:00
|
|
|
* @return
|
|
|
|
* - @c RETURN_OK on success.
|
|
|
|
* - Otherwise, the OS failure code is returned.
|
2020-08-28 18:33:29 +02:00
|
|
|
*/
|
|
|
|
static ReturnValue_t convertTimeOfDayToTimeval(const TimeOfDay_t* from,
|
|
|
|
timeval* to);
|
|
|
|
|
|
|
|
/**
|
2020-11-03 16:06:30 +01:00
|
|
|
* Converts a time represented as seconds and subseconds since unix
|
|
|
|
* epoch to days since J2000
|
2020-08-28 18:33:29 +02:00
|
|
|
*
|
|
|
|
* @param time seconds since unix epoch
|
|
|
|
* @param[out] JD2000 days since J2000
|
2020-11-03 16:06:30 +01:00
|
|
|
* @return @c RETURN_OK
|
2020-08-28 18:33:29 +02:00
|
|
|
*/
|
|
|
|
static ReturnValue_t convertTimevalToJD2000(timeval time, double* JD2000);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates and adds the offset between UTC and TT
|
|
|
|
*
|
|
|
|
* Depends on the leap seconds to be set correctly.
|
|
|
|
*
|
|
|
|
* @param utc timeval, corresponding to UTC time
|
|
|
|
* @param[out] tt timeval, corresponding to Terrestial Time
|
2020-11-03 16:06:30 +01:00
|
|
|
* @return
|
|
|
|
* - @c RETURN_OK on success
|
|
|
|
* - @c RETURN_FAILED if leapSeconds are not set
|
2020-08-28 18:33:29 +02:00
|
|
|
*/
|
|
|
|
static ReturnValue_t convertUTCToTT(timeval utc, timeval* tt);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the Leap Seconds since 1972
|
|
|
|
*
|
|
|
|
* @param leapSeconds_
|
2020-11-03 16:06:30 +01:00
|
|
|
* @return
|
|
|
|
* - @c RETURN_OK on success.
|
|
|
|
* - Otherwise, the OS failure code is returned.
|
2020-08-28 18:33:29 +02:00
|
|
|
*/
|
|
|
|
static ReturnValue_t setLeapSeconds(const uint16_t leapSeconds_);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Leap Seconds since 1972
|
|
|
|
*
|
|
|
|
* Must be set before!
|
|
|
|
*
|
|
|
|
* @param[out] leapSeconds_
|
2020-11-03 16:06:30 +01:00
|
|
|
* @return
|
|
|
|
* - @c RETURN_OK on success.
|
|
|
|
* - Otherwise, the OS failure code is returned.
|
2020-08-28 18:33:29 +02:00
|
|
|
*/
|
|
|
|
static ReturnValue_t getLeapSeconds(uint16_t *leapSeconds_);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to check and create the Mutex for the clock
|
2020-11-03 16:06:30 +01:00
|
|
|
* @return
|
|
|
|
* - @c RETURN_OK on success.
|
|
|
|
* - Otherwise @c RETURN_FAILED if not able to create one
|
2020-08-28 18:33:29 +02:00
|
|
|
*/
|
|
|
|
static ReturnValue_t checkOrCreateClockMutex();
|
|
|
|
|
|
|
|
private:
|
|
|
|
static MutexIF* timeMutex;
|
|
|
|
static uint16_t leapSeconds;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-11-03 16:06:30 +01:00
|
|
|
#endif /* FSFW_TIMEMANAGER_CLOCK_H_ */
|