Skip to content

IDL gen improvements 2 - Convert all system clusters to use the import! macro #237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 32 additions & 21 deletions examples/src/bin/onoff_light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,25 @@ use log::info;
use rs_matter::core::{Matter, MATTER_PORT};
use rs_matter::data_model::core::IMBuffer;
use rs_matter::data_model::device_types::DEV_TYPE_ON_OFF_LIGHT;
use rs_matter::data_model::objects::*;
use rs_matter::data_model::networks::unix::UnixNetifs;
use rs_matter::data_model::objects::{
Async, AsyncHandler, AsyncMetadata, Dataver, EmptyHandler, Endpoint, Node,
};
use rs_matter::data_model::on_off::{self, ClusterHandler as _};
use rs_matter::data_model::root_endpoint;
use rs_matter::data_model::sdm::net_comm::NetworkType;
use rs_matter::data_model::subscriptions::Subscriptions;
use rs_matter::data_model::system_model::descriptor;
use rs_matter::data_model::system_model::desc::{self, ClusterHandler as _};
use rs_matter::error::Error;
use rs_matter::mdns::MdnsService;
use rs_matter::pairing::DiscoveryCapabilities;
use rs_matter::persist::Psm;
use rs_matter::respond::DefaultResponder;
use rs_matter::test_device;
use rs_matter::transport::core::MATTER_SOCKET_BIND_ADDR;
use rs_matter::utils::init::InitMaybeUninit;
use rs_matter::utils::select::Coalesce;
use rs_matter::utils::storage::pooled::PooledBuffers;
use rs_matter::{clusters, devices, test_device};

use static_cell::StaticCell;

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

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

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

// Create a default responder capable of handling up to 3 subscriptions
// All other subscription requests will be turned down with "resource exhausted"
Expand Down Expand Up @@ -192,11 +196,11 @@ fn run() -> Result<(), Error> {
const NODE: Node<'static> = Node {
id: 0,
endpoints: &[
root_endpoint::endpoint(0, root_endpoint::OperNwType::Ethernet),
root_endpoint::root_endpoint(NetworkType::Ethernet),
Endpoint {
id: 1,
device_types: &[DEV_TYPE_ON_OFF_LIGHT],
clusters: &[descriptor::CLUSTER, on_off::OnOffHandler::CLUSTER],
device_types: devices!(DEV_TYPE_ON_OFF_LIGHT),
clusters: clusters!(desc::DescHandler::CLUSTER, on_off::OnOffHandler::CLUSTER),
},
],
};
Expand All @@ -206,21 +210,28 @@ const NODE: Node<'static> = Node {
fn dm_handler<'a>(
matter: &'a Matter<'a>,
on_off: &'a on_off::OnOffHandler,
) -> impl Metadata + NonBlockingHandler + 'a {
) -> impl AsyncMetadata + AsyncHandler + 'a {
(
NODE,
root_endpoint::eth_handler(0, matter.rand())
.chain(
1,
descriptor::ID,
Async(descriptor::DescriptorCluster::new(Dataver::new_rand(
matter.rand(),
))),
)
.chain(
1,
on_off::OnOffHandler::CLUSTER.id,
Async(on_off::HandlerAdaptor(on_off)),
root_endpoint::with_eth(
&(),
&UnixNetifs,
matter.rand(),
root_endpoint::with_sys(
&false,
matter.rand(),
EmptyHandler
.chain(
1,
desc::DescHandler::CLUSTER.id,
Async(desc::DescHandler::new(Dataver::new_rand(matter.rand())).adapt()),
)
.chain(
1,
on_off::OnOffHandler::CLUSTER.id,
Async(on_off::HandlerAdaptor(on_off)),
),
),
),
)
}
118 changes: 57 additions & 61 deletions examples/src/bin/onoff_light_bt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,38 +30,40 @@ use core::pin::pin;

use std::net::UdpSocket;

use comm::WifiNwCommCluster;
use embassy_futures::select::{select, select4};

use embassy_sync::blocking_mutex::raw::NoopRawMutex;
use embassy_time::{Duration, Timer};
use log::{info, warn};
use log::info;

use rs_matter::core::Matter;
use rs_matter::data_model::device_types::DEV_TYPE_ON_OFF_LIGHT;
use rs_matter::data_model::objects::*;
use rs_matter::data_model::networks::unix::UnixNetifs;
use rs_matter::data_model::networks::wireless::{
NetCtlState, NetCtlWithStatusImpl, NoopWirelessNetCtl, WifiNetworks,
};
use rs_matter::data_model::objects::{
Async, AsyncHandler, AsyncMetadata, Dataver, EmptyHandler, Endpoint, Node,
};
use rs_matter::data_model::on_off::{self, ClusterHandler as _};
use rs_matter::data_model::root_endpoint;
use rs_matter::data_model::sdm::wifi_nw_diagnostics::{
self, WiFiSecurity, WiFiVersion, WifiNwDiagCluster, WifiNwDiagData,
};
use rs_matter::data_model::sdm::net_comm::{NetCtl, NetCtlStatus, NetworkType, Networks};
use rs_matter::data_model::sdm::wifi_diag::WifiDiag;
use rs_matter::data_model::subscriptions::Subscriptions;
use rs_matter::data_model::system_model::descriptor;
use rs_matter::data_model::system_model::desc::{self, ClusterHandler as _};
use rs_matter::error::Error;
use rs_matter::mdns::MdnsService;
use rs_matter::pairing::DiscoveryCapabilities;
use rs_matter::persist::Psm;
use rs_matter::respond::DefaultResponder;
use rs_matter::test_device;
use rs_matter::transport::core::MATTER_SOCKET_BIND_ADDR;
use rs_matter::transport::network::btp::{Btp, BtpContext};
use rs_matter::utils::select::Coalesce;
use rs_matter::utils::storage::pooled::PooledBuffers;
use rs_matter::utils::sync::{blocking::raw::StdRawMutex, Notification};
use rs_matter::MATTER_PORT;
use rs_matter::utils::sync::blocking::raw::StdRawMutex;
use rs_matter::{clusters, test_device};
use rs_matter::{devices, MATTER_PORT};

