AgIsoStack++
A control-function-focused implementation of the major ISOBUS and J1939 protocols
Loading...
Searching...
No Matches
can_message.hpp
Go to the documentation of this file.
1//================================================================================================
9//================================================================================================
10
11#ifndef CAN_MESSAGE_HPP
12#define CAN_MESSAGE_HPP
13
17#include "isobus/utility/data_span.hpp"
18
19#include <vector>
20
21namespace isobus
22{
24 using CANDataSpan = DataSpan<const std::uint8_t>;
25
26 //================================================================================================
30 //================================================================================================
32 {
33 public:
35 enum class Type
36 {
37 Transmit,
38 Receive,
39 };
40
42 enum class ByteFormat
43 {
44 LittleEndian,
45 BigEndian
46 };
47
52 static const std::uint32_t ABSOLUTE_MAX_MESSAGE_LENGTH = 117440505;
53
62 CANMessage(Type type,
64 const std::uint8_t *dataBuffer,
65 std::uint32_t length,
66 std::shared_ptr<ControlFunction> source,
67 std::shared_ptr<ControlFunction> destination,
68 std::uint8_t CANPort);
69
77 CANMessage(Type type,
79 std::vector<std::uint8_t> data,
80 std::shared_ptr<ControlFunction> source,
81 std::shared_ptr<ControlFunction> destination,
82 std::uint8_t CANPort);
83
87
90 Type get_type() const;
91
94 const std::vector<std::uint8_t> &get_data() const;
95
98 std::uint32_t get_data_length() const;
99
102 std::shared_ptr<ControlFunction> get_source_control_function() const;
103
107
110 std::shared_ptr<ControlFunction> get_destination_control_function() const;
111
115
118 bool is_broadcast() const;
119
122 bool is_destination_our_device() const;
123
127 bool is_destination(std::shared_ptr<ControlFunction> controlFunction) const;
128
132 bool is_source(std::shared_ptr<ControlFunction> controlFunction) const;
133
137
141 bool is_parameter_group_number(CANLibParameterGroupNumber parameterGroupNumber) const;
142
145 std::uint8_t get_can_port_index() const;
146
150 void set_data(const std::uint8_t *dataBuffer, std::uint32_t length);
151
155 void set_data(std::uint8_t dataByte, const std::uint32_t insertPosition);
156
159 void set_data_size(std::uint32_t length);
160
163 void set_identifier(const CANIdentifier &value);
164
170 std::uint8_t get_uint8_at(const std::uint32_t index) const;
171
177 std::int8_t get_int8_at(const std::uint32_t index) const;
178
185 std::uint16_t get_uint16_at(const std::uint32_t index, const ByteFormat format = ByteFormat::LittleEndian) const;
186
193 std::int16_t get_int16_at(const std::uint32_t index, const ByteFormat format = ByteFormat::LittleEndian) const;
194
201 std::uint32_t get_uint24_at(const std::uint32_t index, const ByteFormat format = ByteFormat::LittleEndian) const;
202
209 std::int32_t get_int24_at(const std::uint32_t index, const ByteFormat format = ByteFormat::LittleEndian) const;
210
217 std::uint32_t get_uint32_at(const std::uint32_t index, const ByteFormat format = ByteFormat::LittleEndian) const;
218
225 std::int32_t get_int32_at(const std::uint32_t index, const ByteFormat format = ByteFormat::LittleEndian) const;
226
233 std::uint64_t get_uint64_at(const std::uint32_t index, const ByteFormat format = ByteFormat::LittleEndian) const;
234
241 std::int64_t get_int64_at(const std::uint32_t index, const ByteFormat format = ByteFormat::LittleEndian) const;
242
249 bool get_bool_at(const std::uint32_t byteIndex, const std::uint8_t bitIndex, const std::uint8_t length = 1) const;
250
259 std::uint64_t get_data_custom_length(const std::uint32_t startBitIndex, const std::uint32_t length, const ByteFormat format = ByteFormat::LittleEndian) const;
260
261 private:
264 std::vector<std::uint8_t> data;
265 std::shared_ptr<ControlFunction> source;
266 std::shared_ptr<ControlFunction> destination;
267 std::uint8_t CANPortIndex;
268 };
269
270} // namespace isobus
271
272#endif // CAN_MESSAGE_HPP
Defines a base class to represent a generic ISOBUS control function.
Defines some PGNs that are used in the library or are very common.
A representation of a classical CAN identifier with utility functions for ectracting values that are ...
A utility class that allows easy interpretation of a 32 bit CAN identifier.
A class that represents a generic CAN message of arbitrary length.
std::int16_t get_int16_at(const std::uint32_t index, const ByteFormat format=ByteFormat::LittleEndian) const
Get a 16-bit signed integer from the buffer at a specific index. A 16-bit signed integer can hold a v...
bool is_source(std::shared_ptr< ControlFunction > controlFunction) const
Returns whether the message is originated from the control function.
static const std::uint32_t ABSOLUTE_MAX_MESSAGE_LENGTH
ISO11783-3 defines this: The maximum number of packets that can be sent in a single connection with e...
std::shared_ptr< ControlFunction > source
The source control function of the message.
Type
The internal message type.
@ Receive
Message is being received.
@ Transmit
Message is to be transmitted from the stack.
bool is_destination(std::shared_ptr< ControlFunction > controlFunction) const
Returns whether the message is destined for the control function.
bool has_valid_destination_control_function() const
Returns whether the message is sent to a specific device on the bus.
std::int32_t get_int24_at(const std::uint32_t index, const ByteFormat format=ByteFormat::LittleEndian) const
Get a right-aligned 24-bit integer from the buffer (returned as a int32_t) at a specific index....
Type messageType
The internal message type associated with the message.
Type get_type() const
Returns the CAN message type.
static CANMessage create_invalid_message()
Factory method to construct an intentionally invalid CANMessage.
ByteFormat
The different byte formats that can be used when reading bytes from the buffer.
void set_data(const std::uint8_t *dataBuffer, std::uint32_t length)
Sets the message data to the value supplied. Creates a copy.
std::uint8_t get_can_port_index() const
Returns the CAN channel index associated with the message.
const std::vector< std::uint8_t > & get_data() const
Gets a reference to the data in the CAN message.
std::uint32_t get_data_length() const
Returns the length of the data in the CAN message.
bool has_valid_source_control_function() const
Returns whether the message is sent by a device that claimed its address on the bus.
void set_data_size(std::uint32_t length)
Sets the size of the data payload.
std::uint32_t get_uint24_at(const std::uint32_t index, const ByteFormat format=ByteFormat::LittleEndian) const
Get a right-aligned 24-bit integer from the buffer (returned as a uint32_t) at a specific index....
std::uint64_t get_data_custom_length(const std::uint32_t startBitIndex, const std::uint32_t length, const ByteFormat format=ByteFormat::LittleEndian) const
Get a 64-bit unsinged integer from the buffer at a specific index but custom length Why 64 bit?...
CANIdentifier identifier
The CAN ID of the message.
std::shared_ptr< ControlFunction > get_source_control_function() const
Gets the source control function that the message is from.
std::uint8_t get_uint8_at(const std::uint32_t index) const
Get a 8-bit unsigned byte from the buffer at a specific index. A 8-bit unsigned byte can hold a value...
std::shared_ptr< ControlFunction > destination
The destination control function of the message.
bool is_parameter_group_number(CANLibParameterGroupNumber parameterGroupNumber) const
Compares the identifier of the message to the parameter group number (PGN) supplied.
std::vector< std::uint8_t > data
A data buffer for the message, used when not using data chunk callbacks.
void set_identifier(const CANIdentifier &value)
Sets the CAN ID of the message.
std::int32_t get_int32_at(const std::uint32_t index, const ByteFormat format=ByteFormat::LittleEndian) const
Get a 32-bit signed integer from the buffer at a specific index. A 32-bit signed integer can hold a v...
std::int8_t get_int8_at(const std::uint32_t index) const
Get a 8-bit signed byte from the buffer at a specific index. A 8-bit signed byte can hold a value bet...
std::uint16_t get_uint16_at(const std::uint32_t index, const ByteFormat format=ByteFormat::LittleEndian) const
Get a 16-bit unsigned integer from the buffer at a specific index. A 16-bit unsigned integer can hold...
CANIdentifier get_identifier() const
Returns the identifier of the message.
std::int64_t get_int64_at(const std::uint32_t index, const ByteFormat format=ByteFormat::LittleEndian) const
Get a 64-bit signed integer from the buffer at a specific index. A 64-bit signed integer can hold a v...
std::uint8_t CANPortIndex
The CAN channel index associated with the message.
bool get_bool_at(const std::uint32_t byteIndex, const std::uint8_t bitIndex, const std::uint8_t length=1) const
Get a bit-boolean from the buffer at a specific index.
std::uint32_t get_uint32_at(const std::uint32_t index, const ByteFormat format=ByteFormat::LittleEndian) const
Get a 32-bit unsigned integer from the buffer at a specific index. A 32-bit unsigned integer can hold...
CANMessage(Type type, CANIdentifier identifier, const std::uint8_t *dataBuffer, std::uint32_t length, std::shared_ptr< ControlFunction > source, std::shared_ptr< ControlFunction > destination, std::uint8_t CANPort)
Construct a CAN message from the parameters supplied.
std::shared_ptr< ControlFunction > get_destination_control_function() const
Gets the destination control function that the message is to.
bool is_broadcast() const
Returns whether the message is sent as a broadcast message / to all devices on the bus.
bool is_destination_our_device() const
Returns whether the message is destined for our device on the bus.
std::uint64_t get_uint64_at(const std::uint32_t index, const ByteFormat format=ByteFormat::LittleEndian) const
Get a 64-bit unsigned integer from the buffer at a specific index. A 64-bit unsigned integer can hold...
This namespace encompasses all of the ISO11783 stack's functionality to reduce global namespace pollu...
CANLibParameterGroupNumber
PGNs commonly used by the CAN stack.
DataSpan< const std::uint8_t > CANDataSpan
A read-only span of data for a CAN message.