Precommit (#1)

* first commit

* cleanup
This commit is contained in:
tompzf
2025-11-04 13:28:06 +01:00
committed by GitHub
parent dba45dc636
commit 6ed4b1534e
898 changed files with 256340 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
# Define project
project(can_com_sim VERSION 1.0 LANGUAGES CXX)
# Define target
add_library(can_com_sim SHARED "can_com_sim.h" "can_com_sim.cpp")
target_link_options(can_com_sim PRIVATE)
target_link_libraries(can_com_sim ${CMAKE_THREAD_LIBS_INIT})
set_target_properties(can_com_sim PROPERTIES PREFIX "")
set_target_properties(can_com_sim PROPERTIES SUFFIX ".sdv")
# Build dependencies
add_dependencies(can_com_sim CompileCoreIDL)
# Appending the service in the service list
set(SDV_Service_List ${SDV_Service_List} can_com_sim PARENT_SCOPE)

View File

@@ -0,0 +1,192 @@
#include "can_com_sim.h"
#include <support/toml.h>
#include "../../global/ascformat/ascreader.cpp"
#include "../../global/ascformat/ascwriter.cpp"
CCANSimulation::CCANSimulation()
{}
CCANSimulation::~CCANSimulation()
{}
void CCANSimulation::Initialize(const sdv::u8string& rssObjectConfig)
{
try
{
sdv::toml::CTOMLParser config(rssObjectConfig.c_str());
auto nodeSource = config.GetDirect("Source");
if (nodeSource.GetType() == sdv::toml::ENodeType::node_string)
m_pathSource = nodeSource.GetValue();
auto nodeTarget = config.GetDirect("Target");
if (nodeTarget.GetType() == sdv::toml::ENodeType::node_string)
m_pathTarget = nodeTarget.GetValue();
if (m_pathSource.empty() && m_pathTarget.empty())
{
SDV_LOG(sdv::core::ELogSeverity::error,
"At least the source or the target ASC files must be specified.");
m_eStatus = sdv::EObjectStatus::initialization_failure;
}
else if (m_pathSource == m_pathTarget)
{
SDV_LOG(sdv::core::ELogSeverity::error,
"Source and target ASC files '" + m_pathSource.generic_u8string() + "' cannot be the same.");
m_eStatus = sdv::EObjectStatus::initialization_failure;
}
else if (std::filesystem::exists(m_pathTarget))
{
SDV_LOG(sdv::core::ELogSeverity::warning,
"Target ASC file '" + m_pathSource.generic_u8string() + "' will be overwritten.");
}
else if (m_pathSource.empty() && m_pathTarget.empty())
{
SDV_LOG(sdv::core::ELogSeverity::error, "No ASC file configured for reading or writing.");
m_eStatus = sdv::EObjectStatus::initialization_failure;
}
}
catch (const sdv::toml::XTOMLParseException& e)
{
SDV_LOG(sdv::core::ELogSeverity::error, "Configuration could not be read: ", e.what());
m_eStatus = sdv::EObjectStatus::initialization_failure;
}
catch (const std::runtime_error& e)
{
SDV_LOG(sdv::core::ELogSeverity::error, "Configuration could not be read: ", e.what());
m_eStatus = sdv::EObjectStatus::initialization_failure;
}
if (m_eStatus == sdv::EObjectStatus::initialization_failure) return;
// Initialize the ASC writer
if (!m_pathTarget.empty())
SDV_LOG(sdv::core::ELogSeverity::info,
"CAN simulator uses ASC file '" + m_pathTarget.generic_u8string() + "' to record CAN data.");
m_writer.StartTimer();
// Initialize the ASC reader
if (!m_pathSource.empty())
SDV_LOG(sdv::core::ELogSeverity::info,
"CAN simulator uses ASC file '" + m_pathSource.generic_u8string() + "' to playback CAN data.");
if (!m_pathSource.empty() && !m_reader.Read(m_pathSource))
{
SDV_LOG(sdv::core::ELogSeverity::error,
"Failed to read ASC file '" + m_pathSource.generic_u8string() + "' for CAN playback.");
m_eStatus = sdv::EObjectStatus::initialization_failure;
return;
}
// Update the status
m_eStatus = sdv::EObjectStatus::initialized;
}
sdv::EObjectStatus CCANSimulation::GetStatus() const
{
return m_eStatus;
}
void CCANSimulation::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;
// Stop playback
m_reader.StopPlayback();
}
break;
case sdv::EOperationMode::running:
if (m_eStatus == sdv::EObjectStatus::configuring || m_eStatus == sdv::EObjectStatus::initialized)
{
m_eStatus = sdv::EObjectStatus::running;
// Start playback
m_reader.StartPlayback([&](const asc::SCanMessage& rsMsg) { PlaybackFunc(rsMsg); });
}
break;
default:
break;
}
}
void CCANSimulation::Shutdown()
{
m_eStatus = sdv::EObjectStatus::shutdown_in_progress;
// Stop playback
m_reader.StopPlayback();
// Write the recording
if (m_writer.HasSamples() && !m_pathTarget.empty())
{
if (!m_writer.Write(m_pathTarget))
SDV_LOG(sdv::core::ELogSeverity::error,
"Failed to write ASC file '" + m_pathTarget.generic_u8string() + "' with CAN recording.");
}
// Update the status
m_eStatus = sdv::EObjectStatus::destruction_pending;
}
void CCANSimulation::RegisterReceiver(/*in*/ sdv::can::IReceive* pReceiver)
{
if (m_eStatus != sdv::EObjectStatus::configuring) return;
if (!pReceiver) return;
std::unique_lock<std::mutex> lock(m_mtxReceivers);
m_setReceivers.insert(pReceiver);
}
void CCANSimulation::UnregisterReceiver(/*in*/ sdv::can::IReceive* pReceiver)
{
// NOTE: Normally the remove function should be called in the configuration mode. Since it doesn't give
// feedback and the associated caller might delete any receiving function, allow the removal to take place even
// when running.
if (!pReceiver) return;
std::unique_lock<std::mutex> lock(m_mtxReceivers);
m_setReceivers.erase(pReceiver);
}
void CCANSimulation::Send(/*in*/ const sdv::can::SMessage& sMsg, /*in*/ uint32_t uiIfcIndex)
{
if (m_eStatus != sdv::EObjectStatus::running) return;
asc::SCanMessage sAscCan{};
sAscCan.uiChannel = uiIfcIndex + 1;
sAscCan.uiId = sMsg.uiID;
sAscCan.bExtended = sMsg.bExtended;
sAscCan.bCanFd = sMsg.bCanFd;
sAscCan.eDirection = asc::SCanMessage::EDirection::tx;
sAscCan.uiLength = static_cast<uint32_t>(sMsg.seqData.length());
std::copy_n(sMsg.seqData.begin(), sMsg.seqData.length(), std::begin(sAscCan.rguiData));
m_writer.AddSample(sAscCan);
}
sdv::sequence<sdv::u8string> CCANSimulation::GetInterfaces() const
{
sdv::sequence<sdv::u8string> seqIfcNames;
if (m_eStatus != sdv::EObjectStatus::running) return seqIfcNames;
std::unique_lock<std::mutex> lock(m_mtxInterfaces);
for (const auto& rprInterface : m_vecInterfaces)
seqIfcNames.push_back(rprInterface.second);
return seqIfcNames;
}
void CCANSimulation::PlaybackFunc(const asc::SCanMessage& rsMsg)
{
if (m_eStatus != sdv::EObjectStatus::running) return;
// Create sdv CAN message
sdv::can::SMessage sSdvCan{};
sSdvCan.uiID = rsMsg.uiId;
sSdvCan.bExtended = rsMsg.bExtended;
sSdvCan.bCanFd = rsMsg.bCanFd;
sSdvCan.seqData = sdv::sequence<uint8_t>(std::begin(rsMsg.rguiData), std::begin(rsMsg.rguiData) + rsMsg.uiLength);
// Distribute the CAN message to all receivers
std::unique_lock<std::mutex> lock(m_mtxReceivers);
for (sdv::can::IReceive* pReceiver : m_setReceivers)
pReceiver->Receive(sSdvCan, rsMsg.uiChannel - 1);
}

