medius 3.4.3

Host control library for the medius transparent mouse passthrough 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
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
//! `RESP(FIRMWARE)` decoding (§4.16) and staged-image chunk sequencing. Byte vectors come from the
//! firmware's `ota_proto.h` and `control-protocol.md` §4.16, not from this decoder's expectations.

use crate::device::update::{ChunkPlan, begin_body};
use crate::protocol::opcode::{OTA_CHUNK, OTA_OP_DATA, Q_FIRMWARE};
use crate::protocol::{Resp, parse_resp};
use crate::types::{FirmwareInfo, ImageState, UpdateStatus, UpdateTarget};

fn payload(host_present: u8, staged: u8) -> [u8; 17] {
    [
        Q_FIRMWARE, // what
        3,
        2,
        0, // device version
        0, // device slot ota_0
        2, // device state valid
        host_present,
        3,
        2,
        0, // host version
        1, // host slot ota_1
        1, // host state pending-verify
        0x00,
        0x00,
        0x0F,
        0x00, // slot size 0x000F0000 little-endian
        staged,
    ]
}

#[test]
fn decode_firmware_reports_both_chips() {
    let Some(Resp::Firmware(f)) = parse_resp(&payload(1, 0x03)) else {
        panic!("expected Firmware");
    };
    assert_eq!((f.device.major, f.device.minor, f.device.patch), (3, 2, 0));
    assert_eq!(f.device.slot, 0);
    assert_eq!(f.device.state, ImageState::Valid);
    let h = f.host.expect("host present");
    assert_eq!(h.slot, 1);
    assert_eq!(h.state, ImageState::PendingVerify);
    assert_eq!(f.slot_size, 0x000F_0000);
    assert!(f.device_staged && f.host_staged);
    assert!(f.any_pending(), "a pending host chip is still pending");
}

#[test]
fn decode_firmware_absent_host_is_none() {
    let Some(Resp::Firmware(f)) = parse_resp(&payload(0, 0)) else {
        panic!("expected Firmware");
    };
    assert!(f.host.is_none());
    assert!(!f.device_staged && !f.host_staged);
    assert!(!f.any_pending());
}

#[test]
fn decode_firmware_staged_bits_are_independent() {
    let dev_only = parse_resp(&payload(1, 0x01));
    let Some(Resp::Firmware(f)) = dev_only else {
        panic!("expected Firmware");
    };
    assert!(f.device_staged && !f.host_staged);
    let Some(Resp::Firmware(g)) = parse_resp(&payload(1, 0x02)) else {
        panic!("expected Firmware");
    };
    assert!(!g.device_staged && g.host_staged);
}

#[test]
fn decode_firmware_rejects_short_payload() {
    assert!(FirmwareInfo::from_payload(&payload(1, 0)[..16]).is_none());
    assert!(FirmwareInfo::from_payload(&[]).is_none());
    // A different selector in byte 0 is a different response, not a short one.
    let mut wrong = payload(1, 0);
    wrong[0] = 10;
    assert!(FirmwareInfo::from_payload(&wrong).is_none());
}

#[test]
fn image_states_round_trip_their_wire_values() {
    for (v, want) in [
        (0u8, ImageState::New),
        (1, ImageState::PendingVerify),
        (2, ImageState::Valid),
        (3, ImageState::Invalid),
        (4, ImageState::Aborted),
        (0xFF, ImageState::Unknown(0xFF)),
    ] {
        assert_eq!(ImageState::from_u8(v), want);
    }
    assert!(ImageState::PendingVerify.is_pending());
    assert!(!ImageState::Valid.is_pending());
}

#[test]
fn begin_body_is_the_length_then_the_digest() {
    let body = begin_body(&[1u8, 2, 3, 4]);
    assert_eq!(body.len(), 36);
    assert_eq!(&body[..4], &4u32.to_le_bytes());
    // Not the digest of anything else: a truncated or padded image must not produce the same 32 bytes.
    assert_ne!(&body[4..], &begin_body(&[1u8, 2, 3])[4..]);
}

