Skip to content

Commit

Permalink
refactor(core): move the control function' create and destroy functio…
Browse files Browse the repository at this point in the history
…ns to the network manager

The new way of making control functions will be `CANNetworkManager::create_internal_control_function` and `CANNetworkManager::create_partnered_control_function` respectively. For de-activating a control function the function  `CANNetworkManager::deactivate_control_function` should now be used.
  • Loading branch information
GwnDaan committed Apr 17, 2024
1 parent 2c58692 commit 87d0b09
Show file tree
Hide file tree
Showing 39 changed files with 242 additions and 335 deletions.
2 changes: 1 addition & 1 deletion examples/diagnostic_protocol/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ int main()
TestDeviceNAME.set_device_class_instance(0);
TestDeviceNAME.set_manufacturer_code(1407);

auto TestInternalECU = isobus::InternalControlFunction::create(TestDeviceNAME, 0x1C, 0);
auto TestInternalECU = isobus::CANNetworkManager::CANNetwork.create_internal_control_function(TestDeviceNAME, 0);

// Make sure address claiming is done before we continue
auto addressClaimedFuture = std::async(std::launch::async, [&TestInternalECU]() {
Expand Down
2 changes: 1 addition & 1 deletion examples/nmea2000/fast_packet_protocol/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ int main()
TestDeviceNAME.set_device_class_instance(0);
TestDeviceNAME.set_manufacturer_code(1407);

auto TestInternalECU = isobus::InternalControlFunction::create(TestDeviceNAME, 0x1C, 0);
auto TestInternalECU = isobus::CANNetworkManager::CANNetwork.create_internal_control_function(TestDeviceNAME, 0);

isobus::CANNetworkManager::CANNetwork.get_fast_packet_protocol(0)->register_multipacket_message_callback(0x1F001, nmea2k_callback, nullptr);

Expand Down
2 changes: 1 addition & 1 deletion examples/nmea2000/nmea2000_generator/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ int main()
TestDeviceNAME.set_device_class_instance(0);
TestDeviceNAME.set_manufacturer_code(1407);

auto TestInternalECU = isobus::InternalControlFunction::create(TestDeviceNAME, 0x1C, 0);
auto TestInternalECU = isobus::CANNetworkManager::CANNetwork.create_internal_control_function(TestDeviceNAME, 0);

// Make sure address claiming is done before we continue
auto addressClaimedFuture = std::async(std::launch::async, [&TestInternalECU]() {
Expand Down
2 changes: 1 addition & 1 deletion examples/nmea2000/nmea2000_parser/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ int main()
TestDeviceNAME.set_device_class_instance(0);
TestDeviceNAME.set_manufacturer_code(1407);

auto TestInternalECU = isobus::InternalControlFunction::create(TestDeviceNAME, 0x1D, 0);
auto TestInternalECU = isobus::CANNetworkManager::CANNetwork.create_internal_control_function(TestDeviceNAME, 0);

// Make sure address claiming is done before we continue
auto addressClaimedFuture = std::async(std::launch::async, [&TestInternalECU]() {
Expand Down
2 changes: 1 addition & 1 deletion examples/pgn_requests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ int main()
TestDeviceNAME.set_device_class_instance(0);
TestDeviceNAME.set_manufacturer_code(1407);

auto TestInternalECU = isobus::InternalControlFunction::create(TestDeviceNAME, 0x1C, 0);
auto TestInternalECU = isobus::CANNetworkManager::CANNetwork.create_internal_control_function(TestDeviceNAME, 0);
std::signal(SIGINT, signal_handler);

// Make sure address claiming is done before we continue
Expand Down
7 changes: 4 additions & 3 deletions examples/seeder_example/seeder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "isobus/hardware_integration/available_can_drivers.hpp"
#include "isobus/hardware_integration/can_hardware_interface.hpp"
#include "isobus/isobus/isobus_diagnostic_protocol.hpp"
#include "isobus/isobus/isobus_preferred_addresses.hpp"
#include "isobus/isobus/isobus_standard_data_description_indices.hpp"
#include "isobus/isobus/isobus_task_controller_client.hpp"

Expand Down Expand Up @@ -76,9 +77,9 @@ bool Seeder::initialize()
filterTaskControllerIndustryGroup,
filterTaskControllerDeviceClass };
const std::vector<isobus::NAMEFilter> vtNameFilters = { filterVirtualTerminal };
auto InternalECU = isobus::InternalControlFunction::create(TestDeviceNAME, 0x81, 0);
auto PartnerVT = isobus::PartneredControlFunction::create(0, vtNameFilters);
auto PartnerTC = isobus::PartneredControlFunction::create(0, tcNameFilters);
auto InternalECU = isobus::CANNetworkManager::CANNetwork.create_internal_control_function(TestDeviceNAME, 0);
auto PartnerVT = isobus::CANNetworkManager::CANNetwork.create_partnered_control_function(0, vtNameFilters);
auto PartnerTC = isobus::CANNetworkManager::CANNetwork.create_partnered_control_function(0, tcNameFilters);

diagnosticProtocol = std::make_unique<isobus::DiagnosticProtocol>(InternalECU);
diagnosticProtocol->initialize();
Expand Down
4 changes: 2 additions & 2 deletions examples/task_controller_client/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ int main()
filterTaskControllerInstance,
filterTaskControllerIndustryGroup,
filterTaskControllerDeviceClass };
auto TestInternalECU = isobus::InternalControlFunction::create(TestDeviceNAME, 0x1C, 0);
auto TestPartnerTC = isobus::PartneredControlFunction::create(0, tcNameFilters);
auto TestInternalECU = isobus::CANNetworkManager::CANNetwork.create_internal_control_function(TestDeviceNAME, 0);
auto TestPartnerTC = isobus::CANNetworkManager::CANNetwork.create_partnered_control_function(0, tcNameFilters);

TestTCClient = std::make_shared<isobus::TaskControllerClient>(TestPartnerTC, TestInternalECU, nullptr);

Expand Down
8 changes: 4 additions & 4 deletions examples/transport_layer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ int main()
const NAMEFilter filterOriginator(NAME::NAMEParameters::FunctionCode, static_cast<std::uint8_t>(NAME::Function::SteeringControl));
const NAMEFilter filterRecipient(NAME::NAMEParameters::FunctionCode, static_cast<std::uint8_t>(NAME::Function::VirtualTerminal));

auto originatorECU = InternalControlFunction::create(originatorNAME, 0x1C, 0);
auto originatorPartner = PartneredControlFunction::create(1, { filterOriginator });
auto recipientECU = InternalControlFunction::create(recipientNAME, 0x1D, 1);
auto recipientPartner = PartneredControlFunction::create(0, { filterRecipient });
auto originatorECU = CANNetworkManager::CANNetwork.create_internal_control_function(originatorNAME, 0, 0x1C);
auto originatorPartner = CANNetworkManager::CANNetwork.create_partnered_control_function(1, { filterOriginator });
auto recipientECU = CANNetworkManager::CANNetwork.create_internal_control_function(recipientNAME, 1, 0x1D);
auto recipientPartner = CANNetworkManager::CANNetwork.create_partnered_control_function(0, { filterRecipient });

// We want to make sure address claiming is successful before continuing
auto addressClaimedFuture = std::async(std::launch::async, [&originatorECU, &recipientECU, &originatorPartner, &recipientPartner]() {
Expand Down
4 changes: 2 additions & 2 deletions examples/virtual_terminal/aux_functions/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ int main()

const isobus::NAMEFilter filterVirtualTerminal(isobus::NAME::NAMEParameters::FunctionCode, static_cast<std::uint8_t>(isobus::NAME::Function::VirtualTerminal));
const std::vector<isobus::NAMEFilter> vtNameFilters = { filterVirtualTerminal };
auto TestInternalECU = isobus::InternalControlFunction::create(TestDeviceNAME, 0x1D, 0);
auto TestPartnerVT = isobus::PartneredControlFunction::create(0, vtNameFilters);
auto TestInternalECU = isobus::CANNetworkManager::CANNetwork.create_internal_control_function(TestDeviceNAME, 0);
auto TestPartnerVT = isobus::CANNetworkManager::CANNetwork.create_partnered_control_function(0, vtNameFilters);

TestVirtualTerminalClient = std::make_shared<isobus::VirtualTerminalClient>(TestPartnerVT, TestInternalECU);
TestVirtualTerminalClient->set_object_pool(0, testPool.data(), testPool.size(), objectPoolHash);
Expand Down
4 changes: 2 additions & 2 deletions examples/virtual_terminal/aux_inputs/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ int main()

const isobus::NAMEFilter filterVirtualTerminal(isobus::NAME::NAMEParameters::FunctionCode, static_cast<std::uint8_t>(isobus::NAME::Function::VirtualTerminal));
const std::vector<isobus::NAMEFilter> vtNameFilters = { filterVirtualTerminal };
auto TestInternalECU = isobus::InternalControlFunction::create(TestDeviceNAME, 0x1E, 0);
auto TestPartnerVT = isobus::PartneredControlFunction::create(0, vtNameFilters);
auto TestInternalECU = isobus::CANNetworkManager::CANNetwork.create_internal_control_function(TestDeviceNAME, 0);
auto TestPartnerVT = isobus::CANNetworkManager::CANNetwork.create_partnered_control_function(0, vtNameFilters);

TestVirtualTerminalClient = std::make_shared<isobus::VirtualTerminalClient>(TestPartnerVT, TestInternalECU);
TestVirtualTerminalClient->set_object_pool(0, testPool.data(), testPool.size(), objectPoolHash);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ extern "C" void app_main()

const isobus::NAMEFilter filterVirtualTerminal(isobus::NAME::NAMEParameters::FunctionCode, static_cast<std::uint8_t>(isobus::NAME::Function::VirtualTerminal));
const std::vector<isobus::NAMEFilter> vtNameFilters = { filterVirtualTerminal };
auto TestInternalECU = isobus::InternalControlFunction::create(TestDeviceNAME, 0x1C, 0);
auto TestPartnerVT = isobus::PartneredControlFunction::create(0, vtNameFilters);
auto TestInternalECU = isobus::CANNetworkManager::CANNetwork.create_internal_control_function(TestDeviceNAME, 0);
auto TestPartnerVT = isobus::CANNetworkManager::CANNetwork.create_partnered_control_function(0, vtNameFilters);

virtualTerminalClient = std::make_shared<isobus::VirtualTerminalClient>(TestPartnerVT, TestInternalECU);
virtualTerminalClient->set_object_pool(0, testPool, (object_pool_end - object_pool_start) - 1, "ais1");
Expand Down
4 changes: 2 additions & 2 deletions examples/virtual_terminal/version3_object_pool/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ int main()

const isobus::NAMEFilter filterVirtualTerminal(isobus::NAME::NAMEParameters::FunctionCode, static_cast<std::uint8_t>(isobus::NAME::Function::VirtualTerminal));
const std::vector<isobus::NAMEFilter> vtNameFilters = { filterVirtualTerminal };
auto TestInternalECU = isobus::InternalControlFunction::create(TestDeviceNAME, 0x1C, 0);
auto TestPartnerVT = isobus::PartneredControlFunction::create(0, vtNameFilters);
auto TestInternalECU = isobus::CANNetworkManager::CANNetwork.create_internal_control_function(TestDeviceNAME, 0);
auto TestPartnerVT = isobus::CANNetworkManager::CANNetwork.create_partnered_control_function(0, vtNameFilters);

virtualTerminalClient = std::make_shared<isobus::VirtualTerminalClient>(TestPartnerVT, TestInternalECU);
virtualTerminalClient->set_object_pool(0, testPool.data(), testPool.size(), objectPoolHash);
Expand Down
29 changes: 7 additions & 22 deletions isobus/include/isobus/isobus/can_control_function.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/// @author Adrian Del Grosso
/// @author Daan Steenbergen
///
/// @copyright 2022 Adrian Del Grosso
/// @copyright 2024 The Open-Agriculture Developers
//================================================================================================

#ifndef CAN_CONTROL_FUNCTION_HPP
Expand All @@ -19,12 +19,8 @@

namespace isobus
{
//================================================================================================
/// @class ControlFunction
///
/// @brief A class that describes an ISO11783 control function, which includes a NAME and address.
//================================================================================================
class ControlFunction : public std::enable_shared_from_this<ControlFunction>
class ControlFunction
{
public:
/// @brief The type of the control function
Expand All @@ -35,19 +31,15 @@ namespace isobus
Partnered ///< An external control function that you explicitly want to talk to
};

virtual ~ControlFunction() = default;

/// @brief The factory function to construct a control function
/// @brief The constructor of a control function. In most cases use `CANNetworkManager::create_internal_control_function()` or
/// `CANNetworkManager::create_partnered_control_function()` instead, only use this constructor if you have advanced needs.
/// @param[in] NAMEValue The NAME of the control function
/// @param[in] addressValue The current address of the control function
/// @param[in] CANPort The CAN channel index that the control function communicates on
/// @returns A shared pointer to a ControlFunction object created with the parameters passed in
static std::shared_ptr<ControlFunction> create(NAME NAMEValue, std::uint8_t addressValue, std::uint8_t CANPort);
/// @param[in] type The 'Type' of control function to create
ControlFunction(NAME NAMEValue, std::uint8_t addressValue, std::uint8_t CANPort, Type type = Type::External);

/// @brief Destroys this control function, by removing it from the network manager
/// @param[in] expectedRefCount The expected number of shared pointers to this control function after removal
/// @returns true if the control function was successfully removed from everywhere in the stack, otherwise false
virtual bool destroy(std::uint32_t expectedRefCount = 1);
virtual ~ControlFunction() = default;

/// @brief Returns the current address of the control function
/// @returns The current address of the control function
Expand All @@ -74,13 +66,6 @@ namespace isobus
std::string get_type_string() const;

protected:
/// @brief The protected constructor for the control function, which is called by the (inherited) factory function
/// @param[in] NAMEValue The NAME of the control function
/// @param[in] addressValue The current address of the control function
/// @param[in] CANPort The CAN channel index that the control function communicates on
/// @param[in] type The 'Type' of control function to create
ControlFunction(NAME NAMEValue, std::uint8_t addressValue, std::uint8_t CANPort, Type type = Type::External);

friend class CANNetworkManager; ///< The network manager needs access to the control function's internals
static Mutex controlFunctionProcessingMutex; ///< Protects the control function tables
const Type controlFunctionType; ///< The Type of the control function
Expand Down
31 changes: 8 additions & 23 deletions isobus/include/isobus/isobus/can_internal_control_function.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,13 @@ namespace isobus
class InternalControlFunction : public ControlFunction
{
public:
/// @brief The factory function to construct an internal control function
/// @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.
/// @param[in] desiredName The NAME for this control function to claim as
/// @param[in] preferredAddress The preferred NAME for this control function
/// @param[in] CANPort The CAN channel index for this control function to use
/// @returns A shared pointer to an InternalControlFunction object created with the parameters passed in
static std::shared_ptr<InternalControlFunction> create(NAME desiredName, std::uint8_t preferredAddress, std::uint8_t CANPort);

/// @brief The factory function to construct an internal control function.
/// This version of the factory function will automatically assign the preferred address somewhere in the arbitrary address
/// range, which means your NAME must have the arbitrary address bit set.
/// @param[in] desiredName The NAME for this control function to claim as
/// @param[in] CANPort The CAN channel index for this control function to use
/// @returns A shared pointer to an InternalControlFunction object created with the parameters passed in
static std::shared_ptr<InternalControlFunction> create(NAME desiredName, std::uint8_t CANPort);

/// @brief Destroys this control function, by removing it from the network manager
/// @param[in] expectedRefCount The expected number of shared pointers to this control function after removal
/// @returns true if the control function was successfully removed from everywhere in the stack, otherwise false
bool destroy(std::uint32_t expectedRefCount = 1) override;

/// @brief The protected constructor for the internal control function, which is called by the (inherited) factory function
/// @param[in] desiredName The NAME for this control function to claim as
/// @param[in] preferredAddress The preferred NAME for this control function
/// @param[in] CANPort The CAN channel index for this control function to use
InternalControlFunction(NAME desiredName, std::uint8_t preferredAddress, std::uint8_t CANPort, CANLibBadge<InternalControlFunction>);
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.
Expand All @@ -77,9 +59,12 @@ namespace isobus
/// @returns The PGN request protocol for this ICF
std::weak_ptr<ParameterGroupNumberRequestProtocol> get_pgn_request_protocol() const;

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
std::shared_ptr<ParameterGroupNumberRequestProtocol> pgnRequestProtocol; ///< The PGN request protocol for this ICF
};

} // namespace isobus
Expand Down
Loading

0 comments on commit 87d0b09

Please sign in to comment.