Skip to content

Commit 67d6510

Browse files
committed
Update to rs-matter with HandlerContext
1 parent ad8cb61 commit 67d6510

File tree

5 files changed

+43
-27
lines changed

5 files changed

+43
-27
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,4 @@ jobs:
5454

5555
- name: Examples
5656
if: matrix.features == 'os'
57-
run: cargo build --examples --features ${{matrix.features}},nix,log
57+
run: cargo build --examples --features ${{matrix.features}},nix,log,examples

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ rust-version = "1.77"
1414

1515
[patch.crates-io]
1616
#rs-matter = { git = "https://github.com/project-chip/rs-matter" }
17-
rs-matter = { git = "https://github.com/sysgrok/rs-matter", branch = "next" }
17+
rs-matter = { git = "https://github.com/sysgrok/rs-matter", branch = "next-run-handlers" }
1818
#rs-matter = { path = "../rs-matter/rs-matter" }
1919
#edge-nal = { git = "https://github.com/sysgrok/edge-net" }
2020
#edge-nal-std = { git = "https://github.com/sysgrok/edge-net" }

examples/light.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ use rs_matter_stack::wireless::WifiMatterStack;
3838

3939
use static_cell::StaticCell;
4040

41+
const BUMP_SIZE: usize = 24000;
42+
4143
fn main() -> Result<(), Error> {
4244
env_logger::init_from_env(
4345
env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "info"),
@@ -78,7 +80,7 @@ fn main() -> Result<(), Error> {
7880
// Using `pin!` is completely optional, but saves some memory due to `rustc`
7981
// not being very intelligent w.r.t. stack usage in async functions
8082
let store = stack.create_shared_store(DirKvBlobStore::new_default());
81-
let mut matter = pin!(stack.run(
83+
let mut matter = pin!(stack.run_coex(
8284
PreexistingWireless::new(
8385
// The Matter stack needs UDP sockets to communicate with other Matter devices
8486
edge_nal_std::Stack::new(),
@@ -113,7 +115,7 @@ fn main() -> Result<(), Error> {
113115

114116
// Let the Matter stack know that we have changed
115117
// the state of our Light device
116-
stack.notify_changed();
118+
stack.notify_cluster_changed(1, on_off::OnOffHandler::CLUSTER.id);
117119

118120
info!("Light toggled");
119121
}
@@ -128,7 +130,7 @@ fn main() -> Result<(), Error> {
128130
/// The Matter stack is allocated statically to avoid
129131
/// program stack blowups.
130132
/// It is also a mandatory requirement when the `WifiBle` stack variation is used.
131-
static MATTER_STACK: StaticCell<WifiMatterStack<StdRawMutex>> = StaticCell::new();
133+
static MATTER_STACK: StaticCell<WifiMatterStack<BUMP_SIZE, StdRawMutex>> = StaticCell::new();
132134

133135
/// Endpoint 0 (the root endpoint) always runs
134136
/// the hidden Matter system clusters, so we pick ID=1
@@ -138,7 +140,7 @@ const LIGHT_ENDPOINT_ID: u16 = 1;
138140
const NODE: Node = Node {
139141
id: 0,
140142
endpoints: &[
141-
WifiMatterStack::<StdRawMutex, ()>::root_endpoint(),
143+
WifiMatterStack::<0, StdRawMutex, ()>::root_endpoint(),
142144
Endpoint {
143145
id: LIGHT_ENDPOINT_ID,
144146
device_types: devices!(DEV_TYPE_ON_OFF_LIGHT),

examples/light_eth.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ use rs_matter_stack::persist::DirKvBlobStore;
3737

3838
use static_cell::StaticCell;
3939

40+
const BUMP_SIZE: usize = 20000;
41+
4042
fn main() -> Result<(), Error> {
4143
env_logger::Builder::from_env(
4244
env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "info"),
@@ -111,7 +113,7 @@ fn main() -> Result<(), Error> {
111113

112114
// Let the Matter stack know that we have changed
113115
// the state of our Light device
114-
stack.notify_changed();
116+
stack.notify_cluster_changed(1, on_off::OnOffHandler::CLUSTER.id);
115117

116118
info!("Light toggled");
117119
}
@@ -123,7 +125,7 @@ fn main() -> Result<(), Error> {
123125

124126
/// The Matter stack is allocated statically to avoid
125127
/// program stack blowups.
126-
static MATTER_STACK: StaticCell<EthMatterStack> = StaticCell::new();
128+
static MATTER_STACK: StaticCell<EthMatterStack<BUMP_SIZE, ()>> = StaticCell::new();
127129

128130
/// Endpoint 0 (the root endpoint) always runs
129131
/// the hidden Matter system clusters, so we pick ID=1
@@ -133,7 +135,7 @@ const LIGHT_ENDPOINT_ID: u16 = 1;
133135
const NODE: Node = Node {
134136
id: 0,
135137
endpoints: &[
136-
EthMatterStack::<()>::root_endpoint(),
138+
EthMatterStack::<0, ()>::root_endpoint(),
137139
Endpoint {
138140
id: LIGHT_ENDPOINT_ID,
139141
device_types: devices!(DEV_TYPE_ON_OFF_LIGHT),

src/lib.rs

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ use rs_matter::dm::clusters::dev_att::DevAttDataFetcher;
2929
use rs_matter::dm::clusters::gen_diag::NetifDiag;
3030
use rs_matter::dm::networks::NetChangeNotif;
3131
use rs_matter::dm::subscriptions::Subscriptions;
32-
use rs_matter::dm::IMBuffer;
33-
use rs_matter::dm::{AsyncHandler, AsyncMetadata};
32+
use rs_matter::dm::{AsyncHandler, AsyncMetadata, ClusterId, DataModel, EndptId, IMBuffer};
3433
use rs_matter::error::{Error, ErrorCode};
3534
use rs_matter::respond::{DefaultResponder, ExchangeHandler, Responder};
3635
use rs_matter::transport::network::{Address, ChainedNetwork, NetworkReceive, NetworkSend};
@@ -344,13 +343,18 @@ where
344343
/// Notifies the Matter instance that there is a change in the state
345344
/// of one of the clusters.
346345
///
347-
/// User is expected to call this method when user-provided clusters
348-
/// change their state.
346+
/// User is expected to call this method when a user-provided cluster
347+
/// changes its state.
349348
///
350349
/// This is necessary so as the Matter instance can notify clients
351350
/// that have active subscriptions to some of the changed clusters.
352-
pub fn notify_changed(&self) {
353-
self.subscriptions.notify_changed();
351+
///
352+
/// # Arguments
353+
/// - `endpoint_id` - the endpoint ID where the cluster is located
354+
/// - `cluster_id` - the ID of the cluster that has changed
355+
pub fn notify_cluster_changed(&self, endpoint_id: EndptId, cluster_id: ClusterId) {
356+
self.subscriptions
357+
.notify_cluster_changed(endpoint_id, cluster_id);
354358
}
355359

356360
// /// User code hook to get the state of the netif passed to the
@@ -607,10 +611,12 @@ where
607611
// Reset the Matter transport buffers and all sessions first
608612
// self.matter().reset_transport()?;
609613

610-
let mut responder = pin!(self.run_responder(&handler));
611-
let mut handler = pin!(handler.run());
614+
let dm = DataModel::new(self.matter(), &self.buffers, &self.subscriptions, handler);
615+
616+
let mut responder = pin!(self.run_responder(&dm));
617+
let mut dm_job = pin!(dm.run());
612618

613-
select(&mut responder, &mut handler).coalesce().await
619+
select(&mut responder, &mut dm_job).coalesce().await
614620
}
615621

616622
async fn run_handler_with_bump<H>(&self, handler: H) -> Result<(), Error>
@@ -621,10 +627,12 @@ where
621627
// Reset the Matter transport buffers and all sessions first
622628
// self.matter().reset_transport()?;
623629

624-
let mut responder = pin_alloc!(self.bump, self.run_responder_with_bump(&handler));
625-
let mut handler = pin_alloc!(self.bump, handler.run());
630+
let dm = DataModel::new(self.matter(), &self.buffers, &self.subscriptions, handler);
626631

627-
select(&mut responder, &mut handler).coalesce().await
632+
let mut responder = pin_alloc!(self.bump, self.run_responder_with_bump(&dm));
633+
let mut dm_job = pin!(dm.run());
634+
635+
select(&mut responder, &mut dm_job).coalesce().await
628636
}
629637

630638
fn run_psm<'t, S, C>(
@@ -638,12 +646,14 @@ where
638646
persist.run()
639647
}
640648

641-
async fn run_responder<H>(&self, handler: H) -> Result<(), Error>
649+
async fn run_responder<const SN: usize, H>(
650+
&self,
651+
dm: &DataModel<'_, SN, PooledBuffers<MAX_IM_BUFFERS, NoopRawMutex, IMBuffer>, H>,
652+
) -> Result<(), Error>
642653
where
643654
H: AsyncHandler + AsyncMetadata,
644655
{
645-
let responder =
646-
DefaultResponder::new(self.matter(), &self.buffers, &self.subscriptions, handler);
656+
let responder = DefaultResponder::new(dm);
647657

648658
// Run the responder with up to MAX_RESPONDERS handlers (i.e. MAX_RESPONDERS exchanges can be handled simultenously)
649659
// Clients trying to open more exchanges than the ones currently running will get "I'm busy, please try again later"
@@ -652,12 +662,14 @@ where
652662
Ok(())
653663
}
654664

655-
async fn run_responder_with_bump<H>(&self, handler: H) -> Result<(), Error>
665+
async fn run_responder_with_bump<const SN: usize, H>(
666+
&self,
667+
dm: &DataModel<'_, SN, PooledBuffers<MAX_IM_BUFFERS, NoopRawMutex, IMBuffer>, H>,
668+
) -> Result<(), Error>
656669
where
657670
H: AsyncHandler + AsyncMetadata,
658671
{
659-
let responder =
660-
DefaultResponder::new(self.matter(), &self.buffers, &self.subscriptions, handler);
672+
let responder = DefaultResponder::new(dm);
661673

662674
let mut actual = pin_alloc!(
663675
self.bump,

0 commit comments

Comments
 (0)