2018-07-13 15:56:37 +02:00
|
|
|
#include "MessageQueue.h"
|
|
|
|
|
|
|
|
#include <framework/serviceinterface/ServiceInterfaceStream.h>
|
|
|
|
|
|
|
|
// 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
|
2020-02-25 12:54:28 +01:00
|
|
|
// As a first step towards this, introduces system context variable which needs to be switched manually
|
|
|
|
// Haven't found function to find system context.
|
2018-07-13 15:56:37 +02:00
|
|
|
MessageQueue::MessageQueue(size_t message_depth, size_t max_message_size) :
|
2020-03-02 01:00:17 +01:00
|
|
|
defaultDestination(0),lastPartner(0), callContext(CallContext::task) {
|
2018-07-13 15:56:37 +02:00
|
|
|
handle = xQueueCreate(message_depth, max_message_size);
|
|
|
|
if (handle == NULL) {
|
2020-04-23 19:13:18 +02:00
|
|
|
sif::error << "MessageQueue creation failed" << std::endl;
|
2018-07-13 15:56:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageQueue::~MessageQueue() {
|
|
|
|
if (handle != NULL) {
|
|
|
|
vQueueDelete(handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-02 01:00:17 +01:00
|
|
|
void MessageQueue::switchSystemContext(CallContext callContext) {
|
2020-02-25 12:54:28 +01:00
|
|
|
this->callContext = callContext;
|
|
|
|
}
|
|
|
|
|
2018-07-13 15:56:37 +02:00
|
|
|
ReturnValue_t MessageQueue::sendMessage(MessageQueueId_t sendTo,
|
|
|
|
MessageQueueMessage* 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
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t MessageQueue::sendToDefault(MessageQueueMessage* message) {
|
2018-07-13 18:28:26 +02:00
|
|
|
return sendToDefaultFrom(message, this->getId());
|
2018-07-13 15:56:37 +02:00
|
|
|
}
|
|
|
|
|
2020-02-25 12:54:28 +01:00
|
|
|
ReturnValue_t MessageQueue::sendToDefaultFrom(MessageQueueMessage* message,
|
|
|
|
MessageQueueId_t sentFrom, bool ignoreFault) {
|
|
|
|
return sendMessageFrom(defaultDestination,message,sentFrom,ignoreFault);
|
|
|
|
}
|
|
|
|
|
2018-07-13 15:56:37 +02:00
|
|
|
ReturnValue_t MessageQueue::reply(MessageQueueMessage* message) {
|
|
|
|
if (this->lastPartner != 0) {
|
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-02-25 12:54:28 +01:00
|
|
|
ReturnValue_t MessageQueue::sendMessageFrom(MessageQueueId_t sendTo,
|
|
|
|
MessageQueueMessage* message, MessageQueueId_t sentFrom,
|
|
|
|
bool ignoreFault) {
|
|
|
|
return sendMessageFromMessageQueue(sendTo,message,sentFrom,ignoreFault, callContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ReturnValue_t MessageQueue::handleSendResult(BaseType_t result, bool ignoreFault) {
|
|
|
|
if (result != pdPASS) {
|
|
|
|
if (!ignoreFault) {
|
|
|
|
InternalErrorReporterIF* internalErrorReporter = objectManager->get<InternalErrorReporterIF>(
|
|
|
|
objects::INTERNAL_ERROR_REPORTER);
|
|
|
|
if (internalErrorReporter != NULL) {
|
|
|
|
internalErrorReporter->queueMessageNotSent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return MessageQueueIF::FULL;
|
|
|
|
}
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
}
|
|
|
|
|
2018-07-13 15:56:37 +02:00
|
|
|
ReturnValue_t MessageQueue::receiveMessage(MessageQueueMessage* message,
|
|
|
|
MessageQueueId_t* receivedFrom) {
|
|
|
|
ReturnValue_t status = this->receiveMessage(message);
|
2020-01-12 15:51:59 +01:00
|
|
|
if(status == HasReturnvaluesIF::RETURN_OK) {
|
|
|
|
*receivedFrom = this->lastPartner;
|
|
|
|
}
|
2018-07-13 15:56:37 +02:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t MessageQueue::receiveMessage(MessageQueueMessage* message) {
|
2019-08-28 14:50:24 +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 {
|
|
|
|
return (MessageQueueId_t) handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageQueue::setDefaultDestination(MessageQueueId_t defaultDestination) {
|
|
|
|
this->defaultDestination = defaultDestination;
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageQueueId_t MessageQueue::getDefaultDestination() const {
|
|
|
|
return defaultDestination;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MessageQueue::isDefaultDestinationSet() const {
|
|
|
|
return 0;
|
|
|
|
}
|
2019-12-26 16:44:50 +01:00
|
|
|
|
2019-08-28 14:50:24 +02:00
|
|
|
|
2020-04-18 13:30:22 +02:00
|
|
|
// static core function to send messages.
|
|
|
|
ReturnValue_t MessageQueue::sendMessageFromMessageQueue(MessageQueueId_t sendTo,
|
|
|
|
MessageQueueMessage *message, MessageQueueId_t sentFrom,
|
|
|
|
bool ignoreFault, CallContext callContext) {
|
|
|
|
message->setSender(sentFrom);
|
|
|
|
BaseType_t result;
|
|
|
|
if(callContext == CallContext::task) {
|
2020-05-14 16:12:01 +02:00
|
|
|
result = xQueueSendToBack(reinterpret_cast<QueueHandle_t>(sendTo),
|
2020-05-14 21:26:04 +02:00
|
|
|
static_cast<const void*>(message->getBuffer()), 0);
|
2020-04-18 13:30:22 +02:00
|
|
|
}
|
|
|
|
else {
|
2020-04-18 13:32:40 +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-04-18 13:30:22 +02:00
|
|
|
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
2020-05-14 16:12:01 +02:00
|
|
|
result = xQueueSendFromISR(reinterpret_cast<QueueHandle_t>(sendTo),
|
2020-05-14 21:26:04 +02:00
|
|
|
static_cast<const void*>(message->getBuffer()),
|
2020-04-18 13:30:22 +02:00
|
|
|
&xHigherPriorityTaskWoken);
|
|
|
|
if(xHigherPriorityTaskWoken == pdTRUE) {
|
|
|
|
TaskManagement::requestContextSwitch(callContext);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return handleSendResult(result, ignoreFault);
|
2019-08-28 14:50:24 +02:00
|
|
|
}
|