Skip to content

Commit ecdf30c

Browse files
authored
Merge pull request #26 from thermigo/main
feat: add api for fetching provisioning info
2 parents 74e02ac + c760c52 commit ecdf30c

File tree

9 files changed

+71
-19
lines changed

9 files changed

+71
-19
lines changed

btmesh-common/src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,11 @@ impl Uuid {
368368
.as_bytes(),
369369
)
370370
}
371+
372+
// Get slice
373+
pub fn as_slice(&self) -> &[u8] {
374+
&self.0
375+
}
371376
}
372377

373378
impl Deref for Uuid {

btmesh-device/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ log = { version = "0.4", optional = true }
1919
defmt = { version = "0.3", optional = true }
2020

2121
[features]
22-
defmt = ["dep:defmt", "heapless/defmt-03"]
22+
defmt = ["dep:defmt", "heapless/defmt-03", "embassy-time/defmt"]
2323

2424
[patch.crates-io]
2525
embassy-sync = { git = "https://github.com/embassy-rs/embassy.git", rev = "1a1d5c4689a8b6c57ebb74e99fdea8df39adb037" }

btmesh-driver/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ flash = [
4545
"embedded-storage",
4646
"embedded-storage-async",
4747
"postcard",
48-
"postcard/use-defmt",
4948
"serde/derive",
5049
"btmesh-common/serde",
5150
"btmesh-pdu/serde",
@@ -58,6 +57,7 @@ defmt = [
5857
"btmesh-device/defmt",
5958
"btmesh-models/defmt",
6059
"btmesh-pdu/defmt",
60+
"postcard/use-defmt",
6161
]
6262
log = ["dep:log"]
6363

btmesh-driver/src/lib.rs

+9
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use btmesh_pdu::provisioned::Message;
2323
use btmesh_pdu::provisioning::generic::Reason;
2424
use btmesh_pdu::provisioning::{Capabilities, ProvisioningPDU};
2525
use btmesh_pdu::PDU;
26+
use storage::StorageError;
2627
use core::cell::RefCell;
2728
use core::future::{pending, Future};
2829
use embassy_futures::select::{select, select3, select4, Either, Either3, Either4};
@@ -99,6 +100,14 @@ impl<N: NetworkInterfaces, R: RngCore + CryptoRng, B: BackingStore> Driver<N, R,
99100
persist_interval: config.persist_interval,
100101
}
101102
}
103+
104+
pub async fn get_provisioning_info(&self) -> Result<Configuration, DriverError> {
105+
match self.storage.load().await {
106+
Ok(configuration) => Ok(Configuration::Provisioned(configuration)),
107+
Err(StorageError::Serialization) => {Ok(Configuration::Unprovisioned(self.storage.default_config()))},
108+
Err(e) => Err(DriverError::Storage(e))
109+
}
110+
}
102111
}
103112

