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
use crate::mion::proto::cgis::MionCGIErrors;
#[cfg(feature = "clients")]
use mac_address::MacAddress;
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::net::Ipv4Addr;

/// The parameters you can fetch from the `setup.cgi` page.
///
/// While this page is normally really more intended for humans, rather
/// than an automated parsers. HOWEVER, `fsemul` & several other useful
/// functions are only available on this page.
///
/// Why they're not available in the parameter space, or why there's not
/// a `get_param` action, but is a `set_param` action I'll never know.
/// It will make me sad though.
#[allow(
	// Clippy this is not a state machine, >:(
	clippy::struct_excessive_bools,
)]
#[cfg_attr(docsrs, doc(cfg(feature = "clients")))]
#[cfg(feature = "clients")]
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct SetupParameters {
	/// The 'ip address' of the bridge, default value is "192.168.0.1".
	///
	/// *note: this value doesn't get used when DHCP is on)*.
	///
	/// Internal HTML ID: `id_5`
	static_ip_address: Ipv4Addr,
	/// The Subnet mask of the bridge, default value is "255.255.255.0".
	///
	/// Internal HTML ID: `id_6`
	subnet_mask: Ipv4Addr,
	/// The default Gateway to reach out to for the bridge, default value is
	/// "0.0.0.0".
	///
	/// Internal HTML ID: `id_7`
	default_gateway: Ipv4Addr,
	/// If the device should be using DHCP, default is TRUE.
	///
	/// Internal HTML ID: `id_4`
	dhcp: bool,
	/// If the device should query it's own DNS Servers, default is FALSE.
	///
	/// Internal HTML ID: `id_8`
	dns: bool,
	/// The Primary DNS Server to reach out too if DNS is flipped on, default is
	/// "0.0.0.0".
	///
	/// Internal HTML ID: `id_9`
	primary_dns_server: Ipv4Addr,
	/// The Secondary DNS Server to reach out too if DNS is flipped on, default is
	/// "0.0.0.0".
	///
	/// Internal HTML ID: `id_10`.
	secondary_dns_server: Ipv4Addr,
	/// If we support jumbo frames on the network, default is TRUE.
	///
	/// Internal HTML ID: `id_11`
	jumbo_frame: bool,
	/// The IP Address of the Host PC to reach out too, default is "0.0.0.0".
	///
	/// Internal HTML ID: `id_12`
	host_pc_ip: Ipv4Addr,
	/// The bank size (or hard-disk) size of the CAT-DEV.
	///
	/// Internal HTML ID is: `id_26`
	bank_size: CatDevBankSize,
	/// The Hard Disk "bank" number to use (which area to read from when booting
	/// a game), default is "0".
	///
	/// Internal HTML ID: `id_27`
	hdd_bank_no: u8,
	/// The port of the ATAPI Emulator, default is `7974`.
	///
	/// Internal HTML ID is: `id_13`
	atapi_emulator_port: u16,
	/// The port of the SDIO Printf/Control traffic, default is `7975`.
	///
	/// Internal HTML ID: `id_14`
	sdio_printf_port: u16,
	/// The port of the SDIO Block Data traffic, default is `7976`.
	///
	/// Internal HTML ID: `id_15`
	sdio_block_port: u16,
	/// The port of the EXI traffic, default is `7977`.
	///
	/// Internal HTML ID: `id_16`
	exi_port: u16,
	/// The port for parameter-space traffic, default is `7978`.
	///
	/// *note: changing this will break official nintendo tools. Although our
	/// tools do uspport it, it will require manually passing the port in. it
	/// is unideal to change this port.*
	///
	/// Internal HTML ID: `id_17`
	parameter_space_port: u16,
	/// If drive timing emulation is on (make the disk slower on purpose),
	/// default is FALSE.
	///
	/// Internal HTML ID: `id_33`
	drive_timing_emulation: bool,
	/// If the CAT-DEV device is operating in CAT-DEV mode, or H-READER mode,
	/// default is FALSE.
	///
	/// Internal HTML ID: `id_32`
	operational_mode_is_reader: bool,
	/// Internal Hard-Drive product revision in hex.
	///
	/// Internal HTML ID: `id_28`
	drive_product_revision: String,
	/// Internal Hard-Drive vendor code in hex.
	///
	/// Internal HTML ID: `id_29`
	drive_vendor_code: String,
	/// Internal Hard-Drive device code in hex.
	///
	/// Internal HTML ID: `id_30`
	drive_device_code: String,
	/// Internal Hard-Drive release date in hex.
	///
	/// Internal HTML ID: `id_31`
	drive_release_date: String,
	/// The unique name of the cat-dev on the network.
	///
	/// Internal HTML ID: `id_2`
	device_name: String,
	/// The MAC Address of the machine.
	///
	/// Internal HTML ID: N/A
	mac_address: MacAddress,
}

