2020-08-13 20:53:35 +02:00
|
|
|
#include "ObjectManager.h"
|
2021-01-27 20:04:16 +01:00
|
|
|
#include "../serviceinterface/ServiceInterface.h"
|
2021-01-03 14:08:40 +01:00
|
|
|
|
2021-01-03 14:16:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2020-06-10 20:51:59 +02:00
|
|
|
#include <iomanip>
|
2021-01-03 14:08:40 +01:00
|
|
|
#endif
|
2016-06-15 23:48:41 +02:00
|
|
|
#include <cstdlib>
|
|
|
|
|
2021-06-05 19:52:38 +02:00
|
|
|
ObjectManager* ObjectManager::objManagerInstance = nullptr;
|
|
|
|
|
|
|
|
ObjectManager* ObjectManager::instance() {
|
|
|
|
if(objManagerInstance == nullptr) {
|
|
|
|
objManagerInstance = new ObjectManager();
|
|
|
|
}
|
|
|
|
return objManagerInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectManager::setObjectFactoryFunction(produce_function_t objFactoryFunc, void *factoryArgs) {
|
|
|
|
this->objectFactoryFunction = objFactoryFunc;
|
|
|
|
this->factoryArgs = factoryArgs;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-05 19:52:38 +02:00
|
|
|
ObjectManager::ObjectManager() {}
|
|
|
|
|
|
|
|
|
2016-06-15 23:48:41 +02:00
|
|
|
ObjectManager::~ObjectManager() {
|
2020-06-04 21:25:14 +02:00
|
|
|
for (auto const& iter : objectList) {
|
|
|
|
delete iter.second;
|
|
|
|
}
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t ObjectManager::insert( object_id_t id, SystemObjectIF* object) {
|
2020-06-04 21:10:56 +02:00
|
|
|
auto returnPair = objectList.emplace(id, object);
|
|
|
|
if (returnPair.second) {
|
2021-01-03 14:16:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2020-04-23 19:13:18 +02:00
|
|
|
// sif::debug << "ObjectManager::insert: Object " << std::hex
|
|
|
|
// << (int)id << std::dec << " inserted." << std::endl;
|
2021-01-03 13:58:18 +01:00
|
|
|
#endif
|
2016-06-15 23:48:41 +02:00
|
|
|
return this->RETURN_OK;
|
|
|
|
} else {
|
2021-01-03 14:16:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2021-06-05 19:52:38 +02:00
|
|
|
sif::error << "ObjectManager::insert: Object ID " << std::hex <<
|
|
|
|
static_cast<uint32_t>(id) << std::dec << " is already in use!" << std::endl;
|
|
|
|
sif::error << "Terminating program" << std::endl;
|
|
|
|
#else
|
|
|
|
sif::printError("ObjectManager::insert: Object ID 0x%08x is already in use!\n",
|
|
|
|
static_cast<unsigned int>(id));
|
|
|
|
sif::printError("Terminating program");
|
2021-01-03 13:58:18 +01:00
|
|
|
#endif
|
2020-06-04 21:05:08 +02:00
|
|
|
//This is very severe and difficult to handle in other places.
|
|
|
|
std::exit(INSERTION_FAILED);
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t ObjectManager::remove( object_id_t id ) {
|
|
|
|
if ( this->getSystemObject(id) != NULL ) {
|
|
|
|
this->objectList.erase( id );
|
2021-01-03 14:16:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2020-06-07 13:59:22 +02:00
|
|
|
//sif::debug << "ObjectManager::removeObject: Object " << std::hex
|
|
|
|
// << (int)id << std::dec << " removed." << std::endl;
|
2021-01-03 13:58:18 +01:00
|
|
|
#endif
|
2016-06-15 23:48:41 +02:00
|
|
|
return RETURN_OK;
|
|
|
|
} else {
|
2021-01-03 14:16:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2020-04-23 19:13:18 +02:00
|
|
|
sif::error << "ObjectManager::removeObject: Requested object "
|
|
|
|
<< std::hex << (int)id << std::dec << " not found." << std::endl;
|
2021-01-03 13:58:18 +01:00
|
|
|
#endif
|
2016-06-15 23:48:41 +02:00
|
|
|
return NOT_FOUND;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SystemObjectIF* ObjectManager::getSystemObject( object_id_t id ) {
|
2020-06-04 21:10:56 +02:00
|
|
|
auto listIter = this->objectList.find( id );
|
|
|
|
if (listIter == this->objectList.end() ) {
|
|
|
|
return nullptr;
|
2016-06-15 23:48:41 +02:00
|
|
|
} else {
|
2020-06-04 21:10:56 +02:00
|
|
|
return listIter->second;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectManager::initialize() {
|
2021-06-05 19:52:38 +02:00
|
|
|
if(objectFactoryFunction == nullptr) {
|
2021-01-03 14:16:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2020-06-06 13:04:29 +02:00
|
|
|
sif::error << "ObjectManager::initialize: Passed produceObjects "
|
|
|
|
"functions is nullptr!" << std::endl;
|
2021-01-27 20:04:16 +01:00
|
|
|
#else
|
|
|
|
sif::printError("ObjectManager::initialize: Passed produceObjects functions is nullptr!\n");
|
2021-01-03 13:58:18 +01:00
|
|
|
#endif
|
2020-06-04 21:14:53 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-06-05 19:52:38 +02:00
|
|
|
objectFactoryFunction(factoryArgs);
|
2020-06-06 13:04:29 +02:00
|
|
|
ReturnValue_t result = RETURN_FAILED;
|
|
|
|
uint32_t errorCount = 0;
|
2020-06-04 21:26:56 +02:00
|
|
|
for (auto const& it : objectList) {
|
2020-06-06 13:04:29 +02:00
|
|
|
result = it.second->initialize();
|
|
|
|
if ( result != RETURN_OK ) {
|
2021-01-03 14:16:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2021-01-03 14:00:29 +01:00
|
|
|
object_id_t var = it.first;
|
2020-06-06 13:04:29 +02:00
|
|
|
sif::error << "ObjectManager::initialize: Object 0x" << std::hex <<
|
|
|
|
std::setw(8) << std::setfill('0')<< var << " failed to "
|
|
|
|
"initialize with code 0x" << result << std::dec <<
|
|
|
|
std::setfill(' ') << std::endl;
|
2021-01-03 13:58:18 +01:00
|
|
|
#endif
|
2020-06-06 13:04:29 +02:00
|
|
|
errorCount++;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-06 13:04:29 +02:00
|
|
|
if (errorCount > 0) {
|
2021-01-03 14:16:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2020-06-06 13:04:29 +02:00
|
|
|
sif::error << "ObjectManager::ObjectManager: Counted " << errorCount
|
2020-04-23 19:13:18 +02:00
|
|
|
<< " failed initializations." << std::endl;
|
2021-01-03 13:58:18 +01:00
|
|
|
#endif
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
//Init was successful. Now check successful interconnections.
|
2020-06-06 13:04:29 +02:00
|
|
|
errorCount = 0;
|
2020-06-04 21:26:56 +02:00
|
|
|
for (auto const& it : objectList) {
|
2020-06-06 13:04:29 +02:00
|
|
|
result = it.second->checkObjectConnections();
|
|
|
|
if ( result != RETURN_OK ) {
|
2021-01-03 14:16:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2020-06-06 13:04:29 +02:00
|
|
|
sif::error << "ObjectManager::ObjectManager: Object " << std::hex <<
|
|
|
|
(int) it.first << " connection check failed with code 0x"
|
|
|
|
<< result << std::dec << std::endl;
|
2021-01-03 13:58:18 +01:00
|
|
|
#endif
|
2020-06-06 13:04:29 +02:00
|
|
|
errorCount++;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-06 13:04:29 +02:00
|
|
|
if (errorCount > 0) {
|
2021-01-03 14:16:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2020-06-06 13:04:29 +02:00
|
|
|
sif::error << "ObjectManager::ObjectManager: Counted " << errorCount
|
2020-04-23 19:13:18 +02:00
|
|
|
<< " failed connection checks." << std::endl;
|
2021-01-03 13:58:18 +01:00
|
|
|
#endif
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectManager::printList() {
|
2021-01-03 14:16:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2020-04-23 19:13:18 +02:00
|
|
|
sif::debug << "ObjectManager: Object List contains:" << std::endl;
|
2020-06-04 21:25:14 +02:00
|
|
|
for (auto const& it : objectList) {
|
|
|
|
sif::debug << std::hex << it.first << " | " << it.second << std::endl;
|
2021-01-03 13:58:18 +01:00
|
|
|
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
2021-01-03 13:58:18 +01:00
|
|
|
#endif
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|