2020-08-13 20:53:35 +02:00
|
|
|
#include "HealthTable.h"
|
2020-10-29 17:52:28 +01:00
|
|
|
#include "../ipc/MutexHelper.h"
|
2020-08-13 20:53:35 +02:00
|
|
|
#include "../ipc/MutexFactory.h"
|
2020-10-29 17:52:28 +01:00
|
|
|
#include "../serialize/SerializeAdapter.h"
|
2016-06-15 23:48:41 +02:00
|
|
|
|
|
|
|
HealthTable::HealthTable(object_id_t objectid) :
|
|
|
|
SystemObject(objectid) {
|
2018-07-12 16:29:32 +02:00
|
|
|
mutex = MutexFactory::instance()->createMutex();;
|
2016-06-15 23:48:41 +02:00
|
|
|
|
|
|
|
mapIterator = healthMap.begin();
|
|
|
|
}
|
|
|
|
|
2020-11-02 15:00:01 +01:00
|
|
|
void HealthTable::setMutexTimeout(MutexIF::TimeoutType timeoutType,
|
|
|
|
uint32_t timeoutMs) {
|
|
|
|
this->timeoutType = timeoutType;
|
|
|
|
this->mutexTimeoutMs = timeoutMs;
|
|
|
|
}
|
|
|
|
|
2016-06-15 23:48:41 +02:00
|
|
|
HealthTable::~HealthTable() {
|
2018-07-12 16:29:32 +02:00
|
|
|
MutexFactory::instance()->deleteMutex(mutex);
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t HealthTable::registerObject(object_id_t object,
|
|
|
|
HasHealthIF::HealthState initilialState) {
|
|
|
|
if (healthMap.count(object) != 0) {
|
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
2020-10-29 17:52:28 +01:00
|
|
|
healthMap.emplace(object, initilialState);
|
2016-06-15 23:48:41 +02:00
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HealthTable::setHealth(object_id_t object,
|
|
|
|
HasHealthIF::HealthState newState) {
|
2021-03-09 11:25:13 +01:00
|
|
|
MutexGuard(mutex, timeoutType, mutexTimeoutMs);
|
2016-06-15 23:48:41 +02:00
|
|
|
HealthMap::iterator iter = healthMap.find(object);
|
|
|
|
if (iter != healthMap.end()) {
|
|
|
|
iter->second = newState;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HasHealthIF::HealthState HealthTable::getHealth(object_id_t object) {
|
|
|
|
HasHealthIF::HealthState state = HasHealthIF::HEALTHY;
|
2021-03-09 11:25:13 +01:00
|
|
|
MutexGuard(mutex, timeoutType, mutexTimeoutMs);
|
2016-06-15 23:48:41 +02:00
|
|
|
HealthMap::iterator iter = healthMap.find(object);
|
|
|
|
if (iter != healthMap.end()) {
|
|
|
|
state = iter->second;
|
|
|
|
}
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HealthTable::hasHealth(object_id_t object) {
|
2021-03-09 11:25:13 +01:00
|
|
|
MutexGuard(mutex, timeoutType, mutexTimeoutMs);
|
2016-06-15 23:48:41 +02:00
|
|
|
HealthMap::iterator iter = healthMap.find(object);
|
|
|
|
if (iter != healthMap.end()) {
|
2020-10-29 17:52:28 +01:00
|
|
|
return true;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
2020-10-29 17:52:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t HealthTable::getPrintSize() {
|
2021-03-09 11:25:13 +01:00
|
|
|
MutexGuard(mutex, timeoutType, mutexTimeoutMs);
|
2020-10-29 17:52:28 +01:00
|
|
|
uint32_t size = healthMap.size() * sizeof(object_id_t) +
|
|
|
|
sizeof(HasHealthIF::HealthState) + sizeof(uint16_t);
|
|
|
|
return size;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
2020-04-21 22:28:43 +02:00
|
|
|
void HealthTable::printAll(uint8_t* pointer, size_t maxSize) {
|
2021-03-09 11:25:13 +01:00
|
|
|
MutexGuard(mutex, timeoutType, mutexTimeoutMs);
|
2020-04-21 22:28:43 +02:00
|
|
|
size_t size = 0;
|
2016-06-15 23:48:41 +02:00
|
|
|
uint16_t count = healthMap.size();
|
2020-10-29 17:52:28 +01:00
|
|
|
SerializeAdapter::serialize(&count,
|
2020-04-21 22:28:43 +02:00
|
|
|
&pointer, &size, maxSize, SerializeIF::Endianness::BIG);
|
2020-10-29 17:52:28 +01:00
|
|
|
for (const auto& health: healthMap) {
|
|
|
|
SerializeAdapter::serialize(&health.first,
|
2020-04-21 22:28:43 +02:00
|
|
|
&pointer, &size, maxSize, SerializeIF::Endianness::BIG);
|
2020-10-29 17:52:28 +01:00
|
|
|
uint8_t healthValue = health.second;
|
|
|
|
SerializeAdapter::serialize(&healthValue, &pointer, &size,
|
2020-04-21 22:28:43 +02:00
|
|
|
maxSize, SerializeIF::Endianness::BIG);
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-29 17:52:28 +01:00
|
|
|
ReturnValue_t HealthTable::iterate(HealthEntry *value, bool reset) {
|
2016-06-15 23:48:41 +02:00
|
|
|
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
|
2021-03-09 11:25:13 +01:00
|
|
|
MutexGuard(mutex, timeoutType, mutexTimeoutMs);
|
2016-06-15 23:48:41 +02:00
|
|
|
if (reset) {
|
|
|
|
mapIterator = healthMap.begin();
|
|
|
|
}
|
|
|
|
if (mapIterator == healthMap.end()) {
|
|
|
|
result = HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
|
|
|
*value = *mapIterator;
|
|
|
|
mapIterator++;
|
|
|
|
return result;
|
|
|
|
}
|