Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

viva-genapi — the NodeMap

viva-genapi turns the XmlModel produced by viva-genapi-xml into a NodeMap: the thing that knows what ExposureTime means, where it lives, what it depends on, and whether you are allowed to write it right now.

It has no transport of its own. Every accessor takes a &dyn RegisterIo, which is the whole coupling between this crate and the wire:

pub trait RegisterIo {
    fn read(&self, addr: u64, len: usize) -> Result<Vec<u8>, GenApiError>;
    fn write(&self, addr: u64, data: &[u8]) -> Result<(), GenApiError>;
}

Two implementations ship with the stack: GigeRegisterIo and U3vRegisterIo in viva-genicam, and NullIo here — which returns zeroes for reads and discards writes. NullIo is not a placeholder: it is how a GenApi document is browsed with no camera attached, which is what Viva Studio and the WASM build do. Values that depend only on the XML — SwissKnife expressions over constants, enum entry lists, ranges, the category tree — come out correct; anything backed by a real register comes out zero, so read structure from it, not data.


Node kinds

VariantGenICam name
Node::IntegerInteger, IntReg, MaskedIntReg
Node::FloatFloat, FloatReg
Node::EnumEnumeration
Node::BooleanBoolean
Node::CommandCommand
Node::CategoryCategory
Node::SwissKnifeSwissKnife, IntSwissKnife
Node::ConverterConverter
Node::IntConverterIntConverter
Node::StringStringReg, String (a <Value>, or delegating through <pValue>)
Node::RegisterRegister (a <Length>, or a <pLength> read each time)

Node::kind_name() returns the GenICam name, name() the feature name, and access_mode() the declared mode — which is not the same as the effective one, see below.


Reading and writing

The accessors are typed, and each takes the transport:

let width  = nodemap.get_integer("Width", &io)?;
let expo   = nodemap.get_float("ExposureTime", &io)?;
let fmt    = nodemap.get_enum("PixelFormat", &io)?;
let on     = nodemap.get_bool("ReverseX", &io)?;
let serial = nodemap.get_string("DeviceSerialNumber", &io)?;

nodemap.set_integer("Width", 640, &io)?;
nodemap.exec_command("AcquisitionStart", &io)?;

Camera in viva-genicam wraps these behind string-valued get/set, which is what most application code should use — see Registers & features.

Limits

A write is checked against the feature’s Min, Max and Inc before anything is sent — and before it is forwarded through <pValue>, so a limit declared on Width binds even though the register behind it declares none. Many cameras report their limits at run time instead (<pMin>, <pMax>, <pInc> pointing at other nodes); those are read when the write happens. A feature that declares no limit of its own takes the one of the node it delegates to. A value outside them is GenApiError::Range.

NodeMap::feature_state reports the same limits — the bounds in force now — and a <Converter> reports its raw node’s range mapped through its formula, in the order its <Slope> says (a Decreasing converter turns the raw maximum into its minimum).

An <Integer> with a <Value> and nothing behind it is a floating value: writing it changes what it reads, within its limits, and nothing reaches the camera. Selectors on several cameras (AVT’s TriggerSelector, Sony’s UserSetSelector) are built this way.

Commands

exec_command writes the value the command’s XML names — a constant <CommandValue>, or the current value of the node <pCommandValue> points at (Hikrobot’s UserSetSave writes the selected user set’s number) — after checking the command’s own pIsAvailable and pIsLocked. It returns when the write is acknowledged. is_done(name, &io) is GenICam’s IsDone: a command whose register is write-only is done at once; otherwise the register is read from the camera, past every cache, and the command is done when it no longer holds the command value. Camera::execute_and_wait polls it.

Addressing

A node’s address is not always a constant. It can be:

  • Fixed — an <Address> in the XML.
  • Computed — a sum of <Address>, <pAddress> (another node’s value) and <Integer> offsets, resolved at access time.
  • Delegated — <pValue> pointing at another node, which may itself delegate. The declaration you are reading is often not the one holding the data.

This matters because it is where the interesting bugs live. A parser can accept a document perfectly and the address model still be wrong, which is why the vendor corpus test does not stop at parsing: it builds a NodeMap from each real document and evaluates every node.

