Skip to content

Commit

Permalink
Merge pull request #56 from RAMLABNL/8bit-support
Browse files Browse the repository at this point in the history
8bit support
  • Loading branch information
jadamroth authored Jun 25, 2021
2 parents a7e88cf + 9b6e59a commit a910b03
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 29 deletions.
6 changes: 4 additions & 2 deletions src/MessageRouter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ namespace eipScanner {
using eip::EncapsPacket;
using eip::EncapsPacketFactory;

MessageRouter::MessageRouter() = default;
MessageRouter::MessageRouter(bool use_8_bit_path_segments)
: _use_8_bit_path_segments(use_8_bit_path_segments)
{};

MessageRouter::~MessageRouter() = default;

Expand All @@ -41,7 +43,7 @@ namespace eipScanner {
Logger(LogLevel::INFO) << "Send request: service=0x" << std::hex << static_cast<int>(service)
<< " epath=" << path.toString();

MessageRouterRequest request{service, path, data};
MessageRouterRequest request{service, path, data, _use_8_bit_path_segments};

CommonPacketItemFactory commonPacketItemFactory;
CommonPacket commonPacket;
Expand Down
6 changes: 5 additions & 1 deletion src/MessageRouter.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ namespace eipScanner {
public:
using SPtr = std::shared_ptr<MessageRouter>;

static constexpr bool USE_8_BIT_PATH_SEGMENTS = true;

/**
* @brief Default constructor
*/
MessageRouter();
MessageRouter(bool use_8_bit_path_segments=false);

/**
* @brief Default destructor
Expand Down Expand Up @@ -72,6 +74,8 @@ namespace eipScanner {
virtual cip::MessageRouterResponse sendRequest(SessionInfoIf::SPtr si, cip::CipUsint service,
const cip::EPath& path) const;

private:
bool _use_8_bit_path_segments;
};
}

Expand Down
65 changes: 46 additions & 19 deletions src/cip/EPath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,45 @@ namespace cip {
, _size{3} {
}

std::vector<uint8_t> EPath::packPaddedPath() const {
Buffer buffer(_size*4);

auto classSegment = static_cast<CipUint>(EPathSegmentTypes::CLASS_16_BITS);
buffer << classSegment << _classId;

if (_size > 1) {
auto instanceSegment = static_cast<CipUint>(EPathSegmentTypes::INSTANCE_16_BITS);
buffer << instanceSegment << _objectId;

if (_size > 2) {
auto attributeSegment = static_cast<CipUint>(EPathSegmentTypes::ATTRIBUTE_16_BITS);
buffer << attributeSegment << _attributeId;
}
}

return buffer.data();
std::vector<uint8_t> EPath::packPaddedPath(bool use_8_bit_path_segments) const {
if (use_8_bit_path_segments)
{
Buffer buffer(_size*2);

auto classSegment = static_cast<CipUsint>(EPathSegmentTypes::CLASS_8_BITS);
buffer << classSegment << static_cast<CipUsint>(_classId);

if (_size > 1) {
auto instanceSegment = static_cast<CipUsint>(EPathSegmentTypes::INSTANCE_8_BITS);
buffer << instanceSegment << static_cast<CipUsint>(_objectId);

if (_size > 2) {
auto attributeSegment = static_cast<CipUsint>(EPathSegmentTypes::ATTRIBUTE_8_BITS);
buffer << attributeSegment << static_cast<CipUsint>(_attributeId);
}
}

return buffer.data();
}
else
{
Buffer buffer(_size*4);

auto classSegment = static_cast<CipUint>(EPathSegmentTypes::CLASS_16_BITS);
buffer << classSegment << _classId;

if (_size > 1) {
auto instanceSegment = static_cast<CipUint>(EPathSegmentTypes::INSTANCE_16_BITS);
buffer << instanceSegment << _objectId;

if (_size > 2) {
auto attributeSegment = static_cast<CipUint>(EPathSegmentTypes::ATTRIBUTE_16_BITS);
buffer << attributeSegment << _attributeId;
}
}

return buffer.data();
}
}

CipUint EPath::getClassId() const {
Expand All @@ -80,8 +102,13 @@ namespace cip {
return _attributeId;
}

CipUsint EPath::getSizeInWords() const {
return _size*2;
CipUsint EPath::getSizeInWords(bool use_8_bit_path_segments) const {
if (use_8_bit_path_segments) {
return _size;
}
else {
return _size*2;
}
}

std::string EPath::toString() const {
Expand Down
4 changes: 2 additions & 2 deletions src/cip/EPath.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ namespace cip {
explicit EPath(CipUint classId);
EPath(CipUint classId, CipUint objectId);
EPath(CipUint classId, CipUint objectId, CipUint attributeId);
std::vector<uint8_t> packPaddedPath() const;
std::vector<uint8_t> packPaddedPath(bool use_8_bit_path_segments=false) const;
void expandPaddedPath(const std::vector<uint8_t>& data);

CipUint getClassId() const;
CipUint getObjectId() const;
CipUint getAttributeId() const;
CipUsint getSizeInWords() const;
CipUsint getSizeInWords(bool use_8_bit_path_segments=false) const;

std::string toString() const;
bool operator==(const EPath& other) const;
Expand Down
9 changes: 5 additions & 4 deletions src/cip/MessageRouterRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@ namespace cip {
using utils::Buffer;

MessageRouterRequest::MessageRouterRequest(CipUsint serviceCode,
const EPath& ePath, const std::vector<uint8_t> data)
const EPath& ePath, const std::vector<uint8_t> data, bool use_8_bit_path_segments)
: _serviceCode{serviceCode}
, _ePath{ePath}
, _data(data) {
, _data(data)
, _use_8_bit_path_segments(use_8_bit_path_segments) {
}

MessageRouterRequest::~MessageRouterRequest() = default;

std::vector<uint8_t> MessageRouterRequest::pack() const {
Buffer buffer;
buffer << _serviceCode
<< _ePath.getSizeInWords()
<< _ePath.packPaddedPath()
<< _ePath.getSizeInWords(_use_8_bit_path_segments)
<< _ePath.packPaddedPath(_use_8_bit_path_segments)
<< _data;

return buffer.data();
Expand Down
3 changes: 2 additions & 1 deletion src/cip/MessageRouterRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ namespace eipScanner {
namespace cip {
class MessageRouterRequest {
public:
MessageRouterRequest(CipUsint serviceCode, const EPath& ePath, const std::vector<uint8_t> data);
MessageRouterRequest(CipUsint serviceCode, const EPath& ePath, const std::vector<uint8_t> data, bool use_8_bit_path_segments=false);
~MessageRouterRequest();

std::vector<uint8_t> pack() const;
private:
CipUsint _serviceCode;
EPath _ePath;
std::vector<uint8_t> _data;
bool _use_8_bit_path_segments;
};

}
Expand Down

0 comments on commit a910b03

Please sign in to comment.