cat-dev 0.0.13

A library for interacting with the CAT-DEV hardware units distributed by Nintendo (i.e. a type of Wii-U DevKits).
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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
//! Protocols for speaking with the "parameter" port of the MION.
//!
//! The parameter port as of today is only known for getting/setting 512 bytes
//! of parameters. These values are usually things reachable from other parts
//! of the MION interface, but are available through the MION too.

mod errors;
pub mod well_known;

pub use errors::*;

use crate::{
	errors::NetworkParseError,
	mion::proto::parameter::well_known::{ValuableParameterDump, index_from_parameter_name},
};
use bytes::{BufMut, Bytes, BytesMut};
use std::fmt::{Display, Formatter, Result as FmtResult};
use valuable::{Fields, NamedField, NamedValues, StructDef, Structable, Valuable, Value};

/// The type of MION Parameters Packet this is.
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Valuable)]
pub enum PacketType {
	/// This packet is a read request/response.
	Read,
	/// This packet is a write request/response.
	Write,
}

impl From<&PacketType> for i32 {
	fn from(value: &PacketType) -> Self {
		match *value {
			PacketType::Read => 0,
			PacketType::Write => 1,
		}
	}
}
impl From<PacketType> for i32 {
	fn from(value: PacketType) -> Self {
		Self::from(&value)
	}
}

impl TryFrom<i32> for PacketType {
	type Error = MionParamProtocolError;

	fn try_from(value: i32) -> Result<Self, Self::Error> {
		match value {
			0 => Ok(Self::Read),
			1 => Ok(Self::Write),
			_ => Err(MionParamProtocolError::PacketType(value)),
		}
	}
}

/// A request to dump all the 512 parameters available on the MION board.
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Valuable)]
pub struct MionDumpParameters;

impl MionDumpParameters {
	/// Create a new request packet to send to a mion parameter port to tell it
	/// to give us all the parameters.
	#[must_use]
	pub const fn new() -> Self {
		Self {}
	}
}

impl Default for MionDumpParameters {
	fn default() -> Self {
		Self::new()
	}
}

impl Display for MionDumpParameters {
	fn fmt(&self, fmt: &mut Formatter<'_>) -> FmtResult {
		write!(fmt, "MionDumpParameters")
	}
}

impl TryFrom<Bytes> for MionDumpParameters {
	type Error = NetworkParseError;

	fn try_from(packet: Bytes) -> Result<Self, Self::Error> {
		if packet.len() < 8 {
			return Err(NetworkParseError::NotEnoughData(
				"MionDumpParameters",
				8,
				packet.len(),
				packet,
			));
		}
		if packet.len() > 8 {
			return Err(NetworkParseError::UnexpectedTrailer(
				"MionDumpParameters",
				packet.slice(8..),
			));
		}

		// Header only -- read request id is 0, so 4 zeroes, and no body afterwards
		// and no error status so 4 0's for length after.
		let static_bytes: &'static [u8] = &[0_u8, 0, 0, 0, 0, 0, 0, 0];
		if packet != static_bytes {
			return Err(NetworkParseError::DoesntMatchStaticPayload(
				"MionDumpParameters",
				static_bytes,
				packet,
			));
		}

		Ok(Self)
	}
}

impl From<&MionDumpParameters> for Bytes {
	fn from(_: &MionDumpParameters) -> Self {
		BytesMut::zeroed(8).freeze()
	}
}
impl From<MionDumpParameters> for Bytes {
	fn from(value: MionDumpParameters) -> Self {
		Self::from(&value)
	}
}

