AgIsoStack++
A control-function-focused implementation of the major ISOBUS and J1939 protocols
Loading...
Searching...
No Matches
spi_transaction_frame.cpp
Go to the documentation of this file.
1//================================================================================================
8//================================================================================================
11#include "isobus/utility/to_string.hpp"
12
13#include <cstring>
14
15namespace isobus
16{
17 SPITransactionFrame::SPITransactionFrame(const std::vector<std::uint8_t> *txBuffer, bool read) :
18 txBuffer(txBuffer),
19 read(read),
20 rxBuffer({})
21 {
22 if (read)
23 {
24 rxBuffer.reserve(txBuffer->size());
25 }
26 }
27
28 bool SPITransactionFrame::read_byte(std::size_t index, std::uint8_t &byte) const
29 {
30 bool retVal = false;
31
32 if (read)
33 {
34 if (index < rxBuffer.size())
35 {
36 byte = rxBuffer[index];
37 retVal = true;
38 }
39 else
40 {
41 LOG_ERROR("[SPIFrame] Tried to read byte at index " + isobus::to_string(index) + ", but the buffer only contains " + isobus::to_string(rxBuffer.size()) + " bytes");
42 }
43 }
44 else
45 {
46 LOG_ERROR("[SPIFrame] The transaction was not configured to read, but tried to read byte at index: " + isobus::to_string(index));
47 }
48 return retVal;
49 }
50
51 bool SPITransactionFrame::read_bytes(std::size_t index, std::uint8_t *buffer, std::size_t length) const
52 {
53 bool retVal = false;
54 if (read)
55 {
56 if (index + length <= rxBuffer.size())
57 {
58 std::memcpy(buffer, &rxBuffer[index], length);
59 retVal = true;
60 }
61 else
62 {
63 LOG_ERROR("[SPIFrame] Tried to read " + isobus::to_string(length) + " bytes at index " + isobus::to_string(index) + ", but the buffer only contains " + isobus::to_string(rxBuffer.size()) + " bytes");
64 }
65 }
66 else
67 {
68 LOG_ERROR("[SPIFrame] The transaction was not configured to read, but tried to read " + isobus::to_string(length) + " bytes at index: " + isobus::to_string(index));
69 }
70 return retVal;
71 }
72
73 std::vector<std::uint8_t> &SPITransactionFrame::get_rx_buffer()
74 {
75 return rxBuffer;
76 }
77
78 const std::vector<std::uint8_t> *SPITransactionFrame::get_tx_buffer() const
79 {
80 return txBuffer;
81 }
82
84 {
85 return read;
86 }
87}
A class that acts as a logging sink. The intent is that someone could make their own derived class of...
const bool read
If true, the plugin will read the response to the write operation.
const std::vector< std::uint8_t > * get_tx_buffer() const
Get the buffer to transmit.
std::vector< std::uint8_t > rxBuffer
The buffer to store the response in.
bool get_is_read() const
Get whether the interface should read the response to the write operation.
bool read_byte(std::size_t index, std::uint8_t &byte) const
Read a byte from the response buffer.
std::vector< std::uint8_t > & get_rx_buffer()
Get the buffer to store the response in.
const std::vector< std::uint8_t > * txBuffer
The buffer to transmit.
SPITransactionFrame(const std::vector< std::uint8_t > *txBuffer, bool read=false)
Construct a new SPITransactionFrame object.
bool read_bytes(std::size_t index, std::uint8_t *buffer, std::size_t length) const
Read multiple bytes from the response buffer.
This namespace encompasses all of the ISO11783 stack's functionality to reduce global namespace pollu...
A class containing the data for a single SPI transaction. Used in combination with SPIHardwarePlugin ...