2020-08-13 20:53:35 +02:00
|
|
|
#include "ActionHelper.h"
|
|
|
|
#include "HasActionsIF.h"
|
|
|
|
#include "../objectmanager/ObjectManagerIF.h"
|
2020-07-16 12:49:53 +02:00
|
|
|
|
2018-07-12 16:29:32 +02:00
|
|
|
ActionHelper::ActionHelper(HasActionsIF* setOwner, MessageQueueIF* useThisQueue) :
|
2020-07-16 12:49:53 +02:00
|
|
|
owner(setOwner), queueToUse(useThisQueue), ipcStore(nullptr) {
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ActionHelper::~ActionHelper() {
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t ActionHelper::handleActionMessage(CommandMessage* command) {
|
|
|
|
if (command->getCommand() == ActionMessage::EXECUTE_ACTION) {
|
|
|
|
ActionId_t currentAction = ActionMessage::getActionId(command);
|
|
|
|
prepareExecution(command->getSender(), currentAction,
|
|
|
|
ActionMessage::getStoreId(command));
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
} else {
|
2020-07-16 12:49:53 +02:00
|
|
|
return CommandMessage::UNKNOWN_COMMAND;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-13 18:28:26 +02:00
|
|
|
ReturnValue_t ActionHelper::initialize(MessageQueueIF* queueToUse_) {
|
2016-06-15 23:48:41 +02:00
|
|
|
ipcStore = objectManager->get<StorageManagerIF>(objects::IPC_STORE);
|
2020-07-16 12:49:53 +02:00
|
|
|
if (ipcStore == nullptr) {
|
2016-06-15 23:48:41 +02:00
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
2020-07-16 12:49:53 +02:00
|
|
|
if(queueToUse_ != nullptr) {
|
|
|
|
setQueueToUse(queueToUse_);
|
|
|
|
}
|
2018-07-13 18:28:26 +02:00
|
|
|
|
2016-06-15 23:48:41 +02:00
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ActionHelper::step(uint8_t step, MessageQueueId_t reportTo, ActionId_t commandId, ReturnValue_t result) {
|
|
|
|
CommandMessage reply;
|
|
|
|
ActionMessage::setStepReply(&reply, commandId, step + STEP_OFFSET, result);
|
|
|
|
queueToUse->sendMessage(reportTo, &reply);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ActionHelper::finish(MessageQueueId_t reportTo, ActionId_t commandId, ReturnValue_t result) {
|
|
|
|
CommandMessage reply;
|
|
|
|
ActionMessage::setCompletionReply(&reply, commandId, result);
|
|
|
|
queueToUse->sendMessage(reportTo, &reply);
|
|
|
|
}
|
|
|
|
|
2018-07-12 16:29:32 +02:00
|
|
|
void ActionHelper::setQueueToUse(MessageQueueIF* queue) {
|
|
|
|
queueToUse = queue;
|
|
|
|
}
|
|
|
|
|
2016-06-15 23:48:41 +02:00
|
|
|
void ActionHelper::prepareExecution(MessageQueueId_t commandedBy, ActionId_t actionId,
|
|
|
|
store_address_t dataAddress) {
|
|
|
|
const uint8_t* dataPtr = NULL;
|
2020-05-04 16:53:04 +02:00
|
|
|
size_t size = 0;
|
2016-06-15 23:48:41 +02:00
|
|
|
ReturnValue_t result = ipcStore->getData(dataAddress, &dataPtr, &size);
|
|
|
|
if (result != HasReturnvaluesIF::RETURN_OK) {
|
|
|
|
CommandMessage reply;
|
|
|
|
ActionMessage::setStepReply(&reply, actionId, 0, result);
|
|
|
|
queueToUse->sendMessage(commandedBy, &reply);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
result = owner->executeAction(actionId, commandedBy, dataPtr, size);
|
|
|
|
ipcStore->deleteData(dataAddress);
|
|
|
|
if (result != HasReturnvaluesIF::RETURN_OK) {
|
|
|
|
CommandMessage reply;
|
|
|
|
ActionMessage::setStepReply(&reply, actionId, 0, result);
|
|
|
|
queueToUse->sendMessage(commandedBy, &reply);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-16 12:49:53 +02:00
|
|
|
ReturnValue_t ActionHelper::reportData(MessageQueueId_t reportTo,
|
|
|
|
ActionId_t replyId, SerializeIF* data, bool hideSender) {
|
2016-06-15 23:48:41 +02:00
|
|
|
CommandMessage reply;
|
|
|
|
store_address_t storeAddress;
|
|
|
|
uint8_t *dataPtr;
|
2020-04-21 22:28:43 +02:00
|
|
|
size_t maxSize = data->getSerializedSize();
|
2016-06-15 23:48:41 +02:00
|
|
|
if (maxSize == 0) {
|
2018-07-12 16:29:32 +02:00
|
|
|
//No error, there's simply nothing to report.
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
2020-04-21 22:28:43 +02:00
|
|
|
size_t size = 0;
|
2016-06-15 23:48:41 +02:00
|
|
|
ReturnValue_t result = ipcStore->getFreeElement(&storeAddress, maxSize,
|
|
|
|
&dataPtr);
|
|
|
|
if (result != HasReturnvaluesIF::RETURN_OK) {
|
2018-07-12 16:29:32 +02:00
|
|
|
return result;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
2020-04-21 22:28:43 +02:00
|
|
|
result = data->serialize(&dataPtr, &size, maxSize, SerializeIF::Endianness::BIG);
|
2016-06-15 23:48:41 +02:00
|
|
|
if (result != HasReturnvaluesIF::RETURN_OK) {
|
|
|
|
ipcStore->deleteData(storeAddress);
|
2018-07-12 16:29:32 +02:00
|
|
|
return result;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
2018-07-12 16:29:32 +02:00
|
|
|
//We don't need to report the objectId, as we receive REQUESTED data before the completion success message.
|
|
|
|
//True aperiodic replies need to be reported with another dedicated message.
|
2016-06-15 23:48:41 +02:00
|
|
|
ActionMessage::setDataReply(&reply, replyId, storeAddress);
|
2018-07-12 16:29:32 +02:00
|
|
|
|
|
|
|
//TODO Service Implementation sucks at the moment
|
|
|
|
if (hideSender){
|
|
|
|
result = MessageQueueSenderIF::sendMessage(reportTo, &reply);
|
|
|
|
} else {
|
|
|
|
result = queueToUse->sendMessage(reportTo, &reply);
|
|
|
|
}
|
|
|
|
if ( result != HasReturnvaluesIF::RETURN_OK){
|
2016-06-15 23:48:41 +02:00
|
|
|
ipcStore->deleteData(storeAddress);
|
|
|
|
}
|
2018-07-12 16:29:32 +02:00
|
|
|
return result;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
2018-07-13 18:28:26 +02:00
|
|
|
|
|
|
|
void ActionHelper::resetHelper() {
|
|
|
|
}
|