AgIsoStack++
A control-function-focused implementation of the major ISOBUS and J1939 protocols
Loading...
Searching...
No Matches
Classes | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
isobus::FastPacketProtocol Class Reference

A protocol that handles the NMEA 2000 fast packet protocol. More...

#include <nmea2000_fast_packet_protocol.hpp>

Classes

struct  FastPacketHistory
 A structure for keeping track of past sessions so we can resume with the right session number. More...
 
class  FastPacketProtocolSession
 An object for tracking fast packet session state. More...
 

Public Member Functions

 FastPacketProtocol (const CANMessageFrameCallback &sendCANFrameCallback)
 The constructor for the FastPacketProtocol, for advanced use only. In most cases, you should use the CANNetworkManager::get_fast_packet_protocol().send_message() function to transmit messages.
 
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 remove_multipacket_message_callback (std::uint32_t parameterGroupNumber, CANLibCallback callback, void *parent, std::shared_ptr< InternalControlFunction > internalControlFunction=nullptr)
 
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.
 
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.
 
void update ()
 Updates all sessions managed by this protocol manager instance.
 
void process_message (const CANMessage &message)
 A generic way for a protocol to process a received message.
 

Static Public Member Functions

static std::uint8_t calculate_number_of_frames (std::uint8_t messageLength)
 Calculates the number of frames needed for a message.
 

Private Member Functions

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.
 
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.
 
std::shared_ptr< FastPacketProtocolSessionget_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.
 
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.
 
void update_session (const std::shared_ptr< FastPacketProtocolSession > &session)
 Update a single session.
 

Private Attributes

std::vector< std::shared_ptr< FastPacketProtocolSession > > activeSessions
 A list of all active TP sessions.
 
Mutex sessionMutex
 A mutex to lock the sessions list in case someone starts a Tx while the stack is processing sessions.
 
std::vector< FastPacketHistorysessionHistory
 Used to keep track of sequence numbers for future sessions.
 
std::vector< ParameterGroupNumberCallbackDataparameterGroupNumberCallbacks
 A list of all parameter group number callbacks that will be parsed as fast packet messages.
 
bool allowAnyControlFunction = false
 Denotes if messages for non-internal control functions should be parsed by this protocol.
 
const CANMessageFrameCallback sendCANFrameCallback
 A callback for sending a CAN frame.
 

Static Private Attributes

static constexpr std::uint32_t FP_MIN_PARAMETER_GROUP_NUMBER = 0x1F000
 Start of PGNs that can be received via Fast Packet.
 
static constexpr std::uint32_t FP_MAX_PARAMETER_GROUP_NUMBER = 0x1FFFF
 End of PGNs that can be received via Fast Packet.
 
static constexpr std::uint32_t FP_TIMEOUT_MS = 750
 Protocol timeout in milliseconds.
 
static constexpr std::uint8_t MAX_PROTOCOL_MESSAGE_LENGTH = 223
 Max message length based on there being 5 bits of sequence data.
 
static constexpr std::uint8_t FRAME_COUNTER_BIT_MASK = 0x1F
 Bit mask for masking out the frame counter.
 
static constexpr std::uint8_t SEQUENCE_NUMBER_BIT_MASK = 0x07
 Bit mask for masking out the sequence number bits.
 
static constexpr std::uint8_t SEQUENCE_NUMBER_BIT_OFFSET = 5
 The bit offset into the first byte of data to get the seq number.
 
static constexpr std::uint8_t PROTOCOL_BYTES_PER_FRAME = 7
 The number of payload bytes per frame for all but the first message, which has 6.
 

Detailed Description

A protocol that handles the NMEA 2000 fast packet protocol.

Definition at line 37 of file nmea2000_fast_packet_protocol.hpp.

Constructor & Destructor Documentation

◆ FastPacketProtocol()

isobus::FastPacketProtocol::FastPacketProtocol ( const CANMessageFrameCallback & sendCANFrameCallback)
explicit

The constructor for the FastPacketProtocol, for advanced use only. In most cases, you should use the CANNetworkManager::get_fast_packet_protocol().send_message() function to transmit messages.

Parameters
[in]sendCANFrameCallbackA callback for sending a CAN frame to hardware

Definition at line 105 of file nmea2000_fast_packet_protocol.cpp.

Member Function Documentation

◆ add_session_history()

void isobus::FastPacketProtocol::add_session_history ( const std::shared_ptr< FastPacketProtocolSession > & session)
private

Adds a session's info to the history so that we can continue the sequence number later.

Parameters
[in]sessionThe session to add to the history

Definition at line 211 of file nmea2000_fast_packet_protocol.cpp.

Here is the caller graph for this function:

◆ allow_any_control_function()

void isobus::FastPacketProtocol::allow_any_control_function ( bool allow)

Set whether or not to allow messages for non-internal control functions to be parsed by this protocol.

Parameters
[in]allowDenotes if messages for non-internal control functions should be parsed by this protocol

