medius 3.4.6

Rust library for Medius, replacement firmware for the MAKCU box
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
#![cfg(feature = "mock")]

use std::time::{Duration, Instant};

use crate::{
    Button, Device, DeviceInfo, DeviceKind, Error, FrameType, Health, LogLevel, MockBox,
    RebootTarget, Version,
};

#[test]
fn device_info_and_version_mac_round_trip_through_the_mock() {
    let mock = MockBox::new()
        .with_version(Version {
            proto_ver: 2,
            fw_major: 2,
            fw_minor: 3,
            fw_patch: 0,
            mac: [0x5A, 0x4E, 0x00, 0x11, 0x1e, 0x28],
            name: "Left PC".to_string(),
        })
        .with_device_info(DeviceInfo {
            vid: 0x1532,
            pid: 0x0072,
            bcd_device: 0x0200,
            bcd_usb: 0x0200,
            has_serial: true,
            has_bos: false,
            kind: DeviceKind::Mouse,
            product: "Razer Mamba Elite".to_string(),
        });
    let device = Device::with_mock(mock);

    let v = device.query_version().unwrap();
    assert_eq!(v.mac_hex(), "5a4e00111e28");
    assert_eq!(v.name, "Left PC"); // the name rides RESP(VERSION) beside the MAC

    let info = device.device_info().unwrap();
    assert_eq!((info.vid, info.pid), (0x1532, 0x0072));
    assert_eq!(info.kind, DeviceKind::Mouse);
    assert_eq!(info.product, "Razer Mamba Elite");
    assert!(info.has_serial && !info.has_bos);
}

#[test]
fn pushed_logs_reach_the_logs_channel_in_order() {
    let mock = MockBox::new();
    let device = Device::with_mock(mock.clone());
    let rx = device.logs();

    mock.push_log(LogLevel::Warn, "overheating");
    mock.push_log(LogLevel::Info, "recovered");

    let a = rx.recv_timeout(Duration::from_secs(1)).unwrap();
    let b = rx.recv_timeout(Duration::from_secs(1)).unwrap();
    assert_eq!((a.level, a.text.as_str()), (LogLevel::Warn, "overheating"));
    assert_eq!((b.level, b.text.as_str()), (LogLevel::Info, "recovered"));
}

#[test]
fn set_health_updates_subsequent_queries() {
    let mock = MockBox::new();
    let device = Device::with_mock(mock.clone());
    assert!(!device.query_health().unwrap().mouse_attached);
    mock.set_health(Health::from_flags(0x02));
    assert!(device.query_health().unwrap().mouse_attached);
}

#[test]
fn handshake_rejects_wrong_proto_ver() {
    let mock = MockBox::new().with_version(Version {
        proto_ver: 11,
        fw_major: 0,
        fw_minor: 0,
        fw_patch: 0,
        mac: [0; 6],
        name: String::new(),
    });
    let err = Device::open_mock(mock).unwrap_err();
    assert!(matches!(err, Error::BadProtoVer { got: 11 }), "got {err:?}");
}

#[test]
fn handshake_refuses_a_v3_4_0_box() {
    // v3.4.0 firmware reports protocol 7; this build speaks 9.
    let mock = MockBox::new().with_version(Version {
        proto_ver: 7,
        fw_major: 3,
        fw_minor: 4,
        fw_patch: 0,
        mac: [0; 6],
        name: String::new(),
    });
    let err = Device::open_mock(mock).unwrap_err();
    assert!(matches!(err, Error::BadProtoVer { got: 7 }), "got {err:?}");
    assert_eq!(
        err.to_string(),
        "unsupported protocol version 7 (expected 9)"
    );
}

#[test]
fn handshake_on_silent_box_is_no_reply() {
    let err = Device::open_mock(MockBox::new().silent()).unwrap_err();
    assert!(matches!(err, Error::NoReply), "got {err:?}");
}

#[test]
fn dropping_the_last_clone_joins_threads_without_hanging() {
    let device = Device::with_mock(MockBox::new());
    device.move_rel(1, 1).unwrap();
    let start = Instant::now();
    drop(device);
    assert!(
        start.elapsed() < Duration::from_millis(500),
        "drop took too long: {:?}",
        start.elapsed()
    );
}

#[test]
fn a_clone_keeps_the_reader_alive_until_the_last_drop() {
    let mock = MockBox::new();
    let device = Device::with_mock(mock.clone());
    let clone = device.clone();
    drop(device);
    let rx = clone.logs();
    mock.push_log(LogLevel::Info, "still here");
    assert_eq!(
        rx.recv_timeout(Duration::from_secs(1)).unwrap().text,
        "still here"
    );
}

#[test]
fn device_is_send_and_sync() {
    fn assert_send_sync<T: Send + Sync>() {}
    assert_send_sync::<Device>();
}

