2020-08-13 20:53:35 +02:00
|
|
|
#include "ActionHelper.h"
|
|
|
|
#include "HasActionsIF.h"
|
2020-09-15 16:58:38 +02:00
|
|
|
#include "../ipc/MessageQueueSenderIF.h"
|
2020-08-13 20:53:35 +02:00
|
|
|
#include "../objectmanager/ObjectManagerIF.h"
|
2020-07-16 12:49:53 +02:00
|
|
|
|
2020-10-10 17:04:43 +02:00
|
|
|
ActionHelper::ActionHelper(HasActionsIF* setOwner,
|
|
|
|
MessageQueueIF* useThisQueue) :
|
|
|
|
owner(setOwner), queueToUse(useThisQueue) {
|
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;
|
|
|
|
}
|
|
|
|
|
2020-10-10 17:04:43 +02:00
|
|
|
void ActionHelper::step(uint8_t step, MessageQueueId_t reportTo,
|
|
|
|
ActionId_t commandId, ReturnValue_t result) {
|
2016-06-15 23:48:41 +02:00
|
|
|
CommandMessage reply;
|
|
|
|
ActionMessage::setStepReply(&reply, commandId, step + STEP_OFFSET, result);
|
|
|
|
queueToUse->sendMessage(reportTo, &reply);
|
|
|
|
}
|
|
|
|
|
2020-10-10 17:04:43 +02:00
|
|
|
void ActionHelper::finish(MessageQueueId_t reportTo, ActionId_t commandId,
|
|
|
|
ReturnValue_t result) {
|
2016-06-15 23:48:41 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-10-10 17:04:43 +02:00
|
|
|
void ActionHelper::prepareExecution(MessageQueueId_t commandedBy,
|
|
|
|
ActionId_t actionId, store_address_t dataAddress) {
|
2016-06-15 23:48:41 +02:00
|
|
|
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-10-12 16:58:04 +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
|
|
|
}
|
2020-10-12 16:58:04 +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
|
|
|
|
2020-10-29 13:08:09 +01:00
|
|
|
// If the sender needs to be hidden, for example to handle packet
|
|
|
|
// as unrequested reply, this will be done here.
|
2020-10-12 16:58:04 +02:00
|
|
|
if (hideSender) {
|
2018-07-12 16:29:32 +02:00
|
|
|
result = MessageQueueSenderIF::sendMessage(reportTo, &reply);
|
2020-10-12 16:58:04 +02:00
|
|
|
}
|
|
|
|
else {
|
2018-07-12 16:29:32 +02:00
|
|
|
result = queueToUse->sendMessage(reportTo, &reply);
|
|
|
|
}
|
2020-10-12 16:58:04 +02:00
|
|
|
|
|
|
|
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() {
|
|
|
|
}
|
2020-10-12 16:58:04 +02:00
|
|
|
|
|
|
|
ReturnValue_t ActionHelper::reportData(MessageQueueId_t reportTo,
|
|
|
|
ActionId_t replyId, const uint8_t *data, size_t dataSize,
|
|
|
|
bool hideSender) {
|
|
|
|
CommandMessage reply;
|
|
|
|
store_address_t storeAddress;
|
|
|
|
ReturnValue_t result = ipcStore->addData(&storeAddress, data, dataSize);
|
|
|
|
if (result != HasReturnvaluesIF::RETURN_OK) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result != HasReturnvaluesIF::RETURN_OK) {
|
|
|
|
ipcStore->deleteData(storeAddress);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
ActionMessage::setDataReply(&reply, replyId, storeAddress);
|
|
|
|
|
2020-10-29 13:07:08 +01:00
|
|
|
// If the sender needs to be hidden, for example to handle packet
|
|
|
|
// as unrequested reply, this will be done here.
|
2020-10-12 16:58:04 +02:00
|
|
|
if (hideSender) {
|
|
|
|
result = MessageQueueSenderIF::sendMessage(reportTo, &reply);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = queueToUse->sendMessage(reportTo, &reply);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result != HasReturnvaluesIF::RETURN_OK){
|
|
|
|
ipcStore->deleteData(storeAddress);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|