Skip to content

Commit

Permalink
Merge pull request #49 from nimbuscontrols/issue-45-add-msvc-support
Browse files Browse the repository at this point in the history
Add winsock startup-cleanup to examples and link ws2_32
  • Loading branch information
Broekman authored May 7, 2021
2 parents c1fa5c1 + f4f8b9a commit a7e88cf
Show file tree
Hide file tree
Showing 8 changed files with 346 additions and 186 deletions.
11 changes: 11 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,19 @@ target_link_libraries(implicit_messaging EIPScanner)

add_executable(parameter_object_example ParameterObjectExample.cpp)
target_link_libraries(parameter_object_example EIPScanner)

add_executable(discovery_example DiscoveryManagerExample.cpp)
target_link_libraries(discovery_example EIPScanner)

add_executable(yaskawa_assembly_object_example vendors/yaskawa/mp3300iec/Yaskawa_AssemblyObjectExample.cpp)
target_link_libraries(yaskawa_assembly_object_example EIPScanner)

if(WIN32)
target_link_libraries(explicit_messaging ws2_32)
target_link_libraries(file_object_example ws2_32)
target_link_libraries(identity_object_example ws2_32)
target_link_libraries(implicit_messaging ws2_32)
target_link_libraries(parameter_object_example ws2_32)
target_link_libraries(discovery_example ws2_32)
target_link_libraries(yaskawa_assembly_object_example ws2_32)
endif()
38 changes: 29 additions & 9 deletions examples/DiscoveryManagerExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
// Created by Aleksey Timin on 12/17/19.
//

#if defined(_WIN32) || defined(WIN32) || defined(_WIN64)
#include <winsock2.h>
#define OS_Windows (1)
#endif

#include <DiscoveryManager.h>
#include <utils/Logger.h>

Expand All @@ -10,14 +15,29 @@ using eipScanner::utils::Logger;
using eipScanner::utils::LogLevel;

int main() {
Logger::setLogLevel(LogLevel::DEBUG);
Logger::setLogLevel(LogLevel::DEBUG);

#if OS_Windows
WSADATA wsaData;
int winsockStart = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (winsockStart != 0) {
Logger(LogLevel::ERROR) << "Failed to start WinSock - error code: " << winsockStart;
return EXIT_FAILURE;
}
#endif

DiscoveryManager discoveryManager("172.28.255.255", 0xAF12, std::chrono::seconds(1));
auto devices = discoveryManager.discover();

for (auto& device : devices) {
Logger(LogLevel::INFO) << "Discovered device: "
<< device.identityObject.getProductName()
<< " with address " << device.socketAddress.toString();
}

DiscoveryManager discoveryManager("172.28.255.255", 0xAF12, std::chrono::seconds(1));
auto devices = discoveryManager.discover();
#if OS_Windows
WSACleanup();
#endif

for (auto& device : devices) {
Logger(LogLevel::INFO) << "Discovered device: "
<< device.identityObject.getProductName()
<< " with address " << device.socketAddress.toString();
}
}
return EXIT_SUCCESS;
}
94 changes: 56 additions & 38 deletions examples/ExplicitMessagingExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
// Created by Aleksey Timin on 11/16/19.
//

#if defined(_WIN32) || defined(WIN32) || defined(_WIN64)
#include <winsock2.h>
#define OS_Windows (1)
#endif

#include <cstdlib>
#include <sstream>
#include <cip/connectionManager/NetworkConnectionParams.h>
Expand All @@ -22,50 +27,63 @@ using eipScanner::utils::Logger;
using eipScanner::utils::LogLevel;

int main() {
Logger::setLogLevel(LogLevel::DEBUG);
auto si = std::make_shared<SessionInfo>("127.0.0.1", 0xAF12, std::chrono::seconds(10));
auto messageRouter = std::make_shared<MessageRouter>();
Logger::setLogLevel(LogLevel::DEBUG);

#if OS_Windows
WSADATA wsaData;
int winsockStart = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (winsockStart != 0) {
Logger(LogLevel::ERROR) << "Failed to start WinSock - error code: " << winsockStart;
return EXIT_FAILURE;
}
#endif

auto si = std::make_shared<SessionInfo>("127.0.0.1", 0xAF12, std::chrono::seconds(10));
auto messageRouter = std::make_shared<MessageRouter>();

// Read attribute
auto response = messageRouter->sendRequest(si, ServiceCodes::GET_ATTRIBUTE_SINGLE,
EPath(0x01, 1, 1),
{});
// Read attribute
auto response = messageRouter->sendRequest(si, ServiceCodes::GET_ATTRIBUTE_SINGLE,
EPath(0x01, 1, 1),
{});

if (response.getGeneralStatusCode() == GeneralStatusCodes::SUCCESS) {
Buffer buffer(response.getData());
CipUint vendorId;
buffer >> vendorId;
if (response.getGeneralStatusCode() == GeneralStatusCodes::SUCCESS) {
Buffer buffer(response.getData());
CipUint vendorId;
buffer >> vendorId;

Logger(LogLevel::INFO) << "Vendor ID is " << vendorId;
} else {
Logger(LogLevel::ERROR) << "We got error=0x" << std::hex << response.getGeneralStatusCode();
}
Logger(LogLevel::INFO) << "Vendor ID is " << vendorId;
} else {
Logger(LogLevel::ERROR) << "We got error=0x" << std::hex << response.getGeneralStatusCode();
}

//Write attribute
// See OpenEr EDS 160 line
Buffer assembly151;
assembly151 << CipUsint(1)
<< CipUsint(2)
<< CipUsint(3)
<< CipUsint(4)
<< CipUsint(5)
<< CipUsint(6)
<< CipUsint(7)
<< CipUsint(8)
<< CipUsint(9)
<< CipUsint(10);
//Write attribute
// See OpenEr EDS 160 line
Buffer assembly151;
assembly151 << CipUsint(1)
<< CipUsint(2)
<< CipUsint(3)
<< CipUsint(4)
<< CipUsint(5)
<< CipUsint(6)
<< CipUsint(7)
<< CipUsint(8)
<< CipUsint(9)
<< CipUsint(10);


response = messageRouter->sendRequest(si, ServiceCodes::SET_ATTRIBUTE_SINGLE,
EPath(0x04, 151, 3),
assembly151.data());
response = messageRouter->sendRequest(si, ServiceCodes::SET_ATTRIBUTE_SINGLE,
EPath(0x04, 151, 3),
assembly151.data());

if (response.getGeneralStatusCode() == GeneralStatusCodes::SUCCESS) {
Logger(LogLevel::INFO) << "Writing is successful";
} else {
Logger(LogLevel::ERROR) << "We got error=0x" << std::hex << response.getGeneralStatusCode();
}
if (response.getGeneralStatusCode() == GeneralStatusCodes::SUCCESS) {
Logger(LogLevel::INFO) << "Writing is successful";
} else {
Logger(LogLevel::ERROR) << "We got error=0x" << std::hex << response.getGeneralStatusCode();
}

#if OS_Windows
WSACleanup();
#endif

return 0;
}
return EXIT_SUCCESS;
}
51 changes: 36 additions & 15 deletions examples/FileObjectExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
// Created by Aleksey Timin on 11/24/19.
//

