Skip to main content

batman_robin/cli/
utils.rs

1/// Converts a VLAN ID stored in a `u16` to a printable integer.
2///
3/// The `vid` format uses the highest bit (bit 15) as a validity flag:
4/// - If bit 15 is set, the lower 12 bits contain the actual VLAN ID.
5/// - If bit 15 is not set, the VLAN ID is considered invalid.
6///
7/// # Arguments
8/// - `vid`: The raw VLAN ID value (`u16`) from the kernel.
9///
10/// # Returns
11/// - The VLAN ID as `i32` if valid (bit 15 set).
12/// - `-1` if the VLAN ID is invalid (bit 15 not set).
13///
14/// # Example
15/// ```
16/// use batman_robin::cli::utils::print_vid;
17///
18/// let vid: u16 = 0x8005; // bit 15 set, VLAN ID = 5
19/// assert_eq!(print_vid(vid), 5);
20///
21/// let invalid_vid: u16 = 0x0005; // bit 15 not set
22/// assert_eq!(print_vid(invalid_vid), -1);
23/// ```
24pub fn print_vid(vid: u16) -> i32 {
25    if (vid & (1 << 15)) != 0 {
26        (vid & 0x0fff) as i32
27    } else {
28        -1
29    }
30}