diff --git a/btmesh-common/src/lib.rs b/btmesh-common/src/lib.rs index 57aedc8..fc6c972 100644 --- a/btmesh-common/src/lib.rs +++ b/btmesh-common/src/lib.rs @@ -368,6 +368,11 @@ impl Uuid { .as_bytes(), ) } + + // Get slice + pub fn as_slice(&self) -> &[u8] { + &self.0 + } } impl Deref for Uuid { diff --git a/btmesh-device/Cargo.toml b/btmesh-device/Cargo.toml index 191512f..fbcb0bd 100644 --- a/btmesh-device/Cargo.toml +++ b/btmesh-device/Cargo.toml @@ -19,7 +19,7 @@ log = { version = "0.4", optional = true } defmt = { version = "0.3", optional = true } [features] -defmt = ["dep:defmt", "heapless/defmt-03"] +defmt = ["dep:defmt", "heapless/defmt-03", "embassy-time/defmt"] [patch.crates-io] embassy-sync = { git = "https://github.com/embassy-rs/embassy.git", rev = "1a1d5c4689a8b6c57ebb74e99fdea8df39adb037" } diff --git a/btmesh-driver/Cargo.toml b/btmesh-driver/Cargo.toml index 0e90a3d..4a590ae 100644 --- a/btmesh-driver/Cargo.toml +++ b/btmesh-driver/Cargo.toml @@ -45,7 +45,6 @@ flash = [ "embedded-storage", "embedded-storage-async", "postcard", - "postcard/use-defmt", "serde/derive", "btmesh-common/serde", "btmesh-pdu/serde", @@ -58,6 +57,7 @@ defmt = [ "btmesh-device/defmt", "btmesh-models/defmt", "btmesh-pdu/defmt", + "postcard/use-defmt", ] log = ["dep:log"] 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/stack/provisioned/network/mod.rs b/btmesh-driver/src/stack/provisioned/network/mod.rs index 174b678..5e82624 100644 --- a/btmesh-driver/src/stack/provisioned/network/mod.rs +++ b/btmesh-driver/src/stack/provisioned/network/mod.rs @@ -71,6 +71,10 @@ impl DeviceInfo { pub fn is_local_unicast(&self, dst: Address) -> bool { self.local_element_index(dst).is_some() } + + pub fn primary_unicast_address(&self) -> UnicastAddress { + self.primary_unicast_address + } } pub struct NetworkDriver { 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/provisioned/mod.rs b/btmesh-driver/src/storage/provisioned/mod.rs index 8a11d28..525ef67 100644 --- a/btmesh-driver/src/storage/provisioned/mod.rs +++ b/btmesh-driver/src/storage/provisioned/mod.rs @@ -89,7 +89,7 @@ impl ProvisionedConfiguration { &mut self.secrets } - pub(crate) fn device_info(&self) -> &DeviceInfo { + pub fn device_info(&self) -> &DeviceInfo { &self.device_info } diff --git a/btmesh-driver/src/storage/unprovisioned.rs b/btmesh-driver/src/storage/unprovisioned.rs index e3fdd61..b6ff149 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, } @@ -22,6 +22,10 @@ impl UnprovisionedConfiguration { pub fn new(uuid: Uuid) -> Self { Self { uuid } } + + pub fn uuid(&self) -> Uuid { + self.uuid + } } impl From for Configuration { 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 {