openlogi-hid 0.6.25

HID++ device discovery for OpenLogi, wrapping the hidpp crate over async-hid.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//! Raw HID driver for Logitech Litra lights.
//!
//! Litra is deliberately implemented beside, not inside, the HID++ feature
//! writers. The driver owns product matching, semantic-range conversion, and
//! the fixed report encoding; the generic transport only owns enumeration and
//! opening the selected raw HID node.

use std::collections::HashMap;
use std::sync::{Arc, LazyLock};
use std::time::Duration;

use async_hid::AsyncHidWrite as _;
use openlogi_core::device::{LightCapabilities, LightValueRange, LightValueUnit};
use serde::{Deserialize, Serialize};
use tokio::sync::{Mutex, OwnedMutexGuard};
use tracing::debug;

use crate::route::DeviceRoute;

use super::WriteError;

/// Logitech vendor ID.
pub const LOGITECH_VENDOR_ID: u16 = 0x046d;
/// Stable driver-family identifier carried by standalone inventory records.
pub const LITRA_DRIVER_ID: &str = "litra";
/// Litra Glow product ID.
pub const LITRA_GLOW_PRODUCT_ID: u16 = 0xc900;
/// Litra Beam product ID.
pub const LITRA_BEAM_PRODUCT_ID: u16 = 0xc901;
/// Litra vendor usage page.
pub const LITRA_USAGE_PAGE: u16 = 0xff43;
/// Litra Glow usage ID.
pub const LITRA_USAGE_ID: u16 = 0x0202;

const REPORT_LEN: usize = 20;
const REPORT_ID: u8 = 0x11;
const REPORT_PREFIX: [u8; 2] = [0xff, 0x04];
const COMMAND_POWER: u8 = 0x1c;
const COMMAND_BRIGHTNESS: u8 = 0x4c;
const COMMAND_TEMPERATURE: u8 = 0x9c;
const MIN_BRIGHTNESS_LUMENS: u16 = 20;
const MAX_BRIGHTNESS_LUMENS: u16 = 250;
const MIN_TEMPERATURE_KELVIN: u16 = 2700;
const MAX_TEMPERATURE_KELVIN: u16 = 6500;
const TEMPERATURE_STEP_KELVIN: u16 = 100;
const RAW_WRITE_TIMEOUT: Duration = Duration::from_secs(2);

const fn validated_range(min: u16, max: u16, step: u16, unit: LightValueUnit) -> LightValueRange {
    match LightValueRange::new(min, max, step, unit) {
        Ok(range) => range,
        Err(_) => panic!("invalid static Litra capability range"),
    }
}

const GLOW_BRIGHTNESS_RANGE: LightValueRange = validated_range(
    MIN_BRIGHTNESS_LUMENS,
    MAX_BRIGHTNESS_LUMENS,
    1,
    LightValueUnit::Lumens,
);
const GLOW_TEMPERATURE_RANGE: LightValueRange = validated_range(
    MIN_TEMPERATURE_KELVIN,
    MAX_TEMPERATURE_KELVIN,
    TEMPERATURE_STEP_KELVIN,
    LightValueUnit::Kelvin,
);

/// A supported Litra product family variant.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LitraModel {
    /// Logitech Litra Glow.
    Glow,
    /// Logitech Litra Beam.
    Beam,
}

impl LitraModel {
    /// Resolve a Litra model from its USB product ID.
    #[must_use]
    pub const fn from_product_id(product_id: u16) -> Option<Self> {
        match product_id {
            LITRA_GLOW_PRODUCT_ID => Some(Self::Glow),
            LITRA_BEAM_PRODUCT_ID => Some(Self::Beam),
            _ => None,
        }
    }

