openlogi-hidpp 0.7.1

OpenLogi's hard fork of the `hidpp` crate (Logitech HID++ protocol).
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
//! Tests for the HID++ channel, and the mock transport they run on.
//!
//! `MockRawHidChannel` is also used by `device.rs`, so this module is
//! `pub(crate)` rather than private.

use super::*;
use std::{
    error::Error,
    io,
    sync::{
        Arc, Mutex,
        atomic::{AtomicBool, AtomicUsize, Ordering},
    },
    time::{Duration, Instant},
};

use async_trait::async_trait;

use crate::{
    nibble,
    protocol::v20::{self, ErrorType, Hidpp20Error},
};

#[test]
fn short_payload_widens_preserving_header_and_padding() {
    // [device, feature, function|sw, p0, p1, p2]
    let short = [0xff, 0x05, 0x1e, 0xaa, 0xbb, 0xcc];
    let HidppMessage::Long(long) = HidppMessage::Short(short).widened() else {
        panic!("widening a short message must produce a long one");
    };
    assert_eq!(&long[..short.len()], &short[..]); // header + payload copied verbatim
    assert!(long[short.len()..].iter().all(|&b| b == 0)); // remainder zero-padded
    assert_eq!(long.len(), LONG_REPORT_LENGTH - 1);
}

#[test]
fn widening_an_already_long_message_is_a_no_op() {
    let long = HidppMessage::Long([0x5a; LONG_REPORT_LENGTH - 1]);

    assert_eq!(long.widened(), long);
}

#[test]
fn send_returns_response_before_timeout() {
    futures::executor::block_on(async {
        let (raw, handle) = MockRawHidChannel::new();
        let channel = HidppChannel::from_raw_channel(raw).await.unwrap();

        let request = short_msg(0x10);
        let response = short_msg(0x20);
        handle.queue_response(response);

        let actual = channel
            .send_with_timeout(
                request,
                move |candidate| *candidate == response,
                Duration::from_secs(1),
            )
            .await
            .unwrap();

        assert_eq!(actual, response);
        assert_eq!(handle.written_reports().len(), 1);
        assert_pending_empty(&channel);
    });
}

#[test]
fn send_times_out_and_removes_pending_message() {
    futures::executor::block_on(async {
        let (raw, handle) = MockRawHidChannel::new();
        let channel = HidppChannel::from_raw_channel(raw).await.unwrap();
        let request = short_msg(0x10);
        let response = short_msg(0x20);

        let started = Instant::now();
        let err = channel
            .send_with_timeout(
                request,
                move |candidate| *candidate == response,
                Duration::from_millis(25),
            )
            .await
            .unwrap_err();

        assert!(matches!(err, ChannelError::Timeout));
        assert!(started.elapsed() < Duration::from_secs(1));
        assert_eq!(handle.written_reports().len(), 1);
        assert_pending_empty(&channel);
    });
}

#[test]
fn timeout_removes_only_its_own_pending_message() {
    futures::executor::block_on(async {
        let (raw, handle) = MockRawHidChannel::new();
        let channel = HidppChannel::from_raw_channel(raw).await.unwrap();

        let never_answered = short_msg(0x20);
        let slow_response = short_msg(0x21);

        let timed_out = channel.send_with_timeout(
            short_msg(0x10),
            move |candidate| *candidate == never_answered,
            Duration::from_millis(25),
        );
        let answered = channel.send_with_timeout(
            short_msg(0x11),
            move |candidate| *candidate == slow_response,
            Duration::from_secs(1),
        );
        // Answer the second request only after the first has timed out, so
        // a removal that took the wrong entry would fail this test.
        let respond_late = async {
            futures_timer::Delay::new(Duration::from_millis(100)).await;
            handle.send_incoming(slow_response).await;
        };

        let (timed_out, answered, ()) = futures::join!(timed_out, answered, respond_late);

        assert!(matches!(timed_out.unwrap_err(), ChannelError::Timeout));
        assert_eq!(answered.unwrap(), slow_response);
        assert_pending_empty(&channel);
    });
}