#[test]
fn reapply_re_emits_only_held_overrides() {
    let mock = MockBox::new();
    let device = Device::with_mock(mock.clone());
    device.press(Button::LEFT).unwrap();
    device.force_release(Button::SIDE1).unwrap();
    device.press(Button::MIDDLE).unwrap();
    device.release(Button::MIDDLE).unwrap();
    mock.clear_recorded();

    device.reapply().unwrap();
    let buttons: Vec<Vec<u8>> = mock
        .recorded_frames()
        .iter()
        .filter(|f| f.ty == FrameType::Inject)
        .map(|f| f.payload.clone())
        .collect();
    // INJECT [class=btn][id u16][action]: Left press, Side1 force-release.
    assert_eq!(buttons, vec![vec![0, 0, 0, 1], vec![0, 3, 0, 2]]);
    drop(device);
}

#[test]
fn reapply_re_emits_held_locks_but_not_released_ones() {
    use crate::{Axis, Blanket, Direction, Key};
    let mock = MockBox::new();
    let device = Device::with_mock(mock.clone());
    device.lock(Axis::X, Direction::Positive).unwrap();
    device.lock(Key::A, Direction::Both).unwrap();
    device.lock_all(Blanket::Keys, Direction::Both).unwrap();
    device.unlock(Key::A, Direction::Both).unwrap();
    mock.clear_recorded();

    device.reapply().unwrap();
    let locks: Vec<Vec<u8>> = mock
        .recorded_frames()
        .iter()
        .filter(|f| f.ty == FrameType::Lock)
        .map(|f| f.payload.clone())
        .collect();
    // Only the two still-held locks, each re-asserted at the scale it was set to; key A is gone.
    assert_eq!(
        locks,
        vec![vec![1, 0xFF, 0xFF, 0, 0, 0], vec![3, 0, 0, 1, 0, 0]]
    );
    drop(device);
}

#[test]
fn reapply_re_emits_a_scale_at_its_own_value() {
    use crate::{Axis, Direction};
    let mock = MockBox::new();
    let device = Device::with_mock(mock.clone());
    device.scale(Axis::X, Direction::Against, 40).unwrap();
    device.scale(Axis::Y, Direction::With, 130).unwrap();
    mock.clear_recorded();

    device.reapply().unwrap();
    let locks: Vec<Vec<u8>> = mock
        .recorded_frames()
        .iter()
        .filter(|f| f.ty == FrameType::Lock)
        .map(|f| f.payload.clone())
        .collect();
    // A weighing replays as a weighing: as a blanket lock it would turn 40% into a block across a
    // reconnect.
    assert_eq!(
        locks,
        vec![vec![3, 0, 0, 4, 40, 0], vec![3, 1, 0, 3, 130, 0]]
    );
    drop(device);
}

#[test]
fn reapply_re_emits_a_reversal_at_its_own_sign() {
    // A replay clamping the scale to zero would send a block where the host holds a reversal; the
    // row is what a reconnect rebuilds from.
    use crate::{Axis, Direction};
    let mock = MockBox::new();
    let device = Device::with_mock(mock.clone());
    device.scale(Axis::X, Direction::Both, -100).unwrap();
    device.scale(Axis::Y, Direction::Against, -255).unwrap();
    mock.clear_recorded();

    device.reapply().unwrap();
    let locks: Vec<Vec<u8>> = mock
        .recorded_frames()
        .iter()
        .filter(|f| f.ty == FrameType::Lock)
        .map(|f| f.payload.clone())
        .collect();
    assert_eq!(
        locks,
        vec![vec![3, 0, 0, 0, 0x9C, 0xFF], vec![3, 1, 0, 4, 0x01, 0xFF]]
    );
    drop(device);
}

#[test]
fn unlocking_both_forgets_the_relative_scales_too() {
    use crate::{Axis, Direction};
    let mock = MockBox::new();
    let device = Device::with_mock(mock.clone());
    device.scale(Axis::X, Direction::Against, 40).unwrap();
    device.scale(Axis::X, Direction::Positive, 60).unwrap();
    device.unlock(Axis::X, Direction::Both).unwrap();
    mock.clear_recorded();

    device.reapply().unwrap();
    let locks: Vec<Vec<u8>> = mock
        .recorded_frames()
        .iter()
        .filter(|f| f.ty == FrameType::Lock)
        .map(|f| f.payload.clone())
        .collect();
    // Both clears the whole target on the box, so the shadow must too, or a replay restores the
    // cleared 40%.
    assert!(
        locks.is_empty(),
        "expected nothing re-asserted, got {locks:?}"
    );
    drop(device);
}

