2016-06-15 23:48:41 +02:00
|
|
|
#include <framework/action/ActionMessage.h>
|
|
|
|
#include <framework/action/CommandActionHelper.h>
|
|
|
|
#include <framework/action/CommandsActionsIF.h>
|
|
|
|
#include <framework/action/HasActionsIF.h>
|
|
|
|
#include <framework/objectmanager/ObjectManagerIF.h>
|
|
|
|
|
2020-04-21 22:28:43 +02:00
|
|
|
CommandActionHelper::CommandActionHelper(CommandsActionsIF *setOwner) :
|
2018-07-13 18:28:26 +02:00
|
|
|
owner(setOwner), queueToUse(NULL), ipcStore(
|
2020-04-21 22:28:43 +02:00
|
|
|
NULL), commandCount(0), lastTarget(0) {
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CommandActionHelper::~CommandActionHelper() {
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t CommandActionHelper::commandAction(object_id_t commandTo,
|
2020-04-21 22:28:43 +02:00
|
|
|
ActionId_t actionId, SerializeIF *data) {
|
|
|
|
HasActionsIF *receiver = objectManager->get<HasActionsIF>(commandTo);
|
2016-06-15 23:48:41 +02:00
|
|
|
if (receiver == NULL) {
|
|
|
|
return CommandsActionsIF::OBJECT_HAS_NO_FUNCTIONS;
|
|
|
|
}
|
|
|
|
store_address_t storeId;
|
2020-04-21 22:28:43 +02:00
|
|
|
uint8_t *storePointer;
|
|
|
|
size_t maxSize = data->getSerializedSize();
|
2016-06-15 23:48:41 +02:00
|
|
|
ReturnValue_t result = ipcStore->getFreeElement(&storeId, maxSize,
|
|
|
|
&storePointer);
|
|
|
|
if (result != HasReturnvaluesIF::RETURN_OK) {
|
|
|
|
return result;
|
|
|
|
}
|
2020-04-21 22:28:43 +02:00
|
|
|
size_t size = 0;
|
|
|
|
result = data->serialize(&storePointer, &size, maxSize,
|
|
|
|
SerializeIF::Endianness::BIG);
|
2016-06-15 23:48:41 +02:00
|
|
|
if (result != HasReturnvaluesIF::RETURN_OK) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return sendCommand(receiver->getCommandQueue(), actionId, storeId);
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t CommandActionHelper::commandAction(object_id_t commandTo,
|
2020-04-21 22:28:43 +02:00
|
|
|
ActionId_t actionId, const uint8_t *data, uint32_t size) {
|
2016-06-15 23:48:41 +02:00
|
|
|
// if (commandCount != 0) {
|
|
|
|
// return CommandsFunctionsIF::ALREADY_COMMANDING;
|
|
|
|
// }
|
2020-04-21 22:28:43 +02:00
|
|
|
HasActionsIF *receiver = objectManager->get<HasActionsIF>(commandTo);
|
2016-06-15 23:48:41 +02:00
|
|
|
if (receiver == NULL) {
|
|
|
|
return CommandsActionsIF::OBJECT_HAS_NO_FUNCTIONS;
|
|
|
|
}
|
|
|
|
store_address_t storeId;
|
|
|
|
ReturnValue_t result = ipcStore->addData(&storeId, data, size);
|
|
|
|
if (result != HasReturnvaluesIF::RETURN_OK) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return sendCommand(receiver->getCommandQueue(), actionId, storeId);
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t CommandActionHelper::sendCommand(MessageQueueId_t queueId,
|
|
|
|
ActionId_t actionId, store_address_t storeId) {
|
|
|
|
CommandMessage command;
|
|
|
|
ActionMessage::setCommand(&command, actionId, storeId);
|
|
|
|
ReturnValue_t result = queueToUse->sendMessage(queueId, &command);
|
|
|
|
if (result != HasReturnvaluesIF::RETURN_OK) {
|
|
|
|
ipcStore->deleteData(storeId);
|
|
|
|
}
|
|
|
|
lastTarget = queueId;
|
|
|
|
commandCount++;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t CommandActionHelper::initialize() {
|
|
|
|
ipcStore = objectManager->get<StorageManagerIF>(objects::IPC_STORE);
|
2018-07-13 18:28:26 +02:00
|
|
|
if (ipcStore == NULL) {
|
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
queueToUse = owner->getCommandQueuePtr();
|
2020-04-21 22:28:43 +02:00
|
|
|
if (queueToUse == NULL) {
|
2016-06-15 23:48:41 +02:00
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
2018-07-13 18:28:26 +02:00
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
2020-04-21 22:28:43 +02:00
|
|
|
ReturnValue_t CommandActionHelper::handleReply(CommandMessage *reply) {
|
2016-06-15 23:48:41 +02:00
|
|
|
if (reply->getSender() != lastTarget) {
|
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
|
|
|
switch (reply->getCommand()) {
|
|
|
|
case ActionMessage::COMPLETION_SUCCESS:
|
|
|
|
commandCount--;
|
|
|
|
owner->completionSuccessfulReceived(ActionMessage::getActionId(reply));
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
case ActionMessage::COMPLETION_FAILED:
|
|
|
|
commandCount--;
|
2020-04-21 22:28:43 +02:00
|
|
|
owner->completionFailedReceived(ActionMessage::getActionId(reply),
|
|
|
|
ActionMessage::getReturnCode(reply));
|
2016-06-15 23:48:41 +02:00
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
case ActionMessage::STEP_SUCCESS:
|
|
|
|
owner->stepSuccessfulReceived(ActionMessage::getActionId(reply),
|
|
|
|
ActionMessage::getStep(reply));
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
case ActionMessage::STEP_FAILED:
|
|
|
|
commandCount--;
|
2020-04-21 22:28:43 +02:00
|
|
|
owner->stepFailedReceived(ActionMessage::getActionId(reply),
|
|
|
|
ActionMessage::getStep(reply),
|
2016-06-15 23:48:41 +02:00
|
|
|
ActionMessage::getReturnCode(reply));
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
case ActionMessage::DATA_REPLY:
|
2020-04-21 22:28:43 +02:00
|
|
|
extractDataForOwner(ActionMessage::getActionId(reply),
|
|
|
|
ActionMessage::getStoreId(reply));
|
2016-06-15 23:48:41 +02:00
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
default:
|
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t CommandActionHelper::getCommandCount() const {
|
|
|
|
return commandCount;
|
|
|
|
}
|
|
|
|
|
2020-04-21 22:28:43 +02:00
|
|
|
void CommandActionHelper::extractDataForOwner(ActionId_t actionId,
|
|
|
|
store_address_t storeId) {
|
|
|
|
const uint8_t *data = NULL;
|
2016-06-15 23:48:41 +02:00
|
|
|
uint32_t size = 0;
|
|
|
|
ReturnValue_t result = ipcStore->getData(storeId, &data, &size);
|
|
|
|
if (result != HasReturnvaluesIF::RETURN_OK) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
owner->dataReceived(actionId, data, size);
|
|
|
|
ipcStore->deleteData(storeId);
|
|
|
|
}
|