#[test]
fn late_response_after_timeout_is_ignored() {
    futures::executor::block_on(async {
        let (raw, handle) = MockRawHidChannel::new();
        let channel = HidppChannel::from_raw_channel(raw).await.unwrap();
        let events = Arc::new(Mutex::new(Vec::new()));
        let listener_events = Arc::clone(&events);
        channel.add_msg_listener(move |msg, matched| {
            listener_events.lock().unwrap().push((msg, matched));
        });

        let request = short_msg(0x10);
        let late_response = short_msg(0x20);
        let err = channel
            .send_with_timeout(
                request,
                move |candidate| *candidate == late_response,
                Duration::from_millis(25),
            )
            .await
            .unwrap_err();

        assert!(matches!(err, ChannelError::Timeout));
        assert_pending_empty(&channel);

        handle.send_incoming(late_response).await;
        wait_for_event_count(&events, 1).await;
        assert_eq!(events.lock().unwrap()[0], (late_response, false));
        assert_pending_empty(&channel);

        let followup_request = short_msg(0x30);
        let followup_response = short_msg(0x40);
        handle.queue_response(followup_response);
        let actual = channel
            .send_with_timeout(
                followup_request,
                move |candidate| *candidate == followup_response,
                Duration::from_secs(1),
            )
            .await
            .unwrap();

        assert_eq!(actual, followup_response);
        wait_for_event_count(&events, 2).await;
        assert_eq!(events.lock().unwrap()[1], (followup_response, true));
        assert_pending_empty(&channel);
    });
}

#[test]
fn send_and_forget_writes_without_pending_message() {
    futures::executor::block_on(async {
        let (raw, handle) = MockRawHidChannel::new();
        let channel = HidppChannel::from_raw_channel(raw).await.unwrap();

        channel.send_and_forget(short_msg(0x10)).await.unwrap();

        assert_eq!(handle.written_reports().len(), 1);
        assert_pending_empty(&channel);
    });
}

#[test]
fn raw_report_write_forwards_exact_bytes_and_length() {
    futures::executor::block_on(async {
        let (raw, handle) = MockRawHidChannel::new();
        let channel = HidppChannel::from_raw_channel(raw).await.unwrap();
        let report = [0x12; MAX_RAW_REPORT_LENGTH];

        let written = channel.write_raw_report(&report).await.unwrap();

        assert_eq!(written, report.len());
        assert_eq!(handle.written_reports(), [report.to_vec()]);
    });
}

#[test]
fn raw_report_write_rejects_empty_and_oversized_inputs_without_io() {
    futures::executor::block_on(async {
        let (raw, handle) = MockRawHidChannel::new();
        let channel = HidppChannel::from_raw_channel(raw).await.unwrap();

        let empty = channel.write_raw_report(&[]).await.unwrap_err();
        let oversized = channel
            .write_raw_report(&[0; MAX_RAW_REPORT_LENGTH + 1])
            .await
            .unwrap_err();

        assert!(matches!(empty, ChannelError::InvalidRawReportLength(0)));
        assert!(matches!(
            oversized,
            ChannelError::InvalidRawReportLength(65)
        ));
        assert!(handle.written_reports().is_empty());
    });
}

#[test]
fn raw_report_write_times_out_when_the_transport_parks() {
    futures::executor::block_on(async {
        let (raw, handle) = MockRawHidChannel::new();
        handle.park_writes();
        let channel = HidppChannel::from_raw_channel(raw).await.unwrap();
        let started = Instant::now();

        let error = channel
            .write_raw_report_with_timeout(&[LONG_REPORT_ID], Duration::from_millis(25))
            .await
            .unwrap_err();

        assert!(matches!(error, ChannelError::Timeout));
        assert!(started.elapsed() < Duration::from_secs(1));
    });
}

#[test]
fn listener_can_remove_another_listener_during_dispatch() {
    futures::executor::block_on(async {
        let (raw, handle) = MockRawHidChannel::new();
        let channel = Arc::new(HidppChannel::from_raw_channel(raw).await.unwrap());
        let removed_listener_calls = Arc::new(AtomicUsize::new(0));
        let removing_listener_calls = Arc::new(AtomicUsize::new(0));

        let removed_listener_calls_for_listener = Arc::clone(&removed_listener_calls);
        let removed_hdl = channel.add_msg_listener(move |_, _| {
            removed_listener_calls_for_listener.fetch_add(1, Ordering::SeqCst);
        });

        let channel_for_listener = Arc::clone(&channel);
        let removing_listener_calls_for_listener = Arc::clone(&removing_listener_calls);
        channel.add_msg_listener(move |_, _| {
            removing_listener_calls_for_listener.fetch_add(1, Ordering::SeqCst);
            channel_for_listener.remove_msg_listener(removed_hdl);
        });

        handle.send_incoming(short_msg(0x20)).await;
        wait_for_atomic_count(&removing_listener_calls, 1).await;
        wait_for_atomic_count(&removed_listener_calls, 1).await;

        handle.send_incoming(short_msg(0x21)).await;
        wait_for_atomic_count(&removing_listener_calls, 2).await;

        assert_eq!(removed_listener_calls.load(Ordering::SeqCst), 1);
    });
}

