Skip to content

Commit f699bb3

Browse files
committed
Fix root dir
1 parent c6a3545 commit f699bb3

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

src/api/processor.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use serde::{Deserialize, Serialize};
88
use std::fs::{read_dir, FileType};
99
use std::io::{Read, Seek};
1010
use std::mem::MaybeUninit;
11+
use std::path::Path;
1112
use std::time::SystemTime;
1213
use time::serde::timestamp::milliseconds;
1314
use time::OffsetDateTime;
@@ -79,6 +80,7 @@ pub type DeviceInfoProducer = Box<dyn Fn() -> anyhow::Result<DeviceInfo>>;
7980

8081
pub struct Processor {
8182
pub device_info_producer: DeviceInfoProducer,
83+
pub root_dir: String,
8284
}
8385

8486
impl Processor {
@@ -91,10 +93,11 @@ impl Processor {
9193
}
9294

9395
fn list_files(&self, path: &str) -> anyhow::Result<Response> {
96+
let dir_path = Path::new(&self.root_dir).join(path);
9497
// Ideally we should find a way to learn the size of all files, but we need to
9598
// iterate over all files anyway... so.. maybe not? :/
9699
let mut files: Vec<File> = vec![];
97-
for entry in read_dir(path)? {
100+
for entry in read_dir(dir_path)? {
98101
let entry = entry?;
99102
let path = entry.path().into_os_string().into_string().map_err(|e| {
100103
anyhow!(
@@ -183,9 +186,11 @@ impl Processor {
183186
pub async fn process_events(
184187
mut client: WebSocketSession<'_>,
185188
device_info_producer: DeviceInfoProducer,
189+
root_dir: &str,
186190
) {
187191
let mut processor: Option<Box<Processor>> = Some(Box::new(Processor {
188192
device_info_producer,
193+
root_dir: root_dir.to_string(),
189194
}));
190195
client.connect();
191196

@@ -198,9 +203,8 @@ pub async fn process_events(
198203
new_state: ConnectionState::Connected,
199204
..
200205
} => {
201-
processor = Some(Box::new(Processor {
202-
device_info_producer: processor.map(|p| p.device_info_producer).unwrap(),
203-
}));
206+
let last_processor = processor.unwrap();
207+
processor = Some(Box::new(Processor { ..*last_processor }));
204208
}
205209
SessionEvent::ReceiveText { text } => {
206210
let request: serde_json::Result<CommandRequest> = serde_json::from_str(&text);

src/main.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,15 @@ const VERSION: &str = env!("CARGO_PKG_VERSION");
2424
const SSID: &str = env!("WIFI_SSID");
2525
const PASSWORD: &str = env!("WIFI_PASS");
2626
const API_ENDPOINT: &str = env!("API_ENDPOINT");
27+
const PARTITION_LABEL: Option<&str> = option_env!("PARTITION_LABEL");
28+
const MOUNT_PATH: Option<&str> = option_env!("MOUNT_PATH");
2729

2830
async fn run_async(spawner: LocalSpawner) -> Result<(), anyhow::Error> {
2931
log::info!("Start {} - {}", PKG_NAME, VERSION,);
3032

33+
let partition_label = PARTITION_LABEL.unwrap_or("storage");
34+
let mount_path = MOUNT_PATH.unwrap_or("/disk");
35+
3136
let peripherals = Peripherals::take()?;
3237
let mut wifi = WifiSession::new(SSID, PASSWORD, AuthMethod::WPA2Personal, peripherals.modem)?;
3338
wifi.connect().await?;
@@ -37,7 +42,7 @@ async fn run_async(spawner: LocalSpawner) -> Result<(), anyhow::Error> {
3742
let _sntp = EspSntp::new_default()?;
3843
log::info!("SNTP initialized");
3944

40-
let mut msc_device = MSCDevice::new("storage", "/disk");
45+
let mut msc_device = MSCDevice::new(partition_label, mount_path);
4146
msc_device.install()?;
4247

4348
let mut button = PinDriver::input(peripherals.pins.gpio14)?;
@@ -60,7 +65,7 @@ async fn run_async(spawner: LocalSpawner) -> Result<(), anyhow::Error> {
6065
button.wait_for_low().await?;
6166
log::info!("Button pressed!");
6267

63-
spawner.spawn_local(process_events(client, device_info_producer))?;
68+
spawner.spawn_local(process_events(client, device_info_producer, mount_path))?;
6469

6570
loop {
6671
// Asynchronously wait for GPIO events, allowing other tasks

src/usb/msc_device.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use std::ffi::CString;
1212
#[derive(Default)]
1313
pub struct MSCDevice {
1414
pub partition_label: String,
15-
pub base_path: String,
16-
base_path_c_str: CString,
15+
pub mount_path: String,
16+
mount_path_c_str: CString,
1717
wl_partition: Option<EspWlPartition<EspPartition>>,
1818
}
1919

@@ -36,7 +36,7 @@ unsafe extern "C" fn storage_mount_changed_cb(event: *mut tinyusb_msc_event_t) {
3636
impl MSCDevice {
3737
pub fn new(partition_label: &str, base_path: &str) -> Self {
3838
Self {
39-
base_path: base_path.to_string(),
39+
mount_path: base_path.to_string(),
4040
partition_label: partition_label.to_string(),
4141
..Default::default()
4242
}
@@ -58,7 +58,7 @@ impl MSCDevice {
5858
Some(EspWlPartition::new(partition.unwrap()).with_context(|| {
5959
format!(
6060
"Failed to mount partition {} at {}",
61-
self.partition_label, self.base_path
61+
self.partition_label, self.mount_path
6262
)
6363
})?);
6464

@@ -78,17 +78,17 @@ impl MSCDevice {
7878
esp!(unsafe { tinyusb_msc_storage_init_spiflash(&config_spi) })
7979
.with_context(|| "Failed to initialize spiflash")?;
8080

81-
let base_path_c_str = CString::new(self.base_path.as_bytes()).unwrap();
81+
let base_path_c_str = CString::new(self.mount_path.as_bytes()).unwrap();
8282
esp!(unsafe { tinyusb_msc_storage_mount(base_path_c_str.as_ptr()) })
83-
.with_context(|| format!("Failed to mount storage at {}", self.base_path))?;
83+
.with_context(|| format!("Failed to mount storage at {}", self.mount_path))?;
8484

8585
let tusb_cfg = tinyusb_config_t::default();
8686
esp!(unsafe { tinyusb_driver_install(&tusb_cfg) })
8787
.with_context(|| "Failed to install TinyUSB driver")?;
8888

8989
log::info!("TinyUSB driver installed.");
9090

91-
self.base_path_c_str = base_path_c_str;
91+
self.mount_path_c_str = base_path_c_str;
9292
self.wl_partition = wl_partition;
9393
Ok(())
9494
}

0 commit comments

Comments
 (0)