#[test]
fn chunk_plan_fills_the_window_then_waits() {
    let image = vec![0u8; OTA_CHUNK * 20];
    let mut plan = ChunkPlan::new(&image, UpdateTarget::Device, 16);
    let mut frames = 0;
    while let Some(f) = plan.next_frame() {
        assert_eq!(f[0], OTA_OP_DATA);
        assert_eq!(f[1], 0);
        assert_eq!(u16::from_le_bytes([f[2], f[3]]), frames);
        frames += 1;
    }
    assert_eq!(frames, 16, "the window bounds what may be in flight");
    assert!(plan.awaiting_ack());
    assert!(!plan.done());
}

#[test]
fn chunk_plan_carries_on_after_an_ack() {
    let image = vec![0u8; OTA_CHUNK * 20];
    let mut plan = ChunkPlan::new(&image, UpdateTarget::Host, 16);
    while plan.next_frame().is_some() {}
    let p = plan.on_ack(UpdateStatus::ACK, 16).expect("window landed");
    assert_eq!(p.sent, OTA_CHUNK * 16);
    assert_eq!(p.total, image.len());
    assert_eq!(p.target, UpdateTarget::Host);
    let mut more = 0;
    while plan.next_frame().is_some() {
        more += 1;
    }
    assert_eq!(more, 4, "the tail is shorter than a full window");
    plan.on_ack(UpdateStatus::ACK, 20).expect("tail landed");
    assert!(plan.done());
}

#[test]
fn chunk_plan_refuses_an_offset_the_box_does_not_share() {
    let image = vec![0u8; OTA_CHUNK * 4];
    let mut plan = ChunkPlan::new(&image, UpdateTarget::Device, 16);
    while plan.next_frame().is_some() {}
    // The box says it wants chunk 2 while four have been sent: writing on would misplace every byte.
    let err = plan.on_ack(UpdateStatus::ACK, 2).unwrap_err();
    assert!(matches!(
        err,
        crate::Error::Update {
            status: UpdateStatus::SEQ_GAP,
            arg: 2,
            ..
        }
    ));
}

#[test]
fn chunk_plan_surfaces_a_refusal_verbatim() {
    let image = vec![0u8; OTA_CHUNK];
    let mut plan = ChunkPlan::new(&image, UpdateTarget::Device, 16);
    plan.next_frame();
    let err = plan.on_ack(UpdateStatus::WRITE_FAILED, 7).unwrap_err();
    assert!(matches!(
        err,
        crate::Error::Update {
            status: UpdateStatus::WRITE_FAILED,
            arg: 7,
            ..
        }
    ));
}

#[test]
fn a_short_final_chunk_is_sent_whole() {
    let image = vec![0u8; OTA_CHUNK + 9];
    let mut plan = ChunkPlan::new(&image, UpdateTarget::Device, 16);
    let first = plan.next_frame().expect("first");
    let second = plan.next_frame().expect("second");
    assert_eq!(first.len(), 4 + OTA_CHUNK);
    assert_eq!(second.len(), 4 + 9);
    assert!(plan.next_frame().is_none());
    assert!(
        plan.awaiting_ack(),
        "the last chunk always asks for an answer"
    );
}

#[test]
fn a_zero_credit_falls_back_to_the_default() {
    let image = vec![0u8; OTA_CHUNK * 20];
    let mut plan = ChunkPlan::new(&image, UpdateTarget::Device, 0);
    let mut frames = 0;
    while plan.next_frame().is_some() {
        frames += 1;
    }
    assert_eq!(frames, 16);
}

#[test]
fn update_status_names_every_value_the_box_can_answer() {
    // Derived from name(): a hand-written list missed the very status it guarded, since adding one
    // meant remembering a test in another file.
    let named: Vec<u8> = (0u8..=0xFF)
        .filter(|&v| UpdateStatus(v).name() != "unknown")
        .collect();
    assert_eq!(
        named,
        vec![
            0x00, 0x01, 0x02, 0x03, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,
            0x1A, 0x1B, 0x1C,
        ],
        "a status gained or lost a name"
    );
}

#[test]
fn update_targets_round_trip() {
    assert_eq!(UpdateTarget::from_u8(0), Some(UpdateTarget::Device));
    assert_eq!(UpdateTarget::from_u8(1), Some(UpdateTarget::Host));
    assert_eq!(UpdateTarget::from_u8(2), None);
    assert_eq!(UpdateTarget::Device.as_u8(), 0);
    assert_eq!(UpdateTarget::Host.as_u8(), 1);
}