Selectors

When a feature is selector-dependent (Gain behind GainSelector), the selector’s current value takes part in resolving the address. Writing the selector invalidates the cached values of everything that depends on it, so the next read of Gain returns the newly selected channel rather than a stale value.

SwissKnife and Converter

SwissKnife nodes compute a value from other nodes with a formula — arithmetic, comparisons, bitwise operators, and a ternary. Converter and IntConverter run a formula in both directions, so they are writable: the FROM expression maps a user value back to the raw one. Evaluation is transparent — you call get_float and the inputs are read or computed first.


Access mode is evaluated, not declared

AccessMode in the XML is a starting point. The effective mode also depends on predicates the device answers at runtime:

  • pIsImplemented — the feature is absent on this model.
  • pIsAvailable — present but not currently applicable.
  • pIsLocked — writable in principle, locked right now.

effective_access_mode, is_implemented, is_available and available_enum_entries expose this. Writes go through it: a locked node is refused locally, with GenApiError::Locked { name, locked_by } naming the feature that holds the lock — because “access denied” leaves a caller nowhere to go, whereas “ExposureTime is locked by ExposureTime_Lck” says what to change first.


Introspection

Built for consumers that need to render a feature tree rather than read one value:

MethodReturns
node_names()Every feature name
node(name)The Node, or None
categories()Category name → its members
dependents(name)Which nodes are invalidated when this one changes
nodes_at_visibility(level)Beginner / Expert / Guru / Invisible filtering
version()The document’s schema version
skipped()Nodes that could not be built
ignored()Child elements of the nodes that were built, read past without interpretation

skipped() is the important one. A construct this crate cannot handle does not fail the whole document — it lands here, and the parser’s own losses (XmlModel::skipped) are carried along with it, so a consumer sees both. The camera still opens, minus the features that could not be built.

ignored() lists the smaller losses: an element of a node that loaded, which the parser read past. Most are documentation (<Extension>, an entry’s <ToolTip>); one that changes a value is worth reporting, as a skipped node is. viva-camctl report prints both.


Caching and invalidation

Values are cached and invalidated by dependency: writing a node clears the cached values of everything dependents() lists for it, including through pValue delegation and selectors — a selector names the features it selects in <pSelected>, and writing it clears them and the registers behind them. Cache correctness is a conformance question, not an optimisation — a stale Width after a selector change is a wrong answer, not a slow one.


Errors

GenApiError is specific on purpose; the variant usually says what to do next. It is #[non_exhaustive], so a match on it needs a wildcard arm.

VariantMeans
NodeNotFound(name)No such feature in this camera’s document
Type { name, kind, expected }Asked for the wrong type — get_float on an Integer
Access { name, refused }The node’s access mode forbids the read or write refused names
Locked { name, locked_by }pIsLocked is engaged; locked_by is the feature to change
InvalidText { name, expected, text }Text that is not a value of the feature’s type
Range { name, value, violation }Value below Min, above Max, or off Inc
Unavailable(name)pIsImplemented or pIsAvailable says the feature cannot be used now
NoValueSource { name }The node declares no register, <pValue> or <Value> to read
Io(source)The transport failed; source is the transport’s error (GigeError, U3vError)
RegisterEncoding { name, width, problem }The register’s bytes do not convert — the camera’s answer, not the XML
Declaration { name, problem }The XML declares the node in a way that cannot be evaluated
ExprParse / ExprEval / UnknownVariableA SwissKnife formula failed to parse, evaluate, or resolve a reference
EnumValueUnknown / EnumNoSuchEntryRaw value maps to no entry, or no entry has that name
BadIndirectAddresspAddress resolved to something impossible
BitfieldOutOfRange / ValueTooWideBitfield metadata exceeds the register, or the value exceeds the field
NotAttached { node, port }A chunk or event feature read with no frame or event attached to its port
PortNotWritable { node, port }A write to a chunk or event port, which holds data the camera sent
PortOutOfRangeA chunk or event register reaches past the data attached to its port

See also