ipmi_rs_core/connection/
netfn.rs

1macro_rules! netfn {
2    ($($name:ident => [$req:literal | $resp:literal]),*) => {
3        #[derive(Debug, Clone, Copy, PartialEq)]
4        #[allow(missing_docs)]
5        pub enum NetFn {
6            $($name,)*
7            /// A reserved netfn.
8            Reserved(u8),
9        }
10
11        impl From<u8> for NetFn {
12            fn from(value: u8) -> Self {
13                match value {
14                    $($req | $resp => Self::$name,)*
15                    v => Self::Reserved(v),
16                }
17            }
18        }
19
20        impl NetFn {
21            /// Get the raw data for the request value of this netfn.
22            pub fn request_value(&self) -> u8 {
23                match self {
24                    $(Self::$name => $req,)*
25                    Self::Reserved(v) => {
26                        if v % 2 == 1 {
27                            v - 1
28                        } else {
29                            *v
30                        }
31                    }
32                }
33            }
34
35            /// Get the raw data for the response value of this netfn.
36            pub fn response_value(&self) -> u8 {
37                match self {
38                    $(Self::$name => $resp,)*
39                    NetFn::Reserved(v) => {
40                        if v % 2 == 0 {
41                            v + 1
42                        } else {
43                            *v
44                        }
45                    }
46                }
47            }
48        }
49    };
50}
51
52netfn!(
53    Chassis => [0x00 | 0x01],
54    Bridge => [0x02 | 0x03],
55    SensorEvent => [0x04 | 0x05],
56    App => [0x06 | 0x07],
57    Firmware => [0x08 | 0x09],
58    Storage => [0x0A | 0x0B],
59    Transport => [0x0C | 0x0D]
60);
61
62impl NetFn {
63    /// Check whether `v` is a response value.
64    pub fn is_response_value(v: u8) -> bool {
65        v % 2 == 0
66    }
67
68    /// Check whether `v` is a request value.
69    pub fn is_request_value(v: u8) -> bool {
70        !Self::is_response_value(v)
71    }
72}