Motion Master
Loading...
Searching...
No Matches
Public Member Functions | List of all members
EthernetDevice Class Reference

Handles TCP communication with SOMANET devices over Ethernet. More...

#include <ethernet_client.h>

Inheritance diagram for EthernetDevice:
Inheritance graph
[legend]
Collaboration diagram for EthernetDevice:
Collaboration graph
[legend]

Public Member Functions

 EthernetDevice (const std::string &ip, unsigned short port)
 Constructs an EthernetDevice object with the specified IP address and port. More...
 
 ~EthernetDevice ()
 Destructor for the EthernetDevice class. More...
 
uint16_t incrementSeqId ()
 Increments the sequence ID atomically and wraps it around at the maximum value. More...
 
bool connect ()
 Establishes a connection to the remote server. More...
 
bool isConnected ()
 Checks if the Ethernet socket is currently open. More...
 
bool disconnect ()
 Closes the Ethernet socket connection. More...
 
EthernetMessage exchangeWithTimeout (const EthernetMessage &request, const std::chrono::steady_clock::duration expiryTime)
 Exchanges a message with a remote server and waits for a response with a timeout. More...
 
uint8_t getState (const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(3000)) override
 Sends a request to read the state of the device and returns the state value. More...
 
bool setState (uint8_t state, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(3000)) override
 Sends a state control command to the Ethernet device and checks the response. More...
 
std::vector< uint8_t > readFile (const std::string &filename, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(5000)) override
 Reads the contents of a file over Ethernet. More...
 
std::vector< std::string > readFileList (const bool stripSizeSuffix=true, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(3000))
 Reads a list of file names from the device. More...
 
bool removeFile (const std::string &filename, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(3000))
 Removes a file from the device. More...
 
bool writeFile (const std::string &filename, const std::vector< uint8_t > &data, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(5000)) override
 Sends a file in chunks to the remote server via Ethernet. More...
 
bool triggerFirmwareUpdate (const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(2000))
 Sends a firmware update request to the connected Integro device. More...
 
std::vector< common::ParametergetParameters (bool readValues=false, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(1000))
 
std::vector< uint8_t > readSdo (uint16_t index, uint8_t subindex, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(1000))
 Reads an SDO (Service Data Object) from an Ethernet device. More...
 
bool writeSdo (uint16_t index, uint8_t subindex, const std::vector< uint8_t > &data, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(1000))
 Writes an SDO (Service Data Object) to an Ethernet device. More...
 
void loadParameters (bool readValues=false, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(9000)) override
 Loads parameters from the device and stores them locally. More...
 
void clearParameters () override
 Clears all loaded object dictionary parameters. More...
 
common::ParameterfindParameter (uint16_t index, uint8_t subindex) override
 Finds and returns a reference to a parameter by its index and subindex. More...
 
common::Parameterupload (const uint16_t index, const uint8_t subindex, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(3000)) override
 Uploads a parameter from the device via SDO and updates the local parameter store. More...
 
template<typename T >
upload (uint16_t index, uint8_t subindex, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(5000))
 Uploads a parameter from the device and returns its value as the specified type. More...
 
void download (uint16_t index, uint8_t subindex, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(5000)) override
 Downloads a parameter to the device using SDO communication. More...
 
void download (uint16_t index, uint8_t subindex, const common::ParameterValue &value, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(5000)) override
 Sets a parameter value in the local store and downloads it to the device. More...
 
template<typename T >
void download (uint16_t index, uint8_t subindex, const T &value, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(5000))
 Sets and downloads a parameter value to the device using the specified type. More...
 
std::vector< uint8_t > sendAndReceiveProcessData (const std::vector< uint8_t > &data, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(1000))
 Sends process data to a remote device and receives the response. More...
 
void exchangeProcessDataAndUpdateParameters () override
 Exchanges process data with the remote device and updates local parameters accordingly. More...
 

Detailed Description

Handles TCP communication with SOMANET devices over Ethernet.

This class provides methods for connecting to a remote server, sending messages, and receiving responses over a TCP connection using Boost.Asio. It manages the underlying socket, connection, and I/O operations required for client-server communication.

Constructor & Destructor Documentation

◆ EthernetDevice()

EthernetDevice::EthernetDevice ( const std::string &  ip,
unsigned short  port 
)

Constructs an EthernetDevice object with the specified IP address and port.

Initializes the client with the target server's IP address and port number.

Parameters
ipThe IP address of the server to connect to.
portThe port number to use for the connection.