#[test]
fn releasing_one_sign_of_a_both_lock_is_not_undone_by_a_reapply() {
    use crate::{Axis, Direction};
    let mock = MockBox::new();
    let device = Device::with_mock(mock.clone());
    device.lock(Axis::X, Direction::Both).unwrap();
    device.unlock(Axis::X, Direction::Negative).unwrap();
    mock.clear_recorded();

    device.reapply().unwrap();
    let locks: Vec<Vec<u8>> = mock
        .recorded_frames()
        .iter()
        .filter(|f| f.ty == FrameType::Lock)
        .map(|f| f.payload.clone())
        .collect();
    // Both wrote two slots and the unlock cleared one, so only the positive sign is held; re-sending
    // Both would re-block a released direction.
    assert_eq!(locks, vec![vec![3, 0, 0, 1, 0, 0]]);
    drop(device);
}

#[test]
fn releasing_one_button_of_a_blanket_is_not_undone_by_a_reapply() {
    use crate::{Blanket, Button, Direction};
    let mock = MockBox::new();
    let device = Device::with_mock(mock.clone());
    device.lock_all(Blanket::Buttons, Direction::Both).unwrap();
    device.unlock(Button::LEFT, Direction::Both).unwrap();
    mock.clear_recorded();

    device.reapply().unwrap();
    let ids: Vec<u16> = mock
        .recorded_frames()
        .iter()
        .filter(|f| f.ty == FrameType::Lock)
        .map(|f| u16::from_le_bytes([f.payload[1], f.payload[2]]))
        .collect();
    // The box has no button-blanket state: it wrote the five rows, so a release of one leaves four.
    assert_eq!(ids, vec![1, 2, 3, 4]);
    drop(device);
}

#[test]
fn a_button_blanket_over_a_wide_mouse_reasserts_the_wide_buttons_on_reconnect() {
    use crate::types::MouseCaps;
    use crate::{Blanket, Direction};
    let mock = MockBox::new().with_mouse_caps(MouseCaps {
        n_buttons: 16,
        has_x: true,
        has_y: true,
        has_wheel: true,
        pan: false,
        has_report_id: false,
        n_hid: 1,
    });
    let device = Device::with_mock(mock.clone());
    // Reading CAPS caches the declared count; the blanket then expands onto every declared button.
    assert_eq!(device.caps().unwrap().mouse.n_buttons, 16);
    device.lock_all(Blanket::Buttons, Direction::Both).unwrap();
    mock.clear_recorded();

    device.reapply().unwrap();
    let ids: Vec<u16> = mock
        .recorded_frames()
        .iter()
        .filter(|f| f.ty == FrameType::Lock)
        .map(|f| u16::from_le_bytes([f.payload[1], f.payload[2]]))
        .collect();
    // Every declared button comes back, button 8 among them, not only the five named ones.
    assert_eq!(ids, (0..16).collect::<Vec<u16>>());
    drop(device);
}

#[test]
fn a_button_blanket_set_before_any_caps_call_reasserts_the_wide_buttons() {
    use crate::types::MouseCaps;
    use crate::{Blanket, Direction};
    let mock = MockBox::new().with_mouse_caps(MouseCaps {
        n_buttons: 8,
        has_x: true,
        has_y: true,
        has_wheel: true,
        pan: false,
        has_report_id: false,
        n_hid: 1,
    });
    // The handshake reads CAPS, so the count is cached before any lock. No caps() call: this is the
    // path the bug narrowed to five buttons.
    let device = Device::open_mock(mock.clone()).unwrap();
    device.lock_all(Blanket::Buttons, Direction::Both).unwrap();
    mock.clear_recorded();

    // A reconnect replays the held state; every declared button comes back, not only the named
    // five.
    device.reapply().unwrap();
    let ids: Vec<u16> = mock
        .recorded_frames()
        .iter()
        .filter(|f| f.ty == FrameType::Lock)
        .map(|f| u16::from_le_bytes([f.payload[1], f.payload[2]]))
        .collect();
    assert_eq!(ids, (0..8).collect::<Vec<u16>>());
    drop(device);
}

#[test]
fn a_scale_a_one_bit_class_cannot_hold_is_not_held_here_either() {
    use crate::{Button, Direction};
    let mock = MockBox::new();
    let device = Device::with_mock(mock.clone());
    device
        .scale(Button::LEFT, Direction::Positive, 150)
        .unwrap();
    mock.clear_recorded();

    device.reapply().unwrap();
    // 150% truncates to a pass (an unlock) on the box: nothing to keep open or re-assert.
    assert!(!mock.saw(FrameType::Lock));
    drop(device);
}

#[test]
fn reboot_emits_the_target_byte() {
    let mock = MockBox::new();
    let device = Device::with_mock(mock.clone());
    for target in [
        RebootTarget::DeviceRun,
        RebootTarget::HostRun,
        RebootTarget::DeviceDownload,
        RebootTarget::HostDownload,
    ] {
        device.reboot(target).unwrap();
    }
    let reboots: Vec<u8> = mock
        .recorded_frames()
        .iter()
        .filter(|f| f.ty == FrameType::RebootDl)
        .map(|f| f.payload[0])
        .collect();
    assert_eq!(reboots, vec![2, 3, 0, 1]);
    drop(device);
}