trying to fuse header / inc

This commit is contained in:
2021-07-19 16:25:51 +02:00
parent 7849b8e391
commit d47906e833
767 changed files with 117 additions and 135 deletions

View File

@ -0,0 +1,615 @@
#include "fsfw/FSFW.h"
#include "fsfw/timemanager/CCSDSTime.h"
#include <cstdio>
#include <cinttypes>
#include <cmath>
CCSDSTime::CCSDSTime() {
}
CCSDSTime::~CCSDSTime() {
}
ReturnValue_t CCSDSTime::convertToCcsds(Ccs_seconds* to,
const Clock::TimeOfDay_t* from) {
ReturnValue_t result = checkTimeOfDay(from);
if (result != RETURN_OK) {
return result;
}
to->pField = (CCS << 4);
to->yearMSB = (from->year >> 8);
to->yearLSB = from->year & 0xff;
to->month = from->month;
to->day = from->day;
to->hour = from->hour;
to->minute = from->minute;
to->second = from->second;
return RETURN_OK;
}
ReturnValue_t CCSDSTime::convertToCcsds(Ccs_mseconds* to,
const Clock::TimeOfDay_t* from) {
ReturnValue_t result = checkTimeOfDay(from);
if (result != RETURN_OK) {
return result;
}
to->pField = (CCS << 4) + 2;
to->yearMSB = (from->year >> 8);
to->yearLSB = from->year & 0xff;
to->month = from->month;
to->day = from->day;
to->hour = from->hour;
to->minute = from->minute;
to->second = from->second;
to->secondEminus2 = from->usecond / 10000;
to->secondEminus4 = (from->usecond % 10000) / 100;
return RETURN_OK;
}
ReturnValue_t CCSDSTime::convertFromCcsds(Clock::TimeOfDay_t* to,
const uint8_t* from, size_t length) {
ReturnValue_t result;
if (length > 0xFF) {
return LENGTH_MISMATCH;
}
result = convertFromASCII(to, from, length); //Try to parse it as ASCII
if (result == RETURN_OK) {
return RETURN_OK;
}
//Seems to be no ascii, try the other formats
uint8_t codeIdentification = (*from >> 4);
switch (codeIdentification) {
case CUC_LEVEL1: //CUC_LEVEL2 can not be converted to TimeOfDay (ToD is Level 1) <- Well, if we know the epoch, we can... <- see bug 1133
return convertFromCUC(to, from, length);
case CDS:
return convertFromCDS(to, from, length);
case CCS: {
size_t temp = 0;
return convertFromCCS(to, from, &temp, length);
}
default:
return UNSUPPORTED_TIME_FORMAT;
}
}
ReturnValue_t CCSDSTime::convertFromCUC(Clock::TimeOfDay_t* to,
const uint8_t* from, uint8_t length) {
return UNSUPPORTED_TIME_FORMAT;
}
ReturnValue_t CCSDSTime::convertFromCDS(Clock::TimeOfDay_t* to,
const uint8_t* from, uint8_t length) {
timeval time;
ReturnValue_t result = convertFromCDS(&time, from, NULL, length);
if (result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
return convertTimevalToTimeOfDay(to, &time);
}
ReturnValue_t CCSDSTime::convertFromCCS(Clock::TimeOfDay_t* to,
const uint8_t* from, size_t* foundLength, size_t maxLength) {
uint8_t subsecondsLength = *from & 0b111;
uint32_t totalLength = subsecondsLength + 8;
if (maxLength < totalLength) {
return LENGTH_MISMATCH;
}
*foundLength = totalLength;
ReturnValue_t result = checkCcs(from, maxLength);
if (result != RETURN_OK) {
return result;
}
Ccs_mseconds *temp = (Ccs_mseconds *) from;
to->year = (temp->yearMSB << 8) + temp->yearLSB;
to->hour = temp->hour;
to->minute = temp->minute;
to->second = temp->second;
if (temp->pField & (1 << 3)) { //day of year variation
uint16_t tempDay = (temp->month << 8) + temp->day;
result = convertDaysOfYear(tempDay, to->year,
&(temp->month), &(temp->day));
if (result != RETURN_OK) {
return result;
}
}
to->month = temp->month;
to->day = temp->day;
to->usecond = 0;
if (subsecondsLength > 0) {
*foundLength += 1;
if (temp->secondEminus2 >= 100) {
return INVALID_TIME_FORMAT;
}
to->usecond = temp->secondEminus2 * 10000;
}
if (subsecondsLength > 1) {
*foundLength += 1;
if (temp->secondEminus4 >= 100) {
return INVALID_TIME_FORMAT;
}
to->usecond += temp->secondEminus4 * 100;
}
return RETURN_OK;
}
ReturnValue_t CCSDSTime::convertFromASCII(Clock::TimeOfDay_t* to,
const uint8_t* from, uint8_t length) {
if (length < 19) {
return RETURN_FAILED;
}
// Newlib nano can't parse uint8, see SCNu8 documentation and https://sourceware.org/newlib/README
// Suggestion: use uint16 all the time. This should work on all systems.
#if FSFW_NO_C99_IO == 1
uint16_t year;
uint16_t month;
uint16_t day;
uint16_t hour;
uint16_t minute;
float second;
int count = sscanf((char *) from, "%4" SCNu16 "-%2" SCNu16 "-%2" SCNu16 "T%"
"2" SCNu16 ":%2" SCNu16 ":%fZ", &year, &month, &day, &hour,
&minute, &second);
if (count == 6) {
to->year = year;
to->month = month;
to->day = day;
to->hour = hour;
to->minute = minute;
to->second = second;
to->usecond = (second - floor(second)) * 1000000;
return RETURN_OK;
}
// try Code B (yyyy-ddd)
count = sscanf((char *) from, "%4" SCNu16 "-%3" SCNu16 "T%2" SCNu16 ":%"
"2" SCNu16 ":%fZ", &year, &day, &hour, &minute, &second);
if (count == 5) {
uint8_t tempDay;
ReturnValue_t result = CCSDSTime::convertDaysOfYear(day, year,
reinterpret_cast<uint8_t *>(&month),
reinterpret_cast<uint8_t *>(&tempDay));
if (result != RETURN_OK) {
return RETURN_FAILED;
}
to->year = year;
to->month = month;
to->day = tempDay;
to->hour = hour;
to->minute = minute;
to->second = second;
to->usecond = (second - floor(second)) * 1000000;
return RETURN_OK;
}
// Warning: Compiler/Linker fails ambiguously if library does not implement
// C99 I/O
#else
uint16_t year;
uint8_t month;
uint16_t day;
uint8_t hour;
uint8_t minute;
float second;
//try Code A (yyyy-mm-dd)
int count = sscanf((char *) from, "%4" SCNu16 "-%2" SCNu8 "-%2" SCNu16
"T%2" SCNu8 ":%2" SCNu8 ":%fZ", &year, &month, &day,
&hour, &minute, &second);
if (count == 6) {
to->year = year;
to->month = month;
to->day = day;
to->hour = hour;
to->minute = minute;
to->second = second;
to->usecond = (second - floor(second)) * 1000000;
return RETURN_OK;
}
//try Code B (yyyy-ddd)
count = sscanf((char *) from, "%4" SCNu16 "-%3" SCNu16 "T%2" SCNu8
":%2" SCNu8 ":%fZ", &year, &day, &hour, &minute, &second);
if (count == 5) {
uint8_t tempDay;
ReturnValue_t result = CCSDSTime::convertDaysOfYear(day, year, &month,
&tempDay);
if (result != RETURN_OK) {
return RETURN_FAILED;
}
to->year = year;
to->month = month;
to->day = tempDay;
to->hour = hour;
to->minute = minute;
to->second = second;
to->usecond = (second - floor(second)) * 1000000;
return RETURN_OK;
}
#endif
return UNSUPPORTED_TIME_FORMAT;
}
ReturnValue_t CCSDSTime::checkCcs(const uint8_t* time, uint8_t length) {
Ccs_mseconds *time_struct = (Ccs_mseconds *) time;
uint8_t additionalBytes = time_struct->pField & 0b111;
if ((additionalBytes == 0b111) || (length < (additionalBytes + 8))) {
return INVALID_TIME_FORMAT;
}
if (time_struct->pField & (1 << 3)) { //day of year variation
uint16_t day = (time_struct->month << 8) + time_struct->day;
if (day > 366) {
return INVALID_TIME_FORMAT;
}
} else {
if (time_struct->month > 12) {
return INVALID_TIME_FORMAT;
}
if (time_struct->day > 31) {
return INVALID_TIME_FORMAT;
}
}
if (time_struct->hour > 23) {
return INVALID_TIME_FORMAT;
}
if (time_struct->minute > 59) {
return INVALID_TIME_FORMAT;
}
if (time_struct->second > 59) {
return INVALID_TIME_FORMAT;
}
uint8_t *additionalByte = &time_struct->secondEminus2;
for (; additionalBytes != 0; additionalBytes--) {
if (*additionalByte++ > 99) {
return INVALID_TIME_FORMAT;
}
}
return RETURN_OK;
}
ReturnValue_t CCSDSTime::convertDaysOfYear(uint16_t dayofYear, uint16_t year,
uint8_t* month, uint8_t* day) {
if (isLeapYear(year)) {
if (dayofYear > 366) {
return INVALID_DAY_OF_YEAR;
}
} else {
if (dayofYear > 365) {
return INVALID_DAY_OF_YEAR;
}
}
*month = 1;
if (dayofYear <= 31) {
*day = dayofYear;
return RETURN_OK;
}
*month += 1;
dayofYear -= 31;
if (isLeapYear(year)) {
if (dayofYear <= 29) {
*day = dayofYear;
return RETURN_OK;
}
*month += 1;
dayofYear -= 29;
} else {
if (dayofYear <= 28) {
*day = dayofYear;
return RETURN_OK;
}
*month += 1;
dayofYear -= 28;
}
while (*month <= 12) {
if (dayofYear <= 31) {
*day = dayofYear;
return RETURN_OK;
}
*month += 1;
dayofYear -= 31;
if (*month == 8) {
continue;
}
if (dayofYear <= 30) {
*day = dayofYear;
return RETURN_OK;
}
*month += 1;
dayofYear -= 30;
}
return INVALID_DAY_OF_YEAR;
}
bool CCSDSTime::isLeapYear(uint32_t year) {
if ((year % 400) == 0) {
return true;
}
if ((year % 100) == 0) {
return false;
}
if ((year % 4) == 0) {
return true;
}
return false;
}
ReturnValue_t CCSDSTime::convertToCcsds(CDS_short* to, const timeval* from) {
to->pField = (CDS << 4) + 0;
uint32_t days = ((from->tv_sec) / SECONDS_PER_DAY)
+ DAYS_CCSDS_TO_UNIX_EPOCH;
if (days > (1 << 16)) {
//Date is beyond year 2137
return TIME_DOES_NOT_FIT_FORMAT;
}
to->dayMSB = (days & 0xFF00) >> 8;
to->dayLSB = (days & 0xFF);
uint32_t msDay = ((from->tv_sec % SECONDS_PER_DAY) * 1000)
+ (from->tv_usec / 1000);
to->msDay_hh = (msDay & 0xFF000000) >> 24;
to->msDay_h = (msDay & 0xFF0000) >> 16;
to->msDay_l = (msDay & 0xFF00) >> 8;
to->msDay_ll = (msDay & 0xFF);
return RETURN_OK;
}
ReturnValue_t CCSDSTime::convertToCcsds(OBT_FLP* to, const timeval* from) {
to->pFiled = (AGENCY_DEFINED << 4) + 5;
to->seconds_hh = (from->tv_sec >> 24) & 0xff;
to->seconds_h = (from->tv_sec >> 16) & 0xff;
to->seconds_l = (from->tv_sec >> 8) & 0xff;
to->seconds_ll = (from->tv_sec >> 0) & 0xff;
//convert the µs to 2E-16 seconds
uint64_t temp = from->tv_usec;
temp = temp << 16;
temp = temp / 1E6;
to->subsecondsMSB = (temp >> 8) & 0xff;
to->subsecondsLSB = temp & 0xff;
return RETURN_OK;
}
ReturnValue_t CCSDSTime::convertFromCcsds(timeval* to, const uint8_t* from,
size_t* foundLength, size_t maxLength) {
if(maxLength >= 19) {
Clock::TimeOfDay_t timeOfDay;
/* Try to parse it as ASCII */
ReturnValue_t result = convertFromASCII(&timeOfDay, from, maxLength);
if (result == RETURN_OK) {
return Clock::convertTimeOfDayToTimeval(&timeOfDay, to);
}
}
uint8_t codeIdentification = (*from >> 4);
switch (codeIdentification) {
/* Unsupported, as Leap second correction would have to be applied */
case CUC_LEVEL1:
return UNSUPPORTED_TIME_FORMAT;
case CDS:
return convertFromCDS(to, from, foundLength, maxLength);
case CCS:
return convertFromCCS(to, from, foundLength, maxLength);
default:
return UNSUPPORTED_TIME_FORMAT;
}
}
ReturnValue_t CCSDSTime::convertFromCUC(timeval* to, const uint8_t* from,
size_t* foundLength, size_t maxLength) {
if (maxLength < 1) {
return INVALID_TIME_FORMAT;
}
uint8_t pField = *from;
from++;
ReturnValue_t result = convertFromCUC(to, pField, from, foundLength,
maxLength - 1);
if (result == HasReturnvaluesIF::RETURN_OK) {
if (foundLength != NULL) {
*foundLength += 1;
}
}
return result;
}
ReturnValue_t CCSDSTime::checkTimeOfDay(const Clock::TimeOfDay_t* time) {
if ((time->month > 12) || (time->month == 0)) {
return INVALID_TIME_FORMAT;
}
if (time->day == 0) {
return INVALID_TIME_FORMAT;
}
switch (time->month) {
case 2:
if (isLeapYear(time->year)) {
if (time->day > 29) {
return INVALID_TIME_FORMAT;
}
} else {
if (time->day > 28) {
return INVALID_TIME_FORMAT;
}
}
break;
case 4:
case 6:
case 9:
case 11:
if (time->day > 30) {
return INVALID_TIME_FORMAT;
}
break;
default:
if (time->day > 31) {
return INVALID_TIME_FORMAT;
}
break;
}
if (time->hour > 23) {
return INVALID_TIME_FORMAT;
}
if (time->minute > 59) {
return INVALID_TIME_FORMAT;
}
if (time->second > 59) {
return INVALID_TIME_FORMAT;
}
if (time->usecond > 999999) {
return INVALID_TIME_FORMAT;
}
return RETURN_OK;
}
ReturnValue_t CCSDSTime::convertTimevalToTimeOfDay(Clock::TimeOfDay_t* to,
timeval* from) {
//This is rather tricky. Implement only if needed. Also, if so, move to OSAL.
return UNSUPPORTED_TIME_FORMAT;
}
ReturnValue_t CCSDSTime::convertFromCDS(timeval* to, const uint8_t* from,
size_t* foundLength, size_t maxLength) {
uint8_t pField = *from;
from++;
//Check epoch
if (pField & 0b1000) {
return NOT_ENOUGH_INFORMATION_FOR_TARGET_FORMAT;
}
//Check length
uint8_t expectedLength = 7; //Including p-Field.
bool extendedDays = pField & 0b100;
if (extendedDays) {
expectedLength += 1;
}
if ((pField & 0b11) == 0b01) {
expectedLength += 2;
} else if ((pField & 0b11) == 0b10) {
expectedLength += 4;
}
if (foundLength != NULL) {
*foundLength = expectedLength;
}
if (expectedLength > maxLength) {
return LENGTH_MISMATCH;
}
//Check and count days
uint32_t days = 0;
if (extendedDays) {
days = (from[0] << 16) + (from[1] << 8) + from[2];
from += 3;
} else {
days = (from[0] << 8) + from[1];
from += 2;
}
//Move to POSIX epoch.
if (days <= DAYS_CCSDS_TO_UNIX_EPOCH) {
return INVALID_TIME_FORMAT;
}
days -= DAYS_CCSDS_TO_UNIX_EPOCH;
to->tv_sec = days * SECONDS_PER_DAY;
uint32_t msDay = (from[0] << 24) + (from[1] << 16) + (from[2] << 8)
+ from[3];
from += 4;
to->tv_sec += (msDay / 1000);
to->tv_usec = (msDay % 1000) * 1000;
if ((pField & 0b11) == 0b01) {
uint16_t usecs = (from[0] << 16) + from[1];
from += 2;
if (usecs > 999) {
return INVALID_TIME_FORMAT;
}
to->tv_usec += usecs;
} else if ((pField & 0b11) == 0b10) {
uint32_t picosecs = (from[0] << 24) + (from[1] << 16) + (from[2] << 8)
+ from[3];
from += 4;
if (picosecs > 999999) {
return INVALID_TIME_FORMAT;
}
//Not very useful.
to->tv_usec += (picosecs / 1000);
}
return RETURN_OK;
}
ReturnValue_t CCSDSTime::convertFromCUC(timeval* to, uint8_t pField,
const uint8_t* from, size_t* foundLength, size_t maxLength) {
uint32_t secs = 0;
uint32_t subSeconds = 0;
uint8_t nCoarse = ((pField & 0b1100) >> 2) + 1;
uint8_t nFine = (pField & 0b11);
size_t totalLength = nCoarse + nFine;
if (foundLength != NULL) {
*foundLength = totalLength;
}
if (totalLength > maxLength) {
return LENGTH_MISMATCH;
}
for (int count = 0; count < nCoarse; count++) {
secs += *from << ((nCoarse * 8 - 8) * (1 + count));
from++;
}
for (int count = 0; count < nFine; count++) {
subSeconds += *from << ((nFine * 8 - 8) * (1 + count));
from++;
}
//Move to POSIX epoch.
to->tv_sec = secs;
if (pField & 0b10000) {
//CCSDS-Epoch
to->tv_sec -= (DAYS_CCSDS_TO_UNIX_EPOCH * SECONDS_PER_DAY);
}
to->tv_usec = subsecondsToMicroseconds(subSeconds);
return RETURN_OK;
}
uint32_t CCSDSTime::subsecondsToMicroseconds(uint16_t subseconds) {
uint64_t temp = (uint64_t) subseconds * 1000000
/ (1 << (sizeof(subseconds) * 8));
return temp;
}
ReturnValue_t CCSDSTime::convertFromCCS(timeval* to, const uint8_t* from,
size_t* foundLength, size_t maxLength) {
Clock::TimeOfDay_t tempTime;
ReturnValue_t result = convertFromCCS(&tempTime, from, foundLength,
maxLength);
if (result != RETURN_OK) {
return result;
}
return Clock::convertTimeOfDayToTimeval(&tempTime, to);
}

View File

@ -0,0 +1,235 @@
#ifndef FSFW_TIMEMANAGER_CCSDSTIME_H_
#define FSFW_TIMEMANAGER_CCSDSTIME_H_
// COULDDO: have calls in Clock.h which return time quality and use timespec accordingly
#include "Clock.h"
#include "clockDefinitions.h"
#include "../returnvalues/HasReturnvaluesIF.h"
#include <cstdint>
#include <cstddef>
bool operator<(const timeval& lhs, const timeval& rhs);
bool operator<=(const timeval& lhs, const timeval& rhs);
bool operator==(const timeval& lhs, const timeval& rhs);
/**
* static helper class for CCSDS Time Code Formats
*
* as described in CCSDS 301.0-B-3
*
* Still work in progress
*/
class CCSDSTime: public HasReturnvaluesIF {
public:
/**
* The Time code identifications, bits 4-6 in the P-Field
*/
enum TimeCodeIdentification {
CCS = 0b101,
CUC_LEVEL1 = 0b001,
CUC_LEVEL2 = 0b010,
CDS = 0b100,
AGENCY_DEFINED = 0b110
};
static const uint8_t P_FIELD_CUC_6B_CCSDS = (CUC_LEVEL1 << 4) + (3 << 2)
+ 2;
static const uint8_t P_FIELD_CUC_6B_AGENCY = (CUC_LEVEL2 << 4) + (3 << 2)
+ 2;
static const uint8_t P_FIELD_CDS_SHORT = (CDS << 4);
/**
* Struct for CDS day-segmented format.
*/
struct CDS_short {
uint8_t pField;
uint8_t dayMSB;
uint8_t dayLSB;
uint8_t msDay_hh;
uint8_t msDay_h;
uint8_t msDay_l;
uint8_t msDay_ll;
};
/**
* Struct for the CCS fromat in day of month variation with max resolution
*/
struct Ccs_seconds {
uint8_t pField;
uint8_t yearMSB;
uint8_t yearLSB;
uint8_t month;
uint8_t day;
uint8_t hour;
uint8_t minute;
uint8_t second;
};
/**
* Struct for the CCS fromat in day of month variation with 10E-4 seconds resolution
*/
struct Ccs_mseconds {
uint8_t pField;
uint8_t yearMSB;
uint8_t yearLSB;
uint8_t month;
uint8_t day;
uint8_t hour;
uint8_t minute;
uint8_t second;
uint8_t secondEminus2;
uint8_t secondEminus4;
};
struct OBT_FLP {
uint8_t pFiled;
uint8_t seconds_hh;
uint8_t seconds_h;
uint8_t seconds_l;
uint8_t seconds_ll;
uint8_t subsecondsMSB;
uint8_t subsecondsLSB;
};
struct TimevalLess {
bool operator()(const timeval& lhs, const timeval& rhs) const {
return (lhs < rhs);
}
};
static const uint8_t INTERFACE_ID = CLASS_ID::CCSDS_TIME_HELPER_CLASS;
static const ReturnValue_t UNSUPPORTED_TIME_FORMAT = MAKE_RETURN_CODE(0);
static const ReturnValue_t NOT_ENOUGH_INFORMATION_FOR_TARGET_FORMAT =
MAKE_RETURN_CODE(1);
static const ReturnValue_t LENGTH_MISMATCH = MAKE_RETURN_CODE(2);
static const ReturnValue_t INVALID_TIME_FORMAT = MAKE_RETURN_CODE(3);
static const ReturnValue_t INVALID_DAY_OF_YEAR = MAKE_RETURN_CODE(4);
static const ReturnValue_t TIME_DOES_NOT_FIT_FORMAT = MAKE_RETURN_CODE(5);
/**
* convert a TimeofDay struct to ccs with seconds resolution
*
* @param to pointer to a CCS struct
* @param from pointer to a TimeOfDay Struct
* @return
* - @c RETURN_OK if OK
* - @c INVALID_TIMECODE if not OK
*/
static ReturnValue_t convertToCcsds(Ccs_seconds *to,
Clock::TimeOfDay_t const *from);
/**
* Converts to CDS format from timeval.
* @param to pointer to the CDS struct to generate
* @param from pointer to a timeval struct which comprises a time of day since UNIX epoch.
* @return
* - @c RETURN_OK as it assumes a valid timeval.
*/
static ReturnValue_t convertToCcsds(CDS_short* to, timeval const *from);
static ReturnValue_t convertToCcsds(OBT_FLP* to, timeval const *from);
/**
* convert a TimeofDay struct to ccs with 10E-3 seconds resolution
*
* The 10E-4 seconds in the CCS Struct are 0 as the TimeOfDay only has ms resolution
*
* @param to pointer to a CCS struct
* @param from pointer to a TimeOfDay Struct
* @return
* - @c RETURN_OK if OK
* - @c INVALID_TIMECODE if not OK
*/
static ReturnValue_t convertToCcsds(Ccs_mseconds *to,
Clock::TimeOfDay_t const *from);
/**
* SHOULDDO: can this be modified to recognize padding?
* Tries to interpret a Level 1 CCSDS time code
*
* It assumes binary formats contain a valid P Field and recognizes the ASCII format
* by the lack of one.
*
* @param to an empty TimeOfDay struct
* @param from pointer to an CCSDS Time code
* @param length length of the Time code
* @return
* - @c RETURN_OK if successful
* - @c UNSUPPORTED_TIME_FORMAT if a (possibly valid) time code is not supported
* - @c LENGTH_MISMATCH if the length does not match the P Field
* - @c INVALID_TIME_FORMAT if the format or a value is invalid
*/
static ReturnValue_t convertFromCcsds(Clock::TimeOfDay_t *to,
uint8_t const *from, size_t length);
/**
* not implemented yet
*
* @param to
* @param from
* @return
*/
static ReturnValue_t convertFromCcsds(timeval *to, uint8_t const *from,
size_t* foundLength, size_t maxLength);
static ReturnValue_t convertFromCUC(Clock::TimeOfDay_t *to,
uint8_t const *from, uint8_t length);
static ReturnValue_t convertFromCUC(timeval *to, uint8_t const *from,
size_t* foundLength, size_t maxLength);
static ReturnValue_t convertFromCUC(timeval *to, uint8_t pField,
uint8_t const *from, size_t* foundLength, size_t maxLength);
static ReturnValue_t convertFromCCS(timeval *to, uint8_t const *from,
size_t* foundLength, size_t maxLength);
static ReturnValue_t convertFromCCS(timeval *to, uint8_t pField,
uint8_t const *from, size_t* foundLength, size_t maxLength);
static ReturnValue_t convertFromCDS(Clock::TimeOfDay_t *to,
uint8_t const *from, uint8_t length);
static ReturnValue_t convertFromCDS(timeval *to, uint8_t const *from,
size_t* foundLength, size_t maxLength);
static ReturnValue_t convertFromCCS(Clock::TimeOfDay_t *to,
uint8_t const *from, size_t* foundLength, size_t maxLength);
static ReturnValue_t convertFromASCII(Clock::TimeOfDay_t *to,
uint8_t const *from, uint8_t length);
static uint32_t subsecondsToMicroseconds(uint16_t subseconds);
private:
CCSDSTime();
virtual ~CCSDSTime();
/**
* checks a ccs time stream for validity
*
* Stream may be longer than the actual timecode
*
* @param time pointer to an Ccs stream
* @param length length of stream
* @return
*/
static ReturnValue_t checkCcs(const uint8_t* time, uint8_t length);
static ReturnValue_t checkTimeOfDay(const Clock::TimeOfDay_t *time);
static const uint32_t SECONDS_PER_DAY = 24 * 60 * 60;
static const uint32_t SECONDS_PER_NON_LEAP_YEAR = SECONDS_PER_DAY * 365;
static const uint32_t DAYS_CCSDS_TO_UNIX_EPOCH = 4383; //!< Time difference between CCSDS and POSIX epoch. This is exact, because leap-seconds where not introduced before 1972.
static const uint32_t SECONDS_CCSDS_TO_UNIX_EPOCH = DAYS_CCSDS_TO_UNIX_EPOCH
* SECONDS_PER_DAY;
/**
* @param dayofYear
* @param year
* @param month
* @param day
*/
static ReturnValue_t convertDaysOfYear(uint16_t dayofYear, uint16_t year,
uint8_t *month, uint8_t *day);
static bool isLeapYear(uint32_t year);
static ReturnValue_t convertTimevalToTimeOfDay(Clock::TimeOfDay_t* to,
timeval* from);
};
#endif /* FSFW_TIMEMANAGER_CCSDSTIME_H_ */

View File

@ -0,0 +1,8 @@
target_sources(${LIB_FSFW_NAME} PRIVATE
CCSDSTime.cpp
Countdown.cpp
Stopwatch.cpp
TimeMessage.cpp
TimeStamper.cpp
ClockCommon.cpp
)

View File

@ -0,0 +1,172 @@
#ifndef FSFW_TIMEMANAGER_CLOCK_H_
#define FSFW_TIMEMANAGER_CLOCK_H_
#include "clockDefinitions.h"
#include "fsfw/returnvalues/HasReturnvaluesIF.h"
#include "fsfw/ipc/MutexFactory.h"
#include "fsfw/globalfunctions/timevalOperations.h"
#include <cstdint>
#ifdef WIN32
#include <winsock2.h>
#else
#include <sys/time.h>
#endif
class Clock {
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
*/
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();
/**
* 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.
* @return
* - @c RETURN_OK on success.
* - 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
* - @c RETURN_OK on success.
* - Otherwise, the OS failure code is returned.
*/
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.
* @return
* - @c RETURN_OK on success.
* - 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
* @return @c RETURN_OK
*/
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
* - @c RETURN_OK on success
* - @c RETURN_FAILED if leapSeconds are not set
*/
static ReturnValue_t convertUTCToTT(timeval utc, timeval *tt);
/**
* Set the Leap Seconds since 1972
*
* @param leapSeconds_
* @return
* - @c RETURN_OK on success.
*/
static ReturnValue_t setLeapSeconds(const uint16_t leapSeconds_);
/**
* Get the Leap Seconds since 1972
*
* Setter must be called before
*
* @param[out] leapSeconds_
* @return
* - @c RETURN_OK on success.
* - @c RETURN_FAILED on error
*/
static ReturnValue_t getLeapSeconds(uint16_t *leapSeconds_);
private:
/**
* Function to check and create the Mutex for the clock
* @return
* - @c RETURN_OK on success.
* - Otherwise @c RETURN_FAILED if not able to create one
*/
static ReturnValue_t checkOrCreateClockMutex();
static MutexIF *timeMutex;
static uint16_t leapSeconds;
};
#endif /* FSFW_TIMEMANAGER_CLOCK_H_ */

View File

@ -0,0 +1,57 @@
#include "fsfw/timemanager/Clock.h"
#include "fsfw/ipc/MutexGuard.h"
ReturnValue_t Clock::convertUTCToTT(timeval utc, timeval *tt) {
uint16_t leapSeconds;
ReturnValue_t result = getLeapSeconds(&leapSeconds);
if (result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
timeval leapSeconds_timeval = { 0, 0 };
leapSeconds_timeval.tv_sec = leapSeconds;
//initial offset between UTC and TAI
timeval UTCtoTAI1972 = { 10, 0 };
timeval TAItoTT = { 32, 184000 };
*tt = utc + leapSeconds_timeval + UTCtoTAI1972 + TAItoTT;
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t Clock::setLeapSeconds(const uint16_t leapSeconds_) {
if (checkOrCreateClockMutex() != HasReturnvaluesIF::RETURN_OK) {
return HasReturnvaluesIF::RETURN_FAILED;
}
MutexGuard helper(timeMutex);
leapSeconds = leapSeconds_;
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t Clock::getLeapSeconds(uint16_t *leapSeconds_) {
if (timeMutex == nullptr) {
return HasReturnvaluesIF::RETURN_FAILED;
}
MutexGuard helper(timeMutex);
*leapSeconds_ = leapSeconds;
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t Clock::checkOrCreateClockMutex() {
if (timeMutex == nullptr) {
MutexFactory *mutexFactory = MutexFactory::instance();
if (mutexFactory == nullptr) {
return HasReturnvaluesIF::RETURN_FAILED;
}
timeMutex = mutexFactory->createMutex();
if (timeMutex == nullptr) {
return HasReturnvaluesIF::RETURN_FAILED;
}
}
return HasReturnvaluesIF::RETURN_OK;
}

View File

@ -0,0 +1,37 @@
#include "fsfw/timemanager/Countdown.h"
Countdown::Countdown(uint32_t initialTimeout): timeout(initialTimeout) {
}
Countdown::~Countdown() {
}
ReturnValue_t Countdown::setTimeout(uint32_t miliseconds) {
ReturnValue_t return_value = Clock::getUptime( &startTime );
timeout = miliseconds;
return return_value;
}
bool Countdown::hasTimedOut() const {
uint32_t current_time;
Clock::getUptime( &current_time );
if ( uint32_t(current_time - startTime) >= timeout) {
return true;
} else {
return false;
}
}
bool Countdown::isBusy() const {
return !hasTimedOut();
}
ReturnValue_t Countdown::resetTimer() {
return setTimeout(timeout);
}
void Countdown::timeOut() {
uint32_t current_time;
Clock::getUptime( &current_time );
startTime= current_time - timeout;
}

View File

@ -0,0 +1,31 @@
#ifndef FSFW_TIMEMANAGER_COUNTDOWN_H_
#define FSFW_TIMEMANAGER_COUNTDOWN_H_
#include "Clock.h"
/**
* @brief This file defines the Countdown class.
* @author baetz
*/
class Countdown {
public:
uint32_t timeout;
Countdown(uint32_t initialTimeout = 0);
~Countdown();
ReturnValue_t setTimeout(uint32_t miliseconds);
bool hasTimedOut() const;
bool isBusy() const;
//!< Use last set timeout value and restart timer.
ReturnValue_t resetTimer();
//!< Make hasTimedOut() return true
void timeOut();
private:
uint32_t startTime = 0;
};
#endif /* FSFW_TIMEMANAGER_COUNTDOWN_H_ */

View File

@ -0,0 +1,26 @@
#ifndef FSFW_TIMEMANAGER_RECEIVESTIMEINFOIF_H_
#define FSFW_TIMEMANAGER_RECEIVESTIMEINFOIF_H_
#include "../ipc/MessageQueueSenderIF.h"
/**
* This is a Interface for classes that receive timing information
* with the help of a dedicated message queue.
*/
class ReceivesTimeInfoIF {
public:
/**
* Returns the id of the queue which receives the timing information.
* @return Queue id of the timing queue.
*/
virtual MessageQueueId_t getTimeReceptionQueue() const = 0;
/**
* Empty virtual destructor.
*/
virtual ~ReceivesTimeInfoIF() {
}
};
#endif /* FSFW_TIMEMANAGER_RECEIVESTIMEINFOIF_H_ */

View File

@ -0,0 +1,74 @@
#include "fsfw/timemanager/Stopwatch.h"
#include "fsfw/serviceinterface/ServiceInterface.h"
#if FSFW_CPP_OSTREAM_ENABLED == 1
#include <iomanip>
#endif
Stopwatch::Stopwatch(bool displayOnDestruction,
StopwatchDisplayMode displayMode): displayOnDestruction(
displayOnDestruction), displayMode(displayMode) {
// Measures start time on initialization.
Clock::getUptime(&startTime);
}
void Stopwatch::start() {
Clock::getUptime(&startTime);
}
dur_millis_t Stopwatch::stop(bool display) {
stopInternal();
if(display) {
this->display();
}
return elapsedTime.tv_sec * 1000 + elapsedTime.tv_usec / 1000;
}
double Stopwatch::stopSeconds() {
stopInternal();
return timevalOperations::toDouble(elapsedTime);
}
void Stopwatch::display() {
if(displayMode == StopwatchDisplayMode::MILLIS) {
dur_millis_t timeMillis = static_cast<dur_millis_t>(
elapsedTime.tv_sec * 1000 + elapsedTime.tv_usec / 1000);
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::info << "Stopwatch: Operation took " << timeMillis << " milliseconds" << std::endl;
#else
sif::printInfo("Stopwatch: Operation took %lu milliseconds\n\r",
static_cast<unsigned int>(timeMillis));
#endif
}
else if(displayMode == StopwatchDisplayMode::SECONDS) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::info <<"Stopwatch: Operation took " << std::setprecision(3)
<< std::fixed << timevalOperations::toDouble(elapsedTime)
<< " seconds" << std::endl;
#else
sif::printInfo("Stopwatch: Operation took %.3f seconds\n\r",
static_cast<float>(timevalOperations::toDouble(elapsedTime)));
#endif
}
}
Stopwatch::~Stopwatch() {
if(displayOnDestruction) {
stopInternal();
display();
}
}
void Stopwatch::setDisplayMode(StopwatchDisplayMode displayMode) {
this->displayMode = displayMode;
}
StopwatchDisplayMode Stopwatch::getDisplayMode() const {
return displayMode;
}
void Stopwatch::stopInternal() {
timeval endTime;
Clock::getUptime(&endTime);
elapsedTime = endTime - startTime;
}

View File

@ -0,0 +1,70 @@
#ifndef FSFW_TIMEMANAGER_STOPWATCH_H_
#define FSFW_TIMEMANAGER_STOPWATCH_H_
#include "Clock.h"
enum class StopwatchDisplayMode {
MILLIS,
SECONDS
};
/**
* @brief Simple Stopwatch implementation to measure elapsed time
* @details
* This class can be used to measure elapsed times. It also displays elapsed
* times automatically on destruction if not explicitely deactivated in the
* constructor. The default time format is the elapsed time in miliseconds
* in seconds as a double.
* @author R. Mueller
*/
class Stopwatch {
public:
/**
* Default constructor. Call "Stopwatch stopwatch" without brackets if
* no parameters are required!
* @param displayOnDestruction If set to true, displays measured time on
* object destruction
* @param displayMode Display format is either MS rounded or MS as double
* format
* @param outputPrecision If using double format, specify precision here.
*/
Stopwatch(bool displayOnDestruction = true, StopwatchDisplayMode displayMode
= StopwatchDisplayMode::MILLIS);
virtual~ Stopwatch();
/**
* Caches the start time
*/
void start();
/**
* Calculates the elapsed time since start and returns it
* @return elapsed time in milliseconds (rounded)
*/
dur_millis_t stop(bool display = false);
/**
* Calculates the elapsed time since start and returns it
* @return elapsed time in seconds (double precision)
*/
double stopSeconds();
/**
* Displays the elapsed times on the osstream, depending on internal display
* mode.
*/
void display();
StopwatchDisplayMode getDisplayMode() const;
void setDisplayMode(StopwatchDisplayMode displayMode);
bool displayOnDestruction = true;
private:
timeval startTime {0, 0};
timeval elapsedTime {0, 0};
StopwatchDisplayMode displayMode = StopwatchDisplayMode::MILLIS;
void stopInternal();
};
#endif /* FSFW_TIMEMANAGER_STOPWATCH_H_ */

View File

@ -0,0 +1,30 @@
#include "fsfw/timemanager/TimeMessage.h"
TimeMessage::TimeMessage() {
this->messageSize += sizeof(timeval) + sizeof(uint32_t);
}
TimeMessage::TimeMessage(timeval setTime, uint32_t CounterValue) {
memcpy (this->getData(), &setTime, sizeof(timeval));
this->messageSize += sizeof(timeval) + sizeof(uint32_t);
memcpy (this->getData() + sizeof(timeval), &CounterValue, sizeof(uint32_t));
}
TimeMessage::~TimeMessage() {
}
timeval TimeMessage::getTime() {
timeval temp;
memcpy( &temp, this->getData(), sizeof(timeval));
return temp;
}
uint32_t TimeMessage::getCounterValue() {
uint32_t temp;
memcpy ( &temp, this->getData() + sizeof(timeval), sizeof(uint32_t));
return temp;
}
size_t TimeMessage::getMinimumMessageSize() const {
return this->MAX_SIZE;
}

View File

@ -0,0 +1,49 @@
#ifndef FSFW_TIMEMANAGER_TIMEMESSAGE_H_
#define FSFW_TIMEMANAGER_TIMEMESSAGE_H_
#include "Clock.h"
#include "../ipc/MessageQueueMessage.h"
#include <cstring>
class TimeMessage : public MessageQueueMessage {
protected:
/**
* @brief This call always returns the same fixed size of the message.
* @return Returns HEADER_SIZE + \c sizeof(timeval) + sizeof(uint32_t).
*/
size_t getMinimumMessageSize() const override;
public:
/**
* @ brief the size of a TimeMessage
*/
static const uint32_t MAX_SIZE = HEADER_SIZE + sizeof(timeval) + sizeof(uint32_t);
/**
* @brief In the default constructor, only the message_size is set.
*/
TimeMessage();
/**
* @brief With this constructor, the passed time information is directly put
* into the message.
* @param setTime The time information to put into the message.
* @param counterValue The counterValue to put into the message (GPS PPS).
*/
TimeMessage( timeval setTime, uint32_t counterValue = 0 );
/**
* @brief The class's destructor is empty.
*/
~TimeMessage();
/**
* @brief This getter returns the time information in timeval format.
* @return Returns the time stored in this packet.
*/
timeval getTime();
/**
* @brief This getter returns the CounterValue in uint32_t format.
* @return Returns the CounterValue stored in this packet.
*/
uint32_t getCounterValue();
};
#endif /* FSFW_TIMEMANAGER_TIMEMESSAGE_H_ */

View File

@ -0,0 +1,24 @@
#include "fsfw/timemanager/TimeStamper.h"
#include "fsfw/timemanager/Clock.h"
#include <cstring>
TimeStamper::TimeStamper(object_id_t objectId): SystemObject(objectId) {}
ReturnValue_t TimeStamper::addTimeStamp(uint8_t* buffer,
const uint8_t maxSize) {
if(maxSize < TimeStamperIF::MISSION_TIMESTAMP_SIZE){
return HasReturnvaluesIF::RETURN_FAILED;
}
timeval now;
Clock::getClock_timeval(&now);
CCSDSTime::CDS_short cds;
ReturnValue_t result = CCSDSTime::convertToCcsds(&cds,&now);
if(result != HasReturnvaluesIF::RETURN_OK){
return result;
}
std::memcpy(buffer,&cds,sizeof(cds));
return result;
}

View File

@ -0,0 +1,36 @@
#ifndef FSFW_TIMEMANAGER_TIMESTAMPER_H_
#define FSFW_TIMEMANAGER_TIMESTAMPER_H_
#include "TimeStamperIF.h"
#include "CCSDSTime.h"
#include "../objectmanager/SystemObject.h"
/**
* @brief Time stamper which can be used to add any timestamp to a
* given buffer.
* @details
* This time stamper uses the CCSDS CDC short timestamp as a fault timestamp.
* This timestamp has a size of 8 bytes. A custom timestamp can be used by
* overriding the #addTimeStamp function.
* @ingroup utility
*/
class TimeStamper: public TimeStamperIF, public SystemObject {
public:
/**
* @brief Default constructor which also registers the time stamper as a
* system object so it can be found with the #objectManager.
* @param objectId
*/
TimeStamper(object_id_t objectId);
/**
* Adds a CCSDS CDC short 8 byte timestamp to the given buffer.
* This function can be overriden to use a custom timestamp.
* @param buffer
* @param maxSize
* @return
*/
virtual ReturnValue_t addTimeStamp(uint8_t* buffer, const uint8_t maxSize);
};
#endif /* FSFW_TIMEMANAGER_TIMESTAMPER_H_ */

View File

@ -0,0 +1,30 @@
#ifndef FSFW_TIMEMANAGER_TIMESTAMPERIF_H_
#define FSFW_TIMEMANAGER_TIMESTAMPERIF_H_
#include <FSFWConfig.h>
#include "../returnvalues/HasReturnvaluesIF.h"
#include <FSFWConfig.h>
/**
* A class implementing this IF provides facilities to add a time stamp to the
* buffer provided.
* Implementors need to ensure that calling the method is thread-safe, i.e.
* addTimeStamp may be called in parallel from a different context.
*/
class TimeStamperIF {
public:
static const uint8_t INTERFACE_ID = CLASS_ID::TIME_STAMPER_IF;
static const ReturnValue_t BAD_TIMESTAMP = MAKE_RETURN_CODE(1);
//! This is a mission-specific constant and determines the total
//! size reserved for timestamps.
static const uint8_t MISSION_TIMESTAMP_SIZE = fsfwconfig::FSFW_MISSION_TIMESTAMP_SIZE;
virtual ReturnValue_t addTimeStamp(uint8_t* buffer,
const uint8_t maxSize) = 0;
virtual ~TimeStamperIF() {}
};
#endif /* FSFW_TIMEMANAGER_TIMESTAMPERIF_H_ */

View File

@ -0,0 +1,13 @@
#ifndef FSFW_TIMEMANAGER_CLOCKDEFINITIONS_H_
#define FSFW_TIMEMANAGER_CLOCKDEFINITIONS_H_
#include <cstdint>
// I'd also like to include the TimeOfDay_t struct here, but that would
// break code which uses Clock::TimeOfDay_t. Solution would be to use
// a Clock namespace instead of class with static functions.
//! Don't use these for time points, type is not large enough for UNIX epoch.
using dur_millis_t = uint32_t;
#endif /* FSFW_TIMEMANAGER_CLOCKDEFINITIONS_H_ */