-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add in Svc::ChronoTime for C++11 std::chrono implementation (#3049)
* Add in Svc::ChronoTime for C++11 std::chrono implementation. Fixes: #3048 * Fix test name * Fixing spelling problems
- Loading branch information
Showing
13 changed files
with
298 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -952,6 +952,7 @@ Thhmmss | |
thiscol | ||
thisdirdoesnotexist | ||
thisfiledoesnotexist | ||
Thu | ||
timebase | ||
timerfd | ||
timetag | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#### | ||
# FPrime CMakeLists.txt: | ||
# | ||
# SOURCE_FILES: combined list of source and autocoding files | ||
# MOD_DEPS: (optional) module dependencies | ||
# UT_SOURCE_FILES: list of source files for unit tests | ||
# | ||
# More information in the F´ CMake API documentation: | ||
# https://nasa.github.io/fprime/UsersGuide/api/cmake/API.html | ||
# | ||
#### | ||
|
||
set(SOURCE_FILES | ||
"${CMAKE_CURRENT_LIST_DIR}/ChronoTime.fpp" | ||
"${CMAKE_CURRENT_LIST_DIR}/ChronoTime.cpp" | ||
) | ||
|
||
register_fprime_module() | ||
|
||
set(UT_AUTO_HELPERS ON) | ||
set(UT_SOURCE_FILES | ||
"${CMAKE_CURRENT_LIST_DIR}/ChronoTime.fpp" | ||
"${CMAKE_CURRENT_LIST_DIR}/test/ut/ChronoTimeTester.cpp" | ||
"${CMAKE_CURRENT_LIST_DIR}/test/ut/ChronoTimeTestMain.cpp" | ||
) | ||
register_fprime_ut() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// ====================================================================== | ||
// \title ChronoTime.cpp | ||
// \author mstarch | ||
// \brief cpp file for ChronoTime component implementation class | ||
// ====================================================================== | ||
|
||
#include "Svc/ChronoTime/ChronoTime.hpp" | ||
#include <chrono> | ||
#include "FpConfig.hpp" | ||
|
||
namespace Svc { | ||
|
||
// ---------------------------------------------------------------------- | ||
// Component construction and destruction | ||
// ---------------------------------------------------------------------- | ||
|
||
ChronoTime ::ChronoTime(const char* const compName) : ChronoTimeComponentBase(compName) {} | ||
|
||
ChronoTime ::~ChronoTime() {} | ||
|
||
// ---------------------------------------------------------------------- | ||
// Handler implementations for user-defined typed input ports | ||
// ---------------------------------------------------------------------- | ||
|
||
void ChronoTime ::timeGetPort_handler(FwIndexType portNum, Fw::Time& time) { | ||
const auto time_now = std::chrono::system_clock::now(); | ||
time.set(TimeBase::TB_WORKSTATION_TIME, | ||
static_cast<U32>(std::chrono::duration_cast<std::chrono::seconds>(time_now.time_since_epoch()).count()), | ||
static_cast<U32>( | ||
std::chrono::duration_cast<std::chrono::microseconds>(time_now.time_since_epoch()).count() % 1000000)); | ||
} | ||
|
||
} // namespace Svc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module Svc { | ||
@ A time component using C++11 chrono library | ||
passive component ChronoTime { | ||
include "../Interfaces/TimeInterface.fppi" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// ====================================================================== | ||
// \title ChronoTime.hpp | ||
// \author mstarch | ||
// \brief hpp file for ChronoTime component implementation class | ||
// ====================================================================== | ||
|
||
#ifndef Svc_ChronoTime_HPP | ||
#define Svc_ChronoTime_HPP | ||
|
||
#include "Svc/ChronoTime/ChronoTimeComponentAc.hpp" | ||
|
||
namespace Svc { | ||
|
||
class ChronoTime : public ChronoTimeComponentBase { | ||
public: | ||
// ---------------------------------------------------------------------- | ||
// Component construction and destruction | ||
// ---------------------------------------------------------------------- | ||
|
||
//! Construct ChronoTime object | ||
ChronoTime(const char* const compName //!< The component name | ||
); | ||
|
||
//! Destroy ChronoTime object | ||
~ChronoTime(); | ||
|
||
PRIVATE: | ||
// ---------------------------------------------------------------------- | ||
// Handler implementations for user-defined typed input ports | ||
// ---------------------------------------------------------------------- | ||
|
||
//! Handler implementation for timeGetPort | ||
//! | ||
//! Port to retrieve time | ||
void timeGetPort_handler(FwIndexType portNum, //!< The port number | ||
Fw::Time& time //!< Reference to Time object | ||
) override; | ||
}; | ||
|
||
} // namespace Svc | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Svc::ChronoTime | ||
|
||
As an F Prime project matures, a custom time component will likely be developed soliciting time in the project directed | ||
way. For reference projects and projects in an early phase of development, this custom time component does not exist. | ||
This miss-match was historically filled by Svc::PosixTime (formerly Svc::LinuxTime), however; this requires a project to | ||
support posix. | ||
|
||
The std::chrono library provided by C++11 is a language feature and as such can implement time without the need for | ||
posix. | ||
|
||
This is a C++11 implementation of the TimeInterface used to supply time to the various parts of the system. | ||
|
||
## Requirements | ||
|
||
The following requirements describe the behavior of Svc::ChronoTime. | ||
|
||
| Name | Description | Validation | | ||
|---------------------|--------------------------------------------------------------------------|------------| | ||
| SVC_CHRONO_TIME_001 | Svc::ChronoTime shall be implemented using only C++11 language features. | Inspection | | ||
| SVC_CHRONO_TIME_002 | Svc::ChronoTime shall implement the Svc.TimeInterface. | Unit-Test | | ||
| SVC_CHRONO_TIME_003 | Svc::ChronoTime shall provide time with resolution to microseconds. | Unit-Test | | ||
|
||
|
||
## Usage Examples | ||
|
||
In order to use this component, projects must supply `TimeBase::TB_WORKSTATION_TIME` in their `TimeBase` enumeration. | ||
|
||
To use Svc::ChronoTime in your project, make sure to set the time service with the following lines in your topology: | ||
|
||
**Add the instance to the instance list:** | ||
``` | ||
instance chronoTime: Svc.PosixTime base id 0x4500 | ||
``` | ||
|
||
**Use the instance to supply time:** | ||
``` | ||
time connections instance chronoTime | ||
``` | ||
|
||
> Since an implementation of the time interface comes with he standard topology template, projects should replace it. | ||
## Change Log | ||
| Date | Description | | ||
|------------|---------------| | ||
| 2024-11-25 | Initial Draft | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// ====================================================================== | ||
// \title ChronoTimeTestMain.cpp | ||
// \author mstarch | ||
// \brief cpp file for ChronoTime component test main function | ||
// ====================================================================== | ||
|
||
#include "ChronoTimeTester.hpp" | ||
|
||
TEST(Nominal, TestBasicTime) { | ||
Svc::ChronoTimeTester tester; | ||
tester.test_basic_time(); | ||
} | ||
|
||
int main(int argc, char** argv) { | ||
::testing::InitGoogleTest(&argc, argv); | ||
return RUN_ALL_TESTS(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// ====================================================================== | ||
// \title ChronoTimeTester.cpp | ||
// \author mstarch | ||
// \brief cpp file for ChronoTime component test harness implementation class | ||
// ====================================================================== | ||
|
||
#include "ChronoTimeTester.hpp" | ||
#include <Fw/Test/UnitTest.hpp> | ||
#include <iostream> | ||
namespace Svc { | ||
|
||
// ---------------------------------------------------------------------- | ||
// Construction and destruction | ||
// ---------------------------------------------------------------------- | ||
|
||
ChronoTimeTester ::ChronoTimeTester() | ||
: ChronoTimeGTestBase("ChronoTimeTester", ChronoTimeTester::MAX_HISTORY_SIZE), component("ChronoTime") { | ||
this->initComponents(); | ||
this->connectPorts(); | ||
} | ||
|
||
ChronoTimeTester ::~ChronoTimeTester() {} | ||
|
||
// ---------------------------------------------------------------------- | ||
// Tests | ||
// ---------------------------------------------------------------------- | ||
|
||
void ChronoTimeTester ::test_basic_time() { | ||
const U32 SECONDS_SINCE_EPOCH_2024_11_25 = 1732492800ull; // Time since epoch as of date of writing | ||
const std::string expected_epoch_time("Thu Jan 1 00:00:00 1970\n"); //std::asctime adds \n | ||
|
||
// Calculate the system_clock epoch. This test is only valid when the system_clock uses unix epoch. | ||
const auto epoch = std::chrono::time_point<std::chrono::system_clock>{}; | ||
std::time_t epoch_time = std::chrono::system_clock::to_time_t(epoch); | ||
const std::string reported_epoch_time(std::asctime(std::gmtime(&epoch_time))); | ||
|
||
// Skip test when epoch is not the unix epoch | ||
if (expected_epoch_time != reported_epoch_time) { | ||
GTEST_SKIP() << "Cannot run std::chrono test with non-unix epoch of: " << reported_epoch_time; | ||
} | ||
|
||
REQUIREMENT("SVC_CHRONO_TIME_002"); | ||
REQUIREMENT("SVC_CHRONO_TIME_003"); | ||
|
||
// Invoke port | ||
Fw::Time time; | ||
this->invoke_to_timeGetPort(0, time); | ||
ASSERT_GT(time.getSeconds(), SECONDS_SINCE_EPOCH_2024_11_25); | ||
// Check for correct use of milliseconds | ||
ASSERT_GE(time.getUSeconds(), 0U); | ||
ASSERT_LE(time.getUSeconds(), 999999U); | ||
} | ||
|
||
} // namespace Svc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// ====================================================================== | ||
// \title ChronoTimeTester.hpp | ||
// \author mstarch | ||
// \brief hpp file for ChronoTime component test harness implementation class | ||
// ====================================================================== | ||
|
||
#ifndef Svc_ChronoTimeTester_HPP | ||
#define Svc_ChronoTimeTester_HPP | ||
|
||
#include "Svc/ChronoTime/ChronoTime.hpp" | ||
#include "Svc/ChronoTime/ChronoTimeGTestBase.hpp" | ||
|
||
namespace Svc { | ||
|
||
class ChronoTimeTester : public ChronoTimeGTestBase { | ||
public: | ||
// ---------------------------------------------------------------------- | ||
// Constants | ||
// ---------------------------------------------------------------------- | ||
|
||
// Maximum size of histories storing events, telemetry, and port outputs | ||
static const FwSizeType MAX_HISTORY_SIZE = 10; | ||
|
||
// Instance ID supplied to the component instance under test | ||
static const FwEnumStoreType TEST_INSTANCE_ID = 0; | ||
|
||
public: | ||
// ---------------------------------------------------------------------- | ||
// Construction and destruction | ||
// ---------------------------------------------------------------------- | ||
|
||
//! Construct object ChronoTimeTester | ||
ChronoTimeTester(); | ||
|
||
//! Destroy object ChronoTimeTester | ||
~ChronoTimeTester(); | ||
|
||
public: | ||
// ---------------------------------------------------------------------- | ||
// Tests | ||
// ---------------------------------------------------------------------- | ||
|
||
//! To do | ||
void test_basic_time(); | ||
|
||
private: | ||
// ---------------------------------------------------------------------- | ||
// Helper functions | ||
// ---------------------------------------------------------------------- | ||
|
||
//! Connect ports | ||
void connectPorts(); | ||
|
||
//! Initialize components | ||
void initComponents(); | ||
|
||
private: | ||
// ---------------------------------------------------------------------- | ||
// Member variables | ||
// ---------------------------------------------------------------------- | ||
|
||
//! The component under test | ||
ChronoTime component; | ||
}; | ||
|
||
} // namespace Svc | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters