fsfw/src/fsfw/health/HealthTable.cpp

115 lines
3.5 KiB
C++
Raw Normal View History

2021-07-13 20:22:54 +02:00
#include "fsfw/health/HealthTable.h"
#include "fsfw/ipc/MutexFactory.h"
2022-02-02 10:29:30 +01:00
#include "fsfw/ipc/MutexGuard.h"
2021-07-13 20:22:54 +02:00
#include "fsfw/serialize/SerializeAdapter.h"
2022-02-02 10:29:30 +01:00
HealthTable::HealthTable(object_id_t objectid) : SystemObject(objectid) {
mutex = MutexFactory::instance()->createMutex();
mapIterator = healthMap.begin();
}
2022-02-02 10:29:30 +01:00
void HealthTable::setMutexTimeout(MutexIF::TimeoutType timeoutType, uint32_t timeoutMs) {
this->timeoutType = timeoutType;
this->mutexTimeoutMs = timeoutMs;
2020-11-02 15:00:01 +01:00
}
2022-02-02 10:29:30 +01:00
HealthTable::~HealthTable() { MutexFactory::instance()->deleteMutex(mutex); }
ReturnValue_t HealthTable::registerObject(object_id_t object,
2022-02-02 10:29:30 +01:00
HasHealthIF::HealthState initilialState) {
if (healthMap.count(object) != 0) {
2022-08-15 20:28:16 +02:00
return returnvalue::FAILED;
2022-02-02 10:29:30 +01:00
}
healthMap.emplace(object, initilialState);
2022-08-15 20:28:16 +02:00
return returnvalue::OK;
}
2022-06-05 12:52:55 +02:00
ReturnValue_t HealthTable::removeObject(object_id_t object) {
2022-06-06 11:55:42 +02:00
mapIterator = healthMap.find(object);
if (mapIterator == healthMap.end()) {
2022-08-15 20:28:16 +02:00
return returnvalue::FAILED;
2022-06-06 11:55:42 +02:00
}
2022-06-05 12:52:55 +02:00
healthMap.erase(mapIterator);
2022-08-15 20:28:16 +02:00
return returnvalue::OK;
2022-06-05 12:52:55 +02:00
}
2022-02-02 10:29:30 +01:00
void HealthTable::setHealth(object_id_t object, HasHealthIF::HealthState newState) {
MutexGuard(mutex, timeoutType, mutexTimeoutMs);
HealthMap::iterator iter = healthMap.find(object);
if (iter != healthMap.end()) {
iter->second = newState;
}
}
HasHealthIF::HealthState HealthTable::getHealth(object_id_t object) {
2022-02-02 10:29:30 +01:00
HasHealthIF::HealthState state = HasHealthIF::HEALTHY;
MutexGuard(mutex, timeoutType, mutexTimeoutMs);
HealthMap::iterator iter = healthMap.find(object);
if (iter != healthMap.end()) {
state = iter->second;
}
return state;
}
bool HealthTable::hasHealth(object_id_t object) {
2022-02-02 10:29:30 +01:00
MutexGuard(mutex, timeoutType, mutexTimeoutMs);
HealthMap::iterator iter = healthMap.find(object);
if (iter != healthMap.end()) {
return true;
}
return false;
2020-10-29 17:52:28 +01:00
}
size_t HealthTable::getPrintSize() {
2022-02-02 10:29:30 +01:00
MutexGuard(mutex, timeoutType, mutexTimeoutMs);
uint32_t size =
healthMap.size() * sizeof(object_id_t) + sizeof(HasHealthIF::HealthState) + sizeof(uint16_t);
return size;
}
void HealthTable::printAll(uint8_t* pointer, size_t maxSize) {
2022-02-02 10:29:30 +01:00
MutexGuard(mutex, timeoutType, mutexTimeoutMs);
size_t size = 0;
uint16_t count = healthMap.size();
ReturnValue_t result =
SerializeAdapter::serialize(&count, &pointer, &size, maxSize, SerializeIF::Endianness::BIG);
2022-08-15 20:28:16 +02:00
if (result != returnvalue::OK) {
2021-04-11 21:54:48 +02:00
#if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::warning << "HealthTable::printAll: Serialization of health table failed" << std::endl;
2021-04-11 21:54:48 +02:00
#else
2022-02-02 10:29:30 +01:00
sif::printWarning("HealthTable::printAll: Serialization of health table failed\n");
2021-04-11 21:54:48 +02:00
#endif
#endif /* FSFW_VERBOSE_LEVEL >= 1 */
2022-02-02 10:29:30 +01:00
return;
}
for (const auto& health : healthMap) {
result = SerializeAdapter::serialize(&health.first, &pointer, &size, maxSize,
SerializeIF::Endianness::BIG);
2022-08-15 20:28:16 +02:00
if (result != returnvalue::OK) {
2022-02-02 10:29:30 +01:00
return;
}
uint8_t healthValue = health.second;
result = SerializeAdapter::serialize(&healthValue, &pointer, &size, maxSize,
SerializeIF::Endianness::BIG);
2022-08-15 20:28:16 +02:00
if (result != returnvalue::OK) {
2022-02-02 10:29:30 +01:00
return;
}
}
}
2022-02-02 10:29:30 +01:00
ReturnValue_t HealthTable::iterate(HealthEntry* value, bool reset) {
2022-08-15 20:28:16 +02:00
ReturnValue_t result = returnvalue::OK;
2022-02-02 10:29:30 +01:00
MutexGuard(mutex, timeoutType, mutexTimeoutMs);
if (reset) {
mapIterator = healthMap.begin();
}
if (mapIterator == healthMap.end()) {
2022-08-15 20:28:16 +02:00
return returnvalue::FAILED;
2022-02-02 10:29:30 +01:00
}
*value = *mapIterator;
mapIterator++;
return result;
}