Compare commits

..

6 Commits

5 changed files with 110 additions and 148 deletions

View File

@@ -467,7 +467,7 @@ ReturnValue_t DeviceHandlerBase::insertInCommandMap(DeviceCommandId_t deviceComm
info.expectedReplies = 0; info.expectedReplies = 0;
info.isExecuting = false; info.isExecuting = false;
info.sendReplyTo = NO_COMMANDER; info.sendReplyTo = NO_COMMANDER;
info.useAlternativeReplyId = alternativeReplyId; info.useAlternativeReplyId = useAlternativeReply;
info.alternativeReplyId = alternativeReplyId; info.alternativeReplyId = alternativeReplyId;
auto resultPair = deviceCommandMap.emplace(deviceCommand, info); auto resultPair = deviceCommandMap.emplace(deviceCommand, info);
if (resultPair.second) { if (resultPair.second) {
@@ -1601,3 +1601,14 @@ void DeviceHandlerBase::disableCommandsAndReplies() {
} }
} }
} }
ReturnValue_t DeviceHandlerBase::finishAction(bool success, DeviceCommandId_t action,
ReturnValue_t result) {
auto commandIter = deviceCommandMap.find(action);
if (commandIter == deviceCommandMap.end()) {
return MessageQueueIF::NO_QUEUE;
}
commandIter->second.isExecuting = false;
actionHelper.finish(success, commandIter->second.sendReplyTo, action, result);
return returnvalue::OK;
}

View File

