Skip to content

Commit

Permalink
[Core]: Refactorization of control function address claiming and fact…
Browse files Browse the repository at this point in the history
…ory methods (#463)

* refactor(core): move the control function' create and destroy functions 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.

* refactor(core): move address claiming into internal control function class

* feat: add range detection based on IG for dynamic address claiming

Other small improvements:
- removed unused include in seeder example
- added boundary check on the control function table
  • Loading branch information
GwnDaan authored May 19, 2024
1 parent 2e88daa commit 830fe91
Show file tree
Hide file tree
Showing 43 changed files with 701 additions and 953 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
6 changes: 3 additions & 3 deletions examples/seeder_example/seeder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,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
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 @@ -62,7 +61,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.

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
Loading

0 comments on commit 830fe91

Please sign in to comment.