smoltcp/
macros.rs

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
#[cfg(not(test))]
#[cfg(feature = "log")]
macro_rules! net_log {
    (trace, $($arg:expr),*) => { log::trace!($($arg),*) };
    (debug, $($arg:expr),*) => { log::debug!($($arg),*) };
}

#[cfg(test)]
#[cfg(feature = "log")]
macro_rules! net_log {
    (trace, $($arg:expr),*) => { println!($($arg),*) };
    (debug, $($arg:expr),*) => { println!($($arg),*) };
}

#[cfg(feature = "defmt")]
macro_rules! net_log {
    (trace, $($arg:expr),*) => { defmt::trace!($($arg),*) };
    (debug, $($arg:expr),*) => { defmt::debug!($($arg),*) };
}

#[cfg(not(any(feature = "log", feature = "defmt")))]
macro_rules! net_log {
    ($level:ident, $($arg:expr),*) => {{ $( let _ = $arg; )* }}
}

macro_rules! net_trace {
    ($($arg:expr),*) => (net_log!(trace, $($arg),*));
}

macro_rules! net_debug {
    ($($arg:expr),*) => (net_log!(debug, $($arg),*));
}

macro_rules! enum_with_unknown {
    (
        $( #[$enum_attr:meta] )*
        pub enum $name:ident($ty:ty) {
            $(
              $( #[$variant_attr:meta] )*
              $variant:ident = $value:expr
            ),+ $(,)?
        }
    ) => {
        #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
        #[cfg_attr(feature = "defmt", derive(defmt::Format))]
        $( #[$enum_attr] )*
        pub enum $name {
            $(
              $( #[$variant_attr] )*
              $variant
            ),*,
            Unknown($ty)
        }

        impl ::core::convert::From<$ty> for $name {
            fn from(value: $ty) -> Self {
                match value {
                    $( $value => $name::$variant ),*,
                    other => $name::Unknown(other)
                }
            }
        }

        impl ::core::convert::From<$name> for $ty {
            fn from(value: $name) -> Self {
                match value {
                    $( $name::$variant => $value ),*,
                    $name::Unknown(other) => other
                }
            }
        }
    }
}

#[cfg(feature = "proto-rpl")]
macro_rules! get {
    ($buffer:expr, into: $into:ty, fun: $fun:ident, field: $field:expr $(,)?) => {
        {
            <$into>::$fun(&$buffer.as_ref()[$field])
        }
    };

    ($buffer:expr, into: $into:ty, field: $field:expr $(,)?) => {
        get!($buffer, into: $into, field: $field, shift: 0, mask: 0b1111_1111)
    };

    ($buffer:expr, into: $into:ty, field: $field:expr, mask: $bit_mask:expr $(,)?) => {
        get!($buffer, into: $into, field: $field, shift: 0, mask: $bit_mask)
    };

    ($buffer:expr, into: $into:ty, field: $field:expr, shift: $bit_shift:expr, mask: $bit_mask:expr $(,)?) => {
        {
            <$into>::from((&$buffer.as_ref()[$field] >> $bit_shift) & $bit_mask)
        }
    };

    ($buffer:expr, field: $field:expr $(,)?) => {
        get!($buffer, field: $field, shift: 0, mask: 0b1111_1111)
    };

    ($buffer:expr, field: $field:expr, mask: $bit_mask:expr $(,)?) => {
        get!($buffer, field: $field, shift: 0, mask: $bit_mask)
    };

    ($buffer:expr, field: $field:expr, shift: $bit_shift:expr, mask: $bit_mask:expr $(,)?)
        =>
    {
        {
            (&$buffer.as_ref()[$field] >> $bit_shift) & $bit_mask
        }
    };

    ($buffer:expr, u16, field: $field:expr $(,)?) => {
        {
            NetworkEndian::read_u16(&$buffer.as_ref()[$field])
        }
    };

    ($buffer:expr, bool, field: $field:expr, shift: $bit_shift:expr, mask: $bit_mask:expr $(,)?) => {
        {
            (($buffer.as_ref()[$field] >> $bit_shift) & $bit_mask) == 0b1
        }
    };

    ($buffer:expr, u32, field: $field:expr $(,)?) => {
        {
            NetworkEndian::read_u32(&$buffer.as_ref()[$field])
        }
    };
}

#[cfg(feature = "proto-rpl")]
macro_rules! set {
    ($buffer:expr, address: $address:ident, field: $field:expr $(,)?) => {{
        $buffer.as_mut()[$field].copy_from_slice($address.as_bytes());
    }};

    ($buffer:expr, $value:ident, field: $field:expr $(,)?) => {
        set!($buffer, $value, field: $field, shift: 0, mask: 0b1111_1111)
    };

    ($buffer:expr, $value:ident, field: $field:expr, mask: $bit_mask:expr $(,)?) => {
        set!($buffer, $value, field: $field, shift: 0, mask: $bit_mask)
    };

    ($buffer:expr, $value:ident, field: $field:expr, shift: $bit_shift:expr, mask: $bit_mask:expr $(,)?) => {{
        let raw =
            ($buffer.as_ref()[$field] & !($bit_mask << $bit_shift)) | ($value << $bit_shift);
        $buffer.as_mut()[$field] = raw;
    }};

    ($buffer:expr, $value:ident, bool, field: $field:expr, mask: $bit_mask:expr $(,)?) => {
        set!($buffer, $value, bool, field: $field, shift: 0, mask: $bit_mask);
    };

    ($buffer:expr, $value:ident, bool, field: $field:expr, shift: $bit_shift:expr, mask: $bit_mask:expr $(,)?) => {{
        let raw = ($buffer.as_ref()[$field] & !($bit_mask << $bit_shift))
            | (if $value { 0b1 } else { 0b0 } << $bit_shift);
        $buffer.as_mut()[$field] = raw;
    }};

    ($buffer:expr, $value:ident, u16, field: $field:expr $(,)?) => {{
        NetworkEndian::write_u16(&mut $buffer.as_mut()[$field], $value);
    }};

    ($buffer:expr, $value:ident, u32, field: $field:expr $(,)?) => {{
        NetworkEndian::write_u32(&mut $buffer.as_mut()[$field], $value);
    }};
}