AgIsoStack++
A control-function-focused implementation of the major ISOBUS and J1939 protocols
Loading...
Searching...
No Matches
can_extended_transport_protocol.hpp
Go to the documentation of this file.
1//================================================================================================
10//================================================================================================
11
12#ifndef CAN_EXTENDED_TRANSPORT_PROTOCOL_HPP
13#define CAN_EXTENDED_TRANSPORT_PROTOCOL_HPP
14
18
19namespace isobus
20{
26 {
27 public:
41
43 enum class ConnectionAbortReason : std::uint8_t
44 {
45 Reserved = 0,
48 Timeout = 3,
54 UnexpectedDataPacketOffsetReceived = 9, // Received a data packet offset outside the proper state
60 AnyOtherError = 250
61 };
62
65 {
66 public:
68
72
75 std::uint32_t get_total_bytes_transferred() const override;
76
77 protected:
79
82 void set_state(StateMachineState value);
83
86 std::uint8_t get_dpo_number_of_packets_remaining() const;
87
90 void set_dpo_number_of_packets(std::uint8_t value);
91
94 std::uint8_t get_dpo_number_of_packets() const;
95
98 std::uint8_t get_cts_number_of_packet_limit() const;
99
102 void set_cts_number_of_packet_limit(std::uint8_t value);
103
106 std::uint8_t get_last_sequence_number() const;
107
110 std::uint32_t get_last_packet_number() const;
111
114 void set_sequence_number_offset(std::uint32_t value);
115
118 void set_last_sequency_number(std::uint8_t value);
119
122 void set_acknowledged_packet_number(std::uint32_t value);
123
126 std::uint32_t get_last_acknowledged_packet_number() const;
127
130 std::uint32_t get_number_of_remaining_packets() const;
131
134 std::uint32_t get_total_number_of_packets() const;
135
136 private:
138
140 std::uint32_t sequenceNumberOffset = 0;
141 std::uint8_t lastSequenceNumber = 0;
143 std::uint8_t clearToSendPacketCountLimit = 0xFF;
144 };
145
146 static constexpr std::uint32_t REQUEST_TO_SEND_MULTIPLEXOR = 0x14;
147 static constexpr std::uint32_t CLEAR_TO_SEND_MULTIPLEXOR = 0x15;
148 static constexpr std::uint32_t DATA_PACKET_OFFSET_MULTIPLXOR = 0x16;
149 static constexpr std::uint32_t END_OF_MESSAGE_ACKNOWLEDGE_MULTIPLEXOR = 0x17;
150 static constexpr std::uint32_t CONNECTION_ABORT_MULTIPLEXOR = 0xFF;
151 static constexpr std::uint32_t MAX_PROTOCOL_DATA_LENGTH = 117440505;
152 static constexpr std::uint16_t T1_TIMEOUT_MS = 750;
153 static constexpr std::uint16_t T2_T3_TIMEOUT_MS = 1250;
154 static constexpr std::uint16_t T4_TIMEOUT_MS = 1050;
155 static constexpr std::uint8_t TR_TIMEOUT_MS = 200;
156 static constexpr std::uint8_t SEQUENCE_NUMBER_DATA_INDEX = 0;
157 static constexpr std::uint8_t PROTOCOL_BYTES_PER_FRAME = 7;
158
167
169 void update();
170
175 bool has_session(std::shared_ptr<ControlFunction> source, std::shared_ptr<ControlFunction> destination);
176
180 const std::vector<std::shared_ptr<ExtendedTransportProtocolSession>> &get_sessions() const;
181
184 void process_message(const CANMessage &message);
185
194 bool protocol_transmit_message(std::uint32_t parameterGroupNumber,
195 std::unique_ptr<CANMessageData> &data,
196 std::shared_ptr<ControlFunction> source,
197 std::shared_ptr<ControlFunction> destination,
198 TransmitCompleteCallback sessionCompleteCallback,
199 void *parentPointer);
200
201 private:
206 bool abort_session(std::shared_ptr<ExtendedTransportProtocolSession> &session, ConnectionAbortReason reason);
207
214 bool send_abort(std::shared_ptr<InternalControlFunction> sender,
215 std::shared_ptr<ControlFunction> receiver,
216 std::uint32_t parameterGroupNumber,
217 ConnectionAbortReason reason) const;
218
222 void close_session(std::shared_ptr<ExtendedTransportProtocolSession> &session, bool successful);
223
227 bool send_request_to_send(std::shared_ptr<ExtendedTransportProtocolSession> &session) const;
228
232 bool send_clear_to_send(std::shared_ptr<ExtendedTransportProtocolSession> &session) const;
233
237 bool send_data_packet_offset(std::shared_ptr<ExtendedTransportProtocolSession> &session) const;
238
242 bool send_end_of_session_acknowledgement(std::shared_ptr<ExtendedTransportProtocolSession> &session) const;
243
246 void send_data_transfer_packets(std::shared_ptr<ExtendedTransportProtocolSession> &session) const;
247
253 void process_request_to_send(const std::shared_ptr<ControlFunction> source, const std::shared_ptr<ControlFunction> destination, std::uint32_t parameterGroupNumber, std::uint32_t totalMessageSize);
254
261 void process_clear_to_send(const std::shared_ptr<ControlFunction> source, const std::shared_ptr<ControlFunction> destination, std::uint32_t parameterGroupNumber, std::uint8_t packetsToBeSent, std::uint32_t nextPacketNumber);
262
269 void process_data_packet_offset(const std::shared_ptr<ControlFunction> source, const std::shared_ptr<ControlFunction> destination, std::uint32_t parameterGroupNumber, std::uint8_t numberOfPackets, std::uint32_t packetOffset);
270
276 void process_end_of_session_acknowledgement(const std::shared_ptr<ControlFunction> source, const std::shared_ptr<ControlFunction> destination, std::uint32_t parameterGroupNumber, std::uint32_t numberOfBytesTransferred);
277
283 void process_abort(const std::shared_ptr<ControlFunction> source, const std::shared_ptr<ControlFunction> destination, std::uint32_t parameterGroupNumber, ExtendedTransportProtocolManager::ConnectionAbortReason reason);
284
288
291 void process_data_transfer_message(const CANMessage &message);
292
297 std::shared_ptr<ExtendedTransportProtocolSession> get_session(std::shared_ptr<ControlFunction> source, std::shared_ptr<ControlFunction> destination);
298
301 void update_state_machine(std::shared_ptr<ExtendedTransportProtocolSession> &session);
302
303 std::vector<std::shared_ptr<ExtendedTransportProtocolSession>> activeSessions;
307 };
308
309} // namespace isobus
310
311#endif // CAN_EXTENDED_TRANSPORT_PROTOCOL_HPP
A classical CAN frame, with 8 data bytes.
This is a class for changing stack settings.
Abstract base class for CAN transport protocols.
A class that represents a generic CAN message of arbitrary length.
A class that defines stack-wide configuration data. You can set the values in there to suit your spec...
A storage object to keep track of session information internally.
std::uint32_t get_last_acknowledged_packet_number() const
Get the last acknowledged packet number by the receiver.
std::uint32_t get_last_packet_number() const
Get the last packet number that was processed.
std::uint32_t sequenceNumberOffset
The offset of the sequence number relative to the packet number.
std::uint8_t get_dpo_number_of_packets_remaining() const
Get the number of packets to be sent with the current DPO.
std::uint8_t get_last_sequence_number() const
Get the last sequence number that was processed.
std::uint32_t lastAcknowledgedPacketNumber
The last acknowledged packet number by the receiver.
void set_last_sequency_number(std::uint8_t value)
Set the last sequence number that has be processed.
std::uint8_t get_cts_number_of_packet_limit() const
Get the maximum number of packets that can be sent per DPO as indicated by the CTS message.
std::uint8_t lastSequenceNumber
The last processed sequence number for this set of packets.
void set_cts_number_of_packet_limit(std::uint8_t value)
Set the maximum number of packets that can be sent per DPO as indicated by the CTS message.
void set_dpo_number_of_packets(std::uint8_t value)
Set the number of packets to be sent with the current DPO.
void set_sequence_number_offset(std::uint32_t value)
Set the last packet number that was processed.
std::uint32_t get_total_number_of_packets() const
Get the total number of packets that will be sent or received in this session.
void set_acknowledged_packet_number(std::uint32_t value)
Set the last acknowledged packet number by the receiver.
std::uint32_t get_number_of_remaining_packets() const
Get the number of packets that remain to be sent or received in this session.
std::uint32_t get_total_bytes_transferred() const override
Get the number of bytes that have been sent or received in this session.
std::uint8_t dataPacketOffsetPacketCount
The number of packets that will be sent with the current DPO.
std::uint8_t get_dpo_number_of_packets() const
Get the number of packets that will be sent with the current DPO.
std::uint8_t clearToSendPacketCountLimit
The max packets that can be sent per DPO as indicated by the CTS message.
A class that handles the ISO11783 extended transport protocol.
static constexpr std::uint16_t T1_TIMEOUT_MS
The t1 timeout as defined by the standard.
void process_message(const CANMessage &message)
A generic way for a protocol to process a received message.
bool send_request_to_send(std::shared_ptr< ExtendedTransportProtocolSession > &session) const
Sends the "request to send" message as part of initiating a transmit.
bool abort_session(std::shared_ptr< ExtendedTransportProtocolSession > &session, ConnectionAbortReason reason)
Aborts the session with the specified abort reason. Sends a CAN message.
static constexpr std::uint8_t PROTOCOL_BYTES_PER_FRAME
The number of payload bytes per frame minus overhead of sequence number.
void process_abort(const std::shared_ptr< ControlFunction > source, const std::shared_ptr< ControlFunction > destination, std::uint32_t parameterGroupNumber, ExtendedTransportProtocolManager::ConnectionAbortReason reason)
Processes an abort message in the CAN transport protocol.
static constexpr std::uint8_t TR_TIMEOUT_MS
The Tr Timeout as defined by the standard.
void process_clear_to_send(const std::shared_ptr< ControlFunction > source, const std::shared_ptr< ControlFunction > destination, std::uint32_t parameterGroupNumber, std::uint8_t packetsToBeSent, std::uint32_t nextPacketNumber)
Processes the Clear To Send (CTS) message.
void send_data_transfer_packets(std::shared_ptr< ExtendedTransportProtocolSession > &session) const
Sends data transfer packets for the specified ExtendedTransportProtocolSession.
const CANMessageFrameCallback sendCANFrameCallback
A callback for sending a CAN frame.
void process_data_packet_offset(const std::shared_ptr< ControlFunction > source, const std::shared_ptr< ControlFunction > destination, std::uint32_t parameterGroupNumber, std::uint8_t numberOfPackets, std::uint32_t packetOffset)
Processes the Data Packet Offset (DPO) message.
bool send_end_of_session_acknowledgement(std::shared_ptr< ExtendedTransportProtocolSession > &session) const
Sends the "end of message acknowledgement" message for the provided session.
std::shared_ptr< ExtendedTransportProtocolSession > get_session(std::shared_ptr< ControlFunction > source, std::shared_ptr< ControlFunction > destination)
Gets a ETP session from the passed in source and destination and PGN combination.
const std::vector< std::shared_ptr< ExtendedTransportProtocolSession > > & get_sessions() const
Gets all the active transport protocol sessions that are currently active.
ExtendedTransportProtocolManager(const CANMessageFrameCallback &sendCANFrameCallback, const CANMessageCallback &canMessageReceivedCallback, const CANNetworkConfiguration *configuration)
The constructor for the ExtendedTransportProtocolManager, for advanced use only. In most cases,...
static constexpr std::uint32_t CLEAR_TO_SEND_MULTIPLEXOR
(21) ETP.CM_CTS Multiplexor
std::vector< std::shared_ptr< ExtendedTransportProtocolSession > > activeSessions
A list of all active ETP sessions.
void update()
Updates all sessions managed by this protocol manager instance.
void process_request_to_send(const std::shared_ptr< ControlFunction > source, const std::shared_ptr< ControlFunction > destination, std::uint32_t parameterGroupNumber, std::uint32_t totalMessageSize)
Processes a request to send a message over the CAN transport protocol.
const CANNetworkConfiguration * configuration
The configuration to use for this protocol.
static constexpr std::uint32_t END_OF_MESSAGE_ACKNOWLEDGE_MULTIPLEXOR
(23) TP.CM_EOMA Multiplexor
bool send_abort(std::shared_ptr< InternalControlFunction > sender, std::shared_ptr< ControlFunction > receiver, std::uint32_t parameterGroupNumber, ConnectionAbortReason reason) const
Send an abort with no corresponding session with the specified abort reason. Sends a CAN message.
void update_state_machine(std::shared_ptr< ExtendedTransportProtocolSession > &session)
Update the state machine for the passed in session.
static constexpr std::uint8_t SEQUENCE_NUMBER_DATA_INDEX
The index of the sequence number in a frame.
const CANMessageCallback canMessageReceivedCallback
A callback for when a complete CAN message is received using the ETP protocol.
bool protocol_transmit_message(std::uint32_t parameterGroupNumber, std::unique_ptr< CANMessageData > &data, std::shared_ptr< ControlFunction > source, std::shared_ptr< ControlFunction > destination, TransmitCompleteCallback sessionCompleteCallback, void *parentPointer)
The network manager calls this to see if the protocol can accept a long CAN message for processing.
static constexpr std::uint32_t CONNECTION_ABORT_MULTIPLEXOR
(255) Abort multiplexor
void close_session(std::shared_ptr< ExtendedTransportProtocolSession > &session, bool successful)
Gracefully closes a session to prepare for a new session.
void process_end_of_session_acknowledgement(const std::shared_ptr< ControlFunction > source, const std::shared_ptr< ControlFunction > destination, std::uint32_t parameterGroupNumber, std::uint32_t numberOfBytesTransferred)
Processes the end of session acknowledgement.
static constexpr std::uint32_t MAX_PROTOCOL_DATA_LENGTH
The max number of bytes that this protocol can transfer.
static constexpr std::uint16_t T2_T3_TIMEOUT_MS
The t2/t3 timeouts as defined by the standard.
void process_data_transfer_message(const CANMessage &message)
Processes a data transfer message.
bool send_data_packet_offset(std::shared_ptr< ExtendedTransportProtocolSession > &session) const
Sends the "data packet offset" message for the provided session.
bool send_clear_to_send(std::shared_ptr< ExtendedTransportProtocolSession > &session) const
Sends the "clear to send" message.
static constexpr std::uint32_t DATA_PACKET_OFFSET_MULTIPLXOR
(22) ETP.CM_DPO Multiplexor
bool has_session(std::shared_ptr< ControlFunction > source, std::shared_ptr< ControlFunction > destination)
Checks if the source and destination control function have an active session/connection.
void process_connection_management_message(const CANMessage &message)
Processes a connection management message.
StateMachineState
The states that a ETP session could be in. Used for the internal state machine.
@ WaitForEndOfMessageAcknowledge
We are waiting for an end of message acknowledgement.
@ SendClearToSend
We are sending clear to send message.
@ SendRequestToSend
We are sending the request to send message.
@ WaitForDataTransferPacket
We are waiting for data transfer packets.
@ WaitForClearToSend
We are waiting for a clear to send message.
@ SendDataPacketOffset
We are sending a data packet offset message.
@ WaitForDataPacketOffset
We are waiting for a data packet offset message.
@ SendDataTransferPackets
A Tx data session is in progress.
static constexpr std::uint16_t T4_TIMEOUT_MS
The t4 timeout as defined by the standard.
static constexpr std::uint32_t REQUEST_TO_SEND_MULTIPLEXOR
(20) ETP.CM_RTS Multiplexor
ConnectionAbortReason
A list of all defined abort reasons in ISO11783.
@ DataPacketOffsetExceedsClearToSend
Received a number of packets in EDPO greater than CTS.
@ DuplicateSequenceNumber
Re-received a sequence number we've already processed.
@ BadSequenceNumber
Incorrect sequence number was received and cannot be recovered.
@ SystemResourcesNeeded
Session must be aborted because the system needs resources.
@ AlreadyInCMSession
We are already in a connection mode session and can't support another.
@ UnexpectedClearToSendPGN
Received a CTS with an unexpected PGN.
@ ClearToSendReceivedWhileTransferInProgress
A CTS was received while already processing the last CTS.
@ AnyOtherError
Any reason not defined in the standard.
@ Reserved
Reserved, not to be used, but should be tolerated.
@ MaximumRetransmitRequestLimitReached
Maximum retries for the data has been reached.
@ NumberOfClearToSendPacketsExceedsMessage
Received a CTS with a number of packets greater than the message.
@ UnexpectedDataPacketOffsetPGN
Received a data packet offset with an unexpected PGN.
@ UnexpectedDataTransferPacketReceived
A data packet was received outside the proper state.
@ BadDataPacketOffset
Received a data packet offset that is incorrect.
An object to keep track of session information internally.
TransportProtocolSessionBase(TransportProtocolSessionBase::Direction direction, std::unique_ptr< CANMessageData > data, std::uint32_t parameterGroupNumber, std::uint32_t totalMessageSize, std::shared_ptr< ControlFunction > source, std::shared_ptr< ControlFunction > destination, TransmitCompleteCallback sessionCompleteCallback, void *parentPointer)
The constructor for a session.
This namespace encompasses all of the ISO11783 stack's functionality to reduce global namespace pollu...
std::function< bool(std::uint32_t parameterGroupNumber, CANDataSpan data, std::shared_ptr< InternalControlFunction > sourceControlFunction, std::shared_ptr< ControlFunction > destinationControlFunction, CANIdentifier::CANPriority priority)> CANMessageFrameCallback
A callback for communicating CAN message frames.
void(*)(std::uint32_t parameterGroupNumber, std::uint32_t dataLength, std::shared_ptr< InternalControlFunction > sourceControlFunction, std::shared_ptr< ControlFunction > destinationControlFunction, bool successful, void *parentPointer) TransmitCompleteCallback
A callback for when a transmit is completed by the stack.
std::function< void(const CANMessage &message)> CANMessageCallback
A callback for communicating CAN messages.