    /// Resolve a model only when the complete raw-HID route matches a known
    /// Litra interface. Product ID alone is insufficient protection against
    /// writing a vendor report to an unrelated HID collection.
    #[must_use]
    pub fn from_route(route: &DeviceRoute) -> Option<Self> {
        let DeviceRoute::RawHid {
            vendor_id,
            product_id,
            usage_page,
            usage_id,
            ..
        } = route
        else {
            return None;
        };
        matches_litra(*vendor_id, *product_id, *usage_page, *usage_id)
            .then(|| Self::from_product_id(*product_id))
            .flatten()
    }

    /// Stable driver-family identifier for this model.
    #[must_use]
    pub const fn driver_id(self) -> &'static str {
        LITRA_DRIVER_ID
    }

    /// Exact model identifier used by the OpenLogi asset registry.
    #[must_use]
    pub const fn registry_model_id(self) -> Option<&'static str> {
        match self {
            Self::Glow => Some("8c900"),
            Self::Beam => Some("8c901"),
        }
    }

    /// Static capabilities exposed by the model.
    #[must_use]
    pub const fn capabilities(self) -> LightCapabilities {
        match self {
            Self::Glow | Self::Beam => LightCapabilities {
                power: true,
                brightness: Some(GLOW_BRIGHTNESS_RANGE),
                temperature: Some(GLOW_TEMPERATURE_RANGE),
                color: false,
                zones: false,
            },
        }
    }
}

/// Whether an HID descriptor identifies a supported Litra interface.
#[must_use]
pub fn matches_litra(vendor_id: u16, product_id: u16, usage_page: u16, usage_id: u16) -> bool {
    vendor_id == LOGITECH_VENDOR_ID
        && usage_page == LITRA_USAGE_PAGE
        && usage_id == LITRA_USAGE_ID
        && LitraModel::from_product_id(product_id).is_some()
}

/// A semantic command accepted by the standalone-light layer.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum LightCommand {
    /// Turn the light on or off.
    Power(bool),
    /// Set normalized brightness from 0 to 100 percent.
    BrightnessPercent(u8),
    /// Set colour temperature in Kelvin.
    TemperatureKelvin(u16),
    /// Set brightness in the native unit advertised by the selected model.
    /// This is primarily a diagnostic/CLI convenience; persisted settings
    /// remain normalized percentages.
    BrightnessNative(u16),
}

/// Encode a semantic command into the exact fixed-width Litra report.
pub fn encode_command(
    model: LitraModel,
    command: LightCommand,
) -> Result<[u8; REPORT_LEN], WriteError> {
    let mut report = [0; REPORT_LEN];
    report[0] = REPORT_ID;
    report[1..3].copy_from_slice(&REPORT_PREFIX);
    match command {
        LightCommand::Power(enabled) => {
            report[3] = COMMAND_POWER;
            report[4] = u8::from(enabled);
        }
        LightCommand::BrightnessPercent(percent) => {
            report[3] = COMMAND_BRIGHTNESS;
            let range = model
                .capabilities()
                .brightness
                .ok_or_else(|| unsupported("brightness"))?;
            let lumens = percent_to_native(percent, range)?;
            report[4..6].copy_from_slice(&lumens.to_be_bytes());
        }
        LightCommand::TemperatureKelvin(kelvin) => {
            report[3] = COMMAND_TEMPERATURE;
            let range = model
                .capabilities()
                .temperature
                .ok_or_else(|| unsupported("temperature"))?;
            if !range.contains(kelvin) {
                return Err(WriteError::InvalidLightValue {
                    control: "temperature_kelvin".into(),
                    value: kelvin,
                });
            }
            report[4..6].copy_from_slice(&kelvin.to_be_bytes());
        }
        LightCommand::BrightnessNative(value) => {
            report[3] = COMMAND_BRIGHTNESS;
            let range = model
                .capabilities()
                .brightness
                .ok_or_else(|| unsupported("brightness"))?;
            if !range.contains(value) {
                return Err(WriteError::InvalidLightValue {
                    control: "brightness_native".into(),
                    value,
                });
            }
            report[4..6].copy_from_slice(&value.to_be_bytes());
        }
    }
    Ok(report)
}

