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,3 +1,16 @@
#*******************************************************************************
# 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:
# Sudipta Durjoy - initial API and implementation
#*******************************************************************************
# Build for linux only
if(UNIX)

View File

@@ -1,11 +1,25 @@
/********************************************************************************
* 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:
* Sudipta Durjoy - initial API and implementation
* Thomas Pfleiderer - refactored and finalized
********************************************************************************/
#include "can_com_sockets.h"
void CCANSockets::Initialize(const sdv::u8string& rssObjectConfig)
bool CCANSockets::OnInitialize()
{
std::deque<std::string> vecConfigInterfaces;
try
{
sdv::toml::CTOMLParser config(rssObjectConfig.c_str());
sdv::toml::CTOMLParser config(GetObjectConfig());
sdv::toml::CNodeCollection nodeSource = config.GetDirect("canSockets");
if (nodeSource.GetType() == sdv::toml::ENodeType::node_array)
{
@@ -35,8 +49,7 @@ void CCANSockets::Initialize(const sdv::u8string& rssObjectConfig)
catch (const sdv::toml::XTOMLParseException& e)
{
SDV_LOG_ERROR("Configuration could not be read: ", e.what());
m_eStatus = sdv::EObjectStatus::initialization_failure;
return;
return false;
}
if (vecConfigInterfaces.size() == 0)
@@ -46,13 +59,40 @@ void CCANSockets::Initialize(const sdv::u8string& rssObjectConfig)
if (!SetupCANSockets(vecConfigInterfaces))
{
m_eStatus = sdv::EObjectStatus::initialization_failure;
return;
LogAllCanInterfaceNames();
return false;
}
LogConfigurations();
m_threadReceive = std::thread(&CCANSockets::ReceiveThreadFunc, this);
m_eStatus = sdv::EObjectStatus::initialized;
return true;
}
void CCANSockets::LogConfigurations()
{
for (const auto& socket : m_vecSockets)
{
if ((socket.localSocket > 0) && (socket.networkInterface > 0))
{
SDV_LOG_INFO("Configured socket: ", socket.name, ", index: ", socket.networkInterface);
}
}
}
void CCANSockets::LogAllCanInterfaceNames()
{
// Retrieve the list of available can interfaces
struct ifaddrs *ifaddr, *ifa;
if (getifaddrs(&ifaddr) != -1)
{
SDV_LOG_INFO("List of available can socket interfaces:");
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
{
SDV_LOG_INFO(ifa->ifa_name);
}
freeifaddrs(ifaddr);
}
}
bool CCANSockets::SetupCANSockets(const std::deque<std::string>& vecConfigInterfaces)
@@ -60,13 +100,16 @@ bool CCANSockets::SetupCANSockets(const std::deque<std::string>& vecConfigInterf
// Retrieve the list of available interfaces
std::set<std::string> availableInterfaces;
struct ifaddrs *ifaddr, *ifa;
if (getifaddrs(&ifaddr) != -1)
if (getifaddrs(&ifaddr) == -1)
{
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
{
availableInterfaces.insert(ifa->ifa_name);
}
SDV_LOG_ERROR(" Error: getifaddrs()");
return false;
}
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
{
availableInterfaces.insert(ifa->ifa_name);
}
freeifaddrs(ifaddr);
CreateAndBindSockets(vecConfigInterfaces, availableInterfaces);
@@ -152,32 +195,8 @@ void CCANSockets::CreateAndBindSockets(const std::deque<std::string>& vecConfigI
}
}
sdv::EObjectStatus CCANSockets::GetStatus() const
void CCANSockets::OnShutdown()
{
return m_eStatus;
}
void CCANSockets::SetOperationMode(sdv::EOperationMode eMode)
{
switch (eMode)
{
case sdv::EOperationMode::configuring:
if (m_eStatus == sdv::EObjectStatus::running || m_eStatus == sdv::EObjectStatus::initialized)
m_eStatus = sdv::EObjectStatus::configuring;
break;
case sdv::EOperationMode::running:
if (m_eStatus == sdv::EObjectStatus::configuring || m_eStatus == sdv::EObjectStatus::initialized)
m_eStatus = sdv::EObjectStatus::running;
break;
default:
break;
}
}
void CCANSockets::Shutdown()
{
m_eStatus = sdv::EObjectStatus::shutdown_in_progress;
// Wait until the receiving thread is finished.
if (m_threadReceive.joinable())
m_threadReceive.join();
@@ -190,13 +209,11 @@ void CCANSockets::Shutdown()
close(socket.localSocket);
}
}
m_eStatus = sdv::EObjectStatus::destruction_pending;
}
void CCANSockets::RegisterReceiver(/*in*/ sdv::can::IReceive* pReceiver)
{
if (m_eStatus != sdv::EObjectStatus::configuring) return;
if (GetObjectState() != sdv::EObjectState::configuring) return;
if (!pReceiver) return;
SDV_LOG_INFO("Registering VAPI CAN communication receiver...");
@@ -222,7 +239,7 @@ void CCANSockets::UnregisterReceiver(/*in*/ sdv::can::IReceive* pReceiver)
sdv::sequence<sdv::u8string> CCANSockets::GetInterfaces() const
{
sdv::sequence<sdv::u8string> seqIfcNames;
if (m_eStatus != sdv::EObjectStatus::running)
if (GetObjectState() != sdv::EObjectState::running)
{
return seqIfcNames;
}
@@ -241,7 +258,7 @@ sdv::sequence<sdv::u8string> CCANSockets::GetInterfaces() const
void CCANSockets::Send(const sdv::can::SMessage& sMsg, uint32_t uiConfigIndex)
{
if (m_eStatus != sdv::EObjectStatus::running) return;
if (GetObjectState() != sdv::EObjectState::running) return;
if (sMsg.bCanFd) return; // CAN-FD not supported
if (sMsg.seqData.size() > 8) return; // Invalid message length.
@@ -267,15 +284,17 @@ void CCANSockets::Send(const sdv::can::SMessage& sMsg, uint32_t uiConfigIndex)
std::unique_lock<std::mutex> lock(m_mtxSockets);
auto it = m_vecSockets.begin();
std::advance(it, uiConfigIndex);
if (it != m_vecSockets.end())
for (const auto& socket : m_vecSockets)
{
if ((it->localSocket > 0) && (it->networkInterface > 0))
if ((socket.localSocket > 0) && (socket.networkInterface > 0))
{
sAddr.can_ifindex = it->networkInterface;
sAddr.can_family = AF_CAN;
sendto(it->localSocket, &sFrame, sizeof(can_frame), 0, reinterpret_cast<sockaddr*>(&sAddr), sizeof(sAddr));
if ((uint)socket.networkInterface == uiConfigIndex)
{
sAddr.can_ifindex = socket.networkInterface;
sAddr.can_family = AF_CAN;
sendto(socket.localSocket, &sFrame, sizeof(can_frame), 0, reinterpret_cast<sockaddr*>(&sAddr), sizeof(sAddr));
break;
}
}
}
}
@@ -285,16 +304,16 @@ void CCANSockets::ReceiveThreadFunc()
while (true)
{
enum {retry, cont, exit} eNextStep = exit;
switch (m_eStatus)
switch (GetObjectState())
{
case sdv::EObjectStatus::configuring:
case sdv::EObjectStatus::initialization_pending:
case sdv::EObjectStatus::initialized:
case sdv::EObjectStatus::initializing:
case sdv::EObjectState::configuring:
case sdv::EObjectState::initialization_pending:
case sdv::EObjectState::initialized:
case sdv::EObjectState::initializing:
std::this_thread::sleep_for(std::chrono::milliseconds(10));
eNextStep = retry;
break;
case sdv::EObjectStatus::running:
case sdv::EObjectState::running:
eNextStep = cont;
break;
default:

View File

@@ -1,3 +1,17 @@
/********************************************************************************
* 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:
* Sudipta Durjoy - initial API and implementation
* Thomas Pfleiderer - refactored and finalized
********************************************************************************/
#ifndef CAN_COM_SOCKET_H
#define CAN_COM_SOCKET_H
@@ -29,7 +43,7 @@
/**
* @brief Component to establish Socket CAN communication between VAPI and external application
*/
class CCANSockets : public sdv::CSdvObject, public sdv::IObjectControl, public sdv::can::IRegisterReceiver,
class CCANSockets : public sdv::CSdvObject, public sdv::can::IRegisterReceiver,
public sdv::can::ISend, sdv::can::IInformation
{
public:
@@ -42,39 +56,27 @@ public:
SDV_INTERFACE_ENTRY(sdv::can::IInformation)
END_SDV_INTERFACE_MAP()
DECLARE_OBJECT_CLASS_TYPE(sdv::EObjectType::Device)
DECLARE_OBJECT_CLASS_TYPE(sdv::EObjectType::vehicle_bus)
DECLARE_OBJECT_CLASS_NAME("CAN_Com_Sockets")
DECLARE_DEFAULT_OBJECT_NAME("CAN_Communication_Object")
DECLARE_OBJECT_SINGLETON()
/**
* @brief Initialize the object. Overload of sdv::IObjectControl::Initialize.
* @param[in] ssObjectConfig Optional configuration string.
* @brief Initialize the object. Overload of sdv::CSdvObject::OnInitialize.
* The configuration contains either one interface name a list of interface names.
* The Send() method must use the index of this list to determine the interface
* In case of a single interface name the index is 0.
* canSockets = "vcan0"
* or
* canSockets = ["vcan1", "vcan8", "vcan9", "vcan2"]
* @return Returns 'true' when the initialization was successful, 'false' when not.
*/
virtual void Initialize(const sdv::u8string& ssObjectConfig) override;
virtual bool OnInitialize() override;
/**
* @brief Get the current status of the object. Overload of sdv::IObjectControl::GetStatus.
* @return Return the current status of the object.
* @brief Shutdown the object. Overload of sdv::CSdvObject::OnShutdown.
*/
virtual sdv::EObjectStatus GetStatus() const override;
/**
* @brief Set the component operation mode. Overload of sdv::IObjectControl::SetOperationMode.
* @param[in] eMode The operation mode, the component should run in.
*/
void SetOperationMode(sdv::EOperationMode eMode) override;
/**
* @brief Shutdown called before the object is destroyed. Overload of sdv::IObjectControl::Shutdown.
*/
virtual void Shutdown() override;
virtual void OnShutdown() override;
/**
* @brief Register a CAN message receiver. Overload of sdv::can::IRegisterReceiver::RegisterReceiver.
@@ -124,6 +126,16 @@ private:
void CreateAndBindSockets(const std::deque<std::string>& vecConfigInterfaces,
const std::set<std::string>& availableInterfaces);
/**
* @brief Write log information about the configured can sockets
*/
void LogConfigurations();
/**
* @brief Write log information about the existing can hardware
*/
void LogAllCanInterfaceNames();
/**
* @brief Socket definition structure
*/
@@ -134,7 +146,6 @@ private:
std::string name; ///< interface name, can be empty in case of an invalid socket element
};
std::atomic<sdv::EObjectStatus> m_eStatus = sdv::EObjectStatus::initialization_pending; ///< Object status
std::thread m_threadReceive; ///< Receive thread.
mutable std::mutex m_mtxReceivers; ///< Protect the receiver set.
std::set<sdv::can::IReceive*> m_setReceivers; ///< Set with receiver interfaces.

View File

@@ -12,6 +12,7 @@ Class = "DataDispatchService"
[[Component]]
Path = "can_com_sockets.sdv"
Class = "CAN_Com_Sockets"
[Component.Parameters]
canSockets=["llcecan0"]
[[Component]]