Skip to content

Commit

Permalink
Merge pull request #26 from thermigo/main
Browse files Browse the repository at this point in the history
feat: add api for fetching provisioning info
  • Loading branch information
lulf authored Jan 13, 2025
2 parents 74e02ac + c760c52 commit ecdf30c
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 19 deletions.
5 changes: 5 additions & 0 deletions btmesh-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,11 @@ impl Uuid {
.as_bytes(),
)
}

// Get slice
pub fn as_slice(&self) -> &[u8] {
&self.0
}
}

impl Deref for Uuid {
Expand Down
2 changes: 1 addition & 1 deletion btmesh-device/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
2 changes: 1 addition & 1 deletion btmesh-driver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ flash = [
"embedded-storage",
"embedded-storage-async",
"postcard",
"postcard/use-defmt",
"serde/derive",
"btmesh-common/serde",
"btmesh-pdu/serde",
Expand All @@ -58,6 +57,7 @@ defmt = [
"btmesh-device/defmt",
"btmesh-models/defmt",
"btmesh-pdu/defmt",
"postcard/use-defmt",
]
log = ["dep:log"]

Expand Down
9 changes: 9 additions & 0 deletions btmesh-driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -99,6 +100,14 @@ impl<N: NetworkInterfaces, R: RngCore + CryptoRng, B: BackingStore> Driver<N, R,
persist_interval: config.persist_interval,
}
}

pub async fn get_provisioning_info(&self) -> Result<Configuration, DriverError> {
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> {
Expand Down
4 changes: 4 additions & 0 deletions btmesh-driver/src/stack/provisioned/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
47 changes: 32 additions & 15 deletions btmesh-driver/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,36 @@ impl<B: BackingStore> Storage<B> {
}
}

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<ProvisionedConfiguration, StorageError> {
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(())
}
Expand Down Expand Up @@ -194,4 +207,8 @@ impl<B: BackingStore> Storage<B> {
pub(crate) fn set_composition(&self, composition: Composition) {
self.composition.borrow_mut().replace(composition);
}

pub fn default_config(&self) -> UnprovisionedConfiguration {
self.default_config
}
}
2 changes: 1 addition & 1 deletion btmesh-driver/src/storage/provisioned/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
6 changes: 5 additions & 1 deletion btmesh-driver/src/storage/unprovisioned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -22,6 +22,10 @@ impl UnprovisionedConfiguration {
pub fn new(uuid: Uuid) -> Self {
Self { uuid }
}

pub fn uuid(&self) -> Uuid {
self.uuid
}
}

impl From<UnprovisionedConfiguration> for Configuration {
Expand Down
13 changes: 13 additions & 0 deletions btmesh-nrf-softdevice/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -85,6 +86,10 @@ impl<N: NetworkInterfaces> NrfSoftdeviceDriver<N> {
}
}

pub async fn get_provisioning_info(&self) -> Result<Configuration, DriverError> {
self.driver.get_provisioning_info().await
}

#[allow(unreachable_code)]
pub async fn run<'r, D: BluetoothMeshDevice>(
&'r mut self,
Expand Down Expand Up @@ -128,6 +133,10 @@ impl NrfSoftdeviceAdvertisingOnlyDriver {
))
}

pub async fn get_provisioning_info(&self) -> Result<Configuration, DriverError> {
self.0.get_provisioning_info().await
}

pub fn softdevice(&self) -> &'static Softdevice {
self.0.sd
}
Expand Down Expand Up @@ -190,6 +199,10 @@ impl NrfSoftdeviceAdvertisingAndGattDriver {
config,
))
}

pub async fn get_provisioning_info(&self) -> Result<Configuration, DriverError> {
self.0.get_provisioning_info().await
}
}

impl BluetoothMeshDriver for NrfSoftdeviceAdvertisingAndGattDriver {
Expand Down

0 comments on commit ecdf30c

Please sign in to comment.