Bus Messages 1.0
Bus Message Serialization Library
Loading...
Searching...
No Matches
candataframe.h
Go to the documentation of this file.
1/*
2* Copyright 2025 Ingemar Hedvall
3* SPDX-License-Identifier: MIT
4*/
5
12#pragma once
13
14 #include <bitset>
15#include <cstdint>
16
17#include "bus/ibusmessage.h"
18
19namespace bus {
29
61class CanDataFrame : public IBusMessage {
62 public:
64 explicit CanDataFrame(CanErrorType type) = delete;
65
67 explicit CanDataFrame(const std::shared_ptr<IBusMessage>& message);
68
76 void MessageId(uint32_t msg_id);
77
84 [[nodiscard]] uint32_t MessageId() const;
85
87 void CanId(uint32_t can_id);
88
97 [[nodiscard]] uint32_t CanId() const;
98
103 void ExtendedId(bool extended );
104
109 [[nodiscard]] bool ExtendedId() const;
110
118 void Dlc(uint8_t dlc) { dlc_ = dlc;};
119
127 [[nodiscard]] uint8_t Dlc() const { return dlc_;};
128
130 void Crc(uint32_t crc) { crc_ = crc;};
131
133 [[nodiscard]] uint32_t Crc() const { return crc_;};
134
142 void DataLength(uint8_t data_length);
143 [[nodiscard]] uint8_t DataLength() const;
144
151 void DataBytes(const std::vector<uint8_t>& data);
152
154 [[nodiscard]] const std::vector<uint8_t>& DataBytes() const;
155
157 void Dir(bool transmit );
159 [[nodiscard]] bool Dir() const;
160
161 void Srr(bool srr );
162 [[nodiscard]] bool Srr() const;
163
164 void Edl(bool edl );
165 [[nodiscard]] bool Edl() const;
166
167 void Brs(bool brs );
168 [[nodiscard]] bool Brs() const;
169
170 void Esi(bool esi );
171 [[nodiscard]] bool Esi() const;
172
173 void Rtr(bool rtr );
174 [[nodiscard]] bool Rtr() const;
175
176 void WakeUp(bool wake_up );
177 [[nodiscard]] bool WakeUp() const;
178
179 void SingleWire(bool single_wire );
180 [[nodiscard]] bool SingleWire() const;
181
182 void R0(bool flag);
183 [[nodiscard]] bool R0() const;
184
185 void R1(bool flag);
186 [[nodiscard]] bool R1() const;
187/*
188 void BitPosition(uint16_t position); ///< Error bit position (error frame).
189 [[nodiscard]] uint16_t BitPosition() const; ///< Error bit position.
190
191 void ErrorType(CanErrorType error_type); ///< Type of error.
192 [[nodiscard]] CanErrorType ErrorType() const; ///< Type of error.
193*/
194 void FrameDuration(uint32_t duration);
195 [[nodiscard]] uint32_t FrameDuration() const;
196
197 static size_t DlcToLength(uint8_t dlc);
198
205 void ToRaw(std::vector<uint8_t>& dest) const override;
206
212 void FromRaw(const std::vector<uint8_t>& source) override;
213 private:
214 uint32_t message_id_ = 0;
215 uint8_t dlc_ = 0;
216 std::bitset<16> flags_;
217 std::vector<uint8_t> data_bytes_;
218 uint16_t bit_position_ = 0;
220 uint32_t frame_duration_ = 0;
221 uint32_t crc_ = 0;
222};
223
224} // namespace mdf
bool SingleWire() const
Indicate a single wire CAN bus.
void ToRaw(std::vector< uint8_t > &dest) const override
Serialize the message.
bool R0() const
Optional R0 flag.
void CanId(uint32_t can_id)
Returns the 11/27 bit CAN ID.
void WakeUp(bool wake_up)
Indicate a CAN bus wake up status.
uint32_t Crc() const
Returns the CRC code.
Definition candataframe.h:133
uint8_t DataLength() const
Returns number of data bytes.
bool Dir() const
Returns true if the message was transmitted.
uint32_t CanId() const
29/11 bit CAN message ID. Note that bit 31 is not used.
uint32_t FrameDuration() const
Frame duration in nano-seconds.
void Edl(bool edl)
Extended (CAN FD) data length.
void Brs(bool brs)
Bit rate switch (CAN FD).
void DataLength(uint8_t data_length)
Sets number of data bytes.
bool Esi() const
Error state indicator (CAN FD).
void Dlc(uint8_t dlc)
Sets the CAN message data length code.
Definition candataframe.h:118
uint32_t MessageId() const
DBC message ID. Note that bit 31 indicate extended ID.
void Dir(bool transmit)
If set true, the message was transmitted.
bool Rtr() const
Returns the RTR bit.
void Crc(uint32_t crc)
Sets the CRC code.
Definition candataframe.h:130
CanDataFrame(const std::shared_ptr< IBusMessage > &message)
Create a message from a raw byte array.
void SingleWire(bool single_wire)
Indicate a single wire CAN bus.
void Srr(bool srr)
Sets the SRR bit. *‍/.
void R1(bool flag)
Optional R1 flag.
const std::vector< uint8_t > & DataBytes() const
Returns a reference to the payload data bytes.
void ExtendedId(bool extended)
Set true if the CAN ID uses 20-bit addressing.
bool Brs() const
Bit rate switch (CAN FD).
void MessageId(uint32_t msg_id)
DBC message ID. Note that bit 31 indicate extended ID.
void Rtr(bool rtr)
Sets the RTR bit (remote frame).
void R0(bool flag)
Optional R0 flag.
static size_t DlcToLength(uint8_t dlc)
Return the data length by DLC.
void DataBytes(const std::vector< uint8_t > &data)
Sets the payload data bytes.
bool Srr() const
Returns the SRR bit.
CanDataFrame()
Deafult constructor.
bool WakeUp() const
Indicate a CAN bus wake up message.
void FrameDuration(uint32_t duration)
Frame duration in nano-seconds.
bool Edl() const
Extended (CAN FD) data length.
bool ExtendedId() const
Returns true if the CAN ID uses 29-bit addressing,.
bool R1() const
Optional R1 flag.
uint8_t Dlc() const
Returns the data length code (DLC).
Definition candataframe.h:127
void FromRaw(const std::vector< uint8_t > &source) override
Deserialize the message.
void Esi(bool esi)
Error state indicator (CAN FD).
Defines an interface against a bus message.
Main namespace for the MDF library.
Definition buslogstream.h:24
CanErrorType
Enumerate yhat defines type of CAN bus error.
Definition candataframe.h:21
@ BIT_STUFFING_ERROR
Bit stuffing error.
Definition candataframe.h:25
@ FORM_ERROR
CAN format error.
Definition candataframe.h:24
@ UNKNOWN_ERROR
Unspecified error.
Definition candataframe.h:22
@ BIT_ERROR
CAN bit error.
Definition candataframe.h:23
@ ACK_ERROR
Acknowledgement error.
Definition candataframe.h:27
@ CRC_ERROR
Checksum error.
Definition candataframe.h:26