Skip to content

Commit

Permalink
EIPScanner-9: pass additional CommonPacketItems to message router
Browse files Browse the repository at this point in the history
  • Loading branch information
atimin committed Dec 10, 2019
1 parent 2ae2844 commit ab2042f
Show file tree
Hide file tree
Showing 36 changed files with 234 additions and 74 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
**Features**:

* add Identity Object #5, @flipback
* add utils::LogAppender to customise logging #5, @flipback
* add utils::LogAppenderIf to customise logging #5, @flipback

# Release 1.0.0 (2019-12-08)

Expand Down
8 changes: 4 additions & 4 deletions src/ConnectionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace eipScanner {
ConnectionManager::~ConnectionManager() = default;

IOConnection::WPtr
ConnectionManager::forwardOpen(SessionInfo::SPtr si, ConnectionParameters connectionParameters) {
ConnectionManager::forwardOpen(SessionInfoIf::SPtr si, ConnectionParameters connectionParameters) {
static int serialNumberCount = 0;
connectionParameters.connectionSerialNumber = ++serialNumberCount;

Expand Down Expand Up @@ -111,7 +111,7 @@ namespace eipScanner {
return ioConnection;
}

void ConnectionManager::forwardClose(SessionInfo::SPtr si, const IOConnection::WPtr& ioConnection) {
void ConnectionManager::forwardClose(SessionInfoIf::SPtr si, const IOConnection::WPtr& ioConnection) {
if (auto ptr = ioConnection.lock()) {
ForwardCloseRequest request;

Expand Down Expand Up @@ -189,14 +189,14 @@ namespace eipScanner {
commonPacket.expand(recvData);

// TODO: Check TypeIDs and sequnce of the packages
Buffer buffer(commonPacket[0].getData());
Buffer buffer(commonPacket.getItems().at(0).getData());
cip::CipUdint connectionId;
buffer >> connectionId;
Logger(LogLevel::DEBUG) << "Received data from connection T2O_ID=" << connectionId;

auto io = _connectionMap.find(connectionId);
if (io != _connectionMap.end()) {
io->second->notifyReceiveData(commonPacket[1].getData());
io->second->notifyReceiveData(commonPacket.getItems().at(1).getData());
} else {
Logger(LogLevel::ERROR) << "Received data from unknow connection T2O_ID=" << connectionId;
}
Expand Down
4 changes: 2 additions & 2 deletions src/ConnectionManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ namespace eipScanner {
explicit ConnectionManager(MessageRouter::SPtr messageRouter);
~ConnectionManager();

IOConnection::WPtr forwardOpen(SessionInfo::SPtr si, cip::connectionManager::ConnectionParameters connectionParameters);
void forwardClose(SessionInfo::SPtr si, const IOConnection::WPtr& ioConnection);
IOConnection::WPtr forwardOpen(SessionInfoIf::SPtr si, cip::connectionManager::ConnectionParameters connectionParameters);
void forwardClose(SessionInfoIf::SPtr si, const IOConnection::WPtr& ioConnection);

/**
* Handles active connections
Expand Down
6 changes: 3 additions & 3 deletions src/FileObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace eipScanner {



FileObject::FileObject(cip::CipUint instanceId, SessionInfo::SPtr si, MessageRouter::SPtr messageRouter)
FileObject::FileObject(cip::CipUint instanceId, SessionInfoIf::SPtr si, MessageRouter::SPtr messageRouter)
: BaseObject(fileObject::FILE_OBJECT_CLASS_ID, instanceId)
, _state(new fileObject::FileObjectState(FileObjectStateCodes::UNKNOWN, *this, instanceId, messageRouter)) {
_state->SyncState(si);
Expand All @@ -27,11 +27,11 @@ namespace eipScanner {
return _state;
}

void FileObject::beginUpload(SessionInfo::SPtr si, fileObject::EndUploadHandler handle) {
void FileObject::beginUpload(SessionInfoIf::SPtr si, fileObject::EndUploadHandler handle) {
_state->initiateUpload(si, handle);
}

bool FileObject::handleTransfers(SessionInfo::SPtr si) {
bool FileObject::handleTransfers(SessionInfoIf::SPtr si) {
return _state->transfer(si);
}
}
6 changes: 3 additions & 3 deletions src/FileObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ namespace eipScanner {
public:
using UPtr = std::unique_ptr<FileObject>;

FileObject(cip::CipUint instanceId, SessionInfo::SPtr si, MessageRouter::SPtr messageRouter);
FileObject(cip::CipUint instanceId, SessionInfoIf::SPtr si, MessageRouter::SPtr messageRouter);
~FileObject();
std::unique_ptr<fileObject::FileObjectState>& getState();
void beginUpload(SessionInfo::SPtr si, fileObject::EndUploadHandler handle);
void beginUpload(SessionInfoIf::SPtr si, fileObject::EndUploadHandler handle);

/**
* handle active download/upload transfers
* @return true if downloading/uploading is in progress
*/
bool handleTransfers(SessionInfo::SPtr si);
bool handleTransfers(SessionInfoIf::SPtr si);


private:
Expand Down
4 changes: 2 additions & 2 deletions src/IdentityObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ namespace eipScanner {
, _productName("") {
}

IdentityObject::IdentityObject(cip::CipUint instanceId, const SessionInfo::SPtr &si)
IdentityObject::IdentityObject(cip::CipUint instanceId, const SessionInfoIf::SPtr &si)
: IdentityObject(instanceId, si, std::make_shared<MessageRouter>()){
}

IdentityObject::IdentityObject(cip::CipUint instanceId, const SessionInfo::SPtr &si, const MessageRouter::SPtr &messageRouter)
IdentityObject::IdentityObject(cip::CipUint instanceId, const SessionInfoIf::SPtr &si, const MessageRouter::SPtr &messageRouter)
: BaseObject(CLASS_ID, instanceId) {

auto response = messageRouter->sendRequest(
Expand Down
4 changes: 2 additions & 2 deletions src/IdentityObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ namespace eipScanner {
public:
static const cip::CipUint CLASS_ID = 0x01;
IdentityObject(cip::CipUint instanceId);
IdentityObject(cip::CipUint instanceId, const SessionInfo::SPtr& si);
IdentityObject(cip::CipUint instanceId, const SessionInfo::SPtr& si, const MessageRouter::SPtr& messageRouter);
IdentityObject(cip::CipUint instanceId, const SessionInfoIf::SPtr& si);
IdentityObject(cip::CipUint instanceId, const SessionInfoIf::SPtr& si, const MessageRouter::SPtr& messageRouter);

cip::CipUint getVendorId() const;
cip::CipUint getDeviceType() const;
Expand Down
31 changes: 27 additions & 4 deletions src/MessageRouter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace eipScanner {
using namespace utils;
using eip::CommonPacketItemFactory;
using eip::CommonPacket;
using eip::CommonPacketItem;
using eip::EncapsPacket;
using eip::EncapsPacketFactory;

Expand All @@ -26,12 +27,19 @@ namespace eipScanner {
MessageRouter::~MessageRouter() = default;

MessageRouterResponse
MessageRouter::sendRequest(SessionInfo::SPtr si, CipUsint service, const EPath &path,
MessageRouter::sendRequest(SessionInfoIf::SPtr si, CipUsint service, const EPath &path,
const std::vector<uint8_t> &data) const {
return this->sendRequest(si, service, path, data, {});
}

MessageRouterResponse
MessageRouter::sendRequest(SessionInfoIf::SPtr si, CipUsint service, const EPath &path,
const std::vector<uint8_t> &data,
const std::vector<eip::CommonPacketItem>& additionalPacketItems) const {
assert(si);

Logger(LogLevel::INFO) << "Send request: service=0x" << std::hex << static_cast<int>(service)
<< " epath=" << path.toString();
<< " epath=" << path.toString();

MessageRouterRequest request{service, path, data};

Expand All @@ -40,6 +48,10 @@ namespace eipScanner {
commonPacket << commonPacketItemFactory.createNullAddressItem();
commonPacket << commonPacketItemFactory.createUnconnectedDataItem(request.pack());

for(auto& item : additionalPacketItems) {
commonPacket << item;
}

auto packetToSend = EncapsPacketFactory()
.createSendRRDataPacket(si->getSessionHandle(), 0, commonPacket.pack());

Expand All @@ -51,12 +63,23 @@ namespace eipScanner {
std::vector<uint8_t> receivedData(receivedPacket.getData().size() - 6);

buffer >> interfaceHandle >> timeout >> receivedData;

commonPacket.expand(receivedData);

MessageRouterResponse response;
response.expand(commonPacket[1].getData());
const CommonPacketItem::Vec &items = commonPacket.getItems();

response.expand(items.at(1).getData());
if (items.size() > 2) {
response.setAdditionalPacketItems(
CommonPacketItem::Vec(items.begin() + 2, items.end()));
}


return response;
}

MessageRouterResponse
MessageRouter::sendRequest(SessionInfoIf::SPtr si, CipUsint service, const EPath &path) const {
return this->sendRequest(si, service, path, {}, {});
}
}
10 changes: 9 additions & 1 deletion src/MessageRouter.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "cip/EPath.h"
#include "cip/Services.h"
#include "cip/MessageRouterResponse.h"
#include "eip/CommonPacketItem.h"
#include "SessionInfo.h"

namespace eipScanner {
Expand All @@ -18,9 +19,16 @@ namespace eipScanner {

MessageRouter();
virtual ~MessageRouter();
virtual cip::MessageRouterResponse sendRequest(SessionInfo::SPtr si, cip::CipUsint service,
virtual cip::MessageRouterResponse sendRequest(SessionInfoIf::SPtr si, cip::CipUsint service,
const cip::EPath& path, const std::vector<uint8_t>& data,
const std::vector<eip::CommonPacketItem>& additionalPacketItems) const;

virtual cip::MessageRouterResponse sendRequest(SessionInfoIf::SPtr si, cip::CipUsint service,
const cip::EPath& path, const std::vector<uint8_t>& data) const;

virtual cip::MessageRouterResponse sendRequest(SessionInfoIf::SPtr si, cip::CipUsint service,
const cip::EPath& path) const;

};
}

Expand Down
6 changes: 3 additions & 3 deletions src/ParameterObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace eipScanner {
};

ParameterObject::ParameterObject(cip::CipUint id, bool fullAttributes,
const SessionInfo::SPtr &si)
const SessionInfoIf::SPtr &si)
: ParameterObject(id, fullAttributes, si, std::make_shared<MessageRouter>()) {
}

Expand All @@ -54,7 +54,7 @@ namespace eipScanner {
}

ParameterObject::ParameterObject(cip::CipUint instanceId, bool fullAttributes,
const SessionInfo::SPtr &si,
const SessionInfoIf::SPtr &si,
const MessageRouter::SPtr& messageRouter)
: BaseObject(CLASS_ID, instanceId)
, _name{""}
Expand Down Expand Up @@ -160,7 +160,7 @@ namespace eipScanner {
}
}

void ParameterObject::updateValue(const SessionInfo::SPtr& si) {
void ParameterObject::updateValue(const SessionInfoIf::SPtr& si) {
auto response = _messageRouter->sendRequest(si,
ServiceCodes::GET_ATTRIBUTE_SINGLE,
EPath(CLASS_ID, getInstanceId(),
Expand Down
6 changes: 3 additions & 3 deletions src/ParameterObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ class ParameterObject : public BaseObject {
* @param fullAttributes if true, then read all the attributes
* @param si
*/
ParameterObject(cip::CipUint instanceId, bool fullAttributes, const SessionInfo::SPtr& si);
ParameterObject(cip::CipUint instanceId, bool fullAttributes, const SessionInfoIf::SPtr& si);

ParameterObject(cip::CipUint instanceId, bool fullAttributes, const SessionInfo::SPtr& si, const MessageRouter::SPtr&);
ParameterObject(cip::CipUint instanceId, bool fullAttributes, const SessionInfoIf::SPtr& si, const MessageRouter::SPtr&);

/**
* Create an empty instance without any EIP requests
Expand All @@ -51,7 +51,7 @@ class ParameterObject : public BaseObject {
* Updates value from the instance
* @param si
*/
void updateValue(const SessionInfo::SPtr& si);
void updateValue(const SessionInfoIf::SPtr& si);

template <typename T>
T getActualValue() const {
Expand Down
12 changes: 5 additions & 7 deletions src/SessionInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,21 @@
#include <vector>
#include <memory>

#include "eip/EncapsPacket.h"
#include "SessionInfoIf.h"
#include "sockets/TCPSocket.h"

namespace eipScanner {
class SessionInfo {
class SessionInfo : public SessionInfoIf {
public:
using SPtr = std::shared_ptr<SessionInfo>;
using WPtr = std::weak_ptr<SessionInfo>;

SessionInfo(const std::string &host, int port, const std::chrono::milliseconds& recvTimeout);
SessionInfo(const std::string &host, int port);
~SessionInfo();

virtual eip::EncapsPacket sendAndReceive(const eip::EncapsPacket& packet) const;

cip::CipUdint getSessionHandle() const;
std::string getHost() const;
eip::EncapsPacket sendAndReceive(const eip::EncapsPacket& packet) const override;
cip::CipUdint getSessionHandle() const override;
std::string getHost() const override;

private:
sockets::TCPSocket _socket;
Expand Down
22 changes: 22 additions & 0 deletions src/SessionInfoIf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// Created by Aleksey Timin on 12/10/19.
//

#ifndef EIPSCANNER_SESSIONINFOIF_H
#define EIPSCANNER_SESSIONINFOIF_H

#include "eip/EncapsPacket.h"
namespace eipScanner {

class SessionInfoIf {
public:
using SPtr = std::shared_ptr<SessionInfoIf>;

virtual eip::EncapsPacket sendAndReceive(const eip::EncapsPacket &packet) const = 0;

virtual cip::CipUdint getSessionHandle() const = 0;

virtual std::string getHost() const = 0;
};
}
#endif //EIPSCANNER_SESSIONINFOIF_H
12 changes: 11 additions & 1 deletion src/cip/MessageRouterResponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ namespace cip {
: _serviceCode{ServiceCodes::GET_ATTRIBUTE_ALL}
, _generalStatusCode{GeneralStatusCodes::SUCCESS}
, _additionalStatus(0)
, _data(0) {
, _data(0)
, _additionalPacketItems(0) {
}

MessageRouterResponse::~MessageRouterResponse() = default;
Expand Down Expand Up @@ -68,6 +69,15 @@ namespace cip {
_data = data;
}

const std::vector<eip::CommonPacketItem> &MessageRouterResponse::getAdditionalPacketItems() const {
return _additionalPacketItems;
}

void
MessageRouterResponse::setAdditionalPacketItems(const std::vector<eip::CommonPacketItem> &_additionalPacketItems) {
MessageRouterResponse::_additionalPacketItems = _additionalPacketItems;
}

void logGeneralAndAdditionalStatus(const MessageRouterResponse &response) {
Logger logger(LogLevel::ERROR);
logger << "Message Router error=0x"
Expand Down
7 changes: 6 additions & 1 deletion src/cip/MessageRouterResponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "GeneralStatusCodes.h"
#include "Services.h"
#include "eip/CommonPacketItem.h"

namespace eipScanner {
namespace cip {
Expand All @@ -24,15 +25,19 @@ namespace cip {
ServiceCodes getServiceCode() const;
const std::vector<uint16_t> &getAdditionalStatus() const;
const std::vector<uint8_t> &getData() const;
const eip::CommonPacketItem::Vec &getAdditionalPacketItems() const;

void setGeneralStatusCode(GeneralStatusCodes generalStatusCode);

void setData(const std::vector<uint8_t> &data);

void setAdditionalPacketItems(const std::vector<eip::CommonPacketItem> &_additionalPacketItems);

private:
ServiceCodes _serviceCode;
GeneralStatusCodes _generalStatusCode;
std::vector<uint16_t> _additionalStatus;
std::vector<uint8_t> _data;
eip::CommonPacketItem::Vec _additionalPacketItems;
};

void logGeneralAndAdditionalStatus(const MessageRouterResponse& response);
Expand Down
4 changes: 2 additions & 2 deletions src/eip/CommonPacket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ namespace eip {
}
}

CommonPacketItem &CommonPacket::operator[](size_t index) {
return _items[index];
const CommonPacketItem::Vec &CommonPacket::getItems() {
return _items;
}
}
}
2 changes: 1 addition & 1 deletion src/eip/CommonPacket.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace eip {

std::vector<uint8_t> pack() const;
void expand(const std::vector<uint8_t>& data);
CommonPacketItem& operator [](size_t index);
const CommonPacketItem::Vec& getItems();
private:
CommonPacketItem::Vec _items;
};
Expand Down
Loading

0 comments on commit ab2042f

Please sign in to comment.