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);
90uint64_t
packLeBits(std::span<const uint8_t> bytes);
162 if (
this != &other) {
173 uint64_t
load()
const {
return std::atomic_ref<uint64_t>(
value).load(std::memory_order_relaxed); }
176 std::atomic_ref<uint64_t>(
value).store(v, std::memory_order_relaxed);
182 static_assert(std::atomic_ref<uint64_t>::is_always_lock_free,
183 "the parameter cell must be lock-free so the RT path never blocks");
184 static_assert(
alignof(uint64_t) >= std::atomic_ref<uint64_t>::required_alignment,
185 "the parameter cell must satisfy std::atomic_ref's alignment requirement");
251 template <
typename T>
253 static_assert(std::is_arithmetic_v<T>,
"the cell holds arithmetic types only");
254 static_assert(
sizeof(T) <=
sizeof(uint64_t),
"the cell holds at most eight bytes");
263 std::memcpy(&out, &v,
sizeof(T));
291 template <
typename T>
294 if (
const auto* p = std::get_if<T>(&v)) {
297 return std::unexpected(
298 std::format(
"parameter 0x{:04X}:{:02X} holds a different type",
index,
subindex));
307 std::expected<double, std::string>
numeric()
const;
321 template <
typename T>
322 std::expected<void, std::string>
setValue(
const T& v) {
351std::expected<DeviceParameterValue, std::string>
decodeSdoBytes(uint16_t dataType,
352 std::span<const uint8_t> bytes);
367std::expected<std::vector<uint8_t>, std::string>
encodeSdoBytes(uint16_t dataType,
374void 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:257
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:195
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
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:149
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:134
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:122
@ 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:119
std::optional< double > numericValue(const DeviceParameterValue &value)
Coerces a DeviceParameterValue to a double, when it holds a number.
Definition device_parameter.cc:244
void unpackLeBits(uint64_t bits, std::span< uint8_t > out)
Unpacks bits into out, little-endian. The inverse of packLeBits.
Definition device_parameter.cc:143
std::string_view parameterOriginName(ParameterOrigin origin)
Returns the string form of origin ("objectDictionary" / "sii").
Definition device_parameter.cc:269
bool isScalarDataType(uint16_t dataType)
Whether a value of dataType is stored in DeviceParameter::bits.
Definition device_parameter.cc:94
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.
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
A single object dictionary entry held by a Device.
Definition device_parameter.h:194
uint16_t access
ObjAccess bitfield (read/write per-state flags).
Definition device_parameter.h:201
bool inRange(const DeviceParameterValue &v) const
Whether v lies within [minValue, maxValue].
Definition device_parameter.cc:371
SyncState syncState
Freshness of the value relative to the device.
Definition device_parameter.h:214
std::optional< DeviceParameterValue > defaultValue
Slave-reported default, when available.
Definition device_parameter.h:216
ScalarCell cell
Scalar value, LSB-aligned little-endian, zero-extended.
Definition device_parameter.h:212
std::expected< void, std::string > setValue(const DeviceParameterValue &v)
Sets value, coercing v into the parameter's declared data type.
Definition device_parameter.cc:322
uint64_t loadBits() const
Loads the scalar cell. Lock-free, non-allocating — safe from the RT loop.
Definition device_parameter.h:226
std::vector< uint8_t > rawValueBytes() const
Returns the value's raw wire bytes at the object's declared width.
Definition device_parameter.cc:296
uint16_t objectCode
OTYPE_VAR / OTYPE_ARRAY / OTYPE_RECORD (ETG.1000.6 §5).
Definition device_parameter.h:198
std::optional< uint32_t > unit
ETG.1004 unit code, when reported.
Definition device_parameter.h:215
std::string name
Textual description from ecx_readOE.
Definition device_parameter.h:197
void setRawValue(std::span< const uint8_t > bytes)
Replaces the stored value with bytes, its raw little-endian wire encoding.
Definition device_parameter.cc:306
uint16_t bitLength
Bit length of the value.
Definition device_parameter.h:200
ParameterOrigin origin
Where this definition came from.
Definition device_parameter.h:202
uint16_t index
CoE object index.
Definition device_parameter.h:195
std::optional< DeviceParameterValue > maxValue
Slave-reported maximum, when available.
Definition device_parameter.h:218
std::expected< void, std::string > setValue(const T &v)
Strongly-typed setValue overload; wraps v and coerces as above.
Definition device_parameter.h:322
DeviceParameterValue currentValue() const
Returns the current value as a DeviceParameterValue, built on the spot.
Definition device_parameter.cc:279
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
DeviceParameterValue clampToRange(const DeviceParameterValue &v) const
Returns v clamped to [minValue, maxValue].
Definition device_parameter.cc:389
uint8_t subindex
CoE object subindex.
Definition device_parameter.h:196
uint16_t dataType
ETG.1020 data type code (e.g. 0x0007 = UNSIGNED32).
Definition device_parameter.h:199
std::expected< T, std::string > getValue() const
Returns the value as the requested type T.
Definition device_parameter.h:292
bool isReadable() const
Whether the object is readable in any state (ETG.1000.6 ObjAccess read bits 0-2).
Definition device_parameter.h:330
std::vector< uint8_t > rawValue
Non-scalar value (string / byte array) as wire bytes.
Definition device_parameter.h:213
std::optional< DeviceParameterValue > minValue
Slave-reported minimum, when available.
Definition device_parameter.h:217
uint32_t key() const
Returns the packed (index, subindex) key used in the parameter map.
Definition device_parameter.h:221
std::optional< T > scalar() const
Reads the value as T straight out of the cell. Lock-free, non-allocating.
Definition device_parameter.h:252
std::expected< double, std::string > numeric() const
Returns the value coerced to a double, for any numeric parameter.
Definition device_parameter.cc:314
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:154
uint64_t value
Definition device_parameter.h:155
uint64_t load() const
Reads the value. Lock-free, non-allocating — safe from the RT loop.
Definition device_parameter.h:173
ScalarCell & operator=(ScalarCell &&other) noexcept
Definition device_parameter.h:167
ScalarCell(const ScalarCell &other)
Definition device_parameter.h:159
ScalarCell(ScalarCell &&other) noexcept
Definition device_parameter.h:160
ScalarCell & operator=(const ScalarCell &other)
Definition device_parameter.h:161
void store(uint64_t v) const
Writes the value. Lock-free, non-allocating — safe from the RT loop.
Definition device_parameter.h:175