Skip to content

Commit

Permalink
Bugfixing, more logging
Browse files Browse the repository at this point in the history
  • Loading branch information
ivmarkov committed May 1, 2024
1 parent ec0505e commit 6fb14ee
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 49 deletions.
4 changes: 2 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::{info, warn};
use log::{debug, info, warn};

use rs_matter::error::ErrorCode;
use rs_matter::transport::network::btp::{
Expand Down Expand Up @@ -304,7 +304,7 @@ where
if let Some((gatts_if, conn_id, attr_handle)) = conn {
self.gatts.indicate(gatts_if, conn_id, attr_handle, data)?;

info!("Indicated {} bytes to {address}", data.len());
debug!("Indicated {} bytes to {address}", data.len());

Ok(true)
} else {
Expand Down
18 changes: 15 additions & 3 deletions src/netif.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@
use core::net::{Ipv4Addr, Ipv6Addr};
use core::pin::pin;

use alloc::sync::Arc;

use embassy_futures::select::select;
use embassy_time::{Duration, Timer};

use esp_idf_svc::eventloop::EspSystemEventLoop;
use esp_idf_svc::hal::task::embassy_sync::EspRawMutex;
use esp_idf_svc::handle::RawHandle;
use esp_idf_svc::netif::{EspNetif, IpEvent};
use esp_idf_svc::sys::{esp, esp_netif_get_ip6_linklocal, EspError, ESP_FAIL};

use log::info;

use rs_matter::utils::notification::Notification;

use crate::error::Error;

pub trait NetifAccess {
Expand All @@ -24,15 +29,22 @@ pub trait NetifAccess {
where
F: FnMut(&EspNetif) -> Result<Option<R>, Error>,
{
// TODO: Maybe wait on Wifi and Eth events as well
let mut subscription = sysloop.subscribe_async::<IpEvent>()?;
let notification = Arc::new(Notification::<EspRawMutex>::new());

let _subscription = {
let notification = notification.clone();

sysloop.subscribe::<IpEvent, _>(move |_| {
notification.notify();
})
}?;

loop {
if let Some(result) = self.with_netif(&mut f).await? {
break Ok(result);
}

let mut events = pin!(subscription.recv());
let mut events = pin!(notification.wait());
let mut timer = pin!(Timer::after(Duration::from_secs(5)));

select(&mut events, &mut timer).await;
Expand Down
9 changes: 9 additions & 0 deletions src/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,14 @@ where
N: NetifAccess,
{
loop {
info!("Waiting for the network to come up...");

let (ipv4, ipv6) = netif
.wait(sysloop.clone(), |netif| Ok(get_ips(netif).ok()))
.await?;

info!("Got network with IPs: IPv4={ipv4}, IPv6={ipv6}");

let socket = async_io::Async::<std::net::UdpSocket>::bind(MATTER_SOCKET_BIND_ADDR)?;

let mut main =
Expand All @@ -131,6 +135,8 @@ where
}));

select3(&mut main, &mut mdns, &mut down).coalesce().await?;

info!("Network change detected");
}
}

Expand Down Expand Up @@ -485,6 +491,9 @@ mod wifible {
.await?;
}

// Reset the matter transport to free up sessions and exchanges
self.matter().reset();

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

info!("Wifi driver initialized");
Expand Down
20 changes: 15 additions & 5 deletions src/wifi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use core::cell::RefCell;

use embassy_sync::blocking_mutex::{self, raw::RawMutex};

use log::info;

use rs_matter::data_model::sdm::nw_commissioning::NetworkCommissioningStatus;
use rs_matter::error::{Error, ErrorCode};
use rs_matter::tlv::{self, FromTLV, TLVList, TLVWriter, TagType, ToTLV};
Expand Down Expand Up @@ -38,12 +40,16 @@ impl<const N: usize> WifiState<N> {
if let Some(ssid) = self.connect_requested.take() {
let creds = self.networks.iter().find(|creds| creds.ssid == ssid);

if creds.is_some() {
return creds.cloned();
if let Some(creds) = creds {
info!("Trying with requested network first - SSID: {}", creds.ssid);

return Some(creds.clone());
}
}

if let Some(last_ssid) = last_ssid {
info!("Looking for network after the one with SSID: {}", last_ssid);

// Return the network positioned after the last one used

let mut networks = self.networks.iter();
Expand All @@ -54,13 +60,17 @@ impl<const N: usize> WifiState<N> {
}
}

let network = networks.next();
if network.is_some() {
return network.cloned();
let creds = networks.next();
if let Some(creds) = creds {
info!("Trying with next network - SSID: {}", creds.ssid);

return Some(creds.clone());
}
}

// Wrap over
info!("Wrapping over");

self.networks.first().cloned()
}

Expand Down
50 changes: 21 additions & 29 deletions src/wifi/comm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,11 @@ where
_req: &ScanNetworksRequest<'_>,
encoder: CmdDataEncoder<'_, '_, '_>,
) -> Result<(), Error> {
let mut tw = encoder.with_command(ResponseCommands::ScanNetworksResponse as _)?;
let writer = encoder.with_command(ResponseCommands::ScanNetworksResponse as _)?;

warn!("Scan network not supported");

Status::new(IMStatusCode::Busy, 0).to_tlv(&mut tw, TagType::Anonymous)?;
writer.set(Status::new(IMStatusCode::Busy, 0))?;

Ok(())
}
Expand All @@ -182,7 +182,7 @@ where
.iter()
.position(|conf| conf.ssid.as_str().as_bytes() == req.ssid.0);

let mut tw = encoder.with_command(ResponseCommands::NetworkConfigResponse as _)?;
let writer = encoder.with_command(ResponseCommands::NetworkConfigResponse as _)?;

if let Some(index) = index {
// Update
Expand All @@ -199,12 +199,11 @@ where

info!("Updated network with SSID {}", state.networks[index].ssid);

NetworkConfigResponse {
writer.set(NetworkConfigResponse {
status: NetworkCommissioningStatus::Success,
debug_text: None,
network_index: Some(index as _),
}
.to_tlv(&mut tw, TagType::Anonymous)?;
})?;
} else {
// Add
let network = WifiCredentials {
Expand All @@ -228,22 +227,20 @@ where
state.networks.last().unwrap().ssid
);

NetworkConfigResponse {
writer.set(NetworkConfigResponse {
status: NetworkCommissioningStatus::Success,
debug_text: None,
network_index: Some(state.networks.len() as _),
}
.to_tlv(&mut tw, TagType::Anonymous)?;
})?;
}
Err(network) => {
warn!("Adding network with SSID {} failed: too many", network.ssid);

NetworkConfigResponse {
writer.set(NetworkConfigResponse {
status: NetworkCommissioningStatus::BoundsExceeded,
debug_text: None,
network_index: None,
}
.to_tlv(&mut tw, TagType::Anonymous)?;
})?;
}
}
}
Expand All @@ -268,7 +265,7 @@ where
.iter()
.position(|conf| conf.ssid.as_str().as_bytes() == req.network_id.0);

let mut tw = encoder.with_command(ResponseCommands::NetworkConfigResponse as _)?;
let writer = encoder.with_command(ResponseCommands::NetworkConfigResponse as _)?;

if let Some(index) = index {
// Found
Expand All @@ -277,25 +274,23 @@ where

info!("Removed network with SSID {}", network.ssid);

NetworkConfigResponse {
writer.set(NetworkConfigResponse {
status: NetworkCommissioningStatus::Success,
debug_text: None,
network_index: Some(index as _),
}
.to_tlv(&mut tw, TagType::Anonymous)?;
})?;
} else {
warn!(
"Network with SSID {} not found",
core::str::from_utf8(req.network_id.0).unwrap()
);

// Not found
NetworkConfigResponse {
writer.set(NetworkConfigResponse {
status: NetworkCommissioningStatus::NetworkIdNotFound,
debug_text: None,
network_index: None,
}
.to_tlv(&mut tw, TagType::Anonymous)?;
})?;
}

Ok(())
Expand Down Expand Up @@ -346,7 +341,7 @@ where
.iter()
.position(|conf| conf.ssid.as_str().as_bytes() == req.network_id.0);

let mut tw = encoder.with_command(ResponseCommands::NetworkConfigResponse as _)?;
let writer = encoder.with_command(ResponseCommands::NetworkConfigResponse as _)?;

if let Some(index) = index {
// Found
Expand All @@ -367,25 +362,23 @@ where
req.index
);

NetworkConfigResponse {
writer.set(NetworkConfigResponse {
status: NetworkCommissioningStatus::Success,
debug_text: None,
network_index: Some(req.index as _),
}
.to_tlv(&mut tw, TagType::Anonymous)?;
})?;
} else {
warn!(
"Reordering network with SSID {} to index {} failed: out of range",
core::str::from_utf8(req.network_id.0).unwrap(),
req.index
);

NetworkConfigResponse {
writer.set(NetworkConfigResponse {
status: NetworkCommissioningStatus::OutOfRange,
debug_text: None,
network_index: Some(req.index as _),
}
.to_tlv(&mut tw, TagType::Anonymous)?;
})?;
}
} else {
warn!(
Expand All @@ -394,12 +387,11 @@ where
);

// Not found
NetworkConfigResponse {
writer.set(NetworkConfigResponse {
status: NetworkCommissioningStatus::NetworkIdNotFound,
debug_text: None,
network_index: None,
}
.to_tlv(&mut tw, TagType::Anonymous)?;
})?;
}

Ok(())
Expand Down
Loading

0 comments on commit 6fb14ee

Please sign in to comment.