AgIsoStack++
A control-function-focused implementation of the major ISOBUS and J1939 protocols
Loading...
Searching...
No Matches
can_message_data.cpp
1//================================================================================================
8//================================================================================================
9
11
12#include <algorithm>
13
14namespace isobus
15{
17 {
18 vector::resize(size);
19 }
20
21 CANMessageDataVector::CANMessageDataVector(const std::vector<std::uint8_t> &data)
22 {
23 vector::assign(data.begin(), data.end());
24 }
25
26 CANMessageDataVector::CANMessageDataVector(const std::uint8_t *data, std::size_t size)
27 {
28 vector::assign(data, data + size);
29 }
30
31 std::size_t CANMessageDataVector::size() const
32 {
33 return vector::size();
34 }
35
36 std::uint8_t CANMessageDataVector::get_byte(std::size_t index)
37 {
38 return vector::at(index);
39 }
40
41 void CANMessageDataVector::set_byte(std::size_t index, std::uint8_t value)
42 {
43 vector::at(index) = value;
44 }
45
47 {
48 return CANDataSpan(vector::data(), vector::size());
49 }
50
51 std::unique_ptr<CANMessageData> CANMessageDataVector::copy_if_not_owned(std::unique_ptr<CANMessageData> self) const
52 {
53 // We know that a CANMessageDataVector is always owned by itself, so we can just return itself.
54 return self;
55 }
56
57 CANMessageDataView::CANMessageDataView(const std::uint8_t *ptr, std::size_t len) :
58 CANDataSpan(ptr, len)
59 {
60 }
61
62 std::size_t CANMessageDataView::size() const
63 {
64 return DataSpan::size();
65 }
66
67 std::uint8_t CANMessageDataView::get_byte(std::size_t index)
68 {
69 return DataSpan::operator[](index);
70 }
71
73 {
74 return CANDataSpan(DataSpan::begin(), DataSpan::size());
75 }
76
77 std::unique_ptr<CANMessageData> CANMessageDataView::copy_if_not_owned(std::unique_ptr<CANMessageData>) const
78 {
79 // A view doesn't own the data, so we need to make a copy.
80 return std::unique_ptr<CANMessageData>(new CANMessageDataVector(DataSpan::begin(), DataSpan::size()));
81 }
82
84 DataChunkCallback callback,
85 void *parentPointer,
86 std::size_t chunkSize) :
87 totalSize(size),
88 callback(callback),
89 parentPointer(parentPointer),
90 buffer(chunkSize),
91 bufferSize(chunkSize)
92 {
93 }
94
95 std::size_t CANMessageDataCallback::size() const
96 {
97 return totalSize;
98 }
99
100 std::uint8_t CANMessageDataCallback::get_byte(std::size_t index)
101 {
102 if (index >= totalSize)
103 {
104 return 0;
105 }
106
107 if ((index >= dataOffset + bufferSize) || (index < dataOffset) || (!initialized))
108 {
109 initialized = true;
110 dataOffset = index;
112 }
113 return buffer[index - dataOffset];
114 }
115
116 std::unique_ptr<CANMessageData> CANMessageDataCallback::copy_if_not_owned(std::unique_ptr<CANMessageData> self) const
117 {
118 // A callback doesn't own it's data, but it does own the callback function, so we can just return itself.
119 return self;
120 }
121
122} // namespace isobus
An interface class that represents data payload of a CAN message of arbitrary length.
std::size_t dataOffset
The offset of the data in the buffer.
bool initialized
Whether the buffer has been initialized.
std::size_t bufferSize
The size of the buffer.
std::size_t size() const override
Get the size of the data.
CANMessageDataCallback(std::size_t size, DataChunkCallback callback, void *parentPointer=nullptr, std::size_t chunkSize=7)
Constructor for transport data that uses a callback function.
std::uint8_t get_byte(std::size_t index) override
Get the byte at the given index.
std::vector< std::uint8_t > buffer
The buffer to store the data chunks.
DataChunkCallback callback
The callback function to be called for each data chunk.
std::size_t totalSize
The total size of the data.
void * parentPointer
The parent object that gets passed to the callback function.
std::unique_ptr< CANMessageData > copy_if_not_owned(std::unique_ptr< CANMessageData > self) const override
If the data isn't owned by this class, make a copy of the data.
A class that represents data of a CAN message by holding a vector of bytes.
CANMessageDataVector(std::size_t size)
Construct a new CANMessageDataVector object.
std::size_t size() const override
Get the size of the data.
std::unique_ptr< CANMessageData > copy_if_not_owned(std::unique_ptr< CANMessageData > self) const override
If the data isn't owned by this class, make a copy of the data.
CANDataSpan data() const
Get the data span.
void set_byte(std::size_t index, std::uint8_t value)
Set the byte at the given index.
std::uint8_t get_byte(std::size_t index) override
Get the byte at the given index.
CANMessageDataView(const std::uint8_t *ptr, std::size_t len)
Construct a new CANMessageDataView object.
std::uint8_t get_byte(std::size_t index) override
Get the byte at the given index.
std::unique_ptr< CANMessageData > copy_if_not_owned(std::unique_ptr< CANMessageData > self) const override
If the data isn't owned by this class, make a copy of the data.
CANDataSpan data() const
Get the data span.
std::size_t size() const override
Get the size of the data.
This namespace encompasses all of the ISO11783 stack's functionality to reduce global namespace pollu...
DataSpan< const std::uint8_t > CANDataSpan
A read-only span of data for a CAN message.
bool(*)(std::uint32_t callbackIndex, std::uint32_t bytesOffset, std::uint32_t numberOfBytesNeeded, std::uint8_t *chunkBuffer, void *parentPointer) DataChunkCallback
A callback to get chunks of data for transfer by a protocol.