fsfw/datapoollocal/LocalDataPoolManager.cpp

268 lines
9.3 KiB
C++
Raw Normal View History

#include "../datapoollocal/LocalDataPoolManager.h"
#include "../datapoollocal/LocalDataSet.h"
#include "../housekeeping/AcceptsHkPacketsIF.h"
#include "../ipc/MutexFactory.h"
#include "../ipc/MutexHelper.h"
#include "../ipc/QueueFactory.h"
2020-05-17 01:17:11 +02:00
2020-06-05 20:35:08 +02:00
#include <array>
2020-08-08 19:43:28 +02:00
#include <cmath>
2020-06-05 20:35:08 +02:00
2020-08-23 21:00:25 +02:00
object_id_t LocalDataPoolManager::defaultHkDestination = objects::NO_OBJECT;
2020-07-09 00:59:10 +02:00
LocalDataPoolManager::LocalDataPoolManager(HasLocalDataPoolIF* owner,
2020-06-30 21:22:26 +02:00
MessageQueueIF* queueToUse, bool appendValidityBuffer):
2020-06-19 03:03:17 +02:00
appendValidityBuffer(appendValidityBuffer) {
2020-05-17 01:17:11 +02:00
if(owner == nullptr) {
sif::error << "HkManager: Invalid supplied owner!" << std::endl;
2020-06-19 03:03:17 +02:00
return;
2020-05-17 01:17:11 +02:00
}
this->owner = owner;
mutex = MutexFactory::instance()->createMutex();
2020-06-07 18:53:55 +02:00
if(mutex == nullptr) {
sif::error << "LocalDataPoolManager::LocalDataPoolManager: "
"Could not create mutex." << std::endl;
}
ipcStore = objectManager->get<StorageManagerIF>(objects::IPC_STORE);
if(ipcStore == nullptr) {
sif::error << "LocalDataPoolManager::LocalDataPoolManager: "
"Could not set IPC store." << std::endl;
}
2020-08-08 19:43:28 +02:00
2020-06-30 21:22:26 +02:00
hkQueue = queueToUse;
}
ReturnValue_t LocalDataPoolManager::initialize(MessageQueueIF* queueToUse,
2020-08-23 21:00:25 +02:00
uint8_t nonDiagInvlFactor) {
2020-06-30 21:22:26 +02:00
if(queueToUse == nullptr) {
sif::error << "LocalDataPoolManager::initialize: Supplied queue "
"invalid!" << std::endl;
}
hkQueue = queueToUse;
2020-08-08 19:43:28 +02:00
setNonDiagnosticIntervalFactor(nonDiagInvlFactor);
diagnosticMinimumInterval = owner->getPeriodicOperationFrequency();
regularMinimumInterval = diagnosticMinimumInterval * nonDiagnosticIntervalFactor;
2020-06-30 21:22:26 +02:00
return initializeHousekeepingPoolEntriesOnce();
}
void LocalDataPoolManager::setHkPacketDestination(
MessageQueueId_t hkDestination) {
this->hkDestination = hkDestination;
2020-05-17 01:17:11 +02:00
}
2020-06-07 02:22:18 +02:00
LocalDataPoolManager::~LocalDataPoolManager() {}
2020-05-17 01:17:11 +02:00
2020-06-07 02:22:18 +02:00
ReturnValue_t LocalDataPoolManager::initializeHousekeepingPoolEntriesOnce() {
2020-05-17 01:17:11 +02:00
if(not mapInitialized) {
2020-07-09 00:59:10 +02:00
ReturnValue_t result = owner->initializePoolEntries(localPoolMap);
2020-05-17 01:17:11 +02:00
if(result == HasReturnvaluesIF::RETURN_OK) {
mapInitialized = true;
}
return result;
}
2020-06-19 03:03:17 +02:00
sif::warning << "HousekeepingManager: The map should only be initialized "
"once!" << std::endl;
2020-05-17 01:17:11 +02:00
return HasReturnvaluesIF::RETURN_OK;
}
2020-06-07 02:22:18 +02:00
ReturnValue_t LocalDataPoolManager::handleHousekeepingMessage(
CommandMessage* message) {
Command_t command = message->getCommand();
2020-06-19 03:03:17 +02:00
switch(command) {
// I think those are the only commands which can be handled here..
case(HousekeepingMessage::ADD_HK_REPORT_STRUCT):
case(HousekeepingMessage::ADD_DIAGNOSTICS_REPORT_STRUCT):
// We should use OwnsLocalPoolDataIF to specify those functions..
return HasReturnvaluesIF::RETURN_OK;
case(HousekeepingMessage::REPORT_DIAGNOSTICS_REPORT_STRUCTURES):
case(HousekeepingMessage::REPORT_HK_REPORT_STRUCTURES):
//return generateSetStructurePacket(message->getSid());
2020-06-19 03:03:17 +02:00
case(HousekeepingMessage::GENERATE_ONE_PARAMETER_REPORT):
case(HousekeepingMessage::GENERATE_ONE_DIAGNOSTICS_REPORT):
//return generateHousekeepingPacket(message->getSid());
2020-06-19 03:03:17 +02:00
default:
return CommandMessageIF::UNKNOWN_COMMAND;
}
2020-05-17 01:17:11 +02:00
}
2020-06-07 02:22:18 +02:00
ReturnValue_t LocalDataPoolManager::printPoolEntry(
2020-06-05 20:35:08 +02:00
lp_id_t localPoolId) {
2020-07-09 00:59:10 +02:00
auto poolIter = localPoolMap.find(localPoolId);
if (poolIter == localPoolMap.end()) {
2020-06-05 20:35:08 +02:00
sif::debug << "HousekeepingManager::fechPoolEntry:"
" Pool entry not found." << std::endl;
2020-06-19 03:03:17 +02:00
return POOL_ENTRY_NOT_FOUND;
2020-06-05 20:35:08 +02:00
}
poolIter->second->print();
return HasReturnvaluesIF::RETURN_OK;
}
2020-06-07 02:22:18 +02:00
MutexIF* LocalDataPoolManager::getMutexHandle() {
2020-05-17 01:17:11 +02:00
return mutex;
}
2020-07-09 00:59:10 +02:00
const HasLocalDataPoolIF* LocalDataPoolManager::getOwner() const {
2020-06-19 03:03:17 +02:00
return owner;
}
2020-06-30 21:22:26 +02:00
ReturnValue_t LocalDataPoolManager::generateHousekeepingPacket(sid_t sid,
float collectionInterval, MessageQueueId_t sendTo) {
2020-08-08 12:51:31 +02:00
LocalPoolDataSetBase* dataSetToSerialize = dynamic_cast<LocalPoolDataSetBase*>(
2020-06-05 20:35:08 +02:00
owner->getDataSetHandle(sid));
if(dataSetToSerialize == nullptr) {
sif::warning << "HousekeepingManager::generateHousekeepingPacket:"
" Set ID not found" << std::endl;
2020-06-07 18:53:55 +02:00
return HasReturnvaluesIF::RETURN_FAILED;
}
2020-08-08 19:56:42 +02:00
2020-06-07 18:53:55 +02:00
store_address_t storeId;
HousekeepingPacketDownlink hkPacket(sid, collectionInterval,
dataSetToSerialize->getFillCount(), dataSetToSerialize);
ReturnValue_t result = serializeHkPacketIntoStore(hkPacket, &storeId);
2020-06-07 18:53:55 +02:00
if(result != HasReturnvaluesIF::RETURN_OK) {
return result;
2020-06-05 20:35:08 +02:00
}
2020-05-17 01:17:11 +02:00
2020-06-19 03:03:17 +02:00
// and now we set a HK message and send it the HK packet destination.
2020-06-29 15:46:16 +02:00
CommandMessage hkMessage;
HousekeepingMessage::setHkReportMessage(&hkMessage, sid, storeId);
if(hkQueue == nullptr) {
2020-06-30 21:22:26 +02:00
return QUEUE_OR_DESTINATION_NOT_SET;
2020-06-29 15:46:16 +02:00
}
2020-06-30 21:22:26 +02:00
if(sendTo != MessageQueueIF::NO_QUEUE) {
result = hkQueue->sendMessage(sendTo, &hkMessage);
2020-06-29 15:46:16 +02:00
}
else {
2020-06-30 21:22:26 +02:00
if(hkDestination == MessageQueueIF::NO_QUEUE) {
sif::warning << "LocalDataPoolManager::generateHousekeepingPacket:"
" Destination is not set properly!" << std::endl;
return QUEUE_OR_DESTINATION_NOT_SET;
}
else {
result = hkQueue->sendMessage(hkDestination, &hkMessage);
}
2020-06-29 15:46:16 +02:00
}
2020-06-19 03:03:17 +02:00
return result;
2020-06-07 18:53:55 +02:00
}
ReturnValue_t LocalDataPoolManager::serializeHkPacketIntoStore(
HousekeepingPacketDownlink& hkPacket,
store_address_t *storeId) {
uint8_t* dataPtr = nullptr;
size_t serializedSize = hkPacket.getSerializedSize();
const size_t maxSize = serializedSize;
ReturnValue_t result = ipcStore->getFreeElement(storeId,
serializedSize, &dataPtr);
if(result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
return hkPacket.serialize(&dataPtr, &serializedSize, maxSize,
SerializeIF::Endianness::MACHINE);
}
ReturnValue_t LocalDataPoolManager::generateSetStructurePacket(sid_t sid) {
2020-08-08 12:51:31 +02:00
LocalPoolDataSetBase* dataSet = dynamic_cast<LocalPoolDataSetBase*>(
owner->getDataSetHandle(sid));
if(dataSet == nullptr) {
sif::warning << "HousekeepingManager::generateHousekeepingPacket:"
" Set ID not found" << std::endl;
return HasReturnvaluesIF::RETURN_FAILED;
}
size_t expectedSize = dataSet->getFillCount() * sizeof(lp_id_t);
uint8_t* storePtr = nullptr;
store_address_t storeId;
ReturnValue_t result = ipcStore->getFreeElement(&storeId,
expectedSize,&storePtr);
if(result != HasReturnvaluesIF::RETURN_OK) {
sif::error << "HousekeepingManager::generateHousekeepingPacket: "
"Could not get free element from IPC store." << std::endl;
return result;
}
size_t size = 0;
result = dataSet->serializeLocalPoolIds(&storePtr, &size,
2020-07-01 14:17:55 +02:00
expectedSize, SerializeIF::Endianness::BIG);
if(expectedSize != size) {
sif::error << "HousekeepingManager::generateSetStructurePacket: "
"Expected size is not equal to serialized size" << std::endl;
}
return result;
}
2020-08-08 19:43:28 +02:00
void LocalDataPoolManager::setNonDiagnosticIntervalFactor(
uint8_t nonDiagInvlFactor) {
this->nonDiagnosticIntervalFactor = nonDiagInvlFactor;
2020-06-30 21:22:26 +02:00
}
2020-06-19 03:03:17 +02:00
2020-07-09 00:59:10 +02:00
ReturnValue_t LocalDataPoolManager::performHkOperation() {
2020-08-08 19:43:28 +02:00
for(auto& hkReceiversIter: hkReceiversMap) {
HkReceiver* receiver = &hkReceiversIter.second;
2020-08-08 19:56:42 +02:00
if(not receiver->reportingEnabled) {
return HasReturnvaluesIF::RETURN_OK;
}
2020-08-08 19:43:28 +02:00
switch(receiver->reportingType) {
case(ReportingType::PERIODIC): {
2020-08-08 21:32:15 +02:00
if(receiver->dataId.dataSetSid.notSet()) {
2020-08-08 19:56:42 +02:00
// Periodic packets shall only be generated from datasets.
continue;
}
2020-08-08 19:43:28 +02:00
performPeriodicHkGeneration(receiver);
break;
}
2020-08-23 21:00:25 +02:00
case(ReportingType::UPDATE_SNAPSHOT): {
2020-08-08 19:43:28 +02:00
// check whether data has changed and send messages in case it has.
break;
}
default:
// This should never happen.
return HasReturnvaluesIF::RETURN_FAILED;
}
}
2020-07-09 00:59:10 +02:00
return HasReturnvaluesIF::RETURN_OK;
}
2020-08-08 19:43:28 +02:00
void LocalDataPoolManager::performPeriodicHkGeneration(HkReceiver* receiver) {
2020-08-08 19:56:42 +02:00
if(receiver->intervalCounter >= intervalSecondsToInterval(
receiver->isDiagnostics,
2020-08-10 18:33:28 +02:00
receiver->hkParameter.collectionIntervalSeconds)) {
2020-08-08 19:56:42 +02:00
ReturnValue_t result = generateHousekeepingPacket(
receiver->dataId.dataSetSid, receiver->destinationQueue);
if(result != HasReturnvaluesIF::RETURN_OK) {
// configuration error
sif::debug << "LocalDataPoolManager::performHkOperation:"
<< "0x" << std::setfill('0') << std::setw(8)
<< owner->getObjectId() << " Error generating "
<< "HK packet" << std::setfill(' ') << std::endl;
2020-08-08 19:43:28 +02:00
}
2020-08-08 19:56:42 +02:00
receiver->intervalCounter = 1;
}
else {
receiver->intervalCounter++;
2020-08-08 19:43:28 +02:00
}
}
uint32_t LocalDataPoolManager::intervalSecondsToInterval(bool isDiagnostics,
float collectionIntervalSeconds) {
if(isDiagnostics) {
return std::ceil(collectionIntervalSeconds/diagnosticMinimumInterval);
2020-08-08 19:43:28 +02:00
}
else {
return std::ceil(collectionIntervalSeconds/regularMinimumInterval);
}
}
float LocalDataPoolManager::intervalToIntervalSeconds(bool isDiagnostics,
uint32_t collectionInterval) {
if(isDiagnostics) {
2020-08-10 18:33:28 +02:00
return static_cast<float>(collectionInterval * diagnosticMinimumInterval);
2020-08-08 19:43:28 +02:00
}
else {
return static_cast<float>(collectionInterval * regularMinimumInterval);
}
}