dot15d4_frame/repr/ie/
nested.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
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
use super::super::super::{
    ChannelHopping, NestedInformationElement, NestedSubId, NestedSubIdLong, NestedSubIdShort,
    TschSlotframeAndLink, TschSynchronization, TschTimeslot,
};
use super::super::super::{Error, Result};

/// A high-level representation of a MLME Payload Information Element.
#[derive(Debug)]
#[cfg_attr(feature = "fuzz", derive(arbitrary::Arbitrary))]
pub enum NestedInformationElementRepr {
    /// TSCH Synchronization Information Element.
    TschSynchronization(TschSynchronizationRepr),
    /// TSCH Timeslot Information Element.
    TschTimeslot(TschTimeslotRepr),
    /// TSCH Slotframe and Link Information Element.
    TschSlotframeAndLink(TschSlotframeAndLinkRepr),
    /// Channel Hopping Information Element.
    ChannelHopping(ChannelHoppingRepr),
}

impl NestedInformationElementRepr {
    /// Parse a Nested Information Element.
    pub fn parse(ie: &NestedInformationElement<&[u8]>) -> Result<Self> {
        Ok(match ie.sub_id() {
            NestedSubId::Short(NestedSubIdShort::TschSynchronization) => Self::TschSynchronization(
                TschSynchronizationRepr::parse(&TschSynchronization::new(ie.content())?),
            ),
            NestedSubId::Short(NestedSubIdShort::TschTimeslot) => {
                Self::TschTimeslot(TschTimeslotRepr::parse(&TschTimeslot::new(ie.content())?))
            }
            NestedSubId::Short(NestedSubIdShort::TschSlotframeAndLink) => {
                Self::TschSlotframeAndLink(TschSlotframeAndLinkRepr::parse(
                    &TschSlotframeAndLink::new(ie.content())?,
                ))
            }
            NestedSubId::Long(NestedSubIdLong::ChannelHopping) => Self::ChannelHopping(
                ChannelHoppingRepr::parse(&ChannelHopping::new(ie.content())?),
            ),
            _ => return Err(Error),
        })
    }

    /// The buffer length required to emit the Nested Information Element.
    pub fn buffer_len(&self) -> usize {
        2 + self.inner_len()
    }

    /// The buffer length required to emit the inner part of the Nested
    /// Information Element.
    pub fn inner_len(&self) -> usize {
        match self {
            Self::TschSynchronization(repr) => repr.buffer_len(),
            Self::TschTimeslot(repr) => repr.buffer_len(),
            Self::TschSlotframeAndLink(repr) => repr.buffer_len(),
            Self::ChannelHopping(repr) => repr.buffer_len(),
        }
    }

    /// Emit the Nested Information Element into a buffer.
    pub fn emit(&self, w: &mut NestedInformationElement<&mut [u8]>) {
        let id = NestedSubId::from(self);

        w.clear();
        w.set_length(self.inner_len() as u16, id);
        w.set_sub_id(id);

        match self {
            Self::TschSynchronization(repr) => {
                repr.emit(&mut TschSynchronization::new_unchecked(w.content_mut()))
            }
            Self::TschTimeslot(repr) => {
                repr.emit(&mut TschTimeslot::new_unchecked(w.content_mut()))
            }
            Self::TschSlotframeAndLink(repr) => {
                repr.emit(&mut TschSlotframeAndLink::new_unchecked(w.content_mut()))
            }
            Self::ChannelHopping(repr) => {
                repr.emit(&mut ChannelHopping::new_unchecked(w.content_mut()))
            }
        }
    }
}

impl From<&NestedInformationElementRepr> for NestedSubId {
    fn from(value: &NestedInformationElementRepr) -> Self {
        match value {
            NestedInformationElementRepr::TschSynchronization(_) => {
                NestedSubId::Short(NestedSubIdShort::TschSynchronization)
            }
            NestedInformationElementRepr::TschTimeslot(_) => {
                NestedSubId::Short(NestedSubIdShort::TschTimeslot)
            }
            NestedInformationElementRepr::TschSlotframeAndLink(_) => {
                NestedSubId::Short(NestedSubIdShort::TschSlotframeAndLink)
            }
            NestedInformationElementRepr::ChannelHopping(_) => {
                NestedSubId::Long(NestedSubIdLong::ChannelHopping)
            }
        }
    }
}

