Skip to content

Commit

Permalink
modify toString and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
wivien19 committed Aug 22, 2023
1 parent d85e738 commit 81d4434
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 48 deletions.
95 changes: 47 additions & 48 deletions Packet++/src/S7commLayer.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
#include "EndianPortable.h"
#include "TpktLayer.h"

#include <iostream>
#include "TcpLayer.h"
#include "S7commLayer.h"
#include "TcpLayer.h"
#include <iostream>

#include <cstring>
#include <sstream>

namespace pcpp {
namespace pcpp
{

S7commLayer::S7commLayer(uint8_t msg_type, uint16_t pdu_ref,
uint16_t param_length, uint16_t data_length) {
S7commLayer::S7commLayer(uint8_t msg_type, uint16_t pdu_ref, uint16_t param_length, uint16_t data_length)
{
const size_t headerLen = sizeof(s7commhdr);
m_DataLen = headerLen;
m_Data = new uint8_t[headerLen];
memset(m_Data, 0, headerLen);
s7commhdr *s7commHdr = (s7commhdr *) m_Data;
s7commhdr *s7commHdr = (s7commhdr *)m_Data;
s7commHdr->protocol_id = 0x32;
s7commHdr->msg_type = msg_type;
s7commHdr->reserved = 0x0000;
Expand All @@ -26,22 +27,30 @@ namespace pcpp {
m_Protocol = S7COMM;
}

std::string S7commLayer::toString() const {
std::ostringstream msgTypeStream;
msgTypeStream << getMsgType();
std::ostringstream pduRefStream;
pduRefStream << getPduRef();
std::ostringstream paramLengthStream;
paramLengthStream << getParamLength();
std::ostringstream dataLengthStream;
dataLengthStream << getDataLength();

return "S7comm Layer, msg_type: " + msgTypeStream.str() +
", pdu_ref: " + pduRefStream.str() +
", param_length: " + paramLengthStream.str() +
", data_length: " + dataLengthStream.str();

}
std::string S7commLayer::toString() const
{
// std::ostringstream msgTypeStream;
// msgTypeStream << getMsgType();
// std::ostringstream pduRefStream;
// pduRefStream << getPduRef();
// std::ostringstream paramLengthStream;
// paramLengthStream << getParamLength();
// std::ostringstream dataLengthStream;
// dataLengthStream << getDataLength();

// return "S7comm Layer, msg_type: " + msgTypeStream.str() +
// ", pdu_ref: " + pduRefStream.str() +
// ", param_length: " + paramLengthStream.str() +
// ", data_length: " + dataLengthStream.str();

std::ostringstream str;
str << "S7comm Layer, "
<< "msg_type: " << std::to_string(getMsgType())
<< ", pdu_ref: " << std::to_string(getPduRef())
<< ", param_length: " << std::to_string(getParamLength())
<< ", data_length: " << std::to_string(getDataLength());
return str.str();
}

bool S7commLayer::isDataValid(const uint8_t *data, size_t dataSize)
{
Expand All @@ -51,43 +60,33 @@ namespace pcpp {
return data[0] == 0x32;
}

uint8_t S7commLayer::getProtocolId() const {
return getS7commHeader()->protocol_id;
}
uint8_t S7commLayer::getProtocolId() const { return getS7commHeader()->protocol_id; }

uint8_t S7commLayer::getMsgType() const {
return getS7commHeader()->msg_type;
}
uint8_t S7commLayer::getMsgType() const { return getS7commHeader()->msg_type; }

uint16_t S7commLayer::getReserved() const {
return htobe16(getS7commHeader()->reserved);
}
uint16_t S7commLayer::getReserved() const { return htobe16(getS7commHeader()->reserved); }

uint16_t S7commLayer::getParamLength() const {
return htobe16(getS7commHeader()->param_length);
}
uint16_t S7commLayer::getParamLength() const { return htobe16(getS7commHeader()->param_length); }

uint16_t S7commLayer::getPduRef() const {
return htobe16(getS7commHeader()->pdu_ref);;
}
uint16_t S7commLayer::getPduRef() const
{
return htobe16(getS7commHeader()->pdu_ref);
;
}

uint16_t S7commLayer::getDataLength() const {
return htobe16(getS7commHeader()->data_length);
}
uint16_t S7commLayer::getDataLength() const { return htobe16(getS7commHeader()->data_length); }

void S7commLayer::setMsgType(uint8_t msg_type) const {
getS7commHeader()->msg_type = msg_type;
}
void S7commLayer::setMsgType(uint8_t msg_type) const { getS7commHeader()->msg_type = msg_type; }

void S7commLayer::setParamLength(uint16_t param_length) const {
void S7commLayer::setParamLength(uint16_t param_length) const
{
getS7commHeader()->param_length = htobe16(param_length);
}

void S7commLayer::setPduRef(uint16_t pdu_ref) const {
getS7commHeader()->pdu_ref = htobe16(pdu_ref);
}
void S7commLayer::setPduRef(uint16_t pdu_ref) const { getS7commHeader()->pdu_ref = htobe16(pdu_ref); }

void S7commLayer::setDataLength(uint16_t data_length) const {
void S7commLayer::setDataLength(uint16_t data_length) const
{
getS7commHeader()->data_length = htobe16(data_length);
}
} // namespace pcpp
1 change: 1 addition & 0 deletions Tests/Packet++Test/Tests/S7commTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ PTF_TEST_CASE(S7commLayerTest) {
PTF_ASSERT_EQUAL(s7commLayer->getPduRef(), 0xfd0b);
PTF_ASSERT_EQUAL(s7commLayer->getParamLength(), 12);
PTF_ASSERT_EQUAL(s7commLayer->getDataLength(), 212);
PTF_ASSERT_EQUAL(s7commLayer->toString(), "S7comm Layer, msg_type: 7, pdu_ref: 64779, param_length: 12, data_length: 212");

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

Expand Down

0 comments on commit 81d4434

Please sign in to comment.