Skip to main content

openthread/ot/types/
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
7use num_derive::FromPrimitive;
8
9/// This structure represents a Thread network info in the history tracker report.
10///
11/// Functional equivalent of [`otsys::otHistoryTrackerNetworkInfo`](crate::otsys::otHistoryTrackerNetworkInfo).
12#[derive(Debug, Default, Clone)]
13#[repr(transparent)]
14pub struct HistoryTrackerNetworkInfo(pub otHistoryTrackerNetworkInfo);
15
16impl_ot_castable!(HistoryTrackerNetworkInfo, otHistoryTrackerNetworkInfo);
17
18impl HistoryTrackerNetworkInfo {
19    /// Returns the device Role.
20    pub fn role(&self) -> DeviceRole {
21        DeviceRole::from(self.0.mRole)
22    }
23
24    /// Returns the device's MLE link mode.
25    pub fn mode(&self) -> LinkModeConfig {
26        LinkModeConfig::from(self.0.mMode)
27    }
28
29    /// Returns the device's RLOC16.
30    pub fn rloc16(&self) -> u16 {
31        self.0.mRloc16
32    }
33
34    /// Returns the Thread network partition ID.
35    pub fn partition_id(&self) -> u32 {
36        self.0.mPartitionId
37    }
38}
39
40/// Defines the events in a neighbor info (i.e. whether neighbor is added, removed, or changed).
41///
42/// Functional equivalent of [`otsys::otHistoryTrackerNeighborEvent`](crate::otsys::otHistoryTrackerNeighborEvent).
43#[derive(Debug, Copy, Clone, Eq, Ord, PartialOrd, PartialEq, FromPrimitive)]
44#[allow(missing_docs)]
45pub enum HistoryTrackerNeighborEvent {
46    /// Functional equivalent of [`otsys::OT_HISTORY_TRACKER_NEIGHBOR_EVENT_ADDED`](crate::otsys::OT_HISTORY_TRACKER_NEIGHBOR_EVENT_ADDED).
47    Added = OT_HISTORY_TRACKER_NEIGHBOR_EVENT_ADDED as isize,
48
49    /// Functional equivalent of [`otsys::OT_HISTORY_TRACKER_NEIGHBOR_EVENT_REMOVED`](crate::otsys::OT_HISTORY_TRACKER_NEIGHBOR_EVENT_REMOVED).
50    Removed = OT_HISTORY_TRACKER_NEIGHBOR_EVENT_REMOVED as isize,
51
52    /// Functional equivalent of [`otsys::OT_HISTORY_TRACKER_NEIGHBOR_EVENT_CHANGED`](crate::otsys::OT_HISTORY_TRACKER_NEIGHBOR_EVENT_CHANGED).
53    Changed = OT_HISTORY_TRACKER_NEIGHBOR_EVENT_CHANGED as isize,
54
55    /// Functional equivalent of [`otsys::OT_HISTORY_TRACKER_NEIGHBOR_EVENT_RESTORING`](crate::otsys::OT_HISTORY_TRACKER_NEIGHBOR_EVENT_RESTORING).
56    Restoring = OT_HISTORY_TRACKER_NEIGHBOR_EVENT_RESTORING as isize,
57}
58
59impl From<otHistoryTrackerNeighborEvent> for HistoryTrackerNeighborEvent {
60    fn from(x: otHistoryTrackerNeighborEvent) -> Self {
61        use num::FromPrimitive;
62        Self::from_u32(x)
63            .unwrap_or_else(|| panic!("Unknown otHistoryTrackerNeighborEvent value: {x}"))
64    }
65}
66
67/// This structure represents a Thread neighbor info in the history tracker report.
68///
69/// Functional equivalent of [`otsys::otHistoryTrackerNeighborInfo`](crate::otsys::otHistoryTrackerNeighborInfo).
70#[derive(Debug, Default, Clone)]
71#[repr(transparent)]
72pub struct HistoryTrackerNeighborInfo(pub otHistoryTrackerNeighborInfo);
73
74impl_ot_castable!(HistoryTrackerNeighborInfo, otHistoryTrackerNeighborInfo);
75
76impl HistoryTrackerNeighborInfo {
77    /// Returns Neighbor's Extended Address.
78    pub fn ext_address(&self) -> ExtAddress {
79        ExtAddress(self.0.mExtAddress)
80    }
81
82    /// Returns the Neighbor's RLOC16.
83    pub fn rloc16(&self) -> u16 {
84        self.0.mRloc16
85    }
86
87    /// Returns the Average RSSI of rx frames from neighbor at the time of recording entry.
88    pub fn avg_rssi(&self) -> i8 {
89        self.0.mAverageRssi
90    }
91
92    /// Returns the neighbor event (`OT_HISTORY_TRACKER_NEIGHBOR_EVENT_*` enumeration).
93    pub fn event(&self) -> HistoryTrackerNeighborEvent {
94        HistoryTrackerNeighborEvent::from(self.0.mEvent() as u32)
95    }
96
97    /// Returns true if the neighbor has its receiver on when not transmitting.
98    pub fn rx_on_while_idle(&self) -> bool {
99        self.0.mRxOnWhenIdle()
100    }
101
102    /// Returns true if the neighbor is a Full Thread Device.
103    pub fn full_thread_device(&self) -> bool {
104        self.0.mFullThreadDevice()
105    }
106
107    /// Returns true if the neighbor requires the full Network Data.
108    pub fn full_network_data(&self) -> bool {
109        self.0.mFullNetworkData()
110    }
111
112    /// Returns true if the neighbor is a child, otherwise, is a router.
113    pub fn is_child(&self) -> bool {
114        self.0.mIsChild()
115    }
116}
117
118/// Defines the events in a router info (i.e. whether router is added, removed, or changed).
119///
120/// Functional equivalent of [`otsys::otHistoryTrackerRouterEvent`](crate::otsys::otHistoryTrackerRouterEvent).
121#[derive(Debug, Copy, Clone, Eq, Ord, PartialOrd, PartialEq, FromPrimitive)]
122#[allow(missing_docs)]
123pub enum HistoryTrackerRouterEvent {
124    /// Functional equivalent of [`otsys::OT_HISTORY_TRACKER_ROUTER_EVENT_ADDED`](crate::otsys::OT_HISTORY_TRACKER_ROUTER_EVENT_ADDED).
125    Added = OT_HISTORY_TRACKER_ROUTER_EVENT_ADDED as isize,
126
127    /// Functional equivalent of [`otsys::OT_HISTORY_TRACKER_ROUTER_EVENT_REMOVED`](crate::otsys::OT_HISTORY_TRACKER_ROUTER_EVENT_REMOVED).
128    Removed = OT_HISTORY_TRACKER_ROUTER_EVENT_REMOVED as isize,
129
130    /// Functional equivalent of [`otsys::OT_HISTORY_TRACKER_ROUTER_EVENT_NEXT_HOP_CHANGED`](crate::otsys::OT_HISTORY_TRACKER_ROUTER_EVENT_NEXT_HOP_CHANGED).
131    NextHopChanged = OT_HISTORY_TRACKER_ROUTER_EVENT_NEXT_HOP_CHANGED as isize,
132
133    /// Functional equivalent of [`otsys::OT_HISTORY_TRACKER_ROUTER_EVENT_COST_CHANGED`](crate::otsys::OT_HISTORY_TRACKER_ROUTER_EVENT_COST_CHANGED).
134    PathCostChanged = OT_HISTORY_TRACKER_ROUTER_EVENT_COST_CHANGED as isize,
135}
136
137impl From<otHistoryTrackerRouterEvent> for HistoryTrackerRouterEvent {
138    fn from(x: otHistoryTrackerRouterEvent) -> Self {
139        use num::FromPrimitive;
140        Self::from_u32(x)
141            .unwrap_or_else(|| panic!("Unknown otHistoryTrackerRouterEvent value: {x}"))
142    }
143}
144
145/// This structure represents a router table entry event in the history tracker report.
146///
147/// Functional equivalent of [`otsys::otHistoryTrackerRouterInfo`](crate::otsys::otHistoryTrackerRouterInfo).
148#[derive(Debug, Default, Clone)]
149#[repr(transparent)]
150pub struct HistoryTrackerRouterInfo(pub otHistoryTrackerRouterInfo);
151
152impl_ot_castable!(HistoryTrackerRouterInfo, otHistoryTrackerRouterInfo);
153
154impl HistoryTrackerRouterInfo {
155    /// Returns a router table entry event (`OT_HISTORY_TRACKER_ROUTER_EVENT_*` enumeration).
156    pub fn event(&self) -> HistoryTrackerRouterEvent {
157        HistoryTrackerRouterEvent::from(self.0.mEvent() as u32)
158    }
159
160    /// Returns the Rotuer ID.
161    pub fn router_id(&self) -> u8 {
162        self.0.mRouterId()
163    }
164
165    /// Returns the next hop Router ID.
166    pub fn next_hop(&self) -> u8 {
167        self.0.mNextHop
168    }
169
170    /// Returns the old path cost (`OT_HISTORY_TRACKER_INFINITE_PATH_COST` if infinite or unknown).
171    pub fn old_path_cost(&self) -> u8 {
172        self.0.mOldPathCost()
173    }
174
175    /// Returns the new path cost (`OT_HISTORY_TRACKER_INFINITE_PATH_COST` if infinite or unknown).
176    pub fn path_cost(&self) -> u8 {
177        self.0.mPathCost()
178    }
179}
180
181/// Defines the events for a Network Data entry (i.e., whether an entry is added or removed).
182///
183/// Functional equivalent of [`otsys::otHistoryTrackerNetDataEvent`](crate::otsys::otHistoryTrackerNetDataEvent).
184#[derive(Debug, Copy, Clone, Eq, Ord, PartialOrd, PartialEq, FromPrimitive)]
185#[allow(missing_docs)]
186pub enum HistoryTrackerNetDataEvent {
187    /// Functional equivalent of [`otsys::OT_HISTORY_TRACKER_NET_DATA_ENTRY_ADDED`](crate::otsys::OT_HISTORY_TRACKER_NET_DATA_ENTRY_ADDED).
188    Added = OT_HISTORY_TRACKER_ROUTER_EVENT_ADDED as isize,
189
190    /// Functional equivalent of [`otsys::OT_HISTORY_TRACKER_NET_DATA_ENTRY_REMOVED`](crate::otsys::OT_HISTORY_TRACKER_NET_DATA_ENTRY_REMOVED).
191    Removed = OT_HISTORY_TRACKER_NET_DATA_ENTRY_REMOVED as isize,
192}
193
194impl From<otHistoryTrackerNetDataEvent> for HistoryTrackerNetDataEvent {
195    fn from(x: otHistoryTrackerNetDataEvent) -> Self {
196        use num::FromPrimitive;
197        Self::from_u32(x)
198            .unwrap_or_else(|| panic!("Unknown otHistoryTrackerNetDataEvent value: {x}"))
199    }
200}
201
202/// This structure represents a NetData on-mesh prefix info in the history tracker report.
203///
204/// Functional equivalent of [`otsys::otHistoryTrackerOnMeshPrefixInfo`](crate::otsys::otHistoryTrackerOnMeshPrefixInfo).
205#[derive(Default, Clone)]
206#[repr(transparent)]
207pub struct HistoryTrackerOnMeshPrefixInfo(pub otHistoryTrackerOnMeshPrefixInfo);
208
209impl_ot_castable!(HistoryTrackerOnMeshPrefixInfo, otHistoryTrackerOnMeshPrefixInfo);
210
211impl HistoryTrackerOnMeshPrefixInfo {
212    /// Returns the on-mesh prefix entry.
213    pub fn prefix(&self) -> BorderRouterConfig {
214        BorderRouterConfig(self.0.mPrefix)
215    }
216
217    /// Returns the NetData on-mesh prefix event (added/removed).
218    pub fn event(&self) -> HistoryTrackerNetDataEvent {
219        HistoryTrackerNetDataEvent::from(self.0.mEvent)
220    }
221}
222
223impl std::fmt::Debug for HistoryTrackerOnMeshPrefixInfo {
224    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
225        let mut ds = f.debug_struct("HistoryTrackerOnMeshPrefixInfo");
226        ds.field("on-mesh prefix", &self.prefix());
227        ds.field("event", &self.event());
228        ds.finish()
229    }
230}
231
232/// This structure represents a NetData external route info in the history tracker report.
233///
234/// Functional equivalent of [`otsys::otHistoryTrackerExternalRouteInfo`](crate::otsys::otHistoryTrackerExternalRouteInfo).
235#[derive(Default, Clone)]
236#[repr(transparent)]
237pub struct HistoryTrackerExternalRouteInfo(pub otHistoryTrackerExternalRouteInfo);
238
239impl_ot_castable!(HistoryTrackerExternalRouteInfo, otHistoryTrackerExternalRouteInfo);
240
241impl HistoryTrackerExternalRouteInfo {
242    /// Returns the external route entry.
243    pub fn route(&self) -> ExternalRouteConfig {
244        ExternalRouteConfig(self.0.mRoute)
245    }
246
247    /// Returns the NetData external route event (added/removed).
248    pub fn event(&self) -> HistoryTrackerNetDataEvent {
249        HistoryTrackerNetDataEvent::from(self.0.mEvent)
250    }
251}
252
253impl std::fmt::Debug for HistoryTrackerExternalRouteInfo {
254    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
255        let mut ds = f.debug_struct("HistoryTrackerExternalRouteInfo");
256        ds.field("external route", &self.route());
257        ds.field("event", &self.event());
258        ds.finish()
259    }
260}