const DUMPED_MION_PARAMETERS_FIELDS: &[NamedField<'static>] = &[NamedField::new("parameters")];
/// The response from a MION documenting all the parameters available on this board.
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct DumpedMionParameters {
	/// All of the parameters available on this board.
	parameters: Bytes,
}

impl DumpedMionParameters {
	/// Get the entire set of parameters for you to mess around with.
	#[must_use]
	pub const fn get_raw_parameters(&self) -> &Bytes {
		&self.parameters
	}

	/// Get a parameter by a name.
	///
	/// ## Errors
	///
	/// - If the name of this parameter is not known.
	pub fn get_parameter_by_name(&self, name: &str) -> Result<u8, MionParameterAPIError> {
		index_from_parameter_name(name)
			.map(|index| self.parameters[index])
			.ok_or_else(|| MionParameterAPIError::NameNotKnown(name.to_owned()))
	}

	/// Get a parameter by a particular index.
	///
	/// ## Errors
	///
	/// - If the index is not within the range of valid parameters.
	pub fn get_parameter_by_index(&self, index: usize) -> Result<u8, MionParameterAPIError> {
		if index > 511 {
			return Err(MionParameterAPIError::NotInRange(index));
		}
		Ok(self.parameters[index])
	}
}

impl TryFrom<Bytes> for DumpedMionParameters {
	type Error = NetworkParseError;

	fn try_from(packet: Bytes) -> Result<Self, Self::Error> {
		if packet.len() < 520 {
			return Err(NetworkParseError::NotEnoughData(
				"DumpedMionParameters",
				520,
				packet.len(),
				packet,
			));
		}
		if packet.len() > 520 {
			return Err(NetworkParseError::UnexpectedTrailer(
				"DumpedMionParameters",
				packet.slice(520..),
			));
		}

		let header = packet.slice(..8);
		let packet_type = PacketType::try_from(i32::from_le_bytes([
			header[0], header[1], header[2], header[3],
		]))?;
		if packet_type != PacketType::Read {
			return Err(MionParamProtocolError::PacketType(i32::from(packet_type)).into());
		}
		let size_or_error = i32::from_le_bytes([header[4], header[5], header[6], header[7]]);
		if size_or_error != 512 {
			return Err(MionParamProtocolError::ErrorCode(size_or_error).into());
		}
		let parameters = packet.slice(8..);

		Ok(Self { parameters })
	}
}

impl From<&DumpedMionParameters> for Bytes {
	fn from(value: &DumpedMionParameters) -> Self {
		let mut buff = BytesMut::with_capacity(520);
		buff.put_i32_le(i32::from(PacketType::Read));
		// The size of parameters.
		buff.put_i32_le(512);
		buff.extend_from_slice(&value.parameters);
		buff.freeze()
	}
}
impl From<DumpedMionParameters> for Bytes {
	fn from(value: DumpedMionParameters) -> Self {
		Self::from(&value)
	}
}

impl Structable for DumpedMionParameters {
	fn definition(&self) -> StructDef<'_> {
		StructDef::new_static(
			"DumpedMionParameters",
			Fields::Named(DUMPED_MION_PARAMETERS_FIELDS),
		)
	}
}
impl Valuable for DumpedMionParameters {
	fn as_value(&self) -> Value<'_> {
		Value::Structable(self)
	}

	fn visit(&self, visitor: &mut dyn valuable::Visit) {
		let dump = ValuableParameterDump(&self.parameters);

		visitor.visit_named_fields(&NamedValues::new(
			DUMPED_MION_PARAMETERS_FIELDS,
			&[Valuable::as_value(&dump)],
		));
	}
}

/// Set all of the parameters on a MION device.
///
/// There is no way to simply set one byte, you have to set them all at once.
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct SetMionParameters {
	parameters: Bytes,
}

impl SetMionParameters {
	/// Create a new packet given a previously dumped header, and the new
	/// complete set of parameters.
	///
	/// This will take a valid `dumped_header`, and will modify it into a
	/// header for setting mion parameters.
	///
	/// ## Errors
	///
	/// - If the `parameters` argument is not exactly 512 bytes long.
	pub fn new(parameters: Bytes) -> Result<Self, MionParameterAPIError> {
		if parameters.len() != 512 {
			return Err(MionParameterAPIError::BodyNotCorrectLength(
				parameters.len(),
			));
		}

		Ok(Self { parameters })
	}

	/// Get the raw parameter body, which contains the raw list of bytes.
	#[must_use]
	pub const fn get_raw_parameters(&self) -> &Bytes {
		&self.parameters
	}

	/// Get a parameter by a name.
	///
	/// ## Errors
	///
	/// - If the name of this parameter is not known.
	pub fn get_parameter_by_name(&self, name: &str) -> Result<u8, MionParameterAPIError> {
		index_from_parameter_name(name)
			.map(|index| self.parameters[index])
			.ok_or_else(|| MionParameterAPIError::NameNotKnown(name.to_owned()))
	}

