AgIsoStack++
A control-function-focused implementation of the major ISOBUS and J1939 protocols
Loading...
Searching...
No Matches
nmea2000_fast_packet_protocol.hpp
Go to the documentation of this file.
1//================================================================================================
25//================================================================================================
26
27#ifndef NMEA2000_FAST_PACKET_PROTOCOL_HPP
28#define NMEA2000_FAST_PACKET_PROTOCOL_HPP
29
31#include "isobus/utility/event_dispatcher.hpp"
32#include "isobus/utility/thread_synchronization.hpp"
33
34namespace isobus
35{
38 {
39 public:
42 {
43 public:
57 std::unique_ptr<CANMessageData> data,
58 std::uint32_t parameterGroupNumber,
59 std::uint16_t totalMessageSize,
60 std::uint8_t sequenceNumber,
62 std::shared_ptr<ControlFunction> source,
63 std::shared_ptr<ControlFunction> destination,
65 void *parentPointer);
66
70 std::uint8_t get_message_length() const;
71
74 std::uint32_t get_total_bytes_transferred() const override;
75
78 bool is_broadcast() const;
79
80 protected:
81 friend class FastPacketProtocol;
82
85 std::uint8_t get_last_packet_number() const;
86
89 std::uint8_t get_number_of_remaining_packets() const;
90
93 std::uint8_t get_total_number_of_packets() const;
94
97 void add_number_of_bytes_transferred(std::uint8_t bytes);
98
99 private:
100 std::uint8_t numberOfBytesTransferred = 0;
101 std::uint8_t sequenceNumber;
103 };
104
107 {
109 std::uint32_t parameterGroupNumber;
110 std::uint8_t sequenceNumber;
111 };
112
117
127 void register_multipacket_message_callback(std::uint32_t parameterGroupNumber, CANLibCallback callback, void *parent, std::shared_ptr<InternalControlFunction> internalControlFunction = nullptr);
128
129 // @brief Removes a callback previously added with register_multipacket_message_callback
134 void remove_multipacket_message_callback(std::uint32_t parameterGroupNumber, CANLibCallback callback, void *parent, std::shared_ptr<InternalControlFunction> internalControlFunction = nullptr);
135
150 bool send_multipacket_message(std::uint32_t parameterGroupNumber,
151 const std::uint8_t *data,
152 std::uint8_t messageLength,
153 std::shared_ptr<InternalControlFunction> source,
154 std::shared_ptr<ControlFunction> destination,
156 TransmitCompleteCallback txCompleteCallback = nullptr,
157 void *parentPointer = nullptr,
158 DataChunkCallback frameChunkCallback = nullptr);
159
162 void allow_any_control_function(bool allow);
163
165 void update();
166
169 void process_message(const CANMessage &message);
170
174 static std::uint8_t calculate_number_of_frames(std::uint8_t messageLength);
175
176 private:
179 void add_session_history(const std::shared_ptr<FastPacketProtocolSession> &session);
180
184 void close_session(std::shared_ptr<FastPacketProtocolSession> session, bool successful);
185
190 std::uint8_t get_new_sequence_number(NAME name, std::uint32_t parameterGroupNumber) const;
191
197 std::shared_ptr<FastPacketProtocolSession> get_session(std::uint32_t parameterGroupNumber, std::shared_ptr<ControlFunction> source, std::shared_ptr<ControlFunction> destination);
198
204 bool has_session(std::uint32_t parameterGroupNumber, std::shared_ptr<ControlFunction> source, std::shared_ptr<ControlFunction> destination);
205
208 void update_session(const std::shared_ptr<FastPacketProtocolSession> &session);
209
210 static constexpr std::uint32_t FP_MIN_PARAMETER_GROUP_NUMBER = 0x1F000;
211 static constexpr std::uint32_t FP_MAX_PARAMETER_GROUP_NUMBER = 0x1FFFF;
212 static constexpr std::uint32_t FP_TIMEOUT_MS = 750;
213 static constexpr std::uint8_t MAX_PROTOCOL_MESSAGE_LENGTH = 223;
214 static constexpr std::uint8_t FRAME_COUNTER_BIT_MASK = 0x1F;
215 static constexpr std::uint8_t SEQUENCE_NUMBER_BIT_MASK = 0x07;
216 static constexpr std::uint8_t SEQUENCE_NUMBER_BIT_OFFSET = 5;
217 static constexpr std::uint8_t PROTOCOL_BYTES_PER_FRAME = 7;
218
219 std::vector<std::shared_ptr<FastPacketProtocolSession>> activeSessions;
221 std::vector<FastPacketHistory> sessionHistory;
222 std::vector<ParameterGroupNumberCallbackData> parameterGroupNumberCallbacks;
225 };
226
227} // namespace isobus
228
229#endif // NMEA2000_FAST_PACKET_PROTOCOL_HPP
Abstract base class for CAN transport protocols.
CANPriority
Defines all the CAN frame priorities that can be encoded in a frame ID.
@ PriorityDefault6
The default priority.
A class that represents a generic CAN message of arbitrary length.
An object for tracking fast packet session state.
std::uint8_t get_message_length() const
Get the total number of bytes that will 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.
bool is_broadcast() const
Get whether or not this session is a broadcast session (BAM)
void add_number_of_bytes_transferred(std::uint8_t bytes)
Add number of bytes to the total number of bytes that have been sent or received in this session.
FastPacketProtocolSession(TransportProtocolSessionBase::Direction direction, std::unique_ptr< CANMessageData > data, std::uint32_t parameterGroupNumber, std::uint16_t totalMessageSize, std::uint8_t sequenceNumber, CANIdentifier::CANPriority priority, 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_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.
CANIdentifier::CANPriority priority
The priority to encode in the IDs of the component CAN messages.
std::uint8_t sequenceNumber
The sequence number for this PGN.
std::uint8_t get_last_packet_number() const
Get the last packet number that was sent or received in this session.
std::uint8_t numberOfBytesTransferred
The total number of bytes that have been processed in this session.
A protocol that handles the NMEA 2000 fast packet protocol.
static constexpr std::uint32_t FP_MIN_PARAMETER_GROUP_NUMBER
Start of PGNs that can be received via Fast Packet.
void add_session_history(const std::shared_ptr< FastPacketProtocolSession > &session)
Adds a session's info to the history so that we can continue the sequence number later.
bool allowAnyControlFunction
Denotes if messages for non-internal control functions should be parsed by this protocol.
static constexpr std::uint8_t FRAME_COUNTER_BIT_MASK
Bit mask for masking out the frame counter.
std::vector< std::shared_ptr< FastPacketProtocolSession > > activeSessions
A list of all active TP sessions.
static constexpr std::uint8_t SEQUENCE_NUMBER_BIT_MASK
Bit mask for masking out the sequence number bits.
static std::uint8_t calculate_number_of_frames(std::uint8_t messageLength)
Calculates the number of frames needed for a message.
void close_session(std::shared_ptr< FastPacketProtocolSession > session, bool successful)
Gracefully closes a session to prepare for a new session.
std::uint8_t get_new_sequence_number(NAME name, std::uint32_t parameterGroupNumber) const
Get the sequence number to use for a new session based on the history of past sessions.
void remove_multipacket_message_callback(std::uint32_t parameterGroupNumber, CANLibCallback callback, void *parent, std::shared_ptr< InternalControlFunction > internalControlFunction=nullptr)
const CANMessageFrameCallback sendCANFrameCallback
A callback for sending a CAN frame.
Mutex sessionMutex
A mutex to lock the sessions list in case someone starts a Tx while the stack is processing sessions.
static constexpr std::uint32_t FP_TIMEOUT_MS
Protocol timeout in milliseconds.
static constexpr std::uint8_t SEQUENCE_NUMBER_BIT_OFFSET
The bit offset into the first byte of data to get the seq number.
std::vector< ParameterGroupNumberCallbackData > parameterGroupNumberCallbacks
A list of all parameter group number callbacks that will be parsed as fast packet messages.
FastPacketProtocol(const CANMessageFrameCallback &sendCANFrameCallback)
The constructor for the FastPacketProtocol, for advanced use only. In most cases, you should use the ...
void allow_any_control_function(bool allow)
Set whether or not to allow messages for non-internal control functions to be parsed by this protocol...
std::vector< FastPacketHistory > sessionHistory
Used to keep track of sequence numbers for future sessions.
void update()
Updates all sessions managed by this protocol manager instance.
std::shared_ptr< FastPacketProtocolSession > get_session(std::uint32_t parameterGroupNumber, std::shared_ptr< ControlFunction > source, std::shared_ptr< ControlFunction > destination)
Gets a FP session from the passed in source and destination and PGN combination.
static constexpr std::uint8_t PROTOCOL_BYTES_PER_FRAME
The number of payload bytes per frame for all but the first message, which has 6.
static constexpr std::uint8_t MAX_PROTOCOL_MESSAGE_LENGTH
Max message length based on there being 5 bits of sequence data.
void process_message(const CANMessage &message)
A generic way for a protocol to process a received message.
void register_multipacket_message_callback(std::uint32_t parameterGroupNumber, CANLibCallback callback, void *parent, std::shared_ptr< InternalControlFunction > internalControlFunction=nullptr)
Add a callback to be called when a message is received by the Fast Packet protocol.
void update_session(const std::shared_ptr< FastPacketProtocolSession > &session)
Update a single session.
static constexpr std::uint32_t FP_MAX_PARAMETER_GROUP_NUMBER
End of PGNs that can be received via Fast Packet.
bool has_session(std::uint32_t parameterGroupNumber, std::shared_ptr< ControlFunction > source, std::shared_ptr< ControlFunction > destination)
Checks if a session by the passed in source and destination and PGN combination exists.
bool send_multipacket_message(std::uint32_t parameterGroupNumber, const std::uint8_t *data, std::uint8_t messageLength, std::shared_ptr< InternalControlFunction > source, std::shared_ptr< ControlFunction > destination, CANIdentifier::CANPriority priority=CANIdentifier::CANPriority::PriorityDefault6, TransmitCompleteCallback txCompleteCallback=nullptr, void *parentPointer=nullptr, DataChunkCallback frameChunkCallback=nullptr)
Used to send CAN messages using fast packet.
A class that represents an ISO11783 control function NAME from an address claim.
Definition can_NAME.hpp:24
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.
void(*)(const CANMessage &message, void *parentPointer) CANLibCallback
A callback for control functions to get CAN messages.
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.
A structure for keeping track of past sessions so we can resume with the right session number.
std::uint8_t sequenceNumber
The sequence number to use in the next matching session.
NAME isoName
The ISO name of the internal control function used in a session.
std::uint32_t parameterGroupNumber
The PGN of the session being saved.