fn percent_to_native(percent: u8, range: LightValueRange) -> Result<u16, WriteError> {
    if percent > 100 {
        return Err(WriteError::InvalidLightValue {
            control: "brightness_percent".into(),
            value: u16::from(percent),
        });
    }
    range
        .native_for_percent(percent)
        .ok_or_else(|| WriteError::InvalidLightValue {
            control: "brightness_percent".into(),
            value: percent.into(),
        })
}

fn unsupported(control: &str) -> WriteError {
    WriteError::LightUnsupported {
        control: control.into(),
    }
}

static DEVICE_LOCKS: LazyLock<Mutex<HashMap<String, Arc<Mutex<()>>>>> =
    LazyLock::new(|| Mutex::new(HashMap::new()));

async fn device_lock(route: &DeviceRoute) -> OwnedMutexGuard<()> {
    let key = route.to_string();
    let lock = {
        let mut locks = DEVICE_LOCKS.lock().await;
        Arc::clone(locks.entry(key).or_insert_with(|| Arc::new(Mutex::new(()))))
    };
    lock.lock_owned().await
}

/// Apply a semantic Litra command through a raw HID route.
pub async fn apply(
    route: &DeviceRoute,
    model: LitraModel,
    command: LightCommand,
) -> Result<(), WriteError> {
    let Some(route_model) = LitraModel::from_route(route) else {
        return Err(unsupported("raw_hid_route"));
    };
    if route_model != model {
        return Err(unsupported("litra_model"));
    }
    let report = encode_command(model, command)?;
    let _guard = device_lock(route).await;
    let Some(mut writer) = crate::transport::open_route_writer(route).await? else {
        return Err(WriteError::DeviceNotFound);
    };
    tokio::time::timeout(RAW_WRITE_TIMEOUT, writer.write_output_report(&report))
        .await
        .map_err(|_| WriteError::RequestTimedOut {
            operation: super::HidppOperation::Light,
        })?
        .map_err(WriteError::from)?;
    debug!(route = %route, "applied raw Litra command");
    Ok(())
}

