Motion Master
Loading...
Searching...
No Matches
ethernet_client.h
Go to the documentation of this file.
1#pragma once
2
3#include <boost/asio.hpp>
4#include <chrono>
5#include <string>
6
7#include "common.h"
8
17enum class EthernetMessageType : uint8_t {
18 SDO_READ = 0x01,
19 SDO_WRITE = 0x02,
21 0x03,
22 PDO_CONTROL = 0x04,
23 PDO_MAP = 0x05,
24 FIRMWARE_UPDATE = 0x0B,
25 FILE_READ = 0x0C,
26 FILE_WRITE = 0x0D,
28 0x0E,
29 STATE_READ = 0x0F,
30 // PARAM_LIST = 0x10, ///< Request a list of parameters.
31 // PARAM_DESC = 0x11, ///< Request a description of a parameter.
32 // PARAM_SUB_DESC =
33 // 0x12, ///< Request a description of a parameter's sub-entries.
34 PARAM_FULL_LIST = 0x13,
35 SERVER_INFO = 0x20
36};
37
46enum class EthernetMessageStatus : uint8_t {
47 OK = 0x00,
48 FIRST = 0x80,
49 MIDDLE = 0xC0,
50 LAST = 0x40,
51 ERR = 0x28
52};
53
60enum class EthernetSqiReplyStatus : uint8_t {
61 BSY = 0x28,
62 ACK = 0x58,
63 ERR = 0x63
64};
65
76 static constexpr size_t kHeaderSize = 7;
77
79 static constexpr size_t kBufferSize = 1500 - kHeaderSize;
80
86
92 uint16_t id;
93
100
110
116 uint16_t size;
117
123 std::vector<uint8_t> data;
124};
125
144EthernetMessage parseEthernetMessage(const std::vector<uint8_t>& buffer);
145
159std::vector<uint8_t> serializeEthernetMessage(const EthernetMessage& message);
160
171 public:
182 EthernetDevice(const std::string& ip, unsigned short port);
183
190
203 uint16_t incrementSeqId();
204
215 bool connect();
216
225 bool isConnected();
226
237 bool disconnect();
238
258 const EthernetMessage& request,
259 const std::chrono::steady_clock::duration expiryTime);
260
287 uint8_t getState(const std::chrono::steady_clock::duration expiryTime =
288 std::chrono::milliseconds(3000)) override;
289
308 bool setState(uint8_t state,
309 const std::chrono::steady_clock::duration expiryTime =
310 std::chrono::milliseconds(3000)) override;
311
330 std::vector<uint8_t> readFile(
331 const std::string& filename,
332 const std::chrono::steady_clock::duration expiryTime =
333 std::chrono::milliseconds(5000)) override;
334
351 std::vector<std::string> readFileList(
352 const bool stripSizeSuffix = true,
353 const std::chrono::steady_clock::duration expiryTime =
354 std::chrono::milliseconds(3000));
355
369 bool removeFile(const std::string& filename,
370 const std::chrono::steady_clock::duration expiryTime =
371 std::chrono::milliseconds(3000));
372
394 bool writeFile(const std::string& filename, const std::vector<uint8_t>& data,
395 const std::chrono::steady_clock::duration expiryTime =
396 std::chrono::milliseconds(5000)) override;
397
415 bool triggerFirmwareUpdate(const std::chrono::steady_clock::duration
416 expiryTime = std::chrono::milliseconds(2000));
417
441 std::vector<common::Parameter> getParameters(
442 bool readValues = false,
443 const std::chrono::steady_clock::duration expiryTime =
444 std::chrono::milliseconds(1000));
445
465 std::vector<uint8_t> readSdo(
466 uint16_t index, uint8_t subindex,
467 const std::chrono::steady_clock::duration expiryTime =
468 std::chrono::milliseconds(1000));
469
490 bool writeSdo(uint16_t index, uint8_t subindex,
491 const std::vector<uint8_t>& data,
492 const std::chrono::steady_clock::duration expiryTime =
493 std::chrono::milliseconds(1000));
494
507 void loadParameters(bool readValues = false,
508 const std::chrono::steady_clock::duration expiryTime =
509 std::chrono::milliseconds(9000)) override;
510
511 void clearParameters() override;
512
513 common::Parameter& findParameter(uint16_t index, uint8_t subindex) override;
514
516 const uint16_t index, const uint8_t subindex,
517 const std::chrono::steady_clock::duration expiryTime =
518 std::chrono::milliseconds(3000)) override;
519
541 template <typename T>
542 T upload(uint16_t index, uint8_t subindex,
543 const std::chrono::steady_clock::duration expiryTime =
544 std::chrono::milliseconds(5000)) {
545 common::Parameter& parameter = upload(index, subindex, expiryTime);
546 return parameter.getValue<T>();
547 }
548
549 void download(uint16_t index, uint8_t subindex,
550 const std::chrono::steady_clock::duration expiryTime =
551 std::chrono::milliseconds(5000)) override;
552
553 void download(uint16_t index, uint8_t subindex,
555 const std::chrono::steady_clock::duration expiryTime =
556 std::chrono::milliseconds(5000)) override;
557
576 template <typename T>
577 void download(uint16_t index, uint8_t subindex, const T& value,
578 const std::chrono::steady_clock::duration expiryTime =
579 std::chrono::milliseconds(5000)) {
581 }
582
599 std::vector<uint8_t> sendAndReceiveProcessData(
600 const std::vector<uint8_t>& data,
601 const std::chrono::steady_clock::duration expiryTime =
602 std::chrono::milliseconds(1000));
603
623
624 private:
625 boost::asio::io_context ioContext_;
627 boost::asio::ip::tcp::endpoint
628 endpoint_;
630 boost::asio::ip::tcp::socket
631 socket_;
632
633 std::mutex mutex_;
635
643 std::atomic<uint16_t> seqId_{0};
644
645 std::unordered_map<common::ParameterKey, common::Parameter>
646 parametersMap_;
648 common::PdoMappings pdoMappings_;
649};
Handles TCP communication with SOMANET devices over Ethernet.
Definition: ethernet_client.h:170
std::vector< common::Parameter > getParameters(bool readValues=false, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(1000))
Definition: ethernet_client.cc:398
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.
Definition: ethernet_client.cc:656
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.
Definition: ethernet_client.cc:141
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.
Definition: ethernet_client.cc:208
bool connect()
Establishes a connection to the remote server.
Definition: ethernet_client.cc:112
bool removeFile(const std::string &filename, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(3000))
Removes a file from the device.
Definition: ethernet_client.cc:321
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.
Definition: ethernet_client.cc:335
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.
Definition: ethernet_client.cc:244
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.
Definition: ethernet_client.h:577
bool isConnected()
Checks if the Ethernet socket is currently open.
Definition: ethernet_client.cc:123
common::Parameter & upload(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.
Definition: ethernet_client.cc:611
uint16_t incrementSeqId()
Increments the sequence ID atomically and wraps it around at the maximum value.
Definition: ethernet_client.cc:107
bool disconnect()
Closes the Ethernet socket connection.
Definition: ethernet_client.cc:125
void exchangeProcessDataAndUpdateParameters() override
Exchanges process data with the remote device and updates local parameters accordingly.
Definition: ethernet_client.cc:680
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.
Definition: ethernet_client.h:542
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.
Definition: ethernet_client.cc:289
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.
Definition: ethernet_client.cc:228
bool triggerFirmwareUpdate(const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(2000))
Sends a firmware update request to the connected Integro device.
Definition: ethernet_client.cc:382
common::Parameter & findParameter(uint16_t index, uint8_t subindex) override
Finds and returns a reference to a parameter by its index and subindex.
Definition: ethernet_client.cc:599
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.
Definition: ethernet_client.cc:525
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.
Definition: ethernet_client.cc:553
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.
Definition: ethernet_client.cc:588
void clearParameters() override
Clears all loaded object dictionary parameters.
Definition: ethernet_client.cc:597
~EthernetDevice()
Destructor for the EthernetDevice class.
Definition: ethernet_client.cc:101
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.
Definition: ethernet_client.cc:629
Abstract interface representing a generic device.
Definition: common.h:858
Represents a device parameter identified by index and subindex.
Definition: common.h:472
ParameterValue getValue() const
Retrieves the value of the parameter based on its data type.
Definition: common.cc:31
uint8_t * value
Definition: co_dictionary.h:9
uint16_t index
Definition: co_dictionary.h:0
std::vector< uint8_t > serializeEthernetMessage(const EthernetMessage &message)
Serializes an EthernetMessage object into a byte buffer.
Definition: ethernet_client.cc:37
EthernetMessage parseEthernetMessage(const std::vector< uint8_t > &buffer)
Parses a raw Ethernet message buffer into a structured EthernetMessage.
Definition: ethernet_client.cc:9
EthernetMessageType
Enumeration of Ethernet message types used in the protocol.
Definition: ethernet_client.h:17
@ PARAM_FULL_LIST
Request a full list of parameters.
@ PDO_MAP
Map PDO entries or configurations.
@ SERVER_INFO
Request information about the server or device.
@ STATE_CONTROL
Control the state of the device (e.g., INIT, PREOP).
@ FILE_READ
Read a file from the device.
@ STATE_READ
Read the current state of the device.
@ SDO_WRITE
Write a value to a Service Data Object (SDO).
@ SDO_READ
Read a Service Data Object (SDO) value.
@ FIRMWARE_UPDATE
Perform firmware update operation.
@ FILE_WRITE
Write a file to the device.
@ PDO_CONTROL
Control or configure PDO behavior.
@ PDO_RXTX_FRAME
Transmit or receive a Process Data Object (PDO) frame.
EthernetMessageStatus
Represents the status of an Ethernet message during transmission or processing.
Definition: ethernet_client.h:46
@ FIRST
First segment of a multi-part message.
@ MIDDLE
Middle segment of a multi-part message.
@ ERR
Message contains an error or failed validation.
@ OK
Message is complete and valid.
@ LAST
Last segment of a multi-part message.
EthernetSqiReplyStatus
Enum class representing the status codes for an Ethernet SQI reply.
Definition: ethernet_client.h:60
@ ACK
Acknowledgment received.
@ BSY
Device is busy and cannot process the request.
std::variant< bool, std::int8_t, std::int16_t, std::int32_t, std::int64_t, std::uint8_t, std::uint16_t, std::uint32_t, std::uint64_t, float, double, std::string, std::vector< std::uint8_t > > ParameterValue
Definition: common.h:449
Structure representing a parsed Ethernet response message.
Definition: ethernet_client.h:74
std::vector< uint8_t > data
The payload data of the response message.
Definition: ethernet_client.h:123
EthernetSqiReplyStatus sqiStatus
The status of the SQI reply in the response message.
Definition: ethernet_client.h:109
EthernetMessageStatus status
The status of the response message.
Definition: ethernet_client.h:99
static constexpr size_t kBufferSize
Definition: ethernet_client.h:79
EthernetMessageType type
The type of the response message.
Definition: ethernet_client.h:85
uint16_t id
The sequence ID of the response message.
Definition: ethernet_client.h:92
uint16_t size
The size of the buffer in the response message.
Definition: ethernet_client.h:116
static constexpr size_t kHeaderSize
Definition: ethernet_client.h:76
Represents the mapped PDO entries for a slave device.
Definition: common.h:180