Skip to content

Commit

Permalink
Merge branch 'main' into lucas/mdns-cache-flag
Browse files Browse the repository at this point in the history
  • Loading branch information
andy31415 authored Jul 26, 2024
2 parents 924c1fd + 40e3820 commit f02e3fa
Show file tree
Hide file tree
Showing 96 changed files with 14,358 additions and 4,227 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci-tlv-tool.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ jobs:
toolchain: ${{ env.RUST_TOOLCHAIN }}
components: rustfmt, clippy, rust-src

- name: Install libdbus
run: sudo apt-get install -y libdbus-1-dev

- name: Checkout
uses: actions/checkout@v3

Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ jobs:
toolchain: ${{ env.RUST_TOOLCHAIN }}
components: rustfmt, clippy, rust-src

- name: Install libdbus
run: sudo apt-get install -y libdbus-1-dev

- name: Checkout
uses: actions/checkout@v3

Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/publish-dry-run.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ jobs:
toolchain: ${{ env.RUST_TOOLCHAIN }}
components: rustfmt, clippy, rust-src

- name: Install libdbus
run: sudo apt-get install -y libdbus-1-dev

- name: Checkout
uses: actions/checkout@v3

Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ jobs:
toolchain: ${{ env.RUST_TOOLCHAIN }}
components: rustfmt, clippy, rust-src

- name: Install libdbus
run: sudo apt-get install -y libdbus-1-dev

- name: Checkout
uses: actions/checkout@v3

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ $ cargo run --example onoff_light --features async-io

## Test

