diff --git a/btmesh-driver/src/lib.rs b/btmesh-driver/src/lib.rs index 8fa1641..9b82eac 100644 --- a/btmesh-driver/src/lib.rs +++ b/btmesh-driver/src/lib.rs @@ -23,6 +23,7 @@ use btmesh_pdu::provisioned::Message; use btmesh_pdu::provisioning::generic::Reason; use btmesh_pdu::provisioning::{Capabilities, ProvisioningPDU}; use btmesh_pdu::PDU; +use storage::StorageError; use core::cell::RefCell; use core::future::{pending, Future}; use embassy_futures::select::{select, select3, select4, Either, Either3, Either4}; @@ -99,6 +100,14 @@ impl Driver Result { + match self.storage.load().await { + Ok(configuration) => Ok(Configuration::Provisioned(configuration)), + Err(StorageError::Serialization) => {Ok(Configuration::Unprovisioned(self.storage.default_config()))}, + Err(e) => Err(DriverError::Storage(e)) + } + } } pub struct InnerDriver<'s, N: NetworkInterfaces, R: RngCore + CryptoRng, B: BackingStore + 's> { diff --git a/btmesh-driver/src/storage/mod.rs b/btmesh-driver/src/storage/mod.rs index e5f0d4d..d592b55 100644 --- a/btmesh-driver/src/storage/mod.rs +++ b/btmesh-driver/src/storage/mod.rs @@ -85,23 +85,36 @@ impl Storage { } } - pub async fn init(&self) -> Result<(), StorageError> { - let mut locked_config = self.config.lock().await; - let mut backing_store = self.backing_store.borrow_mut(); - if let Ok(mut config) = backing_store.load().await { - let seq = config.sequence(); + pub async fn load(&self) -> Result { + let config = { + let mut backing_store = self.backing_store.borrow_mut(); + backing_store.load().await? + }; + Ok(config) + } - let mut extra = seq % 100; - if extra == 100 { - extra = 0; + pub async fn init(&self) -> Result<(), StorageError> { + let config = self.load().await; + match config { + Ok(mut config) => { + let seq = config.sequence(); + + let mut extra = seq % 100; + if extra == 100 { + extra = 0; + } + let seq = (seq - extra) + 100; + + *config.sequence_mut() = seq; + let mut backing_store = self.backing_store.borrow_mut(); + backing_store.store(&config).await?; + let mut locked_config = self.config.lock().await; + locked_config.replace(Configuration::Provisioned(config)); + } + Err(_) => { + let mut locked_config = self.config.lock().await; + locked_config.replace(Configuration::Unprovisioned(self.default_config.clone())); } - let seq = (seq - extra) + 100; - - *config.sequence_mut() = seq; - backing_store.store(&config).await?; - locked_config.replace(Configuration::Provisioned(config)); - } else { - locked_config.replace(Configuration::Unprovisioned(self.default_config.clone())); } Ok(()) } @@ -194,4 +207,8 @@ impl Storage { pub(crate) fn set_composition(&self, composition: Composition) { self.composition.borrow_mut().replace(composition); } + + pub fn default_config(&self) -> UnprovisionedConfiguration { + self.default_config + } } diff --git a/btmesh-driver/src/storage/unprovisioned.rs b/btmesh-driver/src/storage/unprovisioned.rs index e3fdd61..499e258 100644 --- a/btmesh-driver/src/storage/unprovisioned.rs +++ b/btmesh-driver/src/storage/unprovisioned.rs @@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize}; #[cfg_attr(feature = "defmt", derive(::defmt::Format))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -#[derive(Clone, Hash, Debug)] +#[derive(Clone, Hash, Debug, Copy)] pub struct UnprovisionedConfiguration { pub(crate) uuid: Uuid, } diff --git a/btmesh-nrf-softdevice/src/driver.rs b/btmesh-nrf-softdevice/src/driver.rs index 10b7013..81a4839 100644 --- a/btmesh-nrf-softdevice/src/driver.rs +++ b/btmesh-nrf-softdevice/src/driver.rs @@ -6,6 +6,7 @@ use btmesh_driver::interface::{ AdvertisingAndGattNetworkInterfaces, AdvertisingOnlyNetworkInterfaces, NetworkInterfaces, }; use btmesh_driver::storage::flash::FlashBackingStore; +use btmesh_driver::storage::Configuration; use btmesh_driver::{ BluetoothMeshDriver, BluetoothMeshDriverConfig, Driver as BaseDriver, DriverError, }; @@ -85,6 +86,10 @@ impl NrfSoftdeviceDriver { } } + pub async fn get_provisioning_info(&self) -> Result { + self.driver.get_provisioning_info().await + } + #[allow(unreachable_code)] pub async fn run<'r, D: BluetoothMeshDevice>( &'r mut self, @@ -128,6 +133,10 @@ impl NrfSoftdeviceAdvertisingOnlyDriver { )) } + pub async fn get_provisioning_info(&self) -> Result { + self.0.get_provisioning_info().await + } + pub fn softdevice(&self) -> &'static Softdevice { self.0.sd } @@ -190,6 +199,10 @@ impl NrfSoftdeviceAdvertisingAndGattDriver { config, )) } + + pub async fn get_provisioning_info(&self) -> Result { + self.0.get_provisioning_info().await + } } impl BluetoothMeshDriver for NrfSoftdeviceAdvertisingAndGattDriver {