◆ ~EthernetDevice()

EthernetDevice::~EthernetDevice ( )

Destructor for the EthernetDevice class.

Closes the socket and disconnects from the server if the socket is open.

Member Function Documentation

◆ clearParameters()

void EthernetDevice::clearParameters ( )
overridevirtual

Clears all loaded object dictionary parameters.

This function removes all entries from the internal parameter map, effectively resetting the parameter list of the slave device.

Implements common::Device.

◆ connect()

bool EthernetDevice::connect ( )

Establishes a connection to the remote server.

Attempts to connect to the specified IP address and port provided during construction. This method blocks until the connection is either successfully established or fails.

Returns
true if the connection was successfully established; false otherwise.

◆ disconnect()

bool EthernetDevice::disconnect ( )

Closes the Ethernet socket connection.

This function attempts to close the Ethernet socket. It handles any errors that may occur during the process, logging the result. If the socket is successfully closed, it returns true; otherwise, it returns false.

Returns
true if the socket was closed successfully, false if an error occurred or an exception was thrown during the operation.

◆ download() [1/3]

void EthernetDevice::download ( uint16_t  index,
uint8_t  subindex,
const common::ParameterValue value,
const std::chrono::steady_clock::duration  expiryTime = std::chrono::milliseconds(5000) 
)
overridevirtual

Sets a parameter value in the local store and downloads it to the device.

This function updates the specified parameter in the local store with the provided value, serializes it, and then performs an SDO download to transfer the data to the device.

Parameters
indexThe 16-bit index of the parameter in the object dictionary.
subindexThe 8-bit subindex of the parameter.
valueThe new parameter value to set and download.
expiryTimeThe maximum duration to wait for the SDO download operation to complete.
Exceptions
std::runtime_errorIf the parameter data is empty after setting the value, or if the SDO download operation fails.

Implements common::Device.

◆ download() [2/3]

void EthernetDevice::download ( uint16_t  index,
uint8_t  subindex,
const std::chrono::steady_clock::duration  expiryTime = std::chrono::milliseconds(5000) 
)
overridevirtual

Downloads a parameter to the device using SDO communication.

This function sends the binary data of the specified parameter (retrieved from the local store) to the device. The operation fails if the parameter's data is empty.

Parameters
indexThe 16-bit index of the parameter in the object dictionary.
subindexThe 8-bit subindex of the parameter.
expiryTimeThe maximum duration to wait for the SDO download operation to complete.
Exceptions
std::runtime_errorIf the parameter data is empty or the SDO download fails.

Implements common::Device.

◆ download() [3/3]

template<typename T >
void EthernetDevice::download ( uint16_t  index,
uint8_t  subindex,
const T &  value,
const std::chrono::steady_clock::duration  expiryTime = std::chrono::milliseconds(5000) 
)
inline

Sets and downloads a parameter value to the device using the specified type.

This templated overload wraps the given value in a common::ParameterValue, updates the local parameter store, and performs an SDO download to transfer the data to the device.

Template Parameters
TThe type of the value to set and download.
Parameters
indexThe 16-bit index of the parameter in the object dictionary.
subindexThe 8-bit subindex of the parameter.
valueThe new parameter value to set and download.
expiryTimeThe maximum duration to wait for the SDO download operation. Defaults to 5000 milliseconds.
Exceptions
std::runtime_errorIf the parameter data is empty after setting the value, or if the SDO download operation fails.

◆ exchangeProcessDataAndUpdateParameters()

void EthernetDevice::exchangeProcessDataAndUpdateParameters ( )
overridevirtual

Exchanges process data with the remote device and updates local parameters accordingly.

This function prepares the process data to send by collecting the current data from parameters mapped as RxPDOs. It then sends this data and receives updated process data from the remote device. The received data is used to update local parameters mapped as TxPDOs.

The bitlength of each PDO entry is converted to byte size, rounding up to handle non-byte-aligned bit lengths. If received data is smaller than expected for a given PDO, an error is logged and the update for that PDO is skipped. After processing all PDOs, if there is any extra received data not mapped to parameters, a warning is logged.

Exceptions
std::runtime_errorif findParameter throws (e.g., if parameter not found).

Implements common::Device.

◆ exchangeWithTimeout()

EthernetMessage EthernetDevice::exchangeWithTimeout ( const EthernetMessage request,
const std::chrono::steady_clock::duration  expiryTime 
)

Exchanges a message with a remote server and waits for a response with a timeout.

