9#include <nlohmann/json_fwd.hpp>
23static_assert(std::endian::native == std::endian::little,
24 "parameter values are stored as little-endian wire bytes");
31 uint8_t, uint16_t, uint32_t, uint64_t,
33 std::string, std::vector<uint8_t>>;
39 return (
static_cast<uint32_t
>(index) << 8) |
static_cast<uint32_t
>(
subindex);
163 if (
this != &other) {
174 uint64_t
load()
const {
return std::atomic_ref<uint64_t>(
value).load(std::memory_order_relaxed); }
177 std::atomic_ref<uint64_t>(
value).store(v, std::memory_order_relaxed);
183 static_assert(std::atomic_ref<uint64_t>::is_always_lock_free,
184 "the parameter cell must be lock-free so the RT path never blocks");
185 static_assert(
alignof(uint64_t) >= std::atomic_ref<uint64_t>::required_alignment,
186 "the parameter cell must satisfy std::atomic_ref's alignment requirement");
252 template <
typename T>
254 static_assert(std::is_arithmetic_v<T>,
"the cell holds arithmetic types only");
255 static_assert(
sizeof(T) <=
sizeof(uint64_t),
"the cell holds at most eight bytes");
264 std::memcpy(&out, &v,
sizeof(T));
305 template <
typename T>
308 if (
const auto* p = std::get_if<T>(&v)) {
311 return std::unexpected(
312 std::format(
"parameter 0x{:04X}:{:02X} holds a different type",
index,
subindex));
321 std::expected<double, std::string>
numeric()
const;
343 template <
typename T>
344 std::expected<void, std::string>
setValue(
const T& v) {
361std::expected<DeviceParameterValue, std::string>
decodeSdoBytes(uint16_t dataType,
362 std::span<const uint8_t>
bytes);
377std::expected<std::vector<uint8_t>, std::string>
encodeSdoBytes(uint16_t dataType,
384void to_json(nlohmann::json& j,
const DeviceParameter& p);
uint8_t subindex
Definition esi_entry.cc:283
EsiEntrySource origin
Definition esi_entry.cc:62
Definition bus_health_source.h:7
std::string_view syncStateName(SyncState state)
Returns the lowercase string form of state ("unknown" etc.).
Definition device_parameter.cc:259
std::expected< std::vector< uint8_t >, std::string > encodeSdoBytes(uint16_t dataType, const DeviceParameterValue &value)
Serialises a DeviceParameterValue to raw SDO bytes — the inverse of decodeSdoBytes.
Definition device_parameter.cc:197
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:60
std::expected< DeviceParameterValue, std::string > decodeSdoBytes(uint16_t dataType, std::span< const uint8_t > bytes)
Decodes a raw SDO byte sequence according to an ETG.1020 data type code.
Definition device_parameter.cc:151
uint64_t packLeBits(std::span< const uint8_t > bytes)
Packs up to eight raw little-endian wire bytes into a uint64_t (zero-extended).
Definition device_parameter.cc:136
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
ParameterOrigin
Where a parameter's definition (schema/metadata) came from.
Definition device_parameter.h:123
@ Sii
Derived from the SII EEPROM PDO categories (no CoE mailbox).
@ ObjectDictionary
Enumerated over the CoE object dictionary (SDO-Info).
size_t scalarByteWidth(uint16_t dataType)
Width in bytes of a value of dataType, or 0 when it is not a scalar.
Definition device_parameter.cc:121
std::optional< double > numericValue(const DeviceParameterValue &value)
Coerces a DeviceParameterValue to a double, when it holds a number.
Definition device_parameter.cc:246
void unpackLeBits(uint64_t bits, std::span< uint8_t > out)
Unpacks bits into out, little-endian. The inverse of packLeBits.
Definition device_parameter.cc:145
std::string_view parameterOriginName(ParameterOrigin origin)
Returns the string form of origin ("objectDictionary" / "sii").
Definition device_parameter.cc:273
bool isScalarDataType(uint16_t dataType)
Whether a value of dataType is stored in DeviceParameter::bits.
Definition device_parameter.cc:96
SyncState
Tracks how a cached parameter value relates to the device.
Definition device_parameter.h:106
@ Pending
value was set locally while offline / after a failed write.
@ Synced
value matches the device (last successful read or write).
@ Unknown
Never read; value is the type-appropriate default.
@ Stale
value came from an earlier read or write; the latest read of the object failed.
constexpr uint32_t makeParameterKey(uint16_t index, uint8_t subindex)
Packs an object dictionary index and subindex into a single 32-bit key.
Definition device_parameter.h:38
std::size_t bytes
Definition spoe_connection.cc:24
A single object dictionary entry held by a Device.
Definition device_parameter.h:195
uint16_t access
ObjAccess bitfield (read/write per-state flags).
Definition device_parameter.h:202
SyncState syncState
Freshness of the value relative to the device.
Definition device_parameter.h:215
void markReadFailed()
Records that a read of this object failed. Keeps the value.
Definition device_parameter.h:292
std::optional< DeviceParameterValue > defaultValue
Slave-reported default, when available.
Definition device_parameter.h:217
ScalarCell cell
Scalar value, LSB-aligned little-endian, zero-extended.
Definition device_parameter.h:213
std::expected< void, std::string > setValue(const DeviceParameterValue &v)
Sets the value to v, encoded as encode does. Does not touch syncState; the caller decides that.
Definition device_parameter.cc:421
uint64_t loadBits() const
Loads the scalar cell. Lock-free, non-allocating — safe from the RT loop.
Definition device_parameter.h:227
std::vector< uint8_t > rawValueBytes() const
Returns the value's raw wire bytes at the object's declared width.
Definition device_parameter.cc:300
uint16_t objectCode
OTYPE_VAR / OTYPE_ARRAY / OTYPE_RECORD (ETG.1000.6 §5).
Definition device_parameter.h:199
std::optional< uint32_t > unit
ETG.1004 unit code, when reported.
Definition device_parameter.h:216
std::string name
Textual description from ecx_readOE.
Definition device_parameter.h:198
void setRawValue(std::span< const uint8_t > bytes)
Replaces the stored value with bytes, its raw little-endian wire encoding.
Definition device_parameter.cc:310
uint16_t bitLength
Bit length of the value.
Definition device_parameter.h:201
ParameterOrigin origin
Where this definition came from.
Definition device_parameter.h:203
uint16_t index
CoE object index.
Definition device_parameter.h:196
std::optional< DeviceParameterValue > maxValue
Slave-reported maximum, when available.
Definition device_parameter.h:219
std::expected< void, std::string > setValue(const T &v)
Strongly-typed setValue overload; wraps v and converts as above.
Definition device_parameter.h:344
DeviceParameterValue currentValue() const
Returns the current value as a DeviceParameterValue, built on the spot.
Definition device_parameter.cc:283
void storeBits(uint64_t v) const
Stores the scalar cell. Lock-free, non-allocating, relaxed — safe from the RT loop.
Definition device_parameter.h:230
uint8_t subindex
CoE object subindex.
Definition device_parameter.h:197
uint16_t dataType
ETG.1020 data type code (e.g. 0x0007 = UNSIGNED32).
Definition device_parameter.h:200
std::expected< T, std::string > getValue() const
Returns the value as the requested type T.
Definition device_parameter.h:306
std::expected< std::vector< uint8_t >, std::string > encode(const DeviceParameterValue &v) const
Encodes v as the wire bytes of the parameter's declared data type. Stores nothing.
Definition device_parameter.cc:370
bool isReadable() const
Whether the object is readable in any state (ETG.1000.6 ObjAccess read bits 0-2).
Definition device_parameter.h:352
std::vector< uint8_t > rawValue
Non-scalar value (string / byte array) as wire bytes.
Definition device_parameter.h:214
std::optional< DeviceParameterValue > minValue
Slave-reported minimum, when available.
Definition device_parameter.h:218
uint32_t key() const
Returns the packed (index, subindex) key used in the parameter map.
Definition device_parameter.h:222
std::optional< T > scalar() const
Reads the value as T straight out of the cell. Lock-free, non-allocating.
Definition device_parameter.h:253
std::expected< double, std::string > numeric() const
Returns the value coerced to a double, for any numeric parameter.
Definition device_parameter.cc:318
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
One scalar value, and the only part of a DeviceParameter that two threads touch at once.
Definition device_parameter.h:155
uint64_t value
Definition device_parameter.h:156
uint64_t load() const
Reads the value. Lock-free, non-allocating — safe from the RT loop.
Definition device_parameter.h:174
ScalarCell & operator=(ScalarCell &&other) noexcept
Definition device_parameter.h:168
ScalarCell(const ScalarCell &other)
Definition device_parameter.h:160
ScalarCell(ScalarCell &&other) noexcept
Definition device_parameter.h:161
ScalarCell & operator=(const ScalarCell &other)
Definition device_parameter.h:162
void store(uint64_t v) const
Writes the value. Lock-free, non-allocating — safe from the RT loop.
Definition device_parameter.h:176