mx-remote 4.0.1

Client library for Pulse-Eight MatrixOS devices over UDP multicast/broadcast
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
// Author: Lars Op den Kamp (lars@opdenkamp-it.nl)
// Copyright (c) 2026 Op den Kamp IT Solutions

//! Byte-exact transmit vectors.
//!
//! The expected bytes come from outside this crate, which is the whole point: a
//! builder and a decoder that are wrong together look correct to every round
//! trip, so only a vector from elsewhere catches that. Two sources, and a
//! reader should know which one a vector rests on.
//!
//! Most were generated from the reference Python library, and predate this
//! port. The five marked "adjudicated" were derived from the receiving struct's
//! own declaration, read out of the receiver by whoever maintains it and pinned
//! with that target\'s compiler rather than by reading a header - which matters,
//! because on that target an enum can be one byte and an `ALIGN(8)` written
//! ahead of the `struct` keyword is ignored. Neither kind is a captured frame;
//! no capture exists in this tree.
//!
//! Every field in a fixture carries a distinct value wherever the layout allows
//! it. A fixture hides a shift whenever the neighbouring bytes hold the same
//! value, so equal neighbours here would pass for an offset that is off by one.

use std::net::Ipv4Addr;

use crate::types::{
    AmpZoneSettings, V2ipAudioFormat, VideoWallOp, VideoWallWindow, VolumeMuteStatus,
};

use super::bayconfig::{parse_bay_config, BAY_CONFIG_SIZE};
use super::enums::{
    BayFeatures, BayStatus, DeviceFeature, EdidProfile, MultiviewerViewMode, RcAction, RcKey,
};
use super::frame::build_frame;
use super::opcode::{audio_sub, op, protocol_for};
use super::payload::*;
use super::uid::DeviceUid;

/// A UID whose every byte differs, so a misread offset lands on a wrong value.
const TEST_UID: DeviceUid =
    DeviceUid::from_array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]);

/// A sink and a source whose bytes differ from `TEST_UID` and from each other,
/// so a frame that names the wrong one of the three is visible as wrong.
const SINK_UID: DeviceUid = DeviceUid::from_array([
    0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
]);
const SOURCE_UID: DeviceUid = DeviceUid::from_array([
    0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
]);

fn hex_of(b: &[u8]) -> String {
    use core::fmt::Write as _;
    b.iter()
        .fold(String::with_capacity(b.len() * 2), |mut out, x| {
            let _ = write!(out, "{x:02x}");
            out
        })
}

/// Sub-opcodes of `V2IP_MULTIVIEWER`, from the multiviewer module.
const MV_OP_VIEW_MODE: u8 = 1;
const MV_OP_AUDIO_VOLUME: u8 = 4;

#[test]
fn uid_round_trips_through_its_dotted_hex_form() {
    assert_eq!(TEST_UID.to_string(), "03020100.07060504.0b0a0908.0f0e0d0c");
    assert_eq!(
        TEST_UID.to_string().parse::<DeviceUid>().unwrap(),
        TEST_UID,
        "dotted-hex form did not survive a round trip"
    );
}

#[test]
fn hello_frame() {
    // The version string and the announced protocol are fixed data rather than
    // the crate's own VERSION and PROTOCOL_VERSION: this vector pins the
    // payload layout, which a number that moves cannot do. That this client
    // announces PROTOCOL_VERSION is asserted where a peer decodes its hello,
    // in runtime::tests.
    let payload = build_hello(
        0x28,
        "TestApp",
        "P9SN00000000",
        "2.1.3",
        DeviceFeature::MANAGER.bits(),
    );
    let got = build_frame(
        TEST_UID,
        op::SYS_HELLO,
        protocol_for(op::SYS_HELLO),
        &payload,
    );
    assert_eq!(
        hex_of(&got),
        "50380100000102030405060708090a0b0c0d0e0f000036002800546573744170700000000000000000005039534e303030303030303000000000322e312e33000000000000000000000000000800"
    );
}

#[test]
fn discover_frame() {
    let got = build_frame(
        TEST_UID,
        op::SYS_DISCOVER,
        protocol_for(op::SYS_DISCOVER),
        &[],
    );
    assert_eq!(
        hex_of(&got),
        "50380100000102030405060708090a0b0c0d0e0f01000000"
    );
}

#[test]
fn set_bay_name_frame() {
    // Six bytes longer than the reference Python library sends, which stops at
    // the last name byte. `mxr_bay_name_data` is ALIGN(8) and the addressed
    // device measures the payload against the whole struct, so the shorter form
    // is dropped on the length check. The bytes up to the padding are the
    // reference's.
    let payload = build_set_bay_name(TEST_UID, 5, "Living Room");
    let got = build_frame(
        TEST_UID,
        op::CHANGE_BAY_NAME,
        protocol_for(op::CHANGE_BAY_NAME),
        &payload,
    );
    assert_eq!(
        hex_of(&got),
        "50380600000102030405060708090a0b0c0d0e0f22002800000102030405060708090a0b0c0d0e0f05004c6976696e6720526f6f6d0000000000000000000000"
    );
}

