AgIsoStack++
A control-function-focused implementation of the major ISOBUS and J1939 protocols
Loading...
Searching...
No Matches
can_transport_protocol.hpp
Go to the documentation of this file.
1//================================================================================================
10//================================================================================================
11
12#ifndef CAN_TRANSPORT_PROTOCOL_HPP
13#define CAN_TRANSPORT_PROTOCOL_HPP
14
18
19namespace isobus
20{
21
32 {
33 public:
46
62
65 {
66 public:
79 std::unique_ptr<CANMessageData> data,
80 std::uint32_t parameterGroupNumber,
81 std::uint16_t totalMessageSize,
82 std::uint8_t clearToSendPacketMax,
83 std::shared_ptr<ControlFunction> source,
84 std::shared_ptr<ControlFunction> destination,
86 void *parentPointer);
87
91
94 std::uint16_t get_message_length() const;
95
98 bool is_broadcast() const;
99
102 std::uint32_t get_total_bytes_transferred() const override;
103
104 protected:
106
109 void set_state(StateMachineState value);
110
113 std::uint8_t get_cts_number_of_packets_remaining() const;
114
117 void set_cts_number_of_packets(std::uint8_t value);
118
121 std::uint8_t get_cts_number_of_packets() const;
122
125 std::uint8_t get_rts_number_of_packet_limit() const;
126
129 std::uint8_t get_last_sequence_number() const;
130
133 std::uint8_t get_last_packet_number() const;
134
137 void set_last_sequency_number(std::uint8_t value);
138
141 void set_acknowledged_packet_number(std::uint8_t value);
142
145 std::uint8_t get_number_of_remaining_packets() const;
146
149 std::uint8_t get_total_number_of_packets() const;
150
151 private:
153
154 std::uint8_t lastSequenceNumber = 0;
156 std::uint8_t clearToSendPacketCount = 0;
157 std::uint8_t clearToSendPacketCountMax = 0xFF;
158 };
159
160 static constexpr std::uint32_t REQUEST_TO_SEND_MULTIPLEXOR = 0x10;
161 static constexpr std::uint32_t CLEAR_TO_SEND_MULTIPLEXOR = 0x11;
162 static constexpr std::uint32_t END_OF_MESSAGE_ACKNOWLEDGE_MULTIPLEXOR = 0x13;
163 static constexpr std::uint32_t BROADCAST_ANNOUNCE_MESSAGE_MULTIPLEXOR = 0x20;
164 static constexpr std::uint32_t CONNECTION_ABORT_MULTIPLEXOR = 0xFF;
165 static constexpr std::uint32_t MAX_PROTOCOL_DATA_LENGTH = 1785;
166 static constexpr std::uint16_t T1_TIMEOUT_MS = 750;
167 static constexpr std::uint16_t T2_T3_TIMEOUT_MS = 1250;
168 static constexpr std::uint16_t T4_TIMEOUT_MS = 1050;
169 static constexpr std::uint8_t R_TIMEOUT_MS = 200;
170 static constexpr std::uint8_t SEQUENCE_NUMBER_DATA_INDEX = 0;
171 static constexpr std::uint8_t PROTOCOL_BYTES_PER_FRAME = 7;
172
181
183 void update();
184
189 bool has_session(std::shared_ptr<ControlFunction> source, std::shared_ptr<ControlFunction> destination);
190
194 const std::vector<std::shared_ptr<TransportProtocolSession>> &get_sessions() const;
195
198 void process_message(const CANMessage &message);
199
208 bool protocol_transmit_message(std::uint32_t parameterGroupNumber,
209 std::unique_ptr<CANMessageData> &data,
210 std::shared_ptr<ControlFunction> source,
211 std::shared_ptr<ControlFunction> destination,
212 TransmitCompleteCallback sessionCompleteCallback,
213 void *parentPointer);
214
215 private:
220 bool abort_session(std::shared_ptr<TransportProtocolSession> &session, ConnectionAbortReason reason);
221
228 bool send_abort(std::shared_ptr<InternalControlFunction> sender,
229 std::shared_ptr<ControlFunction> receiver,
230 std::uint32_t parameterGroupNumber,
231 ConnectionAbortReason reason) const;
232
236 void close_session(std::shared_ptr<TransportProtocolSession> &session, bool successful);
237
241 bool send_broadcast_announce_message(std::shared_ptr<TransportProtocolSession> &session) const;
242
246 bool send_request_to_send(std::shared_ptr<TransportProtocolSession> &session) const;
247
251 bool send_clear_to_send(std::shared_ptr<TransportProtocolSession> &session) const;
252
256 bool send_end_of_session_acknowledgement(std::shared_ptr<TransportProtocolSession> &session) const;
257
260 void send_data_transfer_packets(std::shared_ptr<TransportProtocolSession> &session);
261
267 void process_broadcast_announce_message(const std::shared_ptr<ControlFunction> source, std::uint32_t parameterGroupNumber, std::uint16_t totalMessageSize, std::uint8_t totalNumberOfPackets);
268
276 void process_request_to_send(const std::shared_ptr<ControlFunction> source, const std::shared_ptr<ControlFunction> destination, std::uint32_t parameterGroupNumber, std::uint16_t totalMessageSize, std::uint8_t totalNumberOfPackets, std::uint8_t clearToSendPacketMax);
277
284 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::uint8_t nextPacketNumber);
285
290 void process_end_of_session_acknowledgement(const std::shared_ptr<ControlFunction> source, const std::shared_ptr<ControlFunction> destination, std::uint32_t parameterGroupNumber);
291
297 void process_abort(const std::shared_ptr<ControlFunction> source, const std::shared_ptr<ControlFunction> destination, std::uint32_t parameterGroupNumber, TransportProtocolManager::ConnectionAbortReason reason);
298
302
305 void process_data_transfer_message(const CANMessage &message);
306
311 std::shared_ptr<TransportProtocolSession> get_session(std::shared_ptr<ControlFunction> source, std::shared_ptr<ControlFunction> destination);
312
315 void update_state_machine(std::shared_ptr<TransportProtocolSession> &session);
316
317 std::vector<std::shared_ptr<TransportProtocolSession>> activeSessions;
321 };
322
323} // namespace isobus
324
325#endif // CAN_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::uint8_t clearToSendPacketCountMax
The max packets that can be sent per CTS as indicated by the RTS message.
void set_acknowledged_packet_number(std::uint8_t value)
Set the last acknowledged packet number by the receiver.
TransportProtocolSession(TransportProtocolSessionBase::Direction direction, std::unique_ptr< CANMessageData > data, std::uint32_t parameterGroupNumber, std::uint16_t totalMessageSize, std::uint8_t clearToSendPacketMax, std::shared_ptr< ControlFunction > source, std::shared_ptr< ControlFunction > destination, TransmitCompleteCallback sessionCompleteCallback, void *parentPointer)
The constructor for a session, for advanced use only. In most cases, you should use the CANNetworkMan...
std::uint8_t get_cts_number_of_packets_remaining() const
Get the number of packets to be sent in response to the current CTS.
void set_state(StateMachineState value)
Set the state of the session.
std::uint8_t lastAcknowledgedPacketNumber
The last acknowledged packet number by the receiver.
StateMachineState get_state() const
Get the state of the session.
StateMachineState state
The state machine state for 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 lastSequenceNumber
The last processed sequence number for this set of packets.
std::uint8_t get_cts_number_of_packets() const
Get the number of packets that can be sent in response to the current CTS.
std::uint8_t get_rts_number_of_packet_limit() const
Get the maximum number of packets that can be sent per CTS as indicated by the RTS message.
std::uint8_t clearToSendPacketCount
The number of packets to be sent in response to one CTS.
std::uint16_t get_message_length() const
Get the total number of bytes that will be sent or received in this session.
void set_cts_number_of_packets(std::uint8_t value)
Set the number of packets to be sent in response to the curent CTS.
std::uint8_t get_last_sequence_number() const
Get the last sequence number that was processed.
bool is_broadcast() const
Get whether or not this session is a broadcast session (BAM)
std::uint8_t get_number_of_remaining_packets() const
Get the number of packets that remain to be sent or received in this session.
std::uint8_t get_total_number_of_packets() const
Get the total number of packets that will be sent or received in this session.
std::uint8_t get_last_packet_number() const
Get the last packet number that was processed.
void set_last_sequency_number(std::uint8_t value)
Set the last sequence number that will be processed.
A class that handles the ISO11783/J1939 transport protocol.
void process_connection_management_message(const CANMessage &message)
Processes a connection management message.
static constexpr std::uint32_t REQUEST_TO_SEND_MULTIPLEXOR
(16) TP.CM_RTS Multiplexor
static constexpr std::uint32_t END_OF_MESSAGE_ACKNOWLEDGE_MULTIPLEXOR
(19) TP.CM_EOM_ACK Multiplexor
bool send_broadcast_announce_message(std::shared_ptr< TransportProtocolSession > &session) const
Sends the "broadcast announce" message.
bool send_end_of_session_acknowledgement(std::shared_ptr< TransportProtocolSession > &session) const
Sends the "end of message acknowledgement" message for the provided session.
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 MAX_PROTOCOL_DATA_LENGTH
The max number of bytes that this protocol can transfer.
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::uint8_t nextPacketNumber)
Processes the Clear To Send (CTS) message.
void process_abort(const std::shared_ptr< ControlFunction > source, const std::shared_ptr< ControlFunction > destination, std::uint32_t parameterGroupNumber, TransportProtocolManager::ConnectionAbortReason reason)
Processes an abort message in the CAN transport protocol.
const CANMessageFrameCallback sendCANFrameCallback
A callback for sending a CAN frame.
static constexpr std::uint32_t BROADCAST_ANNOUNCE_MESSAGE_MULTIPLEXOR
(32) TP.BAM Multiplexor
TransportProtocolManager(const CANMessageFrameCallback &sendCANFrameCallback, const CANMessageCallback &canMessageReceivedCallback, const CANNetworkConfiguration *configuration)
The constructor for the TransportProtocolManager, for advanced use only. In most cases,...
ConnectionAbortReason
A list of all defined abort reasons in ISO11783.
@ DuplicateSequenceNumber
Re-received a sequence number we've already processed.
@ TotalMessageSizeTooBig
TP Can't support a message this large (>1785 bytes)
@ 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.
@ 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.
@ UnexpectedDataTransferPacketReceived
A data packet was received outside the proper state.
bool send_request_to_send(std::shared_ptr< TransportProtocolSession > &session) const
Sends the "request to send" message as part of initiating a transmit.
static constexpr std::uint16_t T1_TIMEOUT_MS
The t1 timeout as defined by the standard.
std::shared_ptr< TransportProtocolSession > get_session(std::shared_ptr< ControlFunction > source, std::shared_ptr< ControlFunction > destination)
Gets a TP session from the passed in source and destination combination.
const std::vector< std::shared_ptr< TransportProtocolSession > > & get_sessions() const
Gets all the active transport protocol sessions that are currently active.
static constexpr std::uint32_t CLEAR_TO_SEND_MULTIPLEXOR
(17) TP.CM_CTS Multiplexor
const CANMessageCallback canMessageReceivedCallback
A callback for when a complete CAN message is received using the TP protocol.
bool abort_session(std::shared_ptr< TransportProtocolSession > &session, ConnectionAbortReason reason)
Aborts the session with the specified abort reason. Sends a CAN message.
std::vector< std::shared_ptr< TransportProtocolSession > > activeSessions
A list of all active TP sessions.
void process_request_to_send(const std::shared_ptr< ControlFunction > source, const std::shared_ptr< ControlFunction > destination, std::uint32_t parameterGroupNumber, std::uint16_t totalMessageSize, std::uint8_t totalNumberOfPackets, std::uint8_t clearToSendPacketMax)
Processes a request to send a message over the CAN transport protocol.
void process_end_of_session_acknowledgement(const std::shared_ptr< ControlFunction > source, const std::shared_ptr< ControlFunction > destination, std::uint32_t parameterGroupNumber)
Processes the end of session acknowledgement.
void close_session(std::shared_ptr< TransportProtocolSession > &session, bool successful)
Gracefully closes a session to prepare for a new session.
void send_data_transfer_packets(std::shared_ptr< TransportProtocolSession > &session)
Sends data transfer packets for the specified TransportProtocolSession.
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.
static constexpr std::uint8_t SEQUENCE_NUMBER_DATA_INDEX
The index of the sequence number in a frame.
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.
static constexpr std::uint8_t PROTOCOL_BYTES_PER_FRAME
The number of payload bytes per frame minus overhead of sequence number.
void update_state_machine(std::shared_ptr< TransportProtocolSession > &session)
Update the state machine for the passed in session.
static constexpr std::uint16_t T2_T3_TIMEOUT_MS
The t2/t3 timeouts as defined by the standard.
void process_message(const CANMessage &message)
A generic way for a protocol to process a received message.
bool send_clear_to_send(std::shared_ptr< TransportProtocolSession > &session) const
Sends the "clear to send" message.
static constexpr std::uint8_t R_TIMEOUT_MS
The Tr Timeout as defined by the standard.
StateMachineState
The states that a TP session could be in. Used for the internal state machine.
@ SendBroadcastAnnounce
We are sending the broadcast announce message (BAM)
@ 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.
@ None
Protocol session is not in progress.
@ SendDataTransferPackets
A Tx data session is in progress.
void update()
Updates all sessions managed by this protocol manager instance.
void process_data_transfer_message(const CANMessage &message)
Processes a data transfer message.
const CANNetworkConfiguration * configuration
The configuration to use for this protocol.
static constexpr std::uint32_t CONNECTION_ABORT_MULTIPLEXOR
(255) Abort multiplexor
static constexpr std::uint16_t T4_TIMEOUT_MS
The t4 timeout as defined by the standard.
void process_broadcast_announce_message(const std::shared_ptr< ControlFunction > source, std::uint32_t parameterGroupNumber, std::uint16_t totalMessageSize, std::uint8_t totalNumberOfPackets)
Processes a broadcast announce message.
An object to keep track of session information internally.
std::shared_ptr< ControlFunction > destination
The destination control function.
Direction direction
The direction of the session.
Direction
Enumerates the possible session directions.
std::shared_ptr< ControlFunction > source
The source control function.
TransmitCompleteCallback sessionCompleteCallback
A callback that is to be called when the session is completed.
std::uint32_t parameterGroupNumber
The PGN of the message.
std::uint32_t totalMessageSize
The total size of the message in bytes (the maximum size of a message is from ETP and can fit in an u...
std::unique_ptr< CANMessageData > data
The data buffer for the message.
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.