Device-domain runtime and inventory
The device domain turns HID++ transport into a stable view of Logitech devices and safe device actions. It coordinates inventory, pairing, sessions, writes, diagnostics, and replay above host-specific transport. Its public boundary keeps these product actions usable with native, scripted, and portable backends. See Device integration stack, HID device access and discovery, Logitech HID++ protocol support, and Device registry.
Device-domain boundary
The device domain gives devices meaning above HID++ transport. It selects probe paths, describes identity and capability, resolves routes, and owns the life cycle of device actions.
Backend contract
openlogi_device::backend::HidBackend separates device policy from host hardware access. It supplies current HID nodes, the nodes that can carry HID++ traffic, HID++ channels, raw output writers, and hotplug events. The device crate stays independent of the host operating system, so the same product contract can run with a native backend, a scripted backend, or a portable backend.
The backend reduces host errors to two product-level cases: a disconnected device or another backend failure. Hotplug events carry no device identity, so inventory treats them as hints to run a fresh enumeration. A backend node identity is valid only for the current operating-system node; physical identity comes from device data and must not use that node identity as a persisted key.
Raw output is a separate escape path for reports that HID++ cannot model. It supports device-specific actions such as Litra lighting without adding host types or raw-report rules to the higher device domain.
References: crates/…/backend.rs• L1-14 crates/…/backend.rs• L24-58 crates/…/backend.rs• L60-74 crates/…/backend.rs• L127-177
Device routes
A DeviceRoute is the stable address used by later operations. It distinguishes receiver slots, direct HID++ devices, and standalone raw-HID devices. Route identity is separate from the live channel, which lets inventory replace a connection without changing the address used by clients.
SharedChannel carries a route and its open channel. The channel registry publishes the current route owner and signals when that publication changes. Sessions and writes use that shared view instead of opening competing readers.
Raw-device routes use a runtime identity derived from the node's serial when available. If no serial exists, they use the backend node identity for the current runtime only. Neither value replaces the device identity learned from HID++ data.
References: crates/…/lib.rs• L41-46 crates/…/backend.rs• L60-124 crates/…/inventory.rs• L65-113
Host arbitration
Host arbitration protects HID traffic shared by separate OpenLogi processes. A receiver register phase uses an exclusive host lock because HID++ 1.0 register replies do not carry a software ID. A process that cannot obtain the phase defers its receiver probe instead of reading or writing registers without protection.
The lock is released by the operating system when its holder exits. This prevents a crashed process from leaving receiver access blocked forever.
References: crates/…/host_lock.rs• L1-12 crates/…/host_lock.rs• L75-113 crates/…/host_lock.rs• L132-165
Inventory model
Inventory is the current statement of which hardware is present and what the last trusted probe learned. The persistent enumerator keeps open channels, probe facts, event subscriptions, and health state across reconciliation passes.
Inventory reconciliation
Reconciliation starts with HID++ nodes from the backend. It reuses open channels, opens new nodes concurrently, probes receiver or direct paths, and publishes a settled inventory. A receiver probe can describe several paired devices. A direct probe describes the device at its own HID++ index.
The enumerator treats hotplug and HID++ events as hints for another authoritative pass. It also retains a live cached channel when the operating system briefly omits its node. Duplicate candidates cannot create two live channels for one node.
The main flow is shown below.
flowchart LR
A[Backend nodes] --> B[Enumerator]
B --> C[Reuse or open channel]
C --> D{Probe path}
D -->|Receiver| E[Receiver and slots]
D -->|Direct| F[Direct device]
E --> G[Identity capability and battery data]
F --> G
G --> H[Settled inventory]
H --> I[Route and channel registry]
J[Lifecycle or setting hint] --> B
References: crates/…/inventory.rs• L173-225 crates/…/inventory.rs• L468-589 crates/…/inventory.rs• L608-771 crates/…/channel_cache.rs• L62-73
Probe trust
A healthy probe is authoritative. A failed probe means the node could not be checked. An incomplete probe means the receiver answered but could not produce a full device list. A deferred probe means another process owns the receiver register phase and this process did no device I/O.
The health ledger can replay the last complete inventory for a bounded period after a failure. It then retires the channel so a later pass can open a fresh one. Deferral does not count as failure and does not age cache entries. A receiver that answers liveness but cannot replay arrival data remains distinct from a dead transport.
References: crates/…/host_lock.rs• L145-165 crates/…/probe.rs• L260-289 crates/…/inventory.rs• L146-170 crates/…/publication.rs• L232-319
Receiver and direct probes
Receiver probes combine live arrival evidence with receiver pairing records. This keeps a paired device visible while it sleeps, uses another host, or has not yet produced a live arrival report. An online slot receives a feature and battery probe. An offline slot uses receiver identity data without probing the device.
Direct probing addresses the device itself. It accepts a node only when a completed feature walk shows a real peripheral through battery or useful configuration capability. This filters receiver-side HID interfaces while preserving a transient failure as a failure rather than treating it as device absence.
References: crates/…/probe.rs• L359-445 crates/…/direct.rs• L31-177 crates/…/unifying.rs• L31-75 crates/…/publication.rs• L5-80
Probe cache
The cache keeps slow-changing identity, capability, feature-index, event-index, and battery-source facts between passes. A cache hit can refresh battery state without repeating the full feature walk. Cache freshness is time based.
Cache keys follow safe identity. Bolt entries can use a stable unit identity for warm start. Unifying entries use receiver identity and slot only for the running process. Direct entries use the current HID node identity. Live battery state is not restored as current state after restart. An unavailable reading keeps known state, while an explicit unlinked result clears the reading and marks the paired device offline.
References: crates/…/inventory.rs• L53-63 crates/…/inventory.rs• L774-888 crates/…/features.rs• L154-205 crates/…/cache.rs• L15-145
Identity and capability mapping
The probe maps device-reported identity and feature support into the product model. The device's own name and type take precedence over a generic host label. Capabilities come from the live feature table and capability checks. Device kind remains identity data and does not decide which controls are supported.
The probe chooses a battery source and records event-capable feature indexes. It supports unified, legacy, voltage, ADC, and HID++ 1.0 register battery paths. A partial identity or capability read can serve the current pass, but it cannot replace complete cached evidence.
References: crates/…/features.rs• L47-130 crates/…/features.rs• L154-205 crates/…/features.rs• L351-379 crates/…/features.rs• L487-595 crates/…/direct.rs• L90-169
Lifecycle events
The inventory-owned channel listener decodes device lifecycle, battery, and setting broadcasts. These events do not edit inventory directly. They coalesce into a request for another authoritative reconciliation.
Setting broadcasts advance a per-device settings revision before the reconciliation. Clients use that revision to know that they must re-read the setting. Backlight events carry the latest display value separately. Lock-key events compare the new state with one state read per connection and send only the keys that changed.
References: crates/…/events.rs• L1-21 crates/…/events.rs• L196-293 crates/…/events.rs• L346-401 crates/…/lock_keys.rs• L1-97
Channel publication
After a probe settles, inventory publishes the routes reachable through the current channel. Publication is the live link between inventory and operations. It prevents sessions and writes from using a stale or competing channel.
An offline receiver slot withdraws its device route but keeps the receiver transport available. A failed channel retires and drains before a later pass opens its replacement. A replacement is reported only when the device stayed published through the repair, so clients can re-apply volatile state without treating every channel repair as a new arrival.
References: crates/…/inventory.rs• L692-735 crates/…/channel_cache.rs• L14-27 crates/…/channel_cache.rs• L103-201 crates/…/publication.rs• L156-230
Session runtime
Sessions are long-lived actions that change device reporting or listen for device input. They use one shared channel, watch for replacement, and restore temporary device state before they finish.
Capture sessions
Capture sessions divert selected controls and convert HID++ reports into neutral CapturedInput values. Gesture capture supports gesture sources, buttons, DPI or ModeShift controls, wheels, and onboard controls. Keyboard capture supports HID++ controls plus keypad and gaming-cluster reports.
The shared lifecycle arms controls, publishes its channel for related writes to reuse, listens for reports, re-arms after a wireless reconnect, and stops on request or channel replacement. A watched session uses bounded liveness checks and reports a dead channel so inventory can replace it. A wake-feature failure ends the session epoch so its owner can retry instead of keeping a session that cannot re-arm.
Keyboard bindings can change while the session runs. The session hands back controls that leave the set and diverts only controls that join it. A G-key row is taken as one unit when requested. A keypad grid is observed from raw reports without changing device state.
Thumbwheel capture uses the device's native resolution and direction when available. Devices without the dedicated thumbwheel feature can use Gestures2 gesture 46. If safe decoding data is missing, the wheel stays native.
References: crates/…/gesture.rs• L1-24 crates/…/gesture.rs• L61-127 crates/…/gesture.rs• L210-249 crates/…/arm.rs• L240-274 crates/…/arm.rs• L387-468 crates/…/keyboard.rs• L1-35 crates/…/keyboard.rs• L125-151 crates/…/keyboard.rs• L895-907 crates/…/gesture46.rs• L156-230
Session restoration
Capture records the reporting state it owns and restores only those changes. Pointer controls restore diversion and raw motion state. Keyboard controls restore diversion without changing raw keyboard motion. Wheels restore their prior reporting mode and direction. A taken G-key row is released as one unit.
If the original channel is gone, the session returns a pending restore token. Restoration retries through the current publication and succeeds only while that publication stays current. An offline receiver slot waits for the exact route to return. The listener remains active until cleanup finishes.
References: crates/…/capture_restore.rs• L71-104 crates/…/capture_restore.rs• L252-332 crates/…/capture_restore.rs• L347-435 crates/…/capture_restore.rs• L484-563 crates/…/session_replay_tests.rs• L50-110
Host switching
Host switching coordinates a keyboard with linked pointing devices that share host choices. It changes linked devices before the keyboard so the keyboard channel remains available for the full operation.
The session accepts a keyboard announcement, a diverted host-key press, or an analytics report. Host names are advisory. A linked device changes slot only when its destination is valid and not explicitly empty; missing or unreadable slot status does not block the change, and a non-empty name can override a bad empty status. The session keeps rollback state and reports unfinished restoration if a publication changes during the switch.
The one-device host-switch path can also check the device's unit identity before its fire-and-forget host change. This prevents a receiver slot that now holds another device from receiving a request meant for the earlier device. An unknown identity skips this comparison.
References: crates/…/host_switch.rs• L1-30 crates/…/host_switch.rs• L1090-1144 crates/…/restore.rs• L14-158 crates/…/tests.rs• L151-191 crates/…/tests.rs• L371-431
Pairing flows
Pairing is a receiver session that changes receiver state and reports progress. The device domain owns receiver register access so inventory and other processes do not consume the same replies.
Receiver pairing
Bolt pairing reports nearby unpaired devices, pairs a selected device, and guides a passkey step when needed. Unifying opens a pairing window and waits for a compatible device. Both flows report search, progress, success, failure, and cancellation.
The assigned slot appears through the next authoritative inventory pass. Pairing changes receiver state first and does not publish a temporary product record.
References: crates/…/pairing.rs• L1-24 crates/…/pairing.rs• L206-231 crates/…/pairing.rs• L284-415 crates/…/pairing.rs• L444-559
Pairing cleanup
Pairing owns cancellation and notification cleanup. An unsuccessful flow sends its cancel action, restores notification flags, and releases the receiver register phase. Unpairing uses the route to select receiver family, identity, and slot. Direct and raw-HID routes cannot use receiver unpairing.
References: crates/…/pairing.rs• L401-441 crates/…/pairing.rs• L607-660 crates/…/registers.rs• L6-39 crates/…/session_replay_tests.rs• L649-712
Write abstractions
The write layer exposes route-based reads and writes for device controls. It resolves a route to a channel and device index, then selects a feature path from the device's reported support.
Route-based operations
One public operation can address a receiver-backed or direct HID++ device because route resolution owns that difference. Route-based operations open a route for a standalone call. _on operations reuse a SharedChannel already owned by inventory or a session.
Feature eligibility is checked when the feature is opened. A missing, hidden, or non-user-configurable feature is not exposed as a usable control. Typed errors distinguish unsupported controls, invalid values, transport failure, and device refusal.
References: crates/…/write.rs• L1-9 crates/…/write.rs• L123-189 crates/…/write.rs• L192-217
Device identity guards
Some writes must prove that the device on a route is still the device that started the operation. The optional HID++ DeviceInformation unit ID provides that proof when the device exposes it, while a missing feature means that no comparison is possible.
The shared unit-ID read also gives these writes one error contract. A timeout belongs to the operation that requested the identity, and other failures remain feature errors. Haptic restoration and RGB control release use the recorded identity before changing device state.
References: crates/…/unit_id.rs• L16-52 crates/…/lib.rs• L77-105 crates/…/haptic.rs• L226-372 crates/…/rgb.rs• L29-58 crates/…/rgb.rs• L261-303
Device control families
The write surface presents common product actions such as pointer and wheel settings, Fn lock, backlight, DPI, SmartShift, haptics, lighting, Easy-Switch hosts, report rate, disabled keys, force sensing, sidetone, and onboard profile reads.
Several products expose different feature paths for one behavior. The write layer selects a supported path and maps it to one product value. It bounds requests by device capabilities and preserves fields that the caller does not own.
Applied state matters more than request acceptance. Where firmware can clip, ignore, or normalize a value, the write reports an echo or read-back result. This keeps the product view aligned with device state.
Temporary effects add ownership to that model. Haptic sessions restore only an enable state they applied and leave later changes alone. RGB writes keep a lease for the device that accepted control and return control only to that same device.
References: crates/…/write.rs• L44-118 crates/…/haptic.rs• L226-372 crates/…/rgb.rs• L29-58 crates/…/rgb.rs• L261-303 crates/…/tests.rs• L135-195 crates/…/tests.rs• L509-592
Onboard profile reads
Onboard profile reads expose stored profile state without changing it. They report onboard or host mode, the active profile, and the profile directory. A failed individual name read does not hide the profile entry, while failure of the main state or directory makes the view incomplete.
References: crates/…/write.rs• L92-93 crates/…/onboard.rs• L19-75
Diagnostic reads
Diagnostic reads expose firmware evidence for support work without changing the product model. They can list feature support and reprogrammable controls, show raw battery data, and report each declared firmware entity.
An entity-local refusal becomes an unreadable row so the report keeps the device's declared count. A channel failure aborts the dump because it describes a missing route rather than a bad firmware record. Raw battery output keeps source values such as voltage instead of presenting an estimate as wire truth.
References: crates/…/diagnostics.rs• L26-75 crates/…/diagnostics.rs• L198-219 crates/…/diagnostics.rs• L316-418 crates/…/diagnostics.rs• L496-597 crates/…/tests.rs• L1151-1246
Raw-HID lighting
Litra lights use HID++ illumination when that path is available. Otherwise the write layer uses a model-specific raw-HID report. It validates the complete raw-HID identity, maps values into the model's range, and serializes commands per route.
The HID++ path keeps a live link, reads state before use, and returns applied state. A temporary backend refusal is retryable and does not prove that raw HID is required. The raw path has no read-back, so callers receive no applied state from it.
References: crates/…/litra.rs• L1-16 crates/…/litra.rs• L78-107 crates/…/litra.rs• L181-235 crates/…/litra.rs• L320-389
Replay and capture support
Replay provides controlled runtime seams for testing device behavior. It supplies a virtual backend, strict HID traffic, mutable topology, input reports, raw writers, and ordering barriers.
Replay topology
Replay models virtual nodes, shared channels, receiver slots, connection state, raw writers, and hotplug delivery. Tests can change topology, connection state, slot state, and report delivery independently. This lets tests distinguish a missing node, a disconnected channel, an offline paired device, and a device that refuses HID++.
Single-device fixtures derive topology from a route and profile. The profile's settings route must match its inventory so replay uses the same route and channel rules as production.
References: crates/…/session_replay_tests.rs• L435-538 crates/…/replay_tests.rs• L283-388 crates/…/replay_tests.rs• L481-657
Replay contracts
Replay uses strict cassette matching. Unexpected requests, unused required exchanges, or receiver-slot state that contradicts the cassette fail the test. Response barriers and native-write completion barriers make ordering and cleanup visible.
Replay checks ownership as well as feature results. Session replay covers arming, shared-channel publication, restoration, wake recovery, keyboard retargeting, pairing cancellation, and channel replacement. Inventory replay covers recovery, battery changes, event coalescing, and route publication.
References: crates/…/session_replay_tests.rs• L50-235 crates/…/session_replay_tests.rs• L321-432 crates/…/session_replay_tests.rs• L649-712 crates/…/replay_tests.rs• L138-230 crates/…/replay_tests.rs• L989-1074
Runtime lifecycle
Inventory supplies the authoritative route and channel view. Sessions and writes borrow it. Pairing changes receiver state and becomes visible through the next reconciliation.
sequenceDiagram
participant Backend
participant Inventory
participant Registry
participant Operation
Backend->>Inventory: Enumerate nodes
Inventory->>Backend: Open or reuse channel
Inventory->>Inventory: Probe and settle
Inventory->>Registry: Publish routes
Operation->>Registry: Resolve current route
Registry-->>Operation: Shared channel
Operation->>Operation: Read, write, capture, or pair
Inventory-->>Registry: Replace or remove stale channel
Registry-->>Operation: Stop, restore, or retry
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the device crate independent of host APIs. Alternate and portable backends must still satisfy the same public contract.
- Keep backend node identities runtime-only. Use device-reported identity for persistence and replacement checks.
- Keep hotplug events as re-enumeration hints. Do not make an identity-free event select or remove a route directly.
- Keep the backend error boundary narrow. Higher layers must distinguish disconnection from other backend failures without depending on host error types.
- Keep route variants aligned with inventory publication and route opening.
- Keep receiver register arbitration around every register read or write. A deferred probe must not become a failed probe.
- Keep probe verdicts separate from inventory data. Failed, incomplete, deferred, and healthy results have different effects on replay and retry.
- Keep channel retirement one-channel-per-node and reopen only after the old channel drains.
- Keep receiver pairing evidence separate from live device evidence so offline paired slots remain visible.
- Keep cache keys tied to safe identity. Do not restore live battery state as current after restart.
- Keep unavailable battery, unlinked battery, and coarse battery results distinct.
- Keep feature-table capability evidence separate from device kind. A partial capability read must trigger repair rather than hide support.
- Keep lifecycle events as reconciliation hints. Advance setting revisions before the requested pass, and keep backlight and lock-key indicators separate from inventory.
- Keep exact route publication changes visible to sessions. An offline slot is not the same as a retired transport.
- Keep capture ownership, liveness restart, wake re-arm, and native restoration aligned for every control family.
- Keep keyboard retargeting and G-key-row ownership aligned with teardown state.
- Keep receiver slot checks advisory. Refuse an explicit empty slot, but do not turn missing or unreadable status into an empty result.
- Keep unit-ID reads behind the shared write abstraction, and preserve the operation-specific timeout and feature-error mapping.
- Keep temporary haptic and RGB ownership tied to the device identity and the state each operation actually changed.
- Keep pairing cancellation and notification restoration inside the receiver register phase.
- Keep feature fallbacks narrow. Verify the applied value after writes that firmware can normalize.
- Keep diagnostic entity-local failures distinct from channel failures.
- Keep Litra raw-HID matching tied to the complete route identity and do not hide a real HID++ refusal with raw fallback.
- Keep replay topology, cassettes, response barriers, and completion checks aligned with production ownership rules.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A failed or incomplete probe becomes an empty inventory | Devices disappear during a short transport failure | Replay the last trusted inventory through bounded ledger grace |
| A receiver register phase is used without host arbitration | Another OpenLogi process consumes the register reply | Defer the probe when the shared phase stays locked |
| A hotplug event is treated as a complete device update | Inventory can publish stale routes or miss a node in the changed device tree | Use the event to trigger authoritative enumeration |
| A backend node identity is persisted as physical identity | A later node or machine can inherit the wrong device state | Use serial or HID++ device identity for persisted or replacement-sensitive state |
| A receiver slot is offline but its pairing record is ignored | A known paired device disappears from inventory | Keep the slot in inventory without publishing a live device route |
| A direct node is rejected after an incomplete walk | A real device is mistaken for a receiver-side interface | Treat an incomplete walk as a failed probe and retain last-known identity |
| A cache key uses only a reusable receiver slot | A later device inherits stale identity or capability data | Use stable Bolt identity for persistence and runtime-only keys elsewhere |
| A setting broadcast only queues a scan | A client misses that it must re-read the setting | Advance the device settings revision before reconciliation |
| A session writes through a retired channel | Cleanup or a setting change targets stale hardware | Stop on publication replacement and restore through the current route |
| A host switch treats an unreadable slot check as proof that the slot is empty | A valid host change is blocked by a transient or unsupported status read | Refuse only an explicit empty result; treat unknown status as advisory |
| Capture cleanup drops its listener before restoration | Diverted controls can remain dead | Keep the listener active until firmware cleanup finishes |
| A thumbwheel is diverted without safe resolution data | Scrolling has the wrong scale or direction | Leave the wheel native when required decode data is unavailable |
| A keyboard binding change re-arms every key | Native key actions run during the change | Diff the wanted set and write only controls that leave or join |
| A write trusts request acceptance instead of applied state | The product shows a value firmware ignored or clipped | Verify with an echo or read-back |
| A lease or restore writes without checking the recorded unit identity | A replacement device on the same route receives another device's cleanup | Compare the unit ID before the write and leave a different device alone |
| A restore overwrites state changed by another writer | A user or another host loses its newer setting | Restore only when the current state still matches the state owned by the session |
| A diagnostic drops an unreadable firmware entity | A support report understates the device's firmware table | Preserve the declared entity as an unreadable row |
| Litra falls back to raw HID after a real HID++ refusal | A device error is hidden and state can diverge | Use raw fallback only when HID++ cannot be resolved |
| Replay accepts unmatched traffic or contradictory slot state | Tests pass while ownership or ordering is wrong | Require strict cassette completion and barrier checks |
Key files
| File | Why to read it |
|---|---|
| crates/…/lib.rs | Public device-domain boundary and exports. |
| crates/…/backend.rs | Host-independent backend contract, node identity, hotplug hints, and raw output boundary. |
| crates/…/host_lock.rs | Host-wide receiver register arbitration. |
| crates/…/inventory.rs | Reconciliation, channel reuse, health settlement, and publication. |
| crates/…/channel_cache.rs | Channel retirement and replacement lifecycle. |
| crates/…/events.rs | Lifecycle hints, setting revisions, and transient indicators. |
| crates/…/lock_keys.rs | Per-connection lock-key state tracking. |
| crates/…/probe.rs | Probe paths, trust verdicts, and budgets. |
| crates/…/direct.rs | Direct-device recognition and filtering. |
| crates/…/features.rs | Identity, capabilities, and battery sources. |
| crates/…/gesture.rs | Neutral capture inputs and gesture-session behavior. |
| crates/…/arm.rs | Control ownership, wheel capture, and wake re-arm. |
| crates/…/gesture46.rs | Legacy thumbwheel capture and restoration. |
| crates/…/keyboard.rs | Keyboard, keypad, gaming-cluster, and retargeted capture. |
| crates/…/capture_restore.rs | Restore plans and current-route cleanup. |
| crates/…/host_switch.rs | Keyboard-led host switching and identity-safe host changes. |
| crates/…/thumbwheel.rs | Thumbwheel reporting modes, scaling, and event decoding. |
| crates/…/pairing.rs | Pairing progress, cancellation, cleanup, and unpairing. |
| crates/…/write.rs | Route-based control API and feature eligibility. |
| crates/…/unit_id.rs | Shared device identity read and error contract for write preflights. |
| crates/…/haptic.rs | Haptic arming, identity-safe restoration, and cached feature use. |
| crates/…/rgb.rs | RGB control claims, device identity leases, and hand-back. |
| crates/…/diagnostics.rs | Feature, battery, control, and firmware evidence. |
| crates/…/litra.rs | HID++ and raw-HID light control. |
| crates/…/session_replay_tests.rs | Production-path session ownership and teardown checks. |
| crates/…/replay_tests.rs | Inventory recovery, battery events, and channel replacement. |
| crates/…/tests.rs | Cross-feature applied-state and fallback contracts. |