2016-06-15 23:48:41 +02:00
|
|
|
#include <framework/health/HealthTable.h>
|
|
|
|
#include <framework/serialize/SerializeAdapter.h>
|
2018-07-12 16:29:32 +02:00
|
|
|
#include <framework/ipc/MutexFactory.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();
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
healthMap.insert(
|
|
|
|
std::pair<object_id_t, HasHealthIF::HealthState>(object,
|
|
|
|
initilialState));
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HealthTable::setHealth(object_id_t object,
|
|
|
|
HasHealthIF::HealthState newState) {
|
2018-07-12 16:29:32 +02:00
|
|
|
mutex->lockMutex(MutexIF::NO_TIMEOUT);
|
2016-06-15 23:48:41 +02:00
|
|
|
HealthMap::iterator iter = healthMap.find(object);
|
|
|
|
if (iter != healthMap.end()) {
|
|
|
|
iter->second = newState;
|
|
|
|
}
|
2018-07-12 16:29:32 +02:00
|
|
|
mutex->unlockMutex();
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
HasHealthIF::HealthState HealthTable::getHealth(object_id_t object) {
|
|
|
|
HasHealthIF::HealthState state = HasHealthIF::HEALTHY;
|
2018-07-12 16:29:32 +02:00
|
|
|
mutex->lockMutex(MutexIF::NO_TIMEOUT);
|
2016-06-15 23:48:41 +02:00
|
|
|
HealthMap::iterator iter = healthMap.find(object);
|
|
|
|
if (iter != healthMap.end()) {
|
|
|
|
state = iter->second;
|
|
|
|
}
|
2018-07-12 16:29:32 +02:00
|
|
|
mutex->unlockMutex();
|
2016-06-15 23:48:41 +02:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t HealthTable::getPrintSize() {
|
2018-07-12 16:29:32 +02:00
|
|
|
mutex->lockMutex(MutexIF::NO_TIMEOUT);
|
2016-06-15 23:48:41 +02:00
|
|
|
uint32_t size = healthMap.size() * 5 + 2;
|
2018-07-12 16:29:32 +02:00
|
|
|
mutex->unlockMutex();
|
2016-06-15 23:48:41 +02:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HealthTable::hasHealth(object_id_t object) {
|
|
|
|
bool exits = false;
|
2018-07-12 16:29:32 +02:00
|
|
|
mutex->lockMutex(MutexIF::NO_TIMEOUT);
|
2016-06-15 23:48:41 +02:00
|
|
|
HealthMap::iterator iter = healthMap.find(object);
|
|
|
|
if (iter != healthMap.end()) {
|
|
|
|
exits = true;
|
|
|
|
}
|
2018-07-12 16:29:32 +02:00
|
|
|
mutex->unlockMutex();
|
2016-06-15 23:48:41 +02:00
|
|
|
return exits;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HealthTable::printAll(uint8_t* pointer, uint32_t maxSize) {
|
2018-07-12 16:29:32 +02:00
|
|
|
mutex->lockMutex(MutexIF::NO_TIMEOUT);
|
2016-06-15 23:48:41 +02:00
|
|
|
uint32_t size = 0;
|
|
|
|
uint16_t count = healthMap.size();
|
|
|
|
ReturnValue_t result = SerializeAdapter<uint16_t>::serialize(&count,
|
|
|
|
&pointer, &size, maxSize, true);
|
|
|
|
HealthMap::iterator iter;
|
|
|
|
for (iter = healthMap.begin();
|
|
|
|
iter != healthMap.end() && result == HasReturnvaluesIF::RETURN_OK;
|
|
|
|
++iter) {
|
|
|
|
result = SerializeAdapter<object_id_t>::serialize(&iter->first,
|
|
|
|
&pointer, &size, maxSize, true);
|
|
|
|
uint8_t health = iter->second;
|
|
|
|
result = SerializeAdapter<uint8_t>::serialize(&health, &pointer, &size,
|
|
|
|
maxSize, true);
|
|
|
|
}
|
2018-07-12 16:29:32 +02:00
|
|
|
mutex->unlockMutex();
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t HealthTable::iterate(
|
|
|
|
std::pair<object_id_t, HasHealthIF::HealthState> *value, bool reset) {
|
|
|
|
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
|
2018-07-12 16:29:32 +02:00
|
|
|
mutex->lockMutex(MutexIF::NO_TIMEOUT);
|
2016-06-15 23:48:41 +02:00
|
|
|
if (reset) {
|
|
|
|
mapIterator = healthMap.begin();
|
|
|
|
}
|
|
|
|
if (mapIterator == healthMap.end()) {
|
|
|
|
result = HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
|
|
|
*value = *mapIterator;
|
|
|
|
mapIterator++;
|
2018-07-12 16:29:32 +02:00
|
|
|
mutex->unlockMutex();
|
2016-06-15 23:48:41 +02:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|