Skip to main content

openthread/ot/
history_tracker.rs

1// Copyright 2026 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::prelude_internal::*;
6
7/// Represents an iterator to iterate the net info history.
8#[allow(missing_debug_implementations)]
9pub struct HistoryTrackerNetInfoIterator<'a, T: ?Sized> {
10    ot_instance: &'a T,
11    ot_iter: otHistoryTrackerIterator,
12}
13
14impl<T: ?Sized + HistoryTracker> Iterator for HistoryTrackerNetInfoIterator<'_, T> {
15    type Item = (HistoryTrackerNetworkInfo, u32);
16    fn next(&mut self) -> Option<Self::Item> {
17        let mut entry_age = 0;
18        self.ot_instance
19            .iter_next_net_info_history(&mut self.ot_iter, &mut entry_age)
20            .map(|info| (info, entry_age))
21    }
22}
23
24/// Represents an iterator to iterate the neighbor history.
25#[allow(missing_debug_implementations)]
26pub struct HistoryTrackerNeighborIterator<'a, T: ?Sized> {
27    ot_instance: &'a T,
28    ot_iter: otHistoryTrackerIterator,
29}
30
31impl<T: ?Sized + HistoryTracker> Iterator for HistoryTrackerNeighborIterator<'_, T> {
32    type Item = (HistoryTrackerNeighborInfo, u32);
33    fn next(&mut self) -> Option<Self::Item> {
34        let mut entry_age = 0;
35        self.ot_instance
36            .iter_next_neighbor_history(&mut self.ot_iter, &mut entry_age)
37            .map(|info| (info, entry_age))
38    }
39}
40
41/// Represents an iterator to iterate the router info history.
42#[allow(missing_debug_implementations)]
43pub struct HistoryTrackerRouterIterator<'a, T: ?Sized> {
44    ot_instance: &'a T,
45    ot_iter: otHistoryTrackerIterator,
46}
47
48impl<T: ?Sized + HistoryTracker> Iterator for HistoryTrackerRouterIterator<'_, T> {
49    type Item = (HistoryTrackerRouterInfo, u32);
50    fn next(&mut self) -> Option<Self::Item> {
51        let mut entry_age = 0;
52        self.ot_instance
53            .iter_next_router_history(&mut self.ot_iter, &mut entry_age)
54            .map(|info| (info, entry_age))
55    }
56}
57
58/// Represents an iterator to iterate the NetData on-mesh prefix info history.
59#[allow(missing_debug_implementations)]
60pub struct HistoryTrackerOnMeshPrefixIterator<'a, T: ?Sized> {
61    ot_instance: &'a T,
62    ot_iter: otHistoryTrackerIterator,
63}
64
65impl<T: ?Sized + HistoryTracker> Iterator for HistoryTrackerOnMeshPrefixIterator<'_, T> {
66    type Item = (HistoryTrackerOnMeshPrefixInfo, u32);
67    fn next(&mut self) -> Option<Self::Item> {
68        let mut entry_age = 0;
69        self.ot_instance
70            .iter_next_on_mesh_prefix_history(&mut self.ot_iter, &mut entry_age)
71            .map(|info| (info, entry_age))
72    }
73}
74
75/// Represents an iterator to iterate the NetData external route info history.
76#[allow(missing_debug_implementations)]
77pub struct HistoryTrackerExternalRouteIterator<'a, T: ?Sized> {
78    ot_instance: &'a T,
79    ot_iter: otHistoryTrackerIterator,
80}
81
82impl<T: ?Sized + HistoryTracker> Iterator for HistoryTrackerExternalRouteIterator<'_, T> {
83    type Item = (HistoryTrackerExternalRouteInfo, u32);
84    fn next(&mut self) -> Option<Self::Item> {
85        let mut entry_age = 0;
86        self.ot_instance
87            .iter_next_external_route_history(&mut self.ot_iter, &mut entry_age)
88            .map(|info| (info, entry_age))
89    }
90}
91
92/// Methods from the [OpenThread "history-tracker" Module][1].
93///
94/// [1]: https://openthread.io/reference/group/api-history-tracker
95pub trait HistoryTracker {
96    /// Functional equivalent of [`otsys::otHistoryTrackerInitIterator`]
97    /// (crate::otsys::otHistoryTrackerInitIterator).
98    fn history_tracker_init_iterator(&self, iter: &mut otHistoryTrackerIterator);
99
100    /// Get the history tracker net info history iterator instance.
101    fn history_tracker_net_info_history_get_iterator(
102        &self,
103    ) -> HistoryTrackerNetInfoIterator<'_, Self> {
104        let mut ot_iter = otHistoryTrackerIterator::default();
105        self.history_tracker_init_iterator(&mut ot_iter);
106        HistoryTrackerNetInfoIterator { ot_instance: self, ot_iter }
107    }
108
109    /// Functional equivalent of
110    /// [`otsys::otHistoryTrackerIterateNetInfoHistory`]
111    /// (crate::otsys::otHistoryTrackerIterateNetInfoHistory).
112    fn iter_next_net_info_history(
113        &self,
114        ot_iter: &mut otHistoryTrackerIterator,
115        entry_age: &mut u32,
116    ) -> Option<HistoryTrackerNetworkInfo>;
117
118    /// Get the history tracker neighbor info history iterator instance.
119    fn history_tracker_neighbor_history_get_iterator(
120        &self,
121    ) -> HistoryTrackerNeighborIterator<'_, Self> {
122        let mut ot_iter = otHistoryTrackerIterator::default();
123        self.history_tracker_init_iterator(&mut ot_iter);
124        HistoryTrackerNeighborIterator { ot_instance: self, ot_iter }
125    }
126
127    /// Functional equivalent of
128    /// [`otsys::otHistoryTrackerIterateNeighborHistory`]
129    /// (crate::otsys::otHistoryTrackerIterateNeighborHistory).
130    fn iter_next_neighbor_history(
131        &self,
132        ot_iter: &mut otHistoryTrackerIterator,
133        entry_age: &mut u32,
134    ) -> Option<HistoryTrackerNeighborInfo>;
135
136    /// Get the history tracker router info history iterator instance.
137    fn history_tracker_router_history_get_iterator(
138        &self,
139    ) -> HistoryTrackerRouterIterator<'_, Self> {
140        let mut ot_iter = otHistoryTrackerIterator::default();
141        self.history_tracker_init_iterator(&mut ot_iter);
142        HistoryTrackerRouterIterator { ot_instance: self, ot_iter }
143    }
144
145    /// Functional equivalent of
146    /// [`otsys::otHistoryTrackerIterateRouterHistory`]
147    /// (crate::otsys::otHistoryTrackerIterateRouterHistory).
148    fn iter_next_router_history(
149        &self,
150        ot_iter: &mut otHistoryTrackerIterator,
151        entry_age: &mut u32,
152    ) -> Option<HistoryTrackerRouterInfo>;
153
154    /// Get the history tracker netdata on-mesh prefix info history iterator instance.
155    fn history_tracker_on_mesh_prefix_history_get_iterator(
156        &self,
157    ) -> HistoryTrackerOnMeshPrefixIterator<'_, Self> {
158        let mut ot_iter = otHistoryTrackerIterator::default();
159        self.history_tracker_init_iterator(&mut ot_iter);
160        HistoryTrackerOnMeshPrefixIterator { ot_instance: self, ot_iter }
161    }
162
163    /// Functional equivalent of
164    /// [`otsys::otHistoryTrackerIterateOnMeshPrefixHistory`]
165    /// (crate::otsys::otHistoryTrackerIterateOnMeshPrefixHistory).
166    fn iter_next_on_mesh_prefix_history(
167        &self,
168        ot_iter: &mut otHistoryTrackerIterator,
169        entry_age: &mut u32,
170    ) -> Option<HistoryTrackerOnMeshPrefixInfo>;
171
172    /// Get the history tracker netdata external route info history iterator instance.
173    fn history_tracker_external_route_history_get_iterator(
174        &self,
175    ) -> HistoryTrackerExternalRouteIterator<'_, Self> {
176        let mut ot_iter = otHistoryTrackerIterator::default();
177        self.history_tracker_init_iterator(&mut ot_iter);
178        HistoryTrackerExternalRouteIterator { ot_instance: self, ot_iter }
179    }
180
181    /// Functional equivalent of
182    /// [`otsys::otHistoryTrackerIterateExternalRouteHistory`]
183    /// (crate::otsys::otHistoryTrackerIterateExternalRouteHistory).
184    fn iter_next_external_route_history(
185        &self,
186        ot_iter: &mut otHistoryTrackerIterator,
187        entry_age: &mut u32,
188    ) -> Option<HistoryTrackerExternalRouteInfo>;
189}
190
191impl<T: HistoryTracker + Boxable> HistoryTracker for ot::Box<T> {
192    fn history_tracker_init_iterator(&self, iter: &mut otHistoryTrackerIterator) {
193        self.as_ref().history_tracker_init_iterator(iter)
194    }
195
196    fn iter_next_net_info_history(
197        &self,
198        ot_iter: &mut otHistoryTrackerIterator,
199        entry_age: &mut u32,
200    ) -> Option<HistoryTrackerNetworkInfo> {
201        self.as_ref().iter_next_net_info_history(ot_iter, entry_age)
202    }
203
204    fn iter_next_neighbor_history(
205        &self,
206        ot_iter: &mut otHistoryTrackerIterator,
207        entry_age: &mut u32,
208    ) -> Option<HistoryTrackerNeighborInfo> {
209        self.as_ref().iter_next_neighbor_history(ot_iter, entry_age)
210    }
211
212    fn iter_next_router_history(
213        &self,
214        ot_iter: &mut otHistoryTrackerIterator,
215        entry_age: &mut u32,
216    ) -> Option<HistoryTrackerRouterInfo> {
217        self.as_ref().iter_next_router_history(ot_iter, entry_age)
218    }
219
220    fn iter_next_on_mesh_prefix_history(
221        &self,
222        ot_iter: &mut otHistoryTrackerIterator,
223        entry_age: &mut u32,
224    ) -> Option<HistoryTrackerOnMeshPrefixInfo> {
225        self.as_ref().iter_next_on_mesh_prefix_history(ot_iter, entry_age)
226    }
227
228    fn iter_next_external_route_history(
229        &self,
230        ot_iter: &mut otHistoryTrackerIterator,
231        entry_age: &mut u32,
232    ) -> Option<HistoryTrackerExternalRouteInfo> {
233        self.as_ref().iter_next_external_route_history(ot_iter, entry_age)
234    }
235}
236
237impl HistoryTracker for Instance {
238    fn history_tracker_init_iterator(&self, iter: &mut otHistoryTrackerIterator) {
239        unsafe { otHistoryTrackerInitIterator(iter) }
240    }
241
242    fn iter_next_net_info_history(
243        &self,
244        ot_iter: &mut otHistoryTrackerIterator,
245        entry_age: &mut u32,
246    ) -> Option<HistoryTrackerNetworkInfo> {
247        unsafe {
248            let info_ptr = otHistoryTrackerIterateNetInfoHistory(
249                self.as_ot_ptr(),
250                ot_iter as *mut otHistoryTrackerIterator,
251                entry_age as *mut u32,
252            );
253
254            info_ptr.as_ref().map(|raw| HistoryTrackerNetworkInfo(*raw))
255        }
256    }
257
258    fn iter_next_neighbor_history(
259        &self,
260        ot_iter: &mut otHistoryTrackerIterator,
261        entry_age: &mut u32,
262    ) -> Option<HistoryTrackerNeighborInfo> {
263        unsafe {
264            let info_ptr = otHistoryTrackerIterateNeighborHistory(
265                self.as_ot_ptr(),
266                ot_iter as *mut otHistoryTrackerIterator,
267                entry_age as *mut u32,
268            );
269
270            info_ptr.as_ref().map(|raw| HistoryTrackerNeighborInfo(*raw))
271        }
272    }
273
274    fn iter_next_router_history(
275        &self,
276        ot_iter: &mut otHistoryTrackerIterator,
277        entry_age: &mut u32,
278    ) -> Option<HistoryTrackerRouterInfo> {
279        unsafe {
280            let info_ptr = otHistoryTrackerIterateRouterHistory(
281                self.as_ot_ptr(),
282                ot_iter as *mut otHistoryTrackerIterator,
283                entry_age as *mut u32,
284            );
285
286            info_ptr.as_ref().map(|raw| HistoryTrackerRouterInfo(*raw))
287        }
288    }
289
290    fn iter_next_on_mesh_prefix_history(
291        &self,
292        ot_iter: &mut otHistoryTrackerIterator,
293        entry_age: &mut u32,
294    ) -> Option<HistoryTrackerOnMeshPrefixInfo> {
295        unsafe {
296            let info_ptr = otHistoryTrackerIterateOnMeshPrefixHistory(
297                self.as_ot_ptr(),
298                ot_iter as *mut otHistoryTrackerIterator,
299                entry_age as *mut u32,
300            );
301
302            info_ptr.as_ref().map(|raw| HistoryTrackerOnMeshPrefixInfo(*raw))
303        }
304    }
305
306    fn iter_next_external_route_history(
307        &self,
308        ot_iter: &mut otHistoryTrackerIterator,
309        entry_age: &mut u32,
310    ) -> Option<HistoryTrackerExternalRouteInfo> {
311        unsafe {
312            let info_ptr = otHistoryTrackerIterateExternalRouteHistory(
313                self.as_ot_ptr(),
314                ot_iter as *mut otHistoryTrackerIterator,
315                entry_age as *mut u32,
316            );
317
318            info_ptr.as_ref().map(|raw| HistoryTrackerExternalRouteInfo(*raw))
319        }
320    }
321}