#[cfg(feature = "mock")]
mod against_the_mock {
    use super::*;
    use crate::protocol::FrameType;

    fn image(n: usize) -> Vec<u8> {
        (0..n).map(|i| (i * 31 + 7) as u8).collect()
    }

    #[test]
    fn a_whole_image_stages_and_activates() {
        let mock = crate::MockBox::new();
        let dev = crate::Device::with_mock(mock.clone());
        let img = image(OTA_CHUNK * 18 + 13);
        let mut seen = Vec::new();
        let wrote = dev
            .stage_firmware(UpdateTarget::Device, &img, &mut |p| seen.push(p.sent))
            .expect("staged");
        assert_eq!(wrote, img.len() as u32);
        assert!(
            seen.last() == Some(&img.len()),
            "progress must end on the whole image, got {seen:?}"
        );
        assert!(dev.firmware_info().expect("info").device_staged);
        dev.activate_firmware().expect("activated");
        assert!(!dev.firmware_info().expect("info").device_staged);
    }

    #[test]
    fn every_image_byte_reaches_the_box_in_order() {
        let mock = crate::MockBox::new();
        let dev = crate::Device::with_mock(mock.clone());
        let img = image(OTA_CHUNK * 3 + 5);
        dev.stage_firmware(UpdateTarget::Device, &img, &mut |_| {})
            .expect("staged");
        let mut rebuilt = Vec::new();
        for f in mock.recorded_frames() {
            if f.ty == FrameType::Update && f.payload.first() == Some(&OTA_OP_DATA) {
                rebuilt.extend_from_slice(&f.payload[4..]);
            }
        }
        assert_eq!(rebuilt, img, "the bytes on the wire are the image");
    }

    #[test]
    fn activate_with_nothing_staged_is_refused() {
        let dev = crate::Device::with_mock(crate::MockBox::new());
        let err = dev.activate_firmware().unwrap_err();
        assert!(matches!(
            err,
            crate::Error::Update {
                status: UpdateStatus::NOTHING_STAGED,
                ..
            }
        ));
    }

    #[test]
    fn an_image_over_the_slot_is_refused_before_a_byte_is_sent() {
        let dev = crate::Device::with_mock(crate::MockBox::new());
        let huge = vec![0u8; 0xF_0000 + 1];
        let err = dev
            .stage_firmware(UpdateTarget::Device, &huge, &mut |_| {})
            .unwrap_err();
        assert!(matches!(
            err,
            crate::Error::Update {
                status: UpdateStatus::TOO_BIG,
                arg: 0xF_0000,
                ..
            }
        ));
    }

    #[test]
    fn an_empty_image_is_refused() {
        let dev = crate::Device::with_mock(crate::MockBox::new());
        assert!(
            dev.stage_firmware(UpdateTarget::Device, &[], &mut |_| {})
                .is_err()
        );
    }

    #[test]
    fn aborting_leaves_nothing_staged() {
        let dev = crate::Device::with_mock(crate::MockBox::new());
        let img = image(OTA_CHUNK * 2);
        dev.stage_firmware(UpdateTarget::Device, &img, &mut |_| {})
            .expect("staged");
        assert!(dev.firmware_info().expect("info").device_staged);
        dev.abort_update(UpdateTarget::Device).expect("aborted");
        assert!(!dev.firmware_info().expect("info").device_staged);
    }

    #[test]
    fn the_host_target_rides_the_same_path() {
        let mock = crate::MockBox::new();
        let dev = crate::Device::with_mock(mock.clone());
        let img = image(OTA_CHUNK + 1);
        dev.stage_firmware(UpdateTarget::Host, &img, &mut |_| {})
            .expect("staged");
        let targets: Vec<u8> = mock
            .recorded_frames()
            .iter()
            .filter(|f| f.ty == FrameType::Update)
            .map(|f| f.payload[1])
            .collect();
        assert!(
            targets.iter().all(|&t| t == 1),
            "every frame must name the host chip, got {targets:?}"
        );
    }
}

#[cfg(feature = "mock")]
mod correlation {
    use super::*;
    use crate::protocol::FrameType;

