diff --git a/src/MessageRouter.cpp b/src/MessageRouter.cpp index 8603343..16f2232 100644 --- a/src/MessageRouter.cpp +++ b/src/MessageRouter.cpp @@ -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; @@ -41,7 +43,7 @@ namespace eipScanner { Logger(LogLevel::INFO) << "Send request: service=0x" << std::hex << static_cast(service) << " epath=" << path.toString(); - MessageRouterRequest request{service, path, data}; + MessageRouterRequest request{service, path, data, _use_8_bit_path_segments}; CommonPacketItemFactory commonPacketItemFactory; CommonPacket commonPacket; diff --git a/src/MessageRouter.h b/src/MessageRouter.h index 8036c9c..c4a0bda 100644 --- a/src/MessageRouter.h +++ b/src/MessageRouter.h @@ -22,10 +22,12 @@ namespace eipScanner { public: using SPtr = std::shared_ptr; + static constexpr bool USE_8_BIT_PATH_SEGMENTS = true; + /** * @brief Default constructor */ - MessageRouter(); + MessageRouter(bool use_8_bit_path_segments=false); /** * @brief Default destructor @@ -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; }; } diff --git a/src/cip/EPath.cpp b/src/cip/EPath.cpp index 626e288..447ef30 100644 --- a/src/cip/EPath.cpp +++ b/src/cip/EPath.cpp @@ -49,23 +49,45 @@ namespace cip { , _size{3} { } - std::vector EPath::packPaddedPath() const { - Buffer buffer(_size*4); - - auto classSegment = static_cast(EPathSegmentTypes::CLASS_16_BITS); - buffer << classSegment << _classId; - - if (_size > 1) { - auto instanceSegment = static_cast(EPathSegmentTypes::INSTANCE_16_BITS); - buffer << instanceSegment << _objectId; - - if (_size > 2) { - auto attributeSegment = static_cast(EPathSegmentTypes::ATTRIBUTE_16_BITS); - buffer << attributeSegment << _attributeId; - } - } - - return buffer.data(); + std::vector EPath::packPaddedPath(bool use_8_bit_path_segments) const { + if (use_8_bit_path_segments) + { + Buffer buffer(_size*2); + + auto classSegment = static_cast(EPathSegmentTypes::CLASS_8_BITS); + buffer << classSegment << static_cast(_classId); + + if (_size > 1) { + auto instanceSegment = static_cast(EPathSegmentTypes::INSTANCE_8_BITS); + buffer << instanceSegment << static_cast(_objectId); + + if (_size > 2) { + auto attributeSegment = static_cast(EPathSegmentTypes::ATTRIBUTE_8_BITS); + buffer << attributeSegment << static_cast(_attributeId); + } + } + + return buffer.data(); + } + else + { + Buffer buffer(_size*4); + + auto classSegment = static_cast(EPathSegmentTypes::CLASS_16_BITS); + buffer << classSegment << _classId; + + if (_size > 1) { + auto instanceSegment = static_cast(EPathSegmentTypes::INSTANCE_16_BITS); + buffer << instanceSegment << _objectId; + + if (_size > 2) { + auto attributeSegment = static_cast(EPathSegmentTypes::ATTRIBUTE_16_BITS); + buffer << attributeSegment << _attributeId; + } + } + + return buffer.data(); + } } CipUint EPath::getClassId() const { @@ -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 { diff --git a/src/cip/EPath.h b/src/cip/EPath.h index 67cccd2..d2270b6 100644 --- a/src/cip/EPath.h +++ b/src/cip/EPath.h @@ -19,13 +19,13 @@ namespace cip { explicit EPath(CipUint classId); EPath(CipUint classId, CipUint objectId); EPath(CipUint classId, CipUint objectId, CipUint attributeId); - std::vector packPaddedPath() const; + std::vector packPaddedPath(bool use_8_bit_path_segments=false) const; void expandPaddedPath(const std::vector& 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; diff --git a/src/cip/MessageRouterRequest.cpp b/src/cip/MessageRouterRequest.cpp index 46b2c9c..43e5ab7 100644 --- a/src/cip/MessageRouterRequest.cpp +++ b/src/cip/MessageRouterRequest.cpp @@ -11,10 +11,11 @@ namespace cip { using utils::Buffer; MessageRouterRequest::MessageRouterRequest(CipUsint serviceCode, - const EPath& ePath, const std::vector data) + const EPath& ePath, const std::vector 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; @@ -22,8 +23,8 @@ namespace cip { std::vector 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(); diff --git a/src/cip/MessageRouterRequest.h b/src/cip/MessageRouterRequest.h index 4e2f486..09777b7 100644 --- a/src/cip/MessageRouterRequest.h +++ b/src/cip/MessageRouterRequest.h @@ -15,7 +15,7 @@ namespace eipScanner { namespace cip { class MessageRouterRequest { public: - MessageRouterRequest(CipUsint serviceCode, const EPath& ePath, const std::vector data); + MessageRouterRequest(CipUsint serviceCode, const EPath& ePath, const std::vector data, bool use_8_bit_path_segments=false); ~MessageRouterRequest(); std::vector pack() const; @@ -23,6 +23,7 @@ namespace cip { CipUsint _serviceCode; EPath _ePath; std::vector _data; + bool _use_8_bit_path_segments; }; }