Definition at line 125 of file nmea2000_fast_packet_protocol.cpp.

◆ calculate_number_of_frames()

std::uint8_t isobus::FastPacketProtocol::calculate_number_of_frames ( std::uint8_t messageLength)
static

Calculates the number of frames needed for a message.

Parameters
[in]messageLengthThe length of the message in bytes
Returns
The number of frames needed for the message

Definition at line 88 of file nmea2000_fast_packet_protocol.cpp.

Here is the caller graph for this function:

◆ close_session()

void isobus::FastPacketProtocol::close_session ( std::shared_ptr< FastPacketProtocolSession > session,
bool successful )
private

Gracefully closes a session to prepare for a new session.

Parameters
[in]sessionThe session to close
[in]successfulDenotes if the session was successful

Definition at line 240 of file nmea2000_fast_packet_protocol.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_new_sequence_number()

std::uint8_t isobus::FastPacketProtocol::get_new_sequence_number ( NAME name,
std::uint32_t parameterGroupNumber ) const
private

Get the sequence number to use for a new session based on the history of past sessions.

Parameters
[in]nameThe ISO name of the internal control function used in a session
[in]parameterGroupNumberThe PGN of the session being started
Returns
The sequence number to use for the new session

Definition at line 255 of file nmea2000_fast_packet_protocol.cpp.

Here is the caller graph for this function:

◆ get_session()

std::shared_ptr< FastPacketProtocol::FastPacketProtocolSession > isobus::FastPacketProtocol::get_session ( std::uint32_t parameterGroupNumber,
std::shared_ptr< ControlFunction > source,
std::shared_ptr< ControlFunction > destination )
private

Gets a FP session from the passed in source and destination and PGN combination.

Parameters
[in]parameterGroupNumberThe PGN of the session
[in]sourceThe source control function for the session
[in]destinationThe destination control function for the session
Returns
a matching session, or nullptr if no session matched the supplied parameters

Definition at line 502 of file nmea2000_fast_packet_protocol.cpp.

Here is the caller graph for this function:

◆ has_session()

bool isobus::FastPacketProtocol::has_session ( std::uint32_t parameterGroupNumber,
std::shared_ptr< ControlFunction > source,
std::shared_ptr< ControlFunction > destination )
private

Checks if a session by the passed in source and destination and PGN combination exists.

Parameters
[in]parameterGroupNumberThe PGN of the session
[in]sourceThe source control function for the session
[in]destinationThe destination control function for the session
Returns
true if a matching session exists, false if not

Definition at line 494 of file nmea2000_fast_packet_protocol.cpp.

Here is the caller graph for this function:

◆ process_message()

void isobus::FastPacketProtocol::process_message ( const CANMessage & message)

A generic way for a protocol to process a received message.

Parameters
[in]messageA received CAN message

Definition at line 269 of file nmea2000_fast_packet_protocol.cpp.

Here is the call graph for this function:

◆ register_multipacket_message_callback()

void isobus::FastPacketProtocol::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.

Parameters
[in]parameterGroupNumberThe PGN to parse as fast packet
[in]callbackThe callback that the stack will call when a matching message is received
[in]parentGeneric context variable for the callback
[in]internalControlFunctionAn internal control function to use as an additional filter for the callback. Only messages destined for the specified ICF will generate a callback. Use nullptr to receive messages for destined for any ICF and broadcast messages.
Note
You can also sniff all messages by allowing messages for messages destined to non-internal control functions to be parsed by this protocol, use the "allow_any_control_function()" function to enable this.

Definition at line 110 of file nmea2000_fast_packet_protocol.cpp.

◆ remove_multipacket_message_callback()

void isobus::FastPacketProtocol::remove_multipacket_message_callback ( std::uint32_t parameterGroupNumber,
CANLibCallback callback,
void * parent,
std::shared_ptr< InternalControlFunction > internalControlFunction = nullptr )
Parameters
[in]parameterGroupNumberThe PGN to parse as fast packet
[in]callbackThe callback that the stack will call when a matching message is received
[in]parentGeneric context variable
[in]internalControlFunctionAn internal control function used as an additional filter for the callback

Definition at line 115 of file nmea2000_fast_packet_protocol.cpp.

◆ send_multipacket_message()

bool isobus::FastPacketProtocol::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.

You have to use this function instead of the network manager because otherwise the CAN stack has no way of knowing to send your message with FP instead of TP.

Parameters
[in]parameterGroupNumberThe PGN of the message
[in]dataThe data to be sent
[in]messageLengthThe length of the data to be sent
[in]sourceThe source control function
[in]destinationThe destination control function
[in]priorityThe priority to encode in the IDs of the component CAN messages
[in]txCompleteCallbackA callback for when the protocol completes its work
[in]parentPointerA generic context object for the tx complete and chunk callbacks
[in]frameChunkCallbackA callback to get some data to send
Returns
true if the message was accepted by the protocol for processing

