File
Blob: firmware/crates/esp32-radio/src/platform/crypto_profile.rs
| 1 | //! Opt-in aggregate profiling; no packet or key material enters the counters. |
| 2 | use super::ffi; |
| 3 | use crate::error::{Result, check}; |
| 4 | |
| 5 | #[derive(Default)] |
| 6 | enum State { |
| 7 | #[default] |
| 8 | Waiting, |
| 9 | Warmup(u64), |
| 10 | Measuring(u64), |
| 11 | Complete, |
| 12 | } |
| 13 | |
| 14 | #[derive(Default)] |
| 15 | pub(crate) struct CryptoProfile(State); |
| 16 | |
| 17 | impl CryptoProfile { |
| 18 | /// Called only by the radio task while its connected playback loop runs. |
| 19 | pub(crate) fn tick(&mut self, now_us: u64) -> Result<()> { |
| 20 | match self.0 { |
| 21 | State::Waiting => self.0 = State::Warmup(now_us + 5_000_000), |
| 22 | State::Warmup(deadline) if now_us >= deadline => { |
| 23 | // SAFETY: the caller is the sole radio owner, outside a crypto |
| 24 | // operation. C records only this task's aggregate counters. |
| 25 | let status = unsafe { ffi::radio_crypto_profile_start() }; |
| 26 | check(status, "crypto profile start")?; |
| 27 | self.0 = State::Measuring(now_us + 30_000_000); |
| 28 | } |
| 29 | State::Measuring(deadline) if now_us >= deadline => { |
| 30 | // SAFETY: same owner, with all synchronous crypto calls finished. |
| 31 | // C disables counting before formatting its fixed-size summary. |
| 32 | let status = unsafe { ffi::radio_crypto_profile_finish() }; |
| 33 | check(status, "crypto profile finish")?; |
| 34 | self.0 = State::Complete; |
| 35 | } |
| 36 | _ => {} |
| 37 | } |
| 38 | Ok(()) |
| 39 | } |
| 40 | } |