Skip to main content

openthread/ot/types/
cache_entry_info.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::*;
6use std::fmt::{Debug, Formatter};
7
8/// Represents an EID cache entry.
9#[derive(Default, Clone, Copy)]
10#[repr(transparent)]
11pub struct CacheEntryInfo(pub otCacheEntryInfo);
12
13impl_ot_castable!(CacheEntryInfo, otCacheEntryInfo);
14
15/// Defines the EID cache entry state.
16///
17/// Functional equivalent of [`otsys::otCacheEntryState`](crate::otsys::otCacheEntryState).
18#[derive(Debug, Copy, Clone, Eq, Ord, PartialOrd, PartialEq, num_derive::FromPrimitive)]
19#[allow(missing_docs)]
20pub enum CacheEntryState {
21    Cached = OT_CACHE_ENTRY_STATE_CACHED as isize,
22    Snooped = OT_CACHE_ENTRY_STATE_SNOOPED as isize,
23    Query = OT_CACHE_ENTRY_STATE_QUERY as isize,
24    Retry = OT_CACHE_ENTRY_STATE_RETRY_QUERY as isize,
25    Unknown,
26}
27
28impl From<otCacheEntryState> for CacheEntryState {
29    fn from(x: otCacheEntryState) -> Self {
30        use num::FromPrimitive;
31        Self::from_u32(x).unwrap_or_else(|| {
32            warn!("Unknown otCacheEntryState value: {x}. Falling back to default state.");
33            CacheEntryState::Unknown
34        })
35    }
36}
37
38impl Debug for CacheEntryInfo {
39    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
40        f.debug_struct("CacheEntryInfo")
41            .field("target", &self.target())
42            .field("rloc16", &self.rloc16())
43            .field("state", &self.state())
44            .field("can_evict", &self.can_evict())
45            .field("ramp_down", &self.ramp_down())
46            .field("valid_last_trans", &self.valid_last_trans())
47            .field("last_trans_time", &self.last_trans_time())
48            .field("mesh_local_eid", &self.mesh_local_eid())
49            .field("timeout", &self.timeout())
50            .field("retry_delay", &self.retry_delay())
51            .finish()
52    }
53}
54
55impl CacheEntryInfo {
56    /// Returns the target EID.
57    pub fn target(&self) -> &Ip6Address {
58        Ip6Address::ref_from_ot_ref(&self.0.mTarget)
59    }
60
61    /// Returns the RLOC16.
62    pub fn rloc16(&self) -> ShortAddress {
63        self.0.mRloc16
64    }
65
66    /// Returns the Entry state.
67    pub fn state(&self) -> CacheEntryState {
68        CacheEntryState::from(self.0.mState)
69    }
70
71    /// Indicates whether the entry can be evicted.
72    pub fn can_evict(&self) -> bool {
73        self.0.mCanEvict()
74    }
75
76    /// Indicates whether in ramp-down mode while in `OT_CACHE_ENTRY_STATE_RETRY_QUERY`.
77    pub fn ramp_down(&self) -> bool {
78        self.0.mRampDown()
79    }
80
81    /// Indicates whether last transaction time and ML-EID are valid.
82    pub fn valid_last_trans(&self) -> bool {
83        self.0.mValidLastTrans()
84    }
85
86    /// Returns the last transaction time (applicable in cached state).
87    pub fn last_trans_time(&self) -> u32 {
88        self.0.mLastTransTime
89    }
90
91    /// Returns the Mesh Local EID (applicable if entry in cached state).
92    pub fn mesh_local_eid(&self) -> &Ip6Address {
93        Ip6Address::ref_from_ot_ref(&self.0.mMeshLocalEid)
94    }
95
96    /// Returns the timeout in seconds (applicable if in snooped/query/retry-query states).
97    pub fn timeout(&self) -> u16 {
98        self.0.mTimeout
99    }
100
101    /// Returns retry delay in seconds (applicable if in query-retry state).
102    pub fn retry_delay(&self) -> u16 {
103        self.0.mRetryDelay
104    }
105}