Skip to content

Commit e8fbe3b

Browse files
committed
replacing numbered options in config with rust enum implementation, unique commit to make easier to debug or rollback
1 parent 06cf9af commit e8fbe3b

15 files changed

Lines changed: 87 additions & 50 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

daemon/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ apidocs = ["dep:utoipa"]
2323
rayhunter = { path = "../lib" }
2424
toml = "0.8.8"
2525
serde = { version = "1.0.193", features = ["derive"] }
26+
serde_repr = "0.1"
2627
tokio = { version = "1.44.2", default-features = false, features = ["fs", "signal", "process", "rt"] }
2728
axum = { version = "0.8", default-features = false, features = ["http1", "tokio", "json"] }
2829
thiserror = "1.0.52"

daemon/src/config.rs

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,40 @@
11
use log::warn;
22
use serde::{Deserialize, Serialize};
3+
use serde_repr::{Deserialize_repr, Serialize_repr};
34

45
use rayhunter::Device;
56
use rayhunter::analysis::analyzer::AnalyzerConfig;
67

78
use crate::error::RayhunterError;
9+
10+
#[repr(u8)]
11+
#[derive(Debug, Clone, Copy, PartialEq, Serialize_repr, Deserialize_repr)]
12+
#[cfg_attr(feature = "apidocs", derive(utoipa::ToSchema))]
13+
pub enum GpsMode {
14+
Disabled = 0,
15+
Fixed = 1,
16+
Api = 2,
17+
}
18+
19+
#[repr(u8)]
20+
#[derive(Debug, Clone, Copy, PartialEq, Serialize_repr, Deserialize_repr)]
21+
#[cfg_attr(feature = "apidocs", derive(utoipa::ToSchema))]
22+
pub enum UiLevel {
23+
Invisible = 0,
24+
Subtle = 1,
25+
Demo = 2,
26+
EffLogo = 3,
27+
HighVisibility = 4,
28+
TransFlag = 128,
29+
}
30+
31+
#[repr(u8)]
32+
#[derive(Debug, Clone, Copy, PartialEq, Serialize_repr, Deserialize_repr)]
33+
#[cfg_attr(feature = "apidocs", derive(utoipa::ToSchema))]
34+
pub enum KeyInputMode {
35+
Disabled = 0,
36+
DoubleTapPower = 1,
37+
}
838
use crate::notifications::NotificationType;
939