#[cfg_attr(docsrs, doc(cfg(feature = "clients")))]
#[cfg(feature = "clients")]
impl SetupParameters {
	/// Create a new setup set of parameters.
	#[allow(
		// This is how many fields we have clippy.
		clippy::too_many_arguments,
		// This isn't a state machine, or anything.
		clippy::fn_params_excessive_bools,
	)]
	#[must_use]
	pub const fn new(
		static_ip_address: Ipv4Addr,
		subnet_mask: Ipv4Addr,
		default_gateway: Ipv4Addr,
		dhcp: bool,
		dns: bool,
		primary_dns_server: Ipv4Addr,
		secondary_dns_server: Ipv4Addr,
		jumbo_frame: bool,
		host_pc_ip: Ipv4Addr,
		bank_size: CatDevBankSize,
		hdd_bank_no: u8,
		atapi_emulator_port: u16,
		sdio_printf_port: u16,
		sdio_block_port: u16,
		exi_port: u16,
		parameter_space_port: u16,
		drive_timing_emulation: bool,
		operational_mode_is_reader: bool,
		drive_product_revision: String,
		drive_vendor_code: String,
		drive_device_code: String,
		drive_release_date: String,
		device_name: String,
		mac_address: MacAddress,
	) -> Self {
		Self {
			static_ip_address,
			subnet_mask,
			default_gateway,
			dhcp,
			dns,
			primary_dns_server,
			secondary_dns_server,
			jumbo_frame,
			host_pc_ip,
			bank_size,
			hdd_bank_no,
			atapi_emulator_port,
			sdio_printf_port,
			sdio_block_port,
			exi_port,
			parameter_space_port,
			drive_timing_emulation,
			operational_mode_is_reader,
			drive_product_revision,
			drive_vendor_code,
			drive_device_code,
			drive_release_date,
			device_name,
			mac_address,
		}
	}

	/// Create a new set of default settings based on the device name, and mac.
	#[must_use]
	pub fn default_settings(device_name: String, mac_address: MacAddress) -> Self {
		Self {
			static_ip_address: Ipv4Addr::new(192, 168, 0, 1),
			subnet_mask: Ipv4Addr::new(255, 255, 255, 0),
			default_gateway: Ipv4Addr::UNSPECIFIED,
			dhcp: true,
			dns: false,
			primary_dns_server: Ipv4Addr::UNSPECIFIED,
			secondary_dns_server: Ipv4Addr::UNSPECIFIED,
			jumbo_frame: true,
			host_pc_ip: Ipv4Addr::UNSPECIFIED,
			bank_size: CatDevBankSize::Blank,
			hdd_bank_no: 0,
			atapi_emulator_port: 7974,
			sdio_printf_port: 7975,
			sdio_block_port: 7976,
			exi_port: 7977,
			parameter_space_port: 7978,
			drive_timing_emulation: false,
			operational_mode_is_reader: false,
			drive_product_revision: String::with_capacity(0),
			drive_vendor_code: String::with_capacity(0),
			drive_device_code: String::with_capacity(0),
			drive_release_date: String::with_capacity(0),
			device_name,
			mac_address,
		}
	}

	/// Get the static ip address being used.
	///
	/// This will return `None` if the console is using DHCP.
	#[must_use]
	pub const fn static_ip_address(&self) -> Option<Ipv4Addr> {
		if self.dhcp {
			None
		} else {
			Some(self.static_ip_address)
		}
	}
	/// Get the value for the static ip address.
	///
	/// *note: may not be correct, and used if dhcp is on.*
	#[must_use]
	pub const fn raw_static_ip_address(&self) -> Ipv4Addr {
		self.static_ip_address
	}

	/// Get the subnet mask for the CAT-DEV device.
	#[must_use]
	pub const fn subnet_mask(&self) -> Ipv4Addr {
		self.subnet_mask
	}

	/// Get the gateway to use for the CAT-DEV.
	#[must_use]
	pub const fn default_gateway(&self) -> Ipv4Addr {
		self.default_gateway
	}

	/// If we're using DHCP to get an IP Address.
	#[must_use]
	pub const fn using_dhcp(&self) -> bool {
		self.dhcp
	}

	/// If we're using our own DNS ips set in our configuration.
	#[must_use]
	pub const fn using_self_managed_dns(&self) -> bool {
		self.dns
	}
	/// Get the primary dns server the cat-dev will be using.
	///
	/// *note: this will return none if the cat-dev isn't using self managed
	/// dns.*
	#[must_use]
	pub const fn primary_dns(&self) -> Option<Ipv4Addr> {
		if self.dns {
			Some(self.primary_dns_server)
		} else {
			None
		}
	}
	/// Get the raw value of the Primary DNS Server for the cat-dev.
	///
	/// *note: this value will not be used if self managed dns is off.
	#[must_use]
	pub const fn raw_primary_dns(&self) -> Ipv4Addr {
		self.primary_dns_server
	}
	/// Get the secondary dns server the cat-dev will be using.
	///
	/// *note: this will return none if the cat-dev isn't using self managed
	/// dns.*
	#[must_use]
	pub const fn secondary_dns(&self) -> Option<Ipv4Addr> {
		if self.dns {
			Some(self.secondary_dns_server)
		} else {
			None
		}
	}
	/// Get the raw value of the Secondary DNS Server for the cat-dev.
	///
	/// *note: this value will not be used if self managed dns is off.
	#[must_use]
	pub const fn raw_secondary_dns(&self) -> Ipv4Addr {
		self.secondary_dns_server
	}

	/// If we're using jumbo frames.
	#[must_use]
	pub const fn jumbo_frame(&self) -> bool {
		self.jumbo_frame
	}

	/// The potential IP Address of the host machine we're using.
	#[must_use]
	pub const fn host_pc_ip_address(&self) -> Ipv4Addr {
		self.host_pc_ip
	}

	/// Get the hard disk size of the cat-dev.
	#[must_use]
	pub const fn hdd_bank_size(&self) -> CatDevBankSize {
		self.bank_size
	}

	/// Get the bank number being used for the HDD.
	#[must_use]
	pub const fn hdd_bank_no(&self) -> u8 {
		self.hdd_bank_no
	}

	/// The port for the ATAPI emulator.
	#[must_use]
	pub const fn atapi_emulator_port(&self) -> u16 {
		self.atapi_emulator_port
	}

	/// The port for SDIO Printf/Control.
	#[must_use]
	pub const fn sdio_printf_port(&self) -> u16 {
		self.sdio_printf_port
	}

	/// The port for SDIO Block Data.
	#[must_use]
	pub const fn sdio_block_port(&self) -> u16 {
		self.sdio_block_port
	}

	/// Get the port for EXI traffic.
	#[must_use]
	pub const fn exi_port(&self) -> u16 {
		self.exi_port
	}

	/// Get the port to use for parameter-space queries.
	#[must_use]
	pub const fn parameter_space_port(&self) -> u16 {
		self.parameter_space_port
	}

	/// If we're using drive-timing emulation of a production console.
	#[must_use]
	pub const fn drive_timing_emulation_enabled(&self) -> bool {
		self.drive_timing_emulation
	}

	/// Check if our operational mode is CAT-DEV mode.
	#[must_use]
	pub const fn is_cat_dev_mode(&self) -> bool {
		!self.operational_mode_is_reader
	}
	/// Check if our operational mode is in H-Reader mode.
	#[must_use]
	pub const fn is_h_reader_mode(&self) -> bool {
		self.operational_mode_is_reader
	}

	/// Get the hard-drive product revision.
	#[must_use]
	pub const fn drive_product_revision(&self) -> &String {
		&self.drive_product_revision
	}

	/// Get the hard-drive vendor code.
	#[must_use]
	pub const fn drive_vendor_code(&self) -> &String {
		&self.drive_vendor_code
	}

	/// Get the hard-drive device code.
	#[must_use]
	pub const fn drive_device_code(&self) -> &String {
		&self.drive_device_code
	}

	/// Get the release date of the internal hard-drive.
	#[must_use]
	pub const fn drive_release_date(&self) -> &String {
		&self.drive_release_date
	}

	/// Get the name of the cat-dev.
	#[must_use]
	pub const fn device_name(&self) -> &String {
		&self.device_name
	}

	#[must_use]
	pub const fn mac_address(&self) -> MacAddress {
		self.mac_address
	}
}

