Update sdv_packager (#6)

This commit is contained in:
tompzf
2026-03-27 14:12:49 +01:00
committed by GitHub
parent 234be8917f
commit aefefd52f7
717 changed files with 42252 additions and 11334 deletions

View File

@@ -1,34 +1,36 @@
/********************************************************************************
* Copyright (c) 2025-2026 ZF Friedrichshafen AG
*
* This program and the accompanying materials are made available under the
* terms of the Apache License Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Erik Verhoeven - initial API and implementation
********************************************************************************/
#include "repository.h"
#include "support/interface_ptr.h"
#include "sdv_core.h"
#include <iostream>
#include <cassert>
#include <algorithm>
#include "object_lifetime_control.h"
#include "../../global/base64.h"
#include "module_control.h"
#include "app_config.h"
#include "app_control.h"
#include "app_settings.h"
/**
* @brief Get the object type string
* @param[in] eType The object type to get the string from.
* @return The object type string.
*/
std::string GetObjectTypeString(sdv::EObjectType eType)
// GetRepository might be redirected for unit tests.
#ifndef GetRepository
CRepository& GetRepository()
{
std::string ssType;
switch (eType)
{
case sdv::EObjectType::SystemObject: ssType = "system object"; break;
case sdv::EObjectType::Device: ssType = "device"; break;
case sdv::EObjectType::BasicService: ssType = "basic_service"; break;
case sdv::EObjectType::ComplexService: ssType = "complex_service"; break;
case sdv::EObjectType::Application: ssType = "application"; break;
case sdv::EObjectType::Proxy: ssType = "proxy"; break;
case sdv::EObjectType::Stub: ssType = "stub"; break;
case sdv::EObjectType::Utility: ssType = "utility"; break;
default: ssType = "unknown"; break;
}
return ssType;
static CRepository repository;
return repository;
}
#endif // !defined GetRepository
void CRepository::SetConfigMode()
{
@@ -66,8 +68,8 @@ sdv::IInterfaceAccess* CRepository::GetObject(const sdv::u8string& ssObjectName)
// In case the object is not in the service map and this is a main or isolated application, create the object if the object is
// known in the installation manifest and is a system object.
auto optManifest = GetAppConfig().FindInstalledComponent(ssObjectName);
if (optManifest && optManifest->eType == sdv::EObjectType::SystemObject)
return GetObjectByID(CreateObject(optManifest->ssClassName, optManifest->ssDefaultObjectName, ""));
if (optManifest && optManifest->eType == sdv::EObjectType::system_object)
return GetObjectByID(CreateObject(optManifest->ssName, optManifest->ssDefaultObjectName, ""));
// Forward the request to core repository if one is linked here (this can only occur with isolated and external applications).
if (!m_ptrCoreRepoAccess) return nullptr;
@@ -114,10 +116,10 @@ sdv::IInterfaceAccess* CRepository::CreateUtility(/*in*/ const sdv::u8string& ss
SDV_LOG_ERROR("Object creation requested but object class was not found \"", ssClassName, "\"!");
return nullptr;
}
if (optClassInfo->eType != sdv::EObjectType::Utility)
if (optClassInfo->eType != sdv::EObjectType::utility)
{
// Utilities and marshall objects cannot be created using the CreateObject function.
SDV_LOG_ERROR("Creation of an utility of invalid type \"", GetObjectTypeString(optClassInfo->eType),
SDV_LOG_ERROR("Creation of an utility of invalid type \"", sdv::ObjectType2String(optClassInfo->eType),
"\" requested for class \"", ssClassName, "\"!");
return nullptr;
}
@@ -147,7 +149,7 @@ sdv::IInterfaceAccess* CRepository::CreateUtility(/*in*/ const sdv::u8string& ss
lock.unlock();
// Print info
if (GetAppControl().IsConsoleVerbose())
if (GetAppSettings().IsConsoleVerbose())
std::cout << "Creating a utility #" << ptrObjectEntry->tObjectID << " of type " << ptrObjectEntry->ssName << std::endl;
// Create the object
@@ -160,7 +162,7 @@ sdv::IInterfaceAccess* CRepository::CreateUtility(/*in*/ const sdv::u8string& ss
if (pObjectControl)
{
pObjectControl->Initialize(ssObjectConfig);
if (pObjectControl->GetStatus() != sdv::EObjectStatus::running)
if (pObjectControl->GetObjectState() != sdv::EObjectState::initialized)
{
// Destroy the object
ptrModule->DestroyObject(ptrObject);
@@ -222,10 +224,10 @@ sdv::IInterfaceAccess* CRepository::CreateProxyObject(/*in*/ sdv::interface_id i
SDV_LOG_ERROR("Object creation requested but object class was not found \"", ssClassName, "\"!");
return nullptr;
}
if (optClassInfo->eType != sdv::EObjectType::Proxy)
if (optClassInfo->eType != sdv::EObjectType::proxy)
{
// Utilities and marshall objects cannot be created using the CreateObject function.
SDV_LOG_ERROR("Creation of an object of invalid type \"", GetObjectTypeString(optClassInfo->eType),
SDV_LOG_ERROR("Creation of an object of invalid type \"", sdv::ObjectType2String(optClassInfo->eType),
"\" requested for class \"", ssClassName, "\"!");
return nullptr;
}
@@ -255,7 +257,7 @@ sdv::IInterfaceAccess* CRepository::CreateProxyObject(/*in*/ sdv::interface_id i
lock.unlock();
// Print info
if (GetAppControl().IsConsoleVerbose())
if (GetAppSettings().IsConsoleVerbose())
std::cout << "Creating a proxy object #" << ptrObjectEntry->tObjectID << " of type " <<
ptrObjectEntry->ssName << std::endl;
@@ -269,7 +271,7 @@ sdv::IInterfaceAccess* CRepository::CreateProxyObject(/*in*/ sdv::interface_id i
if (pObjectControl)
{
pObjectControl->Initialize("");
if (pObjectControl->GetStatus() != sdv::EObjectStatus::running)
if (pObjectControl->GetObjectState() != sdv::EObjectState::initialized)
{
// Destroy the object
ptrModule->DestroyObject(ptrObject);
@@ -331,10 +333,10 @@ sdv::IInterfaceAccess* CRepository::CreateStubObject(/*in*/ sdv::interface_id id
SDV_LOG_ERROR("Object creation requested but object class was not found \"", ssClassName, "\"!");
return nullptr;
}
if (optClassInfo->eType != sdv::EObjectType::Stub)
if (optClassInfo->eType != sdv::EObjectType::stub)
{
// Utilities and marshall objects cannot be created using the CreateObject function.
SDV_LOG_ERROR("Creation of an object of invalid type \"", GetObjectTypeString(optClassInfo->eType),
SDV_LOG_ERROR("Creation of an object of invalid type \"", sdv::ObjectType2String(optClassInfo->eType),
"\" requested for class \"", ssClassName, "\"!");
return nullptr;
}
@@ -364,7 +366,7 @@ sdv::IInterfaceAccess* CRepository::CreateStubObject(/*in*/ sdv::interface_id id
lock.unlock();
// Print info
if (GetAppControl().IsConsoleVerbose())
if (GetAppSettings().IsConsoleVerbose())
std::cout << "Creating a stub #" << ptrObjectEntry->tObjectID << " of type " << ptrObjectEntry->ssName << std::endl;
// Create the object
@@ -377,7 +379,7 @@ sdv::IInterfaceAccess* CRepository::CreateStubObject(/*in*/ sdv::interface_id id
if (pObjectControl)
{
pObjectControl->Initialize("");
if (pObjectControl->GetStatus() != sdv::EObjectStatus::running)
if (pObjectControl->GetObjectState() != sdv::EObjectState::initialized)
{
// Destroy the object
ptrModule->DestroyObject(ptrObject);
@@ -436,6 +438,9 @@ sdv::core::TObjectID CRepository::CreateObject2(const sdv::u8string& ssClassName
return false;
}
// TODO: Request the parameters from the class info and create a config with them. Then add the parameters from the supplied
// config and overwrite existing and extend non-existing parameters. Then create a new config string.
// Check the class type
auto optClassInfo = ptrModule->GetClassInfo(ssClassName);
if (!optClassInfo)
@@ -445,22 +450,27 @@ sdv::core::TObjectID CRepository::CreateObject2(const sdv::u8string& ssClassName
}
bool bError = false; // Creation not allowed
bool bIsolate = false; // Creation should be isolated
bool bDeviceAndBasicServiceAllowed = GetAppControl().IsMainApplication() || GetAppControl().IsStandaloneApplication() ||
GetAppControl().IsEssentialApplication();
bool bComplexServiceAllowed = !GetAppControl().IsMaintenanceApplication() &&
((!GetAppControl().IsIsolatedApplication() && !GetAppControl().IsExternalApplication()) || !m_bIsoObjectLoaded);
bool bDeviceAndBasicServiceAllowed = GetAppSettings().IsMainApplication() || GetAppSettings().IsStandaloneApplication() ||
GetAppSettings().IsEssentialApplication();
bool bComplexServiceAllowed = !GetAppSettings().IsMaintenanceApplication() &&
((!GetAppSettings().IsIsolatedApplication() && !GetAppSettings().IsExternalApplication()) || !m_bIsoObjectLoaded);
switch (optClassInfo->eType)
{
case sdv::EObjectType::SystemObject:
case sdv::EObjectType::system_object:
break;
case sdv::EObjectType::Device:
case sdv::EObjectType::device:
case sdv::EObjectType::platform_abstraction:
case sdv::EObjectType::vehicle_bus:
bError = !bDeviceAndBasicServiceAllowed;
break;
case sdv::EObjectType::BasicService:
case sdv::EObjectType::basic_service:
case sdv::EObjectType::sensor:
case sdv::EObjectType::actuator:
bError = !bDeviceAndBasicServiceAllowed;
break;
case sdv::EObjectType::ComplexService:
bIsolate = GetAppControl().IsMainApplication();
case sdv::EObjectType::complex_service:
case sdv::EObjectType::vehicle_function:
bIsolate = GetAppSettings().IsMainApplication();
bError = !bComplexServiceAllowed;
m_bIsoObjectLoaded = true;
break;
@@ -471,17 +481,69 @@ sdv::core::TObjectID CRepository::CreateObject2(const sdv::u8string& ssClassName
if (bError)
{
// Utilities and marshall objects cannot be created using the CreateObject function.
SDV_LOG_ERROR("Creation of an object of invalid/unallowed type \"", GetObjectTypeString(optClassInfo->eType),
SDV_LOG_ERROR("Creation of an object of invalid/unallowed type \"", sdv::ObjectType2String(optClassInfo->eType),
"\" requested for class \"", ssClassName, "\"!");
return 0;
}
// Check for an object name. If not existing get the default name (being either one specified by the object or the class name).
std::shared_lock<std::shared_mutex> lock(m_mtxObjects);
std::string ssObjectName2 = ssObjectName;
if (ssObjectName2.empty())
ssObjectName2 = optClassInfo->ssDefaultObjectName;
if (ssObjectName2.empty())
ssObjectName2 = ssClassName;
// Check for an object with the same name.
auto itPreviousService = m_mapServiceObjects.find(ssObjectName2);
if (itPreviousService != m_mapServiceObjects.end())
{
// Object entry is valid?
if (!*itPreviousService->second)
{
// This should not occur... there is a previous object in the service map, but the object is empty.
SDV_LOG_ERROR("Object creation requested for class \"", ssClassName, "\", but object with the same name \"",
ssObjectName2, "\" was already instantiated, but cannot be found!");
return 0;
}
// Trying to create an object with the same name is not an error if the classes are identical.
if ((*itPreviousService->second)->sClassInfo.ssName == ssClassName)
return (*itPreviousService->second)->tObjectID;
// Object name was already used by another class. This is an error.
SDV_LOG_ERROR("Object creation requested for class \"", ssClassName, "\", but object with the same name \"",
ssObjectName2, "\" was already instantiated for class \"", (*itPreviousService->second)->sClassInfo.ssName,
"\"!");
return 0;
}
// Check with singleton objects if the object was already instantiated.
if (optClassInfo->uiFlags & static_cast<uint32_t>(sdv::EObjectFlags::singleton))
{
for (const auto& rprService : m_mapServiceObjects)
{
if (!*rprService.second)
continue;
if ((*rprService.second)->ptrModule && (*rprService.second)->ptrModule->GetModuleID() == ptrModule->GetModuleID()
&& (*rprService.second)->sClassInfo.ssName == ssClassName)
{
SDV_LOG_ERROR("Object creation requested but object from the same class \"", ssClassName,
"\" was already instantiated and only one instance is allowed!");
return 0;
}
}
}
// Unlock; the next function will lock again.
lock.unlock();
// Create an isolated object.
if (bIsolate)
return CreateIsolatedObject(*optClassInfo, ssObjectName, ssObjectConfig);
return CreateIsolatedObject(*optClassInfo, ssObjectName2, ssObjectConfig);
// Create an internal object.
return InternalCreateObject(ptrModule, *optClassInfo, ssObjectName, ssObjectConfig);
return InternalCreateObject(ptrModule, *optClassInfo, ssObjectName2, ssObjectConfig);
}
sdv::core::TObjectID CRepository::CreateObjectFromModule(/*in*/ sdv::core::TModuleID tModuleID,
@@ -505,25 +567,30 @@ sdv::core::TObjectID CRepository::CreateObjectFromModule(/*in*/ sdv::core::TModu
return false;
}
bool bError = false;
bool bDeviceAndBasicServiceAllowed = GetAppControl().IsMainApplication() || GetAppControl().IsStandaloneApplication() ||
GetAppControl().IsEssentialApplication();
bool bComplexServiceAllowed = !GetAppControl().IsMaintenanceApplication() &&
((!GetAppControl().IsIsolatedApplication() && !GetAppControl().IsExternalApplication()) || !m_bIsoObjectLoaded);
bool bDeviceAndBasicServiceAllowed = GetAppSettings().IsMainApplication() || GetAppSettings().IsStandaloneApplication() ||
GetAppSettings().IsEssentialApplication();
bool bComplexServiceAllowed = !GetAppSettings().IsMaintenanceApplication() &&
((!GetAppSettings().IsIsolatedApplication() && !GetAppSettings().IsExternalApplication()) || !m_bIsoObjectLoaded);
switch (optClassInfo->eType)
{
case sdv::EObjectType::SystemObject:
case sdv::EObjectType::system_object:
break;
case sdv::EObjectType::Device:
case sdv::EObjectType::device:
case sdv::EObjectType::platform_abstraction:
case sdv::EObjectType::vehicle_bus:
// Allowed?
bError = !bDeviceAndBasicServiceAllowed;
break;
case sdv::EObjectType::BasicService:
case sdv::EObjectType::basic_service:
case sdv::EObjectType::sensor:
case sdv::EObjectType::actuator:
// Allowed?
bError = !bDeviceAndBasicServiceAllowed;
break;
case sdv::EObjectType::ComplexService:
case sdv::EObjectType::complex_service:
case sdv::EObjectType::vehicle_function:
// Isolation (which is needed for the main application) is not supported for direct creation.
bError = !bComplexServiceAllowed || GetAppControl().IsMainApplication();
bError = !bComplexServiceAllowed || GetAppSettings().IsMainApplication();
m_bIsoObjectLoaded = true;
break;
default:
@@ -533,13 +600,73 @@ sdv::core::TObjectID CRepository::CreateObjectFromModule(/*in*/ sdv::core::TModu
if (bError)
{
// Utilities and marshall objects cannot be created using the CreateObject function.
SDV_LOG_ERROR("Creation of an object of invalid type \"", GetObjectTypeString(optClassInfo->eType),
SDV_LOG_ERROR("Creation of an object of invalid type \"", sdv::ObjectType2String(optClassInfo->eType),
"\" requested for class \"", ssClassName, "\"!");
return 0;
}
// Check for an object name. If not existing get the default name (being either one specified by the object or the class name).
std::shared_lock<std::shared_mutex> lock(m_mtxObjects);
std::string ssObjectName2 = ssObjectName;
if (ssObjectName2.empty())
ssObjectName2 = optClassInfo->ssDefaultObjectName;
if (ssObjectName2.empty())
ssObjectName2 = ssClassName;
// Check for an object with the same name.
auto itPreviousService = m_mapServiceObjects.find(ssObjectName2);
if (itPreviousService != m_mapServiceObjects.end())
{
// Object entry is valid?
if (!*itPreviousService->second)
{
// This should not occur... there is a previous object in the service map, but the object is empty.
SDV_LOG_ERROR("Object creation requested for class \"",
ssClassName,
"\", but object with the same name \"",
ssObjectName2,
"\" was already instantiated, but cannot be found!");
return 0;
}
// Trying to create an object with the same name is not an error if the classes are identical.
if ((*itPreviousService->second)->sClassInfo.ssName == ssClassName)
return (*itPreviousService->second)->tObjectID;
// Object name was already used by another class. This is an error.
SDV_LOG_ERROR("Object creation requested for class \"",
ssClassName,
"\", but object with the same name \"",
ssObjectName2,
"\" was already instantiated for class \"",
(*itPreviousService->second)->sClassInfo.ssName,
"\"!");
return 0;
}
// Check with singleton objects if the object was already instantiated.
if (optClassInfo->uiFlags & static_cast<uint32_t>(sdv::EObjectFlags::singleton))
{
for (const auto& rprService : m_mapServiceObjects)
{
if (!*rprService.second)
continue;
if ((*rprService.second)->ptrModule && (*rprService.second)->ptrModule->GetModuleID() == ptrModule->GetModuleID()
&& (*rprService.second)->sClassInfo.ssName == ssClassName)
{
SDV_LOG_ERROR("Object creation requested but object from the same class \"",
ssClassName,
"\" was already instantiated and only one instance is allowed!");
return 0;
}
}
}
// Unlock; the next function will lock again.
lock.unlock();
// Create an internal object.
return InternalCreateObject(ptrModule, *optClassInfo, ssObjectName, ssObjectConfig);
return InternalCreateObject(ptrModule, *optClassInfo, ssObjectName2, ssObjectConfig);
}
bool CRepository::DestroyObject(/*in*/ const sdv::u8string& ssObjectName)
@@ -555,9 +682,9 @@ bool CRepository::DestroyObject2(/*in*/ const sdv::u8string& ssObjectName)
// Print info
auto ptrObjectEntry = *itService->second;
if (GetAppControl().IsConsoleVerbose())
std::cout << "Destroy a " << GetObjectTypeString(ptrObjectEntry->sClassInfo.eType) << " #" << ptrObjectEntry->tObjectID <<
" of type " << ptrObjectEntry->sClassInfo.ssClassName << " with the name " << ssObjectName << std::endl;
if (GetAppSettings().IsConsoleVerbose())
std::cout << "Destroy a " << sdv::ObjectType2String(ptrObjectEntry->sClassInfo.eType) << " #" << ptrObjectEntry->tObjectID <<
" of type " << ptrObjectEntry->sClassInfo.ssName << " with the name " << ssObjectName << std::endl;
// Remove the service
m_lstOrderedServiceObjects.erase(itService->second);
@@ -569,7 +696,6 @@ bool CRepository::DestroyObject2(/*in*/ const sdv::u8string& ssObjectName)
// Remove the object from the list and map.
if (!ptrObjectEntry) return false; // Failure: should not happen!
m_mapObjects.erase(ptrObjectEntry->tObjectID);
m_setConfigObjects.erase(ptrObjectEntry->tObjectID); // Erase from config if part of config.
lock.unlock();
// Try to destroy objects that depend on this object first
@@ -594,13 +720,13 @@ bool CRepository::DestroyObject2(/*in*/ const sdv::u8string& ssObjectName)
if (ptrDependingObject->bControlled) DestroyObject(ptrDependingObject->ssName);
}
};
fnDestroyDependingObjects(GetDependingObjectInstancesByClass(ptrObjectEntry->sClassInfo.ssClassName));
if (ptrObjectEntry->sClassInfo.ssClassName != ptrObjectEntry->sClassInfo.ssDefaultObjectName)
fnDestroyDependingObjects(GetDependingObjectInstancesByClass(ptrObjectEntry->sClassInfo.ssName));
if (ptrObjectEntry->sClassInfo.ssName != ptrObjectEntry->sClassInfo.ssDefaultObjectName)
fnDestroyDependingObjects(GetDependingObjectInstancesByClass(ptrObjectEntry->sClassInfo.ssDefaultObjectName));
// Shutdown the object (not all objects expose IObjectControl).
auto* pObjectControl = ptrObjectEntry->ptrObject.GetInterface<sdv::IObjectControl>();
if (pObjectControl && pObjectControl->GetStatus() != sdv::EObjectStatus::destruction_pending)
if (pObjectControl && pObjectControl->GetObjectState() != sdv::EObjectState::destruction_pending)
pObjectControl->Shutdown();
// Destroy the object (not all objects have a module and some are wrapped by an isolation monitor).
@@ -646,7 +772,7 @@ sdv::core::TObjectID CRepository::RegisterObject(IInterfaceAccess* pObjectIfc, c
lock.unlock();
if (!ptrIsolatedObject) return 0; // Already deleted
if (ptrIsolatedObject->ptrObject) return 0; // only one time registration allowed.
if (GetAppControl().IsConsoleVerbose())
if (GetAppSettings().IsConsoleVerbose())
std::cout << "Registering the isolated object #" << ptrIsolatedObject->tObjectID << " with the name " << ssObjectName << std::endl;
ptrIsolatedObject->ptrObject = pObjectIfc;
std::unique_lock<std::mutex> lockObject(ptrIsolatedObject->mtxConnect);
@@ -673,7 +799,7 @@ sdv::core::TObjectID CRepository::RegisterObject(IInterfaceAccess* pObjectIfc, c
ptrObjectEntry->bControlled = true;
// Print info
if (GetAppControl().IsConsoleVerbose())
if (GetAppSettings().IsConsoleVerbose())
std::cout << "Registering a application object #" << tObjectID << " with the name " << ssObjectName << std::endl;
return tObjectID;
@@ -695,19 +821,11 @@ sdv::SClassInfo CRepository::FindClass(/*in*/ const sdv::u8string& ssClassName)
std::shared_lock<std::shared_mutex> lock(m_mtxObjects);
// For main and isolated applications, search in the installation.
if (GetAppControl().IsMainApplication() || GetAppControl().IsIsolatedApplication())
if (GetAppSettings().IsMainApplication() || GetAppSettings().IsIsolatedApplication())
{
auto optManifest = GetAppConfig().FindInstalledComponent(ssClassName);
if (!optManifest) return {};
sdv::SClassInfo sClassInfo{};
sClassInfo.ssModulePath = optManifest->pathRelModule.generic_u8string();
sClassInfo.ssClassName = optManifest->ssClassName;
sClassInfo.seqClassAliases = optManifest->seqAliases;
sClassInfo.ssDefaultObjectName = optManifest->ssDefaultObjectName;
sClassInfo.eType = optManifest->eType;
sClassInfo.uiFlags = optManifest->uiFlags;
sClassInfo.seqDependencies = optManifest->seqDependencies;
return sClassInfo;
return *optManifest;
}
// Get the information through the module.
@@ -833,7 +951,7 @@ void CRepository::OnDestroyObject(sdv::IInterfaceAccess* pObject)
// Shutdown the object
auto* pObjectControl = ptrObjectEntry->ptrObject.GetInterface<sdv::IObjectControl>();
if (pObjectControl && pObjectControl->GetStatus() != sdv::EObjectStatus::destruction_pending)
if (pObjectControl && pObjectControl->GetObjectState() != sdv::EObjectState::destruction_pending)
pObjectControl->Shutdown();
// Destroy the object
@@ -864,7 +982,7 @@ void CRepository::DestroyModuleObjects(sdv::core::TModuleID tModuleID)
// Shutdown the object
auto* pObjectControl = ptrObjectEntry->ptrObject.GetInterface<sdv::IObjectControl>();
if (pObjectControl && pObjectControl->GetStatus() != sdv::EObjectStatus::destruction_pending)
if (pObjectControl && pObjectControl->GetObjectState() != sdv::EObjectState::destruction_pending)
pObjectControl->Shutdown();
// Destroy the object
@@ -924,34 +1042,114 @@ void CRepository::DestroyAllObjects(const std::vector<std::string>& rvecIgnoreOb
void CRepository::ResetConfigBaseline()
{
// Reset the configuration set.
std::unique_lock<std::shared_mutex> lock(m_mtxObjects);
m_setConfigObjects.clear();
for (const auto& rvtObject : m_mapObjects)
m_setConfigObjects.insert(rvtObject.first);
lock.unlock();
GetModuleControl().ResetConfigBaseline();
}
sdv::core::EConfigProcessResult CRepository::StartFromConfig(const CAppConfigFile& rconfig, bool bAllowPartialLoad)
{
// First load the modules
sdv::core::EConfigProcessResult eResult = GetModuleControl().LoadModulesFromConfig(rconfig, bAllowPartialLoad);
if (eResult == sdv::core::EConfigProcessResult::failed) return eResult;
bool bRunsAsServer = false;
switch (GetAppSettings().GetContextType())
{
case sdv::app::EAppContext::main:
case sdv::app::EAppContext::isolated:
case sdv::app::EAppContext::maintenance:
bRunsAsServer = true;
default:
break;
}
// Load all the components from the component list
auto vecComponents = rconfig.GetComponentList();
size_t nSuccess = 0, nFail = 0;
std::vector<std::string> vecLoadedObjects;
for (const auto& rsComponent : vecComponents)
{
// If there a path is stored with the component and not running a server, load and get the module ID and then start a
// specific component. If there is no path stored, use the default object creation function.
sdv::core::TObjectID tObjectID = 0;
if (!bRunsAsServer && !rsComponent.pathModule.empty())
{
sdv::core::TModuleID tModuleID = GetModuleControl().Load(rsComponent.pathModule.generic_u8string());
if (tModuleID)
tObjectID = CreateObjectFromModule(tModuleID, rsComponent.ssClassName, rsComponent.ssInstanceName, rsComponent.ssParameterTOML);
} else
tObjectID = CreateObject(rsComponent.ssClassName, rsComponent.ssInstanceName, rsComponent.ssParameterTOML);
if (tObjectID)
{
++nSuccess;
// Request the object name. This name is unique, but might be assigned automatically.
vecLoadedObjects.push_back(GetObjectInfo(tObjectID).ssObjectName);
} else
++nFail;
if (!bAllowPartialLoad && nFail)
break;
}
// Destroy the objects when an error occurred during loading.
if (!bAllowPartialLoad && nFail)
{
for (const std::string& rssName : vecLoadedObjects)
DestroyObject(rssName);
return sdv::core::EConfigProcessResult::failed;
}
// TODO: The class list contains the parameters to set for each component.
//// Load the class information from the class list
// auto vecClasses = rconfig.GetClassList();
// for (const auto& rsClass : vecClasses)
//{
// if (rsClass.ssModulePath.empty())
// continue;
// sdv::core::TModuleID tModuleID = Load(std::filesystem::u8path(rsClass.ssModulePath));
// if (tModuleID)
// ++nSuccess;
// else
// ++nFail;
//}
//if (!bAllowPartialLoad && nFail)
// return sdv::core::EConfigProcessResult::failed;
if (!nFail)
return sdv::core::EConfigProcessResult::successful;
if (nSuccess)
return sdv::core::EConfigProcessResult::partially_successful;
return sdv::core::EConfigProcessResult::failed;
}
std::string CRepository::SaveConfig()
{
std::stringstream sstream;
std::shared_lock<std::shared_mutex> lock(m_mtxObjects);
std::set<std::filesystem::path> setModules;
for (const std::shared_ptr<SObjectEntry>& rptrObject : m_lstOrderedServiceObjects)
{
if (m_setConfigObjects.find(rptrObject->tObjectID) != m_setConfigObjects.end())
{
sstream << std::endl;
sstream << "[[Component]]" << std::endl;
sstream << "Path = \"" << rptrObject->ptrModule->GetModulePath().generic_u8string() << "\"" << std::endl;
setModules.insert(rptrObject->ptrModule->GetModulePath());
sstream << "Class = \"" << rptrObject->sClassInfo.ssClassName << "\"" << std::endl;
sstream << "Name = \"" << rptrObject->ssName << "\"" << std::endl;
// TODO: attributes...
}
}
//std::shared_lock<std::shared_mutex> lock(m_mtxObjects);
//std::set<std::filesystem::path> setModules;
//for (const std::shared_ptr<SObjectEntry>& rptrObject : m_lstOrderedServiceObjects)
//{
// if (m_setConfigObjects.find(rptrObject->tObjectID) != m_setConfigObjects.end())
// {
// sstream << std::endl;
// sstream << "[[Component]]" << std::endl;
// sstream << "Path = \"" << rptrObject->ptrModule->GetModulePath().generic_u8string() << "\"" << std::endl;
// setModules.insert(rptrObject->ptrModule->GetModulePath());
// sstream << "Class = \"" << rptrObject->sClassInfo.ssName << "\"" << std::endl;
// sstream << "Name = \"" << rptrObject->ssName << "\"" << std::endl;
// // TODO: attributes...
// }
//}
// Add the modules
sstream << GetModuleControl().SaveConfig(setModules);
//// Add the modules
//sstream << GetModuleControl().SaveConfig(setModules);
return sstream.str();
}
@@ -960,10 +1158,27 @@ sdv::core::TObjectID CRepository::CreateIsolatedObject(const sdv::SClassInfo& rs
const sdv::u8string& rssObjectConfig)
{
// Check whether running as main application.
if (!GetAppControl().IsMainApplication()) return 0;
if (!GetAppSettings().IsMainApplication()) return 0;
// Check the object type
if (rsClassInfo.eType != sdv::EObjectType::ComplexService && rsClassInfo.eType != sdv::EObjectType::Utility) return 0;
if (rssObjectName.empty())
{
SDV_LOG_ERROR("Object creation requested for class \"", rsClassInfo.ssName, "\", but no object name was provided!");
return 0;
}
// Check the object type and define whether this should be a controlled object
bool bControlled = false;
switch (rsClassInfo.eType)
{
case sdv::EObjectType::complex_service:
case sdv::EObjectType::vehicle_function:
bControlled = true;
break;
case sdv::EObjectType::utility:
break;
default:
return 0;
}
// Create server connection (to the repository service object... not to this class!).
sdv::com::IConnectionControl* pConnectionControl =
@@ -979,7 +1194,7 @@ sdv::core::TObjectID CRepository::CreateIsolatedObject(const sdv::SClassInfo& rs
// Create the isolation process configuration
std::stringstream sstreamConfig;
sstreamConfig << "[Isolation]" << std::endl;
sstreamConfig << "Class = \"" << rsClassInfo.ssClassName << "\"" << std::endl;
sstreamConfig << "Class = \"" << rsClassInfo.ssName << "\"" << std::endl;
sstreamConfig << "Object = \"" << rssObjectName << "\"" << std::endl << std::endl;
if (!rssObjectConfig.empty())
{
@@ -992,12 +1207,12 @@ sdv::core::TObjectID CRepository::CreateIsolatedObject(const sdv::SClassInfo& rs
// Create command line arguments
sdv::sequence<sdv::u8string> seqArguments;
seqArguments.push_back(Base64EncodePlainText(sstreamConfig.str()));
if (GetAppControl().IsConsoleVerbose())
if (GetAppSettings().IsConsoleVerbose())
seqArguments.push_back("--verbose");
else
seqArguments.push_back("--silent");
seqArguments.push_back("--instance" + std::to_string(GetAppControl().GetInstanceID()));
seqArguments.push_back("--install_dir" + GetAppControl().GetInstallDir().generic_u8string());
seqArguments.push_back("--instance" + std::to_string(GetAppSettings().GetInstanceID()));
seqArguments.push_back("--install_dir" + GetAppSettings().GetRootDir().generic_u8string());
sdv::core::TObjectID tObjectID = CreateObjectID();
auto ptrObjectEntry = std::make_shared<SObjectEntry>();
@@ -1006,11 +1221,10 @@ sdv::core::TObjectID CRepository::CreateIsolatedObject(const sdv::SClassInfo& rs
// cppcheck-suppress knownConditionTrueFalse
if (!ptrObjectEntry)
{
SDV_LOG_ERROR("Object creation failed for class \"", rsClassInfo.ssClassName, "\"!");
SDV_LOG_ERROR("Object creation failed for class \"", rsClassInfo.ssName, "\"!");
return 0;
}
std::unique_lock<std::shared_mutex> lock(m_mtxObjects);
bool bControlled = rsClassInfo.eType == sdv::EObjectType::ComplexService;
ptrObjectEntry->tObjectID = tObjectID;
ptrObjectEntry->sClassInfo = rsClassInfo;
ptrObjectEntry->ssName = rssObjectName;
@@ -1019,15 +1233,14 @@ sdv::core::TObjectID CRepository::CreateIsolatedObject(const sdv::SClassInfo& rs
ptrObjectEntry->bIsolated = true;
if (bControlled)
m_mapServiceObjects[rssObjectName] = m_lstOrderedServiceObjects.insert(m_lstOrderedServiceObjects.end(), ptrObjectEntry);
m_setConfigObjects.insert(tObjectID);
m_mapObjects[tObjectID] = ptrObjectEntry;
m_mapIsolatedObjects[rssObjectName] = ptrObjectEntry;
lock.unlock();
// Print info
if (GetAppControl().IsConsoleVerbose())
std::cout << "Creating an isolated " << GetObjectTypeString(rsClassInfo.eType) << " #" << tObjectID << " of type " <<
rsClassInfo.ssClassName << std::endl;
if (GetAppSettings().IsConsoleVerbose())
std::cout << "Creating an isolated " << sdv::ObjectType2String(rsClassInfo.eType) << " #" << tObjectID << " of type " <<
rsClassInfo.ssName << std::endl;
// Start the isolation process
sdv::process::IProcessControl* pProcessControl = sdv::core::GetObject<sdv::process::IProcessControl>("ProcessControlService");
@@ -1046,51 +1259,14 @@ sdv::core::TObjectID CRepository::CreateIsolatedObject(const sdv::SClassInfo& rs
sdv::core::TObjectID CRepository::InternalCreateObject(const std::shared_ptr<CModuleInst>& rptrModule,
const sdv::SClassInfo& rsClassInfo, const sdv::u8string& rssObjectName, const sdv::u8string& rssObjectConfig)
{
// Check for an object name. If not existing get the default name (being either one specified by the object or the class name).
std::unique_lock<std::shared_mutex> lock(m_mtxObjects);
std::string ssObjectName2 = rssObjectName;
if (rssObjectName.empty())
ssObjectName2 = rptrModule->GetDefaultObjectName(rsClassInfo.ssClassName);
// Check for an object with the same name.
auto itPreviousService = m_mapServiceObjects.find(ssObjectName2);
if (itPreviousService != m_mapServiceObjects.end())
{
// Object entry is valid?
if (!*itPreviousService->second)
{
// This should not occur... there is a previous object in the service map, but the object is empty.
SDV_LOG_ERROR("Object creation requested for class \"", rsClassInfo.ssClassName, "\", but object with the same name \"",
ssObjectName2, "\" was already instantiated, but cannot be found!");
return 0;
}
// Trying to create an object with the same name is not an error if the classes are identical.
if ((*itPreviousService->second)->sClassInfo.ssClassName == rsClassInfo.ssClassName)
return (*itPreviousService->second)->tObjectID;
// Object name was already used by another class. This is an error.
SDV_LOG_ERROR("Object creation requested for class \"", rsClassInfo.ssClassName, "\", but object with the same name \"",
ssObjectName2, "\" was already instantiated for class \"", (*itPreviousService->second)->sClassInfo.ssClassName, "\"!");
SDV_LOG_ERROR("Object creation requested for class \"", rsClassInfo.ssName, "\", but no object name was provided!");
return 0;
}
// Check with singleton objects if the object was already instantiated.
if (rsClassInfo.uiFlags & static_cast<uint32_t>(sdv::EObjectFlags::singleton))
{
for (const auto& rprService : m_mapServiceObjects)
{
if (!*rprService.second) continue;
if ((*rprService.second)->ptrModule && (*rprService.second)->ptrModule->GetModuleID() == rptrModule->GetModuleID() &&
(*rprService.second)->sClassInfo.ssClassName == rsClassInfo.ssClassName)
{
SDV_LOG_ERROR("Object creation requested but object from the same class \"",
rsClassInfo.ssClassName, "\" was already instantiated and only one instance is allowed!");
return 0;
}
}
}
// Check for an object name. If not existing get the default name (being either one specified by the object or the class name).
std::unique_lock<std::shared_mutex> lock(m_mtxObjects);
// Reserve the object and return the lock
sdv::core::TObjectID tObjectID = CreateObjectID();
auto ptrObjectEntry = std::make_shared<SObjectEntry>();
@@ -1099,31 +1275,30 @@ sdv::core::TObjectID CRepository::InternalCreateObject(const std::shared_ptr<CMo
// cppcheck-suppress knownConditionTrueFalse
if (!ptrObjectEntry)
{
SDV_LOG_ERROR("Object creation failed for class \"", rsClassInfo.ssClassName, "\"!");
SDV_LOG_ERROR("Object creation failed for class \"", rsClassInfo.ssName, "\"!");
return 0;
}
m_mapServiceObjects[ssObjectName2] = m_lstOrderedServiceObjects.insert(m_lstOrderedServiceObjects.end(), ptrObjectEntry);
m_setConfigObjects.insert(tObjectID);
m_mapServiceObjects[rssObjectName] = m_lstOrderedServiceObjects.insert(m_lstOrderedServiceObjects.end(), ptrObjectEntry);
m_mapObjects[tObjectID] = ptrObjectEntry;
ptrObjectEntry->tObjectID = tObjectID;
ptrObjectEntry->sClassInfo = rsClassInfo;
ptrObjectEntry->ssName = ssObjectName2;
ptrObjectEntry->ssName = rssObjectName;
ptrObjectEntry->ssConfig = rssObjectConfig;
ptrObjectEntry->ptrModule = rptrModule;
ptrObjectEntry->bControlled = true;
lock.unlock();
// Print info
if (GetAppControl().IsConsoleVerbose())
std::cout << "Creating a " << GetObjectTypeString(rsClassInfo.eType) << " #" << tObjectID << " of type " <<
rsClassInfo.ssClassName << " with the name " << ssObjectName2 << std::endl;
if (GetAppSettings().IsConsoleVerbose())
std::cout << "Creating a " << sdv::ObjectType2String(rsClassInfo.eType) << " #" << tObjectID << " of type " <<
rsClassInfo.ssName << " with the name " << rssObjectName << std::endl;
// Create the object
sdv::TInterfaceAccessPtr ptrObject = rptrModule->CreateObject(rsClassInfo.ssClassName);
sdv::TInterfaceAccessPtr ptrObject = rptrModule->CreateObject(rsClassInfo.ssName);
if (!ptrObject)
{
// Destroy the object again
DestroyObject(ssObjectName2);
DestroyObject(rssObjectName);
return 0;
}
@@ -1132,7 +1307,7 @@ sdv::core::TObjectID CRepository::InternalCreateObject(const std::shared_ptr<CMo
if (pObjectControl)
{
pObjectControl->Initialize(rssObjectConfig);
if (pObjectControl->GetStatus() != sdv::EObjectStatus::initialized)
if (pObjectControl->GetObjectState() != sdv::EObjectState::initialized)
{
// Shutdown the object (even if the initialization didn't work properly).
pObjectControl->Shutdown();
@@ -1141,7 +1316,7 @@ sdv::core::TObjectID CRepository::InternalCreateObject(const std::shared_ptr<CMo
rptrModule->DestroyObject(ptrObject);
// Destroy the object again
DestroyObject(ssObjectName2);
DestroyObject(rssObjectName);
return 0;
}
switch (GetAppControl().GetOperationState())
@@ -1158,7 +1333,8 @@ sdv::core::TObjectID CRepository::InternalCreateObject(const std::shared_ptr<CMo
}
// For an one isolated object: add iso control and store this as pointer in object entry...
if (rsClassInfo.eType == sdv::EObjectType::ComplexService && GetAppControl().IsIsolatedApplication())
if ((rsClassInfo.eType == sdv::EObjectType::complex_service || rsClassInfo.eType == sdv::EObjectType::vehicle_function) &&
GetAppSettings().IsIsolatedApplication())
{
ptrObjectEntry->ptrIsoMon = std::make_shared<CIsoMonitor>(ptrObject);
ptrObjectEntry->ptrObject = ptrObjectEntry->ptrIsoMon.get();
@@ -1199,32 +1375,23 @@ std::vector<sdv::core::TObjectID> CRepository::GetDependingObjectInstancesByClas
return vecDependingObjects;
}
#ifndef DO_NOT_INCLUDE_IN_UNIT_TEST
CRepository& CRepositoryService::GetRepository()
{
return ::GetRepository();
}
bool CRepositoryService::EnableRepositoryObjectControl()
{
return GetAppControl().IsMainApplication() ||
GetAppControl().IsStandaloneApplication() ||
GetAppControl().IsEssentialApplication() ||
GetAppControl().IsIsolatedApplication();
return GetAppSettings().IsMainApplication() ||
GetAppSettings().IsStandaloneApplication() ||
GetAppSettings().IsEssentialApplication() ||
GetAppSettings().IsIsolatedApplication();
}
bool CRepositoryService::EnableRepositoryRegisterForeignApp()
{
return GetAppControl().IsMainApplication() ||
GetAppControl().IsStandaloneApplication() ||
GetAppControl().IsEssentialApplication() ||
GetAppControl().IsIsolatedApplication();
return GetAppSettings().IsMainApplication() ||
GetAppSettings().IsStandaloneApplication() ||
GetAppSettings().IsEssentialApplication() ||
GetAppSettings().IsIsolatedApplication();
}
bool CRepositoryService::EnableRepositoryLink()
{
return GetAppControl().IsIsolatedApplication() || GetAppControl().IsExternalApplication();
return GetAppSettings().IsIsolatedApplication() || GetAppSettings().IsExternalApplication();
}
#endif