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

Resolve Windows (MinGW) issues and minor improvements #47

Merged
merged 11 commits into from
Apr 12, 2021
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ set(SOURCE_FILES
sockets/TCPSocket.cpp
sockets/UDPBoundSocket.cpp
sockets/UDPSocket.cpp
sockets/Platform.cpp

utils/Logger.cpp
utils/Buffer.cpp
Expand Down
22 changes: 10 additions & 12 deletions src/ConnectionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ namespace eipScanner {
auto t2oSize = t2oNCP.getConnectionSize();

connectionParameters.connectionPathSize = (connectionParameters.connectionPath .size() / 2)
+ (connectionParameters.connectionPath .size() % 2);
+ (connectionParameters.connectionPath.size() % 2);

if ((connectionParameters.transportTypeTrigger & NetworkConnectionParams::CLASS1) > 0
|| (connectionParameters.transportTypeTrigger & NetworkConnectionParams::CLASS3) > 0) {
Expand All @@ -87,22 +87,17 @@ namespace eipScanner {
connectionParameters.t2oNetworkConnectionParams += 4;
}

Buffer buffer;
buffer << sockets::EndPoint("0.0.0.0", 2222);
eip::CommonPacketItem addrItem(eip::CommonPacketItemIds::T2O_SOCKADDR_INFO, buffer.data());


MessageRouterResponse messageRouterResponse;
if (isLarge) {
LargeForwardOpenRequest request(connectionParameters);
messageRouterResponse = _messageRouter->sendRequest(si,
static_cast<cip::CipUsint>(ConnectionManagerServiceCodes::LARGE_FORWARD_OPEN),
EPath(6, 1), request.pack(), {addrItem});
EPath(6, 1), request.pack(), {});
} else {
ForwardOpenRequest request(connectionParameters);
messageRouterResponse = _messageRouter->sendRequest(si,
static_cast<cip::CipUsint>(ConnectionManagerServiceCodes::FORWARD_OPEN),
EPath(6, 1), request.pack(), {addrItem});
EPath(6, 1), request.pack(), {});
}

IOConnection::SPtr ioConnection;
Expand Down Expand Up @@ -148,15 +143,15 @@ namespace eipScanner {
} else {
ioConnection->_socket = std::make_unique<UDPSocket>(endPoint);
}

} else {
ioConnection->_socket = std::make_unique<UDPSocket>(si->getRemoteEndPoint().getHost(), 2222);
ioConnection->_socket = std::make_unique<UDPSocket>(si->getRemoteEndPoint().getHost(), EIP_DEFAULT_IMPLICIT_PORT);
}

Logger(LogLevel::INFO) << "Open UDP socket to send data to "
<< ioConnection->_socket->getRemoteEndPoint().toString();

findOrCreateSocket(sockets::EndPoint(si->getRemoteEndPoint().getHost(), 2222));
findOrCreateSocket(sockets::EndPoint(si->getRemoteEndPoint().getHost(), EIP_DEFAULT_IMPLICIT_PORT));

auto result = _connectionMap
.insert(std::make_pair(response.getT2ONetworkConnectionId(), ioConnection));
Expand Down Expand Up @@ -202,6 +197,7 @@ namespace eipScanner {
}

