Initial commit

Signed-off-by: Thomas Klaehn <thomas.klaehn@perinet.io>
This commit is contained in:
Thomas Klaehn
2025-02-08 08:48:24 +01:00
commit 18e9f86c36
28 changed files with 669 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
[build]
target = "riscv32imc-esp-espidf"
[target.riscv32imc-esp-espidf]
linker = "ldproxy"
runner = "espflash flash --monitor"
# Future - necessary for the experimental "native build" of esp-idf-sys with ESP32C3
# See also https://github.com/ivmarkov/embuild/issues/16
rustflags = ["--cfg", "espidf_time64"]
[unstable]
build-std = ["panic_abort", "std"]
[env]
# Enables the esp-idf-sys "native" build feature (`cargo build --features native`) to build against ESP-IDF (v5.3.2)
ESP_IDF_VERSION = { value = "tag:v5.3.2" }
# These configurations will pick up your custom "sdkconfig.release", "sdkconfig.debug" or "sdkconfig.defaults[.*]" files
# that you might put in the root of the project
# The easiest way to generate a full "sdkconfig[.release|debug]" configuration (as opposed to manually enabling only the necessary flags via "sdkconfig.defaults[.*]"
# is by running "cargo pio espidf menuconfig" (that is, if using the pio builder)
#ESP_IDF_SDKCONFIG = { value = "./sdkconfig.release", relative = true }
#ESP_IDF_SDKCONFIG = { value = "./sdkconfig.debug", relative = true }
ESP_IDF_SDKCONFIG_DEFAULTS = { value = "./sdkconfig.defaults", relative = true }
# ESP-IDF will be installed in ~/.espressif so it can be reused across the different examples.
# See also https://github.com/esp-rs/esp-idf-sys/blob/master/BUILD-OPTIONS.md#esp_idf_tools_install_dir-esp_idf_tools_install_dir
ESP_IDF_TOOLS_INSTALL_DIR = { value = "global" }

2
lib/wifi/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
/target
Cargo.lock

16
lib/wifi/Cargo.toml Normal file
View File

@@ -0,0 +1,16 @@
[package]
name = "wifi"
version = "0.1.0"
edition = "2021"
authors = ["Sergio Gasquez <sergio.gasquez@gmail.com>"]
[dependencies]
anyhow = "=1.0.95"
esp-idf-svc = "=0.50.1"
log = "=0.4.22"
[build-dependencies]
embuild = "=0.33.0"
[dev-dependencies]
toml-cfg = "=0.1.3"

3
lib/wifi/build.rs Normal file
View File

@@ -0,0 +1,3 @@
fn main() {
embuild::espidf::sysenv::output();
}

View File

@@ -0,0 +1,3 @@
[wifi]
wifi_ssid = "FBI Surveillance Van"
wifi_psk = "hunter2"

40
lib/wifi/examples/wifi.rs Normal file
View File

@@ -0,0 +1,40 @@
use anyhow::{bail, Result};
use esp_idf_svc::{eventloop::EspSystemEventLoop, hal::prelude::Peripherals};
use wifi::wifi;
/// This configuration is picked up at compile time by `build.rs` from the
/// file `cfg.toml`.
#[toml_cfg::toml_config]
pub struct Config {
#[default("")]
wifi_ssid: &'static str,
#[default("")]
wifi_psk: &'static str,
}
fn main() -> Result<()> {
esp_idf_svc::sys::link_patches();
esp_idf_svc::log::EspLogger::initialize_default();
let peripherals = Peripherals::take().unwrap();
let sysloop = EspSystemEventLoop::take()?;
let app_config = CONFIG;
// Connect to the Wi-Fi network
let _wifi = match wifi(
app_config.wifi_ssid,
app_config.wifi_psk,
peripherals.modem,
sysloop,
) {
Ok(inner) => {
println!("Connected to Wi-Fi network!");
inner
}
Err(err) => {
// Red!
bail!("Could not connect to Wi-Fi network: {:?}", err)
}
};
Ok(())
}

View File

@@ -0,0 +1,3 @@
[toolchain]
channel = "nightly-2025-01-01"
components = ["rust-src"]

View File

@@ -0,0 +1,2 @@
CONFIG_ESP_MAIN_TASK_STACK_SIZE=20000
CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE=4096

78
lib/wifi/src/lib.rs Normal file
View File

@@ -0,0 +1,78 @@
use anyhow::{bail, Result};
use esp_idf_svc::{
eventloop::EspSystemEventLoop,
hal::peripheral,
wifi::{AuthMethod, BlockingWifi, ClientConfiguration, Configuration, EspWifi},
};
use log::info;
pub fn wifi(
ssid: &str,
pass: &str,
modem: impl peripheral::Peripheral<P = esp_idf_svc::hal::modem::Modem> + 'static,
sysloop: EspSystemEventLoop,
) -> Result<Box<EspWifi<'static>>> {
let mut auth_method = AuthMethod::WPA2Personal;
if ssid.is_empty() {
bail!("Missing WiFi name")
}
if pass.is_empty() {
auth_method = AuthMethod::None;
info!("Wifi password is empty");
}
let mut esp_wifi = EspWifi::new(modem, sysloop.clone(), None)?;
let mut wifi = BlockingWifi::wrap(&mut esp_wifi, sysloop)?;
wifi.set_configuration(&Configuration::Client(ClientConfiguration::default()))?;
info!("Starting wifi...");
wifi.start()?;
info!("Scanning...");
let ap_infos = wifi.scan()?;
let ours = ap_infos.into_iter().find(|a| a.ssid == ssid);
let channel = if let Some(ours) = ours {
info!(
"Found configured access point {} on channel {}",
ssid, ours.channel
);
Some(ours.channel)
} else {
info!(
"Configured access point {} not found during scanning, will go with unknown channel",
ssid
);
None
};
wifi.set_configuration(&Configuration::Client(ClientConfiguration {
ssid: ssid
.try_into()
.expect("Could not parse the given SSID into WiFi config"),
password: pass
.try_into()
.expect("Could not parse the given password into WiFi config"),
channel,
auth_method,
..Default::default()
}))?;
info!("Connecting wifi...");
wifi.connect()?;
info!("Waiting for DHCP lease...");
wifi.wait_netif_up()?;
let ip_info = wifi.wifi().sta_netif().get_ip_info()?;
info!("Wifi DHCP info: {:?}", ip_info);
Ok(Box::new(esp_wifi))
}