	/// Get a parameter by a particular index.
	///
	/// ## Errors
	///
	/// - If the index is not within the range of valid parameters.
	pub fn get_parameter_by_index(&self, index: usize) -> Result<u8, MionParameterAPIError> {
		if index > 511 {
			return Err(MionParameterAPIError::NotInRange(index));
		}
		Ok(self.parameters[index])
	}
}

impl TryFrom<Bytes> for SetMionParameters {
	type Error = NetworkParseError;

	fn try_from(packet: Bytes) -> Result<Self, Self::Error> {
		if packet.len() < 520 {
			return Err(NetworkParseError::NotEnoughData(
				"SetMionParameters",
				520,
				packet.len(),
				packet,
			));
		}
		if packet.len() > 520 {
			return Err(NetworkParseError::UnexpectedTrailer(
				"SetMionParameters",
				packet.slice(520..),
			));
		}

		let header = packet.slice(..8);
		let packet_type = PacketType::try_from(i32::from_le_bytes([
			header[0], header[1], header[2], header[3],
		]))?;
		if packet_type != PacketType::Write {
			return Err(MionParamProtocolError::PacketType(i32::from(packet_type)).into());
		}
		let size_or_error_code = i32::from_le_bytes([header[4], header[5], header[6], header[7]]);
		if size_or_error_code != 512 {
			return Err(MionParamProtocolError::ErrorCode(size_or_error_code).into());
		}
		let parameters = packet.slice(8..);

		Ok(Self { parameters })
	}
}

impl From<&SetMionParameters> for Bytes {
	fn from(value: &SetMionParameters) -> Self {
		let mut buff = BytesMut::with_capacity(520);
		buff.put_i32_le(i32::from(PacketType::Write));
		// The size of the body.
		buff.put_i32_le(512);
		buff.extend_from_slice(&value.parameters);
		buff.freeze()
	}
}
impl From<SetMionParameters> for Bytes {
	fn from(value: SetMionParameters) -> Self {
		Self::from(&value)
	}
}

/// The response to a [`SetMionParameters`] being sent.
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct SetMionParametersResponse {
	/// The return code, anything that isn't a 0 should be considered an error.
	return_code: i32,
}

impl SetMionParametersResponse {
	/// Get the return code of this request.
	///
	/// 0 is used to indicate success, anything else is considered a failure.
	#[must_use]
	pub const fn get_return_code(&self) -> i32 {
		self.return_code
	}

	/// If this operation can be considered a success.
	#[must_use]
	pub const fn is_success(&self) -> bool {
		self.return_code == 0
	}

	/// If this operation error'd out, and was not successful.
	#[must_use]
	pub const fn is_error(&self) -> bool {
		self.return_code != 0
	}
}

impl TryFrom<Bytes> for SetMionParametersResponse {
	type Error = NetworkParseError;

	fn try_from(packet: Bytes) -> Result<Self, Self::Error> {
		if packet.len() < 12 {
			return Err(NetworkParseError::NotEnoughData(
				"SetMionParametersResponse",
				12,
				packet.len(),
				packet,
			));
		}
		if packet.len() > 12 {
			return Err(NetworkParseError::UnexpectedTrailer(
				"SetMionParametersResponse",
				packet.slice(12..),
			));
		}

		let header = packet.slice(..8);
		let packet_type = PacketType::try_from(i32::from_le_bytes([
			header[0], header[1], header[2], header[3],
		]))?;
		if packet_type != PacketType::Write {
			return Err(MionParamProtocolError::PacketType(i32::from(packet_type)).into());
		}
		let size_or_status = i32::from_le_bytes([header[4], header[5], header[6], header[7]]);
		if size_or_status != 4 {
			return Err(MionParamProtocolError::ErrorCode(size_or_status).into());
		}

		let body = packet.slice(8..);
		let return_code = i32::from_le_bytes([body[0], body[1], body[2], body[3]]);

		Ok(Self { return_code })
	}
}

