fsfw/datapoollocal/LocalDataPoolManager.cpp

306 lines
11 KiB
C++
Raw Normal View History

2020-09-06 14:57:05 +02:00
#include "LocalDataPoolManager.h"
#include "LocalPoolDataSetBase.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;
2020-08-24 22:08:27 +02:00
if(defaultHkDestination != objects::NO_OBJECT) {
AcceptsHkPacketsIF* hkPacketReceiver =
objectManager->get<AcceptsHkPacketsIF>(defaultHkDestination);
if(hkPacketReceiver != nullptr) {
defaultHkDestinationId = hkPacketReceiver->getHkQueue();
}
}
2020-06-30 21:22:26 +02:00
}
2020-08-23 23:24:48 +02:00
LocalDataPoolManager::~LocalDataPoolManager() {}
2020-08-24 22:08:27 +02:00
ReturnValue_t LocalDataPoolManager::initialize(MessageQueueIF* queueToUse) {
2020-06-30 21:22:26 +02:00
if(queueToUse == nullptr) {
sif::error << "LocalDataPoolManager::initialize: Supplied queue "
"invalid!" << std::endl;
}
hkQueue = queueToUse;
2020-08-24 22:08:27 +02:00
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t LocalDataPoolManager::initializeAfterTaskCreation(
uint8_t nonDiagInvlFactor) {
2020-08-08 19:43:28 +02:00
setNonDiagnosticIntervalFactor(nonDiagInvlFactor);
diagnosticMinimumInterval = owner->getPeriodicOperationFrequency();
2020-08-23 23:24:48 +02:00
regularMinimumInterval = diagnosticMinimumInterval *
2020-08-24 22:08:27 +02:00
nonDiagnosticIntervalFactor;
2020-06-30 21:22:26 +02:00
return initializeHousekeepingPoolEntriesOnce();
}
2020-06-07 02:22:18 +02:00
ReturnValue_t LocalDataPoolManager::initializeHousekeepingPoolEntriesOnce() {
2020-05-17 01:17:11 +02:00
if(not mapInitialized) {
2020-08-24 22:08:27 +02:00
ReturnValue_t result = owner->initializeLocalDataPool(localPoolMap,
*this);
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-08-23 23:24:48 +02:00
ReturnValue_t LocalDataPoolManager::performHkOperation() {
for(auto& hkReceiversIter: hkReceiversMap) {
HkReceiver* receiver = &hkReceiversIter.second;
if(not receiver->reportingEnabled) {
return HasReturnvaluesIF::RETURN_OK;
}
switch(receiver->reportingType) {
case(ReportingType::PERIODIC): {
if(receiver->dataId.dataSetSid.notSet()) {
// Periodic packets shall only be generated from datasets.
continue;
}
performPeriodicHkGeneration(receiver);
break;
}
case(ReportingType::UPDATE_SNAPSHOT): {
// check whether data has changed and send messages in case it has.
break;
}
default:
// This should never happen.
return HasReturnvaluesIF::RETURN_FAILED;
}
}
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t LocalDataPoolManager::subscribeForPeriodicPacket(sid_t sid,
bool enableReporting, float collectionInterval, bool isDiagnostics,
object_id_t packetDestination) {
AcceptsHkPacketsIF* hkReceiverObject =
objectManager->get<AcceptsHkPacketsIF>(packetDestination);
if(hkReceiverObject == nullptr) {
sif::error << "LocalDataPoolManager::subscribeForPeriodicPacket:"
" Invalid receiver!"<< std::endl;
return HasReturnvaluesIF::RETURN_OK;
}
struct HkReceiver hkReceiver;
hkReceiver.dataId.dataSetSid = sid;
hkReceiver.reportingType = ReportingType::PERIODIC;
hkReceiver.destinationQueue = hkReceiverObject->getHkQueue();
hkReceiver.reportingEnabled = enableReporting;
2020-08-24 22:08:27 +02:00
if(not isDiagnostics) {
hkReceiver.hkParameter.collectionIntervalTicks =
intervalSecondsToInterval(isDiagnostics, collectionInterval *
nonDiagnosticIntervalFactor);
}
else {
hkReceiver.hkParameter.collectionIntervalTicks =
intervalSecondsToInterval(isDiagnostics, collectionInterval);
}
2020-08-23 23:24:48 +02:00
hkReceiver.isDiagnostics = isDiagnostics;
hkReceiver.intervalCounter = 1;
hkReceiversMap.emplace(packetDestination, hkReceiver);
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-08-24 22:08:27 +02:00
ReturnValue_t LocalDataPoolManager::generateHousekeepingPacket(sid_t sid,
float collectionInterval, MessageQueueId_t destination) {
2020-08-23 23:24:48 +02:00
LocalPoolDataSetBase* dataSetToSerialize =
dynamic_cast<LocalPoolDataSetBase*>(owner->getDataSetHandle(sid));
2020-06-05 20:35:08 +02:00
if(dataSetToSerialize == nullptr) {
sif::warning << "HousekeepingManager::generateHousekeepingPacket:"
2020-08-24 22:08:27 +02:00
" Set ID not found or dataset not assigned!" << std::endl;
2020-06-07 18:53:55 +02:00
return HasReturnvaluesIF::RETURN_FAILED;
}
2020-09-06 14:57:05 +02:00
2020-08-24 22:08:27 +02:00
store_address_t storeId;
HousekeepingPacketDownlink hkPacket(sid, collectionInterval,
dataSetToSerialize->getFillCount(), dataSetToSerialize);
ReturnValue_t result = serializeHkPacketIntoStore(hkPacket, &storeId);
if(result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
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-08-24 22:08:27 +02:00
CommandMessage hkMessage;
HousekeepingMessage::setHkReportMessage(&hkMessage, sid, storeId);
if(hkQueue == nullptr) {
return QUEUE_OR_DESTINATION_NOT_SET;
}
if(destination == MessageQueueIF::NO_QUEUE) {
if(defaultHkDestinationId == MessageQueueIF::NO_QUEUE) {
// error, all destinations invalid
return HasReturnvaluesIF::RETURN_FAILED;
}
destination = defaultHkDestinationId;
}
2020-06-19 03:03:17 +02:00
2020-08-24 22:08:27 +02:00
return hkQueue->sendMessage(destination, &hkMessage);
2020-06-07 18:53:55 +02:00
}
ReturnValue_t LocalDataPoolManager::serializeHkPacketIntoStore(
HousekeepingPacketDownlink& hkPacket,
store_address_t *storeId) {
uint8_t* dataPtr = nullptr;
2020-08-24 22:08:27 +02:00
size_t serializedSize = 0;
const size_t maxSize = hkPacket.getSerializedSize();
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-08-08 19:43:28 +02:00
void LocalDataPoolManager::performPeriodicHkGeneration(HkReceiver* receiver) {
2020-08-24 22:08:27 +02:00
if(receiver->intervalCounter >=
receiver->hkParameter.collectionIntervalTicks) {
2020-08-08 19:56:42 +02:00
ReturnValue_t result = generateHousekeepingPacket(
2020-08-24 22:08:27 +02:00
receiver->dataId.dataSetSid);
2020-08-08 19:56:42 +02:00
if(result != HasReturnvaluesIF::RETURN_OK) {
// configuration error
sif::debug << "LocalDataPoolManager::performHkOperation:"
<< "0x" << std::setfill('0') << std::setw(8)
2020-08-24 22:08:27 +02:00
<< 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) {
2020-08-24 22:08:27 +02:00
return std::ceil(collectionIntervalSeconds * 1000
/diagnosticMinimumInterval);
2020-08-08 19:43:28 +02:00
}
else {
2020-08-24 22:08:27 +02:00
return std::ceil(collectionIntervalSeconds * 1000
/regularMinimumInterval);
2020-08-08 19:43:28 +02:00
}
}
float LocalDataPoolManager::intervalToIntervalSeconds(bool isDiagnostics,
uint32_t collectionInterval) {
if(isDiagnostics) {
2020-08-23 23:24:48 +02:00
return static_cast<float>(collectionInterval *
diagnosticMinimumInterval);
2020-08-08 19:43:28 +02:00
}
else {
2020-08-24 22:08:27 +02:00
return static_cast<float>(collectionInterval *
regularMinimumInterval);
2020-08-08 19:43:28 +02:00
}
}