1040
/// The structure of a valid rayhunter configuration
@@ -21,11 +51,11 @@ pub struct Config {
2151
/// Internal device name
2252
pub device: Device,
2353
/// UI level
24-
pub ui_level: u8,
54+
pub ui_level: UiLevel,
2555
/// Colorblind mode
2656
pub colorblind_mode: bool,
2757
/// Key input mode
28-
pub key_input_mode: u8,
58+
pub key_input_mode: KeyInputMode,
2959
/// ntfy.sh URL
3060
pub ntfy_url: Option<String>,
3161
/// Vector containing the types of enabled notifications
@@ -34,8 +64,8 @@ pub struct Config {
3464
pub analyzers: AnalyzerConfig,
3565
pub min_space_to_start_recording_mb: u64,
3666
pub min_space_to_continue_recording_mb: u64,
37-
/// GPS mode: 0=Disabled, 1=Fixed coordinates, 2=API endpoint
38-
pub gps_mode: u8,
67+
/// GPS mode
68+
pub gps_mode: GpsMode,
3969
/// Fixed latitude used when gps_mode=1
4070
pub gps_fixed_latitude: Option<f64>,
4171
/// Fixed longitude used when gps_mode=1
@@ -49,15 +79,15 @@ impl Default for Config {
4979
port: 8080,
5080
debug_mode: false,
5181
device: Device::Orbic,
52-
ui_level: 1,
82+
ui_level: UiLevel::Subtle,
5383
colorblind_mode: false,
54-
key_input_mode: 0,
84+
key_input_mode: KeyInputMode::Disabled,
5585
analyzers: AnalyzerConfig::default(),
5686
ntfy_url: None,
5787
enabled_notifications: vec![NotificationType::Warning, NotificationType::LowBattery],
5888
min_space_to_start_recording_mb: 1,
5989
min_space_to_continue_recording_mb: 1,
60-
gps_mode: 0,
90+
gps_mode: GpsMode::Disabled,
6191
gps_fixed_latitude: None,
6292
gps_fixed_longitude: None,
6393
}

daemon/src/diag.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use rayhunter::diag_device::DiagDevice;
2727
use rayhunter::qmdl::QmdlWriter;
2828

2929
use crate::analysis::{AnalysisCtrlMessage, AnalysisWriter};
30+
use crate::config::GpsMode;
3031
use crate::display;
3132
use crate::notifications::{Notification, NotificationType};
3233
use crate::qmdl_store::{RecordingStore, RecordingStoreError};
@@ -57,7 +58,7 @@ pub struct DiagTask {
5758
notification_channel: tokio::sync::mpsc::Sender<Notification>,
5859
min_space_to_start_mb: u64,
5960
min_space_to_continue_mb: u64,
60-
gps_mode: u8,
61+
gps_mode: GpsMode,
6162
gps_fixed_coords: Option<(f64, f64)>,
6263
state: DiagState,
6364
max_type_seen: EventType,
@@ -107,7 +108,7 @@ impl DiagTask {
107108
notification_channel: tokio::sync::mpsc::Sender<Notification>,
108109
min_space_to_start_mb: u64,
109110
min_space_to_continue_mb: u64,
110-
gps_mode: u8,
111+
gps_mode: GpsMode,
111112
gps_fixed_coords: Option<(f64, f64)>,
112113
) -> Self {
113114
Self {
@@ -162,7 +163,7 @@ impl DiagTask {
162163
// For fixed-mode sessions, write the configured coordinates to the sidecar
163164
// immediately so the per-session GPS is stored durably and isn't affected
164165
// by future config changes or GPS API calls.
165-
if self.gps_mode == 1 {
166+
if self.gps_mode == GpsMode::Fixed {
166167
if let Some((lat, lon)) = self.gps_fixed_coords {
167168
if let Some((entry_idx, _)) = qmdl_store.get_current_entry() {
168169
if let Ok(mut gps_file) = qmdl_store.open_entry_gps_for_append(entry_idx).await
@@ -410,7 +411,7 @@ pub fn run_diag_read_thread(
410411
notification_channel: tokio::sync::mpsc::Sender<Notification>,
411412
min_space_to_start_mb: u64,
412413
min_space_to_continue_mb: u64,
413-
gps_mode: u8,
414+
gps_mode: GpsMode,
414415
gps_fixed_coords: Option<(f64, f64)>,
415416
) {
416417
task_tracker.spawn(async move {

daemon/src/display/generic_framebuffer.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use image::{AnimationDecoder, DynamicImage, codecs::gif::GifDecoder, imageops::F
33
use std::io::Cursor;
44
use std::time::Duration;
55

6-
use crate::config;
6+
use crate::config::{self, UiLevel};
77
use crate::display::DisplayState;
88
use rayhunter::analysis::analyzer::EventType;
99

@@ -176,7 +176,7 @@ pub fn update_ui(
176176
) {
177177
static IMAGE_DIR: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/images/");
178178
let display_level = config.ui_level;
179-
if display_level == 0 {
179+
if display_level == UiLevel::Invisible {
180180
info!("Invisible mode, not spawning UI.");
181181
return;
182182
}
@@ -187,14 +187,14 @@ pub fn update_ui(
187187
task_tracker.spawn(async move {
188188
// this feels wrong, is there a more rusty way to do this?
189189
let mut img: Option<&[u8]> = None;
190-
if display_level == 2 {
190+
if display_level == UiLevel::Demo {
191191
img = Some(
192192
IMAGE_DIR
193193
.get_file("orca.gif")
194194
.expect("failed to read orca.gif")
195195
.contents(),
196196
);
197-
} else if display_level == 3 {
197+
} else if display_level == UiLevel::EffLogo {
198198
img = Some(
199199
IMAGE_DIR
200200
.get_file("eff.png")
@@ -217,20 +217,19 @@ pub fn update_ui(
217217

218218
let mut status_bar_height = 2;
219219
match display_level {
220-
2 => fb.draw_gif(img.unwrap()).await,
221-
3 => fb.draw_img(img.unwrap()).await,
222-
4 => {
220+
UiLevel::Demo => fb.draw_gif(img.unwrap()).await,
221+
UiLevel::EffLogo => fb.draw_img(img.unwrap()).await,
222+
UiLevel::HighVisibility => {
223223
status_bar_height = fb.dimensions().height;
224224
}
225-
128 => {
225+
UiLevel::TransFlag => {
226226
fb.draw_line(Color::Cyan, 128).await;
227227
fb.draw_line(Color::Pink, 102).await;
228228
fb.draw_line(Color::White, 76).await;
229229
fb.draw_line(Color::Pink, 50).await;
230230
fb.draw_line(Color::Cyan, 25).await;
231231
}
232-
// this branch is for ui_level 1, which is also the default if an
233-
// unknown value is used
232+
// UiLevel::Subtle (1) and anything else: just the status bar line
234233
_ => {}
235234
};
236235
let (color, pattern) = display_style;

daemon/src/display/tmobile.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use tokio_util::task::TaskTracker;
99

1010
use std::time::Duration;
1111

12-
use crate::config;
12+
use crate::config::{self, UiLevel};
1313
use crate::display::DisplayState;
1414

1515
macro_rules! led {
@@ -31,7 +31,7 @@ pub fn update_ui(
3131
mut ui_update_rx: mpsc::Receiver<DisplayState>,
3232
) {
3333
let mut invisible: bool = false;
34-
if config.ui_level == 0 {
34+
if config.ui_level == UiLevel::Invisible {
3535
info!("Invisible mode, not spawning UI.");
3636
invisible = true;
3737
}

daemon/src/display/tplink.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use tokio::sync::mpsc::Receiver;
33
use tokio_util::sync::CancellationToken;
44
use tokio_util::task::TaskTracker;
55

6-
use crate::config;
6+
use crate::config::{self, UiLevel};
77
use crate::display::{DisplayState, tplink_framebuffer, tplink_onebit};
88

99
use std::fs;
@@ -15,7 +15,7 @@ pub fn update_ui(
1515
ui_update_rx: Receiver<DisplayState>,
1616
) {
1717
let display_level = config.ui_level;
18-
if display_level == 0 {
18+
if display_level == UiLevel::Invisible {
1919
info!("Invisible mode, not spawning UI.");
2020
}
2121

daemon/src/display/tplink_onebit.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// Display module for the TP-Link M7350 oled one-bit display.
22
///
33
/// https://github.com/m0veax/tplink_m7350/tree/main/oled
4-
use crate::config;
4+
use crate::config::{self, UiLevel};
55
use crate::display::DisplayState;
66

77
use log::{error, info};
@@ -115,7 +115,7 @@ pub fn update_ui(
115115
mut ui_update_rx: Receiver<DisplayState>,
116116
) {
117117
let display_level = config.ui_level;
118-
if display_level == 0 {
118+
if display_level == UiLevel::Invisible {
119119
info!("Invisible mode, not spawning UI.");
120120
}
121121

@@ -140,7 +140,7 @@ pub fn update_ui(
140140

141141
// we write the status every second because it may have been overwritten through menu
142142
// navigation.
143-
if display_level != 0
143+
if display_level != UiLevel::Invisible
144144
&& let Err(e) = tokio::fs::write(OLED_PATH, pixels).await
145145
{
146146
error!("failed to write to display: {e}");

daemon/src/display/uz801.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use tokio_util::task::TaskTracker;
99

1010
use std::time::Duration;
1111

12-
use crate::config;
12+
use crate::config::{self, UiLevel};
1313
use crate::display::DisplayState;
1414

1515
macro_rules! led {
@@ -31,7 +31,7 @@ pub fn update_ui(
3131
mut ui_update_rx: mpsc::Receiver<DisplayState>,
3232
) {
3333
let mut invisible: bool = false;
34-
if config.ui_level == 0 {
34+
if config.ui_level == UiLevel::Invisible {
3535
info!("Invisible mode, not spawning UI.");
3636
invisible = true;
3737
}

daemon/src/gps.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use serde::{Deserialize, Deserializer, Serialize};
77
use std::sync::Arc;
88
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
99

10+
use crate::config::GpsMode;
1011
use crate::server::ServerState;
1112

1213
fn deserialize_unix_ts<'de, D>(deserializer: D) -> Result<i64, D::Error>
@@ -79,7 +80,7 @@ pub async fn post_gps(
7980
State(state): State<Arc<ServerState>>,
8081
Json(gps_data): Json<GpsData>,
8182
) -> Result<StatusCode, (StatusCode, String)> {
82-
if state.config.gps_mode != 2 {
83+
if state.config.gps_mode != GpsMode::Api {
8384
return Err((
8485
StatusCode::FORBIDDEN,
8586
"GPS API endpoint is disabled. Set gps_mode to 2 in configuration.".to_string(),

0 commit comments

Comments
 (0)