11#include <nlohmann/json_fwd.hpp>
16#include <unordered_map>
72 std::map<uint8_t, DeviceParameterValue>
values;
81 return std::unexpected(
82 std::format(
"object 0x{:04X} has no readable subindex {:02X}",
index,
subindex));
84 if (
const auto* p = std::get_if<T>(&it->second)) {
87 return std::unexpected(
88 std::format(
"parameter 0x{:04X}:{:02X} holds a different type",
index,
subindex));
139 const std::string&
name()
const;
207 std::expected<std::vector<uint8_t>, std::string>
readSdo(uint16_t index, uint8_t
subindex)
const;
220 std::span<const uint8_t> data)
const;
230 const std::string& filename)
const;
237 std::expected<void, mm::comm::FoeError>
writeFile(
const std::string& filename,
238 std::span<const uint8_t> data)
const;
247 std::expected<void, std::string>
readRegister(uint16_t address, std::span<uint8_t> data)
const;
256 std::expected<void, std::string>
writeRegister(uint16_t address,
257 std::span<const uint8_t> data)
const;
266 std::expected<std::vector<uint8_t>, std::string>
readSii()
const;
277 std::expected<void, std::string>
writeSii(std::span<const uint8_t> data)
const;
300 bool useCompleteAccess =
true);
375 uint16_t index, uint8_t
subindex, std::span<const uint8_t> bytes);
417 std::optional<DeviceParameter>
parameter(uint16_t index, uint8_t
subindex)
const;
482 std::expected<void, std::string>
readAllParameters(
bool useCompleteAccess =
true);
504 std::expected<ObjectValues, std::string>
readObject(uint16_t index,
505 bool useCompleteAccess =
true);
542 template <
typename T>
558 template <
typename T>
562 return std::unexpected(v.error());
564 if (
const auto* p = std::get_if<T>(&*v)) {
567 return std::unexpected(
568 std::format(
"parameter 0x{:04X}:{:02X} holds a different type", index,
subindex));
575 template <
typename T>
581 template <
typename T>
583 return writeValue<T>(address.
index, address.
subindex, std::move(newValue));
608 template <
typename T>
611 std::lock_guard<std::mutex> lock(*parametersMutex_);
614 return p->getValue<T>();
617 return readValue<T>(index,
subindex);
666 template <
typename T>
700 template <
typename T>
702 static_assert(std::is_arithmetic_v<T>,
"the cell holds arithmetic types only");
703 static_assert(
sizeof(T) <=
sizeof(uint64_t),
"the cell holds at most eight bytes");
709 std::memcpy(&bits, &newValue,
sizeof(T));
718 template <
typename T>
724 template <
typename T>
726 return setValue<T>(address.
index, address.
subindex, newValue);
747 void publishParameters(std::unordered_map<uint32_t, DeviceParameter>&& built);
757 void readParameterValues(std::vector<DeviceParameter>& defs,
bool useCompleteAccess);
768 bool recordDeclaredSubindexRead(
bool ok);
773 bool declaredSubindexReadsWorthTrying()
const;
792 bool readObjectComplete(uint16_t index, std::span<const uint8_t> subindices,
793 bool useCompleteAccess);
803 std::expected<std::vector<PdoMappingObject>, std::string> readPdoAssignment(
804 uint16_t assignmentIndex);
816 std::expected<FlatPdoMapping, std::string> readSiiPdoMapping();
828 std::expected<std::vector<DeviceParameter>, std::string> buildSiiParameterDefinitions();
830 uint16_t slavePosition_;
842 uint32_t productCode_;
843 uint32_t revisionNumber_;
844 uint32_t serialNumber_;
848 uint16_t mailboxProtocols_ = 0;
869 std::unique_ptr<std::mutex> parametersMutex_;
873 std::expected<void, std::string> readParameterDefinitions(
bool readValues,
874 bool useCompleteAccess);
880 bool parametersUnavailable_ =
false;
903 std::unique_ptr<std::deque<DeviceParameter>> cells_;
906 std::unordered_map<uint32_t, DeviceParameter*> parameters_;
910 std::vector<std::unordered_map<uint32_t, DeviceParameter*>> retiredMaps_;
930void to_json(nlohmann::json& j,
const Device& d);
Abstract interface for an EtherCAT fieldbus driver.
Definition fieldbus_driver.h:404
Represents a single node on the fieldbus.
Definition device.h:116
bool setValue(uint16_t index, uint8_t subindex, T newValue)
Sets a scalar parameter's value as T. Lock-free, non-allocating.
Definition device.h:701
std::expected< void, std::string > writeSdo(uint16_t index, uint8_t subindex, std::span< const uint8_t > data) const
Writes an object dictionary entry to the device (CoE SDO download).
Definition device.cc:109
std::expected< DeviceParameterValue, std::string > readParameter(uint16_t index, uint8_t subindex)
Reads a parameter value, keeping the cached store in sync.
Definition device.cc:1073
std::expected< void, std::string > readRegister(uint16_t address, std::span< uint8_t > data) const
Reads bytes from an ESC register on this device.
Definition device.cc:124
DeviceParameter * findParameter(uint16_t index, uint8_t subindex)
Parameter lookup by (index, subindex). O(1); nullptr if absent. Takes no lock.
Definition device.cc:1063
std::expected< void, std::string > initializeParameters(bool readValues=false, bool useCompleteAccess=true)
Enumerates the device's CoE object dictionary and populates parameters().
Definition device.cc:142
std::optional< DeviceParameterValue > parameterValue(uint16_t index, uint8_t subindex) const
Returns a copy of a parameter's last known value. No bus access.
Definition device.cc:1036
std::expected< T, std::string > readCachedValue(uint16_t index, uint8_t subindex)
Read-once typed accessor for objects the caller knows are immutable.
Definition device.h:609
uint32_t serialNumber() const
Serial number from EEPROM.
Definition device.cc:68
std::expected< void, std::string > writeParameter(uint16_t index, uint8_t subindex, const DeviceParameterValue &newValue)
Writes a parameter value, always updating the cache first.
Definition device.cc:1346
std::expected< std::vector< uint8_t >, std::string > readSdo(uint16_t index, uint8_t subindex) const
Reads an object dictionary entry from the device (CoE SDO upload).
Definition device.cc:104
std::expected< void, std::string > readAllParameters(bool useCompleteAccess=true)
Refreshes the cached value of every readable parameter, keeping the list intact.
Definition device.cc:1137
bool hasParameters() const
Whether the object dictionary is enumerated (thread-safe; no bus access).
Definition device.cc:584
bool supportsCoe() const
Whether the slave advertises a CoE mailbox — a fixed capability, not a live check.
Definition device.cc:92
std::expected< void, std::string > writePdoMapping(const PdoMapping &mapping)
Writes a new PDO mapping to the device via SDO, then reads it back to verify.
Definition device.cc:936
std::expected< void, std::string > writeRegister(uint16_t address, std::span< const uint8_t > data) const
Writes bytes to an ESC register on this device.
Definition device.cc:129
const std::string & name() const
Human-readable node name from SII EEPROM.
Definition device.cc:56
std::expected< void, std::string > writeValue(uint16_t index, uint8_t subindex, T newValue)
Typed convenience wrapper for writeParameter.
Definition device.h:543
bool setValue(ObjectAddress< T > address, T newValue)
setValue<T>() addressed by an ObjectAddress. Lock-free, non-allocating.
Definition device.h:725
std::optional< DeviceParameter > parameter(uint16_t index, uint8_t subindex) const
Looks up one parameter by (index, subindex) and returns a copy of it. O(1).
Definition device.cc:1045
uint32_t revisionNumber() const
Revision number from EEPROM.
Definition device.cc:67
const FlatPdoMapping & flatPdoMapping() const
Returns the device's PDO mapping. Empty until readFlatPdoMapping() succeeds.
Definition device.cc:836
bool exchangesProcessData() const
Whether the device is in a process-data-exchanging state (SAFE-OP or OP, error bit clear).
Definition device.cc:96
std::expected< PdoMapping, std::string > readPdoMapping()
Reads the device's PDO mapping grouped by mapping object (0x16xx / 0x1Axx).
Definition device.cc:690
std::vector< DeviceParameter > parametersOrdered() const
Returns all parameters sorted ascending by (index, subindex).
Definition device.cc:1024
bool parametersUnavailable() const
Whether any initializeParameters call on this device failed since it was scanned (thread-safe; no bus...
Definition device.cc:589
std::expected< void, std::string > readFlatPdoMapping()
Reads the device's PDO mapping from its assignment and mapping objects.
Definition device.cc:790
std::expected< std::vector< uint8_t >, mm::comm::FoeError > readFile(const std::string &filename) const
Reads a file from this device via File over EtherCAT (FoE).
Definition device.cc:114
std::expected< DeviceParameterValue, std::string > setValueFromBytes(uint16_t index, uint8_t subindex, std::span< const uint8_t > bytes)
Sets the parameter's value from its raw on-the-wire bytes (the bytes-domain setter).
Definition device.cc:1005
std::expected< ObjectValues, std::string > readObject(uint16_t index, bool useCompleteAccess=true)
Reads every readable sub-entry of one object and returns the decoded values.
Definition device.cc:1293
std::expected< T, std::string > readValue(ObjectAddress< T > address)
readValue<T>() addressed by an ObjectAddress. Blocking; off the RT thread only.
Definition device.h:576
bool mailboxActive() const
Whether the device's CoE/SDO mailbox is currently active (AL state PRE-OP, SAFE-OP,...
Definition device.cc:80
uint32_t productCode() const
Product code from EEPROM.
Definition device.cc:66
std::expected< void, std::string > writeSii(std::span< const uint8_t > data) const
Writes a raw SII (EEPROM) image to this device.
Definition device.cc:138
std::expected< T, std::string > readValue(uint16_t index, uint8_t subindex)
Typed convenience wrapper for readParameter.
Definition device.h:559
std::expected< void, std::string > writeValue(ObjectAddress< T > address, T newValue)
writeValue<T>() addressed by an ObjectAddress. Blocking; off the RT thread only.
Definition device.h:582
bool isCia402() const
Whether this device implements the CiA402 drive profile.
Definition device.cc:70
uint16_t slavePosition() const
Returns the 1-based position of this node on the fieldbus.
Definition device.cc:55
std::string productName() const
Canonical product name, independent of the SII EEPROM contents.
Definition device.cc:57
std::optional< uint16_t > dataType(uint16_t index, uint8_t subindex) const
Returns a parameter's declared ETG.1020 data-type code, thread-safely (cache lock).
Definition device.cc:1054
std::optional< T > value(uint16_t index, uint8_t subindex) const
Reads a scalar parameter's current value as T. Lock-free, non-allocating.
Definition device.h:667
uint32_t vendorId() const
Vendor ID from EEPROM.
Definition device.cc:65
std::optional< T > value(ObjectAddress< T > address) const
value<T>() addressed by an ObjectAddress. Lock-free, non-allocating.
Definition device.h:719
std::expected< void, mm::comm::FoeError > writeFile(const std::string &filename, std::span< const uint8_t > data) const
Writes a file to this device via File over EtherCAT (FoE).
Definition device.cc:119
std::expected< std::vector< uint8_t >, std::string > readSii() const
Reads this device's raw Slave Information Interface (SII / EEPROM) image.
Definition device.cc:134
On-disk cache of CoE parameter definitions, keyed by device identity.
Definition parameter_cache.h:53
uint8_t subindex
Definition esi_entry.cc:283
Definition bus_health_source.h:7
std::variant< int8_t, int16_t, int32_t, int64_t, uint8_t, uint16_t, uint32_t, uint64_t, float, double, std::string, std::vector< uint8_t > > DeviceParameterValue
Decoded value of a single device parameter (CoE object dictionary entry).
Definition device_parameter.h:33
DeviceParameterValue defaultValueForDataType(uint16_t dataType)
Returns a zero-equivalent value for the given ETG.1020 dataType.
Definition device_parameter.cc:58
void to_json(nlohmann::json &j, const Cia402Status &s)
Serialises a Cia402Status. Emits state/modeName as human-readable strings alongside the numeric statu...
Definition cia402_drive.cc:13
DeclaredSubindexReads
Whether a device serves the subindices an array or a record declares but does not hold.
Definition device.h:66
@ Synced
value matches the device (last successful read or write).
CompleteAccessSupport
Runtime discovery state of CoE Complete Access support. CA is optional in CoE and the EEPROM capabili...
Definition device.h:41
std::expected< int, std::string > reconcileDetectedModules(const Device &device)
Reconciles a device's Configured Module Ident List with its Detected list.
Definition device.cc:1434
A structured FoE failure. String-like where a caller only forwards it (operator<<,...
Definition foe_error.h:109
A single object dictionary entry held by a Device.
Definition device_parameter.h:194
SyncState syncState
Freshness of the value relative to the device.
Definition device_parameter.h:214
void storeBits(uint64_t v) const
Stores the scalar cell. Lock-free, non-allocating, relaxed — safe from the RT loop.
Definition device_parameter.h:229
uint16_t dataType
ETG.1020 data type code (e.g. 0x0007 = UNSIGNED32).
Definition device_parameter.h:199
std::optional< T > scalar() const
Reads the value as T straight out of the cell. Lock-free, non-allocating.
Definition device_parameter.h:252
A device's complete PDO mapping across both directions.
Definition pdo_mapping.h:62
The address of one object-dictionary entry, carrying the type that entry holds.
Definition device_parameter.h:63
uint16_t index
CoE object index.
Definition device_parameter.h:64
uint8_t subindex
CoE object subindex.
Definition device_parameter.h:65
Decoded values of one object's readable sub-entries, as returned by Device::readObject.
Definition device.h:70
std::map< uint8_t, DeviceParameterValue > values
Decoded value per readable subindex.
Definition device.h:72
uint16_t index
CoE object index the values belong to.
Definition device.h:71
std::expected< T, std::string > get(uint8_t subindex) const
Returns the value of subindex as T — type-exact, like DeviceParameter::getValue.
Definition device.h:78
A device's desired PDO configuration to write and assign — the write-side input to Device::writePdoMa...
Definition pdo_mapping.h:89
The live process-data runtime: the published image, the cross-thread exchange buffers,...
Definition process_data.h:36