adtf_file_library  0.13.1
CAN Writer

Description

Example application which shows the usage of adtf_file::ADTFDatFileWriter.

Usage

canwriter <adtfdat|dat>

Source

#include <stdio.h>
#include <iostream>
#include <cstring>
#include <a_util/strings.h>
#include "can_stream_type_definitions.h"
using namespace adtf_file;
std::shared_ptr<ADTFDatFileWriter> create_writer_for_adtf2(a_util::filesystem::Path& filename,
size_t& stream_id)
{
//create a writer to handle .dat files from ADTF 2.x
std::shared_ptr<ADTFDatFileWriter> writer(new ADTFDatFileWriter(filename,
std::chrono::microseconds(0),
adtf2::StandardTypeSerializers(),
//define CAN stream type for ADTF 2.x
DefaultStreamType can_stream_type(ADTF2_STREAM_META_TYPE);
can_stream_type.setProperty("major", "tUInt32", a_util::strings::format("%ul", ADTF2_MEDIA_TYPE_CAN));
can_stream_type.setProperty("sub", "tUInt32", a_util::strings::format("%ul", ADTF2_MEDIA_SUBTYPE_CAN_DATA));
//create stream for can messages
//by default the copy serializer is adequate enough
stream_id = writer->createStream("rawcan", can_stream_type, std::make_shared<adtf2::AdtfCoreMediaSampleSerializer>());
return writer;
}
std::shared_ptr<ADTFDatFileWriter> create_writer_for_adtf3(a_util::filesystem::Path& filename,
size_t& stream_id)
{
//create a writer to handle .adtfdat files from ADTF 3.x
std::shared_ptr<ADTFDatFileWriter> writer(new ADTFDatFileWriter(filename,
std::chrono::microseconds(0),
adtf3::StandardTypeSerializers(),
// define CAN stream type for ADTF 3.x
DefaultStreamType can_stream_type(ADTF3_STREAM_META_TYPE);
can_stream_type.setProperty("md_struct", "cString", ADTF3_MEDIA_DESC_CANDATA_NAME);
can_stream_type.setProperty("md_definitions", "cString", ADTF3_MEDIA_DESC_CANDATA);
//create stream for can messages
//by default the copy serializer is adequate enough
stream_id = writer->createStream("rawcan", can_stream_type, std::make_shared<adtf3::SampleCopySerializer>());
return writer;
}
int main(int argc, char* argv[])
{
try
{
size_t stream_id;
std::shared_ptr<ADTFDatFileWriter> writer;
a_util::filesystem::Path filename(argv[1]);
bool create_trigger = false;
//setup writer
if (argc > 1)
{
if (filename.getExtension() == "dat")
{
writer = create_writer_for_adtf2(filename, stream_id);
}
else if (filename.getExtension() == "adtfdat")
{
writer = create_writer_for_adtf3(filename, stream_id);
create_trigger = true;
}
else
{
throw std::invalid_argument("usage: canwriter <adtfdat|dat>");
}
}
else
{
throw std::invalid_argument("usage: canwriter <adtfdat|dat>");
}
//short/long description is separated by \n
writer->setFileDescription("can test \nfile with a can_data stream");
//fill CAN data
tCANData message_data = {};
{
message_data.sHeader.ui8Tag = tCANData::MT_Data;
message_data.sHeader.ui8Channel = 03;
for (uint8_t data = 0; data < 8; ++data)
{
message_data.sData.aui8Data[data] = data;
}
message_data.sData.ui8Length = 8;
}
//write to stream
for (uint32_t counter = 0; counter < 99; ++counter)
{
message_data.sData.ui32Id = counter;
DefaultSample sample;
sample.setTimeStamp(std::chrono::microseconds(counter * 10000));
sample.setContent(message_data);
//write the sample to the file
writer->write(stream_id, std::chrono::microseconds(counter * 10000), sample);
//if you want a trigger within this stream you need to set it !!
//this will result in a data trigger within adtf
//otherwise the player will NOT create a Data Trigger
if (create_trigger)
{
writer->writeTrigger(stream_id, std::chrono::microseconds(counter * 10000));
}
}
std::cout << "File written to: " << filename.toString().c_str() << std::endl;
}
catch (const std::exception& ex)
{
std::cout << ex.what() << std::endl;
}
return 0;
}
@ adtf3
target version is ADTF 3 file, containing samples, triggers and stream types.
Definition: adtf_file_writer.h:319
@ adtf2
target version is ADTF 2 file, containing samples only.
Definition: adtf_file_writer.h:314
namespace for ADTF File library
Definition: adtf2_adtf_core_media_sample_deserializer.h:25

Copyright © CARIAD SE.
Generated on Fri Apr 19 2024 by doxygen 1.9.1
GIT Commit Hash: 82d535f82776c20b12fc60740bdae991b62444a7