2022-03-07 18:22:10 +01:00
|
|
|
#include <fsfw/globalfunctions/timevalOperations.h>
|
2022-02-23 18:23:22 +01:00
|
|
|
#include <fsfw/timemanager/CCSDSTime.h>
|
|
|
|
|
2022-02-23 18:24:07 +01:00
|
|
|
#include <array>
|
2022-02-23 18:23:22 +01:00
|
|
|
#include <catch2/catch_approx.hpp>
|
2022-02-23 18:24:07 +01:00
|
|
|
#include <catch2/catch_test_macros.hpp>
|
2022-02-23 18:23:22 +01:00
|
|
|
|
|
|
|
#include "fsfw_tests/unit/CatchDefinitions.h"
|
|
|
|
|
|
|
|
TEST_CASE("CCSDSTime Tests", "[TestCCSDSTime]") {
|
|
|
|
INFO("CCSDSTime Tests");
|
|
|
|
CCSDSTime::Ccs_mseconds cssMilliSecconds;
|
|
|
|
Clock::TimeOfDay_t time;
|
|
|
|
time.year = 2020;
|
|
|
|
time.month = 2;
|
|
|
|
time.day = 29;
|
|
|
|
time.hour = 13;
|
|
|
|
time.minute = 24;
|
|
|
|
time.second = 45;
|
|
|
|
time.usecond = 123456;
|
|
|
|
SECTION("Test CCS Time") {
|
|
|
|
auto result = CCSDSTime::convertToCcsds(&cssMilliSecconds, &time);
|
|
|
|
REQUIRE(result == HasReturnvaluesIF::RETURN_OK);
|
|
|
|
REQUIRE(cssMilliSecconds.pField == 0x52); // 0b01010010
|
|
|
|
REQUIRE(cssMilliSecconds.yearMSB == 0x07);
|
|
|
|
REQUIRE(cssMilliSecconds.yearLSB == 0xe4);
|
|
|
|
REQUIRE(cssMilliSecconds.month == 2);
|
|
|
|
REQUIRE(cssMilliSecconds.day == 29);
|
|
|
|
REQUIRE(cssMilliSecconds.hour == 13);
|
|
|
|
REQUIRE(cssMilliSecconds.minute == 24);
|
|
|
|
REQUIRE(cssMilliSecconds.second == 45);
|
|
|
|
uint16_t secondsMinus4 = (static_cast<uint16_t>(cssMilliSecconds.secondEminus2) * 100) +
|
|
|
|
cssMilliSecconds.secondEminus4;
|
|
|
|
REQUIRE(secondsMinus4 == 1234);
|
|
|
|
Clock::TimeOfDay_t timeTo;
|
|
|
|
const uint8_t* dataPtr = reinterpret_cast<const uint8_t*>(&cssMilliSecconds);
|
|
|
|
size_t length = sizeof(CCSDSTime::Ccs_mseconds);
|
|
|
|
result = CCSDSTime::convertFromCCS(&timeTo, dataPtr, &length, sizeof(CCSDSTime::Ccs_mseconds));
|
|
|
|
REQUIRE(result == HasReturnvaluesIF::RETURN_OK);
|
|
|
|
REQUIRE(cssMilliSecconds.pField == 0x52); // 0b01010010
|
|
|
|
REQUIRE(cssMilliSecconds.yearMSB == 0x07);
|
|
|
|
REQUIRE(cssMilliSecconds.yearLSB == 0xe4);
|
|
|
|
REQUIRE(cssMilliSecconds.month == 2);
|
|
|
|
REQUIRE(cssMilliSecconds.day == 29);
|
|
|
|
REQUIRE(cssMilliSecconds.hour == 13);
|
|
|
|
REQUIRE(cssMilliSecconds.minute == 24);
|
|
|
|
REQUIRE(cssMilliSecconds.second == 45);
|
|
|
|
REQUIRE(timeTo.year == 2020);
|
|
|
|
REQUIRE(timeTo.month == 2);
|
|
|
|
REQUIRE(timeTo.day == 29);
|
|
|
|
REQUIRE(timeTo.hour == 13);
|
|
|
|
REQUIRE(timeTo.minute == 24);
|
|
|
|
REQUIRE(timeTo.second == 45);
|
|
|
|
REQUIRE(timeTo.usecond == 123400);
|
|
|
|
}
|
2022-02-23 18:24:07 +01:00
|
|
|
SECTION("CCS_Day of Year") {
|
2022-02-23 18:23:22 +01:00
|
|
|
Clock::TimeOfDay_t timeTo;
|
2022-02-23 18:24:07 +01:00
|
|
|
std::array<uint8_t, 8> ccsDayOfYear = {0b01011000, 0x07, 0xe4, 0, 60, 13, 24, 45};
|
2022-02-23 18:23:22 +01:00
|
|
|
size_t length = ccsDayOfYear.size();
|
2022-02-23 18:24:07 +01:00
|
|
|
auto result =
|
|
|
|
CCSDSTime::convertFromCCS(&timeTo, ccsDayOfYear.data(), &length, ccsDayOfYear.size());
|
2022-02-23 18:23:22 +01:00
|
|
|
REQUIRE(result == HasReturnvaluesIF::RETURN_OK);
|
|
|
|
// Check constness
|
|
|
|
REQUIRE(ccsDayOfYear[0] == 0b01011000);
|
|
|
|
REQUIRE(ccsDayOfYear[1] == 0x07);
|
|
|
|
REQUIRE(ccsDayOfYear[2] == 0xe4);
|
|
|
|
REQUIRE(ccsDayOfYear[3] == 0x0);
|
|
|
|
REQUIRE(ccsDayOfYear[4] == 60);
|
|
|
|
REQUIRE(ccsDayOfYear[5] == 13);
|
|
|
|
REQUIRE(ccsDayOfYear[6] == 24);
|
|
|
|
REQUIRE(ccsDayOfYear[7] == 45);
|
|
|
|
REQUIRE(timeTo.year == 2020);
|
|
|
|
REQUIRE(timeTo.month == 2);
|
|
|
|
REQUIRE(timeTo.day == 29);
|
|
|
|
REQUIRE(timeTo.hour == 13);
|
|
|
|
REQUIRE(timeTo.minute == 24);
|
|
|
|
REQUIRE(timeTo.second == 45);
|
|
|
|
REQUIRE(timeTo.usecond == 0);
|
|
|
|
}
|
2022-02-23 18:24:07 +01:00
|
|
|
SECTION("Test convertFromASCII") {
|
|
|
|
std::string timeAscii = "2022-12-31T23:59:59.123Z";
|
|
|
|
Clock::TimeOfDay_t timeTo;
|
|
|
|
const uint8_t* timeChar = reinterpret_cast<const uint8_t*>(timeAscii.c_str());
|
2022-03-28 18:32:51 +02:00
|
|
|
auto result = CCSDSTime::convertFromASCII(&timeTo, timeChar, timeAscii.length());
|
|
|
|
REQUIRE(result == HasReturnvaluesIF::RETURN_OK);
|
2022-02-23 18:23:22 +01:00
|
|
|
REQUIRE(timeTo.year == 2022);
|
|
|
|
REQUIRE(timeTo.month == 12);
|
|
|
|
REQUIRE(timeTo.day == 31);
|
|
|
|
REQUIRE(timeTo.hour == 23);
|
|
|
|
REQUIRE(timeTo.minute == 59);
|
|
|
|
REQUIRE(timeTo.second == 59);
|
|
|
|
REQUIRE(timeTo.usecond == Catch::Approx(123000));
|
2022-03-28 18:32:51 +02:00
|
|
|
|
|
|
|
std::string timeAscii2 = "2022-365T23:59:59.123Z";
|
|
|
|
const uint8_t* timeChar2 = reinterpret_cast<const uint8_t*>(timeAscii2.c_str());
|
|
|
|
Clock::TimeOfDay_t timeTo2;
|
|
|
|
result = CCSDSTime::convertFromCcsds(&timeTo2, timeChar2, timeAscii2.length());
|
|
|
|
REQUIRE(result == HasReturnvaluesIF::RETURN_OK);
|
|
|
|
REQUIRE(timeTo2.year == 2022);
|
|
|
|
REQUIRE(timeTo2.month == 12);
|
|
|
|
REQUIRE(timeTo2.day == 31);
|
|
|
|
REQUIRE(timeTo2.hour == 23);
|
|
|
|
REQUIRE(timeTo2.minute == 59);
|
|
|
|
REQUIRE(timeTo2.second == 59);
|
|
|
|
REQUIRE(timeTo2.usecond == Catch::Approx(123000));
|
2022-02-23 18:23:22 +01:00
|
|
|
}
|
2022-03-07 18:22:10 +01:00
|
|
|
|
|
|
|
SECTION("CDS Conversions") {
|
|
|
|
// Preperation
|
|
|
|
Clock::TimeOfDay_t time;
|
|
|
|
time.year = 2020;
|
|
|
|
time.month = 2;
|
|
|
|
time.day = 29;
|
|
|
|
time.hour = 13;
|
|
|
|
time.minute = 24;
|
|
|
|
time.second = 45;
|
|
|
|
time.usecond = 123456;
|
|
|
|
timeval timeAsTimeval;
|
|
|
|
auto result = Clock::convertTimeOfDayToTimeval(&time, &timeAsTimeval);
|
|
|
|
CHECK(result == HasReturnvaluesIF::RETURN_OK);
|
|
|
|
CHECK(timeAsTimeval.tv_sec == 1582982685);
|
|
|
|
CHECK(timeAsTimeval.tv_usec == 123456);
|
|
|
|
|
|
|
|
// Conversion to CDS Short
|
|
|
|
CCSDSTime::CDS_short cdsTime;
|
|
|
|
result = CCSDSTime::convertToCcsds(&cdsTime, &timeAsTimeval);
|
|
|
|
CHECK(result == HasReturnvaluesIF::RETURN_OK);
|
2022-03-09 18:56:08 +01:00
|
|
|
// Days in CCSDS Epoch 22704 (0x58B0)
|
|
|
|
CHECK(cdsTime.dayMSB == 0x58);
|
|
|
|
CHECK(cdsTime.dayLSB == 0xB0);
|
|
|
|
// MS of day 48285123.456 (floored here)
|
|
|
|
CHECK(cdsTime.msDay_hh == 0x2);
|
|
|
|
CHECK(cdsTime.msDay_h == 0xE0);
|
|
|
|
CHECK(cdsTime.msDay_l == 0xC5);
|
|
|
|
CHECK(cdsTime.msDay_ll == 0xC3);
|
2022-03-28 18:32:51 +02:00
|
|
|
CHECK(cdsTime.pField == CCSDSTime::P_FIELD_CDS_SHORT);
|
2022-03-07 18:22:10 +01:00
|
|
|
|
|
|
|
// Conversion back to timeval
|
|
|
|
timeval timeReturnAsTimeval;
|
|
|
|
result = CCSDSTime::convertFromCDS(&timeReturnAsTimeval, &cdsTime);
|
|
|
|
CHECK(result == HasReturnvaluesIF::RETURN_OK);
|
2022-03-09 18:56:08 +01:00
|
|
|
// micro seconds precision is lost
|
2022-03-07 18:22:10 +01:00
|
|
|
timeval difference = timeAsTimeval - timeReturnAsTimeval;
|
|
|
|
CHECK(difference.tv_usec == 456);
|
|
|
|
CHECK(difference.tv_sec == 0);
|
2022-03-25 18:47:31 +01:00
|
|
|
|
|
|
|
Clock::TimeOfDay_t timeReturnAsTimeOfDay;
|
|
|
|
result = CCSDSTime::convertFromCDS(&timeReturnAsTimeOfDay, &cdsTime);
|
|
|
|
CHECK(result == HasReturnvaluesIF::RETURN_OK);
|
|
|
|
CHECK(timeReturnAsTimeOfDay.year == 2020);
|
|
|
|
CHECK(timeReturnAsTimeOfDay.month == 2);
|
|
|
|
CHECK(timeReturnAsTimeOfDay.day == 29);
|
|
|
|
CHECK(timeReturnAsTimeOfDay.hour == 13);
|
|
|
|
CHECK(timeReturnAsTimeOfDay.minute == 24);
|
|
|
|
CHECK(timeReturnAsTimeOfDay.second == 45);
|
|
|
|
// micro seconds precision is lost
|
|
|
|
CHECK(timeReturnAsTimeOfDay.usecond == 123000);
|
2022-03-28 18:32:51 +02:00
|
|
|
|
|
|
|
Clock::TimeOfDay_t timeReturnAsTodFromBuffer;
|
|
|
|
const uint8_t* buffer = reinterpret_cast<const uint8_t*>(&cdsTime);
|
|
|
|
result = CCSDSTime::convertFromCDS(&timeReturnAsTodFromBuffer, buffer, sizeof(cdsTime));
|
|
|
|
REQUIRE(result == HasReturnvaluesIF::RETURN_OK);
|
|
|
|
CHECK(timeReturnAsTodFromBuffer.year == time.year);
|
|
|
|
CHECK(timeReturnAsTodFromBuffer.month == time.month);
|
|
|
|
CHECK(timeReturnAsTodFromBuffer.day == time.day);
|
|
|
|
CHECK(timeReturnAsTodFromBuffer.hour == time.hour);
|
|
|
|
CHECK(timeReturnAsTodFromBuffer.minute == time.minute);
|
|
|
|
CHECK(timeReturnAsTodFromBuffer.second == time.second);
|
|
|
|
CHECK(timeReturnAsTodFromBuffer.usecond == 123000);
|
|
|
|
|
|
|
|
|
|
|
|
Clock::TimeOfDay_t todFromCCSDS;
|
|
|
|
result = CCSDSTime::convertFromCcsds(&todFromCCSDS, buffer, sizeof(cdsTime));
|
|
|
|
CHECK(result == HasReturnvaluesIF::RETURN_OK);
|
|
|
|
CHECK(todFromCCSDS.year == time.year);
|
|
|
|
CHECK(todFromCCSDS.month == time.month);
|
|
|
|
CHECK(todFromCCSDS.day == time.day);
|
|
|
|
CHECK(todFromCCSDS.hour == time.hour);
|
|
|
|
CHECK(todFromCCSDS.minute == time.minute);
|
|
|
|
CHECK(todFromCCSDS.second == time.second);
|
|
|
|
CHECK(todFromCCSDS.usecond == 123000);
|
|
|
|
}
|
|
|
|
SECTION("CCSDS Failures"){
|
|
|
|
Clock::TimeOfDay_t time;
|
|
|
|
time.year = 2020;
|
|
|
|
time.month = 12;
|
|
|
|
time.day = 32;
|
|
|
|
time.hour = 13;
|
|
|
|
time.minute = 24;
|
|
|
|
time.second = 45;
|
|
|
|
time.usecond = 123456;
|
|
|
|
CCSDSTime::Ccs_mseconds to;
|
|
|
|
auto result = CCSDSTime::convertToCcsds(&to, &time);
|
|
|
|
REQUIRE(result == CCSDSTime::INVALID_TIME_FORMAT);
|
|
|
|
CCSDSTime::Ccs_seconds to2;
|
|
|
|
result = CCSDSTime::convertToCcsds(&to2, &time);
|
|
|
|
REQUIRE(result == CCSDSTime::INVALID_TIME_FORMAT);
|
2022-03-07 18:22:10 +01:00
|
|
|
}
|
2022-02-23 18:23:22 +01:00
|
|
|
}
|