1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//! # sensor-db
//!
//! Append-only sensor database for NOR flash on embedded devices.
//!
//! Designed for the RP2350 (Raspberry Pi Pico 2) and similar Cortex-M
//! microcontrollers with external or internal QSPI NOR flash.
//!
//! ## Architecture
//!
//! ```text
//! ┌───────────────────────────────────────────────┐
//! │ SensorLogger (clock injection) │
//! │ (generic over the ports — never over a log) │
//! ├───────────────────────────────────────────────┤
//! │ LogSink / LogSource (capability ports) │
//! │ ├── append · append_run · sync │
//! │ ├── latest · oldest · count · query_range │
//! │ └── typed API via the *Ext mixins │
//! ├───────────────────────────────────────────────┤
//! │ TieredDb (optional: removable media) │
//! │ ├── routes append() onboard ↔ card │
//! │ └── Spooler (copies onboard → card) │
//! ├───────────────────────────────────────────────┤
//! │ SensorDb / SensorDbAsync (high-level API) │
//! │ ├── append / sync / latest / query_range │
//! │ ├── compact / format / stats │
//! │ ├── mount (footers + rebuild index) │
//! │ └── read_from (replication stream) │
//! ├───────────────────────────────────────────────┤
//! │ Sector ring (circular buffer of sectors) │
//! │ ├── SectorHeader (28 B, SD05: magic+version │
//! │ │ +log_id+epoch+seq+erase_cnt+crc) │
//! │ ├── SectorFooter (52 B, SDF2, last page: │
//! │ │ the summary, written at seal time) │
//! │ └── SectorSummary (in-RAM, per-sector) │
//! ├───────────────────────────────────────────────┤
//! │ Entry log (sequential within pages) │
//! │ ├── EntryHeader + payload + CRC-32C │
//! │ ├── Two-phase commit (flags 0xFF→0x00) │
//! │ └── Page-buffered writes (one RAM page) │
//! ├───────────────────────────────────────────────┤
//! │ embedded-storage NorFlash trait │
//! │ embedded-storage-async (async feature) │
//! │ (RP2350 internal / external SPI / mock) │
//! └───────────────────────────────────────────────┘
//! ```
//!
//! ## Quick Start (Sync)
//!
//! ```ignore
//! use sensor_db::SensorDb;
//!
//! // RP2350: 256-byte pages, up to 512 sectors (2 MB region)
//! let mut db: SensorDb<_, 256, 512> = SensorDb::new(
//! flash, // embassy_rp::flash::Flash
//! 0x0020_0000, // base offset: 2 MB into flash
//! 512, // sector count
//! 2, // reserved sectors for headroom
//! )?;
//!
//! db.mount()?;
//!
//! // Write sensor data
//! db.append(0x01, timestamp_us, &temperature_bytes)?;
//!
//! // Query latest reading
//! let mut buf = [0u8; 64];
//! if let Some((header, n)) = db.latest(0x01, &mut buf)? {
//! let payload = &buf[..n];
//! // ...
//! }
//!
//! // Range query with callback
//! db.query_range(Some(0x01), t_start, t_end, |hdr, payload| {
//! // process each matching entry
//! true // return false to stop early
//! })?;
//! ```
//!
//! ## Quick Start (Async)
//!
//! Enable the `async` feature in `Cargo.toml`:
//!
//! ```ignore
//! use sensor_db::SensorDbAsync;
//!
//! let mut db: SensorDbAsync<_, 256, 512> = SensorDbAsync::new(
//! flash, 0x0020_0000, 512, 2,
//! )?;
//! db.mount().await?;
//! db.append(0x01, timestamp_us, &temperature_bytes).await?;
//! ```
// Re-export main public types at crate root for convenience
pub use ;
pub use EntryHeader;
pub use Error;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use SCHEMA_TAG_SIZE;
pub use ;
pub use ;
pub use SensorData;
/// Re-export of the `zerocopy` version this crate was built against.
///
/// `SensorData` is bounded on `zerocopy`'s traits, so `zerocopy` is a *public*
/// dependency: a payload type must derive the same `FromBytes`/`IntoBytes` this
/// crate names, from the same version. Importing them through here rather than
/// from your own dependency makes a version skew impossible to introduce
/// silently — the failure mode otherwise is `the trait bound
/// `Temperature: FromBytes` is not satisfied` pointing at a type that plainly
/// derives it.
///
/// ```ignore
/// use sensor_db::zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};
/// ```
///
/// `zerocopy` must still appear in your `Cargo.toml`, because its derives
/// expand to absolute `::zerocopy::` paths. Keep the version in step with this
/// re-export.
pub use zerocopy;
/// Derive [`SensorData`], binding a type to a sensor ID.
///
/// Requires the `derive` feature. See the macro's own documentation for the
/// `#[sensor(id = ..., version = ...)]` attribute.
pub use SensorData;
pub use SensorDbAsync;
pub use ;
/// Every public value type that reports state must format under `defmt`.
///
/// A generic function's body is type-checked whether or not it is ever called,
/// so this is a compile-time check that costs nothing at runtime and cannot be
/// skipped. It is written this way rather than as a `#[test]` because a test
/// *binary* linking `defmt` needs a global logger this crate does not provide —
/// which is the same reason `--all-features` must never reach a cargo command
/// that links one.
///
/// The rule it enforces: if a caller can hold it and would want it in a log
/// line, it formats. The feature previously covered [`Error`] and four wire
/// types but none of the statistics, which is the half an operator actually
/// reads.