|
| 1 | +use crate::wire::Ipv6Address; |
| 2 | + |
| 3 | +use super::{lollipop::SequenceCounter, rank::Rank}; |
| 4 | +use crate::config::RPL_PARENTS_BUFFER_COUNT; |
| 5 | + |
| 6 | +#[derive(Debug, Clone, Copy, PartialEq)] |
| 7 | +pub(crate) struct Parent { |
| 8 | + rank: Rank, |
| 9 | + address: Ipv6Address, |
| 10 | + preference: u8, |
| 11 | + version_number: SequenceCounter, |
| 12 | + dodag_id: Ipv6Address, |
| 13 | +} |
| 14 | + |
| 15 | +impl Parent { |
| 16 | + /// Create a new parent. |
| 17 | + pub(crate) fn new( |
| 18 | + address: Ipv6Address, |
| 19 | + preference: u8, |
| 20 | + rank: Rank, |
| 21 | + version_number: SequenceCounter, |
| 22 | + dodag_id: Ipv6Address, |
| 23 | + ) -> Self { |
| 24 | + Self { |
| 25 | + rank, |
| 26 | + address, |
| 27 | + preference, |
| 28 | + version_number, |
| 29 | + dodag_id, |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + /// Return the Rank of the parent. |
| 34 | + pub(crate) fn rank(&self) -> &Rank { |
| 35 | + &self.rank |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +#[derive(Debug, Default)] |
| 40 | +pub(crate) struct ParentSet { |
| 41 | + parents: heapless::Vec<Parent, { RPL_PARENTS_BUFFER_COUNT }>, |
| 42 | +} |
| 43 | + |
| 44 | +impl ParentSet { |
| 45 | + /// Add a new parent to the parent set. The Rank of the new parent should be lower than the |
| 46 | + /// Rank of the node that holds this parent set. |
| 47 | + pub(crate) fn add(&mut self, parent: Parent) { |
| 48 | + if let Some(p) = self.find_mut(parent.address) { |
| 49 | + // Update information |
| 50 | + *p = parent; |
| 51 | + } else if let Err(p) = self.parents.push(parent) { |
| 52 | + // Look for the worst parent |
| 53 | + if let Some(worst) = self.worst_parent() { |
| 54 | + if worst.rank().dag_rank() > parent.rank().dag_rank() { |
| 55 | + *worst = parent; |
| 56 | + } else { |
| 57 | + net_debug!("could not add parent"); |
| 58 | + } |
| 59 | + } else { |
| 60 | + // WARNING: there should be a worst parent, since the list of parents is not empty |
| 61 | + unreachable!(); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /// Find a parent based on its address. |
| 67 | + pub(crate) fn find(&self, address: Ipv6Address) -> Option<&Parent> { |
| 68 | + self.parents.iter().find(|p| p.address == address) |
| 69 | + } |
| 70 | + |
| 71 | + /// Find a mutable parent based on its address. |
| 72 | + pub(crate) fn find_mut(&mut self, address: Ipv6Address) -> Option<&mut Parent> { |
| 73 | + self.parents.iter_mut().find(|p| p.address == address) |
| 74 | + } |
| 75 | + |
| 76 | + /// Return a slice to the parent set. |
| 77 | + pub(crate) fn parents(&self) -> &[Parent] { |
| 78 | + &self.parents |
| 79 | + } |
| 80 | + |
| 81 | + /// Find the worst parent that is currently in the parent set. |
| 82 | + fn worst_parent(&mut self) -> Option<&mut Parent> { |
| 83 | + let mut worst: Option<&mut Parent> = None; |
| 84 | + |
| 85 | + for p in self.parents.iter_mut() { |
| 86 | + if worst.is_none() || worst.as_mut().unwrap().rank.dag_rank() < p.rank.dag_rank() { |
| 87 | + worst = Some(p); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + worst |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +#[cfg(test)] |
| 96 | +mod tests { |
| 97 | + use super::*; |
| 98 | + |
| 99 | + #[test] |
| 100 | + fn add_parent() { |
| 101 | + let mut set = ParentSet::default(); |
| 102 | + set.add(Parent::new( |
| 103 | + Default::default(), |
| 104 | + 0, |
| 105 | + Rank::ROOT, |
| 106 | + Default::default(), |
| 107 | + Default::default(), |
| 108 | + )); |
| 109 | + |
| 110 | + assert_eq!( |
| 111 | + set.find(Default::default()), |
| 112 | + Some(&Parent::new( |
| 113 | + Default::default(), |
| 114 | + 0, |
| 115 | + Rank::ROOT, |
| 116 | + Default::default(), |
| 117 | + Default::default() |
| 118 | + )) |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + #[test] |
| 123 | + fn add_more_parents() { |
| 124 | + use super::super::consts::DEFAULT_MIN_HOP_RANK_INCREASE; |
| 125 | + let mut set = ParentSet::default(); |
| 126 | + |
| 127 | + let mut last_address = Default::default(); |
| 128 | + for i in 0..RPL_PARENTS_BUFFER_COUNT { |
| 129 | + let i = i as u16; |
| 130 | + let mut address = Ipv6Address::default(); |
| 131 | + address.0[15] = i as u8; |
| 132 | + last_address = address; |
| 133 | + |
| 134 | + set.add(Parent::new( |
| 135 | + address, |
| 136 | + 0, |
| 137 | + Rank::new(256 * i, DEFAULT_MIN_HOP_RANK_INCREASE), |
| 138 | + Default::default(), |
| 139 | + address, |
| 140 | + )); |
| 141 | + |
| 142 | + assert_eq!( |
| 143 | + set.find(address), |
| 144 | + Some(&Parent::new( |
| 145 | + address, |
| 146 | + 0, |
| 147 | + Rank::new(256 * i, DEFAULT_MIN_HOP_RANK_INCREASE), |
| 148 | + Default::default(), |
| 149 | + address, |
| 150 | + )) |
| 151 | + ); |
| 152 | + } |
| 153 | + |
| 154 | + // This one is not added to the set, because its Rank is worse than any other parent in the |
| 155 | + // set. |
| 156 | + let mut address = Ipv6Address::default(); |
| 157 | + address.0[15] = 8; |
| 158 | + set.add(Parent::new( |
| 159 | + address, |
| 160 | + 0, |
| 161 | + Rank::new(256 * 8, DEFAULT_MIN_HOP_RANK_INCREASE), |
| 162 | + Default::default(), |
| 163 | + address, |
| 164 | + )); |
| 165 | + assert_eq!(set.find(address), None); |
| 166 | + |
| 167 | + /// This Parent has a better rank than the last one in the set. |
| 168 | + let mut address = Ipv6Address::default(); |
| 169 | + address.0[15] = 9; |
| 170 | + set.add(Parent::new( |
| 171 | + address, |
| 172 | + 0, |
| 173 | + Rank::new(0, DEFAULT_MIN_HOP_RANK_INCREASE), |
| 174 | + Default::default(), |
| 175 | + address, |
| 176 | + )); |
| 177 | + assert_eq!( |
| 178 | + set.find(address), |
| 179 | + Some(&Parent::new( |
| 180 | + address, |
| 181 | + 0, |
| 182 | + Rank::new(0, DEFAULT_MIN_HOP_RANK_INCREASE), |
| 183 | + Default::default(), |
| 184 | + address |
| 185 | + )) |
| 186 | + ); |
| 187 | + assert_eq!(set.find(last_address), None); |
| 188 | + } |
| 189 | +} |
0 commit comments