MessageQueue refactoring complete

This commit is contained in:
2020-06-09 02:18:39 +02:00
parent b1f91439c6
commit 8ff6506ad9
34 changed files with 293 additions and 191 deletions

View File

@ -14,14 +14,26 @@ void clearMissionMessage(CommandMessage* message);
}
CommandMessage::CommandMessage() {
this->messageSize = COMMAND_MESSAGE_SIZE;
CommandMessage::CommandMessage(MessageQueueMessage* receiverMessage):
internalMessage(receiverMessage) {
if(receiverMessage == nullptr) {
sif::error << "CommandMessage::CommandMessage: Don't pass a nullptr"
" as the message queue message, pass the address of an actual"
" message!" << std::endl;
}
internalMessage->messageSize = COMMAND_MESSAGE_SIZE;
setCommand(CMD_NONE);
}
CommandMessage::CommandMessage(Command_t command, uint32_t parameter1,
uint32_t parameter2) {
this->messageSize = COMMAND_MESSAGE_SIZE;
CommandMessage::CommandMessage(MessageQueueMessage* messageToSet,
Command_t command, uint32_t parameter1, uint32_t parameter2):
internalMessage(messageToSet) {
if(messageToSet == nullptr) {
sif::error << "CommandMessage::CommandMessage: Don't pass a nullptr"
" as the message queue message, pass the address of an actual"
" message!" << std::endl;
}
internalMessage->messageSize = COMMAND_MESSAGE_SIZE;
setCommand(command);
setParameter(parameter1);
setParameter2(parameter2);
@ -29,7 +41,7 @@ CommandMessage::CommandMessage(Command_t command, uint32_t parameter1,
Command_t CommandMessage::getCommand() const {
Command_t command;
memcpy(&command, getData(), sizeof(Command_t));
memcpy(&command, internalMessage->getData(), sizeof(Command_t));
return command;
}
@ -38,29 +50,35 @@ uint8_t CommandMessage::getMessageType() const {
}
void CommandMessage::setCommand(Command_t command) {
memcpy(getData(), &command, sizeof(command));
memcpy(internalMessage->getData(), &command, sizeof(command));
}
uint32_t CommandMessage::getParameter() const {
uint32_t parameter1;
memcpy(&parameter1, getData() + sizeof(Command_t), sizeof(parameter1));
memcpy(&parameter1, internalMessage->getData() + sizeof(Command_t),
sizeof(parameter1));
return parameter1;
}
void CommandMessage::setParameter(uint32_t parameter1) {
memcpy(getData() + sizeof(Command_t), &parameter1, sizeof(parameter1));
memcpy(internalMessage->getData() + sizeof(Command_t),
&parameter1, sizeof(parameter1));
}
uint32_t CommandMessage::getParameter2() const {
uint32_t parameter2;
memcpy(&parameter2, getData() + sizeof(Command_t) + sizeof(uint32_t),
sizeof(parameter2));
memcpy(&parameter2, internalMessage->getData() + sizeof(Command_t)
+ sizeof(uint32_t), sizeof(parameter2));
return parameter2;
}
void CommandMessage::setParameter2(uint32_t parameter2) {
memcpy(getData() + sizeof(Command_t) + sizeof(uint32_t), &parameter2,
sizeof(parameter2));
memcpy(internalMessage-> getData() + sizeof(Command_t) + sizeof(uint32_t),
&parameter2, sizeof(parameter2));
}
void CommandMessage::clear() {
clearCommandMessage();
}
void CommandMessage::clearCommandMessage() {
@ -109,7 +127,7 @@ size_t CommandMessage::getMinimumMessageSize() const {
void CommandMessage::setToUnknownCommand() {
Command_t initialCommand = getCommand();
clearCommandMessage();
setReplyRejected(UNKNOW_COMMAND, initialCommand);
setReplyRejected(UNKNOWN_COMMAND, initialCommand);
}
void CommandMessage::setReplyRejected(ReturnValue_t reason,
@ -118,3 +136,19 @@ void CommandMessage::setReplyRejected(ReturnValue_t reason,
setParameter(reason);
setParameter2(initialCommand);
}
MessageQueueId_t CommandMessage::getSender() const {
return internalMessage->getSender();
}
uint8_t* CommandMessage::getBuffer() {
return internalMessage->getBuffer();
}
void CommandMessage::setSender(MessageQueueId_t setId) {
internalMessage->setSender(setId);
}
const uint8_t* CommandMessage::getBuffer() const {
return internalMessage->getBuffer();
}

View File

@ -13,10 +13,10 @@ typedef uint16_t Command_t;
* @brief Used to pass command messages between tasks
* @author Bastian Baetz
*/
class CommandMessage : public MessageQueueMessage {
class CommandMessage: public MessageQueueMessageIF {
public:
static const uint8_t INTERFACE_ID = CLASS_ID::COMMAND_MESSAGE;
static const ReturnValue_t UNKNOW_COMMAND = MAKE_RETURN_CODE(0x01);
static const ReturnValue_t UNKNOWN_COMMAND = MAKE_RETURN_CODE(0x01);
static const uint8_t MESSAGE_ID = messagetypes::COMMAND;
@ -31,7 +31,7 @@ public:
* This is the size of a message as it is seen by the MessageQueue.
* 14 of the 24 available MessageQueueMessage bytes are used.
*/
static const size_t COMMAND_MESSAGE_SIZE = HEADER_SIZE
static const size_t COMMAND_MESSAGE_SIZE = MessageQueueMessage::HEADER_SIZE
+ sizeof(Command_t) + 2 * sizeof(uint32_t);
/**
@ -40,7 +40,7 @@ public:
* This constructor should be used when receiving a Message, as the
* content is filled by the MessageQueue.
*/
CommandMessage();
CommandMessage(MessageQueueMessage* receiverMessage);
/**
* This constructor creates a new message with all message content
* initialized
@ -49,7 +49,7 @@ public:
* @param parameter1 The first parameter
* @param parameter2 The second parameter
*/
CommandMessage(Command_t command,
CommandMessage(MessageQueueMessage* messageToSet, Command_t command,
uint32_t parameter1, uint32_t parameter2);
/**
@ -58,6 +58,8 @@ public:
virtual ~CommandMessage() {
}
uint8_t * getBuffer() override;
const uint8_t * getBuffer() const override;
/**
* Read the DeviceHandlerCommand_t that is stored in the message,
* usually used after receiving.
@ -66,6 +68,16 @@ public:
*/
Command_t getCommand() const;
/**
* @brief This method is used to set the sender's message queue id
* information prior to sending the message.
* @param setId
* The message queue id that identifies the sending message queue.
*/
void setSender(MessageQueueId_t setId) override;
MessageQueueId_t getSender() const override;
uint8_t getMessageType() const;
/**
* Set the DeviceHandlerCOmmand_t of the message
@ -102,6 +114,7 @@ public:
*/
void setParameter2(uint32_t parameter2);
void clear() override;
/**
* Set the command to CMD_NONE and try to find
* the correct class to handle a more detailed
@ -129,7 +142,10 @@ public:
void setToUnknownCommand();
void setReplyRejected(ReturnValue_t reason,
Command_t initialCommand = CMD_NONE);
size_t getMinimumMessageSize() const;
size_t getMinimumMessageSize() const override;
private:
MessageQueueMessage* internalMessage;
};

View File

@ -15,7 +15,7 @@
#include <framework/returnvalues/HasReturnvaluesIF.h>
class MessageQueueIF {
public:
static const MessageQueueId_t NO_QUEUE = MessageQueueSenderIF::NO_QUEUE; //!< Ugly hack.
static const MessageQueueId_t NO_QUEUE = MessageQueueMessageIF::NO_QUEUE; //!< Ugly hack.
static const uint8_t INTERFACE_ID = CLASS_ID::MESSAGE_QUEUE_IF;
/**
@ -41,7 +41,7 @@ public:
* @return RETURN_OK if ok
* @return NO_REPLY_PARTNER Should return NO_REPLY_PARTNER if partner was found
*/
virtual ReturnValue_t reply( MessageQueueMessage* message ) = 0;
virtual ReturnValue_t reply( MessageQueueMessageIF* message ) = 0;
/**
* @brief This function reads available messages from the message queue and returns the sender.
@ -50,7 +50,7 @@ public:
* @param message A pointer to a message in which the received data is stored.
* @param receivedFrom A pointer to a queue id in which the sender's id is stored.
*/
virtual ReturnValue_t receiveMessage(MessageQueueMessage* message,
virtual ReturnValue_t receiveMessage(MessageQueueMessageIF* message,
MessageQueueId_t *receivedFrom) = 0;
/**
@ -65,7 +65,7 @@ public:
* @return -@c RETURN_OK on success
* -@c MessageQueueIF::EMPTY if queue is empty
*/
virtual ReturnValue_t receiveMessage(MessageQueueMessage* message) = 0;
virtual ReturnValue_t receiveMessage(MessageQueueMessageIF* message) = 0;
/**
* Deletes all pending messages in the queue.
* @param count The number of flushed messages.
@ -95,7 +95,7 @@ public:
* -@c MessageQueueIF::FULL if queue is full
*/
virtual ReturnValue_t sendMessageFrom( MessageQueueId_t sendTo,
MessageQueueMessage* message, MessageQueueId_t sentFrom,
MessageQueueMessageIF* message, MessageQueueId_t sentFrom,
bool ignoreFault = false ) = 0;
/**
@ -107,7 +107,7 @@ public:
* @param ignoreFault If set to true, the internal software fault counter is not incremented if queue is full.
*/
virtual ReturnValue_t sendMessage( MessageQueueId_t sendTo,
MessageQueueMessage* message, bool ignoreFault = false ) = 0;
MessageQueueMessageIF* message, bool ignoreFault = false ) = 0;
/**
* @brief The sendToDefaultFrom method sends a queue message to the default destination.
@ -118,7 +118,7 @@ public:
* @return -@c RETURN_OK on success
* -@c MessageQueueIF::FULL if queue is full
*/
virtual ReturnValue_t sendToDefaultFrom( MessageQueueMessage* message,
virtual ReturnValue_t sendToDefaultFrom( MessageQueueMessageIF* message,
MessageQueueId_t sentFrom, bool ignoreFault = false ) = 0;
/**
* @brief This operation sends a message to the default destination.
@ -128,7 +128,7 @@ public:
* @return -@c RETURN_OK on success
* -@c MessageQueueIF::FULL if queue is full
*/
virtual ReturnValue_t sendToDefault( MessageQueueMessage* message ) = 0;
virtual ReturnValue_t sendToDefault( MessageQueueMessageIF* message ) = 0;
/**
* \brief This method is a simple setter for the default destination.
*/

View File

@ -51,7 +51,7 @@ void MessageQueueMessage::setSender(MessageQueueId_t setId) {
memcpy(this->internalBuffer, &setId, sizeof(MessageQueueId_t));
}
size_t MessageQueueMessage::getMinimumMessageSize() {
size_t MessageQueueMessage::getMinimumMessageSize() const {
return this->HEADER_SIZE;
}

View File

@ -132,7 +132,7 @@ public:
* @details The method must be overwritten by child classes if size checks shall be more strict.
* @return The default implementation returns HEADER_SIZE.
*/
virtual size_t getMinimumMessageSize();
virtual size_t getMinimumMessageSize() const;
};
#endif /* MESSAGEQUEUEMESSAGE_H_ */

View File

@ -1,10 +1,23 @@
#ifndef FRAMEWORK_IPC_MESSAGEQUEUEMESSAGEIF_H_
#define FRAMEWORK_IPC_MESSAGEQUEUEMESSAGEIF_H_
#include <framework/ipc/MessageQueueSenderIF.h>
#include <cstddef>
#include <cstdint>
/*
* TODO: Actually, the definition of this ID to be a uint32_t is not ideal and
* breaks layering. However, it is difficult to keep layering, as the ID is
* stored in many places and sent around in MessageQueueMessage.
* Ideally, one would use the (current) object_id_t only, however, doing a
* lookup of queueIDs for every call does not sound ideal.
* In a first step, I'll circumvent the issue by not touching it,
* maybe in a second step. This also influences Interface design
* (getCommandQueue) and some other issues.. */
typedef uint32_t MessageQueueId_t;
class MessageQueueMessageIF {
public:
static const MessageQueueId_t NO_QUEUE = 0xffffffff;
virtual ~MessageQueueMessageIF() {};
/**
@ -12,11 +25,11 @@ public:
* size is set to zero.
*/
virtual void clear() = 0;
/**
* @brief This is a debug method that prints the content
* (till messageSize) to the debug output.
*/
virtual void print() = 0;
// /**
// * @brief This is a debug method that prints the content
// * (till messageSize) to the debug output.
// */
// virtual void print() = 0;
/**
* @brief Get read-only pointer to the raw buffer.
@ -37,11 +50,13 @@ public:
*/
virtual void setSender(MessageQueueId_t setId) = 0;
virtual MessageQueueId_t getSender() const = 0;
/**
* @brief This helper function is used by the MessageQueue class to
* check the size of an incoming message.
*/
virtual size_t getMinimumMessageSize() = 0;
virtual size_t getMinimumMessageSize() const = 0;
};

View File

@ -1,22 +1,11 @@
#ifndef FRAMEWORK_IPC_MESSAGEQUEUESENDERIF_H_
#define FRAMEWORK_IPC_MESSAGEQUEUESENDERIF_H_
#include <framework/ipc/MessageQueueMessageIF.h>
#include <framework/objectmanager/ObjectManagerIF.h>
class MessageQueueMessage;
//TODO: Actually, the definition of this ID to be a uint32_t is not ideal and breaks layering.
//However, it is difficult to keep layering, as the ID is stored in many places and sent around in
//MessageQueueMessage.
//Ideally, one would use the (current) object_id_t only, however, doing a lookup of queueIDs for every
//call does not sound ideal.
//In a first step, I'll circumvent the issue by not touching it, maybe in a second step.
//This also influences Interface design (getCommandQueue) and some other issues..
typedef uint32_t MessageQueueId_t;
class MessageQueueSenderIF {
public:
static const MessageQueueId_t NO_QUEUE = 0xffffffff;
virtual ~MessageQueueSenderIF() {}
@ -26,7 +15,8 @@ public:
* Must be implemented by a subclass.
*/
static ReturnValue_t sendMessage(MessageQueueId_t sendTo,
MessageQueueMessage* message, MessageQueueId_t sentFrom = MessageQueueSenderIF::NO_QUEUE,
MessageQueueMessageIF* message,
MessageQueueId_t sentFrom = MessageQueueMessageIF::NO_QUEUE,
bool ignoreFault=false);
private:
MessageQueueSenderIF() {}