diff --git a/linux/boardtest/UartTestClass.cpp b/linux/boardtest/UartTestClass.cpp index 7723dc86..b4a23c6c 100644 --- a/linux/boardtest/UartTestClass.cpp +++ b/linux/boardtest/UartTestClass.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include // write(), read(), close() @@ -167,6 +168,7 @@ void UartTestClass::scexInit() { void UartTestClass::scexPeriodic() { using namespace std; + using namespace scex; if (reader == nullptr) { return; } @@ -187,38 +189,39 @@ void UartTestClass::scexPeriodic() { ReturnValue_t result = reader->readReceivedMessage(uartCookie, &decodedPacket, &len); if (len > 0) { - sif::info << "CmdByte: " << std::setw(2) << std::setfill('0') << std::hex - << (int)decodedPacket[0] << std::dec << endl; - scex::ScexCmds cmd = static_cast((decodedPacket[0] >> 1) & 0b11111); - sif::info << "Command: 0x" << std::setw(2) << std::setfill('0') << std::hex - << static_cast(cmd) << std::dec << std::endl; - size_t packetCounter = decodedPacket[1]; - sif::info << "PacketCounter: " << packetCounter << endl; - size_t totalPacketCounter = decodedPacket[2]; - sif::info << "TotalPacketCount: " << totalPacketCounter << endl; - uint16_t packetLen = (decodedPacket[3] << 8) | (decodedPacket[4]); - sif::info << "PacketLength: " << packetLen << endl; - uint16_t expectedPacketLen = packetLen + 7; + ScexHelper helper; + const uint8_t* helperPtr = decodedPacket; + result = helper.deSerialize(&helperPtr, &len); + if (result == ScexHelper::INVALID_CRC) { + sif::warning << "CRC invalid" << std::endl; + } + sif::info << helper << endl; - sif::info << "ExpectedPacketLength: " << packetLen + 7 << endl; - if (expectedPacketLen != len) { - sif::warning << "ExpectedPacketLength " << expectedPacketLen << " is not Length" << len - << endl; + //ping + //if ping cmd + ofstream out("/tmp/scex-ping.bin", ofstream::binary ); + if (out.bad()) { + sif::warning << "bad" < + //countdown (max 2min), wenn nicht if (helper.getPacketCounter() == helper.getTotalPacketCounter()) { nach 2min reader->finish(); + if(helper.getCmd() == FRAM) { + if(helper.getPacketCounter() == 0) { + // neues file anlegen wie oben ping + } else { + // an bestehendes file hinzufügen + } } - if (packetCounter == totalPacketCounter) { + out << helper; + + if (helper.getPacketCounter() == helper.getTotalPacketCounter()) { reader->finish(); sif::info << "Reader is finished" << endl; cmdDone = true; - // TODO: Bug in firmware, other command will be returned - cmdSent = false; - // if (cmd == scex::ScexCmds::PING) { - // cmdSent = false; - // } + if (helper.getCmd() == scex::ScexCmds::PING) { + cmdSent = false; + } } } } diff --git a/linux/boardtest/UartTestClass.h b/linux/boardtest/UartTestClass.h index 03c6805d..0a228782 100644 --- a/linux/boardtest/UartTestClass.h +++ b/linux/boardtest/UartTestClass.h @@ -61,6 +61,7 @@ class UartTestClass : public TestTask { std::array cmdBuf = {}; std::array recBuf = {}; ScexDleParser* dleParser; + scex::ScexCmds cmdHelper; uint8_t recvCnt = 0; }; diff --git a/linux/devices/CMakeLists.txt b/linux/devices/CMakeLists.txt index 6be5da2f..9a4695ca 100644 --- a/linux/devices/CMakeLists.txt +++ b/linux/devices/CMakeLists.txt @@ -7,6 +7,7 @@ endif() target_sources(${OBSW_NAME} PRIVATE ScexUartReader.cpp ScexDleParser.cpp + ScexHelper.cpp ) add_subdirectory(ploc) add_subdirectory(startracker) diff --git a/linux/devices/ScexHelper.cpp b/linux/devices/ScexHelper.cpp new file mode 100644 index 00000000..bc7ee04f --- /dev/null +++ b/linux/devices/ScexHelper.cpp @@ -0,0 +1,83 @@ +#include "ScexHelper.h" + +#include + +#include "fsfw/serviceinterface.h" + +ScexHelper::ScexHelper() {} + +ReturnValue_t ScexHelper::serialize(uint8_t** buffer, size_t* size, size_t maxSize, + Endianness streamEndianness) const { + return RETURN_FAILED; +} + +size_t ScexHelper::getSerializedSize() const { return totalPacketLen; } + +ReturnValue_t ScexHelper::deSerialize(const uint8_t** buffer, size_t* size, + Endianness streamEndianness) { + if (buffer == nullptr or size == nullptr) { + return RETURN_FAILED; + } + if (*size < 7) { + return STREAM_TOO_SHORT; + } + start = *buffer; + cmdByteRaw = **buffer; + cmd = static_cast((cmdByteRaw >> 1) & 0b11111); + + *buffer += 1; + packetCounter = **buffer; + + *buffer += 1; + totalPacketCounter = **buffer; + + *buffer += 1; + payloadLen = (**buffer << 8) | *(*buffer + 1); + + *buffer += 2; + totalPacketLen = payloadLen + HEADER_LEN + CRC_LEN; + if (totalPacketLen >= *size) { + return STREAM_TOO_SHORT; + } + *buffer += payloadLen; + crc = (**buffer << 8) | *(*buffer + 1); + if (CRC::crc16ccitt(start, totalPacketLen) != 0) { + return INVALID_CRC; + } + return RETURN_OK; +} + +scex::ScexCmds ScexHelper::getCmd() const { return cmd; } + +uint8_t ScexHelper::getCmdByteRaw() const { return cmdByteRaw; } + +uint16_t ScexHelper::getCrc() const { return crc; } + +size_t ScexHelper::getExpectedPacketLen() const { return totalPacketLen; } + +uint8_t ScexHelper::getPacketCounter() const { return packetCounter; } + +uint16_t ScexHelper::getPayloadLen() const { return payloadLen; } + +const uint8_t* ScexHelper::getStart() const { return start; } + +uint8_t ScexHelper::getTotalPacketCounter() const { return totalPacketCounter; } + +std::ostream& operator<<(std::ostream& os, const ScexHelper& h) { + using namespace std; + sif::info << "Command Byte Raw: 0x" << std::setw(2) << std::setfill('0') << std::hex + << (int)h.cmdByteRaw << " | Command: 0x" << std::setw(2) << std::setfill('0') + << std::hex << static_cast(h.cmd) << std::dec << std::endl; + sif::info << "PacketCounter: " << h.packetCounter << endl; + sif::info << "TotalPacketCount: " << h.totalPacketCounter << endl; + sif::info << "PayloadLength: " << h.payloadLen << endl; + sif::info << "TotalPacketLength: " << h.totalPacketLen << endl; + + return os; +} + +std::ofstream& operator<<(std::ofstream& of, const ScexHelper& h) { + of.write(reinterpret_cast(h.start), h.getSerializedSize()); + + return of; +} diff --git a/linux/devices/ScexHelper.h b/linux/devices/ScexHelper.h new file mode 100644 index 00000000..a835bd0a --- /dev/null +++ b/linux/devices/ScexHelper.h @@ -0,0 +1,48 @@ +#ifndef LINUX_DEVICES_SCEXHELPER_H_ +#define LINUX_DEVICES_SCEXHELPER_H_ +#include +#include + +#include +#include +#include +#include +// ScexHelper helper; +// helper.deSerialize(data, ...); +// sif::info << helper << std::endl; +class ScexHelper : public HasReturnvaluesIF, public SerializeIF { + public: + static const ReturnValue_t INVALID_CRC = HasReturnvaluesIF::makeReturnCode(0, 2); + static constexpr uint8_t HEADER_LEN = 5; + static constexpr uint8_t CRC_LEN = 2; + ScexHelper(); + ReturnValue_t serialize(uint8_t **buffer, size_t *size, size_t maxSize, + Endianness streamEndianness) const override; + + size_t getSerializedSize() const override; + ReturnValue_t deSerialize(const uint8_t **buffer, size_t *size, + Endianness streamEndianness = Endianness::BIG) override; + friend std::ostream &operator<<(std::ostream &os, const ScexHelper &h); + friend std::ofstream &operator<<(std::ofstream &os, const ScexHelper &h); + + scex::ScexCmds getCmd() const; + uint8_t getCmdByteRaw() const; + uint16_t getCrc() const; + size_t getExpectedPacketLen() const; + uint8_t getPacketCounter() const; + uint16_t getPayloadLen() const; + const uint8_t *getStart() const; + uint8_t getTotalPacketCounter() const; + + private: + const uint8_t *start = nullptr; + uint16_t crc = 0; + uint8_t cmdByteRaw = 0; + scex::ScexCmds cmd = scex::ScexCmds::INVALID; + int packetCounter = 0; + int totalPacketCounter = 0; + uint16_t payloadLen = 0; + size_t totalPacketLen = 0; +}; + +#endif /* LINUX_DEVICES_SCEXHELPER_H_ */ diff --git a/mission/devices/devicedefinitions/ScexDefinitions.h b/mission/devices/devicedefinitions/ScexDefinitions.h index 704500f4..f479981e 100644 --- a/mission/devices/devicedefinitions/ScexDefinitions.h +++ b/mission/devices/devicedefinitions/ScexDefinitions.h @@ -6,7 +6,7 @@ // Definitions for the Solar Cell Experiment namespace scex { -enum ScexCmds : uint8_t { PING = 0b00111, ONE_CELL = 0b00110, FRAM = 0b00001 }; +enum ScexCmds : uint8_t { PING = 0b00111, ONE_CELL = 0b00110, FRAM = 0b00001, INVALID = 255 }; static constexpr uint8_t IDLE_BIT_0_DEF_STATE = 0; static constexpr uint8_t IDLE_BIT_1_DEF_STATE = 1;