Motion Master
Loading...
Searching...
No Matches
base.h
Go to the documentation of this file.
1#pragma once
2
3#include <condition_variable>
4#include <map>
5#include <nlohmann/json.hpp>
6#include <optional>
7#include <set>
8#include <sstream>
9#include <string>
10#include <typeindex>
11#include <variant>
12#include <vector>
13
14#include "core/util.h"
15
16namespace mm::comm::base {
27 std::map<std::uint16_t, std::vector<std::uint32_t>> rx;
28 std::map<std::uint16_t, std::vector<std::uint32_t>> tx;
29};
30
38};
39
51void from_json(const nlohmann::json& j, UiPdoMapping& p);
52
62void from_json(const nlohmann::json& j, UiConfigJson& r);
63
71enum class EtherCATVendorID : uint32_t {
77 SYNAPTICON = 0x000022d2,
78};
79
80inline constexpr std::size_t kDeviceFilePartSize =
81 9000;
83
95inline bool operator==(uint32_t lhs, EtherCATVendorID rhs) {
96 return lhs == static_cast<uint32_t>(rhs);
97}
98
110inline bool operator==(EtherCATVendorID lhs, uint32_t rhs) {
111 return static_cast<uint32_t>(lhs) == rhs;
112}
113
124 uint16_t pdoIndex;
125 uint16_t index;
126 uint8_t subindex;
127 uint8_t
129};
130
146 std::vector<PdoMappingEntry> rxPdos;
148 std::vector<PdoMappingEntry> txPdos;
150};
151
159enum class ObjectFlags : uint16_t {
160 None = 0x0000,
161
162 // Read access
163 PO_RD = 0x0001,
164 SO_RD = 0x0002,
165 OP_RD = 0x0004,
166 ALL_RD = PO_RD | SO_RD | OP_RD,
167
168 // Write access
169 PO_WR = 0x0008,
170 SO_WR = 0x0010,
171 OP_WR = 0x0020,
172 ALL_WR = PO_WR | SO_WR | OP_WR,
173
174 // Read/Write combinations
175 PO_RDWR = PO_RD | PO_WR,
176 SO_RDWR = SO_RD | SO_WR,
177 OP_RDWR = OP_RD | OP_WR,
179
180 // Mapping
181 RXPDO_MAP = 0x0040,
182 TXPDO_MAP = 0x0080,
183 RXTXPDO_MAP = 0x00C0,
184
185 // Other flags
186 BACKUP = 0x0100,
187 STARTUP = 0x0200,
188
190};
191
203 return static_cast<ObjectFlags>(static_cast<uint16_t>(lhs) |
204 static_cast<uint16_t>(rhs));
205}
206
218 return static_cast<ObjectFlags>(static_cast<uint16_t>(lhs) &
219 static_cast<uint16_t>(rhs));
220}
221
233 lhs = lhs | rhs;
234 return lhs;
235}
236
251 ObjectFlags a) {
252 return b | s | p | a;
253}
254
267enum class ObjectDataType : uint16_t {
268 UNSPECIFIED = 0x0000,
269
270 BOOLEAN = 0x0001,
271 BYTE = 0x001E,
272 WORD = 0x001F,
273 DWORD = 0x0020,
274
275 BIT1 = 0x0030,
276 BIT2 = 0x0031,
277 BIT3 = 0x0032,
278 BIT4 = 0x0033,
279 BIT5 = 0x0034,
280 BIT6 = 0x0035,
281 BIT7 = 0x0036,
282 BIT8 = 0x0037,
283 BIT9 = 0x0038,
284 BIT10 = 0x0039,
285 BIT11 = 0x003A,
286 BIT12 = 0x003B,
287 BIT13 = 0x003C,
288 BIT14 = 0x003D,
289 BIT15 = 0x003E,
290 BIT16 = 0x003F,
291
292 BITARR8 = 0x002D,
293 BITARR16 = 0x002E,
294 BITARR32 = 0x002F,
295
296 INTEGER8 = 0x0002,
297 INTEGER16 = 0x0003,
298 INTEGER24 = 0x0010,
299 INTEGER32 = 0x0004,
300 INTEGER40 = 0x0012,
301 INTEGER48 = 0x0013,
302 INTEGER56 = 0x0014,
303 INTEGER64 = 0x0015,
304
305 UNSIGNED8 = 0x0005,
306 UNSIGNED16 = 0x0006,
307 UNSIGNED24 = 0x0016,
308 UNSIGNED32 = 0x0007,
309 UNSIGNED40 = 0x0018,
310 UNSIGNED48 = 0x0019,
311 UNSIGNED56 = 0x001A,
312 UNSIGNED64 = 0x001B,
313
314 REAL32 = 0x0008,
315 REAL64 = 0x0011,
316
317 GUID = 0x001D,
318
319 VISIBLE_STRING = 0x0009,
320 OCTET_STRING = 0x000A,
321 UNICODE_STRING = 0x000B,
323 ARRAY_OF_INT = 0x0260,
324 ARRAY_OF_SINT = 0x0261,
325 ARRAY_OF_DINT = 0x0262,
326 ARRAY_OF_UDINT = 0x0263,
327
328 PDO_MAPPING = 0x0021,
329 IDENTITY = 0x0023,
330 COMMAND_PAR = 0x0025,
331 PDO_PARAMETER = 0x0027,
332 ENUM = 0x0028,
334 0x0029,
335 RECORD = 0x002A,
337 BACKUP_PARAMETER = 0x002B,
338 MODULAR_DEVICE_PARAMETER = 0x002C,
340 ERROR_SETTING = 0x0281,
342 0x0282,
343 EXTERNAL_SYNC_STATUS = 0x0283,
345 EXTERNAL_SYNC_SETTINGS = 0x0284,
347 DEFTYPE_FSOEFRAME = 0x0285,
350 0x0286,
352
353 TIME_OF_DAY = 0x000C,
355 0x000D,
356 UTYPE_START = 0x0800,
357 UTYPE_END = 0x0FFF
358};
359
369enum class ObjectCode : uint16_t {
370 DEFTYPE = 0x0005,
371 DEFSTRUCT = 0x0006,
372 VAR = 0x0007,
373 ARRAY = 0x0008,
374 RECORD = 0x0009,
375};
376
401 std::variant<bool, std::int8_t, std::int16_t, std::int32_t, std::int64_t,
402 std::uint8_t, std::uint16_t, std::uint32_t, std::uint64_t,
403 float, double, std::string,
404 std::vector<std::uint8_t>>;
406
414 uint16_t index;
415 uint8_t subindex;
417};
418
426 std::vector<MappedParameterValue> rx;
427 std::vector<MappedParameterValue> tx;
428};
429
437 NOT_MAPPED,
440};
441
464 public:
473 using Address = std::pair<uint16_t, uint8_t>;
474
479 std::string group;
480
484 std::string name;
485
489 std::uint16_t index;
490
494 std::uint8_t subindex;
495
499 std::uint16_t bitLength;
500
505
510
515
520
525
533
537 std::vector<std::uint8_t> data;
538
569 ParameterValue getValue() const;
570
588 template <typename T>
589 T getValue() const {
590 // Get the value as a ParameterValue
591 const ParameterValue& val = getValue();
592
593 // Try to get the value of type T from the variant
594 if (auto ptr = std::get_if<T>(&val)) {
595 return *ptr; // Return the value of type T
596 } else {
597 throw std::bad_variant_access(); // If type T is not found in the variant
598 }
599 }
600
616 template <typename T>
617 std::optional<T> tryGetValue() const {
618 const ParameterValue& val = getValue();
619 if (auto ptr = std::get_if<T>(&val)) {
620 return *ptr;
621 } else {
622 return std::nullopt;
623 }
624 }
625
637 template <typename T>
639 return mm::core::util::toInteger<T>(data, 0,
641 }
642
663 void setValue(const ParameterValue& value);
664
681 template <typename T>
682 void setValue(const T& value) {
683 // Convert the input value to ParameterValue (variant)
684 ParameterValue parameterValue(value);
685
686 // Reuse the existing setValue function to handle the assignment
687 setValue(parameterValue);
688 }
689
704 template <typename T>
705 bool trySetValue(const T& value) {
706 auto expectedType = [this]() -> std::type_index {
707 switch (dataType) {
709 return typeid(bool);
710 }
712 return typeid(std::int8_t);
713 }
715 return typeid(std::int16_t);
716 }
719 return typeid(std::int32_t);
720 }
722 return typeid(std::int64_t);
723 }
729 return typeid(std::uint8_t);
730 }
732 return typeid(std::uint16_t);
733 }
736 return typeid(std::uint32_t);
737 }
739 return typeid(std::uint64_t);
740 }
742 return typeid(float);
743 }
745 return typeid(double);
746 }
750 return typeid(std::string);
751 }
752 default: {
753 return typeid(void);
754 }
755 }
756 };
757
758 const std::type_index expected = expectedType();
759 if (expected != typeid(T) &&
760 typeid(T) != typeid(std::vector<std::uint8_t>)) {
761 return false;
762 }
763
765 return true;
766 }
767
776 bool hasAllRead() const;
777
786 bool hasAllWrite() const;
787
798 bool operator<(const Parameter& other) const {
799 if (index != other.index) {
800 return index < other.index; // Compare by index first
801 }
802 return subindex <
803 other.subindex; // Compare by subindex if indices are equal
804 }
805
816 bool operator>(const Parameter& other) const {
817 if (index != other.index) {
818 return index > other.index; // Compare by index first
819 }
820 return subindex >
821 other.subindex; // Compare by subindex if indices are equal
822 }
823
834 bool operator==(const Parameter& other) const {
835 return index == other.index && subindex == other.subindex;
836 }
837
848 static void to_json(nlohmann::json& j, const Parameter& p);
849
860 static void from_json(const nlohmann::json& j, Parameter& p);
861};
862
876
891void assignGroupsToParameters(std::vector<Parameter>& parameters);
892
913 const std::unordered_map<Parameter::Address, Parameter>& parametersMap,
914 bool sortParameters = true);
915
928inline bool isAppBinFile(const std::string& filename) {
929 return filename.rfind("app_", 0) == 0 && filename.size() >= 4 &&
930 filename.compare(filename.size() - 4, 4, ".bin") == 0;
931}
932
945inline bool isComBinFile(const std::string& filename) {
946 return filename.rfind("com_", 0) == 0 && filename.size() >= 4 &&
947 filename.compare(filename.size() - 4, 4, ".bin") == 0;
948}
949
959inline bool isFirmwareBinFile(const std::string& filename) {
960 return isAppBinFile(filename) || isComBinFile(filename);
961}
962
967enum class DeviceFileError : uint8_t {
968 None = 0x00,
969 NotFound = 0x01,
970 AccessDenied = 0x02,
971 StorageFull = 0x03,
972 IllegalRequest = 0x04,
973 PacketNumber = 0x05,
974 AlreadyExists = 0x06,
975 NoUser = 0x07,
976 BootstrapOnly = 0x08,
977 NotBootstrap = 0x09,
978 NoRights = 0x0A,
979 ProgramError = 0x0B,
980 Busy = 0x0C,
981 FilenameLength = 0x0D,
982 Timeout = 0x0E,
983 FlashBusy = 0x28,
986 EmptyContent = 0xD0,
988 ZipFailure = 0xD2,
989 UnzipFailure = 0xD3,
990 FlashFailure = 0xD4,
991 WriteFailure = 0xD5,
992 RemoveFailure = 0xD6,
993 Unknown = 0xFF,
994};
995
1002inline std::string deviceFileErrorToString(DeviceFileError error) {
1003 switch (error) {
1005 return "No error";
1007 return "File not found";
1009 return "File is empty";
1011 return "File starts with a null terminator";
1013 return "Failed to unzip file";
1015 return "Failed to flash device";
1017 return "Failed to write to device";
1019 return "An unknown error occurred";
1020 default:
1021 return "Unrecognized error";
1022 }
1023}
1024
1032class Device {
1033 public:
1042 virtual ~Device() = default;
1043
1052 virtual uint16_t getPosition() const = 0;
1053
1071 virtual uint8_t getState(
1072 bool refresh = false,
1073 const std::chrono::steady_clock::duration expiryTime =
1074 std::chrono::milliseconds(5000)) = 0;
1075
1088 virtual bool setState(uint8_t state,
1089 const std::chrono::steady_clock::duration expiryTime =
1090 std::chrono::milliseconds(5000)) = 0;
1091
1106 virtual std::vector<uint8_t> readFile(
1107 const std::string& filename,
1108 const std::chrono::steady_clock::duration expiryTime =
1109 std::chrono::milliseconds(5000)) = 0;
1110
1133 virtual std::vector<std::string> readFileList(
1134 const bool stripSizeSuffix,
1135 const std::chrono::steady_clock::duration expiryTime =
1136 std::chrono::milliseconds(5000)) = 0;
1137
1150 virtual void writeFile(const std::string& filename,
1151 const std::vector<uint8_t>& data,
1152 const std::chrono::steady_clock::duration expiryTime =
1153 std::chrono::milliseconds(5000)) = 0;
1154
1162 virtual void removeFile(const std::string& filename,
1163 const std::chrono::steady_clock::duration expiryTime =
1164 std::chrono::milliseconds(5000)) = 0;
1165
1177 virtual void updateFirmware(
1178 const std::vector<std::uint8_t>& data,
1179 const std::vector<std::string>& skipFiles = {},
1180 std::function<void(uint8_t, std::string)> progressCallback = nullptr,
1181 const std::chrono::steady_clock::duration expiryTime =
1182 std::chrono::milliseconds(60000)) = 0;
1183
1193 virtual size_t loadParameters(
1194 bool readValues, const std::chrono::steady_clock::duration expiryTime =
1195 std::chrono::milliseconds(9000)) = 0;
1196
1203 virtual void clearParameters() = 0;
1204
1211 virtual std::vector<std::reference_wrapper<Parameter>> parameters() = 0;
1212
1228 virtual Parameter& findParameter(uint16_t index, uint8_t subindex) = 0;
1229
1248 uint16_t index, uint8_t subindex,
1249 const std::chrono::steady_clock::duration expiryTime =
1250 std::chrono::milliseconds(5000)) = 0;
1251
1267 virtual void download(uint16_t index, uint8_t subindex,
1268 const std::chrono::steady_clock::duration expiryTime =
1269 std::chrono::milliseconds(5000)) = 0;
1270
1288 virtual void download(uint16_t index, uint8_t subindex,
1289 const ParameterValue& value,
1290 const std::chrono::steady_clock::duration expiryTime =
1291 std::chrono::milliseconds(5000)) = 0;
1292
1305 uint64_t missedCycles = 0) = 0;
1306
1323 uint8_t subindex) const = 0;
1324
1330 virtual bool isOnline() = 0;
1331};
1332
1342 public:
1357 virtual bool setState(
1358 uint8_t state, const std::chrono::steady_clock::duration expiryTime) = 0;
1359
1372 virtual std::vector<uint8_t> exchangeProcessData(
1373 const std::vector<uint8_t>& data,
1374 const std::chrono::steady_clock::duration expiryTime) = 0;
1375
1388 uint64_t missedCycles,
1389 const std::chrono::steady_clock::duration expiryTime) = 0;
1390 virtual std::vector<Device>& devices() = 0;
1391
1392 private:
1393 // Container holding all managed devices.
1394 std::vector<Device> devices_;
1395};
1396
1405class DeviceResponseException : public std::runtime_error {
1406 public:
1413 DeviceResponseException(const std::string& what_arg, uint16_t position,
1414 int code);
1415
1419 uint16_t position() const noexcept;
1420
1424 int code() const noexcept;
1425
1426 private:
1427 uint16_t position_;
1428 int code_;
1429};
1430
1443extern std::set<std::string> kNonPartedFileNames;
1444
1475std::vector<std::uint8_t> readCompleteDeviceFile(
1476 Device& device, const std::string& filename,
1477 const std::chrono::steady_clock::duration expiryTime =
1478 std::chrono::milliseconds(20000));
1479
1506 Device& device, const std::string& filename,
1507 const std::vector<std::uint8_t>& data,
1508 std::function<void(uint8_t, std::string)> progressCallback = nullptr,
1509 const std::chrono::steady_clock::duration expiryTime =
1510 std::chrono::milliseconds(30000));
1511
1530void removeDeviceFileOrParts(Device& device, const std::string& filename,
1531 const std::chrono::steady_clock::duration
1532 expiryTime = std::chrono::milliseconds(20000));
1533
1554 Device& device, const std::vector<std::uint8_t>& data,
1555 const std::vector<std::string>& skipFiles = {},
1556 std::function<void(uint8_t, std::string)> progressCallback = nullptr,
1557 const std::chrono::steady_clock::duration expiryTime =
1558 std::chrono::milliseconds(60000));
1559
1574inline std::string makeParameterId(int index, int subindex) {
1575 std::stringstream oss;
1576 // Format index and subindex into "0xINDEX:SUBINDEX"
1577 oss << "0x" << std::setw(4) << std::setfill('0') << std::hex << std::uppercase
1578 << index << ":" << std::setw(2) << std::setfill('0') << std::hex
1579 << std::uppercase << subindex;
1580
1581 return oss.str();
1582}
1583
1606 public:
1614 std::chrono::milliseconds intervalBetweenUploads);
1615
1620
1628 void add(uint16_t index, uint8_t subindex);
1629
1638 void remove(uint16_t index, uint8_t subindex);
1639
1645 void clear();
1646
1657 void start();
1658
1671 void stop();
1672
1682 void triggerManualRefresh(bool async = false);
1683
1684 protected:
1700 virtual void refresh(const std::vector<Parameter::Address>& addresses) = 0;
1701
1702 std::chrono::milliseconds
1704 std::condition_variable
1706 std::mutex mutex_;
1707 std::vector<Parameter::Address>
1711 std::thread worker_;
1712
1713 private:
1734 void run();
1735};
1736
1737} // namespace mm::comm::base
1738
1739namespace nlohmann {
1744template <>
1745struct adl_serializer<mm::comm::base::ParameterValue> {
1747 std::visit([&j](const auto& val) { j = val; }, value);
1748 }
1749
1751 if (j.is_boolean()) {
1752 value = j.get<bool>();
1753 } else if (j.is_number_integer()) {
1754 int64_t num = j.get<int64_t>();
1755 if (num >= -128 && num <= 127) {
1756 value = static_cast<int8_t>(num);
1757 } else if (num >= -32768 && num <= 32767) {
1758 value = static_cast<int16_t>(num);
1759 } else if (num >= -2147483648LL && num <= 2147483647LL) {
1760 value = static_cast<int32_t>(num);
1761 } else {
1762 value = num; // int64_t
1763 }
1764 } else if (j.is_number_unsigned()) {
1765 uint64_t num = j.get<uint64_t>();
1766 if (num <= 255) {
1767 value = static_cast<uint8_t>(num);
1768 } else if (num <= 65535) {
1769 value = static_cast<uint16_t>(num);
1770 } else if (num <= 4294967295ULL) {
1771 value = static_cast<uint32_t>(num);
1772 } else {
1773 value = num; // uint64_t
1774 }
1775 } else if (j.is_number_float()) {
1776 double f = j.get<double>();
1777 // float range: approx ±3.4e38
1778 if (f >= -3.4e38 && f <= 3.4e38) {
1779 value = static_cast<float>(f);
1780 } else {
1781 value = f; // double
1782 }
1783 } else if (j.is_string()) {
1784 value = j.get<std::string>();
1785 } else if (j.is_array()) {
1786 value = j.get<std::vector<uint8_t>>();
1787 } else {
1788 throw std::runtime_error("Unsupported JSON type for ParameterValue");
1789 }
1790 }
1791};
1792
1798inline void to_json(nlohmann::json& j,
1800 j = nlohmann::json{
1801 {"index", p.index}, {"subindex", p.subindex}, {"value", p.value}};
1802}
1803
1809inline void from_json(const nlohmann::json& j,
1811 j.at("index").get_to(p.index);
1812 j.at("subindex").get_to(p.subindex);
1813 j.at("value").get_to(p.value);
1814}
1815
1822 j = json{{"rx", p.rx}, {"tx", p.tx}};
1823}
1824
1831 j.at("rx").get_to(p.rx);
1832 j.at("tx").get_to(p.tx);
1833}
1834} // namespace nlohmann
nlohmann::json json
Definition: circulo_encoder_configuration.cc:13
Manages devices and performs cyclic process data exchange.
Definition: base.h:1341
virtual void exchangeProcessDataAndUpdateParameters(uint64_t missedCycles, const std::chrono::steady_clock::duration expiryTime)=0
Exchanges process data and updates internal parameters for each device.
virtual std::vector< Device > & devices()=0
virtual bool setState(uint8_t state, const std::chrono::steady_clock::duration expiryTime)=0
Sets the operational state of all managed devices.
virtual std::vector< uint8_t > exchangeProcessData(const std::vector< uint8_t > &data, const std::chrono::steady_clock::duration expiryTime)=0
Exchanges process data with devices.
Base class for periodically refreshing a set of device parameters in a background thread.
Definition: base.h:1605
virtual void refresh(const std::vector< Parameter::Address > &addresses)=0
Rereads the specified device parameters.
void triggerManualRefresh(bool async=false)
Triggers a manual refresh of device parameters.
Definition: base.cc:691
void remove(uint16_t index, uint8_t subindex)
Remove a parameter identified by (index, subindex) from the refresh list.
Definition: base.cc:623
void start()
Starts the refresher background thread if it is not already running.
Definition: base.cc:635
std::vector< Parameter::Address > addresses_
List of parameter addresses to refresh.
Definition: base.h:1708
~DeviceParameterRefresher()
Destructor ensures the refresher thread is stopped cleanly.
Definition: base.cc:602
bool running_
Flag indicating whether the refresher thread is running.
Definition: base.h:1710
void clear()
Remove all parameters from the refresh list.
Definition: base.cc:630
Device & device_
Reference to the device instance used for uploading.
Definition: base.h:1709
std::chrono::milliseconds interval_
Time to wait between consecutive parameter uploads.
Definition: base.h:1703
std::mutex mutex_
Mutex protecting access to shared data members.
Definition: base.h:1706
std::condition_variable cv_
Condition variable used to notify the worker thread.
Definition: base.h:1705
std::thread worker_
The background thread performing periodic uploads.
Definition: base.h:1711
void add(uint16_t index, uint8_t subindex)
Add a parameter identified by (index, subindex) to the refresh list.
Definition: base.cc:604
void stop()
Stops the refresher background thread if it is running.
Definition: base.cc:646
Exception thrown when a device responds with an error.
Definition: base.h:1405
uint16_t position() const noexcept
Returns the device position or ID.
Definition: base.cc:319
int code() const noexcept
Returns the device-specific error code.
Definition: base.cc:323
Abstract interface representing a generic device.
Definition: base.h:1032
virtual ~Device()=default
Virtual destructor for Device interface.
virtual uint16_t getPosition() const =0
Gets the device's position in the network chain.
virtual size_t 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 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.
virtual void removeFile(const std::string &filename, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(5000))=0
Removes a file from the device.
virtual void exchangeProcessDataAndUpdateParameters(uint64_t missedCycles=0)=0
Exchanges process data with the remote device and updates local parameters accordingly.
virtual void 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 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 PdoMappingStatus getPdoMappingStatus(uint16_t index, uint8_t subindex) const =0
Determines the PDO mapping status of a parameter by its index and subindex.
virtual void updateFirmware(const std::vector< std::uint8_t > &data, const std::vector< std::string > &skipFiles={}, std::function< void(uint8_t, std::string)> progressCallback=nullptr, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(60000))=0
Updates the device firmwares.
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 std::vector< std::string > readFileList(const bool stripSizeSuffix, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(5000))=0
Reads a list of file names from the device.
virtual void clearParameters()=0
Clears all loaded object dictionary parameters.
virtual bool isOnline()=0
Checks if the device is online.
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 bool setState(uint8_t state, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(5000))=0
Sets the device to a specified state.
virtual Parameter & findParameter(uint16_t index, uint8_t subindex)=0
Finds and returns a reference to a parameter by its index and subindex.
virtual std::vector< std::reference_wrapper< Parameter > > parameters()=0
Get a vector of references to parameters.
virtual uint8_t getState(bool refresh=false, const std::chrono::steady_clock::duration expiryTime=std::chrono::milliseconds(5000))=0
Reads and returns the current state of the device.
Represents a device parameter identified by index and subindex.
Definition: base.h:463
bool hasAllRead() const
Checks if all read access flags are set in the access field.
Definition: base.cc:203
ParameterValue getValue() const
Retrieves the value of the parameter based on its data type.
Definition: base.cc:36
ObjectDataType dataType
The data type of the parameter.
Definition: base.h:509
T getIntValue()
Converts the internal byte vector to an integer of type T.
Definition: base.h:638
ObjectCode code
The object code for the parameter.
Definition: base.h:514
ObjectFlags access
The access flags for the parameter.
Definition: base.h:524
std::optional< T > tryGetValue() const
Attempts to retrieve the parameter value from the ParameterValue variant as the specified type.
Definition: base.h:617
std::string name
Name of the parameter.
Definition: base.h:484
static void to_json(nlohmann::json &j, const Parameter &p)
Serializes a Parameter object to JSON.
Definition: base.cc:211
static void from_json(const nlohmann::json &j, Parameter &p)
Deserializes a Parameter object from JSON.
Definition: base.cc:223
std::pair< uint16_t, uint8_t > Address
Alias for a pair of uint16_t and uint8_t representing a parameter address.
Definition: base.h:473
void setValue(const T &value)
Sets the parameter value using a strongly-typed input.
Definition: base.h:682
std::vector< std::uint8_t > data
Holds raw data as a vector of uint8_t elements.
Definition: base.h:537
bool operator==(const Parameter &other) const
Checks if two Parameter objects are equal.
Definition: base.h:834
std::uint16_t index
Index of the parameter.
Definition: base.h:489
T getValue() const
Retrieves the parameter value from the ParameterValue variant as the specified type.
Definition: base.h:589
bool operator<(const Parameter &other) const
Compares two Parameter objects for ordering.
Definition: base.h:798
bool trySetValue(const T &value)
Attempts to set the parameter value with a strongly-typed input.
Definition: base.h:705
std::uint16_t bitLength
Bit length of the parameter.
Definition: base.h:499
bool hasAllWrite() const
Checks if all write access flags are set in the access field.
Definition: base.cc:207
std::string group
Name of the group this parameter belongs to. Applicable only to ARRAY and RECORD objects.
Definition: base.h:479
ObjectFlags flags
The object flags for the parameter.
Definition: base.h:519
bool operator>(const Parameter &other) const
Compares two Parameter objects for reverse ordering.
Definition: base.h:816
std::uint8_t subindex
Subindex of the parameter.
Definition: base.h:494
void setValue(const ParameterValue &value)
Sets the internal raw data representation from a given value.
Definition: base.cc:124
int byteLength
Byte length of the parameter.
Definition: base.h:504
PdoMappingStatus pdoMappingStatus
PDO mapping status of this parameter.
Definition: base.h:532
uint8_t * value
Definition: co_dictionary.h:9
uint16_t index
Definition: co_dictionary.h:0
Definition: base.cc:8
void from_json(const nlohmann::json &j, UiPdoMapping &p)
Deserialize a JSON object into a UiPdoMapping structure.
Definition: base.cc:10
std::string makeParameterId(int index, int subindex)
Formats the given index and subindex into a parameter identifier string.
Definition: base.h:1574
std::string deviceFileErrorToString(DeviceFileError error)
Convert a DeviceFileError enum value to a human-readable string.
Definition: base.h:1002
std::set< std::string > kNonPartedFileNames
Set of filenames that are always stored as complete files and can be read directly.
Definition: base.cc:325
bool operator==(uint32_t lhs, EtherCATVendorID rhs)
Compares a uint32_t value with an EtherCATVendorID enum value.
Definition: base.h:95
constexpr std::size_t kDeviceFilePartSize
Definition: base.h:80
ObjectDataType
Enumerates the supported data types for parameters.
Definition: base.h:267
@ 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.
@ 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.
void writeDeviceFileInOptionalParts(Device &device, const std::string &filename, const std::vector< std::uint8_t > &data, std::function< void(uint8_t, std::string)> progressCallback, const std::chrono::steady_clock::duration expiryTime)
Write a file to a device, splitting it into parts if necessary.
Definition: base.cc:425
EtherCATVendorID
Enum class representing EtherCAT vendor IDs.
Definition: base.h:71
@ SYNAPTICON
Vendor ID for Synapticon GmbH.
ObjectCode
Enum class representing different object types used in the EtherCAT protocol.
Definition: base.h:369
@ DEFSTRUCT
Definition structure object.
@ DEFTYPE
Definition type object.
@ ARRAY
Array object type.
@ VAR
Variable object type.
void logParametersMap(const std::unordered_map< Parameter::Address, Parameter > &parametersMap, bool sortParameters)
Logs the contents of a map of parameters with an option to sort them.
Definition: base.cc:288
ObjectFlags SetObjectFlags(ObjectFlags b, ObjectFlags s, ObjectFlags p, ObjectFlags a)
Combine multiple ObjectFlags values into one.
Definition: base.h:250
bool isComBinFile(const std::string &filename)
Checks whether a given filename starts with "com_" and ends with ".bin".
Definition: base.h:945
void removeDeviceFileOrParts(Device &device, const std::string &filename, const std::chrono::steady_clock::duration expiryTime)
Removes a file from the device, including handling multipart files if applicable.
Definition: base.cc:498
ObjectFlags operator|(ObjectFlags lhs, ObjectFlags rhs)
Bitwise OR operator for ObjectFlags enum class.
Definition: base.h:202
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: base.h:405
DeviceFileError
Enumeration of possible device file-related error codes.
Definition: base.h:967
PdoMappingStatus
Represents the PDO mapping status of a parameter.
Definition: base.h:436
@ MAPPED_RX_PDO
The parameter is mapped as a Receive PDO.
@ NOT_MAPPED
The parameter is not mapped in any PDO.
@ MAPPED_TX_PDO
The parameter is mapped as a Transmit PDO.
void writeFirmwarePackage(Device &device, const std::vector< std::uint8_t > &data, const std::vector< std::string > &skipFiles, std::function< void(uint8_t, std::string)> progressCallback, const std::chrono::steady_clock::duration expiryTime)
Writes a firmware package to the given device.
Definition: base.cc:545
void assignGroupsToParameters(std::vector< Parameter > &parameters)
Assigns group names to parameters based on their index and subindex.
Definition: base.cc:254
std::vector< std::uint8_t > readCompleteDeviceFile(Device &device, const std::string &filename, const std::chrono::steady_clock::duration expiryTime)
Reads a file from the device, supporting both complete and multipart zipped files.
Definition: base.cc:331
ObjectFlags operator&(ObjectFlags lhs, ObjectFlags rhs)
Bitwise AND operator for ObjectFlags enum class.
Definition: base.h:217
ObjectFlags & operator|=(ObjectFlags &lhs, ObjectFlags rhs)
Bitwise OR assignment operator for ObjectFlags enum class.
Definition: base.h:232
ObjectFlags
Enum class representing various object flags.
Definition: base.h:159
bool isAppBinFile(const std::string &filename)
Checks whether a given filename starts with "app_" and ends with ".bin".
Definition: base.h:928
bool isFirmwareBinFile(const std::string &filename)
Checks whether the given file is a firmware binary file.
Definition: base.h:959
std::string convertParameterValueToString(const ParameterValue &value)
Converts a ParameterValue to a string representation.
Definition: base.cc:235
Definition: base.cc:8
Definition: base.h:1739
void to_json(nlohmann::json &j, const mm::comm::base::MappedParameterValue &p)
Serialize MappedParameterValue to JSON.
Definition: base.h:1798
void from_json(const nlohmann::json &j, mm::comm::base::MappedParameterValue &p)
Deserialize JSON into MappedParameterValue.
Definition: base.h:1809
Represents a single mapped parameter value.
Definition: base.h:413
uint8_t subindex
Definition: base.h:415
uint16_t index
Definition: base.h:414
ParameterValue value
Definition: base.h:416
Holds collections of mapped parameter values for RX and TX directions.
Definition: base.h:425
std::vector< MappedParameterValue > rx
Definition: base.h:426
std::vector< MappedParameterValue > tx
Definition: base.h:427
Represents a single PDO (Process Data Object) mapping entry.
Definition: base.h:123
uint16_t index
The object dictionary index (e.g., 0x607A)
Definition: base.h:125
uint8_t subindex
The subindex of the object (e.g., 0x00)
Definition: base.h:126
uint8_t bitlength
The size of the mapped entry in bits (e.g., 16, 32, etc.)
Definition: base.h:128
uint16_t pdoIndex
The PDO index (e.g., 0x1600 or 0x1A00)
Definition: base.h:124
Represents the mapped PDO entries for a slave device.
Definition: base.h:145
std::vector< PdoMappingEntry > txPdos
Definition: base.h:148
std::vector< PdoMappingEntry > rxPdos
Definition: base.h:146
Top-level UI configuration JSON structure.
Definition: base.h:36
UiPdoMapping pdoMapping
Definition: base.h:37
Represents the PDO mapping for RX and TX channels.
Definition: base.h:26
std::map< std::uint16_t, std::vector< std::uint32_t > > rx
Definition: base.h:27
std::map< std::uint16_t, std::vector< std::uint32_t > > tx
Definition: base.h:28
static void to_json(json &j, const mm::comm::base::ParameterValue &value)
Definition: base.h:1746
static void from_json(const json &j, mm::comm::base::ParameterValue &value)
Definition: base.h:1750