This method serializes the request message, sends it to the server using an asynchronous write operation, and waits for a response with a timeout. If the operation takes longer than the specified expiry time, the operation will be canceled and an error will be thrown.

Parameters
requestMessageThe message to send to the remote server.
expiryTimeThe duration to wait for the socket operation (read/write) before timing out.
Returns
EthernetMessage The parsed response message from the server.
Exceptions
std::runtime_errorIf the write or read operation fails, or if the operation times out.

◆ findParameter()

common::Parameter & EthernetDevice::findParameter ( uint16_t  index,
uint8_t  subindex 
)
overridevirtual

Finds and returns a reference to a parameter by its index and subindex.

This function looks up a parameter in the internal parameter map using the specified index and subindex. If the parameter is found, a reference to it is returned. If not, a runtime exception is thrown.

Parameters
indexThe index of the parameter to find.
subindexThe subindex of the parameter to find.
Returns
Reference to the found parameter.
Exceptions
std::runtime_errorIf the parameter with the specified index and subindex is not found in the map.

Implements common::Device.

◆ getParameters()

std::vector< common::Parameter > EthernetDevice::getParameters ( bool  readValues = false,
const std::chrono::steady_clock::duration  expiryTime = std::chrono::milliseconds(1000) 
)

Retrieves a list of parameters from the device, potentially updating their values.

This function communicates with the device to request and retrieve parameter data in multiple segments. It constructs request messages to retrieve parameter data and processes the received responses, accumulating all parameter data in the content buffer. Once all data has been retrieved, it parses the content buffer into individual parameter objects and returns them as a std::vector<common::Parameter>.

Parameters
readValuesIf true, the function will attempt to read parameter values from the device. If false, it will use the existing data from the content buffer.
expiryTimeThe duration to wait for the socket operation (read/write) before timing out. Defaults to 1000 milliseconds.
Returns
A vector of parsed common::Parameter objects containing the parameters retrieved from the device.
Exceptions
boost::system::system_errorif sending or receiving fails.
std::runtime_errorif the response buffer is too small to parse.

◆ getState()

uint8_t EthernetDevice::getState ( const std::chrono::steady_clock::duration  expiryTime = std::chrono::milliseconds(3000))
overridevirtual

Sends a request to read the state of the device and returns the state value.

This function sends a request to the device using the EthernetMessageType::STATE_READ message type. It increments the sequence ID, constructs the request message, serializes it, and sends it to the device. After sending the request, the function waits for the response, parses the response message, and returns the state value as a uint8_t.

The returned state corresponds to the EtherCAT state machine states, where:

  • INIT: 1
  • PREOP: 2
  • SAFEOP: 4
  • OP: 8
  • BOOT: 3
Parameters
expiryTimeThe duration to wait for the socket operation (read/write) before timing out. Defaults to 3000 milliseconds.
Returns
The state value of the device as a uint8_t.
Exceptions
boost::system::system_errorif sending or receiving fails.
std::runtime_errorif the response buffer is too small to parse.

Implements common::Device.

◆ incrementSeqId()

uint16_t EthernetDevice::incrementSeqId ( )

Increments the sequence ID atomically and wraps it around at the maximum value.

This function atomically increments the seq_id (a 16-bit unsigned integer). When the value reaches the maximum value of uint16_t (0xFFFF), it wraps around to 0. It ensures thread safety when used in multithreaded environments.

Returns
The updated sequence ID after incrementing, wrapped around if necessary.

◆ isConnected()

bool EthernetDevice::isConnected ( )

Checks if the Ethernet socket is currently open.

Returns true if the underlying socket is open, indicating an active or initialized connection. Returns false otherwise.

Returns
true if the socket is open, false otherwise.

◆ loadParameters()

void EthernetDevice::loadParameters ( bool  readValues = false,
const std::chrono::steady_clock::duration  expiryTime = std::chrono::milliseconds(9000) 
)
overridevirtual

Loads parameters from the device and stores them locally.

This function retrieves a list of parameters using the getParameters method. It then stores each parameter in a map for later use, keyed by a pair consisting of the parameter's index and subindex.

Parameters
readValuesIf true, the values of the parameters are read from the device; otherwise, only the parameter metadata is retrieved.
expiryTimeThe duration to wait for the socket operation (read/write) before timing out. Defaults to 9000 milliseconds.

Implements common::Device.

◆ readFile()