// --- HID++2.0 (v20) send/matcher characterization tests -----------------
//
// `HidppChannel::send`/`send_with_timeout` above are protocol-agnostic:
// they match on an arbitrary predicate over raw `HidppMessage`s. The
// v20-specific correlation logic (matching by header, splitting out error
// frames) lives in `protocol::v20::HidppChannel::send_v20`, which is built
// directly on top of `send`. These tests pin that logic's current
// behaviour using the same mock transport as the tests above.

#[test]
fn send_v20_matches_response_by_header_ignoring_unrelated_messages() {
    futures::executor::block_on(async {
        let (raw, handle) = MockRawHidChannel::new();
        let channel = HidppChannel::from_raw_channel(raw).await.unwrap();

        let header = v20::MessageHeader {
            device_index: 0x01,
            feature_index: 0x05,
            function_id: U4::from_lo(0x2),
            software_id: U4::from_lo(0x3),
        };
        let request = v20::Message::Short(header, [0x00, 0x00, 0x00]);
        let response = v20::Message::Short(header, [0xaa, 0xbb, 0xcc]);

        // Each decoy differs from the request in exactly one header field, so
        // none of them may be mistaken for its response.
        let wrong_device = v20::Message::Short(
            v20::MessageHeader {
                device_index: 0x02,
                ..header
            },
            [0, 0, 0],
        );
        let wrong_feature = v20::Message::Short(
            v20::MessageHeader {
                feature_index: 0x06,
                ..header
            },
            [0, 0, 0],
        );
        let wrong_sw_id = v20::Message::Short(
            v20::MessageHeader {
                software_id: U4::from_lo(0x4),
                ..header
            },
            [0, 0, 0],
        );

        let send_fut = channel.send_v20(request);
        let feed_fut = async {
            handle.send_incoming(wrong_device.into()).await;
            handle.send_incoming(wrong_feature.into()).await;
            handle.send_incoming(wrong_sw_id.into()).await;
            handle.send_incoming(response.into()).await;
        };

        let (result, ()) = futures::join!(send_fut, feed_fut);

        assert_eq!(result.unwrap(), response);
        assert_pending_empty(&channel);
    });
}

#[test]
fn send_v20_broadcast_event_does_not_resolve_pending_request() {
    futures::executor::block_on(async {
        let (raw, handle) = MockRawHidChannel::new();
        let channel = HidppChannel::from_raw_channel(raw).await.unwrap();
        let events = Arc::new(Mutex::new(Vec::new()));
        let listener_events = Arc::clone(&events);
        channel.add_msg_listener(move |msg, matched| {
            listener_events.lock().unwrap().push((msg, matched));
        });

        let header = v20::MessageHeader {
            device_index: 0x01,
            feature_index: 0x05,
            function_id: U4::from_lo(0x2),
            software_id: U4::from_lo(0x3),
        };
        let request = v20::Message::Short(header, [0, 0, 0]);
        let response = v20::Message::Short(header, [0xaa, 0xbb, 0xcc]);

        // Software ID 0 is reserved for unsolicited device notifications
        // (see `feature::event_payload`). The request above uses a non-zero
        // ID, so an incoming broadcast sharing device/feature but using ID 0
        // must be routed to listeners, not consumed as this request's
        // response.
        let event = v20::Message::Short(
            v20::MessageHeader {
                software_id: U4::from_lo(0x0),
                ..header
            },
            [0x01, 0x02, 0x03],
        );

        let send_fut = channel.send_v20(request);
        let feed_fut = async {
            handle.send_incoming(event.into()).await;
            wait_for_event_count(&events, 1).await;
            handle.send_incoming(response.into()).await;
        };

        let (result, ()) = futures::join!(send_fut, feed_fut);

        assert_eq!(result.unwrap(), response);
        // The oneshot resolves before the listener loop runs on the read
        // thread; wait for both deliveries before asserting on them.
        wait_for_event_count(&events, 2).await;
        let recorded = events.lock().unwrap().clone();
        assert_eq!(
            recorded,
            vec![
                (HidppMessage::from(event), false),
                (HidppMessage::from(response), true),
            ]
        );
        assert_pending_empty(&channel);
    });
}