@@ -399,6 +399,8 @@ class DeviceHandlerBase : public DeviceHandlerIF,
*/ */
virtual ReturnValue_t interpretDeviceReply(DeviceCommandId_t id, const uint8_t *packet) = 0; virtual ReturnValue_t interpretDeviceReply(DeviceCommandId_t id, const uint8_t *packet) = 0;
MessageQueueId_t getCommanderQueueId(DeviceCommandId_t replyId) const; MessageQueueId_t getCommanderQueueId(DeviceCommandId_t replyId) const;
ReturnValue_t finishAction(bool success, DeviceCommandId_t action, ReturnValue_t result);
/** /**
* Helper function to get pending command. This is useful for devices * Helper function to get pending command. This is useful for devices
* like SPI sensors to identify the last sent command. * like SPI sensors to identify the last sent command.

View File

@@ -3,159 +3,95 @@
#include <fsfw/serviceinterface/ServiceInterface.h> #include <fsfw/serviceinterface/ServiceInterface.h>
#include <cstdio> #include <cstdio>
#include <fstream>
#include <iostream>
DleParser::DleParser(SimpleRingBuffer& decodeRingBuf, DleEncoder& decoder, BufPair encodedBuf, DleParser::DleParser(SimpleRingBuffer& decodeRingBuf, DleEncoder& decoder, BufPair encodedBuf,
BufPair decodedBuf, UserHandler handler, void* args) BufPair decodedBuf)
: decodeRingBuf(decodeRingBuf), : decodeRingBuf(decodeRingBuf),
decoder(decoder), decoder(decoder),
encodedBuf(encodedBuf), encodedBuf(encodedBuf),
decodedBuf(decodedBuf), decodedBuf(decodedBuf) {
handler(handler),
ctx(args) {
if (handler == nullptr) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "DleParser::DleParser: Invalid user handler" << std::endl;
#else
sif::printError("DleParser::DleParser: Invalid user handler\n");
#endif
}
} }
ReturnValue_t DleParser::passData(uint8_t* data, size_t len) { ReturnValue_t DleParser::passData(const uint8_t* data, size_t len) {
if (data == nullptr or len == 0 or handler == nullptr) { if (data == nullptr or len == 0) {
return returnvalue::FAILED; return returnvalue::FAILED;
} }
size_t copyIntoRingBufFromHere = 0; return decodeRingBuf.writeData(data, len);
size_t copyAmount = len; }
size_t startIdx = 0;
ReturnValue_t result = returnvalue::OK; ReturnValue_t DleParser::parseRingBuf(size_t& readSize) {
bool startFoundInThisPacket = false; ctx.setType(DleParser::ContextType::NONE);
for (size_t idx = 0; idx < len; idx++) { size_t availableData = decodeRingBuf.getAvailableReadData();
if (data[idx] == DleEncoder::STX_CHAR) { if(availableData == 0) {
if (not startFound and not startFoundInThisPacket) { return NO_PACKET_FOUND;
startIdx = idx; }
copyIntoRingBufFromHere = idx; if (availableData > encodedBuf.second) {
copyAmount = len - idx; ErrorInfo info;
info.len = decodeRingBuf.getAvailableReadData();
setErrorContext(ErrorTypes::DECODING_BUF_TOO_SMALL, info);
return returnvalue::FAILED;
}
ReturnValue_t result = decodeRingBuf.readData(encodedBuf.first, availableData);
if (result != returnvalue::OK) {
ErrorInfo info;
info.res = result;
setErrorContext(ErrorTypes::RING_BUF_ERROR, info);
return result;
}
bool stxFound = false;
size_t stxIdx = 0;
for (size_t vectorIdx = 0; vectorIdx < availableData; vectorIdx++) {
// handle STX char
if (encodedBuf.first[vectorIdx] == DleEncoder::STX_CHAR) {
if (not stxFound) {
stxFound = true;
stxIdx = vectorIdx;
} else { } else {
// Maybe print warning, should not happen // might be lost packet, so we should advance the read pointer
decodeRingBuf.clear(); // without skipping the STX
readSize = vectorIdx;
ErrorInfo info; ErrorInfo info;
info.len = idx; setErrorContext(ErrorTypes::CONSECUTIVE_STX_CHARS, info);
prepareErrorContext(ErrorTypes::CONSECUTIVE_STX_CHARS, info); return POSSIBLE_PACKET_LOSS;
handler(ctx);
copyIntoRingBufFromHere = idx;
copyAmount = len - idx;
} }
startFound = true; }
startFoundInThisPacket = true; // handle ETX char
} else if (data[idx] == DleEncoder::ETX_CHAR) { if (encodedBuf.first[vectorIdx] == DleEncoder::ETX_CHAR) {
if (startFoundInThisPacket) { if (stxFound) {
size_t readLen = 0; // This is propably a packet, so we decode it.
size_t decodedLen = 0; size_t decodedLen = 0;
result = decoder.decode(data + startIdx, idx + 1 - startIdx, &readLen, decodedBuf.first, size_t dummy = 0;
decodedBuf.second, &decodedLen);
ReturnValue_t result =
decoder.decode(&encodedBuf.first[stxIdx], availableData - stxIdx, &dummy,
decodedBuf.first, decodedBuf.second, &decodedLen);
if (result == returnvalue::OK) { if (result == returnvalue::OK) {
ctx.setType(ContextType::PACKET_FOUND); ctx.setType(ContextType::PACKET_FOUND);
ctx.decodedPacket.first = decodedBuf.first; ctx.decodedPacket.first = decodedBuf.first;
ctx.decodedPacket.second = decodedLen; ctx.decodedPacket.second = decodedLen;
this->handler(ctx); readSize = ++vectorIdx;
} else if (result == DleEncoder::STREAM_TOO_SHORT) { return returnvalue::OK;
} else {
// invalid packet, skip.
readSize = ++vectorIdx;
ErrorInfo info; ErrorInfo info;
info.res = result; info.res = result;
prepareErrorContext(ErrorTypes::DECODING_BUF_TOO_SMALL, info); setErrorContext(ErrorTypes::DECODE_ERROR, info);
handler(ctx); return POSSIBLE_PACKET_LOSS;
} else {
ErrorInfo info;
info.res = result;
prepareErrorContext(ErrorTypes::DECODING_BUF_TOO_SMALL, info);
handler(ctx);
}
decodeRingBuf.clear();
if ((idx + 1) < len) {
copyIntoRingBufFromHere = idx + 1;
copyAmount = len - idx - 1;
} else {
copyAmount = 0;
}
} else if (startFound) {
// ETX found but STX was found in another mini packet. Reconstruct the full packet
// to decode it
result = decodeRingBuf.writeData(data, idx + 1);
if (result != returnvalue::OK) {
ErrorInfo info;
info.res = result;
prepareErrorContext(ErrorTypes::RING_BUF_ERROR, info);
handler(ctx);
}
size_t fullEncodedLen = decodeRingBuf.getAvailableReadData();
if (fullEncodedLen > encodedBuf.second) {
ErrorInfo info;
info.len = fullEncodedLen;
prepareErrorContext(ErrorTypes::ENCODED_BUF_TOO_SMALL, info);
handler(ctx);
decodeRingBuf.clear();
} else {
size_t decodedLen = 0;
size_t readLen = 0;
decodeRingBuf.readData(encodedBuf.first, fullEncodedLen, true);
result = decoder.decode(encodedBuf.first, fullEncodedLen, &readLen, decodedBuf.first,
decodedBuf.second, &decodedLen);
if (result == returnvalue::OK) {
if (this->handler != nullptr) {
ctx.setType(ContextType::PACKET_FOUND);
ctx.decodedPacket.first = decodedBuf.first;
ctx.decodedPacket.second = decodedLen;
this->handler(ctx);
}
} else if (result == DleEncoder::STREAM_TOO_SHORT) {
ErrorInfo info;
info.res = result;
prepareErrorContext(ErrorTypes::DECODING_BUF_TOO_SMALL, info);
handler(ctx);
} else {
ErrorInfo info;
info.res = result;
prepareErrorContext(ErrorTypes::DECODE_ERROR, info);
handler(ctx);
}
decodeRingBuf.clear();
startFound = false;
startFoundInThisPacket = false;
if ((idx + 1) < len) {
copyIntoRingBufFromHere = idx + 1;
copyAmount = len - idx - 1;
} else {
copyAmount = 0;
}
} }
} else { } else {
// End data without preceeding STX // might be lost packet, so we should advance the read pointer
readSize = ++vectorIdx;
ErrorInfo info; ErrorInfo info;
info.len = idx + 1; info.len = 0;
prepareErrorContext(ErrorTypes::CONSECUTIVE_ETX_CHARS, info); setErrorContext(ErrorTypes::CONSECUTIVE_ETX_CHARS, info);
handler(ctx); return POSSIBLE_PACKET_LOSS;
decodeRingBuf.clear();
if ((idx + 1) < len) {
copyIntoRingBufFromHere = idx + 1;
copyAmount = len - idx - 1;
} else {
copyAmount = 0;
}
} }
startFoundInThisPacket = false;
startFound = false;
} }
} }
if (copyAmount > 0) { return NO_PACKET_FOUND;
result = decodeRingBuf.writeData(data + copyIntoRingBufFromHere, copyAmount);
if (result != returnvalue::OK) {
ErrorInfo info;
info.res = result;
prepareErrorContext(ErrorTypes::RING_BUF_ERROR, info);
handler(ctx);
}
}
return returnvalue::OK;
} }
void DleParser::defaultFoundPacketHandler(uint8_t* packet, size_t len, void* args) { void DleParser::defaultFoundPacketHandler(uint8_t* packet, size_t len, void* args) {
@@ -169,8 +105,12 @@ void DleParser::defaultFoundPacketHandler(uint8_t* packet, size_t len, void* arg
#endif #endif
} }
void DleParser::defaultErrorHandler(ErrorTypes err, ErrorInfo ctx) { void DleParser::defaultErrorHandler() {
switch (err) { if(ctx.getType() != DleParser::ContextType::ERROR) {
errorPrinter("No error");
return;
}
switch (ctx.error.first) {
case (ErrorTypes::NONE): { case (ErrorTypes::NONE): {
errorPrinter("No error"); errorPrinter("No error");
break; break;
@@ -186,8 +126,8 @@ void DleParser::defaultErrorHandler(ErrorTypes err, ErrorInfo ctx) {
case (ErrorTypes::ENCODED_BUF_TOO_SMALL): case (ErrorTypes::ENCODED_BUF_TOO_SMALL):
case (ErrorTypes::DECODING_BUF_TOO_SMALL): { case (ErrorTypes::DECODING_BUF_TOO_SMALL): {
char opt[64]; char opt[64];
snprintf(opt, sizeof(opt), ": Too small for packet with length %zu", ctx.len); snprintf(opt, sizeof(opt), ": Too small for packet with length %zu", ctx.decodedPacket.second);
if (err == ErrorTypes::ENCODED_BUF_TOO_SMALL) { if (ctx.error.first == ErrorTypes::ENCODED_BUF_TOO_SMALL) {
errorPrinter("Encoded buf too small", opt); errorPrinter("Encoded buf too small", opt);
} else { } else {
errorPrinter("Decoding buf too small", opt); errorPrinter("Decoding buf too small", opt);
@@ -218,13 +158,20 @@ void DleParser::errorPrinter(const char* str, const char* opt) {
#endif #endif
} }
void DleParser::prepareErrorContext(ErrorTypes err, ErrorInfo info) { void DleParser::setErrorContext(ErrorTypes err, ErrorInfo info) {
ctx.setType(ContextType::ERROR); ctx.setType(ContextType::ERROR);
ctx.error.first = err; ctx.error.first = err;
ctx.error.second = info; ctx.error.second = info;
} }
ReturnValue_t DleParser::confirmBytesRead(size_t bytesRead) {
return decodeRingBuf.deleteData(bytesRead);
}
const DleParser::Context& DleParser::getContext() {
return ctx;
}
void DleParser::reset() { void DleParser::reset() {
startFound = false;
decodeRingBuf.clear(); decodeRingBuf.clear();
} }

View File

@@ -18,9 +18,11 @@
*/ */
class DleParser { class DleParser {
public: public:
static constexpr ReturnValue_t NO_PACKET_FOUND = returnvalue::makeCode(1, 1);
static constexpr ReturnValue_t POSSIBLE_PACKET_LOSS = returnvalue::makeCode(1, 2);
using BufPair = std::pair<uint8_t*, size_t>; using BufPair = std::pair<uint8_t*, size_t>;
enum class ContextType { PACKET_FOUND, ERROR }; enum class ContextType { NONE, PACKET_FOUND, ERROR };
enum class ErrorTypes { enum class ErrorTypes {
NONE, NONE,
@@ -41,7 +43,7 @@ class DleParser {
struct Context { struct Context {
public: public:
Context(void* args) : userArgs(args) { setType(ContextType::PACKET_FOUND); } Context() { setType(ContextType::PACKET_FOUND); }
void setType(ContextType type) { void setType(ContextType type) {
this->type = type; this->type = type;
@@ -58,14 +60,11 @@ class DleParser {
BufPair decodedPacket = {}; BufPair decodedPacket = {};
ErrorPair error; ErrorPair error;
void* userArgs;
private: private:
ContextType type; ContextType type;
}; };
using UserHandler = void (*)(const Context& ctx);
/** /**
* Base class constructor * Base class constructor
* @param decodeRingBuf Ring buffer used to store multiple packets to allow detecting DLE packets * @param decodeRingBuf Ring buffer used to store multiple packets to allow detecting DLE packets
@@ -79,7 +78,7 @@ class DleParser {
* @param args Arbitrary user argument * @param args Arbitrary user argument
*/ */
DleParser(SimpleRingBuffer& decodeRingBuf, DleEncoder& decoder, BufPair encodedBuf, DleParser(SimpleRingBuffer& decodeRingBuf, DleEncoder& decoder, BufPair encodedBuf,
BufPair decodedBuf, UserHandler handler, void* args); BufPair decodedBuf);
/** /**
* This function allows to pass new data into the parser. It then scans for DLE packets * This function allows to pass new data into the parser. It then scans for DLE packets
@@ -88,8 +87,13 @@ class DleParser {
* @param len * @param len
* @return * @return
*/ */
ReturnValue_t passData(uint8_t* data, size_t len); ReturnValue_t passData(const uint8_t* data, size_t len);
ReturnValue_t parseRingBuf(size_t& bytesRead);
ReturnValue_t confirmBytesRead(size_t bytesRead);
const Context& getContext();
/** /**
* Example found packet handler * Example found packet handler
* function call * function call
@@ -104,11 +108,11 @@ class DleParser {
* - For buffer length errors, will be set to the detected packet length which is too large * - For buffer length errors, will be set to the detected packet length which is too large
* - For decode or ring buffer errors, will be set to the result returned from the failed call * - For decode or ring buffer errors, will be set to the result returned from the failed call
*/ */
static void defaultErrorHandler(ErrorTypes err, ErrorInfo ctx); void defaultErrorHandler();
static void errorPrinter(const char* str, const char* opt = nullptr); static void errorPrinter(const char* str, const char* opt = nullptr);
void prepareErrorContext(ErrorTypes err, ErrorInfo ctx); void setErrorContext(ErrorTypes err, ErrorInfo ctx);
/** /**
* Resets the parser by resetting the internal states and clearing the decoding ring buffer * Resets the parser by resetting the internal states and clearing the decoding ring buffer
*/ */
@@ -119,7 +123,5 @@ class DleParser {
DleEncoder& decoder; DleEncoder& decoder;
BufPair encodedBuf; BufPair encodedBuf;
BufPair decodedBuf; BufPair decodedBuf;
UserHandler handler = nullptr;
Context ctx; Context ctx;
bool startFound = false;
}; };

View File

@@ -15,8 +15,8 @@ Service5EventReporting::Service5EventReporting(PsbParams params, size_t maxNumbe
maxNumberReportsPerCycle(maxNumberReportsPerCycle) { maxNumberReportsPerCycle(maxNumberReportsPerCycle) {
auto mqArgs = MqArgs(getObjectId(), static_cast<void*>(this)); auto mqArgs = MqArgs(getObjectId(), static_cast<void*>(this));
psbParams.name = "PUS 5 Event Reporting"; psbParams.name = "PUS 5 Event Reporting";
eventQueue = QueueFactory::instance()->createMessageQueue(messageQueueDepth, eventQueue = QueueFactory::instance()->createMessageQueue(
MessageQueueMessage::MAX_MESSAGE_SIZE, &mqArgs); messageQueueDepth, MessageQueueMessage::MAX_MESSAGE_SIZE, &mqArgs);
} }
Service5EventReporting::~Service5EventReporting() { Service5EventReporting::~Service5EventReporting() {