fsfw/src/fsfw/datapool/Dataset.cpp

141 lines
3.3 KiB
C++

#include "Dataset.h"
#include <fsfw/ipc/MutexFactory.h>
#include <fsfw/objectmanager/ObjectManager.h>
#include <fsfw/returnvalues/returnvalue.h>
//TODO for non allocated: detect if initialized, and if not, call it
#include "HasDatapoolIF.h"
#ifdef FSFW_INTROSPECTION
Dataset::Dataset(HasDatapoolIF* owner, const EnumIF &id, bool allowUserCommit)
: HousekeepingSet(owner, id), allocated(true), allowUserCommit(allowUserCommit) {
this->owner.pointer = owner;
owner->getDatapoolHelper()->registerSet(this);
mutex = MutexFactory::instance()->createMutex();
}
Dataset::Dataset(uint32_t owner_id, const EnumIF &id)
: HousekeepingSet(nullptr, id), allocated(false), allowUserCommit(false) {
this->owner.id = owner_id;
}
#else
Dataset::Dataset(HasDatapoolIF* owner, HousekeepingSetId_t id, bool allowUserCommit)
: HousekeepingSet(owner, id), allocated(true), allowUserCommit(allowUserCommit) {
this->owner.pointer = owner;
owner->getDatapoolHelper()->registerSet(this);
mutex = MutexFactory::instance()->createMutex();
}
Dataset::Dataset(uint32_t owner_id, HousekeepingSetId_t id) : HousekeepingSet(nullptr, 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;
}
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) {
puts("owner type");
return returnvalue::FAILED;
}
const Dataset* theOtherSet = actualOwner->getDatapoolHelper()->getDataSet(this->id);
if (theOtherSet == nullptr) {
puts("no set");
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[]
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;
}