Skip to content

Commit 38d56ce

Browse files
committed
Convert all system cluster handlers to strongly-typed ones using the import macro
1 parent b8a1f74 commit 38d56ce

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+6662
-4309
lines changed

examples/src/bin/onoff_light.rs

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,25 @@ use log::info;
3030
use rs_matter::core::{Matter, MATTER_PORT};
3131
use rs_matter::data_model::core::IMBuffer;
3232
use rs_matter::data_model::device_types::DEV_TYPE_ON_OFF_LIGHT;
33-
use rs_matter::data_model::objects::*;
33+
use rs_matter::data_model::networks::unix::UnixNetifs;
34+
use rs_matter::data_model::objects::{
35+
Async, AsyncHandler, AsyncMetadata, Dataver, EmptyHandler, Endpoint, Node,
36+
};
3437
use rs_matter::data_model::on_off::{self, ClusterHandler as _};
3538
use rs_matter::data_model::root_endpoint;
39+
use rs_matter::data_model::sdm::net_comm::NetworkType;
3640
use rs_matter::data_model::subscriptions::Subscriptions;
37-
use rs_matter::data_model::system_model::descriptor;
41+
use rs_matter::data_model::system_model::desc::{self, ClusterHandler as _};
3842
use rs_matter::error::Error;
3943
use rs_matter::mdns::MdnsService;
4044
use rs_matter::pairing::DiscoveryCapabilities;
4145
use rs_matter::persist::Psm;
4246
use rs_matter::respond::DefaultResponder;
43-
use rs_matter::test_device;
4447
use rs_matter::transport::core::MATTER_SOCKET_BIND_ADDR;
4548
use rs_matter::utils::init::InitMaybeUninit;
4649
use rs_matter::utils::select::Coalesce;
4750
use rs_matter::utils::storage::pooled::PooledBuffers;
51+
use rs_matter::{clusters, devices, test_device};
4852

4953
use static_cell::StaticCell;
5054

@@ -68,7 +72,7 @@ fn main() -> Result<(), Error> {
6872
// e.g., an opt-level of "0" will require a several times' larger stack.
6973
//
7074
// Optimizing/lowering `rs-matter` memory consumption is an ongoing topic.
71-
.stack_size(45 * 1024)
75+
.stack_size(55 * 1024)
7276
.spawn(run)
7377
.unwrap();
7478

@@ -120,7 +124,7 @@ fn run() -> Result<(), Error> {
120124
let on_off = on_off::OnOffHandler::new(Dataver::new_rand(matter.rand()));
121125

122126
// Assemble our Data Model handler by composing the predefined Root Endpoint handler with the On/Off handler
123-
let dm_handler = Async(dm_handler(matter, &on_off));
127+
let dm_handler = dm_handler(matter, &on_off);
124128

125129
// Create a default responder capable of handling up to 3 subscriptions
126130
// All other subscription requests will be turned down with "resource exhausted"
@@ -192,11 +196,11 @@ fn run() -> Result<(), Error> {
192196
const NODE: Node<'static> = Node {
193197
id: 0,
194198
endpoints: &[
195-
root_endpoint::endpoint(0, root_endpoint::OperNwType::Ethernet),
199+
root_endpoint::root_endpoint(NetworkType::Ethernet),
196200
Endpoint {
197201
id: 1,
198-
device_types: &[DEV_TYPE_ON_OFF_LIGHT],
199-
clusters: &[descriptor::CLUSTER, on_off::OnOffHandler::CLUSTER],
202+
device_types: devices!(DEV_TYPE_ON_OFF_LIGHT),
203+
clusters: clusters!(desc::DescHandler::CLUSTER, on_off::OnOffHandler::CLUSTER),
200204
},
201205
],
202206
};
@@ -206,21 +210,28 @@ const NODE: Node<'static> = Node {
206210
fn dm_handler<'a>(
207211
matter: &'a Matter<'a>,
208212
on_off: &'a on_off::OnOffHandler,
209-
) -> impl Metadata + NonBlockingHandler + 'a {
213+
) -> impl AsyncMetadata + AsyncHandler + 'a {
210214
(
211215
NODE,
212-
root_endpoint::eth_handler(0, matter.rand())
213-
.chain(
214-
1,
215-
descriptor::ID,
216-
Async(descriptor::DescriptorCluster::new(Dataver::new_rand(
217-
matter.rand(),
218-
))),
219-
)
220-
.chain(
221-
1,
222-
on_off::OnOffHandler::CLUSTER.id,
223-
Async(on_off::HandlerAdaptor(on_off)),
216+
root_endpoint::with_eth(
217+
&(),
218+
&UnixNetifs,
219+
matter.rand(),
220+
root_endpoint::with_sys(
221+
&false,
222+
matter.rand(),
223+
EmptyHandler
224+
.chain(
225+
1,
226+
desc::DescHandler::CLUSTER.id,
227+
Async(desc::DescHandler::new(Dataver::new_rand(matter.rand())).adapt()),
228+
)
229+
.chain(
230+
1,
231+
on_off::OnOffHandler::CLUSTER.id,
232+
Async(on_off::HandlerAdaptor(on_off)),
233+
),
224234
),
235+
),
225236
)
226237
}

