2020-08-25 18:09:05 +02:00
|
|
|
#include "MessageQueue.h"
|
2020-09-15 16:42:17 +02:00
|
|
|
#include "../../objectmanager/ObjectManagerIF.h"
|
2020-08-23 20:27:00 +02:00
|
|
|
#include "../../serviceinterface/ServiceInterfaceStream.h"
|
2018-07-13 15:56:37 +02:00
|
|
|
|
2020-05-18 17:22:10 +02:00
|
|
|
// TODO I guess we should have a way of checking if we are in an ISR and then
|
|
|
|
// use the "fromISR" versions of all calls
|
|
|
|
// As a first step towards this, introduces system context variable which needs
|
|
|
|
// to be switched manually
|
|
|
|
// Haven't found function to find system context.
|
2020-06-22 15:34:35 +02:00
|
|
|
MessageQueue::MessageQueue(size_t messageDepth, size_t maxMessageSize):
|
|
|
|
maxMessageSize(maxMessageSize) {
|
2020-05-18 17:22:10 +02:00
|
|
|
handle = xQueueCreate(messageDepth, maxMessageSize);
|
2020-08-25 18:09:05 +02:00
|
|
|
if (handle == nullptr) {
|
|
|
|
sif::error << "MessageQueue::MessageQueue Creation failed" << std::endl;
|
2018-07-13 15:56:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageQueue::~MessageQueue() {
|
2020-08-25 18:09:05 +02:00
|
|
|
if (handle != nullptr) {
|
2018-07-13 15:56:37 +02:00
|
|
|
vQueueDelete(handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 17:22:10 +02:00
|
|
|
void MessageQueue::switchSystemContext(CallContext callContext) {
|
|
|
|
this->callContext = callContext;
|
|
|
|
}
|
|
|
|
|
2018-07-13 15:56:37 +02:00
|
|
|
ReturnValue_t MessageQueue::sendMessage(MessageQueueId_t sendTo,
|
2020-06-09 02:18:39 +02:00
|
|
|
MessageQueueMessageIF* message, bool ignoreFault) {
|
2018-07-13 18:28:26 +02:00
|
|
|
return sendMessageFrom(sendTo, message, this->getId(), ignoreFault);
|
2018-07-13 15:56:37 +02:00
|
|
|
}
|
|
|
|
|
2020-06-09 02:18:39 +02:00
|
|
|
ReturnValue_t MessageQueue::sendToDefault(MessageQueueMessageIF* message) {
|
2018-07-13 18:28:26 +02:00
|
|
|
return sendToDefaultFrom(message, this->getId());
|
2018-07-13 15:56:37 +02:00
|
|
|
}
|
|
|
|
|
2020-06-09 02:18:39 +02:00
|
|
|
ReturnValue_t MessageQueue::sendToDefaultFrom(MessageQueueMessageIF* message,
|
2020-05-18 17:22:10 +02:00
|
|
|
MessageQueueId_t sentFrom, bool ignoreFault) {
|
|
|
|
return sendMessageFrom(defaultDestination,message,sentFrom,ignoreFault);
|
|
|
|
}
|
|
|
|
|
2020-06-09 02:18:39 +02:00
|
|
|
ReturnValue_t MessageQueue::reply(MessageQueueMessageIF* message) {
|
2020-09-15 16:42:17 +02:00
|
|
|
if (this->lastPartner != MessageQueueIF::NO_QUEUE) {
|
2018-07-13 18:28:26 +02:00
|
|
|
return sendMessageFrom(this->lastPartner, message, this->getId());
|
2018-07-13 15:56:37 +02:00
|
|
|
} else {
|
2019-08-28 14:50:24 +02:00
|
|
|
return NO_REPLY_PARTNER;
|
2018-07-13 15:56:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 17:22:10 +02:00
|
|
|
ReturnValue_t MessageQueue::sendMessageFrom(MessageQueueId_t sendTo,
|
2020-06-09 02:18:39 +02:00
|
|
|
MessageQueueMessageIF* message, MessageQueueId_t sentFrom,
|
2020-05-18 17:22:10 +02:00
|
|
|
bool ignoreFault) {
|
2020-06-23 21:03:22 +02:00
|
|
|
return sendMessageFromMessageQueue(sendTo, message, sentFrom, ignoreFault,
|
|
|
|
callContext);
|
2020-05-18 17:22:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ReturnValue_t MessageQueue::handleSendResult(BaseType_t result, bool ignoreFault) {
|
|
|
|
if (result != pdPASS) {
|
2020-05-18 17:38:19 +02:00
|
|
|
if (not ignoreFault) {
|
2020-09-15 16:42:17 +02:00
|
|
|
InternalErrorReporterIF* internalErrorReporter = objectManager->
|
|
|
|
get<InternalErrorReporterIF>(
|
2020-05-18 17:22:10 +02:00
|
|
|
objects::INTERNAL_ERROR_REPORTER);
|
2020-05-18 17:38:19 +02:00
|
|
|
if (internalErrorReporter != nullptr) {
|
2020-05-18 17:22:10 +02:00
|
|
|
internalErrorReporter->queueMessageNotSent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return MessageQueueIF::FULL;
|
|
|
|
}
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
}
|
|
|
|
|
2020-06-09 02:18:39 +02:00
|
|
|
ReturnValue_t MessageQueue::receiveMessage(MessageQueueMessageIF* message,
|
2018-07-13 15:56:37 +02:00
|
|
|
MessageQueueId_t* receivedFrom) {
|
|
|
|
ReturnValue_t status = this->receiveMessage(message);
|
2020-02-15 15:37:00 +01:00
|
|
|
if(status == HasReturnvaluesIF::RETURN_OK) {
|
|
|
|
*receivedFrom = this->lastPartner;
|
|
|
|
}
|
2018-07-13 15:56:37 +02:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2020-06-09 02:18:39 +02:00
|
|
|
ReturnValue_t MessageQueue::receiveMessage(MessageQueueMessageIF* message) {
|
2020-05-18 17:31:05 +02:00
|
|
|
BaseType_t result = xQueueReceive(handle,reinterpret_cast<void*>(
|
|
|
|
message->getBuffer()), 0);
|
2018-07-13 15:56:37 +02:00
|
|
|
if (result == pdPASS){
|
2019-08-28 14:50:24 +02:00
|
|
|
this->lastPartner = message->getSender();
|
2018-07-13 15:56:37 +02:00
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
} else {
|
2018-07-13 18:28:26 +02:00
|
|
|
return MessageQueueIF::EMPTY;
|
2018-07-13 15:56:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageQueueId_t MessageQueue::getLastPartner() const {
|
|
|
|
return lastPartner;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t MessageQueue::flush(uint32_t* count) {
|
|
|
|
//TODO FreeRTOS does not support flushing partially
|
|
|
|
//Is always successful
|
|
|
|
xQueueReset(handle);
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageQueueId_t MessageQueue::getId() const {
|
2020-06-13 17:37:48 +02:00
|
|
|
return reinterpret_cast<MessageQueueId_t>(handle);
|
2018-07-13 15:56:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MessageQueue::setDefaultDestination(MessageQueueId_t defaultDestination) {
|
2020-05-18 17:38:19 +02:00
|
|
|
defaultDestinationSet = true;
|
2018-07-13 15:56:37 +02:00
|
|
|
this->defaultDestination = defaultDestination;
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageQueueId_t MessageQueue::getDefaultDestination() const {
|
|
|
|
return defaultDestination;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MessageQueue::isDefaultDestinationSet() const {
|
2020-05-18 17:38:19 +02:00
|
|
|
return defaultDestinationSet;
|
2018-07-13 15:56:37 +02:00
|
|
|
}
|
2020-02-15 15:37:00 +01:00
|
|
|
|
2019-08-28 14:50:24 +02:00
|
|
|
|
2020-05-18 17:22:10 +02:00
|
|
|
// static core function to send messages.
|
|
|
|
ReturnValue_t MessageQueue::sendMessageFromMessageQueue(MessageQueueId_t sendTo,
|
2020-06-23 21:03:22 +02:00
|
|
|
MessageQueueMessageIF* message, MessageQueueId_t sentFrom,
|
|
|
|
bool ignoreFault, CallContext callContext) {
|
2020-08-08 13:04:31 +02:00
|
|
|
BaseType_t result = pdFALSE;
|
|
|
|
QueueHandle_t destination = nullptr;
|
|
|
|
|
2020-08-25 18:09:05 +02:00
|
|
|
if(sendTo == MessageQueueIF::NO_QUEUE or sendTo == 0x00) {
|
2020-08-08 13:04:31 +02:00
|
|
|
return MessageQueueIF::DESTINVATION_INVALID;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
destination = reinterpret_cast<QueueHandle_t>(sendTo);
|
|
|
|
}
|
2020-08-10 18:33:28 +02:00
|
|
|
|
2020-05-18 17:22:10 +02:00
|
|
|
message->setSender(sentFrom);
|
2020-08-08 13:04:31 +02:00
|
|
|
|
2020-06-22 15:34:35 +02:00
|
|
|
|
2020-05-29 13:02:13 +02:00
|
|
|
if(callContext == CallContext::TASK) {
|
2020-08-08 13:04:31 +02:00
|
|
|
result = xQueueSendToBack(destination,
|
2020-05-18 17:22:10 +02:00
|
|
|
static_cast<const void*>(message->getBuffer()), 0);
|
|
|
|
}
|
|
|
|
else {
|
2020-05-18 17:38:19 +02:00
|
|
|
/* If the call context is from an interrupt,
|
|
|
|
* request a context switch if a higher priority task
|
|
|
|
* was blocked by the interrupt. */
|
2020-05-18 17:22:10 +02:00
|
|
|
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
|
|
|
result = xQueueSendFromISR(reinterpret_cast<QueueHandle_t>(sendTo),
|
|
|
|
static_cast<const void*>(message->getBuffer()),
|
|
|
|
&xHigherPriorityTaskWoken);
|
|
|
|
if(xHigherPriorityTaskWoken == pdTRUE) {
|
|
|
|
TaskManagement::requestContextSwitch(callContext);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return handleSendResult(result, ignoreFault);
|
2019-08-28 14:50:24 +02:00
|
|
|
}
|