zigbee_core/storage/mod.rs
1//! Persistent storage for the information bases.
2//!
3//! The NIB/AIB live in a RAM mirror; every setter marks its field dirty and
4//! signals a change. The application only chooses where the state lives:
5//! RAM-only via the plain `init()` functions, or NOR flash via
6//! [`init_with_flash`] plus a spawned task running [`FlashStorage::run`] that
7//! persists changes as they happen.
8//!
9//! Frame counters are persisted with headroom so a reboot can never reuse an
10//! outgoing counter value: outgoing counters are stored rounded up two
11//! `HEADROOM` boundaries ahead, incoming counters rounded down to a `WINDOW`
12//! boundary. After a reboot up to `WINDOW` already-seen incoming counter
13//! values may be accepted again — the cost of not writing flash on every
14//! received frame.
15//!
16//! The per-IB restore/flush logic lives with the respective information base
17//! (`nwk::nib::storage`, `aps::aib::storage`); this module only provides the
18//! flash map plumbing.
19
20/// Sink dirty information-base state is flushed into.
21pub trait StorageDriver {
22 /// Persists all information-base fields modified since the last call.
23 async fn flush(&self);
24
25 /// Persists information-base changes as they happen, until cancelled.
26 ///
27 /// Drivers that write in the background implement this; the default never
28 /// completes, so a caller can drive any driver the same way.
29 async fn run(&self) {
30 core::future::pending::<()>().await;
31 }
32}
33
34/// Lets a driver be shared: the stack can hold a reference to one the
35/// application also flushes itself.
36impl<T: StorageDriver> StorageDriver for &T {
37 async fn flush(&self) {
38 (**self).flush().await;
39 }
40
41 async fn run(&self) {
42 (**self).run().await;
43 }
44}
45
46/// RAM-only operation: state is lost on reset.
47pub struct NoStorage;
48
49impl StorageDriver for NoStorage {
50 async fn flush(&self) {}
51}
52
53#[cfg(feature = "storage")]
54pub(crate) mod flash;
55
56#[cfg(feature = "storage")]
57pub(crate) use flash::FlashMap;
58#[cfg(feature = "storage")]
59pub use flash::FlashStorage;
60#[cfg(feature = "storage")]
61pub(crate) use flash::Shadow;
62#[cfg(feature = "storage")]
63pub use flash::init_with_flash;
64#[cfg(feature = "storage")]
65pub(crate) use flash::round_down;
66#[cfg(feature = "storage")]
67pub(crate) use flash::round_up;