|
| 1 | +use anyhow::{bail, Context}; |
| 2 | +use esp_idf_svc::partition::{EspPartition, EspWlPartition}; |
| 3 | + |
| 4 | +#[derive(Debug, Clone)] |
| 5 | +pub struct SPIFlashConfig { |
| 6 | + pub partition_label: String, |
| 7 | + pub mount_path: String, |
| 8 | +} |
| 9 | + |
| 10 | +pub struct SPIFlashStorage { |
| 11 | + config: SPIFlashConfig, |
| 12 | + wl_partition: Option<EspWlPartition<EspPartition>>, |
| 13 | +} |
| 14 | + |
| 15 | +impl SPIFlashStorage { |
| 16 | + pub fn new(config: &SPIFlashConfig) -> Self { |
| 17 | + Self { |
| 18 | + config: config.clone(), |
| 19 | + wl_partition: None, |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + pub fn install(&mut self) -> anyhow::Result<()> { |
| 24 | + if self.wl_partition.is_some() { |
| 25 | + bail!("Already installed"); |
| 26 | + } |
| 27 | + let partition = Some( |
| 28 | + unsafe { EspPartition::new(&self.config.partition_label) }?.ok_or_else(|| { |
| 29 | + anyhow::anyhow!( |
| 30 | + "Failed to find partition with label {:#?}", |
| 31 | + self.config.partition_label |
| 32 | + ) |
| 33 | + })?, |
| 34 | + ); |
| 35 | + self.wl_partition = Some(EspWlPartition::new(partition.unwrap()).with_context(|| { |
| 36 | + format!( |
| 37 | + "Failed to mount partition {} at {}", |
| 38 | + self.config.partition_label, self.config.mount_path |
| 39 | + ) |
| 40 | + })?); |
| 41 | + log::info!( |
| 42 | + "Mount SPI Flash storage with label {} at {}", |
| 43 | + self.config.partition_label, |
| 44 | + self.config.mount_path |
| 45 | + ); |
| 46 | + Ok(()) |
| 47 | + } |
| 48 | +} |
0 commit comments