From c7145c58b0aff6d5da5525ac422bbe9c54337d6e Mon Sep 17 00:00:00 2001 From: ivmarkov Date: Tue, 30 Apr 2024 18:39:40 +0000 Subject: [PATCH] Optimize memory and other small fixes --- .github/configs/sdkconfig.defaults | 32 ++++++++---------------------- .github/workflows/ci.yml | 2 +- Cargo.toml | 14 ++++++++++--- README.md | 2 +- src/ble.rs | 3 ++- src/stack.rs | 25 +++++++++++++++-------- 6 files changed, 40 insertions(+), 38 deletions(-) diff --git a/.github/configs/sdkconfig.defaults b/.github/configs/sdkconfig.defaults index 771bd42..615a52f 100644 --- a/.github/configs/sdkconfig.defaults +++ b/.github/configs/sdkconfig.defaults @@ -1,35 +1,19 @@ -CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=y -CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=y - -# Examples often require a larger than the default stack size for the main thread and for the sysloop event task. +# rs-matter needs a large stack size CONFIG_ESP_MAIN_TASK_STACK_SIZE=20000 CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE=8192 -CONFIG_PTHREAD_TASK_STACK_SIZE_DEFAULT=8192 +#CONFIG_PTHREAD_TASK_STACK_SIZE_DEFAULT=8192 # Go figure... CONFIG_FREERTOS_IDLE_TASK_STACKSIZE=4096 -# Enable WS support -CONFIG_HTTPD_WS_SUPPORT=y - -# SPI Ethernet demo -CONFIG_ETH_SPI_ETHERNET_DM9051=y -CONFIG_ETH_SPI_ETHERNET_W5500=y -CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL=y - -## Uncomment to enable Classic BT +# BT CONFIG_BT_ENABLED=y CONFIG_BT_BLUEDROID_ENABLED=y -CONFIG_BT_CLASSIC_ENABLED=y -CONFIG_BTDM_CTRL_MODE_BLE_ONLY=n +CONFIG_BT_CLASSIC_ENABLED=n +CONFIG_BTDM_CTRL_MODE_BLE_ONLY=y CONFIG_BTDM_CTRL_MODE_BR_EDR_ONLY=n CONFIG_BTDM_CTRL_MODE_BTDM=n -CONFIG_BT_A2DP_ENABLE=y -CONFIG_BT_HFP_ENABLE=y -CONFIG_BT_HFP_CLIENT_ENABLE=y -CONFIG_BT_HFP_AUDIO_DATA_PATH_HCI=y +CONFIG_BT_A2DP_ENABLE=n CONFIG_BT_BLE_42_FEATURES_SUPPORTED=y -CONFIG_BT_BLE_50_FEATURES_SUPPORTED=y - -# Support for TLS with a pre-shared key. -#CONFIG_ESP_TLS_PSK_VERIFICATION=y +CONFIG_BT_BLE_50_FEATURES_SUPPORTED=n +CONFIG_BT_BLE_DYNAMIC_ENV_MEMORY=n diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1649374..fe3951c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -82,4 +82,4 @@ jobs: ESP_IDF_SDKCONFIG_DEFAULTS: "${{ github.workspace }}/.github/configs/sdkconfig.defaults" RUSTFLAGS: "${{ startsWith(matrix.idf-version, 'v5') && '--cfg espidf_time64' || '' }} ${{ '-C default-linker-libraries' }}" if: matrix.target == 'riscv32imc-esp-espidf' - run: cargo build --examples --target ${{ matrix.target }} -Zbuild-std=std,panic_abort -Zbuild-std-features=panic_immediate_abort + run: cargo build --examples --features examples --target ${{ matrix.target }} -Zbuild-std=std,panic_abort -Zbuild-std-features=panic_immediate_abort diff --git a/Cargo.toml b/Cargo.toml index f3ae448..933f1f0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" resolver = "2" categories = ["embedded", "hardware-support"] keywords = ["matter", "embedded", "esp-idf", "esp32"] -description = "Implementation of the embedded-svc traits for ESP-IDF (Espressif's IoT Development Framework)" +description = "Run rs-matter on Espressif chips with ESP IDF" repository = "https://github.com/ivmarkov/esp-idf-matter" license = "MIT OR Apache-2.0" readme = "README.md" @@ -20,9 +20,17 @@ esp-idf-svc = { git = "https://github.com/esp-rs/esp-idf-svc", branch = "gatt" } rs-matter = { git = "https://github.com/ivmarkov/rs-matter", branch = "wifi" } rs-matter-macros = { git = "https://github.com/ivmarkov/rs-matter", branch = "wifi" } +[profile.release] +opt-level = "s" + +[profile.dev] +debug = true +opt-level = "z" + [features] default = ["std"] -std = ["async-io", "rs-matter/std", "rs-matter/async-io"] +std = ["async-io", "rs-matter/std", "rs-matter/async-io", "esp-idf-svc/std"] +examples = ["std", "esp-idf-svc/binstart", "esp-idf-svc/critical-section"] # Enable only when building the examples [dependencies] log = { version = "0.4", default-features = false } @@ -48,4 +56,4 @@ static_cell = "2.1" [[example]] name = "light" path = "examples/light.rs" -required-features = ["std"] +required-features = ["examples"] diff --git a/README.md b/README.md index 8f916c6..832d6ec 100644 --- a/README.md +++ b/README.md @@ -210,7 +210,7 @@ The examples could be built and flashed conveniently with [`cargo-espflash`](htt with `cargo-espflash`: ```sh -$ MCU=esp32c3 cargo espflash flash --target riscv32imc-esp-espidf --example light --monitor +$ MCU=esp32c3 cargo espflash flash --target riscv32imc-esp-espidf --example light --features examples --monitor ``` | MCU | "--target" | diff --git a/src/ble.rs b/src/ble.rs index b5381cc..0816fbb 100644 --- a/src/ble.rs +++ b/src/ble.rs @@ -37,6 +37,7 @@ use rs_matter::utils::ifmutex::IfMutex; use crate::error::Error; const MAX_CONNECTIONS: usize = MAX_BTP_SESSIONS; +const MAX_MTU_SIZE: usize = 512; #[derive(Debug, Clone)] struct Connection { @@ -59,7 +60,7 @@ struct State { #[derive(Debug, Clone)] struct IndBuffer { addr: BtAddr, - data: heapless::Vec, + data: heapless::Vec, } pub struct BtpGattContext { diff --git a/src/stack.rs b/src/stack.rs index 08ccb5f..3f735b3 100644 --- a/src/stack.rs +++ b/src/stack.rs @@ -29,6 +29,10 @@ use crate::multicast::{join_multicast_v4, join_multicast_v6}; use crate::netif::{get_ips, NetifAccess}; use crate::nvs; +const MAX_SUBSCRIPTIONS: usize = 3; +const MAX_IM_BUFFERS: usize = 10; +const PSM_BUFFER_SIZE: usize = 4096; + #[cfg(all( not(esp32h2), not(esp32s2), @@ -55,9 +59,9 @@ where T: Network, { matter: Matter<'a>, - buffers: PooledBuffers<10, NoopRawMutex, IMBuffer>, - psm_buffer: PooledBuffers<1, NoopRawMutex, heapless::Vec>, - subscriptions: Subscriptions<3>, + buffers: PooledBuffers, + psm_buffer: PooledBuffers<1, NoopRawMutex, heapless::Vec>, + subscriptions: Subscriptions, #[allow(unused)] network: T, } @@ -176,6 +180,8 @@ where let mut psm = nvs::Psm::new(self.matter(), network, nvs, &mut psm_buf)?; psm.run().await + + // core::future::pending().await } async fn run_responder(&self, handler: H) -> Result<(), Error> @@ -340,10 +346,12 @@ mod wifible { use crate::wifi::{comm, diag, WifiContext}; use crate::{MatterStack, Network}; + const MAX_WIFI_NETWORKS: usize = 2; + pub struct WifiBle { btp_context: BtpContext, btp_gatt_context: BtpGattContext, - wifi_context: WifiContext<3, NoopRawMutex>, + wifi_context: WifiContext, } impl WifiBle { @@ -374,7 +382,8 @@ mod wifible { } pub async fn is_commissioned(&self, _nvs: EspDefaultNvsPartition) -> Result { - todo!() + // TODO + Ok(false) } pub async fn operate<'d, T>( @@ -480,7 +489,7 @@ mod wifible { HandlerCompat>, HandlerCompat>, HandlerCompat>, - comm::WifiNwCommCluster<'a, 3, NoopRawMutex>, + comm::WifiNwCommCluster<'a, MAX_WIFI_NETWORKS, NoopRawMutex>, HandlerCompat>, HandlerCompat>, HandlerCompat>, @@ -505,7 +514,7 @@ mod wifible { fn handler<'a, T>( endpoint_id: u16, matter: &'a T, - networks: &'a WifiContext<3, NoopRawMutex>, + networks: &'a WifiContext, ) -> RootEndpointHandler<'a> where T: Borrow> @@ -546,7 +555,7 @@ mod wifible { mdns: &'a dyn Mdns, epoch: Epoch, rand: Rand, - networks: &'a WifiContext<3, NoopRawMutex>, + networks: &'a WifiContext, ) -> RootEndpointHandler<'a> { EmptyHandler .chain(