Skip to content

Commit

Permalink
Logging; fix a busyloop bug in Gatt impl
Browse files Browse the repository at this point in the history
  • Loading branch information
ivmarkov committed Apr 30, 2024
1 parent c7145c5 commit fc0070f
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 15 deletions.
4 changes: 2 additions & 2 deletions .github/configs/sdkconfig.defaults
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# rs-matter needs a large stack size
CONFIG_ESP_MAIN_TASK_STACK_SIZE=20000
# Increase a bit these stack sizes as they are too low by default
CONFIG_ESP_MAIN_TASK_STACK_SIZE=4096
CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE=8192
#CONFIG_PTHREAD_TASK_STACK_SIZE_DEFAULT=8192

Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use esp_idf_matter::{Error, MatterStack, WifiBle};

use esp_idf_svc::eventloop::EspSystemEventLoop;
use esp_idf_svc::hal::peripherals::Peripherals;
use esp_idf_svc::log::EspLogger;
use esp_idf_svc::nvs::EspDefaultNvsPartition;
use esp_idf_svc::timer::EspTaskTimerService;

Expand All @@ -55,6 +56,22 @@ use static_cell::ConstStaticCell;
mod dev_att;

fn main() -> Result<(), Error> {
EspLogger::initialize_default();

info!("Starting...");

// Run in a higher-prio thread to avoid issues with `async-io` getting
// confused by the low priority of the ESP IDF main task
// Also allocate a large stack as `rs-matter` futures do occupy quite some space
let thread = std::thread::Builder::new()
.stack_size(40 * 1024)
.spawn(run)
.unwrap();

thread.join().unwrap()
}

fn run() -> Result<(), Error> {
// Take the Matter stack (can be done only once),
// as we'll run it in this thread
let stack = MATTER_STACK.take();
Expand Down
17 changes: 17 additions & 0 deletions examples/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use esp_idf_matter::{Error, MatterStack, WifiBle};

use esp_idf_svc::eventloop::EspSystemEventLoop;
use esp_idf_svc::hal::peripherals::Peripherals;
use esp_idf_svc::log::EspLogger;
use esp_idf_svc::nvs::EspDefaultNvsPartition;
use esp_idf_svc::timer::EspTaskTimerService;

Expand All @@ -34,6 +35,22 @@ use static_cell::ConstStaticCell;
mod dev_att;

fn main() -> Result<(), Error> {
EspLogger::initialize_default();

info!("Starting...");

// Run in a higher-prio thread to avoid issues with `async-io` getting
// confused by the low priority of the ESP IDF main task
// Also allocate a large stack as `rs-matter` futures do occupy quite some space
let thread = std::thread::Builder::new()
.stack_size(40 * 1024)
.spawn(run)
.unwrap();

thread.join().unwrap()
}

fn run() -> Result<(), Error> {
// Take the Matter stack (can be done only once),
// as we'll run it in this thread
let stack = MATTER_STACK.take();
Expand Down
8 changes: 6 additions & 2 deletions src/ble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use esp_idf_svc::bt::{BdAddr, BleEnabled, BtDriver, BtStatus, BtUuid};
use esp_idf_svc::hal::task::embassy_sync::EspRawMutex;
use esp_idf_svc::sys::{EspError, ESP_FAIL};

use log::warn;
use log::{info, warn};

use rs_matter::error::ErrorCode;
use rs_matter::transport::network::btp::{
Expand Down Expand Up @@ -136,6 +136,8 @@ where
let gap = EspBleGap::new(self.driver)?;
let gatts = EspGatts::new(self.driver)?;

info!("BLE Gap and Gatts initialized");

unsafe {
gap.subscribe_nonstatic(|event| {
let ctx = GattExecContext::new(&gap, &gatts, self.context);
Expand All @@ -161,8 +163,10 @@ where
})?;
}

info!("BLE Gap and Gatts subscriptions initialized");

loop {
let mut ind = self.context.ind.lock_if(|ind| ind.data.is_empty()).await;
let mut ind = self.context.ind.lock_if(|ind| !ind.data.is_empty()).await;

let ctx = GattExecContext::new(&gap, &gatts, self.context);

Expand Down
36 changes: 25 additions & 11 deletions src/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,20 +168,22 @@ where
P: NvsPartitionId,
M: RawMutex,
{
let mut psm_buf = self
.psm_buffer
.get()
.await
.ok_or(ErrorCode::ResourceExhausted)?;
psm_buf.resize_default(4096).unwrap();
if false {
let mut psm_buf = self
.psm_buffer
.get()
.await
.ok_or(ErrorCode::ResourceExhausted)?;
psm_buf.resize_default(4096).unwrap();

let nvs = EspNvs::new(nvs, "rs_matter", true)?;
let nvs = EspNvs::new(nvs, "rs_matter", true)?;

let mut psm = nvs::Psm::new(self.matter(), network, nvs, &mut psm_buf)?;
let mut psm = nvs::Psm::new(self.matter(), network, nvs, &mut psm_buf)?;

psm.run().await

// core::future::pending().await
psm.run().await
} else {
core::future::pending().await
}
}

async fn run_responder<H>(&self, handler: H) -> Result<(), Error>
Expand Down Expand Up @@ -307,6 +309,8 @@ mod wifible {
use esp_idf_svc::timer::EspTaskTimerService;
use esp_idf_svc::wifi::{AsyncWifi, EspWifi};

use log::info;

use rs_matter::acl::AclMgr;
use rs_matter::data_model::cluster_basic_information::{
self, BasicInfoCluster, BasicInfoConfig,
Expand Down Expand Up @@ -397,6 +401,8 @@ mod wifible {
where
T: AsyncHandler + AsyncMetadata,
{
info!("Running Matter in operating mode (Wifi)");

let wifi = Mutex::<NoopRawMutex, _>::new(AsyncWifi::wrap(
wifi,
sysloop.clone(),
Expand All @@ -422,6 +428,8 @@ mod wifible {
T: AsyncHandler + AsyncMetadata,
M: BleEnabled,
{
info!("Running Matter in commissioning mode (BLE)");

let peripheral = BtpGattPeripheral::new(bt, &self.network.btp_gatt_context);

let btp = Btp::new(peripheral, &self.network.btp_context);
Expand Down Expand Up @@ -457,10 +465,14 @@ mod wifible {
where
T: AsyncHandler + AsyncMetadata,
{
info!("Matter Stack memory: {}B", core::mem::size_of_val(self));

loop {
if !self.is_commissioned(nvs.clone()).await? {
let bt = BtDriver::<Ble>::new(&mut modem, Some(nvs.clone()))?;

info!("BLE driver initialized");

let mut main =
pin!(self.commission(nvs.clone(), &bt, dev_comm.clone(), &handler));
let mut wait_network_connect =
Expand All @@ -473,6 +485,8 @@ mod wifible {

let mut wifi = EspWifi::new(&mut modem, sysloop.clone(), Some(nvs.clone()))?;

info!("Wifi driver initialized");

self.operate(
sysloop.clone(),
timer_service.clone(),
Expand Down

0 comments on commit fc0070f

Please sign in to comment.