std::vector< uint8_t > EthernetDevice::readFile ( const std::string &  filename,
const std::chrono::steady_clock::duration  expiryTime = std::chrono::milliseconds(5000) 
)
overridevirtual

Reads the contents of a file over Ethernet.

This function sends a series of Ethernet messages to request file data in segments. It uses a loop to send requests and receive responses from a remote server until the entire file is read. The file content is accumulated in a vector and returned.

Parameters
filenameThe name of the file to be read.
expiryTimeThe duration to wait for the socket operation (read/write) before timing out. Defaults to 3000 milliseconds.
Returns
A std::vector<uint8_t> containing the file data received from the remote server.
Exceptions
boost::system::system_errorif sending or receiving fails.
std::runtime_errorif the response buffer is too small to parse.

Implements common::Device.

◆ readFileList()

std::vector< std::string > EthernetDevice::readFileList ( const bool  stripSizeSuffix = true,
const std::chrono::steady_clock::duration  expiryTime = std::chrono::milliseconds(3000) 
)

Reads a list of file names from the device.

Sends a request to read the file list using the "fs-getlist" command, waits up to the specified timeout, and parses the result into individual lines. Each line represents a file name. Handles both Unix (\n) and Windows (\r\n) line endings.

Parameters
stripSizeSuffixIf true, removes the ", size: <bytes>" suffix from each line. Defaults to true.
expiryTimeThe duration to wait for the socket operation (read/write) before timing out. Defaults to 3000 milliseconds.
Returns
A vector of strings, where each string is a file name from the response.

◆ readSdo()

std::vector< uint8_t > EthernetDevice::readSdo ( uint16_t  index,
uint8_t  subindex,
const std::chrono::steady_clock::duration  expiryTime = std::chrono::milliseconds(1000) 
)

Reads an SDO (Service Data Object) from an Ethernet device.

This function sends a request to read an SDO at a specified index and subindex, then receives and parses the response. If the response indicates a failure, it logs an error and returns an empty vector. Otherwise, it returns the SDO data.

Parameters
indexThe index of the SDO to read.
subindexThe subindex of the SDO to read.
expiryTimeThe duration to wait for the socket operation (read/write) before timing out. Defaults to 1000 milliseconds.
Returns
A vector containing the data from the SDO response. An empty vector is returned if the operation fails.
Exceptions
boost::system::system_errorif sending or receiving fails.
std::runtime_errorif the response buffer is too small to parse.

◆ removeFile()

bool EthernetDevice::removeFile ( const std::string &  filename,
const std::chrono::steady_clock::duration  expiryTime = std::chrono::milliseconds(3000) 
)

Removes a file from the device.

Sends a file removal request via Ethernet by attempting to read the file with a special prefix (fs-remove=). It verifies that the response content begins with the expected success message.

Parameters
filenameThe name of the file to be removed.
expiryTimeThe duration to wait for the socket operation (read/write) before timing out. Defaults to 3000 milliseconds.
Returns
true if the file was successfully removed, false otherwise.

◆ sendAndReceiveProcessData()

std::vector< uint8_t > EthernetDevice::sendAndReceiveProcessData ( const std::vector< uint8_t > &  data,
const std::chrono::steady_clock::duration  expiryTime = std::chrono::milliseconds(1000) 
)

Sends process data to a remote device and receives the response.

Sends a process data message over Ethernet to the remote device and waits for a corresponding response. This method increments the sequence ID, constructs and serializes the request message, transmits it via the socket, and parses the received response.

Parameters
dataThe payload data to transmit to the device.
expiryTimeThe duration to wait for the socket operation (read/write) before timing out. Defaults to 1000 milliseconds.
Returns
A vector of bytes containing the response data from the device. Returns an empty vector if the exchange fails (e.g., due to a non-OK status).

◆ setState()

bool EthernetDevice::setState ( uint8_t  state,
const std::chrono::steady_clock::duration  expiryTime = std::chrono::milliseconds(3000) 
)
overridevirtual

Sends a state control command to the Ethernet device and checks the response.

This method constructs a STATE_CONTROL type EthernetMessage with the given state, sends it over the socket, and waits for a response. It then parses the received message and returns true if the operation was acknowledged with OK status.

Parameters
stateThe new state to set on the remote device.
expiryTimeThe duration to wait for the socket operation (read/write) before timing out. Defaults to 3000 milliseconds.
Returns
true if the response status is OK, false otherwise.
Exceptions
boost::system::system_errorif sending or receiving fails.
std::runtime_errorif the response buffer is too small to parse.

