keep ota around for alter queries to it

This commit is contained in:
2025-09-16 01:41:38 +02:00
parent 65f6670ca4
commit 1397f5d775
6 changed files with 166 additions and 190 deletions

View File

@@ -7,7 +7,7 @@ use chrono::{DateTime, Utc};
use serde::Serialize;
use alloc::string::ToString;
use alloc::{string::String, vec::Vec};
use alloc::{format, string::String, vec::Vec};
use core::marker::PhantomData;
use core::net::{IpAddr, Ipv4Addr};
use core::str::FromStr;
@@ -15,15 +15,18 @@ use embassy_executor::{SendSpawner, Spawner};
use embassy_net::tcp::TcpSocket;
use embassy_net::{IpListenEndpoint, Ipv4Cidr, Runner, Stack, StackResources, StaticConfigV4};
use embassy_time::{Duration, Instant, Timer};
use esp_bootloader_esp_idf::ota::OtaImageState;
use esp_bootloader_esp_idf::ota::{Ota, OtaImageState, Slot};
use esp_bootloader_esp_idf::partitions::{Error, FlashRegion, PartitionEntry, PartitionTable};
use esp_hal::gpio::Input;
use esp_hal::rng::Rng;
use esp_hal::rtc_cntl::sleep::RtcSleepConfig;
use esp_println::{print, println};
use esp_storage::FlashStorage;
use esp_wifi::wifi::{
AccessPointConfiguration, Configuration, Interfaces, WifiController, WifiDevice, WifiEvent,
WifiState,
};
use log::{info, warn};
#[link_section = ".rtc.data"]
static mut LAST_WATERING_TIMESTAMP: [i64; PLANT_COUNT] = [0; PLANT_COUNT];
@@ -72,11 +75,8 @@ pub struct Esp<'a> {
pub boot_button: Input<'a>,
pub(crate) wall_clock_offset: u64,
pub storage: FlashStorage,
pub slot: usize,
pub next_slot: usize,
pub ota_state: OtaImageState,
pub slot_addres: u32,
pub ota: Ota<'static, FlashStorage>,
pub ota_next: &'static PartitionEntry<'static>,
}
pub struct IpInfo {
@@ -103,6 +103,36 @@ impl Esp<'_> {
const CONFIG_FILE: &'static str = "/spiffs/config.cfg";
const BASE_PATH: &'static str = "/spiffs";
pub(crate) fn get_ota_slot(&mut self) -> String {
match self.ota.current_slot() {
Ok(slot) => {
format!("{:?}", slot)
}
Err(err) => {
format!("{:?}", err)
}
}
}
pub(crate) fn get_ota_state(&mut self) -> String {
match self.ota.current_ota_state() {
Ok(state) => {
format!("{:?}", state)
}
Err(err) => {
format!("{:?}", err)
}
}
}
// let current = ota.current_slot()?;
// println!(
// "current image state {:?} (only relevant if the bootloader was built with auto-rollback support)",
// ota.current_ota_state()
// );
// println!("current {:?} - next {:?}", current, current.next());
// let ota_state = ota.current_ota_state()?;
pub(crate) fn mode_override_pressed(&mut self) -> bool {
self.boot_button.is_low()
}
@@ -119,6 +149,14 @@ impl Esp<'_> {
//self.time()
todo!();
}
pub async fn flash_ota(&mut self) -> anyhow::Result<()> {
let mut storage_ota_next = FlashStorage::new();
let ota_next = self.ota_next.as_embedded_storage(&mut storage_ota_next);
bail!("not implemented")
}
pub(crate) fn time(&mut self) -> DateTime<Utc> {
let wall_clock = Instant::now().as_millis() + self.wall_clock_offset;
DateTime::from_timestamp_millis(wall_clock as i64).unwrap()
@@ -231,6 +269,36 @@ impl Esp<'_> {
anyhow::Ok(stack.clone())
}
pub async fn deep_sleep(&mut self, duration_in_ms: u64) -> ! {
let mut cfg = RtcSleepConfig::deep();
let cur = self.ota.current_ota_state().unwrap();
//we made it till here, so fine
if cur == OtaImageState::PendingVerify {
self.ota
.set_current_ota_state(OtaImageState::Valid)
.expect("Could not set image to valid");
}
//unsafe {
// //allow early wakeup by pressing the boot button
if duration_in_ms == 0 {
loop {
info!("todo reboot")
}
} else {
loop {
info!("todo deepsleep")
}
// //configure gpio 1 to wakeup on low, reused boot button for this
// esp_sleep_enable_ext1_wakeup(
// 0b10u64,
// esp_sleep_ext1_wakeup_mode_t_ESP_EXT1_WAKEUP_ANY_LOW,
// );
// esp_deep_sleep(duration_in_ms);
}
//};
}
pub(crate) async fn wifi(&mut self, network_config: &NetworkConfig) -> anyhow::Result<IpInfo> {
let _ssid = network_config
.ssid