From 86a1b5ce7e64888bdb45f623ab648a7819e506c8 Mon Sep 17 00:00:00 2001 From: ivmarkov Date: Mon, 24 Apr 2023 07:20:49 +0000 Subject: [PATCH] Fix several no_std incompatibilities --- matter/Cargo.toml | 7 ++++--- matter/src/acl.rs | 2 +- matter/src/cert/mod.rs | 12 ++++++++++-- matter/src/error.rs | 1 + matter/src/transport/dedup.rs | 10 ++++++---- matter/src/transport/exchange.rs | 14 +++++++------- 6 files changed, 29 insertions(+), 17 deletions(-) diff --git a/matter/Cargo.toml b/matter/Cargo.toml index 2769010..9b68ced 100644 --- a/matter/Cargo.toml +++ b/matter/Cargo.toml @@ -16,10 +16,11 @@ path = "src/lib.rs" [features] default = ["std", "crypto_mbedtls"] -std = [] +std = ["alloc"] +alloc = [] nightly = [] crypto_openssl = ["openssl", "foreign-types", "hmac", "sha2"] -crypto_mbedtls = ["mbedtls"] +crypto_mbedtls = ["mbedtls", "alloc"] crypto_esp_mbedtls = ["esp-idf-sys"] crypto_rustcrypto = ["sha2", "hmac", "pbkdf2", "hkdf", "aes", "ccm", "p256", "elliptic-curve", "crypto-bigint", "x509-cert"] @@ -33,6 +34,7 @@ generic-array = "0.14.6" num = "0.4" num-derive = "0.3.3" num-traits = "0.2.15" +strum = { version = "0.24", features = ["derive"], default-features = false, no-default-feature = true } log = { version = "0.4.17", features = ["max_level_debug", "release_max_level_debug"] } env_logger = { version = "0.10.0", default-features = false, features = [] } rand = "0.8.5" @@ -44,7 +46,6 @@ owning_ref = "0.4.1" safemem = "0.3.3" chrono = { version = "0.4.23", default-features = false, features = ["clock", "std"] } async-channel = "1.8" -strum = { version = "0.24", features = ["derive"], no-default-feature = true } # crypto openssl = { git = "https://github.com/sfackler/rust-openssl", optional = true } diff --git a/matter/src/acl.rs b/matter/src/acl.rs index 2bfd0d6..d73ce47 100644 --- a/matter/src/acl.rs +++ b/matter/src/acl.rs @@ -143,7 +143,7 @@ impl AccessorSubjects { } impl Display for AccessorSubjects { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::result::Result<(), core::fmt::Error> { write!(f, "[")?; for i in self.0 { if is_noc_cat(i) { diff --git a/matter/src/cert/mod.rs b/matter/src/cert/mod.rs index 757a9d6..918737a 100644 --- a/matter/src/cert/mod.rs +++ b/matter/src/cert/mod.rs @@ -519,8 +519,16 @@ fn encode_dn_value( w.oid(name, oid)?; match value { DistNameValue::Uint(v) => match expected_len { - Some(IntToStringLen::Len16) => w.utf8str("", format!("{:016X}", v).as_str())?, - Some(IntToStringLen::Len8) => w.utf8str("", format!("{:08X}", v).as_str())?, + Some(IntToStringLen::Len16) => { + let mut string = heapless::String::<32>::new(); + write!(&mut string, "{:016X}", v).unwrap(); + w.utf8str("", &string)? + } + Some(IntToStringLen::Len8) => { + let mut string = heapless::String::<32>::new(); + write!(&mut string, "{:08X}", v).unwrap(); + w.utf8str("", &string)? + } _ => { error!("Invalid encoding"); return Err(Error::Invalid); diff --git a/matter/src/error.rs b/matter/src/error.rs index 22a04e4..04d55b3 100644 --- a/matter/src/error.rs +++ b/matter/src/error.rs @@ -182,4 +182,5 @@ impl fmt::Display for Error { } } +#[cfg(feature = "std")] impl std::error::Error for Error {} diff --git a/matter/src/transport/dedup.rs b/matter/src/transport/dedup.rs index 981bda7..f2c382e 100644 --- a/matter/src/transport/dedup.rs +++ b/matter/src/transport/dedup.rs @@ -86,6 +86,8 @@ impl RxCtrState { #[cfg(test)] mod tests { + use log::info; + use super::RxCtrState; const ENCRYPTED: bool = true; @@ -194,10 +196,10 @@ mod tests { #[test] fn unencrypted_device_reboot() { - println!("Sub 65532 is {:?}", 1_u16.overflowing_sub(65532)); - println!("Sub 65535 is {:?}", 1_u16.overflowing_sub(65535)); - println!("Sub 11-13 is {:?}", 11_u32.wrapping_sub(13_u32) as i32); - println!("Sub regular is {:?}", 2000_u16.overflowing_sub(1998)); + info!("Sub 65532 is {:?}", 1_u16.overflowing_sub(65532)); + info!("Sub 65535 is {:?}", 1_u16.overflowing_sub(65535)); + info!("Sub 11-13 is {:?}", 11_u32.wrapping_sub(13_u32) as i32); + info!("Sub regular is {:?}", 2000_u16.overflowing_sub(1998)); let mut s = RxCtrState::new(20010); assert_ndup(s.recv(20011, NOT_ENCRYPTED)); diff --git a/matter/src/transport/exchange.rs b/matter/src/transport/exchange.rs index c28a5b2..053bf79 100644 --- a/matter/src/transport/exchange.rs +++ b/matter/src/transport/exchange.rs @@ -457,13 +457,9 @@ mod tests { error::Error, transport::{ network::Address, - packet::{Packet, MAX_TX_BUF_SIZE}, - session::{CloneData, SessionMode, MAX_SESSIONS}, - }, - utils::{ - epoch::{dummy_epoch, sys_epoch}, - rand::dummy_rand, + session::{CloneData, SessionMode}, }, + utils::{epoch::dummy_epoch, rand::dummy_rand}, }; use super::{ExchangeMgr, Role}; @@ -526,13 +522,17 @@ mod tests { } } + #[cfg(feature = "std")] #[test] /// We purposefuly overflow the sessions /// and when the overflow happens, we confirm that /// - The sessions are evicted in LRU /// - The exchanges associated with those sessions are evicted too fn test_sess_evict() { - let mut mgr = ExchangeMgr::new(sys_epoch, dummy_rand); + use crate::transport::packet::{Packet, MAX_TX_BUF_SIZE}; + use crate::transport::session::MAX_SESSIONS; + + let mut mgr = ExchangeMgr::new(crate::utils::epoch::sys_epoch, dummy_rand); fill_sessions(&mut mgr, MAX_SESSIONS + 1); // Sessions are now full from local session id 1 to 16