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 <optional>
6#include <set>
7#include <sstream>
8#include <string>
9#include <typeindex>
10#include <unordered_map>
11#include <unordered_set>
12#include <utility>
13#include <variant>
14#include <vector>
15
16#include <nlohmann/json.hpp>
17
18#include "core/util.h"
19
20namespace mm::comm::base {
31 std::map<std::uint16_t, std::vector<std::uint32_t>> rx;
32 std::map<std::uint16_t, std::vector<std::uint32_t>> tx;
33};
34
43
55void from_json(const nlohmann::json& j, UiPdoMapping& p);
56
66void from_json(const nlohmann::json& j, UiConfigJson& r);
67
75enum class EtherCATVendorID : uint32_t {
81 SYNAPTICON = 0x000022d2,
82
83 SENSODRIVE = 0x0000063a
84};
85
86inline constexpr std::size_t kDeviceFilePartSize =
87 9000;
89
101inline bool operator==(uint32_t lhs, EtherCATVendorID rhs) {
102 return lhs == static_cast<uint32_t>(rhs);
103}
104
116inline bool operator==(EtherCATVendorID lhs, uint32_t rhs) {
117 return static_cast<uint32_t>(lhs) == rhs;
118}
119
131 uint16_t pdoIndex;
132 uint8_t pdoSubindex;
133 uint16_t index;
134 uint8_t subindex;
135 uint8_t
137
141 PdoMappingEntry() = default;
142
152 PdoMappingEntry(uint16_t pdoIndex, uint8_t pdoSubindex, uint16_t index,
153 uint8_t subindex, uint8_t bitlength)
156 index(index),
159};
160
176 std::vector<PdoMappingEntry> rxPdos;
178 std::vector<PdoMappingEntry> txPdos;
180
185 [[nodiscard]] size_t rxSizeBits() const {
186 size_t total = 0;
187 for (const auto& entry : rxPdos) {
188 total += entry.bitlength;
189 }
190 return total;
191 }
192
197 [[nodiscard]] size_t txSizeBits() const {
198 size_t total = 0;
199 for (const auto& entry : txPdos) {
200 total += entry.bitlength;
201 }
202 return total;
203 }
204
211 [[nodiscard]] size_t rxSizeBytes() const { return (rxSizeBits() + 7) / 8; }
212
219 [[nodiscard]] size_t txSizeBytes() const { return (txSizeBits() + 7) / 8; }
220};
221
229enum class ObjectFlags : uint16_t {
230 None = 0x0000,
231
232 // Read access
233 PO_RD = 0x0001,
234 SO_RD = 0x0002,
235 OP_RD = 0x0004,
237
238 // Write access
239 PO_WR = 0x0008,
240 SO_WR = 0x0010,
241 OP_WR = 0x0020,
243
244 // Read/Write combinations
249
250 // Mapping
251 RXPDO_MAP = 0x0040,
252 TXPDO_MAP = 0x0080,
253 RXTXPDO_MAP = 0x00C0,
254
255 // Other flags
256 BACKUP = 0x0100,
257 STARTUP = 0x0200,
258
260};
261
273 return static_cast<ObjectFlags>(static_cast<uint16_t>(lhs) |
274 static_cast<uint16_t>(rhs));
275}
276
288 return static_cast<ObjectFlags>(static_cast<uint16_t>(lhs) &
289 static_cast<uint16_t>(rhs));
290}
291
303 lhs = lhs | rhs;
304 return lhs;
305}
306
321 ObjectFlags a) {
322 return b | s | p | a;
323}
324
337enum class ObjectDataType : uint16_t {
338 UNSPECIFIED = 0x0000,
339
340 BOOLEAN = 0x0001,
341 BYTE = 0x001E,
342 WORD = 0x001F,
343 DWORD = 0x0020,
344
345 BIT1 = 0x0030,
346 BIT2 = 0x0031,
347 BIT3 = 0x0032,
348 BIT4 = 0x0033,
349 BIT5 = 0x0034,
350 BIT6 = 0x0035,
351 BIT7 = 0x0036,
352 BIT8 = 0x0037,
353 BIT9 = 0x0038,
354 BIT10 = 0x0039,
355 BIT11 = 0x003A,
356 BIT12 = 0x003B,
357 BIT13 = 0x003C,
358 BIT14 = 0x003D,
359 BIT15 = 0x003E,
360 BIT16 = 0x003F,
361
362 BITARR8 = 0x002D,
363 BITARR16 = 0x002E,
364 BITARR32 = 0x002F,
365
366 INTEGER8 = 0x0002,
367 INTEGER16 = 0x0003,
368 INTEGER24 = 0x0010,
369 INTEGER32 = 0x0004,
370 INTEGER40 = 0x0012,
371 INTEGER48 = 0x0013,
372 INTEGER56 = 0x0014,
373 INTEGER64 = 0x0015,
374
375 UNSIGNED8 = 0x0005,
376 UNSIGNED16 = 0x0006,
377 UNSIGNED24 = 0x0016,
378 UNSIGNED32 = 0x0007,
379 UNSIGNED40 = 0x0018,
380 UNSIGNED48 = 0x0019,
381 UNSIGNED56 = 0x001A,
382 UNSIGNED64 = 0x001B,
383
384 REAL32 = 0x0008,
385 REAL64 = 0x0011,
386
387 GUID = 0x001D,
388
389 VISIBLE_STRING = 0x0009,
390 OCTET_STRING = 0x000A,
391 UNICODE_STRING = 0x000B,
393 ARRAY_OF_INT = 0x0260,
394 ARRAY_OF_SINT = 0x0261,
395 ARRAY_OF_DINT = 0x0262,
396 ARRAY_OF_UDINT = 0x0263,
397
398 PDO_MAPPING = 0x0021,
399 IDENTITY = 0x0023,
400 COMMAND_PAR = 0x0025,
401 PDO_PARAMETER = 0x0027,
402 ENUM = 0x0028,
404 0x0029,
405 RECORD = 0x002A,
410 ERROR_SETTING = 0x0281,
412 0x0282,
420 0x0286,
422
423 TIME_OF_DAY = 0x000C,
425 0x000D,
426 UTYPE_START = 0x0800,
427 UTYPE_END = 0x0FFF
428};
429
439enum class ObjectCode : uint16_t {
440 DEFTYPE = 0x0005,
441 DEFSTRUCT = 0x0006,
442 VAR = 0x0007,
443 ARRAY = 0x0008,
444 RECORD = 0x0009,
445};
446
471 std::variant<bool, std::int8_t, std::int16_t, std::int32_t, std::int64_t,
472 std::uint8_t, std::uint16_t, std::uint32_t, std::uint64_t,
473 float, double, std::string,
474 std::vector<std::uint8_t>>;
476
488
496 std::vector<MappedParameterValue> rx;
497 std::vector<MappedParameterValue> tx;
498};
499
511
534 public:
543 using Address = std::pair<uint16_t, uint8_t>;
544
549 std::string group;
550
554 std::string name;
555
559 std::uint16_t index;
560
564 std::uint8_t subindex;
565
569 std::uint16_t bitLength;
570
575
580
585
590
595
603
607 std::vector<std::uint8_t> data;
608
639 ParameterValue getValue() const;
640
658 template <typename T>
659 T getValue() const {
660 // Get the value as a ParameterValue
661 const ParameterValue& val = getValue();
662
663 // Try to get the value of type T from the variant
664 if (auto ptr = std::get_if<T>(&val)) {
665 return *ptr; // Return the value of type T
666 } else {
667 throw std::bad_variant_access(); // If type T is not found in the variant
668 }
669 }
670
686 template <typename T>
687 std::optional<T> tryGetValue() const {
688 const ParameterValue& val = getValue();
689 if (auto ptr = std::get_if<T>(&val)) {
690 return *ptr;
691 } else {
692 return std::nullopt;
693 }
694 }
695
707 template <typename T>
712
733 void setValue(const ParameterValue& value);
734
751 template <typename T>
752 void setValue(const T& value) {
753 // Convert the input value to ParameterValue (variant)
754 ParameterValue parameterValue(value);
755
756 // Reuse the existing setValue function to handle the assignment
757 setValue(parameterValue);
758 }
759
774 template <typename T>
775 bool trySetValue(const T& value) {
776 auto expectedType = [this]() -> std::type_index {
777 switch (dataType) {
779 return typeid(bool);
780 }
782 return typeid(std::int8_t);
783 }
785 return typeid(std::int16_t);
786 }
789 return typeid(std::int32_t);
790 }
792 return typeid(std::int64_t);
793 }
799 return typeid(std::uint8_t);
800 }
802 return typeid(std::uint16_t);
803 }
806 return typeid(std::uint32_t);
807 }
809 return typeid(std::uint64_t);
810 }
812 return typeid(float);
813 }
815 return typeid(double);
816 }
820 return typeid(std::string);
821 }
822 default: {
823 return typeid(void);
824 }
825 }
826 };
827
828 const std::type_index expected = expectedType();
829 if (expected != typeid(T) &&
830 typeid(T) != typeid(std::vector<std::uint8_t>)) {
831 return false;
832 }
833
835 return true;
836 }
837
846 bool hasAllRead() const;
847
856 bool hasAllWrite() const;
857
868 bool operator<(const Parameter& other) const {
869 if (index != other.index) {
870 return index < other.index; // Compare by index first
871 }
872 return subindex <
873 other.subindex; // Compare by subindex if indices are equal
874 }
875
886 bool operator>(const Parameter& other) const {
887 if (index != other.index) {
888 return index > other.index; // Compare by index first
889 }
890 return subindex >
891 other.subindex; // Compare by subindex if indices are equal
892 }
893
904 bool operator==(const Parameter& other) const {
905 return index == other.index && subindex == other.subindex;
906 }
907
918 static void to_json(nlohmann::json& j, const Parameter& p);
919
930 static void from_json(const nlohmann::json& j, Parameter& p);
931};
932
946
961void assignGroupsToParameters(std::vector<Parameter>& parameters);
962
983 const std::unordered_map<Parameter::Address, Parameter>& parametersMap,
984 bool sortParameters = true);
985
993const std::unordered_set<std::string> kFirmwareInstallationFilenameWhitelist = {
994 "stack_image.svg.zip", "SOMANET_CiA_402.xml.zip"};
995
1008inline bool isAppBinFile(const std::string& filename) {
1009 return filename.rfind("app_", 0) == 0 && filename.size() >= 4 &&
1010 filename.compare(filename.size() - 4, 4, ".bin") == 0;
1011}
1012
1025inline bool isComBinFile(const std::string& filename) {
1026 return filename.rfind("com_", 0) == 0 && filename.size() >= 4 &&
1027 filename.compare(filename.size() - 4, 4, ".bin") == 0;
1028}
1029
1039inline bool isFirmwareBinFile(const std::string& filename) {
1040 return isAppBinFile(filename) || isComBinFile(filename);
1041}
1042
1075
1082inline std::string deviceFileErrorToString(DeviceFileError error) {
1083 switch (error) {
1085 return "No error";
1087 return "File not found";
1089 return "File is empty";
1091 return "File starts with a null terminator";
1093 return "Failed to unzip file";
1095 return "Failed to flash device";
1097 return "Failed to write to device";
1099 return "An unknown error occurred";
1100 default:
1101 return "Unrecognized error";
1102 }
1103}
1104
1112class Device {
1113 public:
1122 virtual ~Device() = default;
1123
1132 virtual uint16_t getPosition() const = 0;
1133
1151 virtual uint8_t getState(
1152 bool refresh = false,
1153 const std::chrono::steady_clock::duration expiryTime =
1154 std::chrono::milliseconds(5000)) = 0;
1155
1168 virtual bool setState(uint8_t state,
1169 const std::chrono::steady_clock::duration expiryTime =
1170 std::chrono::milliseconds(5000)) = 0;
1171
1186 virtual std::vector<uint8_t> readFile(
1187 const std::string& filename,
1188 const std::chrono::steady_clock::duration expiryTime =
1189 std::chrono::milliseconds(5000)) = 0;
1190
1213 virtual std::vector<std::string> readFileList(
1214 const bool stripSizeSuffix,
1215 const std::chrono::steady_clock::duration expiryTime =
1216 std::chrono::milliseconds(5000)) = 0;
1217
1230 virtual void writeFile(const std::string& filename,
1231 const std::vector<uint8_t>& data,
1232 const std::chrono::steady_clock::duration expiryTime =
1233 std::chrono::milliseconds(5000)) = 0;
1234
1242 virtual void removeFile(const std::string& filename,
1243 const std::chrono::steady_clock::duration expiryTime =
1244 std::chrono::milliseconds(5000)) = 0;
1245
1257 virtual void updateFirmware(
1258 const std::vector<std::uint8_t>& data,
1259 const std::vector<std::string>& skipFiles = {},
1260 std::function<void(uint8_t, std::string)> progressCallback = nullptr,
1261 const std::chrono::steady_clock::duration expiryTime =
1262 std::chrono::milliseconds(60000)) = 0;
1263
1273 virtual size_t loadParameters(
1274 bool readValues, const std::chrono::steady_clock::duration expiryTime =
1275 std::chrono::milliseconds(9000)) = 0;
1276
1283 virtual void clearParameters() = 0;
1284
1291 virtual std::vector<std::reference_wrapper<Parameter>> parameters() = 0;
1292
1308 virtual Parameter& findParameter(uint16_t index, uint8_t subindex) = 0;
1309
1328 uint16_t index, uint8_t subindex,
1329 const std::chrono::steady_clock::duration expiryTime =
1330 std::chrono::milliseconds(5000)) = 0;
1331
1347 virtual void download(uint16_t index, uint8_t subindex,
1348 const std::chrono::steady_clock::duration expiryTime =
1349 std::chrono::milliseconds(5000)) = 0;
1350
1368 virtual void download(uint16_t index, uint8_t subindex,
1369 const ParameterValue& value,
1370 const std::chrono::steady_clock::duration expiryTime =
1371 std::chrono::milliseconds(5000)) = 0;
1372
1385 uint64_t missedCycles = 0) = 0;
1386
1403 uint8_t subindex) const = 0;
1404
1410 virtual bool isOnline() = 0;
1411};
1412
1422 public:
1437 virtual bool setState(
1438 uint8_t state, const std::chrono::steady_clock::duration expiryTime) = 0;
1439
1452 virtual std::vector<uint8_t> exchangeProcessData(
1453 const std::vector<uint8_t>& data,
1454 const std::chrono::steady_clock::duration expiryTime) = 0;
1455
1468 uint64_t missedCycles,
1469 const std::chrono::steady_clock::duration expiryTime) = 0;
1470 virtual std::vector<Device>& devices() = 0;
1471
1472 private:
1473 // Container holding all managed devices.
1474 std::vector<Device> devices_;
1475};
1476
1485class DeviceResponseException : public std::runtime_error {
1486 public:
1493 DeviceResponseException(const std::string& what_arg, uint16_t position,
1494 int code);
1495
1499 uint16_t position() const noexcept;
1500
1504 int code() const noexcept;
1505
1506 private:
1507 uint16_t position_;
1508 int code_;
1509};
1510
1523extern std::set<std::string> kNonPartedFileNames;
1524
1555std::vector<std::uint8_t> readCompleteDeviceFile(
1556 Device& device, const std::string& filename,
1557 const std::chrono::steady_clock::duration expiryTime =
1558 std::chrono::milliseconds(20000));
1559
1586 Device& device, const std::string& filename,
1587 const std::vector<std::uint8_t>& data,
1588 std::function<void(uint8_t, std::string)> progressCallback = nullptr,
1589 const std::chrono::steady_clock::duration expiryTime =
1590 std::chrono::milliseconds(30000));
1591
1610void removeDeviceFileOrParts(Device& device, const std::string& filename,
1611 const std::chrono::steady_clock::duration
1612 expiryTime = std::chrono::milliseconds(20000));
1613
1634 Device& device, const std::vector<std::uint8_t>& data,
1635 const std::vector<std::string>& skipFiles = {},
1636 std::function<void(uint8_t, std::string)> progressCallback = nullptr,
1637 const std::chrono::steady_clock::duration expiryTime =
1638 std::chrono::milliseconds(60000));
1639
1654inline std::string makeParameterId(int index, int subindex) {
1655 std::stringstream oss;
1656 // Format index and subindex into "0xINDEX:SUBINDEX"
1657 oss << "0x" << std::setw(4) << std::setfill('0') << std::hex << std::uppercase
1658 << index << ":" << std::setw(2) << std::setfill('0') << std::hex
1659 << std::uppercase << subindex;
1660
1661 return oss.str();
1662}
1663
1686 public:
1694 std::chrono::milliseconds intervalBetweenUploads);
1695
1700
1708 void add(uint16_t index, uint8_t subindex);
1709
1718 void remove(uint16_t index, uint8_t subindex);
1719
1725 void clear();
1726
1737 void start();
1738
1751 void stop();
1752
1762 void triggerManualRefresh(bool async = false);
1763
1764 protected:
1780 virtual void refresh(const std::vector<Parameter::Address>& addresses) = 0;
1781
1782 std::chrono::milliseconds
1784 std::condition_variable
1786 std::mutex mutex_;
1787 std::vector<Parameter::Address>
1791 std::thread worker_;
1792
1793 private:
1814 void run();
1815};
1816
1831 uint16_t pdoIndex,
1832 uint8_t pdoSubindex);
1833
1846std::vector<mm::comm::base::Parameter::Address> makeParameterAddressIndexRange(
1847 uint16_t start, uint16_t end, uint8_t maxSubIndex);
1848
1858std::vector<mm::comm::base::Parameter::Address> makeAllPdoAddresses();
1859
1860} // namespace mm::comm::base
1861
1862namespace nlohmann {
1867template <>
1868struct adl_serializer<mm::comm::base::ParameterValue> {
1870 std::visit([&j](const auto& val) { j = val; }, value);
1871 }
1872
1874 if (j.is_boolean()) {
1875 value = j.get<bool>();
1876 } else if (j.is_number_integer()) {
1877 int64_t num = j.get<int64_t>();
1878 if (num >= -128 && num <= 127) {
1879 value = static_cast<int8_t>(num);
1880 } else if (num >= -32768 && num <= 32767) {
1881 value = static_cast<int16_t>(num);
1882 } else if (num >= -2147483648LL && num <= 2147483647LL) {
1883 value = static_cast<int32_t>(num);
1884 } else {
1885 value = num; // int64_t
1886 }
1887 } else if (j.is_number_unsigned()) {
1888 uint64_t num = j.get<uint64_t>();
1889 if (num <= 255) {
1890 value = static_cast<uint8_t>(num);
1891 } else if (num <= 65535) {
1892 value = static_cast<uint16_t>(num);
1893 } else if (num <= 4294967295ULL) {
1894 value = static_cast<uint32_t>(num);
1895 } else {
1896 value = num; // uint64_t
1897 }
1898 } else if (j.is_number_float()) {
1899 double f = j.get<double>();
1900 // float range: approx ±3.4e38
1901 if (f >= -3.4e38 && f <= 3.4e38) {
1902 value = static_cast<float>(f);
1903 } else {
1904 value = f; // double
1905 }
1906 } else if (j.is_string()) {
1907 value = j.get<std::string>();
1908 } else if (j.is_array()) {
1909 value = j.get<std::vector<uint8_t>>();
1910 } else {
1911 throw std::runtime_error("Unsupported JSON type for ParameterValue");
1912 }
1913 }
1914};
1915
1921inline void to_json(nlohmann::json& j,
1923 j = nlohmann::json{
1924 {"index", p.index}, {"subindex", p.subindex}, {"value", p.value}};
1925}
1926
1932inline void from_json(const nlohmann::json& j,
1934 j.at("index").get_to(p.index);
1935 j.at("subindex").get_to(p.subindex);
1936 j.at("value").get_to(p.value);
1937}
1938
1945 j = json{{"rx", p.rx}, {"tx", p.tx}};
1946}
1947
1954 j.at("rx").get_to(p.rx);
1955 j.at("tx").get_to(p.tx);
1956}
1957} // namespace nlohmann
nlohmann::json json
Definition circulo_encoder_configuration.cc:13
Manages devices and performs cyclic process data exchange.
Definition base.h:1421
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.
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:716
void remove(uint16_t index, uint8_t subindex)
Remove a parameter identified by (index, subindex) from the refresh list.
Definition base.cc:647
void start()
Starts the refresher background thread if it is not already running.
Definition base.cc:659
std::vector< Parameter::Address > addresses_
List of parameter addresses to refresh.
Definition base.h:1788
~DeviceParameterRefresher()
Destructor ensures the refresher thread is stopped cleanly.
Definition base.cc:626
bool running_
Flag indicating whether the refresher thread is running.
Definition base.h:1790
void clear()
Remove all parameters from the refresh list.
Definition base.cc:654
Device & device_
Reference to the device instance used for uploading.
Definition base.h:1789
DeviceParameterRefresher(Device &device, std::chrono::milliseconds intervalBetweenUploads)
Constructor initializes the refresher with a device reference and upload interval.
Definition base.cc:622
std::chrono::milliseconds interval_
Time to wait between consecutive parameter uploads.
Definition base.h:1783
std::mutex mutex_
Mutex protecting access to shared data members.
Definition base.h:1786
std::condition_variable cv_
Condition variable used to notify the worker thread.
Definition base.h:1785
std::thread worker_
The background thread performing periodic uploads.
Definition base.h:1791
void add(uint16_t index, uint8_t subindex)
Add a parameter identified by (index, subindex) to the refresh list.
Definition base.cc:628
void stop()
Stops the refresher background thread if it is running.
Definition base.cc:670
uint16_t position() const noexcept
Returns the device position or ID.
Definition base.cc:324
int code() const noexcept
Returns the device-specific error code.
Definition base.cc:328
DeviceResponseException(const std::string &what_arg, uint16_t position, int code)
Constructs a DeviceResponseException.
Definition base.cc:320
Abstract interface representing a generic device.
Definition base.h:1112
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:533
bool hasAllRead() const
Checks if all read access flags are set in the access field.
Definition base.cc:208
ParameterValue getValue() const
Retrieves the value of the parameter based on its data type.
Definition base.cc:41
ObjectDataType dataType
The data type of the parameter.
Definition base.h:579
T getIntValue()
Converts the internal byte vector to an integer of type T.
Definition base.h:708
ObjectCode code
The object code for the parameter.
Definition base.h:584
ObjectFlags access
The access flags for the parameter.
Definition base.h:594
std::optional< T > tryGetValue() const
Attempts to retrieve the parameter value from the ParameterValue variant as the specified type.
Definition base.h:687
std::string name
Name of the parameter.
Definition base.h:554
static void to_json(nlohmann::json &j, const Parameter &p)
Serializes a Parameter object to JSON.
Definition base.cc:216
static void from_json(const nlohmann::json &j, Parameter &p)
Deserializes a Parameter object from JSON.
Definition base.cc:228
std::pair< uint16_t, uint8_t > Address
Alias for a pair of uint16_t and uint8_t representing a parameter address.
Definition base.h:543
void setValue(const T &value)
Sets the parameter value using a strongly-typed input.
Definition base.h:752
std::vector< std::uint8_t > data
Holds raw data as a vector of uint8_t elements.
Definition base.h:607
bool operator==(const Parameter &other) const
Checks if two Parameter objects are equal.
Definition base.h:904
std::uint16_t index
Index of the parameter.
Definition base.h:559
T getValue() const
Retrieves the parameter value from the ParameterValue variant as the specified type.
Definition base.h:659
bool operator<(const Parameter &other) const
Compares two Parameter objects for ordering.
Definition base.h:868
bool trySetValue(const T &value)
Attempts to set the parameter value with a strongly-typed input.
Definition base.h:775
std::uint16_t bitLength
Bit length of the parameter.
Definition base.h:569
bool hasAllWrite() const
Checks if all write access flags are set in the access field.
Definition base.cc:212
std::string group
Name of the group this parameter belongs to. Applicable only to ARRAY and RECORD objects.
Definition base.h:549
ObjectFlags flags
The object flags for the parameter.
Definition base.h:589
bool operator>(const Parameter &other) const
Compares two Parameter objects for reverse ordering.
Definition base.h:886
std::uint8_t subindex
Subindex of the parameter.
Definition base.h:564
void setValue(const ParameterValue &value)
Sets the internal raw data representation from a given value.
Definition base.cc:129
int byteLength
Byte length of the parameter.
Definition base.h:574
PdoMappingStatus pdoMappingStatus
PDO mapping status of this parameter.
Definition base.h:602
uint8_t * value
Definition co_dictionary.h:9
uint16_t index
Definition co_dictionary.h:0
COD_Subitem * entry
Definition co_dictionary.h:8
Definition base.cc:13
void from_json(const nlohmann::json &j, UiPdoMapping &p)
Deserialize a JSON object into a UiPdoMapping structure.
Definition base.cc:15
std::string makeParameterId(int index, int subindex)
Formats the given index and subindex into a parameter identifier string.
Definition base.h:1654
std::string deviceFileErrorToString(DeviceFileError error)
Convert a DeviceFileError enum value to a human-readable string.
Definition base.h:1082
std::set< std::string > kNonPartedFileNames
Set of filenames that are always stored as complete files and can be read directly.
Definition base.cc:330
const std::unordered_set< std::string > kFirmwareInstallationFilenameWhitelist
Whitelist of allowed filenames inside a firmware package archive.
Definition base.h:993
bool operator==(uint32_t lhs, EtherCATVendorID rhs)
Compares a uint32_t value with an EtherCATVendorID enum value.
Definition base.h:101
constexpr std::size_t kDeviceFilePartSize
Definition base.h:86
ObjectDataType
Enumerates the supported data types for parameters.
Definition base.h:337
@ BIT14
14-bit field.
Definition base.h:358
@ UNSIGNED40
40-bit unsigned integer.
Definition base.h:379
@ INTEGER16
16-bit signed integer.
Definition base.h:367
@ UNICODE_STRING
Definition base.h:391
@ BACKUP_PARAMETER
For BACKUP_PARAMETER definition see ETG.1020.
Definition base.h:407
@ UNSPECIFIED
Undefined or unknown data type.
Definition base.h:338
@ INTEGER64
64-bit signed integer.
Definition base.h:373
@ SM_SYNCHRONIZATION
For SM_SYNCHRONIZATION definition see ETG.1000.
Definition base.h:403
@ EXTERNAL_SYNC_STATUS
Definition base.h:413
@ BIT8
8-bit field.
Definition base.h:352
@ ERROR_SETTING
For ERROR_SETTING definition see ETG.1020.
Definition base.h:410
@ INTEGER32
32-bit signed integer.
Definition base.h:369
@ BIT10
10-bit field.
Definition base.h:354
@ ARRAY_OF_INT
Sequence of INT.
Definition base.h:393
@ TIME_OF_DAY
Time of day format (implementation-defined).
Definition base.h:423
@ REAL32
32-bit floating-point number.
Definition base.h:384
@ UNSIGNED56
56-bit unsigned integer.
Definition base.h:381
@ UNSIGNED8
8-bit unsigned integer.
Definition base.h:375
@ MODULAR_DEVICE_PARAMETER
Definition base.h:408
@ BITARR8
Array of 8 bits.
Definition base.h:362
@ BITARR32
Array of 32 bits.
Definition base.h:364
@ UNSIGNED64
64-bit unsigned integer.
Definition base.h:382
@ BIT3
3-bit field.
Definition base.h:347
@ REAL64
64-bit floating-point number (double).
Definition base.h:385
@ DEFTYPE_FSOECOMMPAR
Definition base.h:419
@ BIT13
13-bit field.
Definition base.h:357
@ GUID
128-bit globally unique identifier.
Definition base.h:387
@ BIT1
1-bit field.
Definition base.h:345
@ INTEGER40
40-bit signed integer.
Definition base.h:370
@ PDO_PARAMETER
For PDO_PARAMETER definition see ETG.1020.
Definition base.h:401
@ ENUM
For ENUM definition see ETG.1020.
Definition base.h:402
@ EXTERNAL_SYNC_SETTINGS
Definition base.h:415
@ UNSIGNED48
48-bit unsigned integer.
Definition base.h:380
@ DWORD
Four octets (32 bits) unsigned integer.
Definition base.h:343
@ BIT9
9-bit field.
Definition base.h:353
@ BIT16
16-bit field.
Definition base.h:360
@ UTYPE_END
End of user-defined type range.
Definition base.h:427
@ ARRAY_OF_SINT
Sequence of SINT.
Definition base.h:394
@ RECORD
Definition base.h:405
@ UNSIGNED16
16-bit unsigned integer.
Definition base.h:376
@ INTEGER48
48-bit signed integer.
Definition base.h:371
@ ARRAY_OF_UDINT
Sequence of UDINT.
Definition base.h:396
@ BIT6
6-bit field.
Definition base.h:350
@ BIT2
2-bit field.
Definition base.h:346
@ INTEGER56
56-bit signed integer.
Definition base.h:372
@ DEFTYPE_FSOEFRAME
Definition base.h:417
@ BITARR16
Array of 16 bits.
Definition base.h:363
@ VISIBLE_STRING
Null-terminated ASCII string.
Definition base.h:389
@ BIT11
11-bit field.
Definition base.h:355
@ BIT4
4-bit field.
Definition base.h:348
@ BIT12
12-bit field.
Definition base.h:356
@ WORD
Two octets (16 bits) unsigned integer.
Definition base.h:342
@ BIT5
5-bit field.
Definition base.h:349
@ BIT15
15-bit field.
Definition base.h:359
@ BOOLEAN
Boolean value (true or false).
Definition base.h:340
@ BIT7
7-bit field.
Definition base.h:351
@ UNSIGNED32
32-bit unsigned integer.
Definition base.h:378
@ DIAGNOSIS_HISTORY
For DIAGNOSIS_HISTORY definition see ETG.1020.
Definition base.h:411
@ INTEGER24
24-bit signed integer.
Definition base.h:368
@ ARRAY_OF_DINT
Sequence of DINT.
Definition base.h:395
@ COMMAND_PAR
For COMMAND_PAR definition see ETG.1000.
Definition base.h:400
@ IDENTITY
For IDENTITY definition see ETG.1000.
Definition base.h:399
@ TIME_DIFFERENCE
Time difference format (implementation-defined).
Definition base.h:424
@ BYTE
8-bit unsigned integer.
Definition base.h:341
@ PDO_MAPPING
For PDO_MAPPING definition see ETG.1000.
Definition base.h:398
@ UNSIGNED24
24-bit unsigned integer.
Definition base.h:377
@ OCTET_STRING
Array of raw bytes.
Definition base.h:390
@ INTEGER8
8-bit signed integer.
Definition base.h:366
@ UTYPE_START
Start of user-defined type range.
Definition base.h:426
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:430
mm::comm::base::PdoMappingEntry parsePdoMapping(uint32_t mapping, uint16_t pdoIndex, uint8_t pdoSubindex)
Parses a 32-bit raw PDO mapping value into a PdoMappingEntry.
Definition base.cc:737
EtherCATVendorID
Enum class representing EtherCAT vendor IDs.
Definition base.h:75
@ SYNAPTICON
Vendor ID for Synapticon GmbH.
Definition base.h:81
@ SENSODRIVE
Vendor ID for Sensodrive GmbH.
Definition base.h:83
ObjectCode
Enum class representing different object types used in the EtherCAT protocol.
Definition base.h:439
@ DEFSTRUCT
Definition structure object.
Definition base.h:441
@ DEFTYPE
Definition type object.
Definition base.h:440
@ ARRAY
Array object type.
Definition base.h:443
@ VAR
Variable object type.
Definition base.h:442
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:293
ObjectFlags SetObjectFlags(ObjectFlags b, ObjectFlags s, ObjectFlags p, ObjectFlags a)
Combine multiple ObjectFlags values into one.
Definition base.h:320
bool isComBinFile(const std::string &filename)
Checks whether a given filename starts with "com_" and ends with ".bin".
Definition base.h:1025
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:503
ObjectFlags operator|(ObjectFlags lhs, ObjectFlags rhs)
Bitwise OR operator for ObjectFlags enum class.
Definition base.h:272
DeviceFileError
Enumeration of possible device file-related error codes.
Definition base.h:1047
@ CommunicationBridgeError
Definition base.h:1065
@ StorageFull
Definition base.h:1051
@ PacketNumber
Definition base.h:1053
@ EmptyContent
Definition base.h:1066
@ FilenameLength
Definition base.h:1061
@ ZipFailure
Definition base.h:1068
@ FlashBusy
Definition base.h:1063
@ NotFound
Definition base.h:1049
@ None
Definition base.h:1048
@ IllegalRequest
Definition base.h:1052
@ BootstrapOnly
Definition base.h:1056
@ Unknown
Definition base.h:1073
@ SuccessAcknowledgement
Definition base.h:1064
@ AlreadyExists
Definition base.h:1054
@ UnzipFailure
Definition base.h:1069
@ RemoveFailure
Definition base.h:1072
@ NoUser
Definition base.h:1055
@ NoRights
Definition base.h:1058
@ FlashFailure
Definition base.h:1070
@ Timeout
Definition base.h:1062
@ AccessDenied
Definition base.h:1050
@ NotBootstrap
Definition base.h:1057
@ Busy
Definition base.h:1060
@ ProgramError
Definition base.h:1059
@ UnexpectedNullTerminator
Definition base.h:1067
@ WriteFailure
Definition base.h:1071
std::vector< mm::comm::base::Parameter::Address > makeAllPdoAddresses()
Generates a complete vector of all PDO addresses for Rx and Tx.
Definition base.cc:766
PdoMappingStatus
Represents the PDO mapping status of a parameter.
Definition base.h:506
@ MAPPED_RX_PDO
The parameter is mapped as a Receive PDO.
Definition base.h:508
@ NOT_MAPPED
The parameter is not mapped in any PDO.
Definition base.h:507
@ MAPPED_TX_PDO
The parameter is mapped as a Transmit PDO.
Definition base.h:509
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:551
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
Alias for a variant that can hold multiple types of values.
Definition base.h:470
void assignGroupsToParameters(std::vector< Parameter > &parameters)
Assigns group names to parameters based on their index and subindex.
Definition base.cc:259
std::vector< mm::comm::base::Parameter::Address > makeParameterAddressIndexRange(uint16_t start, uint16_t end, uint8_t maxSubIndex)
Generates a vector of Parameter addresses in a given index/subindex range.
Definition base.cc:751
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:336
ObjectFlags operator&(ObjectFlags lhs, ObjectFlags rhs)
Bitwise AND operator for ObjectFlags enum class.
Definition base.h:287
ObjectFlags & operator|=(ObjectFlags &lhs, ObjectFlags rhs)
Bitwise OR assignment operator for ObjectFlags enum class.
Definition base.h:302
ObjectFlags
Enum class representing various object flags.
Definition base.h:229
@ ALL_WR
Definition base.h:242
@ ALL_LIST_FLAGS
Definition base.h:259
@ RXPDO_MAP
Definition base.h:251
@ STARTUP
Definition base.h:257
@ OP_RDWR
Definition base.h:247
@ ALL_RDWR
Definition base.h:248
@ PO_RD
Definition base.h:233
@ ALL_RD
Definition base.h:236
@ PO_RDWR
Definition base.h:245
@ None
Definition base.h:230
@ RXTXPDO_MAP
Definition base.h:253
@ SO_WR
Definition base.h:240
@ OP_RD
Definition base.h:235
@ OP_WR
Definition base.h:241
@ SO_RDWR
Definition base.h:246
@ BACKUP
Definition base.h:256
@ SO_RD
Definition base.h:234
@ TXPDO_MAP
Definition base.h:252
@ PO_WR
Definition base.h:239
bool isAppBinFile(const std::string &filename)
Checks whether a given filename starts with "app_" and ends with ".bin".
Definition base.h:1008
bool isFirmwareBinFile(const std::string &filename)
Checks whether the given file is a firmware binary file.
Definition base.h:1039
std::string convertParameterValueToString(const ParameterValue &value)
Converts a ParameterValue to a string representation.
Definition base.cc:240
@ LITTLE
Definition util.h:77
T toInteger(const std::vector< std::uint8_t > &data, std::size_t offset=0, Endianness endian=Endianness::LITTLE)
Converts a byte sequence to an integer of type T with optional offset and endianness.
Definition util.h:103
Definition base.cc:13
Definition base.h:1862
void to_json(nlohmann::json &j, const mm::comm::base::MappedParameterValue &p)
Serialize MappedParameterValue to JSON.
Definition base.h:1921
void from_json(const nlohmann::json &j, mm::comm::base::MappedParameterValue &p)
Deserialize JSON into MappedParameterValue.
Definition base.h:1932
Represents a single mapped parameter value.
Definition base.h:483
uint8_t subindex
Definition base.h:485
uint16_t index
Definition base.h:484
ParameterValue value
Definition base.h:486
Holds collections of mapped parameter values for RX and TX directions.
Definition base.h:495
std::vector< MappedParameterValue > rx
Definition base.h:496
std::vector< MappedParameterValue > tx
Definition base.h:497
Represents a single PDO (Process Data Object) mapping entry.
Definition base.h:130
PdoMappingEntry(uint16_t pdoIndex, uint8_t pdoSubindex, uint16_t index, uint8_t subindex, uint8_t bitlength)
Constructs a PDO mapping entry.
Definition base.h:152
uint16_t index
The object dictionary index (e.g., 0x607A)
Definition base.h:133
PdoMappingEntry()=default
Default constructor for PdoMappingEntry.
uint8_t subindex
The subindex of the object (e.g., 0x00)
Definition base.h:134
uint8_t pdoSubindex
The subindex within the PDO (e.g., 1, 2, etc.)
Definition base.h:132
uint8_t bitlength
The size of the mapped entry in bits (e.g., 16, 32, etc.)
Definition base.h:136
uint16_t pdoIndex
The PDO index (e.g., 0x1600 or 0x1A00)
Definition base.h:131
Represents the mapped PDO entries for a slave device.
Definition base.h:175
std::vector< PdoMappingEntry > txPdos
Definition base.h:178
size_t txSizeBytes() const
Get total size of TxPDOs in bytes.
Definition base.h:219
size_t txSizeBits() const
Get total size of TxPDOs in bits.
Definition base.h:197
size_t rxSizeBytes() const
Get total size of RxPDOs in bytes.
Definition base.h:211
size_t rxSizeBits() const
Get total size of RxPDOs in bits.
Definition base.h:185
std::vector< PdoMappingEntry > rxPdos
Definition base.h:176
Top-level UI configuration JSON structure.
Definition base.h:40
UiPdoMapping pdoMapping
Definition base.h:41
Represents the PDO mapping for RX and TX channels.
Definition base.h:30
std::map< std::uint16_t, std::vector< std::uint32_t > > rx
Definition base.h:31
std::map< std::uint16_t, std::vector< std::uint32_t > > tx
Definition base.h:32
static void to_json(json &j, const mm::comm::base::ParameterValue &value)
Definition base.h:1869
static void from_json(const json &j, mm::comm::base::ParameterValue &value)
Definition base.h:1873