diff --git a/daemon/src/config.rs b/daemon/src/config.rs index 9bc4809..74cdca0 100644 --- a/daemon/src/config.rs +++ b/daemon/src/config.rs @@ -1,6 +1,7 @@ use log::warn; use serde::{Deserialize, Serialize}; +use rayhunter::Device; use rayhunter::analysis::analyzer::AnalyzerConfig; use crate::error::RayhunterError; @@ -18,15 +19,6 @@ pub struct Config { pub analyzers: AnalyzerConfig, } -#[derive(PartialEq, Debug, Clone, Deserialize, Serialize)] -#[serde(rename_all = "lowercase")] -pub enum Device { - Orbic, - Tplink, - Tmobile, - Wingtech, -} - impl Default for Config { fn default() -> Self { Config { diff --git a/daemon/src/main.rs b/daemon/src/main.rs index 991160f..2c48b2f 100644 --- a/daemon/src/main.rs +++ b/daemon/src/main.rs @@ -13,7 +13,7 @@ use std::net::SocketAddr; use std::sync::Arc; use std::sync::atomic::{AtomicBool, Ordering}; -use crate::config::{Device, parse_args, parse_config}; +use crate::config::{parse_args, parse_config}; use crate::diag::run_diag_read_thread; use crate::error::RayhunterError; use crate::pcap::get_pcap; @@ -33,6 +33,7 @@ use diag::{ }; use log::{error, info}; use qmdl_store::RecordingStoreError; +use rayhunter::Device; use rayhunter::diag_device::DiagDevice; use tokio::net::TcpListener; use tokio::select; @@ -216,7 +217,7 @@ async fn run_with_config( let (ui_shutdown_tx, ui_shutdown_rx) = oneshot::channel(); maybe_ui_shutdown_tx = Some(ui_shutdown_tx); info!("Using configuration for device: {0:?}", config.device); - let mut dev = DiagDevice::new(config.device == Device::Tplink) + let mut dev = DiagDevice::new(&config.device) .await .map_err(RayhunterError::DiagInitError)?; dev.config_logs() diff --git a/lib/src/diag_device.rs b/lib/src/diag_device.rs index bb60865..dd4c5cf 100644 --- a/lib/src/diag_device.rs +++ b/lib/src/diag_device.rs @@ -3,7 +3,7 @@ use crate::diag::{ MessagesContainer, Request, RequestContainer, ResponsePayload, build_log_mask_request, }; use crate::hdlc::hdlc_encapsulate; -use crate::log_codes; +use crate::{Device, log_codes}; use deku::prelude::*; use futures::TryStream; @@ -86,11 +86,14 @@ pub struct DiagDevice { } impl DiagDevice { - pub async fn new(tplink: bool) -> DiagResult { - Self::new_with_retries(Duration::from_secs(30), tplink).await + pub async fn new(configured_device: &Device) -> DiagResult { + Self::new_with_retries(Duration::from_secs(30), configured_device).await } - pub async fn new_with_retries(max_duration: Duration, tplink: bool) -> DiagResult { + pub async fn new_with_retries( + max_duration: Duration, + configured_device: &Device, + ) -> DiagResult { // For some reason the diag device needs a very long time to become available again with in // the same process, on TP-Link M7350 v3. While process restart would reset it faster. @@ -101,7 +104,7 @@ impl DiagDevice { let mut num_retries = 0; loop { - match Self::try_new(tplink).await { + match Self::try_new(configured_device).await { Ok(device) => { info!("Diag device initialization succeeded after {num_retries} retries"); return Ok(device); @@ -125,7 +128,7 @@ impl DiagDevice { } } - async fn try_new(tplink: bool) -> DiagResult { + async fn try_new(configured_device: &Device) -> DiagResult { let diag_file = File::options() .read(true) .write(true) @@ -134,7 +137,7 @@ impl DiagDevice { .map_err(DiagDeviceError::OpenDiagDeviceError)?; let fd = diag_file.as_raw_fd(); - enable_frame_readwrite(fd, MEMORY_DEVICE_MODE, tplink)?; + enable_frame_readwrite(fd, MEMORY_DEVICE_MODE, configured_device)?; let use_mdm = determine_use_mdm(fd)?; Ok(DiagDevice { @@ -300,34 +303,29 @@ struct DiagLoggingModeParam { } // Triggers the diag device's debug logging mode -fn enable_frame_readwrite(fd: i32, mode: u32, tplink: bool) -> DiagResult<()> { +fn enable_frame_readwrite(fd: i32, mode: u32, configured_device: &Device) -> DiagResult<()> { unsafe { if libc::ioctl(fd, DIAG_IOCTL_SWITCH_LOGGING, mode, 0, 0, 0) < 0 { - let try_params: &[DiagLoggingModeParam] = match tplink { - true => &[ - // tplink M7350 HW revision 3-8 need this mode + let mut try_params = vec![DiagLoggingModeParam { + req_mode: mode, + peripheral_mask: u32::MAX, + mode_param: 0, + }]; + if configured_device == &Device::Tplink { + // tplink M7350 HW revision 3-8 need this mode + try_params.insert( + 0, DiagLoggingModeParam { req_mode: mode, peripheral_mask: 0, mode_param: 1, }, - // tplink M7350 HW revision v9 requires the same parameters as orbic - DiagLoggingModeParam { - req_mode: mode, - peripheral_mask: u32::MAX, - mode_param: 0, - }, - ], - false => &[DiagLoggingModeParam { - req_mode: mode, - peripheral_mask: u32::MAX, - mode_param: 0, - }], - }; + ); + } let mut ret = 0; - for params in try_params { + for params in &try_params { let mut params = *params; ret = libc::ioctl( fd, diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 7e47f59..e231b31 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -1,3 +1,5 @@ +use serde::{Deserialize, Serialize}; + pub mod analysis; pub mod diag; pub mod gsmtap; @@ -14,3 +16,12 @@ pub mod diag_device; // re-export telcom_parser, since we use its types in our API pub use telcom_parser; + +#[derive(PartialEq, Debug, Clone, Deserialize, Serialize)] +#[serde(rename_all = "lowercase")] +pub enum Device { + Orbic, + Tplink, + Tmobile, + Wingtech, +}