diff --git a/asam_cmp_capture_module/include/asam_cmp_capture_module/capture_module_fb.h b/asam_cmp_capture_module/include/asam_cmp_capture_module/capture_module_fb.h index 5396ede..f964bdb 100644 --- a/asam_cmp_capture_module/include/asam_cmp_capture_module/capture_module_fb.h +++ b/asam_cmp_capture_module/include/asam_cmp_capture_module/capture_module_fb.h @@ -34,8 +34,6 @@ class CaptureModuleFb final : public asam_cmp_common_lib::NetworkManagerFb private: void createFbs(); - void networkAdapterChangedInternal() override; }; - END_NAMESPACE_ASAM_CMP_CAPTURE_MODULE diff --git a/asam_cmp_capture_module/src/capture_module_fb.cpp b/asam_cmp_capture_module/src/capture_module_fb.cpp index f938bd9..20505fe 100644 --- a/asam_cmp_capture_module/src/capture_module_fb.cpp +++ b/asam_cmp_capture_module/src/capture_module_fb.cpp @@ -32,9 +32,4 @@ void CaptureModuleFb::createFbs() functionBlocks.addItem(newFb); } -void CaptureModuleFb::networkAdapterChangedInternal() -{ - -} - END_NAMESPACE_ASAM_CMP_CAPTURE_MODULE diff --git a/asam_cmp_common_lib/include/asam_cmp_common_lib/network_manager_fb.h b/asam_cmp_common_lib/include/asam_cmp_common_lib/network_manager_fb.h index 33a8d2f..17528e4 100644 --- a/asam_cmp_common_lib/include/asam_cmp_common_lib/network_manager_fb.h +++ b/asam_cmp_common_lib/include/asam_cmp_common_lib/network_manager_fb.h @@ -37,7 +37,7 @@ class NetworkManagerFb : public FunctionBlock void addNetworkAdaptersProperty(); protected: - virtual void networkAdapterChangedInternal() = 0; + virtual void networkAdapterChangedInternal(); protected: std::shared_ptr ethernetWrapper; diff --git a/asam_cmp_common_lib/src/network_manager_fb.cpp b/asam_cmp_common_lib/src/network_manager_fb.cpp index 2fbfa8f..46a79c8 100644 --- a/asam_cmp_common_lib/src/network_manager_fb.cpp +++ b/asam_cmp_common_lib/src/network_manager_fb.cpp @@ -1,5 +1,5 @@ -#include #include +#include BEGIN_NAMESPACE_ASAM_CMP_COMMON @@ -45,26 +45,27 @@ void NetworkManagerFb::addNetworkAdaptersProperty() propName = "NetworkAdapters"; prop = SelectionPropertyBuilder(propName, devicesDescriptions, 0).build(); objPtr.addProperty(prop); - objPtr.getOnPropertyValueWrite(propName) += [this, propName](PropertyObjectPtr& obj, PropertyValueEventArgsPtr& args) - { - StringPtr oldName = objPtr.getPropertySelectionValue("NetworkAdaptersNames"); - int oldInd = objPtr.getPropertyValue("NetworkAdaptersNames"); + objPtr.getOnPropertyValueWrite(propName) += + [this, propName](PropertyObjectPtr& obj, PropertyValueEventArgsPtr& args) { networkAdapterChangedInternal(); }; +} - setPropertyValueInternal(String("NetworkAdaptersNames"), args.getValue(), false, false, false); - std::string newName = objPtr.getPropertySelectionValue("NetworkAdaptersNames"); +void NetworkManagerFb::networkAdapterChangedInternal() +{ + int oldInd = objPtr.getPropertyValue("NetworkAdaptersNames"); + int newInd = objPtr.getPropertyValue("NetworkAdapters"); - if (ethernetWrapper->setDevice(newName)) - { - selectedEthernetDeviceName = newName; - networkAdapterChangedInternal(); - } - else - { - setPropertyValueInternal(String("NetworkAdaptersNames"), BaseObjectPtr(oldInd), false, false, false); - setPropertyValueInternal(String("NetworkAdapters"), BaseObjectPtr(oldInd), false, false, false); - } - }; -} + setPropertyValueInternal(String("NetworkAdaptersNames"), BaseObjectPtr(newInd), false, false, false); + StringPtr newName = objPtr.getPropertySelectionValue("NetworkAdaptersNames"); + if (ethernetWrapper->setDevice(newName)) + { + selectedEthernetDeviceName = newName; + } + else + { + setPropertyValueInternal(String("NetworkAdaptersNames"), BaseObjectPtr(oldInd), false, false, false); + setPropertyValueInternal(String("NetworkAdapters"), BaseObjectPtr(oldInd), false, false, false); + } +} END_NAMESPACE_ASAM_CMP_COMMON diff --git a/asam_cmp_data_sink/src/data_sink_module_fb.cpp b/asam_cmp_data_sink/src/data_sink_module_fb.cpp index 4afaca5..fa55c70 100644 --- a/asam_cmp_data_sink/src/data_sink_module_fb.cpp +++ b/asam_cmp_data_sink/src/data_sink_module_fb.cpp @@ -43,6 +43,8 @@ ErrCode INTERFACE_FUNC DataSinkModuleFb::remove() void DataSinkModuleFb::networkAdapterChangedInternal() { + stopCapture(); + NetworkManagerFb::networkAdapterChangedInternal(); startCapture(); } @@ -68,7 +70,6 @@ void DataSinkModuleFb::startCapture() { auto lock = this->getRecursiveConfigLock(); - stopCapture(); ethernetWrapper->startCapture([this](pcpp::RawPacket* packet, pcpp::PcapLiveDevice* dev, void* cookie) { onPacketArrives(packet, dev, cookie); }); captureStartedOnThisFb = true; diff --git a/asam_cmp_data_sink/tests/test_data_sink_module_fb.cpp b/asam_cmp_data_sink/tests/test_data_sink_module_fb.cpp index 08d5087..0217a63 100644 --- a/asam_cmp_data_sink/tests/test_data_sink_module_fb.cpp +++ b/asam_cmp_data_sink/tests/test_data_sink_module_fb.cpp @@ -59,6 +59,9 @@ class DataSinkModuleFbTest : public ::testing::Test template void testProperty(const StringPtr& name, T newValue, bool success = true); +protected: + static constexpr std::string_view networkAdapters = "NetworkAdapters"; + protected: std::shared_ptr ethernetWrapper; ListPtr names; @@ -98,11 +101,29 @@ void DataSinkModuleFbTest::testProperty(const StringPtr& name, T newValue, bool TEST_F(DataSinkModuleFbTest, NetworkAdaptersProperties) { - constexpr std::string_view networkAdapters = "NetworkAdapters"; auto propList = funcBlock.getProperty(networkAdapters.data()).getSelectionValues().asPtrOrNull(); - int newVal = 0; - if (propList.getCount() > 0) - newVal = 1; + ASSERT_EQ(propList.getCount(), descriptions.getCount()); + ASSERT_TRUE(std::equal(propList.begin(), propList.end(), descriptions.begin())); +} + +TEST_F(DataSinkModuleFbTest, ChangeNetworkAdapter) +{ + auto propList = funcBlock.getProperty(networkAdapters.data()).getSelectionValues().asPtrOrNull(); + ASSERT_GT(propList.getCount(), 1); + constexpr int newVal = 1; + testProperty(networkAdapters.data(), newVal); +} + +TEST_F(DataSinkModuleFbTest, ChangeNetworkAdapterSequenceCall) +{ + { + InSequence inSeq; + EXPECT_CALL(*ethernetWrapper, stopCapture()).Times(Exactly(1)); + EXPECT_CALL(*ethernetWrapper, setDevice(_)).Times(Exactly(1)).WillRepeatedly(Return(true)); + } + + ASSERT_GT(descriptions.getCount(), 1); + constexpr int newVal = 1; testProperty(networkAdapters.data(), newVal); }