#if defined(_WIN32) || defined(WIN32) || defined(_WIN64)
#include <winsock2.h>
#define OS_Windows (1)
#endif

#include <fstream>
#include "FileObject.h"
#include "utils/Logger.h"
Expand All @@ -14,18 +19,34 @@ using eipScanner::utils::LogLevel;
using eipScanner::FileObject;

int main() {
Logger::setLogLevel(LogLevel::DEBUG);
auto si = std::make_shared<SessionInfo>("172.28.1.3", 0xAF12);

FileObject edsFile(0xC8, si);
edsFile.beginUpload(si, [](GeneralStatusCodes status, const std::vector<uint8_t> &fileContent) {
if (status == GeneralStatusCodes::SUCCESS) {
std::ofstream outFile("Device.eds", std::ios::out | std::ios::trunc | std::ios::binary);
outFile.write(reinterpret_cast<const char *>(fileContent.data()), fileContent.size());
}
});

while (edsFile.handleTransfers(si)) {
continue;
};
}
Logger::setLogLevel(LogLevel::DEBUG);

#if OS_Windows
WSADATA wsaData;
int winsockStart = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (winsockStart != 0) {
Logger(LogLevel::ERROR) << "Failed to start WinSock - error code: " << winsockStart;
return EXIT_FAILURE;
}
#endif

auto si = std::make_shared<SessionInfo>("172.28.1.3", 0xAF12);

FileObject edsFile(0xC8, si);
edsFile.beginUpload(si, [](GeneralStatusCodes status, const std::vector<uint8_t> &fileContent) {
if (status == GeneralStatusCodes::SUCCESS) {
std::ofstream outFile("Device.eds", std::ios::out | std::ios::trunc | std::ios::binary);
outFile.write(reinterpret_cast<const char *>(fileContent.data()), fileContent.size());
}
});

while (edsFile.handleTransfers(si)) {
continue;
};

#if OS_Windows
WSACleanup();
#endif

return EXIT_SUCCESS;
}
46 changes: 33 additions & 13 deletions examples/IdentityObjectExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
// Created by Aleksey Timin on 12/19/19.
//

#if defined(_WIN32) || defined(WIN32) || defined(_WIN64)
#include <winsock2.h>
#define OS_Windows (1)
#endif

#include "IdentityObject.h"
#include "utils/Logger.h"

Expand All @@ -11,16 +16,31 @@ using eipScanner::utils::Logger;
using eipScanner::utils::LogLevel;

int main() {
auto si = std::make_shared<SessionInfo>("172.28.1.3", 0xAF12);
IdentityObject identityObject(1, si);

Logger(LogLevel::INFO) << identityObject.getVendorId()
<< identityObject.getDeviceType()
<< identityObject.getProductCode()
<< identityObject.getRevision().toString()
<< identityObject.getStatus()
<< identityObject.getSerialNumber()
<< identityObject.getProductName();

return 0;
}
Logger::setLogLevel(LogLevel::DEBUG);

#if OS_Windows
WSADATA wsaData;
int winsockStart = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (winsockStart != 0) {
Logger(LogLevel::ERROR) << "Failed to start WinSock - error code: " << winsockStart;
return EXIT_FAILURE;
}
#endif

auto si = std::make_shared<SessionInfo>("172.28.1.3", 0xAF12);
IdentityObject identityObject(1, si);

Logger(LogLevel::INFO) << identityObject.getVendorId()
<< identityObject.getDeviceType()
<< identityObject.getProductCode()
<< identityObject.getRevision().toString()
<< identityObject.getStatus()
<< identityObject.getSerialNumber()
<< identityObject.getProductName();

#if OS_Windows
WSACleanup();
#endif

return EXIT_SUCCESS;
}
Loading

0 comments on commit a7e88cf

Please sign in to comment.