/// The bank sizes a cat-dev hard disk can have normally.
///
/// The blank value is a bit of a weird value, that i'm not sure why they
/// ever had it, but it shows up in the `setup.cgi` page, so whatever they
/// must have had a reason for it.
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum CatDevBankSize {
	/// Unknown choice present in `setup.cgi`
	Blank,
	/// The internal hard-disk has 25 Gigabytes of storage.
	TwentyFiveGbs,
	/// The internal hard-disk has 5 Gigabytes of storage.
	FiveGbs,
	/// The internal hard-disk has 9 Gigabytes of storage.
	NineGbs,
	/// The internal hard-disk has 12 Gigabytes of storage.
	TwelveGbs,
	/// The internal hard-disk has 14 Gigabytes of storage.
	FourteenGbs,
	/// The internal hard-disk has 16 Gigabytes of storage.
	SixteenGbs,
	/// The internal hard-disk has 18 Gigabytes of storage.
	EighteenGbs,
	/// The internal hard-disk has 21 Gigabytes of storage.
	TwentyOneGbs,
}
impl From<&CatDevBankSize> for u32 {
	fn from(size: &CatDevBankSize) -> u32 {
		match *size {
			CatDevBankSize::Blank => 4_294_967_295,
			CatDevBankSize::TwentyFiveGbs => 0,
			CatDevBankSize::FiveGbs => 1,
			CatDevBankSize::NineGbs => 2,
			CatDevBankSize::TwelveGbs => 3,
			CatDevBankSize::FourteenGbs => 4,
			CatDevBankSize::SixteenGbs => 5,
			CatDevBankSize::EighteenGbs => 6,
			CatDevBankSize::TwentyOneGbs => 7,
		}
	}
}
impl From<CatDevBankSize> for u32 {
	fn from(value: CatDevBankSize) -> Self {
		Self::from(&value)
	}
}
impl TryFrom<u32> for CatDevBankSize {
	type Error = MionCGIErrors;