View File

@@ -0,0 +1,116 @@
#ifndef CAN_COM_SIMULATION_H
#define CAN_COM_SIMULATION_H
#include <iostream>
#include <queue>
#include <set>
#include <thread>
#include <mutex>
#include <interfaces/can.h>
#include <support/component_impl.h>
#include "../../global/ascformat/ascreader.h"
#include "../../global/ascformat/ascwriter.h"
/**
* @brief Component to establish Socket CAN communication between VAPI and external application
*/
class CCANSimulation : public sdv::CSdvObject, public sdv::IObjectControl, public sdv::can::IRegisterReceiver,
public sdv::can::ISend, sdv::can::IInformation
{
public:
/**
* @brief Constructor
*/
CCANSimulation();
/**
* @brief Destructor
*/
virtual ~CCANSimulation() override;
// Interface map
BEGIN_SDV_INTERFACE_MAP()
SDV_INTERFACE_ENTRY(sdv::IObjectControl)
SDV_INTERFACE_ENTRY(sdv::can::IRegisterReceiver)
SDV_INTERFACE_ENTRY(sdv::can::ISend)
SDV_INTERFACE_ENTRY(sdv::can::IInformation)
END_SDV_INTERFACE_MAP()
DECLARE_OBJECT_CLASS_TYPE(sdv::EObjectType::Device)
DECLARE_OBJECT_CLASS_NAME("CAN_Com_Sim")
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.
*/
virtual void Initialize(const sdv::u8string& ssObjectConfig) override;
/**
* @brief Get the current status of the object. Overload of sdv::IObjectControl::GetStatus.
* @return Return the current status of the object.
*/
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;
/**
* @brief Register a CAN message receiver. Overload of sdv::can::IRegisterReceiver::RegisterReceiver.
* @param[in] pReceiver Pointer to the receiver interface.
*/
virtual void RegisterReceiver(/*in*/ sdv::can::IReceive* pReceiver) override;
/**
* @brief Unregister a previously registered CAN message receiver. Overload of
* sdv::can::IRegisterReceiver::UnregisterReceiver.
* @param[in] pReceiver Pointer to the receiver interface.
*/
virtual void UnregisterReceiver(/*in*/ sdv::can::IReceive* pReceiver) override;
/**
* @brief Send a CAN message. Overload of sdv::can::ISend::Send.
* @param[in] sMsg Message that is to be sent. The source node information is ignored. The target node determines over
* what interface the message will be sent.
* @param[in] uiIfcIndex Interface index to use for sending.
*/
virtual void Send(/*in*/ const sdv::can::SMessage& sMsg, /*in*/ uint32_t uiIfcIndex) override;
/**
* @brief Get a list of interface names. Overload of sdv::can::IInformation::GetInterfaces.
* @return Sequence containing the names of the interfaces.
*/
virtual sdv::sequence<sdv::u8string> GetInterfaces() const override;
private:
/**
* @brief Playback function for ASC data playback.
*/
void PlaybackFunc(const asc::SCanMessage& rsMsg);
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.
mutable std::mutex m_mtxInterfaces; ///< Protect the nodes set.
std::map<int, size_t> m_mapIfc2Idx; ///< Map with interface to index.
std::vector<std::pair<int, std::string>> m_vecInterfaces; ///< Vector with interfaces.
std::filesystem::path m_pathSource; ///< Path to the source ASC file.
std::filesystem::path m_pathTarget; ///< Path to the target ASC file.
asc::CAscReader m_reader; ///< Reader for ASC file playback.
asc::CAscWriter m_writer; ///< Writer for ASC file recording.
};
DEFINE_SDV_OBJECT(CCANSimulation)
#endif // ! defined CAN_COM_SIMULATION_H