AgIsoStack++
A control-function-focused implementation of the major ISOBUS and J1939 protocols
Loading...
Searching...
No Matches
isobus_heartbeat.hpp
Go to the documentation of this file.
1//================================================================================================
16//================================================================================================
17#ifndef ISOBUS_HEARTBEAT_HPP
18#define ISOBUS_HEARTBEAT_HPP
19
23#include "isobus/utility/event_dispatcher.hpp"
24
25#include <list>
26
27namespace isobus
28{
31 {
32 public:
34 enum class HeartBeatError
35 {
38 };
39
43
49 void set_enabled(bool enable);
50
54 bool is_enabled() const;
55
66 bool request_heartbeat(std::shared_ptr<InternalControlFunction> sourceControlFunction,
67 std::shared_ptr<ControlFunction> destinationControlFunction) const;
68
72 void on_new_internal_control_function(std::shared_ptr<InternalControlFunction> newControlFunction);
73
77 void on_destroyed_internal_control_function(std::shared_ptr<InternalControlFunction> destroyedControlFunction);
78
84 EventDispatcher<HeartBeatError, std::shared_ptr<ControlFunction>> &get_heartbeat_error_event_dispatcher();
85
90 EventDispatcher<std::shared_ptr<ControlFunction>> &get_new_tracked_heartbeat_event_dispatcher();
91
94 void process_rx_message(const CANMessage &message);
95
98 void update();
99
100 private:
102 enum class SequenceCounterSpecialValue : std::uint8_t
103 {
104 Initial = 251,
105 Error = 254,
106 NotAvailable = 255
107 };
108
109 static constexpr std::uint32_t SEQUENCE_TIMEOUT_MS = 300;
110 static constexpr std::uint32_t SEQUENCE_INITIAL_RESPONSE_TIMEOUT_MS = 250;
111 static constexpr std::uint32_t SEQUENCE_REPETITION_RATE_MS = 100;
112
115 {
116 public:
119 explicit Heartbeat(std::shared_ptr<ControlFunction> sendingControlFunction);
120
125 bool send(const HeartbeatInterface &parent);
126
127 std::shared_ptr<ControlFunction> controlFunction;
128 std::uint32_t timestamp_ms;
130 std::uint8_t sequenceCounter = static_cast<std::uint8_t>(SequenceCounterSpecialValue::Initial);
131 };
132
140 static bool process_request_for_heartbeat(std::uint32_t parameterGroupNumber,
141 std::shared_ptr<ControlFunction> requestingControlFunction,
142 std::shared_ptr<ControlFunction> targetControlFunction,
143 std::uint32_t repetitionRate,
144 void *parentPointer);
145
147 EventDispatcher<HeartBeatError, std::shared_ptr<ControlFunction>> heartbeatErrorEventDispatcher;
148 EventDispatcher<std::shared_ptr<ControlFunction>> newTrackedHeartbeatEventDispatcher;
149 std::list<Heartbeat> trackedHeartbeats;
150 bool enabled = true;
151 };
152} // namespace isobus
153
154#endif
An object to represent common callbacks used within this CAN stack.
A representation of an ISOBUS ECU that we can send from. Use this class when defining your own contro...
An abstraction of a CAN message, could be > 8 data bytes.
A class that represents a generic CAN message of arbitrary length.
This class is used to store information about a tracked heartbeat.
std::shared_ptr< ControlFunction > controlFunction
The CF that is sending the message.
Heartbeat(std::shared_ptr< ControlFunction > sendingControlFunction)
Constructor for a Heartbeat.
bool send(const HeartbeatInterface &parent)
Transmits a heartbeat message (for internal control functions only). Updates the sequence counter and...
std::uint32_t repetitionRate_ms
For internal control functions, this controls how often the heartbeat is sent. This should really sta...
std::uint32_t timestamp_ms
The last time the message was sent by the associated control function.
std::uint8_t sequenceCounter
The sequence counter used to validate the heartbeat. Counts from 0-250 normally.
This class is used to send and receive ISOBUS heartbeats.
EventDispatcher< std::shared_ptr< ControlFunction > > newTrackedHeartbeatEventDispatcher
Event dispatcher for when a heartbeat message from another control function becomes tracked by this i...
static bool process_request_for_heartbeat(std::uint32_t parameterGroupNumber, std::shared_ptr< ControlFunction > requestingControlFunction, std::shared_ptr< ControlFunction > targetControlFunction, std::uint32_t repetitionRate, void *parentPointer)
Processes a PGN request for a heartbeat.
void set_enabled(bool enable)
This can be used to disable or enable this heartbeat functionality. It's probably best to leave it en...
EventDispatcher< std::shared_ptr< ControlFunction > > & get_new_tracked_heartbeat_event_dispatcher()
Returns an event dispatcher which can be used to register for new tracked heartbeat events....
std::list< Heartbeat > trackedHeartbeats
Store tracked heartbeat data, per CF.
HeartBeatError
This enum is used to define the possible errors that can occur when receiving a heartbeat.
@ InvalidSequenceCounter
The sequence counter is not valid.
@ TimedOut
The heartbeat message has not been received within the repetition rate.
EventDispatcher< HeartBeatError, std::shared_ptr< ControlFunction > > & get_heartbeat_error_event_dispatcher()
Returns an event dispatcher which can be used to register for heartbeat errors. Heartbeat errors are ...
static constexpr std::uint32_t SEQUENCE_TIMEOUT_MS
If the repetition rate exceeds 300 ms an error in the communication is detected.
bool is_enabled() const
Returns if the interface is currently enabled or not.
SequenceCounterSpecialValue
This enum is used to define special values for the sequence counter.
@ Initial
The heartbeat sequence number value shall be set to 251 once upon initialization of a CF.
@ NotAvailable
This value shall be used when the transmitted CF is in a shutdown status and is gracefully disconnect...
@ Error
Sequence Number value 254 indicates an error condition.
bool enabled
Attribute that specifies if this interface is enabled. When false, the interface does nothing.
bool request_heartbeat(std::shared_ptr< InternalControlFunction > sourceControlFunction, std::shared_ptr< ControlFunction > destinationControlFunction) const
This method can be used to request that another control function on the bus start sending the heartbe...
static constexpr std::uint32_t SEQUENCE_INITIAL_RESPONSE_TIMEOUT_MS
When requesting a heartbeat from another device, If no response for the repetition rate has been rece...
void update()
Updates the interface. Called by the network manager, so there is no need for you to call it in your ...
void process_rx_message(const CANMessage &message)
Processes a CAN message, called by the network manager.
static constexpr std::uint32_t SEQUENCE_REPETITION_RATE_MS
A consuming CF shall send a Request for Repetition rate for the heart beat message with a repetition ...
EventDispatcher< HeartBeatError, std::shared_ptr< ControlFunction > > heartbeatErrorEventDispatcher
Event dispatcher for heartbeat errors.
const CANMessageFrameCallback sendCANFrameCallback
A callback for sending a CAN frame.
void on_destroyed_internal_control_function(std::shared_ptr< InternalControlFunction > destroyedControlFunction)
Called when an internal control function is deleted. Cleans up stale registrations with PGN request p...
void on_new_internal_control_function(std::shared_ptr< InternalControlFunction > newControlFunction)
Called by the internal control function class when a new internal control function is added....
HeartbeatInterface(const CANMessageFrameCallback &sendCANFrameCallback)
Constructor for a HeartbeatInterface.
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.