scan wireless networks & publish over ble

does not actually connect yet
This commit is contained in:
2026-02-02 21:19:39 +00:00
parent 938cb4fda0
commit 2cfe436ac9
4 changed files with 110 additions and 42 deletions
+27 -8
View File
@@ -6,7 +6,7 @@ use log::{warn,info};
use embassy_futures::join::join;
use embassy_futures::select::select;
use alloc::string::String;
use alloc::vec::Vec;
/// Max number of connections
const CONNECTIONS_MAX: usize = 1;
@@ -48,7 +48,8 @@ struct WifiProvisioningService {
pub async fn run_ble(radio_init: &RadioController<'static>,
peripheral: esp_hal::peripherals::BT<'_>) {
peripheral: esp_hal::peripherals::BT<'_>,
networks: Vec<[u8; 40]>) {
// let radio_init = esp_radio::init().expect("Failed to initialize Wi-Fi/BLE controller");
let connector = BleConnector::new(&radio_init, peripheral, Default::default()).expect("connector");
let controller: ExternalController<_, 20> = ExternalController::new(connector);
@@ -78,7 +79,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);
let a = gatt_events_task(&server, &conn, &networks);
let b = custom_task(&server, &conn, &stack);
// run until any task ends (usually because the connection has been closed),
// then return to advertising state.
@@ -107,8 +108,16 @@ async fn ble_task<C: Controller, P: PacketPool>(mut runner: Runner<'_, C, P>) {
///
/// This function will handle the GATT events and process them.
/// This is how we interact with read and write requests.
async fn gatt_events_task<P: PacketPool>(server: &Server<'_>, conn: &GattConnection<'_, '_, P>) -> Result<(), Error> {
let current_index = server.wifi_provisioning_service.current_network_index;
async fn gatt_events_task<P: PacketPool>(server: &Server<'_>,
conn: &GattConnection<'_, '_, P>,
networks: &Vec<[u8; 40]> ) -> 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 {
match conn.next().await {
GattConnectionEvent::Disconnected { reason } => break reason,
@@ -123,10 +132,20 @@ async fn gatt_events_task<P: PacketPool>(server: &Server<'_>, conn: &GattConnect
// }
// }
GattEvent::Write(event) => {
info!("[gatt] write handle {:?}", event.handle());
info!("[gatt] write handle {:?} {:?}", event.handle(),
event.data());
if event.handle() == current_index.handle {
info!("[gatt] switch current index: {:?}", event.data());
}
let i = (event.data()[0]) as usize;
info!("[gatt] switch current index: {:?}", i);
if i == 0 {
server.set(&wps.current_network, &[0u8;40]);
} else if i <= networks.len() {
server.set(&wps.current_network,
&(networks[i - 1]));
} else {
warn!("index {:?} out of range", i);
}
}
},
_ => {}
};