Skip to content

Commit

Permalink
move the method to layers
Browse files Browse the repository at this point in the history
  • Loading branch information
tigercosmos committed Oct 4, 2024
1 parent fede80b commit 6038329
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 157 deletions.
116 changes: 58 additions & 58 deletions Packet++/header/GvcpLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,44 +125,10 @@ namespace pcpp
requestId(hostToNet16(requestId))
{}

GvcpFlag getFlag() const
{
return flag;
}

GvcpCommand getCommand() const
{
return static_cast<GvcpCommand>(netToHost16(command));
}

uint16_t getDataSize() const
{
return netToHost16(dataSize);
}

uint16_t getRequestId() const
{
return netToHost16(requestId);
}

/**
* @brief Verify the magic number
* @return true The magic number is valid
*/
bool verifyMagicNumber() const
{
return magicNumber == internal::kGvcpMagicNumber;
}

/**
* @brief Check if the acknowledge is required
* @return true The acknowledge is required
*/
bool hasAcknowledgeFlag() const
{
constexpr GvcpFlag kAcknowledgeFlag = 0b0000001;
return (flag & kAcknowledgeFlag) == kAcknowledgeFlag;
}
};
static_assert(sizeof(GvcpRequestHeader) == internal::kGvcpRequestHeaderLength,
"Gvcp request header size should be 8 bytes");
Expand Down Expand Up @@ -206,25 +172,10 @@ namespace pcpp
dataSize(hostToNet16(dataSize)), ackId(hostToNet16(ackId))
{}

GvcpResponseStatus getStatus() const
{
return static_cast<GvcpResponseStatus>(netToHost16(status));
}

GvcpCommand getCommand() const
{
return static_cast<GvcpCommand>(netToHost16(command));
}

uint16_t getDataSize() const
{
return netToHost16(dataSize);
}

uint16_t getAckId() const
{
return netToHost16(ackId);
}
};
static_assert(sizeof(GvcpAckHeader) == internal::kGvcpAckHeaderLength, "Gvcp ack header size should be 8 bytes");

Expand Down Expand Up @@ -378,10 +329,56 @@ namespace pcpp
return reinterpret_cast<GvcpRequestHeader*>(m_Data); // the header is at the beginning of the data
}

/// @brief get the GVCP command
/**
* @brief Get the flag from the header
*/
GvcpFlag getFlag() const
{
return getGvcpHeader()->flag;
}

/**
* @brief Get the data size from the header
*/
uint16_t getDataSize() const
{
return netToHost16(getGvcpHeader()->dataSize);
}

/**
* @brief Get the request ID from the header
*/
uint16_t getRequestId() const
{
return netToHost16(getGvcpHeader()->requestId);
}


/**
* @brief Get the command from the header
*/
GvcpCommand getCommand() const
{
return getGvcpHeader()->getCommand();
return static_cast<GvcpCommand>(netToHost16(getGvcpHeader()->command));
}

/**
* @brief Verify the magic number in the header
* @return true The magic number is valid
*/
bool verifyMagicNumber() const
{
return getGvcpHeader()->magicNumber == internal::kGvcpMagicNumber;
}

/**
* @brief Check if the acknowledge is required from the header
* @return true The acknowledge is required
*/
bool hasAcknowledgeFlag() const
{
constexpr GvcpFlag kAcknowledgeFlag = 0b0000001;
return (getGvcpHeader()->flag & kAcknowledgeFlag) == kAcknowledgeFlag;
}

// implement Layer's abstract methods
Expand Down Expand Up @@ -435,33 +432,36 @@ namespace pcpp
return reinterpret_cast<GvcpAckHeader*>(m_Data); // the header is at the beginning of the data
}

/**
* @return the response status from the header
*/
GvcpResponseStatus getStatus() const
{
return getGvcpHeader()->getStatus();
return static_cast<GvcpResponseStatus>((netToHost16(getGvcpHeader()->status)));
}

/**
* @return the response command type
* @return the response command type from the header
*/
GvcpCommand getCommand() const
{
return getGvcpHeader()->getCommand();
return static_cast<GvcpCommand>(netToHost16(getGvcpHeader()->command));
}

