Skip to content

Commit

Permalink
Stop packet capture thread before changing network adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
sedovmo committed Nov 29, 2024
1 parent 8ce455a commit 66b3c81
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 0 additions & 5 deletions asam_cmp_capture_module/src/capture_module_fb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,4 @@ void CaptureModuleFb::createFbs()
functionBlocks.addItem(newFb);
}

void CaptureModuleFb::networkAdapterChangedInternal()
{

}

END_NAMESPACE_ASAM_CMP_CAPTURE_MODULE
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class NetworkManagerFb : public FunctionBlock
void addNetworkAdaptersProperty();

protected:
virtual void networkAdapterChangedInternal() = 0;
virtual void networkAdapterChangedInternal();

protected:
std::shared_ptr<EthernetPcppItf> ethernetWrapper;
Expand Down
39 changes: 20 additions & 19 deletions asam_cmp_common_lib/src/network_manager_fb.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <asam_cmp_common_lib/network_manager_fb.h>
#include <asam_cmp_common_lib/ethernet_pcpp_impl.h>
#include <asam_cmp_common_lib/network_manager_fb.h>

BEGIN_NAMESPACE_ASAM_CMP_COMMON

Expand Down Expand Up @@ -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
3 changes: 2 additions & 1 deletion asam_cmp_data_sink/src/data_sink_module_fb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ ErrCode INTERFACE_FUNC DataSinkModuleFb::remove()

void DataSinkModuleFb::networkAdapterChangedInternal()
{
stopCapture();
NetworkManagerFb::networkAdapterChangedInternal();
startCapture();
}

Expand All @@ -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;
Expand Down
29 changes: 25 additions & 4 deletions asam_cmp_data_sink/tests/test_data_sink_module_fb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class DataSinkModuleFbTest : public ::testing::Test
template <typename T>
void testProperty(const StringPtr& name, T newValue, bool success = true);

protected:
static constexpr std::string_view networkAdapters = "NetworkAdapters";

protected:
std::shared_ptr<asam_cmp_common_lib::EthernetPcppMock> ethernetWrapper;
ListPtr<StringPtr> names;
Expand Down Expand Up @@ -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<IList>();
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<IList>();
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);
}

Expand Down

0 comments on commit 66b3c81

Please sign in to comment.