diff --git a/inc/Message.hpp b/inc/Message.hpp index 5ad9f049e0194b7d8634d5565ac27ee814c8143d..d471cd366689f697bdc744d1c1897929a999481a 100644 --- a/inc/Message.hpp +++ b/inc/Message.hpp @@ -4,6 +4,7 @@ #include "ECSS_Definitions.hpp" #include <cstdint> #include <cassert> +#include <iostream> /** * A telemetry (TM) or telecommand (TC) message (request/report), as specified in ECSS-E-ST-70-41C @@ -401,8 +402,18 @@ public: * * PTC = 7, PFC = 0 */ - void readOctetString(uint16_t size, uint8_t *byteString) { - readString(byteString, size); + uint8_t *readOctetString(uint16_t *length) { + uint16_t size = readUint16(); // Get the data length from the message + uint8_t *byteString = nullptr; // Pointer to store the string + + byteString = static_cast<uint8_t *>(malloc(size)); + if (byteString == nullptr) { + return nullptr; + } + readString(byteString, size); // Read the string data + *length = size; // Save the data length to the provided variable + + return byteString; // Return the read string pointer } /** diff --git a/src/Services/MemoryManagementService.cpp b/src/Services/MemoryManagementService.cpp index 5ba62e649188647ef3c6ae0fdae03e8c457bab97..26de5566a7c7caf82823e84b9be873d99da1f990 100644 --- a/src/Services/MemoryManagementService.cpp +++ b/src/Services/MemoryManagementService.cpp @@ -26,32 +26,16 @@ void MemoryManagementService::RawDataMemoryManagement::loadRawData(Message &requ assert(request.messageType == 2); // Variable declaration - uint8_t *readData = nullptr, *tempMemory = nullptr; // Pointer to store the received data + uint16_t dataLength = 0; // Data length to load uint8_t memoryID = request.readEnum8(); // Read the memory ID from the request uint16_t iterationCount = request.readUint16(); // Get the iteration count if (memoryID == MemoryManagementService::MemoryID::RAM) { - for (std::size_t j = 0, allocatedLength = 0; j < iterationCount; j++) { + for (std::size_t j = 0; j < iterationCount; j++) { uint64_t startAddress = request.readUint64(); // Start address of the memory - uint16_t dataLength = request.readUint16(); // Data length to load - - // Allocate more array space if needed - if (allocatedLength < dataLength) { - // todo: In embedded implementation use the malloc, due to FreeRTOS constraints - tempMemory = static_cast<uint8_t *>(realloc(readData, dataLength)); - if (tempMemory == nullptr) { - // todo: Add error logging and reporting - free(readData); - } else { - readData = tempMemory; - allocatedLength = dataLength; - } - } - - for (std::size_t i = 0; i < dataLength; i++) { - readData[i] = request.readByte(); - } + uint8_t *readData = request.readOctetString(&dataLength); + // todo: Error logging has to be included, if memory allocation above fails // todo: Continue only if the checksum passes (when the checksum will be implemented) for (std::size_t i = 0; i < dataLength; i++) { diff --git a/test/Services/MemoryManagementService.cpp b/test/Services/MemoryManagementService.cpp index 984c51f958e6c860e086a5b89ec5831a6600f641..8fb43736e51ec13f2c709463e461d9766a00fa24 100644 --- a/test/Services/MemoryManagementService.cpp +++ b/test/Services/MemoryManagementService.cpp @@ -32,7 +32,7 @@ TEST_CASE("TM[6,5]", "[service][st06]") { uint8_t testString_2[8] = "SecStrT"; uint8_t testString_3[2] = {5, 8}; - uint8_t checkString[8] = "\0"; + uint8_t *checkString = nullptr; uint16_t readSize = 0; MemoryManagementService memMangService; @@ -58,9 +58,8 @@ TEST_CASE("TM[6,5]", "[service][st06]") { CHECK(response.readEnum8() == MemoryManagementService::MemoryID::RAM); CHECK(response.readUint16() == 3); CHECK(response.readUint64() == reinterpret_cast<uint64_t >(testString_1)); - readSize = sizeof(testString_1)/ sizeof(testString_1[0]); - CHECK(response.readUint16() == readSize); - response.readOctetString(readSize, checkString); + checkString = response.readOctetString(&readSize); + CHECK(readSize == sizeof(testString_1)/ sizeof(testString_1[0])); CHECK(checkString[0] == 'F'); CHECK(checkString[1] == 'S'); CHECK(checkString[2] == 't'); @@ -69,9 +68,8 @@ TEST_CASE("TM[6,5]", "[service][st06]") { CHECK(checkString[5] == '\0'); CHECK(response.readUint64() == reinterpret_cast<uint64_t >(testString_2)); - readSize = sizeof(testString_2)/ sizeof(testString_2[0]); - CHECK(response.readUint16() == readSize); - response.readOctetString(readSize, checkString); + checkString = response.readOctetString(&readSize); + CHECK(readSize == sizeof(testString_2)/ sizeof(testString_2[0])); CHECK(checkString[0] == 'S'); CHECK(checkString[1] == 'e'); CHECK(checkString[2] == 'c'); @@ -82,9 +80,8 @@ TEST_CASE("TM[6,5]", "[service][st06]") { CHECK(checkString[7] == '\0'); CHECK(response.readUint64() == reinterpret_cast<uint64_t >(testString_3)); - readSize = sizeof(testString_3)/ sizeof(testString_3[0]); - CHECK(response.readUint16() == readSize); - response.readOctetString(readSize, checkString); + checkString = response.readOctetString(&readSize); + CHECK(readSize == sizeof(testString_3)/ sizeof(testString_3[0])); CHECK(checkString[0] == 5); CHECK(checkString[1] == 8); }