Definition at line 130 of file nmea2000_fast_packet_protocol.cpp.

Here is the call graph for this function:

◆ update()

void isobus::FastPacketProtocol::update ( )

Updates all sessions managed by this protocol manager instance.

Definition at line 190 of file nmea2000_fast_packet_protocol.cpp.

Here is the call graph for this function:

◆ update_session()

void isobus::FastPacketProtocol::update_session ( const std::shared_ptr< FastPacketProtocolSession > & session)
private

Update a single session.

Parameters
[in]sessionThe session to update

Definition at line 420 of file nmea2000_fast_packet_protocol.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ activeSessions

std::vector<std::shared_ptr<FastPacketProtocolSession> > isobus::FastPacketProtocol::activeSessions
private

A list of all active TP sessions.

Definition at line 219 of file nmea2000_fast_packet_protocol.hpp.

◆ allowAnyControlFunction

bool isobus::FastPacketProtocol::allowAnyControlFunction = false
private

Denotes if messages for non-internal control functions should be parsed by this protocol.

Definition at line 223 of file nmea2000_fast_packet_protocol.hpp.

◆ FP_MAX_PARAMETER_GROUP_NUMBER

constexpr std::uint32_t isobus::FastPacketProtocol::FP_MAX_PARAMETER_GROUP_NUMBER = 0x1FFFF
staticconstexprprivate

End of PGNs that can be received via Fast Packet.

Definition at line 211 of file nmea2000_fast_packet_protocol.hpp.

◆ FP_MIN_PARAMETER_GROUP_NUMBER

constexpr std::uint32_t isobus::FastPacketProtocol::FP_MIN_PARAMETER_GROUP_NUMBER = 0x1F000
staticconstexprprivate

Start of PGNs that can be received via Fast Packet.

Definition at line 210 of file nmea2000_fast_packet_protocol.hpp.

◆ FP_TIMEOUT_MS

constexpr std::uint32_t isobus::FastPacketProtocol::FP_TIMEOUT_MS = 750
staticconstexprprivate

Protocol timeout in milliseconds.

Definition at line 212 of file nmea2000_fast_packet_protocol.hpp.

◆ FRAME_COUNTER_BIT_MASK

constexpr std::uint8_t isobus::FastPacketProtocol::FRAME_COUNTER_BIT_MASK = 0x1F
staticconstexprprivate

Bit mask for masking out the frame counter.

Definition at line 214 of file nmea2000_fast_packet_protocol.hpp.

◆ MAX_PROTOCOL_MESSAGE_LENGTH

constexpr std::uint8_t isobus::FastPacketProtocol::MAX_PROTOCOL_MESSAGE_LENGTH = 223
staticconstexprprivate

Max message length based on there being 5 bits of sequence data.

Definition at line 213 of file nmea2000_fast_packet_protocol.hpp.

◆ parameterGroupNumberCallbacks

std::vector<ParameterGroupNumberCallbackData> isobus::FastPacketProtocol::parameterGroupNumberCallbacks
private

A list of all parameter group number callbacks that will be parsed as fast packet messages.

Definition at line 222 of file nmea2000_fast_packet_protocol.hpp.

◆ PROTOCOL_BYTES_PER_FRAME

constexpr std::uint8_t isobus::FastPacketProtocol::PROTOCOL_BYTES_PER_FRAME = 7
staticconstexprprivate

The number of payload bytes per frame for all but the first message, which has 6.

Definition at line 217 of file nmea2000_fast_packet_protocol.hpp.

◆ sendCANFrameCallback

const CANMessageFrameCallback isobus::FastPacketProtocol::sendCANFrameCallback
private

A callback for sending a CAN frame.

Definition at line 224 of file nmea2000_fast_packet_protocol.hpp.

◆ SEQUENCE_NUMBER_BIT_MASK

constexpr std::uint8_t isobus::FastPacketProtocol::SEQUENCE_NUMBER_BIT_MASK = 0x07
staticconstexprprivate

Bit mask for masking out the sequence number bits.

Definition at line 215 of file nmea2000_fast_packet_protocol.hpp.

◆ SEQUENCE_NUMBER_BIT_OFFSET

constexpr std::uint8_t isobus::FastPacketProtocol::SEQUENCE_NUMBER_BIT_OFFSET = 5
staticconstexprprivate

The bit offset into the first byte of data to get the seq number.

Definition at line 216 of file nmea2000_fast_packet_protocol.hpp.

◆ sessionHistory

std::vector<FastPacketHistory> isobus::FastPacketProtocol::sessionHistory
private

Used to keep track of sequence numbers for future sessions.

Definition at line 221 of file nmea2000_fast_packet_protocol.hpp.

◆ sessionMutex

Mutex isobus::FastPacketProtocol::sessionMutex
private

A mutex to lock the sessions list in case someone starts a Tx while the stack is processing sessions.

Definition at line 220 of file nmea2000_fast_packet_protocol.hpp.


The documentation for this class was generated from the following files: