diff --git a/datapool/PoolDataSetIF.h b/datapool/PoolDataSetIF.h index aa45fa54..99c06cfd 100644 --- a/datapool/PoolDataSetIF.h +++ b/datapool/PoolDataSetIF.h @@ -1,19 +1,17 @@ #ifndef FSFW_DATAPOOL_POOLDATASETIF_H_ #define FSFW_DATAPOOL_POOLDATASETIF_H_ +#include "ReadCommitIF.h" #include "DataSetIF.h" /** * @brief Extendes the DataSetIF by adding abstract functions to lock * and unlock a data pool and read/commit semantics. */ -class PoolDataSetIF: public DataSetIF { +class PoolDataSetIF: public DataSetIF, public ReadCommitIF { public: virtual~ PoolDataSetIF() {}; - virtual ReturnValue_t read(dur_millis_t lockTimeout) = 0; - virtual ReturnValue_t commit(dur_millis_t lockTimeout) = 0; - /** * @brief Most underlying data structures will have a pool like structure * and will require a lock and unlock mechanism to ensure diff --git a/datapool/PoolVariableIF.h b/datapool/PoolVariableIF.h index cd15f744..9740fc12 100644 --- a/datapool/PoolVariableIF.h +++ b/datapool/PoolVariableIF.h @@ -3,6 +3,7 @@ #include "../returnvalues/HasReturnvaluesIF.h" #include "../serialize/SerializeIF.h" +#include "ReadCommitIF.h" /** * @brief This interface is used to control data pool @@ -17,9 +18,9 @@ * @author Bastian Baetz * @ingroup data_pool */ -class PoolVariableIF : public SerializeIF { +class PoolVariableIF : public SerializeIF, + public ReadCommitIF { friend class PoolDataSetBase; - friend class GlobDataSet; friend class LocalPoolDataSetBase; public: static constexpr uint8_t INTERFACE_ID = CLASS_ID::POOL_VARIABLE_IF; @@ -57,41 +58,6 @@ public: */ virtual void setValid(bool validity) = 0; - /** - * @brief The commit call shall write back a newly calculated local - * value to the data pool. - * @details - * It is assumed that these calls are implemented in a thread-safe manner! - */ - virtual ReturnValue_t commit(uint32_t lockTimeout) = 0; - /** - * @brief The read call shall read the value of this parameter from - * the data pool and store the content locally. - * @details - * It is assumbed that these calls are implemented in a thread-safe manner! - */ - virtual ReturnValue_t read(uint32_t lockTimeout) = 0; - -protected: - - /** - * @brief Same as commit with the difference that comitting will be - * performed without a lock - * @return - * This can be used if the lock protection is handled externally - * to avoid the overhead of locking and unlocking consecutively. - * Declared protected to avoid free public usage. - */ - virtual ReturnValue_t readWithoutLock() = 0; - /** - * @brief Same as commit with the difference that comitting will be - * performed without a lock - * @return - * This can be used if the lock protection is handled externally - * to avoid the overhead of locking and unlocking consecutively. - * Declared protected to avoid free public usage. - */ - virtual ReturnValue_t commitWithoutLock() = 0; }; using pool_rwm_t = PoolVariableIF::ReadWriteMode_t; diff --git a/datapool/ReadCommitIF.h b/datapool/ReadCommitIF.h new file mode 100644 index 00000000..0cdce371 --- /dev/null +++ b/datapool/ReadCommitIF.h @@ -0,0 +1,31 @@ +#ifndef FSFW_DATAPOOL_READCOMMITIF_H_ +#define FSFW_DATAPOOL_READCOMMITIF_H_ + +#include + +/** + * @brief Common interface for all software objects which employ read-commit + * semantics. + */ +class ReadCommitIF { +public: + virtual ~ReadCommitIF() {} + virtual ReturnValue_t read(uint32_t mutexTimeout) = 0; + virtual ReturnValue_t commit(uint32_t mutexTimeout) = 0; + +protected: + + //! Optional and protected because this is interesting for classes grouping + //! members with commit and read semantics where the lock is only necessary + //! once. + virtual ReturnValue_t readWithoutLock() { + return read(20); + } + + virtual ReturnValue_t commitWithoutLock() { + return commit(20); + } +}; + + +#endif /* FSFW_DATAPOOL_READCOMMITIF_H_ */ diff --git a/datapoollocal/LocalPoolDataSetBase.h b/datapoollocal/LocalPoolDataSetBase.h index 7eabd368..d9b6a221 100644 --- a/datapoollocal/LocalPoolDataSetBase.h +++ b/datapoollocal/LocalPoolDataSetBase.h @@ -192,7 +192,7 @@ protected: */ ReturnValue_t unlockDataPool() override; - LocalDataPoolManager* hkManager; + LocalDataPoolManager* hkManager = nullptr; /** * Set n-th bit of a byte, with n being the position from 0 diff --git a/datapoollocal/PoolReadHelper.h b/datapoollocal/PoolReadHelper.h new file mode 100644 index 00000000..4fe506ee --- /dev/null +++ b/datapoollocal/PoolReadHelper.h @@ -0,0 +1,41 @@ +#ifndef FSFW_DATAPOOLLOCAL_POOLREADHELPER_H_ +#define FSFW_DATAPOOLLOCAL_POOLREADHELPER_H_ + +#include +#include + +/** + * @brief Helper class to read data sets or pool variables + */ +class PoolReadHelper { +public: + PoolReadHelper(ReadCommitIF* readObject, uint32_t mutexTimeout = 20): + readObject(readObject), mutexTimeout(mutexTimeout) { + if(readObject != nullptr) { + readResult = readObject->read(mutexTimeout); +#if FSFW_PRINT_VERBOSITY_LEVEL == 1 + sif::error << "PoolReadHelper: Read failed!" << std::endl; +#endif + } + } + + ReturnValue_t getReadResult() const { + return readResult; + } + + ~PoolReadHelper() { + if(readObject != nullptr) { + readObject->commit(mutexTimeout); + } + + } + +private: + ReadCommitIF* readObject = nullptr; + ReturnValue_t readResult = HasReturnvaluesIF::RETURN_OK; + uint32_t mutexTimeout = 20; +}; + + + +#endif /* FSFW_DATAPOOLLOCAL_POOLREADHELPER_H_ */