#[test]
fn send_v20_response_may_arrive_as_a_different_report_width() {
    futures::executor::block_on(async {
        let (raw, handle) = MockRawHidChannel::new();
        let channel = HidppChannel::from_raw_channel(raw).await.unwrap();

        let header = v20::MessageHeader {
            device_index: 0x01,
            feature_index: 0x05,
            function_id: U4::from_lo(0x2),
            software_id: U4::from_lo(0x3),
        };
        let request = v20::Message::Short(header, [0, 0, 0]);
        // Quirk: `send_v20`'s response predicate compares only the parsed
        // v20 header, not the underlying report width. A device replying
        // with a long report to a short request — same header, wider
        // payload — is still accepted as the response.
        let response = v20::Message::Long(header, [0xaa; 16]);
        handle.queue_response(response.into());

        let result = channel.send_v20(request).await.unwrap();

        assert_eq!(result, response);
        assert_pending_empty(&channel);
    });
}

#[test]
fn send_v20_error_frame_resolves_to_feature_error() {
    futures::executor::block_on(async {
        let (raw, handle) = MockRawHidChannel::new();
        let channel = HidppChannel::from_raw_channel(raw).await.unwrap();

        let header = v20::MessageHeader {
            device_index: 0x01,
            feature_index: 0x05,
            function_id: U4::from_lo(0x2),
            software_id: U4::from_lo(0x3),
        };
        let request = v20::Message::Short(header, [0, 0, 0]);
        let error_response = v20_error_frame(header, ErrorType::InvalidArgument.into());
        handle.queue_response(error_response.into());

        let err = channel.send_v20(request).await.unwrap_err();

        assert!(matches!(
            err,
            Hidpp20Error::Feature(ErrorType::InvalidArgument)
        ));
        assert_pending_empty(&channel);
    });
}

#[test]
fn send_v20_error_frame_with_unmapped_code_is_unsupported_response() {
    futures::executor::block_on(async {
        let (raw, handle) = MockRawHidChannel::new();
        let channel = HidppChannel::from_raw_channel(raw).await.unwrap();

        let header = v20::MessageHeader {
            device_index: 0x01,
            feature_index: 0x05,
            function_id: U4::from_lo(0x2),
            software_id: U4::from_lo(0x3),
        };
        let request = v20::Message::Short(header, [0, 0, 0]);
        // 0xfe is not a defined `ErrorType` variant.
        let error_response = v20_error_frame(header, 0xfe);
        handle.queue_response(error_response.into());

        let err = channel.send_v20(request).await.unwrap_err();

        assert!(matches!(err, Hidpp20Error::UnsupportedResponse));
        assert_pending_empty(&channel);
    });
}

/// Builds the HID++2.0 error-frame encoding for `request_header`: feature
/// index 0xFF, with the original feature index and function|software byte
/// shifted one byte to the right (see `v20::HidppChannel::send_v20`'s
/// `is_error` predicate for the reverse mapping).
fn v20_error_frame(request_header: v20::MessageHeader, error_code: u8) -> v20::Message {
    let error_header = v20::MessageHeader {
        device_index: request_header.device_index,
        feature_index: 0xff,
        function_id: U4::from_hi(request_header.feature_index),
        software_id: U4::from_lo(request_header.feature_index),
    };
    let mut payload = [0u8; 3];
    payload[0] = nibble::combine(request_header.function_id, request_header.software_id);
    payload[1] = error_code;
    v20::Message::Short(error_header, payload)
}

#[derive(Clone)]
pub(crate) struct MockRawHidHandle {
    incoming_tx: async_channel::Sender<Vec<u8>>,
    written_reports: Arc<Mutex<Vec<Vec<u8>>>>,
    responses_on_write: Arc<Mutex<VecDeque<Vec<u8>>>>,
    park_writes: Arc<AtomicBool>,
}

impl MockRawHidHandle {
    pub(crate) fn queue_response(&self, msg: HidppMessage) {
        self.responses_on_write
            .lock()
            .unwrap()
            .push_back(raw_report(msg));
    }

