fsfw/src/fsfw/datapool/Dataset.cpp

145 lines
3.2 KiB
C++

#include "Dataset.h"
#include <fsfw/returnvalues/returnvalue.h>
#include <fsfw/ipc/MutexFactory.h>
#include <fsfw/objectmanager/ObjectManager.h>
#include "HasDatapoolIF.h"
#ifdef FSFW_INTROSPECTION
Dataset::Dataset(HasDatapoolIF* owner, bool allowUserCommit)
: allocated(true), allowUserCommit(allowUserCommit) {
this->owner.pointer = owner;
mutex = MutexFactory::instance()->createMutex();
}
Dataset::Dataset(uint32_t owner_id )
: allocated(false), allowUserCommit(false) {
this->owner.id = owner_id;
}
void Dataset::setEnum(EnumIF *theEnum) {
id = theEnum->getValue();
description = theEnum->getDescription();
}
#else
Dataset::Dataset(HasDatapoolIF* owner, DataSetId_t id, bool allowUserCommit)
: allocated(true), allowUserCommit(allowUserCommit), id(id) {
this->owner.pointer = owner;
mutex = MutexFactory::instance()->createMutex();
}
Dataset::Dataset(uint32_t owner_id, DataSetId_t id): id(id) { this->owner.id = owner_id; }
#endif
Dataset::~Dataset() { MutexFactory::instance()->deleteMutex(mutex); }
void Dataset::commit() {
if ((!allocated) && (!allowUserCommit)) {
return;
}
lock();
for (auto variable : variables) {
variable->commit();
}
unlock();
}
void Dataset::commit(bool valid) {
if ((!allocated) && (!allowUserCommit)) {
return;
}
setAllValid(valid);
commit();
}
void Dataset::setAllValid(bool valid) {
if ((!allocated) && (!allowUserCommit)) {
return;
}
for (auto variable : variables) {
variable->setValid(valid);
}
}
void Dataset::read() {
lock();
for (auto variable : variables) {
variable->read();
}
unlock();
}
bool Dataset::hasChanged() {
bool changed = hasChangedNoRead();
read();
return changed;
}
bool Dataset::hasChangedOrOlderThan(uint32_t seconds) {
bool changed = hasChanged();
//TODO time
read();
return changed;
}
uint8_t Dataset::getId() const {
return id;
}
const std::vector<DatasetEntryIF*>* Dataset::getVariables() const { return &variables; }
ReturnValue_t Dataset::initialize() {
if (allocated) {
//nothing to do
return returnvalue::OK;
}
HasDatapoolIF* actualOwner = ObjectManager::instance()->get<HasDatapoolIF>(owner.id);
if (actualOwner == nullptr) {
return returnvalue::FAILED;
}
const Dataset* theOtherSet = actualOwner->getDatapoolHelper()->getDataSet(this->id);
if (theOtherSet == nullptr) {
return returnvalue::FAILED;
}
if (theOtherSet->variables.size() != variables.size()) {
return returnvalue::FAILED;
}
this->mutex = theOtherSet->mutex;
this->allowUserCommit = theOtherSet->allowUserCommit;
for (size_t i = 0; i < variables.size(); i++) {
variables[i]->connect(theOtherSet->variables[i]);
}
return returnvalue::OK;
}
// operator[]
#ifdef FSFW_INTROSPECTION
const char* Dataset::getDescription() const { return description; }
#endif
bool Dataset::registerEntry(DatasetEntryIF* entry) {
variables.push_back(entry);
return allocated;
}
void Dataset::lock() { mutex->lockMutex(MutexIF::TimeoutType::BLOCKING); }
void Dataset::unlock() { mutex->unlockMutex(); }
bool Dataset::hasChangedNoRead(){
bool changed = false;
for (auto variable: variables){
if (variable->changed()){
changed = true;
break;
}
}
return changed;
}