impl From<&SetMionParametersResponse> for Bytes {
	fn from(value: &SetMionParametersResponse) -> Self {
		let mut buff = BytesMut::with_capacity(12);
		buff.put_i32_le(i32::from(PacketType::Write));
		// Size of body.
		buff.put_i32_le(4);
		buff.put_i32_le(value.return_code);
		buff.freeze()
	}
}
impl From<SetMionParametersResponse> for Bytes {
	fn from(value: SetMionParametersResponse) -> Self {
		Self::from(&value)
	}
}

#[cfg(test)]
mod unit_tests {
	use super::*;
	use bytes::Bytes;
	use std::sync::LazyLock;

	static REAL_LIFE_DUMPED_MION_PARAMETERS_PACKET: LazyLock<Vec<u8>> = LazyLock::new(|| {
		vec![
			0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x02, 0x02, 0x0c, 0x0d,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff,
		]
	});
	static REAL_LIFE_SET_MION_PARAMETERS_PACKET: LazyLock<Vec<u8>> = LazyLock::new(|| {
		vec![
			0x01, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x02, 0x02, 0x0c, 0x0d,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
			0xff, 0x45,
		]
	});

	#[test]
	pub fn deser_mion_dump_parameters() {
		// Round-trip success.
		{
			assert!(
				MionDumpParameters::try_from(Bytes::from(MionDumpParameters)).is_ok(),
				"Deserializing a serialized MionDumpParameters was not a success!",
			);
		}

		// Size Differences.
		{
			let short_data = vec![0x0; 4];
			let too_much_data = vec![0x0; 16];

			assert_eq!(
				MionDumpParameters::try_from(Bytes::from(short_data.clone())),
				Err(NetworkParseError::NotEnoughData(
					"MionDumpParameters",
					8,
					short_data.len(),
					Bytes::from(short_data),
				)),
			);

			assert_eq!(
				MionDumpParameters::try_from(Bytes::from(too_much_data.clone())),
				Err(NetworkParseError::UnexpectedTrailer(
					"MionDumpParameters",
					Bytes::from(too_much_data).slice(8..),
				)),
			);
		}

		// Invalid packet contents
		{
			let invalid_static_packet = vec![0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0];
			assert_eq!(
				MionDumpParameters::try_from(Bytes::from(invalid_static_packet.clone())),
				Err(NetworkParseError::DoesntMatchStaticPayload(
					"MionDumpParameters",
					&[0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0],
					Bytes::from(invalid_static_packet),
				)),
			);
		}
	}

	#[test]
	pub fn deser_dumped_mion_parameters() {
		// Size Differences
		{
			let short_data = vec![0x0; 519];
			let too_long_data = vec![0x0; 521];

			assert_eq!(
				DumpedMionParameters::try_from(Bytes::from(short_data.clone())),
				Err(NetworkParseError::NotEnoughData(
					"DumpedMionParameters",
					520,
					short_data.len(),
					Bytes::from(short_data),
				)),
			);

			assert_eq!(
				DumpedMionParameters::try_from(Bytes::from(too_long_data.clone())),
				Err(NetworkParseError::UnexpectedTrailer(
					"DumpedMionParameters",
					Bytes::from(too_long_data).slice(520..),
				)),
			);
		}

		// Invalid Command
		{
			// Bogus value.
			let mut data = vec![0x0; 520];
			data[0] = 11;
			let packet_with_bad_data = Bytes::from(data);

			assert_eq!(
				DumpedMionParameters::try_from(packet_with_bad_data),
				Err(MionParamProtocolError::PacketType(11).into()),
			);

			// Write request, isn't valid either.
			let mut data = vec![0x0; 520];
			data[0] = 1;
			let packet_with_bad_data = Bytes::from(data);

			assert_eq!(
				DumpedMionParameters::try_from(packet_with_bad_data),
				Err(MionParamProtocolError::PacketType(1).into()),
			);
		}

		// Real life packet
		{
			let result = DumpedMionParameters::try_from(Bytes::from(
				REAL_LIFE_DUMPED_MION_PARAMETERS_PACKET.clone(),
			));
			assert!(
				result.is_ok(),
				"Failed to parse a real life DumpedMionParameters packet:\n\n  {result:?}",
			);
			let result_two = DumpedMionParameters::try_from(Bytes::from(result.unwrap()));
			assert!(
				result_two.is_ok(),
				"Failed to round-trip real life DumpedMionParameters:\n\n  {result_two:?}",
			);
		}
	}

