fsfw/datapoollocal/LocalDataPoolManager.cpp

912 lines
29 KiB
C++
Raw Normal View History

#include "HasLocalDataPoolIF.h"
2020-10-01 12:05:24 +02:00
#include "LocalDataPoolManager.h"
2020-12-03 13:00:04 +01:00
#include "LocalPoolObjectBase.h"
2020-10-01 12:05:24 +02:00
#include "LocalPoolDataSetBase.h"
2021-01-12 14:08:51 +01:00
#include "LocalPoolDataSetAttorney.h"
#include "HasLocalDpIFManagerAttorney.h"
2020-10-01 12:05:24 +02:00
2020-12-03 13:00:04 +01:00
#include "../housekeeping/HousekeepingPacketUpdate.h"
2020-10-01 12:05:24 +02:00
#include "../housekeeping/HousekeepingSetPacket.h"
#include "../housekeeping/AcceptsHkPacketsIF.h"
2020-12-03 13:00:04 +01:00
#include "../timemanager/CCSDSTime.h"
2020-10-01 12:05:24 +02:00
#include "../ipc/MutexFactory.h"
#include "../ipc/MutexHelper.h"
#include "../ipc/QueueFactory.h"
#include <array>
#include <cmath>
2021-01-11 22:46:40 +01:00
object_id_t LocalDataPoolManager::defaultHkDestination = objects::PUS_SERVICE_3_HOUSEKEEPING;
2020-10-01 12:05:24 +02:00
2021-01-11 22:32:36 +01:00
LocalDataPoolManager::LocalDataPoolManager(HasLocalDataPoolIF* owner, MessageQueueIF* queueToUse,
bool appendValidityBuffer):
2021-01-11 22:46:40 +01:00
appendValidityBuffer(appendValidityBuffer) {
2021-01-11 22:45:04 +01:00
if(owner == nullptr) {
printWarningOrError(sif::OutputTypes::OUT_WARNING,
2021-01-11 22:45:04 +01:00
"LocalDataPoolManager", HasReturnvaluesIF::RETURN_FAILED,
2021-01-08 15:10:33 +01:00
"Invalid supplied owner");
2021-01-11 22:45:04 +01:00
return;
}
this->owner = owner;
mutex = MutexFactory::instance()->createMutex();
if(mutex == nullptr) {
printWarningOrError(sif::OutputTypes::OUT_ERROR,
2021-01-11 22:45:04 +01:00
"LocalDataPoolManager", HasReturnvaluesIF::RETURN_FAILED,
2021-01-08 15:10:33 +01:00
"Could not create mutex");
2021-01-11 22:45:04 +01:00
}
2020-10-01 12:05:24 +02:00
2021-01-11 22:45:04 +01:00
hkQueue = queueToUse;
2020-10-01 12:05:24 +02:00
}
LocalDataPoolManager::~LocalDataPoolManager() {}
ReturnValue_t LocalDataPoolManager::initialize(MessageQueueIF* queueToUse) {
2021-01-11 22:45:04 +01:00
if(queueToUse == nullptr) {
// error, all destinations invalid
printWarningOrError(sif::OutputTypes::OUT_ERROR,
2021-01-11 22:45:04 +01:00
"initialize", QUEUE_OR_DESTINATION_INVALID);
}
hkQueue = queueToUse;
ipcStore = objectManager->get<StorageManagerIF>(objects::IPC_STORE);
if(ipcStore == nullptr) {
// error, all destinations invalid
printWarningOrError(sif::OutputTypes::OUT_ERROR,
2021-01-11 22:45:04 +01:00
"initialize", HasReturnvaluesIF::RETURN_FAILED,
2021-01-08 15:10:33 +01:00
"Could not set IPC store.");
2021-01-11 22:45:04 +01:00
return HasReturnvaluesIF::RETURN_FAILED;
}
if(defaultHkDestination != objects::NO_OBJECT) {
AcceptsHkPacketsIF* hkPacketReceiver =
objectManager->get<AcceptsHkPacketsIF>(defaultHkDestination);
if(hkPacketReceiver != nullptr) {
hkDestinationId = hkPacketReceiver->getHkQueue();
}
else {
printWarningOrError(sif::OutputTypes::OUT_ERROR,
2021-01-11 22:45:04 +01:00
"initialize", QUEUE_OR_DESTINATION_INVALID);
return QUEUE_OR_DESTINATION_INVALID;
}
}
return HasReturnvaluesIF::RETURN_OK;
2020-10-01 12:05:24 +02:00
}
ReturnValue_t LocalDataPoolManager::initializeAfterTaskCreation(
2021-01-11 22:45:04 +01:00
uint8_t nonDiagInvlFactor) {
setNonDiagnosticIntervalFactor(nonDiagInvlFactor);
return initializeHousekeepingPoolEntriesOnce();
2020-10-01 12:05:24 +02:00
}
ReturnValue_t LocalDataPoolManager::initializeHousekeepingPoolEntriesOnce() {
2021-01-11 22:45:04 +01:00
if(not mapInitialized) {
ReturnValue_t result = owner->initializeLocalDataPool(localPoolMap,
*this);
if(result == HasReturnvaluesIF::RETURN_OK) {
mapInitialized = true;
}
return result;
}
2021-01-08 15:10:33 +01:00
printWarningOrError(sif::OutputTypes::OUT_WARNING,
2021-01-08 15:10:33 +01:00
"initialize", HasReturnvaluesIF::RETURN_FAILED,
"The map should only be initialized once");
2021-01-11 22:45:04 +01:00
return HasReturnvaluesIF::RETURN_OK;
2020-10-01 12:05:24 +02:00
}
ReturnValue_t LocalDataPoolManager::performHkOperation() {
2021-01-11 22:45:04 +01:00
ReturnValue_t status = HasReturnvaluesIF::RETURN_OK;
for(auto& receiver: hkReceiversMap) {
switch(receiver.reportingType) {
case(ReportingType::PERIODIC): {
if(receiver.dataType == DataType::LOCAL_POOL_VARIABLE) {
// Periodic packets shall only be generated from datasets.
continue;
}
performPeriodicHkGeneration(receiver);
break;
}
case(ReportingType::UPDATE_HK): {
handleHkUpdate(receiver, status);
break;
}
case(ReportingType::UPDATE_NOTIFICATION): {
handleNotificationUpdate(receiver, status);
break;
}
case(ReportingType::UPDATE_SNAPSHOT): {
handleNotificationSnapshot(receiver, status);
break;
}
default:
// This should never happen.
return HasReturnvaluesIF::RETURN_FAILED;
}
}
resetHkUpdateResetHelper();
return status;
2020-12-03 13:00:04 +01:00
}
ReturnValue_t LocalDataPoolManager::handleHkUpdate(HkReceiver& receiver,
2021-01-11 22:45:04 +01:00
ReturnValue_t& status) {
if(receiver.dataType == DataType::LOCAL_POOL_VARIABLE) {
// Update packets shall only be generated from datasets.
return HasReturnvaluesIF::RETURN_FAILED;
}
2021-01-12 14:08:51 +01:00
LocalPoolDataSetBase* dataSet = HasLocalDpIFManagerAttorney::getDataSetHandle(owner,
2021-01-11 22:45:04 +01:00
receiver.dataId.sid);
if(dataSet->hasChanged()) {
// prepare and send update notification
ReturnValue_t result = generateHousekeepingPacket(
receiver.dataId.sid, dataSet, true);
if(result != HasReturnvaluesIF::RETURN_OK) {
status = result;
}
}
handleChangeResetLogic(receiver.dataType, receiver.dataId,
dataSet);
return HasReturnvaluesIF::RETURN_OK;
2020-12-03 13:00:04 +01:00
}
2021-01-11 22:32:36 +01:00
ReturnValue_t LocalDataPoolManager::handleNotificationUpdate(HkReceiver& receiver,
ReturnValue_t& status) {
2021-01-11 22:45:04 +01:00
MarkChangedIF* toReset = nullptr;
if(receiver.dataType == DataType::LOCAL_POOL_VARIABLE) {
2021-01-12 14:08:51 +01:00
LocalPoolObjectBase* poolObj = HasLocalDpIFManagerAttorney::getPoolObjectHandle(owner,
receiver.dataId.localPoolId);
//LocalPoolObjectBase* poolObj = owner->getPoolObjectHandle(receiver.dataId.localPoolId);
2021-01-11 22:45:04 +01:00
if(poolObj == nullptr) {
printWarningOrError(sif::OutputTypes::OUT_WARNING,
2021-01-11 22:45:04 +01:00
"handleNotificationUpdate", POOLOBJECT_NOT_FOUND);
return POOLOBJECT_NOT_FOUND;
}
if(poolObj->hasChanged()) {
// prepare and send update notification.
CommandMessage notification;
HousekeepingMessage::setUpdateNotificationVariableCommand(&notification,
receiver.dataId.localPoolId);
ReturnValue_t result = hkQueue->sendMessage(
receiver.destinationQueue, &notification);
if(result != HasReturnvaluesIF::RETURN_OK) {
status = result;
}
toReset = poolObj;
}
}
else {
2021-01-12 14:08:51 +01:00
LocalPoolDataSetBase* dataSet = HasLocalDpIFManagerAttorney::getDataSetHandle(owner,
receiver.dataId.sid);
2021-01-11 22:45:04 +01:00
if(dataSet == nullptr) {
printWarningOrError(sif::OutputTypes::OUT_WARNING,
2021-01-11 22:45:04 +01:00
"handleNotificationUpdate", DATASET_NOT_FOUND);
return DATASET_NOT_FOUND;
}
if(dataSet->hasChanged()) {
// prepare and send update notification
CommandMessage notification;
HousekeepingMessage::setUpdateNotificationSetCommand(
&notification, receiver.dataId.sid);
ReturnValue_t result = hkQueue->sendMessage(
receiver.destinationQueue, &notification);
if(result != HasReturnvaluesIF::RETURN_OK) {
status = result;
}
toReset = dataSet;
}
}
if(toReset != nullptr) {
handleChangeResetLogic(receiver.dataType,
receiver.dataId, toReset);
}
return HasReturnvaluesIF::RETURN_OK;
2020-10-01 12:05:24 +02:00
}
2020-12-03 13:00:04 +01:00
ReturnValue_t LocalDataPoolManager::handleNotificationSnapshot(
2021-01-11 22:45:04 +01:00
HkReceiver& receiver, ReturnValue_t& status) {
MarkChangedIF* toReset = nullptr;
// check whether data has changed and send messages in case it has.
if(receiver.dataType == DataType::LOCAL_POOL_VARIABLE) {
2021-01-12 14:08:51 +01:00
LocalPoolObjectBase* poolObj = HasLocalDpIFManagerAttorney::getPoolObjectHandle(owner,
2021-01-11 22:45:04 +01:00
receiver.dataId.localPoolId);
if(poolObj == nullptr) {
printWarningOrError(sif::OutputTypes::OUT_WARNING,
2021-01-11 22:45:04 +01:00
"handleNotificationSnapshot", POOLOBJECT_NOT_FOUND);
return POOLOBJECT_NOT_FOUND;
}
if (not poolObj->hasChanged()) {
return HasReturnvaluesIF::RETURN_OK;
}
// prepare and send update snapshot.
timeval now;
Clock::getClock_timeval(&now);
CCSDSTime::CDS_short cds;
CCSDSTime::convertToCcsds(&cds, &now);
HousekeepingPacketUpdate updatePacket(reinterpret_cast<uint8_t*>(&cds),
2021-01-12 14:08:51 +01:00
sizeof(cds), HasLocalDpIFManagerAttorney::getPoolObjectHandle(owner,
2021-01-11 22:45:04 +01:00
receiver.dataId.localPoolId));
store_address_t storeId;
ReturnValue_t result = addUpdateToStore(updatePacket, storeId);
if(result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
CommandMessage notification;
HousekeepingMessage::setUpdateSnapshotVariableCommand(&notification,
receiver.dataId.localPoolId, storeId);
result = hkQueue->sendMessage(receiver.destinationQueue,
&notification);
if (result != HasReturnvaluesIF::RETURN_OK) {
status = result;
}
toReset = poolObj;
}
else {
2021-01-12 14:08:51 +01:00
LocalPoolDataSetBase* dataSet = HasLocalDpIFManagerAttorney::getDataSetHandle(owner,
2021-01-11 22:45:04 +01:00
receiver.dataId.sid);
if(dataSet == nullptr) {
printWarningOrError(sif::OutputTypes::OUT_WARNING,
2021-01-11 22:45:04 +01:00
"handleNotificationSnapshot", DATASET_NOT_FOUND);
return DATASET_NOT_FOUND;
}
if(not dataSet->hasChanged()) {
return HasReturnvaluesIF::RETURN_OK;
}
// prepare and send update snapshot.
timeval now;
Clock::getClock_timeval(&now);
CCSDSTime::CDS_short cds;
CCSDSTime::convertToCcsds(&cds, &now);
HousekeepingPacketUpdate updatePacket(reinterpret_cast<uint8_t*>(&cds),
2021-01-12 14:08:51 +01:00
sizeof(cds), HasLocalDpIFManagerAttorney::getDataSetHandle(owner,
receiver.dataId.sid));
2021-01-11 22:45:04 +01:00
store_address_t storeId;
ReturnValue_t result = addUpdateToStore(updatePacket, storeId);
if(result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
CommandMessage notification;
HousekeepingMessage::setUpdateSnapshotSetCommand(
&notification, receiver.dataId.sid, storeId);
result = hkQueue->sendMessage(receiver.destinationQueue, &notification);
if(result != HasReturnvaluesIF::RETURN_OK) {
status = result;
}
toReset = dataSet;
}
if(toReset != nullptr) {
handleChangeResetLogic(receiver.dataType,
receiver.dataId, toReset);
}
return HasReturnvaluesIF::RETURN_OK;
2020-12-03 13:00:04 +01:00
}
ReturnValue_t LocalDataPoolManager::addUpdateToStore(
2021-01-11 22:45:04 +01:00
HousekeepingPacketUpdate& updatePacket, store_address_t& storeId) {
size_t updatePacketSize = updatePacket.getSerializedSize();
uint8_t *storePtr = nullptr;
ReturnValue_t result = ipcStore->getFreeElement(&storeId,
updatePacket.getSerializedSize(), &storePtr);
if (result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
size_t serializedSize = 0;
result = updatePacket.serialize(&storePtr, &serializedSize,
updatePacketSize, SerializeIF::Endianness::MACHINE);
return result;;
2020-12-03 13:00:04 +01:00
}
void LocalDataPoolManager::handleChangeResetLogic(
2021-01-11 22:45:04 +01:00
DataType type, DataId dataId, MarkChangedIF* toReset) {
if(hkUpdateResetList == nullptr) {
// config error!
return;
}
HkUpdateResetList& listRef = *hkUpdateResetList;
for(auto& changeInfo: listRef) {
if(changeInfo.dataType != type) {
continue;
}
if((changeInfo.dataType == DataType::DATA_SET) and
(changeInfo.dataId.sid != dataId.sid)) {
continue;
}
if((changeInfo.dataType == DataType::LOCAL_POOL_VARIABLE) and
(changeInfo.dataId.localPoolId != dataId.localPoolId)) {
continue;
}
// only one update recipient, we can reset changes status immediately.
if(changeInfo.updateCounter <= 1) {
toReset->setChanged(false);
}
// All recipients have been notified, reset the changed flag.
if(changeInfo.currentUpdateCounter <= 1) {
toReset->setChanged(false);
changeInfo.currentUpdateCounter = 0;
}
// Not all recipiens have been notified yet, decrement.
else {
changeInfo.currentUpdateCounter--;
}
return;
}
2020-12-03 13:00:04 +01:00
}
void LocalDataPoolManager::resetHkUpdateResetHelper() {
2021-01-11 22:45:04 +01:00
if(hkUpdateResetList == nullptr) {
return;
}
2020-12-03 13:00:04 +01:00
2021-01-11 22:45:04 +01:00
for(auto& changeInfo: *hkUpdateResetList) {
changeInfo.currentUpdateCounter = changeInfo.updateCounter;
}
2020-12-03 13:00:04 +01:00
}
2020-10-01 12:05:24 +02:00
ReturnValue_t LocalDataPoolManager::subscribeForPeriodicPacket(sid_t sid,
2021-01-11 22:45:04 +01:00
bool enableReporting, float collectionInterval, bool isDiagnostics,
object_id_t packetDestination) {
AcceptsHkPacketsIF* hkReceiverObject =
objectManager->get<AcceptsHkPacketsIF>(packetDestination);
if(hkReceiverObject == nullptr) {
printWarningOrError(sif::OutputTypes::OUT_WARNING,
2021-01-11 22:45:04 +01:00
"subscribeForPeriodicPacket", QUEUE_OR_DESTINATION_INVALID);
return QUEUE_OR_DESTINATION_INVALID;
}
struct HkReceiver hkReceiver;
hkReceiver.dataId.sid = sid;
hkReceiver.reportingType = ReportingType::PERIODIC;
hkReceiver.dataType = DataType::DATA_SET;
hkReceiver.destinationQueue = hkReceiverObject->getHkQueue();
2021-01-12 14:08:51 +01:00
LocalPoolDataSetBase* dataSet = HasLocalDpIFManagerAttorney::getDataSetHandle(owner, sid);
//LocalPoolDataSetBase* dataSet = owner->getDataSetHandle(sid);
2021-01-11 22:45:04 +01:00
if(dataSet != nullptr) {
2021-01-12 14:08:51 +01:00
LocalPoolDataSetAttorney::setReportingEnabled(*dataSet, enableReporting);
LocalPoolDataSetAttorney::setDiagnostic(*dataSet, isDiagnostics);
LocalPoolDataSetAttorney::initializePeriodicHelper(*dataSet, collectionInterval,
2021-01-11 22:45:04 +01:00
owner->getPeriodicOperationFrequency(), isDiagnostics);
}
hkReceiversMap.push_back(hkReceiver);
return HasReturnvaluesIF::RETURN_OK;
2020-12-03 13:00:04 +01:00
}
ReturnValue_t LocalDataPoolManager::subscribeForUpdatePackets(sid_t sid,
2021-01-11 22:45:04 +01:00
bool isDiagnostics, bool reportingEnabled,
object_id_t packetDestination) {
AcceptsHkPacketsIF* hkReceiverObject =
objectManager->get<AcceptsHkPacketsIF>(packetDestination);
if(hkReceiverObject == nullptr) {
printWarningOrError(sif::OutputTypes::OUT_WARNING,
2021-01-11 22:45:04 +01:00
"subscribeForPeriodicPacket", QUEUE_OR_DESTINATION_INVALID);
return QUEUE_OR_DESTINATION_INVALID;
}
struct HkReceiver hkReceiver;
hkReceiver.dataId.sid = sid;
hkReceiver.reportingType = ReportingType::UPDATE_HK;
hkReceiver.dataType = DataType::DATA_SET;
hkReceiver.destinationQueue = hkReceiverObject->getHkQueue();
2021-01-12 14:08:51 +01:00
LocalPoolDataSetBase* dataSet = HasLocalDpIFManagerAttorney::getDataSetHandle(owner, sid);
//LocalPoolDataSetBase* dataSet = owner->getDataSetHandle(sid);
2021-01-11 22:45:04 +01:00
if(dataSet != nullptr) {
2021-01-12 14:08:51 +01:00
LocalPoolDataSetAttorney::setReportingEnabled(*dataSet, true);
LocalPoolDataSetAttorney::setDiagnostic(*dataSet, isDiagnostics);
2021-01-11 22:45:04 +01:00
}
hkReceiversMap.push_back(hkReceiver);
handleHkUpdateResetListInsertion(hkReceiver.dataType, hkReceiver.dataId);
return HasReturnvaluesIF::RETURN_OK;
2020-12-03 13:00:04 +01:00
}
ReturnValue_t LocalDataPoolManager::subscribeForSetUpdateMessages(
2021-01-11 22:45:04 +01:00
const uint32_t setId, object_id_t destinationObject,
MessageQueueId_t targetQueueId, bool generateSnapshot) {
struct HkReceiver hkReceiver;
hkReceiver.dataType = DataType::DATA_SET;
2021-01-12 14:08:51 +01:00
hkReceiver.dataId.sid = sid_t(owner->getObjectId(), setId);
2021-01-11 22:45:04 +01:00
hkReceiver.destinationQueue = targetQueueId;
hkReceiver.objectId = destinationObject;
if(generateSnapshot) {
hkReceiver.reportingType = ReportingType::UPDATE_SNAPSHOT;
}
else {
hkReceiver.reportingType = ReportingType::UPDATE_NOTIFICATION;
}
hkReceiversMap.push_back(hkReceiver);
handleHkUpdateResetListInsertion(hkReceiver.dataType, hkReceiver.dataId);
return HasReturnvaluesIF::RETURN_OK;
2020-12-03 13:00:04 +01:00
}
ReturnValue_t LocalDataPoolManager::subscribeForVariableUpdateMessages(
2021-01-11 22:45:04 +01:00
const lp_id_t localPoolId, object_id_t destinationObject,
MessageQueueId_t targetQueueId, bool generateSnapshot) {
struct HkReceiver hkReceiver;
hkReceiver.dataType = DataType::LOCAL_POOL_VARIABLE;
hkReceiver.dataId.localPoolId = localPoolId;
hkReceiver.destinationQueue = targetQueueId;
hkReceiver.objectId = destinationObject;
if(generateSnapshot) {
hkReceiver.reportingType = ReportingType::UPDATE_SNAPSHOT;
}
else {
hkReceiver.reportingType = ReportingType::UPDATE_NOTIFICATION;
}
hkReceiversMap.push_back(hkReceiver);
handleHkUpdateResetListInsertion(hkReceiver.dataType, hkReceiver.dataId);
return HasReturnvaluesIF::RETURN_OK;
2020-12-03 13:00:04 +01:00
}
void LocalDataPoolManager::handleHkUpdateResetListInsertion(DataType dataType,
2021-01-11 22:45:04 +01:00
DataId dataId) {
if(hkUpdateResetList == nullptr) {
hkUpdateResetList = new std::vector<struct HkUpdateResetHelper>();
}
for(auto& updateResetStruct: *hkUpdateResetList) {
if(dataType == DataType::DATA_SET) {
if(updateResetStruct.dataId.sid == dataId.sid) {
updateResetStruct.updateCounter++;
updateResetStruct.currentUpdateCounter++;
return;
}
}
else {
if(updateResetStruct.dataId.localPoolId == dataId.localPoolId) {
updateResetStruct.updateCounter++;
updateResetStruct.currentUpdateCounter++;
return;
}
}
}
HkUpdateResetHelper hkUpdateResetHelper;
hkUpdateResetHelper.currentUpdateCounter = 1;
hkUpdateResetHelper.updateCounter = 1;
hkUpdateResetHelper.dataType = dataType;
if(dataType == DataType::DATA_SET) {
hkUpdateResetHelper.dataId.sid = dataId.sid;
}
else {
hkUpdateResetHelper.dataId.localPoolId = dataId.localPoolId;
}
hkUpdateResetList->push_back(hkUpdateResetHelper);
2020-10-01 12:05:24 +02:00
}
ReturnValue_t LocalDataPoolManager::handleHousekeepingMessage(
2021-01-11 22:45:04 +01:00
CommandMessage* message) {
Command_t command = message->getCommand();
sid_t sid = HousekeepingMessage::getSid(message);
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
switch(command) {
// Houskeeping interface handling.
case(HousekeepingMessage::ENABLE_PERIODIC_DIAGNOSTICS_GENERATION): {
result = togglePeriodicGeneration(sid, true, true);
break;
}
case(HousekeepingMessage::DISABLE_PERIODIC_DIAGNOSTICS_GENERATION): {
result = togglePeriodicGeneration(sid, false, true);
break;
}
case(HousekeepingMessage::ENABLE_PERIODIC_HK_REPORT_GENERATION): {
result = togglePeriodicGeneration(sid, true, false);
break;
}
case(HousekeepingMessage::DISABLE_PERIODIC_HK_REPORT_GENERATION): {
result = togglePeriodicGeneration(sid, false, false);
break;
}
case(HousekeepingMessage::REPORT_DIAGNOSTICS_REPORT_STRUCTURES): {
return generateSetStructurePacket(sid, true);
}
case(HousekeepingMessage::REPORT_HK_REPORT_STRUCTURES): {
return generateSetStructurePacket(sid, false);
}
case(HousekeepingMessage::MODIFY_DIAGNOSTICS_REPORT_COLLECTION_INTERVAL):
case(HousekeepingMessage::MODIFY_PARAMETER_REPORT_COLLECTION_INTERVAL): {
float newCollIntvl = 0;
HousekeepingMessage::getCollectionIntervalModificationCommand(message,
&newCollIntvl);
if(command == HousekeepingMessage::
MODIFY_DIAGNOSTICS_REPORT_COLLECTION_INTERVAL) {
result = changeCollectionInterval(sid, newCollIntvl, true);
}
else {
result = changeCollectionInterval(sid, newCollIntvl, false);
}
break;
}
case(HousekeepingMessage::GENERATE_ONE_PARAMETER_REPORT):
case(HousekeepingMessage::GENERATE_ONE_DIAGNOSTICS_REPORT): {
2021-01-12 14:08:51 +01:00
LocalPoolDataSetBase* dataSet =HasLocalDpIFManagerAttorney::getDataSetHandle(owner, sid);
//LocalPoolDataSetBase* dataSet = owner->getDataSetHandle(sid);
2021-01-11 22:45:04 +01:00
if(command == HousekeepingMessage::GENERATE_ONE_PARAMETER_REPORT
2021-01-12 14:08:51 +01:00
and LocalPoolDataSetAttorney::isDiagnostics(*dataSet)) {
2021-01-11 22:45:04 +01:00
return WRONG_HK_PACKET_TYPE;
}
else if(command == HousekeepingMessage::GENERATE_ONE_DIAGNOSTICS_REPORT
2021-01-12 14:08:51 +01:00
and not LocalPoolDataSetAttorney::isDiagnostics(*dataSet)) {
2021-01-11 22:45:04 +01:00
return WRONG_HK_PACKET_TYPE;
}
return generateHousekeepingPacket(HousekeepingMessage::getSid(message),
dataSet, true);
}
// Notification handling.
case(HousekeepingMessage::UPDATE_NOTIFICATION_SET): {
owner->handleChangedDataset(sid);
return HasReturnvaluesIF::RETURN_OK;
}
case(HousekeepingMessage::UPDATE_NOTIFICATION_VARIABLE): {
lp_id_t locPoolId = HousekeepingMessage::
getUpdateNotificationVariableCommand(message);
owner->handleChangedPoolVariable(locPoolId);
return HasReturnvaluesIF::RETURN_OK;
}
case(HousekeepingMessage::UPDATE_SNAPSHOT_SET): {
store_address_t storeId;
HousekeepingMessage::getUpdateSnapshotSetCommand(message, &storeId);
owner->handleChangedDataset(sid, storeId);
return HasReturnvaluesIF::RETURN_OK;
}
case(HousekeepingMessage::UPDATE_SNAPSHOT_VARIABLE): {
store_address_t storeId;
lp_id_t localPoolId = HousekeepingMessage::
getUpdateSnapshotVariableCommand(message, &storeId);
owner->handleChangedPoolVariable(localPoolId, storeId);
return HasReturnvaluesIF::RETURN_OK;
}
default:
return CommandMessageIF::UNKNOWN_COMMAND;
}
CommandMessage reply;
if(result != HasReturnvaluesIF::RETURN_OK) {
HousekeepingMessage::setHkRequestFailureReply(&reply, sid, result);
}
else {
HousekeepingMessage::setHkRequestSuccessReply(&reply, sid);
}
hkQueue->sendMessage(hkDestinationId, &reply);
return result;
2020-10-01 12:05:24 +02:00
}
ReturnValue_t LocalDataPoolManager::printPoolEntry(
2021-01-11 22:45:04 +01:00
lp_id_t localPoolId) {
auto poolIter = localPoolMap.find(localPoolId);
if (poolIter == localPoolMap.end()) {
printWarningOrError(sif::OutputTypes::OUT_WARNING, "printPoolEntry",
localpool::POOL_ENTRY_NOT_FOUND);
return localpool::POOL_ENTRY_NOT_FOUND;
2021-01-11 22:45:04 +01:00
}
poolIter->second->print();
return HasReturnvaluesIF::RETURN_OK;
2020-10-01 12:05:24 +02:00
}
MutexIF* LocalDataPoolManager::getMutexHandle() {
2021-01-11 22:45:04 +01:00
return mutex;
2020-10-01 12:05:24 +02:00
}
HasLocalDataPoolIF* LocalDataPoolManager::getOwner() {
2021-01-11 22:45:04 +01:00
return owner;
2020-10-01 12:05:24 +02:00
}
ReturnValue_t LocalDataPoolManager::generateHousekeepingPacket(sid_t sid,
2021-01-11 22:45:04 +01:00
LocalPoolDataSetBase* dataSet, bool forDownlink,
MessageQueueId_t destination) {
if(dataSet == nullptr) {
// Configuration error.
printWarningOrError(sif::OutputTypes::OUT_WARNING,
2021-01-11 22:45:04 +01:00
"generateHousekeepingPacket",
DATASET_NOT_FOUND);
return DATASET_NOT_FOUND;
}
store_address_t storeId;
HousekeepingPacketDownlink hkPacket(sid, dataSet);
size_t serializedSize = 0;
ReturnValue_t result = serializeHkPacketIntoStore(hkPacket, storeId,
forDownlink, &serializedSize);
if(result != HasReturnvaluesIF::RETURN_OK or serializedSize == 0) {
return result;
}
// and now we set a HK message and send it the HK packet destination.
CommandMessage hkMessage;
2021-01-12 14:08:51 +01:00
if(LocalPoolDataSetAttorney::isDiagnostics(*dataSet)) {
2021-01-11 22:45:04 +01:00
HousekeepingMessage::setHkDiagnosticsReply(&hkMessage, sid, storeId);
}
else {
HousekeepingMessage::setHkReportReply(&hkMessage, sid, storeId);
}
if(hkQueue == nullptr) {
// error, no queue available to send packet with.
printWarningOrError(sif::OutputTypes::OUT_WARNING,
2021-01-11 22:45:04 +01:00
"generateHousekeepingPacket",
QUEUE_OR_DESTINATION_INVALID);
return QUEUE_OR_DESTINATION_INVALID;
}
if(destination == MessageQueueIF::NO_QUEUE) {
if(hkDestinationId == MessageQueueIF::NO_QUEUE) {
// error, all destinations invalid
printWarningOrError(sif::OutputTypes::OUT_WARNING,
2021-01-11 22:45:04 +01:00
"generateHousekeepingPacket",
QUEUE_OR_DESTINATION_INVALID);
}
destination = hkDestinationId;
}
return hkQueue->sendMessage(destination, &hkMessage);
2020-10-01 12:05:24 +02:00
}
ReturnValue_t LocalDataPoolManager::serializeHkPacketIntoStore(
2021-01-11 22:45:04 +01:00
HousekeepingPacketDownlink& hkPacket,
store_address_t& storeId, bool forDownlink,
size_t* serializedSize) {
uint8_t* dataPtr = nullptr;
const size_t maxSize = hkPacket.getSerializedSize();
ReturnValue_t result = ipcStore->getFreeElement(&storeId,
maxSize, &dataPtr);
if(result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
if(forDownlink) {
return hkPacket.serialize(&dataPtr, serializedSize, maxSize,
SerializeIF::Endianness::BIG);
}
return hkPacket.serialize(&dataPtr, serializedSize, maxSize,
SerializeIF::Endianness::MACHINE);
2020-10-01 12:05:24 +02:00
}
void LocalDataPoolManager::setNonDiagnosticIntervalFactor(
2021-01-11 22:45:04 +01:00
uint8_t nonDiagInvlFactor) {
this->nonDiagnosticIntervalFactor = nonDiagInvlFactor;
2020-10-01 12:05:24 +02:00
}
void LocalDataPoolManager::performPeriodicHkGeneration(HkReceiver& receiver) {
2021-01-11 22:45:04 +01:00
sid_t sid = receiver.dataId.sid;
2021-01-12 14:08:51 +01:00
//LocalPoolDataSetBase* dataSet = owner->getDataSetHandle(sid);
LocalPoolDataSetBase* dataSet = HasLocalDpIFManagerAttorney::getDataSetHandle(owner, sid);
2021-01-11 22:45:04 +01:00
if(dataSet == nullptr) {
printWarningOrError(sif::OutputTypes::OUT_WARNING,
2021-01-11 22:45:04 +01:00
"performPeriodicHkGeneration",
DATASET_NOT_FOUND);
return;
}
2021-01-12 14:08:51 +01:00
if(not LocalPoolDataSetAttorney::getReportingEnabled(*dataSet)) {
2021-01-11 22:45:04 +01:00
return;
}
2021-01-12 14:08:51 +01:00
PeriodicHousekeepingHelper* periodicHelper =
LocalPoolDataSetAttorney::getPeriodicHelper(*dataSet);
if(periodicHelper == nullptr) {
2021-01-11 22:45:04 +01:00
// Configuration error.
return;
}
2021-01-12 14:08:51 +01:00
if(periodicHelper->checkOpNecessary()) {
2021-01-11 22:45:04 +01:00
return;
}
ReturnValue_t result = generateHousekeepingPacket(
sid, dataSet, true);
if(result != HasReturnvaluesIF::RETURN_OK) {
// configuration error
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2021-01-11 22:45:04 +01:00
sif::warning << "LocalDataPoolManager::performHkOperation: "
<< "HK generation failed." << std::endl;
2021-01-08 15:10:33 +01:00
#else
sif::printWarning("LocalDataPoolManager::performHkOperation: "
2021-01-11 22:45:04 +01:00
"HK generation failed.\n");
#endif
2021-01-11 22:45:04 +01:00
}
2020-10-01 12:05:24 +02:00
}
ReturnValue_t LocalDataPoolManager::togglePeriodicGeneration(sid_t sid,
2021-01-11 22:45:04 +01:00
bool enable, bool isDiagnostics) {
2021-01-12 14:08:51 +01:00
//LocalPoolDataSetBase* dataSet = owner->getDataSetHandle(sid);
LocalPoolDataSetBase* dataSet = HasLocalDpIFManagerAttorney::getDataSetHandle(owner, sid);
if((LocalPoolDataSetAttorney::isDiagnostics(*dataSet) and not isDiagnostics) or
(not LocalPoolDataSetAttorney::isDiagnostics(*dataSet) and isDiagnostics)) {
2021-01-11 22:45:04 +01:00
return WRONG_HK_PACKET_TYPE;
}
2021-01-12 14:08:51 +01:00
if((LocalPoolDataSetAttorney::getReportingEnabled(*dataSet) and enable) or
(not LocalPoolDataSetAttorney::getReportingEnabled(*dataSet) and not enable)) {
2021-01-11 22:45:04 +01:00
return REPORTING_STATUS_UNCHANGED;
}
2021-01-12 14:08:51 +01:00
LocalPoolDataSetAttorney::setReportingEnabled(*dataSet, enable);
2021-01-11 22:45:04 +01:00
return HasReturnvaluesIF::RETURN_OK;
2020-10-01 12:05:24 +02:00
}
ReturnValue_t LocalDataPoolManager::changeCollectionInterval(sid_t sid,
2021-01-11 22:45:04 +01:00
float newCollectionInterval, bool isDiagnostics) {
2021-01-12 14:08:51 +01:00
//LocalPoolDataSetBase* dataSet = owner->getDataSetHandle(sid);
LocalPoolDataSetBase* dataSet = HasLocalDpIFManagerAttorney::getDataSetHandle(owner, sid);
bool targetIsDiagnostics = LocalPoolDataSetAttorney::isDiagnostics(*dataSet);
2021-01-11 22:45:04 +01:00
if((targetIsDiagnostics and not isDiagnostics) or
(not targetIsDiagnostics and isDiagnostics)) {
return WRONG_HK_PACKET_TYPE;
}
2021-01-12 14:08:51 +01:00
PeriodicHousekeepingHelper* periodicHelper =
LocalPoolDataSetAttorney::getPeriodicHelper(*dataSet);
if(periodicHelper == nullptr) {
2021-01-11 22:45:04 +01:00
// config error
return PERIODIC_HELPER_INVALID;
}
2021-01-12 14:08:51 +01:00
periodicHelper->changeCollectionInterval(newCollectionInterval);
2021-01-11 22:45:04 +01:00
return HasReturnvaluesIF::RETURN_OK;
2020-10-01 12:05:24 +02:00
}
ReturnValue_t LocalDataPoolManager::generateSetStructurePacket(sid_t sid,
2021-01-11 22:45:04 +01:00
bool isDiagnostics) {
// Get and check dataset first.
2021-01-12 14:08:51 +01:00
//LocalPoolDataSetBase* dataSet = owner->getDataSetHandle(sid);
LocalPoolDataSetBase* dataSet = HasLocalDpIFManagerAttorney::getDataSetHandle(owner, sid);
2021-01-11 22:45:04 +01:00
if(dataSet == nullptr) {
printWarningOrError(sif::OutputTypes::OUT_WARNING,
2021-01-11 22:45:04 +01:00
"performPeriodicHkGeneration",
DATASET_NOT_FOUND);
return DATASET_NOT_FOUND;
}
2021-01-12 14:08:51 +01:00
bool targetIsDiagnostics = LocalPoolDataSetAttorney::isDiagnostics(*dataSet);
2021-01-11 22:45:04 +01:00
if((targetIsDiagnostics and not isDiagnostics) or
(not targetIsDiagnostics and isDiagnostics)) {
return WRONG_HK_PACKET_TYPE;
}
bool valid = dataSet->isValid();
2021-01-12 14:08:51 +01:00
bool reportingEnabled = LocalPoolDataSetAttorney::getReportingEnabled(*dataSet);
float collectionInterval = LocalPoolDataSetAttorney::getPeriodicHelper(*dataSet)->
getCollectionIntervalInSeconds();
2021-01-11 22:45:04 +01:00
// Generate set packet which can be serialized.
HousekeepingSetPacket setPacket(sid,
reportingEnabled, valid, collectionInterval, dataSet);
size_t expectedSize = setPacket.getSerializedSize();
uint8_t* storePtr = nullptr;
store_address_t storeId;
ReturnValue_t result = ipcStore->getFreeElement(&storeId,
expectedSize,&storePtr);
if(result != HasReturnvaluesIF::RETURN_OK) {
printWarningOrError(sif::OutputTypes::OUT_ERROR,
2021-01-11 22:45:04 +01:00
"generateSetStructurePacket", HasReturnvaluesIF::RETURN_FAILED,
2021-01-08 15:10:33 +01:00
"Could not get free element from IPC store.");
2021-01-11 22:45:04 +01:00
return result;
}
// Serialize set packet into store.
size_t size = 0;
result = setPacket.serialize(&storePtr, &size, expectedSize,
SerializeIF::Endianness::BIG);
if(expectedSize != size) {
printWarningOrError(sif::OutputTypes::OUT_WARNING,
2021-01-11 22:45:04 +01:00
"generateSetStructurePacket", HasReturnvaluesIF::RETURN_FAILED,
2021-01-08 15:10:33 +01:00
"Expected size is not equal to serialized size");
2021-01-11 22:45:04 +01:00
}
// Send structure reporting reply.
CommandMessage reply;
if(isDiagnostics) {
HousekeepingMessage::setDiagnosticsStuctureReportReply(&reply,
sid, storeId);
}
else {
HousekeepingMessage::setHkStuctureReportReply(&reply,
sid, storeId);
}
hkQueue->reply(&reply);
return result;
2020-10-01 12:05:24 +02:00
}
2021-01-08 15:10:33 +01:00
2021-01-10 14:54:05 +01:00
void LocalDataPoolManager::clearReceiversList() {
// clear the vector completely and releases allocated memory.
HkReceivers().swap(hkReceiversMap);
}
2021-01-12 14:08:51 +01:00
MutexIF* LocalDataPoolManager::getLocalPoolMutex() {
return this->mutex;
2021-01-12 00:13:49 +01:00
}
object_id_t LocalDataPoolManager::getCreatorObjectId() const {
2021-01-12 14:08:51 +01:00
return owner->getObjectId();
//return owner->getObjectId();
2021-01-12 00:13:49 +01:00
}
void LocalDataPoolManager::printWarningOrError(sif::OutputTypes outputType,
2021-01-08 15:10:33 +01:00
const char* functionName, ReturnValue_t error, const char* errorPrint) {
if(errorPrint == nullptr) {
if(error == DATASET_NOT_FOUND) {
errorPrint = "Dataset not found";
}
else if(error == POOLOBJECT_NOT_FOUND) {
errorPrint = "Pool Object not found";
}
else if(error == HasReturnvaluesIF::RETURN_FAILED) {
if(outputType == sif::OutputTypes::OUT_WARNING) {
2021-01-08 15:10:33 +01:00
errorPrint = "Generic Warning";
}
else {
errorPrint = "Generic error";
}
}
else if(error == QUEUE_OR_DESTINATION_INVALID) {
errorPrint = "Queue or destination not set";
}
else if(error == localpool::POOL_ENTRY_TYPE_CONFLICT) {
2021-01-08 15:15:02 +01:00
errorPrint = "Pool entry type conflict";
}
else if(error == localpool::POOL_ENTRY_NOT_FOUND) {
2021-01-08 15:15:02 +01:00
errorPrint = "Pool entry not found";
}
2021-01-08 15:10:33 +01:00
else {
errorPrint = "Unknown error";
}
}
if(outputType == sif::OutputTypes::OUT_WARNING) {
2021-01-08 15:10:33 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::warning << "LocalDataPoolManager::" << functionName
<< ": Object ID " << std::setw(8) << std::setfill('0')
2021-01-12 14:08:51 +01:00
<< std::hex << owner->getObjectId() << " | " << errorPrint
2021-01-08 15:10:33 +01:00
<< std::dec << std::setfill(' ') << std::endl;
#else
sif::printWarning("LocalDataPoolManager::%s: Object ID 0x%08x | %s\n",
2021-01-08 15:10:33 +01:00
owner->getObjectId(), errorPrint);
#endif
}
else if(outputType == sif::OutputTypes::OUT_ERROR) {
2021-01-08 15:10:33 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
sif::error << "LocalDataPoolManager::" << functionName
<< ": Object ID " << std::setw(8) << std::setfill('0')
2021-01-12 14:08:51 +01:00
<< std::hex << owner->getObjectId() << " | " << errorPrint
2021-01-08 15:10:33 +01:00
<< std::dec << std::setfill(' ') << std::endl;
#else
sif::printError("LocalDataPoolManager::%s: Object ID 0x%08x | %s\n",
2021-01-08 15:10:33 +01:00
owner->getObjectId(), errorPrint);
#endif
}
}
2021-01-12 14:08:51 +01:00
LocalDataPoolManager* LocalDataPoolManager::getHkManagerHandle() {
return this;
}