Skip to content

Commit

Permalink
Fix getParameter()
Browse files Browse the repository at this point in the history
  • Loading branch information
seladb committed Oct 3, 2023
1 parent 9ffc140 commit 6502dcb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
7 changes: 5 additions & 2 deletions Packet++/header/S7commLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ namespace pcpp

public:
S7CommParameter() {}
uint8_t *getData() { return m_Data; }
uint8_t *getData() const { return m_Data; }
size_t getDataLength() const { return m_DataLen; }

private:
Expand Down Expand Up @@ -126,7 +126,7 @@ namespace pcpp
*/
uint8_t getErrorClass() const;

S7CommParameter *getParameter() const;
const S7CommParameter *getParameter();

/**
* Set the value of the message type
Expand Down Expand Up @@ -207,6 +207,9 @@ namespace pcpp
}
return nullptr;
}

size_t getS7commHeaderLength() const;

S7CommParameter* m_Parameter;
};

Expand Down
22 changes: 15 additions & 7 deletions Packet++/src/S7commLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,24 @@ namespace pcpp

void S7commLayer::setErrorClass(uint8_t error_class) const { getS7commAckDataHeader()->error_class = error_class; }

S7CommParameter *S7commLayer::getParameter() const
const S7CommParameter *S7commLayer::getParameter()
{
S7CommParameter *m_Parameter = nullptr;

uint8_t *payload = m_Data + getHeaderLen() - getParamLength() -getDataLen();//- getHeaderLen() + parameterLen; //;+ parameterLen;
size_t payloadLen = m_DataLen - getHeaderLen() + getParamLength() + getDataLength();

m_Parameter = new S7CommParameter(payload, payloadLen);
if (!m_Parameter)
{
uint8_t *payload = m_Data + getS7commHeaderLength();
m_Parameter = new S7CommParameter(payload, getParamLength());
}

return m_Parameter;
}

size_t S7commLayer::getS7commHeaderLength() const
{
if (getS7commHeader()->msg_type == 0x03)
{
return sizeof(s7comm_ack_data_hdr);
}
return sizeof(s7commhdr);
}

} // namespace pcpp
7 changes: 4 additions & 3 deletions Tests/Packet++Test/Tests/S7commTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "Packet.h"
#include "S7commLayer.h"
#include "SystemUtils.h"
#include <stdio.h>

PTF_TEST_CASE(S7commLayerTest)
{
Expand All @@ -26,8 +25,10 @@ PTF_TEST_CASE(S7commLayerTest)
PTF_ASSERT_EQUAL(s7commLayer->getHeaderLen(), 0xea);
PTF_ASSERT_EQUAL(s7commLayer->toString(),
"S7comm Layer, msg_type: 7, pdu_ref: 64779, param_length: 12, data_length: 212");
std::cout << (s7commLayer->getParameter()->getDataLength()) << std::endl;
std::cout << (s7commLayer->getParameter()->getData()) << std::endl;

PTF_ASSERT_EQUAL(s7commLayer->getParameter()->getDataLength(), 12);
uint8_t expectedParameterData[] = { 0x00, 0x01, 0x12, 0x08, 0x12, 0x84, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 };
PTF_ASSERT_BUF_COMPARE(s7commLayer->getParameter()->getData(), expectedParameterData, 12);

pcpp::S7commLayer newS7commPacket(0x09, 0xfd0c, 13, 213);

Expand Down

0 comments on commit 6502dcb

Please sign in to comment.