Skip to content

Commit

Permalink
Fix broken build
Browse files Browse the repository at this point in the history
  • Loading branch information
fangpenlin committed Jan 7, 2025
1 parent 6b55268 commit e96e2bd
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ pub struct Config {
}

impl Config {
pub fn read(file_path: &str) -> anyhow<Self> {
pub fn read(file_path: &str) -> anyhow::Result<Self> {
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)?)
}
}
47 changes: 33 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Config> {
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();
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/usb/msc_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
}

0 comments on commit e96e2bd

Please sign in to comment.