@@ -6,6 +6,7 @@ mod wifi;
6
6
7
7
use crate :: api:: processor:: { process_events, DeviceInfo , DeviceInfoProducer } ;
8
8
use crate :: api:: websocket:: { ConnectionState , SessionEvent , WebSocketSession } ;
9
+ use crate :: config:: Config ;
9
10
use crate :: storage:: spiflash:: SPIFlashStorage ;
10
11
use crate :: usb:: msc_device:: { MSCDevice , MSCDeviceConfig } ;
11
12
use crate :: wifi:: session:: WifiSession ;
@@ -18,46 +19,64 @@ use esp_idf_svc::sys::{esp, esp_vfs_fat_info, free};
18
19
use futures:: executor:: { LocalPool , LocalSpawner } ;
19
20
use futures:: task:: LocalSpawnExt ;
20
21
use std:: ffi:: CString ;
22
+ use std:: path:: Path ;
21
23
use std:: rc:: Rc ;
22
24
use std:: thread;
23
25
use std:: time:: Duration ;
24
26
use time:: OffsetDateTime ;
25
27
26
- const DEFAULT_PARTITION_LABEL : & str = "storage" ;
27
- const DEFAULT_MOUNT_PATH : & str = "/disk" ;
28
-
29
28
const PKG_NAME : & str = env ! ( "CARGO_PKG_NAME" ) ;
30
29
const VERSION : & str = env ! ( "CARGO_PKG_VERSION" ) ;
31
30
32
- const SSID : & str = env ! ( "WIFI_SSID" ) ;
33
- const PASSWORD : & str = env ! ( "WIFI_PASS" ) ;
34
- const API_ENDPOINT : & str = env ! ( "API_ENDPOINT" ) ;
31
+ const CONFIG_PATH : Option < & str > = option_env ! ( "CONFIG_PATH" ) ;
32
+ const DEFAULT_CONFIG_PATH : & str = "securedash.toml" ;
35
33
const PARTITION_LABEL : Option < & str > = option_env ! ( "PARTITION_LABEL" ) ;
34
+ const DEFAULT_PARTITION_LABEL : & str = "storage" ;
36
35
const MOUNT_PATH : Option < & str > = option_env ! ( "MOUNT_PATH" ) ;
36
+ const DEFAULT_MOUNT_PATH : & str = "/disk" ;
37
+
38
+ fn load_config ( config_file : & str ) -> Option < Config > {
39
+ log:: info!( "Reading config from {}" , config_file) ;
40
+ let config = Config :: read ( & config_file) ;
41
+ if let Err ( error) = & config {
42
+ log:: error!( "Failed to load config: {error}" ) ;
43
+ }
44
+ let config = config. ok ( ) ;
45
+ if let Some ( config) = & config {
46
+ log:: info!( "Loaded config: {config:#?}" ) ;
47
+ }
48
+ config
49
+ }
37
50
38
51
async fn run_async ( spawner : LocalSpawner ) -> Result < ( ) , anyhow:: Error > {
39
- log:: info!( "Start {} - {}" , PKG_NAME , VERSION , ) ;
52
+ let partition_label = PARTITION_LABEL . unwrap_or ( DEFAULT_PARTITION_LABEL ) ;
53
+ let mount_path = MOUNT_PATH . unwrap_or ( DEFAULT_MOUNT_PATH ) ;
54
+ let config_path = CONFIG_PATH . unwrap_or ( DEFAULT_CONFIG_PATH ) ;
55
+ log:: info!( "Start {PKG_NAME} - version={VERSION}, partition_label={partition_label}, mount_path={mount_path}, config_path={config_path}" ) ;
40
56
41
- let mount_path = MOUNT_PATH . unwrap_or ( DEFAULT_MOUNT_PATH ) . to_string ( ) ;
42
57
let mut storage = Box :: new ( SPIFlashStorage :: new ( ) ) ;
43
- storage. initialize_partition ( PARTITION_LABEL . unwrap_or ( DEFAULT_PARTITION_LABEL ) ) ?;
58
+ storage. initialize_partition ( partition_label ) ?;
44
59
storage. mount ( & mount_path, 5 ) ;
45
60
61
+ let config = load_config ( Path :: new ( mount_path) . join ( config_path) . to_str ( ) . unwrap ( ) ) ;
62
+
63
+ let mut msc_device = MSCDevice :: new ( & MSCDeviceConfig { high_speed : true } , storage) ;
64
+ msc_device. install ( ) ?;
65
+
46
66
let peripherals = Peripherals :: take ( ) ?;
67
+ /*
47
68
let mut wifi = WifiSession::new(SSID, PASSWORD, AuthMethod::WPA2Personal, peripherals.modem)?;
48
69
wifi.connect().await?;
49
- log:: info!( "Connected wifi: {:#?}" , wifi. get_ip_info( ) ) ;
70
+ log::info!("Connected wifi: {:#?}", wifi.get_ip_info());*/
50
71
51
72
// Keep it around or else the SNTP service will stop
52
73
let _sntp = EspSntp :: new_default ( ) ?;
53
74
log:: info!( "SNTP initialized" ) ;
54
75
55
- let mut msc_device = MSCDevice :: new ( & MSCDeviceConfig { high_speed : true } , storage) ;
56
- msc_device. install ( ) ?;
57
-
58
76
let mut button = PinDriver :: input ( peripherals. pins . gpio14 ) ?;
59
77
button. set_pull ( Pull :: Up ) ?;
60
78
79
+ /*
61
80
let mut client = WebSocketSession::new(API_ENDPOINT, Duration::from_secs(30));
62
81
63
82
let captured_mount_path = mount_path.clone();
@@ -86,7 +105,7 @@ async fn run_async(spawner: LocalSpawner) -> Result<(), anyhow::Error> {
86
105
button.wait_for_low().await?;
87
106
log::info!("Button pressed!");
88
107
89
- spawner. spawn_local ( process_events ( client, device_info_producer, mount_path) ) ?;
108
+ spawner.spawn_local(process_events(client, device_info_producer, mount_path))?;*/
90
109
91
110
loop {
92
111
// Asynchronously wait for GPIO events, allowing other tasks
0 commit comments