Implements common::Device.

◆ triggerFirmwareUpdate()

bool EthernetDevice::triggerFirmwareUpdate ( const std::chrono::steady_clock::duration  expiryTime = std::chrono::milliseconds(2000))

Sends a firmware update request to the connected Integro device.

Constructs and transmits a firmware update request using the custom Ethernet communication protocol. Waits for the device's response and verifies whether the request was successfully acknowledged.

This request must be issued after uploading one or both of the following files:

  • app_firmware.bin: Contains the SoC firmware.
  • com_firmware.bin: Contains the communication chip firmware.
Parameters
expiryTimeThe duration to wait for the socket operation (read/write) before timing out. Defaults to 2000 milliseconds.
Returns
true if the device responds with an OK status; false otherwise.

◆ upload() [1/2]

common::Parameter & EthernetDevice::upload ( const uint16_t  index,
const uint8_t  subindex,
const std::chrono::steady_clock::duration  expiryTime = std::chrono::milliseconds(3000) 
)
overridevirtual

Uploads a parameter from the device via SDO and updates the local parameter store.

This function reads a parameter value from the device using SDO communication, updates the corresponding local Parameter object with the received value, and returns a reference to it.

Parameters
indexThe 16-bit parameter index in the object dictionary.
subindexThe 8-bit subindex of the parameter.
expiryTimeThe maximum duration to wait for the SDO upload operation to complete.
Returns
Reference to the updated local common::Parameter object.
Exceptions
std::runtime_errorIf the upload fails or returns an empty payload.

Implements common::Device.

◆ upload() [2/2]

template<typename T >
T EthernetDevice::upload ( uint16_t  index,
uint8_t  subindex,
const std::chrono::steady_clock::duration  expiryTime = std::chrono::milliseconds(5000) 
)
inlinevirtual

Uploads a parameter from the device and returns its value as the specified type.

This templated overload uploads a parameter using the given index and subindex, updates the corresponding parameter in the local store, and returns its value as type T.

Template Parameters
TThe type to extract from the uploaded parameter value.
Parameters
indexThe 16-bit index of the parameter in the object dictionary.
subindexThe 8-bit subindex of the parameter.
expiryTimeThe maximum duration to wait for the SDO upload operation. Defaults to 5000 milliseconds.
Returns
The uploaded parameter value cast to type T.
Exceptions
std::runtime_errorIf the SDO upload fails or the value cannot be extracted as type T.
std::bad_variant_accessIf the internal variant does not hold the requested type T.

Implements common::Device.

◆ writeFile()

bool EthernetDevice::writeFile ( const std::string &  filename,
const std::vector< uint8_t > &  data,
const std::chrono::steady_clock::duration  expiryTime = std::chrono::milliseconds(5000) 
)
overridevirtual

Sends a file in chunks to the remote server via Ethernet.

This function splits the file data into smaller chunks and sends them to the server in multiple segments. The file name is sent as the first segment, followed by the file data in subsequent chunks.

The server's response to each chunk is parsed, and the next chunk is sent until the entire file is transmitted.

Parameters
filenameThe name of the file to be written.
dataThe data to be written to the file.
expiryTimeThe duration to wait for the socket operation (read/write) before timing out. Defaults to 5000 milliseconds.
Returns
Returns true if the file was successfully written in chunks; otherwise, false.
Note
This method assumes that the server is expecting the file in segments and will respond accordingly.

Implements common::Device.

◆ writeSdo()

bool EthernetDevice::writeSdo ( uint16_t  index,
uint8_t  subindex,
const std::vector< uint8_t > &  data,
const std::chrono::steady_clock::duration  expiryTime = std::chrono::milliseconds(1000) 
)

Writes an SDO (Service Data Object) to an Ethernet device.

This function sends a request to write an SDO to a specified index and subindex, along with the provided data. It then receives and parses the response. If the response indicates an error, it logs an error message and returns false. Otherwise, it returns true to indicate that the write operation was successful.

Parameters
indexThe index of the SDO to write.
subindexThe subindex of the SDO to write.
valueA vector containing the data to be written to the SDO.
expiryTimeThe duration to wait for the socket operation (read/write) before timing out. Defaults to 1000 milliseconds.
Returns
true if the write operation was successful, false otherwise.
Exceptions
boost::system::system_errorif sending or receiving fails.
std::runtime_errorif the response buffer is too small to parse.

The documentation for this class was generated from the following files: