Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: attach 8-bit path segments to session info #107

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions docs/source/misc/8-bit_path_segments.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,30 @@
===================

Some devices only support 8-bit path segments. In order to set up
**EIPScanner** to use 8-bit path segments, a *MessageRouter* with the
**USE_8_BIT_PATH_SEGMENTS** flag set should be passed to the *ConnectionManager*
upon construction:
**EIPScanner** to use 8-bit path segments, specify the **use_8_bit_path_segments**
parameter when creating the *SessionInfo* object for the adapter.

.. code-block:: cpp

#include "MessageRouter.h"
#include <cip/connectionManager/NetworkConnectionParams.h>
#include "ConnectionManager.h"
#include "MessageRouter.h"
#include "SessionInfo.h"

using eipScanner::cip::connectionManager::ConnectionParameters;
using eipScanner::ConnectionManager;
using eipScanner::MessageRouter;
using eipScanner::SessionInfo;

int main()
{
MessageRouter::SPtr mr_ptr = std::make_shared<MessageRouter>(MessageRouter::USE_8_BIT_PATH_SEGMENTS);
ConnectionManager _connectionManager = ConnectionManager(mr_ptr);

/* ConnectionManager now uses 8-bit path segments */
auto si = std::make_shared<SessionInfo>("172.28.1.3", 0xAF12, true);

/* The connection now uses 8-bit path segments for the forward open*/
ConnectionManager connectionManager;
ConnectionParameters parameters;
auto io = connectionManager.forwardOpen(si, parameters);
connectionManager.forwardClose(si, io);

return 0;
}
Expand Down
6 changes: 2 additions & 4 deletions src/MessageRouter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ namespace eipScanner {
using eip::EncapsPacket;
using eip::EncapsPacketFactory;

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

MessageRouter::~MessageRouter() = default;

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

MessageRouterRequest request{service, path, data, _use_8_bit_path_segments};
MessageRouterRequest request{service, path, data, si->getUse8BitPathSegments()};

CommonPacketItemFactory commonPacketItemFactory;
CommonPacket commonPacket;
Expand Down
8 changes: 1 addition & 7 deletions src/MessageRouter.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,10 @@ namespace eipScanner {
class MessageRouter {
public:
using SPtr = std::shared_ptr<MessageRouter>;

static constexpr bool USE_8_BIT_PATH_SEGMENTS = true;

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

/**
* @brief Default destructor
Expand Down Expand Up @@ -73,9 +70,6 @@ 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
19 changes: 16 additions & 3 deletions src/SessionInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,18 @@ namespace eipScanner {
using eip::EncapsPacketFactory;
using eip::EncapsStatusCodes;

SessionInfo::SessionInfo(const std::string &host, int port, const std::chrono::milliseconds &timeout, bool use_8_bit_path_segments) : SessionInfo(host, port, timeout) {
_use_8_bit_path_segments = use_8_bit_path_segments;
}

SessionInfo::SessionInfo(const std::string &host, int port, bool use_8_bit_path_segments) : SessionInfo(host, port, std::chrono::milliseconds(1000)) {
_use_8_bit_path_segments = use_8_bit_path_segments;
}

SessionInfo::SessionInfo(const std::string &host, int port, const std::chrono::milliseconds &timeout)
: _socket{sockets::EndPoint(host, port), timeout}
, _sessionHandle{0} {
, _sessionHandle{0}
, _use_8_bit_path_segments{false} {
_socket.setRecvTimeout(timeout);

EncapsPacket packet = EncapsPacketFactory().createRegisterSessionPacket();
Expand All @@ -35,7 +44,7 @@ namespace eipScanner {
}

SessionInfo::SessionInfo(const std::string &host, int port)
: SessionInfo(host, port, std::chrono::milliseconds(1000)) {
: SessionInfo(host, port, std::chrono::milliseconds(1000), false) {
}

SessionInfo::~SessionInfo() {
Expand Down Expand Up @@ -76,4 +85,8 @@ namespace eipScanner {
return _socket.getRemoteEndPoint();
}

}
bool SessionInfo::getUse8BitPathSegments() const {
return _use_8_bit_path_segments;
}

}
31 changes: 29 additions & 2 deletions src/SessionInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,28 @@ namespace eipScanner {
* @brief Establishes an EIP session with an EIP adapter
* @param host The IP address of the adapter
* @param port The port of the adapter
* @param timeout timout to connect and receive the response
* @param timeout timeout to connect and receive the response
* @param use_8_bit_path_segments use 8-bit instead of 16-bit path segments
* @throw std::runtime_error
* @throw std::system_error
*/
SessionInfo(const std::string &host, int port, const std::chrono::milliseconds& timeout, bool use_8_bit_path_segments);

/**
* @brief Establishes an EIP session with an EIP adapter
* @param host The IP address of the adapter
* @param port The port of the adapter
* @param use_8_bit_path_segments use 8-bit instead of 16-bit path segments
* @throw std::runtime_error
* @throw std::system_error
*/
SessionInfo(const std::string &host, int port, bool use_8_bit_path_segments);

/**
* @brief Establishes an EIP session with an EIP adapter
* @param host The IP address of the adapter
* @param port The port of the adapter
* @param timeout timeout to connect and receive the response
* @throw std::runtime_error
* @throw std::system_error
*/
Expand Down Expand Up @@ -66,10 +87,16 @@ namespace eipScanner {
*/
sockets::EndPoint getRemoteEndPoint() const override;

/**
* Gets whether this connection should use 8-bit path segments.
* @return
*/
bool getUse8BitPathSegments() const override;

private:
sockets::TCPSocket _socket;
cip::CipUdint _sessionHandle;

bool _use_8_bit_path_segments;
};
}

Expand Down
6 changes: 6 additions & 0 deletions src/SessionInfoIf.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ namespace eipScanner {
* @return
*/
virtual sockets::EndPoint getRemoteEndPoint() const = 0;

/**
* Gets whether this connection should use 8-bit path segments.
* @return
*/
virtual bool getUse8BitPathSegments() const = 0;
};
}
#endif //EIPSCANNER_SESSIONINFOIF_H