Skip to content

Commit e8babed

Browse files
committed
Support for ESP-IDF build
1 parent 488ef5b commit e8babed

File tree

6 files changed

+66
-23
lines changed

6 files changed

+66
-23
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[workspace]
2-
members = ["matter", "matter_macro_derive", "tools/tlv_tool"]
2+
members = ["matter", "matter_macro_derive"]
33

44
exclude = ["examples/*"]
55

README.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,31 @@ Building the library:
4747
$ cargo build
4848
```
4949

50-
Building the example:
50+
Building and running the example (Linux, MacOS X):
5151

5252
```
53-
$ RUST_LOG="matter" cargo run --example onoff_light
53+
$ cargo run --example onoff_light
5454
```
5555

56-
With the chip-tool (the current tool for testing Matter) use the Ethernet commissioning mechanism:
56+
Building the example (Espressif's ESP-IDF):
57+
* Install all build prerequisites described [here](https://github.com/esp-rs/esp-idf-template#prerequisites)
58+
* Build with the following command line:
59+
```
60+
export MCU=esp32; export CARGO_TARGET_XTENSA_ESP32_ESPIDF_LINKER=ldproxy; export RUSTFLAGS="-C default-linker-libraries"; export WIFI_SSID=ssid;export WIFI_PASS=pass; cargo build --example onoff_light --no-default-features --features std,crypto_rustcrypto --target xtensa-esp32-espidf -Zbuild-std=std,panic_abort
61+
```
62+
* If you are building for a different Espressif MCU, change the `MCU` variable, the `xtensa-esp32-espidf` target and the name of the `CARGO_TARGET_<esp-idf-target-uppercase>_LINKER` variable to match your MCU and its Rust target. Available Espressif MCUs and targets are:
63+
* esp32 / xtensa-esp32-espidf
64+
* esp32s2 / xtensa-esp32s2-espidf
65+
* esp32s3 / xtensa-esp32s3-espidf
66+
* esp32c3 / riscv32imc-esp-espidf
67+
* esp32c5 / riscv32imc-esp-espidf
68+
* esp32c6 / risxcv32imac-esp-espidf
69+
* Put in `WIFI_SSID` / `WIFI_PASS` the SSID & password for your wireless router
70+
* Flash using the `espflash` utility described in the build prerequsites' link above
71+
72+
## Test
73+
74+
With the `chip-tool` (the current tool for testing Matter) use the Ethernet commissioning mechanism:
5775

5876
```
5977
$ chip-tool pairing code 12344321 <Pairing-Code>

