11#include <nlohmann/json.hpp>
16#include <unordered_map>
43 static_assert(std::is_unsigned<T>::value,
44 "parseHex only supports unsigned integer types");
47 if constexpr (
sizeof(T) <=
sizeof(
unsigned long)) {
49 return static_cast<T
>(std::stoul(s,
nullptr, 16));
52 return static_cast<T
>(std::stoull(s,
nullptr, 16));
66 std::map<std::uint16_t, std::vector<std::uint32_t>>
rx;
67 std::map<std::uint16_t, std::vector<std::uint32_t>>
tx;
131 return lhs ==
static_cast<uint32_t
>(rhs);
146 return static_cast<uint32_t
>(lhs) == rhs;
238 return static_cast<ObjectFlags>(
static_cast<uint16_t
>(lhs) |
239 static_cast<uint16_t
>(rhs));
253 return static_cast<ObjectFlags>(
static_cast<uint16_t
>(lhs) &
254 static_cast<uint16_t
>(rhs));
287 return b | s | p | a;
445 std::variant<bool, std::int8_t, std::int16_t, std::int32_t, std::int64_t,
446 std::uint8_t, std::uint16_t, std::uint32_t, std::uint64_t,
447 float, double, std::string,
448 std::vector<std::uint8_t>>;
525 std::vector<std::uint8_t>
data;
576 template <
typename T>
582 if (
auto ptr = std::get_if<T>(&val)) {
585 throw std::bad_variant_access();
604 template <
typename T>
607 if (
auto ptr = std::get_if<T>(&val)) {
652 template <
typename T>
675 template <
typename T>
677 auto expectedType = [
this]() -> std::type_index {
683 return typeid(std::int8_t);
686 return typeid(std::int16_t);
690 return typeid(std::int32_t);
693 return typeid(std::int64_t);
700 return typeid(std::uint8_t);
703 return typeid(std::uint16_t);
707 return typeid(std::uint32_t);
710 return typeid(std::uint64_t);
713 return typeid(float);
716 return typeid(double);
721 return typeid(std::string);
729 const std::type_index expected = expectedType();
730 if (expected !=
typeid(T) &&
731 typeid(T) !=
typeid(std::vector<std::uint8_t>)) {
848 Parameter>& parametersMap,
849 bool sortParameters =
true);
887 const std::chrono::steady_clock::duration expiryTime =
888 std::chrono::milliseconds(3000)) = 0;
903 const std::chrono::steady_clock::duration expiryTime =
904 std::chrono::milliseconds(3000)) = 0;
917 const std::string& filename,
918 const std::chrono::steady_clock::duration expiryTime =
919 std::chrono::milliseconds(5000)) = 0;
933 const std::vector<uint8_t>& data,
934 const std::chrono::steady_clock::duration expiryTime =
935 std::chrono::milliseconds(5000)) = 0;
946 bool readValues,
const std::chrono::steady_clock::duration expiryTime =
947 std::chrono::milliseconds(9000)) = 0;
992 uint16_t
index, uint8_t subindex,
993 const std::chrono::steady_clock::duration expiryTime =
994 std::chrono::milliseconds(5000)) = 0;
1012 const std::chrono::steady_clock::duration expiryTime =
1013 std::chrono::milliseconds(5000)) = 0;
1034 const std::chrono::steady_clock::duration expiryTime =
1035 std::chrono::milliseconds(5000)) = 0;
1068template <
typename T>
1070 static_assert(std::is_arithmetic_v<T>,
"T must be an arithmetic type");
1072 auto [ptr, ec] = std::from_chars(sv.data(), sv.data() + sv.size(), result);
1073 return ec == std::errc();
1079#include <type_traits>
1101template <
typename T>
1103 static_assert(std::is_trivially_copyable<T>::value,
1104 "T must be trivially copyable");
1106 std::vector<uint8_t> bytes(
sizeof(T));
1107 uint8_t* bytePtr =
reinterpret_cast<uint8_t*
>(&
value);
1109 for (
size_t i = 0; i <
sizeof(T); ++i) {
1110 bytes[i] = bytePtr[i];
1114 std::reverse(bytes.begin(), bytes.end());
1135 std::stringstream oss;
1137 oss <<
"0x" << std::setw(4) << std::setfill(
'0') << std::hex << std::uppercase
1138 <<
index <<
":" << std::setw(2) << std::setfill(
'0') << std::hex
1139 << std::uppercase << subindex;
1159 std::ostringstream oss;
1160 for (uint8_t
byte : data) {
1161 oss <<
"0x" << std::hex << std::setw(2) << std::setfill(
'0')
1162 <<
static_cast<int>(byte) <<
" ";
1193std::string
joinStrings(
const std::vector<std::string>& list,
1194 const std::string& delimiter);
1226struct hash<
std::pair<uint16_t, uint8_t>> {
1238 return hash<uint16_t>()(p.first) ^ (hash<uint8_t>()(p.second) << 1);
Abstract interface representing a generic device.
Definition: common.h:858
virtual bool writeFile(const std::string &filename, const std::vector< uint8_t > &data, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(5000))=0
Sends a file to the device.
virtual void download(uint16_t index, uint8_t subindex, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(5000))=0
Downloads a parameter to the device using SDO communication.
virtual Parameter & findParameter(uint16_t index, uint8_t subindex)=0
Finds and returns a reference to a parameter by its index and subindex.
virtual void download(uint16_t index, uint8_t subindex, const ParameterValue &value, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(5000))=0
Sets a parameter value in the local store and downloads it to the device.
virtual std::vector< uint8_t > readFile(const std::string &filename, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(5000))=0
Reads the contents of a file from the device.
virtual void exchangeProcessDataAndUpdateParameters()=0
Exchanges process data with the remote device and updates local parameters accordingly.
virtual bool setState(uint8_t state, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(3000))=0
Sets the device to a specified state.
virtual void loadParameters(bool readValues, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(9000))=0
Loads parameters from the device and stores them locally.
virtual ~Device()=default
Virtual destructor for Device interface.
virtual void clearParameters()=0
Clears all loaded object dictionary parameters.
virtual uint8_t getState(const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(3000))=0
Reads and returns the current state of the device.
virtual Parameter & upload(uint16_t index, uint8_t subindex, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(5000))=0
Uploads a parameter from the device via SDO and updates the local parameter store.
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
std::string name
Name of the parameter.
Definition: common.h:477
bool trySetValue(const T &value)
Attempts to set the parameter value with a strongly-typed input.
Definition: common.h:676
std::uint16_t bitLength
Bit length of the parameter.
Definition: common.h:492
common::ObjectFlags flags
The object flags for the parameter, defined by common::ObjectFlags.
Definition: common.h:514
void setValue(const ParameterValue &value)
Sets the internal raw data representation from a given value.
Definition: common.cc:118
std::optional< T > tryGetValue() const
Attempts to retrieve the parameter value from the ParameterValue variant as the specified type.
Definition: common.h:605
void setValue(const T &value)
Sets the parameter value using a strongly-typed input.
Definition: common.h:653
T getValue() const
Retrieves the parameter value from the ParameterValue variant as the specified type.
Definition: common.h:577
bool operator<(const Parameter &other) const
Compares two Parameter objects for ordering.
Definition: common.h:749
common::ObjectCode code
The object code for the parameter, defined by common::ObjectCode.
Definition: common.h:508
int byteLength
Byte length of the parameter.
Definition: common.h:497
static void from_json(const nlohmann::json &j, Parameter &p)
Deserializes a Parameter object from JSON.
Definition: common.cc:208
common::ObjectFlags access
The access flags for the parameter, defined by common::ObjectFlags.
Definition: common.h:520
bool operator==(const Parameter &other) const
Checks if two Parameter objects are equal.
Definition: common.h:785
bool operator>(const Parameter &other) const
Compares two Parameter objects for reverse ordering.
Definition: common.h:767
std::uint8_t subindex
Subindex of the parameter.
Definition: common.h:487
std::vector< std::uint8_t > data
Holds raw data as a vector of uint8_t elements.
Definition: common.h:525
std::uint16_t index
Index of the parameter.
Definition: common.h:482
static void to_json(nlohmann::json &j, const Parameter &p)
Serializes a Parameter object to JSON.
Definition: common.cc:197
common::ObjectDataType dataType
The data type of the parameter, defined by common::ObjectDataType.
Definition: common.h:503
uint8_t * value
Definition: co_dictionary.h:9
uint16_t index
Definition: co_dictionary.h:0
std::string joinStrings(const std::vector< std::string > &list, const std::string &delimiter)
Joins a list of strings into a single string with a delimiter.
Definition: common.cc:278
std::vector< uint8_t > readBinaryFile(const std::string &filename)
Reads the contents of a binary file into a vector of uint8_t.
Definition: common.cc:265
T parseHex(const std::string &s)
Parses a hexadecimal string into an unsigned integer of type T.
Definition: common.h:42
std::string makeParameterId(int index, int subindex)
Formats the given index and subindex into a parameter identifier string.
Definition: common.h:1134
EtherCATVendorID
Enum class representing EtherCAT vendor IDs.
Definition: common.h:110
@ SYNAPTICON
Vendor ID for Synapticon GmbH.
ObjectFlags operator&(ObjectFlags lhs, ObjectFlags rhs)
Bitwise AND operator for ObjectFlags enum class.
Definition: common.h:252
ObjectCode
Enum class representing different object types used in the EtherCAT protocol.
Definition: common.h:404
@ DEFSTRUCT
Definition structure object.
@ DEFTYPE
Definition type object.
@ ARRAY
Array object type.
@ VAR
Variable object type.
std::pair< uint16_t, uint8_t > ParameterKey
Alias for a pair of uint16_t and uint8_t representing a parameter key.
Definition: common.h:419
void logParametersMap(const std::unordered_map< std::pair< uint16_t, uint8_t >, Parameter > ¶metersMap, bool sortParameters)
Logs the contents of a map of parameters with an option to sort them.
Definition: common.cc:238
std::string convertParameterValueToString(const ParameterValue &value)
Converts a ParameterValue to a string representation.
Definition: common.cc:219
std::string formatMacAddress(const std::string &originalMacAddress)
Formats a MAC address string to ensure each component is two digits and uppercase.
Definition: common.cc:293
std::vector< uint8_t > toBytes(T value, bool bigEndian=false)
Converts a value of type T to a vector of bytes (uint8_t) in the specified byte order.
Definition: common.h:1102
void from_json(const nlohmann::json &j, UiPdoMapping &p)
Deserialize a JSON object into a UiPdoMapping structure.
Definition: common.cc:7
bool stringViewToNumber(std::string_view sv, T &result)
Converts a string_view to a numeric type.
Definition: common.h:1069
ObjectFlags
Enum class representing various object flags.
Definition: common.h:194
ObjectFlags operator|(ObjectFlags lhs, ObjectFlags rhs)
Bitwise OR operator for ObjectFlags enum class.
Definition: common.h:237
bool operator==(uint32_t lhs, EtherCATVendorID rhs)
Compares a uint32_t value with an EtherCATVendorID enum value.
Definition: common.h:130
ObjectDataType
Enumerates the supported data types for parameters.
Definition: common.h:302
@ UNSIGNED40
40-bit unsigned integer.
@ INTEGER16
16-bit signed integer.
@ BACKUP_PARAMETER
For BACKUP_PARAMETER definition see ETG.1020.
@ UNSPECIFIED
Undefined or unknown data type.
@ INTEGER64
64-bit signed integer.
@ SM_SYNCHRONIZATION
For SM_SYNCHRONIZATION definition see ETG.1000.
@ ERROR_SETTING
For ERROR_SETTING definition see ETG.1020.
@ INTEGER32
32-bit signed integer.
@ ARRAY_OF_INT
Sequence of INT.
@ TIME_OF_DAY
Time of day format (implementation-defined).
@ REAL32
32-bit floating-point number.
@ UNSIGNED56
56-bit unsigned integer.
@ UNSIGNED8
8-bit unsigned integer.
@ MODULAR_DEVICE_PARAMETER
@ BITARR8
Array of 8 bits.
@ BITARR32
Array of 32 bits.
@ UNSIGNED64
64-bit unsigned integer.
@ REAL64
64-bit floating-point number (double).
@ GUID
128-bit globally unique identifier.
@ INTEGER40
40-bit signed integer.
@ PDO_PARAMETER
For PDO_PARAMETER definition see ETG.1020.
@ ENUM
For ENUM definition see ETG.1020.
@ UNSIGNED48
48-bit unsigned integer.
@ DWORD
Four octets (32 bits) unsigned integer.
@ UTYPE_END
End of user-defined type range.
@ ARRAY_OF_SINT
Sequence of SINT.
@ UNSIGNED16
16-bit unsigned integer.
@ INTEGER48
48-bit signed integer.
@ ARRAY_OF_UDINT
Sequence of UDINT.
@ INTEGER56
56-bit signed integer.
@ BITARR16
Array of 16 bits.
@ VISIBLE_STRING
Null-terminated ASCII string.
@ WORD
Two octets (16 bits) unsigned integer.
@ BOOLEAN
Boolean value (true or false).
@ UNSIGNED32
32-bit unsigned integer.
@ DIAGNOSIS_HISTORY
For DIAGNOSIS_HISTORY definition see ETG.1020.
@ INTEGER24
24-bit signed integer.
@ ARRAY_OF_DINT
Sequence of DINT.
@ COMMAND_PAR
For COMMAND_PAR definition see ETG.1000.
@ IDENTITY
For IDENTITY definition see ETG.1000.
@ TIME_DIFFERENCE
Time difference format (implementation-defined).
@ BYTE
8-bit unsigned integer.
@ PDO_MAPPING
For PDO_MAPPING definition see ETG.1000.
@ UNSIGNED24
24-bit unsigned integer.
@ OCTET_STRING
Array of raw bytes.
@ INTEGER8
8-bit signed integer.
@ UTYPE_START
Start of user-defined type range.
ObjectFlags SetObjectFlags(ObjectFlags b, ObjectFlags s, ObjectFlags p, ObjectFlags a)
Combine multiple ObjectFlags values into one.
Definition: common.h:285
std::string bytesToHexString(const std::vector< uint8_t > &data)
Converts a vector of bytes into a space-separated hexadecimal string.
Definition: common.h:1158
ObjectFlags & operator|=(ObjectFlags &lhs, ObjectFlags rhs)
Bitwise OR assignment operator for ObjectFlags enum class.
Definition: common.h:267
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
Specialization of std::hash for std::pair<uint16_t, uint8_t>.
Definition: common.h:1224
Represents a single PDO (Process Data Object) mapping entry.
Definition: common.h:158
uint16_t pdoIndex
The PDO index (e.g., 0x1600 or 0x1A00)
Definition: common.h:159
uint8_t subindex
The subindex of the object (e.g., 0x00)
Definition: common.h:161
uint16_t index
The object dictionary index (e.g., 0x607A)
Definition: common.h:160
uint8_t bitlength
The size of the mapped entry in bits (e.g., 16, 32, etc.)
Definition: common.h:163
Represents the mapped PDO entries for a slave device.
Definition: common.h:180
std::vector< PdoMappingEntry > txPdos
Definition: common.h:183
std::vector< PdoMappingEntry > rxPdos
Definition: common.h:181
Top-level UI configuration JSON structure.
Definition: common.h:75
UiPdoMapping pdoMapping
Definition: common.h:76
Represents the PDO mapping for RX and TX channels.
Definition: common.h:65
std::map< std::uint16_t, std::vector< std::uint32_t > > tx
Definition: common.h:67
std::map< std::uint16_t, std::vector< std::uint32_t > > rx
Definition: common.h:66
size_t operator()(const std::pair< uint16_t, uint8_t > &p) const
Computes a hash for a given std::pair<uint16_t, uint8_t>.
Definition: common.h:1237