2020-08-13 20:53:35 +02:00
|
|
|
#include "../../serviceinterface/ServiceInterfaceStream.h"
|
2020-11-17 19:25:57 +01:00
|
|
|
#include "../../objectmanager/ObjectManagerIF.h"
|
2018-07-12 16:29:32 +02:00
|
|
|
#include "MessageQueue.h"
|
|
|
|
#include "RtemsBasic.h"
|
2018-07-13 18:28:26 +02:00
|
|
|
#include <cstring>
|
2018-07-12 16:29:32 +02:00
|
|
|
MessageQueue::MessageQueue(size_t message_depth, size_t max_message_size) :
|
2020-11-17 19:35:37 +01:00
|
|
|
id(0), lastPartner(0), defaultDestination(NO_QUEUE), internalErrorReporter(nullptr) {
|
2018-07-12 16:29:32 +02:00
|
|
|
rtems_name name = ('Q' << 24) + (queueCounter++ << 8);
|
|
|
|
rtems_status_code status = rtems_message_queue_create(name, message_depth,
|
|
|
|
max_message_size, 0, &(this->id));
|
|
|
|
if (status != RTEMS_SUCCESSFUL) {
|
2021-01-03 14:16:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2020-11-17 19:25:57 +01:00
|
|
|
sif::error << "MessageQueue::MessageQueue: Creating Queue " << std::hex
|
2018-07-12 16:29:32 +02:00
|
|
|
<< name << std::dec << " failed with status:"
|
|
|
|
<< (uint32_t) status << std::endl;
|
2021-01-03 13:58:18 +01:00
|
|
|
#endif
|
2018-07-12 16:29:32 +02:00
|
|
|
this->id = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageQueue::~MessageQueue() {
|
|
|
|
rtems_message_queue_delete(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t MessageQueue::sendMessage(MessageQueueId_t sendTo,
|
2020-11-17 19:25:57 +01:00
|
|
|
MessageQueueMessageIF* message, bool ignoreFault) {
|
2018-07-13 18:28:26 +02:00
|
|
|
return sendMessageFrom(sendTo, message, this->getId(), ignoreFault);
|
2018-07-12 16:29:32 +02:00
|
|
|
}
|
|
|
|
|
2020-11-17 19:25:57 +01:00
|
|
|
ReturnValue_t MessageQueue::sendToDefault(MessageQueueMessageIF* message) {
|
2018-07-13 18:28:26 +02:00
|
|
|
return sendToDefaultFrom(message, this->getId());
|
2018-07-12 16:29:32 +02:00
|
|
|
}
|
|
|
|
|
2020-11-17 19:25:57 +01:00
|
|
|
ReturnValue_t MessageQueue::reply(MessageQueueMessageIF* message) {
|
2018-07-12 16:29:32 +02:00
|
|
|
if (this->lastPartner != 0) {
|
|
|
|
return sendMessage(this->lastPartner, message, this->getId());
|
|
|
|
} else {
|
2019-08-28 14:50:24 +02:00
|
|
|
return NO_REPLY_PARTNER;
|
2018-07-12 16:29:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-17 19:25:57 +01:00
|
|
|
ReturnValue_t MessageQueue::receiveMessage(MessageQueueMessageIF* message,
|
2018-07-12 16:29:32 +02:00
|
|
|
MessageQueueId_t* receivedFrom) {
|
|
|
|
ReturnValue_t status = this->receiveMessage(message);
|
|
|
|
*receivedFrom = this->lastPartner;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2020-11-17 19:25:57 +01:00
|
|
|
ReturnValue_t MessageQueue::receiveMessage(MessageQueueMessageIF* message) {
|
|
|
|
size_t size = 0;
|
2018-07-12 16:29:32 +02:00
|
|
|
rtems_status_code status = rtems_message_queue_receive(id,
|
2020-11-17 19:25:57 +01:00
|
|
|
message->getBuffer(),&size,
|
2018-07-12 16:29:32 +02:00
|
|
|
RTEMS_NO_WAIT, 1);
|
|
|
|
if (status == RTEMS_SUCCESSFUL) {
|
2020-11-17 19:25:57 +01:00
|
|
|
message->setMessageSize(size);
|
2018-07-12 16:29:32 +02:00
|
|
|
this->lastPartner = message->getSender();
|
|
|
|
//Check size of incoming message.
|
2020-11-17 19:25:57 +01:00
|
|
|
if (message->getMessageSize() < message->getMinimumMessageSize()) {
|
2018-07-12 16:29:32 +02:00
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//No message was received. Keep lastPartner anyway, I might send something later.
|
|
|
|
//But still, delete packet content.
|
2020-11-17 19:25:57 +01:00
|
|
|
memset(message->getData(), 0, message->getMaximumMessageSize());
|
2018-07-12 16:29:32 +02:00
|
|
|
}
|
2018-07-13 18:28:26 +02:00
|
|
|
return convertReturnCode(status);
|
2018-07-12 16:29:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
MessageQueueId_t MessageQueue::getLastPartner() const {
|
|
|
|
return this->lastPartner;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t MessageQueue::flush(uint32_t* count) {
|
|
|
|
rtems_status_code status = rtems_message_queue_flush(id, count);
|
2018-07-13 18:28:26 +02:00
|
|
|
return convertReturnCode(status);
|
2018-07-12 16:29:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
MessageQueueId_t MessageQueue::getId() const {
|
|
|
|
return this->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageQueue::setDefaultDestination(MessageQueueId_t defaultDestination) {
|
|
|
|
this->defaultDestination = defaultDestination;
|
|
|
|
}
|
|
|
|
|
2018-07-13 18:28:26 +02:00
|
|
|
ReturnValue_t MessageQueue::sendMessageFrom(MessageQueueId_t sendTo,
|
2020-11-17 19:25:57 +01:00
|
|
|
MessageQueueMessageIF* message, MessageQueueId_t sentFrom,
|
2018-07-12 16:29:32 +02:00
|
|
|
bool ignoreFault) {
|
|
|
|
|
|
|
|
message->setSender(sentFrom);
|
|
|
|
rtems_status_code result = rtems_message_queue_send(sendTo,
|
2020-11-17 19:25:57 +01:00
|
|
|
message->getBuffer(), message->getMessageSize());
|
2018-07-12 16:29:32 +02:00
|
|
|
|
|
|
|
//TODO: Check if we're in ISR.
|
|
|
|
if (result != RTEMS_SUCCESSFUL && !ignoreFault) {
|
2020-11-17 19:35:37 +01:00
|
|
|
if (internalErrorReporter == nullptr) {
|
2018-07-12 16:29:32 +02:00
|
|
|
internalErrorReporter = objectManager->get<InternalErrorReporterIF>(
|
|
|
|
objects::INTERNAL_ERROR_REPORTER);
|
|
|
|
}
|
2020-11-17 19:35:37 +01:00
|
|
|
if (internalErrorReporter != nullptr) {
|
2018-07-12 16:29:32 +02:00
|
|
|
internalErrorReporter->queueMessageNotSent();
|
|
|
|
}
|
|
|
|
}
|
2018-07-13 18:28:26 +02:00
|
|
|
|
|
|
|
ReturnValue_t returnCode = convertReturnCode(result);
|
|
|
|
if(result == MessageQueueIF::EMPTY){
|
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return returnCode;
|
2018-07-12 16:29:32 +02:00
|
|
|
}
|
|
|
|
|
2020-11-17 19:25:57 +01:00
|
|
|
ReturnValue_t MessageQueue::sendToDefaultFrom(MessageQueueMessageIF* message,
|
2018-07-12 16:29:32 +02:00
|
|
|
MessageQueueId_t sentFrom, bool ignoreFault) {
|
2018-07-13 18:28:26 +02:00
|
|
|
return sendMessageFrom(defaultDestination, message, sentFrom, ignoreFault);
|
2018-07-12 16:29:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
MessageQueueId_t MessageQueue::getDefaultDestination() const {
|
|
|
|
return this->defaultDestination;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MessageQueue::isDefaultDestinationSet() const {
|
|
|
|
return (defaultDestination != NO_QUEUE);
|
|
|
|
}
|
|
|
|
|
2018-07-13 18:28:26 +02:00
|
|
|
ReturnValue_t MessageQueue::convertReturnCode(rtems_status_code inValue){
|
|
|
|
switch(inValue){
|
|
|
|
case RTEMS_SUCCESSFUL:
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
case RTEMS_INVALID_ID:
|
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
case RTEMS_TIMEOUT:
|
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
case RTEMS_OBJECT_WAS_DELETED:
|
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
case RTEMS_INVALID_ADDRESS:
|
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
case RTEMS_INVALID_SIZE:
|
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
case RTEMS_TOO_MANY:
|
|
|
|
return MessageQueueIF::FULL;
|
|
|
|
case RTEMS_UNSATISFIED:
|
|
|
|
return MessageQueueIF::EMPTY;
|
|
|
|
default:
|
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-07-12 16:29:32 +02:00
|
|
|
uint16_t MessageQueue::queueCounter = 0;
|