diff --git a/datapoollocal/LocalPoolDataSetBase.cpp b/datapoollocal/LocalPoolDataSetBase.cpp index 9f23fc6c..085ae227 100644 --- a/datapoollocal/LocalPoolDataSetBase.cpp +++ b/datapoollocal/LocalPoolDataSetBase.cpp @@ -96,22 +96,22 @@ ReturnValue_t LocalPoolDataSetBase::serializeWithValidityBuffer(uint8_t **buffer SerializeIF::Endianness streamEndianness) const { ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; uint8_t validityMaskSize = std::ceil(static_cast(fillCount)/8.0); - uint8_t validityMask[validityMaskSize]; + uint8_t validityMask[validityMaskSize] = {}; uint8_t validBufferIndex = 0; uint8_t validBufferIndexBit = 0; for (uint16_t count = 0; count < fillCount; count++) { if(registeredVariables[count]->isValid()) { - // set validity buffer here. - this->bitSetter(validityMask + validBufferIndex, - validBufferIndexBit); - if(validBufferIndexBit == 7) { - validBufferIndex ++; - validBufferIndexBit = 0; - } - else { - validBufferIndexBit ++; - } + /* Set bit at correct position */ + this->bitSetter(validityMask + validBufferIndex, validBufferIndexBit); } + if(validBufferIndexBit == 7) { + validBufferIndex ++; + validBufferIndexBit = 0; + } + else { + validBufferIndexBit ++; + } + result = registeredVariables[count]->serialize(buffer, size, maxSize, streamEndianness); if (result != HasReturnvaluesIF::RETURN_OK) { @@ -246,21 +246,6 @@ ReturnValue_t LocalPoolDataSetBase::serialize(uint8_t **buffer, size_t *size, } } -void LocalPoolDataSetBase::bitSetter(uint8_t* byte, uint8_t position) const { - if(position > 7) { -#if FSFW_CPP_OSTREAM_ENABLED == 1 - sif::warning << "LocalPoolDataSetBase::bitSetter: Invalid position!" - << std::endl; -#else - sif::printWarning("LocalPoolDataSetBase::bitSetter: " - "Invalid position!\n\r"); -#endif - return; - } - uint8_t shiftNumber = position + (7 - 2 * position); - *byte |= 1 << shiftNumber; -} - void LocalPoolDataSetBase::setDiagnostic(bool isDiagnostics) { this->diagnostic = isDiagnostics; } @@ -296,19 +281,6 @@ sid_t LocalPoolDataSetBase::getSid() const { return sid; } -bool LocalPoolDataSetBase::bitGetter(const uint8_t* byte, - uint8_t position) const { - if(position > 7) { -#if FSFW_CPP_OSTREAM_ENABLED == 1 - sif::debug << "Pool Raw Access: Bit setting invalid position" - << std::endl; -#endif - return false; - } - uint8_t shiftNumber = position + (7 - 2 * position); - return *byte & (1 << shiftNumber); -} - bool LocalPoolDataSetBase::isValid() const { return this->valid; } @@ -328,3 +300,30 @@ object_id_t LocalPoolDataSetBase::getCreatorObjectId() { } return objects::NO_OBJECT; } + +void LocalPoolDataSetBase::bitSetter(uint8_t* byte, uint8_t position) { + if(position > 7) { +#if FSFW_CPP_OSTREAM_ENABLED == 1 + sif::warning << "LocalPoolDataSetBase::bitSetter: Invalid position!" + << std::endl; +#else + sif::printWarning("LocalPoolDataSetBase::bitSetter: " + "Invalid position!\n\r"); +#endif + return; + } + uint8_t shiftNumber = position + (7 - 2 * position); + *byte |= 1 << shiftNumber; +} + +bool LocalPoolDataSetBase::bitGetter(const uint8_t* byte, uint8_t position) { + if(position > 7) { +#if FSFW_CPP_OSTREAM_ENABLED == 1 + sif::debug << "Pool Raw Access: Bit setting invalid position" + << std::endl; +#endif + return false; + } + uint8_t shiftNumber = position + (7 - 2 * position); + return *byte & (1 << shiftNumber); +} diff --git a/datapoollocal/LocalPoolDataSetBase.h b/datapoollocal/LocalPoolDataSetBase.h index ca907431..c676285c 100644 --- a/datapoollocal/LocalPoolDataSetBase.h +++ b/datapoollocal/LocalPoolDataSetBase.h @@ -160,6 +160,13 @@ public: object_id_t getCreatorObjectId(); + /* Static helper functions for manipulating validity buffers */ + /** + * Set n-th bit of a byte, with n being the position from 0 + * (most significant bit) to 7 (least significant bit) + */ + static void bitSetter(uint8_t* byte, uint8_t position); + static bool bitGetter(const uint8_t* byte, uint8_t position); protected: sid_t sid; //! This mutex is used if the data is created by one object only. @@ -218,13 +225,6 @@ protected: */ ReturnValue_t unlockDataPool() override; - /** - * Set n-th bit of a byte, with n being the position from 0 - * (most significant bit) to 7 (least significant bit) - */ - void bitSetter(uint8_t* byte, uint8_t position) const; - bool bitGetter(const uint8_t* byte, uint8_t position) const; - PeriodicHousekeepingHelper* periodicHelper = nullptr; LocalDataPoolManager* poolManager = nullptr; diff --git a/unittest/tests/datapoollocal/DataSetTest.cpp b/unittest/tests/datapoollocal/DataSetTest.cpp index 929e5d76..7b2632c2 100644 --- a/unittest/tests/datapoollocal/DataSetTest.cpp +++ b/unittest/tests/datapoollocal/DataSetTest.cpp @@ -134,7 +134,39 @@ TEST_CASE("LocalDataSet" , "[LocDataSetTest]") { CHECK(localSet.localPoolUint16Vec.isValid()); /* Now we do the same process but with the validity buffer */ + localSet.localPoolVarUint8 = 232; + localSet.localPoolVarFloat = -2324.322; + localSet.localPoolUint16Vec.value[0] = 232; + localSet.localPoolUint16Vec.value[1] = 23923; + localSet.localPoolUint16Vec.value[2] = 1; + localSet.localPoolVarUint8.setValid(true); + localSet.localPoolVarFloat.setValid(false); + localSet.localPoolUint16Vec.setValid(true); localSet.setValidityBufferGeneration(true); + maxSize = localSet.getSerializedSize(); + CHECK(maxSize == sizeof(uint8_t) + sizeof(uint16_t) * 3 + sizeof(float) + 1); + serSize = 0; + buffPtr = buffer; + CHECK(localSet.serialize(&buffPtr, &serSize, maxSize, + SerializeIF::Endianness::MACHINE) == retval::CATCH_OK); + CHECK(rawUint8 == 232); + std::memcpy(&rawFloat, buffer + sizeof(uint8_t), sizeof(float)); + CHECK(rawFloat == Catch::Approx(-2324.322)); + + std::memcpy(&rawUint16Vec, buffer + sizeof(uint8_t) + sizeof(float), + 3 * sizeof(uint16_t)); + CHECK(rawUint16Vec[0] == 232); + CHECK(rawUint16Vec[1] == 23923); + CHECK(rawUint16Vec[2] == 1); + /* We can do it like this because the buffer only has one byte for + less than 8 variables */ + uint8_t validityByte = buffer[sizeof(buffer) - 1]; + CHECK(LocalPoolDataSetBase::bitGetter(&validityByte, 0) == true); + CHECK(LocalPoolDataSetBase::bitGetter(&validityByte, 1) == false); + CHECK(LocalPoolDataSetBase::bitGetter(&validityByte, 2) == true); + + /* Now we manipulate the validity buffer for the deserialization */ + } /* Common fault test cases */ @@ -145,6 +177,7 @@ TEST_CASE("LocalDataSet" , "[LocDataSetTest]") { variableHandle = nullptr; REQUIRE(localSet.registerVariable(variableHandle) == static_cast(DataSetIF::POOL_VAR_NULL)); + } /* we need to reset the subscription list because the pool owner