	#[test]
	pub fn dumped_mion_parameters_api() {
		let parsed_packet = DumpedMionParameters::try_from(Bytes::from(
			REAL_LIFE_DUMPED_MION_PARAMETERS_PACKET.clone(),
		))
		.expect("Failed to parse real life dumped mion parmaeters packet!");

		assert_eq!(
			parsed_packet.get_raw_parameters(),
			&REAL_LIFE_DUMPED_MION_PARAMETERS_PACKET[8..],
			".get_raw_parameters() for DumpedMionParameters did not return the correct body!",
		);

		assert_eq!(
			parsed_packet.get_parameter_by_name("major-version"),
			Ok(0x02),
		);
		assert_eq!(
			parsed_packet.get_parameter_by_name("minor version"),
			Ok(0x0C),
		);
		assert_eq!(
			// Name also should accept indexes that are just a string.
			parsed_packet.get_parameter_by_name("5"),
			Ok(0x0D),
		);
		assert_eq!(
			parsed_packet.get_parameter_by_name("512"),
			Err(MionParameterAPIError::NameNotKnown("512".to_owned())),
		);

		assert_eq!(parsed_packet.get_parameter_by_index(511), Ok(0xFF));
		assert_eq!(
			// 512 is out of bounds as we start counting at 0.
			parsed_packet.get_parameter_by_index(512),
			Err(MionParameterAPIError::NotInRange(512)),
		);
	}

	#[test]
	pub fn deser_set_mion_parameters() {
		// Size Differences
		{
			let short_data = vec![0x0; 519];
			let too_long_data = vec![0x0; 521];

			assert_eq!(
				SetMionParameters::try_from(Bytes::from(short_data.clone())),
				Err(NetworkParseError::NotEnoughData(
					"SetMionParameters",
					520,
					short_data.len(),
					Bytes::from(short_data),
				)),
			);

			assert_eq!(
				SetMionParameters::try_from(Bytes::from(too_long_data.clone())),
				Err(NetworkParseError::UnexpectedTrailer(
					"SetMionParameters",
					Bytes::from(too_long_data).slice(520..),
				)),
			);
		}

		// Invalid Command
		{
			// Bogus value.
			let mut data = vec![0x0; 520];
			data[0] = 11;
			let packet_with_bad_data = Bytes::from(data);

			assert_eq!(
				SetMionParameters::try_from(packet_with_bad_data),
				Err(MionParamProtocolError::PacketType(11).into()),
			);

			// Write request, isn't valid either.
			let mut data = vec![0x0; 520];
			data[0] = 0;
			let packet_with_bad_data = Bytes::from(data);

			assert_eq!(
				SetMionParameters::try_from(packet_with_bad_data),
				Err(MionParamProtocolError::PacketType(0).into()),
			);
		}

		// Real life packet
		{
			let result = SetMionParameters::try_from(Bytes::from(
				REAL_LIFE_SET_MION_PARAMETERS_PACKET.clone(),
			));
			assert!(
				result.is_ok(),
				"Failed to parse a real life SetMionParameters packet:\n\n  {result:?}",
			);
			let result_two = SetMionParameters::try_from(Bytes::from(result.unwrap()));
			assert!(
				result_two.is_ok(),
				"Failed to round-trip real life SetMionParameters:\n\n  {result_two:?}",
			);
		}
	}

