Skip to content

Commit

Permalink
[Core]: Changed control function state callbacks to work for all CFs
Browse files Browse the repository at this point in the history
Added suggestion from GwnDaan to have the CF state callbacks provide
callbacks for all CFs rather than specific ones.
  • Loading branch information
ad3154 committed Sep 2, 2023
1 parent b0ff083 commit a2b008c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 20 deletions.
10 changes: 4 additions & 6 deletions isobus/include/isobus/isobus/can_network_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,12 @@ namespace isobus

/// @brief Use this to get a callback when a control function goes online or offline.
/// This could be useful if you want event driven notifications for when your partners are disconnected from the bus.
/// @param[in] controlFunction The control function you want callbacks for
/// @param[in] callback The callback you want to be called when the specified control function changes state
void add_control_function_status_change_callback(std::shared_ptr<ControlFunction> controlFunction, ControlFunctionStateCallback callback);
/// @param[in] callback The callback you want to be called when the any control function changes state
void add_control_function_status_change_callback(ControlFunctionStateCallback callback);

/// @brief Used to remove callbacks added with add_control_function_status_change_callback
/// @param[in] controlFunction The control function you want to stop receiving callbacks for
/// @param[in] callback The callback you want to remove
void remove_control_function_status_change_callback(std::shared_ptr<ControlFunction> controlFunction, ControlFunctionStateCallback callback);
void remove_control_function_status_change_callback(ControlFunctionStateCallback callback);

/// @brief Gets all the internal control functions that are currently registered in the network manager
/// @returns A list of all the internal control functions
Expand Down Expand Up @@ -345,7 +343,7 @@ namespace isobus

std::list<ParameterGroupNumberCallbackData> protocolPGNCallbacks; ///< A list of PGN callback registered by CAN protocols
std::list<CANMessage> receiveMessageList; ///< A queue of Rx messages to process
std::list<std::pair<std::shared_ptr<ControlFunction>, ControlFunctionStateCallback>> controlFunctionStateCallbacks; ///< List of all control function state callbacks
std::list<ControlFunctionStateCallback> controlFunctionStateCallbacks; ///< List of all control function state callbacks
std::vector<ParameterGroupNumberCallbackData> globalParameterGroupNumberCallbacks; ///< A list of all global PGN callbacks
std::vector<ParameterGroupNumberCallbackData> anyControlFunctionParameterGroupNumberCallbacks; ///< A list of all global PGN callbacks
std::mutex receiveMessageMutex; ///< A mutex for receive messages thread safety
Expand Down
19 changes: 7 additions & 12 deletions isobus/src/can_network_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,23 +331,21 @@ namespace isobus
}
}

void CANNetworkManager::add_control_function_status_change_callback(std::shared_ptr<ControlFunction> controlFunction, ControlFunctionStateCallback callback)
void CANNetworkManager::add_control_function_status_change_callback(ControlFunctionStateCallback callback)
{
if ((nullptr != controlFunction) &&
(nullptr != callback))
if (nullptr != callback)
{
const std::lock_guard<std::mutex> lock(controlFunctionStatusCallbacksMutex);
controlFunctionStateCallbacks.emplace_back(controlFunction, callback);
controlFunctionStateCallbacks.emplace_back(callback);
}
}

void CANNetworkManager::remove_control_function_status_change_callback(std::shared_ptr<ControlFunction> controlFunction, ControlFunctionStateCallback callback)
void CANNetworkManager::remove_control_function_status_change_callback(ControlFunctionStateCallback callback)
{
if ((nullptr != controlFunction) &&
(nullptr != callback))
if (nullptr != callback)
{
const std::lock_guard<std::mutex> lock(controlFunctionStatusCallbacksMutex);
std::pair<std::shared_ptr<ControlFunction>, ControlFunctionStateCallback> targetCallback(controlFunction, callback);
ControlFunctionStateCallback targetCallback(callback);
auto callbackLocation = std::find(controlFunctionStateCallbacks.begin(), controlFunctionStateCallbacks.end(), targetCallback);

if (controlFunctionStateCallbacks.end() != callbackLocation)
Expand Down Expand Up @@ -810,10 +808,7 @@ namespace isobus
const std::lock_guard<std::mutex> lock(controlFunctionStatusCallbacksMutex);
for (const auto &callback : controlFunctionStateCallbacks)
{
if (callback.first == controlFunction)
{
callback.second(callback.first, state);
}
callback(controlFunction, state);
}
}

Expand Down
4 changes: 2 additions & 2 deletions test/core_network_management_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ TEST(CORE_TESTS, InvalidatingControlFunctions)
std::vector<NAMEFilter> testFilter = { NAMEFilter(NAME::NAMEParameters::IdentityNumber, 967) };
auto testPartner = PartneredControlFunction::create(0, testFilter);

CANNetworkManager::CANNetwork.add_control_function_status_change_callback(testPartner, test_control_function_state_callback);
CANNetworkManager::CANNetwork.add_control_function_status_change_callback(test_control_function_state_callback);
EXPECT_FALSE(wasTestStateCallbackHit);
EXPECT_EQ(testControlFunction, nullptr);
EXPECT_EQ(testControlFunctionState, ControlFunctionState::Offline);
Expand Down Expand Up @@ -270,7 +270,7 @@ TEST(CORE_TESTS, InvalidatingControlFunctions)
EXPECT_TRUE(wasTestStateCallbackHit);
EXPECT_NE(testControlFunction, nullptr);
EXPECT_EQ(testControlFunctionState, ControlFunctionState::Offline);
CANNetworkManager::CANNetwork.remove_control_function_status_change_callback(testPartner, test_control_function_state_callback);
CANNetworkManager::CANNetwork.remove_control_function_status_change_callback(test_control_function_state_callback);
testControlFunction.reset();
EXPECT_TRUE(testPartner->destroy());
}
Expand Down

0 comments on commit a2b008c

Please sign in to comment.