ADTF_DEVICE_TOOLBOX  3.12.1 (ADTF 3.18.3)
Source Code for Demo FlexRay Coder Generator
Location
./src/examples/src/flexray_coder_generator/
This example shows:
Header file for the Demo FlexRay Coder Generator Example
#pragma once
#include <adtf_filtersdk.h>
#include <adtf_base.h>
#include <adtf_systemsdk.h>
class cFlexrayCoderGenerator : public adtf::filter::cSampleStreamingSource
{
public:
ADTF_CLASS_ID_NAME(cFlexrayCoderGenerator, "demo_flexray_coder_generator.streaming_source.devicetb.cid","Demo FlexRay Coder Generator");
ADTF_CLASS_DEPENDENCIES(REQUIRE_INTERFACE(adtf::services::IReferenceClock),
cFlexrayCoderGenerator();
tResult Init() override;
tResult StartStreaming() override;
tResult StopStreaming() override;
tResult ProcessSample(const adtf::devicetb::sdk::flexray::tSignalID& nId);
private:
adtf::base::property_variable<tUInt> m_nTimer = 10;
adtf::base::property_variable<adtf::util::cString> m_strSignal;
adtf::base::property_variable<tUInt> m_nChannelID = 1; // which channel from the flexray-service
adtf::base::property_variable<adtf::util::cString> m_strClustername; // which clustername. Leave empty for default
adtf::base::property_variable<tInt> m_nValue = 0; // send this value
adtf::system::kernel_timer m_oLooper;
adtf::ucom::object_ptr<adtf::services::IReferenceClock> m_pClock;
adtf::streaming::ISampleWriter* m_pWriter;
adtf::ucom::object_ptr<adtf::devicetb::sdk::flexray::IFlexRayCoder> m_pCoder;
adtf::ucom::object_ptr<adtf::devicetb::sdk::flexray::IFlexRayDatabase> m_pDatabase;
tResult SetClusterNameProperty(adtf::ucom::object_ptr<adtf::streaming::IStreamType>& pStreamType);
tResult SetProjectNameProperty(adtf::ucom::object_ptr<adtf::streaming::IStreamType>& pStreamType);
tResult SetDatabaseNameProperty(adtf::ucom::object_ptr<adtf::streaming::IStreamType>& pStreamType);
tResult SetChannelNumberProperty(adtf::ucom::object_ptr<adtf::streaming::IStreamType>& pStreamType);
tResult FillTheSample(adtf::ucom::object_ptr<adtf::streaming::ISample>& pSample);
};
The IFlexRaySupport interface provides methods to query FIBEX databases.
Copyright © CARIAD SE.
axle::tSignalID tSignalID
FlexRay signal ID.
Implementation for the Demo FlexRay Coder Generator Example
#include "demo_flexray_coder_generator.h"
#include <cstring>
#include <regex>
using namespace adtf;
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() :
m_pWriter(CreateOutputPin("flexray_output", stream_meta_type_flexray()))
{
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()
{
// we first set the properties and then create the output pin
ucom::object_ptr<streaming::IStreamType> pStreamType =
ucom::make_object_ptr<streaming::cStreamType>(stream_meta_type_flexray());
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));
// setting some basic properties
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)
{
// get the information for the signal
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);
// create the runner
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();
}
tResult cFlexrayCoderGenerator::ProcessSample(const adtf::devicetb::sdk::flexray::tSignalID& nId)
{
// sets the value
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
Definition: can_types.h:301
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.
ADTF - Namespace.
Copyright © CARIAD SE.