write selected wifi credentials to flash nvs

todo
- reboot after writing
- delete bad networks after failing to connect
This commit is contained in:
2026-02-04 00:37:24 +00:00
parent 2cfe436ac9
commit 601c7391a3
7 changed files with 133 additions and 54 deletions
+17 -8
View File
@@ -8,6 +8,8 @@ use embassy_futures::select::select;
use alloc::vec::Vec;
use crate::ota::Flasher;
/// Max number of connections
const CONNECTIONS_MAX: usize = 1;
@@ -49,8 +51,8 @@ struct WifiProvisioningService {
pub async fn run_ble(radio_init: &RadioController<'static>,
peripheral: esp_hal::peripherals::BT<'_>,
networks: Vec<[u8; 40]>) {
// let radio_init = esp_radio::init().expect("Failed to initialize Wi-Fi/BLE controller");
networks: Vec<[u8; 40]>,
mut flasher: Flasher<'_>) {
let connector = BleConnector::new(&radio_init, peripheral, Default::default()).expect("connector");
let controller: ExternalController<_, 20> = ExternalController::new(connector);
@@ -79,7 +81,7 @@ pub async fn run_ble(radio_init: &RadioController<'static>,
match advertise("Eculocate", &mut peripheral, &server).await {
Ok(conn) => {
// set up tasks when the connection is established to a central, so they don't run when no one is connected.
let a = gatt_events_task(&server, &conn, &networks);
let a = gatt_events_task(&server, &conn, &networks, &mut flasher);
let b = custom_task(&server, &conn, &stack);
// run until any task ends (usually because the connection has been closed),
// then return to advertising state.
@@ -110,12 +112,11 @@ async fn ble_task<C: Controller, P: PacketPool>(mut runner: Runner<'_, C, P>) {
/// This is how we interact with read and write requests.
async fn gatt_events_task<P: PacketPool>(server: &Server<'_>,
conn: &GattConnection<'_, '_, P>,
networks: &Vec<[u8; 40]> ) -> Result<(), Error> {
networks: &Vec<[u8; 40]>,
flasher: &mut Flasher<'_>) -> Result<(), Error> {
let wps = &server.wifi_provisioning_service;
for ap in networks {
info!("ap {:?}", ap)
};
let max_index : u8 = (networks.len() & 0xff).try_into().unwrap();
server.set(&wps.max_network_index, &max_index);
let current_index = wps.current_network_index;
let reason = loop {
@@ -145,7 +146,15 @@ async fn gatt_events_task<P: PacketPool>(server: &Server<'_>,
} else {
warn!("index {:?} out of range", i);
}
}
} else if event.handle() == wps.secret.handle {
info!("[gatt] got secret {:?}", event.data());
let n = server.get(&wps.current_network).unwrap();
let n_s = core::str::from_utf8(&n[8..]).unwrap();
let n0 =
if let Some(i) = n_s.find("\0") { i } else { n_s.len() };
flasher.set_var("ssid", Some(&n_s[..n0]));
flasher.set_var("secret", Some(core::str::from_utf8(event.data()).unwrap()));
}
},
_ => {}
};