diff --git a/src/config.rs b/src/config.rs index accadfe..97565b7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -58,9 +58,9 @@ pub struct Config { } impl Config { - pub fn read(file_path: &str) -> anyhow { + pub fn read(file_path: &str) -> anyhow::Result { let mut config_str = String::new(); File::open(file_path)?.read_to_string(&mut config_str)?; - toml::from_str(&*config_str)? + Ok(toml::from_str(&*config_str)?) } } diff --git a/src/main.rs b/src/main.rs index ae44259..acb1894 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ mod wifi; use crate::api::processor::{process_events, DeviceInfo, DeviceInfoProducer}; use crate::api::websocket::{ConnectionState, SessionEvent, WebSocketSession}; +use crate::config::Config; use crate::storage::spiflash::SPIFlashStorage; use crate::usb::msc_device::{MSCDevice, MSCDeviceConfig}; use crate::wifi::session::WifiSession; @@ -18,46 +19,64 @@ use esp_idf_svc::sys::{esp, esp_vfs_fat_info, free}; use futures::executor::{LocalPool, LocalSpawner}; use futures::task::LocalSpawnExt; use std::ffi::CString; +use std::path::Path; use std::rc::Rc; use std::thread; use std::time::Duration; use time::OffsetDateTime; -const DEFAULT_PARTITION_LABEL: &str = "storage"; -const DEFAULT_MOUNT_PATH: &str = "/disk"; - const PKG_NAME: &str = env!("CARGO_PKG_NAME"); const VERSION: &str = env!("CARGO_PKG_VERSION"); -const SSID: &str = env!("WIFI_SSID"); -const PASSWORD: &str = env!("WIFI_PASS"); -const API_ENDPOINT: &str = env!("API_ENDPOINT"); +const CONFIG_PATH: Option<&str> = option_env!("CONFIG_PATH"); +const DEFAULT_CONFIG_PATH: &str = "securedash.toml"; const PARTITION_LABEL: Option<&str> = option_env!("PARTITION_LABEL"); +const DEFAULT_PARTITION_LABEL: &str = "storage"; const MOUNT_PATH: Option<&str> = option_env!("MOUNT_PATH"); +const DEFAULT_MOUNT_PATH: &str = "/disk"; + +fn load_config(config_file: &str) -> Option { + log::info!("Reading config from {}", config_file); + let config = Config::read(&config_file); + if let Err(error) = &config { + log::error!("Failed to load config: {error}"); + } + let config = config.ok(); + if let Some(config) = &config { + log::info!("Loaded config: {config:#?}"); + } + config +} async fn run_async(spawner: LocalSpawner) -> Result<(), anyhow::Error> { - log::info!("Start {} - {}", PKG_NAME, VERSION,); + let partition_label = PARTITION_LABEL.unwrap_or(DEFAULT_PARTITION_LABEL); + let mount_path = MOUNT_PATH.unwrap_or(DEFAULT_MOUNT_PATH); + let config_path = CONFIG_PATH.unwrap_or(DEFAULT_CONFIG_PATH); + log::info!("Start {PKG_NAME} - version={VERSION}, partition_label={partition_label}, mount_path={mount_path}, config_path={config_path}"); - let mount_path = MOUNT_PATH.unwrap_or(DEFAULT_MOUNT_PATH).to_string(); let mut storage = Box::new(SPIFlashStorage::new()); - storage.initialize_partition(PARTITION_LABEL.unwrap_or(DEFAULT_PARTITION_LABEL))?; + storage.initialize_partition(partition_label)?; storage.mount(&mount_path, 5); + let config = load_config(Path::new(mount_path).join(config_path).to_str().unwrap()); + + let mut msc_device = MSCDevice::new(&MSCDeviceConfig { high_speed: true }, storage); + msc_device.install()?; + let peripherals = Peripherals::take()?; + /* let mut wifi = WifiSession::new(SSID, PASSWORD, AuthMethod::WPA2Personal, peripherals.modem)?; wifi.connect().await?; - log::info!("Connected wifi: {:#?}", wifi.get_ip_info()); + log::info!("Connected wifi: {:#?}", wifi.get_ip_info());*/ // Keep it around or else the SNTP service will stop let _sntp = EspSntp::new_default()?; log::info!("SNTP initialized"); - let mut msc_device = MSCDevice::new(&MSCDeviceConfig { high_speed: true }, storage); - msc_device.install()?; - let mut button = PinDriver::input(peripherals.pins.gpio14)?; button.set_pull(Pull::Up)?; + /* let mut client = WebSocketSession::new(API_ENDPOINT, Duration::from_secs(30)); let captured_mount_path = mount_path.clone(); @@ -86,7 +105,7 @@ async fn run_async(spawner: LocalSpawner) -> Result<(), anyhow::Error> { button.wait_for_low().await?; log::info!("Button pressed!"); - spawner.spawn_local(process_events(client, device_info_producer, mount_path))?; + spawner.spawn_local(process_events(client, device_info_producer, mount_path))?;*/ loop { // Asynchronously wait for GPIO events, allowing other tasks diff --git a/src/usb/msc_device.rs b/src/usb/msc_device.rs index 7dacaaa..821fc20 100644 --- a/src/usb/msc_device.rs +++ b/src/usb/msc_device.rs @@ -71,7 +71,7 @@ impl MSCDevice { esp!(unsafe { tinyusb_driver_install(&tusb_cfg) }) .with_context(|| "Failed to install TinyUSB driver")?; - log::info!("TinyUSB driver installed."); + log::info!("TinyUSB MSC driver installed."); Ok(()) } }