#[path = "../common/comm.rs"]
mod comm;
#[path = "../common/mdns.rs"]
mod mdns;

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

// A notification when the Wifi is setup, so that Matter over UDP can start
let wifi_complete = Notification::new();
// A storage for the Wifi networks
let networks = WifiNetworks::<3, NoopRawMutex>::new();

// The network controller
// We would be using a "fake" network controller that does not actually manage any Wifi networks
let net_ctl_state = NetCtlState::new_with_mutex::<NoopRawMutex>();
let net_ctl =
NetCtlWithStatusImpl::new(&net_ctl_state, NoopWirelessNetCtl::new(NetworkType::Wifi));

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

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

psm.load(&dir, &matter)?;
psm.load_networks(&dir, &networks)?;

let mut persist = pin!(psm.run(dir, &matter));
let mut persist = pin!(psm.run_with_networks(dir, &matter, Some(&networks)));

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

let mut transport = pin!(matter.run(&btp, &btp, DiscoveryCapabilities::BLE));

let mut wifi_complete_task = pin!(async {
wifi_complete.wait().await;
warn!(
"Wifi setup complete, giving 4 seconds to BTP to finish any outstanding messages"
);

Timer::after(Duration::from_secs(4)).await;

let mut wifi_prov_task = pin!(async {
NetCtlState::wait_prov_ready(&net_ctl_state, &btp).await;
Ok(())
});

// Combine all async tasks in a single one
let all = select4(
&mut transport,
&mut bluetooth,
select(&mut wifi_complete_task, &mut persist).coalesce(),
select(&mut wifi_prov_task, &mut persist).coalesce(),
select(&mut respond, &mut device).coalesce(),
);

Expand Down Expand Up @@ -193,55 +195,49 @@ fn main() -> Result<(), Error> {
const NODE: Node<'static> = Node {
id: 0,
endpoints: &[
root_endpoint::endpoint(0, root_endpoint::OperNwType::Wifi),
root_endpoint::root_endpoint(NetworkType::Wifi),
Endpoint {
id: 1,
device_types: &[DEV_TYPE_ON_OFF_LIGHT],
clusters: &[descriptor::CLUSTER, on_off::OnOffHandler::CLUSTER],
device_types: devices!(DEV_TYPE_ON_OFF_LIGHT),
clusters: clusters!(desc::DescHandler::CLUSTER, on_off::OnOffHandler::CLUSTER),
},
],
};

/// The Data Model handler + meta-data for our Matter device.
/// The handler is the root endpoint 0 handler plus the on-off handler and its descriptor.
fn dm_handler<'a>(
fn dm_handler<'a, N>(
matter: &'a Matter<'a>,
on_off: &'a on_off::OnOffHandler,
nw_setup_complete: &'a Notification<NoopRawMutex>,
) -> impl Metadata + NonBlockingHandler + 'a {
net_ctl: &'a N,
networks: &'a dyn Networks,
) -> impl AsyncMetadata + AsyncHandler + 'a
where
N: NetCtl + NetCtlStatus + WifiDiag,
{
(
NODE,
root_endpoint::handler(
0,
Async(WifiNwCommCluster::new(
Dataver::new_rand(matter.rand()),
nw_setup_complete,
)),
wifi_nw_diagnostics::ID,
Async(WifiNwDiagCluster::new(
Dataver::new_rand(matter.rand()),
WifiNwDiagData {
bssid: [0; 6],
security_type: WiFiSecurity::Wpa2Personal,
wifi_version: WiFiVersion::B,
channel_number: 20,
rssi: 0,
},
)),
&false,
root_endpoint::with_wifi(
&(),
&UnixNetifs,
net_ctl,
networks,
matter.rand(),
)
.chain(
1,
descriptor::ID,
Async(descriptor::DescriptorCluster::new(Dataver::new_rand(
root_endpoint::with_sys(
&false,
matter.rand(),
))),
)
.chain(
1,
on_off::OnOffHandler::CLUSTER.id,
Async(on_off::HandlerAdaptor(on_off)),
EmptyHandler
.chain(
1,
desc::DescHandler::CLUSTER.id,
Async(desc::DescHandler::new(Dataver::new_rand(matter.rand())).adapt()),
)
.chain(
1,
on_off::OnOffHandler::CLUSTER.id,
Async(on_off::HandlerAdaptor(on_off)),
),
),
),
)
}
Loading