Skip to content

Commit 66b3c81

Browse files
committed
Stop packet capture thread before changing network adapter
1 parent 8ce455a commit 66b3c81

File tree

6 files changed

+48
-32
lines changed

6 files changed

+48
-32
lines changed

asam_cmp_capture_module/include/asam_cmp_capture_module/capture_module_fb.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ class CaptureModuleFb final : public asam_cmp_common_lib::NetworkManagerFb
3434

3535
private:
3636
void createFbs();
37-
void networkAdapterChangedInternal() override;
3837
};
3938

40-
4139
END_NAMESPACE_ASAM_CMP_CAPTURE_MODULE

asam_cmp_capture_module/src/capture_module_fb.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,4 @@ void CaptureModuleFb::createFbs()
3232
functionBlocks.addItem(newFb);
3333
}
3434

35-
void CaptureModuleFb::networkAdapterChangedInternal()
36-
{
37-
38-
}
39-
4035
END_NAMESPACE_ASAM_CMP_CAPTURE_MODULE

asam_cmp_common_lib/include/asam_cmp_common_lib/network_manager_fb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class NetworkManagerFb : public FunctionBlock
3737
void addNetworkAdaptersProperty();
3838

3939
protected:
40-
virtual void networkAdapterChangedInternal() = 0;
40+
virtual void networkAdapterChangedInternal();
4141

4242
protected:
4343
std::shared_ptr<EthernetPcppItf> ethernetWrapper;

asam_cmp_common_lib/src/network_manager_fb.cpp

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#include <asam_cmp_common_lib/network_manager_fb.h>
21
#include <asam_cmp_common_lib/ethernet_pcpp_impl.h>
2+
#include <asam_cmp_common_lib/network_manager_fb.h>
33

44
BEGIN_NAMESPACE_ASAM_CMP_COMMON
55

@@ -45,26 +45,27 @@ void NetworkManagerFb::addNetworkAdaptersProperty()
4545
propName = "NetworkAdapters";
4646
prop = SelectionPropertyBuilder(propName, devicesDescriptions, 0).build();
4747
objPtr.addProperty(prop);
48-
objPtr.getOnPropertyValueWrite(propName) += [this, propName](PropertyObjectPtr& obj, PropertyValueEventArgsPtr& args)
49-
{
50-
StringPtr oldName = objPtr.getPropertySelectionValue("NetworkAdaptersNames");
51-
int oldInd = objPtr.getPropertyValue("NetworkAdaptersNames");
48+
objPtr.getOnPropertyValueWrite(propName) +=
49+
[this, propName](PropertyObjectPtr& obj, PropertyValueEventArgsPtr& args) { networkAdapterChangedInternal(); };
50+
}
5251

53-
setPropertyValueInternal(String("NetworkAdaptersNames"), args.getValue(), false, false, false);
54-
std::string newName = objPtr.getPropertySelectionValue("NetworkAdaptersNames");
52+
void NetworkManagerFb::networkAdapterChangedInternal()
53+
{
54+
int oldInd = objPtr.getPropertyValue("NetworkAdaptersNames");
55+
int newInd = objPtr.getPropertyValue("NetworkAdapters");
5556

56-
if (ethernetWrapper->setDevice(newName))
57-
{
58-
selectedEthernetDeviceName = newName;
59-
networkAdapterChangedInternal();
60-
}
61-
else
62-
{
63-
setPropertyValueInternal(String("NetworkAdaptersNames"), BaseObjectPtr(oldInd), false, false, false);
64-
setPropertyValueInternal(String("NetworkAdapters"), BaseObjectPtr(oldInd), false, false, false);
65-
}
66-
};
67-
}
57+
setPropertyValueInternal(String("NetworkAdaptersNames"), BaseObjectPtr(newInd), false, false, false);
58+
StringPtr newName = objPtr.getPropertySelectionValue("NetworkAdaptersNames");
6859

60+
if (ethernetWrapper->setDevice(newName))
61+
{
62+
selectedEthernetDeviceName = newName;
63+
}
64+
else
65+
{
66+
setPropertyValueInternal(String("NetworkAdaptersNames"), BaseObjectPtr(oldInd), false, false, false);
67+
setPropertyValueInternal(String("NetworkAdapters"), BaseObjectPtr(oldInd), false, false, false);
68+
}
69+
}
6970

7071
END_NAMESPACE_ASAM_CMP_COMMON

asam_cmp_data_sink/src/data_sink_module_fb.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ ErrCode INTERFACE_FUNC DataSinkModuleFb::remove()
4343

4444
void DataSinkModuleFb::networkAdapterChangedInternal()
4545
{
46+
stopCapture();
47+
NetworkManagerFb::networkAdapterChangedInternal();
4648
startCapture();
4749
}
4850

@@ -68,7 +70,6 @@ void DataSinkModuleFb::startCapture()
6870
{
6971
auto lock = this->getRecursiveConfigLock();
7072

71-
stopCapture();
7273
ethernetWrapper->startCapture([this](pcpp::RawPacket* packet, pcpp::PcapLiveDevice* dev, void* cookie)
7374
{ onPacketArrives(packet, dev, cookie); });
7475
captureStartedOnThisFb = true;

asam_cmp_data_sink/tests/test_data_sink_module_fb.cpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ class DataSinkModuleFbTest : public ::testing::Test
5959
template <typename T>
6060
void testProperty(const StringPtr& name, T newValue, bool success = true);
6161

62+
protected:
63+
static constexpr std::string_view networkAdapters = "NetworkAdapters";
64+
6265
protected:
6366
std::shared_ptr<asam_cmp_common_lib::EthernetPcppMock> ethernetWrapper;
6467
ListPtr<StringPtr> names;
@@ -98,11 +101,29 @@ void DataSinkModuleFbTest::testProperty(const StringPtr& name, T newValue, bool
98101

99102
TEST_F(DataSinkModuleFbTest, NetworkAdaptersProperties)
100103
{
101-
constexpr std::string_view networkAdapters = "NetworkAdapters";
102104
auto propList = funcBlock.getProperty(networkAdapters.data()).getSelectionValues().asPtrOrNull<IList>();
103-
int newVal = 0;
104-
if (propList.getCount() > 0)
105-
newVal = 1;
105+
ASSERT_EQ(propList.getCount(), descriptions.getCount());
106+
ASSERT_TRUE(std::equal(propList.begin(), propList.end(), descriptions.begin()));
107+
}
108+
109+
TEST_F(DataSinkModuleFbTest, ChangeNetworkAdapter)
110+
{
111+
auto propList = funcBlock.getProperty(networkAdapters.data()).getSelectionValues().asPtrOrNull<IList>();
112+
ASSERT_GT(propList.getCount(), 1);
113+
constexpr int newVal = 1;
114+
testProperty(networkAdapters.data(), newVal);
115+
}
116+
117+
TEST_F(DataSinkModuleFbTest, ChangeNetworkAdapterSequenceCall)
118+
{
119+
{
120+
InSequence inSeq;
121+
EXPECT_CALL(*ethernetWrapper, stopCapture()).Times(Exactly(1));
122+
EXPECT_CALL(*ethernetWrapper, setDevice(_)).Times(Exactly(1)).WillRepeatedly(Return(true));
123+
}
124+
125+
ASSERT_GT(descriptions.getCount(), 1);
126+
constexpr int newVal = 1;
106127
testProperty(networkAdapters.data(), newVal);
107128
}
108129

0 commit comments

Comments
 (0)