#[test]
fn set_volume_frame() {
    let vol = VolumeMuteStatus {
        volume_left: Some(40),
        volume_right: Some(40),
        muted_left: Some(false),
        muted_right: Some(false),
    };
    assert_eq!(hex_of(&vol.wire()), "282800");

    let payload = build_set_volume(TEST_UID, 5, vol);
    let got = build_frame(
        TEST_UID,
        op::AUDIO_SET_VOLUME,
        protocol_for(op::AUDIO_SET_VOLUME),
        &payload,
    );
    assert_eq!(
        hex_of(&got),
        "50381100000102030405060708090a0b0c0d0e0f14001800000102030405060708090a0b0c0d0e0f0500282800000000"
    );
}

#[test]
fn manual_source_switch_frame() {
    let format = V2ipAudioFormat {
        sample_rate: 48000,
        channels: 2,
    };
    let payload = build_v2ip_manual_source_switch(
        TEST_UID,
        V2ipStreams {
            video: StreamAddr {
                ip: Ipv4Addr::new(239, 1, 2, 3),
                port: 50020,
            },
            audio: StreamAddr {
                ip: Ipv4Addr::new(239, 1, 2, 4),
                port: 50022,
            },
            anc: StreamAddr::default(),
        },
        Some(format),
    );
    let got = build_frame(
        TEST_UID,
        op::V2IP_MANUAL_SRC_SWITCH,
        protocol_for(op::V2IP_MANUAL_SRC_SWITCH),
        &payload,
    );
    assert_eq!(
        hex_of(&got),
        "50380700000102030405060708090a0b0c0d0e0f24003000000102030405060708090a0b0c0d0e0fef01020364c30000ef01020466c30000000000000000000080bb000002000000"
    );
}

#[test]
fn edid_profile_frame() {
    let payload = build_edid_profile(TEST_UID, EdidProfile::HDR_SURROUND71_4K);
    let got = build_frame(
        TEST_UID,
        op::BAY_EDID_PROFILE,
        protocol_for(op::BAY_EDID_PROFILE),
        &payload,
    );
    assert_eq!(
        hex_of(&got),
        "50380800000102030405060708090a0b0c0d0e0f34001800000102030405060708090a0b0c0d0e0f0800000000000000"
    );
}

#[test]
fn reboot_frame() {
    let payload = build_target_only(TEST_UID);
    let got = build_frame(
        TEST_UID,
        op::SYS_REBOOT,
        protocol_for(op::SYS_REBOOT),
        &payload,
    );
    assert_eq!(
        hex_of(&got),
        "50380100000102030405060708090a0b0c0d0e0f28001000000102030405060708090a0b0c0d0e0f"
    );
}

/// Adjudicated against `mxr_tx_key_data`: `PACKED`, 20 bytes, no tail padding.
///
/// The uid names the device that should act, not the sender, and the bay is a
/// port on that target - the inverse of `RC_KEY`, which reports a press on a
/// port of the device sending it. Both call the field the same thing and encode
/// it identically, so only the direction distinguishes them and nothing on the
/// wire catches a swap.
/// Adjudicated against `mxr_v2ip_audio_change_source`: a 20-byte command header
/// then a 36-byte body, 56 in all.
///
/// Three distinct uids, because two of the three regions carry the sink and a
/// vector that used one uid for sender and sink would pass for a builder that
/// confused them. The body holds the route end to end - sink at 20, source at
/// 36, their endpoint ids at 52 and 54 - while the header names the device
/// addressed for this hop, which a single-hop command makes the sink again.
///
/// The receiving struct's own field names are the reverse of what they carry,
/// and its receiver has no length check at all on this sub-command: a payload
/// short of 56 is read past its end rather than refused, so the size is as much
/// the assertion here as the offsets are.
#[test]
fn audio_select_input_frame() {
    let payload = build_audio_select_input(SINK_UID, 0x0203, SOURCE_UID, 0x0405);
    let got = build_frame(
        TEST_UID,
        op::V2IP_AUDIO,
        protocol_for(op::V2IP_AUDIO),
        &payload,
    );
    assert_eq!(
        hex_of(&got),
        "50381a00000102030405060708090a0b0c0d0e0f4300380003000000202122232425262728292a2b2c2d2e2f202122232425262728292a2b2c2d2e2f101112131415161718191a1b1c1d1e1f03020504"
    );
}

