MDF Lib 2.2
Interface against MDF 3/4 files
|
Interface against an MDF writer object. More...
#include <mdfwriter.h>
Public Member Functions | |
MdfWriter ()=default | |
Default constructor. | |
virtual | ~MdfWriter () |
Default destructor. | |
MdfWriter (const MdfWriter &writer)=delete | |
MdfWriter & | operator= (const MdfWriter &writer)=delete |
std::string | Name () const |
Returns the filename without extension and path (stem). | |
bool | Init (const std::string &filename) |
Initiate the file. | |
bool | IsFileNew () const |
Returns true if this is a new file. | |
void | PreTrigTime (double pre_trig_time) |
Sets the pre-trig time (s) of the writer. | |
double | PreTrigTime () const |
Pre-trig time (s). | |
uint64_t | StartTime () const |
Returns start time in nano-seconds since 1970. | |
uint64_t | StopTime () const |
Returns stop time in nano-seconds since 1970. | |
MdfFile * | GetFile () const |
Returns the MDF file interface. | |
IHeader * | Header () const |
Returns the header block (HD). | |
bool | CreateBusLogConfiguration () |
Creates all default DG, CG and CN blocks that bus loggers uses. | |
IDataGroup * | CreateDataGroup () |
Create a new data group (DG) block. | |
virtual IChannelConversion * | CreateChannelConversion (IChannel *parent)=0 |
Create a new channel conversion (CC) block. | |
virtual bool | InitMeasurement () |
Initialize the sample queue and write any unwritten block to the file. | |
virtual void | SaveSample (const IChannelGroup &group, uint64_t time) |
Saves a sample record for a channel group. | |
void | SaveCanMessage (const IChannelGroup &group, uint64_t time, const CanMessage &msg) |
Saves a CAN message into a bus logger channel group. | |
virtual void | StartMeasurement (uint64_t start_time) |
Starts the measurement. | |
virtual void | StartMeasurement (ITimestamp &start_time) |
Starts the measurement. | |
virtual void | StopMeasurement (uint64_t stop_time) |
Stops the measurement. | |
virtual void | StopMeasurement (ITimestamp &start_time) |
Stops the measurement. | |
virtual bool | FinalizeMeasurement () |
Stop the sample queue and write all unwritten blocks to the file. | |
void | BusType (MdfBusType type) |
Only used when doing bus logging. It defines the default channel and channel group names when doing bus logging. | |
MdfBusType | BusType () const |
Returns the type of bus the MDF file is associated with. Only used when doing bus logging. | |
std::string_view | BusTypeAsString () const |
Returns the bus type as text. | |
void | StorageType (MdfStorageType type) |
Only used when doing bus logging. It defines how raw data is stored. | |
MdfStorageType | StorageType () const |
Returns the type of data storage the MDF file is associated with. Only used when doing bus logging. | |
void | MaxLength (uint32_t max_length) |
Sets max number of payload data bytes. | |
uint32_t | MaxLength () const |
Returns maximum number of payload data bytes. | |
void | CompressData (bool compress) |
If set to true, the data block will be compressed. | |
bool | CompressData () const |
Returns true if the data block is compressed. | |
Static Public Member Functions | |
static IChannelGroup * | CreateChannelGroup (IDataGroup *parent) |
Create a new channel group (CG) block. | |
static IChannel * | CreateChannel (IChannelGroup *parent) |
Creates a new channel (CN) block. | |
Protected Types | |
enum class | WriteState : uint8_t { Create , Init , StartMeas , StopMeas , Finalize } |
Internal state of the thread. More... | |
using | SampleQueue = std::deque<SampleRecord> |
Sample queue. | |
Protected Attributes | |
std::atomic< WriteState > | write_state_ |
Keeps track of the worker thread state. | |
std::unique_ptr< MdfFile > | mdf_file_ |
Holds the actual file object. | |
std::string | filename_ |
Full name of file with path and extension. | |
std::atomic< uint64_t > | pre_trig_time_ = 0 |
Nanoseconds difference. | |
std::atomic< uint64_t > | start_time_ = 0 |
Nanoseconds since 1970. | |
std::atomic< uint64_t > | stop_time_ = 0 |
Nanoseconds since 1970. | |
std::thread | work_thread_ |
Sample queue thread. | |
std::atomic_bool | stop_thread_ = false |
Set to true to stop the thread. | |
std::mutex | locker_ |
Mutex for thread-safe handling of the sample queue. | |
std::condition_variable | sample_event_ |
Used internally. | |
std::atomic< size_t > | sample_queue_size_ = 0 |
Used to trig flushing to disc. | |
SampleQueue | sample_queue_ |
Sample queue. | |
The MDF writer purpose is to create MDF files. It simplifies the writing into some steps.
The first is to create type of writer. This is done by using the MdfFactory::CreateMdfWriter() function.
The next step is to call the Init() function with a filename with valid path. The Init function will create the actual MDF file object and check if it exist or not. If it exist, it reads in the existing file content so it can be appended.
The next step is to prepare the file for a new measurement. This is done by creating the DG/CG/CN/CC blocks that defines the measurement. Note that it is the last DG block that is the target.
Next step is to call the InitMeasurement() function. This create a thread that handle the queue of samples. The function also write the configuration to the file and closes it.
The user shall know starts adding samples to the queue by first setting the current channel value to each channel and then call the SaveSample() function for each channel group (CG). Note that no samples are saved to the file yet. The max queue size is set to the pre-trig time, see PreTrigTime().
At some point the user shall call the StartMeasurement() function. The sample queue is now saved onto the file. The save rate is actually dependent if CompressData() is enabled or not. If compression is used the data is saved at the 4MB size or a 10 minute max. If not using compression, the samples are saved each 10 second.
The user shall now call StopMeasurement() which flush out the remaining sample queue. After stopping the queue, the user may add some extra block as event (EV) and attachment (AT) blocks.
The FinalizeMeasurement() function. Stops the thread and write all unwritten blocks to the file.
|
strongprotected |
|
inlinenodiscard |
|
inline |
When using the MDF writer as a bus logger, the naming of channel groups and channels are defined in an ASAM standard. The naming is depending on the basic low level protocol.
type | Type of basic protocol on the bus. |
bool mdf::MdfWriter::CreateBusLogConfiguration | ( | ) |
This function create all data groups, channel groups and channels for a typical bus logger. Before calling this function, set the bus and storage types as this function uses these settings.
|
inline |
Returns the MDF file interface. The user may change the file version and the finalize status of the file. By default is the MDF 4.2 or 3.2 version of the file used.
bool mdf::MdfWriter::Init | ( | const std::string & | filename | ) |
Initiate the writer by defining which file it shall work with. Note that appending an existing file is supported.
filename | Filename with full path. |
|
inline |
Number of payload data bytes is default set to 8 bytes. Standard CAN may have max 8 byte of data while CAN FD can have up to 64 bytes data bytes. If you should store CAN FD data, you should set the max length to 64 bytes.
max_length | Maximum number of payload data bytes. |
void mdf::MdfWriter::PreTrigTime | ( | double | pre_trig_time | ) |
Sets the pre-trig time (note seconds). This defines the maximum number of samples the writer holds internally, before the measurement is started.
pre_trig_time | Pre-trig time in seconds. |
void mdf::MdfWriter::SaveCanMessage | ( | const IChannelGroup & | group, |
uint64_t | time, | ||
const CanMessage & | msg ) |
This function replace the normal SaveSample() function. It shall be used when logging CAN/CAN-FD messages into a standard ASAM bus logger configuration.
As before the function creates a record byte array and puts it onto an internal sample buffer. The time shall be absolute time (ns since 1970).
group | Reference to the channel group (CG). |
time | Absolute time nano-seconds since 1970. |
msg | The CAN message to store. |
|
virtual |
Call this function after all channel values have been updated with the latest value. The function creates the a record byte buffer and puts the buffer onto an internal sample buffer.
The time shall be absolute time (nano-seconds since 1970-01-01). Note that the function or actually the internal queue, assume that the samples are added in chronological order. The time will be converted to a relative time before it is stored onto the disc. The will be relative to the start time, see StartMeasurement() function.
|
inlinenodiscard |
|
inline |
Defines how the raw data (payload) is stored. Only used when doing bus logging.
By default the fixed length storage is used. This means that the data records have fixed length. This is the traditional way of storage. Variable length channels as strings, are stored in separate signal data blocks (SD).
The Variable Length Signal Data (VLSD) is stored in the data block. In practice this option is only used when doing bus logging.
The Maximum Length Signal Data is typicallay used when logging CAN bus traffic
type | Type of storage. |
|
protected |