/**
* @return the size of the data in bytes
* @return the size of the data in bytes from the header
*/
uint16_t getDataSize() const
{
return getGvcpHeader()->getDataSize();
return netToHost16(getGvcpHeader()->dataSize);
}

/**
* @return uint16_t The acknowledge ID
* @return uint16_t The acknowledge ID from the header
*/
uint16_t getAckId() const
{
return getGvcpHeader()->getAckId();
return netToHost16(getGvcpHeader()->ackId);
}

// implement Layer's abstract methods
Expand Down
2 changes: 1 addition & 1 deletion Packet++/header/ProtocolType.h
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ namespace pcpp
/*
* GVCP protocol
*/
const ProtocolType Gvcp = 57;
const ProtocolType GVCP = 57;

/**
* An enum representing OSI model layers
Expand Down
16 changes: 8 additions & 8 deletions Packet++/src/GvcpLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace pcpp
/*---------------------- Class GvcpLayer ----------------------------*/

GvcpLayer::GvcpLayer(uint8_t* data, size_t dataSize, Layer* prevLayer, Packet* packet)
: Layer(data, dataSize, prevLayer, packet, Gvcp)
: Layer(data, dataSize, prevLayer, packet, GVCP)
{}

GvcpLayer* GvcpLayer::parseGvcpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
Expand Down Expand Up @@ -74,7 +74,7 @@ namespace pcpp
GvcpRequestLayer::GvcpRequestLayer(GvcpCommand command, const uint8_t* payloadData, uint16_t payloadDataSize,
GvcpFlag flag, uint16_t requestId)
{
m_Protocol = Gvcp;
m_Protocol = GVCP;

m_DataLen = getHeaderLen() + payloadDataSize;
m_Data = new uint8_t[m_DataLen];
Expand All @@ -92,7 +92,7 @@ namespace pcpp

GvcpRequestLayer::GvcpRequestLayer(const uint8_t* data, size_t dataSize)
{
m_Protocol = Gvcp;
m_Protocol = GVCP;

m_DataLen = dataSize;
m_Data = new uint8_t[m_DataLen];
Expand All @@ -102,7 +102,7 @@ namespace pcpp
std::string GvcpRequestLayer::toString() const
{
std::stringstream ss;
ss << "GVCP Request Layer, Command: " << getCommand() << ", Request ID: " << getGvcpHeader()->getRequestId();
ss << "GVCP Request Layer, Command: " << getCommand() << ", Request ID: " << getRequestId();
return ss.str();
}

Expand All @@ -114,7 +114,7 @@ namespace pcpp
GvcpAcknowledgeLayer::GvcpAcknowledgeLayer(GvcpResponseStatus status, GvcpCommand command,
const uint8_t* payloadData, uint16_t payloadDataSize, uint16_t ackId)
{
m_Protocol = Gvcp;
m_Protocol = GVCP;

m_DataLen = getHeaderLen() + payloadDataSize;
m_Data = new uint8_t[m_DataLen];
Expand All @@ -132,7 +132,7 @@ namespace pcpp

GvcpAcknowledgeLayer::GvcpAcknowledgeLayer(const uint8_t* data, size_t dataSize)
{
m_Protocol = Gvcp;
m_Protocol = GVCP;

m_DataLen = dataSize;
m_Data = new uint8_t[m_DataLen];
Expand All @@ -142,8 +142,8 @@ namespace pcpp
std::string GvcpAcknowledgeLayer::toString() const
{
std::stringstream ss;
ss << "GVCP Acknowledge Layer, Command: " << getCommand() << ", Acknowledge ID: " << getGvcpHeader()->getAckId()
<< ", Status: " << getGvcpHeader()->getStatus();
ss << "GVCP Acknowledge Layer, Command: " << getCommand() << ", Acknowledge ID: " << getAckId()
<< ", Status: " << getStatus();
return ss.str();
}
} // namespace pcpp
Loading

0 comments on commit 6038329

Please sign in to comment.