Adding a network driver
How to implement a Hypercolor network driver: the driver-api boundary, DDP/E1.31/HTTP pairing patterns, using WLED and Govee as reference.
Network drivers live in their own crates behind the stable hypercolor-driver-api boundary. They depend on hypercolor-driver-api and hypercolor-types, never on hypercolor-core directly. Native drivers can also use hypercolor-driver-support for credentials, mDNS, control documents, pairing lifecycle, and endpoint validation. The hypercolor-network crate holds only the registry and capability-dispatch shell; protocol logic stays in each driver crate. This page walks through the full lifecycle of adding a new one, using WLED and Govee as concrete reference implementations.
#The driver-api boundary
hypercolor-driver-api defines every trait and type the daemon needs from a driver. The dependency rule is strict:
hypercolor-types ──▶ hypercolor-driver-api ──▶ hypercolor-driver-support
│ │
╰───────────┬─────────────╯
▼
your-driver-crate
│
╭────────────────┴────────────────╮
▼ ▼
hypercolor-network (registry) hypercolor-driver-builtin (bundle)Never reach into hypercolor-core from a driver crate. Core depends on driver-api, not the reverse. The hypercolor-network crate is only the DriverModuleRegistry orchestration layer; it does not own any protocol logic.
The API crate contains only capability contracts and shared wire values. The support crate owns concrete native behavior, so crypto, mDNS, filesystem I/O, and reusable driver policy never leak into the stable trait boundary.
#Key types at a glance
| Type | Module | Role |
|---|---|---|
DriverModule | driver_api::module | Capability root: the entry point the host sees |
DriverDescriptor | driver_api::descriptor | Static ID, display name, transport kind, schema version |
DeviceBackendFactory | driver_api::module | Constructs one finalized output backend |
DriverContext | driver_api::host | Host services a backend is built with: live config, installation identity, runtime actions, optional device input |
DeviceBackend | driver_api::backend | Adopts discoveries and owns connected-device output |
DiscoveryCapability | driver_api::driver_discovery | Async scan returning a DiscoveryBatch of observations plus coverage |
PairingCapability | driver_api::pairing | auth_summary, pair, clear_credentials |
DriverControlProvider | driver_api::controls | Driver-level and device-level control surfaces |
DriverRuntimeCacheProvider | driver_api::module | Persist discovery hints across daemon restarts |
DriverModuleRegistry | hypercolor_network | Host-side lookup and capability dispatch |
#Declaring a descriptor
Every driver crate exposes a static DESCRIPTOR at crate root. DriverDescriptor::new stamps it with the current DRIVER_API_SCHEMA_VERSION automatically; the registry rejects mismatches at load time.
use hypercolor_driver_api::DriverDescriptor;
use hypercolor_types::device::DriverTransportKind;
pub static DESCRIPTOR: DriverDescriptor =
DriverDescriptor::new("acme", "Acme Lights", DriverTransportKind::Network, true, false);
// id display_name transport discovery pairingSet supports_discovery to true if you implement DiscoveryCapability. Set supports_pairing to true if you implement PairingCapability. Govee does; WLED does not (no auth required).
#Implementing DriverModule
DriverModule is the capability root. Implement it on a struct that carries any compile-time configuration the module needs, for example whether mDNS browsing is enabled.
use std::sync::Arc;
use hypercolor_driver_api::{
DeviceBackend, DeviceBackendFactory, DiscoveryCapability,
DriverConfigProvider, DriverConfigView, DriverContext, DriverDescriptor,
DriverError, DriverModule, OutputBinding,
};
use hypercolor_types::identity::BackendId;
pub struct AcmeDriverModule {
mdns_enabled: bool,
}
impl DriverModule for AcmeDriverModule {
fn descriptor(&self) -> &'static DriverDescriptor {
&DESCRIPTOR
}
fn output(&self) -> OutputBinding<'_> {
OutputBinding::Owned {
id: BackendId::new(DESCRIPTOR.id).expect("Acme backend ID must be valid"),
factory: self,
}
}
fn discovery(&self) -> Option<&dyn DiscoveryCapability> {
Some(self)
}
fn config(&self) -> Option<&dyn DriverConfigProvider> {
Some(self)
}
}
impl DeviceBackendFactory for AcmeDriverModule {
fn build(
&self,
_context: DriverContext,
config: DriverConfigView<'_>,
) -> Result<Arc<dyn DeviceBackend>, DriverError> {
let cfg = config.parse_settings::<AcmeConfig>()?;
Ok(Arc::new(AcmeBackend::new(cfg, self.mdns_enabled)))
}
}config is the entry this backend is built for, and the host rebuilds the backend when that entry changes. context carries the host services a backend may keep: a live view of this driver’s config, the installation identity, and retained runtime actions. A driver whose devices exist only because config names them keeps context.config in a hypercolor_driver_support::ConfiguredDevices and resolves every device through it right before opening; discovery metadata may describe such a device but never authorizes one.
Methods you do not need (pairing, controls, runtime_cache, presentation, protocol_catalog) have default implementations that return None. Override only what you support; DriverModule::module_descriptor assembles the capability set automatically from those Option returns.
#Implementing DeviceBackend
DeviceBackend is the hot-path trait the render loop calls on every frame. It is Send + Sync and must not block the async executor.
The backend owns routes. Everything that lives and dies with one connection, the socket included, belongs in a DeviceSession kept in a SessionTable:
use hypercolor_driver_api::{
BackendInfo, DeviceBackend, DeviceFrameSink, DiscoveredDevice, Frame, OutputPolicy,
SkipReason, Submission,
};
use hypercolor_driver_support::{DeviceSession, Session, SessionTable};
use hypercolor_types::device::{DeviceError, DeviceId};
struct AcmeSession {
device_id: DeviceId,
socket: UdpSocket,
}
impl DeviceSession for AcmeSession {
// Facts fixed at connect, readable without waiting for the session's I/O.
type Facts = OutputPolicy;
async fn submit(&mut self, frame: Frame) -> Submission {
let (colors, attempt) = frame.into_parts();
// Encode one frame. Called on every render tick; keep allocations minimal.
// A frame the device already shows settles without touching the wire:
// return attempt.skip(SkipReason::Unchanged).into();
let transmission = attempt.start();
// Send it. A failed send settles with transmission.failed(error).
transmission.written().into()
}
async fn finish(&mut self) -> Result<(), DeviceError> {
// Say goodbye on the wire, within DeviceSession::CLEANUP.
Ok(())
}
// release(self) gives the transport back. The default just drops the
// session; a transport whose close can block awaits that there.
}
#[async_trait::async_trait]
impl DeviceBackend for AcmeBackend {
fn info(&self) -> BackendInfo {
BackendInfo {
id: "acme".into(),
name: "Acme Lights".into(),
description: "Acme UDP streaming backend".into(),
}
}
fn adopt_device(&self, discovered: &DiscoveredDevice) -> Result<(), DeviceError> {
// Validate driver metadata, then install the route under discovered.info.id.
self.routes.insert(discovered)?;
Ok(())
}
async fn connect(&self, id: &DeviceId) -> Result<(), DeviceError> {
// Runs only when this connect starts an open. The future runs on a
// task the table owns, so it takes what it needs from `self` up front.
let id = *id;
self.sessions
.open(&id, || {
let opener = self.opener();
// Open the UDP socket or handshake as required.
async move { opener.open_session(id).await }
})
.await
}
async fn disconnect(&self, id: &DeviceId) -> Result<(), DeviceError> {
self.sessions.close(id).await
}
async fn retire(&self) -> Result<(), DeviceError> {
self.sessions.retire().await
}
async fn write_colors(&self, id: &DeviceId, colors: &[[u8; 3]]) -> Result<(), DeviceError> {
self.sessions.require(id)?.write(colors).await
}
fn frame_sink(&self, id: &DeviceId) -> Option<Arc<dyn DeviceFrameSink>> {
self.sessions.frame_sink(id)
}
fn output_policy(&self, id: &DeviceId) -> Option<OutputPolicy> {
// Govern how often the render loop sends frames to this device.
self.sessions.facts(id).map(|cadence| *cadence)
}
}Each frame carries an Attempt, and the receipt says what became of it. Attempt::start marks the first transport I/O and exactly one terminal call settles it: skip for a frame that was not written (Unchanged, Deferred { retry_at }, or Superseded), reject for a failure before any I/O, and on the started Transmission, written, answered for a peer that replied, or failed. Settling consumes it, so a frame cannot report twice, and an attempt dropped unsettled still reports. Written counts bytes of the submitted frame, three per LED, whatever the wire format adds. A driver that hands the frame to its own worker wraps it in a MailboxFrame, moves the attempt to that worker, and returns Submission::Pending. A lane that has to wait for its own reasons (a quota, a backlog, another application holding the device) answers Readiness::Deferred and skips with its retry_at, and the host offers the latest frame again then.
The table keeps the session contract for you. When disconnect returns Ok, new operations are revoked, admitted I/O has settled or been cancelled, finish ran, tasks started with Session::spawn are joined, and the transport is gone. A frame sink somebody retained holds an empty slot, so it rejects every frame and keeps no socket alive. A reconnect is a new session, and a retired backend closes everything and refuses new sessions. Do not add closed flags, Drop hooks, or your own sink for any of that.
Each device has at most one generation Opening or Open at a time, and a closing generation stays in the table until its transport is gone. Concurrent connects share one open and one result, a connect waits for a close to finish, and retirement waits for both, so one session’s cleanup can never hit the next, even when device state outlives any one connection. A device whose target can move uses open_unless instead of open, which closes a session that no longer matches before the new open starts.
The table owns every open, so a connect that times out only stops waiting. The open keeps going under DeviceSession::OPEN, and if nobody is waiting when it finishes, the session closes through normal cleanup. An open that awaits blocking native work sets OPEN to Duration::MAX, because dropping that wait would detach the work.
A socket every device of the backend sends from stays on the backend, with each session holding a reference, so closing one session leaves the others running. Session::spawn aborts its tasks, so a task must be safe to stop at any await. A task that must finish a write on a link other sessions share stays on the session and stops in finish instead, as OpenRGB’s writer does.
DeviceLifecyclePolicy::default() gives a 5-second connect timeout with inline execution and retry on timeout. Override lifecycle_policy only when your connect call blocks for several seconds and you need ConnectExecution::Background, for example a DTLS handshake like Hue’s Entertainment API.
#Discovery patterns
#WLED: mDNS + HTTP enrichment
WLED discovers via mDNS service type _wled._tcp.local. and then enriches each result with a GET http://<ip>/json/info call that returns firmware version, LED count, RGBW flag, and max FPS. It also probes a list of known IPs for controllers that do not advertise via mDNS, which is common on networks with multicast filtering.
The DiscoveryCapability implementation loads cached probe targets from the previous run via DriverHost::discovery_state().load_cached_json(...), merges them with the current config and tracked devices, then passes the combined list to the scanner:
#[async_trait::async_trait]
impl DiscoveryCapability for WledDriverModule {
async fn discover(
&self,
host: &dyn DriverHost,
request: &DiscoveryRequest,
config: DriverConfigView<'_>,
) -> Result<DiscoveryBatch, DriverError> {
let config = config.parse_settings::<WledConfig>()?;
let tracked = host.discovery_state().tracked_devices(DESCRIPTOR.id).await;
let known_targets = resolve_wled_probe_targets_from_sources(
&config, &tracked, &cached_probe_ips, &cached_targets,
);
let mut scanner = WledScanner::with_known_targets(
known_targets, request.mdns_enabled, request.timeout,
);
scanner.scan().await.map_err(DriverError::discovery)
}
}Device fingerprints use DeviceFingerprint::mint(FingerprintNamespace::Net, "wled", mac) rather than the IP address, so a DHCP reassignment keeps the same DeviceId.
#Reporting what a scan covered
A scan returns observations plus coverage, and the host treats them differently:
- Observations always apply.
DiscoveryObservation::Devicecarries a verified description.DiscoveryObservation::Presentcarries the fingerprint of a device the host already tracks (take it fromDriverTrackedDevice) that answered but could not be described. It renews the device without touching its routes, capabilities, or metadata, so never rebuild a record from tracked or cached fields to keep a device alive. - Coverage decides retirement. A
CoverageGapis anything that could hide a device, and it has one of two scopes.CoverageGap::DeviceUnverifiednames one tracked device: the host holds that device as if seen and the rest of the target can still retire. Every other gap (a failed send, a receive failure, a cancelled probe, a truncated admission, a discovery source that could not run) covers the whole target, so nothing retires. An intended collection window ending is complete, and so is a known target that never answered. - Errors mean nothing ran. Return
Erronly when discovery could not start at all. One failed source must not throw away what the others found.
Bound what unsolicited replies can make a scan do with hypercolor_driver_support::discovery. CandidateAdmission caps retained candidates before any state grows and reserves room for configured and tracked targets. probe_candidates bounds concurrency under a deadline. Both hand back the gaps their bounds cost. Pass tracked targets to MdnsBrowser::browse so a flood of advertisements cannot crowd them out.
#Govee: UDP multicast + optional cloud fallback
Govee has no mDNS. Discovery sends a JSON scan command to the multicast address 239.255.255.250:4001; devices respond on port 4002. Each device is then reachable for control on port 4003.
After the LAN scan, if the user has stored an API key, the driver calls the Govee Developer Cloud API (list_v1_devices) and merges any cloud-only devices into the result set. Cloud devices whose MAC matches a LAN entry are merged in-place; cloud-only devices are added with DiscoveryConnectBehavior::Deferred because they cannot be LAN-controlled without an IP.
// After the LAN scan completes:
if let Some(api_key) = account_api_key(host).await? {
match self.cloud_client(api_key)?.list_v1_devices().await {
Ok(cloud_devices) => merge_cloud_inventory(&mut devices, cloud_devices),
Err(error) => warn!(error = %error, "Govee cloud inventory failed"),
}
}The cloud call is best-effort: a failure logs a warning and does not fail the scan.
mDNS frequently fails on managed networks with multicast filtering, across VLANs, or when systemd-resolved is running as a stub. Always provide a known_ips config field as an escape hatch so users can add IPs manually. WLED, Hue, and Nanoleaf all follow this pattern.
#Pairing
Drivers that require credentials implement PairingCapability. Govee is the reference; WLED intentionally sets supports_pairing: false because it requires no authentication.
The three methods:
#[async_trait::async_trait]
impl PairingCapability for GoveeDriverModule {
async fn auth_summary(
&self,
host: &dyn DriverHost,
device: &TrackedDeviceCtx<'_>,
) -> Result<Option<DeviceAuthSummary>, DriverError> {
// Inspect host.credentials() and return the current auth state.
// Return None only if this driver cannot describe auth for this device at all.
todo!()
}
async fn pair(
&self,
host: &dyn DriverHost,
device: &TrackedDeviceCtx<'_>,
request: &PairDeviceRequest,
) -> Result<PairDeviceOutcome, DriverError> {
// Validate the credential against the remote service, then store it.
let Some(api_key) = api_key_from_request(request) else {
return Ok(PairDeviceOutcome {
status: PairDeviceStatus::InvalidInput,
message: "API key is required.".into(),
auth_state: DeviceAuthState::Open,
activated: false,
});
};
self.cloud_client(api_key.clone())
.map_err(DriverError::pairing)?
.list_v1_devices()
.await
.map_err(DriverError::pairing)?;
host.credentials()
.set_json(DESCRIPTOR.id, "account", serde_json::json!({ "api_key": api_key }))
.await
.map_err(DriverError::pairing)?;
Ok(PairDeviceOutcome {
status: PairDeviceStatus::Paired,
message: "API key validated and stored.".into(),
auth_state: DeviceAuthState::Configured,
activated: false,
})
}
async fn clear_credentials(
&self,
host: &dyn DriverHost,
device: &TrackedDeviceCtx<'_>,
) -> Result<ClearPairingOutcome, DriverError> {
host.credentials()
.remove(DESCRIPTOR.id, "account")
.await
.map_err(DriverError::pairing)?;
Ok(ClearPairingOutcome {
message: "Credentials removed.".into(),
auth_state: DeviceAuthState::Open,
disconnected: false,
})
}
}Describe the pairing flow to the UI by returning a PairingDescriptor from auth_summary. Govee uses PairingFlowKind::CredentialsForm with step-by-step instructions:
PairingDescriptor {
kind: PairingFlowKind::CredentialsForm,
title: "Pair Govee Account".into(),
instructions: vec![
"Open the Govee Home app.".into(),
"Go to Profile, Settings, Apply for API Key.".into(),
"Paste the API key here to validate it and unlock cloud fallback.".into(),
],
action_label: "Validate API Key".into(),
fields: vec![PairingFieldDescriptor {
key: "api_key".into(),
label: "Govee API Key".into(),
secret: true,
optional: false,
placeholder: Some("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".into()),
}],
}#Streaming protocols
#DDP (WLED default)
DDP (Distributed Display Protocol) sends pixel data as a UDP datagram to port 4048. The payload is capped at 1440 bytes to stay under the 1472-byte UDP MTU, limiting a single packet to 480 RGB pixels or 360 RGBW pixels. For longer strips the implementation segments across multiple packets using sequential push flags.
DDP data types: 0x0B for RGB24, 0x1B for RGBW32. The WLED driver exposes a per-device protocol override so individual controllers can run E1.31 while the rest use DDP.
#E1.31 / sACN
E1.31 is the sACN (Streaming ACN) protocol used in professional lighting. It spreads pixels across DMX universes: one universe carries 512 channels, mapping to 170 RGB pixels or 127 RGBW pixels. Use E1.31 only when integrating with an existing sACN workflow. For most setups, DDP is preferred; it is simpler and carries a full strip in fewer packets.
#Govee LAN UDP
Govee uses a JSON-over-UDP control protocol on port 4003. For SKUs with the RAZER_STREAMING capability flag, the driver encodes pixel data as base64 frames at up to 25 fps, with a cap of 255 LEDs per frame. Plain LAN devices without Razer streaming support run at up to 10 fps.
#Bounded HTTP and framing
Every reply from a device is untrusted, so a driver never reads one without a limit. Drivers that make HTTP calls for pairing or discovery enrichment (WLED’s GET /json/info, Govee’s cloud API) use hypercolor_driver_support::http and do not depend on reqwest directly; deny.toml fails CI if one does.
A request cannot go out without a ReadBudget, which sets an absolute deadline for the whole exchange and a byte cap the body is held to as it arrives, whatever Content-Length claims. Choose the cap per endpoint from what that endpoint can legitimately return, and say why in a comment: one small global cap breaks large inventories. Share a single client in a LazyLock at crate root rather than building one per call:
use std::sync::LazyLock;
use std::time::Duration;
use hypercolor_driver_support::http::{BoundedHttpClient, BoundedResponse, ReadBudget};
const INFO_TIMEOUT: Duration = Duration::from_secs(5);
/// Largest info reply read. The device serializes it from a 32 KiB buffer.
const INFO_REPLY_BYTES: usize = 128 * 1024;
static HTTP_CLIENT: LazyLock<Result<BoundedHttpClient, String>> =
LazyLock::new(|| BoundedHttpClient::new().map_err(|e| e.to_string()));
fn http_client() -> anyhow::Result<&'static BoundedHttpClient> {
HTTP_CLIENT
.as_ref()
.map_err(|e| anyhow::anyhow!("HTTP client unavailable: {e}"))
}
async fn fetch_info(url: &str) -> anyhow::Result<serde_json::Value> {
let info = http_client()?
.get(url)
.send(ReadBudget::new(INFO_REPLY_BYTES, INFO_TIMEOUT))
.await
.and_then(BoundedResponse::error_for_status)?
.json()
.await?;
Ok(info)
}Protocols that speak delimited lines or frames over TCP use hypercolor_driver_support::framing. BoundedLines caps every line and reads on an absolute deadline; pass the same deadline to every call of a multi-line reply so a slow peer cannot extend it. next_after_idle is for a long-lived connection that may legitimately stay silent: it waits as long as the peer does, then holds any line the peer starts to a completion bound.
FramedWriter owns its stream and drops it when a frame fails, times out, or is abandoned, so a half-written frame can never be followed by anything. It flushes every frame, which makes it wrong for serial ports: flushing a tty blocks a runtime thread until the UART drains.
#Config and control surfaces
Expose driver-level configuration via DriverConfigProvider. The default_config method returns a DriverConfigEntry::enabled(...) with a BTreeMap<String, serde_json::Value> of default settings. validate_config parses and validates the config on user changes.
WLED’s driver-level config covers known_ips, default_protocol, realtime_http_enabled, and dedup_threshold. Per-device overrides (for example, an individual WLED controller using E1.31 while others use DDP) are exposed separately through DriverControlProvider::device_surface.
ApplyImpact (in hypercolor_types::controls) tells the host what needs to happen when a field changes. All eight variants:
| Variant | Meaning |
|---|---|
ApplyImpact::None | No operational impact |
ApplyImpact::Live | Takes effect immediately, no reconnect |
ApplyImpact::DiscoveryRescan | A fresh discovery scan should run |
ApplyImpact::DeviceReconnect | Only the affected device reconnects |
ApplyImpact::BackendRebind | Backend must be torn down and rebuilt |
ApplyImpact::TopologyRebuild | Topology is rebuilt for the affected devices |
ApplyImpact::HardwarePersist | State is written into the physical hardware |
ApplyImpact::Custom(String) | A driver-defined dynamic impact |
#Runtime cache
Implement DriverRuntimeCacheProvider so discovered device addresses survive daemon restarts. The snapshot method serializes whatever you need into a BTreeMap<String, serde_json::Value>; the host writes it to disk and feeds it back via DriverHost::discovery_state().load_cached_json(...) on the next boot.
WLED serializes probe_ips and probe_targets. Govee serializes probe_devices. Keep the cached data compact; it is loaded synchronously at discovery startup.
#[async_trait::async_trait]
impl DriverRuntimeCacheProvider for AcmeDriverModule {
async fn snapshot(
&self,
host: &dyn DriverHost,
) -> anyhow::Result<BTreeMap<String, serde_json::Value>> {
let tracked = host.discovery_state().tracked_devices(DESCRIPTOR.id).await;
let probe_ips = collect_probe_ips(&tracked);
Ok(BTreeMap::from([(
"probe_ips".into(),
serde_json::to_value(probe_ips)?,
)]))
}
}#Registering in the bundle
Add your crate as an optional feature-gated dependency of hypercolor-driver-builtin and register the module in register_driver_modules:
// In hypercolor-driver-builtin/src/lib.rs
#[cfg(feature = "acme")]
use hypercolor_driver_acme::AcmeDriverModule;
// Inside register_driver_modules():
#[cfg(feature = "acme")]
registry.register(AcmeDriverModule::new(config.discovery.mdns_enabled))?;Also add a normalize_driver_config_entries call so the driver’s config entry is created on first run:
#[cfg(feature = "acme")]
config
.drivers
.entry(hypercolor_driver_acme::DESCRIPTOR.id.to_owned())
.or_default();DriverModuleRegistry::register verifies that your DriverDescriptor::schema_version matches DRIVER_API_SCHEMA_VERSION. A mismatch returns DriverModuleRegistryError::SchemaVersionMismatch and prevents the daemon from starting.
#Tests
Tests live in crates/hypercolor-driver-<name>/tests/, not inline #[cfg(test)] blocks. Name each file after the capability being tested: discovery_tests.rs, pairing_tests.rs, and so on.
At minimum, cover:
- Config round-trips:
default_configdeserializes cleanly viaparse_settings. - Known-IP merging:
resolve_*_probe_targets_from_sourcesmerges config, tracked devices, and cache without duplicates. - Fingerprint stability: the same MAC address produces the same
DeviceIdacross independent calls. - Pairing validation: missing fields and empty credentials return
PairDeviceStatus::InvalidInput, not an error. - The session contract: implement
SessionRigfromhypercolor-driver-conformanceover your emulator and callassert_session_contract. Every driver runs this same test.
just test-crate hypercolor-driver-acmecargo check --workspace does not cover hypercolor-ui, but it does cover your new driver crate as long as you add it to the workspace Cargo.toml. Run just verify (fmt + lint + test) before opening a PR.
#Cross-links
- Network device ports and mDNS service types: @/hardware/network-devices.md
- WLED setup and streaming parameters: @/hardware/wled.md
- Govee setup, LAN ports, and cloud pairing: @/hardware/govee.md
- HAL (USB/HID/SMBus) driver contribution guide: @/contributing/adding-a-driver.md
- Render pipeline and
BackendManager::write_frame: @/architecture/render-pipeline.md