From d3f244ec239f98496c6e5434bcd23b577aaef24f Mon Sep 17 00:00:00 2001 From: Jerome Hue Date: Tue, 28 May 2024 11:57:17 +0200 Subject: [PATCH] Clean includes and move members from public to private --- Sts1CobcSw/Periphery/FramRingBuffer.cpp | 0 Sts1CobcSw/Periphery/FramRingBuffer.hpp | 77 +++++++++---------------- Tests/UnitTests/CMakeLists.txt | 5 +- Tests/UnitTests/FramRingBuffer.test.cpp | 8 ++- 4 files changed, 37 insertions(+), 53 deletions(-) delete mode 100644 Sts1CobcSw/Periphery/FramRingBuffer.cpp diff --git a/Sts1CobcSw/Periphery/FramRingBuffer.cpp b/Sts1CobcSw/Periphery/FramRingBuffer.cpp deleted file mode 100644 index e69de29b..00000000 diff --git a/Sts1CobcSw/Periphery/FramRingBuffer.hpp b/Sts1CobcSw/Periphery/FramRingBuffer.hpp index da41d008..79cebcb1 100644 --- a/Sts1CobcSw/Periphery/FramRingBuffer.hpp +++ b/Sts1CobcSw/Periphery/FramRingBuffer.hpp @@ -2,111 +2,84 @@ #include -#include #include +#include #include -#include -#include #include #include #include -#include "Sts1CobcSw/Utility/Span.hpp" - namespace sts1cobcsw::fram { -using sts1cobcsw::Byte; using sts1cobcsw::Span; -using sts1cobcsw::operator""_b; // NOLINT(misc-unused-using-decls) using sts1cobcsw::TriviallySerializable; -// TODO: Handle non trivially serializable types/class -// template template class RingBuffer { public: - Address startAddress; - std::uint32_t writeIndex = 0; - std::uint32_t readIndex = 0; - std::uint32_t currentWrite = UINT32_MAX; - // TODO: Check that this is correct, or that it should be - std::array vals; - + // NOLINTNEXTLINE(*non-private-member-variables-in-classes) std::uint64_t writeCnt = 0; + // NOLINTNEXTLINE(*non-private-member-variables-in-classes) std::uint64_t readCnt = 0; + // NOLINTNEXTLINE(*non-private-member-variables-in-classes) std::uint32_t occupiedCnt = 0; - explicit RingBuffer(Address address) : startAddress(address) - { - } - - auto GetNextEntryToPut() -> T * + explicit RingBuffer(Address address) : startAddress_(address) { - return &vals[writeIndex]; } void Put(T const & newdata) { - currentWrite = writeIndex; - // NOTE: For trivially serializable types, sizeof and serialSize should be the same. - auto const address = startAddress + writeIndex * serialSize; + currentWrite_ = writeIndex_; + auto const address = startAddress_ + writeIndex_ * serialSize; fram::WriteTo(address, Span(Serialize(newdata)), 0); - writeIndex++; + writeIndex_++; - if(writeIndex >= poolSize) + if(writeIndex_ >= poolSize) { - writeIndex = 0; + writeIndex_ = 0; } if(occupiedCnt < poolSize) { occupiedCnt++; } writeCnt++; - currentWrite = UINT32_MAX; + currentWrite_ = UINT32_MAX; } // Get next item void Get(T & fromRing) { // Jump the current being written record - if(readIndex == currentWrite) + if(readIndex_ == currentWrite_) { - readIndex++; + readIndex_++; } - if(readIndex >= poolSize) + if(readIndex_ >= poolSize) { - readIndex = 0; + readIndex_ = 0; } // readIndex should not pass write index if there is no data after it - if(writeCnt < poolSize && readIndex >= writeIndex) + if(writeCnt < poolSize && readIndex_ >= writeIndex_) { - readIndex = 0; + readIndex_ = 0; } - auto const address = startAddress + readIndex * serialSize; - // TODO: Deal with timeout + auto const address = startAddress_ + readIndex_ * serialSize; + // TODO: Add a real timeout value (spiTimeout in hw tests is 30ms, but for much more data) auto readData = fram::ReadFrom>(address, 0); fromRing = Deserialize(std::span(readData)); - // DEBUG PRINT - std::printf("Printing read data in buffer.Get() method :\n"); - for(auto byte : readData) + readIndex_++; + if(readIndex_ >= poolSize) { - std::printf("0x%02x ", static_cast(byte)); - } - std::printf("\n"); - // END DEBUG PRINT - - readIndex++; - if(readIndex >= poolSize) - { - readIndex = 0; + readIndex_ = 0; } if(occupiedCnt > 0) @@ -120,5 +93,11 @@ class RingBuffer { return poolSize; } + +private: + Address startAddress_; + std::uint32_t writeIndex_ = 0; + std::uint32_t readIndex_ = 0; + std::uint32_t currentWrite_ = UINT32_MAX; }; } diff --git a/Tests/UnitTests/CMakeLists.txt b/Tests/UnitTests/CMakeLists.txt index 8853c413..2b1c8193 100644 --- a/Tests/UnitTests/CMakeLists.txt +++ b/Tests/UnitTests/CMakeLists.txt @@ -12,7 +12,10 @@ target_link_libraries( ) add_program(RingBuffer FramRingBuffer.test.cpp) -target_link_libraries(Sts1CobcSwTests_RingBuffer PRIVATE Catch2::Catch2WithMain Sts1CobcSw_Periphery Sts1CobcSw_Serial) +target_link_libraries( + Sts1CobcSwTests_RingBuffer PRIVATE Catch2::Catch2WithMain Sts1CobcSw_Periphery + Sts1CobcSw_Serial +) # TODO: Enable again once problem with segmentation violation on CI is fixed add_program(LfsRam LfsRam.test.cpp) diff --git a/Tests/UnitTests/FramRingBuffer.test.cpp b/Tests/UnitTests/FramRingBuffer.test.cpp index 80f49b4d..1a024f45 100644 --- a/Tests/UnitTests/FramRingBuffer.test.cpp +++ b/Tests/UnitTests/FramRingBuffer.test.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include @@ -76,7 +77,7 @@ TEST_CASE("RODOS ringbuffer wrapping") } } -// TODO: Split in differents tests or sections +// TODO: Split in different tests or sections TEST_CASE("RingBuffer put and get operations") { fram::ram::SetAllDoFunctions(); @@ -177,8 +178,9 @@ TEST_CASE("Program status and history") // Put() and Get() a ProgramStatusHistoryEntry variable. auto ringBuffer = fram::RingBuffer(0); - auto value = ProgramStatusHistoryEntry{ - .programId = 12, .startTime = 34, .status = ProgramStatus::programExecutionFailed}; + auto value = ProgramStatusHistoryEntry{.programId = sts1cobcsw::ProgramId(12), + .startTime = 34, + .status = ProgramStatus::programExecutionFailed}; auto result = ProgramStatusHistoryEntry{}; ringBuffer.Put(value);