MDF Lib 2.2
Interface against MDF 3/4 files
Loading...
Searching...
No Matches
mdfwriter.h
Go to the documentation of this file.
1/*
2 * Copyright 2022 Ingemar Hedvall
3 * SPDX-License-Identifier: MIT
4 */
5
6#pragma once
7#include <atomic>
8#include <condition_variable>
9#include <deque>
10#include <mutex>
11#include <string>
12#include <thread>
13#include <map>
14
15#include "mdf/mdffile.h"
16#include "mdf/samplerecord.h"
17#include "mdf/canmessage.h"
18
22namespace mdf {
23
29enum class MdfBusType : int {
30 CAN,
31 LIN,
32 FlexRay,
33 MOST,
34 Ethernet,
35 UNKNOWN
36};
37
53enum class MdfStorageType : int {
57};
58
59class IChannelGroup;
60class IChannel;
61class IChannelConversion;
62class IDataGroup;
63
105 public:
106 MdfWriter() = default;
107 virtual ~MdfWriter();
108
109 MdfWriter(const MdfWriter& writer) = delete;
110 MdfWriter& operator=(const MdfWriter& writer) = delete;
111
113 [[nodiscard]] std::string Name() const;
114
122 bool Init(const std::string& filename);
123
125 [[nodiscard]] bool IsFileNew() const {
127 }
128
135 void PreTrigTime(double pre_trig_time);
136 [[nodiscard]] double PreTrigTime() const;
137
139 [[nodiscard]] uint64_t StartTime() const { return start_time_; }
141 [[nodiscard]] uint64_t StopTime() const { return stop_time_; }
142
150 MdfFile* GetFile() const { return mdf_file_.get(); }
151
152 [[nodiscard]] IHeader* Header() const;
153
161
163 [[nodiscard]] IDataGroup* CreateDataGroup();
165 [[nodiscard]] static IChannelGroup* CreateChannelGroup(IDataGroup* parent);
167 [[nodiscard]] static IChannel* CreateChannel(IChannelGroup* parent);
170
174 virtual bool InitMeasurement();
175
188 virtual void SaveSample(const IChannelGroup& group, uint64_t time);
189
202 void SaveCanMessage(const IChannelGroup& group, uint64_t time,
203 const CanMessage& msg);
204
206 virtual void StartMeasurement(uint64_t start_time);
207
209 virtual void StartMeasurement(ITimestamp &start_time);
211 virtual void StopMeasurement(uint64_t stop_time);
213 virtual void StopMeasurement(ITimestamp &start_time);
214
217 virtual bool FinalizeMeasurement();
218
227 void BusType(MdfBusType type) { bus_type_ = type; }
228
233 [[nodiscard]] MdfBusType BusType() const { return bus_type_; }
234
236 [[nodiscard]] std::string_view BusTypeAsString() const;
237
256 void StorageType(MdfStorageType type) { storage_type_ = type; }
257
262 [[nodiscard]] MdfStorageType StorageType() const { return storage_type_; }
263
272 void MaxLength(uint32_t max_length) {max_length_ = max_length;};
273
275 [[nodiscard]] uint32_t MaxLength() const { return max_length_; }
276
278 void CompressData(bool compress) {compress_data_ = compress;}
280 [[nodiscard]] bool CompressData() const { return compress_data_;}
281
282 protected:
284 enum class WriteState : uint8_t {
285 Create,
286 Init,
287 StartMeas,
288 StopMeas,
289 Finalize
290 };
291 std::atomic<WriteState> write_state_ =
293
294 std::unique_ptr<MdfFile> mdf_file_;
295 std::string filename_;
296
297 std::atomic<uint64_t> pre_trig_time_ = 0;
298 std::atomic<uint64_t> start_time_ = 0;
299 std::atomic<uint64_t> stop_time_ = 0;
300
301 std::thread work_thread_;
302 std::atomic_bool stop_thread_ = false;
303 std::mutex locker_;
304 std::condition_variable sample_event_;
305 std::atomic<size_t> sample_queue_size_ = 0;
306
307 using SampleQueue = std::deque<SampleRecord>;
309
310 virtual void CreateMdfFile() = 0;
311 virtual bool PrepareForWriting() = 0;
312 virtual void SetDataPosition(std::FILE* file);
313 virtual bool WriteSignalData(std::FILE* file);
314
316 void WorkThread();
317
318 virtual void TrimQueue();
320 virtual void SaveQueue(std::unique_lock<std::mutex>& lock);
322 virtual void CleanQueue(std::unique_lock<std::mutex>& lock);
324 void IncrementNofSamples(uint64_t record_id) const;
326 virtual void SetLastPosition(std::FILE* file) = 0;
327
328
329 private:
330 bool compress_data_ = false;
333 uint32_t max_length_ = 8;
334 std::map<uint64_t, const IChannel*> master_channels_;
335 void RecalculateTimeMaster();
336 void CreateCanConfig(IDataGroup& dg_block) const;
337
356 void CreateCanDataFrameChannel(IChannelGroup& group) const;
357
372 void CreateCanRemoteFrameChannel(IChannelGroup& group) const;
373
394 void CreateCanErrorFrameChannel(IChannelGroup& group) const;
395
405 static void CreateCanOverloadFrameChannel(IChannelGroup& group);
406};
407
408} // namespace mdf
Simple wrapper around a CAN or CAN FD message.
Helper class when logging CAN and CAN FD messages.
Definition canmessage.h:47
Defines a channel conversion (CC) block.
Definition ichannelconversion.h:142
Interface against a channel group (CG) block.
Definition ichannelgroup.h:66
Defines a MDF channel (CN) block.
Definition ichannel.h:126
Interface to a data group (DG) block.
Definition idatagroup.h:42
Interface class against an MDF HD block.
Definition iheader.h:34
Interface for timestamp handling in MDF files.
Definition itimestamp.h:17
Implements an user interface against a MDF file.
Definition mdffile.h:37
Interface against an MDF writer object.
Definition mdfwriter.h:104
static IChannel * CreateChannel(IChannelGroup *parent)
Creates a new channel (CN) block.
MdfStorageType StorageType() const
Returns the type of data storage the MDF file is associated with. Only used when doing bus logging.
Definition mdfwriter.h:262
void BusType(MdfBusType type)
Only used when doing bus logging. It defines the default channel and channel group names when doing b...
Definition mdfwriter.h:227
std::atomic< uint64_t > pre_trig_time_
Nanoseconds difference.
Definition mdfwriter.h:297
bool CompressData() const
Returns true if the data block is compressed.
Definition mdfwriter.h:280
std::condition_variable sample_event_
Used internally.
Definition mdfwriter.h:304
virtual bool FinalizeMeasurement()
Stop the sample queue and write all unwritten blocks to the file.
SampleQueue sample_queue_
Sample queue.
Definition mdfwriter.h:308
IDataGroup * CreateDataGroup()
Create a new data group (DG) block.
void WorkThread()
Worker thread function.
double PreTrigTime() const
Pre-trig time (s).
std::unique_ptr< MdfFile > mdf_file_
Holds the actual file object.
Definition mdfwriter.h:294
virtual void StartMeasurement(ITimestamp &start_time)
Starts the measurement.
virtual void StartMeasurement(uint64_t start_time)
Starts the measurement.
void SaveCanMessage(const IChannelGroup &group, uint64_t time, const CanMessage &msg)
Saves a CAN message into a bus logger channel group.
virtual void StopMeasurement(ITimestamp &start_time)
Stops the measurement.
std::atomic_bool stop_thread_
Set to true to stop the thread.
Definition mdfwriter.h:302
virtual bool PrepareForWriting()=0
Prepare for writing.
MdfFile * GetFile() const
Returns the MDF file interface.
Definition mdfwriter.h:150
bool CreateBusLogConfiguration()
Creates all default DG, CG and CN blocks that bus loggers uses.
void StorageType(MdfStorageType type)
Only used when doing bus logging. It defines how raw data is stored.
Definition mdfwriter.h:256
virtual void SetLastPosition(std::FILE *file)=0
Set the last file position.
static IChannelGroup * CreateChannelGroup(IDataGroup *parent)
Create a new channel group (CG) block.
std::mutex locker_
Mutex for thread-safe handling of the sample queue.
Definition mdfwriter.h:303
void PreTrigTime(double pre_trig_time)
Sets the pre-trig time (s) of the writer.
virtual void CreateMdfFile()=0
Creates an MDF file.
bool Init(const std::string &filename)
Initiate the file.
virtual bool WriteSignalData(std::FILE *file)
Write an SD block.
MdfBusType BusType() const
Returns the type of bus the MDF file is associated with. Only used when doing bus logging.
Definition mdfwriter.h:233
std::string filename_
Full name of file with path and extension.
Definition mdfwriter.h:295
virtual IChannelConversion * CreateChannelConversion(IChannel *parent)=0
Create a new channel conversion (CC) block.
void CompressData(bool compress)
If set to true, the data block will be compressed.
Definition mdfwriter.h:278
virtual void SaveSample(const IChannelGroup &group, uint64_t time)
Saves a sample record for a channel group.
std::thread work_thread_
Sample queue thread.
Definition mdfwriter.h:301
std::atomic< uint64_t > stop_time_
Nanoseconds since 1970.
Definition mdfwriter.h:299
virtual bool InitMeasurement()
Initialize the sample queue and write any unwritten block to the file.
std::string_view BusTypeAsString() const
Returns the bus type as text.
uint64_t StartTime() const
Returns start time in nano-seconds since 1970.
Definition mdfwriter.h:139
IHeader * Header() const
Returns the header block (HD).
virtual ~MdfWriter()
Default destructor.
virtual void CleanQueue(std::unique_lock< std::mutex > &lock)
Flush the sample queue.
std::atomic< WriteState > write_state_
Keeps track of the worker thread state.
Definition mdfwriter.h:291
MdfWriter()=default
Default constructor.
void IncrementNofSamples(uint64_t record_id) const
Increment the sample counter.
std::atomic< uint64_t > start_time_
Nanoseconds since 1970.
Definition mdfwriter.h:298
virtual void TrimQueue()
Trims the sample queue.
virtual void SaveQueue(std::unique_lock< std::mutex > &lock)
Saves the queue to file.
void StopWorkThread()
Stops the worker thread.
virtual void SetDataPosition(std::FILE *file)
Set the data position.
WriteState
Internal state of the thread.
Definition mdfwriter.h:284
@ StopMeas
Stop saving samples. OK to.
@ StartMeas
Start saving samples to file.
@ Create
Only at first measurement.
@ Init
Start work thread and start collecting samples.
@ Finalize
OK to add new DG and CG blocks.
virtual void StopMeasurement(uint64_t stop_time)
Stops the measurement.
void MaxLength(uint32_t max_length)
Sets max number of payload data bytes.
Definition mdfwriter.h:272
bool IsFileNew() const
Returns true if this is a new file.
Definition mdfwriter.h:125
uint32_t MaxLength() const
Returns maximum number of payload data bytes.
Definition mdfwriter.h:275
std::atomic< size_t > sample_queue_size_
Used to trig flushing to disc.
Definition mdfwriter.h:305
uint64_t StopTime() const
Returns stop time in nano-seconds since 1970.
Definition mdfwriter.h:141
std::deque< SampleRecord > SampleQueue
Sample queue.
Definition mdfwriter.h:307
std::string Name() const
Returns the filename without extension and path (stem).
Interface against an MDF file object.
Main namespace for the MDF library.
Definition canmessage.h:17
@ FlexRay
FlexRay bus.
@ Ethernet
EtherNet bus.
MdfStorageType
Enumerate that defines how the raw data is stored. By default the fixed length record is stored....
Definition mdfwriter.h:53
@ VlsdStorage
Using variable length storage.
@ FixedLengthStorage
The default is to use fixed length records.
@ MlsdStorage
Using maximum length storage.
MdfBusType
Enumerate that defines type of bus. Only relevant for bus logging.
Definition mdfwriter.h:29
@ UNKNOWN
Unknown bus type (Default)
@ CAN
CAN or CAN-FD bus.
@ MOST
MOST bus.