auto rc = _connectionMap.erase(ptr->_t2oNetworkConnectionId);
(void) rc;
assert(rc);
} else {
Logger(LogLevel::WARNING) << "Attempt to close an already closed connection";
Expand All @@ -212,6 +208,7 @@ namespace eipScanner {
std::vector<BaseSocket::SPtr > sockets;
std::transform(_socketMap.begin(), _socketMap.end(), std::back_inserter(sockets), [](auto entry) {
auto fd = entry.second->getSocketFd();
(void) fd;
return entry.second;
});

Expand All @@ -235,6 +232,7 @@ namespace eipScanner {
auto newSocket = std::make_shared<UDPBoundSocket>(endPoint);
_socketMap[endPoint] = newSocket;
newSocket->setBeginReceiveHandler([](sockets::BaseSocket& sock) {
(void) sock;
Logger(LogLevel::DEBUG) << "Received something";
});

Expand All @@ -253,7 +251,7 @@ namespace eipScanner {
if (io != _connectionMap.end()) {
io->second->notifyReceiveData(commonPacket.getItems().at(1).getData());
} else {
Logger(LogLevel::ERROR) << "Received data from unknow connection T2O_ID=" << connectionId;
Logger(LogLevel::ERROR) << "Received data from unknown connection T2O_ID=" << connectionId;
}
});

Expand Down
12 changes: 9 additions & 3 deletions src/DiscoveryManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Created by Aleksey Timin on 12/17/19.
//
#include <system_error>
#include <cerrno>

#include "eip/EncapsPacketFactory.h"
#include "eip/CommonPacket.h"
Expand All @@ -11,6 +10,13 @@
#include "utils/Buffer.h"

#include "DiscoveryManager.h"
#include "sockets/Platform.h"

#if defined (__unix__) || defined(__APPLE__)
#define DISCOVERY_SOCKET_RECEIVE_END_ERROR_CODE (EIPSCANNER_SOCKET_ERROR(EAGAIN))
#elif defined(_WIN32) || defined(WIN32) || defined(_WIN64)
#define DISCOVERY_SOCKET_RECEIVE_END_ERROR_CODE (EIPSCANNER_SOCKET_ERROR(ETIMEDOUT))
#endif

namespace eipScanner {
using namespace cip;
Expand Down Expand Up @@ -79,7 +85,7 @@ namespace eipScanner {
}
}
} catch (std::system_error& er) {
if (er.code().value() != EAGAIN) {
if (er.code().value() != DISCOVERY_SOCKET_RECEIVE_END_ERROR_CODE) {
throw er;
}
}
Expand All @@ -93,7 +99,7 @@ namespace eipScanner {

int broadcast = 1;
if(setsockopt(socket->getSocketFd(), SOL_SOCKET, SO_BROADCAST, (char*)&broadcast, sizeof(broadcast)) < 0) {
throw std::system_error(errno, std::generic_category());
throw std::system_error(sockets::BaseSocket::getLastError(), sockets::BaseSocket::getErrorCategory());
}

return socket;
Expand Down
2 changes: 1 addition & 1 deletion src/DiscoveryManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace eipScanner {
/**
* @brief default destructor
*/
~DiscoveryManager();
virtual ~DiscoveryManager();

/**
* @brief Discovers the EIP network
Expand Down
12 changes: 6 additions & 6 deletions src/IOConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,20 @@ namespace eipScanner {
, _connectionTimeoutMultiplier{0}
, _connectionTimeoutCount{0}
, _o2tSequenceNumber{0}
, _t2oSequenceNumber{0}
, _serialNumber{0}
, _transportTypeTrigger{0}
, _o2tRealTimeFormat{0}
, _t2oRealTimeFormat{0}
, _t2oSequenceNumber{0}
, _sequenceValueCount{0}
, _connectionPath(0)
, _originatorVendorId{0}
, _originatorSerialNumber{0}
, _serialNumber{0}
, _outputData()
, _lastHandleTime(std::chrono::steady_clock::now())
, _receiveDataHandle([](auto a, auto b, auto data) {})
, _sendDataHandle([](auto data) {})
, _closeHandle([]() {}) {
, _receiveDataHandle([](auto, auto, auto) {})
, _closeHandle([]() {})
, _sendDataHandle([](auto) {})
, _lastHandleTime(std::chrono::steady_clock::now()) {
}

IOConnection::~IOConnection() = default;
Expand Down
13 changes: 10 additions & 3 deletions src/IdentityObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace eipScanner {
using namespace cip;
using utils::Buffer;

IdentityObject::IdentityObject(cip::CipUint instanceId)
: BaseObject(CLASS_ID, instanceId)
, _vendorId(0)
Expand All @@ -25,7 +25,14 @@ namespace eipScanner {
}

IdentityObject::IdentityObject(cip::CipUint instanceId, const SessionInfoIf::SPtr &si, const MessageRouter::SPtr &messageRouter)
: BaseObject(CLASS_ID, instanceId) {
: BaseObject(CLASS_ID, instanceId)
, _vendorId(0)
, _deviceType(0)
, _productCode(0)
, _revision(0,0)
, _status(0)
, _serialNumber(0)
, _productName("") {

auto response = messageRouter->sendRequest(
si,
Expand Down Expand Up @@ -105,4 +112,4 @@ namespace eipScanner {
void IdentityObject::setProductName(const std::string &productName) {
_productName = CipShortString(productName);
}
}
}
2 changes: 1 addition & 1 deletion src/MessageRouter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@ namespace eipScanner {
MessageRouter::sendRequest(SessionInfoIf::SPtr si, CipUsint service, const EPath &path) const {
return this->sendRequest(si, service, path, {}, {});
}
}
}
23 changes: 16 additions & 7 deletions src/ParameterObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,15 @@ namespace eipScanner {
ParameterObject::ParameterObject(cip::CipUint instanceId, bool fullAttributes, size_t typeSize)
: BaseObject(CLASS_ID, instanceId)
, _hasFullAttributes(fullAttributes)
, _isScalable(false)
, _isReadOnly(false)
, _parameter(0)
, _value(typeSize)
, _maxValue(typeSize)
, _type(CipDataTypes::ANY)
, _name{""}
, _minValue(typeSize)
, _maxValue(typeSize)
, _defaultValue(typeSize)
, _isScalable(false)
, _isReadOnly(false)
, _scalingMultiplier(1)
, _scalingDivisor(1)
, _scalingBase(1)
Expand All @@ -60,10 +63,16 @@ namespace eipScanner {
const SessionInfoIf::SPtr &si,
const MessageRouter::SPtr& messageRouter)
: BaseObject(CLASS_ID, instanceId)
, _name{""}
, _hasFullAttributes{fullAttributes}
, _isScalable{false}
, _hasFullAttributes(fullAttributes)
, _isScalable(false)
, _isReadOnly(false)
, _parameter(0)
, _value(0)
, _type(CipDataTypes::ANY)
, _name{""}
, _minValue(0)
, _maxValue(0)
, _defaultValue(0)
, _scalingMultiplier(1)
, _scalingDivisor(1)
, _scalingBase(1)
Expand Down Expand Up @@ -302,4 +311,4 @@ namespace eipScanner {
return engValue;
}
}
};
}
2 changes: 1 addition & 1 deletion src/ParameterObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ class ParameterObject : public BaseObject {
bool _isScalable;
bool _isReadOnly;

cip::CipUint _parameter;
cip::CipUint _parameter;
std::vector<uint8_t> _value;
cip::CipDataTypes _type;
std::string _name;
Expand Down
2 changes: 1 addition & 1 deletion src/cip/EPath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,4 @@ namespace cip {
&& _attributeId == other._attributeId;
}
}
}
}
2 changes: 1 addition & 1 deletion src/cip/MessageRouterResponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,4 @@ namespace cip {
}
}
}
}
}
19 changes: 19 additions & 0 deletions src/cip/Services.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,27 @@ namespace cip {
/* Start CIP common services */
GET_ATTRIBUTE_ALL = 0X01,
SET_ATTRIBUTE_ALL = 0X02,
GET_ATTRIBUTE_LIST = 0x03,
SET_ATTRIBUTE_LIST = 0x04,
RESET = 0x05,
START = 0x06,
STOP = 0x07,
CREATE_OBJECT_INSTANCE = 0x08,
DELETE_OBJECT_INSTANCE = 0x09,
MULTIPLE_SERVICE_PACKET = 0x0A,
APPLY_ATTRIBUTES = 0x0D,
GET_ATTRIBUTE_SINGLE = 0X0E,
SET_ATTRIBUTE_SINGLE = 0X10,
FIND_NEXT_OBJECT_INSTANCE = 0x11,
ERROR_RESPONSE = 0x14, //DeviceNet only
RESTORE = 0x15,
SAVE = 0x16,
GET_MEMBER = 0x18,
NO_OPERATION = 0x17,
SET_MEMBER = 0x19,
INSERT_MEMBER = 0x1A,
REMOVE_MEMBER = 0x1B,
GROUP_SYNC = 0x1C
/* End CIP common services */
};

Expand Down
5 changes: 2 additions & 3 deletions src/cip/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace cip {
typedef double CipLreal; /**< 64-bit IEEE 754 floating point */
typedef uint64_t CipLword; /**< 64-bit bit unsigned integer */
typedef int64_t CipLint; /**< 64-bit bit signed integer */
typedef uint64_t CipUlint; /**< 64-bit bit unsigned integer */
typedef uint64_t CipUlint; /**< 64-bit bit unsigned integer */


enum class CipDataTypes : CipUsint {
Expand Down Expand Up @@ -54,8 +54,7 @@ namespace cip {
LTIME = 0xD7, /**< Duration in micro-seconds, high resolution, range of LINT */
ITIME = 0xD8, /**< Duration in milli-seconds, short; range of INT*/
STRINGN = 0xD9, /**< Character string, N byte per character */
SHORT_STRING = 0xDA, /**< Character string, 1 byte per character, 1 byte
length indicator */
SHORT_STRING = 0xDA, /**< Character string, 1 byte per character, 1 byte length indicator */
TIME = 0xDB, /**< Duration in milli-seconds; range of DINT */
EPATH = 0xDC, /**< CIP path segments*/
ENG_UNIT = 0xDD, /**< Engineering Units*/
Expand Down
2 changes: 1 addition & 1 deletion src/cip/connectionManager/ForwardCloseRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ namespace connectionManager {

connectionManager::ForwardCloseRequest::ForwardCloseRequest()
: _connectionSerialNumber{0}
, _originatorVendorID{0}
, _originatorSerialNumber{0}
, _originatorVendorID{0}
, _connectionPath(0) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ namespace connectionManager {
using utils::Buffer;

NetworkConnectionParametersBuilder::NetworkConnectionParametersBuilder(CipUdint val, bool lfo) :
_lfo{lfo}, _value{val} {
_value{val}, _lfo{lfo} {

};
}

NetworkConnectionParametersBuilder NetworkConnectionParametersBuilder::setRedundantOwner(RedundantOwner val) {
if (_lfo) {
Expand Down
2 changes: 1 addition & 1 deletion src/eip/CommonPacketItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ namespace eip {
return !(rhs == *this);
}
}
}
}
4 changes: 2 additions & 2 deletions src/eip/EncapsPacket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace eip {
if (data.size() < HEADER_SIZE) {
throw std::runtime_error("EncapsPacket header must be 24 bytes");
}

Buffer buffer(data);
buffer >> reinterpret_cast<cip::CipUint&>(_command)
>> _length
Expand All @@ -41,7 +41,7 @@ namespace eip {
throw std::runtime_error("EncapsPacket data must be " + std::to_string(_length)
+ " but we have only " + std::to_string(dataSize) + " bytes");
}

_data.resize(_length);
buffer >> _data;
}
Expand Down
5 changes: 4 additions & 1 deletion src/fileObject/FileObjectEmptyState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ namespace fileObject {
}

void FileObjectEmptyState::initiateUpload(SessionInfoIf::SPtr si, EndUploadHandler handle) {
(void) si;
(void) handle;
logWithStateName(LogLevel::WARNING, "File cannot be uploaded");
}

bool FileObjectEmptyState::transfer(SessionInfoIf::SPtr si) {
(void) si;
logWithStateName(LogLevel::WARNING, "Nothing to transfer");
return false;
}

}
}
}
Loading