fsfw/src/fsfw/timemanager/Clock.h

198 lines
6.1 KiB
C
Raw Normal View History

2020-12-14 11:17:22 +01:00
#ifndef FSFW_TIMEMANAGER_CLOCK_H_
#define FSFW_TIMEMANAGER_CLOCK_H_
2022-02-02 10:29:30 +01:00
#include <cstdint>
2020-12-15 23:00:30 +01:00
#include "clockDefinitions.h"
2021-07-13 20:22:54 +02:00
#include "fsfw/globalfunctions/timevalOperations.h"
2022-02-02 10:29:30 +01:00
#include "fsfw/ipc/MutexFactory.h"
2022-08-16 12:48:22 +02:00
#include "fsfw/returnvalues/returnvalue.h"
2020-12-21 14:07:06 +01:00
#ifdef WIN32
#include <winsock2.h>
#else
2022-07-25 10:31:49 +02:00
#include <ctime>
2020-12-20 15:32:03 +01:00
#endif
2020-05-29 17:45:08 +02:00
class Clock {
2022-02-02 10:29:30 +01:00
public:
typedef struct {
uint32_t year; //!< Year, A.D.
uint32_t month; //!< Month, 1 .. 12.
uint32_t day; //!< Day, 1 .. 31.
uint32_t hour; //!< Hour, 0 .. 23.
uint32_t minute; //!< Minute, 0 .. 59.
uint32_t second; //!< Second, 0 .. 59.
uint32_t usecond; //!< Microseconds, 0 .. 999999
} TimeOfDay_t;
/**
* 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
*/
2022-07-25 10:31:49 +02:00
static uint32_t getTicksPerSecond();
2022-02-02 10:29:30 +01:00
/**
* 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.
2022-08-16 12:12:21 +02:00
* @return -@c returnvalue::OK on success. Otherwise, the OS failure code
2022-02-02 10:29:30 +01:00
* 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.
2022-08-16 12:12:21 +02:00
* @return -@c returnvalue::OK on success. Otherwise, the OS failure code is returned.
2022-02-02 10:29:30 +01:00
*/
static ReturnValue_t setClock(const timeval *time);
2023-03-04 11:03:22 +01:00
/**
* @deprecated Use getClock instead, which does the same.
* @param time
* @return
*/
static ReturnValue_t getClock_timeval(timeval *time);
2022-02-02 10:29:30 +01:00
/**
* 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.
2022-08-16 12:12:21 +02:00
* @return @c returnvalue::OK on success. Otherwise, the OS failure code is returned.
2022-02-02 10:29:30 +01:00
*/
2023-03-04 11:03:22 +01:00
static ReturnValue_t getClock(timeval *time);
/**
* Retrieve a monotonic clock. This clock this is also more suited for measuring elapsed times
* between two time points, but less suited when the absolute time is required.
*
* Implementation example: A generic UNIX implementation can use CLOCK_MONOTONIC_RAW with
* `clock_gettime`.
* @param time
* @return
*/
static ReturnValue_t getClockMonotonic(timeval *time);
2022-02-02 10:29:30 +01:00
/**
* Get the time since boot in a timeval struct
*
* @param[out] time A pointer to a timeval struct where the uptime is stored.
2022-08-16 12:12:21 +02:00
* @return @c returnvalue::OK on success. Otherwise, the OS failure code is returned.
2022-02-02 10:29:30 +01:00
*
* @deprecated, I do not think this should be able to fail, use timeval getUptime()
*/
static ReturnValue_t getUptime(timeval *uptime);
static timeval getUptime();
/**
* 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
2022-08-16 12:12:21 +02:00
* @return returnvalue::OK on success. Otherwise, the OS failure code is returned.
2022-02-02 10:29:30 +01:00
*/
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.
* @return
2022-08-16 12:12:21 +02:00
* - @c returnvalue::OK on success.
2022-02-02 10:29:30 +01:00
* - Otherwise, the OS failure code is returned.
*/
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.
* @return
2022-08-16 12:12:21 +02:00
* - @c returnvalue::OK on success.
2022-02-02 10:29:30 +01:00
* - Otherwise, the OS failure code is returned.
*/
static ReturnValue_t getDateAndTime(TimeOfDay_t *time);
/**
* Convert to time of day struct given the POSIX timeval struct
* @param from
* @param to
* @return
*/
static ReturnValue_t convertTimevalToTimeOfDay(const timeval *from, TimeOfDay_t *to);
2022-02-02 10:29:30 +01:00
/**
* 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.
* @return
2022-08-16 12:12:21 +02:00
* - @c returnvalue::OK on success.
2022-02-02 10:29:30 +01:00
* - Otherwise, the OS failure code is returned.
*/
static ReturnValue_t convertTimeOfDayToTimeval(const TimeOfDay_t *from, timeval *to);
/**
* Converts a time represented as seconds and subseconds since unix
* epoch to days since J2000
*
* @param time seconds since unix epoch
* @param[out] JD2000 days since J2000
2022-08-16 12:12:21 +02:00
* @return @c returnvalue::OK
2022-02-02 10:29:30 +01: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.
* Therefore, it does not work for historic
* dates as only the current leap seconds are known.
*
* @param utc timeval, corresponding to UTC time
* @param[out] tt timeval, corresponding to Terrestial Time
* @return
2022-08-16 12:12:21 +02:00
* - @c returnvalue::OK on success
2022-08-16 01:08:26 +02:00
* - @c returnvalue::FAILED if leapSeconds are not set
2022-02-02 10:29:30 +01:00
*/
static ReturnValue_t convertUTCToTT(timeval utc, timeval *tt);
/**
* Set the Leap Seconds since 1972
*
* @param leapSeconds_
* @return
2022-08-16 12:12:21 +02:00
* - @c returnvalue::OK on success.
2022-02-02 10:29:30 +01:00
*/
2022-07-25 10:31:49 +02:00
static ReturnValue_t setLeapSeconds(uint16_t leapSeconds_);
2022-02-02 10:29:30 +01:00
/**
* Get the Leap Seconds since 1972
*
* Setter must be called before
*
* @param[out] leapSeconds_
* @return
2022-08-16 12:12:21 +02:00
* - @c returnvalue::OK on success.
2022-08-16 01:08:26 +02:00
* - @c returnvalue::FAILED on error
2022-02-02 10:29:30 +01:00
*/
static ReturnValue_t getLeapSeconds(uint16_t *leapSeconds_);
private:
/**
* Function to check and create the Mutex for the clock
* @return
2022-08-16 12:12:21 +02:00
* - @c returnvalue::OK on success.
2022-08-16 01:08:26 +02:00
* - Otherwise @c returnvalue::FAILED if not able to create one
2022-02-02 10:29:30 +01:00
*/
static ReturnValue_t checkOrCreateClockMutex();
static MutexIF *timeMutex;
static uint16_t leapSeconds;
static bool leapSecondsSet;
};
2020-12-14 11:17:22 +01:00
#endif /* FSFW_TIMEMANAGER_CLOCK_H_ */