	fn try_from(value: u32) -> Result<Self, Self::Error> {
		match value {
			u32::MAX => Ok(Self::Blank),
			0 => Ok(Self::TwentyFiveGbs),
			1 => Ok(Self::FiveGbs),
			2 => Ok(Self::NineGbs),
			3 => Ok(Self::TwelveGbs),
			4 => Ok(Self::FourteenGbs),
			5 => Ok(Self::SixteenGbs),
			6 => Ok(Self::EighteenGbs),
			7 => Ok(Self::TwentyOneGbs),
			_ => Err(MionCGIErrors::UnknownCatDevBankSizeId(value)),
		}
	}
}
impl Display for CatDevBankSize {
	fn fmt(&self, fmt: &mut Formatter<'_>) -> FmtResult {
		write!(
			fmt,
			"{}",
			match *self {
				Self::Blank => " ",
				Self::TwentyFiveGbs => "25GB",
				Self::FiveGbs => "5GB",
				Self::NineGbs => "9GB",
				Self::TwelveGbs => "12GB",
				Self::FourteenGbs => "14GB",
				Self::SixteenGbs => "16GB",
				Self::EighteenGbs => "18GB",
				Self::TwentyOneGbs => "21GB",
			}
		)
	}
}

#[cfg(test)]
mod unit_tests {
	use super::*;

	#[test]
	pub fn convert_bank_size() {
		for bank_size in vec![
			CatDevBankSize::Blank,
			CatDevBankSize::TwentyFiveGbs,
			CatDevBankSize::FiveGbs,
			CatDevBankSize::NineGbs,
			CatDevBankSize::TwelveGbs,
			CatDevBankSize::FourteenGbs,
			CatDevBankSize::SixteenGbs,
			CatDevBankSize::EighteenGbs,
			CatDevBankSize::TwentyOneGbs,
		] {
			assert_eq!(
				Ok(bank_size),
				CatDevBankSize::try_from(u32::from(bank_size)),
				"bank size: {} was not the same after converting it back & forth",
				bank_size,
			);
		}
	}
}