	#[test]
	pub fn set_mion_parameters_api() {
		// Test creation APIs.
		{
			// We should always construct from a dumped packet, because we don't yet
			// know what the header values are, and they could be important :)
			assert!(
				SetMionParameters::new(Bytes::from(&REAL_LIFE_DUMPED_MION_PARAMETERS_PACKET[8..]))
					.is_ok(),
				"Failed to construct `SetMionParameters` from a valid dumped parameters set!",
			);

			// Invalid parameters length
			assert_eq!(
				SetMionParameters::new(Bytes::from(&REAL_LIFE_DUMPED_MION_PARAMETERS_PACKET[7..])),
				Err(MionParameterAPIError::BodyNotCorrectLength(513)),
			);
		}

		// Test non-creation APIs.
		{
			let parsed_packet = SetMionParameters::try_from(Bytes::from(
				REAL_LIFE_SET_MION_PARAMETERS_PACKET.clone(),
			))
			.expect("Failed to parse real life set mion parameters packet!");

			assert_eq!(
				parsed_packet.get_raw_parameters(),
				&REAL_LIFE_SET_MION_PARAMETERS_PACKET[8..],
				".get_raw_parameters() for SetMionParameters did not return the correct body!",
			);

			assert_eq!(
				parsed_packet.get_parameter_by_name("major-version"),
				Ok(0x02),
			);
			assert_eq!(
				parsed_packet.get_parameter_by_name("minor version"),
				Ok(0x0C),
			);
			assert_eq!(
				// Name also should accept indexes that are just a string.
				parsed_packet.get_parameter_by_name("5"),
				Ok(0x0D),
			);
			assert_eq!(
				parsed_packet.get_parameter_by_name("512"),
				Err(MionParameterAPIError::NameNotKnown("512".to_owned())),
			);

			assert_eq!(parsed_packet.get_parameter_by_index(511), Ok(69));
			assert_eq!(
				// 512 is out of bounds as we start counting at 0.
				parsed_packet.get_parameter_by_index(512),
				Err(MionParameterAPIError::NotInRange(512)),
			);
		}
	}

	#[test]
	pub fn deser_set_mion_parameters_response() {
		// Size Mismatch
		{
			let short_data = vec![0x0; 11];
			let too_long_data = vec![0x0; 13];

			assert_eq!(
				SetMionParametersResponse::try_from(Bytes::from(short_data.clone())),
				Err(NetworkParseError::NotEnoughData(
					"SetMionParametersResponse",
					12,
					short_data.len(),
					Bytes::from(short_data),
				)),
			);

			assert_eq!(
				SetMionParametersResponse::try_from(Bytes::from(too_long_data.clone())),
				Err(NetworkParseError::UnexpectedTrailer(
					"SetMionParametersResponse",
					Bytes::from(too_long_data).slice(12..),
				)),
			);
		}

		// unknown packet type
		{
			// Bogus Packet Type
			assert_eq!(
				SetMionParametersResponse::try_from(Bytes::from(vec![
					// Packet type -- bogus
					0x11, 0x0, 0x0, 0x0, // Body length.
					0x4, 0x0, 0x0, 0x0, // return code.
					0x0, 0x0, 0x0, 0x0,
				])),
				Err(MionParamProtocolError::PacketType(0x11).into()),
			);

			// Wrong Packet Type -- read instead of write
			assert_eq!(
				SetMionParametersResponse::try_from(Bytes::from(vec![
					// Packet type -- read
					0x0, 0x0, 0x0, 0x0, // Body length.
					0x4, 0x0, 0x0, 0x0, // Return Code
					0x0, 0x0, 0x0, 0x0,
				])),
				Err(MionParamProtocolError::PacketType(0).into()),
			);
		}

		// Bad Size/Status
		{
			assert_eq!(
				SetMionParametersResponse::try_from(Bytes::from(vec![
					// Packet type -- write
					0x1, 0x0, 0x0, 0x0, // bogus size.
					0x5, 0x0, 0x0, 0x0, // return code
					0x0, 0x0, 0x0, 0x0,
				])),
				Err(MionParamProtocolError::ErrorCode(5).into()),
			);
		}

		// Successful -- includes roundtrip
		{
			let result = SetMionParametersResponse::try_from(Bytes::from(vec![
				// Packet type -- write
				0x1, 0x0, 0x0, 0x0, // Size -- correct.
				0x4, 0x0, 0x0, 0x0, // Return code -- success!
				0x0, 0x0, 0x0, 0x0,
			]));
			assert!(
				result.is_ok(),
				"Failed to respond to real life SetMionParametersResponse success!"
			);
			assert!(
				SetMionParametersResponse::try_from(Bytes::from(result.unwrap())).is_ok(),
				"Failed to round-trip real-life SetMionParametersResponse success!",
			);
		}
	}
}