From 6889ba45619164abffe3bed513c8d924a4c0df07 Mon Sep 17 00:00:00 2001 From: ju6ge Date: Sun, 10 May 2026 13:34:34 +0200 Subject: [PATCH] refactor: move try_connect_wifi_sntp_mqtt to network module --- rust/src/main.rs | 84 +-------------------------------------------- rust/src/network.rs | 79 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 79 insertions(+), 84 deletions(-) diff --git a/rust/src/main.rs b/rust/src/main.rs index 46196c2..859509d 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -211,7 +211,7 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> { let mut stack: OptionLock = OptionLock::empty(); let network_mode = if board.board_hal.get_config().network.ssid.is_some() { - try_connect_wifi_sntp_mqtt(&mut board, &mut stack, spawner).await + network::try_connect_wifi_sntp_mqtt(&mut board, &mut stack, spawner).await } else { info!("No wifi configured"); //the current sensors require this amount to stabilize, in the case of Wi-Fi this is already handled due to connect timings; @@ -740,88 +740,6 @@ async fn publish_firmware_info( .await; mqtt::publish("/state", "online").await; } -macro_rules! mk_static { - ($t:ty,$val:expr) => {{ - static STATIC_CELL: static_cell::StaticCell<$t> = static_cell::StaticCell::new(); - #[deny(unused_attributes)] - let x = STATIC_CELL.uninit().write(($val)); - x - }}; -} -async fn try_connect_wifi_sntp_mqtt( - board: &mut MutexGuard<'static, CriticalSectionRawMutex, HAL<'static>>, - stack_store: &mut OptionLock>, - spawner: Spawner, -) -> network::NetworkMode { - let nw_conf = &board.board_hal.get_config().network.clone(); - let esp = board.board_hal.get_esp(); - let device = match esp.interface_sta.take() { - Some(d) => d, - None => { - info!("Offline mode due to STA interface already taken"); - board.board_hal.general_fault(true).await; - return network::NetworkMode::OFFLINE; - } - }; - match network::wifi(nw_conf, device, &esp.controller, &mut esp.rng, spawner).await { - Ok(stack) => { - stack_store.replace(stack); - - let sntp_mode: network::SntpMode = match network::sntp(1000 * 10, stack).await { - Ok(new_time) => { - info!("Using time from sntp {}", new_time.to_rfc3339()); - let _ = board - .board_hal - .get_rtc_module() - .set_rtc_time(&new_time) - .await; - network::SntpMode::SYNC { current: new_time } - } - Err(err) => { - warn!("sntp error: {err}"); - board.board_hal.general_fault(true).await; - network::SntpMode::OFFLINE - } - }; - - let mqtt_connected = if board.board_hal.get_config().network.mqtt_url.is_some() { - let nw_config = board.board_hal.get_config().network.clone(); - let nw_config = mk_static!(NetworkConfig, nw_config); - match mqtt::mqtt_init(nw_config, stack, spawner).await { - Ok(_) => { - info!("Mqtt connection ready"); - true - } - Err(err) => { - warn!("Could not connect mqtt due to {err}"); - false - } - } - } else { - false - }; - - let ip = match stack.config_v4() { - Some(config) => config.address.address().to_string(), - None => match stack.config_v6() { - Some(config) => config.address.address().to_string(), - None => String::from("No IP"), - }, - }; - network::NetworkMode::WIFI { - sntp: sntp_mode, - mqtt: mqtt_connected, - ip_address: ip, - } - } - Err(err) => { - info!("Offline mode due to {err}"); - board.board_hal.general_fault(true).await; - network::NetworkMode::OFFLINE - } - } -} - async fn pump_info( plant_id: usize, pump_active: bool, diff --git a/rust/src/network.rs b/rust/src/network.rs index 848eae8..dfe545e 100644 --- a/rust/src/network.rs +++ b/rust/src/network.rs @@ -1,6 +1,8 @@ use crate::bail; use crate::config::NetworkConfig; use crate::fat_error::{ContextExt, FatError, FatResult}; +use crate::hal::{PlantHal, HAL}; +use crate::mqtt; use alloc::string::{String, ToString}; use alloc::sync::Arc; use chrono::{DateTime, Utc}; @@ -10,8 +12,9 @@ use embassy_net::dns::DnsQueryType; use embassy_net::udp::{PacketMetadata, UdpSocket}; use embassy_net::{DhcpConfig, Runner, Stack, StackResources, StaticConfigV4}; use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; -use embassy_sync::mutex::Mutex; +use embassy_sync::mutex::{Mutex, MutexGuard}; use embassy_time::{Duration, Timer, WithTimeout}; +use option_lock::OptionLock; use edge_dhcp::{ io::{self, DEFAULT_SERVER_PORT}, server::{Server, ServerOptions}, @@ -348,3 +351,77 @@ pub async fn wifi( info!("Connected WIFI, dhcp: {:?}", stack.config_v4()); Ok(*stack) } + +pub async fn try_connect_wifi_sntp_mqtt( + board: &mut MutexGuard<'static, CriticalSectionRawMutex, HAL<'static>>, + stack_store: &mut OptionLock>, + spawner: Spawner, +) -> NetworkMode { + let nw_conf = &board.board_hal.get_config().network.clone(); + let esp = board.board_hal.get_esp(); + let device = match esp.interface_sta.take() { + Some(d) => d, + None => { + info!("Offline mode due to STA interface already taken"); + board.board_hal.general_fault(true).await; + return NetworkMode::OFFLINE; + } + }; + match wifi(nw_conf, device, &esp.controller, &mut esp.rng, spawner).await { + Ok(stack) => { + stack_store.replace(stack); + + let sntp_mode: SntpMode = match sntp(1000 * 10, stack).await { + Ok(new_time) => { + info!("Using time from sntp {}", new_time.to_rfc3339()); + let _ = board + .board_hal + .get_rtc_module() + .set_rtc_time(&new_time) + .await; + SntpMode::SYNC { current: new_time } + } + Err(err) => { + warn!("sntp error: {err}"); + board.board_hal.general_fault(true).await; + SntpMode::OFFLINE + } + }; + + let mqtt_connected = if board.board_hal.get_config().network.mqtt_url.is_some() { + let nw_config = board.board_hal.get_config().network.clone(); + let nw_config = mk_static!(NetworkConfig, nw_config); + match mqtt::mqtt_init(nw_config, stack, spawner).await { + Ok(_) => { + info!("Mqtt connection ready"); + true + } + Err(err) => { + warn!("Could not connect mqtt due to {err}"); + false + } + } + } else { + false + }; + + let ip = match stack.config_v4() { + Some(config) => config.address.address().to_string(), + None => match stack.config_v6() { + Some(config) => config.address.address().to_string(), + None => String::from("No IP"), + }, + }; + NetworkMode::WIFI { + sntp: sntp_mode, + mqtt: mqtt_connected, + ip_address: ip, + } + } + Err(err) => { + info!("Offline mode due to {err}"); + board.board_hal.general_fault(true).await; + NetworkMode::OFFLINE + } + } +}