Bus Messages 1.0
Bus Message Serialization Library
Loading...
Searching...
No Matches
ibusmessage.h
Go to the documentation of this file.
1/*
2* Copyright 2025 Ingemar Hedvall
3* SPDX-License-Identifier: MIT
4*/
5
12
13#pragma once
14#include <cstdint>
15#include <memory>
16#include <vector>
17
18namespace bus {
19
21enum class BusMessageType : uint16_t {
22 Unknown = 0,
23 Ctrl_BusChannel = 1,
24 CAN_DataFrame = 10,
25 CAN_RemoteFrame = 11,
26 CAN_ErrorFrame = 12,
27 CAN_OverloadFrame = 13,
28
29};
30
56class IBusMessage {
57public:
58
59 IBusMessage() = default;
67
68 virtual ~IBusMessage() = default;
69
78 static std::shared_ptr<IBusMessage> Create(BusMessageType type);
79
88 virtual void ToRaw(std::vector<uint8_t>& dest) const;
89
95 virtual void FromRaw(const std::vector<uint8_t>& source);
96
101 [[nodiscard]] BusMessageType Type() const { return type_; }
102
111 void Version(uint16_t version) { version_ = version; }
118 [[nodiscard]] uint16_t Version() const { return version_; }
119
126 [[nodiscard]] uint32_t Size() const { return size_;};
127
134 [[nodiscard]] bool Valid() const { return valid_;};
135
144 void Timestamp(uint64_t timestamp) { timestamp_ = timestamp; }
145
153 [[nodiscard]] uint64_t Timestamp() const { return timestamp_; }
154
173 void BusChannel(uint16_t channel) { bus_channel_ = channel; }
174
193 [[nodiscard]] uint16_t BusChannel() const { return bus_channel_; }
194
195protected:
204 void Size(uint32_t size) const {size_ = size; }
205
213 void Valid(bool valid) const { valid_ = valid;}
214
215private:
216 uint64_t timestamp_ = 0;
217 BusMessageType type_ = BusMessageType::Unknown;
218 uint16_t version_ = 0;
219 uint8_t bus_channel_ = 0;
220
221 mutable uint32_t size_ = 18;
222 mutable bool valid_ = true;
223
224};
225
226} // bus
227
virtual void FromRaw(const std::vector< uint8_t > &source)
Deserialize the message.
uint64_t Timestamp() const
Retuns the timestampe.
Definition ibusmessage.h:153
bool Valid() const
Returns true if the message is valid.
Definition ibusmessage.h:134
static std::shared_ptr< IBusMessage > Create(BusMessageType type)
Creates a message by its type.
IBusMessage(BusMessageType type)
void Valid(bool valid) const
Sets the message valid or invalid.
Definition ibusmessage.h:213
uint16_t Version() const
Returns the version number of the message.
Definition ibusmessage.h:118
BusMessageType Type() const
Returns type of message.
Definition ibusmessage.h:101
void Version(uint16_t version)
Sets the vewrsion number for a message.
Definition ibusmessage.h:111
void Size(uint32_t size) const
Sets the total size of the message.
Definition ibusmessage.h:204
void Timestamp(uint64_t timestamp)
Sets the absolute time.
Definition ibusmessage.h:144
uint16_t BusChannel() const
Returns the bus channel number.
Definition ibusmessage.h:193
uint32_t Size() const
Returns the total size of the message.
Definition ibusmessage.h:126
virtual void ToRaw(std::vector< uint8_t > &dest) const
Serialize the message.
void BusChannel(uint16_t channel)
Sets the source channel.
Definition ibusmessage.h:173
Main namespace for the MDF library.
Definition buslogstream.h:24
BusMessageType
Defines all message types.
Definition ibusmessage.h:21