#[cfg(test)]
mod tests {
    #![allow(
        clippy::expect_used,
        reason = "expect is idiomatic in pure encoding tests"
    )]

    use std::assert_matches;

    use super::{
        COMMAND_BRIGHTNESS, COMMAND_POWER, COMMAND_TEMPERATURE, LITRA_BEAM_PRODUCT_ID,
        LightCommand, LitraModel, REPORT_ID, encode_command, matches_litra,
    };
    use crate::{DeviceRoute, WriteError};

    #[test]
    fn glow_power_reports_are_fixed_width() {
        let on = encode_command(LitraModel::Glow, LightCommand::Power(true)).expect("valid");
        let off = encode_command(LitraModel::Glow, LightCommand::Power(false)).expect("valid");
        assert_eq!(&on[..5], &[REPORT_ID, 0xff, 0x04, COMMAND_POWER, 1]);
        assert_eq!(&off[..5], &[REPORT_ID, 0xff, 0x04, COMMAND_POWER, 0]);
        assert_eq!(on.len(), 20);
        assert!(on[5..].iter().all(|byte| *byte == 0));
        assert!(off[5..].iter().all(|byte| *byte == 0));
    }

    #[test]
    fn glow_brightness_uses_big_endian_native_lumens() {
        let report =
            encode_command(LitraModel::Glow, LightCommand::BrightnessPercent(50)).expect("valid");
        assert_eq!(&report[3..6], &[COMMAND_BRIGHTNESS, 0, 0x87]);
    }

    #[test]
    fn glow_brightness_maps_normalized_boundaries_to_native_range() {
        let minimum =
            encode_command(LitraModel::Glow, LightCommand::BrightnessPercent(0)).expect("valid");
        let maximum =
            encode_command(LitraModel::Glow, LightCommand::BrightnessPercent(100)).expect("valid");
        assert_eq!(&minimum[3..6], &[COMMAND_BRIGHTNESS, 0, 20]);
        assert_eq!(&maximum[3..6], &[COMMAND_BRIGHTNESS, 0, 250]);
    }

    #[test]
    fn glow_native_brightness_preserves_the_exact_requested_lumens() {
        let report =
            encode_command(LitraModel::Glow, LightCommand::BrightnessNative(136)).expect("valid");
        assert_eq!(&report[3..6], &[COMMAND_BRIGHTNESS, 0, 136]);
        assert_matches!(
            encode_command(LitraModel::Glow, LightCommand::BrightnessNative(251)),
            Err(WriteError::InvalidLightValue { .. })
        );
    }

    #[test]
    fn glow_temperature_uses_big_endian_kelvin() {
        let report =
            encode_command(LitraModel::Glow, LightCommand::TemperatureKelvin(4600)).expect("valid");
        assert_eq!(&report[3..6], &[COMMAND_TEMPERATURE, 0x11, 0xf8]);
    }

    #[test]
    fn glow_temperature_accepts_only_aligned_inclusive_boundaries() {
        assert!(encode_command(LitraModel::Glow, LightCommand::TemperatureKelvin(2700)).is_ok());
        assert!(encode_command(LitraModel::Glow, LightCommand::TemperatureKelvin(6500)).is_ok());
        for invalid in [2600, 2750, 6600] {
            assert_matches!(
                encode_command(LitraModel::Glow, LightCommand::TemperatureKelvin(invalid)),
                Err(WriteError::InvalidLightValue { .. })
            );
        }
    }

    #[test]
    fn invalid_values_are_rejected() {
        assert_matches!(
            encode_command(LitraModel::Glow, LightCommand::BrightnessPercent(101)),
            Err(WriteError::InvalidLightValue { .. })
        );
        assert_matches!(
            encode_command(LitraModel::Glow, LightCommand::TemperatureKelvin(2750)),
            Err(WriteError::InvalidLightValue { .. })
        );
    }

    #[test]
    fn matcher_requires_the_full_identity_tuple() {
        assert!(matches_litra(0x046d, 0xc900, 0xff43, 0x0202));
        assert!(!matches_litra(0x046d, 0xc900, 0xff43, 0x0203));
        assert!(!matches_litra(0x046d, 0xc902, 0xff43, 0x0202));
        assert!(!matches_litra(0x1234, 0xc900, 0xff43, 0x0202));
    }

    #[test]
    fn glow_descriptor_exposes_driver_identity() {
        assert_eq!(LitraModel::Glow.driver_id(), "litra");
    }

    #[test]
    fn registry_model_ids_match_the_asset_registry() {
        assert_eq!(LitraModel::Glow.registry_model_id(), Some("8c900"));
        assert_eq!(LitraModel::Beam.registry_model_id(), Some("8c901"));
    }

    #[test]
    fn beam_is_matched_by_its_complete_route_tuple() {
        assert!(matches_litra(0x046d, 0xc901, 0xff43, 0x0202));
        assert_eq!(
            LitraModel::from_product_id(LITRA_BEAM_PRODUCT_ID),
            Some(LitraModel::Beam)
        );
    }

    #[test]
    fn model_resolution_requires_the_complete_raw_route_tuple() {
        let valid = DeviceRoute::RawHid {
            vendor_id: 0x046d,
            product_id: 0xc900,
            usage_page: 0xff43,
            usage_id: 0x0202,
            identity: "serial:test".into(),
        };
        let wrong_usage = DeviceRoute::RawHid {
            vendor_id: 0x046d,
            product_id: 0xc900,
            usage_page: 0xff43,
            usage_id: 0x0203,
            identity: "serial:test".into(),
        };

        assert_eq!(LitraModel::from_route(&valid), Some(LitraModel::Glow));
        assert_eq!(LitraModel::from_route(&wrong_usage), None);
        assert_eq!(
            LitraModel::from_route(&DeviceRoute::Direct {
                vendor_id: 0x046d,
                product_id: 0xc900,
            }),
            None
        );
    }
}