#include "demo_flexray_coder_generator.h"
#include <cstring>
#include <regex>
using namespace devicetb::sdk::flexray;
ADTF_PLUGIN_VERSION("FlexRay coder generator",
devicetb,
DEVICETB_VERSION_MAJOR,
DEVICETB_VERSION_MINOR,
DEVICETB_VERSION_PATCH,
cFlexrayCoderGenerator)
constexpr const char* strSignalDesc = R"(Valid signal syntax: ECU.CHANNEL<.PDU>.Signal<.PROJECT>
E.g: ECU_1.Channel_A<.PDU_1>.Signal_1<.Project_1>
The PDU and PROJECT are optional, depending on the fibex version)";
namespace
{
std::string replace_dots_with_slashes(const util::cString& strOldName)
{
std::string strDotname = strOldName.GetPtr();
return std::regex_replace(strDotname, std::regex("\\."), "/");
}
}
cFlexrayCoderGenerator::cFlexrayCoderGenerator() :
{
SetDescription("flexray_output", "The outcoming FlexRay sample stream containing the configured signal and value.");
m_nTimer.SetDescription("Generation rate in [ms]");
RegisterPropertyVariable("Timer", m_nTimer);
m_strSignal.SetDescription("The name of the signal.");
RegisterPropertyVariable("Signal", m_strSignal);
m_strClustername.SetDescription("The name of the FlexRay device cluster within the database.");
RegisterPropertyVariable("Clustername", m_strClustername);
m_nChannelID.SetDescription("The channel and database which should be used.");
RegisterPropertyVariable("ChannelID", m_nChannelID);
m_nValue.SetDescription("The value for the signal.");
RegisterPropertyVariable("Signalvalue", m_nValue);
SetDescription("Use this Streaming Source to generate FlexRay data for a specific signal from a FIBEX database.");
SetHelpLink("$(ADTF_DEVICE_TOOLBOX_DIR)/doc/adtf_device_toolbox_html/page_example_source_flexray_coder_generator.html");
m_strSignal.SetDescription(strSignalDesc);
}
tResult cFlexrayCoderGenerator::Init()
{
ucom::object_ptr<streaming::IStreamType> pStreamType =
ucom::object_ptr<devicetb::sdk::flexray::IFlexRaySupport> pSupport;
RETURN_IF_FAILED(_runtime->GetObject(pSupport));
RETURN_IF_FAILED(pSupport->GetDatabase(static_cast<devicetb::sdk::flexray::tChannelID>(*m_nChannelID), m_pDatabase));
RETURN_IF_FAILED(pSupport->CreateCoder(m_pDatabase, m_pCoder));
SetDatabaseNameProperty(pStreamType);
SetClusterNameProperty(pStreamType);
SetProjectNameProperty(pStreamType);
SetChannelNumberProperty(pStreamType);
m_pWriter->ChangeType(pStreamType);
RETURN_IF_FAILED(_runtime->GetObject(m_pClock));
return cSampleStreamingSource::Init();
}
tResult cFlexrayCoderGenerator::StartStreaming()
{
if (m_strSignal->IsEmpty())
{
RETURN_ERROR_DESC(ERR_INVALID_ARG,"No Signal name has been set. Please specify signal name");
}
if (m_pDatabase)
{
devicetb::sdk::flexray::tSignalID nId;
std::string strSignalName = replace_dots_with_slashes(m_strSignal);
RETURN_IF_FAILED_DESC(m_pDatabase->GetSignalID(strSignalName.c_str(), &nId),
"signal: '%s' not found. %s", strSignalName.c_str(), strSignalDesc);
auto strMyName = streaming::get_named_graph_object_full_name(*this);
m_oLooper = system::kernel_timer(strMyName, m_nTimer * 1000, 0, &cFlexrayCoderGenerator::ProcessSample, this,
nId);
}
else
{
LOG_WARNING("no database has been set");
}
return cSampleStreamingSource::StartStreaming();
}
tResult cFlexrayCoderGenerator::StopStreaming()
{
m_oLooper.Stop();
return cSampleStreamingSource::StopStreaming();
}
{
oValue.nf64Value = *m_nValue;
m_pCoder->ResetData();
m_pCoder->SetSignalValue(nId, &oValue);
ucom::object_ptr<streaming::ISample> pSample;
FillTheSample(pSample);
m_pWriter->Write(pSample);
return m_pWriter->ManualTrigger();
}
tResult cFlexrayCoderGenerator::FillTheSample(adtf::ucom::object_ptr<adtf::streaming::ant::ISample>& pSample)
{
ucom::object_ptr_locked<streaming::ISampleBuffer> pBuffer;
tInt nSize;
m_pCoder->TransmitData(nullptr, &nSize);
alloc_sample(pSample);
pSample->WriteLock(pBuffer, nSize);
std::memset(pBuffer->GetPtr(), 0, pBuffer->GetSize());
m_pCoder->TransmitData(pBuffer->GetPtr(), &nSize);
return pBuffer->Unlock();
}
tResult cFlexrayCoderGenerator::SetDatabaseNameProperty(adtf::ucom::object_ptr<adtf::streaming::ant::IStreamType>& pStreamType)
{
return streaming::set_property<util::cString>(*pStreamType.Get(), stream_meta_type_flexray::FlexrayDatabaseName,
m_pDatabase->GetName());
}
tResult cFlexrayCoderGenerator::SetClusterNameProperty(ucom::object_ptr<streaming::ant::IStreamType>& pStreamType)
{
util::cString strClusterName = *m_strClustername;
if (m_strClustername->IsEmpty())
{
m_pDatabase->GetAllClusterNames(&pLst);
strClusterName = pLst.strClusterNames[0];
}
return streaming::set_property<util::cString>(*pStreamType.Get(), stream_meta_type_flexray::FlexrayClusterName,
strClusterName);
}
tResult cFlexrayCoderGenerator::SetProjectNameProperty(ucom::object_ptr<streaming::ant::IStreamType>& pStreamType)
{
const tChar* strName;
m_pDatabase->GetProjectID(m_pDatabase->GetName(), &strName);
return streaming::set_property<util::cString>(*pStreamType.Get(), stream_meta_type_flexray::FlexrayProjectName,
strName);
}
tResult cFlexrayCoderGenerator::SetChannelNumberProperty(adtf::ucom::object_ptr<adtf::streaming::ant::IStreamType>& pStreamType)
{
return streaming::set_property<util::cString>(*pStreamType.Get(), stream_meta_type_flexray::FlexrayChannelNumber,
std::to_string(*m_nChannelID).c_str());
}
Copyright 2024 CARIAD SE.
axle::tSignalValue tSignalValue
axle::stream_meta_type_flexray stream_meta_type_flexray
Definition of Stream Meta Type FlexRay Messages.
axle::tClusterNameLst tClusterNameLst
Structure with a list of all Cluster Short-Names.