examples/onoff_light/src/main.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ use matter::data_model::system_model::descriptor;
3131
use matter::error::Error;
3232
use matter::interaction_model::core::InteractionModel;
3333
use matter::mdns::{DefaultMdns, DefaultMdnsRunner};
34-
use matter::persist;
3534
use matter::secure_channel::spake2p::VerifierData;
3635
use matter::transport::network::{Address, IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
3736
use matter::transport::{
@@ -50,7 +49,6 @@ fn main() -> Result<(), Error> {
5049
.unwrap();
5150

5251
thread.join().unwrap()
53-
// run()
5452
}
5553

5654
#[cfg(not(feature = "std"))]
@@ -105,17 +103,21 @@ fn run() -> Result<(), Error> {
105103
let psm_path = std::env::temp_dir().join("matter-iot");
106104
info!("Persisting from/to {}", psm_path.display());
107105

108-
let psm = persist::FilePsm::new(psm_path)?;
106+
#[cfg(all(feature = "std", not(target_os = "espidf")))]
107+
let psm = matter::persist::FilePsm::new(psm_path)?;
109108

110109
let mut buf = [0; 4096];
111110
let buf = &mut buf;
112111

113-
if let Some(data) = psm.load("acls", buf)? {
114-
matter.load_acls(data)?;
115-
}
112+
#[cfg(all(feature = "std", not(target_os = "espidf")))]
113+
{
114+
if let Some(data) = psm.load("acls", buf)? {
115+
matter.load_acls(data)?;
116+
}
116117

117-
if let Some(data) = psm.load("fabrics", buf)? {
118-
matter.load_fabrics(data)?;
118+
if let Some(data) = psm.load("fabrics", buf)? {
119+
matter.load_fabrics(data)?;
120+
}
119121
}
120122

121123
let mut transport = Transport::new(&matter);
@@ -180,12 +182,15 @@ fn run() -> Result<(), Error> {
180182
}
181183
}
182184

183-
if let Some(data) = transport.matter().store_fabrics(buf)? {
184-
psm.store("fabrics", data)?;
185-
}
185+
#[cfg(all(feature = "std", not(target_os = "espidf")))]
186+
{
187+
if let Some(data) = transport.matter().store_fabrics(buf)? {
188+
psm.store("fabrics", data)?;
189+
}
186190

187-
if let Some(data) = transport.matter().store_acls(buf)? {
188-
psm.store("acls", data)?;
191+
if let Some(data) = transport.matter().store_acls(buf)? {
192+
psm.store("acls", data)?;
193+
}
189194
}
190195
}
191196

matter/Cargo.toml

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ path = "src/lib.rs"
1616

1717
[features]
1818
default = ["os", "crypto_rustcrypto"]
19-
#default = ["crypto_rustcrypto"]
2019
os = ["std", "backtrace", "env_logger", "nix", "critical-section/std", "embassy-sync/std", "embassy-time/std"]
2120
std = ["alloc", "rand", "qrcode", "async-io", "smol", "esp-idf-sys/std"]
2221
backtrace = []
@@ -51,7 +50,6 @@ domain = { version = "0.7.2", default_features = false, features = ["heapless"]
5150
# STD-only dependencies
5251
rand = { version = "0.8.5", optional = true }
5352
qrcode = { version = "0.12", default-features = false, optional = true } # Print QR code
54-
astro-dnssd = { version = "0.3", optional = true } # On Linux needs avahi-compat-libdns_sd, i.e. on Ubuntu/Debian do `sudo apt-get install libavahi-compat-libdnssd-dev`
5553
smol = { version = "1.2", optional = true } # =1.2 for compatibility with ESP IDF
5654
async-io = { version = "=1.12", optional = true } # =1.2 for compatibility with ESP IDF
5755

@@ -72,18 +70,27 @@ crypto-bigint = { version = "0.4", default-features = false, optional = true }
7270
rand_core = { version = "0.6", default-features = false, optional = true }
7371
x509-cert = { version = "0.2.0", default-features = false, features = ["pem"], optional = true } # TODO: requires `alloc`
7472

73+
[target.'cfg(target_os = "macos")'.dependencies]
74+
astro-dnssd = { version = "0.3" }
75+
7576
[target.'cfg(not(target_os = "espidf"))'.dependencies]
7677
mbedtls = { git = "https://github.com/fortanix/rust-mbedtls", optional = true }
7778
env_logger = { version = "0.10.0", optional = true }
7879
nix = { version = "0.26", features = ["net"], optional = true }
7980

8081
[target.'cfg(target_os = "espidf")'.dependencies]
81-
esp-idf-sys = { version = "0.33", default-features = false, features = ["native"] }
82+
esp-idf-sys = { version = "0.33", default-features = false, features = ["native", "binstart"] }
83+
esp-idf-hal = { version = "0.41", features = ["embassy-sync", "critical-section"] }
84+
esp-idf-svc = { version = "0.46", features = ["embassy-time-driver"] }
85+
embedded-svc = "0.25"
86+
87+
[build-dependencies]
88+
embuild = "0.31.2"
8289

8390
[[example]]
8491
name = "onoff_light"
8592
path = "../examples/onoff_light/src/main.rs"
8693

87-
[[example]]
88-
name = "speaker"
89-
path = "../examples/speaker/src/main.rs"
94+
# [[example]]
95+
# name = "speaker"
96+
# path = "../examples/speaker/src/main.rs"

matter/build.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use std::env::var;
2+
3+
// Necessary because of this issue: https://github.com/rust-lang/cargo/issues/9641
4+
fn main() -> Result<(), Box<dyn std::error::Error>> {
5+
if var("TARGET").unwrap().ends_with("-espidf") {
6+
embuild::build::CfgArgs::output_propagated("ESP_IDF")?;
7+
embuild::build::LinkArgs::output_propagated("ESP_IDF")?;
8+
}
9+
10+
Ok(())
11+
}

matter/src/transport/udp.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ mod smol_udp {
5252
IpAddr::V6(ip_addr) => self.socket.join_multicast_v6(&ip_addr, 0)?,
5353
}
5454

55+
info!("Joining multicast on {:?}", ip_addr);
56+
5557
Ok(())
5658
}
5759

0 commit comments

Comments
 (0)