/// A high-level representation of a TSCH Synchronization Nested Information
/// Element.
#[derive(Debug)]
#[cfg_attr(feature = "fuzz", derive(arbitrary::Arbitrary))]
pub struct TschSynchronizationRepr {
    /// The absolute slot number (ASN).
    pub absolute_slot_number: u64,
    /// The join metric.
    pub join_metric: u8,
}

impl TschSynchronizationRepr {
    /// Parse a TSCH Synchronization Information Element.
    pub fn parse(ie: &TschSynchronization<&[u8]>) -> Self {
        Self {
            absolute_slot_number: ie.absolute_slot_number(),
            join_metric: ie.join_metric(),
        }
    }

    /// The buffer length required to emit the TSCH Synchronization Information
    /// Element.
    pub const fn buffer_len(&self) -> usize {
        6
    }

    /// Emit the TSCH Synchronization Information Element into a buffer.
    pub fn emit(&self, ie: &mut TschSynchronization<&mut [u8]>) {
        ie.set_absolute_slot_number(self.absolute_slot_number);
        ie.set_join_metric(self.join_metric);
    }
}

/// A high-level representation of a TSCH Slotframe and Link Nested Information
/// Element.
#[derive(Debug)]
#[cfg_attr(feature = "fuzz", derive(arbitrary::Arbitrary))]
pub struct TschSlotframeAndLinkRepr {
    /// The number of slotframes.
    pub number_of_slot_frames: u8,
}

impl TschSlotframeAndLinkRepr {
    /// Parse a TSCH Slotframe and Link Information Element.
    pub fn parse(ie: &TschSlotframeAndLink<&[u8]>) -> Self {
        Self {
            number_of_slot_frames: ie.number_of_slot_frames(),
        }
    }

    /// The buffer length required to emit the TSCH Slotframe and Link
    /// Information Element.
    pub fn buffer_len(&self) -> usize {
        1
    }

    /// Emit the TSCH Slotframe and Link Information Element into a buffer.
    pub fn emit(&self, ie: &mut TschSlotframeAndLink<&mut [u8]>) {
        ie.set_number_of_slot_frames(self.number_of_slot_frames);
    }
}

/// A high-level representation of a TSCH Timeslot Nested Information Element.
#[derive(Debug)]
#[cfg_attr(feature = "fuzz", derive(arbitrary::Arbitrary))]
pub struct TschTimeslotRepr {
    /// The timeslot ID.
    pub id: u8,
}

impl TschTimeslotRepr {
    /// Parse a TSCH Timeslot Information Element.
    pub fn parse(ie: &TschTimeslot<&[u8]>) -> Self {
        Self { id: ie.id() }
    }

    /// The buffer length required to emit the TSCH Timeslot Information
    /// Element.
    pub fn buffer_len(&self) -> usize {
        // TODO: allow to set other time slots than the default one.
        1
    }

    /// Emit the TSCH Timeslot Information Element into a buffer.
    pub fn emit(&self, ie: &mut TschTimeslot<&mut [u8]>) {
        ie.set_time_slot_id(self.id);
    }
}

/// A high-level representation of a Channel Hopping Nested Information Element.
#[derive(Debug)]
#[cfg_attr(feature = "fuzz", derive(arbitrary::Arbitrary))]
pub struct ChannelHoppingRepr {
    /// The hopping sequence ID.
    pub hopping_sequence_id: u8,
}

impl ChannelHoppingRepr {
    /// Parse a Channel Hopping Information Element.
    pub fn parse(ie: &ChannelHopping<&[u8]>) -> Self {
        Self {
            hopping_sequence_id: ie.hopping_sequence_id(),
        }
    }

    /// The buffer length required to emit the Channel Hopping Information
    /// Element.
    pub fn buffer_len(&self) -> usize {
        1
    }

    /// Emit the Channel Hopping Information Element into a buffer.
    pub fn emit(&self, ie: &mut ChannelHopping<&mut [u8]>) {
        ie.set_hopping_sequence_id(self.hopping_sequence_id);
    }
}