diff --git a/CMakeLists.txt b/CMakeLists.txt index 986ff8435fefffb9251e461b81048a9a5f54be4d..68d9b8748c16ea163a3e8d55760c327a40b1f5b0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,6 +25,7 @@ add_library(common OBJECT src/Services/RequestVerificationService.cpp src/Services/TestService.cpp src/Helpers/TimeHelper.cpp + src/Services/TimeManagementService.cpp ) # Specify the .cpp files for the executables diff --git a/inc/Services/TimeManagementService.hpp b/inc/Services/TimeManagementService.hpp new file mode 100644 index 0000000000000000000000000000000000000000..d006a5d712b27ffc0218ef31dee469d5bb7d411a --- /dev/null +++ b/inc/Services/TimeManagementService.hpp @@ -0,0 +1,37 @@ +#ifndef ECSS_SERVICES_TIMEMANAGEMENTSERVICE_HPP +#define ECSS_SERVICES_TIMEMANAGEMENTSERVICE_HPP + +#include <Service.hpp> +#include <ctime> +#include "Helpers/TimeHelper.hpp" + +/** + * Implementation of the ST[09] time management. The current implementation sends + * a report with the spacecraft time that is formatted according to the CUC time code format + * (check TimeHelper for the format) + * + * @todo When the time comes for the application processes we should consider this: All reports + * generated by the application process that is identified by APID 0 are time reports + * @todo Declare the time accuracy that the standard claims in the spacecraft + * time reference section(6.9.3.d,e) + */ +class TimeManagementService : public Service { +public: + TimeManagementService() { + serviceType = 9; + } + + /** + * TM[9,2] CUC time report + * + * @todo check if we need spacecraft time reference status + * @todo ECSS standard claims: <<The time reports generated by the time reporting subservice + * are spacecraft time packets. A spacecraft time packet does not carry the message type, + * consisting of the service type and message subtype.>> Check if we need to implement that + * or should ignore the standard? + */ + void cucTimeReport(); +}; + + +#endif //ECSS_SERVICES_TIMEMANAGEMENTSERVICE_HPP diff --git a/src/Helpers/TimeHelper.cpp b/src/Helpers/TimeHelper.cpp index f8e5c158544a1a519daff6b729c40bf5ef3f3276..462c7e8b35ea06bfe440f84b7e3e568e2a7891d2 100644 --- a/src/Helpers/TimeHelper.cpp +++ b/src/Helpers/TimeHelper.cpp @@ -30,10 +30,14 @@ uint64_t TimeHelper::implementCUCTimeFormat(uint32_t seconds) { /** * Define time format including P-field and T-Field * - * Note: Only the 40 bits of the 64 will be used for the timeFormat(0-7 : P-field, 8-39: - * T-field) + * Notes: + * Only the 40 bits of the 64 will be used for the timeFormat(0-7 : P-field, 8-39: T-field) + * + * Shift operators have high priority. That's why we should do a type-casting first so we + * don't lose valuable bits */ - uint64_t timeFormat = (totalSeconds << 8 | pField); + uint64_t timeFormat = (static_cast<uint64_t>(totalSeconds) << 8 | pField); + return timeFormat; } diff --git a/src/Services/TimeManagementService.cpp b/src/Services/TimeManagementService.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2b3feb68aa059b7131863d84cd2d6e79d8328fa1 --- /dev/null +++ b/src/Services/TimeManagementService.cpp @@ -0,0 +1,20 @@ +#include "Services/TimeManagementService.hpp" + +void TimeManagementService::cucTimeReport() { + // TM[9,2] CUC time report + + Message timeReport = createTM(2); + + /** + * For the time being we will use C++ functions to get a time value, but this will change + * when the RTC will be implemented + */ + uint32_t seconds; + seconds = time(nullptr); // seconds have passed since 00:00:00 GMT, Jan 1, 1970 + uint64_t timeFormat = TimeHelper::implementCUCTimeFormat(seconds); // store the return value + + timeReport.appendByte(timeFormat); // append the P-field + timeReport.appendWord(timeFormat >> 8); // append the T-field + + storeMessage(timeReport); +} diff --git a/src/main.cpp b/src/main.cpp index 3219196bc212f42548f6049597b28e51651bcbb3..6aef88a11b4877eb7b08570e880667da98d210fb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,6 +6,7 @@ #include "Services/RequestVerificationService.hpp" #include "Services/MemoryManagementService.hpp" #include "Services/EventReportService.hpp" +#include "Services/TimeManagementService.hpp" #include "Message.hpp" #include "MessageParser.hpp" #include "Services/MemoryManagementService.hpp" @@ -173,7 +174,11 @@ int main() { // TimeHelper test uint64_t test = TimeHelper::implementCUCTimeFormat(1200); - std::cout << "\n" << test; + std::cout << "\n" << test << "\n"; + + // ST[09] test + TimeManagementService timeReport; + timeReport.cucTimeReport(); // ST[05] (5,5 to 5,8) test [works] EventReportService::Event eventIDs[] = {EventReportService::HighSeverityUnknownEvent, diff --git a/test/Services/TimeManagementService.cpp b/test/Services/TimeManagementService.cpp new file mode 100644 index 0000000000000000000000000000000000000000..714c99e144480c5037a286a5dcdada334f06d145 --- /dev/null +++ b/test/Services/TimeManagementService.cpp @@ -0,0 +1,16 @@ +#include <catch2/catch.hpp> +#include <Services/TimeManagementService.hpp> +#include "ServiceTests.hpp" + +TEST_CASE("TM[9,2]", "[service][st09]") { + TimeManagementService timeFormat; + + uint32_t seconds; + seconds = time(nullptr); + + timeFormat.cucTimeReport(); + Message response = ServiceTests::get(0); + CHECK(response.readByte() == 50); + CHECK(response.readWord() == seconds); + +}