5#include "isobus/utility/system_timing.hpp"
14 maintainPowerTransmitData(sourceControlFunction),
15 txFlags(static_cast<std::uint32_t>(
TransmitFlags::NumberOfFlags), process_flags, this)
44 [](std::shared_ptr<MaintainPowerData> messageInfo) {
45 return SystemTiming::time_expired_ms(messageInfo->get_timestamp_ms(), MAINTAIN_POWER_TIMEOUT_MS);
60 LOG_ERROR(
"[Maintain Power]: Interface has not been initialized yet.");
64 MaintainPowerInterface::MaintainPowerData::MaintainPowerData(std::shared_ptr<ControlFunction> sendingControlFunction) :
65 sendingControlFunction(sendingControlFunction)
71 bool retVal = (inWorkState != currentImplementInWorkState);
72 currentImplementInWorkState = inWorkState;
78 return currentImplementInWorkState;
83 bool retVal = (readyToWorkState != currentImplementReadyToWorkState);
84 currentImplementReadyToWorkState = readyToWorkState;
90 return currentImplementReadyToWorkState;
95 bool retVal = (parkState != currentImplementParkState);
96 currentImplementParkState = parkState;
102 return currentImplementParkState;
107 bool retVal = (transportState != currentImplementTransportState);
108 currentImplementTransportState = transportState;
114 return currentImplementTransportState;
119 bool retVal = (currentMaintainActuatorPowerState != maintainState);
120 currentMaintainActuatorPowerState = maintainState;
126 return currentMaintainActuatorPowerState;
131 bool retVal = (currentMaintainECUPowerState != maintainState);
132 currentMaintainECUPowerState = maintainState;
138 return currentMaintainECUPowerState;
143 return sendingControlFunction;
148 timestamp_ms = timestamp;
188 std::shared_ptr<MaintainPowerInterface::MaintainPowerData> retVal =
nullptr;
199 const std::array<std::uint8_t, CAN_DATA_LENGTH> buffer = {
223 assert(
nullptr != parentPointer);
226 if (!targetInterface->send_maintain_power())
235 assert(
nullptr != parentPointer);
249 switch (decodedKeySwitchState)
253 if (0 != targetInterface->keyNotOffTimestamp)
255 LOG_INFO(
"[Maintain Power]: The key switch state has transitioned from NOT OFF to OFF.");
256 targetInterface->keyNotOffTimestamp = 0;
259 targetInterface->keyOffTimestamp = SystemTiming::get_timestamp_ms();
261 targetInterface->maintainPowerTransmitTimestamp_ms = SystemTiming::get_timestamp_ms();
262 targetInterface->keySwitchOffEventPublisher.invoke();
264 else if (0 == targetInterface->keyOffTimestamp)
266 LOG_INFO(
"[Maintain Power]: The key switch state is detected as OFF.");
267 targetInterface->keyOffTimestamp = SystemTiming::get_timestamp_ms();
274 if (0 != targetInterface->keyOffTimestamp)
276 LOG_INFO(
"[Maintain Power]: The key switch state has transitioned from OFF to NOT OFF.");
277 targetInterface->keyOffTimestamp = 0;
278 targetInterface->keyNotOffTimestamp = SystemTiming::get_timestamp_ms();
280 else if (0 == targetInterface->keyNotOffTimestamp)
282 LOG_INFO(
"[Maintain Power]: The key switch state is detected as NOT OFF.");
283 targetInterface->keyNotOffTimestamp = SystemTiming::get_timestamp_ms();
285 targetInterface->maintainPowerTransmitTimestamp_ms = 0;
289 case KeySwitchState::Error:
291 LOG_WARNING(
"[Maintain Power]: The key switch is in an error state.");
292 targetInterface->keyOffTimestamp = 0;
293 targetInterface->keyNotOffTimestamp = 0;
294 targetInterface->maintainPowerTransmitTimestamp_ms = 0;
308 LOG_WARNING(
"[Maintain Power]: Received malformed wheel based speed PGN. DLC must be 8.");
317 auto result = std::find_if(targetInterface->receivedMaintainPowerMessages.cbegin(),
318 targetInterface->receivedMaintainPowerMessages.cend(),
319 [&message](
const std::shared_ptr<MaintainPowerData> &receivedInfo) {
320 return (nullptr != receivedInfo) && (receivedInfo->get_sender_control_function() == message.get_source_control_function());
323 if (result == targetInterface->receivedMaintainPowerMessages.end())
326 targetInterface->receivedMaintainPowerMessages.push_back(std::make_shared<MaintainPowerData>(message.
get_source_control_function()));
327 result = targetInterface->receivedMaintainPowerMessages.end() - 1;
330 auto &mpMessage = *result;
331 bool changed =
false;
339 mpMessage->set_timestamp_ms(SystemTiming::get_timestamp_ms());
341 targetInterface->maintainPowerDataEventPublisher.call(mpMessage, changed);
346 LOG_WARNING(
"[Maintain Power]: Received malformed maintain power PGN. DLC must be 8.");
Defines some PGNs that are used in the library or are very common.
The main class that manages the ISOBUS stack including: callbacks, Name to Address management,...
A class that acts as a logging sink. The intent is that someone could make their own derived class of...
std::uint32_t get_parameter_group_number() const
Returns the PGN encoded in the identifier.
A class that represents a generic CAN message of arbitrary length.
std::uint32_t get_data_length() const
Returns the length of the data in the CAN message.
std::shared_ptr< ControlFunction > get_source_control_function() const
Gets the source control function that the message is from.
std::uint8_t get_uint8_at(const std::uint32_t index) const
Get a 8-bit unsigned byte from the buffer at a specific index. A 8-bit unsigned byte can hold a value...
CANIdentifier get_identifier() const
Returns the identifier of the message.
static CANNetworkManager CANNetwork
Static singleton of the one network manager. Use this to access stack functionality.
void add_any_control_function_parameter_group_number_callback(std::uint32_t parameterGroupNumber, CANLibCallback callback, void *parent)
Registers a callback for ANY control function sending the associated PGN.
bool send_can_message(std::uint32_t parameterGroupNumber, const std::uint8_t *dataBuffer, std::uint32_t dataLength, std::shared_ptr< InternalControlFunction > sourceControlFunction, std::shared_ptr< ControlFunction > destinationControlFunction=nullptr, CANIdentifier::CANPriority priority=CANIdentifier::CANPriority::PriorityDefault6, TransmitCompleteCallback txCompleteCallback=nullptr, void *parentPointer=nullptr, DataChunkCallback frameChunkCallback=nullptr)
This is the main way to send a CAN message of any length.
void remove_any_control_function_parameter_group_number_callback(std::uint32_t parameterGroupNumber, CANLibCallback callback, void *parent)
This is how you remove a callback added with add_any_control_function_parameter_group_number_callback...
MaintainActuatorPower
Enumerates the different states that can be requested in the "Maintain Actuator Power" SPN.
ImplementTransportState get_implement_transport_state() const
Returns the reported implement transport state.
bool set_implement_transport_state(ImplementTransportState transportState)
Sets the reported implement transport state.
bool set_implement_in_work_state(ImplementInWorkState inWorkState)
Sets the reported implement in-work state.
bool set_implement_park_state(ImplementParkState parkState)
Sets the reported implement park state.
std::shared_ptr< ControlFunction > get_sender_control_function() const
Returns a pointer to the sender of the message. If an ICF is the sender, returns the ICF being used t...
std::uint32_t get_timestamp_ms() const
Returns the timestamp for when the message was received, in milliseconds.
MaintainECUPower get_maintain_ecu_power() const
Returns the reported maintain ECU power state.
bool set_implement_ready_to_work_state(ImplementReadyToWorkState readyToWorkState)
Sets the reported implement ready to work state.
bool set_maintain_ecu_power(MaintainECUPower maintainState)
Sets the reported maintain ECU power state.
ImplementParkState get_implement_park_state() const
Returns the reported implement park state.
ImplementReadyToWorkState
Signal that indicates that an implement is connected to a tractor or power unit and is ready for work...
ImplementInWorkState
Signal that indicates that an implement is connected to a tractor or power unit and is in work state.
MaintainECUPower
Enumerates the different states that can be requested in the "Maintain ECU Power" SPN.
ImplementInWorkState get_implement_in_work_state() const
Returns the reported implement in-work state.
MaintainActuatorPower get_maintain_actuator_power() const
Returns the reported maintain actuator power state.
bool set_maintain_actuator_power(MaintainActuatorPower maintainState)
Sets the reported maintain actuator power state.
ImplementReadyToWorkState get_implement_ready_to_work_state() const
Returns the reported implement ready to work state.
ImplementParkState
Indicates the state of an implement where it may be disconnected from a tractor or power unit.
ImplementTransportState
Indicates the transport state of an implement connected to a tractor or power unit.
void set_timestamp_ms(std::uint32_t timestamp)
Sets the timestamp for when the message was received or sent.
Manages sending and receiving the maintain power message (PGN 65095)
EventDispatcher< const std::shared_ptr< MaintainPowerData >, bool > maintainPowerDataEventPublisher
An event publisher for notifying when new maintain power messages are received.
std::uint32_t maintainPowerTime_ms
The amount of time to ask the TECU to maintain actuator/section power. Will be rounded up to the next...
std::uint32_t maintainPowerTransmitTimestamp_ms
Timestamp used to know when to transmit the maintain power message in milliseconds.
bool initialized
Stores if the interface has been initialized.
KeySwitchState
Enumerates the key switch states of the tractor or power unit.
@ NotOff
Key is not off (does not always mean that it's on!)
std::shared_ptr< MaintainPowerData > get_received_maintain_power(std::size_t index)
Returns the content of a received maintain power message based on the index of the sender....
MaintainPowerInterface(std::shared_ptr< InternalControlFunction > sourceControlFunction)
Constructor for a MaintainPowerInterface.
void set_maintain_power_time(std::uint32_t timeToMaintainPower)
Use this to tell the interface how long it should transmit the maintain power message after it detect...
TransmitFlags
Enumerates a set of flags to manage transmitting messages owned by this interface.
@ SendMaintainPower
A flag to manage sending the maintain power message.
bool send_maintain_power() const
Transmits the maintain power message.
ProcessingFlags txFlags
Tx flag for sending the maintain power message. Handles retries automatically.
EventDispatcher< const std::shared_ptr< MaintainPowerData >, bool > & get_maintain_power_data_event_publisher()
Returns an event dispatcher which you can use to get callbacks when new/updated maintain power messag...
static void process_rx_message(const CANMessage &message, void *parentPointer)
Processes a CAN message.
~MaintainPowerInterface()
Destructor for a MaintainPowerInterface.
std::uint32_t keyOffTimestamp
A timestamp to track when the key is off, used to calculate how many messages to send and when to sen...
EventDispatcher & get_key_switch_transition_off_event_publisher()
Returns an event dispatcher which you can use to get callbacks when the key switch transitions from t...
bool get_initialized() const
Returns if the interface has been initialized.
MaintainPowerData maintainPowerTransmitData
Use this to configure the transmission of the maintain power message.
std::size_t get_number_received_maintain_power_sources() const
Returns the number of unique senders of the maintain power message.
EventDispatcher keySwitchOffEventPublisher
An event publisher for notifying when the key switch transitions to the off state.
void update()
Call this cyclically to update the interface. Transmits messages if needed and processes timeouts for...
std::uint32_t get_maintain_power_time() const
Returns the amount of time that the interface will continue to send the maintain power message after ...
static constexpr std::uint32_t MAINTAIN_POWER_TIMEOUT_MS
The amount of time that power can be maintained per message, used as the timeout as well.
static void process_flags(std::uint32_t flag, void *parentPointer)
Processes one flag (which sends the associated message)
std::vector< std::shared_ptr< MaintainPowerData > > receivedMaintainPowerMessages
A list of all received maintain power messages.
void initialize()
Sets up the class and registers it to receive callbacks from the network manager for processing messa...
Defines an interface for sending and receiving the maintain power message (PGN 65095).
This namespace encompasses all of the ISO11783 stack's functionality to reduce global namespace pollu...
constexpr std::uint8_t CAN_DATA_LENGTH
The length of a classical CAN frame.