Bound disconnected HID edge admission

This commit is contained in:
Meta Vens 2026-07-16 21:24:41 +01:00
commit 16a4c280ba
2 changed files with 49 additions and 1 deletions

View file

@ -5461,6 +5461,8 @@ struct ActiveHidControllerRudpSource {
last_read_error_log_at: Option<Instant>,
}
const MUNINN_HID_MAX_PENDING_EDGES: usize = 256;
fn active_hid_controller_rudp_source(source: MoveStateSource) -> ActiveHidControllerRudpSource {
let epoch = timestamp_ns().unwrap_or_default().max(0) as u64;
ActiveHidControllerRudpSource {
@ -5517,6 +5519,13 @@ fn capture_button_edges(active: &mut ActiveHidControllerRudpSource, buttons: &[S
active.next_edge_sequence = active.next_edge_sequence.saturating_add(1);
}
active.edge_buttons = buttons.to_vec();
if active.pending_edges.len() > MUNINN_HID_MAX_PENDING_EDGES {
active.epoch = active.epoch.saturating_add(1).max(1);
active.sequence = 0;
active.next_edge_sequence = 1;
active.pending_edges.clear();
active.last_edge_send_at = None;
}
}
fn sync_hid_controller_rudp_sources(
@ -13891,6 +13900,27 @@ Device 00:07:04:A8:00:D0 (public)
assert_eq!(edges[0].epoch, edges[1].epoch);
}
#[test]
fn hid_edge_backlog_rotates_epoch_and_rebases_on_latest_state() {
let mut active = active_hid_controller_rudp_source(MoveStateSource {
move_id: "pad-1".to_string(),
hidraw_path: "test".to_string(),
});
let initial_epoch = active.epoch;
for index in 0..=MUNINN_HID_MAX_PENDING_EDGES {
let buttons = if index % 2 == 0 {
vec!["a".to_string()]
} else {
Vec::new()
};
capture_button_edges(&mut active, &buttons);
}
assert!(active.epoch > initial_epoch);
assert!(active.pending_edges.len() <= MUNINN_HID_MAX_PENDING_EDGES);
assert_eq!(active.next_edge_sequence, 1);
assert_eq!(active.edge_buttons, vec!["a".to_string()]);
}
#[test]
fn active_move_state_sync_replaces_stale_hotplug_identity() {
let mut active = active_move_state_sources(vec![MoveStateSource {

View file

@ -205,6 +205,8 @@ struct HidSemanticCursor {
retired_epochs: HashSet<u64>,
}
const SLEIPNIR_HID_MAX_PENDING_EDGE_GAP: u64 = 256;
impl HidSemanticCursor {
fn accept_state(&mut self, epoch: u64, sequence: u64) -> bool {
if epoch == 0 && self.epoch != 0 { return false; }
@ -217,7 +219,12 @@ impl HidSemanticCursor {
true
}
fn push_edge(&mut self, edge: HidButtonEdge) -> Vec<HidButtonEdge> {
if edge.epoch != self.epoch || edge.edge_sequence <= self.edge_sequence { return Vec::new(); }
if edge.epoch != self.epoch
|| edge.edge_sequence <= self.edge_sequence
|| edge.edge_sequence > self.edge_sequence.saturating_add(SLEIPNIR_HID_MAX_PENDING_EDGE_GAP)
{
return Vec::new();
}
self.pending_edges.entry(edge.edge_sequence).or_insert(edge);
let mut ready = Vec::new();
while let Some(edge) = self.pending_edges.remove(&(self.edge_sequence + 1)) {
@ -3107,7 +3114,18 @@ mod tests {
}
#[test]
fn sparse_future_edges_cannot_grow_receiver_memory_without_bound() {
let mut cursor = HidSemanticCursor::default();
assert!(cursor.accept_state(12, 1));
assert!(
cursor
.push_edge(edge(12, SLEIPNIR_HID_MAX_PENDING_EDGE_GAP + 1, "a", true))
.is_empty()
);
assert!(cursor.pending_edges.is_empty());
}
fn coalesces_by_source_timestamp_not_arrival_order() {
#[test]
let mut newer_nav = record(vec![0.0, 1.0, -1.0], Vec::new(), 10);
newer_nav.device_id = "nav".to_string();
newer_nav.source_timestamp_ns = 2_000;