fsfw/tmtcservices/CommandingServiceBase.cpp

467 lines
15 KiB
C++
Raw Normal View History

2020-08-13 20:53:35 +02:00
#include "AcceptsTelemetryIF.h"
#include "CommandingServiceBase.h"
#include "TmTcMessage.h"
2021-04-13 00:19:09 +02:00
#include <FSFWConfig.h>
2020-12-14 21:30:39 +01:00
#include "../tcdistribution/PUSDistributorIF.h"
#include "../objectmanager/ObjectManagerIF.h"
2020-08-13 20:53:35 +02:00
#include "../ipc/QueueFactory.h"
#include "../tmtcpacket/pus/TcPacketStored.h"
2021-04-13 00:19:09 +02:00
#include "../tmtcpacket/pus/TmPacketStored.h"
#include "../serviceinterface/ServiceInterface.h"
2020-07-16 11:47:11 +02:00
object_id_t CommandingServiceBase::defaultPacketSource = objects::NO_OBJECT;
object_id_t CommandingServiceBase::defaultPacketDestination = objects::NO_OBJECT;
CommandingServiceBase::CommandingServiceBase(object_id_t setObjectId,
uint16_t apid, uint8_t service, uint8_t numberOfParallelCommands,
2020-07-16 11:47:11 +02:00
uint16_t commandTimeoutSeconds, size_t queueDepth) :
2020-06-10 22:13:49 +02:00
SystemObject(setObjectId), apid(apid), service(service),
timeoutSeconds(commandTimeoutSeconds),
2020-07-16 11:47:11 +02:00
commandMap(numberOfParallelCommands) {
commandQueue = QueueFactory::instance()->createMessageQueue(queueDepth);
requestQueue = QueueFactory::instance()->createMessageQueue(queueDepth);
}
2020-07-16 11:47:11 +02:00
void CommandingServiceBase::setPacketSource(object_id_t packetSource) {
this->packetSource = packetSource;
}
void CommandingServiceBase::setPacketDestination(
object_id_t packetDestination) {
this->packetDestination = packetDestination;
}
CommandingServiceBase::~CommandingServiceBase() {
QueueFactory::instance()->deleteMessageQueue(commandQueue);
QueueFactory::instance()->deleteMessageQueue(requestQueue);
}
ReturnValue_t CommandingServiceBase::performOperation(uint8_t opCode) {
handleCommandQueue();
handleRequestQueue();
checkTimeout();
doPeriodicOperation();
return RETURN_OK;
}
uint16_t CommandingServiceBase::getIdentifier() {
return service;
}
MessageQueueId_t CommandingServiceBase::getRequestQueue() {
return requestQueue->getId();
}
ReturnValue_t CommandingServiceBase::initialize() {
ReturnValue_t result = SystemObject::initialize();
if (result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
2020-07-16 11:47:11 +02:00
if(packetDestination == objects::NO_OBJECT) {
packetDestination = defaultPacketDestination;
}
AcceptsTelemetryIF* packetForwarding =
objectManager->get<AcceptsTelemetryIF>(packetDestination);
2020-07-16 11:47:11 +02:00
if(packetSource == objects::NO_OBJECT) {
packetSource = defaultPacketSource;
}
PUSDistributorIF* distributor = objectManager->get<PUSDistributorIF>(
packetSource);
2020-07-16 11:47:11 +02:00
if (packetForwarding == nullptr or distributor == nullptr) {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2020-06-24 16:07:02 +02:00
sif::error << "CommandingServiceBase::intialize: Packet source or "
"packet destination invalid!" << std::endl;
#endif
2020-06-24 16:03:54 +02:00
return ObjectManagerIF::CHILD_INIT_FAILED;
}
distributor->registerService(this);
requestQueue->setDefaultDestination(
packetForwarding->getReportReceptionQueue());
IPCStore = objectManager->get<StorageManagerIF>(objects::IPC_STORE);
TCStore = objectManager->get<StorageManagerIF>(objects::TC_STORE);
if (IPCStore == nullptr or TCStore == nullptr) {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2020-06-24 16:07:02 +02:00
sif::error << "CommandingServiceBase::intialize: IPC store or TC store "
"not initialized yet!" << std::endl;
#endif
2020-06-24 16:03:54 +02:00
return ObjectManagerIF::CHILD_INIT_FAILED;
}
return RETURN_OK;
}
void CommandingServiceBase::handleCommandQueue() {
CommandMessage reply;
ReturnValue_t result = RETURN_FAILED;
while(true) {
result = commandQueue->receiveMessage(&reply);
if (result == HasReturnvaluesIF::RETURN_OK) {
handleCommandMessage(&reply);
continue;
}
else if(result == MessageQueueIF::EMPTY) {
break;
}
else {
#if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "CommandingServiceBase::handleCommandQueue: Receiving message failed"
"with code" << result << std::endl;
#else
sif::printWarning("CommandingServiceBase::handleCommandQueue: Receiving message "
"failed with code %d\n", result);
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
#endif /* FSFW_VERBOSE_LEVEL >= 1 */
break;
}
}
}
2020-06-24 16:01:17 +02:00
void CommandingServiceBase::handleCommandMessage(CommandMessage* reply) {
bool isStep = false;
CommandMessage nextCommand;
2020-06-24 16:01:17 +02:00
CommandMapIter iter = commandMap.find(reply->getSender());
// handle unrequested reply first
if (reply->getSender() == MessageQueueIF::NO_QUEUE or
iter == commandMap.end()) {
handleUnrequestedReply(reply);
return;
}
nextCommand.setCommand(CommandMessage::CMD_NONE);
2020-06-24 16:01:17 +02:00
// Implemented by child class, specifies what to do with reply.
2020-08-11 16:21:59 +02:00
ReturnValue_t result = handleReply(reply, iter->second.command, &iter->second.state,
&nextCommand, iter->second.objectId, &isStep);
2020-06-24 16:01:17 +02:00
/* If the child implementation does not implement special handling for
2020-08-01 16:53:17 +02:00
* rejected replies (RETURN_FAILED or INVALID_REPLY is returned), a
* failure verification will be generated with the reason as the
* return code and the initial command as failure parameter 1 */
if((reply->getCommand() == CommandMessage::REPLY_REJECTED) and
(result == RETURN_FAILED or result == INVALID_REPLY)) {
2020-06-29 16:53:32 +02:00
result = reply->getReplyRejectedReason();
2020-08-11 16:21:59 +02:00
failureParameter1 = iter->second.command;
2020-06-24 16:01:17 +02:00
}
switch (result) {
case EXECUTION_COMPLETE:
case RETURN_OK:
case NO_STEP_MESSAGE:
// handle result of reply handler implemented by developer.
2020-06-24 16:01:17 +02:00
handleReplyHandlerResult(result, iter, &nextCommand, reply, isStep);
break;
case INVALID_REPLY:
//might be just an unrequested reply at a bad moment
2020-06-24 16:01:17 +02:00
handleUnrequestedReply(reply);
break;
default:
if (isStep) {
verificationReporter.sendFailureReport(
2020-12-14 21:30:39 +01:00
tc_verification::PROGRESS_FAILURE, iter->second.tcInfo.ackFlags,
2020-08-11 16:21:59 +02:00
iter->second.tcInfo.tcPacketId, iter->second.tcInfo.tcSequenceControl,
result, ++iter->second.step, failureParameter1,
failureParameter2);
} else {
verificationReporter.sendFailureReport(
2020-12-14 21:30:39 +01:00
tc_verification::COMPLETION_FAILURE, iter->second.tcInfo.ackFlags,
2020-08-11 16:21:59 +02:00
iter->second.tcInfo.tcPacketId, iter->second.tcInfo.tcSequenceControl,
result, 0, failureParameter1, failureParameter2);
}
failureParameter1 = 0;
failureParameter2 = 0;
2020-06-24 16:01:17 +02:00
checkAndExecuteFifo(iter);
break;
}
}
void CommandingServiceBase::handleReplyHandlerResult(ReturnValue_t result,
2020-06-24 16:01:17 +02:00
CommandMapIter iter, CommandMessage* nextCommand,
CommandMessage* reply, bool& isStep) {
2020-08-11 16:21:59 +02:00
iter->second.command = nextCommand->getCommand();
// In case a new command is to be sent immediately, this is performed here.
// If no new command is sent, only analyse reply result by initializing
// sendResult as RETURN_OK
ReturnValue_t sendResult = RETURN_OK;
2020-06-24 16:01:17 +02:00
if (nextCommand->getCommand() != CommandMessage::CMD_NONE) {
sendResult = commandQueue->sendMessage(reply->getSender(),
nextCommand);
}
if (sendResult == RETURN_OK) {
if (isStep and result != NO_STEP_MESSAGE) {
verificationReporter.sendSuccessReport(
2020-12-14 21:30:39 +01:00
tc_verification::PROGRESS_SUCCESS,
2020-08-11 16:21:59 +02:00
iter->second.tcInfo.ackFlags, iter->second.tcInfo.tcPacketId,
iter->second.tcInfo.tcSequenceControl, ++iter->second.step);
}
else {
verificationReporter.sendSuccessReport(
2020-12-14 21:30:39 +01:00
tc_verification::COMPLETION_SUCCESS,
2020-08-11 16:21:59 +02:00
iter->second.tcInfo.ackFlags, iter->second.tcInfo.tcPacketId,
iter->second.tcInfo.tcSequenceControl, 0);
2020-06-24 16:01:17 +02:00
checkAndExecuteFifo(iter);
}
}
else {
if (isStep) {
2020-06-24 16:24:16 +02:00
nextCommand->clearCommandMessage();
verificationReporter.sendFailureReport(
2020-12-14 21:30:39 +01:00
tc_verification::PROGRESS_FAILURE, iter->second.tcInfo.ackFlags,
2020-08-11 16:21:59 +02:00
iter->second.tcInfo.tcPacketId,
iter->second.tcInfo.tcSequenceControl, sendResult,
++iter->second.step, failureParameter1, failureParameter2);
} else {
2020-06-24 16:24:16 +02:00
nextCommand->clearCommandMessage();
verificationReporter.sendFailureReport(
2020-12-14 21:30:39 +01:00
tc_verification::COMPLETION_FAILURE,
2020-08-11 16:21:59 +02:00
iter->second.tcInfo.ackFlags, iter->second.tcInfo.tcPacketId,
iter->second.tcInfo.tcSequenceControl, sendResult, 0,
failureParameter1, failureParameter2);
}
failureParameter1 = 0;
failureParameter2 = 0;
2020-06-24 16:01:17 +02:00
checkAndExecuteFifo(iter);
}
}
void CommandingServiceBase::handleRequestQueue() {
TmTcMessage message;
ReturnValue_t result;
store_address_t address;
TcPacketStored packet;
MessageQueueId_t queue;
object_id_t objectId;
for (result = requestQueue->receiveMessage(&message); result == RETURN_OK;
result = requestQueue->receiveMessage(&message)) {
address = message.getStorageId();
packet.setStoreAddress(address);
2020-07-25 10:55:28 +02:00
if ((packet.getSubService() == 0)
or (isValidSubservice(packet.getSubService()) != RETURN_OK)) {
2020-12-14 21:30:39 +01:00
rejectPacket(tc_verification::START_FAILURE, &packet, INVALID_SUBSERVICE);
continue;
}
result = getMessageQueueAndObject(packet.getSubService(),
packet.getApplicationData(), packet.getApplicationDataSize(),
&queue, &objectId);
if (result != HasReturnvaluesIF::RETURN_OK) {
2020-12-14 21:30:39 +01:00
rejectPacket(tc_verification::START_FAILURE, &packet, result);
continue;
}
//Is a command already active for the target object?
2020-06-24 16:01:17 +02:00
CommandMapIter iter;
iter = commandMap.find(queue);
if (iter != commandMap.end()) {
2020-08-11 16:21:59 +02:00
result = iter->second.fifo.insert(address);
if (result != RETURN_OK) {
2020-12-14 21:30:39 +01:00
rejectPacket(tc_verification::START_FAILURE, &packet, OBJECT_BUSY);
}
} else {
CommandInfo newInfo; //Info will be set by startExecution if neccessary
newInfo.objectId = objectId;
result = commandMap.insert(queue, newInfo, &iter);
if (result != RETURN_OK) {
2020-12-14 21:30:39 +01:00
rejectPacket(tc_verification::START_FAILURE, &packet, BUSY);
} else {
2020-06-24 16:01:17 +02:00
startExecution(&packet, iter);
}
}
}
}
2020-06-10 21:41:48 +02:00
ReturnValue_t CommandingServiceBase::sendTmPacket(uint8_t subservice,
const uint8_t* data, size_t dataLen, const uint8_t* headerData,
size_t headerSize) {
2021-04-13 00:19:09 +02:00
#if FSFW_USE_PUS_C_TELEMETRY == 0
2021-04-12 21:53:08 +02:00
TmPacketStoredPusA tmPacketStored(this->apid, this->service, subservice,
this->tmPacketCounter, data, dataLen, headerData, headerSize);
2021-04-13 00:19:09 +02:00
#else
TmPacketStoredPusC tmPacketStored(this->apid, this->service, subservice,
this->tmPacketCounter, data, dataLen, headerData, headerSize);
#endif
ReturnValue_t result = tmPacketStored.sendPacket(
requestQueue->getDefaultDestination(), requestQueue->getId());
if (result == HasReturnvaluesIF::RETURN_OK) {
this->tmPacketCounter++;
}
2020-06-10 21:41:48 +02:00
return result;
}
2020-06-10 21:41:48 +02:00
ReturnValue_t CommandingServiceBase::sendTmPacket(uint8_t subservice,
object_id_t objectId, const uint8_t *data, size_t dataLen) {
uint8_t buffer[sizeof(object_id_t)];
uint8_t* pBuffer = buffer;
2020-07-13 19:47:31 +02:00
size_t size = 0;
SerializeAdapter::serialize(&objectId, &pBuffer, &size,
sizeof(object_id_t), SerializeIF::Endianness::BIG);
2021-04-13 00:19:09 +02:00
#if FSFW_USE_PUS_C_TELEMETRY == 0
2021-04-12 21:53:08 +02:00
TmPacketStoredPusA tmPacketStored(this->apid, this->service, subservice,
2020-06-10 21:41:48 +02:00
this->tmPacketCounter, data, dataLen, buffer, size);
2021-04-13 00:19:09 +02:00
#else
TmPacketStoredPusC tmPacketStored(this->apid, this->service, subservice,
this->tmPacketCounter, data, dataLen, buffer, size);
#endif
2020-06-10 21:41:48 +02:00
ReturnValue_t result = tmPacketStored.sendPacket(
requestQueue->getDefaultDestination(), requestQueue->getId());
if (result == HasReturnvaluesIF::RETURN_OK) {
this->tmPacketCounter++;
}
return result;
}
2020-06-10 21:41:48 +02:00
ReturnValue_t CommandingServiceBase::sendTmPacket(uint8_t subservice,
SerializeIF* content, SerializeIF* header) {
2021-04-13 00:19:09 +02:00
#if FSFW_USE_PUS_C_TELEMETRY == 0
2021-04-12 21:53:08 +02:00
TmPacketStoredPusA tmPacketStored(this->apid, this->service, subservice,
2020-06-10 21:41:48 +02:00
this->tmPacketCounter, content, header);
2021-04-13 00:19:09 +02:00
#else
TmPacketStoredPusC tmPacketStored(this->apid, this->service, subservice,
this->tmPacketCounter, content, header);
#endif
2020-06-10 21:41:48 +02:00
ReturnValue_t result = tmPacketStored.sendPacket(
requestQueue->getDefaultDestination(), requestQueue->getId());
if (result == HasReturnvaluesIF::RETURN_OK) {
this->tmPacketCounter++;
}
return result;
}
2020-06-24 16:01:17 +02:00
void CommandingServiceBase::startExecution(TcPacketStored *storedPacket,
CommandMapIter iter) {
ReturnValue_t result = RETURN_OK;
CommandMessage command;
2020-08-11 16:21:59 +02:00
iter->second.subservice = storedPacket->getSubService();
result = prepareCommand(&command, iter->second.subservice,
2020-06-24 16:01:17 +02:00
storedPacket->getApplicationData(),
2020-08-11 16:21:59 +02:00
storedPacket->getApplicationDataSize(), &iter->second.state,
iter->second.objectId);
2020-06-24 16:01:17 +02:00
ReturnValue_t sendResult = RETURN_OK;
switch (result) {
case RETURN_OK:
2020-06-24 16:01:17 +02:00
if (command.getCommand() != CommandMessage::CMD_NONE) {
sendResult = commandQueue->sendMessage(iter.value->first,
&command);
}
if (sendResult == RETURN_OK) {
2020-08-11 16:21:59 +02:00
Clock::getUptime(&iter->second.uptimeOfStart);
iter->second.step = 0;
iter->second.subservice = storedPacket->getSubService();
iter->second.command = command.getCommand();
iter->second.tcInfo.ackFlags = storedPacket->getAcknowledgeFlags();
iter->second.tcInfo.tcPacketId = storedPacket->getPacketId();
iter->second.tcInfo.tcSequenceControl =
storedPacket->getPacketSequenceControl();
2020-12-14 21:30:39 +01:00
acceptPacket(tc_verification::START_SUCCESS, storedPacket);
} else {
2020-06-24 16:24:16 +02:00
command.clearCommandMessage();
2020-12-14 21:30:39 +01:00
rejectPacket(tc_verification::START_FAILURE, storedPacket, sendResult);
checkAndExecuteFifo(iter);
}
break;
case EXECUTION_COMPLETE:
2020-06-24 16:01:17 +02:00
if (command.getCommand() != CommandMessage::CMD_NONE) {
//Fire-and-forget command.
2020-06-24 16:01:17 +02:00
sendResult = commandQueue->sendMessage(iter.value->first,
&command);
}
if (sendResult == RETURN_OK) {
2020-12-14 21:30:39 +01:00
verificationReporter.sendSuccessReport(tc_verification::START_SUCCESS,
storedPacket);
2020-12-14 21:30:39 +01:00
acceptPacket(tc_verification::COMPLETION_SUCCESS, storedPacket);
checkAndExecuteFifo(iter);
} else {
2020-06-24 16:24:16 +02:00
command.clearCommandMessage();
2020-12-14 21:30:39 +01:00
rejectPacket(tc_verification::START_FAILURE, storedPacket, sendResult);
checkAndExecuteFifo(iter);
}
break;
default:
2020-12-14 21:30:39 +01:00
rejectPacket(tc_verification::START_FAILURE, storedPacket, result);
checkAndExecuteFifo(iter);
break;
}
}
void CommandingServiceBase::rejectPacket(uint8_t report_id,
TcPacketStored* packet, ReturnValue_t error_code) {
verificationReporter.sendFailureReport(report_id, packet, error_code);
packet->deletePacket();
}
void CommandingServiceBase::acceptPacket(uint8_t reportId,
TcPacketStored* packet) {
verificationReporter.sendSuccessReport(reportId, packet);
packet->deletePacket();
}
2020-09-22 14:00:18 +02:00
void CommandingServiceBase::checkAndExecuteFifo(CommandMapIter& iter) {
store_address_t address;
2020-08-11 16:21:59 +02:00
if (iter->second.fifo.retrieve(&address) != RETURN_OK) {
2020-06-24 16:01:17 +02:00
commandMap.erase(&iter);
} else {
TcPacketStored newPacket(address);
startExecution(&newPacket, iter);
}
}
2020-06-24 16:01:17 +02:00
void CommandingServiceBase::handleUnrequestedReply(CommandMessage* reply) {
2020-06-24 16:24:16 +02:00
reply->clearCommandMessage();
}
inline void CommandingServiceBase::doPeriodicOperation() {
}
MessageQueueId_t CommandingServiceBase::getCommandQueue() {
return commandQueue->getId();
}
void CommandingServiceBase::checkTimeout() {
uint32_t uptime;
Clock::getUptime(&uptime);
2020-06-24 16:01:17 +02:00
CommandMapIter iter;
for (iter = commandMap.begin(); iter != commandMap.end(); ++iter) {
2020-08-11 16:21:59 +02:00
if ((iter->second.uptimeOfStart + (timeoutSeconds * 1000)) < uptime) {
verificationReporter.sendFailureReport(
2020-12-14 21:30:39 +01:00
tc_verification::COMPLETION_FAILURE, iter->second.tcInfo.ackFlags,
2020-08-11 16:21:59 +02:00
iter->second.tcInfo.tcPacketId, iter->second.tcInfo.tcSequenceControl,
TIMEOUT);
2020-06-24 16:01:17 +02:00
checkAndExecuteFifo(iter);
}
}
}
2020-06-24 16:01:17 +02:00
void CommandingServiceBase::setTaskIF(PeriodicTaskIF* task_) {
executingTask = task_;
}