Skip to content

Commit

Permalink
refactor(core): move address claiming into internal control function …
Browse files Browse the repository at this point in the history
…class
  • Loading branch information
GwnDaan committed Apr 17, 2024
1 parent 87d0b09 commit b0a1ccb
Show file tree
Hide file tree
Showing 9 changed files with 445 additions and 632 deletions.
2 changes: 0 additions & 2 deletions isobus/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ set(ISOBUS_SRC
"can_control_function.cpp"
"can_message.cpp"
"can_network_manager.cpp"
"can_address_claim_state_machine.cpp"
"can_internal_control_function.cpp"
"can_partnered_control_function.cpp"
"can_NAME_filter.cpp"
Expand Down Expand Up @@ -59,7 +58,6 @@ set(ISOBUS_INCLUDE
"can_message.hpp"
"can_general_parameter_group_numbers.hpp"
"can_network_manager.hpp"
"can_address_claim_state_machine.hpp"
"can_NAME_filter.hpp"
"can_transport_protocol.hpp"
"can_transport_protocol_base.hpp"
Expand Down
116 changes: 0 additions & 116 deletions isobus/include/isobus/isobus/can_address_claim_state_machine.hpp

This file was deleted.

91 changes: 72 additions & 19 deletions isobus/include/isobus/isobus/can_internal_control_function.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,44 @@
/// @author Adrian Del Grosso
/// @author Daan Steenbergen
///
/// @copyright 2022 Adrian Del Grosso
/// @copyright 2024 The Open-Agriculture Developers
//================================================================================================

#ifndef CAN_INTERNAL_CONTROL_FUNCTION_HPP
#define CAN_INTERNAL_CONTROL_FUNCTION_HPP

#include "isobus/isobus/can_address_claim_state_machine.hpp"
#include "isobus/isobus/can_badge.hpp"
#include "isobus/isobus/can_control_function.hpp"
#include "isobus/isobus/can_message.hpp"
#include "isobus/utility/event_dispatcher.hpp"

#include <vector>

namespace isobus
{
class CANNetworkManager;
class ParameterGroupNumberRequestProtocol;

//================================================================================================
/// @class InternalControlFunction
///
/// @brief Describes an internal ECU's NAME and address data. Used to send CAN messages.
/// @details This class is used to define your own ECU's NAME, and is used to transmit messages.
/// Each instance of this class will claim a unique address on the bus, and can be used to
/// send messages.
//================================================================================================
class InternalControlFunction : public ControlFunction
{
public:
/// @brief Defines the states the internal control function can be in
enum class State
{
None, ///< Initial state
WaitForClaim, ///< Waiting for the random delay time to expire
SendRequestForClaim, ///< Sending the request for address claim to the bus
WaitForRequestContentionPeriod, ///< Waiting for the address claim contention period to expire
SendPreferredAddressClaim, ///< Claiming the preferred address as our own
ContendForPreferredAddress, ///< Contending the preferred address with another ECU
SendArbitraryAddressClaim, ///< Claiming an arbitrary (not our preferred) address as our own
SendReclaimAddressOnRequest, ///< An ECU requested address claim, inform the bus of our current address
UnableToClaim, ///< Unable to claim an address
AddressClaimingComplete ///< Address claiming is complete and we have an address
};

/// @brief The constructor of an internal control function.
/// In most cases use `CANNetworkManager::create_internal_control_function()` instead,
/// only use this constructor if you have advanced needs.
Expand All @@ -42,29 +52,72 @@ namespace isobus
/// @param[in] CANPort The CAN channel index for this control function to use
InternalControlFunction(NAME desiredName, std::uint8_t preferredAddress, std::uint8_t CANPort);

/// @brief Used to inform the member address claim state machine that two CFs are using the same source address.
/// @note Address violation occurs when two CFs are using the same source address.
void on_address_violation(CANLibBadge<CANNetworkManager>);
/// @brief Returns the current state of the internal control function
/// @returns The current state
State get_current_state() const;

/// @brief Processes a CAN message for address claiming purposes
/// @param[in] message The CAN message being received
void process_rx_message_for_address_claiming(const CANMessage &message);

/// @brief Used by the network manager to tell the ICF that the address claim state machine needs to process
/// a J1939 command to move address.
/// @param[in] commandedAddress The address that the ICF has been commanded to move to
void process_commanded_address(std::uint8_t commandedAddress, CANLibBadge<CANNetworkManager>);
/// @brief Updates the internal control function address claiming, will be called periodically by
/// the network manager if the ICF is registered there.
/// @returns true if the address of internal control function has changed, otherwise false
bool update_address_claiming();

/// @brief Updates the internal control function together with it's associated address claim state machine
/// @returns Wether the control function has changed address by the end of the update
bool update_address_claiming(CANLibBadge<CANNetworkManager>);
/// @brief Returns the preferred address of the internal control function
/// @returns The preferred address
std::uint8_t get_preferred_address() const;

/// @brief Returns the event dispatcher for when an address is claimed. Use this to register a callback
/// for when an address is claimed.
/// @returns The event dispatcher for when an address is claimed
EventDispatcher<std::uint8_t> &get_address_claimed_event_dispatcher();

/// @brief Gets the PGN request protocol for this ICF
/// @returns The PGN request protocol for this ICF
std::weak_ptr<ParameterGroupNumberRequestProtocol> get_pgn_request_protocol() const;

/// @brief Validates that a CAN message has not caused an address violation for this ICF.
/// If a violation is found, a re-claim will be executed for as is required by ISO 11783-5,
/// and will attempt to activate a DTC that is defined in ISO 11783-5.
/// This function is for advanced use cases only. Normally, the network manager will call this
/// for every message received.
/// @note Address violation occurs when two CFs are using the same source address.
/// @param[in] message The message to process
/// @returns true if the message caused an address violation, otherwise false
bool process_rx_message_for_address_violation(const CANMessage &message);

protected:
friend class CANNetworkManager; ///< Allow the network manager to access the pgn request protocol
std::shared_ptr<ParameterGroupNumberRequestProtocol> pgnRequestProtocol; ///< The PGN request protocol for this ICF

private:
AddressClaimStateMachine stateMachine; ///< The address claimer for this ICF
/// @brief Sends the PGN request for the address claim PGN
/// @returns true if the message was sent, otherwise false
bool send_request_to_claim() const;

/// @brief Sends the address claim message to the bus
/// @param[in] address The address to claim
/// @returns true if the message was sent, otherwise false
bool send_address_claim(std::uint8_t address);

/// @brief Attempts to process a commanded address.
/// @details If the state machine has claimed successfully before,
/// this will attempt to move a NAME from the claimed address to the new, specified address.
/// @param[in] commandedAddress The address to attempt to claim
void process_commanded_address(std::uint8_t commandedAddress);

/// @brief Setter for the state
void set_current_state(State value);

static constexpr std::uint32_t ADDRESS_CONTENTION_TIME_MS = 250;

State state = State::None; ///< The current state of the internal control function
std::uint32_t stateChangeTimestamp_ms = 0; ///< A timestamp in milliseconds used for timing the address claiming process
std::uint8_t preferredAddress; ///< The address we'd prefer to claim as (we may not get it)
std::uint8_t randomClaimDelay_ms; ///< The random delay before claiming an address as required by the ISO11783 standard
EventDispatcher<std::uint8_t> addressClaimedDispatcher; ///< The event dispatcher for when an address is claimed
};

} // namespace isobus
Expand Down
1 change: 1 addition & 0 deletions isobus/include/isobus/isobus/can_message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#ifndef CAN_MESSAGE_HPP
#define CAN_MESSAGE_HPP

#include "isobus/isobus/can_constants.hpp"
#include "isobus/isobus/can_control_function.hpp"
#include "isobus/isobus/can_general_parameter_group_numbers.hpp"
#include "isobus/isobus/can_identifier.hpp"
Expand Down
Loading

0 comments on commit b0a1ccb

Please sign in to comment.