2020-04-06 13:22:42 +02:00
|
|
|
#include <framework/globalfunctions/CRC.h>
|
2016-06-15 23:48:41 +02:00
|
|
|
#include <framework/memory/MemoryHelper.h>
|
|
|
|
#include <framework/memory/MemoryMessage.h>
|
|
|
|
#include <framework/objectmanager/ObjectManagerIF.h>
|
|
|
|
#include <framework/serialize/EndianSwapper.h>
|
2018-07-12 16:29:32 +02:00
|
|
|
#include <framework/serviceinterface/ServiceInterfaceStream.h>
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2020-06-07 01:17:24 +02:00
|
|
|
MemoryHelper::MemoryHelper(HasMemoryIF* workOnThis,
|
|
|
|
MessageQueueIF* useThisQueue):
|
|
|
|
workOnThis(workOnThis), queueToUse(useThisQueue), ipcStore(nullptr),
|
|
|
|
ipcAddress(), lastCommand(CommandMessage::CMD_NONE), busy(false) {
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t MemoryHelper::handleMemoryCommand(CommandMessage* message) {
|
|
|
|
lastSender = message->getSender();
|
|
|
|
lastCommand = message->getCommand();
|
2018-07-12 16:29:32 +02:00
|
|
|
if (busy) {
|
2020-04-23 19:13:18 +02:00
|
|
|
sif::debug << "MemHelper: Busy!" << std::endl;
|
2018-07-12 16:29:32 +02:00
|
|
|
}
|
2016-06-15 23:48:41 +02:00
|
|
|
switch (lastCommand) {
|
|
|
|
case MemoryMessage::CMD_MEMORY_DUMP:
|
|
|
|
handleMemoryCheckOrDump(message);
|
|
|
|
return RETURN_OK;
|
|
|
|
case MemoryMessage::CMD_MEMORY_LOAD:
|
|
|
|
handleMemoryLoad(message);
|
|
|
|
return RETURN_OK;
|
|
|
|
case MemoryMessage::CMD_MEMORY_CHECK:
|
|
|
|
handleMemoryCheckOrDump(message);
|
|
|
|
return RETURN_OK;
|
|
|
|
default:
|
|
|
|
lastCommand = CommandMessage::CMD_NONE;
|
|
|
|
return UNKNOWN_CMD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryHelper::completeLoad(ReturnValue_t errorCode,
|
|
|
|
const uint8_t* dataToCopy, const uint32_t size, uint8_t* copyHere) {
|
2018-07-12 16:29:32 +02:00
|
|
|
busy = false;
|
2016-06-15 23:48:41 +02:00
|
|
|
switch (errorCode) {
|
|
|
|
case HasMemoryIF::DO_IT_MYSELF:
|
2018-07-12 16:29:32 +02:00
|
|
|
busy = true;
|
2016-06-15 23:48:41 +02:00
|
|
|
return;
|
|
|
|
case HasMemoryIF::POINTS_TO_MEMORY:
|
|
|
|
memcpy(copyHere, dataToCopy, size);
|
|
|
|
break;
|
|
|
|
case HasMemoryIF::POINTS_TO_VARIABLE:
|
|
|
|
EndianSwapper::swap(copyHere, dataToCopy, size);
|
|
|
|
break;
|
|
|
|
case HasMemoryIF::ACTIVITY_COMPLETED:
|
|
|
|
case RETURN_OK:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ipcStore->deleteData(ipcAddress);
|
2020-06-24 00:24:15 +02:00
|
|
|
CommandMessage reply;
|
2016-06-15 23:48:41 +02:00
|
|
|
MemoryMessage::setMemoryReplyFailed(&reply, errorCode,
|
|
|
|
MemoryMessage::CMD_MEMORY_LOAD);
|
|
|
|
queueToUse->sendMessage(lastSender, &reply);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//Only reached on success
|
2020-06-24 00:24:15 +02:00
|
|
|
CommandMessage reply( CommandMessage::REPLY_COMMAND_OK, 0, 0);
|
2016-06-15 23:48:41 +02:00
|
|
|
queueToUse->sendMessage(lastSender, &reply);
|
|
|
|
ipcStore->deleteData(ipcAddress);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryHelper::completeDump(ReturnValue_t errorCode,
|
|
|
|
const uint8_t* dataToCopy, const uint32_t size) {
|
2018-07-12 16:29:32 +02:00
|
|
|
busy = false;
|
2020-06-24 00:24:15 +02:00
|
|
|
CommandMessage reply;
|
2016-06-15 23:48:41 +02:00
|
|
|
MemoryMessage::setMemoryReplyFailed(&reply, errorCode, lastCommand);
|
|
|
|
switch (errorCode) {
|
|
|
|
case HasMemoryIF::DO_IT_MYSELF:
|
2018-07-12 16:29:32 +02:00
|
|
|
busy = true;
|
2016-06-15 23:48:41 +02:00
|
|
|
return;
|
|
|
|
case HasReturnvaluesIF::RETURN_OK:
|
|
|
|
case HasMemoryIF::POINTS_TO_MEMORY:
|
|
|
|
case HasMemoryIF::POINTS_TO_VARIABLE:
|
|
|
|
//"data" must be valid pointer!
|
|
|
|
if (errorCode == HasMemoryIF::POINTS_TO_VARIABLE) {
|
|
|
|
EndianSwapper::swap(reservedSpaceInIPC, dataToCopy, size);
|
|
|
|
} else {
|
|
|
|
memcpy(reservedSpaceInIPC, dataToCopy, size);
|
|
|
|
}
|
2018-07-12 16:29:32 +02:00
|
|
|
/* NO BREAK falls through*/
|
2016-06-15 23:48:41 +02:00
|
|
|
case HasMemoryIF::ACTIVITY_COMPLETED:
|
|
|
|
switch (lastCommand) {
|
|
|
|
case MemoryMessage::CMD_MEMORY_DUMP: {
|
|
|
|
MemoryMessage::setMemoryDumpReply(&reply, ipcAddress);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MemoryMessage::CMD_MEMORY_CHECK: {
|
2020-04-06 13:22:42 +02:00
|
|
|
uint16_t crc = CRC::crc16ccitt(reservedSpaceInIPC, size);
|
2016-06-15 23:48:41 +02:00
|
|
|
//Delete data immediately, was temporary.
|
|
|
|
ipcStore->deleteData(ipcAddress);
|
|
|
|
MemoryMessage::setMemoryCheckReply(&reply, crc);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
//This should never happen!
|
|
|
|
//Is it ok to send message? Otherwise: return;
|
|
|
|
ipcStore->deleteData(ipcAddress);
|
|
|
|
reply.setParameter(STATE_MISMATCH);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2018-07-12 16:29:32 +02:00
|
|
|
case HasMemoryIF::DUMP_NOT_SUPPORTED:
|
|
|
|
if (lastCommand == MemoryMessage::CMD_MEMORY_CHECK){
|
|
|
|
MemoryMessage::setMemoryCheckReply(&reply, 0);
|
|
|
|
MemoryMessage::setCrcReturnValue(&reply,HasMemoryIF::DUMP_NOT_SUPPORTED);
|
|
|
|
}
|
|
|
|
ipcStore->deleteData(ipcAddress);
|
|
|
|
break;
|
2016-06-15 23:48:41 +02:00
|
|
|
default:
|
|
|
|
//Reply is already set to REJECTED.
|
|
|
|
ipcStore->deleteData(ipcAddress);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (queueToUse->sendMessage(lastSender, &reply) != RETURN_OK) {
|
2020-06-13 21:01:01 +02:00
|
|
|
reply.clear();
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryHelper::swapMatrixCopy(uint8_t* out, const uint8_t *in,
|
|
|
|
uint32_t totalSize, uint8_t datatypeSize) {
|
|
|
|
if (totalSize % datatypeSize != 0){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (totalSize > 0){
|
|
|
|
EndianSwapper::swap(out,in,datatypeSize);
|
|
|
|
out += datatypeSize;
|
|
|
|
in += datatypeSize;
|
|
|
|
totalSize -= datatypeSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryHelper::~MemoryHelper() {
|
|
|
|
//Nothing to destroy
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryHelper::handleMemoryLoad(CommandMessage* message) {
|
|
|
|
uint32_t address = MemoryMessage::getAddress(message);
|
|
|
|
ipcAddress = MemoryMessage::getStoreID(message);
|
|
|
|
const uint8_t* p_data = NULL;
|
|
|
|
uint8_t* dataPointer = NULL;
|
2020-03-23 17:58:23 +01:00
|
|
|
size_t size = 0;
|
2016-06-15 23:48:41 +02:00
|
|
|
ReturnValue_t returnCode = ipcStore->getData(ipcAddress, &p_data, &size);
|
|
|
|
if (returnCode == RETURN_OK) {
|
|
|
|
returnCode = workOnThis->handleMemoryLoad(address, p_data, size,
|
|
|
|
&dataPointer);
|
|
|
|
completeLoad(returnCode, p_data, size, dataPointer);
|
|
|
|
} else {
|
|
|
|
//At least inform sender.
|
2020-06-24 00:24:15 +02:00
|
|
|
CommandMessage reply;
|
2016-06-15 23:48:41 +02:00
|
|
|
MemoryMessage::setMemoryReplyFailed(&reply, returnCode,
|
|
|
|
MemoryMessage::CMD_MEMORY_LOAD);
|
|
|
|
queueToUse->sendMessage(lastSender, &reply);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryHelper::handleMemoryCheckOrDump(CommandMessage* message) {
|
|
|
|
uint32_t address = MemoryMessage::getAddress(message);
|
|
|
|
uint32_t size = MemoryMessage::getLength(message);
|
|
|
|
uint8_t* dataPointer = NULL;
|
|
|
|
ReturnValue_t returnCode = ipcStore->getFreeElement(&ipcAddress, size,
|
|
|
|
&reservedSpaceInIPC);
|
|
|
|
if (returnCode == RETURN_OK) {
|
|
|
|
returnCode = workOnThis->handleMemoryDump(address, size, &dataPointer,
|
|
|
|
reservedSpaceInIPC);
|
|
|
|
completeDump(returnCode, dataPointer, size);
|
|
|
|
} else {
|
2020-06-24 00:24:15 +02:00
|
|
|
CommandMessage reply;
|
2016-06-15 23:48:41 +02:00
|
|
|
MemoryMessage::setMemoryReplyFailed(&reply, returnCode, lastCommand);
|
|
|
|
queueToUse->sendMessage(lastSender, &reply);
|
|
|
|
}
|
|
|
|
}
|
2018-07-13 18:28:26 +02:00
|
|
|
|
|
|
|
ReturnValue_t MemoryHelper::initialize(MessageQueueIF* queueToUse_) {
|
2020-06-06 23:41:54 +02:00
|
|
|
if(queueToUse_ == nullptr) {
|
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
2018-07-13 18:28:26 +02:00
|
|
|
}
|
2020-06-06 23:41:54 +02:00
|
|
|
this->queueToUse = queueToUse_;
|
2018-07-13 18:28:26 +02:00
|
|
|
return initialize();
|
|
|
|
}
|
2020-06-06 23:41:54 +02:00
|
|
|
|
|
|
|
ReturnValue_t MemoryHelper::initialize() {
|
|
|
|
ipcStore = objectManager->get<StorageManagerIF>(objects::IPC_STORE);
|
|
|
|
if (ipcStore != nullptr) {
|
|
|
|
return RETURN_OK;
|
|
|
|
} else {
|
|
|
|
return RETURN_FAILED;
|
|
|
|
}
|
|
|
|
}
|