    async fn send_incoming(&self, msg: HidppMessage) {
        self.incoming_tx.send(raw_report(msg)).await.unwrap();
    }

    pub(crate) fn written_reports(&self) -> Vec<Vec<u8>> {
        self.written_reports.lock().unwrap().clone()
    }

    fn park_writes(&self) {
        self.park_writes.store(true, Ordering::SeqCst);
    }
}

pub(crate) struct MockRawHidChannel {
    incoming_tx: async_channel::Sender<Vec<u8>>,
    incoming_rx: async_channel::Receiver<Vec<u8>>,
    written_reports: Arc<Mutex<Vec<Vec<u8>>>>,
    responses_on_write: Arc<Mutex<VecDeque<Vec<u8>>>>,
    park_writes: Arc<AtomicBool>,
}

impl MockRawHidChannel {
    pub(crate) fn new() -> (Self, MockRawHidHandle) {
        let (incoming_tx, incoming_rx) = async_channel::unbounded();
        let written_reports = Arc::new(Mutex::new(Vec::new()));
        let responses_on_write = Arc::new(Mutex::new(VecDeque::new()));
        let park_writes = Arc::new(AtomicBool::new(false));

        let handle = MockRawHidHandle {
            incoming_tx: incoming_tx.clone(),
            written_reports: Arc::clone(&written_reports),
            responses_on_write: Arc::clone(&responses_on_write),
            park_writes: Arc::clone(&park_writes),
        };

        (
            Self {
                incoming_tx,
                incoming_rx,
                written_reports,
                responses_on_write,
                park_writes,
            },
            handle,
        )
    }
}

#[async_trait]
impl RawHidChannel for MockRawHidChannel {
    fn vendor_id(&self) -> u16 {
        0x046d
    }

    fn product_id(&self) -> u16 {
        0xc539
    }

    async fn write_report(&self, src: &[u8]) -> Result<usize, Box<dyn Error + Sync + Send>> {
        self.written_reports.lock().unwrap().push(src.to_vec());
        if self.park_writes.load(Ordering::SeqCst) {
            return std::future::pending().await;
        }
        let response = self.responses_on_write.lock().unwrap().pop_front();
        if let Some(response) = response {
            self.incoming_tx.send(response).await.unwrap();
        }

        Ok(src.len())
    }

    async fn read_report(&self, buf: &mut [u8]) -> Result<usize, Box<dyn Error + Sync + Send>> {
        let report = self.incoming_rx.recv().await.map_err(|_| mock_error())?;
        let len = report.len().min(buf.len());
        buf[..len].copy_from_slice(&report[..len]);
        Ok(len)
    }

    fn supports_short_long_hidpp(&self) -> Option<(bool, bool)> {
        Some((true, true))
    }

    async fn get_report_descriptor(
        &self,
        _buf: &mut [u8],
    ) -> Result<usize, Box<dyn Error + Sync + Send>> {
        unreachable!("mock declares HID++ support")
    }
}

fn short_msg(marker: u8) -> HidppMessage {
    HidppMessage::Short([0xff, marker, 0x10, marker, marker, marker])
}

fn raw_report(msg: HidppMessage) -> Vec<u8> {
    let mut buf = [0u8; LONG_REPORT_LENGTH];
    let len = msg.write_raw(&mut buf);
    buf[..len].to_vec()
}

fn assert_pending_empty(channel: &HidppChannel) {
    assert!(channel.pending_messages.lock().unwrap().is_empty());
}

async fn wait_for_event_count(events: &Arc<Mutex<Vec<(HidppMessage, bool)>>>, count: usize) {
    let started = Instant::now();
    while started.elapsed() < Duration::from_secs(1) {
        if events.lock().unwrap().len() >= count {
            return;
        }
        futures_timer::Delay::new(Duration::from_millis(10)).await;
    }

    panic!("timed out waiting for {count} listener events");
}

async fn wait_for_atomic_count(count: &AtomicUsize, expected: usize) {
    let started = Instant::now();
    while started.elapsed() < Duration::from_secs(1) {
        if count.load(Ordering::SeqCst) >= expected {
            return;
        }
        futures_timer::Delay::new(Duration::from_millis(10)).await;
    }

    panic!("timed out waiting for atomic count {expected}");
}

fn mock_error() -> Box<dyn Error + Sync + Send> {
    Box::new(io::Error::new(
        io::ErrorKind::BrokenPipe,
        "mock channel closed",
    ))
}