104113
pub struct InnerDriver<'s, N: NetworkInterfaces, R: RngCore + CryptoRng, B: BackingStore + 's> {

btmesh-driver/src/stack/provisioned/network/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ impl DeviceInfo {
7171
pub fn is_local_unicast(&self, dst: Address) -> bool {
7272
self.local_element_index(dst).is_some()
7373
}
74+
75+
pub fn primary_unicast_address(&self) -> UnicastAddress {
76+
self.primary_unicast_address
77+
}
7478
}
7579

7680
pub struct NetworkDriver {

btmesh-driver/src/storage/mod.rs

+32-15
Original file line numberDiff line numberDiff line change
@@ -85,23 +85,36 @@ impl<B: BackingStore> Storage<B> {
8585
}
8686
}
8787

88-
pub async fn init(&self) -> Result<(), StorageError> {
89-
let mut locked_config = self.config.lock().await;
90-
let mut backing_store = self.backing_store.borrow_mut();
91-
if let Ok(mut config) = backing_store.load().await {
92-
let seq = config.sequence();
88+
pub async fn load(&self) -> Result<ProvisionedConfiguration, StorageError> {
89+
let config = {
90+
let mut backing_store = self.backing_store.borrow_mut();
91+
backing_store.load().await?
92+
};
93+
Ok(config)
94+
}
9395

94-
let mut extra = seq % 100;
95-
if extra == 100 {
96-
extra = 0;
96+
pub async fn init(&self) -> Result<(), StorageError> {
97+
let config = self.load().await;
98+
match config {
99+
Ok(mut config) => {
100+
let seq = config.sequence();
101+
102+
let mut extra = seq % 100;
103+
if extra == 100 {
104+
extra = 0;
105+
}
106+
let seq = (seq - extra) + 100;
107+
108+
*config.sequence_mut() = seq;
109+
let mut backing_store = self.backing_store.borrow_mut();
110+
backing_store.store(&config).await?;
111+
let mut locked_config = self.config.lock().await;
112+
locked_config.replace(Configuration::Provisioned(config));
113+
}
114+
Err(_) => {
115+
let mut locked_config = self.config.lock().await;
116+
locked_config.replace(Configuration::Unprovisioned(self.default_config.clone()));
97117
}
98-
let seq = (seq - extra) + 100;
99-
100-
*config.sequence_mut() = seq;
101-
backing_store.store(&config).await?;
102-
locked_config.replace(Configuration::Provisioned(config));
103-
} else {
104-
locked_config.replace(Configuration::Unprovisioned(self.default_config.clone()));
105118
}
106119
Ok(())
107120
}
@@ -194,4 +207,8 @@ impl<B: BackingStore> Storage<B> {
194207
pub(crate) fn set_composition(&self, composition: Composition) {
195208
self.composition.borrow_mut().replace(composition);
196209
}
210+
211+
pub fn default_config(&self) -> UnprovisionedConfiguration {
212+
self.default_config
213+
}
197214
}

btmesh-driver/src/storage/provisioned/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl ProvisionedConfiguration {
8989
&mut self.secrets
9090
}
9191

92-
pub(crate) fn device_info(&self) -> &DeviceInfo {
92+
pub fn device_info(&self) -> &DeviceInfo {
9393
&self.device_info
9494
}
9595

btmesh-driver/src/storage/unprovisioned.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};
55

66
#[cfg_attr(feature = "defmt", derive(::defmt::Format))]
77
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
8-
#[derive(Clone, Hash, Debug)]
8+
#[derive(Clone, Hash, Debug, Copy)]
99
pub struct UnprovisionedConfiguration {
1010
pub(crate) uuid: Uuid,
1111
}
@@ -22,6 +22,10 @@ impl UnprovisionedConfiguration {
2222
pub fn new(uuid: Uuid) -> Self {
2323
Self { uuid }
2424
}
25+
26+
pub fn uuid(&self) -> Uuid {
27+
self.uuid
28+
}
2529
}
2630

2731
impl From<UnprovisionedConfiguration> for Configuration {

btmesh-nrf-softdevice/src/driver.rs

+13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use btmesh_driver::interface::{
66
AdvertisingAndGattNetworkInterfaces, AdvertisingOnlyNetworkInterfaces, NetworkInterfaces,
77
};
88
use btmesh_driver::storage::flash::FlashBackingStore;
9+
use btmesh_driver::storage::Configuration;
910
use btmesh_driver::{
1011
BluetoothMeshDriver, BluetoothMeshDriverConfig, Driver as BaseDriver, DriverError,
1112
};
@@ -85,6 +86,10 @@ impl<N: NetworkInterfaces> NrfSoftdeviceDriver<N> {
8586
}
8687
}
8788

89+
pub async fn get_provisioning_info(&self) -> Result<Configuration, DriverError> {
90+
self.driver.get_provisioning_info().await
91+
}
92+
8893
#[allow(unreachable_code)]
8994
pub async fn run<'r, D: BluetoothMeshDevice>(
9095
&'r mut self,
@@ -128,6 +133,10 @@ impl NrfSoftdeviceAdvertisingOnlyDriver {
128133
))
129134
}
130135

136+
pub async fn get_provisioning_info(&self) -> Result<Configuration, DriverError> {
137+
self.0.get_provisioning_info().await
138+
}
139+
131140
pub fn softdevice(&self) -> &'static Softdevice {
132141
self.0.sd
133142
}
@@ -190,6 +199,10 @@ impl NrfSoftdeviceAdvertisingAndGattDriver {
190199
config,
191200
))
192201
}
202+
203+
pub async fn get_provisioning_info(&self) -> Result<Configuration, DriverError> {
204+
self.0.get_provisioning_info().await
205+
}
193206
}
194207

195208
impl BluetoothMeshDriver for NrfSoftdeviceAdvertisingAndGattDriver {

0 commit comments

Comments
 (0)