#[test]
fn rc_tx_key_frame() {
    let payload = build_rc_key(TEST_UID, 0x0203, RcKey::from_wire(0x0405));
    let got = build_frame(
        TEST_UID,
        op::RC_TX_KEY,
        protocol_for(op::RC_TX_KEY),
        &payload,
    );
    assert_eq!(
        hex_of(&got),
        "50380c00000102030405060708090a0b0c0d0e0f0c001400000102030405060708090a0b0c0d0e0f03020504"
    );
}

/// Adjudicated against `mxr_bay_hidden_data`: 19 bytes of fields in a 24-byte
/// `ALIGN(8)` struct, and the receiver requires all 24.
///
/// The five zeros are the tail. A payload built by summing field widths is 19
/// and is dropped without a word, so the padding is the assertion here.
#[test]
fn bay_hide_frame() {
    let payload = build_bay_hide(TEST_UID, 0x0203, true);
    let got = build_frame(TEST_UID, op::BAY_HIDE, protocol_for(op::BAY_HIDE), &payload);
    assert_eq!(
        hex_of(&got),
        "50380600000102030405060708090a0b0c0d0e0f27001800000102030405060708090a0b0c0d0e0f0302010000000000"
    );
}

/// Adjudicated: 17 bytes exactly, the uid then 0 for input and 1 for output.
///
/// The receiver takes 17, 257 or 514 and ignores everything else. The two
/// delivery forms put their mode byte first; this one puts it after the uid.
#[test]
fn edid_request_frame() {
    let payload = build_edid_request(TEST_UID, true);
    let got = build_frame(TEST_UID, op::DEV_EDID, protocol_for(op::DEV_EDID), &payload);
    assert_eq!(
        hex_of(&got),
        "50380100000102030405060708090a0b0c0d0e0f07001100000102030405060708090a0b0c0d0e0f01"
    );
}

/// Adjudicated against `mxr_v2ip_switch_data`: 24 bytes, no padding.
///
/// The two addresses are big-endian and sit immediately after little-endian
/// scalars, so they are given octets that differ from each other and read
/// differently reversed. A receiver also accepts 32 bytes, carrying a power-on
/// bit this client does not send.
#[test]
fn v2ip_source_switch_frame() {
    let payload = build_v2ip_source_switch(
        TEST_UID,
        Ipv4Addr::new(10, 8, 8, 9),
        Ipv4Addr::new(172, 20, 30, 40),
    );
    let got = build_frame(
        TEST_UID,
        op::V2IP_SOURCE_SWITCH,
        protocol_for(op::V2IP_SOURCE_SWITCH),
        &payload,
    );
    assert_eq!(
        hex_of(&got),
        "50380600000102030405060708090a0b0c0d0e0f1f001800000102030405060708090a0b0c0d0e0f0a080809ac141e28"
    );
}

/// Adjudicated against `vw_mesh_frame`: 29 bytes of fields in a 32-byte
/// 4-aligned struct, and the receiver requires all 32.
///
/// The raster travels with the window on purpose: only the sender knows what
/// raster the window was measured against, and a window without it cannot be
/// moved onto a later source. Dropping the two raster fields still produces a
/// well-formed 28-byte payload, which is why they are pinned here.
#[test]
fn video_wall_frame() {
    let window = VideoWallWindow {
        pos_x: 1920,
        pos_y: 1080,
        width: 960,
        height: 540,
        raster_w: 3840,
        raster_h: 2160,
    };
    let payload = build_video_wall(TEST_UID, window, VideoWallOp::STORE);
    let got = build_frame(
        TEST_UID,
        op::V2IP_VIDEO_WALL,
        protocol_for(op::V2IP_VIDEO_WALL),
        &payload,
    );
    assert_eq!(
        hex_of(&got),
        "50382800000102030405060708090a0b0c0d0e0f49002000000102030405060708090a0b0c0d0e0f80073804c0031c02000f700801000000"
    );
}

#[test]
fn rc_action_frame() {
    let payload = build_rc_action(TEST_UID, 5, RcAction::POWER_ON);
    let got = build_frame(
        TEST_UID,
        op::RC_TX_ACTION,
        protocol_for(op::RC_TX_ACTION),
        &payload,
    );
    assert_eq!(
        hex_of(&got),
        "50380c00000102030405060708090a0b0c0d0e0f0e001400000102030405060708090a0b0c0d0e0f05000100"
    );
}