With the `chip-tool` (the current tool for testing Matter) use the Ethernet commissioning mechanism:
With the [`chip-tool` (the current tool for testing Matter)](https://github.com/project-chip/connectedhomeip/blob/master/examples/chip-tool/README.md) use the Ethernet commissioning mechanism:

```
$ chip-tool pairing code 12344321 <Pairing-Code>
Expand Down
616 changes: 616 additions & 0 deletions docs/RFC_Transport_Exchange_Layer_Improvements.md

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions examples/Cargo.toml

This file was deleted.

2 changes: 1 addition & 1 deletion examples/onoff_light/src/dev_att.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use rs_matter::error::{Error, ErrorCode};
pub struct HardCodedDevAtt {}

impl HardCodedDevAtt {
pub fn new() -> Self {
pub const fn new() -> Self {
Self {}
}
}
Expand Down
128 changes: 85 additions & 43 deletions examples/onoff_light/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,32 @@
* limitations under the License.
*/

use core::borrow::Borrow;
use core::pin::pin;
use std::net::UdpSocket;

use embassy_futures::select::select3;
use embassy_futures::select::{select, select4};

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

use rs_matter::core::{CommissioningData, Matter};
use rs_matter::data_model::cluster_basic_information::BasicInfoConfig;
use rs_matter::data_model::cluster_on_off;
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::root_endpoint;
use rs_matter::data_model::subscriptions::Subscriptions;
use rs_matter::data_model::system_model::descriptor;
use rs_matter::error::Error;
use rs_matter::mdns::MdnsService;
use rs_matter::persist::Psm;
use rs_matter::respond::DefaultResponder;
use rs_matter::secure_channel::spake2p::VerifierData;
use rs_matter::transport::core::{PacketBuffers, MATTER_SOCKET_BIND_ADDR};
use rs_matter::utils::select::EitherUnwrap;
use rs_matter::transport::core::MATTER_SOCKET_BIND_ADDR;
use rs_matter::utils::buf::PooledBuffers;
use rs_matter::utils::select::Coalesce;
use rs_matter::MATTER_PORT;

mod dev_att;
Expand All @@ -49,7 +54,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(180 * 1024)
.stack_size(95 * 1024)
.spawn(run)
.unwrap();

Expand All @@ -62,9 +67,9 @@ fn run() -> Result<(), Error> {
);

info!(
"Matter memory: Matter={}, PacketBuffers={}",
"Matter memory: Matter={}B, IM Buffers={}B",
core::mem::size_of::<Matter>(),
core::mem::size_of::<PacketBuffers>(),
core::mem::size_of::<PooledBuffers<10, NoopRawMutex, IMBuffer>>()
);

let dev_det = BasicInfoConfig {
Expand All @@ -81,63 +86,101 @@ fn run() -> Result<(), Error> {

let dev_att = dev_att::HardCodedDevAtt::new();

// NOTE:
// For `no_std` environments, provide your own epoch and rand functions here
let epoch = rs_matter::utils::epoch::sys_epoch;
let rand = rs_matter::utils::rand::sys_rand;

let matter = Matter::new(
// vid/pid should match those in the DAC
&dev_det,
&dev_att,
// NOTE:
// For `no_std` environments, provide your own epoch and rand functions here
MdnsService::Builtin,
epoch,
rand,
rs_matter::utils::epoch::sys_epoch,
rs_matter::utils::rand::sys_rand,
MATTER_PORT,
);

matter.initialize_transport_buffers()?;

info!("Matter initialized");

let handler = HandlerCompat(handler(&matter));
let buffers = PooledBuffers::<10, NoopRawMutex, _>::new(0);

info!("IM buffers initialized");

let mut mdns = pin!(run_mdns(&matter));

let on_off = cluster_on_off::OnOffCluster::new(Dataver::new_rand(matter.rand()));

let subscriptions = Subscriptions::<3>::new();

// Assemble our Data Model handler by composing the predefined Root Endpoint handler with our custom On/Off clusters
let dm_handler = HandlerCompat(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"
let responder = DefaultResponder::new(&matter, &buffers, &subscriptions, dm_handler);
info!(
"Responder memory: Responder={}B, Runner={}B",
core::mem::size_of_val(&responder),
core::mem::size_of_val(&responder.run::<4, 4>())
);

// Run the responder with up to 4 handlers (i.e. 4 exchanges can be handled simultenously)
// Clients trying to open more exchanges than the ones currently running will get "I'm busy, please try again later"
let mut respond = pin!(responder.run::<4, 4>());

// This is a sample code that simulates state changes triggered by the HAL
// Changes will be properly communicated to the Matter controllers and other Matter apps (i.e. Google Home, Alexa), thanks to subscriptions
let mut device = pin!(async {
loop {
Timer::after(Duration::from_secs(5)).await;

on_off.set(!on_off.get());
subscriptions.notify_changed();

info!("Lamp toggled");
}
});

// NOTE:
// When using a custom UDP stack (e.g. for `no_std` environments), replace with a UDP socket bind for your custom UDP stack
// The returned socket should be splittable into two halves, where each half implements `UdpSend` and `UdpReceive` respectively
let socket = async_io::Async::<UdpSocket>::bind(MATTER_SOCKET_BIND_ADDR)?;

let mut packet_buffers = PacketBuffers::new();
let mut runner = pin!(matter.run(
// Run the Matter and mDNS transports
let mut transport = pin!(matter.run(
&socket,
&socket,
&mut packet_buffers,
CommissioningData {
// TODO: Hard-coded for now
verifier: VerifierData::new_with_pw(123456, *matter.borrow()),
discriminator: 250,
},
&handler,
Some((
CommissioningData {
// TODO: Hard-coded for now
verifier: VerifierData::new_with_pw(123456, matter.rand()),
discriminator: 250,
},
Default::default(),
)),
));

let mut mdns_runner = pin!(run_mdns(&matter));

// NOTE:
// Replace with your own persister for e.g. `no_std` environments
let mut psm = Psm::new(&matter, std::env::temp_dir().join("rs-matter"))?;
let mut psm_runner = pin!(psm.run());

let runner = select3(&mut runner, &mut mdns_runner, &mut psm_runner);
let mut persist = pin!(psm.run());

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

// NOTE:
// Replace with a different executor for e.g. `no_std` environments
futures_lite::future::block_on(runner).unwrap()?;

Ok(())
futures_lite::future::block_on(all.coalesce())
}

const NODE: Node<'static> = Node {
id: 0,
endpoints: &[
root_endpoint::endpoint(0),
root_endpoint::endpoint(0, root_endpoint::OperNwType::Ethernet),
Endpoint {
id: 1,
device_type: DEV_TYPE_ON_OFF_LIGHT,
Expand All @@ -146,20 +189,19 @@ const NODE: Node<'static> = Node {
],
};

fn handler<'a>(matter: &'a Matter<'a>) -> impl Metadata + NonBlockingHandler + 'a {
fn dm_handler<'a>(
matter: &'a Matter<'a>,
on_off: &'a cluster_on_off::OnOffCluster,
) -> impl Metadata + NonBlockingHandler + 'a {
(
NODE,
root_endpoint::handler(0, matter)
root_endpoint::eth_handler(0, matter.rand())
.chain(
1,
descriptor::ID,
descriptor::DescriptorCluster::new(*matter.borrow()),
descriptor::DescriptorCluster::new(Dataver::new_rand(matter.rand())),
)
.chain(
1,
cluster_on_off::ID,
cluster_on_off::OnOffCluster::new(*matter.borrow()),
),
.chain(1, cluster_on_off::ID, on_off),
)
}

Expand Down Expand Up @@ -249,7 +291,7 @@ async fn run_mdns(matter: &Matter<'_>) -> Result<(), Error> {
.run_builtin_mdns(
&socket,
&socket,
Host {
&Host {
id: 0,
hostname: "rs-matter-demo",
ip: ipv4_addr.octets(),
Expand Down
Loading

0 comments on commit f02e3fa

Please sign in to comment.