    // A DATA acknowledgement answers a window, so it carries its own rolling SEQ.
    #[test]
    fn data_acks_carry_a_rolling_seq_not_the_command_seq() {
        let mock = crate::MockBox::new();
        let dev = crate::Device::with_mock(mock.clone());
        let img: Vec<u8> = (0..(OTA_CHUNK * 40)).map(|i| i as u8).collect();
        dev.stage_firmware(UpdateTarget::Device, &img, &mut |_| {})
            .expect("staged");

        let sent: Vec<u8> = mock
            .recorded_frames()
            .iter()
            .filter(|f| f.ty == FrameType::Update && f.payload.first() == Some(&OTA_OP_DATA))
            .map(|f| f.seq)
            .collect();
        let acks: Vec<u8> = mock
            .replied_frames()
            .iter()
            .filter(|f| f.ty == FrameType::UpdateResp && f.payload.first() == Some(&OTA_OP_DATA))
            .map(|f| f.seq)
            .collect();

        assert!(
            acks.len() >= 3,
            "need several windows, got {} acks",
            acks.len()
        );
        // Starts at 0 and steps by one per acknowledgement, unlike the command SEQs (one per chunk,
        // sixteen per window).
        let expected: Vec<u8> = (0..acks.len() as u8).collect();
        assert_eq!(acks, expected, "acks should roll 0,1,2..., got {acks:?}");
        assert_ne!(
            acks[1], sent[31],
            "the second ack must not echo the SEQ of the chunk that closed its window"
        );
    }

    // An oversized chunk is a malformed frame, not an oversized image. No client sends one, so the
    // mock's state machine is asserted directly.
    #[test]
    fn an_oversized_chunk_is_bad_state_not_too_big() {
        let mut u = crate::mock::MockUpdate::default();
        let mut begin = Vec::new();
        begin.extend_from_slice(&((OTA_CHUNK * 4) as u32).to_le_bytes());
        begin.extend_from_slice(&[0u8; 32]);
        assert_eq!(u.begin_for_test(&begin), (0x01, 16));

        let mut over = vec![0u8, 0]; // seq 0
        over.extend_from_slice(&vec![0u8; OTA_CHUNK + 1]); // one byte too many
        assert_eq!(
            u.data_for_test(&over),
            Some((0x1A, OTA_CHUNK as u32)),
            "an over-long chunk is malformed, and the answer names the chunk size"
        );

        // An image overrun is the other answer, and must stay distinct from it.
        let mut u2 = crate::mock::MockUpdate::default();
        let mut small = Vec::new();
        small.extend_from_slice(&10u32.to_le_bytes());
        small.extend_from_slice(&[0u8; 32]);
        u2.begin_for_test(&small);
        let mut long = vec![0u8, 0];
        long.extend_from_slice(&[0u8; 20]);
        assert_eq!(u2.data_for_test(&long), Some((0x12, 10)));
    }
}

#[cfg(feature = "mock")]
mod stale_replies {
    use super::*;

    // An abandoned transfer leaves its last acknowledgement in the channel.
    #[test]
    fn a_leftover_data_ack_does_not_answer_the_next_transfers_first_window() {
        let mock = crate::MockBox::new();
        let dev = crate::Device::with_mock(mock.clone());
        // op, target, status ACK, then an offset from an image that is no longer being sent.
        let mut stale = vec![OTA_OP_DATA, UpdateTarget::Device.as_u8(), 0x02];
        stale.extend_from_slice(&9999u32.to_le_bytes());
        dev.link.inject_update(stale);

        let img: Vec<u8> = (0..(OTA_CHUNK * 40)).map(|i| i as u8).collect();
        let mut seen = Vec::new();
        dev.stage_firmware(UpdateTarget::Device, &img, &mut |p| seen.push(p.sent))
            .expect("the stale acknowledgement must not be mistaken for this transfer's");
        assert_eq!(seen.last().copied(), Some(img.len()));
        // Monotonic, never ahead of the image: a window-ahead loop reports an unwritten offset.
        assert!(
            seen.windows(2).all(|w| w[0] < w[1]),
            "not monotonic: {seen:?}"
        );
        assert!(seen.iter().all(|&s| s <= img.len()));
    }
}