examples/src/bin/onoff_light_bt.rs

Lines changed: 57 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -30,38 +30,40 @@ use core::pin::pin;
3030

3131
use std::net::UdpSocket;
3232

33-
use comm::WifiNwCommCluster;
3433
use embassy_futures::select::{select, select4};
3534

3635
use embassy_sync::blocking_mutex::raw::NoopRawMutex;
3736
use embassy_time::{Duration, Timer};
38-
use log::{info, warn};
37+
use log::info;
3938

4039
use rs_matter::core::Matter;
4140
use rs_matter::data_model::device_types::DEV_TYPE_ON_OFF_LIGHT;
42-
use rs_matter::data_model::objects::*;
41+
use rs_matter::data_model::networks::unix::UnixNetifs;
42+
use rs_matter::data_model::networks::wireless::{
43+
NetCtlState, NetCtlWithStatusImpl, NoopWirelessNetCtl, WifiNetworks,
44+
};
45+
use rs_matter::data_model::objects::{
46+
Async, AsyncHandler, AsyncMetadata, Dataver, EmptyHandler, Endpoint, Node,
47+
};
4348
use rs_matter::data_model::on_off::{self, ClusterHandler as _};
4449
use rs_matter::data_model::root_endpoint;
45-
use rs_matter::data_model::sdm::wifi_nw_diagnostics::{
46-
self, WiFiSecurity, WiFiVersion, WifiNwDiagCluster, WifiNwDiagData,
47-
};
50+
use rs_matter::data_model::sdm::net_comm::{NetCtl, NetCtlStatus, NetworkType, Networks};
51+
use rs_matter::data_model::sdm::wifi_diag::WifiDiag;
4852
use rs_matter::data_model::subscriptions::Subscriptions;
49-
use rs_matter::data_model::system_model::descriptor;
53+
use rs_matter::data_model::system_model::desc::{self, ClusterHandler as _};
5054
use rs_matter::error::Error;
5155
use rs_matter::mdns::MdnsService;
5256
use rs_matter::pairing::DiscoveryCapabilities;
5357
use rs_matter::persist::Psm;
5458
use rs_matter::respond::DefaultResponder;
55-
use rs_matter::test_device;
5659
use rs_matter::transport::core::MATTER_SOCKET_BIND_ADDR;
5760
use rs_matter::transport::network::btp::{Btp, BtpContext};
5861
use rs_matter::utils::select::Coalesce;
5962
use rs_matter::utils::storage::pooled::PooledBuffers;
60-
use rs_matter::utils::sync::{blocking::raw::StdRawMutex, Notification};
61-
use rs_matter::MATTER_PORT;
63+
use rs_matter::utils::sync::blocking::raw::StdRawMutex;
64+
use rs_matter::{clusters, test_device};
65+
use rs_matter::{devices, MATTER_PORT};
6266

63-
#[path = "../common/comm.rs"]
64-
mod comm;
6567
#[path = "../common/mdns.rs"]
6668
mod mdns;
6769

