// SPDX-License-Identifier: MIT /** Copyright (c) 2016 - 2022 Beckhoff Automation GmbH & Co. KG */ #include "AdsDevice.h" #include "AdsException.h" #include "AdsLib.h" #include "Log.h" #include namespace Beckhoff { namespace Ads { static AmsNetId* Connect(AmsNetId ams, const char* ip) { const auto error = ConnectTarget(ams, ip); if (error) { if(error == ROUTERERR_PORTALREADYINUSE){ LOG_INFO("Connection["<= ADSSTATE::ADSSTATE_MAXSTATES) { //throw std::out_of_range("Unknown ADSSTATE(" + std::to_string(i) + ')'); LOG_ERROR("AdsDevice unkown device state."); } } } return {static_cast(state[0]), static_cast(state[1])}; } void AdsDevice::SetState(const ADSSTATE AdsState, const ADSSTATE DeviceState) const { if(m_Connected == true){ auto error = AdsSyncWriteControlReqEx(GetLocalPort(), &m_Addr, AdsState, DeviceState, 0, nullptr); if (error) { //throw AdsException(error); LOG_ERROR("AdsDevice set device state failed."); } } } void AdsDevice::SetTimeout(const uint32_t timeout) const { if(m_Connected == true){ const auto error = AdsSyncSetTimeoutEx(GetLocalPort(), timeout); if (error) { //throw AdsException(error); LOG_ERROR("AdsDevice set timeout failed."); } } } uint32_t AdsDevice::GetTimeout() const { uint32_t timeout = 0; if(m_Connected == true){ const auto error = AdsSyncGetTimeoutEx(GetLocalPort(), &timeout); if (error) { //throw AdsException(error); LOG_ERROR("AdsDevice get timeout failed."); } } return timeout; } AdsHandle AdsDevice::OpenFile(const std::string& filename, const uint32_t flags) const { if(m_Connected == true){ uint32_t bytesRead = 0; uint32_t handle = 0; const auto error = ReadWriteReqEx2(SYSTEMSERVICE_FOPEN, flags, sizeof(handle), &handle, filename.length(), filename.c_str(), &bytesRead); if (error) { //throw AdsException(error); LOG_ERROR("AdsDevice open file failed."); }else{ handle = Beckhoff::letoh(handle); return {new uint32_t {handle}, {std::bind(&AdsDevice::CloseFile, this, std::placeholders::_1)}}; } } return {new uint32_t {0}, {[](uint32_t){ return 0; }}}; } long AdsDevice::ReadReqEx2(uint32_t group, uint32_t offset, size_t length, void* buffer, uint32_t* bytesRead) const { if(m_Connected == false) return ROUTERERR_HOSTDENY; if (length > std::numeric_limits::max()) { return ADSERR_DEVICE_INVALIDSIZE; } return AdsSyncReadReqEx2(*m_LocalPort, &m_Addr, group, offset, static_cast(length), buffer, bytesRead); } long AdsDevice::ReadWriteReqEx2(uint32_t indexGroup, uint32_t indexOffset, size_t readLength, void* readData, size_t writeLength, const void* writeData, uint32_t* bytesRead) const { if(m_Connected == false) return ROUTERERR_HOSTDENY; if (readLength > std::numeric_limits::max()) { return ADSERR_DEVICE_INVALIDSIZE; } if (writeLength > std::numeric_limits::max()) { return ADSERR_DEVICE_INVALIDSIZE; } return AdsSyncReadWriteReqEx2(GetLocalPort(), &m_Addr, indexGroup, indexOffset, static_cast(readLength), readData, static_cast(writeLength), writeData, bytesRead ); } long AdsDevice::WriteReqEx(uint32_t group, uint32_t offset, size_t length, const void* buffer) const { if(m_Connected == false) return ROUTERERR_HOSTDENY; if (length > std::numeric_limits::max()) { return ADSERR_DEVICE_INVALIDSIZE; } return AdsSyncWriteReqEx(GetLocalPort(), &m_Addr, group, offset, static_cast(length), buffer); } } }