#[test]
fn multiviewer_frames() {
    // The one vector here whose stamped version differs from the reference
    // Python library, which sends this opcode at 0x20. The receiving module
    // dispatches this frame on its payload length and never reads the stamp,
    // so 0x20 enables nothing and is refused by every receiver capped between
    // this opcode's own 0x16 and 0x1F. The payload bytes still come from the
    // reference; only the header version is ours.
    let view = build_frame(
        TEST_UID,
        op::V2IP_MULTIVIEWER,
        protocol_for(op::V2IP_MULTIVIEWER),
        &mv_cmd_payload(
            TEST_UID,
            MV_OP_VIEW_MODE,
            &[MultiviewerViewMode::PIP.to_wire()],
        ),
    );
    assert_eq!(
        hex_of(&view),
        "50381600000102030405060708090a0b0c0d0e0f42001900000102030405060708090a0b0c0d0e0f010000000000000002"
    );

    let volume = build_frame(
        TEST_UID,
        op::V2IP_MULTIVIEWER,
        protocol_for(op::V2IP_MULTIVIEWER),
        &mv_cmd_payload(TEST_UID, MV_OP_AUDIO_VOLUME, &[42, 1]),
    );
    assert_eq!(
        hex_of(&volume),
        "50381600000102030405060708090a0b0c0d0e0f42001a00000102030405060708090a0b0c0d0e0f04000000000000002a01"
    );
}

#[test]
fn audio_command_frames() {
    let mut mute = audio_cmd_header(audio_sub::MUTE, TEST_UID);
    mute.extend_from_slice(&audio_param(3, 1));
    let got = build_frame(
        TEST_UID,
        op::V2IP_AUDIO,
        protocol_for(op::V2IP_AUDIO),
        &mute,
    );
    assert_eq!(
        hex_of(&got),
        "50381a00000102030405060708090a0b0c0d0e0f43001c0001000000000102030405060708090a0b0c0d0e0f0300000001000000"
    );

    let mut volume = audio_cmd_header(audio_sub::VOLUME, TEST_UID);
    volume.extend_from_slice(&audio_param(5, 80));
    let got = build_frame(
        TEST_UID,
        op::V2IP_AUDIO,
        protocol_for(op::V2IP_AUDIO),
        &volume,
    );
    assert_eq!(
        hex_of(&got),
        "50381a00000102030405060708090a0b0c0d0e0f43001c0004000000000102030405060708090a0b0c0d0e0f0500000050000000"
    );
}

/// The expected bytes here are laid out from the C declaration field by field,
/// not produced by `build_amp_zone_settings` - a vector taken from the builder
/// only proves the builder agrees with itself, which is how the delays sat at
/// the wrong offset in two libraries at once.
#[test]
fn amp_zone_settings_frame() {
    let settings = AmpZoneSettings {
        gain_left: 10,
        gain_right: 11,
        volume_min: 0,
        volume_max: 100,
        delay_left: 5,
        delay_right: 6,
        bass: 3,
        treble: 4,
        bridged: 1,
        power_mode: 2,
        power_level: 7,
        power_timeout: 300,
        eq_left: [1, 2, 3, 4, 5],
        eq_right: [6, 7, 8, 9, 10],
    };
    let payload = build_amp_zone_settings(TEST_UID, 5, &settings);
    let got = build_frame(
        TEST_UID,
        op::AMP_ZONE_SETTINGS,
        protocol_for(op::AMP_ZONE_SETTINGS),
        &payload,
    );
    assert_eq!(
        hex_of(&got),
        "50381c00000102030405060708090a0b0c0d0e0f3d003800000102030405060708090a0b0c0d0e0f05000a0b00640000050000000600000003040102070000002c0100000102030405060708090a0000"
    );
}

#[test]
fn stats_request_frame() {
    let payload = build_stats_request(TEST_UID, true);
    let got = build_frame(
        TEST_UID,
        op::V2IP_STATS,
        protocol_for(op::V2IP_STATS),
        &payload,
    );
    assert_eq!(
        hex_of(&got),
        "50381300000102030405060708090a0b0c0d0e0f3f001100000102030405060708090a0b0c0d0e0f01"
    );
}

#[test]
fn bay_config_record_fields() {
    let mut p = vec![0u8; BAY_CONFIG_SIZE];
    p[0] = 7; // port
    p[1] = 1; // output
    p[2] = 2; // bay
    p[3] = 3; // video source
    p[4] = 4; // audio source
    p[5..13].copy_from_slice(b"Output 3");
    p[21..28].copy_from_slice(b"Kitchen");
    p[37..44].copy_from_slice(b"1080p60");
    p[53] = 1 << 3; // status: signal detected
    p[57] = 1 << 0; // features: HDMI out

    let cfg = parse_bay_config(&p).expect("a full-width record must parse");
    assert_eq!((cfg.port, cfg.modenum, cfg.bay), (7, 1, 2));
    assert_eq!(cfg.bay_name, "Output 3");
    assert_eq!(cfg.user_name, "Kitchen");
    assert_eq!(cfg.signal_type, "1080p60");
    assert!(cfg.status.has(BayStatus::SIGNAL_DETECTED));
    assert!(cfg.features.has(BayFeatures::HDMI_OUT));
}

#[test]
fn a_record_shorter_than_the_declared_width_is_dropped() {
    assert!(parse_bay_config(&[0u8; BAY_CONFIG_SIZE - 1]).is_none());
}