@@ -94,11 +96,17 @@ fn main() -> Result<(), Error> {
9496
// Our on-off cluster
9597
let on_off = on_off::OnOffHandler::new(Dataver::new_rand(matter.rand()));
9698

97-
// A notification when the Wifi is setup, so that Matter over UDP can start
98-
let wifi_complete = Notification::new();
99+
// A storage for the Wifi networks
100+
let networks = WifiNetworks::<3, NoopRawMutex>::new();
101+
102+
// The network controller
103+
// We would be using a "fake" network controller that does not actually manage any Wifi networks
104+
let net_ctl_state = NetCtlState::new_with_mutex::<NoopRawMutex>();
105+
let net_ctl =
106+
NetCtlWithStatusImpl::new(&net_ctl_state, NoopWirelessNetCtl::new(NetworkType::Wifi));
99107

100108
// Assemble our Data Model handler by composing the predefined Root Endpoint handler with the On/Off handler
101-
let dm_handler = Async(dm_handler(&matter, &on_off, &wifi_complete));
109+
let dm_handler = dm_handler(&matter, &on_off, &net_ctl, &networks);
102110

103111
// Create a default responder capable of handling up to 3 subscriptions
104112
// All other subscription requests will be turned down with "resource exhausted"
@@ -127,8 +135,9 @@ fn main() -> Result<(), Error> {
127135
let dir = std::env::temp_dir().join("rs-matter");
128136

129137
psm.load(&dir, &matter)?;
138+
psm.load_networks(&dir, &networks)?;
130139

131-
let mut persist = pin!(psm.run(dir, &matter));
140+
let mut persist = pin!(psm.run_with_networks(dir, &matter, Some(&networks)));
132141

133142
// Create and run the mDNS responder
134143
let mut mdns = pin!(mdns::run_mdns(&matter));
@@ -145,23 +154,16 @@ fn main() -> Result<(), Error> {
145154
));
146155

147156
let mut transport = pin!(matter.run(&btp, &btp, DiscoveryCapabilities::BLE));
148-
149-
let mut wifi_complete_task = pin!(async {
150-
wifi_complete.wait().await;
151-
warn!(
152-
"Wifi setup complete, giving 4 seconds to BTP to finish any outstanding messages"
153-
);
154-
155-
Timer::after(Duration::from_secs(4)).await;
156-
157+
let mut wifi_prov_task = pin!(async {
158+
NetCtlState::wait_prov_ready(&net_ctl_state, &btp).await;
157159
Ok(())
158160
});
159161

160162
// Combine all async tasks in a single one
161163
let all = select4(
162164
&mut transport,
163165
&mut bluetooth,
164-
select(&mut wifi_complete_task, &mut persist).coalesce(),
166+
select(&mut wifi_prov_task, &mut persist).coalesce(),
165167
select(&mut respond, &mut device).coalesce(),
166168
);
167169

@@ -193,55 +195,49 @@ fn main() -> Result<(), Error> {
193195
const NODE: Node<'static> = Node {
194196
id: 0,
195197
endpoints: &[
196-
root_endpoint::endpoint(0, root_endpoint::OperNwType::Wifi),
198+
root_endpoint::root_endpoint(NetworkType::Wifi),
197199
Endpoint {
198200
id: 1,
199-
device_types: &[DEV_TYPE_ON_OFF_LIGHT],
200-
clusters: &[descriptor::CLUSTER, on_off::OnOffHandler::CLUSTER],
201+
device_types: devices!(DEV_TYPE_ON_OFF_LIGHT),
202+
clusters: clusters!(desc::DescHandler::CLUSTER, on_off::OnOffHandler::CLUSTER),
201203
},
202204
],
203205
};
204206

205207
/// The Data Model handler + meta-data for our Matter device.
206208
/// The handler is the root endpoint 0 handler plus the on-off handler and its descriptor.
207-
fn dm_handler<'a>(
209+
fn dm_handler<'a, N>(
208210
matter: &'a Matter<'a>,
209211
on_off: &'a on_off::OnOffHandler,
210-
nw_setup_complete: &'a Notification<NoopRawMutex>,
211-
) -> impl Metadata + NonBlockingHandler + 'a {
212+
net_ctl: &'a N,
213+
networks: &'a dyn Networks,
214+
) -> impl AsyncMetadata + AsyncHandler + 'a
215+
where
216+
N: NetCtl + NetCtlStatus + WifiDiag,
217+
{
212218
(
213219
NODE,
214-
root_endpoint::handler(
215-
0,
216-
Async(WifiNwCommCluster::new(
217-
Dataver::new_rand(matter.rand()),
218-
nw_setup_complete,
219-
)),
220-
wifi_nw_diagnostics::ID,
221-
Async(WifiNwDiagCluster::new(
222-
Dataver::new_rand(matter.rand()),
223-
WifiNwDiagData {
224-
bssid: [0; 6],
225-
security_type: WiFiSecurity::Wpa2Personal,
226-
wifi_version: WiFiVersion::B,
227-
channel_number: 20,
228-
rssi: 0,
229-
},
230-
)),
231-
&false,
220+
root_endpoint::with_wifi(
221+
&(),
222+
&UnixNetifs,
223+
net_ctl,
224+
networks,
232225
matter.rand(),
233-
)
234-
.chain(
235-
1,
236-
descriptor::ID,
237-
Async(descriptor::DescriptorCluster::new(Dataver::new_rand(
226+
root_endpoint::with_sys(
227+
&false,
238228
matter.rand(),
239-
))),
240-
)
241-
.chain(
242-
1,
243-
on_off::OnOffHandler::CLUSTER.id,
244-
Async(on_off::HandlerAdaptor(on_off)),
229+
EmptyHandler
230+
.chain(
231+
1,
232+
desc::DescHandler::CLUSTER.id,
233+
Async(desc::DescHandler::new(Dataver::new_rand(matter.rand())).adapt()),
234+
)
235+
.chain(
236+
1,
237+
on_off::OnOffHandler::CLUSTER.id,
238+
Async(on_off::HandlerAdaptor(on_off)),
239+
),
240+
),
245241
),
246242
)
247243
}

0 commit comments

Comments
 (0)