pub const METHOD_BUFFERED: u32 = 0;
pub const METHOD_IN_DIRECT: u32 = 1;
pub const METHOD_OUT_DIRECT: u32 = 2;
pub const METHOD_NEITHER: u32 = 3;
pub const FILE_ANY_ACCESS: u32 = 0;
pub const FILE_READ_ACCESS: u32 = 0x0001;
pub const FILE_WRITE_ACCESS: u32 = 0x0002;
pub const FILE_DEVICE_VCK: u32 = 0x22;
#[inline]
pub const fn ctl_code(device_type: u32, function: u32, method: u32, access: u32) -> u32 {
(device_type << 16) | (access << 14) | (function << 2) | method
}
#[macro_export]
macro_rules! ctl_code {
($device_type:expr, $function:expr, $method:expr, $access:expr) => {
$crate::ioctl::ctl_code($device_type, $function, $method, $access)
};
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ctl_code_matches_windows_layout() {
assert_eq!(
ctl_code(0x22, 0, METHOD_BUFFERED, FILE_ANY_ACCESS),
0x0022_0000
);
assert_eq!(
ctl_code(0, 0, METHOD_BUFFERED, FILE_READ_ACCESS),
0x0000_4000
);
assert_eq!(
ctl_code(0, 0, METHOD_BUFFERED, FILE_WRITE_ACCESS),
0x0000_8000
);
assert_eq!(
ctl_code(0, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS),
0x0000_2000
);
assert_eq!(ctl_code(0, 0, METHOD_NEITHER, FILE_ANY_ACCESS), 0x0000_0003);
}
#[test]
fn vck_ioctl_codes_are_exact() {
let dt = FILE_DEVICE_VCK;
let m = METHOD_BUFFERED;
let r = FILE_READ_ACCESS;
let w = FILE_WRITE_ACCESS;
assert_eq!(ctl_code(dt, 0x800, m, r), 0x0022_6000); assert_eq!(ctl_code(dt, 0x803, m, r), 0x0022_600c);
assert_eq!(ctl_code(dt, 0x801, m, w), 0x0022_a004); assert_eq!(ctl_code(dt, 0x802, m, w), 0x0022_a008); assert_eq!(ctl_code(dt, 0x804, m, w), 0x0022_a010); assert_eq!(ctl_code(dt, 0x805, m, w), 0x0022_a014); assert_eq!(ctl_code(dt, 0x806, m, w), 0x0022_a018); assert_eq!(ctl_code(dt, 0x807, m, w), 0x0022_a01c); assert_eq!(ctl_code(dt, 0x808, m, w), 0x0022_a020); assert_eq!(ctl_code(dt, 0x809, m, w), 0x0022_a024);
assert_eq!(ctl_code(dt, 0x80a, m, r), 0x0022_6028);
assert_eq!(ctl_code(dt, 0x80b, m, r), 0x0022_602c); }
#[test]
fn macro_and_fn_agree() {
assert_eq!(
ctl_code!(FILE_DEVICE_VCK, 0x800, METHOD_BUFFERED, FILE_READ_ACCESS),
ctl_code(FILE_DEVICE_VCK, 0x800, METHOD_BUFFERED, FILE_READ_ACCESS),
);
}
}